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.
Import SVG files for color fonts
1 /* 2 Copyright (C) 2012 2013 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 (Path p in g.active_paths) { 98 g.layers.remove_path (p); 99 g.update_view (); 100 } 101 102 g.active_paths.clear (); 103 } 104 105 if (is_arrow_key (keyval)) { 106 move_selected_paths (keyval); 107 } 108 } 109 110 public void move (int x, int y) { 111 Glyph glyph = MainWindow.get_current_glyph (); 112 double dx = last_x - x; 113 double dy = last_y - y; 114 double p = PenTool.precision; 115 double delta_x, delta_y; 116 117 if (!move_path) { 118 return; 119 } 120 121 if (move_path && (fabs(dx) > 0 || fabs (dy) > 0)) { 122 moved = true; 123 124 delta_x = Glyph.ivz () * -dx * p; 125 delta_y = Glyph.ivz () * dy * p; 126 127 if (glyph.color_svg_data != null) { 128 glyph.svg_x += delta_x; 129 glyph.svg_y += delta_y; 130 } else { 131 foreach (Layer group in glyph.selected_groups) { 132 if (group.gradient != null) { 133 Gradient g = (!) group.gradient; 134 g.x1 += delta_x; 135 g.x2 += delta_x; 136 g.y1 += delta_y; 137 g.y2 += delta_y; 138 } 139 } 140 141 foreach (Path path in glyph.active_paths) { 142 path.move (delta_x, delta_y); 143 } 144 } 145 } 146 147 last_x = x; 148 last_y = y; 149 150 update_selection_boundaries (); 151 152 if (glyph.active_paths.size > 0) { 153 objects_moved (); 154 } 155 156 BirdFont.get_current_font ().touch (); 157 158 GlyphCanvas.redraw (); 159 PenTool.reset_stroke (); 160 } 161 162 public void release (int b, int x, int y) { 163 Glyph glyph = MainWindow.get_current_glyph (); 164 165 move_path = false; 166 167 if (GridTool.is_visible () && moved) { 168 tie_paths_to_grid (glyph); 169 } else if (GridTool.has_ttf_grid ()) { 170 foreach (Path p in glyph.active_paths) { 171 tie_path_to_ttf_grid (p); 172 } 173 } 174 175 if (group_selection) { 176 select_group (); 177 } 178 179 group_selection = false; 180 moved = false; 181 182 if (glyph.active_paths.size > 0) { 183 selection_changed (); 184 objects_moved (); 185 DrawingTools.resize_tool.signal_objects_rotated (); 186 187 foreach (Path p in glyph.active_paths) { 188 p.create_full_stroke (); 189 } 190 } else { 191 objects_deselected (); 192 } 193 } 194 195 public void press (int b, int x, int y) { 196 Glyph glyph = MainWindow.get_current_glyph (); 197 Path p; 198 bool selected = false; 199 Layer? group; 200 Layer g; 201 202 glyph.store_undo_state (); 203 group_selection = false; 204 205 if (glyph.color_svg_data == null) { 206 group = glyph.get_path_at (x, y); 207 208 if (group != null) { 209 g = (!) group; 210 return_if_fail (g.paths.paths.size > 0); 211 p = g.paths.paths.get (0); 212 selected = glyph.active_paths.contains (p); 213 214 if (!selected && !KeyBindings.has_shift ()) { 215 glyph.clear_active_paths (); 216 } 217 218 foreach (Path lp in g.paths.paths) { 219 if (selected && KeyBindings.has_shift ()) { 220 glyph.selected_groups.remove ((!) group); 221 glyph.active_paths.remove (lp); 222 } else { 223 glyph.add_active_path ((!) group, lp); 224 } 225 } 226 } else if (!KeyBindings.has_shift ()) { 227 glyph.clear_active_paths (); 228 } 229 230 update_selection_boundaries (); 231 } 232 233 move_path = true; 234 235 last_x = x; 236 last_y = y; 237 238 if (glyph.active_paths.size == 0) { 239 group_selection = true; 240 selection_x = x; 241 selection_y = y; 242 } 243 244 update_boundaries_for_selection (); 245 selection_changed (); 246 GlyphCanvas.redraw (); 247 } 248 249 250 void select_group () { 251 double x1 = Glyph.path_coordinate_x (Math.fmin (selection_x, last_x)); 252 double y1 = Glyph.path_coordinate_y (Math.fmin (selection_y, last_y)); 253 double x2 = Glyph.path_coordinate_x (Math.fmax (selection_x, last_x)); 254 double y2 = Glyph.path_coordinate_y (Math.fmax (selection_y, last_y)); 255 Glyph glyph = MainWindow.get_current_glyph (); 256 257 glyph.clear_active_paths (); 258 259 foreach (Path p in glyph.get_paths_in_current_layer ()) { 260 if (p.xmin > x1 && p.xmax < x2 && p.ymin < y1 && p.ymax > y2) { 261 if (p.points.size > 0) { 262 glyph.add_active_path (null, p); 263 } 264 } 265 } 266 267 selection_changed (); 268 } 269 270 public static void update_selection_boundaries () { 271 get_selection_box_boundaries (out selection_box_center_x, 272 out selection_box_center_y, out selection_box_width, 273 out selection_box_height); 274 } 275 276 public void move_to_baseline () { 277 Glyph glyph = MainWindow.get_current_glyph (); 278 Font font = BirdFont.get_current_font (); 279 double x, y, w, h; 280 281 get_selection_box_boundaries (out x, out y, out w, out h); 282 283 foreach (Path path in glyph.active_paths) { 284 path.move (glyph.left_limit - x + w / 2, font.base_line - y + h / 2); 285 } 286 287 update_selection_boundaries (); 288 objects_moved (); 289 GlyphCanvas.redraw (); 290 } 291 292 static void draw_selection_box (Context cr) { 293 double x = Math.fmin (selection_x, last_x); 294 double y = Math.fmin (selection_y, last_y); 295 296 double w = Math.fabs (selection_x - last_x); 297 double h = Math.fabs (selection_y - last_y); 298 299 Glyph glyph = MainWindow.get_current_glyph (); 300 301 if (glyph.color_svg_data == null) { 302 cr.save (); 303 Theme.color (cr, "Foreground 1"); 304 cr.set_line_width (2); 305 cr.rectangle (x, y, w, h); 306 cr.stroke (); 307 cr.restore (); 308 } 309 } 310 311 public static void get_selection_box_boundaries (out double x, out double y, out double w, out double h) { 312 double px, py, px2, py2; 313 Glyph glyph = MainWindow.get_current_glyph (); 314 315 px = 10000; 316 py = 10000; 317 px2 = -10000; 318 py2 = -10000; 319 320 foreach (Path p in glyph.active_paths) { 321 p.update_region_boundaries (); 322 323 if (px > p.xmin) { 324 px = p.xmin; 325 } 326 327 if (py > p.ymin) { 328 py = p.ymin; 329 } 330 331 if (px2 < p.xmax) { 332 px2 = p.xmax; 333 } 334 335 if (py2 < p.ymax) { 336 py2 = p.ymax; 337 } 338 } 339 340 w = px2 - px; 341 h = py2 - py; 342 x = px + (w / 2); 343 y = py + (h / 2); 344 } 345 346 void move_selected_paths (uint key) { 347 Glyph glyph = MainWindow.get_current_glyph (); 348 double x, y; 349 350 x = 0; 351 y = 0; 352 353 switch (key) { 354 case Key.UP: 355 y = 1; 356 break; 357 case Key.DOWN: 358 y = -1; 359 break; 360 case Key.LEFT: 361 x = -1; 362 break; 363 case Key.RIGHT: 364 x = 1; 365 break; 366 default: 367 break; 368 } 369 370 foreach (Path path in glyph.active_paths) { 371 path.move (x * Glyph.ivz (), y * Glyph.ivz ()); 372 } 373 374 BirdFont.get_current_font ().touch (); 375 PenTool.reset_stroke (); 376 update_selection_boundaries (); 377 objects_moved (); 378 glyph.redraw_area (0, 0, glyph.allocation.width, glyph.allocation.height); 379 } 380 381 static void tie_path_to_ttf_grid (Path p) { 382 double sx, sy, qx, qy; 383 384 sx = p.xmax; 385 sy = p.ymax; 386 qx = p.xmin; 387 qy = p.ymin; 388 389 GridTool.ttf_grid_coordinate (ref sx, ref sy); 390 GridTool.ttf_grid_coordinate (ref qx, ref qy); 391 392 if (Math.fabs (qy - p.ymin) < Math.fabs (sy - p.ymax)) { 393 p.move (0, qy - p.ymin); 394 } else { 395 p.move (0, sy - p.ymax); 396 } 397 398 if (Math.fabs (qx - p.xmin) < Math.fabs (sx - p.xmax)) { 399 p.move (qx - p.xmin, 0); 400 } else { 401 p.move (sx - p.xmax, 0); 402 } 403 } 404 405 static void tie_paths_to_grid (Glyph g) { 406 double sx, sy, qx, qy; 407 double dx_min, dx_max, dy_min, dy_max;; 408 double maxx, maxy, minx, miny; 409 410 update_selection_boundaries (); 411 412 // tie to grid 413 maxx = selection_box_center_x + selection_box_width / 2; 414 maxy = selection_box_center_y + selection_box_height / 2; 415 minx = selection_box_center_x - selection_box_width / 2; 416 miny = selection_box_center_y - selection_box_height / 2; 417 418 sx = maxx; 419 sy = maxy; 420 qx = minx; 421 qy = miny; 422 423 GridTool.tie_coordinate (ref sx, ref sy); 424 GridTool.tie_coordinate (ref qx, ref qy); 425 426 dy_min = Math.fabs (qy - miny); 427 dy_max = Math.fabs (sy - maxy); 428 dx_min = Math.fabs (qx - minx); 429 dx_max = Math.fabs (sx - maxx); 430 431 foreach (Path p in g.active_paths) { 432 if (dy_min < dy_max) { 433 p.move (0, qy - miny); 434 } else { 435 p.move (0, sy - maxy); 436 } 437 438 if (dx_min < dx_max) { 439 p.move (qx - minx, 0); 440 } else { 441 p.move (sx - maxx, 0); 442 } 443 } 444 445 update_selection_boundaries (); 446 } 447 448 public static void update_boundaries_for_selection () { 449 Glyph glyph = MainWindow.get_current_glyph (); 450 foreach (Path p in glyph.active_paths) { 451 p.update_region_boundaries (); 452 } 453 } 454 455 public static void flip_vertical () { 456 flip (true); 457 } 458 459 public static void flip_horizontal () { 460 flip (false); 461 } 462 463 public static void flip (bool vertical) { 464 double xc, yc, xc2, yc2, w, h; 465 double dx, dy; 466 Glyph glyph = MainWindow.get_current_glyph (); 467 468 update_selection_boundaries (); 469 470 xc = selection_box_center_x; 471 yc = selection_box_center_y; 472 473 foreach (Path p in glyph.active_paths) { 474 if (vertical) { 475 p.flip_vertical (); 476 } else { 477 p.flip_horizontal (); 478 } 479 480 p.reverse (); 481 } 482 483 get_selection_box_boundaries (out xc2, out yc2, out w, out h); 484 485 dx = -(xc2 - xc); 486 dy = -(yc2 - yc); 487 488 foreach (Path p in glyph.active_paths) { 489 p.move (dx, dy); 490 } 491 492 update_selection_boundaries (); 493 PenTool.reset_stroke (); 494 495 BirdFont.get_current_font ().touch (); 496 } 497 498 public void select_all_paths () { 499 Glyph g = MainWindow.get_current_glyph (); 500 501 g.clear_active_paths (); 502 foreach (Path p in g.get_paths_in_current_layer ()) { 503 if (p.points.size > 0) { 504 g.add_active_path (null, p); 505 } 506 } 507 508 g.update_view (); 509 510 update_selection_boundaries (); 511 objects_moved (); 512 } 513 } 514 515 } 516