epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | { |
deanm@chromium.org | 1599a43 | 2009-06-04 15:37:11 +0000 | [diff] [blame] | 16 | if ((node = dom.getFirstChild(node, "shader")) == NULL) |
| 17 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 |
deanm@chromium.org | 1599a43 | 2009-06-04 15:37:11 +0000 | [diff] [blame] | 27 | if ((str = dom.findAttr(node, "c0")) != NULL && |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | SkParse::FindColor(str, &colors[0]) && |
deanm@chromium.org | 1599a43 | 2009-06-04 15:37:11 +0000 | [diff] [blame] | 29 | (str = dom.findAttr(node, "c1")) != NULL && |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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; |
deanm@chromium.org | 1599a43 | 2009-06-04 15:37:11 +0000 | [diff] [blame] | 39 | return SkGradientShader::CreateLinear(pts, colors, NULL, 2, mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | else if (dom.hasAttr(node, "type", "bitmap")) |
| 43 | { |
deanm@chromium.org | 1599a43 | 2009-06-04 15:37:11 +0000 | [diff] [blame] | 44 | if ((str = dom.findAttr(node, "src")) == NULL) |
| 45 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | } |
deanm@chromium.org | 1599a43 | 2009-06-04 15:37:11 +0000 | [diff] [blame] | 60 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |