Clone in a terminal:
Commit 24be6eb
Johan Mattsson - 2016-10-07T09:50:38+00:00
24be6eb5628ad0f5cf439068c9a8f1d13d2941d5johan.mattsson.m@gmail.com

Use libsvgbird for parsing path transformations

Changed files (5)

Diff

diff --git a/libbirdfont/SvgParser.vala b/libbirdfont/SvgParser.valaindex d364735..15611ef 100644--- a/libbirdfont/SvgParser.vala+++ b/libbirdfont/SvgParser.vala@@ -15,6 +15,7 @@ using B; using Math; using SvgBird;+using Cairo;  namespace BirdFont { @@ -276,7 +277,9 @@ public class SvgParser { 			Cairo.Matrix view_box_matrix = view_box.get_matrix (width, height); 			view_box_matrix.multiply (view_box_matrix, matrix); 			SvgTransform t = new SvgTransform.for_matrix (view_box_matrix);-			transform (t.get_xml (), pl);+			SvgTransforms transforms = new SvgTransforms ();+			transforms.add (t);+			transform_paths (transforms, paths); 		} 		 		return paths;@@ -343,64 +346,70 @@ public class SvgParser { 				parse_line (t, pl); 			} 		}--		foreach (Attribute attr in tag.get_attributes ()) {	-			if (attr.get_name () == "transform") {-				transform (attr.get_content (), pl);-			}-		}-	}-	-	private void transform (string transform_functions, Layer layer) {-		PathList path_list = new PathList (); 		-		foreach (SvgBird.Object o in layer.objects) {-			if (o is PathObject) {-				path_list.add (((PathObject) o).get_path ());+		PathList paths = new PathList ();+		+		foreach (SvgBird.Object object in pl.objects) {+			if (object is Path) {+				paths.add ((Path) object); 			} 		} 		-		transform_paths (transform_functions, path_list);-		transform_subgroups (transform_functions, layer);+		SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ());+		transform_paths (transforms, paths); 	} 	-	private void transform_subgroups (string transform_functions, Layer layer) {-		foreach (Layer subgroup in layer.get_sublayers ()) {-			transform (transform_functions, subgroup);+	private void transform_paths (SvgTransforms transforms, PathList pl) {+		foreach (Path p in pl.paths) {+			transform_path (transforms, p); 		} 	}-	-	private void transform_paths (string transform_functions, PathList pl) {-		string data = transform_functions.dup ();-		string[] functions;-				-		// use only a single space as separator-		while (data.index_of ("  ") > -1) {-			data = data.replace ("  ", " ");-		}-		-		return_if_fail (data.index_of (")") > -1);++	private void transform_path (SvgTransforms transforms, Path path) {+		Font font = BirdFont.get_current_font ();+		Glyph glyph = MainWindow.get_current_glyph (); 		-		 // add separator-		data = data.replace (") ", "|");-		data = data.replace (")", "|"); -		functions = data.split ("|");+		foreach (EditPoint ep in path.points) {+			ep.tie_handles = false;+			ep.reflective_point = false;+		} 		-		for (int i = functions.length - 1; i >= 0; i--) {-			if (functions[i].has_prefix ("translate")) {-				translate (functions[i], pl);-			}-			-			if (functions[i].has_prefix ("scale")) {-				scale (functions[i], pl);-			}+		Matrix matrix = transforms.get_matrix (); -			if (functions[i].has_prefix ("matrix")) {-				matrix (functions[i], pl);-			}+		foreach (EditPoint ep in path.points) {+			double x, y;+			+			x = ep.independent_x - glyph.left_limit;+			y = font.top_limit - ep.independent_y;+			+			matrix.transform_point (ref x, ref y);+			+			ep.independent_x = glyph.left_limit + x;+			ep.independent_y = font.top_limit - y; 			-			// TODO: rotate etc.+			x = ep.get_right_handle ().x - glyph.left_limit;+			y = font.top_limit - ep.get_right_handle ().y;+			+			matrix.transform_point (ref x, ref y);+			+			ep.get_right_handle ().x = glyph.left_limit + x;+			ep.get_right_handle ().y = font.top_limit - y;+			+			x = ep.get_left_handle ().x - glyph.left_limit;+			y = font.top_limit - ep.get_left_handle ().y;+			+			matrix.transform_point (ref x, ref y);+			+			ep.get_left_handle ().x = glyph.left_limit + x;+			ep.get_left_handle ().y = font.top_limit - y; 		}+		+		double stroke_x = path.stroke;+		double stroke_y = path.stroke;+		+		matrix.transform_distance (ref stroke_x, ref stroke_y);+		+		path.stroke = stroke_x; 	}  	/** @param path a path in the cartesian coordinate system@@ -595,11 +604,8 @@ public class SvgParser {  		npl.add (p); 		-		foreach (Attribute attr in tag.get_attributes ()) {-			if (attr.get_name () == "transform") {-				transform_paths (attr.get_content (), npl);-			}-		}+		SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ());+		transform_paths (transforms, npl); 		 		npl.apply_style (style); 		append_paths (pl, npl);@@ -663,11 +669,8 @@ public class SvgParser {  		npl.add (p); 		-		foreach (Attribute attr in tag.get_attributes ()) {-			if (attr.get_name () == "transform") {-				transform_paths (attr.get_content (), npl);-			}-		}+		SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ());+		transform_paths (transforms, npl); 		 		npl.apply_style (style); 		append_paths (pl, npl);@@ -740,11 +743,8 @@ public class SvgParser { 		 		npl.add (p); 		-		foreach (Attribute attr in tag.get_attributes ()) {-			if (attr.get_name () == "transform") {-				transform_paths (attr.get_content (), npl);-			}-		}+		SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ());+		transform_paths (transforms, npl); 		 		npl.apply_style (style); 		append_paths (pl, npl);@@ -841,11 +841,8 @@ public class SvgParser { 		npl.add (p); 		 		// FIXME: right layer for other transforms-		foreach (Attribute attr in tag.get_attributes ()) {-			if (attr.get_name () == "transform") {-				transform_paths (attr.get_content (), npl);-			}-		}+		SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ());+		transform_paths (transforms, npl); 		 		npl.apply_style (style); 		append_paths (layer, npl);		@@ -894,11 +891,8 @@ public class SvgParser { 		path_list.add (p); 		path_list.apply_style (style); 		-		foreach (Attribute attr in tag.get_attributes ()) {-			if (attr.get_name () == "transform") {-				transform_paths (attr.get_content (), path_list);-			}-		}+		SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ());+		transform_paths (transforms, path_list); 		 		return path_list; 	}@@ -974,11 +968,8 @@ public class SvgParser { 			} 		} 		-		foreach (Attribute attr in tag.get_attributes ()) {-			if (attr.get_name () == "transform") {-				transform_paths (attr.get_content (), path_list);-			}-		}+		SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ());+		transform_paths (transforms, path_list); 	}  	public static void create_lines_for_segment (Path path, EditPoint start, EditPoint end, double tolerance) {diff --git a/libsvgbird/Doubles.vala b/libsvgbird/Doubles.valaindex 9e44a8b..56b7444 100644--- a/libsvgbird/Doubles.vala+++ b/libsvgbird/Doubles.vala@@ -117,7 +117,7 @@ public class Doubles : GLib.Object { 	 	public void remove (int offset, int length) { 		if (unlikely (offset < 0 || offset + length > size)) {-			warning ("Invalid offset.");+			warning (@"Invalid offset: $offset, length: $length, size: $size"); 			return; 		} 		diff --git a/libsvgbird/Object.vala b/libsvgbird/Object.valaindex 7da1643..2cef02f 100644--- a/libsvgbird/Object.vala+++ b/libsvgbird/Object.vala@@ -145,8 +145,6 @@ public abstract class Object : GLib.Object { 	public abstract bool is_empty (); 	 	public virtual void move (double dx, double dy) {-		Gradient g;- 		left += dx; 		right += dx; 		top += dy;diff --git a/libsvgbird/Points.vala b/libsvgbird/Points.valaindex 4f5f4e1..b4307b6 100644--- a/libsvgbird/Points.vala+++ b/libsvgbird/Points.vala@@ -60,22 +60,18 @@ public class Points : GLib.Object { 			next_handle_x, next_handle_y, x, y); 	} +	public int add_line (double x, double y) {+		return insert_line (size, x, y);+	}+ 	public int insert_cubic (int position, double handle_x, double handle_y, 		double next_handle_x, double next_handle_y, 		double x, double y) { 		-		int index = position;+		int index; 		 		if (size == 0) {-			index = 0;-			insert_type (index, POINT_LINE);-			insert (index + 1, x);-			insert (index + 2, y);-			insert (index + 3, 0);-			insert (index + 4, 0);-			insert (index + 5, 0);-			insert (index + 6, 0);-			insert (index + 7, 0);+			insert_line (0, x, y); 		} 		 		index = position;@@ -90,6 +86,21 @@ public class Points : GLib.Object {  		return index; 	}++	public int insert_line (int position, double x, double y) {+		int index = position;+		+		insert_type (index, POINT_LINE);+		insert (index + 1, x);+		insert (index + 2, y);+		insert (index + 3, 0);+		insert (index + 4, 0);+		insert (index + 5, 0);+		insert (index + 6, 0);+		insert (index + 7, 0);++		return index;+	} 	 	public Points copy () { 		Points p = new Points ();diff --git a/libsvgbird/SvgFile.vala b/libsvgbird/SvgFile.valaindex 8b378e2..0d49c2b 100644--- a/libsvgbird/SvgFile.vala+++ b/libsvgbird/SvgFile.vala@@ -828,7 +828,7 @@ public class SvgFile : GLib.Object { 		return !hidden; 	} -	private SvgTransforms get_transform (Attributes attributes) {+	public static SvgTransforms get_transform (Attributes attributes) { 		foreach (Attribute attr in attributes) { 			if (attr.get_name () == "transform") { 				return parse_transform (attr.get_content ());

Contact

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