aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/views/SkParsePaint.cpp
blob: 5b4476423e885bca27197e53872047f686ef7f92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SkParsePaint.h"
#include "SkTSearch.h"
#include "SkParse.h"
#include "SkImageDecoder.h"
#include "SkGradientShader.h"

static SkShader* inflate_shader(const SkDOM& dom, const SkDOM::Node* node)
{
    if ((node = dom.getFirstChild(node, "shader")) == NULL)
        return NULL;

    const char* str;

    if (dom.hasAttr(node, "type", "linear-gradient"))
    {
        SkColor        colors[2];
        SkPoint        pts[2];

        colors[0] = colors[1] = SK_ColorBLACK;    // need to initialized the alpha to opaque, since FindColor doesn't set it
        if ((str = dom.findAttr(node, "c0")) != NULL &&
            SkParse::FindColor(str, &colors[0]) &&
            (str = dom.findAttr(node, "c1")) != NULL &&
            SkParse::FindColor(str, &colors[1]) &&
            dom.findScalars(node, "p0", &pts[0].fX, 2) &&
            dom.findScalars(node, "p1", &pts[1].fX, 2))
        {
            SkShader::TileMode    mode = SkShader::kClamp_TileMode;
            int                    index;

            if ((index = dom.findList(node, "tile-mode", "clamp,repeat,mirror")) >= 0)
                mode = (SkShader::TileMode)index;
            return SkGradientShader::CreateLinear(pts, colors, NULL, 2, mode);
        }
    }
    else if (dom.hasAttr(node, "type", "bitmap"))
    {
        if ((str = dom.findAttr(node, "src")) == NULL)
            return NULL;

        SkBitmap    bm;

        if (SkImageDecoder::DecodeFile(str, &bm))
        {
            SkShader::TileMode    mode = SkShader::kRepeat_TileMode;
            int                    index;

            if ((index = dom.findList(node, "tile-mode", "clamp,repeat,mirror")) >= 0)
                mode = (SkShader::TileMode)index;

            return SkShader::CreateBitmapShader(bm, mode, mode);
        }
    }
    return NULL;
}

void SkPaint_Inflate(SkPaint* paint, const SkDOM& dom, const SkDOM::Node* node)
{
    SkASSERT(paint);
    SkASSERT(&dom);
    SkASSERT(node);

    SkScalar x;

    if (dom.findScalar(node, "stroke-width", &x))
        paint->setStrokeWidth(x);
    if (dom.findScalar(node, "text-size", &x))
        paint->setTextSize(x);

    bool    b;

    SkASSERT("legacy: use is-stroke" && !dom.findBool(node, "is-frame", &b));

    if (dom.findBool(node, "is-stroke", &b))
        paint->setStyle(b ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
    if (dom.findBool(node, "is-antialias", &b))
        paint->setAntiAlias(b);
    if (dom.findBool(node, "is-lineartext", &b))
        paint->setLinearText(b);

    const char* str = dom.findAttr(node, "color");
    if (str)
    {
        SkColor    c = paint->getColor();
        if (SkParse::FindColor(str, &c))
            paint->setColor(c);
    }

    // do this AFTER parsing for the color
    if (dom.findScalar(node, "opacity", &x))
    {
        x = SkMaxScalar(0, SkMinScalar(x, SK_Scalar1));
        paint->setAlpha(SkScalarRoundToInt(x * 255));
    }

    int    index = dom.findList(node, "text-anchor", "left,center,right");
    if (index >= 0)
        paint->setTextAlign((SkPaint::Align)index);

    SkShader* shader = inflate_shader(dom, node);
    if (shader)
        paint->setShader(shader)->unref();
}