Clone in a terminal:
Commit b3e39f4
Johan Mattsson - 2014-07-11T23:09:04+00:00
b3e39f460afd08b1357cfcfaa150bda6642f39ebjohan.mattsson.m@gmail.com

Resize background image around its center.

Changed files (3)

Diff

diff --git a/libbirdfont/BackgroundImage.vala b/libbirdfont/BackgroundImage.valaindex 23ae99a..46ee5aa 100644--- a/libbirdfont/BackgroundImage.vala+++ b/libbirdfont/BackgroundImage.vala@@ -70,8 +70,8 @@ public class BackgroundImage { 	} 	 	public double img_middle_y {-		get { return img_y + (size_margin * img_scale_y) / 2; }-		set { img_y = value - img_y + (size_margin * img_scale_y) / 2; }+		get { return img_y - (size_margin * img_scale_y) / 2; }+		set { img_y = value + (size_margin * img_scale_y) / 2; } 	} 				 	public BackgroundImage (string file_name) {@@ -114,10 +114,6 @@ public class BackgroundImage { 		img_y = coordinate_y; 	} 	-	public void load () {-		get_img ();-	}-			 	public ImageSurface get_img () { 		if (!path.has_suffix (".png")) { 			create_png ();@@ -477,21 +473,20 @@ public class BackgroundImage { 	}  	bool is_over_resize (double nx, double ny) {-		Glyph g = MainWindow.get_current_glyph (); 		double x, y, size; 		bool inx, iny;+	+		size = 12; -		size = 12 * g.view_zoom;--		x = img_offset_x - g.view_offset_x;-		y = img_offset_y - g.view_offset_y + (size_margin) * img_scale_y;-				-		y *= g.view_zoom;-		x *= g.view_zoom;-				+		x = img_middle_x - (img_scale_x * get_img ().get_width () / 2);+		y = img_middle_y - (img_scale_y * get_img ().get_height () / 2);+		+		x = Glyph.reverse_path_coordinate_x (x);+		y = Glyph.reverse_path_coordinate_y (y);+		 		inx = x - size <= nx <= x + size; 		iny = y - size <= ny <= y + size;-		+	 		return inx && iny; 	} @@ -522,12 +517,12 @@ public class BackgroundImage { 		 		cr.set_source_rgba (1, 0, 0.3, 1); -		x = img_offset_x - g.view_offset_x;-		y = img_offset_y - g.view_offset_y + (size_margin) * img_scale_y;-				-		y *= g.view_zoom;-		x *= g.view_zoom;-				+		x = img_middle_x - (img_scale_x * get_img ().get_width () / 2);+		y = img_middle_y - (img_scale_y * get_img ().get_height () / 2);++		x = Glyph.reverse_path_coordinate_x (x);+		y = Glyph.reverse_path_coordinate_y (y);+			 		draw_handle_triangle (x, y, cr, g, 6); 		 		cr.restore ();diff --git a/libbirdfont/BackgroundTool.vala b/libbirdfont/BackgroundTool.valaindex 05ede58..f16e94a 100644--- a/libbirdfont/BackgroundTool.vala+++ b/libbirdfont/BackgroundTool.vala@@ -12,6 +12,8 @@     Lesser General Public License for more details. */ +using Cairo;+ namespace BirdFont {  public class BackgroundTool : Tool {@@ -33,6 +35,7 @@ public class BackgroundTool : Tool { 	bool move_bg; 	 	static BackgroundImage imported_background;+	static ImageSurface imported_surface; 	 	public BackgroundTool (string name) { 		base (name, t_("Move, resize and rotate background image"));@@ -110,8 +113,7 @@ public class BackgroundTool : Tool { 		Glyph g = MainWindow.get_current_glyph (); 		BackgroundImage? background_image = g.get_background_image (); 		BackgroundImage bg = (!) background_image;-		-		double xscale, yscale, dx, dy;+		double xscale, yscale, dx, dy, xc, yc;  		if (background_image == null) { 			return;@@ -135,9 +137,14 @@ public class BackgroundTool : Tool { 		if (bg.selected_handle == 1) { 			xscale = img_scale_x * ((img_width - dx) / img_width);	 			yscale = xscale;+			+			xc = bg.img_middle_x;+			yc = bg.img_middle_y;  			bg.set_img_scale (xscale, yscale);-			bg.set_img_offset (this.img_offset_x + dx, this.img_offset_y);+			+			bg.img_middle_x = xc;+			bg.img_middle_y = yc; 		}  		 		if (move_bg && bg.selected_handle <= 0) {@@ -149,7 +156,7 @@ public class BackgroundTool : Tool {  	public static void load_background_image () { 		// generate png file if needed and load the image with cairo-		imported_background.load ();+		imported_surface = imported_background.get_img (); 		 		IdleSource idle = new IdleSource (); 		idle.set_callback (() => {@@ -159,9 +166,8 @@ public class BackgroundTool : Tool { 			g.set_background_image (imported_background); 			imported_background.center_in_glyph ();			 			-			tb.select_tab_name (g.get_name ());- 			Toolbox.select_tool_by_name ("zoom_background_image");+			tb.select_tab_name (g.get_name ()); 			Toolbox.select_tool_by_name ("cut_background"); 			 			GlyphCanvas.redraw ();diff --git a/libbirdfont/DrawingTools.vala b/libbirdfont/DrawingTools.valaindex 5e00893..b1dd742 100644--- a/libbirdfont/DrawingTools.vala+++ b/libbirdfont/DrawingTools.vala@@ -512,9 +512,18 @@ public class DrawingTools : ToolCollection  { 			Glyph g = MainWindow.get_current_glyph (); 			BackgroundImage? img = g.get_background_image (); 			double s = sb.get_value ();+			BackgroundImage i;+			double xc, yc; 			 			if (img != null) {-				((!)img).set_img_scale (s, s);+				i = (!) img;+				xc = i.img_middle_x;+				yc = i.img_middle_y;++				i.set_img_scale (s, s);+				+				i.img_middle_x = xc;+				i.img_middle_y = yc; 			} 			 			GlyphCanvas.redraw ();

Contact

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