2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-09-22 23:26:05 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
// Main header first:
|
|
|
|
// This is also necessary to ensure our definition of M_SQRT1_2 is picked up
|
|
|
|
#include "SVGContentUtils.h"
|
|
|
|
|
|
|
|
// Keep others in (case-insensitive) order:
|
2014-07-06 00:53:04 +04:00
|
|
|
#include "gfx2DGlue.h"
|
2012-09-22 23:26:05 +04:00
|
|
|
#include "gfxMatrix.h"
|
2014-07-06 00:53:04 +04:00
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#include "mozilla/gfx/2D.h"
|
2013-10-01 11:50:40 +04:00
|
|
|
#include "mozilla/dom/SVGSVGElement.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2016-07-22 14:07:39 +03:00
|
|
|
#include "mozilla/SVGContextPaint.h"
|
2018-08-31 18:55:04 +03:00
|
|
|
#include "mozilla/TextUtils.h"
|
2012-09-22 23:26:05 +04:00
|
|
|
#include "nsComputedDOMStyle.h"
|
|
|
|
#include "nsFontMetrics.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsLayoutUtils.h"
|
2016-12-23 20:28:13 +03:00
|
|
|
#include "nsMathUtils.h"
|
2013-01-06 13:32:02 +04:00
|
|
|
#include "SVGAnimationElement.h"
|
2012-09-22 23:26:05 +04:00
|
|
|
#include "SVGAnimatedPreserveAspectRatio.h"
|
2013-02-01 03:11:49 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-10-08 03:15:59 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2014-03-24 17:31:19 +04:00
|
|
|
#include "mozilla/gfx/Types.h"
|
2014-10-01 13:26:00 +04:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
2018-03-22 21:20:41 +03:00
|
|
|
#include "mozilla/ComputedStyle.h"
|
2014-03-24 17:31:19 +04:00
|
|
|
#include "nsSVGPathDataParser.h"
|
|
|
|
#include "SVGPathData.h"
|
2014-07-06 00:53:04 +04:00
|
|
|
#include "SVGPathElement.h"
|
2012-09-22 23:26:05 +04:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
2018-06-26 00:20:54 +03:00
|
|
|
using namespace mozilla::dom::SVGPreserveAspectRatio_Binding;
|
2014-07-06 00:53:04 +04:00
|
|
|
using namespace mozilla::gfx;
|
2012-09-22 23:26:05 +04:00
|
|
|
|
2018-12-20 10:58:08 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2012-09-22 23:26:05 +04:00
|
|
|
SVGSVGElement* SVGContentUtils::GetOuterSVGElement(nsSVGElement* aSVGElement) {
|
2018-07-17 20:32:47 +03:00
|
|
|
Element* element = nullptr;
|
|
|
|
Element* ancestor = aSVGElement->GetParentElementCrossingShadowRoot();
|
2012-09-22 23:26:05 +04:00
|
|
|
|
2015-03-03 14:08:59 +03:00
|
|
|
while (ancestor && ancestor->IsSVGElement() &&
|
2015-03-03 14:09:00 +03:00
|
|
|
!ancestor->IsSVGElement(nsGkAtoms::foreignObject)) {
|
2012-09-22 23:26:05 +04:00
|
|
|
element = ancestor;
|
2018-07-17 20:32:47 +03:00
|
|
|
ancestor = element->GetParentElementCrossingShadowRoot();
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
|
2015-03-03 14:09:00 +03:00
|
|
|
if (element && element->IsSVGElement(nsGkAtoms::svg)) {
|
2013-01-10 03:02:45 +04:00
|
|
|
return static_cast<SVGSVGElement*>(element);
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SVGContentUtils::ActivateByHyperlink(nsIContent* aContent) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aContent->IsNodeOfType(nsINode::eANIMATION),
|
|
|
|
"Expecting an animation element");
|
2012-09-22 23:26:05 +04:00
|
|
|
|
2013-01-06 13:32:02 +04:00
|
|
|
static_cast<SVGAnimationElement*>(aContent)->ActivateByHyperlink();
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
|
2014-07-06 00:53:04 +04:00
|
|
|
enum DashState {
|
|
|
|
eDashedStroke,
|
|
|
|
eContinuousStroke, //< all dashes, no gaps
|
|
|
|
eNoStroke //< all gaps, no dashes
|
|
|
|
};
|
|
|
|
|
|
|
|
static DashState GetStrokeDashData(
|
|
|
|
SVGContentUtils::AutoStrokeOptions* aStrokeOptions, nsSVGElement* aElement,
|
2016-07-22 16:56:09 +03:00
|
|
|
const nsStyleSVG* aStyleSVG, SVGContextPaint* aContextPaint) {
|
2014-07-06 00:53:04 +04:00
|
|
|
size_t dashArrayLength;
|
|
|
|
Float totalLengthOfDashes = 0.0, totalLengthOfGaps = 0.0;
|
2015-03-06 17:12:52 +03:00
|
|
|
Float pathScale = 1.0;
|
2014-07-06 00:53:04 +04:00
|
|
|
|
2016-06-03 10:16:39 +03:00
|
|
|
if (aContextPaint && aStyleSVG->StrokeDasharrayFromObject()) {
|
2018-03-11 19:11:24 +03:00
|
|
|
const FallibleTArray<Float>& dashSrc = aContextPaint->GetStrokeDashArray();
|
2014-07-06 00:53:04 +04:00
|
|
|
dashArrayLength = dashSrc.Length();
|
|
|
|
if (dashArrayLength <= 0) {
|
|
|
|
return eContinuousStroke;
|
|
|
|
}
|
|
|
|
Float* dashPattern = aStrokeOptions->InitDashPattern(dashArrayLength);
|
|
|
|
if (!dashPattern) {
|
|
|
|
return eContinuousStroke;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < dashArrayLength; i++) {
|
|
|
|
if (dashSrc[i] < 0.0) {
|
|
|
|
return eContinuousStroke; // invalid
|
|
|
|
}
|
|
|
|
dashPattern[i] = Float(dashSrc[i]);
|
|
|
|
(i % 2 ? totalLengthOfGaps : totalLengthOfDashes) += dashSrc[i];
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-26 09:01:58 +03:00
|
|
|
const nsTArray<nsStyleCoord>& dasharray = aStyleSVG->mStrokeDasharray;
|
|
|
|
dashArrayLength = aStyleSVG->mStrokeDasharray.Length();
|
2014-07-06 00:53:04 +04:00
|
|
|
if (dashArrayLength <= 0) {
|
|
|
|
return eContinuousStroke;
|
|
|
|
}
|
2018-03-13 22:52:03 +03:00
|
|
|
if (aElement->IsNodeOfType(nsINode::eSHAPE)) {
|
|
|
|
pathScale =
|
|
|
|
static_cast<SVGGeometryElement*>(aElement)->GetPathLengthScale(
|
|
|
|
SVGGeometryElement::eForStroking);
|
2014-07-06 00:53:04 +04:00
|
|
|
if (pathScale <= 0) {
|
|
|
|
return eContinuousStroke;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Float* dashPattern = aStrokeOptions->InitDashPattern(dashArrayLength);
|
|
|
|
if (!dashPattern) {
|
|
|
|
return eContinuousStroke;
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < dashArrayLength; i++) {
|
|
|
|
Float dashLength =
|
|
|
|
SVGContentUtils::CoordToFloat(aElement, dasharray[i]) * pathScale;
|
|
|
|
if (dashLength < 0.0) {
|
|
|
|
return eContinuousStroke; // invalid
|
|
|
|
}
|
|
|
|
dashPattern[i] = dashLength;
|
|
|
|
(i % 2 ? totalLengthOfGaps : totalLengthOfDashes) += dashLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:08:13 +04:00
|
|
|
// Now that aStrokeOptions.mDashPattern is fully initialized (we didn't
|
|
|
|
// return early above) we can safely set mDashLength:
|
2014-07-06 00:53:04 +04:00
|
|
|
aStrokeOptions->mDashLength = dashArrayLength;
|
|
|
|
|
2014-09-30 21:08:13 +04:00
|
|
|
if ((dashArrayLength % 2) == 1) {
|
|
|
|
// If we have a dash pattern with an odd number of lengths the pattern
|
|
|
|
// repeats a second time, per the SVG spec., and as implemented by Moz2D.
|
|
|
|
// When deciding whether to return eNoStroke or eContinuousStroke below we
|
|
|
|
// need to take into account that in the repeat pattern the dashes become
|
|
|
|
// gaps, and the gaps become dashes.
|
|
|
|
Float origTotalLengthOfDashes = totalLengthOfDashes;
|
|
|
|
totalLengthOfDashes += totalLengthOfGaps;
|
|
|
|
totalLengthOfGaps += origTotalLengthOfDashes;
|
|
|
|
}
|
|
|
|
|
2015-01-11 01:17:57 +03:00
|
|
|
// Stroking using dashes is much slower than stroking a continuous line
|
|
|
|
// (see bug 609361 comment 40), and much, much slower than not stroking the
|
|
|
|
// line at all. Here we check for cases when the dash pattern causes the
|
|
|
|
// stroke to essentially be continuous or to be nonexistent in which case
|
|
|
|
// we can avoid expensive stroking operations (the underlying platform
|
|
|
|
// graphics libraries don't seem to optimize for this).
|
|
|
|
if (totalLengthOfGaps <= 0) {
|
2014-07-06 00:53:04 +04:00
|
|
|
return eContinuousStroke;
|
|
|
|
}
|
2015-01-11 01:17:57 +03:00
|
|
|
// We can only return eNoStroke if the value of stroke-linecap isn't
|
|
|
|
// adding caps to zero length dashes.
|
|
|
|
if (totalLengthOfDashes <= 0 &&
|
|
|
|
aStyleSVG->mStrokeLinecap == NS_STYLE_STROKE_LINECAP_BUTT) {
|
|
|
|
return eNoStroke;
|
|
|
|
}
|
2014-07-06 00:53:04 +04:00
|
|
|
|
2016-06-03 10:16:39 +03:00
|
|
|
if (aContextPaint && aStyleSVG->StrokeDashoffsetFromObject()) {
|
2014-07-06 00:53:04 +04:00
|
|
|
aStrokeOptions->mDashOffset = Float(aContextPaint->GetStrokeDashOffset());
|
|
|
|
} else {
|
|
|
|
aStrokeOptions->mDashOffset =
|
2015-03-06 17:12:52 +03:00
|
|
|
SVGContentUtils::CoordToFloat(aElement, aStyleSVG->mStrokeDashoffset) *
|
|
|
|
pathScale;
|
2014-07-06 00:53:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return eDashedStroke;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SVGContentUtils::GetStrokeOptions(AutoStrokeOptions* aStrokeOptions,
|
|
|
|
nsSVGElement* aElement,
|
2018-03-22 21:20:41 +03:00
|
|
|
ComputedStyle* aComputedStyle,
|
2016-07-22 16:56:09 +03:00
|
|
|
SVGContextPaint* aContextPaint,
|
2014-10-03 12:50:43 +04:00
|
|
|
StrokeOptionFlags aFlags) {
|
2018-03-22 16:49:21 +03:00
|
|
|
RefPtr<ComputedStyle> computedStyle;
|
2018-03-22 21:20:41 +03:00
|
|
|
if (aComputedStyle) {
|
2018-03-22 16:49:21 +03:00
|
|
|
computedStyle = aComputedStyle;
|
2014-07-06 00:53:04 +04:00
|
|
|
} else {
|
2018-03-22 16:49:21 +03:00
|
|
|
computedStyle =
|
2018-03-22 21:20:41 +03:00
|
|
|
nsComputedDOMStyle::GetComputedStyleNoFlush(aElement, nullptr);
|
2014-07-06 00:53:04 +04:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:49:21 +03:00
|
|
|
if (!computedStyle) {
|
2014-07-06 00:53:04 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:49:21 +03:00
|
|
|
const nsStyleSVG* styleSVG = computedStyle->StyleSVG();
|
2014-07-06 00:53:04 +04:00
|
|
|
|
2016-01-26 21:30:36 +03:00
|
|
|
bool checkedDashAndStrokeIsDashed = false;
|
2014-10-03 12:50:43 +04:00
|
|
|
if (aFlags != eIgnoreStrokeDashing) {
|
|
|
|
DashState dashState =
|
|
|
|
GetStrokeDashData(aStrokeOptions, aElement, styleSVG, aContextPaint);
|
2014-07-06 00:53:04 +04:00
|
|
|
|
2014-10-03 12:50:43 +04:00
|
|
|
if (dashState == eNoStroke) {
|
|
|
|
// Hopefully this will shortcircuit any stroke operations:
|
|
|
|
aStrokeOptions->mLineWidth = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (dashState == eContinuousStroke && aStrokeOptions->mDashPattern) {
|
|
|
|
// Prevent our caller from wasting time looking at a pattern without gaps:
|
|
|
|
aStrokeOptions->DiscardDashPattern();
|
|
|
|
}
|
2016-01-26 21:30:36 +03:00
|
|
|
checkedDashAndStrokeIsDashed = (dashState == eDashedStroke);
|
2014-07-06 00:53:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aStrokeOptions->mLineWidth =
|
2018-03-22 16:49:21 +03:00
|
|
|
GetStrokeWidth(aElement, computedStyle, aContextPaint);
|
2014-07-06 00:53:04 +04:00
|
|
|
|
|
|
|
aStrokeOptions->mMiterLimit = Float(styleSVG->mStrokeMiterlimit);
|
|
|
|
|
|
|
|
switch (styleSVG->mStrokeLinejoin) {
|
|
|
|
case NS_STYLE_STROKE_LINEJOIN_MITER:
|
2014-12-16 18:31:28 +03:00
|
|
|
aStrokeOptions->mLineJoin = JoinStyle::MITER_OR_BEVEL;
|
2014-07-06 00:53:04 +04:00
|
|
|
break;
|
|
|
|
case NS_STYLE_STROKE_LINEJOIN_ROUND:
|
|
|
|
aStrokeOptions->mLineJoin = JoinStyle::ROUND;
|
|
|
|
break;
|
|
|
|
case NS_STYLE_STROKE_LINEJOIN_BEVEL:
|
|
|
|
aStrokeOptions->mLineJoin = JoinStyle::BEVEL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-26 21:30:36 +03:00
|
|
|
if (ShapeTypeHasNoCorners(aElement) && !checkedDashAndStrokeIsDashed) {
|
|
|
|
// Note: if aFlags == eIgnoreStrokeDashing then we may be returning the
|
|
|
|
// wrong linecap value here, since the actual linecap used on render in this
|
|
|
|
// case depends on whether the stroke is dashed or not.
|
2014-07-06 00:53:04 +04:00
|
|
|
aStrokeOptions->mLineCap = CapStyle::BUTT;
|
2016-01-26 21:30:36 +03:00
|
|
|
} else {
|
2015-06-29 21:19:00 +03:00
|
|
|
switch (styleSVG->mStrokeLinecap) {
|
|
|
|
case NS_STYLE_STROKE_LINECAP_BUTT:
|
|
|
|
aStrokeOptions->mLineCap = CapStyle::BUTT;
|
|
|
|
break;
|
|
|
|
case NS_STYLE_STROKE_LINECAP_ROUND:
|
|
|
|
aStrokeOptions->mLineCap = CapStyle::ROUND;
|
|
|
|
break;
|
|
|
|
case NS_STYLE_STROKE_LINECAP_SQUARE:
|
|
|
|
aStrokeOptions->mLineCap = CapStyle::SQUARE;
|
|
|
|
break;
|
|
|
|
}
|
2014-07-06 00:53:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Float SVGContentUtils::GetStrokeWidth(nsSVGElement* aElement,
|
2018-03-22 21:20:41 +03:00
|
|
|
ComputedStyle* aComputedStyle,
|
2016-07-22 16:56:09 +03:00
|
|
|
SVGContextPaint* aContextPaint) {
|
2018-03-22 16:49:21 +03:00
|
|
|
RefPtr<ComputedStyle> computedStyle;
|
2018-03-22 21:20:41 +03:00
|
|
|
if (aComputedStyle) {
|
2018-03-22 16:49:21 +03:00
|
|
|
computedStyle = aComputedStyle;
|
2014-07-06 00:53:04 +04:00
|
|
|
} else {
|
2018-03-22 16:49:21 +03:00
|
|
|
computedStyle =
|
2018-03-22 21:20:41 +03:00
|
|
|
nsComputedDOMStyle::GetComputedStyleNoFlush(aElement, nullptr);
|
2014-07-06 00:53:04 +04:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:49:21 +03:00
|
|
|
if (!computedStyle) {
|
2014-07-06 00:53:04 +04:00
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:49:21 +03:00
|
|
|
const nsStyleSVG* styleSVG = computedStyle->StyleSVG();
|
2014-07-06 00:53:04 +04:00
|
|
|
|
2016-06-03 10:16:39 +03:00
|
|
|
if (aContextPaint && styleSVG->StrokeWidthFromObject()) {
|
2014-07-06 00:53:04 +04:00
|
|
|
return aContextPaint->GetStrokeWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
return SVGContentUtils::CoordToFloat(aElement, styleSVG->mStrokeWidth);
|
|
|
|
}
|
|
|
|
|
2018-04-06 15:52:12 +03:00
|
|
|
float SVGContentUtils::GetFontSize(Element* aElement) {
|
|
|
|
if (!aElement) {
|
2012-09-22 23:26:05 +04:00
|
|
|
return 1.0f;
|
2018-04-06 15:52:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPresContext* pc = nsContentUtils::GetContextForContent(aElement);
|
|
|
|
if (!pc) {
|
|
|
|
return 1.0f;
|
|
|
|
}
|
2012-09-22 23:26:05 +04:00
|
|
|
|
2018-03-22 16:49:21 +03:00
|
|
|
RefPtr<ComputedStyle> computedStyle =
|
2018-03-22 21:20:41 +03:00
|
|
|
nsComputedDOMStyle::GetComputedStyleNoFlush(aElement, nullptr);
|
2018-03-22 16:49:21 +03:00
|
|
|
if (!computedStyle) {
|
2012-09-22 23:26:05 +04:00
|
|
|
// ReportToConsole
|
2018-03-23 16:49:21 +03:00
|
|
|
NS_WARNING("Couldn't get ComputedStyle for content in GetFontStyle");
|
2012-09-22 23:26:05 +04:00
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
2018-04-06 15:52:12 +03:00
|
|
|
return GetFontSize(computedStyle, pc);
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
|
2018-04-06 15:52:12 +03:00
|
|
|
float SVGContentUtils::GetFontSize(nsIFrame* aFrame) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aFrame, "NULL frame in GetFontSize");
|
2018-04-06 15:52:12 +03:00
|
|
|
return GetFontSize(aFrame->Style(), aFrame->PresContext());
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
|
2018-04-06 15:52:12 +03:00
|
|
|
float SVGContentUtils::GetFontSize(ComputedStyle* aComputedStyle,
|
|
|
|
nsPresContext* aPresContext) {
|
|
|
|
MOZ_ASSERT(aComputedStyle);
|
|
|
|
MOZ_ASSERT(aPresContext);
|
2012-09-22 23:26:05 +04:00
|
|
|
|
2018-03-22 21:20:41 +03:00
|
|
|
nscoord fontSize = aComputedStyle->StyleFont()->mSize;
|
2017-02-25 15:22:52 +03:00
|
|
|
return nsPresContext::AppUnitsToFloatCSSPixels(fontSize) /
|
2018-04-06 15:52:12 +03:00
|
|
|
aPresContext->EffectiveTextZoom();
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
|
2018-04-06 15:52:12 +03:00
|
|
|
float SVGContentUtils::GetFontXHeight(Element* aElement) {
|
|
|
|
if (!aElement) {
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPresContext* pc = nsContentUtils::GetContextForContent(aElement);
|
|
|
|
if (!pc) {
|
2012-09-22 23:26:05 +04:00
|
|
|
return 1.0f;
|
2018-04-06 15:52:12 +03:00
|
|
|
}
|
2012-09-22 23:26:05 +04:00
|
|
|
|
2018-04-06 15:52:12 +03:00
|
|
|
RefPtr<ComputedStyle> style =
|
2018-03-22 21:20:41 +03:00
|
|
|
nsComputedDOMStyle::GetComputedStyleNoFlush(aElement, nullptr);
|
2018-04-06 15:52:12 +03:00
|
|
|
if (!style) {
|
2012-09-22 23:26:05 +04:00
|
|
|
// ReportToConsole
|
2018-03-23 16:49:21 +03:00
|
|
|
NS_WARNING("Couldn't get ComputedStyle for content in GetFontStyle");
|
2012-09-22 23:26:05 +04:00
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
2018-04-06 15:52:12 +03:00
|
|
|
return GetFontXHeight(style, pc);
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
2017-04-05 08:39:23 +03:00
|
|
|
|
2012-09-22 23:26:05 +04:00
|
|
|
float SVGContentUtils::GetFontXHeight(nsIFrame* aFrame) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aFrame, "NULL frame in GetFontXHeight");
|
2018-04-06 15:52:12 +03:00
|
|
|
return GetFontXHeight(aFrame->Style(), aFrame->PresContext());
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
|
2018-04-06 15:52:12 +03:00
|
|
|
float SVGContentUtils::GetFontXHeight(ComputedStyle* aComputedStyle,
|
|
|
|
nsPresContext* aPresContext) {
|
|
|
|
MOZ_ASSERT(aComputedStyle && aPresContext);
|
2012-09-22 23:26:05 +04:00
|
|
|
|
2016-03-17 08:55:48 +03:00
|
|
|
RefPtr<nsFontMetrics> fontMetrics =
|
2018-04-06 15:52:12 +03:00
|
|
|
nsLayoutUtils::GetFontMetricsForComputedStyle(aComputedStyle,
|
|
|
|
aPresContext);
|
2012-09-22 23:26:05 +04:00
|
|
|
|
|
|
|
if (!fontMetrics) {
|
|
|
|
// ReportToConsole
|
|
|
|
NS_WARNING("no FontMetrics in GetFontXHeight()");
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
nscoord xHeight = fontMetrics->XHeight();
|
|
|
|
return nsPresContext::AppUnitsToFloatCSSPixels(xHeight) /
|
2018-04-06 15:52:12 +03:00
|
|
|
aPresContext->EffectiveTextZoom();
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
nsresult SVGContentUtils::ReportToConsole(nsIDocument* doc,
|
|
|
|
const char* aWarning,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t** aParams,
|
2012-09-22 23:26:05 +04:00
|
|
|
uint32_t aParamsLength) {
|
|
|
|
return nsContentUtils::ReportToConsole(
|
2013-08-21 23:28:26 +04:00
|
|
|
nsIScriptError::warningFlag, NS_LITERAL_CSTRING("SVG"), doc,
|
2012-09-22 23:26:05 +04:00
|
|
|
nsContentUtils::eSVG_PROPERTIES, aWarning, aParams, aParamsLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SVGContentUtils::EstablishesViewport(nsIContent* aContent) {
|
|
|
|
// Although SVG 1.1 states that <image> is an element that establishes a
|
|
|
|
// viewport, this is really only for the document it references, not
|
|
|
|
// for any child content, which is what this function is used for.
|
2015-03-03 14:09:00 +03:00
|
|
|
return aContent &&
|
|
|
|
aContent->IsAnyOfSVGElements(nsGkAtoms::svg, nsGkAtoms::foreignObject,
|
|
|
|
nsGkAtoms::symbol);
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
|
2017-10-10 09:58:34 +03:00
|
|
|
SVGViewportElement* SVGContentUtils::GetNearestViewportElement(
|
|
|
|
const nsIContent* aContent) {
|
2012-09-22 23:26:05 +04:00
|
|
|
nsIContent* element = aContent->GetFlattenedTreeParent();
|
|
|
|
|
2015-03-03 14:08:59 +03:00
|
|
|
while (element && element->IsSVGElement()) {
|
2012-09-22 23:26:05 +04:00
|
|
|
if (EstablishesViewport(element)) {
|
2015-03-03 14:09:00 +03:00
|
|
|
if (element->IsSVGElement(nsGkAtoms::foreignObject)) {
|
2012-09-22 23:26:05 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-10-10 09:58:34 +03:00
|
|
|
MOZ_ASSERT(element->IsAnyOfSVGElements(nsGkAtoms::svg, nsGkAtoms::symbol),
|
|
|
|
"upcoming static_cast is only valid for "
|
|
|
|
"SVGViewportElement subclasses");
|
|
|
|
return static_cast<SVGViewportElement*>(element);
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
element = element->GetFlattenedTreeParent();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gfx::Matrix GetCTMInternal(nsSVGElement* aElement, bool aScreenCTM,
|
|
|
|
bool aHaveRecursed) {
|
2013-12-27 00:13:57 +04:00
|
|
|
gfxMatrix matrix = aElement->PrependLocalTransformsTo(
|
2015-12-03 01:36:23 +03:00
|
|
|
gfxMatrix(), aHaveRecursed ? eAllTransforms : eUserSpaceToParent);
|
2012-09-22 23:26:05 +04:00
|
|
|
nsSVGElement* element = aElement;
|
|
|
|
nsIContent* ancestor = aElement->GetFlattenedTreeParent();
|
|
|
|
|
2015-03-03 14:08:59 +03:00
|
|
|
while (ancestor && ancestor->IsSVGElement() &&
|
2015-03-03 14:09:00 +03:00
|
|
|
!ancestor->IsSVGElement(nsGkAtoms::foreignObject)) {
|
2012-09-22 23:26:05 +04:00
|
|
|
element = static_cast<nsSVGElement*>(ancestor);
|
2013-12-27 00:13:57 +04:00
|
|
|
matrix *= element->PrependLocalTransformsTo(gfxMatrix()); // i.e. *A*ppend
|
2012-09-22 23:26:05 +04:00
|
|
|
if (!aScreenCTM && SVGContentUtils::EstablishesViewport(element)) {
|
|
|
|
if (!element->NodeInfo()->Equals(nsGkAtoms::svg, kNameSpaceID_SVG) &&
|
|
|
|
!element->NodeInfo()->Equals(nsGkAtoms::symbol, kNameSpaceID_SVG)) {
|
|
|
|
NS_ERROR("New (SVG > 1.1) SVG viewport establishing element?");
|
2013-12-31 22:44:27 +04:00
|
|
|
return gfx::Matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0); // singular
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
// XXX spec seems to say x,y translation should be undone for IsInnerSVG
|
2013-12-31 22:44:27 +04:00
|
|
|
return gfx::ToMatrix(matrix);
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
ancestor = ancestor->GetFlattenedTreeParent();
|
|
|
|
}
|
|
|
|
if (!aScreenCTM) {
|
|
|
|
// didn't find a nearestViewportElement
|
2013-12-31 22:44:27 +04:00
|
|
|
return gfx::Matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0); // singular
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
2015-03-03 14:09:00 +03:00
|
|
|
if (!element->IsSVGElement(nsGkAtoms::svg)) {
|
2012-09-22 23:26:05 +04:00
|
|
|
// Not a valid SVG fragment
|
2013-12-31 22:44:27 +04:00
|
|
|
return gfx::Matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0); // singular
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
if (element == aElement && !aHaveRecursed) {
|
|
|
|
// We get here when getScreenCTM() is called on an outer-<svg>.
|
|
|
|
// Consistency with other elements would have us include only the
|
|
|
|
// eFromUserSpace transforms, but we include the eAllTransforms
|
|
|
|
// transforms in this case since that's what we've been doing for
|
|
|
|
// a while, and it keeps us consistent with WebKit and Opera (if not
|
|
|
|
// really with the ambiguous spec).
|
2013-12-27 00:13:57 +04:00
|
|
|
matrix = aElement->PrependLocalTransformsTo(gfxMatrix());
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
if (!ancestor || !ancestor->IsElement()) {
|
2013-12-31 22:44:27 +04:00
|
|
|
return gfx::ToMatrix(matrix);
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
2015-03-03 14:08:59 +03:00
|
|
|
if (ancestor->IsSVGElement()) {
|
2013-12-31 22:44:27 +04:00
|
|
|
return gfx::ToMatrix(matrix) *
|
|
|
|
GetCTMInternal(static_cast<nsSVGElement*>(ancestor), true, true);
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX this does not take into account CSS transform, or that the non-SVG
|
|
|
|
// content that we've hit may itself be inside an SVG foreignObject higher up
|
2014-10-03 16:32:26 +04:00
|
|
|
nsIDocument* currentDoc = aElement->GetComposedDoc();
|
2012-09-22 23:26:05 +04:00
|
|
|
float x = 0.0f, y = 0.0f;
|
|
|
|
if (currentDoc &&
|
|
|
|
element->NodeInfo()->Equals(nsGkAtoms::svg, kNameSpaceID_SVG)) {
|
|
|
|
nsIPresShell* presShell = currentDoc->GetShell();
|
|
|
|
if (presShell) {
|
|
|
|
nsIFrame* frame = element->GetPrimaryFrame();
|
|
|
|
nsIFrame* ancestorFrame = presShell->GetRootFrame();
|
|
|
|
if (frame && ancestorFrame) {
|
|
|
|
nsPoint point = frame->GetOffsetTo(ancestorFrame);
|
|
|
|
x = nsPresContext::AppUnitsToFloatCSSPixels(point.x);
|
|
|
|
y = nsPresContext::AppUnitsToFloatCSSPixels(point.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 17:26:12 +04:00
|
|
|
return ToMatrix(matrix).PostTranslate(x, y);
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Matrix SVGContentUtils::GetCTM(nsSVGElement* aElement, bool aScreenCTM) {
|
|
|
|
return GetCTMInternal(aElement, aScreenCTM, false);
|
|
|
|
}
|
|
|
|
|
2015-09-01 07:17:00 +03:00
|
|
|
void SVGContentUtils::RectilinearGetStrokeBounds(
|
|
|
|
const Rect& aRect, const Matrix& aToBoundsSpace,
|
|
|
|
const Matrix& aToNonScalingStrokeSpace, float aStrokeWidth, Rect* aBounds) {
|
|
|
|
MOZ_ASSERT(aToBoundsSpace.IsRectilinear(),
|
|
|
|
"aToBoundsSpace must be rectilinear");
|
|
|
|
MOZ_ASSERT(aToNonScalingStrokeSpace.IsRectilinear(),
|
|
|
|
"aToNonScalingStrokeSpace must be rectilinear");
|
|
|
|
|
|
|
|
Matrix nonScalingToSource = aToNonScalingStrokeSpace.Inverse();
|
|
|
|
Matrix nonScalingToBounds = nonScalingToSource * aToBoundsSpace;
|
|
|
|
|
|
|
|
*aBounds = aToBoundsSpace.TransformBounds(aRect);
|
|
|
|
|
|
|
|
// Compute the amounts dx and dy that nonScalingToBounds scales a half-width
|
|
|
|
// stroke in the x and y directions, and then inflate aBounds by those amounts
|
|
|
|
// so that when aBounds is transformed back to non-scaling-stroke space
|
|
|
|
// it will map onto the correct stroked bounds.
|
|
|
|
|
|
|
|
Float dx = 0.0f;
|
|
|
|
Float dy = 0.0f;
|
|
|
|
// nonScalingToBounds is rectilinear, so either _12 and _21 are zero or _11
|
|
|
|
// and _22 are zero, and in each case the non-zero entries (from among _11,
|
|
|
|
// _12, _21, _22) simply scale the stroke width in the x and y directions.
|
|
|
|
if (FuzzyEqual(nonScalingToBounds._12, 0) &&
|
|
|
|
FuzzyEqual(nonScalingToBounds._21, 0)) {
|
|
|
|
dx = (aStrokeWidth / 2.0f) * std::abs(nonScalingToBounds._11);
|
|
|
|
dy = (aStrokeWidth / 2.0f) * std::abs(nonScalingToBounds._22);
|
|
|
|
} else {
|
|
|
|
dx = (aStrokeWidth / 2.0f) * std::abs(nonScalingToBounds._21);
|
|
|
|
dy = (aStrokeWidth / 2.0f) * std::abs(nonScalingToBounds._12);
|
|
|
|
}
|
|
|
|
|
|
|
|
aBounds->Inflate(dx, dy);
|
|
|
|
}
|
|
|
|
|
2012-09-22 23:26:05 +04:00
|
|
|
double SVGContentUtils::ComputeNormalizedHypotenuse(double aWidth,
|
|
|
|
double aHeight) {
|
2016-12-23 20:28:13 +03:00
|
|
|
return NS_hypot(aWidth, aHeight) / M_SQRT2;
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
float SVGContentUtils::AngleBisect(float a1, float a2) {
|
|
|
|
float delta = fmod(a2 - a1, static_cast<float>(2 * M_PI));
|
|
|
|
if (delta < 0) {
|
2013-04-07 12:46:48 +04:00
|
|
|
delta += static_cast<float>(2 * M_PI);
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
/* delta is now the angle from a1 around to a2, in the range [0, 2*M_PI) */
|
|
|
|
float r = a1 + delta / 2;
|
|
|
|
if (delta >= M_PI) {
|
|
|
|
/* the arc from a2 to a1 is smaller, so use the ray on that side */
|
2013-04-07 12:46:48 +04:00
|
|
|
r += static_cast<float>(M_PI);
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-03-03 12:11:03 +04:00
|
|
|
gfx::Matrix SVGContentUtils::GetViewBoxTransform(
|
|
|
|
float aViewportWidth, float aViewportHeight, float aViewboxX,
|
2012-09-22 23:26:05 +04:00
|
|
|
float aViewboxY, float aViewboxWidth, float aViewboxHeight,
|
|
|
|
const SVGAnimatedPreserveAspectRatio& aPreserveAspectRatio) {
|
2013-03-03 12:11:03 +04:00
|
|
|
return GetViewBoxTransform(aViewportWidth, aViewportHeight, aViewboxX,
|
2012-09-22 23:26:05 +04:00
|
|
|
aViewboxY, aViewboxWidth, aViewboxHeight,
|
|
|
|
aPreserveAspectRatio.GetAnimValue());
|
|
|
|
}
|
|
|
|
|
2013-03-03 12:11:03 +04:00
|
|
|
gfx::Matrix SVGContentUtils::GetViewBoxTransform(
|
|
|
|
float aViewportWidth, float aViewportHeight, float aViewboxX,
|
2012-09-22 23:26:05 +04:00
|
|
|
float aViewboxY, float aViewboxWidth, float aViewboxHeight,
|
|
|
|
const SVGPreserveAspectRatio& aPreserveAspectRatio) {
|
|
|
|
NS_ASSERTION(aViewportWidth >= 0, "viewport width must be nonnegative!");
|
|
|
|
NS_ASSERTION(aViewportHeight >= 0, "viewport height must be nonnegative!");
|
|
|
|
NS_ASSERTION(aViewboxWidth > 0, "viewBox width must be greater than zero!");
|
|
|
|
NS_ASSERTION(aViewboxHeight > 0, "viewBox height must be greater than zero!");
|
|
|
|
|
2018-02-18 18:53:13 +03:00
|
|
|
uint16_t align = aPreserveAspectRatio.GetAlign();
|
|
|
|
uint16_t meetOrSlice = aPreserveAspectRatio.GetMeetOrSlice();
|
2012-09-22 23:26:05 +04:00
|
|
|
|
|
|
|
// default to the defaults
|
2012-12-23 08:54:21 +04:00
|
|
|
if (align == SVG_PRESERVEASPECTRATIO_UNKNOWN)
|
|
|
|
align = SVG_PRESERVEASPECTRATIO_XMIDYMID;
|
|
|
|
if (meetOrSlice == SVG_MEETORSLICE_UNKNOWN)
|
|
|
|
meetOrSlice = SVG_MEETORSLICE_MEET;
|
2012-09-22 23:26:05 +04:00
|
|
|
|
|
|
|
float a, d, e, f;
|
|
|
|
a = aViewportWidth / aViewboxWidth;
|
|
|
|
d = aViewportHeight / aViewboxHeight;
|
|
|
|
e = 0.0f;
|
|
|
|
f = 0.0f;
|
|
|
|
|
2012-12-23 08:54:21 +04:00
|
|
|
if (align != SVG_PRESERVEASPECTRATIO_NONE && a != d) {
|
|
|
|
if ((meetOrSlice == SVG_MEETORSLICE_MEET && a < d) ||
|
|
|
|
(meetOrSlice == SVG_MEETORSLICE_SLICE && d < a)) {
|
2012-09-22 23:26:05 +04:00
|
|
|
d = a;
|
|
|
|
switch (align) {
|
2012-12-23 08:54:21 +04:00
|
|
|
case SVG_PRESERVEASPECTRATIO_XMINYMIN:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMIDYMIN:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMAXYMIN:
|
2012-09-22 23:26:05 +04:00
|
|
|
break;
|
2012-12-23 08:54:21 +04:00
|
|
|
case SVG_PRESERVEASPECTRATIO_XMINYMID:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMIDYMID:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMAXYMID:
|
2012-09-22 23:26:05 +04:00
|
|
|
f = (aViewportHeight - a * aViewboxHeight) / 2.0f;
|
|
|
|
break;
|
2012-12-23 08:54:21 +04:00
|
|
|
case SVG_PRESERVEASPECTRATIO_XMINYMAX:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMIDYMAX:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMAXYMAX:
|
2012-09-22 23:26:05 +04:00
|
|
|
f = aViewportHeight - a * aViewboxHeight;
|
|
|
|
break;
|
|
|
|
default:
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown value for align");
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
2012-12-23 08:54:21 +04:00
|
|
|
} else if ((meetOrSlice == SVG_MEETORSLICE_MEET && d < a) ||
|
|
|
|
(meetOrSlice == SVG_MEETORSLICE_SLICE && a < d)) {
|
2012-09-22 23:26:05 +04:00
|
|
|
a = d;
|
|
|
|
switch (align) {
|
2012-12-23 08:54:21 +04:00
|
|
|
case SVG_PRESERVEASPECTRATIO_XMINYMIN:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMINYMID:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMINYMAX:
|
2012-09-22 23:26:05 +04:00
|
|
|
break;
|
2012-12-23 08:54:21 +04:00
|
|
|
case SVG_PRESERVEASPECTRATIO_XMIDYMIN:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMIDYMID:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMIDYMAX:
|
2012-09-22 23:26:05 +04:00
|
|
|
e = (aViewportWidth - a * aViewboxWidth) / 2.0f;
|
|
|
|
break;
|
2012-12-23 08:54:21 +04:00
|
|
|
case SVG_PRESERVEASPECTRATIO_XMAXYMIN:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMAXYMID:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMAXYMAX:
|
2012-09-22 23:26:05 +04:00
|
|
|
e = aViewportWidth - a * aViewboxWidth;
|
|
|
|
break;
|
|
|
|
default:
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown value for align");
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
2018-06-18 08:43:11 +03:00
|
|
|
} else
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown value for meetOrSlice");
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
2013-12-30 10:50:17 +04:00
|
|
|
|
2012-09-22 23:26:05 +04:00
|
|
|
if (aViewboxX) e += -a * aViewboxX;
|
|
|
|
if (aViewboxY) f += -d * aViewboxY;
|
2013-12-30 10:50:17 +04:00
|
|
|
|
|
|
|
return gfx::Matrix(a, 0.0f, 0.0f, d, e, f);
|
2012-09-22 23:26:05 +04:00
|
|
|
}
|
2013-10-01 11:50:40 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
static bool ParseNumber(RangedPtr<const char16_t>& aIter,
|
|
|
|
const RangedPtr<const char16_t>& aEnd, double& aValue) {
|
2013-11-25 23:46:20 +04:00
|
|
|
int32_t sign;
|
|
|
|
if (!SVGContentUtils::ParseOptionalSign(aIter, aEnd, sign)) {
|
2013-10-01 11:50:40 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Absolute value of the integer part of the mantissa.
|
2013-11-25 23:46:20 +04:00
|
|
|
double intPart = 0.0;
|
2013-10-01 11:50:40 +04:00
|
|
|
|
2013-11-25 23:46:20 +04:00
|
|
|
bool gotDot = *aIter == '.';
|
2013-10-01 11:50:40 +04:00
|
|
|
|
|
|
|
if (!gotDot) {
|
2018-08-31 18:55:04 +03:00
|
|
|
if (!mozilla::IsAsciiDigit(*aIter)) {
|
2013-10-01 11:50:40 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
do {
|
2018-08-31 18:55:04 +03:00
|
|
|
intPart = 10.0 * intPart + mozilla::AsciiAlphanumericToNumber(*aIter);
|
2013-11-25 23:46:20 +04:00
|
|
|
++aIter;
|
2018-08-31 18:55:04 +03:00
|
|
|
} while (aIter != aEnd && mozilla::IsAsciiDigit(*aIter));
|
2013-10-01 11:50:40 +04:00
|
|
|
|
2013-11-25 23:46:20 +04:00
|
|
|
if (aIter != aEnd) {
|
|
|
|
gotDot = *aIter == '.';
|
2013-10-01 11:50:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fractional part of the mantissa.
|
2013-11-25 23:46:20 +04:00
|
|
|
double fracPart = 0.0;
|
2013-10-01 11:50:40 +04:00
|
|
|
|
|
|
|
if (gotDot) {
|
2013-11-25 23:46:20 +04:00
|
|
|
++aIter;
|
2018-08-31 18:55:04 +03:00
|
|
|
if (aIter == aEnd || !mozilla::IsAsciiDigit(*aIter)) {
|
2013-10-01 11:50:40 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-11-25 23:46:20 +04:00
|
|
|
|
|
|
|
// Power of ten by which we need to divide the fraction
|
|
|
|
double divisor = 1.0;
|
|
|
|
|
2013-10-01 11:50:40 +04:00
|
|
|
do {
|
2018-08-31 18:55:04 +03:00
|
|
|
fracPart = 10.0 * fracPart + mozilla::AsciiAlphanumericToNumber(*aIter);
|
2013-11-25 23:46:20 +04:00
|
|
|
divisor *= 10.0;
|
|
|
|
++aIter;
|
2018-08-31 18:55:04 +03:00
|
|
|
} while (aIter != aEnd && mozilla::IsAsciiDigit(*aIter));
|
2013-11-25 23:46:20 +04:00
|
|
|
|
|
|
|
fracPart /= divisor;
|
2013-10-01 11:50:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool gotE = false;
|
|
|
|
int32_t exponent = 0;
|
|
|
|
int32_t expSign;
|
|
|
|
|
2013-11-25 23:46:20 +04:00
|
|
|
if (aIter != aEnd && (*aIter == 'e' || *aIter == 'E')) {
|
2014-01-04 19:02:17 +04:00
|
|
|
RangedPtr<const char16_t> expIter(aIter);
|
2013-10-01 11:50:40 +04:00
|
|
|
|
|
|
|
++expIter;
|
2013-10-29 21:15:39 +04:00
|
|
|
if (expIter != aEnd) {
|
2013-10-01 11:50:40 +04:00
|
|
|
expSign = *expIter == '-' ? -1 : 1;
|
|
|
|
if (*expIter == '-' || *expIter == '+') {
|
|
|
|
++expIter;
|
2013-12-06 03:03:08 +04:00
|
|
|
}
|
2018-08-31 18:55:04 +03:00
|
|
|
if (expIter != aEnd && mozilla::IsAsciiDigit(*expIter)) {
|
2013-12-06 03:03:08 +04:00
|
|
|
// At this point we're sure this is an exponent
|
|
|
|
// and not the start of a unit such as em or ex.
|
|
|
|
gotE = true;
|
2013-10-01 11:50:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gotE) {
|
2013-11-25 23:46:20 +04:00
|
|
|
aIter = expIter;
|
2013-10-01 11:50:40 +04:00
|
|
|
do {
|
2018-08-31 18:55:04 +03:00
|
|
|
exponent = 10.0 * exponent + mozilla::AsciiAlphanumericToNumber(*aIter);
|
2013-11-25 23:46:20 +04:00
|
|
|
++aIter;
|
2018-08-31 18:55:04 +03:00
|
|
|
} while (aIter != aEnd && mozilla::IsAsciiDigit(*aIter));
|
2013-10-01 11:50:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble the number
|
2013-11-25 23:46:20 +04:00
|
|
|
aValue = sign * (intPart + fracPart);
|
2013-10-01 11:50:40 +04:00
|
|
|
if (gotE) {
|
2013-11-25 23:46:20 +04:00
|
|
|
aValue *= pow(10.0, expSign * exponent);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class floatType>
|
2014-01-04 19:02:17 +04:00
|
|
|
bool SVGContentUtils::ParseNumber(RangedPtr<const char16_t>& aIter,
|
|
|
|
const RangedPtr<const char16_t>& aEnd,
|
2013-11-25 23:46:20 +04:00
|
|
|
floatType& aValue) {
|
2014-01-04 19:02:17 +04:00
|
|
|
RangedPtr<const char16_t> iter(aIter);
|
2013-11-25 23:46:20 +04:00
|
|
|
|
|
|
|
double value;
|
|
|
|
if (!::ParseNumber(iter, aEnd, value)) {
|
|
|
|
return false;
|
2013-10-01 11:50:40 +04:00
|
|
|
}
|
2013-11-25 23:46:20 +04:00
|
|
|
floatType floatValue = floatType(value);
|
2014-10-01 13:26:00 +04:00
|
|
|
if (!IsFinite(floatValue)) {
|
2013-10-29 21:15:39 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-11-25 23:46:20 +04:00
|
|
|
aValue = floatValue;
|
2013-10-29 21:15:39 +04:00
|
|
|
aIter = iter;
|
|
|
|
return true;
|
2013-10-01 11:50:40 +04:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
template bool SVGContentUtils::ParseNumber<float>(
|
|
|
|
RangedPtr<const char16_t>& aIter, const RangedPtr<const char16_t>& aEnd,
|
2013-10-29 21:15:39 +04:00
|
|
|
float& aValue);
|
2013-11-25 23:46:20 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
template bool SVGContentUtils::ParseNumber<double>(
|
|
|
|
RangedPtr<const char16_t>& aIter, const RangedPtr<const char16_t>& aEnd,
|
2013-10-29 21:15:39 +04:00
|
|
|
double& aValue);
|
|
|
|
|
|
|
|
RangedPtr<const char16_t> SVGContentUtils::GetStartRangedPtr(
|
|
|
|
const nsAString& aString) {
|
2014-01-04 19:02:17 +04:00
|
|
|
return RangedPtr<const char16_t>(aString.Data(), aString.Length());
|
2013-10-29 21:15:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
RangedPtr<const char16_t> SVGContentUtils::GetEndRangedPtr(
|
|
|
|
const nsAString& aString) {
|
2014-01-04 19:02:17 +04:00
|
|
|
return RangedPtr<const char16_t>(aString.Data() + aString.Length(),
|
2013-10-29 21:15:39 +04:00
|
|
|
aString.Data(), aString.Length());
|
|
|
|
}
|
2013-10-01 11:50:40 +04:00
|
|
|
|
|
|
|
template <class floatType>
|
2017-07-06 15:00:35 +03:00
|
|
|
bool SVGContentUtils::ParseNumber(const nsAString& aString, floatType& aValue) {
|
2014-01-04 19:02:17 +04:00
|
|
|
RangedPtr<const char16_t> iter = GetStartRangedPtr(aString);
|
|
|
|
const RangedPtr<const char16_t> end = GetEndRangedPtr(aString);
|
2013-10-01 11:50:40 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
return ParseNumber(iter, end, aValue) && iter == end;
|
2013-10-01 11:50:40 +04:00
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
template bool SVGContentUtils::ParseNumber<float>(const nsAString& aString,
|
2013-10-01 11:50:40 +04:00
|
|
|
float& aValue);
|
2017-07-06 15:00:35 +03:00
|
|
|
template bool SVGContentUtils::ParseNumber<double>(const nsAString& aString,
|
2013-10-01 11:50:40 +04:00
|
|
|
double& aValue);
|
|
|
|
|
2013-11-25 23:46:20 +04:00
|
|
|
/* static */
|
2014-01-04 19:02:17 +04:00
|
|
|
bool SVGContentUtils::ParseInteger(RangedPtr<const char16_t>& aIter,
|
|
|
|
const RangedPtr<const char16_t>& aEnd,
|
2013-10-01 11:50:40 +04:00
|
|
|
int32_t& aValue) {
|
2014-01-04 19:02:17 +04:00
|
|
|
RangedPtr<const char16_t> iter(aIter);
|
2013-10-01 11:50:40 +04:00
|
|
|
|
2013-11-25 23:46:20 +04:00
|
|
|
int32_t sign;
|
|
|
|
if (!ParseOptionalSign(iter, aEnd, sign)) {
|
2013-10-01 11:50:40 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:55:04 +03:00
|
|
|
if (!mozilla::IsAsciiDigit(*iter)) {
|
2013-11-25 23:46:20 +04:00
|
|
|
return false;
|
2013-10-01 11:50:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t value = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (value <= std::numeric_limits<int32_t>::max()) {
|
2018-08-31 18:55:04 +03:00
|
|
|
value = 10 * value + mozilla::AsciiAlphanumericToNumber(*iter);
|
2013-10-01 11:50:40 +04:00
|
|
|
}
|
|
|
|
++iter;
|
2018-08-31 18:55:04 +03:00
|
|
|
} while (iter != aEnd && mozilla::IsAsciiDigit(*iter));
|
2013-10-01 11:50:40 +04:00
|
|
|
|
2013-11-25 23:46:20 +04:00
|
|
|
aIter = iter;
|
2013-10-01 11:50:40 +04:00
|
|
|
aValue = int32_t(clamped(sign * value,
|
|
|
|
int64_t(std::numeric_limits<int32_t>::min()),
|
|
|
|
int64_t(std::numeric_limits<int32_t>::max())));
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-02 15:10:38 +04:00
|
|
|
|
2013-11-25 23:46:20 +04:00
|
|
|
/* static */
|
|
|
|
bool SVGContentUtils::ParseInteger(const nsAString& aString, int32_t& aValue) {
|
2014-01-04 19:02:17 +04:00
|
|
|
RangedPtr<const char16_t> iter = GetStartRangedPtr(aString);
|
|
|
|
const RangedPtr<const char16_t> end = GetEndRangedPtr(aString);
|
2013-11-25 23:46:20 +04:00
|
|
|
|
|
|
|
return ParseInteger(iter, end, aValue) && iter == end;
|
|
|
|
}
|
|
|
|
|
2014-07-05 22:19:13 +04:00
|
|
|
float SVGContentUtils::CoordToFloat(nsSVGElement* aContent,
|
2013-11-02 15:10:38 +04:00
|
|
|
const nsStyleCoord& aCoord) {
|
|
|
|
switch (aCoord.GetUnit()) {
|
|
|
|
case eStyleUnit_Factor:
|
|
|
|
// user units
|
|
|
|
return aCoord.GetFactorValue();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-02 15:10:38 +04:00
|
|
|
case eStyleUnit_Coord:
|
|
|
|
return nsPresContext::AppUnitsToFloatCSSPixels(aCoord.GetCoordValue());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-02 15:10:38 +04:00
|
|
|
case eStyleUnit_Percent: {
|
2017-10-10 09:58:34 +03:00
|
|
|
SVGViewportElement* ctx = aContent->GetCtx();
|
2013-11-02 15:10:38 +04:00
|
|
|
return ctx ? aCoord.GetPercentValue() *
|
|
|
|
ctx->GetLength(SVGContentUtils::XY)
|
|
|
|
: 0.0f;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
}
|
2014-03-24 17:31:19 +04:00
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<gfx::Path> SVGContentUtils::GetPath(
|
2014-03-24 17:31:19 +04:00
|
|
|
const nsAString& aPathString) {
|
|
|
|
SVGPathData pathData;
|
|
|
|
nsSVGPathDataParser parser(aPathString, &pathData);
|
|
|
|
if (!parser.Parse()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DrawTarget> drawTarget =
|
2014-07-06 00:53:04 +04:00
|
|
|
gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PathBuilder> builder =
|
2014-07-06 00:53:04 +04:00
|
|
|
drawTarget->CreatePathBuilder(FillRule::FILL_WINDING);
|
|
|
|
|
|
|
|
return pathData.BuildPath(builder, NS_STYLE_STROKE_LINECAP_BUTT, 1);
|
2014-03-24 17:31:19 +04:00
|
|
|
}
|
2015-06-29 21:19:00 +03:00
|
|
|
|
|
|
|
bool SVGContentUtils::ShapeTypeHasNoCorners(const nsIContent* aContent) {
|
|
|
|
return aContent &&
|
|
|
|
aContent->IsAnyOfSVGElements(nsGkAtoms::circle, nsGkAtoms::ellipse);
|
|
|
|
}
|
2018-12-20 10:58:08 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|