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 #include "bindings/v8/ExceptionStatePlaceholder.h" 27 #include "core/css/CSSValueList.h" 28 #include "core/platform/graphics/GraphicsTypes.h" 29 #include "core/platform/graphics/Path.h" 30 #include "core/rendering/style/DataRef.h" 31 #include "core/rendering/style/RenderStyleConstants.h" 32 #include "core/rendering/style/SVGRenderStyleDefs.h" 33 #include "core/svg/SVGPaint.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 void copyNonInheritedFrom(const SVGRenderStyle*); 50 51 StyleDifference diff(const SVGRenderStyle*) const; 52 53 bool operator==(const SVGRenderStyle&) const; 54 bool operator!=(const SVGRenderStyle& o) const { return !(*this == o); } 55 56 // Initial values for all the properties 57 static EAlignmentBaseline initialAlignmentBaseline() { return AB_AUTO; } 58 static EDominantBaseline initialDominantBaseline() { return DB_AUTO; } 59 static EBaselineShift initialBaselineShift() { return BS_BASELINE; } 60 static EVectorEffect initialVectorEffect() { return VE_NONE; } 61 static EBufferedRendering initialBufferedRendering() { return BR_AUTO; } 62 static LineCap initialCapStyle() { return ButtCap; } 63 static WindRule initialClipRule() { return RULE_NONZERO; } 64 static EColorInterpolation initialColorInterpolation() { return CI_SRGB; } 65 static EColorInterpolation initialColorInterpolationFilters() { return CI_LINEARRGB; } 66 static EColorRendering initialColorRendering() { return CR_AUTO; } 67 static WindRule initialFillRule() { return RULE_NONZERO; } 68 static LineJoin initialJoinStyle() { return MiterJoin; } 69 static EShapeRendering initialShapeRendering() { return SR_AUTO; } 70 static ETextAnchor initialTextAnchor() { return TA_START; } 71 static SVGWritingMode initialWritingMode() { return WM_LRTB; } 72 static EGlyphOrientation initialGlyphOrientationHorizontal() { return GO_0DEG; } 73 static EGlyphOrientation initialGlyphOrientationVertical() { return GO_AUTO; } 74 static float initialFillOpacity() { return 1; } 75 static SVGPaint::SVGPaintType initialFillPaintType() { return SVGPaint::SVG_PAINTTYPE_RGBCOLOR; } 76 static Color initialFillPaintColor() { return Color::black; } 77 static String initialFillPaintUri() { return String(); } 78 static float initialStrokeOpacity() { return 1; } 79 static SVGPaint::SVGPaintType initialStrokePaintType() { return SVGPaint::SVG_PAINTTYPE_NONE; } 80 static Color initialStrokePaintColor() { return Color(); } 81 static String initialStrokePaintUri() { return String(); } 82 static Vector<SVGLength> initialStrokeDashArray() { return Vector<SVGLength>(); } 83 static float initialStrokeMiterLimit() { return 4; } 84 static float initialStopOpacity() { return 1; } 85 static Color initialStopColor() { return Color(0, 0, 0); } 86 static float initialFloodOpacity() { return 1; } 87 static Color initialFloodColor() { return Color(0, 0, 0); } 88 static Color initialLightingColor() { return Color(255, 255, 255); } 89 static String initialClipperResource() { return String(); } 90 static String initialFilterResource() { return String(); } 91 static String initialMaskerResource() { return String(); } 92 static String initialMarkerStartResource() { return String(); } 93 static String initialMarkerMidResource() { return String(); } 94 static String initialMarkerEndResource() { return String(); } 95 static EMaskType initialMaskType() { return MT_LUMINANCE; } 96 97 static SVGLength initialBaselineShiftValue() 98 { 99 SVGLength length; 100 length.newValueSpecifiedUnits(LengthTypeNumber, 0, ASSERT_NO_EXCEPTION); 101 return length; 102 } 103 104 static SVGLength initialKerning() 105 { 106 SVGLength length; 107 length.newValueSpecifiedUnits(LengthTypeNumber, 0, ASSERT_NO_EXCEPTION); 108 return length; 109 } 110 111 static SVGLength initialStrokeDashOffset() 112 { 113 SVGLength length; 114 length.newValueSpecifiedUnits(LengthTypeNumber, 0, ASSERT_NO_EXCEPTION); 115 return length; 116 } 117 118 static SVGLength initialStrokeWidth() 119 { 120 SVGLength length; 121 length.newValueSpecifiedUnits(LengthTypeNumber, 1, ASSERT_NO_EXCEPTION); 122 return length; 123 } 124 125 // SVG CSS Property setters 126 void setAlignmentBaseline(EAlignmentBaseline val) { svg_noninherited_flags.f._alignmentBaseline = val; } 127 void setDominantBaseline(EDominantBaseline val) { svg_noninherited_flags.f._dominantBaseline = val; } 128 void setBaselineShift(EBaselineShift val) { svg_noninherited_flags.f._baselineShift = val; } 129 void setVectorEffect(EVectorEffect val) { svg_noninherited_flags.f._vectorEffect = val; } 130 void setBufferedRendering(EBufferedRendering val) { svg_noninherited_flags.f.bufferedRendering = val; } 131 void setCapStyle(LineCap val) { svg_inherited_flags._capStyle = val; } 132 void setClipRule(WindRule val) { svg_inherited_flags._clipRule = val; } 133 void setColorInterpolation(EColorInterpolation val) { svg_inherited_flags._colorInterpolation = val; } 134 void setColorInterpolationFilters(EColorInterpolation val) { svg_inherited_flags._colorInterpolationFilters = val; } 135 void setColorRendering(EColorRendering val) { svg_inherited_flags._colorRendering = val; } 136 void setFillRule(WindRule val) { svg_inherited_flags._fillRule = val; } 137 void setJoinStyle(LineJoin val) { svg_inherited_flags._joinStyle = val; } 138 void setShapeRendering(EShapeRendering val) { svg_inherited_flags._shapeRendering = val; } 139 void setTextAnchor(ETextAnchor val) { svg_inherited_flags._textAnchor = val; } 140 void setWritingMode(SVGWritingMode val) { svg_inherited_flags._writingMode = val; } 141 void setGlyphOrientationHorizontal(EGlyphOrientation val) { svg_inherited_flags._glyphOrientationHorizontal = val; } 142 void setGlyphOrientationVertical(EGlyphOrientation val) { svg_inherited_flags._glyphOrientationVertical = val; } 143 void setMaskType(EMaskType val) { svg_noninherited_flags.f.maskType = val; } 144 145 void setFillOpacity(float obj) 146 { 147 if (!(fill->opacity == obj)) 148 fill.access()->opacity = obj; 149 } 150 151 void setFillPaint(SVGPaint::SVGPaintType type, const Color& color, const String& uri, bool applyToRegularStyle = true, bool applyToVisitedLinkStyle = false) 152 { 153 if (applyToRegularStyle) { 154 if (!(fill->paintType == type)) 155 fill.access()->paintType = type; 156 if (!(fill->paintColor == color)) 157 fill.access()->paintColor = color; 158 if (!(fill->paintUri == uri)) 159 fill.access()->paintUri = uri; 160 } 161 if (applyToVisitedLinkStyle) { 162 if (!(fill->visitedLinkPaintType == type)) 163 fill.access()->visitedLinkPaintType = type; 164 if (!(fill->visitedLinkPaintColor == color)) 165 fill.access()->visitedLinkPaintColor = color; 166 if (!(fill->visitedLinkPaintUri == uri)) 167 fill.access()->visitedLinkPaintUri = uri; 168 } 169 } 170 171 void setStrokeOpacity(float obj) 172 { 173 if (!(stroke->opacity == obj)) 174 stroke.access()->opacity = obj; 175 } 176 177 void setStrokePaint(SVGPaint::SVGPaintType type, const Color& color, const String& uri, bool applyToRegularStyle = true, bool applyToVisitedLinkStyle = false) 178 { 179 if (applyToRegularStyle) { 180 if (!(stroke->paintType == type)) 181 stroke.access()->paintType = type; 182 if (!(stroke->paintColor == color)) 183 stroke.access()->paintColor = color; 184 if (!(stroke->paintUri == uri)) 185 stroke.access()->paintUri = uri; 186 } 187 if (applyToVisitedLinkStyle) { 188 if (!(stroke->visitedLinkPaintType == type)) 189 stroke.access()->visitedLinkPaintType = type; 190 if (!(stroke->visitedLinkPaintColor == color)) 191 stroke.access()->visitedLinkPaintColor = color; 192 if (!(stroke->visitedLinkPaintUri == uri)) 193 stroke.access()->visitedLinkPaintUri = uri; 194 } 195 } 196 197 void setStrokeDashArray(const Vector<SVGLength>& obj) 198 { 199 if (!(stroke->dashArray == obj)) 200 stroke.access()->dashArray = obj; 201 } 202 203 void setStrokeMiterLimit(float obj) 204 { 205 if (!(stroke->miterLimit == obj)) 206 stroke.access()->miterLimit = obj; 207 } 208 209 void setStrokeWidth(const SVGLength& obj) 210 { 211 if (!(stroke->width == obj)) 212 stroke.access()->width = obj; 213 } 214 215 void setStrokeDashOffset(const SVGLength& obj) 216 { 217 if (!(stroke->dashOffset == obj)) 218 stroke.access()->dashOffset = obj; 219 } 220 221 void setKerning(const SVGLength& obj) 222 { 223 if (!(text->kerning == obj)) 224 text.access()->kerning = obj; 225 } 226 227 void setStopOpacity(float obj) 228 { 229 if (!(stops->opacity == obj)) 230 stops.access()->opacity = obj; 231 } 232 233 void setStopColor(const Color& obj) 234 { 235 if (!(stops->color == obj)) 236 stops.access()->color = obj; 237 } 238 239 void setFloodOpacity(float obj) 240 { 241 if (!(misc->floodOpacity == obj)) 242 misc.access()->floodOpacity = obj; 243 } 244 245 void setFloodColor(const Color& obj) 246 { 247 if (!(misc->floodColor == obj)) 248 misc.access()->floodColor = obj; 249 } 250 251 void setLightingColor(const Color& obj) 252 { 253 if (!(misc->lightingColor == obj)) 254 misc.access()->lightingColor = obj; 255 } 256 257 void setBaselineShiftValue(const SVGLength& obj) 258 { 259 if (!(misc->baselineShiftValue == obj)) 260 misc.access()->baselineShiftValue = obj; 261 } 262 263 // Setters for non-inherited resources 264 void setClipperResource(const String& obj) 265 { 266 if (!(resources->clipper == obj)) 267 resources.access()->clipper = obj; 268 } 269 270 void setFilterResource(const String& obj) 271 { 272 if (!(resources->filter == obj)) 273 resources.access()->filter = obj; 274 } 275 276 void setMaskerResource(const String& obj) 277 { 278 if (!(resources->masker == obj)) 279 resources.access()->masker = obj; 280 } 281 282 // Setters for inherited resources 283 void setMarkerStartResource(const String& obj) 284 { 285 if (!(inheritedResources->markerStart == obj)) 286 inheritedResources.access()->markerStart = obj; 287 } 288 289 void setMarkerMidResource(const String& obj) 290 { 291 if (!(inheritedResources->markerMid == obj)) 292 inheritedResources.access()->markerMid = obj; 293 } 294 295 void setMarkerEndResource(const String& obj) 296 { 297 if (!(inheritedResources->markerEnd == obj)) 298 inheritedResources.access()->markerEnd = obj; 299 } 300 301 // Read accessors for all the properties 302 EAlignmentBaseline alignmentBaseline() const { return (EAlignmentBaseline) svg_noninherited_flags.f._alignmentBaseline; } 303 EDominantBaseline dominantBaseline() const { return (EDominantBaseline) svg_noninherited_flags.f._dominantBaseline; } 304 EBaselineShift baselineShift() const { return (EBaselineShift) svg_noninherited_flags.f._baselineShift; } 305 EVectorEffect vectorEffect() const { return (EVectorEffect) svg_noninherited_flags.f._vectorEffect; } 306 EBufferedRendering bufferedRendering() const { return (EBufferedRendering) svg_noninherited_flags.f.bufferedRendering; } 307 LineCap capStyle() const { return (LineCap) svg_inherited_flags._capStyle; } 308 WindRule clipRule() const { return (WindRule) svg_inherited_flags._clipRule; } 309 EColorInterpolation colorInterpolation() const { return (EColorInterpolation) svg_inherited_flags._colorInterpolation; } 310 EColorInterpolation colorInterpolationFilters() const { return (EColorInterpolation) svg_inherited_flags._colorInterpolationFilters; } 311 EColorRendering colorRendering() const { return (EColorRendering) svg_inherited_flags._colorRendering; } 312 WindRule fillRule() const { return (WindRule) svg_inherited_flags._fillRule; } 313 LineJoin joinStyle() const { return (LineJoin) svg_inherited_flags._joinStyle; } 314 EShapeRendering shapeRendering() const { return (EShapeRendering) svg_inherited_flags._shapeRendering; } 315 ETextAnchor textAnchor() const { return (ETextAnchor) svg_inherited_flags._textAnchor; } 316 SVGWritingMode writingMode() const { return (SVGWritingMode) svg_inherited_flags._writingMode; } 317 EGlyphOrientation glyphOrientationHorizontal() const { return (EGlyphOrientation) svg_inherited_flags._glyphOrientationHorizontal; } 318 EGlyphOrientation glyphOrientationVertical() const { return (EGlyphOrientation) svg_inherited_flags._glyphOrientationVertical; } 319 float fillOpacity() const { return fill->opacity; } 320 const SVGPaint::SVGPaintType& fillPaintType() const { return fill->paintType; } 321 const Color& fillPaintColor() const { return fill->paintColor; } 322 const String& fillPaintUri() const { return fill->paintUri; } 323 float strokeOpacity() const { return stroke->opacity; } 324 const SVGPaint::SVGPaintType& strokePaintType() const { return stroke->paintType; } 325 const Color& strokePaintColor() const { return stroke->paintColor; } 326 const String& strokePaintUri() const { return stroke->paintUri; } 327 Vector<SVGLength> strokeDashArray() const { return stroke->dashArray; } 328 float strokeMiterLimit() const { return stroke->miterLimit; } 329 SVGLength strokeWidth() const { return stroke->width; } 330 SVGLength strokeDashOffset() const { return stroke->dashOffset; } 331 SVGLength kerning() const { return text->kerning; } 332 float stopOpacity() const { return stops->opacity; } 333 const Color& stopColor() const { return stops->color; } 334 float floodOpacity() const { return misc->floodOpacity; } 335 const Color& floodColor() const { return misc->floodColor; } 336 const Color& lightingColor() const { return misc->lightingColor; } 337 SVGLength baselineShiftValue() const { return misc->baselineShiftValue; } 338 String clipperResource() const { return resources->clipper; } 339 String filterResource() const { return resources->filter; } 340 String maskerResource() const { return resources->masker; } 341 String markerStartResource() const { return inheritedResources->markerStart; } 342 String markerMidResource() const { return inheritedResources->markerMid; } 343 String markerEndResource() const { return inheritedResources->markerEnd; } 344 EMaskType maskType() const { return (EMaskType) svg_noninherited_flags.f.maskType; } 345 346 const SVGPaint::SVGPaintType& visitedLinkFillPaintType() const { return fill->visitedLinkPaintType; } 347 const Color& visitedLinkFillPaintColor() const { return fill->visitedLinkPaintColor; } 348 const String& visitedLinkFillPaintUri() const { return fill->visitedLinkPaintUri; } 349 const SVGPaint::SVGPaintType& visitedLinkStrokePaintType() const { return stroke->visitedLinkPaintType; } 350 const Color& visitedLinkStrokePaintColor() const { return stroke->visitedLinkPaintColor; } 351 const String& visitedLinkStrokePaintUri() const { return stroke->visitedLinkPaintUri; } 352 353 // convenience 354 bool hasClipper() const { return !clipperResource().isEmpty(); } 355 bool hasMasker() const { return !maskerResource().isEmpty(); } 356 bool hasFilter() const { return !filterResource().isEmpty(); } 357 bool hasMarkers() const { return !markerStartResource().isEmpty() || !markerMidResource().isEmpty() || !markerEndResource().isEmpty(); } 358 bool hasStroke() const { return strokePaintType() != SVGPaint::SVG_PAINTTYPE_NONE; } 359 bool hasVisibleStroke() const { return hasStroke() && !strokeWidth().isZero(); } 360 bool hasFill() const { return fillPaintType() != SVGPaint::SVG_PAINTTYPE_NONE; } 361 bool isVerticalWritingMode() const { return writingMode() == WM_TBRL || writingMode() == WM_TB; } 362 363 protected: 364 // inherit 365 struct InheritedFlags { 366 bool operator==(const InheritedFlags& other) const 367 { 368 return (_colorRendering == other._colorRendering) 369 && (_shapeRendering == other._shapeRendering) 370 && (_clipRule == other._clipRule) 371 && (_fillRule == other._fillRule) 372 && (_capStyle == other._capStyle) 373 && (_joinStyle == other._joinStyle) 374 && (_textAnchor == other._textAnchor) 375 && (_colorInterpolation == other._colorInterpolation) 376 && (_colorInterpolationFilters == other._colorInterpolationFilters) 377 && (_writingMode == other._writingMode) 378 && (_glyphOrientationHorizontal == other._glyphOrientationHorizontal) 379 && (_glyphOrientationVertical == other._glyphOrientationVertical); 380 } 381 382 bool operator!=(const InheritedFlags& other) const 383 { 384 return !(*this == other); 385 } 386 387 unsigned _colorRendering : 2; // EColorRendering 388 unsigned _shapeRendering : 2; // EShapeRendering 389 unsigned _clipRule : 1; // WindRule 390 unsigned _fillRule : 1; // WindRule 391 unsigned _capStyle : 2; // LineCap 392 unsigned _joinStyle : 2; // LineJoin 393 unsigned _textAnchor : 2; // ETextAnchor 394 unsigned _colorInterpolation : 2; // EColorInterpolation 395 unsigned _colorInterpolationFilters : 2; // EColorInterpolation 396 unsigned _writingMode : 3; // SVGWritingMode 397 unsigned _glyphOrientationHorizontal : 3; // EGlyphOrientation 398 unsigned _glyphOrientationVertical : 3; // EGlyphOrientation 399 } svg_inherited_flags; 400 401 // don't inherit 402 struct NonInheritedFlags { 403 // 32 bit non-inherited, don't add to the struct, or the operator will break. 404 bool operator==(const NonInheritedFlags &other) const { return _niflags == other._niflags; } 405 bool operator!=(const NonInheritedFlags &other) const { return _niflags != other._niflags; } 406 407 union { 408 struct { 409 unsigned _alignmentBaseline : 4; // EAlignmentBaseline 410 unsigned _dominantBaseline : 4; // EDominantBaseline 411 unsigned _baselineShift : 2; // EBaselineShift 412 unsigned _vectorEffect: 1; // EVectorEffect 413 unsigned bufferedRendering: 2; // EBufferedRendering 414 unsigned maskType: 1; // EMaskType 415 // 18 bits unused 416 } f; 417 uint32_t _niflags; 418 }; 419 } svg_noninherited_flags; 420 421 // inherited attributes 422 DataRef<StyleFillData> fill; 423 DataRef<StyleStrokeData> stroke; 424 DataRef<StyleTextData> text; 425 DataRef<StyleInheritedResourceData> inheritedResources; 426 427 // non-inherited attributes 428 DataRef<StyleStopData> stops; 429 DataRef<StyleMiscData> misc; 430 DataRef<StyleResourceData> resources; 431 432 private: 433 enum CreateDefaultType { CreateDefault }; 434 435 SVGRenderStyle(); 436 SVGRenderStyle(const SVGRenderStyle&); 437 SVGRenderStyle(CreateDefaultType); // Used to create the default style. 438 439 void setBitDefaults() 440 { 441 svg_inherited_flags._clipRule = initialClipRule(); 442 svg_inherited_flags._colorRendering = initialColorRendering(); 443 svg_inherited_flags._fillRule = initialFillRule(); 444 svg_inherited_flags._shapeRendering = initialShapeRendering(); 445 svg_inherited_flags._textAnchor = initialTextAnchor(); 446 svg_inherited_flags._capStyle = initialCapStyle(); 447 svg_inherited_flags._joinStyle = initialJoinStyle(); 448 svg_inherited_flags._colorInterpolation = initialColorInterpolation(); 449 svg_inherited_flags._colorInterpolationFilters = initialColorInterpolationFilters(); 450 svg_inherited_flags._writingMode = initialWritingMode(); 451 svg_inherited_flags._glyphOrientationHorizontal = initialGlyphOrientationHorizontal(); 452 svg_inherited_flags._glyphOrientationVertical = initialGlyphOrientationVertical(); 453 454 svg_noninherited_flags._niflags = 0; 455 svg_noninherited_flags.f._alignmentBaseline = initialAlignmentBaseline(); 456 svg_noninherited_flags.f._dominantBaseline = initialDominantBaseline(); 457 svg_noninherited_flags.f._baselineShift = initialBaselineShift(); 458 svg_noninherited_flags.f._vectorEffect = initialVectorEffect(); 459 svg_noninherited_flags.f.bufferedRendering = initialBufferedRendering(); 460 svg_noninherited_flags.f.maskType = initialMaskType(); 461 } 462 }; 463 464 } // namespace WebCore 465 466 #endif // SVGRenderStyle_h 467