Clone in a terminal:
Commit 5f76b15
Johan Mattsson - 2015-03-19T10:10:29+00:00
5f76b1538700ca5b4d5781727f1cdf91e6a05cd6johan.mattsson.m@gmail.com

Fix the object selection bug

Changed files (3)

Diff

diff --git a/libbirdfont/ClickMap.vala b/libbirdfont/ClickMap.valaindex 1dcce99..9fb7da3 100644--- a/libbirdfont/ClickMap.vala+++ b/libbirdfont/ClickMap.vala@@ -1,5 +1,5 @@ /*-    Copyright (C) 2014 Johan Mattsson+    Copyright (C) 2014 2015 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 @@ -12,176 +12,60 @@     Lesser General Public License for more details. */ +using Cairo;+ namespace BirdFont { -// This is a refcounted container in order to avoid double free -// corruption in lambda functions. public class ClickMap : GLib.Object {-	char* map;+	ImageSurface map; 	int width;-	int margin;-	int map_size;-+	 	public ClickMap (int width) { 		this.width = width;-		margin = 10;-		map_size = width + margin;-		map = malloc (map_size * map_size);+		map = new Cairo.ImageSurface (Cairo.Format.ARGB32, width, width); 	}-	-	~ClickMap () {-		delete map;-	}-	-	public char get_value (int x, int y) { 		-		if (unlikely (!(0 <= x < map_size && 0 <= y < map_size))) {-			warning ("Array index out of bounds.");-			return '\0';-		}+	public bool get_value (int x, int y) {+		unowned uchar[] d = map.get_data ();+		bool transparent; 		-		return map[y * width + x];-	}-	-	public void set_value (int x, int y, char val) {-		if (unlikely (!(0 <= x < map_size && 0 <= y < map_size))) {+		if (unlikely (!(0 <= x < width && 0 <= y < width))) { 			warning ("Array index out of bounds.");-			return;+			return true; 		} 		-		map[y * width + x] = val;+		transparent = d[y * map.get_stride () + 4 * x + 3] == 0;+				+		return !transparent; 	}+		+	public void create_click_map (Path path) {+		double w, h;+		Context c;+				+		c = new Context (map);+		+		c.save ();+		+		c.set_source_rgba (0, 0, 0, 1);+		c.new_path (); -	private void map_outline (Path path) { 		path.all_of_path ((cx, cy, ct) => { 			int px = (int) (width * ((cx - path.xmin) / (path.xmax - path.xmin))); 			int py = (int) (width * ((cy - path.ymin) / (path.ymax - path.ymin))); 			-			set_value (px, py, '#');+			c.line_to (px, py); 			 			return true; 		}, 2 * width);-	}--	private void close_outline (Path path) {-		// first to last point in case the path is open-		Path.all_of (path.get_last_point (), path.get_first_point (), (cx, cy, ct) => {-			int px = (int) (width * ((cx - path.xmin) / (path.xmax - path.xmin)));-			int py = (int) (width * ((cy - path.ymin) / (path.ymax - path.ymin)));-			-			set_value (px, py, '#');-			-			return true;-		}, 2 * width);-	}-	-	public void create_click_map (Path path) {-		// Clear the map-		for (int i = 0; i < map_size; i++) {-			for (int j = 0; j < map_size; j++) {-				set_value (i, j, '\0');-			}-		} 		-		// Create outline-		map_outline (path);-		close_outline (path);+		c.close_path ();+		c.fill (); 		-		// Fill the map-		for (int j = 0; j < width; j++) {-			for (int k = 0; k < width; k++) {-				if (get_value (k, j) == '#') {-					-					k++;-					while (k < width && get_value (k, j) == '#') {-						k++;-					}--					while (k < width && get_value (k, j) == '\0') {-						set_value (k, j, 'o');-						k++;-					}--					k++;-					while (k < width && get_value (k, j) == '#') {-						k++;-					}-				}-			}-		}--		// Remove fill from the out side-		for (int k = 0; k < width; k++) {-			-			if (get_value (k, 0) == 'o') {-				set_value (k, 0, '\0'); -			}-			-			for (int l = width - 1; l >= 0; l--) {-				if (get_value (k, l) != '#') {-					if (get_value (k, l + 1) == '\0') {-						set_value (k, l, '\0');-					}-					-					if (get_value (k, l + 1) == 'o') {-						set_value (k, l, 'o');-					}-				}-			}-		} --		for (int k = 0; k < width; k++) {-			if (get_value (0, k) == 'o') {-				set_value (0, k,'\0'); -			}-			-			for (int l = width - 1; l >= 0; l--) {-				if (get_value (l, k) != '#') {-					if (get_value (l + 1, k) == '\0') {-						set_value (l, k, '\0');-					}-					-					if (get_value (l + 1, k) == 'o') {-						set_value (l, k, 'o');-					}-				}-			}-		}--		for (int k = width - 1; k > 0; k--) {-			if (get_value (k, 0) == 'o') {-				set_value (k, 0,'\0'); -			}-			-			for (int l = width - 1; l >= 0; l--) {-				if (get_value (k, l) != '#') {				-					if (get_value (k, l + 1) == 'o') {-						set_value (k, l, 'o');-					}-				}-			}-		}-	}-	-	public void print () {-		char c;-		for (int i = width - 1; i >= 0; i--) {-			for (int j = 0; j < width; j++) {-				c = get_value (j, i);-				if (c == '\0') {-					stdout.printf (" ");-				} else if (c == '#') {-					stdout.printf ("#");-				} else if (c == 'o') {-					stdout.printf ("o");-				} else if (c == 'X') {-					stdout.printf ("X");-				} else {-					stdout.printf ("?");-				}-			}-			-			stdout.printf ("\n");+		c.restore ();+		+		if (path.points.size > 1) {+			map.write_to_png ("/home/johan/map6.png"); 		} 	} }diff --git a/libbirdfont/Path.vala b/libbirdfont/Path.valaindex ed7e570..cb8b5ea 100644--- a/libbirdfont/Path.vala+++ b/libbirdfont/Path.vala@@ -736,7 +736,6 @@ public class Path { 	/** Variable precision */ 	public bool is_over_coordinate_var (double x, double y) { 		PathList pathlist;-		bool in_path = false; 		int width; 		ClickMap click_map; 		int px, py;@@ -774,11 +773,7 @@ public class Path { 		click_x = (int) (width * ((x - xmin) / (xmax - xmin))); 		click_y = (int) (width * ((y - ymin) / (ymax - ymin))); -		in_path = click_map.get_value (click_x, click_y) != '\0';-		-		click_map.set_value (click_x, click_y, 'X');-		-		return in_path;+		return click_map.get_value (click_x, click_y); 	} 	 	public bool is_over_boundry (double x, double y) {diff --git a/libbirdfont/TabContent.vala b/libbirdfont/TabContent.valaindex c149333..84590ec 100644--- a/libbirdfont/TabContent.vala+++ b/libbirdfont/TabContent.vala@@ -23,6 +23,7 @@ public class TabContent : GLib.Object { 	static LineTextArea text_input; 	static Button text_input_button; 	static bool text_input_visible = false;+	static TextListener text_callback;  	static const int TEXT_INPUT_HEIGHT = 51; @@ -319,6 +320,8 @@ public class TabContent : GLib.Object { 	} 	 	public static void show_text_input (TextListener tl) {+		text_callback = tl;+		 		text_input_label = new Text (tl.label); 		text_input = new LineTextArea (20 * MainWindow.units); 		text_input_button = new Button (tl.button_label);

Contact

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