gfx/skia/trunk/src/views/SkParsePaint.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/views/SkParsePaint.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,109 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2011 Google Inc.
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +#include "SkParsePaint.h"
    1.12 +#include "SkTSearch.h"
    1.13 +#include "SkParse.h"
    1.14 +#include "SkImageDecoder.h"
    1.15 +#include "SkGradientShader.h"
    1.16 +
    1.17 +static SkShader* inflate_shader(const SkDOM& dom, const SkDOM::Node* node)
    1.18 +{
    1.19 +    if ((node = dom.getFirstChild(node, "shader")) == NULL)
    1.20 +        return NULL;
    1.21 +
    1.22 +    const char* str;
    1.23 +
    1.24 +    if (dom.hasAttr(node, "type", "linear-gradient"))
    1.25 +    {
    1.26 +        SkColor        colors[2];
    1.27 +        SkPoint        pts[2];
    1.28 +
    1.29 +        colors[0] = colors[1] = SK_ColorBLACK;    // need to initialized the alpha to opaque, since FindColor doesn't set it
    1.30 +        if ((str = dom.findAttr(node, "c0")) != NULL &&
    1.31 +            SkParse::FindColor(str, &colors[0]) &&
    1.32 +            (str = dom.findAttr(node, "c1")) != NULL &&
    1.33 +            SkParse::FindColor(str, &colors[1]) &&
    1.34 +            dom.findScalars(node, "p0", &pts[0].fX, 2) &&
    1.35 +            dom.findScalars(node, "p1", &pts[1].fX, 2))
    1.36 +        {
    1.37 +            SkShader::TileMode    mode = SkShader::kClamp_TileMode;
    1.38 +            int                    index;
    1.39 +
    1.40 +            if ((index = dom.findList(node, "tile-mode", "clamp,repeat,mirror")) >= 0)
    1.41 +                mode = (SkShader::TileMode)index;
    1.42 +            return SkGradientShader::CreateLinear(pts, colors, NULL, 2, mode);
    1.43 +        }
    1.44 +    }
    1.45 +    else if (dom.hasAttr(node, "type", "bitmap"))
    1.46 +    {
    1.47 +        if ((str = dom.findAttr(node, "src")) == NULL)
    1.48 +            return NULL;
    1.49 +
    1.50 +        SkBitmap    bm;
    1.51 +
    1.52 +        if (SkImageDecoder::DecodeFile(str, &bm))
    1.53 +        {
    1.54 +            SkShader::TileMode    mode = SkShader::kRepeat_TileMode;
    1.55 +            int                    index;
    1.56 +
    1.57 +            if ((index = dom.findList(node, "tile-mode", "clamp,repeat,mirror")) >= 0)
    1.58 +                mode = (SkShader::TileMode)index;
    1.59 +
    1.60 +            return SkShader::CreateBitmapShader(bm, mode, mode);
    1.61 +        }
    1.62 +    }
    1.63 +    return NULL;
    1.64 +}
    1.65 +
    1.66 +void SkPaint_Inflate(SkPaint* paint, const SkDOM& dom, const SkDOM::Node* node)
    1.67 +{
    1.68 +    SkASSERT(paint);
    1.69 +    SkASSERT(&dom);
    1.70 +    SkASSERT(node);
    1.71 +
    1.72 +    SkScalar x;
    1.73 +
    1.74 +    if (dom.findScalar(node, "stroke-width", &x))
    1.75 +        paint->setStrokeWidth(x);
    1.76 +    if (dom.findScalar(node, "text-size", &x))
    1.77 +        paint->setTextSize(x);
    1.78 +
    1.79 +    bool    b;
    1.80 +
    1.81 +    SkASSERT("legacy: use is-stroke" && !dom.findBool(node, "is-frame", &b));
    1.82 +
    1.83 +    if (dom.findBool(node, "is-stroke", &b))
    1.84 +        paint->setStyle(b ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
    1.85 +    if (dom.findBool(node, "is-antialias", &b))
    1.86 +        paint->setAntiAlias(b);
    1.87 +    if (dom.findBool(node, "is-lineartext", &b))
    1.88 +        paint->setLinearText(b);
    1.89 +
    1.90 +    const char* str = dom.findAttr(node, "color");
    1.91 +    if (str)
    1.92 +    {
    1.93 +        SkColor    c = paint->getColor();
    1.94 +        if (SkParse::FindColor(str, &c))
    1.95 +            paint->setColor(c);
    1.96 +    }
    1.97 +
    1.98 +    // do this AFTER parsing for the color
    1.99 +    if (dom.findScalar(node, "opacity", &x))
   1.100 +    {
   1.101 +        x = SkMaxScalar(0, SkMinScalar(x, SK_Scalar1));
   1.102 +        paint->setAlpha(SkScalarRoundToInt(x * 255));
   1.103 +    }
   1.104 +
   1.105 +    int    index = dom.findList(node, "text-anchor", "left,center,right");
   1.106 +    if (index >= 0)
   1.107 +        paint->setTextAlign((SkPaint::Align)index);
   1.108 +
   1.109 +    SkShader* shader = inflate_shader(dom, node);
   1.110 +    if (shader)
   1.111 +        paint->setShader(shader)->unref();
   1.112 +}

mercurial