layout/svg/nsSVGGradientFrame.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 // Main header first:
michael@0 7 #include "nsSVGGradientFrame.h"
michael@0 8 #include <algorithm>
michael@0 9
michael@0 10 // Keep others in (case-insensitive) order:
michael@0 11 #include "gfxPattern.h"
michael@0 12 #include "mozilla/dom/SVGGradientElement.h"
michael@0 13 #include "mozilla/dom/SVGStopElement.h"
michael@0 14 #include "nsContentUtils.h"
michael@0 15 #include "nsSVGEffects.h"
michael@0 16 #include "nsSVGAnimatedTransformList.h"
michael@0 17 #include "gfxColor.h"
michael@0 18
michael@0 19 // XXX Tight coupling with content classes ahead!
michael@0 20
michael@0 21 using namespace mozilla;
michael@0 22 using namespace mozilla::dom;
michael@0 23
michael@0 24 //----------------------------------------------------------------------
michael@0 25 // Helper classes
michael@0 26
michael@0 27 class MOZ_STACK_CLASS nsSVGGradientFrame::AutoGradientReferencer
michael@0 28 {
michael@0 29 public:
michael@0 30 AutoGradientReferencer(nsSVGGradientFrame *aFrame
michael@0 31 MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
michael@0 32 : mFrame(aFrame)
michael@0 33 {
michael@0 34 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
michael@0 35 // Reference loops should normally be detected in advance and handled, so
michael@0 36 // we're not expecting to encounter them here
michael@0 37 NS_ABORT_IF_FALSE(!mFrame->mLoopFlag, "Undetected reference loop!");
michael@0 38 mFrame->mLoopFlag = true;
michael@0 39 }
michael@0 40 ~AutoGradientReferencer() {
michael@0 41 mFrame->mLoopFlag = false;
michael@0 42 }
michael@0 43 private:
michael@0 44 nsSVGGradientFrame *mFrame;
michael@0 45 MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
michael@0 46 };
michael@0 47
michael@0 48 //----------------------------------------------------------------------
michael@0 49 // Implementation
michael@0 50
michael@0 51 nsSVGGradientFrame::nsSVGGradientFrame(nsStyleContext* aContext) :
michael@0 52 nsSVGGradientFrameBase(aContext),
michael@0 53 mLoopFlag(false),
michael@0 54 mNoHRefURI(false)
michael@0 55 {
michael@0 56 }
michael@0 57
michael@0 58 NS_IMPL_FRAMEARENA_HELPERS(nsSVGGradientFrame)
michael@0 59
michael@0 60 //----------------------------------------------------------------------
michael@0 61 // nsIFrame methods:
michael@0 62
michael@0 63 nsresult
michael@0 64 nsSVGGradientFrame::AttributeChanged(int32_t aNameSpaceID,
michael@0 65 nsIAtom* aAttribute,
michael@0 66 int32_t aModType)
michael@0 67 {
michael@0 68 if (aNameSpaceID == kNameSpaceID_None &&
michael@0 69 (aAttribute == nsGkAtoms::gradientUnits ||
michael@0 70 aAttribute == nsGkAtoms::gradientTransform ||
michael@0 71 aAttribute == nsGkAtoms::spreadMethod)) {
michael@0 72 nsSVGEffects::InvalidateDirectRenderingObservers(this);
michael@0 73 } else if (aNameSpaceID == kNameSpaceID_XLink &&
michael@0 74 aAttribute == nsGkAtoms::href) {
michael@0 75 // Blow away our reference, if any
michael@0 76 Properties().Delete(nsSVGEffects::HrefProperty());
michael@0 77 mNoHRefURI = false;
michael@0 78 // And update whoever references us
michael@0 79 nsSVGEffects::InvalidateDirectRenderingObservers(this);
michael@0 80 }
michael@0 81
michael@0 82 return nsSVGGradientFrameBase::AttributeChanged(aNameSpaceID,
michael@0 83 aAttribute, aModType);
michael@0 84 }
michael@0 85
michael@0 86 //----------------------------------------------------------------------
michael@0 87
michael@0 88 uint16_t
michael@0 89 nsSVGGradientFrame::GetEnumValue(uint32_t aIndex, nsIContent *aDefault)
michael@0 90 {
michael@0 91 const nsSVGEnum& thisEnum =
michael@0 92 static_cast<dom::SVGGradientElement*>(mContent)->mEnumAttributes[aIndex];
michael@0 93
michael@0 94 if (thisEnum.IsExplicitlySet())
michael@0 95 return thisEnum.GetAnimValue();
michael@0 96
michael@0 97 AutoGradientReferencer gradientRef(this);
michael@0 98
michael@0 99 nsSVGGradientFrame *next = GetReferencedGradientIfNotInUse();
michael@0 100 return next ? next->GetEnumValue(aIndex, aDefault) :
michael@0 101 static_cast<dom::SVGGradientElement*>(aDefault)->
michael@0 102 mEnumAttributes[aIndex].GetAnimValue();
michael@0 103 }
michael@0 104
michael@0 105 uint16_t
michael@0 106 nsSVGGradientFrame::GetGradientUnits()
michael@0 107 {
michael@0 108 // This getter is called every time the others are called - maybe cache it?
michael@0 109 return GetEnumValue(dom::SVGGradientElement::GRADIENTUNITS);
michael@0 110 }
michael@0 111
michael@0 112 uint16_t
michael@0 113 nsSVGGradientFrame::GetSpreadMethod()
michael@0 114 {
michael@0 115 return GetEnumValue(dom::SVGGradientElement::SPREADMETHOD);
michael@0 116 }
michael@0 117
michael@0 118 const nsSVGAnimatedTransformList*
michael@0 119 nsSVGGradientFrame::GetGradientTransformList(nsIContent* aDefault)
michael@0 120 {
michael@0 121 nsSVGAnimatedTransformList *thisTransformList =
michael@0 122 static_cast<dom::SVGGradientElement*>(mContent)->GetAnimatedTransformList();
michael@0 123
michael@0 124 if (thisTransformList && thisTransformList->IsExplicitlySet())
michael@0 125 return thisTransformList;
michael@0 126
michael@0 127 AutoGradientReferencer gradientRef(this);
michael@0 128
michael@0 129 nsSVGGradientFrame *next = GetReferencedGradientIfNotInUse();
michael@0 130 return next ? next->GetGradientTransformList(aDefault) :
michael@0 131 static_cast<const dom::SVGGradientElement*>(aDefault)
michael@0 132 ->mGradientTransform.get();
michael@0 133 }
michael@0 134
michael@0 135 gfxMatrix
michael@0 136 nsSVGGradientFrame::GetGradientTransform(nsIFrame *aSource,
michael@0 137 const gfxRect *aOverrideBounds)
michael@0 138 {
michael@0 139 gfxMatrix bboxMatrix;
michael@0 140
michael@0 141 uint16_t gradientUnits = GetGradientUnits();
michael@0 142 if (gradientUnits != SVG_UNIT_TYPE_USERSPACEONUSE) {
michael@0 143 NS_ASSERTION(gradientUnits == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX,
michael@0 144 "Unknown gradientUnits type");
michael@0 145 // objectBoundingBox is the default anyway
michael@0 146
michael@0 147 gfxRect bbox =
michael@0 148 aOverrideBounds ? *aOverrideBounds : nsSVGUtils::GetBBox(aSource);
michael@0 149 bboxMatrix =
michael@0 150 gfxMatrix(bbox.Width(), 0, 0, bbox.Height(), bbox.X(), bbox.Y());
michael@0 151 }
michael@0 152
michael@0 153 const nsSVGAnimatedTransformList* animTransformList =
michael@0 154 GetGradientTransformList(mContent);
michael@0 155 if (!animTransformList)
michael@0 156 return bboxMatrix;
michael@0 157
michael@0 158 gfxMatrix gradientTransform =
michael@0 159 animTransformList->GetAnimValue().GetConsolidationMatrix();
michael@0 160 return bboxMatrix.PreMultiply(gradientTransform);
michael@0 161 }
michael@0 162
michael@0 163 dom::SVGLinearGradientElement*
michael@0 164 nsSVGGradientFrame::GetLinearGradientWithLength(uint32_t aIndex,
michael@0 165 dom::SVGLinearGradientElement* aDefault)
michael@0 166 {
michael@0 167 // If this was a linear gradient with the required length, we would have
michael@0 168 // already found it in nsSVGLinearGradientFrame::GetLinearGradientWithLength.
michael@0 169 // Since we didn't find the length, continue looking down the chain.
michael@0 170
michael@0 171 AutoGradientReferencer gradientRef(this);
michael@0 172
michael@0 173 nsSVGGradientFrame *next = GetReferencedGradientIfNotInUse();
michael@0 174 return next ? next->GetLinearGradientWithLength(aIndex, aDefault) : aDefault;
michael@0 175 }
michael@0 176
michael@0 177 dom::SVGRadialGradientElement*
michael@0 178 nsSVGGradientFrame::GetRadialGradientWithLength(uint32_t aIndex,
michael@0 179 dom::SVGRadialGradientElement* aDefault)
michael@0 180 {
michael@0 181 // If this was a radial gradient with the required length, we would have
michael@0 182 // already found it in nsSVGRadialGradientFrame::GetRadialGradientWithLength.
michael@0 183 // Since we didn't find the length, continue looking down the chain.
michael@0 184
michael@0 185 AutoGradientReferencer gradientRef(this);
michael@0 186
michael@0 187 nsSVGGradientFrame *next = GetReferencedGradientIfNotInUse();
michael@0 188 return next ? next->GetRadialGradientWithLength(aIndex, aDefault) : aDefault;
michael@0 189 }
michael@0 190
michael@0 191 //----------------------------------------------------------------------
michael@0 192 // nsSVGPaintServerFrame methods:
michael@0 193
michael@0 194 //helper
michael@0 195 static void GetStopInformation(nsIFrame* aStopFrame,
michael@0 196 float *aOffset,
michael@0 197 nscolor *aStopColor,
michael@0 198 float *aStopOpacity)
michael@0 199 {
michael@0 200 nsIContent* stopContent = aStopFrame->GetContent();
michael@0 201 MOZ_ASSERT(stopContent && stopContent->IsSVG(nsGkAtoms::stop));
michael@0 202
michael@0 203 static_cast<SVGStopElement*>(stopContent)->
michael@0 204 GetAnimatedNumberValues(aOffset, nullptr);
michael@0 205
michael@0 206 *aOffset = mozilla::clamped(*aOffset, 0.0f, 1.0f);
michael@0 207 *aStopColor = aStopFrame->StyleSVGReset()->mStopColor;
michael@0 208 *aStopOpacity = aStopFrame->StyleSVGReset()->mStopOpacity;
michael@0 209 }
michael@0 210
michael@0 211 already_AddRefed<gfxPattern>
michael@0 212 nsSVGGradientFrame::GetPaintServerPattern(nsIFrame *aSource,
michael@0 213 const gfxMatrix& aContextMatrix,
michael@0 214 nsStyleSVGPaint nsStyleSVG::*aFillOrStroke,
michael@0 215 float aGraphicOpacity,
michael@0 216 const gfxRect *aOverrideBounds)
michael@0 217 {
michael@0 218 uint16_t gradientUnits = GetGradientUnits();
michael@0 219 MOZ_ASSERT(gradientUnits == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX ||
michael@0 220 gradientUnits == SVG_UNIT_TYPE_USERSPACEONUSE);
michael@0 221 if (gradientUnits == SVG_UNIT_TYPE_USERSPACEONUSE) {
michael@0 222 // Set mSource for this consumer.
michael@0 223 // If this gradient is applied to text, our caller will be the glyph, which
michael@0 224 // is not an element, so we need to get the parent
michael@0 225 mSource = aSource->GetContent()->IsNodeOfType(nsINode::eTEXT) ?
michael@0 226 aSource->GetParent() : aSource;
michael@0 227 }
michael@0 228
michael@0 229 nsAutoTArray<nsIFrame*,8> stopFrames;
michael@0 230 GetStopFrames(&stopFrames);
michael@0 231
michael@0 232 uint32_t nStops = stopFrames.Length();
michael@0 233
michael@0 234 // SVG specification says that no stops should be treated like
michael@0 235 // the corresponding fill or stroke had "none" specified.
michael@0 236 if (nStops == 0) {
michael@0 237 nsRefPtr<gfxPattern> pattern = new gfxPattern(gfxRGBA(0, 0, 0, 0));
michael@0 238 return pattern.forget();
michael@0 239 }
michael@0 240
michael@0 241 if (nStops == 1 || GradientVectorLengthIsZero()) {
michael@0 242 // The gradient paints a single colour, using the stop-color of the last
michael@0 243 // gradient step if there are more than one.
michael@0 244 float stopOpacity = stopFrames[nStops-1]->StyleSVGReset()->mStopOpacity;
michael@0 245 nscolor stopColor = stopFrames[nStops-1]->StyleSVGReset()->mStopColor;
michael@0 246
michael@0 247 nsRefPtr<gfxPattern> pattern = new gfxPattern(
michael@0 248 gfxRGBA(NS_GET_R(stopColor)/255.0,
michael@0 249 NS_GET_G(stopColor)/255.0,
michael@0 250 NS_GET_B(stopColor)/255.0,
michael@0 251 NS_GET_A(stopColor)/255.0 *
michael@0 252 stopOpacity * aGraphicOpacity));
michael@0 253 return pattern.forget();
michael@0 254 }
michael@0 255
michael@0 256 // Get the transform list (if there is one). We do this after the returns
michael@0 257 // above since this call can be expensive when "gradientUnits" is set to
michael@0 258 // "objectBoundingBox" (since that requiring a GetBBox() call).
michael@0 259 gfxMatrix patternMatrix = GetGradientTransform(aSource, aOverrideBounds);
michael@0 260
michael@0 261 if (patternMatrix.IsSingular()) {
michael@0 262 return nullptr;
michael@0 263 }
michael@0 264
michael@0 265 // revert the vector effect transform so that the gradient appears unchanged
michael@0 266 if (aFillOrStroke == &nsStyleSVG::mStroke) {
michael@0 267 patternMatrix.Multiply(nsSVGUtils::GetStrokeTransform(aSource).Invert());
michael@0 268 }
michael@0 269
michael@0 270 patternMatrix.Invert();
michael@0 271
michael@0 272 nsRefPtr<gfxPattern> gradient = CreateGradient();
michael@0 273 if (!gradient || gradient->CairoStatus())
michael@0 274 return nullptr;
michael@0 275
michael@0 276 uint16_t aSpread = GetSpreadMethod();
michael@0 277 if (aSpread == SVG_SPREADMETHOD_PAD)
michael@0 278 gradient->SetExtend(gfxPattern::EXTEND_PAD);
michael@0 279 else if (aSpread == SVG_SPREADMETHOD_REFLECT)
michael@0 280 gradient->SetExtend(gfxPattern::EXTEND_REFLECT);
michael@0 281 else if (aSpread == SVG_SPREADMETHOD_REPEAT)
michael@0 282 gradient->SetExtend(gfxPattern::EXTEND_REPEAT);
michael@0 283
michael@0 284 gradient->SetMatrix(patternMatrix);
michael@0 285
michael@0 286 // setup stops
michael@0 287 float lastOffset = 0.0f;
michael@0 288
michael@0 289 for (uint32_t i = 0; i < nStops; i++) {
michael@0 290 float offset, stopOpacity;
michael@0 291 nscolor stopColor;
michael@0 292
michael@0 293 GetStopInformation(stopFrames[i], &offset, &stopColor, &stopOpacity);
michael@0 294
michael@0 295 if (offset < lastOffset)
michael@0 296 offset = lastOffset;
michael@0 297 else
michael@0 298 lastOffset = offset;
michael@0 299
michael@0 300 gradient->AddColorStop(offset,
michael@0 301 gfxRGBA(NS_GET_R(stopColor)/255.0,
michael@0 302 NS_GET_G(stopColor)/255.0,
michael@0 303 NS_GET_B(stopColor)/255.0,
michael@0 304 NS_GET_A(stopColor)/255.0 *
michael@0 305 stopOpacity * aGraphicOpacity));
michael@0 306 }
michael@0 307
michael@0 308 return gradient.forget();
michael@0 309 }
michael@0 310
michael@0 311 // Private (helper) methods
michael@0 312
michael@0 313 nsSVGGradientFrame *
michael@0 314 nsSVGGradientFrame::GetReferencedGradient()
michael@0 315 {
michael@0 316 if (mNoHRefURI)
michael@0 317 return nullptr;
michael@0 318
michael@0 319 nsSVGPaintingProperty *property = static_cast<nsSVGPaintingProperty*>
michael@0 320 (Properties().Get(nsSVGEffects::HrefProperty()));
michael@0 321
michael@0 322 if (!property) {
michael@0 323 // Fetch our gradient element's xlink:href attribute
michael@0 324 dom::SVGGradientElement*grad = static_cast<dom::SVGGradientElement*>(mContent);
michael@0 325 nsAutoString href;
michael@0 326 grad->mStringAttributes[dom::SVGGradientElement::HREF].GetAnimValue(href, grad);
michael@0 327 if (href.IsEmpty()) {
michael@0 328 mNoHRefURI = true;
michael@0 329 return nullptr; // no URL
michael@0 330 }
michael@0 331
michael@0 332 // Convert href to an nsIURI
michael@0 333 nsCOMPtr<nsIURI> targetURI;
michael@0 334 nsCOMPtr<nsIURI> base = mContent->GetBaseURI();
michael@0 335 nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(targetURI), href,
michael@0 336 mContent->GetCurrentDoc(), base);
michael@0 337
michael@0 338 property =
michael@0 339 nsSVGEffects::GetPaintingProperty(targetURI, this, nsSVGEffects::HrefProperty());
michael@0 340 if (!property)
michael@0 341 return nullptr;
michael@0 342 }
michael@0 343
michael@0 344 nsIFrame *result = property->GetReferencedFrame();
michael@0 345 if (!result)
michael@0 346 return nullptr;
michael@0 347
michael@0 348 nsIAtom* frameType = result->GetType();
michael@0 349 if (frameType != nsGkAtoms::svgLinearGradientFrame &&
michael@0 350 frameType != nsGkAtoms::svgRadialGradientFrame)
michael@0 351 return nullptr;
michael@0 352
michael@0 353 return static_cast<nsSVGGradientFrame*>(result);
michael@0 354 }
michael@0 355
michael@0 356 nsSVGGradientFrame *
michael@0 357 nsSVGGradientFrame::GetReferencedGradientIfNotInUse()
michael@0 358 {
michael@0 359 nsSVGGradientFrame *referenced = GetReferencedGradient();
michael@0 360 if (!referenced)
michael@0 361 return nullptr;
michael@0 362
michael@0 363 if (referenced->mLoopFlag) {
michael@0 364 // XXXjwatt: we should really send an error to the JavaScript Console here:
michael@0 365 NS_WARNING("gradient reference loop detected while inheriting attribute!");
michael@0 366 return nullptr;
michael@0 367 }
michael@0 368
michael@0 369 return referenced;
michael@0 370 }
michael@0 371
michael@0 372 void
michael@0 373 nsSVGGradientFrame::GetStopFrames(nsTArray<nsIFrame*>* aStopFrames)
michael@0 374 {
michael@0 375 nsIFrame *stopFrame = nullptr;
michael@0 376 for (stopFrame = mFrames.FirstChild(); stopFrame;
michael@0 377 stopFrame = stopFrame->GetNextSibling()) {
michael@0 378 if (stopFrame->GetType() == nsGkAtoms::svgStopFrame) {
michael@0 379 aStopFrames->AppendElement(stopFrame);
michael@0 380 }
michael@0 381 }
michael@0 382 if (aStopFrames->Length() > 0) {
michael@0 383 return;
michael@0 384 }
michael@0 385
michael@0 386 // Our gradient element doesn't have stops - try to "inherit" them
michael@0 387
michael@0 388 AutoGradientReferencer gradientRef(this);
michael@0 389 nsSVGGradientFrame* next = GetReferencedGradientIfNotInUse();
michael@0 390 if (!next) {
michael@0 391 return;
michael@0 392 }
michael@0 393
michael@0 394 return next->GetStopFrames(aStopFrames);
michael@0 395 }
michael@0 396
michael@0 397 // -------------------------------------------------------------------------
michael@0 398 // Linear Gradients
michael@0 399 // -------------------------------------------------------------------------
michael@0 400
michael@0 401 #ifdef DEBUG
michael@0 402 void
michael@0 403 nsSVGLinearGradientFrame::Init(nsIContent* aContent,
michael@0 404 nsIFrame* aParent,
michael@0 405 nsIFrame* aPrevInFlow)
michael@0 406 {
michael@0 407 NS_ASSERTION(aContent->IsSVG(nsGkAtoms::linearGradient),
michael@0 408 "Content is not an SVG linearGradient");
michael@0 409
michael@0 410 nsSVGLinearGradientFrameBase::Init(aContent, aParent, aPrevInFlow);
michael@0 411 }
michael@0 412 #endif /* DEBUG */
michael@0 413
michael@0 414 nsIAtom*
michael@0 415 nsSVGLinearGradientFrame::GetType() const
michael@0 416 {
michael@0 417 return nsGkAtoms::svgLinearGradientFrame;
michael@0 418 }
michael@0 419
michael@0 420 nsresult
michael@0 421 nsSVGLinearGradientFrame::AttributeChanged(int32_t aNameSpaceID,
michael@0 422 nsIAtom* aAttribute,
michael@0 423 int32_t aModType)
michael@0 424 {
michael@0 425 if (aNameSpaceID == kNameSpaceID_None &&
michael@0 426 (aAttribute == nsGkAtoms::x1 ||
michael@0 427 aAttribute == nsGkAtoms::y1 ||
michael@0 428 aAttribute == nsGkAtoms::x2 ||
michael@0 429 aAttribute == nsGkAtoms::y2)) {
michael@0 430 nsSVGEffects::InvalidateDirectRenderingObservers(this);
michael@0 431 }
michael@0 432
michael@0 433 return nsSVGGradientFrame::AttributeChanged(aNameSpaceID,
michael@0 434 aAttribute, aModType);
michael@0 435 }
michael@0 436
michael@0 437 //----------------------------------------------------------------------
michael@0 438
michael@0 439 float
michael@0 440 nsSVGLinearGradientFrame::GetLengthValue(uint32_t aIndex)
michael@0 441 {
michael@0 442 dom::SVGLinearGradientElement* lengthElement =
michael@0 443 GetLinearGradientWithLength(aIndex,
michael@0 444 static_cast<dom::SVGLinearGradientElement*>(mContent));
michael@0 445 // We passed in mContent as a fallback, so, assuming mContent is non-null, the
michael@0 446 // return value should also be non-null.
michael@0 447 NS_ABORT_IF_FALSE(lengthElement,
michael@0 448 "Got unexpected null element from GetLinearGradientWithLength");
michael@0 449 const nsSVGLength2 &length = lengthElement->mLengthAttributes[aIndex];
michael@0 450
michael@0 451 // Object bounding box units are handled by setting the appropriate
michael@0 452 // transform in GetGradientTransform, but we need to handle user
michael@0 453 // space units as part of the individual Get* routines. Fixes 323669.
michael@0 454
michael@0 455 uint16_t gradientUnits = GetGradientUnits();
michael@0 456 if (gradientUnits == SVG_UNIT_TYPE_USERSPACEONUSE) {
michael@0 457 return nsSVGUtils::UserSpace(mSource, &length);
michael@0 458 }
michael@0 459
michael@0 460 NS_ASSERTION(
michael@0 461 gradientUnits == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX,
michael@0 462 "Unknown gradientUnits type");
michael@0 463
michael@0 464 return length.GetAnimValue(static_cast<SVGSVGElement*>(nullptr));
michael@0 465 }
michael@0 466
michael@0 467 dom::SVGLinearGradientElement*
michael@0 468 nsSVGLinearGradientFrame::GetLinearGradientWithLength(uint32_t aIndex,
michael@0 469 dom::SVGLinearGradientElement* aDefault)
michael@0 470 {
michael@0 471 dom::SVGLinearGradientElement* thisElement =
michael@0 472 static_cast<dom::SVGLinearGradientElement*>(mContent);
michael@0 473 const nsSVGLength2 &length = thisElement->mLengthAttributes[aIndex];
michael@0 474
michael@0 475 if (length.IsExplicitlySet()) {
michael@0 476 return thisElement;
michael@0 477 }
michael@0 478
michael@0 479 return nsSVGLinearGradientFrameBase::GetLinearGradientWithLength(aIndex,
michael@0 480 aDefault);
michael@0 481 }
michael@0 482
michael@0 483 bool
michael@0 484 nsSVGLinearGradientFrame::GradientVectorLengthIsZero()
michael@0 485 {
michael@0 486 return GetLengthValue(dom::SVGLinearGradientElement::ATTR_X1) ==
michael@0 487 GetLengthValue(dom::SVGLinearGradientElement::ATTR_X2) &&
michael@0 488 GetLengthValue(dom::SVGLinearGradientElement::ATTR_Y1) ==
michael@0 489 GetLengthValue(dom::SVGLinearGradientElement::ATTR_Y2);
michael@0 490 }
michael@0 491
michael@0 492 already_AddRefed<gfxPattern>
michael@0 493 nsSVGLinearGradientFrame::CreateGradient()
michael@0 494 {
michael@0 495 float x1, y1, x2, y2;
michael@0 496
michael@0 497 x1 = GetLengthValue(dom::SVGLinearGradientElement::ATTR_X1);
michael@0 498 y1 = GetLengthValue(dom::SVGLinearGradientElement::ATTR_Y1);
michael@0 499 x2 = GetLengthValue(dom::SVGLinearGradientElement::ATTR_X2);
michael@0 500 y2 = GetLengthValue(dom::SVGLinearGradientElement::ATTR_Y2);
michael@0 501
michael@0 502 nsRefPtr<gfxPattern> pattern = new gfxPattern(x1, y1, x2, y2);
michael@0 503 return pattern.forget();
michael@0 504 }
michael@0 505
michael@0 506 // -------------------------------------------------------------------------
michael@0 507 // Radial Gradients
michael@0 508 // -------------------------------------------------------------------------
michael@0 509
michael@0 510 #ifdef DEBUG
michael@0 511 void
michael@0 512 nsSVGRadialGradientFrame::Init(nsIContent* aContent,
michael@0 513 nsIFrame* aParent,
michael@0 514 nsIFrame* aPrevInFlow)
michael@0 515 {
michael@0 516 NS_ASSERTION(aContent->IsSVG(nsGkAtoms::radialGradient),
michael@0 517 "Content is not an SVG radialGradient");
michael@0 518
michael@0 519 nsSVGRadialGradientFrameBase::Init(aContent, aParent, aPrevInFlow);
michael@0 520 }
michael@0 521 #endif /* DEBUG */
michael@0 522
michael@0 523 nsIAtom*
michael@0 524 nsSVGRadialGradientFrame::GetType() const
michael@0 525 {
michael@0 526 return nsGkAtoms::svgRadialGradientFrame;
michael@0 527 }
michael@0 528
michael@0 529 nsresult
michael@0 530 nsSVGRadialGradientFrame::AttributeChanged(int32_t aNameSpaceID,
michael@0 531 nsIAtom* aAttribute,
michael@0 532 int32_t aModType)
michael@0 533 {
michael@0 534 if (aNameSpaceID == kNameSpaceID_None &&
michael@0 535 (aAttribute == nsGkAtoms::r ||
michael@0 536 aAttribute == nsGkAtoms::cx ||
michael@0 537 aAttribute == nsGkAtoms::cy ||
michael@0 538 aAttribute == nsGkAtoms::fx ||
michael@0 539 aAttribute == nsGkAtoms::fy)) {
michael@0 540 nsSVGEffects::InvalidateDirectRenderingObservers(this);
michael@0 541 }
michael@0 542
michael@0 543 return nsSVGGradientFrame::AttributeChanged(aNameSpaceID,
michael@0 544 aAttribute, aModType);
michael@0 545 }
michael@0 546
michael@0 547 //----------------------------------------------------------------------
michael@0 548
michael@0 549 float
michael@0 550 nsSVGRadialGradientFrame::GetLengthValue(uint32_t aIndex)
michael@0 551 {
michael@0 552 dom::SVGRadialGradientElement* lengthElement =
michael@0 553 GetRadialGradientWithLength(aIndex,
michael@0 554 static_cast<dom::SVGRadialGradientElement*>(mContent));
michael@0 555 // We passed in mContent as a fallback, so, assuming mContent is non-null,
michael@0 556 // the return value should also be non-null.
michael@0 557 NS_ABORT_IF_FALSE(lengthElement,
michael@0 558 "Got unexpected null element from GetRadialGradientWithLength");
michael@0 559 return GetLengthValueFromElement(aIndex, *lengthElement);
michael@0 560 }
michael@0 561
michael@0 562 float
michael@0 563 nsSVGRadialGradientFrame::GetLengthValue(uint32_t aIndex, float aDefaultValue)
michael@0 564 {
michael@0 565 dom::SVGRadialGradientElement* lengthElement =
michael@0 566 GetRadialGradientWithLength(aIndex, nullptr);
michael@0 567
michael@0 568 return lengthElement ? GetLengthValueFromElement(aIndex, *lengthElement)
michael@0 569 : aDefaultValue;
michael@0 570 }
michael@0 571
michael@0 572 float
michael@0 573 nsSVGRadialGradientFrame::GetLengthValueFromElement(uint32_t aIndex,
michael@0 574 dom::SVGRadialGradientElement& aElement)
michael@0 575 {
michael@0 576 const nsSVGLength2 &length = aElement.mLengthAttributes[aIndex];
michael@0 577
michael@0 578 // Object bounding box units are handled by setting the appropriate
michael@0 579 // transform in GetGradientTransform, but we need to handle user
michael@0 580 // space units as part of the individual Get* routines. Fixes 323669.
michael@0 581
michael@0 582 uint16_t gradientUnits = GetGradientUnits();
michael@0 583 if (gradientUnits == SVG_UNIT_TYPE_USERSPACEONUSE) {
michael@0 584 return nsSVGUtils::UserSpace(mSource, &length);
michael@0 585 }
michael@0 586
michael@0 587 NS_ASSERTION(
michael@0 588 gradientUnits == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX,
michael@0 589 "Unknown gradientUnits type");
michael@0 590
michael@0 591 return length.GetAnimValue(static_cast<SVGSVGElement*>(nullptr));
michael@0 592 }
michael@0 593
michael@0 594 dom::SVGRadialGradientElement*
michael@0 595 nsSVGRadialGradientFrame::GetRadialGradientWithLength(uint32_t aIndex,
michael@0 596 dom::SVGRadialGradientElement* aDefault)
michael@0 597 {
michael@0 598 dom::SVGRadialGradientElement* thisElement =
michael@0 599 static_cast<dom::SVGRadialGradientElement*>(mContent);
michael@0 600 const nsSVGLength2 &length = thisElement->mLengthAttributes[aIndex];
michael@0 601
michael@0 602 if (length.IsExplicitlySet()) {
michael@0 603 return thisElement;
michael@0 604 }
michael@0 605
michael@0 606 return nsSVGRadialGradientFrameBase::GetRadialGradientWithLength(aIndex,
michael@0 607 aDefault);
michael@0 608 }
michael@0 609
michael@0 610 bool
michael@0 611 nsSVGRadialGradientFrame::GradientVectorLengthIsZero()
michael@0 612 {
michael@0 613 return GetLengthValue(dom::SVGRadialGradientElement::ATTR_R) == 0;
michael@0 614 }
michael@0 615
michael@0 616 already_AddRefed<gfxPattern>
michael@0 617 nsSVGRadialGradientFrame::CreateGradient()
michael@0 618 {
michael@0 619 float cx, cy, r, fx, fy;
michael@0 620
michael@0 621 cx = GetLengthValue(dom::SVGRadialGradientElement::ATTR_CX);
michael@0 622 cy = GetLengthValue(dom::SVGRadialGradientElement::ATTR_CY);
michael@0 623 r = GetLengthValue(dom::SVGRadialGradientElement::ATTR_R);
michael@0 624 // If fx or fy are not set, use cx/cy instead
michael@0 625 fx = GetLengthValue(dom::SVGRadialGradientElement::ATTR_FX, cx);
michael@0 626 fy = GetLengthValue(dom::SVGRadialGradientElement::ATTR_FY, cy);
michael@0 627
michael@0 628 if (fx != cx || fy != cy) {
michael@0 629 // The focal point (fFx and fFy) must be clamped to be *inside* - not on -
michael@0 630 // the circumference of the gradient or we'll get rendering anomalies. We
michael@0 631 // calculate the distance from the focal point to the gradient center and
michael@0 632 // make sure it is *less* than the gradient radius.
michael@0 633 // 1/128 is the limit of the fractional part of cairo's 24.8 fixed point
michael@0 634 // representation divided by 2 to ensure that we get different cairo
michael@0 635 // fractions
michael@0 636 double dMax = std::max(0.0, r - 1.0/128);
michael@0 637 float dx = fx - cx;
michael@0 638 float dy = fy - cy;
michael@0 639 double d = sqrt((dx * dx) + (dy * dy));
michael@0 640 if (d > dMax) {
michael@0 641 double angle = atan2(dy, dx);
michael@0 642 fx = (float)(dMax * cos(angle)) + cx;
michael@0 643 fy = (float)(dMax * sin(angle)) + cy;
michael@0 644 }
michael@0 645 }
michael@0 646
michael@0 647 nsRefPtr<gfxPattern> pattern = new gfxPattern(fx, fy, 0, cx, cy, r);
michael@0 648 return pattern.forget();
michael@0 649 }
michael@0 650
michael@0 651 // -------------------------------------------------------------------------
michael@0 652 // Public functions
michael@0 653 // -------------------------------------------------------------------------
michael@0 654
michael@0 655 nsIFrame*
michael@0 656 NS_NewSVGLinearGradientFrame(nsIPresShell* aPresShell,
michael@0 657 nsStyleContext* aContext)
michael@0 658 {
michael@0 659 return new (aPresShell) nsSVGLinearGradientFrame(aContext);
michael@0 660 }
michael@0 661
michael@0 662 NS_IMPL_FRAMEARENA_HELPERS(nsSVGLinearGradientFrame)
michael@0 663
michael@0 664 nsIFrame*
michael@0 665 NS_NewSVGRadialGradientFrame(nsIPresShell* aPresShell,
michael@0 666 nsStyleContext* aContext)
michael@0 667 {
michael@0 668 return new (aPresShell) nsSVGRadialGradientFrame(aContext);
michael@0 669 }
michael@0 670
michael@0 671 NS_IMPL_FRAMEARENA_HELPERS(nsSVGRadialGradientFrame)

mercurial