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 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +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 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 19 | const char* str; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 21 | if (dom.hasAttr(node, "type", "linear-gradient")) |
| 22 | { |
| 23 | SkColor colors[2]; |
| 24 | SkPoint pts[2]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 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; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 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; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 47 | SkBitmap bm; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 49 | if (SkImageDecoder::DecodeFile(str, &bm)) |
| 50 | { |
| 51 | SkShader::TileMode mode = SkShader::kRepeat_TileMode; |
| 52 | int index; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 54 | if ((index = dom.findList(node, "tile-mode", "clamp,repeat,mirror")) >= 0) |
| 55 | mode = (SkShader::TileMode)index; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 57 | return SkShader::CreateBitmapShader(bm, mode, mode); |
| 58 | } |
| 59 | } |
| 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 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 65 | SkASSERT(paint); |
| 66 | SkASSERT(&dom); |
| 67 | SkASSERT(node); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 69 | SkScalar x; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 71 | if (dom.findScalar(node, "stroke-width", &x)) |
| 72 | paint->setStrokeWidth(x); |
| 73 | if (dom.findScalar(node, "text-size", &x)) |
| 74 | paint->setTextSize(x); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 76 | bool b; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 78 | SkASSERT("legacy: use is-stroke" && !dom.findBool(node, "is-frame", &b)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 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); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 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 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 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 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 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(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | } |