Home | History | Annotate | Download | only in views
      1 #include "SkParsePaint.h"
      2 #include "SkTSearch.h"
      3 #include "SkParse.h"
      4 #include "SkImageDecoder.h"
      5 #include "SkGradientShader.h"
      6 
      7 static SkShader* inflate_shader(const SkDOM& dom, const SkDOM::Node* node)
      8 {
      9 	if ((node = dom.getFirstChild(node, "shader")) == NULL)
     10 		return NULL;
     11 
     12 	const char* str;
     13 
     14 	if (dom.hasAttr(node, "type", "linear-gradient"))
     15 	{
     16 		SkColor		colors[2];
     17 		SkPoint		pts[2];
     18 
     19 		colors[0] = colors[1] = SK_ColorBLACK;	// need to initialized the alpha to opaque, since FindColor doesn't set it
     20 		if ((str = dom.findAttr(node, "c0")) != NULL &&
     21 			SkParse::FindColor(str, &colors[0]) &&
     22 			(str = dom.findAttr(node, "c1")) != NULL &&
     23 			SkParse::FindColor(str, &colors[1]) &&
     24 			dom.findScalars(node, "p0", &pts[0].fX, 2) &&
     25 			dom.findScalars(node, "p1", &pts[1].fX, 2))
     26 		{
     27 			SkShader::TileMode	mode = SkShader::kClamp_TileMode;
     28 			int					index;
     29 
     30 			if ((index = dom.findList(node, "tile-mode", "clamp,repeat,mirror")) >= 0)
     31 				mode = (SkShader::TileMode)index;
     32 			return SkGradientShader::CreateLinear(pts, colors, NULL, 2, mode);
     33 		}
     34 	}
     35 	else if (dom.hasAttr(node, "type", "bitmap"))
     36 	{
     37 		if ((str = dom.findAttr(node, "src")) == NULL)
     38 			return NULL;
     39 
     40 		SkBitmap	bm;
     41 
     42 		if (SkImageDecoder::DecodeFile(str, &bm))
     43 		{
     44 			SkShader::TileMode	mode = SkShader::kRepeat_TileMode;
     45 			int					index;
     46 
     47 			if ((index = dom.findList(node, "tile-mode", "clamp,repeat,mirror")) >= 0)
     48 				mode = (SkShader::TileMode)index;
     49 
     50 			return SkShader::CreateBitmapShader(bm, mode, mode);
     51 		}
     52 	}
     53 	return NULL;
     54 }
     55 
     56 void SkPaint_Inflate(SkPaint* paint, const SkDOM& dom, const SkDOM::Node* node)
     57 {
     58 	SkASSERT(paint);
     59 	SkASSERT(&dom);
     60 	SkASSERT(node);
     61 
     62 	SkScalar x;
     63 
     64 	if (dom.findScalar(node, "stroke-width", &x))
     65 		paint->setStrokeWidth(x);
     66 	if (dom.findScalar(node, "text-size", &x))
     67 		paint->setTextSize(x);
     68 
     69 	bool	b;
     70 
     71 	SkASSERT("legacy: use is-stroke" && !dom.findBool(node, "is-frame", &b));
     72 
     73 	if (dom.findBool(node, "is-stroke", &b))
     74 		paint->setStyle(b ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
     75 	if (dom.findBool(node, "is-antialias", &b))
     76 		paint->setAntiAlias(b);
     77 	if (dom.findBool(node, "is-lineartext", &b))
     78 		paint->setLinearText(b);
     79 
     80 	const char* str = dom.findAttr(node, "color");
     81 	if (str)
     82 	{
     83 		SkColor	c = paint->getColor();
     84 		if (SkParse::FindColor(str, &c))
     85 			paint->setColor(c);
     86 	}
     87 
     88 	// do this AFTER parsing for the color
     89 	if (dom.findScalar(node, "opacity", &x))
     90 	{
     91 		x = SkMaxScalar(0, SkMinScalar(x, SK_Scalar1));
     92 		paint->setAlpha(SkScalarRound(x * 255));
     93 	}
     94 
     95 	int	index = dom.findList(node, "text-anchor", "left,center,right");
     96 	if (index >= 0)
     97 		paint->setTextAlign((SkPaint::Align)index);
     98 
     99 	SkShader* shader = inflate_shader(dom, node);
    100 	if (shader)
    101 		paint->setShader(shader)->unref();
    102 }
    103 
    104