michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkParsePaint.h" michael@0: #include "SkTSearch.h" michael@0: #include "SkParse.h" michael@0: #include "SkImageDecoder.h" michael@0: #include "SkGradientShader.h" michael@0: michael@0: static SkShader* inflate_shader(const SkDOM& dom, const SkDOM::Node* node) michael@0: { michael@0: if ((node = dom.getFirstChild(node, "shader")) == NULL) michael@0: return NULL; michael@0: michael@0: const char* str; michael@0: michael@0: if (dom.hasAttr(node, "type", "linear-gradient")) michael@0: { michael@0: SkColor colors[2]; michael@0: SkPoint pts[2]; michael@0: michael@0: colors[0] = colors[1] = SK_ColorBLACK; // need to initialized the alpha to opaque, since FindColor doesn't set it michael@0: if ((str = dom.findAttr(node, "c0")) != NULL && michael@0: SkParse::FindColor(str, &colors[0]) && michael@0: (str = dom.findAttr(node, "c1")) != NULL && michael@0: SkParse::FindColor(str, &colors[1]) && michael@0: dom.findScalars(node, "p0", &pts[0].fX, 2) && michael@0: dom.findScalars(node, "p1", &pts[1].fX, 2)) michael@0: { michael@0: SkShader::TileMode mode = SkShader::kClamp_TileMode; michael@0: int index; michael@0: michael@0: if ((index = dom.findList(node, "tile-mode", "clamp,repeat,mirror")) >= 0) michael@0: mode = (SkShader::TileMode)index; michael@0: return SkGradientShader::CreateLinear(pts, colors, NULL, 2, mode); michael@0: } michael@0: } michael@0: else if (dom.hasAttr(node, "type", "bitmap")) michael@0: { michael@0: if ((str = dom.findAttr(node, "src")) == NULL) michael@0: return NULL; michael@0: michael@0: SkBitmap bm; michael@0: michael@0: if (SkImageDecoder::DecodeFile(str, &bm)) michael@0: { michael@0: SkShader::TileMode mode = SkShader::kRepeat_TileMode; michael@0: int index; michael@0: michael@0: if ((index = dom.findList(node, "tile-mode", "clamp,repeat,mirror")) >= 0) michael@0: mode = (SkShader::TileMode)index; michael@0: michael@0: return SkShader::CreateBitmapShader(bm, mode, mode); michael@0: } michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: void SkPaint_Inflate(SkPaint* paint, const SkDOM& dom, const SkDOM::Node* node) michael@0: { michael@0: SkASSERT(paint); michael@0: SkASSERT(&dom); michael@0: SkASSERT(node); michael@0: michael@0: SkScalar x; michael@0: michael@0: if (dom.findScalar(node, "stroke-width", &x)) michael@0: paint->setStrokeWidth(x); michael@0: if (dom.findScalar(node, "text-size", &x)) michael@0: paint->setTextSize(x); michael@0: michael@0: bool b; michael@0: michael@0: SkASSERT("legacy: use is-stroke" && !dom.findBool(node, "is-frame", &b)); michael@0: michael@0: if (dom.findBool(node, "is-stroke", &b)) michael@0: paint->setStyle(b ? SkPaint::kStroke_Style : SkPaint::kFill_Style); michael@0: if (dom.findBool(node, "is-antialias", &b)) michael@0: paint->setAntiAlias(b); michael@0: if (dom.findBool(node, "is-lineartext", &b)) michael@0: paint->setLinearText(b); michael@0: michael@0: const char* str = dom.findAttr(node, "color"); michael@0: if (str) michael@0: { michael@0: SkColor c = paint->getColor(); michael@0: if (SkParse::FindColor(str, &c)) michael@0: paint->setColor(c); michael@0: } michael@0: michael@0: // do this AFTER parsing for the color michael@0: if (dom.findScalar(node, "opacity", &x)) michael@0: { michael@0: x = SkMaxScalar(0, SkMinScalar(x, SK_Scalar1)); michael@0: paint->setAlpha(SkScalarRoundToInt(x * 255)); michael@0: } michael@0: michael@0: int index = dom.findList(node, "text-anchor", "left,center,right"); michael@0: if (index >= 0) michael@0: paint->setTextAlign((SkPaint::Align)index); michael@0: michael@0: SkShader* shader = inflate_shader(dom, node); michael@0: if (shader) michael@0: paint->setShader(shader)->unref(); michael@0: }