The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

MoveTool.vala in libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/MoveTool.vala.
Move the SVG parser and the SVG rendering code to libsvgbird
1 /* 2 Copyright (C) 2012 2013 2015 2016 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Math; 16 using Cairo; 17 18 namespace BirdFont { 19 20 public class MoveTool : Tool { 21 22 static bool move_path = false; 23 static bool moved = false; 24 static double last_x = 0; 25 static double last_y = 0; 26 27 static double selection_x = 0; 28 static double selection_y = 0; 29 static bool group_selection= false; 30 31 public static static double selection_box_width = 0; 32 public static static double selection_box_height = 0; 33 public static static double selection_box_center_x = 0; 34 public static static double selection_box_center_y = 0; 35 36 public signal void selection_changed (); 37 public signal void objects_moved (); 38 public signal void objects_deselected (); 39 40 public MoveTool (string n) { 41 base (n, t_("Move paths")); 42 43 selection_changed.connect (() => { 44 update_selection_boundaries (); 45 redraw(); 46 }); 47 48 objects_deselected.connect (() => { 49 update_selection_boundaries (); 50 redraw(); 51 }); 52 53 select_action.connect((self) => { 54 MainWindow.get_current_glyph ().close_path (); 55 }); 56 57 deselect_action.connect((self) => { 58 }); 59 60 press_action.connect((self, b, x, y) => { 61 press (b, x, y); 62 }); 63 64 release_action.connect((self, b, x, y) => { 65 release (b, x, y); 66 }); 67 68 move_action.connect ((self, x, y) => { 69 move (x, y); 70 }); 71 72 key_press_action.connect ((self, keyval) => { 73 key_down (keyval); 74 }); 75 76 draw_action.connect ((self, cr, glyph) => { 77 draw_actions (cr); 78 }); 79 } 80 81 public static void draw_actions (Context cr) { 82 if (group_selection) { 83 draw_selection_box (cr); 84 } 85 } 86 87 public void key_down (uint32 keyval) { 88 Glyph g = MainWindow.get_current_glyph (); 89 90 // delete selected paths 91 if (keyval == Key.DEL || keyval == Key.BACK_SPACE) { 92 93 if (g.active_paths.size > 0) { 94 g.store_undo_state (); 95 } 96 97 foreach (Object p in g.active_paths) { 98 if (p is PathObject) { 99 g.layers.remove_path (((PathObject) p).get_path ()); 100 } else { 101 g.layers.remove (p); 102 } 103 104 g.update_view (); 105 } 106 107 g.active_paths.clear (); 108 } 109 110 if (is_arrow_key (keyval)) { 111 move_selected_paths (keyval); 112 } 113 } 114 115 public void move (int x, int y) { 116 Glyph glyph = MainWindow.get_current_glyph (); 117 double dx = Glyph.path_coordinate_x (last_x) - Glyph.path_coordinate_x (x); 118 double dy = Glyph.path_coordinate_y (last_y) - Glyph.path_coordinate_y (y); 119 double delta_x, delta_y; 120 121 if (!move_path) { 122 return; 123 } 124 125 if (move_path && (fabs(dx) > 0 || fabs (dy) > 0)) { 126 moved = true; 127 128 delta_x = -dx; 129 delta_y = -dy; 130 131 foreach (Object object in glyph.active_paths) { 132 object.move (delta_x, delta_y); 133 } 134 } 135 136 last_x = x; 137 last_y = y; 138 139 update_selection_boundaries (); 140 141 if (glyph.active_paths.size > 0) { 142 objects_moved (); 143 } 144 145 BirdFont.get_current_font ().touch (); 146 147 GlyphCanvas.redraw (); 148 PenTool.reset_stroke (); 149 } 150 151 public void release (int b, int x, int y) { 152 Glyph glyph = MainWindow.get_current_glyph (); 153 154 move_path = false; 155 156 if (GridTool.is_visible () && moved) { 157 tie_paths_to_grid (glyph); 158 } else if (GridTool.has_ttf_grid ()) { 159 foreach (Object p in glyph.active_paths) { 160 tie_path_to_ttf_grid (p); 161 } 162 } 163 164 if (group_selection) { 165 select_group (); 166 } 167 168 group_selection = false; 169 moved = false; 170 171 if (glyph.active_paths.size > 0) { 172 selection_changed (); 173 objects_moved (); 174 DrawingTools.resize_tool.signal_objects_rotated (); 175 176 foreach (Object o in glyph.active_paths) { 177 if (o is PathObject) { 178 PathObject path = (PathObject) o; 179 path.get_path ().create_full_stroke (); 180 } 181 } 182 } else { 183 objects_deselected (); 184 } 185 } 186 187 public void press (int b, int x, int y) { 188 Glyph glyph = MainWindow.get_current_glyph (); 189 Object object; 190 bool selected = false; 191 Object? o; 192 193 glyph.store_undo_state (); 194 double px = Glyph.path_coordinate_x (x); 195 double py = Glyph.path_coordinate_y (y); 196 o = glyph.get_object_at (px, py); 197 198 if (o != null) { 199 object = (!) o; 200 selected = glyph.active_paths_contains (object); 201 202 if (!selected && !KeyBindings.has_shift ()) { 203 glyph.clear_active_paths (); 204 } 205 206 if (selected && KeyBindings.has_shift ()) { 207 glyph.active_paths.remove (object); 208 } else { 209 glyph.add_active_object (null, object); 210 } 211 } else if (!KeyBindings.has_shift ()) { 212 glyph.clear_active_paths (); 213 } 214 215 update_selection_boundaries (); 216 217 move_path = true; 218 219 last_x = x; 220 last_y = y; 221 222 if (glyph.active_paths.size == 0) { 223 group_selection = true; 224 selection_x = x; 225 selection_y = y; 226 } 227 228 update_boundaries_for_selection (); 229 selection_changed (); 230 GlyphCanvas.redraw (); 231 } 232 233 void select_group () { 234 double x1 = Glyph.path_coordinate_x (Math.fmin (selection_x, last_x)); 235 double y1 = Glyph.path_coordinate_y (Math.fmin (selection_y, last_y)); 236 double x2 = Glyph.path_coordinate_x (Math.fmax (selection_x, last_x)); 237 double y2 = Glyph.path_coordinate_y (Math.fmax (selection_y, last_y)); 238 Glyph glyph = MainWindow.get_current_glyph (); 239 240 glyph.clear_active_paths (); 241 242 foreach (Object p in glyph.get_objects_in_current_layer ()) { 243 if (p.xmin > x1 && p.xmax < x2 && p.ymin < y1 && p.ymax > y2) { 244 if (!p.is_empty ()) { 245 glyph.add_active_object (null, p); 246 } 247 } 248 } 249 250 selection_changed (); 251 } 252 253 public static void update_selection_boundaries () { 254 get_selection_box_boundaries (out selection_box_center_x, 255 out selection_box_center_y, out selection_box_width, 256 out selection_box_height); 257 } 258 259 public void move_to_baseline () { 260 Glyph glyph = MainWindow.get_current_glyph (); 261 Font font = BirdFont.get_current_font (); 262 double x, y, w, h; 263 264 get_selection_box_boundaries (out x, out y, out w, out h); 265 266 foreach (Object path in glyph.active_paths) { 267 path.move (glyph.left_limit - x + w / 2, font.base_line - y + h / 2); 268 } 269 270 update_selection_boundaries (); 271 objects_moved (); 272 GlyphCanvas.redraw (); 273 } 274 275 public static void get_selection_box_boundaries (out double x, out double y, out double w, out double h) { 276 double px, py, px2, py2; 277 Glyph glyph = MainWindow.get_current_glyph (); 278 279 px = 10000; 280 py = 10000; 281 px2 = -10000; 282 py2 = -10000; 283 284 foreach (Object o in glyph.active_paths) { 285 if (o is PathObject) { 286 Path p = ((PathObject) o).get_path (); 287 p.update_region_boundaries (); 288 289 if (px > p.xmin) { 290 px = p.xmin; 291 } 292 293 if (py > p.ymin) { 294 py = p.ymin; 295 } 296 297 if (px2 < p.xmax) { 298 px2 = p.xmax; 299 } 300 301 if (py2 < p.ymax) { 302 py2 = p.ymax; 303 } 304 } 305 } 306 307 w = px2 - px; 308 h = py2 - py; 309 x = px + (w / 2); 310 y = py + (h / 2); 311 } 312 313 void move_selected_paths (uint key) { 314 Glyph glyph = MainWindow.get_current_glyph (); 315 double x, y; 316 317 x = 0; 318 y = 0; 319 320 switch (key) { 321 case Key.UP: 322 y = 1; 323 break; 324 case Key.DOWN: 325 y = -1; 326 break; 327 case Key.LEFT: 328 x = -1; 329 break; 330 case Key.RIGHT: 331 x = 1; 332 break; 333 default: 334 break; 335 } 336 337 foreach (Object path in glyph.active_paths) { 338 path.move (x * Glyph.ivz (), y * Glyph.ivz ()); 339 } 340 341 BirdFont.get_current_font ().touch (); 342 PenTool.reset_stroke (); 343 update_selection_boundaries (); 344 objects_moved (); 345 glyph.redraw_area (0, 0, glyph.allocation.width, glyph.allocation.height); 346 } 347 348 static void tie_path_to_ttf_grid (Object p) { 349 double sx, sy, qx, qy; 350 351 sx = p.xmax; 352 sy = p.ymax; 353 qx = p.xmin; 354 qy = p.ymin; 355 356 GridTool.ttf_grid_coordinate (ref sx, ref sy); 357 GridTool.ttf_grid_coordinate (ref qx, ref qy); 358 359 if (Math.fabs (qy - p.ymin) < Math.fabs (sy - p.ymax)) { 360 p.move (0, qy - p.ymin); 361 } else { 362 p.move (0, sy - p.ymax); 363 } 364 365 if (Math.fabs (qx - p.xmin) < Math.fabs (sx - p.xmax)) { 366 p.move (qx - p.xmin, 0); 367 } else { 368 p.move (sx - p.xmax, 0); 369 } 370 } 371 372 static void tie_paths_to_grid (Glyph g) { 373 double sx, sy, qx, qy; 374 double dx_min, dx_max, dy_min, dy_max;; 375 double maxx, maxy, minx, miny; 376 377 update_selection_boundaries (); 378 379 // tie to grid 380 maxx = selection_box_center_x + selection_box_width / 2; 381 maxy = selection_box_center_y + selection_box_height / 2; 382 minx = selection_box_center_x - selection_box_width / 2; 383 miny = selection_box_center_y - selection_box_height / 2; 384 385 sx = maxx; 386 sy = maxy; 387 qx = minx; 388 qy = miny; 389 390 GridTool.tie_coordinate (ref sx, ref sy); 391 GridTool.tie_coordinate (ref qx, ref qy); 392 393 dy_min = Math.fabs (qy - miny); 394 dy_max = Math.fabs (sy - maxy); 395 dx_min = Math.fabs (qx - minx); 396 dx_max = Math.fabs (sx - maxx); 397 398 foreach (Object p in g.active_paths) { 399 if (dy_min < dy_max) { 400 p.move (0, qy - miny); 401 } else { 402 p.move (0, sy - maxy); 403 } 404 405 if (dx_min < dx_max) { 406 p.move (qx - minx, 0); 407 } else { 408 p.move (sx - maxx, 0); 409 } 410 } 411 412 update_selection_boundaries (); 413 } 414 415 public static void update_boundaries_for_selection () { 416 Glyph glyph = MainWindow.get_current_glyph (); 417 foreach (Object o in glyph.active_paths) { 418 if (o is PathObject) { 419 ((PathObject)o).get_path ().update_region_boundaries (); 420 } 421 } 422 } 423 424 public static void flip_vertical () { 425 flip (true); 426 } 427 428 public static void flip_horizontal () { 429 flip (false); 430 } 431 432 public static void flip (bool vertical) { 433 double xc, yc, xc2, yc2, w, h; 434 double dx, dy; 435 Glyph glyph = MainWindow.get_current_glyph (); 436 437 update_selection_boundaries (); 438 439 xc = selection_box_center_x; 440 yc = selection_box_center_y; 441 442 foreach (Object p in glyph.active_paths) { 443 if (p is PathObject) { 444 Path path = ((PathObject) p).get_path (); 445 446 // FIXME: move to object 447 if (vertical) { 448 path.flip_vertical (); 449 } else { 450 path.flip_horizontal (); 451 } 452 453 path.reverse (); 454 } 455 } 456 457 get_selection_box_boundaries (out xc2, out yc2, out w, out h); 458 459 dx = -(xc2 - xc); 460 dy = -(yc2 - yc); 461 462 foreach (Object p in glyph.active_paths) { 463 p.move (dx, dy); 464 } 465 466 update_selection_boundaries (); 467 PenTool.reset_stroke (); 468 469 BirdFont.get_current_font ().touch (); 470 } 471 472 public void select_all_paths () { 473 Glyph g = MainWindow.get_current_glyph (); 474 475 g.clear_active_paths (); 476 foreach (Object p in g.get_objects_in_current_layer ()) { 477 if (!p.is_empty ()) { 478 g.add_active_object (null, p); 479 } 480 } 481 482 g.update_view (); 483 484 update_selection_boundaries (); 485 objects_moved (); 486 } 487 488 static void draw_selection_box (Context cr) { 489 double x = Math.fmin (selection_x, last_x); 490 double y = Math.fmin (selection_y, last_y); 491 492 double w = Math.fabs (selection_x - last_x); 493 double h = Math.fabs (selection_y - last_y); 494 495 Glyph glyph = MainWindow.get_current_glyph (); 496 497 cr.save (); 498 Theme.color (cr, "Foreground 1"); 499 cr.set_line_width (2); 500 cr.rectangle (x, y, w, h); 501 cr.stroke (); 502 cr.restore (); 503 } 504 } 505 506 } 507