Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Viewbox refactor #4373

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/mapnik/svg/svg_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ class MAPNIK_DECL svg_parser : private util::noncopyable
bool strict_;
bool ignore_;
bool css_style_;
bool dimensions_ = false;
std::map<std::string, gradient> gradient_map_;
std::map<std::string, boost::property_tree::detail::rapidxml::xml_node<char> const*> node_cache_;
mapnik::css_data css_data_;
boost::optional<viewbox> vbox_{};
double normalized_diagonal_ = 0.0;
agg::trans_affine viewbox_tr_{};
std::deque<double> font_sizes_{};
error_handler err_handler_;
};
Expand Down
18 changes: 9 additions & 9 deletions include/mapnik/svg/svg_transform_grammar_x3_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ auto const matrix_action = [](auto const& ctx) {
auto d = boost::fusion::at_c<3>(attr);
auto e = boost::fusion::at_c<4>(attr);
auto f = boost::fusion::at_c<5>(attr);
tr = agg::trans_affine(a, b, c, d, e, f) * tr;
tr.premultiply(agg::trans_affine(a, b, c, d, e, f));
};

auto const rotate_action = [](auto const& ctx) {
Expand All @@ -75,14 +75,14 @@ auto const rotate_action = [](auto const& ctx) {
auto cy = boost::fusion::at_c<2>(attr) ? *boost::fusion::at_c<2>(attr) : 0.0;
if (cx == 0.0 && cy == 0.0)
{
tr = agg::trans_affine_rotation(agg::deg2rad(a)) * tr;
tr.premultiply(agg::trans_affine_rotation(agg::deg2rad(a)));
}
else
{
agg::trans_affine t = agg::trans_affine_translation(-cx, -cy);
t *= agg::trans_affine_rotation(agg::deg2rad(a));
t *= agg::trans_affine_translation(cx, cy);
tr = t * tr;
tr.premultiply(t);
}
};

Expand All @@ -92,9 +92,9 @@ auto const translate_action = [](auto const& ctx) {
auto tx = boost::fusion::at_c<0>(attr);
auto ty = boost::fusion::at_c<1>(attr);
if (ty)
tr = agg::trans_affine_translation(tx, *ty) * tr;
tr.premultiply(agg::trans_affine_translation(tx, *ty));
else
tr = agg::trans_affine_translation(tx, 0.0) * tr;
tr.premultiply(agg::trans_affine_translation(tx, 0.0));
};

auto const scale_action = [](auto const& ctx) {
Expand All @@ -103,21 +103,21 @@ auto const scale_action = [](auto const& ctx) {
auto sx = boost::fusion::at_c<0>(attr);
auto sy = boost::fusion::at_c<1>(attr);
if (sy)
tr = agg::trans_affine_scaling(sx, *sy) * tr;
tr.premultiply(agg::trans_affine_scaling(sx, *sy));
else
tr = agg::trans_affine_scaling(sx, sx) * tr;
tr.premultiply(agg::trans_affine_scaling(sx, sx));
};

auto const skewX_action = [](auto const& ctx) {
auto& tr = extract_transform(ctx);
auto skew_x = _attr(ctx);
tr = agg::trans_affine_skewing(agg::deg2rad(skew_x), 0.0) * tr;
tr.premultiply(agg::trans_affine_skewing(agg::deg2rad(skew_x), 0.0));
};

auto const skewY_action = [](auto const& ctx) {
auto& tr = extract_transform(ctx);
auto skew_y = _attr(ctx);
tr = agg::trans_affine_skewing(0.0, agg::deg2rad(skew_y)) * tr;
tr.premultiply(agg::trans_affine_skewing(0.0, agg::deg2rad(skew_y)));
};

// rules
Expand Down
82 changes: 40 additions & 42 deletions src/svg/svg_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,15 @@ void traverse_tree(svg_parser& parser, rapidxml::xml_node<char> const* node)
process_css(parser, node);
parse_attr(parser, node);
}
break;
case "svg"_case:
if (node->first_node() != nullptr)
{
parser.path_.push_attr();
parse_attr(parser, node);
parse_dimensions(parser, node);
}

break;
case "use"_case:
parser.path_.push_attr();
Expand Down Expand Up @@ -642,38 +651,26 @@ void parse_element(svg_parser& parser, char const* name, rapidxml::xml_node<char
switch (name_to_int(name))
{
case "path"_case:
parser.path_.transform().multiply(parser.viewbox_tr_);
parse_path(parser, node);
break;
case "polygon"_case:
parser.path_.transform().multiply(parser.viewbox_tr_);
parse_polygon(parser, node);
break;
case "polyline"_case:
parser.path_.transform().multiply(parser.viewbox_tr_);
parse_polyline(parser, node);
break;
case "line"_case:
parser.path_.transform().multiply(parser.viewbox_tr_);
parse_line(parser, node);
break;
case "rect"_case:
parser.path_.transform().multiply(parser.viewbox_tr_);
parse_rect(parser, node);
break;
case "circle"_case:
parser.path_.transform().multiply(parser.viewbox_tr_);
parse_circle(parser, node);
break;
case "ellipse"_case:
parser.path_.transform().multiply(parser.viewbox_tr_);
parse_ellipse(parser, node);
break;
case "svg"_case:
parser.path_.push_attr();
parse_dimensions(parser, node);
parse_attr(parser, node);
break;
default:
handle_unsupported(parser, unsupported_elements, name, "element");
break;
Expand Down Expand Up @@ -893,7 +890,6 @@ void parse_dimensions(svg_parser& parser, rapidxml::xml_node<char> const* node)
viewbox vbox = {0, 0, 0, 0};
bool has_percent_height = true;
bool has_percent_width = true;

auto const* width_attr = node->first_attribute("width");
if (width_attr)
{
Expand Down Expand Up @@ -933,58 +929,56 @@ void parse_dimensions(svg_parser& parser, rapidxml::xml_node<char> const* node)
double sx = width / vbox.width;
double sy = height / vbox.height;
double scale = preserve_aspect_ratio.second ? std::min(sx, sy) : std::max(sx, sy);

switch (preserve_aspect_ratio.first)
{
case none:
t = agg::trans_affine_scaling(sx, sy) * t;
t.premultiply(agg::trans_affine_scaling(sx, sy));
break;
case xMinYMin:
t = agg::trans_affine_scaling(scale, scale) * t;
t.premultiply(agg::trans_affine_scaling(scale, scale));
break;
case xMinYMid:
t = agg::trans_affine_scaling(scale, scale) * t;
t = agg::trans_affine_translation(0, -0.5 * (vbox.height - height / scale)) * t;
t.premultiply(agg::trans_affine_scaling(scale, scale));
t.premultiply(agg::trans_affine_translation(0, -0.5 * (vbox.height - height / scale)));
break;
case xMinYMax:
t = agg::trans_affine_scaling(scale, scale) * t;
t = agg::trans_affine_translation(0, -1.0 * (vbox.height - height / scale)) * t;
t.premultiply(agg::trans_affine_scaling(scale, scale));
t.premultiply(agg::trans_affine_translation(0, -1.0 * (vbox.height - height / scale)));
break;
case xMidYMin:
t = agg::trans_affine_scaling(scale, scale) * t;
t = agg::trans_affine_translation(-0.5 * (vbox.width - width / scale), 0.0) * t;
t.premultiply(agg::trans_affine_scaling(scale, scale));
t.premultiply(agg::trans_affine_translation(-0.5 * (vbox.width - width / scale), 0.0));
break;
case xMidYMid: // (the default)
t = agg::trans_affine_scaling(scale, scale) * t;
t = agg::trans_affine_translation(-0.5 * (vbox.width - width / scale),
-0.5 * (vbox.height - height / scale)) *
t;
t.premultiply(agg::trans_affine_scaling(scale, scale));
t.premultiply(agg::trans_affine_translation(-0.5 * (vbox.width - width / scale),
-0.5 * (vbox.height - height / scale)));
break;
case xMidYMax:
t = agg::trans_affine_scaling(scale, scale) * t;
t = agg::trans_affine_translation(-0.5 * (vbox.width - width / scale),
-1.0 * (vbox.height - height / scale)) *
t;
t.premultiply(agg::trans_affine_scaling(scale, scale));
t.premultiply(agg::trans_affine_translation(-0.5 * (vbox.width - width / scale),
-1.0 * (vbox.height - height / scale)));
break;
case xMaxYMin:
t = agg::trans_affine_scaling(scale, scale) * t;
t = agg::trans_affine_translation(-1.0 * (vbox.width - width / scale), 0.0) * t;
t.premultiply(agg::trans_affine_scaling(scale, scale));
t.premultiply(agg::trans_affine_translation(-1.0 * (vbox.width - width / scale), 0.0));
break;
case xMaxYMid:
t = agg::trans_affine_scaling(scale, scale) * t;
t = agg::trans_affine_translation(-1.0 * (vbox.width - width / scale),
-0.5 * (vbox.height - height / scale)) *
t;
t.premultiply(agg::trans_affine_scaling(scale, scale));
t.premultiply(agg::trans_affine_translation(-1.0 * (vbox.width - width / scale),
-0.5 * (vbox.height - height / scale)));
break;
case xMaxYMax:
t = agg::trans_affine_scaling(scale, scale) * t;
t = agg::trans_affine_translation(-1.0 * (vbox.width - width / scale),
-1.0 * (vbox.height - height / scale)) *
t;
t.premultiply(agg::trans_affine_scaling(scale, scale));
t.premultiply(agg::trans_affine_translation(-1.0 * (vbox.width - width / scale),
-1.0 * (vbox.height - height / scale)));
break;
};

t.premultiply(agg::trans_affine_translation(-vbox.x0, -vbox.y0));
parser.path_.transform().premultiply(t);
}
t = agg::trans_affine_translation(-vbox.x0, -vbox.y0) * t;
parser.viewbox_tr_ = t;
}
else if (width == 0 || height == 0 || has_percent_width || has_percent_height)
{
Expand All @@ -1004,7 +998,11 @@ void parse_dimensions(svg_parser& parser, rapidxml::xml_node<char> const* node)
parser.path_.set_dimensions(0, 0);
return;
}
parser.path_.set_dimensions(width, height);
if (!parser.dimensions_)
{
parser.dimensions_ = true;
parser.path_.set_dimensions(width, height);
}
}

void parse_path(svg_parser& parser, rapidxml::xml_node<char> const* node)
Expand Down
23 changes: 14 additions & 9 deletions test/unit/svg/svg_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ TEST_CASE("SVG parser")
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
// REQUIRE(bbox == mapnik::box2d<double>(0.3543307086614174,0.3543307086614174,
// 424.8425196850394059,141.3779527559055396));
REQUIRE(bbox == mapnik::box2d<double>(0.3779527559055118,0.3779527559055118,
453.1653543307086807,150.8031496062992005));
auto storage = svg.get_data();
REQUIRE(storage);
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(storage->source());
Expand Down Expand Up @@ -478,7 +478,8 @@ TEST_CASE("SVG parser")
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
// REQUIRE(bbox == mapnik::box2d<double>(1.0,1.0,1199.0,399.0));
REQUIRE(bbox == mapnik::box2d<double>(0.3779527559055118,0.3779527559055118,
453.1653543307086807,150.8031496062992005));
auto storage = svg.get_data();
REQUIRE(storage);
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(storage->source());
Expand Down Expand Up @@ -517,7 +518,8 @@ TEST_CASE("SVG parser")
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
// REQUIRE(bbox == mapnik::box2d<double>(1.0,1.0,1199.0,399.0));
REQUIRE(bbox == mapnik::box2d<double>(0.3779527559055118,0.3779527559055118,
453.1653543307086807,150.8031496062992005));
auto storage = svg.get_data();
REQUIRE(storage);
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(storage->source());
Expand Down Expand Up @@ -555,7 +557,8 @@ TEST_CASE("SVG parser")
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
// REQUIRE(bbox == mapnik::box2d<double>(1.0,1.0,799.0,599.0));
REQUIRE(bbox == mapnik::box2d<double>(75.7795275590551114,0.1889763779527559,
226.5826771653543119,113.1968503937007853));
auto storage = svg.get_data();
REQUIRE(storage);
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(storage->source());
Expand Down Expand Up @@ -651,7 +654,8 @@ TEST_CASE("SVG parser")
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
// REQUIRE(bbox == mapnik::box2d<double>(1.0,1.0,699.0,199.0));
REQUIRE(bbox == mapnik::box2d<double>(0.3779527559055118,0.3779527559055118,
264.1889763779527698,75.2125984251968447));
auto storage = svg.get_data();
REQUIRE(storage);
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(storage->source());
Expand Down Expand Up @@ -701,7 +705,8 @@ TEST_CASE("SVG parser")
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
// REQUIRE(bbox == mapnik::box2d<double>(1.0,1.0,799.0,599.0));
REQUIRE(bbox == mapnik::box2d<double>(75.7795275590551114,0.1889763779527559,
226.5826771653543119,113.1968503937007853));
auto storage = svg.get_data();
REQUIRE(storage);

Expand All @@ -722,7 +727,7 @@ TEST_CASE("SVG parser")
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
// REQUIRE(bbox == mapnik::box2d<double>(20,20,460,230));
REQUIRE(bbox == mapnik::box2d<double>(20,20,460,230));
auto storage = svg.get_data();
REQUIRE(storage);

Expand All @@ -741,7 +746,7 @@ TEST_CASE("SVG parser")
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
// REQUIRE(bbox == mapnik::box2d<double>(0,0,200,200));
REQUIRE(bbox == mapnik::box2d<double>(0,0,200,200));
auto storage = svg.get_data();
REQUIRE(storage);

Expand Down