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