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.
Resize paths
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 using SvgBird; 18 19 namespace BirdFont { 20 21 public class MoveTool : Tool { 22 23 static bool move_path = false; 24 static bool moved = false; 25 static double last_x = 0; 26 static double last_y = 0; 27 28 static double selection_x = 0; 29 static double selection_y = 0; 30 static bool group_selection= false; 31 32 public static double selection_box_width = 0; 33 public static double selection_box_height = 0; 34 public static double selection_box_center_x = 0; 35 public static double selection_box_center_y = 0; 36 public static double selection_box_left = 0; 37 public static double selection_box_top = 0; 38 39 public signal void selection_changed (); 40 public signal void objects_moved (); 41 public signal void objects_deselected (); 42 43 public MoveTool (string name) { 44 base (name, t_("Move paths")); 45 46 selection_changed.connect (() => { 47 update_selection_boundaries (); 48 redraw(); 49 }); 50 51 objects_deselected.connect (() => { 52 update_selection_boundaries (); 53 redraw(); 54 }); 55 56 select_action.connect((self) => { 57 MainWindow.get_current_glyph ().close_path (); 58 }); 59 60 deselect_action.connect((self) => { 61 }); 62 63 press_action.connect((self, b, x, y) => { 64 press (b, x, y); 65 }); 66 67 release_action.connect((self, b, x, y) => { 68 release (b, x, y); 69 }); 70 71 move_action.connect ((self, x, y) => { 72 move (x, y); 73 }); 74 75 key_press_action.connect ((self, keyval) => { 76 key_down (keyval); 77 }); 78 79 draw_action.connect ((self, cr, glyph) => { 80 draw_actions (cr); 81 }); 82 } 83 84 public static void draw_actions (Context cr) { 85 if (group_selection) { 86 draw_selection_box (cr); 87 } 88 } 89 90 public void key_down (uint32 keyval) { 91 Glyph g = MainWindow.get_current_glyph (); 92 93 // delete selected paths 94 if (keyval == Key.DEL || keyval == Key.BACK_SPACE) { 95 96 if (g.active_paths.size > 0) { 97 g.store_undo_state (); 98 } 99 100 foreach (SvgBird.Object p in g.active_paths) { 101 g.layers.remove (p); 102 g.update_view (); 103 } 104 105 g.active_paths.clear (); 106 } 107 108 if (is_arrow_key (keyval)) { 109 move_selected_paths (keyval); 110 } 111 } 112 113 public void move (int x, int y) { 114 Glyph glyph = MainWindow.get_current_glyph (); 115 double dx = Glyph.path_coordinate_x (last_x) - Glyph.path_coordinate_x (x); 116 double dy = Glyph.path_coordinate_y (last_y) - Glyph.path_coordinate_y (y); 117 double delta_x, delta_y; 118 119 if (!move_path) { 120 return; 121 } 122 123 if (move_path && (fabs(dx) > 0 || fabs (dy) > 0)) { 124 moved = true; 125 126 delta_x = -dx; 127 delta_y = -dy; 128 129 foreach (SvgBird.Object object in glyph.active_paths) { 130 object.move (delta_x, delta_y); 131 } 132 } 133 134 last_x = x; 135 last_y = y; 136 137 update_selection_boundaries (); 138 139 if (glyph.active_paths.size > 0) { 140 objects_moved (); 141 } 142 143 BirdFont.get_current_font ().touch (); 144 145 GlyphCanvas.redraw (); 146 PenTool.reset_stroke (); 147 } 148 149 public void release (int b, int x, int y) { 150 Glyph glyph = MainWindow.get_current_glyph (); 151 152 move_path = false; 153 154 if (GridTool.is_visible () && moved) { 155 tie_paths_to_grid (glyph); 156 } else if (GridTool.has_ttf_grid ()) { 157 foreach (SvgBird.Object p in glyph.active_paths) { 158 tie_path_to_ttf_grid (p); 159 } 160 } 161 162 if (group_selection) { 163 select_group (); 164 } 165 166 group_selection = false; 167 moved = false; 168 169 if (glyph.active_paths.size > 0) { 170 selection_changed (); 171 objects_moved (); 172 DrawingTools.resize_tool.signal_objects_rotated (); 173 174 foreach (SvgBird.Object o in glyph.active_paths) { 175 if (o is PathObject) { 176 PathObject path = (PathObject) o; 177 path.get_path ().create_full_stroke (); 178 } 179 } 180 } else { 181 objects_deselected (); 182 } 183 } 184 185 public void press (int b, int x, int y) { 186 Glyph glyph = MainWindow.get_current_glyph (); 187 SvgBird.Object object; 188 bool selected = false; 189 SvgBird.Object? o; 190 191 glyph.store_undo_state (); 192 double px = Glyph.path_coordinate_x (x); 193 double py = Glyph.path_coordinate_y (y); 194 o = glyph.get_object_at (px, py); 195 196 if (o != null) { 197 object = (!) o; 198 selected = glyph.active_paths_contains (object); 199 200 if (!selected && !KeyBindings.has_shift ()) { 201 glyph.clear_active_paths (); 202 } 203 204 if (selected && KeyBindings.has_shift ()) { 205 glyph.active_paths.remove (object); 206 } else { 207 glyph.add_active_object (object); 208 } 209 } else if (!KeyBindings.has_shift ()) { 210 glyph.clear_active_paths (); 211 } 212 213 update_selection_boundaries (); 214 215 move_path = true; 216 217 last_x = x; 218 last_y = y; 219 220 if (glyph.active_paths.size == 0) { 221 group_selection = true; 222 selection_x = x; 223 selection_y = y; 224 } 225 226 update_boundaries_for_selection (); 227 selection_changed (); 228 GlyphCanvas.redraw (); 229 } 230 231 void select_group () { 232 double x1 = Glyph.path_coordinate_x (Math.fmin (selection_x, last_x)); 233 double y1 = Glyph.path_coordinate_y (Math.fmin (selection_y, last_y)); 234 double x2 = Glyph.path_coordinate_x (Math.fmax (selection_x, last_x)); 235 double y2 = Glyph.path_coordinate_y (Math.fmax (selection_y, last_y)); 236 Glyph glyph = MainWindow.get_current_glyph (); 237 238 glyph.clear_active_paths (); 239 240 foreach (SvgBird.Object p in glyph.get_objects_in_current_layer ()) { 241 if (p.xmin > x1 && p.xmax < x2 && p.ymin < y1 && p.ymax > y2) { 242 if (!p.is_empty ()) { 243 glyph.add_active_object (p); 244 } 245 } 246 } 247 248 selection_changed (); 249 } 250 251 public static void update_selection_boundaries () { 252 get_selection_box_boundaries (out selection_box_center_x, 253 out selection_box_center_y, out selection_box_width, 254 out selection_box_height, out selection_box_left, 255 out selection_box_top); 256 } 257 258 public void move_to_baseline () { 259 Glyph glyph = MainWindow.get_current_glyph (); 260 Font font = BirdFont.get_current_font (); 261 double x, y, w, h, l, t; 262 263 get_selection_box_boundaries (out x, out y, out w, out h, out l, out t); 264 265 foreach (SvgBird.Object path in glyph.active_paths) { 266 path.move (glyph.left_limit - x + w / 2, font.base_line - y + h / 2); 267 } 268 269 update_selection_boundaries (); 270 objects_moved (); 271 GlyphCanvas.redraw (); 272 } 273 274 public static void get_selection_box_boundaries (out double x, out double y, out double w, out double h, 275 out double left, out double top) { 276 277 double px, py, px2, py2; 278 Glyph glyph = MainWindow.get_current_glyph (); 279 280 px = 10000; 281 py = 10000; 282 px2 = -10000; 283 py2 = -10000; 284 top = 10000; 285 left = 10000; 286 287 glyph.layers.update_boundaries_for_object (); 288 289 foreach (SvgBird.Object o in glyph.active_paths) { 290 if (top > o.top) { 291 top = o.top; 292 } 293 294 if (left > o.left) { 295 left = o.left; 296 } 297 298 if (px > o.xmin) { 299 px = o.xmin; 300 } 301 302 if (py > o.ymin) { 303 py = o.ymin; 304 } 305 306 if (px2 < o.xmax) { 307 px2 = o.xmax; 308 } 309 310 if (py2 < o.ymax) { 311 py2 = o.ymax; 312 } 313 } 314 315 w = px2 - px; 316 h = py2 - py; 317 x = px + (w / 2); 318 y = py + (h / 2); 319 } 320 321 void move_selected_paths (uint key) { 322 Glyph glyph = MainWindow.get_current_glyph (); 323 double x, y; 324 325 x = 0; 326 y = 0; 327 328 switch (key) { 329 case Key.UP: 330 y = 1; 331 break; 332 case Key.DOWN: 333 y = -1; 334 break; 335 case Key.LEFT: 336 x = -1; 337 break; 338 case Key.RIGHT: 339 x = 1; 340 break; 341 default: 342 break; 343 } 344 345 foreach (SvgBird.Object path in glyph.active_paths) { 346 path.move (x * Glyph.ivz (), y * Glyph.ivz ()); 347 } 348 349 BirdFont.get_current_font ().touch (); 350 PenTool.reset_stroke (); 351 update_selection_boundaries (); 352 objects_moved (); 353 GlyphCanvas.redraw (); 354 } 355 356 static void tie_path_to_ttf_grid (SvgBird.Object p) { 357 double sx, sy, qx, qy; 358 359 sx = p.xmax; 360 sy = p.ymax; 361 qx = p.xmin; 362 qy = p.ymin; 363 364 GridTool.ttf_grid_coordinate (ref sx, ref sy); 365 GridTool.ttf_grid_coordinate (ref qx, ref qy); 366 367 if (Math.fabs (qy - p.ymin) < Math.fabs (sy - p.ymax)) { 368 p.move (0, qy - p.ymin); 369 } else { 370 p.move (0, sy - p.ymax); 371 } 372 373 if (Math.fabs (qx - p.xmin) < Math.fabs (sx - p.xmax)) { 374 p.move (qx - p.xmin, 0); 375 } else { 376 p.move (sx - p.xmax, 0); 377 } 378 } 379 380 static void tie_paths_to_grid (Glyph g) { 381 double sx, sy, qx, qy; 382 double dx_min, dx_max, dy_min, dy_max;; 383 double maxx, maxy, minx, miny; 384 385 update_selection_boundaries (); 386 387 // tie to grid 388 maxx = selection_box_center_x + selection_box_width / 2; 389 maxy = selection_box_center_y + selection_box_height / 2; 390 minx = selection_box_center_x - selection_box_width / 2; 391 miny = selection_box_center_y - selection_box_height / 2; 392 393 sx = maxx; 394 sy = maxy; 395 qx = minx; 396 qy = miny; 397 398 GridTool.tie_coordinate (ref sx, ref sy); 399 GridTool.tie_coordinate (ref qx, ref qy); 400 401 dy_min = Math.fabs (qy - miny); 402 dy_max = Math.fabs (sy - maxy); 403 dx_min = Math.fabs (qx - minx); 404 dx_max = Math.fabs (sx - maxx); 405 406 foreach (SvgBird.Object p in g.active_paths) { 407 if (dy_min < dy_max) { 408 p.move (0, qy - miny); 409 } else { 410 p.move (0, sy - maxy); 411 } 412 413 if (dx_min < dx_max) { 414 p.move (qx - minx, 0); 415 } else { 416 p.move (sx - maxx, 0); 417 } 418 } 419 420 update_selection_boundaries (); 421 } 422 423 public static void update_boundaries_for_selection () { 424 Glyph glyph = MainWindow.get_current_glyph (); 425 glyph.layers.update_boundaries_for_object (); 426 } 427 428 public static void flip_vertical () { 429 flip (true); 430 } 431 432 public static void flip_horizontal () { 433 flip (false); 434 } 435 436 public static void flip (bool vertical) { 437 double xc, yc, xc2, yc2, w, h, l, t; 438 double dx, dy; 439 Glyph glyph = MainWindow.get_current_glyph (); 440 441 update_selection_boundaries (); 442 443 xc = selection_box_center_x; 444 yc = selection_box_center_y; 445 446 foreach (SvgBird.Object p in glyph.active_paths) { 447 if (p is PathObject) { 448 Path path = ((PathObject) p).get_path (); 449 450 // FIXME: move to object 451 if (vertical) { 452 path.flip_vertical (); 453 } else { 454 path.flip_horizontal (); 455 } 456 457 path.reverse (); 458 } 459 } 460 461 get_selection_box_boundaries (out xc2, out yc2, out w, out h, out l, out t); 462 463 dx = -(xc2 - xc); 464 dy = -(yc2 - yc); 465 466 foreach (SvgBird.Object p in glyph.active_paths) { 467 p.move (dx, dy); 468 } 469 470 update_selection_boundaries (); 471 PenTool.reset_stroke (); 472 473 BirdFont.get_current_font ().touch (); 474 } 475 476 public void select_all_paths () { 477 Glyph g = MainWindow.get_current_glyph (); 478 479 g.clear_active_paths (); 480 foreach (SvgBird.Object p in g.get_objects_in_current_layer ()) { 481 if (!p.is_empty ()) { 482 g.add_active_object (p); 483 } 484 } 485 486 g.update_view (); 487 488 update_selection_boundaries (); 489 objects_moved (); 490 491 ResizeTool.update_selection_box (); 492 } 493 494 static void draw_selection_box (Context cr) { 495 double x = Math.fmin (selection_x, last_x); 496 double y = Math.fmin (selection_y, last_y); 497 498 double w = Math.fabs (selection_x - last_x); 499 double h = Math.fabs (selection_y - last_y); 500 501 cr.save (); 502 Theme.color (cr, "Foreground 1"); 503 cr.set_line_width (2); 504 cr.rectangle (x, y, w, h); 505 cr.stroke (); 506 cr.restore (); 507 } 508 509 public void convert_svg_to_monochrome () { 510 ObjectGroup embedded_paths; 511 Glyph glyph = MainWindow.get_current_glyph (); 512 513 embedded_paths = new ObjectGroup (); 514 515 foreach (SvgBird.Object object in glyph.active_paths) { 516 if (object is EmbeddedSvg) { 517 embedded_paths.add (object); 518 } 519 } 520 521 convert_objects_to_monochrome_glyph (glyph, embedded_paths); 522 GlyphCanvas.redraw (); 523 } 524 525 public void convert_glyph_to_monochrome (Glyph glyph) { 526 ObjectGroup embedded_paths; 527 528 embedded_paths = new ObjectGroup (); 529 530 foreach (SvgBird.Object object in glyph.get_visible_objects ()) { 531 if (object is EmbeddedSvg) { 532 embedded_paths.add (object); 533 } 534 } 535 536 convert_objects_to_monochrome_glyph (glyph, embedded_paths); 537 } 538 539 public void convert_objects_to_monochrome_glyph (Glyph glyph, ObjectGroup embedded_paths) { 540 Font font = BirdFont.get_current_font (); 541 542 foreach (SvgBird.Object object in embedded_paths) { 543 if (object is EmbeddedSvg) { 544 EmbeddedSvg svg = (EmbeddedSvg) object; 545 glyph.clear_active_paths (); 546 string transformed_svg_data = svg.get_transformed_svg_data (); 547 PathList path_list = SvgParser.import_svg_data_in_glyph (transformed_svg_data, glyph); 548 glyph.delete_object (svg); 549 550 foreach (Path path in path_list.paths) { 551 path.move (svg.x - glyph.left_limit, svg.y - font.top_position); 552 } 553 } 554 } 555 } 556 } 557 558 } 559