2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsSVGPathDataParser.h"
|
2013-10-24 05:46:38 -07:00
|
|
|
|
|
|
|
#include "mozilla/gfx/Point.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsSVGDataParser.h"
|
2013-10-29 10:15:39 -07:00
|
|
|
#include "SVGContentUtils.h"
|
2013-03-11 15:05:58 -07:00
|
|
|
#include "SVGPathData.h"
|
|
|
|
#include "SVGPathSegUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
using namespace mozilla;
|
2013-10-24 05:46:38 -07:00
|
|
|
using namespace mozilla::gfx;
|
2010-11-08 07:07:00 -08:00
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
static inline char16_t ToUpper(char16_t aCh)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
return aCh >= 'a' && aCh <= 'z' ? aCh - 'a' + 'A' : aCh;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::Parse()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
mPathSegList->Clear();
|
|
|
|
return ParsePath();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseCoordPair(float& aX, float& aY)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
return SVGContentUtils::ParseNumber(mIter, mEnd, aX) &&
|
|
|
|
SkipCommaWsp() &&
|
|
|
|
SVGContentUtils::ParseNumber(mIter, mEnd, aY);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseFlag(bool& aFlag)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
if (mIter == mEnd || (*mIter != '0' && *mIter != '1')) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-29 10:15:39 -07:00
|
|
|
aFlag = (*mIter == '1');
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
++mIter;
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParsePath()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
while (SkipWsp()) {
|
|
|
|
if (!ParseSubPath()) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseSubPath()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
return ParseMoveto() && ParseSubPathElements();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseSubPathElements()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
while (SkipWsp() && !IsStartOfSubPath()) {
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t commandType = ToUpper(*mIter);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
// Upper case commands have absolute co-ordinates,
|
|
|
|
// lower case commands have relative co-ordinates.
|
|
|
|
bool absCoords = commandType == *mIter;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
++mIter;
|
|
|
|
SkipWsp();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!ParseSubPathElement(commandType, absCoords)) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2013-10-29 10:15:39 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
2014-01-04 07:02:17 -08:00
|
|
|
nsSVGPathDataParser::ParseSubPathElement(char16_t aCommandType,
|
2013-10-29 10:15:39 -07:00
|
|
|
bool aAbsCoords)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
switch (aCommandType) {
|
|
|
|
case 'Z':
|
|
|
|
return ParseClosePath();
|
|
|
|
case 'L':
|
|
|
|
return ParseLineto(aAbsCoords);
|
|
|
|
case 'H':
|
|
|
|
return ParseHorizontalLineto(aAbsCoords);
|
|
|
|
case 'V':
|
|
|
|
return ParseVerticalLineto(aAbsCoords);
|
|
|
|
case 'C':
|
|
|
|
return ParseCurveto(aAbsCoords);
|
|
|
|
case 'S':
|
|
|
|
return ParseSmoothCurveto(aAbsCoords);
|
|
|
|
case 'Q':
|
|
|
|
return ParseQuadBezierCurveto(aAbsCoords);
|
|
|
|
case 'T':
|
|
|
|
return ParseSmoothQuadBezierCurveto(aAbsCoords);
|
|
|
|
case 'A':
|
|
|
|
return ParseEllipticalArc(aAbsCoords);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-29 10:15:39 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::IsStartOfSubPath() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
return *mIter == 'm' || *mIter == 'M';
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseMoveto()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!IsStartOfSubPath()) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool absCoords = (*mIter == 'M');
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
++mIter;
|
|
|
|
SkipWsp();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
float x, y;
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!ParseCoordPair(x, y)) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (NS_FAILED(mPathSegList->AppendSeg(
|
|
|
|
absCoords ? PATHSEG_MOVETO_ABS : PATHSEG_MOVETO_REL,
|
|
|
|
x, y))) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-29 10:15:39 -07:00
|
|
|
|
|
|
|
if (!SkipWsp() || IsAlpha(*mIter)) {
|
|
|
|
// End of data, or start of a new command
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-29 10:15:39 -07:00
|
|
|
|
2013-11-16 01:51:39 -08:00
|
|
|
SkipCommaWsp();
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
// Per SVG 1.1 Section 8.3.2
|
|
|
|
// If a moveto is followed by multiple pairs of coordinates,
|
|
|
|
// the subsequent pairs are treated as implicit lineto commands
|
|
|
|
return ParseLineto(absCoords);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseClosePath()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
return NS_SUCCEEDED(mPathSegList->AppendSeg(PATHSEG_CLOSEPATH));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseLineto(bool aAbsCoords)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
while (true) {
|
2007-03-22 10:30:00 -07:00
|
|
|
float x, y;
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!ParseCoordPair(x, y)) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (NS_FAILED(mPathSegList->AppendSeg(
|
|
|
|
aAbsCoords ? PATHSEG_LINETO_ABS : PATHSEG_LINETO_REL,
|
|
|
|
x, y))) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SkipWsp() || IsAlpha(*mIter)) {
|
|
|
|
// End of data, or start of a new command
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-29 10:15:39 -07:00
|
|
|
SkipCommaWsp();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseHorizontalLineto(bool aAbsCoords)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
while (true) {
|
2007-03-22 10:30:00 -07:00
|
|
|
float x;
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SVGContentUtils::ParseNumber(mIter, mEnd, x)) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (NS_FAILED(mPathSegList->AppendSeg(
|
|
|
|
aAbsCoords ? PATHSEG_LINETO_HORIZONTAL_ABS : PATHSEG_LINETO_HORIZONTAL_REL,
|
|
|
|
x))) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SkipWsp() || IsAlpha(*mIter)) {
|
|
|
|
// End of data, or start of a new command
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-29 10:15:39 -07:00
|
|
|
SkipCommaWsp();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseVerticalLineto(bool aAbsCoords)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
while (true) {
|
2007-03-22 10:30:00 -07:00
|
|
|
float y;
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SVGContentUtils::ParseNumber(mIter, mEnd, y)) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (NS_FAILED(mPathSegList->AppendSeg(
|
|
|
|
aAbsCoords ? PATHSEG_LINETO_VERTICAL_ABS : PATHSEG_LINETO_VERTICAL_REL,
|
|
|
|
y))) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SkipWsp() || IsAlpha(*mIter)) {
|
|
|
|
// End of data, or start of a new command
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-29 10:15:39 -07:00
|
|
|
SkipCommaWsp();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseCurveto(bool aAbsCoords)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
while (true) {
|
|
|
|
float x1, y1, x2, y2, x, y;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!(ParseCoordPair(x1, y1) &&
|
|
|
|
SkipCommaWsp() &&
|
|
|
|
ParseCoordPair(x2, y2) &&
|
|
|
|
SkipCommaWsp() &&
|
|
|
|
ParseCoordPair(x, y))) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (NS_FAILED(mPathSegList->AppendSeg(
|
|
|
|
aAbsCoords ? PATHSEG_CURVETO_CUBIC_ABS : PATHSEG_CURVETO_CUBIC_REL,
|
|
|
|
x1, y1, x2, y2, x, y))) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SkipWsp() || IsAlpha(*mIter)) {
|
|
|
|
// End of data, or start of a new command
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SkipCommaWsp();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseSmoothCurveto(bool aAbsCoords)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
while (true) {
|
|
|
|
float x2, y2, x, y;
|
|
|
|
if (!(ParseCoordPair(x2, y2) &&
|
|
|
|
SkipCommaWsp() &&
|
|
|
|
ParseCoordPair(x, y))) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (NS_FAILED(mPathSegList->AppendSeg(
|
|
|
|
aAbsCoords ? PATHSEG_CURVETO_CUBIC_SMOOTH_ABS : PATHSEG_CURVETO_CUBIC_SMOOTH_REL,
|
|
|
|
x2, y2, x, y))) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SkipWsp() || IsAlpha(*mIter)) {
|
|
|
|
// End of data, or start of a new command
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SkipCommaWsp();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseQuadBezierCurveto(bool aAbsCoords)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
while (true) {
|
|
|
|
float x1, y1, x, y;
|
|
|
|
if (!(ParseCoordPair(x1, y1) &&
|
|
|
|
SkipCommaWsp() &&
|
|
|
|
ParseCoordPair(x, y))) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (NS_FAILED(mPathSegList->AppendSeg(
|
|
|
|
aAbsCoords ? PATHSEG_CURVETO_QUADRATIC_ABS : PATHSEG_CURVETO_QUADRATIC_REL,
|
|
|
|
x1, y1, x, y))) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SkipWsp() || IsAlpha(*mIter)) {
|
|
|
|
// Start of a new command
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SkipCommaWsp();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseSmoothQuadBezierCurveto(bool aAbsCoords)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
while (true) {
|
2007-03-22 10:30:00 -07:00
|
|
|
float x, y;
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!ParseCoordPair(x, y)) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (NS_FAILED(mPathSegList->AppendSeg(
|
|
|
|
aAbsCoords ? PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS : PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL,
|
|
|
|
x, y))) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SkipWsp() || IsAlpha(*mIter)) {
|
|
|
|
// End of data, or start of a new command
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-29 10:15:39 -07:00
|
|
|
SkipCommaWsp();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
nsSVGPathDataParser::ParseEllipticalArc(bool aAbsCoords)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
while (true) {
|
|
|
|
float r1, r2, angle, x, y;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool largeArcFlag, sweepFlag;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!(SVGContentUtils::ParseNumber(mIter, mEnd, r1) &&
|
|
|
|
SkipCommaWsp() &&
|
|
|
|
SVGContentUtils::ParseNumber(mIter, mEnd, r2) &&
|
|
|
|
SkipCommaWsp() &&
|
|
|
|
SVGContentUtils::ParseNumber(mIter, mEnd, angle)&&
|
|
|
|
SkipCommaWsp() &&
|
|
|
|
ParseFlag(largeArcFlag) &&
|
|
|
|
SkipCommaWsp() &&
|
|
|
|
ParseFlag(sweepFlag) &&
|
|
|
|
SkipCommaWsp() &&
|
|
|
|
ParseCoordPair(x, y))) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
// We can only pass floats after 'type', and per the SVG spec for arc,
|
|
|
|
// non-zero args are treated at 'true'.
|
|
|
|
if (NS_FAILED(mPathSegList->AppendSeg(
|
|
|
|
aAbsCoords ? PATHSEG_ARC_ABS : PATHSEG_ARC_REL,
|
|
|
|
r1, r2, angle,
|
|
|
|
largeArcFlag ? 1.0f : 0.0f,
|
|
|
|
sweepFlag ? 1.0f : 0.0f,
|
|
|
|
x, y))) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SkipWsp() || IsAlpha(*mIter)) {
|
|
|
|
// End of data, or start of a new command
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-29 10:15:39 -07:00
|
|
|
SkipCommaWsp();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
static double
|
|
|
|
CalcVectorAngle(double ux, double uy, double vx, double vy)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-11-08 07:07:00 -08:00
|
|
|
double ta = atan2(uy, ux);
|
|
|
|
double tb = atan2(vy, vx);
|
|
|
|
if (tb >= ta)
|
|
|
|
return tb-ta;
|
|
|
|
return 2 * M_PI - (ta-tb);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-24 05:46:38 -07:00
|
|
|
nsSVGArcConverter::nsSVGArcConverter(const Point& from,
|
|
|
|
const Point& to,
|
|
|
|
const Point& radii,
|
2010-11-08 07:07:00 -08:00
|
|
|
double angle,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool largeArcFlag,
|
|
|
|
bool sweepFlag)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-11-08 07:07:00 -08:00
|
|
|
const double radPerDeg = M_PI/180.0;
|
2012-09-24 05:07:41 -07:00
|
|
|
mSegIndex = 0;
|
|
|
|
|
|
|
|
if (from == to) {
|
|
|
|
mNumSegs = 0;
|
|
|
|
return;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
// Convert to center parameterization as shown in
|
|
|
|
// http://www.w3.org/TR/SVG/implnote.html
|
|
|
|
mRx = fabs(radii.x);
|
|
|
|
mRy = fabs(radii.y);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
mSinPhi = sin(angle*radPerDeg);
|
|
|
|
mCosPhi = cos(angle*radPerDeg);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
double x1dash = mCosPhi * (from.x-to.x)/2.0 + mSinPhi * (from.y-to.y)/2.0;
|
|
|
|
double y1dash = -mSinPhi * (from.x-to.x)/2.0 + mCosPhi * (from.y-to.y)/2.0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
double root;
|
|
|
|
double numerator = mRx*mRx*mRy*mRy - mRx*mRx*y1dash*y1dash -
|
|
|
|
mRy*mRy*x1dash*x1dash;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
if (numerator < 0.0) {
|
|
|
|
// If mRx , mRy and are such that there is no solution (basically,
|
|
|
|
// the ellipse is not big enough to reach from 'from' to 'to'
|
|
|
|
// then the ellipse is scaled up uniformly until there is
|
|
|
|
// exactly one solution (until the ellipse is just big enough).
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
// -> find factor s, such that numerator' with mRx'=s*mRx and
|
|
|
|
// mRy'=s*mRy becomes 0 :
|
|
|
|
double s = sqrt(1.0 - numerator/(mRx*mRx*mRy*mRy));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
mRx *= s;
|
|
|
|
mRy *= s;
|
|
|
|
root = 0.0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
}
|
2010-11-08 07:07:00 -08:00
|
|
|
else {
|
|
|
|
root = (largeArcFlag == sweepFlag ? -1.0 : 1.0) *
|
|
|
|
sqrt( numerator/(mRx*mRx*y1dash*y1dash + mRy*mRy*x1dash*x1dash) );
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
double cxdash = root*mRx*y1dash/mRy;
|
|
|
|
double cydash = -root*mRy*x1dash/mRx;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
mC.x = mCosPhi * cxdash - mSinPhi * cydash + (from.x+to.x)/2.0;
|
|
|
|
mC.y = mSinPhi * cxdash + mCosPhi * cydash + (from.y+to.y)/2.0;
|
|
|
|
mTheta = CalcVectorAngle(1.0, 0.0, (x1dash-cxdash)/mRx, (y1dash-cydash)/mRy);
|
|
|
|
double dtheta = CalcVectorAngle((x1dash-cxdash)/mRx, (y1dash-cydash)/mRy,
|
|
|
|
(-x1dash-cxdash)/mRx, (-y1dash-cydash)/mRy);
|
|
|
|
if (!sweepFlag && dtheta>0)
|
|
|
|
dtheta -= 2.0*M_PI;
|
|
|
|
else if (sweepFlag && dtheta<0)
|
|
|
|
dtheta += 2.0*M_PI;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
// Convert into cubic bezier segments <= 90deg
|
|
|
|
mNumSegs = static_cast<int>(ceil(fabs(dtheta/(M_PI/2.0))));
|
|
|
|
mDelta = dtheta/mNumSegs;
|
|
|
|
mT = 8.0/3.0 * sin(mDelta/4.0) * sin(mDelta/4.0) / sin(mDelta/2.0);
|
|
|
|
|
|
|
|
mFrom = from;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2013-10-24 05:46:38 -07:00
|
|
|
nsSVGArcConverter::GetNextSegment(Point* cp1, Point* cp2, Point* to)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-11-08 07:07:00 -08:00
|
|
|
if (mSegIndex == mNumSegs) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
double cosTheta1 = cos(mTheta);
|
|
|
|
double sinTheta1 = sin(mTheta);
|
|
|
|
double theta2 = mTheta + mDelta;
|
|
|
|
double cosTheta2 = cos(theta2);
|
|
|
|
double sinTheta2 = sin(theta2);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
// a) calculate endpoint of the segment:
|
|
|
|
to->x = mCosPhi * mRx*cosTheta2 - mSinPhi * mRy*sinTheta2 + mC.x;
|
|
|
|
to->y = mSinPhi * mRx*cosTheta2 + mCosPhi * mRy*sinTheta2 + mC.y;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
// b) calculate gradients at start/end points of segment:
|
|
|
|
cp1->x = mFrom.x + mT * ( - mCosPhi * mRx*sinTheta1 - mSinPhi * mRy*cosTheta1);
|
|
|
|
cp1->y = mFrom.y + mT * ( - mSinPhi * mRx*sinTheta1 + mCosPhi * mRy*cosTheta1);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
cp2->x = to->x + mT * ( mCosPhi * mRx*sinTheta2 + mSinPhi * mRy*cosTheta2);
|
|
|
|
cp2->y = to->y + mT * ( mSinPhi * mRx*sinTheta2 - mCosPhi * mRy*cosTheta2);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-08 07:07:00 -08:00
|
|
|
// do next segment
|
|
|
|
mTheta = theta2;
|
|
|
|
mFrom = *to;
|
|
|
|
++mSegIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|