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-05-21 15:12:37 +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/. */
|
2006-06-21 19:42:28 +04:00
|
|
|
|
2016-12-18 14:11:47 +03:00
|
|
|
#include "SVGGeometryElement.h"
|
2013-11-02 15:10:38 +04:00
|
|
|
|
2016-12-18 14:11:47 +03:00
|
|
|
#include "DOMSVGPoint.h"
|
2013-11-02 15:10:38 +04:00
|
|
|
#include "gfxPlatform.h"
|
2019-01-25 05:00:52 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2013-11-02 15:10:38 +04:00
|
|
|
#include "nsComputedDOMStyle.h"
|
2019-04-09 09:53:57 +03:00
|
|
|
#include "nsSVGLength2.h"
|
2019-01-25 05:00:52 +03:00
|
|
|
#include "nsSVGUtils.h"
|
|
|
|
#include "mozilla/dom/SVGLengthBinding.h"
|
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "mozilla/SVGContentUtils.h"
|
2013-11-02 15:10:38 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::gfx;
|
2018-02-02 16:21:33 +03:00
|
|
|
using namespace mozilla::dom;
|
2006-06-21 19:42:28 +04:00
|
|
|
|
2018-12-21 11:58:14 +03:00
|
|
|
SVGElement::NumberInfo SVGGeometryElement::sNumberInfo = {nsGkAtoms::pathLength,
|
|
|
|
0, false};
|
2016-12-18 14:11:47 +03:00
|
|
|
|
2011-12-31 13:44:03 +04:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Implementation
|
|
|
|
|
2018-09-21 23:45:49 +03:00
|
|
|
SVGGeometryElement::SVGGeometryElement(
|
|
|
|
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
|
|
|
|
: SVGGeometryElementBase(std::move(aNodeInfo)) {}
|
2006-06-21 19:42:28 +04:00
|
|
|
|
2018-12-21 11:58:14 +03:00
|
|
|
SVGElement::NumberAttributesInfo SVGGeometryElement::GetNumberInfo() {
|
2016-12-18 14:11:47 +03:00
|
|
|
return NumberAttributesInfo(&mPathLength, &sNumberInfo, 1);
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsresult SVGGeometryElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
|
2017-05-19 00:09:01 +03:00
|
|
|
const nsAttrValue* aValue,
|
2017-10-10 00:33:38 +03:00
|
|
|
const nsAttrValue* aOldValue,
|
|
|
|
nsIPrincipal* aSubjectPrincipal,
|
|
|
|
bool aNotify) {
|
2014-10-04 15:13:30 +04:00
|
|
|
if (mCachedPath && aNamespaceID == kNameSpaceID_None &&
|
|
|
|
AttributeDefinesGeometry(aName)) {
|
|
|
|
mCachedPath = nullptr;
|
|
|
|
}
|
2016-12-18 14:11:47 +03:00
|
|
|
return SVGGeometryElementBase::AfterSetAttr(
|
|
|
|
aNamespaceID, aName, aValue, aOldValue, aSubjectPrincipal, aNotify);
|
2014-10-04 15:13:30 +04:00
|
|
|
}
|
|
|
|
|
2018-03-13 22:52:03 +03:00
|
|
|
bool SVGGeometryElement::IsNodeOfType(uint32_t aFlags) const {
|
|
|
|
return !(aFlags & ~eSHAPE);
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
bool SVGGeometryElement::AttributeDefinesGeometry(const nsAtom* aName) {
|
2016-12-18 14:11:47 +03:00
|
|
|
if (aName == nsGkAtoms::pathLength) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-09 09:53:57 +03:00
|
|
|
// Check for nsSVGLength2 attribute
|
2006-06-21 19:42:28 +04:00
|
|
|
LengthAttributesInfo info = GetLengthInfo();
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < info.mLengthCount; i++) {
|
2018-03-29 12:45:24 +03:00
|
|
|
if (aName == info.mLengthInfo[i].mName) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2006-06-21 19:42:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-06-21 19:42:28 +04:00
|
|
|
}
|
|
|
|
|
2016-12-18 14:11:47 +03:00
|
|
|
bool SVGGeometryElement::GeometryDependsOnCoordCtx() {
|
2019-04-09 09:53:57 +03:00
|
|
|
// Check the nsSVGLength2 attribute
|
2016-12-18 14:11:47 +03:00
|
|
|
LengthAttributesInfo info =
|
|
|
|
const_cast<SVGGeometryElement*>(this)->GetLengthInfo();
|
2013-05-08 21:13:11 +04:00
|
|
|
for (uint32_t i = 0; i < info.mLengthCount; i++) {
|
2018-02-02 16:21:33 +03:00
|
|
|
if (info.mLengths[i].GetSpecifiedUnitType() ==
|
2018-06-26 00:20:54 +03:00
|
|
|
SVGLength_Binding::SVG_LENGTHTYPE_PERCENTAGE) {
|
2013-05-08 21:13:11 +04:00
|
|
|
return true;
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
2013-05-08 21:13:11 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-18 14:11:47 +03:00
|
|
|
bool SVGGeometryElement::IsMarkable() { return false; }
|
2006-06-21 19:42:28 +04:00
|
|
|
|
2019-01-26 16:01:31 +03:00
|
|
|
void SVGGeometryElement::GetMarkPoints(nsTArray<SVGMark>* aMarks) {}
|
2006-06-28 02:54:31 +04:00
|
|
|
|
2018-03-09 10:36:13 +03:00
|
|
|
already_AddRefed<Path> SVGGeometryElement::GetOrBuildPath(
|
|
|
|
const DrawTarget* aDrawTarget, FillRule aFillRule) {
|
2014-10-04 15:13:30 +04:00
|
|
|
// We only cache the path if it matches the backend used for screen painting:
|
2018-03-09 10:36:13 +03:00
|
|
|
bool cacheable = aDrawTarget->GetBackendType() ==
|
2015-10-02 09:06:43 +03:00
|
|
|
gfxPlatform::GetPlatform()->GetDefaultContentBackend();
|
2014-10-04 15:13:30 +04:00
|
|
|
|
2017-09-25 23:58:19 +03:00
|
|
|
if (cacheable && mCachedPath && mCachedPath->GetFillRule() == aFillRule &&
|
2018-03-09 10:36:13 +03:00
|
|
|
aDrawTarget->GetBackendType() == mCachedPath->GetBackendType()) {
|
2017-09-25 23:58:19 +03:00
|
|
|
RefPtr<Path> path(mCachedPath);
|
|
|
|
return path.forget();
|
2014-10-04 15:13:30 +04:00
|
|
|
}
|
2018-03-09 10:36:13 +03:00
|
|
|
RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder(aFillRule);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Path> path = BuildPath(builder);
|
2018-03-18 17:05:28 +03:00
|
|
|
if (cacheable) {
|
2014-10-04 15:13:30 +04:00
|
|
|
mCachedPath = path;
|
|
|
|
}
|
|
|
|
return path.forget();
|
|
|
|
}
|
|
|
|
|
2016-12-18 14:11:47 +03:00
|
|
|
already_AddRefed<Path> SVGGeometryElement::GetOrBuildPathForMeasuring() {
|
2018-03-13 22:20:43 +03:00
|
|
|
RefPtr<DrawTarget> drawTarget =
|
|
|
|
gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();
|
|
|
|
FillRule fillRule = mCachedPath ? mCachedPath->GetFillRule() : GetFillRule();
|
|
|
|
return GetOrBuildPath(drawTarget, fillRule);
|
2006-06-28 02:54:31 +04:00
|
|
|
}
|
2013-11-02 15:10:38 +04:00
|
|
|
|
2016-12-18 14:11:47 +03:00
|
|
|
FillRule SVGGeometryElement::GetFillRule() {
|
2016-08-17 09:43:26 +03:00
|
|
|
FillRule fillRule =
|
|
|
|
FillRule::FILL_WINDING; // Equivalent to StyleFillRule::Nonzero
|
2013-11-02 15:10:38 +04:00
|
|
|
|
2018-03-22 16:49:21 +03:00
|
|
|
RefPtr<ComputedStyle> computedStyle =
|
2018-03-22 21:20:41 +03:00
|
|
|
nsComputedDOMStyle::GetComputedStyleNoFlush(this, nullptr);
|
2017-04-05 08:39:23 +03:00
|
|
|
|
2018-03-22 16:49:21 +03:00
|
|
|
if (computedStyle) {
|
|
|
|
MOZ_ASSERT(computedStyle->StyleSVG()->mFillRule == StyleFillRule::Nonzero ||
|
|
|
|
computedStyle->StyleSVG()->mFillRule == StyleFillRule::Evenodd);
|
2013-11-02 15:10:38 +04:00
|
|
|
|
2018-03-22 16:49:21 +03:00
|
|
|
if (computedStyle->StyleSVG()->mFillRule == StyleFillRule::Evenodd) {
|
2014-01-10 23:06:17 +04:00
|
|
|
fillRule = FillRule::FILL_EVEN_ODD;
|
2013-11-02 15:10:38 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ReportToConsole
|
2018-03-23 16:49:21 +03:00
|
|
|
NS_WARNING("Couldn't get ComputedStyle for content in GetFillRule");
|
2013-11-02 15:10:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fillRule;
|
|
|
|
}
|
2016-12-18 14:11:47 +03:00
|
|
|
|
|
|
|
float SVGGeometryElement::GetTotalLength() {
|
|
|
|
RefPtr<Path> flat = GetOrBuildPathForMeasuring();
|
|
|
|
return flat ? flat->ComputeLength() : 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsISVGPoint> SVGGeometryElement::GetPointAtLength(
|
|
|
|
float distance, ErrorResult& rv) {
|
|
|
|
RefPtr<Path> path = GetOrBuildPathForMeasuring();
|
|
|
|
if (!path) {
|
|
|
|
rv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-09 07:35:37 +03:00
|
|
|
nsCOMPtr<nsISVGPoint> point = new DOMSVGPoint(path->ComputePointAtLength(
|
|
|
|
clamped(distance, 0.f, path->ComputeLength())));
|
2016-12-18 14:11:47 +03:00
|
|
|
return point.forget();
|
|
|
|
}
|
|
|
|
|
2018-03-13 22:52:03 +03:00
|
|
|
float SVGGeometryElement::GetPathLengthScale(PathLengthScaleForType aFor) {
|
|
|
|
MOZ_ASSERT(aFor == eForTextPath || aFor == eForStroking, "Unknown enum");
|
|
|
|
if (mPathLength.IsExplicitlySet()) {
|
|
|
|
float authorsPathLengthEstimate = mPathLength.GetAnimValue();
|
|
|
|
if (authorsPathLengthEstimate > 0) {
|
|
|
|
RefPtr<Path> path = GetOrBuildPathForMeasuring();
|
|
|
|
if (!path) {
|
|
|
|
// The path is empty or invalid so its length must be zero and
|
|
|
|
// we know that 0 / authorsPathLengthEstimate = 0.
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
if (aFor == eForTextPath) {
|
|
|
|
// For textPath, a transform on the referenced path affects the
|
|
|
|
// textPath layout, so when calculating the actual path length
|
|
|
|
// we need to take that into account.
|
|
|
|
gfxMatrix matrix = PrependLocalTransformsTo(gfxMatrix());
|
|
|
|
if (!matrix.IsIdentity()) {
|
|
|
|
RefPtr<PathBuilder> builder =
|
|
|
|
path->TransformedCopyToBuilder(ToMatrix(matrix));
|
|
|
|
path = builder->Finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path->ComputeLength() / authorsPathLengthEstimate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:01:03 +03:00
|
|
|
already_AddRefed<DOMSVGAnimatedNumber> SVGGeometryElement::PathLength() {
|
2016-12-18 14:11:47 +03:00
|
|
|
return mPathLength.ToDOMAnimatedNumber(this);
|
|
|
|
}
|