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: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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-09-14 17:48:36 +04:00
|
|
|
|
2011-09-26 01:04:32 +04:00
|
|
|
#include "SVGTransformListParser.h"
|
2018-12-28 05:47:10 +03:00
|
|
|
|
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-10-29 21:15:39 +04:00
|
|
|
#include "SVGContentUtils.h"
|
2018-12-29 12:15:40 +03:00
|
|
|
#include "SVGTransform.h"
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2019-09-21 17:38:56 +03:00
|
|
|
namespace mozilla {
|
2006-09-14 17:48:36 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// private methods
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
bool SVGTransformListParser::Parse() {
|
2011-09-26 01:04:31 +04:00
|
|
|
mTransforms.Clear();
|
2013-10-29 21:15:39 +04:00
|
|
|
return ParseTransforms();
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
bool SVGTransformListParser::ParseTransforms() {
|
|
|
|
if (!SkipWsp()) {
|
|
|
|
return true;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!ParseTransform()) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
while (SkipWsp()) {
|
|
|
|
// The SVG BNF allows multiple comma-wsp between transforms
|
|
|
|
while (*mIter == ',') {
|
|
|
|
++mIter;
|
|
|
|
if (!SkipWsp()) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!ParseTransform()) {
|
|
|
|
return false;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
}
|
2013-10-29 21:15:39 +04:00
|
|
|
return true;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
bool SVGTransformListParser::ParseTransform() {
|
2014-01-04 19:02:17 +04:00
|
|
|
RangedPtr<const char16_t> start(mIter);
|
2013-10-29 21:15:39 +04:00
|
|
|
while (IsAlpha(*mIter)) {
|
|
|
|
++mIter;
|
|
|
|
if (mIter == mEnd) {
|
|
|
|
return false;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (start == mIter) {
|
|
|
|
// Didn't read anything
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
const nsAString& transform = Substring(start.get(), mIter.get());
|
2017-10-27 02:31:13 +03:00
|
|
|
nsStaticAtom* keyAtom = NS_GetStaticAtom(transform);
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!keyAtom || !SkipWsp()) {
|
|
|
|
return false;
|
2009-04-05 13:52:16 +04:00
|
|
|
}
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (keyAtom == nsGkAtoms::translate) {
|
|
|
|
return ParseTranslate();
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
2013-10-29 21:15:39 +04:00
|
|
|
if (keyAtom == nsGkAtoms::scale) {
|
|
|
|
return ParseScale();
|
|
|
|
}
|
|
|
|
if (keyAtom == nsGkAtoms::rotate) {
|
|
|
|
return ParseRotate();
|
|
|
|
}
|
|
|
|
if (keyAtom == nsGkAtoms::skewX) {
|
|
|
|
return ParseSkewX();
|
|
|
|
}
|
|
|
|
if (keyAtom == nsGkAtoms::skewY) {
|
|
|
|
return ParseSkewY();
|
|
|
|
}
|
|
|
|
if (keyAtom == nsGkAtoms::matrix) {
|
|
|
|
return ParseMatrix();
|
|
|
|
}
|
|
|
|
return false;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
bool SVGTransformListParser::ParseArguments(float* aResult, uint32_t aMaxCount,
|
|
|
|
uint32_t* aParsedCount) {
|
|
|
|
if (*mIter != '(') {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-04-05 13:52:16 +04:00
|
|
|
}
|
2013-10-29 21:15:39 +04:00
|
|
|
++mIter;
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!SkipWsp()) {
|
|
|
|
return false;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!SVGContentUtils::ParseNumber(mIter, mEnd, aResult[0])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*aParsedCount = 1;
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2018-04-28 18:09:38 +03:00
|
|
|
while (SkipWsp()) {
|
2013-10-29 21:15:39 +04:00
|
|
|
if (*mIter == ')') {
|
|
|
|
++mIter;
|
|
|
|
return true;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
2013-10-29 21:15:39 +04:00
|
|
|
if (*aParsedCount == aMaxCount) {
|
|
|
|
return false;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
2013-10-29 21:15:39 +04:00
|
|
|
SkipCommaWsp();
|
|
|
|
if (!SVGContentUtils::ParseNumber(mIter, mEnd,
|
|
|
|
aResult[(*aParsedCount)++])) {
|
|
|
|
return false;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
}
|
2013-10-29 21:15:39 +04:00
|
|
|
return false;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
bool SVGTransformListParser::ParseTranslate() {
|
2006-09-14 17:48:36 +04:00
|
|
|
float t[2];
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count;
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!ParseArguments(t, ArrayLength(t), &count)) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-14 17:48:36 +04:00
|
|
|
|
|
|
|
switch (count) {
|
|
|
|
case 1:
|
|
|
|
t[1] = 0.f;
|
2019-12-20 10:16:43 +03:00
|
|
|
[[fallthrough]];
|
2006-09-14 17:48:36 +04:00
|
|
|
case 2: {
|
2018-12-29 12:15:40 +03:00
|
|
|
SVGTransform* transform = mTransforms.AppendElement(fallible);
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!transform) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-14 17:48:36 +04:00
|
|
|
transform->SetTranslate(t[0], t[1]);
|
2013-10-29 21:15:39 +04:00
|
|
|
return true;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
return false;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
bool SVGTransformListParser::ParseScale() {
|
2006-09-14 17:48:36 +04:00
|
|
|
float s[2];
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count;
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!ParseArguments(s, ArrayLength(s), &count)) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-14 17:48:36 +04:00
|
|
|
|
|
|
|
switch (count) {
|
|
|
|
case 1:
|
|
|
|
s[1] = s[0];
|
2019-12-20 10:16:43 +03:00
|
|
|
[[fallthrough]];
|
2006-09-14 17:48:36 +04:00
|
|
|
case 2: {
|
2018-12-29 12:15:40 +03:00
|
|
|
SVGTransform* transform = mTransforms.AppendElement(fallible);
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!transform) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-14 17:48:36 +04:00
|
|
|
transform->SetScale(s[0], s[1]);
|
2013-10-29 21:15:39 +04:00
|
|
|
return true;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
return false;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
bool SVGTransformListParser::ParseRotate() {
|
2006-09-14 17:48:36 +04:00
|
|
|
float r[3];
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count;
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!ParseArguments(r, ArrayLength(r), &count)) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-14 17:48:36 +04:00
|
|
|
|
|
|
|
switch (count) {
|
|
|
|
case 1:
|
|
|
|
r[1] = r[2] = 0.f;
|
2019-12-20 10:16:43 +03:00
|
|
|
[[fallthrough]];
|
2006-09-14 17:48:36 +04:00
|
|
|
case 3: {
|
2018-12-29 12:15:40 +03:00
|
|
|
SVGTransform* transform = mTransforms.AppendElement(fallible);
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!transform) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-14 17:48:36 +04:00
|
|
|
transform->SetRotate(r[0], r[1], r[2]);
|
2013-10-29 21:15:39 +04:00
|
|
|
return true;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
return false;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
bool SVGTransformListParser::ParseSkewX() {
|
2006-09-14 17:48:36 +04:00
|
|
|
float skew;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count;
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!ParseArguments(&skew, 1, &count) || count != 1) {
|
|
|
|
return false;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2018-12-29 12:15:40 +03:00
|
|
|
SVGTransform* transform = mTransforms.AppendElement(fallible);
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!transform) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-14 17:48:36 +04:00
|
|
|
transform->SetSkewX(skew);
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
return true;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
bool SVGTransformListParser::ParseSkewY() {
|
2006-09-14 17:48:36 +04:00
|
|
|
float skew;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count;
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!ParseArguments(&skew, 1, &count) || count != 1) {
|
|
|
|
return false;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2018-12-29 12:15:40 +03:00
|
|
|
SVGTransform* transform = mTransforms.AppendElement(fallible);
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!transform) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-14 17:48:36 +04:00
|
|
|
transform->SetSkewY(skew);
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
return true;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
bool SVGTransformListParser::ParseMatrix() {
|
2006-09-14 17:48:36 +04:00
|
|
|
float m[6];
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count;
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!ParseArguments(m, ArrayLength(m), &count) || count != 6) {
|
|
|
|
return false;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
|
|
|
|
2018-12-29 12:15:40 +03:00
|
|
|
SVGTransform* transform = mTransforms.AppendElement(fallible);
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!transform) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-26 01:04:31 +04:00
|
|
|
transform->SetMatrix(gfxMatrix(m[0], m[1], m[2], m[3], m[4], m[5]));
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
return true;
|
2006-09-14 17:48:36 +04:00
|
|
|
}
|
2019-09-21 17:38:56 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|