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