Merge pull request #1416 from mirecta/transform_expr-sep

transform expressions: whitespace/comma argument separation rules
This commit is contained in:
Dane Springmeyer 2012-08-20 14:28:54 -07:00
commit 3be516a805
4 changed files with 100 additions and 28 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

@ -20,11 +20,15 @@
*
*****************************************************************************/
// 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,45 +64,68 @@ 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();
}

View file

@ -93,8 +93,19 @@ def test_shieldsymbolizer_init():
def test_shieldsymbolizer_modify():
s = mapnik.ShieldSymbolizer(mapnik.Expression('[Field Name]'), 'DejaVu Sans Bold', 6, mapnik.Color('#000000'), mapnik.PathExpression('../data/images/dummy.png'))
# transform expression
s.transform = "rotate(30+[a]) scale(2*[sx] [sy])"
eq_(s.transform, "rotate((30+[a])) scale(2*[sx], [sy])")
def check_transform(expr, expect_str=None):
s.transform = expr
eq_(s.transform, expr if expect_str is None else expect_str)
check_transform("matrix(1 2 3 4 5 6)", "matrix(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)")
check_transform("matrix(1, 2, 3, 4, 5, 6 +7)", "matrix(1, 2, 3, 4, 5, (6+7))")
check_transform("rotate([a])")
check_transform("rotate([a] -2)", "rotate(([a]-2))")
check_transform("rotate([a] -2 -3)", "rotate([a], -2.0, -3.0)")
check_transform("rotate([a] -2 -3 -4)", "rotate(((([a]-2)-3)-4))")
check_transform("rotate([a] -2, 3, 4)", "rotate(([a]-2), 3, 4)")
check_transform("translate([tx]) rotate([a])")
check_transform("scale([sx], [sy]/2)")
# TODO check expected failures
def test_polygonsymbolizer_init():
p = mapnik.PolygonSymbolizer()