Home | History | Annotate | Download | only in style
      1 /*
      2     Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann (at) kde.org>
      3                   2004, 2005 Rob Buis <buis (at) kde.org>
      4     Copyright (C) 2005, 2006 Apple Computer, Inc.
      5     Copyright (C) Research In Motion Limited 2010. All rights reserved.
      6 
      7     This library is free software; you can redistribute it and/or
      8     modify it under the terms of the GNU Library General Public
      9     License as published by the Free Software Foundation; either
     10     version 2 of the License, or (at your option) any later version.
     11 
     12     This library is distributed in the hope that it will be useful,
     13     but WITHOUT ANY WARRANTY; without even the implied warranty of
     14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15     Library General Public License for more details.
     16 
     17     You should have received a copy of the GNU Library General Public License
     18     along with this library; see the file COPYING.LIB.  If not, write to
     19     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     20     Boston, MA 02110-1301, USA.
     21 */
     22 
     23 #ifndef SVGRenderStyle_h
     24 #define SVGRenderStyle_h
     25 
     26 #if ENABLE(SVG)
     27 #include "CSSValueList.h"
     28 #include "DataRef.h"
     29 #include "GraphicsTypes.h"
     30 #include "Path.h"
     31 #include "RenderStyleConstants.h"
     32 #include "SVGPaint.h"
     33 #include "SVGRenderStyleDefs.h"
     34 
     35 namespace WebCore {
     36 
     37 class FloatRect;
     38 class IntRect;
     39 class RenderObject;
     40 
     41 class SVGRenderStyle : public RefCounted<SVGRenderStyle> {
     42 public:
     43     static PassRefPtr<SVGRenderStyle> create() { return adoptRef(new SVGRenderStyle); }
     44     PassRefPtr<SVGRenderStyle> copy() const { return adoptRef(new SVGRenderStyle(*this));}
     45     ~SVGRenderStyle();
     46 
     47     bool inheritedNotEqual(const SVGRenderStyle*) const;
     48     void inheritFrom(const SVGRenderStyle*);
     49 
     50     StyleDifference diff(const SVGRenderStyle*) const;
     51 
     52     bool operator==(const SVGRenderStyle&) const;
     53     bool operator!=(const SVGRenderStyle& o) const { return !(*this == o); }
     54 
     55     // Initial values for all the properties
     56     static EAlignmentBaseline initialAlignmentBaseline() { return AB_AUTO; }
     57     static EDominantBaseline initialDominantBaseline() { return DB_AUTO; }
     58     static EBaselineShift initialBaselineShift() { return BS_BASELINE; }
     59     static EVectorEffect initialVectorEffect() { return VE_NONE; }
     60     static LineCap initialCapStyle() { return ButtCap; }
     61     static WindRule initialClipRule() { return RULE_NONZERO; }
     62     static EColorInterpolation initialColorInterpolation() { return CI_SRGB; }
     63     static EColorInterpolation initialColorInterpolationFilters() { return CI_LINEARRGB; }
     64     static EColorRendering initialColorRendering() { return CR_AUTO; }
     65     static WindRule initialFillRule() { return RULE_NONZERO; }
     66     static EImageRendering initialImageRendering() { return IR_AUTO; }
     67     static LineJoin initialJoinStyle() { return MiterJoin; }
     68     static EShapeRendering initialShapeRendering() { return SR_AUTO; }
     69     static ETextAnchor initialTextAnchor() { return TA_START; }
     70     static SVGWritingMode initialWritingMode() { return WM_LRTB; }
     71     static EGlyphOrientation initialGlyphOrientationHorizontal() { return GO_0DEG; }
     72     static EGlyphOrientation initialGlyphOrientationVertical() { return GO_AUTO; }
     73     static float initialFillOpacity() { return 1.0f; }
     74     static SVGPaint* initialFillPaint() { return SVGPaint::defaultFill(); }
     75     static float initialStrokeOpacity() { return 1.0f; }
     76     static SVGPaint* initialStrokePaint() { return SVGPaint::defaultStroke(); }
     77     static Vector<SVGLength> initialStrokeDashArray() { return Vector<SVGLength>(); }
     78     static float initialStrokeMiterLimit() { return 4.0f; }
     79     static float initialStopOpacity() { return 1.0f; }
     80     static Color initialStopColor() { return Color(0, 0, 0); }
     81     static float initialFloodOpacity() { return 1.0f; }
     82     static Color initialFloodColor() { return Color(0, 0, 0); }
     83     static Color initialLightingColor() { return Color(255, 255, 255); }
     84     static ShadowData* initialShadow() { return 0; }
     85     static String initialClipperResource() { return String(); }
     86     static String initialFilterResource() { return String(); }
     87     static String initialMaskerResource() { return String(); }
     88     static String initialMarkerStartResource() { return String(); }
     89     static String initialMarkerMidResource() { return String(); }
     90     static String initialMarkerEndResource() { return String(); }
     91 
     92     static SVGLength initialBaselineShiftValue()
     93     {
     94         SVGLength length;
     95         ExceptionCode ec = 0;
     96         length.newValueSpecifiedUnits(LengthTypeNumber, 0, ec);
     97         ASSERT(!ec);
     98         return length;
     99     }
    100 
    101     static SVGLength initialKerning()
    102     {
    103         SVGLength length;
    104         ExceptionCode ec = 0;
    105         length.newValueSpecifiedUnits(LengthTypeNumber, 0, ec);
    106         ASSERT(!ec);
    107         return length;
    108     }
    109 
    110     static SVGLength initialStrokeDashOffset()
    111     {
    112         SVGLength length;
    113         ExceptionCode ec = 0;
    114         length.newValueSpecifiedUnits(LengthTypeNumber, 0, ec);
    115         ASSERT(!ec);
    116         return length;
    117     }
    118 
    119     static SVGLength initialStrokeWidth()
    120     {
    121         SVGLength length;
    122         ExceptionCode ec = 0;
    123         length.newValueSpecifiedUnits(LengthTypeNumber, 1, ec);
    124         ASSERT(!ec);
    125         return length;
    126     }
    127 
    128     // SVG CSS Property setters
    129     void setAlignmentBaseline(EAlignmentBaseline val) { svg_noninherited_flags.f._alignmentBaseline = val; }
    130     void setDominantBaseline(EDominantBaseline val) { svg_noninherited_flags.f._dominantBaseline = val; }
    131     void setBaselineShift(EBaselineShift val) { svg_noninherited_flags.f._baselineShift = val; }
    132     void setVectorEffect(EVectorEffect val) { svg_noninherited_flags.f._vectorEffect = val; }
    133     void setCapStyle(LineCap val) { svg_inherited_flags._capStyle = val; }
    134     void setClipRule(WindRule val) { svg_inherited_flags._clipRule = val; }
    135     void setColorInterpolation(EColorInterpolation val) { svg_inherited_flags._colorInterpolation = val; }
    136     void setColorInterpolationFilters(EColorInterpolation val) { svg_inherited_flags._colorInterpolationFilters = val; }
    137     void setColorRendering(EColorRendering val) { svg_inherited_flags._colorRendering = val; }
    138     void setFillRule(WindRule val) { svg_inherited_flags._fillRule = val; }
    139     void setImageRendering(EImageRendering val) { svg_inherited_flags._imageRendering = val; }
    140     void setJoinStyle(LineJoin val) { svg_inherited_flags._joinStyle = val; }
    141     void setShapeRendering(EShapeRendering val) { svg_inherited_flags._shapeRendering = val; }
    142     void setTextAnchor(ETextAnchor val) { svg_inherited_flags._textAnchor = val; }
    143     void setWritingMode(SVGWritingMode val) { svg_inherited_flags._writingMode = val; }
    144     void setGlyphOrientationHorizontal(EGlyphOrientation val) { svg_inherited_flags._glyphOrientationHorizontal = val; }
    145     void setGlyphOrientationVertical(EGlyphOrientation val) { svg_inherited_flags._glyphOrientationVertical = val; }
    146 
    147     void setFillOpacity(float obj)
    148     {
    149         if (!(fill->opacity == obj))
    150             fill.access()->opacity = obj;
    151     }
    152 
    153     void setFillPaint(PassRefPtr<SVGPaint> obj)
    154     {
    155         if (!(fill->paint == obj))
    156             fill.access()->paint = obj;
    157     }
    158 
    159     void setStrokeOpacity(float obj)
    160     {
    161         if (!(stroke->opacity == obj))
    162             stroke.access()->opacity = obj;
    163     }
    164 
    165     void setStrokePaint(PassRefPtr<SVGPaint> obj)
    166     {
    167         if (!(stroke->paint == obj))
    168             stroke.access()->paint = obj;
    169     }
    170 
    171     void setStrokeDashArray(const Vector<SVGLength>& obj)
    172     {
    173         if (!(stroke->dashArray == obj))
    174             stroke.access()->dashArray = obj;
    175     }
    176 
    177     void setStrokeMiterLimit(float obj)
    178     {
    179         if (!(stroke->miterLimit == obj))
    180             stroke.access()->miterLimit = obj;
    181     }
    182 
    183     void setStrokeWidth(const SVGLength& obj)
    184     {
    185         if (!(stroke->width == obj))
    186             stroke.access()->width = obj;
    187     }
    188 
    189     void setStrokeDashOffset(const SVGLength& obj)
    190     {
    191         if (!(stroke->dashOffset == obj))
    192             stroke.access()->dashOffset = obj;
    193     }
    194 
    195     void setKerning(const SVGLength& obj)
    196     {
    197         if (!(text->kerning == obj))
    198             text.access()->kerning = obj;
    199     }
    200 
    201     void setStopOpacity(float obj)
    202     {
    203         if (!(stops->opacity == obj))
    204             stops.access()->opacity = obj;
    205     }
    206 
    207     void setStopColor(const Color& obj)
    208     {
    209         if (!(stops->color == obj))
    210             stops.access()->color = obj;
    211     }
    212 
    213     void setFloodOpacity(float obj)
    214     {
    215         if (!(misc->floodOpacity == obj))
    216             misc.access()->floodOpacity = obj;
    217     }
    218 
    219     void setFloodColor(const Color& obj)
    220     {
    221         if (!(misc->floodColor == obj))
    222             misc.access()->floodColor = obj;
    223     }
    224 
    225     void setLightingColor(const Color& obj)
    226     {
    227         if (!(misc->lightingColor == obj))
    228             misc.access()->lightingColor = obj;
    229     }
    230 
    231     void setBaselineShiftValue(const SVGLength& obj)
    232     {
    233         if (!(misc->baselineShiftValue == obj))
    234             misc.access()->baselineShiftValue = obj;
    235     }
    236 
    237     void setShadow(PassOwnPtr<ShadowData> obj) { shadowSVG.access()->shadow = obj; }
    238 
    239     // Setters for non-inherited resources
    240     void setClipperResource(const String& obj)
    241     {
    242         if (!(resources->clipper == obj))
    243             resources.access()->clipper = obj;
    244     }
    245 
    246     void setFilterResource(const String& obj)
    247     {
    248         if (!(resources->filter == obj))
    249             resources.access()->filter = obj;
    250     }
    251 
    252     void setMaskerResource(const String& obj)
    253     {
    254         if (!(resources->masker == obj))
    255             resources.access()->masker = obj;
    256     }
    257 
    258     // Setters for inherited resources
    259     void setMarkerStartResource(const String& obj)
    260     {
    261         if (!(inheritedResources->markerStart == obj))
    262             inheritedResources.access()->markerStart = obj;
    263     }
    264 
    265     void setMarkerMidResource(const String& obj)
    266     {
    267         if (!(inheritedResources->markerMid == obj))
    268             inheritedResources.access()->markerMid = obj;
    269     }
    270 
    271     void setMarkerEndResource(const String& obj)
    272     {
    273         if (!(inheritedResources->markerEnd == obj))
    274             inheritedResources.access()->markerEnd = obj;
    275     }
    276 
    277     // Read accessors for all the properties
    278     EAlignmentBaseline alignmentBaseline() const { return (EAlignmentBaseline) svg_noninherited_flags.f._alignmentBaseline; }
    279     EDominantBaseline dominantBaseline() const { return (EDominantBaseline) svg_noninherited_flags.f._dominantBaseline; }
    280     EBaselineShift baselineShift() const { return (EBaselineShift) svg_noninherited_flags.f._baselineShift; }
    281     EVectorEffect vectorEffect() const { return (EVectorEffect) svg_noninherited_flags.f._vectorEffect; }
    282     LineCap capStyle() const { return (LineCap) svg_inherited_flags._capStyle; }
    283     WindRule clipRule() const { return (WindRule) svg_inherited_flags._clipRule; }
    284     EColorInterpolation colorInterpolation() const { return (EColorInterpolation) svg_inherited_flags._colorInterpolation; }
    285     EColorInterpolation colorInterpolationFilters() const { return (EColorInterpolation) svg_inherited_flags._colorInterpolationFilters; }
    286     EColorRendering colorRendering() const { return (EColorRendering) svg_inherited_flags._colorRendering; }
    287     WindRule fillRule() const { return (WindRule) svg_inherited_flags._fillRule; }
    288     EImageRendering imageRendering() const { return (EImageRendering) svg_inherited_flags._imageRendering; }
    289     LineJoin joinStyle() const { return (LineJoin) svg_inherited_flags._joinStyle; }
    290     EShapeRendering shapeRendering() const { return (EShapeRendering) svg_inherited_flags._shapeRendering; }
    291     ETextAnchor textAnchor() const { return (ETextAnchor) svg_inherited_flags._textAnchor; }
    292     SVGWritingMode writingMode() const { return (SVGWritingMode) svg_inherited_flags._writingMode; }
    293     EGlyphOrientation glyphOrientationHorizontal() const { return (EGlyphOrientation) svg_inherited_flags._glyphOrientationHorizontal; }
    294     EGlyphOrientation glyphOrientationVertical() const { return (EGlyphOrientation) svg_inherited_flags._glyphOrientationVertical; }
    295     float fillOpacity() const { return fill->opacity; }
    296     SVGPaint* fillPaint() const { return fill->paint.get(); }
    297     float strokeOpacity() const { return stroke->opacity; }
    298     SVGPaint* strokePaint() const { return stroke->paint.get(); }
    299     Vector<SVGLength> strokeDashArray() const { return stroke->dashArray; }
    300     float strokeMiterLimit() const { return stroke->miterLimit; }
    301     SVGLength strokeWidth() const { return stroke->width; }
    302     SVGLength strokeDashOffset() const { return stroke->dashOffset; }
    303     SVGLength kerning() const { return text->kerning; }
    304     float stopOpacity() const { return stops->opacity; }
    305     Color stopColor() const { return stops->color; }
    306     float floodOpacity() const { return misc->floodOpacity; }
    307     Color floodColor() const { return misc->floodColor; }
    308     Color lightingColor() const { return misc->lightingColor; }
    309     SVGLength baselineShiftValue() const { return misc->baselineShiftValue; }
    310     ShadowData* shadow() const { return shadowSVG->shadow.get(); }
    311     String clipperResource() const { return resources->clipper; }
    312     String filterResource() const { return resources->filter; }
    313     String maskerResource() const { return resources->masker; }
    314     String markerStartResource() const { return inheritedResources->markerStart; }
    315     String markerMidResource() const { return inheritedResources->markerMid; }
    316     String markerEndResource() const { return inheritedResources->markerEnd; }
    317 
    318     // convenience
    319     bool hasClipper() const { return !clipperResource().isEmpty(); }
    320     bool hasMasker() const { return !maskerResource().isEmpty(); }
    321     bool hasFilter() const { return !filterResource().isEmpty(); }
    322     bool hasMarkers() const { return !markerStartResource().isEmpty() || !markerMidResource().isEmpty() || !markerEndResource().isEmpty(); }
    323     bool hasStroke() const { return strokePaint()->paintType() != SVGPaint::SVG_PAINTTYPE_NONE; }
    324     bool hasFill() const { return fillPaint()->paintType() != SVGPaint::SVG_PAINTTYPE_NONE; }
    325     bool isVerticalWritingMode() const { return writingMode() == WM_TBRL || writingMode() == WM_TB; }
    326 
    327 protected:
    328     // inherit
    329     struct InheritedFlags {
    330         bool operator==(const InheritedFlags& other) const
    331         {
    332             return (_colorRendering == other._colorRendering)
    333                 && (_imageRendering == other._imageRendering)
    334                 && (_shapeRendering == other._shapeRendering)
    335                 && (_clipRule == other._clipRule)
    336                 && (_fillRule == other._fillRule)
    337                 && (_capStyle == other._capStyle)
    338                 && (_joinStyle == other._joinStyle)
    339                 && (_textAnchor == other._textAnchor)
    340                 && (_colorInterpolation == other._colorInterpolation)
    341                 && (_colorInterpolationFilters == other._colorInterpolationFilters)
    342                 && (_writingMode == other._writingMode)
    343                 && (_glyphOrientationHorizontal == other._glyphOrientationHorizontal)
    344                 && (_glyphOrientationVertical == other._glyphOrientationVertical);
    345         }
    346 
    347         bool operator!=(const InheritedFlags& other) const
    348         {
    349             return !(*this == other);
    350         }
    351 
    352         unsigned _colorRendering : 2; // EColorRendering
    353         unsigned _imageRendering : 2; // EImageRendering
    354         unsigned _shapeRendering : 2; // EShapeRendering
    355         unsigned _clipRule : 1; // WindRule
    356         unsigned _fillRule : 1; // WindRule
    357         unsigned _capStyle : 2; // LineCap
    358         unsigned _joinStyle : 2; // LineJoin
    359         unsigned _textAnchor : 2; // ETextAnchor
    360         unsigned _colorInterpolation : 2; // EColorInterpolation
    361         unsigned _colorInterpolationFilters : 2; // EColorInterpolation
    362         unsigned _writingMode : 3; // SVGWritingMode
    363         unsigned _glyphOrientationHorizontal : 3; // EGlyphOrientation
    364         unsigned _glyphOrientationVertical : 3; // EGlyphOrientation
    365     } svg_inherited_flags;
    366 
    367     // don't inherit
    368     struct NonInheritedFlags {
    369         // 32 bit non-inherited, don't add to the struct, or the operator will break.
    370         bool operator==(const NonInheritedFlags &other) const { return _niflags == other._niflags; }
    371         bool operator!=(const NonInheritedFlags &other) const { return _niflags != other._niflags; }
    372 
    373         union {
    374             struct {
    375                 unsigned _alignmentBaseline : 4; // EAlignmentBaseline
    376                 unsigned _dominantBaseline : 4; // EDominantBaseline
    377                 unsigned _baselineShift : 2; // EBaselineShift
    378                 unsigned _vectorEffect: 1; // EVectorEffect
    379                 // 21 bits unused
    380             } f;
    381             uint32_t _niflags;
    382         };
    383     } svg_noninherited_flags;
    384 
    385     // inherited attributes
    386     DataRef<StyleFillData> fill;
    387     DataRef<StyleStrokeData> stroke;
    388     DataRef<StyleTextData> text;
    389     DataRef<StyleInheritedResourceData> inheritedResources;
    390 
    391     // non-inherited attributes
    392     DataRef<StyleStopData> stops;
    393     DataRef<StyleMiscData> misc;
    394     DataRef<StyleShadowSVGData> shadowSVG;
    395     DataRef<StyleResourceData> resources;
    396 
    397 private:
    398     enum CreateDefaultType { CreateDefault };
    399 
    400     SVGRenderStyle();
    401     SVGRenderStyle(const SVGRenderStyle&);
    402     SVGRenderStyle(CreateDefaultType); // Used to create the default style.
    403 
    404     void setBitDefaults()
    405     {
    406         svg_inherited_flags._clipRule = initialClipRule();
    407         svg_inherited_flags._colorRendering = initialColorRendering();
    408         svg_inherited_flags._fillRule = initialFillRule();
    409         svg_inherited_flags._imageRendering = initialImageRendering();
    410         svg_inherited_flags._shapeRendering = initialShapeRendering();
    411         svg_inherited_flags._textAnchor = initialTextAnchor();
    412         svg_inherited_flags._capStyle = initialCapStyle();
    413         svg_inherited_flags._joinStyle = initialJoinStyle();
    414         svg_inherited_flags._colorInterpolation = initialColorInterpolation();
    415         svg_inherited_flags._colorInterpolationFilters = initialColorInterpolationFilters();
    416         svg_inherited_flags._writingMode = initialWritingMode();
    417         svg_inherited_flags._glyphOrientationHorizontal = initialGlyphOrientationHorizontal();
    418         svg_inherited_flags._glyphOrientationVertical = initialGlyphOrientationVertical();
    419 
    420         svg_noninherited_flags._niflags = 0;
    421         svg_noninherited_flags.f._alignmentBaseline = initialAlignmentBaseline();
    422         svg_noninherited_flags.f._dominantBaseline = initialDominantBaseline();
    423         svg_noninherited_flags.f._baselineShift = initialBaselineShift();
    424         svg_noninherited_flags.f._vectorEffect = initialVectorEffect();
    425     }
    426 };
    427 
    428 } // namespace WebCore
    429 
    430 #endif // ENABLE(SVG)
    431 #endif // SVGRenderStyle_h
    432