Clone in a terminal:
Commit 612d5fe
Johan Mattsson - 2016-07-03T14:04:09+00:00
612d5fe26db60f126e5c86605a0c5b5dc3979e39johan.mattsson.m@gmail.com

Fix alignment in view box

Changed files (3)

Diff

diff --git a/libsvgbird/SvgDrawing.vala b/libsvgbird/SvgDrawing.valaindex 5aeb4ee..2ba1c0b 100644--- a/libsvgbird/SvgDrawing.vala+++ b/libsvgbird/SvgDrawing.vala@@ -64,49 +64,48 @@ public class SvgDrawing : Object { 	}  	void apply_view_box (Context cr) {+		double scale_x = 1;+		double scale_y = 1;+		double scale = 1;+ 		if (view_box != null) { 			ViewBox box = (!) view_box;-			double scale_x = 1;-			double scale_y = 1;-			double scale = 1; 			 			cr.translate (box.minx, box.miny); 			scale_x = width / box.width; 			scale_y = height / box.height; 			-			bool scale_width = height * box.width > width * box.height;+			bool scale_width = scale_x > scale_y; 			-			if (box.alignment == ViewBox.NONE) {	-				cr.scale (scale_x, scale_y);-			} else if (scale_width && box.slice) {-				scale = scale_x;-				cr.scale (scale, scale);-			} else {+			if (scale_width) { 				scale = scale_y;-				cr.scale (scale, scale);+			} else {+				scale = scale_x; 			} 			-			if (!box.slice) {+			if (box.preserve_aspect_ratio) { 				if ((box.alignment & ViewBox.XMID) > 0) {-					cr.translate (((box.width - width) * scale) / 2, 0);+					cr.translate ((width - box.width * scale) / 2, 0); 				} else if ((box.alignment & ViewBox.XMAX) > 0) {-					cr.translate ((box.width - width) * scale, 0);+					cr.translate ((width - box.width * scale), 0); 				}  				if ((box.alignment & ViewBox.YMID) > 0) {-					cr.translate (0, ((box.height - height) * scale) / 2);+					cr.translate ((height - box.height * scale) / 2, 0); 				} else if ((box.alignment & ViewBox.YMAX) > 0) {-					cr.translate (0, (box.height - height) * scale);+					cr.translate ((height - box.height * scale), 0); 				}-			} else {-				Layer layer = new Layer ();-				Rectangle rectangle = new Rectangle ();-				rectangle.width = box.width;-				rectangle.height = box.height;-				layer.add_object (rectangle);-				ClipPath clip = new ClipPath (layer);-				clip_path = clip; 			}++			if (!box.preserve_aspect_ratio) {+				cr.scale (scale_x, scale_y);+			} else if (scale_width) {+				scale = scale_y;+				cr.scale (scale, scale);+			} else {+				scale = scale_x;+				cr.scale (scale, scale);+			}	 		} 	} diff --git a/libsvgbird/SvgFile.vala b/libsvgbird/SvgFile.valaindex 5f7b752..cf9db0a 100644--- a/libsvgbird/SvgFile.vala+++ b/libsvgbird/SvgFile.vala@@ -81,7 +81,8 @@ public class SvgFile : GLib.Object { 	ViewBox? parse_view_box (string parameters, XmlElement tag) { 		string arguments = parameters.replace (",", " "); 		string aspect_ratio = "";-		bool slice = false;+		bool slice = true;+		bool preserve_aspect_ratio = true; 		 		foreach (Attribute attribute in tag.get_attributes ()) { 			if (attribute.get_name () == "preserveAspectRatio") {@@ -96,6 +97,8 @@ public class SvgFile : GLib.Object { 				if (preserveSettings.length >= 2) { 					slice = preserveSettings[1] == "slice"; 				}+				+				preserve_aspect_ratio = false; 			} 		} 		@@ -120,27 +123,27 @@ public class SvgFile : GLib.Object { 		 		if (aspect_ratio == "NONE") { 			alignment = ViewBox.NONE;-		} else if (aspect_ratio == "XMIN_YMIN") {+		} else if (aspect_ratio == "XMINYMIN") { 			alignment = ViewBox.XMIN_YMIN;-		} else if (aspect_ratio == "XMID_YMIN") {+		} else if (aspect_ratio == "XMINYMIN") { 			alignment = ViewBox.XMIN_YMIN;-		} else if (aspect_ratio == "XMAX_YMIN") {+		} else if (aspect_ratio == "XMAXYMIN") { 			alignment = ViewBox.XMIN_YMIN;-		} else if (aspect_ratio == "XMIN_YMID") {+		} else if (aspect_ratio == "XMINYMID") { 			alignment = ViewBox.XMIN_YMIN;-		} else if (aspect_ratio == "XMID_YMID") {+		} else if (aspect_ratio == "XMIDYMID") { 			alignment = ViewBox.XMIN_YMIN;-		} else if (aspect_ratio == "XMAX_YMID") {+		} else if (aspect_ratio == "XMAXYMID") { 			alignment = ViewBox.XMIN_YMIN;-		} else if (aspect_ratio == "XMIN_YMAX") {+		} else if (aspect_ratio == "XMINYMAX") { 			alignment = ViewBox.XMIN_YMIN;-		} else if (aspect_ratio == "XMID_YMAX") {+		} else if (aspect_ratio == "XMIDYMAX") { 			alignment = ViewBox.XMIN_YMIN;-		} else if (aspect_ratio == "XMAX_YMAX") {+		} else if (aspect_ratio == "XMAXYMAX") { 			alignment = ViewBox.XMIN_YMIN; 		} 		-		return new ViewBox (minx, miny, width, height, alignment, slice);+		return new ViewBox (minx, miny, width, height, alignment, slice, preserve_aspect_ratio); 	}  	private void parse_layer (Layer layer, SvgStyle parent_style, XmlElement tag) {		diff --git a/libsvgbird/ViewBox.vala b/libsvgbird/ViewBox.valaindex c7c9c95..a0f2e38 100644--- a/libsvgbird/ViewBox.vala+++ b/libsvgbird/ViewBox.vala@@ -47,8 +47,10 @@ public class ViewBox : GLib.Object { 	public uint alignment; 	public bool slice; +	public bool preserve_aspect_ratio;+ 	public ViewBox (double minx, double miny, double width, double height,-		uint alignment, bool slice) {+		uint alignment, bool slice, bool preserve_aspect_ratio) { 			 		this.minx = minx; 		this.miny = miny;@@ -57,6 +59,7 @@ public class ViewBox : GLib.Object { 		 		this.alignment = alignment; 		this.slice = slice;+		this.preserve_aspect_ratio = preserve_aspect_ratio; 	} 	 }

Contact

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