• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3                   2004, 2005 Rob Buis <buis@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:
create()43     static PassRefPtr<SVGRenderStyle> create() { return adoptRef(new SVGRenderStyle); }
copy()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
initialAlignmentBaseline()56     static EAlignmentBaseline initialAlignmentBaseline() { return AB_AUTO; }
initialDominantBaseline()57     static EDominantBaseline initialDominantBaseline() { return DB_AUTO; }
initialBaselineShift()58     static EBaselineShift initialBaselineShift() { return BS_BASELINE; }
initialVectorEffect()59     static EVectorEffect initialVectorEffect() { return VE_NONE; }
initialCapStyle()60     static LineCap initialCapStyle() { return ButtCap; }
initialClipRule()61     static WindRule initialClipRule() { return RULE_NONZERO; }
initialColorInterpolation()62     static EColorInterpolation initialColorInterpolation() { return CI_SRGB; }
initialColorInterpolationFilters()63     static EColorInterpolation initialColorInterpolationFilters() { return CI_LINEARRGB; }
initialColorRendering()64     static EColorRendering initialColorRendering() { return CR_AUTO; }
initialFillRule()65     static WindRule initialFillRule() { return RULE_NONZERO; }
initialImageRendering()66     static EImageRendering initialImageRendering() { return IR_AUTO; }
initialJoinStyle()67     static LineJoin initialJoinStyle() { return MiterJoin; }
initialShapeRendering()68     static EShapeRendering initialShapeRendering() { return SR_AUTO; }
initialTextAnchor()69     static ETextAnchor initialTextAnchor() { return TA_START; }
initialWritingMode()70     static SVGWritingMode initialWritingMode() { return WM_LRTB; }
initialGlyphOrientationHorizontal()71     static EGlyphOrientation initialGlyphOrientationHorizontal() { return GO_0DEG; }
initialGlyphOrientationVertical()72     static EGlyphOrientation initialGlyphOrientationVertical() { return GO_AUTO; }
initialFillOpacity()73     static float initialFillOpacity() { return 1.0f; }
initialFillPaint()74     static SVGPaint* initialFillPaint() { return SVGPaint::defaultFill(); }
initialStrokeOpacity()75     static float initialStrokeOpacity() { return 1.0f; }
initialStrokePaint()76     static SVGPaint* initialStrokePaint() { return SVGPaint::defaultStroke(); }
initialStrokeDashArray()77     static Vector<SVGLength> initialStrokeDashArray() { return Vector<SVGLength>(); }
initialStrokeMiterLimit()78     static float initialStrokeMiterLimit() { return 4.0f; }
initialStopOpacity()79     static float initialStopOpacity() { return 1.0f; }
initialStopColor()80     static Color initialStopColor() { return Color(0, 0, 0); }
initialFloodOpacity()81     static float initialFloodOpacity() { return 1.0f; }
initialFloodColor()82     static Color initialFloodColor() { return Color(0, 0, 0); }
initialLightingColor()83     static Color initialLightingColor() { return Color(255, 255, 255); }
initialShadow()84     static ShadowData* initialShadow() { return 0; }
initialClipperResource()85     static String initialClipperResource() { return String(); }
initialFilterResource()86     static String initialFilterResource() { return String(); }
initialMaskerResource()87     static String initialMaskerResource() { return String(); }
initialMarkerStartResource()88     static String initialMarkerStartResource() { return String(); }
initialMarkerMidResource()89     static String initialMarkerMidResource() { return String(); }
initialMarkerEndResource()90     static String initialMarkerEndResource() { return String(); }
91 
initialBaselineShiftValue()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 
initialKerning()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 
initialStrokeDashOffset()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 
initialStrokeWidth()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
setAlignmentBaseline(EAlignmentBaseline val)129     void setAlignmentBaseline(EAlignmentBaseline val) { svg_noninherited_flags.f._alignmentBaseline = val; }
setDominantBaseline(EDominantBaseline val)130     void setDominantBaseline(EDominantBaseline val) { svg_noninherited_flags.f._dominantBaseline = val; }
setBaselineShift(EBaselineShift val)131     void setBaselineShift(EBaselineShift val) { svg_noninherited_flags.f._baselineShift = val; }
setVectorEffect(EVectorEffect val)132     void setVectorEffect(EVectorEffect val) { svg_noninherited_flags.f._vectorEffect = val; }
setCapStyle(LineCap val)133     void setCapStyle(LineCap val) { svg_inherited_flags._capStyle = val; }
setClipRule(WindRule val)134     void setClipRule(WindRule val) { svg_inherited_flags._clipRule = val; }
setColorInterpolation(EColorInterpolation val)135     void setColorInterpolation(EColorInterpolation val) { svg_inherited_flags._colorInterpolation = val; }
setColorInterpolationFilters(EColorInterpolation val)136     void setColorInterpolationFilters(EColorInterpolation val) { svg_inherited_flags._colorInterpolationFilters = val; }
setColorRendering(EColorRendering val)137     void setColorRendering(EColorRendering val) { svg_inherited_flags._colorRendering = val; }
setFillRule(WindRule val)138     void setFillRule(WindRule val) { svg_inherited_flags._fillRule = val; }
setImageRendering(EImageRendering val)139     void setImageRendering(EImageRendering val) { svg_inherited_flags._imageRendering = val; }
setJoinStyle(LineJoin val)140     void setJoinStyle(LineJoin val) { svg_inherited_flags._joinStyle = val; }
setShapeRendering(EShapeRendering val)141     void setShapeRendering(EShapeRendering val) { svg_inherited_flags._shapeRendering = val; }
setTextAnchor(ETextAnchor val)142     void setTextAnchor(ETextAnchor val) { svg_inherited_flags._textAnchor = val; }
setWritingMode(SVGWritingMode val)143     void setWritingMode(SVGWritingMode val) { svg_inherited_flags._writingMode = val; }
setGlyphOrientationHorizontal(EGlyphOrientation val)144     void setGlyphOrientationHorizontal(EGlyphOrientation val) { svg_inherited_flags._glyphOrientationHorizontal = val; }
setGlyphOrientationVertical(EGlyphOrientation val)145     void setGlyphOrientationVertical(EGlyphOrientation val) { svg_inherited_flags._glyphOrientationVertical = val; }
146 
setFillOpacity(float obj)147     void setFillOpacity(float obj)
148     {
149         if (!(fill->opacity == obj))
150             fill.access()->opacity = obj;
151     }
152 
setFillPaint(PassRefPtr<SVGPaint> obj)153     void setFillPaint(PassRefPtr<SVGPaint> obj)
154     {
155         if (!(fill->paint == obj))
156             fill.access()->paint = obj;
157     }
158 
setStrokeOpacity(float obj)159     void setStrokeOpacity(float obj)
160     {
161         if (!(stroke->opacity == obj))
162             stroke.access()->opacity = obj;
163     }
164 
setStrokePaint(PassRefPtr<SVGPaint> obj)165     void setStrokePaint(PassRefPtr<SVGPaint> obj)
166     {
167         if (!(stroke->paint == obj))
168             stroke.access()->paint = obj;
169     }
170 
setStrokeDashArray(const Vector<SVGLength> & obj)171     void setStrokeDashArray(const Vector<SVGLength>& obj)
172     {
173         if (!(stroke->dashArray == obj))
174             stroke.access()->dashArray = obj;
175     }
176 
setStrokeMiterLimit(float obj)177     void setStrokeMiterLimit(float obj)
178     {
179         if (!(stroke->miterLimit == obj))
180             stroke.access()->miterLimit = obj;
181     }
182 
setStrokeWidth(const SVGLength & obj)183     void setStrokeWidth(const SVGLength& obj)
184     {
185         if (!(stroke->width == obj))
186             stroke.access()->width = obj;
187     }
188 
setStrokeDashOffset(const SVGLength & obj)189     void setStrokeDashOffset(const SVGLength& obj)
190     {
191         if (!(stroke->dashOffset == obj))
192             stroke.access()->dashOffset = obj;
193     }
194 
setKerning(const SVGLength & obj)195     void setKerning(const SVGLength& obj)
196     {
197         if (!(text->kerning == obj))
198             text.access()->kerning = obj;
199     }
200 
setStopOpacity(float obj)201     void setStopOpacity(float obj)
202     {
203         if (!(stops->opacity == obj))
204             stops.access()->opacity = obj;
205     }
206 
setStopColor(const Color & obj)207     void setStopColor(const Color& obj)
208     {
209         if (!(stops->color == obj))
210             stops.access()->color = obj;
211     }
212 
setFloodOpacity(float obj)213     void setFloodOpacity(float obj)
214     {
215         if (!(misc->floodOpacity == obj))
216             misc.access()->floodOpacity = obj;
217     }
218 
setFloodColor(const Color & obj)219     void setFloodColor(const Color& obj)
220     {
221         if (!(misc->floodColor == obj))
222             misc.access()->floodColor = obj;
223     }
224 
setLightingColor(const Color & obj)225     void setLightingColor(const Color& obj)
226     {
227         if (!(misc->lightingColor == obj))
228             misc.access()->lightingColor = obj;
229     }
230 
setBaselineShiftValue(const SVGLength & obj)231     void setBaselineShiftValue(const SVGLength& obj)
232     {
233         if (!(misc->baselineShiftValue == obj))
234             misc.access()->baselineShiftValue = obj;
235     }
236 
setShadow(PassOwnPtr<ShadowData> obj)237     void setShadow(PassOwnPtr<ShadowData> obj) { shadowSVG.access()->shadow = obj; }
238 
239     // Setters for non-inherited resources
setClipperResource(const String & obj)240     void setClipperResource(const String& obj)
241     {
242         if (!(resources->clipper == obj))
243             resources.access()->clipper = obj;
244     }
245 
setFilterResource(const String & obj)246     void setFilterResource(const String& obj)
247     {
248         if (!(resources->filter == obj))
249             resources.access()->filter = obj;
250     }
251 
setMaskerResource(const String & obj)252     void setMaskerResource(const String& obj)
253     {
254         if (!(resources->masker == obj))
255             resources.access()->masker = obj;
256     }
257 
258     // Setters for inherited resources
setMarkerStartResource(const String & obj)259     void setMarkerStartResource(const String& obj)
260     {
261         if (!(inheritedResources->markerStart == obj))
262             inheritedResources.access()->markerStart = obj;
263     }
264 
setMarkerMidResource(const String & obj)265     void setMarkerMidResource(const String& obj)
266     {
267         if (!(inheritedResources->markerMid == obj))
268             inheritedResources.access()->markerMid = obj;
269     }
270 
setMarkerEndResource(const String & obj)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
alignmentBaseline()278     EAlignmentBaseline alignmentBaseline() const { return (EAlignmentBaseline) svg_noninherited_flags.f._alignmentBaseline; }
dominantBaseline()279     EDominantBaseline dominantBaseline() const { return (EDominantBaseline) svg_noninherited_flags.f._dominantBaseline; }
baselineShift()280     EBaselineShift baselineShift() const { return (EBaselineShift) svg_noninherited_flags.f._baselineShift; }
vectorEffect()281     EVectorEffect vectorEffect() const { return (EVectorEffect) svg_noninherited_flags.f._vectorEffect; }
capStyle()282     LineCap capStyle() const { return (LineCap) svg_inherited_flags._capStyle; }
clipRule()283     WindRule clipRule() const { return (WindRule) svg_inherited_flags._clipRule; }
colorInterpolation()284     EColorInterpolation colorInterpolation() const { return (EColorInterpolation) svg_inherited_flags._colorInterpolation; }
colorInterpolationFilters()285     EColorInterpolation colorInterpolationFilters() const { return (EColorInterpolation) svg_inherited_flags._colorInterpolationFilters; }
colorRendering()286     EColorRendering colorRendering() const { return (EColorRendering) svg_inherited_flags._colorRendering; }
fillRule()287     WindRule fillRule() const { return (WindRule) svg_inherited_flags._fillRule; }
imageRendering()288     EImageRendering imageRendering() const { return (EImageRendering) svg_inherited_flags._imageRendering; }
joinStyle()289     LineJoin joinStyle() const { return (LineJoin) svg_inherited_flags._joinStyle; }
shapeRendering()290     EShapeRendering shapeRendering() const { return (EShapeRendering) svg_inherited_flags._shapeRendering; }
textAnchor()291     ETextAnchor textAnchor() const { return (ETextAnchor) svg_inherited_flags._textAnchor; }
writingMode()292     SVGWritingMode writingMode() const { return (SVGWritingMode) svg_inherited_flags._writingMode; }
glyphOrientationHorizontal()293     EGlyphOrientation glyphOrientationHorizontal() const { return (EGlyphOrientation) svg_inherited_flags._glyphOrientationHorizontal; }
glyphOrientationVertical()294     EGlyphOrientation glyphOrientationVertical() const { return (EGlyphOrientation) svg_inherited_flags._glyphOrientationVertical; }
fillOpacity()295     float fillOpacity() const { return fill->opacity; }
fillPaint()296     SVGPaint* fillPaint() const { return fill->paint.get(); }
strokeOpacity()297     float strokeOpacity() const { return stroke->opacity; }
strokePaint()298     SVGPaint* strokePaint() const { return stroke->paint.get(); }
strokeDashArray()299     Vector<SVGLength> strokeDashArray() const { return stroke->dashArray; }
strokeMiterLimit()300     float strokeMiterLimit() const { return stroke->miterLimit; }
strokeWidth()301     SVGLength strokeWidth() const { return stroke->width; }
strokeDashOffset()302     SVGLength strokeDashOffset() const { return stroke->dashOffset; }
kerning()303     SVGLength kerning() const { return text->kerning; }
stopOpacity()304     float stopOpacity() const { return stops->opacity; }
stopColor()305     Color stopColor() const { return stops->color; }
floodOpacity()306     float floodOpacity() const { return misc->floodOpacity; }
floodColor()307     Color floodColor() const { return misc->floodColor; }
lightingColor()308     Color lightingColor() const { return misc->lightingColor; }
baselineShiftValue()309     SVGLength baselineShiftValue() const { return misc->baselineShiftValue; }
shadow()310     ShadowData* shadow() const { return shadowSVG->shadow.get(); }
clipperResource()311     String clipperResource() const { return resources->clipper; }
filterResource()312     String filterResource() const { return resources->filter; }
maskerResource()313     String maskerResource() const { return resources->masker; }
markerStartResource()314     String markerStartResource() const { return inheritedResources->markerStart; }
markerMidResource()315     String markerMidResource() const { return inheritedResources->markerMid; }
markerEndResource()316     String markerEndResource() const { return inheritedResources->markerEnd; }
317 
318     // convenience
hasClipper()319     bool hasClipper() const { return !clipperResource().isEmpty(); }
hasMasker()320     bool hasMasker() const { return !maskerResource().isEmpty(); }
hasFilter()321     bool hasFilter() const { return !filterResource().isEmpty(); }
hasMarkers()322     bool hasMarkers() const { return !markerStartResource().isEmpty() || !markerMidResource().isEmpty() || !markerEndResource().isEmpty(); }
hasStroke()323     bool hasStroke() const { return strokePaint()->paintType() != SVGPaint::SVG_PAINTTYPE_NONE; }
hasFill()324     bool hasFill() const { return fillPaint()->paintType() != SVGPaint::SVG_PAINTTYPE_NONE; }
isVerticalWritingMode()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 
setBitDefaults()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