The Birdfont Source Code


All Repositories / birdfont.git / commitdiff – RSS feed

Apply css styles

These changes was commited to the Birdfont repository Sun, 03 Jul 2016 23:07:21 +0000.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
[Sun, 03 Jul 2016 23:07:21 +0000]

Updated Files

libsvgbird/Selector.vala
libsvgbird/SelectorPattern.vala
libsvgbird/SelectorTag.vala
libsvgbird/StyleSheet.vala
libsvgbird/SvgDrawing.vala
--- a/libsvgbird/Selector.vala +++ b/libsvgbird/Selector.vala @@ -19,14 +19,27 @@ public class Selector : GLib.Object { - Gee.ArrayList<SelectorPattern> patterns = new Gee.ArrayList<SelectorPattern> (); + Gee.ArrayList<SelectorPattern> tag_patterns = new Gee.ArrayList<SelectorPattern> (); + Gee.ArrayList<SelectorPattern> class_patterns = new Gee.ArrayList<SelectorPattern> (); + Gee.ArrayList<SelectorPattern> id_patterns = new Gee.ArrayList<SelectorPattern> (); + public SvgStyle style { get; set; } public Selector (string pattern, SvgStyle style) { string[] selector_patterns = pattern.split (","); for (int i = 0; i < selector_patterns.length; i++) { - patterns.add (new SelectorPattern (selector_patterns[i])); + SelectorPattern p = new SelectorPattern (selector_patterns[i]); + + if (p.has_id ()) { + id_patterns.add (p); + } + + if (p.has_class ()) { + class_patterns.add (p); + } + + tag_patterns.add (p); } this.style = style; @@ -35,15 +48,23 @@ public Selector.copy_constructor (Selector selector) { style = selector.style.copy (); - foreach (SelectorPattern pattern in selector.patterns) { - patterns.add (pattern.copy ()); + foreach (SelectorPattern pattern in selector.tag_patterns) { + tag_patterns.add (pattern.copy ()); + } + + foreach (SelectorPattern pattern in selector.class_patterns) { + class_patterns.add (pattern.copy ()); + } + + foreach (SelectorPattern pattern in selector.id_patterns) { + id_patterns.add (pattern.copy ()); } } public string to_string () { StringBuilder s = new StringBuilder (); - foreach (SelectorPattern pattern in patterns) { + foreach (SelectorPattern pattern in tag_patterns) { if (s.str != "") { s.append (", "); } @@ -58,8 +79,28 @@ return new Selector.copy_constructor (this); } - public bool match (XmlElement tag, string? id, string? css_class) { - foreach (SelectorPattern pattern in patterns) { + public bool match_tag (XmlElement tag, string? id, string? css_class) { + foreach (SelectorPattern pattern in tag_patterns) { + if (pattern.match (tag, id, css_class)) { + return true; + } + } + + return false; + } + + public bool match_id (XmlElement tag, string? id, string? css_class) { + foreach (SelectorPattern pattern in id_patterns) { + if (pattern.match (tag, id, css_class)) { + return true; + } + } + + return false; + } + + public bool match_class (XmlElement tag, string? id, string? css_class) { + foreach (SelectorPattern pattern in class_patterns) { if (pattern.match (tag, id, css_class)) { return true; }
--- a/libsvgbird/SelectorPattern.vala +++ b/libsvgbird/SelectorPattern.vala @@ -33,6 +33,26 @@ tags.add (new SelectorTag (element)); } } + } + + public bool has_id () { + foreach (SelectorTag tag in tags) { + if (tag.id != null) { + return true; + } + } + + return false; + } + + public bool has_class () { + foreach (SelectorTag tag in tags) { + if (tag.css_class != null) { + return true; + } + } + + return false; } public string to_string () { @@ -82,7 +102,9 @@ if (!is_immediately_following (tag, previous)) { return false; } - } else if (!pattern.match (tag, id, css_class)) { + } + + if (!pattern.match (tag, id, css_class)) { return false; } }
--- a/libsvgbird/SelectorTag.vala +++ b/libsvgbird/SelectorTag.vala @@ -20,8 +20,8 @@ /** A part of a CSS selector pattern. */ public class SelectorTag : GLib.Object { public string name; - string? id = null; - string? css_class = null; + public string? id = null; + public string? css_class = null; Gee.ArrayList<AttributePattern>? attribute_patterns = null; public SelectorTag.empty () {
--- a/libsvgbird/StyleSheet.vala +++ b/libsvgbird/StyleSheet.vala @@ -20,6 +20,7 @@ public class StyleSheet : GLib.Object { + public string css_data { get; set; default = ""; } public Gee.ArrayList<Selector> styles = new Gee.ArrayList<Selector> (); public StyleSheet () { @@ -57,14 +58,28 @@ id = attribute.get_content (); } else if (name == "class") { css_class = attribute.get_content (); + } + } + + foreach (Selector selector in styles) { + if (selector.match_tag (tag, id, css_class)) { + style.apply (selector.style); + } + } + + foreach (Selector selector in styles) { + if (selector.match_class (tag, id, css_class)) { + style.apply (selector.style); } } foreach (Selector selector in styles) { - if (selector.match (tag, id, css_class)) { + if (selector.match_id (tag, id, css_class)) { style.apply (selector.style); } } + + SvgStyle.set_style_properties (null, style); } public void merge (StyleSheet style_sheet) { @@ -72,11 +87,15 @@ styles.add (selector.copy ()); } } - + public static StyleSheet parse (Defs defs, XmlElement style_tag) { + return parse_css_data (defs, style_tag.get_content ()); + } + + public static StyleSheet parse_css_data (Defs defs, string css_data) { StyleSheet style_sheet = new StyleSheet (); - string css = style_tag.get_content (); - css = get_cdata (css); + string css = get_cdata (css_data); + style_sheet.css_data = css; css = add_separators (css); css = replace_whitespace (css);
--- a/libsvgbird/SvgDrawing.vala +++ b/libsvgbird/SvgDrawing.vala @@ -24,28 +24,8 @@ public Defs defs = new Defs (); public ViewBox? view_box = null; - public double width { - get { - return svg_width; - } - - set { - svg_width = value; - } - } - - public double height { - get { - return svg_height; - } - - set { - svg_height = value; - } - } - - public double svg_width = 0; - public double svg_height = 0; + public double width = 0; + public double height = 0; public override bool update_boundaries (Context cr) { apply_transform (cr);