2011-09-25 21:04:32 +00: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 11:12:37 +00: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/. */
|
2006-09-14 13:48:36 +00:00
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-11 05:50:08 +00:00
|
|
|
|
2011-09-25 21:04:32 +00:00
|
|
|
#include "SVGTransformListParser.h"
|
2013-10-29 17:15:39 +00:00
|
|
|
#include "SVGContentUtils.h"
|
2013-04-02 19:17:41 +00:00
|
|
|
#include "nsSVGTransform.h"
|
2006-12-26 17:47:52 +00:00
|
|
|
#include "nsGkAtoms.h"
|
2011-09-25 21:04:31 +00:00
|
|
|
#include "nsIAtom.h"
|
2006-09-14 13:48:36 +00:00
|
|
|
|
2011-09-25 21:04:31 +00:00
|
|
|
using namespace mozilla;
|
2006-09-14 13:48:36 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// private methods
|
|
|
|
|
2013-10-29 17:15:39 +00:00
|
|
|
bool
|
|
|
|
SVGTransformListParser::Parse()
|
2006-09-14 13:48:36 +00:00
|
|
|
{
|
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()
|
2006-09-14 13:48:36 +00:00
|
|
|
{
|
2013-10-29 17:15:39 +00:00
|
|
|
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()
|
2006-09-14 13:48:36 +00:00
|
|
|
{
|
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());
|
|
|
|
nsIAtom* 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
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-10-29 17:15:39 +00:00
|
|
|
SVGTransformListParser::ParseArguments(float* aResult,
|
|
|
|
uint32_t aMaxCount,
|
|
|
|
uint32_t* aParsedCount)
|
2006-09-14 13:48:36 +00:00
|
|
|
{
|
2013-10-29 17:15:39 +00:00
|
|
|
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
|
|
|
|
2013-10-29 17:15:39 +00:00
|
|
|
while (SkipWsp()) {
|
|
|
|
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;
|
|
|
|
// fall-through
|
|
|
|
case 2:
|
|
|
|
{
|
2013-04-02 19:17:41 +00:00
|
|
|
nsSVGTransform* transform = mTransforms.AppendElement();
|
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];
|
|
|
|
// fall-through
|
|
|
|
case 2:
|
|
|
|
{
|
2013-04-02 19:17:41 +00:00
|
|
|
nsSVGTransform* transform = mTransforms.AppendElement();
|
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;
|
|
|
|
// fall-through
|
|
|
|
case 3:
|
|
|
|
{
|
2013-04-02 19:17:41 +00:00
|
|
|
nsSVGTransform* transform = mTransforms.AppendElement();
|
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
|
|
|
}
|
|
|
|
|
2013-04-02 19:17:41 +00:00
|
|
|
nsSVGTransform* transform = mTransforms.AppendElement();
|
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
|
|
|
}
|
|
|
|
|
2013-04-02 19:17:41 +00:00
|
|
|
nsSVGTransform* transform = mTransforms.AppendElement();
|
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
|
|
|
}
|
|
|
|
|
2013-04-02 19:17:41 +00:00
|
|
|
nsSVGTransform* transform = mTransforms.AppendElement();
|
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
|
|
|
}
|