Merge pull request #4450 from mapnik/svg-refactor

Svg refactor
This commit is contained in:
Artem Pavlenko 2024-04-30 10:07:21 +01:00 committed by GitHub
commit f84c30f20c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 61 deletions

View file

@ -73,7 +73,11 @@ class svg_converter : util::noncopyable
current_group_ = &current_group_->elements.back().get<group>(); current_group_ = &current_group_->elements.back().get<group>();
} }
void end_group() { current_group_ = current_group_->parent; } void end_group()
{
if (current_group_->parent != nullptr)
current_group_ = current_group_->parent;
}
void begin_path() void begin_path()
{ {

View file

@ -500,7 +500,7 @@ void traverse_tree(svg_parser& parser, rapidxml::xml_node<char> const* node)
switch (node->type()) switch (node->type())
{ {
case rapidxml::node_element: { case rapidxml::node_element:
parser.font_sizes_.push_back(parser.font_sizes_.back()); parser.font_sizes_.push_back(parser.font_sizes_.back());
switch (name) switch (name)
{ {
@ -525,57 +525,43 @@ void traverse_tree(svg_parser& parser, rapidxml::xml_node<char> const* node)
case "radialGradient"_case: case "radialGradient"_case:
parse_radial_gradient(parser, node); parse_radial_gradient(parser, node);
break; break;
} case "g"_case:
parser.path_.push_attr();
if (!parser.is_defs_) // FIXME parse_id(parser, node);
{ if (parser.css_style_)
switch (name) process_css(parser, node);
{ parse_attr(parser, node);
case "g"_case: parser.path_.begin_group();
if (node->first_node() != nullptr) break;
{ case "use"_case:
parser.path_.push_attr(); parser.path_.push_attr();
parse_id(parser, node); parse_id(parser, node);
if (parser.css_style_) if (parser.css_style_)
process_css(parser, node); process_css(parser, node);
parse_attr(parser, node); parse_attr(parser, node);
parser.path_.begin_group(); if (parser.path_.cur_attr().opacity < 1.0)
} parser.path_.begin_group();
break; parse_use(parser, node);
case "use"_case: if (parser.path_.cur_attr().opacity < 1.0)
parser.path_.push_attr(); parser.path_.end_group();
parse_id(parser, node); parser.path_.pop_attr();
if (parser.css_style_) break;
process_css(parser, node); default:
parse_attr(parser, node); parser.path_.push_attr();
parse_id(parser, node);
if (parser.css_style_)
process_css(parser, node);
parse_attr(parser, node);
if (parser.path_.display())
{
if (parser.path_.cur_attr().opacity < 1.0) if (parser.path_.cur_attr().opacity < 1.0)
parser.path_.begin_group(); parser.path_.begin_group();
parse_use(parser, node); parse_element(parser, node->name(), node);
if (parser.path_.cur_attr().opacity < 1.0) if (parser.path_.cur_attr().opacity < 1.0)
parser.path_.end_group(); parser.path_.end_group();
parser.path_.pop_attr(); }
break; parser.path_.pop_attr();
default: break;
parser.path_.push_attr();
parse_id(parser, node);
if (parser.css_style_)
process_css(parser, node);
parse_attr(parser, node);
if (parser.path_.display())
{
if (parser.path_.cur_attr().opacity < 1.0)
parser.path_.begin_group();
parse_element(parser, node->name(), node);
if (parser.path_.cur_attr().opacity < 1.0)
parser.path_.end_group();
}
parser.path_.pop_attr();
}
}
else
{
// save node for later
parse_id(parser, node);
} }
if ("style"_case == name) if ("style"_case == name)
@ -607,8 +593,7 @@ void traverse_tree(svg_parser& parser, rapidxml::xml_node<char> const* node)
} }
end_element(parser, node); end_element(parser, node);
} break;
break;
default: default:
break; break;
} }
@ -618,20 +603,14 @@ void end_element(svg_parser& parser, rapidxml::xml_node<char> const* node)
{ {
parser.font_sizes_.pop_back(); parser.font_sizes_.pop_back();
auto name = name_to_int(node->name()); auto name = name_to_int(node->name());
if (!parser.is_defs_ && (name == "g"_case)) if (name == "g"_case)
{ {
parser.path_.end_group(); parser.path_.end_group();
if (node->first_node() != nullptr) parser.path_.pop_attr();
{
parser.path_.pop_attr();
}
} }
else if (name == "svg"_case) else if (name == "svg"_case)
{ {
// if (node->first_node() != nullptr) parser.path_.pop_attr();
//{
// parser.path_.pop_attr();
//}
} }
else if (name == "defs"_case) else if (name == "defs"_case)
{ {
@ -680,7 +659,7 @@ void parse_element(svg_parser& parser, char const* name, rapidxml::xml_node<char
parse_ellipse(parser, node); parse_ellipse(parser, node);
break; break;
case "svg"_case: case "svg"_case:
// parser.path_.push_attr(); parser.path_.push_attr();
parse_dimensions(parser, node); parse_dimensions(parser, node);
parse_attr(parser, node); parse_attr(parser, node);
parser.path_.set_opacity(parser.path_.cur_attr().opacity); parser.path_.set_opacity(parser.path_.cur_attr().opacity);