Commit 3a4cb00
crop background image
Changed files (4)
modifiedsrc/CutBackgroundTool.valamodifiedsrc/Glyph.valamodifiedsrc/GlyphBackgroundImage.valamodifiedsrc/ZoomTool.vala
Diff
@@ -44,13 +44,7 @@ class CutBackgroundTool : Tool { deselect_action.connect ((self) => { }); - press_action.connect((self, b, x, y) => {- bool selection = MainWindow.get_current_display () is CutBackgroundCanvas;-- if (!selection) {- return;- }- + press_action.connect((self, b, x, y) => { if (!is_over_rectangle (x, y)) { x1 = x; y1 = y;@@ -58,13 +52,11 @@ class CutBackgroundTool : Tool { cut_background_is_moving = true; cut_background_is_visible = true; } else {- do_cut (); cut_background_is_visible = false; cut_background_is_moving = false; }- }); release_action.connect((self, b, x, y) => {@@ -78,9 +70,9 @@ class CutBackgroundTool : Tool { if (cut_background_is_moving) { x2 = x; y2 = y;+ + MainWindow.get_glyph_canvas ().redraw (); }- - MainWindow.get_glyph_canvas ().redraw (); }); draw_action.connect ((self, cr, glyph) => {@@ -94,6 +86,20 @@ class CutBackgroundTool : Tool { } }); + new_image.connect ((file) => {+ Tool zoom_background;+ Glyph glyph = MainWindow.get_current_glyph ();+ TabBar tb = MainWindow.get_tab_bar ();+ + glyph.set_background_image (new GlyphBackgroundImage (file));+ tb.select_tab_name (glyph.get_name ());+ + glyph.set_background_visible (true);+ + zoom_background = MainWindow.get_tool ("zoom_background_image");+ zoom_background.select_action (zoom_background);+ });+ } public void set_destination_file_name (string fn) {@@ -125,49 +131,68 @@ class CutBackgroundTool : Tool { Surface sr; Context cr; + double tx, ty, vx, vy; if (b == null) { return; } - img = bg.get_img ();-- w = (int) (get_width () / g.view_zoom);- h = (int) (get_height () / g.view_zoom);-- sr = new Surface.similar (img, img.get_content (), w, h);- cr = new Context (sr);+ // Add margin+ Surface sg = new Surface.similar (bg.get_img (), bg.get_img ().get_content (), bg.size_margin, bg.size_margin);+ Context cg = new Context (sg); - // fine for 1:1 but not for scaled image- x = bg.img_offset_x;- y = bg.img_offset_y;+ int wc = (int) ((bg.size_margin - bg.get_img ().get_width ()) / 2);+ int hc = (int) ((bg.size_margin - bg.get_img ().get_height ()) / 2);++ cg.set_source_rgba (1, 1, 1, 1);+ cg.rectangle (0, 0, bg.size_margin, bg.size_margin);+ cg.fill (); - x -= fmin (x1, x2);- y -= fmin (y1, y2);+ cg.set_source_surface (bg.get_img (), wc, hc);+ cg.paint (); - x *= Glyph.ivz ();- y *= Glyph.ivz ();+ // find start+ tx = bg.img_offset_x - g.view_offset_x;+ ty = bg.img_offset_y - g.view_offset_y;+ + ty *= g.view_zoom;+ tx *= g.view_zoom; - x += Glyph.xc ();- y += Glyph.yc ();+ vx = g.path_coordinate_x (tx) - g.path_coordinate_x (x1); + vy = g.path_coordinate_y (ty) - g.path_coordinate_y (y1);+ + x = (int) (vx / bg.img_scale_x);+ y = (int) (-vy / bg.img_scale_y); - x -= g.view_offset_x;- y -= g.view_offset_y;+ // do the cut+ img = bg.get_img (); - x -= bg.img_offset_x * (Glyph.ivz () - 1);- y -= bg.img_offset_y * (Glyph.ivz () - 1);+ w = (int) (get_width () / g.view_zoom);+ h = (int) (get_height () / g.view_zoom); + sr = new Surface.similar (sg, img.get_content (), w, h);+ cr = new Context (sr);+ + cr.scale (bg.img_scale_x, bg.img_scale_y);+ + /* cr.save (); cr.set_source_rgba (1, 1, 1, 1); cr.rectangle (0, 0, w, h); cr.fill (); cr.restore ();+ */ - cr.set_source_surface (img, x, y);+ cr.set_source_surface (sg, x, y); cr.paint (); save_img (sr, g); - cr.restore (); + cr.restore ();+ + // + + bg.img_offset_x = x + g.view_offset_x;+ bg.img_offset_y = y + g.view_offset_y; } void save_img (Surface sr, Glyph g) {@@ -197,6 +222,7 @@ class CutBackgroundTool : Tool { newbg = new GlyphBackgroundImage (fn); g.set_background_image (newbg);+ newbg.reset_scale (g); new_image (fn);@@ -1400,24 +1400,23 @@ class Glyph : FontDisplay { ((!)background_image).draw (cr, allocation, view_offset_x, view_offset_y, view_zoom); cmp.restore (); }- - cmp.save ();- cmp.scale (view_zoom, view_zoom);- cmp.translate (-view_offset_x, -view_offset_y);- draw_path (cmp);- cmp.restore ();- + cmp.save (); tool = MainWindow.get_toolbox ().get_current_tool (); tool.draw_action (tool, cmp, this); cmp.restore ();- - // detta kanske är en del av verktyget+ if (zoom_area_is_visible) { cmp.save (); draw_zoom_area (cmp); cmp.restore (); }+ + cmp.save ();+ cmp.scale (view_zoom, view_zoom);+ cmp.translate (-view_offset_x, -view_offset_y);+ draw_path (cmp); // This does mess up scale for some unknown reason + cmp.restore (); cr.save (); cr.set_source_surface (ps, 0, 0);@@ -28,9 +28,8 @@ class GlyphBackgroundImage { public double img_scale_x = 1; public double img_scale_y = 1; public double img_rotation = 0;- public int width_margin = 0;+ public int size_margin = 0; - public int active_handle = -1; public int selected_handle = -1; @@ -52,6 +51,7 @@ class GlyphBackgroundImage { public GlyphBackgroundImage (string fn = "") { path = fn;+ size_margin = (int) (Math.sqrt (Math.pow (get_img ().get_height (), 2) + Math.pow (get_img ().get_width (), 2)) + 0.5); } public ImageSurface get_img () {@@ -221,6 +221,8 @@ class GlyphBackgroundImage { Surface st; Context ct; + int wc, hc;+ if (unlikely (get_img ().status () != Cairo.Status.SUCCESS)) { stderr.printf (@"Background image is invalid. (\"$path\")\n"); MainWindow.get_current_glyph ().set_background_visible (false);@@ -228,16 +230,14 @@ class GlyphBackgroundImage { } // add margin- width_margin = (int) (Math.sqrt (Math.pow (get_img ().get_height (), 2) + Math.pow (get_img ().get_width (), 2)) + 0.5);-- sg = new Surface.similar (get_img (), get_img ().get_content (), width_margin, width_margin);+ sg = new Surface.similar (get_img (), get_img ().get_content (), size_margin, size_margin); cg = new Context (sg); - int wc = (int) ((width_margin - get_img ().get_width ()) / 2);- int hc = (int) ((width_margin - get_img ().get_height ()) / 2);+ wc = (int) ((size_margin - get_img ().get_width ()) / 2);+ hc = (int) ((size_margin - get_img ().get_height ()) / 2); - cg.set_source_rgba (0, 0.5, 0, 1);- cg.rectangle (0, 0, width_margin, width_margin);+ cg.set_source_rgba (1, 1, 1, 1);+ cg.rectangle (0, 0, size_margin, size_margin); cg.fill (); cg.set_source_surface (get_img (), wc, hc);@@ -254,18 +254,22 @@ class GlyphBackgroundImage { w = (int) iw; h = (int) ih; - oy = width_margin;- ox = width_margin;+ oy = size_margin;+ ox = size_margin; // rotate image s = new Surface.similar (sg, sg.get_content (), (int) (ox), (int) (oy)); c = new Context (s); c.save ();- - c.translate (width_margin * 0.5, width_margin * 0.5);++ c.set_source_rgba (1, 1, 1, 1);+ c.rectangle (0, 0, size_margin, size_margin);+ c.fill ();+ + c.translate (size_margin * 0.5, size_margin * 0.5); c.rotate (img_rotation);- c.translate (-width_margin * 0.5, -width_margin * 0.5);+ c.translate (-size_margin * 0.5, -size_margin * 0.5); c.set_source_surface (cg.get_target (), 0, 0); c.paint (); @@ -276,10 +280,11 @@ class GlyphBackgroundImage { ct = new Context (st); ct.save (); - double xmip = img_offset_x - Glyph.path_coordinate_x (width_margin / 2.0) + view_offset_x;- double ymip = img_offset_y - Glyph.path_coordinate_y (width_margin / 2.0) - view_offset_y;+ ct.set_source_rgba (1, 1, 1, 1);+ ct.rectangle (0, 0, allocation.width, allocation.height);+ ct.fill (); - // scale+ // scale both canvas and image at the same time; rather tricky. scale_x = view_zoom * img_scale_x; scale_y = view_zoom * img_scale_y; @@ -317,8 +322,8 @@ class GlyphBackgroundImage { double a, b; Glyph g = MainWindow.get_current_glyph (); - x = img_offset_x - g.view_offset_x + (width_margin / 2) * img_scale_x;- y = img_offset_y - g.view_offset_y + (width_margin / 2) * img_scale_y;+ x = img_offset_x - g.view_offset_x + (size_margin / 2) * img_scale_x;+ y = img_offset_y - g.view_offset_y + (size_margin / 2) * img_scale_y; y *= g.view_zoom; x *= g.view_zoom;@@ -345,7 +350,7 @@ class GlyphBackgroundImage { size = 12 * g.view_zoom; x = img_offset_x - g.view_offset_x;- y = img_offset_y - g.view_offset_y + (width_margin) * img_scale_y;+ y = img_offset_y - g.view_offset_y + (size_margin) * img_scale_y; y *= g.view_zoom; x *= g.view_zoom;@@ -381,13 +386,19 @@ class GlyphBackgroundImage { double x, y; cr.save (); - cr.scale (g.view_zoom, g.view_zoom);+ // cr.scale (g.view_zoom, g.view_zoom);+ 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 + (width_margin) * img_scale_y;+ y = img_offset_y - g.view_offset_y + (size_margin) * img_scale_y;+ + y *= g.view_zoom;+ x *= g.view_zoom;+ + draw_handle_triangle (x, y, cr, g, 6); - draw_handle_triangle (x - 1, y - 1, cr, g, 6);+ draw_handle_triangle (0, 0, cr, g, 6); cr.restore (); }@@ -405,8 +416,8 @@ class GlyphBackgroundImage { else if (active_handle == 2) cr.set_source_rgba (0, 0, 0.3, 1); else cr.set_source_rgba (0.7, 0.7, 0.8, 1); - x = img_offset_x - g.view_offset_x + (width_margin / 2) * img_scale_x;- y = img_offset_y - g.view_offset_y + (width_margin / 2) * img_scale_y;+ x = img_offset_x - g.view_offset_x + (size_margin / 2) * img_scale_x;+ y = img_offset_y - g.view_offset_y + (size_margin / 2) * img_scale_y; cr.rectangle (x, y, 5 * ivz, 5 * ivz); cr.fill ();@@ -441,7 +452,7 @@ class GlyphBackgroundImage { else if (active_handle == 1) cr.set_source_rgba (0, 0, 0.3, 1); else cr.set_source_rgba (0.7, 0.7, 0.8, 1); - size = (8 * ivz) * s;+ size = (8) * s; cr.scale (1, 1); cr.new_path ();@@ -81,10 +81,10 @@ class ZoomTool : Tool { bg = (!) g.get_background_image (); - x = (int) (Glyph.xc () + bg.img_offset_x);- y = (int) (Glyph.yc () + bg.img_offset_y);+ x = (int) (bg.img_offset_x);+ y = (int) (bg.img_offset_y); - g.set_zoom_area (x, y, (int) (x + bg.get_current_width ()), (int) (y + bg.get_current_height ()));+ g.set_zoom_area (x, y, (int) (x + bg.size_margin * bg.img_scale_x), (int) (y + bg.size_margin * bg.img_scale_y)); g.set_zoom_from_area (); }