Commit e94bd1e
Apply css styles
Changed files (5)
modifiedlibsvgbird/Selector.valamodifiedlibsvgbird/SelectorPattern.valamodifiedlibsvgbird/SelectorTag.valamodifiedlibsvgbird/StyleSheet.valamodifiedlibsvgbird/SvgDrawing.vala
Diff
@@ -19,14 +19,27 @@ namespace SvgBird { 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 class Selector : GLib.Object { 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 @@ public class Selector : GLib.Object { 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; }@@ -35,6 +35,26 @@ public class SelectorPattern : GLib.Object { } } + 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 () { StringBuilder s = new StringBuilder (); @@ -82,7 +102,9 @@ public class SelectorPattern : GLib.Object { 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; } }@@ -20,8 +20,8 @@ namespace SvgBird { /** 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 () {@@ -20,6 +20,7 @@ namespace SvgBird { public class StyleSheet : GLib.Object { + public string css_data { get; set; default = ""; } public Gee.ArrayList<Selector> styles = new Gee.ArrayList<Selector> (); public StyleSheet () {@@ -59,12 +60,26 @@ public class StyleSheet : GLib.Object { 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 @@ public class StyleSheet : GLib.Object { 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); @@ -24,28 +24,8 @@ public class SvgDrawing : Object { 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);