Clone in a terminal:
Commit f75b2b6
Johan Mattsson - 2016-06-24T13:51:47+00:00
f75b2b6cf33e041aa358c51d4346e0678f3a2e76johan.mattsson.m@gmail.com

Fix bounding box

Changed files (8)

Diff

diff --git a/libbirdfont/EmbeddedSvg.vala b/libbirdfont/EmbeddedSvg.valaindex 69a2e0a..a10dbaf 100644--- a/libbirdfont/EmbeddedSvg.vala+++ b/libbirdfont/EmbeddedSvg.vala@@ -30,14 +30,14 @@ public class EmbeddedSvg : SvgBird.Object { 		this.drawing = drawing; 	} -	public override bool update_boundaries (Matrix view_matrix) {-		drawing.update_boundaries (view_matrix);-+	public override bool update_boundaries (Context context) {+		drawing.update_boundaries (context);+		 		left = x + drawing.left; 		right = x + drawing.right; 		top = -y + drawing.top; 		bottom = -y + drawing.bottom;-		+	 		return true; 	} @@ -54,6 +54,8 @@ public class EmbeddedSvg : SvgBird.Object { 	} 	 	public override void draw_outline (Context cr) {+		cr.translate (x, -y);+		drawing.draw_outline (cr); 	} 	 	public override void move (double dx, double dy) {diff --git a/libbirdfont/Glyph.vala b/libbirdfont/Glyph.valaindex 40b3ee7..b51a344 100644--- a/libbirdfont/Glyph.vala+++ b/libbirdfont/Glyph.vala@@ -348,7 +348,7 @@ public class Glyph : FontDisplay { 			return false; 		} -		layers.update_boundaries (Matrix.identity ());+		layers.update_boundaries_for_object ();  		x1 = layers.xmin; 		x2 = layers.xmax;diff --git a/libbirdfont/PathObject.vala b/libbirdfont/PathObject.valaindex 9369877..70d8b0f 100644--- a/libbirdfont/PathObject.vala+++ b/libbirdfont/PathObject.vala@@ -88,6 +88,13 @@ public class PathObject : SvgBird.Object { 	 	public override void draw_outline (Context cr) { 		// drawing is handled in Glyph.draw_bird_font_paths+		+		if (path.stroke > 0) {+			//draw_path_list (path.get_completed_stroke (), cr);+			draw_path_list (path.get_stroke_fast (), cr);+		} else {+			path.draw_path (cr);+		} 	} 	 	public void draw_path (Context cr) {@@ -113,24 +120,12 @@ public class PathObject : SvgBird.Object { 		return path; 	} -	public override bool update_boundaries (Matrix matrix) {+	public override bool update_boundaries (Context cr) { 		if (path.points.size < 2) { 			return false; 		} 		-		xmin = Glyph.CANVAS_MAX;-		xmax = Glyph.CANVAS_MIN;-		ymin = Glyph.CANVAS_MAX;-		ymax = Glyph.CANVAS_MIN;--		path.update_region_boundaries ();-		-		xmin = path.xmin;-		xmax = path.xmax;-		ymin = path.ymin;-		ymax = path.ymax;-		-		return true;+		return base.update_boundaries (cr); 	}  	public override void rotate (double theta, double xc, double yc) {diff --git a/libbirdfont/ResizeTool.vala b/libbirdfont/ResizeTool.valaindex 3640854..c7b99b8 100644--- a/libbirdfont/ResizeTool.vala+++ b/libbirdfont/ResizeTool.vala@@ -555,7 +555,7 @@ public class ResizeTool : Tool { 				Path p = ((PathObject) path).get_path (); 				SvgParser.apply_matrix (p, 1, 0, s, 1, 0, 0); 				p.skew = skew;-				path.update_boundaries (Matrix.identity ());+				path.update_boundaries_for_object (); 			} 		} 		diff --git a/libbirdfont/SvgParser.vala b/libbirdfont/SvgParser.valaindex 851e429..896b4bf 100644--- a/libbirdfont/SvgParser.vala+++ b/libbirdfont/SvgParser.vala@@ -155,7 +155,7 @@ public class SvgParser { 			PathObject path = new PathObject.for_path (p); 			glyph.add_object (path); 			glyph.add_active_object (null, path); // FIXME: groups-			path.update_boundaries (Cairo.Matrix.identity ());+			path.update_boundaries_for_object (); 		} 		 		glyph.close_path ();	diff --git a/libsvgbird/Layer.vala b/libsvgbird/Layer.valaindex 38ce21e..cbda126 100644--- a/libsvgbird/Layer.vala+++ b/libsvgbird/Layer.vala@@ -31,26 +31,46 @@ public class Layer : Object { 		objects = new ObjectGroup (); 		transforms = new SvgTransforms (); 	}-	-	public override bool update_boundaries (Matrix view_matrix) {+++	public override bool update_boundaries (Context cr) { 		if (objects.size == 0) { 			return false; 		}-		+ 		top = CANVAS_MAX; 		bottom = CANVAS_MIN; 		left = CANVAS_MAX; 		right = CANVAS_MIN; -		Matrix layer_matrix = transforms.get_matrix ();-		layer_matrix.multiply (layer_matrix, view_matrix);--		base.update_boundaries (view_matrix);+		cr.save ();+		apply_transform (cr);  		foreach (Object object in objects) {-			object.update_boundaries (layer_matrix);+			bool has_size = false;++			cr.save ();++			if (object is Layer) {+				Layer sublayer = (Layer) object;+				has_size = sublayer.update_boundaries (cr);+			} else {+				object.apply_transform (cr);+				has_size = object.update_boundaries (cr);+			}+			+			if (has_size) {+				left = fmin (left, object.left);+				right = fmax (right, object.right);+				top = fmin (top, object.top);+				bottom = fmax (bottom, object.bottom);+			}++			cr.restore (); 		} 		+		cr.restore ();+		 		return boundaries_width != 0; 	} 	@@ -73,7 +93,6 @@ public class Layer : Object {  		foreach (Object object in objects) { 			cr.save ();-			object.apply_transform (cr); 						 			if (object.clip_path != null) { 				ClipPath clipping = (!) object.clip_path;@@ -89,6 +108,7 @@ public class Layer : Object { 					sublayer.draw_outline (cr); 				} 			} else {+				object.apply_transform (cr); 				object.draw_outline (cr); 				 				if (paint) {@@ -107,12 +127,12 @@ public class Layer : Object { 		 	public void add_layer (Layer layer) { 		objects.add (layer);-		update_boundaries (Matrix.identity ());+		update_boundaries_for_object (); 	} 	 	public void add_object (Object object) { 		objects.add (object);-		update_boundaries (Matrix.identity ());+		update_boundaries_for_object (); 	}  	public void remove (Object o) {@@ -125,7 +145,7 @@ public class Layer : Object { 			} 		} 		-		update_boundaries (Matrix.identity ());+		update_boundaries_for_object (); 	} 	 	public void remove_layer (Layer layer) {@@ -138,7 +158,7 @@ public class Layer : Object { 			} 		} 		-		update_boundaries (Matrix.identity ());+		update_boundaries_for_object (); 	} 	 	public static void copy_layer (Layer from, Layer to) {diff --git a/libsvgbird/Object.vala b/libsvgbird/Object.valaindex b61fb16..ad40b01 100644--- a/libsvgbird/Object.vala+++ b/libsvgbird/Object.vala@@ -249,29 +249,39 @@ public abstract class Object : GLib.Object { 	}  	/** @return true if the object has an area. */-	public virtual bool update_boundaries (Matrix view_matrix) {-		ImageSurface surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, 1, 1);-		Context context = new Cairo.Context (surface);-		-		context.set_matrix (view_matrix);-		apply_transform (context);+	public virtual bool update_boundaries (Context context) {+		double x0, y0, x1, y1;++		context.set_line_width (style.stroke_width); 		draw_outline (context);++		context.save (); 		-		double x0, y0, x1, y1;+		Matrix m = Matrix.identity ();+		context.set_matrix (m); 		-		if (style.stroke_width == 0) {-			context.path_extents (out x0, out y0, out x1, out y1);-		} else {+		if (style.stroke_width > 0) {	 			context.stroke_extents (out x0, out y0, out x1, out y1);+		} else {+			context.path_extents (out x0, out y0, out x1, out y1); 		} 		+		context.restore ();+		 		left = x0; 		top = y0; 		right = x1; 		bottom = y1;-		+				 		return boundaries_width != 0; 	}++	public virtual bool update_boundaries_for_object () {+		ImageSurface surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, 1, 1);+		Context context = new Cairo.Context (surface);+		return update_boundaries (context);+	}+ }  }diff --git a/libsvgbird/SvgDrawing.vala b/libsvgbird/SvgDrawing.valaindex 5294bcd..7f40284 100644--- a/libsvgbird/SvgDrawing.vala+++ b/libsvgbird/SvgDrawing.vala@@ -23,9 +23,6 @@ public class SvgDrawing : Object { 	public Layer root_layer = new Layer (); 	public Defs defs = new Defs (); -	public double x = 0;-	public double y = 0;-	 	public double width { 		get {			 			return svg_width;@@ -49,8 +46,9 @@ public class SvgDrawing : Object { 	public double svg_width = 0; 	public double svg_height = 0; -	public override bool update_boundaries (Matrix view_matrix) {-		root_layer.update_boundaries (view_matrix);+	public override bool update_boundaries (Context cr) {+		apply_transform (cr);+		root_layer.update_boundaries (cr); 		 		left = root_layer.left; 		right = root_layer.right;@@ -61,8 +59,7 @@ public class SvgDrawing : Object { 	}  	public override bool is_over (double x, double y) {-		return (this.x <= x <= this.x + width) -			&& (this.y <= y <= this.y + height);+		return false; 	}  	public void draw (Context cr) {@@ -85,9 +82,6 @@ public class SvgDrawing : Object { 	} 	 	public override void move (double dx, double dy) {-		x += dx;-		y += dy;-		move_bounding_box (dx, dy); 	} 	 	public override void rotate (double theta, double xc, double yc) {@@ -101,7 +95,7 @@ public class SvgDrawing : Object { 	}  	public override string to_string () {-		return @"SvgDrawing x: $x, y: $y, width: $width, height: $height";+		return @"SvgDrawing width: $width, height: $height"; 	} } 

Contact

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