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
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2011-09-26 01:04:32 +04:00
|
|
|
#include "SVGTransformListParser.h"
|
2013-10-29 21:15:39 +04:00
|
|
|
#include "SVGContentUtils.h"
|
2013-04-02 23:17:41 +04:00
|
|
|
#include "nsSVGTransform.h"
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2011-09-26 01:04:31 +04:00
|
|
|
#include "nsIAtom.h"
|
2006-09-14 17:48:36 +04:00
|
|
|
|
2011-09-26 01:04:31 +04:00
|
|
|
using namespace mozilla;
|
2006-09-14 17:48:36 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// private methods
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
bool
|
|
|
|
SVGTransformListParser::Parse()
|
2006-09-14 17:48:36 +04:00
|
|
|
{
|
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()
|
2006-09-14 17:48:36 +04:00
|
|
|
{
|
2013-10-29 21:15:39 +04:00
|
|
|
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()
|
2006-09-14 17:48:36 +04:00
|
|
|
{
|
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());
|
|
|
|
nsIAtom* 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
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-10-29 21:15:39 +04:00
|
|
|
SVGTransformListParser::ParseArguments(float* aResult,
|
|
|
|
uint32_t aMaxCount,
|
|
|
|
uint32_t* aParsedCount)
|
2006-09-14 17:48:36 +04:00
|
|
|
{
|
2013-10-29 21:15:39 +04:00
|
|
|
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
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
while (SkipWsp()) {
|
|
|
|
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;
|
2016-01-14 12:38:47 +03:00
|
|
|
MOZ_FALLTHROUGH;
|
2006-09-14 17:48:36 +04:00
|
|
|
case 2:
|
|
|
|
{
|
2015-05-28 21:07:44 +03:00
|
|
|
nsSVGTransform* 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];
|
2016-01-14 12:38:47 +03:00
|
|
|
MOZ_FALLTHROUGH;
|
2006-09-14 17:48:36 +04:00
|
|
|
case 2:
|
|
|
|
{
|
2015-05-28 21:07:44 +03:00
|
|
|
nsSVGTransform* 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;
|
2016-01-14 12:38:47 +03:00
|
|
|
MOZ_FALLTHROUGH;
|
2006-09-14 17:48:36 +04:00
|
|
|
case 3:
|
|
|
|
{
|
2015-05-28 21:07:44 +03:00
|
|
|
nsSVGTransform* 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
|
|
|
}
|
|
|
|
|
2015-05-28 21:07:44 +03:00
|
|
|
nsSVGTransform* 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
|
|
|
}
|
|
|
|
|
2015-05-28 21:07:44 +03:00
|
|
|
nsSVGTransform* 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
|
|
|
}
|
|
|
|
|
2015-05-28 21:07:44 +03:00
|
|
|
nsSVGTransform* 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
|
|
|
}
|