2011-09-25 14:04:32 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
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
|
|
|
|
2013-12-08 18:52:54 -08:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-10 22:50:08 -07:00
|
|
|
|
2011-09-25 14:04:32 -07:00
|
|
|
#include "SVGTransformListParser.h"
|
2013-10-29 10:15:39 -07:00
|
|
|
#include "SVGContentUtils.h"
|
2013-04-02 12:17:41 -07:00
|
|
|
#include "nsSVGTransform.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsGkAtoms.h"
|
2011-09-25 14:04:31 -07:00
|
|
|
#include "nsIAtom.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-25 14:04:31 -07:00
|
|
|
using namespace mozilla;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// private methods
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
SVGTransformListParser::Parse()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-25 14:04:31 -07:00
|
|
|
mTransforms.Clear();
|
2013-10-29 10:15:39 -07:00
|
|
|
return ParseTransforms();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
SVGTransformListParser::ParseTransforms()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SkipWsp()) {
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!ParseTransform()) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
while (SkipWsp()) {
|
|
|
|
// The SVG BNF allows multiple comma-wsp between transforms
|
|
|
|
while (*mIter == ',') {
|
|
|
|
++mIter;
|
|
|
|
if (!SkipWsp()) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!ParseTransform()) {
|
|
|
|
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
|
|
|
|
SVGTransformListParser::ParseTransform()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-01-04 07:02:17 -08:00
|
|
|
RangedPtr<const char16_t> start(mIter);
|
2013-10-29 10:15:39 -07:00
|
|
|
while (IsAlpha(*mIter)) {
|
|
|
|
++mIter;
|
|
|
|
if (mIter == mEnd) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (start == mIter) {
|
|
|
|
// Didn't read anything
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
const nsAString& transform = Substring(start.get(), mIter.get());
|
|
|
|
nsIAtom* keyAtom = NS_GetStaticAtom(transform);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!keyAtom || !SkipWsp()) {
|
|
|
|
return false;
|
2009-04-05 02:52:16 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (keyAtom == nsGkAtoms::translate) {
|
|
|
|
return ParseTranslate();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-29 10:15:39 -07: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;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2013-10-29 10:15:39 -07:00
|
|
|
SVGTransformListParser::ParseArguments(float* aResult,
|
|
|
|
uint32_t aMaxCount,
|
|
|
|
uint32_t* aParsedCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-29 10:15:39 -07:00
|
|
|
if (*mIter != '(') {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-04-05 02:52:16 -07:00
|
|
|
}
|
2013-10-29 10:15:39 -07:00
|
|
|
++mIter;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SkipWsp()) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SVGContentUtils::ParseNumber(mIter, mEnd, aResult[0])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*aParsedCount = 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
while (SkipWsp()) {
|
|
|
|
if (*mIter == ')') {
|
|
|
|
++mIter;
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-29 10:15:39 -07:00
|
|
|
if (*aParsedCount == aMaxCount) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-29 10:15:39 -07:00
|
|
|
SkipCommaWsp();
|
|
|
|
if (!SVGContentUtils::ParseNumber(mIter, mEnd, aResult[(*aParsedCount)++])) {
|
|
|
|
return false;
|
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
|
|
|
|
SVGTransformListParser::ParseTranslate()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
float t[2];
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!ParseArguments(t, ArrayLength(t), &count)) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
switch (count) {
|
|
|
|
case 1:
|
|
|
|
t[1] = 0.f;
|
|
|
|
// fall-through
|
|
|
|
case 2:
|
|
|
|
{
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform* transform = mTransforms.AppendElement();
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!transform) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
transform->SetTranslate(t[0], t[1]);
|
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
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
SVGTransformListParser::ParseScale()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
float s[2];
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!ParseArguments(s, ArrayLength(s), &count)) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
switch (count) {
|
|
|
|
case 1:
|
|
|
|
s[1] = s[0];
|
|
|
|
// fall-through
|
|
|
|
case 2:
|
|
|
|
{
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform* transform = mTransforms.AppendElement();
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!transform) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
transform->SetScale(s[0], s[1]);
|
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
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
SVGTransformListParser::ParseRotate()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
float r[3];
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!ParseArguments(r, ArrayLength(r), &count)) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
switch (count) {
|
|
|
|
case 1:
|
|
|
|
r[1] = r[2] = 0.f;
|
|
|
|
// fall-through
|
|
|
|
case 3:
|
|
|
|
{
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform* transform = mTransforms.AppendElement();
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!transform) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
transform->SetRotate(r[0], r[1], r[2]);
|
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
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
bool
|
|
|
|
SVGTransformListParser::ParseSkewX()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
float skew;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!ParseArguments(&skew, 1, &count) || count != 1) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform* transform = mTransforms.AppendElement();
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!transform) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
transform->SetSkewX(skew);
|
|
|
|
|
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
|
|
|
|
SVGTransformListParser::ParseSkewY()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
float skew;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!ParseArguments(&skew, 1, &count) || count != 1) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform* transform = mTransforms.AppendElement();
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!transform) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
transform->SetSkewY(skew);
|
|
|
|
|
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
|
|
|
|
SVGTransformListParser::ParseMatrix()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
float m[6];
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!ParseArguments(m, ArrayLength(m), &count) || count != 6) {
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform* transform = mTransforms.AppendElement();
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!transform) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-25 14:04:31 -07:00
|
|
|
transform->SetMatrix(gfxMatrix(m[0], m[1], m[2], m[3], m[4], m[5]));
|
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
|
|
|
}
|