Clone in a terminal:
Commit 3336516
Johan Mattsson - 2016-07-02T03:24:45+00:00
3336516c6e2e065929a97b767ed70e45e6977dcdjohan.mattsson.m@gmail.com

Parse SVG view box

Changed files (4)

Diff

diff --git a/libsvgbird/Object.vala b/libsvgbird/Object.valaindex 55dc3d9..8a560a2 100644--- a/libsvgbird/Object.vala+++ b/libsvgbird/Object.vala@@ -192,7 +192,7 @@ public abstract class Object : GLib.Object { 		} 	} 	-	public void apply_transform (Context cr) {+	public virtual void apply_transform (Context cr) { 		Matrix view_matrix = cr.get_matrix (); 		Matrix object_matrix = transforms.get_matrix (); 		diff --git a/libsvgbird/SvgDrawing.vala b/libsvgbird/SvgDrawing.valaindex 49c5ff6..af48f32 100644--- a/libsvgbird/SvgDrawing.vala+++ b/libsvgbird/SvgDrawing.vala@@ -22,6 +22,7 @@ namespace SvgBird { public class SvgDrawing : Object { 	public Layer root_layer = new Layer (); 	public Defs defs = new Defs ();+	public ViewBox? view_box = null;  	public double width { 		get {			@@ -62,13 +63,31 @@ public class SvgDrawing : Object { 		return false; 	} +	void apply_view_box (Context cr) {+		if (view_box != null) {+			ViewBox box = (!) view_box;+			cr.translate (box.minx, box.miny);+			cr.scale (width / box.width, height / box.height);+		}+	}++	public override void apply_transform (Context cr) {+		apply_view_box (cr);+		base.apply_transform (cr);+	}+ 	public void draw (Context cr) {+		cr.save (); 		apply_transform (cr); 		root_layer.draw (cr);+		cr.restore (); 	} 		 	public override void draw_outline (Context cr) {+		cr.save ();+		apply_transform (cr); 		root_layer.draw_outline (cr);+		cr.restore (); 	} 	 	public override Object copy () {diff --git a/libsvgbird/SvgFile.vala b/libsvgbird/SvgFile.valaindex 050a5b5..2a1a856 100644--- a/libsvgbird/SvgFile.vala+++ b/libsvgbird/SvgFile.vala@@ -44,6 +44,10 @@ public class SvgFile : GLib.Object { 			if (attr.get_name () == "height") { 				drawing.height = parse_number (attr.get_content ()); 			}++			if (attr.get_name () == "viewBox") {+				drawing.view_box = parse_view_box (attr.get_content ());+			} 		} 				 		foreach (XmlElement t in svg_tag) {@@ -53,6 +57,11 @@ public class SvgFile : GLib.Object { 				parse_layer (drawing.root_layer, style, t); 			} +			if (name == "svg") {+				SvgDrawing embedded = parse_svg_file (t);+				drawing.root_layer.add_object (embedded);+			}+			 			if (name == "defs") { 				parse_defs (drawing, t); 			}@@ -69,6 +78,28 @@ public class SvgFile : GLib.Object { 		return drawing; 	} +	ViewBox? parse_view_box (string parameters) {+		string arguments = parameters.replace (",", " ");+		+		while (arguments.index_of ("  ") > -1) {+			arguments = arguments.replace ("  ", " ");+		}+		+		string[] view_box_parameters = arguments.split (" ");+		+		if (view_box_parameters.length != 4) {+			warning ("Expecting four arguments in view box.");+			return null;+		}++		double minx = parse_number (view_box_parameters[0]);+		double miny = parse_number (view_box_parameters[1]);+		double width = parse_number (view_box_parameters[2]);+		double height = parse_number (view_box_parameters[3]);+		+		return new ViewBox (minx, miny, width, height);+	}+ 	private void parse_layer (Layer layer, SvgStyle parent_style, XmlElement tag) {		 		foreach (XmlElement t in tag) { 			string name = t.get_name ();@@ -310,6 +341,12 @@ public class SvgFile : GLib.Object { 		if (name == "line") { 			parse_line (layer, parent_style, tag); 		}+		+		if (name == "svg") {+			SvgDrawing embedded = parse_svg_file (tag);+			layer.add_object (embedded);+		}+ 	}  	private void parse_polygon (Layer layer, SvgStyle parent_style, XmlElement tag) {@@ -1408,7 +1445,6 @@ public class SvgFile : GLib.Object { 		 		return data; 	}- }  }diff --git a/libsvgbird/ViewBox.vala b/libsvgbird/ViewBox.valanew file mode 100644index 0000000..2d1c786--- /dev/null+++ b/libsvgbird/ViewBox.vala@@ -0,0 +1,38 @@+/*+	Copyright (C) 2016 Johan Mattsson++	This library is free software; you can redistribute it and/or modify +	it under the terms of the GNU Lesser General Public License as +	published by the Free Software Foundation; either version 3 of the +	License, or (at your option) any later version.++	This library is distributed in the hope that it will be useful, but +	WITHOUT ANY WARRANTY; without even the implied warranty of +	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +	Lesser General Public License for more details.+*/+++using B;+using Cairo;+using Math;++namespace SvgBird {++public class ViewBox : GLib.Object {++	public double minx = 0;+	public double miny = 0;+	public double width = 0;+	public double height = 0;++	public ViewBox (double minx, double miny, double width, double height) {+		this.minx = minx;+		this.miny = miny;+		this.width = width;+		this.height = height;+	}+	+}++}

Contact

Email: johan.mattsson.m@gmail.com
Mastodon: typo.social/@birdfont
GitHub: johanmattssonm
LinkedIn: johan-mattsson-2ab324112