transform expressions: disallow space-separated compound arguments, refs #1389

This commit is contained in:
Mickey Rose 2012-08-19 16:20:08 +02:00
parent 854d872447
commit 5ca715c1e0
3 changed files with 87 additions and 26 deletions

View file

@ -31,6 +31,10 @@
#include <boost/shared_ptr.hpp>
#include <boost/variant.hpp>
// fusion
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/include/vector.hpp>
// stl
#include <vector>
@ -83,16 +87,36 @@ struct scale_node
struct rotate_node
{
typedef boost::fusion::vector2<expr_node, expr_node> coords_type;
expr_node angle_;
expr_node cx_;
expr_node cy_;
explicit rotate_node(expr_node const& angle)
: angle_(angle) {}
rotate_node(expr_node const& angle,
expr_node const& cx, expr_node const& cy)
: angle_(angle), cx_(cx), cy_(cy) {}
rotate_node(expr_node const& angle,
boost::optional<expr_node> const& cx,
boost::optional<expr_node> const& cy)
: angle_(angle)
, cx_(cx ? *cx : value_null())
, cy_(cy ? *cy : value_null()) {}
rotate_node(expr_node const& angle,
boost::optional<coords_type> const& center)
: angle_(angle)
{
if (center)
{
cx_ = boost::fusion::at_c<0>(*center);
cy_ = boost::fusion::at_c<1>(*center);
}
}
};
struct skewX_node

View file

@ -28,7 +28,6 @@
#include <mapnik/transform_expression.hpp>
// spirit
#include <boost/spirit/home/phoenix/object/construct.hpp>
#include <boost/spirit/home/qi.hpp>
namespace mapnik {
@ -40,20 +39,22 @@ namespace mapnik {
: qi::grammar<Iterator, transform_list(), space_type>
{
explicit transform_expression_grammar(expression_grammar<Iterator> const& g);
typedef qi::locals<expr_node, boost::optional<expr_node>,
boost::optional<expr_node>
> rotate_locals;
typedef qi::rule<Iterator, transform_node(), space_type> node_rule;
typedef qi::rule<Iterator, transform_list(), space_type> list_rule;
// rules
typename expression_grammar<Iterator>::rule_type expr;
qi::rule<Iterator, std::string(), space_type> attr;
qi::rule<Iterator, expr_node(), space_type> atom;
qi::rule<Iterator, expr_node(), space_type> expr;
qi::rule<Iterator, expr_node(), space_type> sep_atom;
qi::rule<Iterator, expr_node(), space_type> sep_expr;
qi::rule<Iterator, transform_list(), space_type> start;
qi::rule<Iterator, transform_node(), space_type> transform_;
qi::rule<Iterator, transform_node(), space_type> matrix;
qi::rule<Iterator, transform_node(), space_type> translate;
qi::rule<Iterator, transform_node(), space_type> scale;
qi::rule<Iterator, transform_node(), space_type, rotate_locals> rotate;
qi::rule<Iterator, transform_node(), space_type> rotate;
qi::rule<Iterator, transform_node(), space_type> skewX;
qi::rule<Iterator, transform_node(), space_type> skewY;
};

View file

@ -19,12 +19,16 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
// mapnik
#include <mapnik/transform_expression_grammar.hpp>
// boost
#include <boost/version.hpp>
// spirit
#include <boost/spirit/home/phoenix/object/construct.hpp>
namespace mapnik {
namespace qi = boost::spirit::qi;
@ -39,8 +43,17 @@ transform_expression_grammar<Iterator>::transform_expression_grammar(expression_
using qi::_c; using qi::_3; using qi::_6;
using qi::_val;
using qi::char_;
using qi::double_;
using qi::lit;
using qi::no_case;
// [http://www.w3.org/TR/SVG/coords.html#TransformAttribute]
// The value of the transform attribute is a <transform-list>, which
// is defined as a list of transform definitions, which are applied in
// the order provided. The individual transform definitions are
// separated by whitespace and/or a comma.
#if BOOST_VERSION > 104200
using qi::no_skip;
start = transform_ % no_skip[char_(", ")] ;
@ -51,48 +64,71 @@ transform_expression_grammar<Iterator>::transform_expression_grammar(expression_
transform_ = matrix | translate | scale | rotate | skewX | skewY ;
// matrix(<a> <b> <c> <d> <e> <f>)
matrix = no_case[lit("matrix")]
>> (lit('(')
>> expr >> -lit(',')
>> expr >> -lit(',')
>> expr >> -lit(',')
>> expr >> -lit(',')
>> expr >> -lit(',')
>> expr >> lit(')'))
>> ( atom >> sep_atom >> sep_atom >> sep_atom >> sep_atom
>> sep_atom >> lit(')')
| expr >> sep_expr >> sep_expr >> sep_expr >> sep_expr
>> sep_expr >> lit(')')
))
[ _val = construct<matrix_node>(_1,_2,_3,_4,_5,_6) ];
// translate(<tx> [<ty>])
translate = no_case[lit("translate")]
>> (lit('(')
>> expr >> -lit(',')
>> -expr >> lit(')'))
[ _val = construct<translate_node>(_1,_2) ];
>> lit('(')
>> ( ( atom >> -sep_atom >> lit(')') )
[ _val = construct<translate_node>(_1,_2) ]
| ( expr >> -sep_expr >> lit(')') )
[ _val = construct<translate_node>(_1,_2) ]
);
// scale(<sx> [<sy>])
scale = no_case[lit("scale")]
>> (lit('(')
>> expr >> -lit(',')
>> -expr >> lit(')'))
[ _val = construct<scale_node>(_1,_2) ];
>> lit('(')
>> ( ( atom >> -sep_atom >> lit(')') )
[ _val = construct<scale_node>(_1,_2) ]
| ( expr >> -sep_expr >> lit(')') )
[ _val = construct<scale_node>(_1,_2) ]
);
// rotate(<rotate-angle> [<cx> <cy>])
rotate = no_case[lit("rotate")]
>> lit('(')
>> expr[_a = _1] >> -lit(',')
>> -(expr [_b = _1] >> -lit(',') >> expr[_c = _1])
>> lit(')')
[ _val = construct<rotate_node>(_a,_b,_c) ];
>> ( ( atom >> -( sep_atom >> sep_atom ) >> lit(')') )
[ _val = construct<rotate_node>(_1,_2) ]
| ( expr >> -( sep_expr >> sep_expr ) >> lit(')') )
[ _val = construct<rotate_node>(_1,_2) ]
);
// skewX(<skew-angle>)
skewX = no_case[lit("skewX")]
>> lit('(')
>> expr [ _val = construct<skewX_node>(_1) ]
>> lit(')');
// skewY(<skew-angle>)
skewY = no_case[lit("skewY")]
>> lit('(')
>> expr [ _val = construct<skewY_node>(_1) ]
>> lit(')');
// number or attribute
atom = double_ [ _val = _1 ]
| attr [ _val = construct<mapnik::attribute>(_1) ];
// Individual arguments in lists consiting solely of numbers and/or
// attributes are separated by whitespace and/or a comma.
sep_atom = -lit(',') >> atom [ _val = _1 ];
// Individual arguments in lists containing one or more compound
// expressions are separated by a comma.
sep_expr = lit(',') >> expr [ _val = _1 ];
attr = g.attr.alias();
expr = g.expr.alias();
}
template struct mapnik::transform_expression_grammar<std::string::const_iterator>;
}
}