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"
|
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "nsComputedDOMStyle.h"
|
2014-10-04 15:13:30 +04:00
|
|
|
#include "nsSVGUtils.h"
|
2013-05-08 21:13:11 +04:00
|
|
|
#include "nsSVGLength2.h"
|
2013-11-02 15:10:38 +04:00
|
|
|
#include "SVGContentUtils.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::gfx;
|
2006-06-21 19:42:28 +04:00
|
|
|
|
2016-12-18 14:11:47 +03:00
|
|
|
nsSVGElement::NumberInfo SVGGeometryElement::sNumberInfo =
|
|
|
|
{ &nsGkAtoms::pathLength, 0, false };
|
|
|
|
|
2011-12-31 13:44:03 +04:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Implementation
|
|
|
|
|
2016-12-18 14:11:47 +03:00
|
|
|
SVGGeometryElement::SVGGeometryElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
|
|
|
|
: SVGGeometryElementBase(aNodeInfo)
|
2006-06-21 19:42:28 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-12-18 14:11:47 +03:00
|
|
|
nsSVGElement::NumberAttributesInfo
|
|
|
|
SVGGeometryElement::GetNumberInfo()
|
|
|
|
{
|
|
|
|
return NumberAttributesInfo(&mPathLength, &sNumberInfo, 1);
|
|
|
|
}
|
|
|
|
|
2014-10-04 15:13:30 +04:00
|
|
|
nsresult
|
2016-12-18 14:11:47 +03:00
|
|
|
SVGGeometryElement::AfterSetAttr(int32_t aNamespaceID, nsIAtom* aName,
|
|
|
|
const nsAttrValue* aValue, 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, aNotify);
|
2014-10-04 15:13:30 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2016-12-18 14:11:47 +03:00
|
|
|
SVGGeometryElement::AttributeDefinesGeometry(const nsIAtom *aName)
|
2006-06-21 19:42:28 +04:00
|
|
|
{
|
2016-12-18 14:11:47 +03:00
|
|
|
if (aName == nsGkAtoms::pathLength) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-06-21 19:42:28 +04:00
|
|
|
// Check for nsSVGLength2 attribute
|
|
|
|
LengthAttributesInfo info = GetLengthInfo();
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < info.mLengthCount; i++) {
|
2006-06-21 19:42:28 +04: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
|
|
|
}
|
|
|
|
|
2013-05-08 21:13:11 +04:00
|
|
|
bool
|
2016-12-18 14:11:47 +03:00
|
|
|
SVGGeometryElement::GeometryDependsOnCoordCtx()
|
2013-05-08 21:13:11 +04: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++) {
|
|
|
|
if (info.mLengths[i].GetSpecifiedUnitType() == nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2016-12-18 14:11:47 +03:00
|
|
|
SVGGeometryElement::IsMarkable()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-12-18 14:11:47 +03:00
|
|
|
SVGGeometryElement::GetMarkPoints(nsTArray<nsSVGMark> *aMarks)
|
2006-06-21 19:42:28 +04:00
|
|
|
{
|
|
|
|
}
|
2006-06-28 02:54:31 +04:00
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<Path>
|
2016-12-18 14:11:47 +03:00
|
|
|
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:
|
|
|
|
bool cacheable = aDrawTarget.GetBackendType() ==
|
2015-10-02 09:06:43 +03:00
|
|
|
gfxPlatform::GetPlatform()->GetDefaultContentBackend();
|
2014-10-04 15:13:30 +04:00
|
|
|
|
|
|
|
// Checking for and returning mCachedPath before checking the pref means
|
|
|
|
// that the pref is only live on page reload (or app restart for SVG in
|
|
|
|
// chrome). The benefit is that we avoid causing a CPU memory cache miss by
|
|
|
|
// looking at the global variable that the pref's stored in.
|
|
|
|
if (cacheable && mCachedPath) {
|
2015-01-08 03:10:49 +03:00
|
|
|
if (aDrawTarget.GetBackendType() == mCachedPath->GetBackendType()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Path> path(mCachedPath);
|
2015-05-01 16:14:16 +03:00
|
|
|
return path.forget();
|
2015-01-08 03:10:49 +03:00
|
|
|
}
|
2014-10-04 15:13:30 +04:00
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PathBuilder> builder = aDrawTarget.CreatePathBuilder(aFillRule);
|
|
|
|
RefPtr<Path> path = BuildPath(builder);
|
2014-10-04 15:13:30 +04:00
|
|
|
if (cacheable && NS_SVGPathCachingEnabled()) {
|
|
|
|
mCachedPath = path;
|
|
|
|
}
|
|
|
|
return path.forget();
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<Path>
|
2016-12-18 14:11:47 +03:00
|
|
|
SVGGeometryElement::GetOrBuildPathForMeasuring()
|
2006-06-28 02:54:31 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2006-06-28 02:54:31 +04:00
|
|
|
}
|
2013-11-02 15:10:38 +04:00
|
|
|
|
|
|
|
FillRule
|
2016-12-18 14:11:47 +03:00
|
|
|
SVGGeometryElement::GetFillRule()
|
2013-11-02 15:10:38 +04:00
|
|
|
{
|
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
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsStyleContext> styleContext =
|
2017-04-05 08:39:23 +03:00
|
|
|
nsComputedDOMStyle::GetStyleContextNoFlush(this, nullptr, nullptr);
|
|
|
|
|
2013-11-02 15:10:38 +04:00
|
|
|
if (styleContext) {
|
2016-08-17 09:43:26 +03:00
|
|
|
MOZ_ASSERT(styleContext->StyleSVG()->mFillRule == StyleFillRule::Nonzero ||
|
|
|
|
styleContext->StyleSVG()->mFillRule == StyleFillRule::Evenodd);
|
2013-11-02 15:10:38 +04:00
|
|
|
|
2016-08-17 09:43:26 +03:00
|
|
|
if (styleContext->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
|
|
|
|
NS_WARNING("Couldn't get style context for content in GetFillRule");
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
float totalLength = path->ComputeLength();
|
|
|
|
if (mPathLength.IsExplicitlySet()) {
|
|
|
|
float pathLength = mPathLength.GetAnimValue();
|
|
|
|
if (pathLength <= 0) {
|
|
|
|
rv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
distance *= totalLength / pathLength;
|
|
|
|
}
|
|
|
|
distance = std::max(0.f, distance);
|
|
|
|
distance = std::min(totalLength, distance);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISVGPoint> point =
|
|
|
|
new DOMSVGPoint(path->ComputePointAtLength(distance));
|
|
|
|
return point.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<SVGAnimatedNumber>
|
|
|
|
SVGGeometryElement::PathLength()
|
|
|
|
{
|
|
|
|
return mPathLength.ToDOMAnimatedNumber(this);
|
|
|
|
}
|