The Birdfont Source Code


All Repositories / birdfont.git / commit – RSS feed

Inherit SVG style tags

These changes was commited to the Birdfont repository Sun, 10 Jan 2016 22:36:11 +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>
Sun, 10 Jan 2016 22:36:11 +0000 (23:36 +0100)
committer Johan Mattsson <johan.mattsson.m@gmail.com>
Sun, 10 Jan 2016 22:36:11 +0000 (23:36 +0100)
commit 8c5aaaf6c066bc3b1a5ba045102f8d1ce802ae81
tree 4d256e2a5cbeb5214bc86ffd94500c142c272341
parent ec3054d1e69652b3c171e4c5f70cf3a9f764542a
Inherit SVG style tags

libsvgbird/Color.vala
libsvgbird/SvgFile.vala
libsvgbird/SvgStyle.vala
--- a/libsvgbird/Color.vala +++ b/libsvgbird/Color.vala @@ -32,7 +32,7 @@ return null; } - string color = (!) svg_color + string color = (!) svg_color; uint32 c; string[] arguments; Color parsed = new Color (0, 0, 0, 1); @@ -114,7 +114,7 @@ } public static string get_hex_for_name (string name) { - string color = name.to_lower (); + string color = name.down (); if (color == "black") return "#000000"; if (color == "silver") return "#C0C0C0";
--- a/libsvgbird/SvgFile.vala +++ b/libsvgbird/SvgFile.vala @@ -26,6 +26,9 @@ public SvgDrawing parse_svg_file (Tag tag) { drawing = new SvgDrawing (); + + SvgStyle style = new SvgStyle (); + SvgStyle.parse (drawing.defs, style, tag.get_attributes ()); foreach (Attribute attr in tag.get_attributes ()) { if (attr.get_name () == "width") { @@ -41,7 +44,7 @@ string name = t.get_name (); if (name == "g") { - parse_layer (drawing.root_layer, t); + parse_layer (drawing.root_layer, style, t); } if (name == "defs") { @@ -49,16 +52,16 @@ } if (name == "a") { - parse_link (drawing.root_layer, tag); + parse_link (drawing.root_layer, style, tag); } - parse_object (drawing.root_layer, t); + parse_object (drawing.root_layer, style, t); } return drawing; } - private void parse_layer (Layer layer, Tag tag) { + private void parse_layer (Layer layer, SvgStyle parent_style, Tag tag) { bool hidden = false; foreach (Attribute attr in tag.get_attributes ()) { @@ -76,21 +79,23 @@ if (hidden) { layer.visible = !hidden; } - + + SvgStyle style = SvgStyle.parse (drawing.defs, parent_style, tag.get_attributes ()); + foreach (Tag t in tag) { string name = t.get_name (); if (name == "g") { Layer sublayer = new Layer (); - parse_layer (layer, t); + parse_layer (layer, parent_style, t); layer.subgroups.add (sublayer); } if (name == "a") { - parse_link (layer, t); + parse_link (layer, style, t); } - parse_object (layer, t); + parse_object (layer, style, t); } foreach (Attribute attr in tag.get_attributes ()) { @@ -174,7 +179,8 @@ } void parse_stop (Gradient gradient, Tag tag) { - SvgStyle style = SvgStyle.parse (drawing.defs, tag.get_attributes ()); + SvgStyle parent_style = new SvgStyle (); // not inherited + SvgStyle style = SvgStyle.parse (drawing.defs, parent_style, tag.get_attributes ()); Stop stop = new Stop (); gradient.stops.add (stop); @@ -212,49 +218,49 @@ } // links are ignored, add the content to the layer - void parse_link (Layer layer, Tag tag) { - parse_layer (layer, tag); + void parse_link (Layer layer, SvgStyle parent_style, Tag tag) { + parse_layer (layer, parent_style, tag); } - void parse_object (Layer layer, Tag tag) { + void parse_object (Layer layer, SvgStyle parent_style, Tag tag) { string name = tag.get_name (); if (name == "path") { - parse_path (layer, tag); + parse_path (layer, parent_style, tag); } if (name == "polygon") { - parse_polygon (layer, tag); + parse_polygon (layer, parent_style, tag); } if (name == "polyline") { - parse_polyline (layer, tag); + parse_polyline (layer, parent_style, tag); } if (name == "rect") { - parse_rect (layer, tag); + parse_rect (layer, parent_style, tag); } if (name == "circle") { - parse_circle (layer, tag); + parse_circle (layer, parent_style, tag); } if (name == "ellipse") { - parse_ellipse (layer, tag); + parse_ellipse (layer, parent_style, tag); } if (name == "line") { - parse_line (layer, tag); + parse_line (layer, parent_style, tag); } } - private void parse_polygon (Layer layer, Tag tag) { + private void parse_polygon (Layer layer, SvgStyle parent_style, Tag tag) { } - private void parse_polyline (Layer layer, Tag tag) { + private void parse_polyline (Layer layer, SvgStyle parent_style, Tag tag) { } - private void parse_rect (Layer layer, Tag tag) { + private void parse_rect (Layer layer, SvgStyle parent_style, Tag tag) { Rectangle rectangle = new Rectangle (); foreach (Attribute attr in tag.get_attributes ()) { @@ -286,13 +292,13 @@ } rectangle.transforms = get_transform (tag.get_attributes ()); - rectangle.style = SvgStyle.parse (drawing.defs,tag.get_attributes ()); + rectangle.style = SvgStyle.parse (drawing.defs, parent_style, tag.get_attributes ()); rectangle.visible = is_visible (tag); layer.add_object (rectangle); } - private void parse_circle (Layer layer, Tag tag) { + private void parse_circle (Layer layer, SvgStyle parent_style, Tag tag) { Circle circle = new Circle (); foreach (Attribute attr in tag.get_attributes ()) { @@ -312,13 +318,13 @@ } circle.transforms = get_transform (tag.get_attributes ()); - circle.style = SvgStyle.parse (drawing.defs, tag.get_attributes ()); + circle.style = SvgStyle.parse (drawing.defs, parent_style, tag.get_attributes ()); circle.visible = is_visible (tag); layer.add_object (circle); } - private void parse_ellipse (Layer layer, Tag tag) { + private void parse_ellipse (Layer layer, SvgStyle parent_style, Tag tag) { Ellipse ellipse = new Ellipse (); foreach (Attribute attr in tag.get_attributes ()) { @@ -342,13 +348,13 @@ } ellipse.transforms = get_transform (tag.get_attributes ()); - ellipse.style = SvgStyle.parse (drawing.defs, tag.get_attributes ()); + ellipse.style = SvgStyle.parse (drawing.defs, parent_style, tag.get_attributes ()); ellipse.visible = is_visible (tag); layer.add_object (ellipse); } - private void parse_line (Layer layer, Tag tag) { + private void parse_line (Layer layer, SvgStyle parent_style, Tag tag) { Line line = new Line (); foreach (Attribute attr in tag.get_attributes ()) { @@ -372,7 +378,7 @@ } line.transforms = get_transform (tag.get_attributes ()); - line.style = SvgStyle.parse (drawing.defs, tag.get_attributes ()); + line.style = SvgStyle.parse (drawing.defs, parent_style, tag.get_attributes ()); line.visible = is_visible (tag); layer.add_object (line); @@ -559,7 +565,7 @@ return new SvgTransforms (); } - private void parse_path (Layer layer, Tag tag) { + private void parse_path (Layer layer, SvgStyle parent_style, Tag tag) { SvgPath path = new SvgPath (); foreach (Attribute attr in tag.get_attributes ()) { @@ -569,7 +575,7 @@ } path.transforms = get_transform (tag.get_attributes ()); - path.style = SvgStyle.parse (drawing.defs, tag.get_attributes ()); + path.style = SvgStyle.parse (drawing.defs, parent_style, tag.get_attributes ()); path.visible = is_visible (tag); layer.add_object (path);
--- a/libsvgbird/SvgStyle.vala +++ b/libsvgbird/SvgStyle.vala @@ -68,11 +68,18 @@ return double.parse (style.get ("stroke-width")); } - public static SvgStyle parse (Defs? d, Attributes attributes) { + public static SvgStyle parse (Defs? d, SvgStyle inherited, Attributes attributes) { SvgStyle s = new SvgStyle (); double fill_opacity = 1; double stroke_opacity = 1; - + + s.style.set ("fill", "#000000"); // default fill value + + // inherit + foreach (string key in inherited.style.keys) { + s.style.set (key, inherited.style.get (key)); + } + foreach (Attribute a in attributes) { if (a.get_name () == "style") { s.parse_key_value_pairs (a.get_content ()); @@ -91,11 +98,11 @@ } if (a.get_name () == "fill-opacity") { - fill_opacity = SvgFile.parse_number (a.get_content ()); + s.style.set ("fill-opacity", a.get_content ()); } if (a.get_name () == "stroke-opacity") { - stroke_opacity = SvgFile.parse_number (a.get_content ()); + s.style.set ("stroke-opacity", a.get_content ()); } } @@ -103,6 +110,16 @@ s.stroke = Color.parse (s.style.get ("stroke")); s.fill = Color.parse (s.style.get ("fill")); + string? opacity = s.style.get ("fill-opacity"); + if (opacity != null) { + fill_opacity = SvgFile.parse_number ((!) opacity); + } + + opacity = s.style.get ("stroke-opacity"); + if (opacity != null) { + stroke_opacity = SvgFile.parse_number ((!) opacity); + } + if (d != null) { Defs defs = (!) d;