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/. */
|
2010-11-08 18:07:00 +03:00
|
|
|
|
|
|
|
#include "SVGPathData.h"
|
2013-10-24 16:46:38 +04:00
|
|
|
|
|
|
|
#include "gfx2DGlue.h"
|
2012-09-22 23:26:05 +04:00
|
|
|
#include "gfxPlatform.h"
|
2013-10-24 19:50:27 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "mozilla/gfx/Types.h"
|
2013-10-24 16:46:38 +04:00
|
|
|
#include "mozilla/gfx/Point.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2010-11-08 18:07:00 +03:00
|
|
|
#include "nsString.h"
|
2018-12-21 23:19:15 +03:00
|
|
|
#include "SVGPathDataParser.h"
|
2010-11-08 18:07:00 +03:00
|
|
|
#include <stdarg.h>
|
2013-11-02 15:10:38 +04:00
|
|
|
#include "nsStyleConsts.h"
|
2012-09-22 23:26:05 +04:00
|
|
|
#include "SVGContentUtils.h"
|
2019-01-26 16:01:31 +03:00
|
|
|
#include "SVGGeometryElement.h"
|
2012-09-22 23:26:05 +04:00
|
|
|
#include "SVGPathSegUtils.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2010-11-08 18:07:00 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2018-06-26 00:20:54 +03:00
|
|
|
using namespace mozilla::dom::SVGPathSeg_Binding;
|
2013-10-24 16:46:38 +04:00
|
|
|
using namespace mozilla::gfx;
|
2010-11-08 18:07:00 +03:00
|
|
|
|
2018-08-21 23:59:15 +03:00
|
|
|
static inline bool IsMoveto(uint16_t aSegType) {
|
2012-12-23 08:54:22 +04:00
|
|
|
return aSegType == PATHSEG_MOVETO_ABS || aSegType == PATHSEG_MOVETO_REL;
|
2011-02-01 22:41:50 +03:00
|
|
|
}
|
|
|
|
|
2018-08-21 23:59:15 +03:00
|
|
|
static inline bool IsMoveto(StylePathCommand::Tag aSegType) {
|
|
|
|
return aSegType == StylePathCommand::Tag::MoveTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsValidType(uint16_t aSegType) {
|
|
|
|
return SVGPathSegUtils::IsValidType(aSegType);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsValidType(StylePathCommand::Tag aSegType) {
|
|
|
|
return aSegType != StylePathCommand::Tag::Unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsClosePath(uint16_t aSegType) {
|
|
|
|
return aSegType == PATHSEG_CLOSEPATH;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsClosePath(StylePathCommand::Tag aSegType) {
|
|
|
|
return aSegType == StylePathCommand::Tag::ClosePath;
|
|
|
|
}
|
|
|
|
|
2010-11-08 18:07:00 +03:00
|
|
|
nsresult SVGPathData::CopyFrom(const SVGPathData& rhs) {
|
2015-07-07 21:27:03 +03:00
|
|
|
if (!mData.Assign(rhs.mData, fallible)) {
|
2010-11-08 18:07:00 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SVGPathData::GetValueAsString(nsAString& aValue) const {
|
|
|
|
// we need this function in DidChangePathSegList
|
|
|
|
aValue.Truncate();
|
|
|
|
if (!Length()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
2010-11-08 18:07:00 +03:00
|
|
|
for (;;) {
|
|
|
|
nsAutoString segAsString;
|
|
|
|
SVGPathSegUtils::GetValueAsString(&mData[i], segAsString);
|
|
|
|
// We ignore OOM, since it's not useful for us to return an error.
|
|
|
|
aValue.Append(segAsString);
|
|
|
|
i += 1 + SVGPathSegUtils::ArgCountForType(mData[i]);
|
|
|
|
if (i >= mData.Length()) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt");
|
2010-11-08 18:07:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
aValue.Append(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult SVGPathData::SetValueFromString(const nsAString& aValue) {
|
|
|
|
// We don't use a temp variable since the spec says to parse everything up to
|
|
|
|
// the first error. We still return any error though so that callers know if
|
|
|
|
// there's a problem.
|
|
|
|
|
2018-12-21 23:19:15 +03:00
|
|
|
SVGPathDataParser pathParser(aValue, this);
|
2013-10-29 21:15:39 +04:00
|
|
|
return pathParser.Parse() ? NS_OK : NS_ERROR_DOM_SYNTAX_ERR;
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult SVGPathData::AppendSeg(uint32_t aType, ...) {
|
|
|
|
uint32_t oldLength = mData.Length();
|
|
|
|
uint32_t newLength = oldLength + 1 + SVGPathSegUtils::ArgCountForType(aType);
|
2015-05-18 23:50:35 +03:00
|
|
|
if (!mData.SetLength(newLength, fallible)) {
|
2010-11-08 18:07:00 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2014-05-29 03:52:47 +04:00
|
|
|
|
2010-11-08 18:07:00 +03:00
|
|
|
mData[oldLength] = SVGPathSegUtils::EncodeType(aType);
|
|
|
|
va_list args;
|
|
|
|
va_start(args, aType);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = oldLength + 1; i < newLength; ++i) {
|
2010-11-08 18:07:00 +03:00
|
|
|
// NOTE! 'float' is promoted to 'double' when passed through '...'!
|
|
|
|
mData[i] = float(va_arg(args, double));
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
float SVGPathData::GetPathLength() const {
|
|
|
|
SVGPathTraversalState state;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
2010-11-08 18:07:00 +03:00
|
|
|
while (i < mData.Length()) {
|
2011-04-08 02:17:36 +04:00
|
|
|
SVGPathSegUtils::TraversePathSegment(&mData[i], state);
|
2010-11-08 18:07:00 +03:00
|
|
|
i += 1 + SVGPathSegUtils::ArgCountForType(mData[i]);
|
|
|
|
}
|
|
|
|
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt");
|
2010-11-08 18:07:00 +03:00
|
|
|
|
2011-04-08 02:17:36 +04:00
|
|
|
return state.length;
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
uint32_t SVGPathData::CountItems() const {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0, count = 0;
|
2010-11-08 18:07:00 +03:00
|
|
|
|
|
|
|
while (i < mData.Length()) {
|
|
|
|
i += 1 + SVGPathSegUtils::ArgCountForType(mData[i]);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt");
|
2010-11-08 18:07:00 +03:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool SVGPathData::GetSegmentLengths(nsTArray<double>* aLengths) const {
|
|
|
|
aLengths->Clear();
|
|
|
|
SVGPathTraversalState state;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
2010-11-08 18:07:00 +03:00
|
|
|
while (i < mData.Length()) {
|
2011-04-08 02:17:36 +04:00
|
|
|
state.length = 0.0;
|
|
|
|
SVGPathSegUtils::TraversePathSegment(&mData[i], state);
|
|
|
|
if (!aLengths->AppendElement(state.length)) {
|
2010-11-08 18:07:00 +03:00
|
|
|
aLengths->Clear();
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
i += 1 + SVGPathSegUtils::ArgCountForType(mData[i]);
|
|
|
|
}
|
|
|
|
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt");
|
2010-11-08 18:07:00 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
2013-11-25 23:46:20 +04:00
|
|
|
bool SVGPathData::GetDistancesFromOriginToEndsOfVisibleSegments(
|
|
|
|
FallibleTArray<double>* aOutput) const {
|
2010-11-08 18:07:00 +03:00
|
|
|
SVGPathTraversalState state;
|
|
|
|
|
|
|
|
aOutput->Clear();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
2010-11-08 18:07:00 +03:00
|
|
|
while (i < mData.Length()) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t segType = SVGPathSegUtils::DecodeType(mData[i]);
|
2011-04-08 02:17:36 +04:00
|
|
|
SVGPathSegUtils::TraversePathSegment(&mData[i], state);
|
2010-11-08 18:07:00 +03:00
|
|
|
|
|
|
|
// We skip all moveto commands except an initial moveto. See the text 'A
|
|
|
|
// "move to" command does not count as an additional point when dividing up
|
|
|
|
// the duration...':
|
|
|
|
//
|
|
|
|
// http://www.w3.org/TR/SVG11/animate.html#AnimateMotionElement
|
|
|
|
//
|
|
|
|
// This is important in the non-default case of calcMode="linear". In
|
|
|
|
// this case an equal amount of time is spent on each path segment,
|
|
|
|
// except on moveto segments which are jumped over immediately.
|
|
|
|
|
2012-12-23 08:54:22 +04:00
|
|
|
if (i == 0 ||
|
|
|
|
(segType != PATHSEG_MOVETO_ABS && segType != PATHSEG_MOVETO_REL)) {
|
2015-05-28 21:07:44 +03:00
|
|
|
if (!aOutput->AppendElement(state.length, fallible)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i += 1 + SVGPathSegUtils::ArgCountForType(segType);
|
|
|
|
}
|
|
|
|
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt?");
|
2010-11-08 18:07:00 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t SVGPathData::GetPathSegAtLength(float aDistance) const {
|
|
|
|
// TODO [SVGWG issue] get specified what happen if 'aDistance' < 0, or
|
|
|
|
// 'aDistance' > the length of the path, or the seg list is empty.
|
|
|
|
// Return -1? Throwing would better help authors avoid tricky bugs (DOM
|
|
|
|
// could do that if we return -1).
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0, segIndex = 0;
|
2010-11-08 18:07:00 +03:00
|
|
|
SVGPathTraversalState state;
|
|
|
|
|
|
|
|
while (i < mData.Length()) {
|
2011-04-08 02:17:36 +04:00
|
|
|
SVGPathSegUtils::TraversePathSegment(&mData[i], state);
|
|
|
|
if (state.length >= aDistance) {
|
2010-11-08 18:07:00 +03:00
|
|
|
return segIndex;
|
|
|
|
}
|
|
|
|
i += 1 + SVGPathSegUtils::ArgCountForType(mData[i]);
|
|
|
|
segIndex++;
|
|
|
|
}
|
|
|
|
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt");
|
2010-11-08 18:07:00 +03:00
|
|
|
|
2017-02-06 18:21:54 +03:00
|
|
|
return std::max(1U, segIndex) -
|
|
|
|
1; // -1 because while loop takes us 1 too far
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
2011-04-20 13:16:02 +04:00
|
|
|
/**
|
|
|
|
* The SVG spec says we have to paint stroke caps for zero length subpaths:
|
|
|
|
*
|
|
|
|
* http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
|
|
|
|
*
|
|
|
|
* Cairo only does this for |stroke-linecap: round| and not for
|
|
|
|
* |stroke-linecap: square| (since that's what Adobe Acrobat has always done).
|
2013-11-02 15:10:38 +04:00
|
|
|
* Most likely the other backends that DrawTarget uses have the same behavior.
|
2011-04-20 13:16:02 +04:00
|
|
|
*
|
|
|
|
* To help us conform to the SVG spec we have this helper function to draw an
|
|
|
|
* approximation of square caps for zero length subpaths. It does this by
|
2013-11-02 15:10:38 +04:00
|
|
|
* inserting a subpath containing a single user space axis aligned straight
|
|
|
|
* line that is as small as it can be while minimizing the risk of it being
|
|
|
|
* thrown away by the DrawTarget's backend for being too small to affect
|
|
|
|
* rendering. The idea is that we'll then get stroke caps drawn for this axis
|
|
|
|
* aligned line, creating an axis aligned rectangle that approximates the
|
|
|
|
* square that would ideally be drawn.
|
|
|
|
*
|
|
|
|
* Since we don't have any information about transforms from user space to
|
|
|
|
* device space, we choose the length of the small line that we insert by
|
|
|
|
* making it a small percentage of the stroke width of the path. This should
|
|
|
|
* hopefully allow us to make the line as long as possible (to avoid rounding
|
|
|
|
* issues in the backend resulting in the backend seeing it as having zero
|
2018-03-18 21:09:51 +03:00
|
|
|
* length) while still avoiding the small rectangle being noticeably different
|
2013-11-02 15:10:38 +04:00
|
|
|
* from a square.
|
2011-04-20 13:16:02 +04:00
|
|
|
*
|
|
|
|
* Note that this function inserts a subpath into the current gfx path that
|
|
|
|
* will be present during both fill and stroke operations.
|
|
|
|
*/
|
2013-11-02 15:10:38 +04:00
|
|
|
static void ApproximateZeroLengthSubpathSquareCaps(PathBuilder* aPB,
|
|
|
|
const Point& aPoint,
|
|
|
|
Float aStrokeWidth) {
|
|
|
|
// Note that caps are proportional to stroke width, so if stroke width is
|
|
|
|
// zero it's actually fine for |tinyLength| below to end up being zero.
|
|
|
|
// However, it would be a waste to inserting a LineTo in that case, so better
|
|
|
|
// not to.
|
|
|
|
MOZ_ASSERT(aStrokeWidth > 0.0f,
|
|
|
|
"Make the caller check for this, or check it here");
|
2011-04-20 13:16:02 +04:00
|
|
|
|
2013-11-02 15:10:38 +04:00
|
|
|
// The fraction of the stroke width that we choose for the length of the
|
|
|
|
// line is rather arbitrary, other than being chosen to meet the requirements
|
|
|
|
// described in the comment above.
|
2011-04-20 13:16:02 +04:00
|
|
|
|
2015-08-01 11:09:00 +03:00
|
|
|
Float tinyLength = aStrokeWidth / SVG_ZERO_LENGTH_PATH_FIX_FACTOR;
|
2013-11-02 15:10:38 +04:00
|
|
|
|
|
|
|
aPB->LineTo(aPoint + Point(tinyLength, 0));
|
|
|
|
aPB->MoveTo(aPoint);
|
2011-04-20 13:16:02 +04:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:21:09 +03:00
|
|
|
#define MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT \
|
|
|
|
do { \
|
|
|
|
if (!subpathHasLength && hasLineCaps && aStrokeWidth > 0 && \
|
2018-08-21 23:59:15 +03:00
|
|
|
subpathContainsNonMoveTo && IsValidType(prevSegType) && \
|
|
|
|
(!IsMoveto(prevSegType) || IsClosePath(segType))) { \
|
2018-08-22 00:21:09 +03:00
|
|
|
ApproximateZeroLengthSubpathSquareCaps(aBuilder, segStart, \
|
|
|
|
aStrokeWidth); \
|
|
|
|
} \
|
2013-10-24 19:50:27 +04:00
|
|
|
} while (0)
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-08-22 00:21:09 +03:00
|
|
|
already_AddRefed<Path> SVGPathData::BuildPath(PathBuilder* aBuilder,
|
2013-11-02 15:10:38 +04:00
|
|
|
uint8_t aStrokeLineCap,
|
|
|
|
Float aStrokeWidth) const {
|
2013-10-24 19:50:27 +04:00
|
|
|
if (mData.IsEmpty() || !IsMoveto(SVGPathSegUtils::DecodeType(mData[0]))) {
|
|
|
|
return nullptr; // paths without an initial moveto are invalid
|
|
|
|
}
|
|
|
|
|
2014-10-03 12:50:42 +04:00
|
|
|
bool hasLineCaps = aStrokeLineCap != NS_STYLE_STROKE_LINECAP_BUTT;
|
2013-10-24 19:50:27 +04:00
|
|
|
bool subpathHasLength = false; // visual length
|
2014-10-03 12:50:43 +04:00
|
|
|
bool subpathContainsNonMoveTo = false;
|
2013-10-24 19:50:27 +04:00
|
|
|
|
|
|
|
uint32_t segType = PATHSEG_UNKNOWN;
|
|
|
|
uint32_t prevSegType = PATHSEG_UNKNOWN;
|
|
|
|
Point pathStart(0.0, 0.0); // start point of [sub]path
|
|
|
|
Point segStart(0.0, 0.0);
|
|
|
|
Point segEnd;
|
|
|
|
Point cp1, cp2; // previous bezier's control points
|
|
|
|
Point tcp1, tcp2; // temporaries
|
|
|
|
|
|
|
|
// Regarding cp1 and cp2: If the previous segment was a cubic bezier curve,
|
|
|
|
// then cp2 is its second control point. If the previous segment was a
|
|
|
|
// quadratic curve, then cp1 is its (only) control point.
|
|
|
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
while (i < mData.Length()) {
|
|
|
|
segType = SVGPathSegUtils::DecodeType(mData[i++]);
|
|
|
|
uint32_t argCount = SVGPathSegUtils::ArgCountForType(segType);
|
|
|
|
|
|
|
|
switch (segType) {
|
|
|
|
case PATHSEG_CLOSEPATH:
|
|
|
|
// set this early to allow drawing of square caps for "M{x},{y} Z":
|
2014-10-03 12:50:43 +04:00
|
|
|
subpathContainsNonMoveTo = true;
|
2013-10-24 19:50:27 +04:00
|
|
|
MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT;
|
|
|
|
segEnd = pathStart;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->Close();
|
2013-10-24 19:50:27 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PATHSEG_MOVETO_ABS:
|
2014-10-03 12:50:43 +04:00
|
|
|
MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT;
|
2013-10-24 19:50:27 +04:00
|
|
|
pathStart = segEnd = Point(mData[i], mData[i + 1]);
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->MoveTo(segEnd);
|
2014-10-03 12:50:43 +04:00
|
|
|
subpathHasLength = false;
|
2013-10-24 19:50:27 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PATHSEG_MOVETO_REL:
|
|
|
|
MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT;
|
|
|
|
pathStart = segEnd = segStart + Point(mData[i], mData[i + 1]);
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->MoveTo(segEnd);
|
2013-10-24 19:50:27 +04:00
|
|
|
subpathHasLength = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
|
2013-10-24 19:50:27 +04:00
|
|
|
case PATHSEG_LINETO_ABS:
|
|
|
|
segEnd = Point(mData[i], mData[i + 1]);
|
|
|
|
if (segEnd != segStart) {
|
2014-10-03 12:50:43 +04:00
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->LineTo(segEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-10-24 19:50:27 +04:00
|
|
|
case PATHSEG_LINETO_REL:
|
|
|
|
segEnd = segStart + Point(mData[i], mData[i + 1]);
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart) {
|
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->LineTo(segEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-10-24 19:50:27 +04:00
|
|
|
case PATHSEG_CURVETO_CUBIC_ABS:
|
|
|
|
cp1 = Point(mData[i], mData[i + 1]);
|
|
|
|
cp2 = Point(mData[i + 2], mData[i + 3]);
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = Point(mData[i + 4], mData[i + 5]);
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart || segEnd != cp1 || segEnd != cp2) {
|
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->BezierTo(cp1, cp2, segEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-10-24 19:50:27 +04:00
|
|
|
case PATHSEG_CURVETO_CUBIC_REL:
|
|
|
|
cp1 = segStart + Point(mData[i], mData[i + 1]);
|
|
|
|
cp2 = segStart + Point(mData[i + 2], mData[i + 3]);
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = segStart + Point(mData[i + 4], mData[i + 5]);
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart || segEnd != cp1 || segEnd != cp2) {
|
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->BezierTo(cp1, cp2, segEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-10-24 19:50:27 +04:00
|
|
|
case PATHSEG_CURVETO_QUADRATIC_ABS:
|
|
|
|
cp1 = Point(mData[i], mData[i + 1]);
|
|
|
|
// Convert quadratic curve to cubic curve:
|
|
|
|
tcp1 = segStart + (cp1 - segStart) * 2 / 3;
|
|
|
|
segEnd = Point(mData[i + 2], mData[i + 3]); // set before setting tcp2!
|
|
|
|
tcp2 = cp1 + (segEnd - cp1) / 3;
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart || segEnd != cp1) {
|
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->BezierTo(tcp1, tcp2, segEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-10-24 19:50:27 +04:00
|
|
|
case PATHSEG_CURVETO_QUADRATIC_REL:
|
|
|
|
cp1 = segStart + Point(mData[i], mData[i + 1]);
|
|
|
|
// Convert quadratic curve to cubic curve:
|
|
|
|
tcp1 = segStart + (cp1 - segStart) * 2 / 3;
|
|
|
|
segEnd = segStart +
|
|
|
|
Point(mData[i + 2], mData[i + 3]); // set before setting tcp2!
|
|
|
|
tcp2 = cp1 + (segEnd - cp1) / 3;
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart || segEnd != cp1) {
|
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->BezierTo(tcp1, tcp2, segEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-10-24 19:50:27 +04:00
|
|
|
case PATHSEG_ARC_ABS:
|
2012-12-23 08:54:22 +04:00
|
|
|
case PATHSEG_ARC_REL: {
|
2013-10-24 19:50:27 +04:00
|
|
|
Point radii(mData[i], mData[i + 1]);
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = Point(mData[i + 5], mData[i + 6]);
|
2013-10-24 19:50:27 +04:00
|
|
|
if (segType == PATHSEG_ARC_REL) {
|
|
|
|
segEnd += segStart;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart) {
|
|
|
|
subpathHasLength = true;
|
2013-10-24 19:50:27 +04:00
|
|
|
if (radii.x == 0.0f || radii.y == 0.0f) {
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->LineTo(segEnd);
|
2013-10-24 19:50:27 +04:00
|
|
|
} else {
|
2018-12-21 23:19:15 +03:00
|
|
|
SVGArcConverter converter(segStart, segEnd, radii, mData[i + 2],
|
|
|
|
mData[i + 3] != 0, mData[i + 4] != 0);
|
2013-10-24 19:50:27 +04:00
|
|
|
while (converter.GetNextSegment(&cp1, &cp2, &segEnd)) {
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->BezierTo(cp1, cp2, segEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-10-24 19:50:27 +04:00
|
|
|
}
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
2013-10-24 19:50:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
case PATHSEG_LINETO_HORIZONTAL_ABS:
|
|
|
|
segEnd = Point(mData[i], segStart.y);
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart) {
|
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->LineTo(segEnd);
|
2013-10-24 19:50:27 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PATHSEG_LINETO_HORIZONTAL_REL:
|
|
|
|
segEnd = segStart + Point(mData[i], 0.0f);
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart) {
|
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->LineTo(segEnd);
|
2013-10-24 19:50:27 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PATHSEG_LINETO_VERTICAL_ABS:
|
|
|
|
segEnd = Point(segStart.x, mData[i]);
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart) {
|
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->LineTo(segEnd);
|
2013-10-24 19:50:27 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PATHSEG_LINETO_VERTICAL_REL:
|
|
|
|
segEnd = segStart + Point(0.0f, mData[i]);
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart) {
|
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->LineTo(segEnd);
|
2013-10-24 19:50:27 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PATHSEG_CURVETO_CUBIC_SMOOTH_ABS:
|
|
|
|
cp1 = SVGPathSegUtils::IsCubicType(prevSegType) ? segStart * 2 - cp2
|
|
|
|
: segStart;
|
|
|
|
cp2 = Point(mData[i], mData[i + 1]);
|
|
|
|
segEnd = Point(mData[i + 2], mData[i + 3]);
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart || segEnd != cp1 || segEnd != cp2) {
|
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->BezierTo(cp1, cp2, segEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-10-24 19:50:27 +04:00
|
|
|
case PATHSEG_CURVETO_CUBIC_SMOOTH_REL:
|
|
|
|
cp1 = SVGPathSegUtils::IsCubicType(prevSegType) ? segStart * 2 - cp2
|
|
|
|
: segStart;
|
|
|
|
cp2 = segStart + Point(mData[i], mData[i + 1]);
|
|
|
|
segEnd = segStart + Point(mData[i + 2], mData[i + 3]);
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart || segEnd != cp1 || segEnd != cp2) {
|
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->BezierTo(cp1, cp2, segEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-10-24 19:50:27 +04:00
|
|
|
case PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS:
|
|
|
|
cp1 = SVGPathSegUtils::IsQuadraticType(prevSegType) ? segStart * 2 - cp1
|
|
|
|
: segStart;
|
|
|
|
// Convert quadratic curve to cubic curve:
|
|
|
|
tcp1 = segStart + (cp1 - segStart) * 2 / 3;
|
|
|
|
segEnd = Point(mData[i], mData[i + 1]); // set before setting tcp2!
|
|
|
|
tcp2 = cp1 + (segEnd - cp1) / 3;
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart || segEnd != cp1) {
|
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->BezierTo(tcp1, tcp2, segEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-10-24 19:50:27 +04:00
|
|
|
case PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL:
|
|
|
|
cp1 = SVGPathSegUtils::IsQuadraticType(prevSegType) ? segStart * 2 - cp1
|
|
|
|
: segStart;
|
|
|
|
// Convert quadratic curve to cubic curve:
|
|
|
|
tcp1 = segStart + (cp1 - segStart) * 2 / 3;
|
|
|
|
segEnd = segStart +
|
|
|
|
Point(mData[i], mData[i + 1]); // changed before setting tcp2!
|
|
|
|
tcp2 = cp1 + (segEnd - cp1) / 3;
|
2014-10-03 12:50:43 +04:00
|
|
|
if (segEnd != segStart || segEnd != cp1) {
|
|
|
|
subpathHasLength = true;
|
2018-08-22 00:21:09 +03:00
|
|
|
aBuilder->BezierTo(tcp1, tcp2, segEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-10-24 19:50:27 +04:00
|
|
|
default:
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Bad path segment type");
|
2013-10-24 19:50:27 +04:00
|
|
|
return nullptr; // according to spec we'd use everything up to the bad
|
|
|
|
// seg anyway
|
|
|
|
}
|
2014-10-03 12:50:43 +04:00
|
|
|
|
|
|
|
subpathContainsNonMoveTo =
|
|
|
|
segType != PATHSEG_MOVETO_ABS && segType != PATHSEG_MOVETO_REL;
|
2013-10-24 19:50:27 +04:00
|
|
|
i += argCount;
|
|
|
|
prevSegType = segType;
|
|
|
|
segStart = segEnd;
|
|
|
|
}
|
|
|
|
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt");
|
|
|
|
MOZ_ASSERT(prevSegType == segType,
|
|
|
|
"prevSegType should be left at the final segType");
|
2013-10-24 19:50:27 +04:00
|
|
|
|
|
|
|
MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT;
|
|
|
|
|
2018-08-22 00:21:09 +03:00
|
|
|
return aBuilder->Finish();
|
2013-10-24 19:50:27 +04:00
|
|
|
}
|
|
|
|
|
2014-10-04 15:13:30 +04:00
|
|
|
already_AddRefed<Path> SVGPathData::BuildPathForMeasuring() const {
|
2013-11-18 05:29:06 +04:00
|
|
|
// Since the path that we return will not be used for painting it doesn't
|
2014-07-06 00:53:04 +04:00
|
|
|
// matter what we pass to CreatePathBuilder as aFillRule. Hawever, we do want
|
|
|
|
// to pass something other than NS_STYLE_STROKE_LINECAP_SQUARE as
|
|
|
|
// aStrokeLineCap to avoid the insertion of extra little lines (by
|
2013-11-18 05:29:06 +04:00
|
|
|
// ApproximateZeroLengthSubpathSquareCaps), in which case the value that we
|
|
|
|
// pass as aStrokeWidth doesn't matter (since it's only used to determine the
|
|
|
|
// length of those extra little lines).
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DrawTarget> drawTarget =
|
2014-10-04 15:13:30 +04:00
|
|
|
gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PathBuilder> builder =
|
2014-10-04 15:13:30 +04:00
|
|
|
drawTarget->CreatePathBuilder(FillRule::FILL_WINDING);
|
|
|
|
return BuildPath(builder, NS_STYLE_STROKE_LINECAP_BUTT, 0);
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
2018-08-21 23:59:15 +03:00
|
|
|
// We could simplify this function because this is only used by CSS motion path
|
|
|
|
// and clip-path, which don't render the SVG Path. i.e. The returned path is
|
|
|
|
// used as a reference.
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<Path> SVGPathData::BuildPath(
|
2018-08-21 23:59:15 +03:00
|
|
|
const nsTArray<StylePathCommand>& aPath, PathBuilder* aBuilder,
|
|
|
|
uint8_t aStrokeLineCap, Float aStrokeWidth, float aZoomFactor) {
|
|
|
|
if (aPath.IsEmpty() || !aPath[0].IsMoveTo()) {
|
|
|
|
return nullptr; // paths without an initial moveto are invalid
|
|
|
|
}
|
|
|
|
|
|
|
|
auto toGfxPoint = [](const StyleCoordPair& aPair) {
|
|
|
|
return Point(aPair._0, aPair._1);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto isCubicType = [](StylePathCommand::Tag aType) {
|
|
|
|
return aType == StylePathCommand::Tag::CurveTo ||
|
|
|
|
aType == StylePathCommand::Tag::SmoothCurveTo;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto isQuadraticType = [](StylePathCommand::Tag aType) {
|
|
|
|
return aType == StylePathCommand::Tag::QuadBezierCurveTo ||
|
|
|
|
aType == StylePathCommand::Tag::SmoothQuadBezierCurveTo;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool hasLineCaps = aStrokeLineCap != NS_STYLE_STROKE_LINECAP_BUTT;
|
|
|
|
bool subpathHasLength = false; // visual length
|
|
|
|
bool subpathContainsNonMoveTo = false;
|
|
|
|
|
|
|
|
StylePathCommand::Tag segType = StylePathCommand::Tag::Unknown;
|
|
|
|
StylePathCommand::Tag prevSegType = StylePathCommand::Tag::Unknown;
|
|
|
|
Point pathStart(0.0, 0.0); // start point of [sub]path
|
|
|
|
Point segStart(0.0, 0.0);
|
|
|
|
Point segEnd;
|
|
|
|
Point cp1, cp2; // previous bezier's control points
|
|
|
|
Point tcp1, tcp2; // temporaries
|
|
|
|
|
2018-08-15 22:27:38 +03:00
|
|
|
auto scale = [aZoomFactor](const Point& p) {
|
|
|
|
return Point(p.x * aZoomFactor, p.y * aZoomFactor);
|
|
|
|
};
|
|
|
|
|
2018-08-21 23:59:15 +03:00
|
|
|
// Regarding cp1 and cp2: If the previous segment was a cubic bezier curve,
|
|
|
|
// then cp2 is its second control point. If the previous segment was a
|
|
|
|
// quadratic curve, then cp1 is its (only) control point.
|
|
|
|
|
|
|
|
for (const StylePathCommand& cmd : aPath) {
|
|
|
|
segType = cmd.tag;
|
|
|
|
switch (segType) {
|
|
|
|
case StylePathCommand::Tag::ClosePath:
|
|
|
|
// set this early to allow drawing of square caps for "M{x},{y} Z":
|
|
|
|
subpathContainsNonMoveTo = true;
|
|
|
|
MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT;
|
|
|
|
segEnd = pathStart;
|
|
|
|
aBuilder->Close();
|
|
|
|
break;
|
|
|
|
case StylePathCommand::Tag::MoveTo: {
|
|
|
|
MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT;
|
|
|
|
const Point& p = toGfxPoint(cmd.move_to.point);
|
2018-09-08 01:25:59 +03:00
|
|
|
pathStart = segEnd =
|
|
|
|
cmd.move_to.absolute == StyleIsAbsolute::Yes ? p : segStart + p;
|
2018-08-15 22:27:38 +03:00
|
|
|
aBuilder->MoveTo(scale(segEnd));
|
2018-08-21 23:59:15 +03:00
|
|
|
subpathHasLength = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StylePathCommand::Tag::LineTo: {
|
|
|
|
const Point& p = toGfxPoint(cmd.line_to.point);
|
2018-09-08 01:25:59 +03:00
|
|
|
segEnd =
|
|
|
|
cmd.line_to.absolute == StyleIsAbsolute::Yes ? p : segStart + p;
|
2018-08-21 23:59:15 +03:00
|
|
|
if (segEnd != segStart) {
|
|
|
|
subpathHasLength = true;
|
2018-08-15 22:27:38 +03:00
|
|
|
aBuilder->LineTo(scale(segEnd));
|
2018-08-21 23:59:15 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StylePathCommand::Tag::CurveTo:
|
|
|
|
cp1 = toGfxPoint(cmd.curve_to.control1);
|
|
|
|
cp2 = toGfxPoint(cmd.curve_to.control2);
|
|
|
|
segEnd = toGfxPoint(cmd.curve_to.point);
|
|
|
|
|
2018-09-08 01:25:59 +03:00
|
|
|
if (cmd.curve_to.absolute == StyleIsAbsolute::No) {
|
2018-08-21 23:59:15 +03:00
|
|
|
cp1 += segStart;
|
|
|
|
cp2 += segStart;
|
|
|
|
segEnd += segStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (segEnd != segStart || segEnd != cp1 || segEnd != cp2) {
|
|
|
|
subpathHasLength = true;
|
2018-08-15 22:27:38 +03:00
|
|
|
aBuilder->BezierTo(scale(cp1), scale(cp2), scale(segEnd));
|
2018-08-21 23:59:15 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case StylePathCommand::Tag::QuadBezierCurveTo:
|
|
|
|
cp1 = toGfxPoint(cmd.quad_bezier_curve_to.control1);
|
|
|
|
segEnd = toGfxPoint(cmd.quad_bezier_curve_to.point);
|
|
|
|
|
2018-09-08 01:25:59 +03:00
|
|
|
if (cmd.quad_bezier_curve_to.absolute == StyleIsAbsolute::No) {
|
2018-08-21 23:59:15 +03:00
|
|
|
cp1 += segStart;
|
|
|
|
segEnd += segStart; // set before setting tcp2!
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert quadratic curve to cubic curve:
|
|
|
|
tcp1 = segStart + (cp1 - segStart) * 2 / 3;
|
|
|
|
tcp2 = cp1 + (segEnd - cp1) / 3;
|
|
|
|
|
|
|
|
if (segEnd != segStart || segEnd != cp1) {
|
|
|
|
subpathHasLength = true;
|
2018-08-15 22:27:38 +03:00
|
|
|
aBuilder->BezierTo(scale(tcp1), scale(tcp2), scale(segEnd));
|
2018-08-21 23:59:15 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case StylePathCommand::Tag::EllipticalArc: {
|
|
|
|
const auto& arc = cmd.elliptical_arc;
|
|
|
|
Point radii(arc.rx, arc.ry);
|
|
|
|
segEnd = toGfxPoint(arc.point);
|
2018-09-08 01:25:59 +03:00
|
|
|
if (arc.absolute == StyleIsAbsolute::No) {
|
2018-08-21 23:59:15 +03:00
|
|
|
segEnd += segStart;
|
|
|
|
}
|
|
|
|
if (segEnd != segStart) {
|
|
|
|
subpathHasLength = true;
|
|
|
|
if (radii.x == 0.0f || radii.y == 0.0f) {
|
2018-08-15 22:27:38 +03:00
|
|
|
aBuilder->LineTo(scale(segEnd));
|
2018-08-21 23:59:15 +03:00
|
|
|
} else {
|
2018-12-21 23:19:15 +03:00
|
|
|
SVGArcConverter converter(segStart, segEnd, radii, arc.angle,
|
|
|
|
arc.large_arc_flag._0, arc.sweep_flag._0);
|
2018-08-21 23:59:15 +03:00
|
|
|
while (converter.GetNextSegment(&cp1, &cp2, &segEnd)) {
|
2018-08-15 22:27:38 +03:00
|
|
|
aBuilder->BezierTo(scale(cp1), scale(cp2), scale(segEnd));
|
2018-08-21 23:59:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StylePathCommand::Tag::HorizontalLineTo:
|
2018-09-08 01:25:59 +03:00
|
|
|
if (cmd.horizontal_line_to.absolute == StyleIsAbsolute::Yes) {
|
2018-08-21 23:59:15 +03:00
|
|
|
segEnd = Point(cmd.horizontal_line_to.x, segStart.y);
|
|
|
|
} else {
|
|
|
|
segEnd = segStart + Point(cmd.horizontal_line_to.x, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (segEnd != segStart) {
|
|
|
|
subpathHasLength = true;
|
2018-08-15 22:27:38 +03:00
|
|
|
aBuilder->LineTo(scale(segEnd));
|
2018-08-21 23:59:15 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case StylePathCommand::Tag::VerticalLineTo:
|
2018-09-08 01:25:59 +03:00
|
|
|
if (cmd.vertical_line_to.absolute == StyleIsAbsolute::Yes) {
|
2018-08-21 23:59:15 +03:00
|
|
|
segEnd = Point(segStart.x, cmd.vertical_line_to.y);
|
|
|
|
} else {
|
|
|
|
segEnd = segStart + Point(0.0f, cmd.vertical_line_to.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (segEnd != segStart) {
|
|
|
|
subpathHasLength = true;
|
2018-08-15 22:27:38 +03:00
|
|
|
aBuilder->LineTo(scale(segEnd));
|
2018-08-21 23:59:15 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case StylePathCommand::Tag::SmoothCurveTo:
|
|
|
|
cp1 = isCubicType(prevSegType) ? segStart * 2 - cp2 : segStart;
|
|
|
|
cp2 = toGfxPoint(cmd.smooth_curve_to.control2);
|
|
|
|
segEnd = toGfxPoint(cmd.smooth_curve_to.point);
|
|
|
|
|
2018-09-08 01:25:59 +03:00
|
|
|
if (cmd.smooth_curve_to.absolute == StyleIsAbsolute::No) {
|
2018-08-21 23:59:15 +03:00
|
|
|
cp2 += segStart;
|
|
|
|
segEnd += segStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (segEnd != segStart || segEnd != cp1 || segEnd != cp2) {
|
|
|
|
subpathHasLength = true;
|
2018-08-15 22:27:38 +03:00
|
|
|
aBuilder->BezierTo(scale(cp1), scale(cp2), scale(segEnd));
|
2018-08-21 23:59:15 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case StylePathCommand::Tag::SmoothQuadBezierCurveTo: {
|
|
|
|
cp1 = isQuadraticType(prevSegType) ? segStart * 2 - cp1 : segStart;
|
|
|
|
// Convert quadratic curve to cubic curve:
|
|
|
|
tcp1 = segStart + (cp1 - segStart) * 2 / 3;
|
|
|
|
|
|
|
|
const Point& p = toGfxPoint(cmd.smooth_quad_bezier_curve_to.point);
|
|
|
|
// set before setting tcp2!
|
2018-09-08 01:25:59 +03:00
|
|
|
segEnd =
|
|
|
|
cmd.smooth_quad_bezier_curve_to.absolute == StyleIsAbsolute::Yes
|
|
|
|
? p
|
|
|
|
: segStart + p;
|
2018-08-21 23:59:15 +03:00
|
|
|
tcp2 = cp1 + (segEnd - cp1) / 3;
|
|
|
|
|
|
|
|
if (segEnd != segStart || segEnd != cp1) {
|
|
|
|
subpathHasLength = true;
|
2018-08-15 22:27:38 +03:00
|
|
|
aBuilder->BezierTo(scale(tcp1), scale(tcp2), scale(segEnd));
|
2018-08-21 23:59:15 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StylePathCommand::Tag::Unknown:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unacceptable path segment type");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
subpathContainsNonMoveTo = !IsMoveto(segType);
|
|
|
|
prevSegType = segType;
|
|
|
|
segStart = segEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(prevSegType == segType,
|
|
|
|
"prevSegType should be left at the final segType");
|
|
|
|
|
|
|
|
MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT;
|
|
|
|
|
|
|
|
return aBuilder->Finish();
|
|
|
|
}
|
|
|
|
|
2014-03-19 05:13:19 +04:00
|
|
|
static double AngleOfVector(const Point& aVector) {
|
2010-11-08 18:07:00 +03:00
|
|
|
// C99 says about atan2 "A domain error may occur if both arguments are
|
|
|
|
// zero" and "On a domain error, the function returns an implementation-
|
|
|
|
// defined value". In the case of atan2 the implementation-defined value
|
|
|
|
// seems to commonly be zero, but it could just as easily be a NaN value.
|
|
|
|
// We specifically want zero in this case, hence the check:
|
|
|
|
|
2014-03-19 05:13:19 +04:00
|
|
|
return (aVector != Point(0.0, 0.0)) ? atan2(aVector.y, aVector.x) : 0.0;
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
2015-02-18 01:15:15 +03:00
|
|
|
static float AngleOfVector(const Point& cp1, const Point& cp2) {
|
|
|
|
return static_cast<float>(AngleOfVector(cp1 - cp2));
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
2019-01-26 16:01:31 +03:00
|
|
|
void SVGPathData::GetMarkerPositioningData(nsTArray<SVGMark>* aMarks) const {
|
2010-11-08 18:07:00 +03:00
|
|
|
// This code should assume that ANY type of segment can appear at ANY index.
|
|
|
|
// It should also assume that segments such as M and Z can appear in weird
|
|
|
|
// places, and repeat multiple times consecutively.
|
|
|
|
|
2010-12-14 23:48:28 +03:00
|
|
|
// info on current [sub]path (reset every M command):
|
2014-03-19 05:13:19 +04:00
|
|
|
Point pathStart(0.0, 0.0);
|
2010-12-14 23:48:28 +03:00
|
|
|
float pathStartAngle = 0.0f;
|
2010-11-08 18:07:00 +03:00
|
|
|
|
2010-12-14 23:48:28 +03:00
|
|
|
// info on previous segment:
|
2012-12-23 08:54:22 +04:00
|
|
|
uint16_t prevSegType = PATHSEG_UNKNOWN;
|
2014-03-19 05:13:19 +04:00
|
|
|
Point prevSegEnd(0.0, 0.0);
|
2010-12-14 23:48:28 +03:00
|
|
|
float prevSegEndAngle = 0.0f;
|
2014-03-19 05:13:19 +04:00
|
|
|
Point prevCP; // if prev seg was a bezier, this was its last control point
|
2010-11-08 18:07:00 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
2010-11-08 18:07:00 +03:00
|
|
|
while (i < mData.Length()) {
|
2010-12-14 23:48:28 +03:00
|
|
|
// info on current segment:
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t segType =
|
2010-12-14 23:48:28 +03:00
|
|
|
SVGPathSegUtils::DecodeType(mData[i++]); // advances i to args
|
2014-03-19 05:13:19 +04:00
|
|
|
Point& segStart = prevSegEnd;
|
|
|
|
Point segEnd;
|
2010-12-14 23:48:28 +03:00
|
|
|
float segStartAngle, segEndAngle;
|
2010-11-08 18:07:00 +03:00
|
|
|
|
|
|
|
switch (segType) // to find segStartAngle, segEnd and segEndAngle
|
|
|
|
{
|
2012-12-23 08:54:22 +04:00
|
|
|
case PATHSEG_CLOSEPATH:
|
2010-11-08 18:07:00 +03:00
|
|
|
segEnd = pathStart;
|
2015-02-18 01:15:15 +03:00
|
|
|
segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
|
2010-11-08 18:07:00 +03:00
|
|
|
break;
|
|
|
|
|
2012-12-23 08:54:22 +04:00
|
|
|
case PATHSEG_MOVETO_ABS:
|
|
|
|
case PATHSEG_MOVETO_REL:
|
|
|
|
if (segType == PATHSEG_MOVETO_ABS) {
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = Point(mData[i], mData[i + 1]);
|
2010-11-08 18:07:00 +03:00
|
|
|
} else {
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = segStart + Point(mData[i], mData[i + 1]);
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
2015-02-18 01:15:15 +03:00
|
|
|
pathStart = segEnd;
|
2010-11-08 18:07:00 +03:00
|
|
|
// If authors are going to specify multiple consecutive moveto commands
|
2015-02-18 01:15:15 +03:00
|
|
|
// with markers, me might as well make the angle do something useful:
|
|
|
|
segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
|
2010-11-08 18:07:00 +03:00
|
|
|
i += 2;
|
|
|
|
break;
|
|
|
|
|
2012-12-23 08:54:22 +04:00
|
|
|
case PATHSEG_LINETO_ABS:
|
|
|
|
case PATHSEG_LINETO_REL:
|
|
|
|
if (segType == PATHSEG_LINETO_ABS) {
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = Point(mData[i], mData[i + 1]);
|
2010-11-08 18:07:00 +03:00
|
|
|
} else {
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = segStart + Point(mData[i], mData[i + 1]);
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
2015-02-18 01:15:15 +03:00
|
|
|
segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
|
2010-11-08 18:07:00 +03:00
|
|
|
i += 2;
|
|
|
|
break;
|
|
|
|
|
2012-12-23 08:54:22 +04:00
|
|
|
case PATHSEG_CURVETO_CUBIC_ABS:
|
|
|
|
case PATHSEG_CURVETO_CUBIC_REL: {
|
2011-09-29 10:19:26 +04:00
|
|
|
Point cp1, cp2; // control points
|
2012-12-23 08:54:22 +04:00
|
|
|
if (segType == PATHSEG_CURVETO_CUBIC_ABS) {
|
2014-03-19 05:13:19 +04:00
|
|
|
cp1 = Point(mData[i], mData[i + 1]);
|
|
|
|
cp2 = Point(mData[i + 2], mData[i + 3]);
|
|
|
|
segEnd = Point(mData[i + 4], mData[i + 5]);
|
2010-11-08 18:07:00 +03:00
|
|
|
} else {
|
2014-03-19 05:13:19 +04:00
|
|
|
cp1 = segStart + Point(mData[i], mData[i + 1]);
|
|
|
|
cp2 = segStart + Point(mData[i + 2], mData[i + 3]);
|
|
|
|
segEnd = segStart + Point(mData[i + 4], mData[i + 5]);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-11-08 18:07:00 +03:00
|
|
|
prevCP = cp2;
|
2015-02-18 01:15:15 +03:00
|
|
|
segStartAngle = AngleOfVector(
|
|
|
|
cp1 == segStart ? (cp1 == cp2 ? segEnd : cp2) : cp1, segStart);
|
|
|
|
segEndAngle = AngleOfVector(
|
|
|
|
segEnd, cp2 == segEnd ? (cp1 == cp2 ? segStart : cp1) : cp2);
|
2018-11-30 13:46:48 +03:00
|
|
|
i += 6;
|
|
|
|
break;
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
2012-12-23 08:54:22 +04:00
|
|
|
case PATHSEG_CURVETO_QUADRATIC_ABS:
|
2010-11-08 18:07:00 +03:00
|
|
|
case PATHSEG_CURVETO_QUADRATIC_REL: {
|
|
|
|
Point cp1; // control point
|
|
|
|
if (segType == PATHSEG_CURVETO_QUADRATIC_ABS) {
|
|
|
|
cp1 = Point(mData[i], mData[i + 1]);
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = Point(mData[i + 2], mData[i + 3]);
|
2010-11-08 18:07:00 +03:00
|
|
|
} else {
|
2014-03-19 05:13:19 +04:00
|
|
|
cp1 = segStart + Point(mData[i], mData[i + 1]);
|
|
|
|
segEnd = segStart + Point(mData[i + 2], mData[i + 3]);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-11-08 18:07:00 +03:00
|
|
|
prevCP = cp1;
|
|
|
|
segStartAngle = AngleOfVector(cp1 == segStart ? segEnd : cp1, segStart);
|
|
|
|
segEndAngle = AngleOfVector(segEnd, cp1 == segEnd ? segStart : cp1);
|
|
|
|
i += 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-24 19:50:27 +04:00
|
|
|
case PATHSEG_ARC_ABS:
|
2010-11-08 18:07:00 +03:00
|
|
|
case PATHSEG_ARC_REL: {
|
2011-01-03 12:26:28 +03:00
|
|
|
double rx = mData[i];
|
2010-11-08 18:07:00 +03:00
|
|
|
double ry = mData[i + 1];
|
|
|
|
double angle = mData[i + 2];
|
|
|
|
bool largeArcFlag = mData[i + 3] != 0.0f;
|
|
|
|
bool sweepFlag = mData[i + 4] != 0.0f;
|
|
|
|
if (segType == PATHSEG_ARC_ABS) {
|
|
|
|
segEnd = Point(mData[i + 5], mData[i + 6]);
|
|
|
|
} else {
|
|
|
|
segEnd = segStart + Point(mData[i + 5], mData[i + 6]);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-11-08 18:07:00 +03:00
|
|
|
|
|
|
|
// See section F.6 of SVG 1.1 for details on what we're doing here:
|
|
|
|
// http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-01-03 12:26:28 +03:00
|
|
|
if (segStart == segEnd) {
|
|
|
|
// F.6.2 says "If the endpoints (x1, y1) and (x2, y2) are identical,
|
2010-11-08 18:07:00 +03:00
|
|
|
// then this is equivalent to omitting the elliptical arc segment
|
2011-01-03 12:26:28 +03:00
|
|
|
// entirely." We take that very literally here, not adding a mark, and
|
|
|
|
// not even setting any of the 'prev' variables so that it's as if
|
|
|
|
// this arc had never existed; note the difference this will make e.g.
|
|
|
|
// if the arc is proceeded by a bezier curve and followed by a
|
|
|
|
// "smooth" bezier curve of the same degree!
|
|
|
|
i += 7;
|
|
|
|
continue;
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
2014-03-19 05:13:19 +04:00
|
|
|
// Below we have funny interleaving of F.6.6 (Correction of out-of-range
|
2010-11-08 18:07:00 +03:00
|
|
|
// radii) and F.6.5 (Conversion from endpoint to center
|
|
|
|
// parameterization) which is designed to avoid some unnecessary
|
|
|
|
// calculations.
|
|
|
|
|
|
|
|
if (rx == 0.0 || ry == 0.0) {
|
2012-02-20 04:37:22 +04:00
|
|
|
// F.6.6 step 1 - straight line or coincidental points
|
|
|
|
segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
|
2018-11-30 13:46:48 +03:00
|
|
|
i += 7;
|
|
|
|
break;
|
|
|
|
}
|
2012-02-20 04:37:22 +04:00
|
|
|
rx = fabs(rx); // F.6.6.1
|
2010-11-08 18:07:00 +03:00
|
|
|
ry = fabs(ry);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-11-08 18:07:00 +03:00
|
|
|
// F.6.5.1:
|
2012-02-20 04:37:22 +04:00
|
|
|
angle = angle * M_PI / 180.0;
|
|
|
|
double x1p = cos(angle) * (segStart.x - segEnd.x) / 2.0 +
|
2011-01-03 12:26:28 +03:00
|
|
|
sin(angle) * (segStart.y - segEnd.y) / 2.0;
|
|
|
|
double y1p = -sin(angle) * (segStart.x - segEnd.x) / 2.0 +
|
|
|
|
cos(angle) * (segStart.y - segEnd.y) / 2.0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-02-20 04:37:22 +04:00
|
|
|
// This is the root in F.6.5.2 and the numerator under that root:
|
2011-01-03 12:26:28 +03:00
|
|
|
double root;
|
|
|
|
double numerator =
|
|
|
|
rx * rx * ry * ry - rx * rx * y1p * y1p - ry * ry * x1p * x1p;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-01-03 12:26:28 +03:00
|
|
|
if (numerator >= 0.0) {
|
2012-02-20 04:37:22 +04:00
|
|
|
root = sqrt(numerator / (rx * rx * y1p * y1p + ry * ry * x1p * x1p));
|
|
|
|
if (largeArcFlag == sweepFlag) root = -root;
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2011-01-03 12:26:28 +03:00
|
|
|
// F.6.6 step 3 - |numerator < 0.0|. This is equivalent to the result
|
|
|
|
// of F.6.6.2 (lamedh) being greater than one. What we have here is
|
|
|
|
// ellipse radii that are too small for the ellipse to reach between
|
2012-02-20 04:37:22 +04:00
|
|
|
// segStart and segEnd. We scale the radii up uniformly so that the
|
2010-11-08 18:07:00 +03:00
|
|
|
// ellipse is just big enough to fit (i.e. to the point where there is
|
2011-01-03 12:26:28 +03:00
|
|
|
// exactly one solution).
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-01-03 12:26:28 +03:00
|
|
|
double lamedh =
|
2010-11-08 18:07:00 +03:00
|
|
|
1.0 - numerator / (rx * rx * ry * ry); // equiv to eqn F.6.6.2
|
2011-01-03 12:26:28 +03:00
|
|
|
double s = sqrt(lamedh);
|
2010-11-08 18:07:00 +03:00
|
|
|
rx *= s; // F.6.6.3
|
2018-11-30 13:46:48 +03:00
|
|
|
ry *= s;
|
2011-01-03 12:26:28 +03:00
|
|
|
root = 0.0;
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
2012-12-23 08:54:22 +04:00
|
|
|
double cxp = root * rx * y1p / ry; // F.6.5.2
|
|
|
|
double cyp = -root * ry * x1p / rx;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-12-23 08:54:22 +04:00
|
|
|
double theta, delta;
|
|
|
|
theta = AngleOfVector(
|
2014-03-19 05:13:19 +04:00
|
|
|
Point((x1p - cxp) / rx, (y1p - cyp) / ry)); // F.6.5.5
|
2015-02-18 01:15:15 +03:00
|
|
|
delta = AngleOfVector(
|
|
|
|
Point((-x1p - cxp) / rx, (-y1p - cyp) / ry)) - // F.6.5.6
|
2018-11-30 13:46:48 +03:00
|
|
|
theta;
|
2010-11-08 18:07:00 +03:00
|
|
|
if (!sweepFlag && delta > 0)
|
|
|
|
delta -= 2.0 * M_PI;
|
2015-02-18 01:15:15 +03:00
|
|
|
else if (sweepFlag && delta < 0)
|
2010-11-08 18:07:00 +03:00
|
|
|
delta += 2.0 * M_PI;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-02-18 01:15:15 +03:00
|
|
|
double tx1, ty1, tx2, ty2;
|
|
|
|
tx1 = -cos(angle) * rx * sin(theta) - sin(angle) * ry * cos(theta);
|
2010-11-08 18:07:00 +03:00
|
|
|
ty1 = -sin(angle) * rx * sin(theta) + cos(angle) * ry * cos(theta);
|
|
|
|
tx2 = -cos(angle) * rx * sin(theta + delta) -
|
|
|
|
sin(angle) * ry * cos(theta + delta);
|
2015-02-18 01:15:15 +03:00
|
|
|
ty2 = -sin(angle) * rx * sin(theta + delta) +
|
2010-11-08 18:07:00 +03:00
|
|
|
cos(angle) * ry * cos(theta + delta);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-11-08 18:07:00 +03:00
|
|
|
if (delta < 0.0f) {
|
|
|
|
tx1 = -tx1;
|
|
|
|
ty1 = -ty1;
|
|
|
|
tx2 = -tx2;
|
|
|
|
ty2 = -ty2;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-11-08 18:07:00 +03:00
|
|
|
|
2014-03-19 05:13:19 +04:00
|
|
|
segStartAngle = static_cast<float>(atan2(ty1, tx1));
|
|
|
|
segEndAngle = static_cast<float>(atan2(ty2, tx2));
|
|
|
|
i += 7;
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
2012-12-23 08:54:22 +04:00
|
|
|
case PATHSEG_LINETO_HORIZONTAL_ABS:
|
|
|
|
case PATHSEG_LINETO_HORIZONTAL_REL:
|
|
|
|
if (segType == PATHSEG_LINETO_HORIZONTAL_ABS) {
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = Point(mData[i++], segStart.y);
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = segStart + Point(mData[i++], 0.0f);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-02-18 01:15:15 +03:00
|
|
|
segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
|
2012-12-23 08:54:22 +04:00
|
|
|
case PATHSEG_LINETO_VERTICAL_ABS:
|
|
|
|
case PATHSEG_LINETO_VERTICAL_REL:
|
|
|
|
if (segType == PATHSEG_LINETO_VERTICAL_ABS) {
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = Point(segStart.x, mData[i++]);
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = segStart + Point(0.0f, mData[i++]);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-02-18 01:15:15 +03:00
|
|
|
segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
|
2012-12-23 08:54:22 +04:00
|
|
|
case PATHSEG_CURVETO_CUBIC_SMOOTH_ABS:
|
|
|
|
case PATHSEG_CURVETO_CUBIC_SMOOTH_REL: {
|
2014-03-19 05:13:19 +04:00
|
|
|
Point cp1 = SVGPathSegUtils::IsCubicType(prevSegType)
|
2010-12-14 23:48:28 +03:00
|
|
|
? segStart * 2 - prevCP
|
|
|
|
: segStart;
|
2014-03-19 05:13:19 +04:00
|
|
|
Point cp2;
|
2012-12-23 08:54:22 +04:00
|
|
|
if (segType == PATHSEG_CURVETO_CUBIC_SMOOTH_ABS) {
|
2014-03-19 05:13:19 +04:00
|
|
|
cp2 = Point(mData[i], mData[i + 1]);
|
|
|
|
segEnd = Point(mData[i + 2], mData[i + 3]);
|
2010-11-08 18:07:00 +03:00
|
|
|
} else {
|
2014-03-19 05:13:19 +04:00
|
|
|
cp2 = segStart + Point(mData[i], mData[i + 1]);
|
|
|
|
segEnd = segStart + Point(mData[i + 2], mData[i + 3]);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-11-08 18:07:00 +03:00
|
|
|
prevCP = cp2;
|
2015-02-18 01:15:15 +03:00
|
|
|
segStartAngle = AngleOfVector(
|
|
|
|
cp1 == segStart ? (cp1 == cp2 ? segEnd : cp2) : cp1, segStart);
|
|
|
|
segEndAngle = AngleOfVector(
|
|
|
|
segEnd, cp2 == segEnd ? (cp1 == cp2 ? segStart : cp1) : cp2);
|
2018-11-30 13:46:48 +03:00
|
|
|
i += 4;
|
|
|
|
break;
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
2012-12-23 08:54:22 +04:00
|
|
|
case PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS:
|
|
|
|
case PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL: {
|
2014-03-19 05:13:19 +04:00
|
|
|
Point cp1 = SVGPathSegUtils::IsQuadraticType(prevSegType)
|
2010-12-14 23:48:28 +03:00
|
|
|
? segStart * 2 - prevCP
|
|
|
|
: segStart;
|
2012-12-23 08:54:22 +04:00
|
|
|
if (segType == PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS) {
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = Point(mData[i], mData[i + 1]);
|
2010-11-08 18:07:00 +03:00
|
|
|
} else {
|
2014-03-19 05:13:19 +04:00
|
|
|
segEnd = segStart + Point(mData[i], mData[i + 1]);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-11-08 18:07:00 +03:00
|
|
|
prevCP = cp1;
|
2015-02-18 01:15:15 +03:00
|
|
|
segStartAngle = AngleOfVector(cp1 == segStart ? segEnd : cp1, segStart);
|
|
|
|
segEndAngle = AngleOfVector(segEnd, cp1 == segEnd ? segStart : cp1);
|
2018-11-30 13:46:48 +03:00
|
|
|
i += 2;
|
|
|
|
break;
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Leave any existing marks in aMarks so we have a visual indication of
|
|
|
|
// when things went wrong.
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(false, "Unknown segment type - path corruption?");
|
2010-11-08 18:07:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the angle of the mark at the start of this segment:
|
|
|
|
if (aMarks->Length()) {
|
2019-01-26 16:01:31 +03:00
|
|
|
SVGMark& mark = aMarks->LastElement();
|
2010-11-08 18:07:00 +03:00
|
|
|
if (!IsMoveto(segType) && IsMoveto(prevSegType)) {
|
|
|
|
// start of new subpath
|
|
|
|
pathStartAngle = mark.angle = segStartAngle;
|
|
|
|
} else if (IsMoveto(segType) && !IsMoveto(prevSegType)) {
|
|
|
|
// end of a subpath
|
2012-12-23 08:54:22 +04:00
|
|
|
if (prevSegType != PATHSEG_CLOSEPATH) mark.angle = prevSegEndAngle;
|
2010-11-08 18:07:00 +03:00
|
|
|
} else {
|
2012-12-23 08:54:22 +04:00
|
|
|
if (!(segType == PATHSEG_CLOSEPATH && prevSegType == PATHSEG_CLOSEPATH))
|
2012-09-22 23:26:05 +04:00
|
|
|
mark.angle =
|
|
|
|
SVGContentUtils::AngleBisect(prevSegEndAngle, segStartAngle);
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the mark at the end of this segment, and set its position:
|
2019-01-26 16:01:31 +03:00
|
|
|
if (!aMarks->AppendElement(SVGMark(static_cast<float>(segEnd.x),
|
|
|
|
static_cast<float>(segEnd.y), 0.0f,
|
|
|
|
SVGMark::eMid))) {
|
2010-11-08 18:07:00 +03:00
|
|
|
aMarks->Clear(); // OOM, so try to free some
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-23 08:54:22 +04:00
|
|
|
if (segType == PATHSEG_CLOSEPATH && prevSegType != PATHSEG_CLOSEPATH) {
|
2013-07-12 10:39:38 +04:00
|
|
|
aMarks->LastElement().angle =
|
2010-11-08 18:07:00 +03:00
|
|
|
// aMarks->ElementAt(pathStartIndex).angle =
|
2012-09-22 23:26:05 +04:00
|
|
|
SVGContentUtils::AngleBisect(segEndAngle, pathStartAngle);
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
prevSegType = segType;
|
2010-12-14 23:48:28 +03:00
|
|
|
prevSegEnd = segEnd;
|
2010-11-08 18:07:00 +03:00
|
|
|
prevSegEndAngle = segEndAngle;
|
|
|
|
}
|
|
|
|
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt");
|
2010-11-08 18:07:00 +03:00
|
|
|
|
2013-07-12 10:39:38 +04:00
|
|
|
if (aMarks->Length()) {
|
|
|
|
if (prevSegType != PATHSEG_CLOSEPATH) {
|
|
|
|
aMarks->LastElement().angle = prevSegEndAngle;
|
|
|
|
}
|
2019-01-26 16:01:31 +03:00
|
|
|
aMarks->LastElement().type = SVGMark::eEnd;
|
|
|
|
aMarks->ElementAt(0).type = SVGMark::eStart;
|
2013-07-12 10:39:38 +04:00
|
|
|
}
|
2010-11-08 18:07:00 +03:00
|
|
|
}
|
|
|
|
|
2014-05-26 19:21:23 +04:00
|
|
|
size_t SVGPathData::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
|
2015-07-29 09:24:24 +03:00
|
|
|
return mData.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2014-05-26 19:21:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t SVGPathData::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|