The Birdfont Source Code


All Repositories / birdfont.git / commit – RSS feed

Use max-height and max-width properties in CSS

These changes was commited to the Birdfont repository Fri, 15 Jan 2016 11:17:07 +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>
Fri, 15 Jan 2016 11:17:07 +0000 (12:17 +0100)
committer Johan Mattsson <johan.mattsson.m@gmail.com>
Fri, 15 Jan 2016 11:17:07 +0000 (12:17 +0100)
commit 8dd2ef3be86e5c55bc8382c346b56a56f1677ea6
tree 84a3a9b122a9a2cff71a346645ea4c3c6a097c7b
parent 7825ea96f0b0602a4a9b104e88194e0be2595a2f
Use max-height and max-width properties in CSS

12 files changed:
birdui/AbsoluteLayout.vala
birdui/BoxLayout.vala
birdui/Component.vala
birdui/GtkWindow.vala
birdui/HBox.vala
birdui/SvgComponent.vala
birdui/VBox.vala
libsvgbird/SelectorPattern.vala
libsvgbird/SelectorTag.vala
libsvgbird/StyleSheet.vala
libsvgbird/SvgStyle.vala
--- a/birdui/AbsoluteLayout.vala +++ b/birdui/AbsoluteLayout.vala @@ -20,11 +20,8 @@ class AbsoluteLayout : Component { - public AbsoluteLayout () { - } - - public AbsoluteLayout.for_tag (XmlElement layout_tag) { - parse (layout_tag); + public AbsoluteLayout (XmlElement layout_tag, Defs defs) { + base (layout_tag, defs); } public override string to_string () {
--- a/birdui/BoxLayout.vala +++ b/birdui/BoxLayout.vala @@ -27,48 +27,32 @@ public BoxOrientation orientation { get; private set; } - public BoxLayout (BoxOrientation orientation) { - base (); + public BoxLayout (XmlElement layout_tag, BoxOrientation orientation, Defs defs) { + base (layout_tag, defs); this.orientation = orientation; } - - public BoxLayout.for_tag (XmlElement layout_tag, BoxOrientation orientation) { - base.for_tag (layout_tag); - this.orientation = orientation; - } - - public override void layout () { - switch (orientation) { - case BoxOrientation.HORIZONTAL: - layout_horizontal (); - break; - case BoxOrientation.VERTICAL: - layout_vertical (); - break; + + void set_max_size (Component component) { + string? max_width = style.get_css_property ("max-width"); + if (max_width != null) { + double w = SvgFile.parse_number (max_width); + + if (component.width > w) { + component.width = w; + } } - } - void layout_horizontal () { - double child_x = 0; - double child_y = 0; - - foreach (Component component in components) { - component.x = child_x; - component.y = child_y; - component.layout (); - component.apply_padding (); - - child_x += component.padded_width; + string? max_height = style.get_css_property ("max-height"); + if (max_height != null) { + double h = SvgFile.parse_number (max_height); - if (component.height > height) { - height = component.height; + if (component.height > h) { + component.height = h; } - } - - width = child_x; + } } - - void layout_vertical () { + + public override void layout () { double child_x = 0; double child_y = 0; @@ -77,15 +61,28 @@ component.y = child_y; component.layout (); component.apply_padding (); + set_max_size (component); - child_y += component.padded_height; - - if (component.width > width) { - width = component.width; + if (orientation == BoxOrientation.HORIZONTAL) { + child_x += component.padded_width; + + if (component.height > height) { + height = component.height; + } + } else { + child_y += component.padded_height; + + if (component.width > width) { + width = component.width; + } } } - height = child_y; + if (orientation == BoxOrientation.HORIZONTAL) { + width = child_x; + } else { + height = child_y; + } } public override string to_string () {
--- a/birdui/Component.vala +++ b/birdui/Component.vala @@ -44,45 +44,44 @@ /** The parts this component is made of. */ protected ArrayList<Component> components = new ArrayList<Component> (); - XmlElement? component_tag = null; + XmlElement component_tag; /** Style sheet and other SVG definitions. */ - Defs? defs = null; - SvgStyle style = new SvgStyle (); + Defs defs = new Defs (); + protected SvgStyle style = new SvgStyle (); string? css_class = null; string? id = null; - public Component () { + public Component (XmlElement component_tag, Defs defs) { + this.defs = defs; + this.component_tag = component_tag; + parse (component_tag); } - public Component.for_tag (XmlElement component_tag) { + public Component.embedded (XmlElement component_tag, Defs defs) { + this.defs = defs; this.component_tag = component_tag; - parse (component_tag); + } + + public Component.load (string file_name) { + load_file (file_name); } - private void inherit_style (Defs? defs) { + private void inherit_style (Defs defs) { this.defs = defs; } protected void apply_padding () { - } - - protected void load_svg_file (string file_name) { - SvgComponent svg = new SvgComponent.for_file (file_name); - add_component (svg); } protected void parse_style (XmlElement style_tag) { Defs definitions = new Defs (); - StyleSheet style_sheet = StyleSheet.parse (definitions, style_tag); - - if (defs != null) { - Defs subscope_definitions = ((!) defs).shallow_copy (); - subscope_definitions.style_sheet.merge (style_sheet); - } else { - defs = definitions; - } + definitions.style_sheet = StyleSheet.parse (definitions, style_tag); + + Defs subscope_definitions = defs.shallow_copy (); + subscope_definitions.style_sheet.merge (definitions.style_sheet); + defs = subscope_definitions; } protected void parse_svg (XmlElement svg_tag) { @@ -90,7 +89,9 @@ string attribute_name = attribute.get_name (); if (attribute_name == "file") { - load_svg_file (attribute.get_content ()); + string file_name = attribute.get_content (); + SvgComponent svg = new SvgComponent (component_tag, defs, file_name); + add_component (svg); } } } @@ -98,22 +99,21 @@ protected void add_component (Component component) { components.add (component); component.inherit_style (defs); - - if (component.component_tag != null) { - style = SvgStyle.parse (defs, style, (!) component.component_tag); - } + component.style = SvgStyle.parse (defs, style, (!) component.component_tag); } protected void parse_layout (XmlElement layout_tag) { + bool parse_style = false; + foreach (Attribute attribute in layout_tag.get_attributes ()) { string attribute_name = attribute.get_name (); if (attribute_name == "type") { if (attribute.get_content () == "hbox") { - HBox hbox = new HBox.for_tag (layout_tag); + HBox hbox = new HBox (layout_tag, defs); add_component (hbox); } else if (attribute.get_content () == "vbox") { - VBox hbox = new VBox.for_tag (layout_tag); + VBox hbox = new VBox (layout_tag, defs); add_component (hbox); } else { warning ("Layout of type " + attribute.get_content () @@ -124,7 +124,7 @@ } else if (attribute_name == "class") { css_class = attribute.get_content (); } else if (attribute_name == "style") { - // style will be parsed later + // style will be parsed later in add_component } else { unused_attribute (attribute_name); } @@ -136,8 +136,7 @@ string attribute_name = attribute.get_name (); if (attribute_name == "file") { - Component component = new Component (); - component.load (attribute.get_content ()); + Component component = new Component (component_tag, defs); add_component (component); } else { unused_attribute (attribute_name); @@ -173,11 +172,12 @@ warning ("The tag " + tag_name + " is not known in this version."); } - public void load (string file_name) { + public void load_file (string file_name) { if (file_name.has_suffix (".ui")) { load_layout (file_name); } else if (file_name.has_suffix (".svg")) { - load_svg_file (file_name); + SvgComponent svg = new SvgComponent.for_file (file_name); + add_component (svg); } else { warning (file_name + " is not a ui file or svg file."); } @@ -202,8 +202,11 @@ } XmlTree xml_parser = new XmlTree (xml_data); - Component component = new Component.for_tag (xml_parser.get_root ()); - add_component (component); + + component_tag = xml_parser.get_root (); + defs = new Defs (); + parse (component_tag); + layout (); } @@ -246,13 +249,7 @@ } public virtual string to_string () { - if (component_tag != null) { - XmlElement tag = (!) component_tag; - return @"Component $(tag.get_name ())"; - } else { - - return "Component"; - } + return @"Component $(component_tag.get_name ())"; } public void print_tree () {
--- a/birdui/GtkWindow.vala +++ b/birdui/GtkWindow.vala @@ -22,8 +22,7 @@ window.set_title ("UI Bird"); window.destroy.connect (Gtk.main_quit); - Component layout = new Component (); - layout.load ("test.ui"); + Component layout = new Component.load ("test.ui"); Bird.Widget primary_layout = new Bird.Widget (layout);
--- a/birdui/HBox.vala +++ b/birdui/HBox.vala @@ -14,16 +14,13 @@ using Gee; using B; + using SvgBird; namespace Bird { class HBox : BoxLayout { - public HBox () { - base (BoxOrientation.HORIZONTAL); - } - - public HBox.for_tag (XmlElement layout) { - base.for_tag (layout, BoxOrientation.HORIZONTAL); + public HBox (XmlElement layout, Defs defs) { + base (layout, BoxOrientation.HORIZONTAL, defs); } public override string to_string () {
--- a/birdui/SvgComponent.vala +++ b/birdui/SvgComponent.vala @@ -24,16 +24,13 @@ string file_name = ""; SvgDrawing? drawing = null; - public SvgComponent () { - base (); - } - - public SvgComponent.for_tag (XmlElement svg_component_tag) { - base (); + public SvgComponent (XmlElement svg_component_tag, Defs defs, string svg_file) { + base.embedded (svg_component_tag, defs); + load_svg (svg_file); } public SvgComponent.for_file (string svg_file) { - base (); + base.embedded (new XmlElement.empty (), new Defs ()); load_svg (svg_file); }
--- a/birdui/VBox.vala +++ b/birdui/VBox.vala @@ -14,16 +14,13 @@ using Gee; using B; + using SvgBird; namespace Bird { class VBox : BoxLayout { - public VBox () { - base (BoxOrientation.VERTICAL); - } - - public VBox.for_tag (XmlElement layout) { - base.for_tag (layout, BoxOrientation.VERTICAL); + public VBox (XmlElement layout, Defs defs) { + base (layout, BoxOrientation.VERTICAL, defs); } public override string to_string () {
--- a/libsvgbird/SelectorPattern.vala +++ b/libsvgbird/SelectorPattern.vala @@ -51,7 +51,6 @@ if (pattern.name == ">") { if (i - 1 < 0) { - return false; }
--- a/libsvgbird/SelectorTag.vala +++ b/libsvgbird/SelectorTag.vala @@ -146,7 +146,7 @@ if (id == null) { return false; } - + if (((!) this.id) != ((!) id)) { return false; }
--- a/libsvgbird/StyleSheet.vala +++ b/libsvgbird/StyleSheet.vala @@ -47,7 +47,7 @@ return style_sheet; } - public void inherit_style (XmlElement tag, SvgStyle style) { + public void apply_style (XmlElement tag, SvgStyle style) { string? id = null; string? css_class = null; @@ -63,7 +63,7 @@ foreach (Selector selector in styles) { if (selector.match (tag, id, css_class)) { - style.inherit (selector.style); + style.apply (selector.style); } } }
--- a/libsvgbird/SvgStyle.vala +++ b/libsvgbird/SvgStyle.vala @@ -28,48 +28,59 @@ public double stroke_width = 0; private static Gee.HashMap<string, string>? inheritance; + private static Mutex inheritance_mutex = new Mutex (); public SvgStyle () { } + public bool has_css_property (string property) { + return style.has_key (property); + } + + public string? get_css_property (string property) { + string p = property.down (); + + if (!has_css_property (p)) { + return null; + } + + return style.get (p); + } + public static bool is_inherited (string property) { - lock (inheritace) { - if (unlikely (inheritance == null)) { - create_inheritance_table (); - } - - Gee.HashMap<string, string> inheritance = (!) inheritance; - string? inherited = inherited.get (property); - - if (inherited == null) { - return true; - } + inheritance_mutex.lock (); + if (unlikely (inheritance == null)) { + create_inheritance_table (); + } + + Gee.HashMap<string, string> inheritance = (!) inheritance; + string? inherited = inheritance.get (property); + inheritance_mutex.unlock (); - string inherited_property = (!) inherited; - return inherited_property == "yes"; + if (inherited == null) { + return false; } + + string inherited_property = (!) inherited; + return inherited_property == "yes"; } /** Specify inheritance for a CSS property. */ - public static bool set_inheritance (string property, bool inherit) { - lock (inheritace) { - if (unlikely (inheritance == null)) { - create_inheritance_table (); - } - - Gee.HashMap<string, string> inheritance = (!) inheritance; - string inherit_property = inherit ? "yes" : "no"; - inheritance.set (property, inherit_property); + public static void set_inheritance (string property, bool inherit) { + inheritance_mutex.lock (); + if (unlikely (inheritance == null)) { + create_inheritance_table (); } + + Gee.HashMap<string, string> inheritance = (!) inheritance; + string inherit_property = inherit ? "yes" : "no"; + inheritance.set (property, inherit_property); + inheritance_mutex.unlock (); } public SvgStyle.for_properties (Defs? defs, string style) { parse_key_value_pairs (style); set_style_properties (defs, this); - } - - public string? get_css_property (string key) { - return style.get (key.down ()); } public SvgStyle copy () { @@ -108,6 +119,21 @@ public double get_stroke_width () { return stroke_width; + } + + public string to_string () { + StringBuilder description = new StringBuilder (); + + description.append ("SvgStyle: "); + + foreach (string key in style.keys) { + description.append (key); + description.append (": "); + description.append (style.get (key)); + description.append ("; "); + } + + return description.str.strip (); } public void inherit (SvgStyle inherited) { @@ -115,6 +141,12 @@ if (is_inherited (key)) { style.set (key, inherited.style.get (key)); } + } + } + + public void apply (SvgStyle inherited) { + foreach (string key in inherited.style.keys) { + style.set (key, inherited.style.get (key)); } } @@ -127,7 +159,7 @@ if (d != null) { StyleSheet style_sheet = ((!) d).style_sheet; - style_sheet.inherit_style (tag, s); + style_sheet.apply_style (tag, s); } foreach (Attribute a in attributes) { @@ -242,8 +274,8 @@ } } - private static create_inheritance_table () { - inheritance = Gee.HashMap<string, string> (); + private static void create_inheritance_table () { + inheritance = new Gee.HashMap<string, string> (); Gee.HashMap<string, string> inherited = (!) inheritance;