2015-05-03 19:32:37 +00: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 11:12:37 +00: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 15:42:28 +00:00
|
|
|
|
2020-07-14 23:40:05 +00:00
|
|
|
#ifndef DOM_SVG_SVGGEOMETRYELEMENT_H_
|
|
|
|
#define DOM_SVG_SVGGEOMETRYELEMENT_H_
|
2006-06-21 15:42:28 +00:00
|
|
|
|
2019-04-09 20:04:33 +00:00
|
|
|
#include "mozilla/dom/SVGGraphicsElement.h"
|
2013-11-02 11:10:38 +00:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2022-10-06 00:37:25 +00:00
|
|
|
#include "mozilla/dom/SVGAnimatedNumber.h"
|
2013-03-11 22:05:58 +00:00
|
|
|
|
2019-01-26 13:01:31 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
struct SVGMark {
|
2013-07-12 06:39:38 +00:00
|
|
|
enum Type {
|
|
|
|
eStart,
|
|
|
|
eMid,
|
|
|
|
eEnd,
|
|
|
|
|
|
|
|
eTypeCount
|
|
|
|
};
|
|
|
|
|
2006-06-21 19:35:59 +00:00
|
|
|
float x, y, angle;
|
2013-07-12 06:39:38 +00:00
|
|
|
Type type;
|
2019-01-26 13:01:31 +00:00
|
|
|
SVGMark(float aX, float aY, float aAngle, Type aType)
|
2013-07-12 06:39:38 +00:00
|
|
|
: x(aX), y(aY), angle(aAngle), type(aType) {}
|
2006-06-21 15:42:28 +00:00
|
|
|
};
|
|
|
|
|
2016-12-18 11:11:47 +00:00
|
|
|
namespace dom {
|
2006-06-21 15:42:28 +00:00
|
|
|
|
2019-03-19 00:01:03 +00:00
|
|
|
class DOMSVGAnimatedNumber;
|
2020-08-25 14:38:32 +00:00
|
|
|
class DOMSVGPoint;
|
2016-12-18 11:11:47 +00:00
|
|
|
|
2020-07-15 10:37:55 +00:00
|
|
|
using SVGGeometryElementBase = mozilla::dom::SVGGraphicsElement;
|
2016-12-18 11:11:47 +00:00
|
|
|
|
|
|
|
class SVGGeometryElement : public SVGGeometryElementBase {
|
2013-11-02 11:10:38 +00:00
|
|
|
protected:
|
2020-07-15 10:37:55 +00:00
|
|
|
using CapStyle = mozilla::gfx::CapStyle;
|
|
|
|
using DrawTarget = mozilla::gfx::DrawTarget;
|
|
|
|
using FillRule = mozilla::gfx::FillRule;
|
|
|
|
using Float = mozilla::gfx::Float;
|
|
|
|
using Matrix = mozilla::gfx::Matrix;
|
|
|
|
using Path = mozilla::gfx::Path;
|
|
|
|
using Point = mozilla::gfx::Point;
|
|
|
|
using PathBuilder = mozilla::gfx::PathBuilder;
|
|
|
|
using Rect = mozilla::gfx::Rect;
|
|
|
|
using StrokeOptions = mozilla::gfx::StrokeOptions;
|
2013-11-02 11:10:38 +00:00
|
|
|
|
2006-06-21 15:42:28 +00:00
|
|
|
public:
|
2018-09-21 20:45:49 +00:00
|
|
|
explicit SVGGeometryElement(
|
|
|
|
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
|
2006-06-21 15:42:28 +00:00
|
|
|
|
2022-12-03 19:18:26 +00:00
|
|
|
nsresult AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
|
|
|
|
const nsAttrValue* aValue, const nsAttrValue* aOldValue,
|
|
|
|
nsIPrincipal* aSubjectPrincipal, bool aNotify) override;
|
2018-03-13 19:52:03 +00:00
|
|
|
bool IsNodeOfType(uint32_t aFlags) const override;
|
2014-10-04 11:13:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Causes this element to discard any Path object that GetOrBuildPath may
|
|
|
|
* have cached.
|
|
|
|
*/
|
2018-02-09 05:22:43 +00:00
|
|
|
void ClearAnyCachedPath() final { mCachedPath = nullptr; }
|
2014-10-04 11:13:30 +00:00
|
|
|
|
2017-10-02 22:05:19 +00:00
|
|
|
virtual bool AttributeDefinesGeometry(const nsAtom* aName);
|
2013-05-08 17:13:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this element's geometry depends on the width or height of
|
|
|
|
* its coordinate context (typically the viewport established by its nearest
|
|
|
|
* <svg> ancestor). In other words, returns true if one of the attributes for
|
|
|
|
* which AttributeDefinesGeometry returns true has a percentage value.
|
|
|
|
*
|
|
|
|
* This could be moved up to a more general class so it can be used for
|
|
|
|
* non-leaf elements, but that would require care and for now there's no need.
|
|
|
|
*/
|
|
|
|
bool GeometryDependsOnCoordCtx();
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
virtual bool IsMarkable();
|
2019-01-26 13:01:31 +00:00
|
|
|
virtual void GetMarkPoints(nsTArray<SVGMark>* aMarks);
|
2013-11-02 11:10:38 +00:00
|
|
|
|
2014-10-29 01:59:36 +00:00
|
|
|
/**
|
|
|
|
* A method that can be faster than using a Moz2D Path and calling GetBounds/
|
|
|
|
* GetStrokedBounds on it. It also helps us avoid rounding error for simple
|
|
|
|
* shapes and simple transforms where the Moz2D Path backends can fail to
|
|
|
|
* produce the clean integer bounds that content authors expect in some cases.
|
2015-09-01 04:17:00 +00:00
|
|
|
*
|
|
|
|
* If |aToNonScalingStrokeSpace| is non-null then |aBounds|, which is computed
|
|
|
|
* in bounds space, has the property that it's the smallest (axis-aligned)
|
|
|
|
* rectangular bound containing the image of this shape as stroked in
|
|
|
|
* non-scaling-stroke space. (When all transforms involved are rectilinear
|
|
|
|
* the bounds of the image of |aBounds| in non-scaling-stroke space will be
|
|
|
|
* tight, but if there are non-rectilinear transforms involved then that may
|
|
|
|
* be impossible and this method will return false).
|
2015-09-30 06:05:00 +00:00
|
|
|
*
|
|
|
|
* If |aToNonScalingStrokeSpace| is non-null then |*aToNonScalingStrokeSpace|
|
|
|
|
* must be non-singular.
|
2014-10-29 01:59:36 +00:00
|
|
|
*/
|
2015-02-03 18:36:32 +00:00
|
|
|
virtual bool GetGeometryBounds(
|
|
|
|
Rect* aBounds, const StrokeOptions& aStrokeOptions,
|
2015-09-01 04:17:00 +00:00
|
|
|
const Matrix& aToBoundsSpace,
|
|
|
|
const Matrix* aToNonScalingStrokeSpace = nullptr) {
|
2014-10-26 18:00:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-22 11:29:05 +00:00
|
|
|
/**
|
|
|
|
* For use with GetAsSimplePath.
|
|
|
|
*/
|
|
|
|
class SimplePath {
|
|
|
|
public:
|
|
|
|
SimplePath()
|
2018-06-16 10:54:44 +00:00
|
|
|
: mX(0.0), mY(0.0), mWidthOrX2(0.0), mHeightOrY2(0.0), mType(NONE) {}
|
2014-10-22 11:29:05 +00:00
|
|
|
bool IsPath() const { return mType != NONE; }
|
|
|
|
void SetRect(Float x, Float y, Float width, Float height) {
|
2017-04-18 05:22:51 +00:00
|
|
|
mX = x;
|
|
|
|
mY = y;
|
|
|
|
mWidthOrX2 = width;
|
|
|
|
mHeightOrY2 = height;
|
2014-10-22 11:29:05 +00:00
|
|
|
mType = RECT;
|
|
|
|
}
|
|
|
|
Rect AsRect() const {
|
|
|
|
MOZ_ASSERT(mType == RECT);
|
|
|
|
return Rect(mX, mY, mWidthOrX2, mHeightOrY2);
|
|
|
|
}
|
|
|
|
bool IsRect() const { return mType == RECT; }
|
|
|
|
void SetLine(Float x1, Float y1, Float x2, Float y2) {
|
2017-04-18 05:22:51 +00:00
|
|
|
mX = x1;
|
|
|
|
mY = y1;
|
|
|
|
mWidthOrX2 = x2;
|
|
|
|
mHeightOrY2 = y2;
|
2014-10-22 11:29:05 +00:00
|
|
|
mType = LINE;
|
|
|
|
}
|
|
|
|
Point Point1() const {
|
|
|
|
MOZ_ASSERT(mType == LINE);
|
|
|
|
return Point(mX, mY);
|
|
|
|
}
|
|
|
|
Point Point2() const {
|
|
|
|
MOZ_ASSERT(mType == LINE);
|
|
|
|
return Point(mWidthOrX2, mHeightOrY2);
|
|
|
|
}
|
|
|
|
bool IsLine() const { return mType == LINE; }
|
|
|
|
void Reset() { mType = NONE; }
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2014-10-22 11:29:05 +00:00
|
|
|
private:
|
|
|
|
enum Type { NONE, RECT, LINE };
|
|
|
|
Float mX, mY, mWidthOrX2, mHeightOrY2;
|
|
|
|
Type mType;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For some platforms there is significant overhead to creating and painting
|
|
|
|
* a Moz2D Path object. For Rects and lines it is better to get the path data
|
|
|
|
* using this method and then use the optimized DrawTarget methods for
|
|
|
|
* filling/stroking rects and lines.
|
|
|
|
*/
|
|
|
|
virtual void GetAsSimplePath(SimplePath* aSimplePath) {
|
|
|
|
aSimplePath->Reset();
|
|
|
|
}
|
|
|
|
|
2013-11-02 11:10:38 +00:00
|
|
|
/**
|
|
|
|
* Returns a Path that can be used to paint, hit-test or calculate bounds for
|
2014-10-04 11:13:30 +00:00
|
|
|
* this element. May return nullptr if there is no [valid] path. The path
|
|
|
|
* that is created may be cached and returned on subsequent calls.
|
|
|
|
*/
|
2018-03-09 07:36:13 +00:00
|
|
|
virtual already_AddRefed<Path> GetOrBuildPath(const DrawTarget* aDrawTarget,
|
2016-12-18 11:11:47 +00:00
|
|
|
FillRule fillRule);
|
2014-10-04 11:13:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The same as GetOrBuildPath, but bypasses the cache (neither returns any
|
|
|
|
* previously cached Path, nor caches the Path that in does return).
|
2013-11-06 10:05:18 +00:00
|
|
|
* this element. May return nullptr if there is no [valid] path.
|
2013-11-02 11:10:38 +00:00
|
|
|
*/
|
2015-06-17 14:00:52 +00:00
|
|
|
virtual already_AddRefed<Path> BuildPath(PathBuilder* aBuilder) = 0;
|
2013-11-02 11:10:38 +00:00
|
|
|
|
2020-10-01 16:19:37 +00:00
|
|
|
/**
|
|
|
|
* Get the distances from the origin of the path segments.
|
|
|
|
* For non-path elements that's just 0 and the total length of the shape.
|
|
|
|
*/
|
|
|
|
virtual bool GetDistancesFromOriginToEndsOfVisibleSegments(
|
|
|
|
FallibleTArray<double>* aOutput) {
|
|
|
|
aOutput->Clear();
|
|
|
|
double distances[] = {0.0, GetTotalLength()};
|
|
|
|
return aOutput->AppendElements(Span<double>(distances), fallible);
|
|
|
|
}
|
|
|
|
|
2014-10-04 11:13:30 +00:00
|
|
|
/**
|
|
|
|
* Returns a Path that can be used to measure the length of this elements
|
|
|
|
* path, or to find the position at a given distance along it.
|
|
|
|
*
|
|
|
|
* This is currently equivalent to calling GetOrBuildPath, but it may not be
|
|
|
|
* in the future. The reason for this function to be separate from
|
|
|
|
* GetOrBuildPath is because SVGPathData::BuildPath inserts small lines into
|
|
|
|
* the path if zero length subpaths are encountered, in order to implement
|
|
|
|
* the SVG specifications requirements that zero length subpaths should
|
|
|
|
* render circles/squares if stroke-linecap is round/square, respectively.
|
|
|
|
* In principle these inserted lines could interfere with path measurement,
|
|
|
|
* so we keep callers that are looking to do measurement separate in case we
|
|
|
|
* run into problems with the inserted lines negatively affecting measuring
|
|
|
|
* for content.
|
|
|
|
*/
|
2015-06-17 14:00:52 +00:00
|
|
|
virtual already_AddRefed<Path> GetOrBuildPathForMeasuring();
|
2013-11-02 11:10:38 +00:00
|
|
|
|
2019-05-16 13:21:20 +00:00
|
|
|
/**
|
|
|
|
* Return |true| if some geometry properties (|x|, |y|, etc) are changed
|
|
|
|
* because of CSS change.
|
|
|
|
*/
|
|
|
|
bool IsGeometryChangedViaCSS(ComputedStyle const& aNewStyle,
|
|
|
|
ComputedStyle const& aOldStyle) const;
|
|
|
|
|
2013-11-02 11:10:38 +00:00
|
|
|
/**
|
|
|
|
* Returns the current computed value of the CSS property 'fill-rule' for
|
|
|
|
* this element.
|
|
|
|
*/
|
|
|
|
FillRule GetFillRule();
|
2014-10-04 11:13:30 +00:00
|
|
|
|
2018-03-13 19:52:03 +00:00
|
|
|
enum PathLengthScaleForType { eForTextPath, eForStroking };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the ratio of the actual element's length to the content author's
|
|
|
|
* estimated length (as provided by the element's 'pathLength' attribute).
|
|
|
|
* This is used to scale stroke dashing, and to scale offsets along a
|
|
|
|
* textPath.
|
|
|
|
*/
|
|
|
|
float GetPathLengthScale(PathLengthScaleForType aFor);
|
|
|
|
|
2016-12-18 11:11:47 +00:00
|
|
|
// WebIDL
|
2019-03-19 00:01:03 +00:00
|
|
|
already_AddRefed<DOMSVGAnimatedNumber> PathLength();
|
2021-12-13 01:03:39 +00:00
|
|
|
MOZ_CAN_RUN_SCRIPT bool IsPointInFill(const DOMPointInit& aPoint);
|
|
|
|
MOZ_CAN_RUN_SCRIPT bool IsPointInStroke(const DOMPointInit& aPoint);
|
|
|
|
MOZ_CAN_RUN_SCRIPT float GetTotalLengthForBinding();
|
|
|
|
MOZ_CAN_RUN_SCRIPT already_AddRefed<DOMSVGPoint> GetPointAtLength(
|
|
|
|
float distance, ErrorResult& rv);
|
2016-12-18 11:11:47 +00:00
|
|
|
|
2014-10-04 11:13:30 +00:00
|
|
|
protected:
|
2018-12-21 08:58:14 +00:00
|
|
|
// SVGElement method
|
2022-12-03 19:18:26 +00:00
|
|
|
NumberAttributesInfo GetNumberInfo() override;
|
2016-12-18 11:11:47 +00:00
|
|
|
|
2021-12-13 01:03:39 +00:00
|
|
|
// d is a presentation attribute, so we would like to make sure style is
|
|
|
|
// up-to-date. This function flushes the style if the path attribute is d.
|
|
|
|
MOZ_CAN_RUN_SCRIPT void FlushStyleIfNeeded();
|
|
|
|
|
2019-04-09 20:04:33 +00:00
|
|
|
SVGAnimatedNumber mPathLength;
|
2016-12-18 11:11:47 +00:00
|
|
|
static NumberInfo sNumberInfo;
|
2015-10-18 05:24:48 +00:00
|
|
|
mutable RefPtr<Path> mCachedPath;
|
2019-06-19 21:14:55 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
already_AddRefed<Path> GetOrBuildPathForHitTest();
|
2021-12-13 01:03:39 +00:00
|
|
|
|
|
|
|
float GetTotalLength();
|
2006-06-21 15:42:28 +00:00
|
|
|
};
|
|
|
|
|
2016-12-18 11:11:47 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2020-07-14 23:40:05 +00:00
|
|
|
#endif // DOM_SVG_SVGGEOMETRYELEMENT_H_
|