The Birdfont Source Code


All Repositories / birdfont.git / commit – RSS feed

Parse more style attributes in SVG files

These changes was commited to the Birdfont repository Wed, 14 Oct 2015 10:54:13 +0000.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
author Johan Mattsson <johan.mattsson.m@gmail.com>
Wed, 14 Oct 2015 10:54:13 +0000 (12:54 +0200)
committer Johan Mattsson <johan.mattsson.m@gmail.com>
Wed, 14 Oct 2015 10:54:13 +0000 (12:54 +0200)
commit f5139a98c883a2164bbd61922abd2bc8ab6243bf
tree 258286570d2e78ff92dc710d38cdc35b0e16460f
parent 6e22243b301c08a5a88eeef69dc1b2ee6e03d9ab
Parse more style attributes in SVG files

libbirdfont/SvgParser.vala
libbirdfont/SvgStyle.vala
--- a/libbirdfont/SvgParser.vala +++ b/libbirdfont/SvgParser.vala @@ -477,16 +477,14 @@ if (attr.get_name () == "r") { r = parse_double (attr.get_content ()); - } - - if (attr.get_name () == "style") { - style = SvgStyle.parse (attr.get_content ()); } if (attr.get_name () == "display" && attr.get_content () == "none") { hidden = true; } } + + style = SvgStyle.parse (tag.get_attributes ()); if (hidden) { return; @@ -547,16 +545,14 @@ if (attr.get_name () == "ry") { ry = parse_double (attr.get_content ()); - } - - if (attr.get_name () == "style") { - style = SvgStyle.parse (attr.get_content ()); } if (attr.get_name () == "display" && attr.get_content () == "none") { hidden = true; } } + + style = SvgStyle.parse (tag.get_attributes ()); if (hidden) { return; @@ -615,16 +611,14 @@ if (attr.get_name () == "xy") { y2 = -parse_double (attr.get_content ()); - } - - if (attr.get_name () == "style") { - style = SvgStyle.parse (attr.get_content ()); } if (attr.get_name () == "display" && attr.get_content () == "none") { hidden = true; } } + + style = SvgStyle.parse (tag.get_attributes ()); if (hidden) { return; @@ -694,16 +688,14 @@ if (attr.get_name () == "height") { y2 = -parse_double (attr.get_content ()); - } - - if (attr.get_name () == "style") { - style = SvgStyle.parse (attr.get_content ()); } if (attr.get_name () == "display" && attr.get_content () == "none") { hidden = true; } } + + style = SvgStyle.parse (tag.get_attributes ()); if (hidden) { return; @@ -784,23 +776,22 @@ foreach (Attribute attr in tag.get_attributes ()) { if (attr.get_name () == "points") { p = parse_poly_data (attr.get_content ()); - } - - if (attr.get_name () == "style") { - style = SvgStyle.parse (attr.get_content ()); } if (attr.get_name () == "display" && attr.get_content () == "none") { hidden = true; } } + + style = SvgStyle.parse (tag.get_attributes ()); if (hidden) { return path_list; } path_list.add (p); - + style.apply (path_list); + foreach (Attribute attr in tag.get_attributes ()) { if (attr.get_name () == "transform") { transform_paths (attr.get_content (), path_list); @@ -819,10 +810,6 @@ foreach (Attribute attr in tag.get_attributes ()) { if (attr.get_name () == "d") { path_list = parse_svg_data (attr.get_content (), glyph); - } - - if (attr.get_name () == "style") { - style = SvgStyle.parse (attr.get_content ()); } if (attr.get_name () == "display" && attr.get_content () == "none") { @@ -835,6 +822,8 @@ hidden = true; } } + + style = SvgStyle.parse (tag.get_attributes ()); if (hidden) { return;
--- a/libbirdfont/SvgStyle.vala +++ b/libbirdfont/SvgStyle.vala @@ -57,15 +57,39 @@ if (!style.has_key ("stroke-width")) { return 0; } - + return double.parse (style.get ("stroke-width")); } - public static SvgStyle parse (string svg_style) { + + public static SvgStyle parse (Attributes attributes) { + SvgStyle s = new SvgStyle (); + + foreach (Attribute a in attributes) { + if (a.get_name () == "style") { + s.parse_key_value_pairs (a.get_content ()); + } + + if (a.get_name () == "stroke-width") { + s.style.set ("stroke-width", a.get_content ()); + } + + if (a.get_name () == "stroke") { + s.style.set ("stroke", a.get_content ()); + } + + if (a.get_name () == "fill") { + s.style.set ("fill", a.get_content ()); + } + } + + return s; + } + + void parse_key_value_pairs (string svg_style) { string[] p = svg_style.split (";"); string[] pair; string k, v; - SvgStyle s = new SvgStyle (); foreach (string kv in p) { pair = kv.split (":"); @@ -78,10 +102,8 @@ k = pair[0]; v = pair[1]; - s.style.set (k, v); + style.set (k, v); } - - return s; } public void apply (PathList path_list) {