The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

ResizeTool.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/ResizeTool.vala.
Cache overview items
1 /* 2 Copyright (C) 2013 2015 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 ResizeTool : Tool { 21 bool resize_path_proportional = false; 22 bool resize_width = false; 23 24 Path? resized_path = null; 25 double last_resize_y; 26 double last_resize_x; 27 28 bool move_paths = false; 29 30 static double selection_box_width = 0; 31 static double selection_box_height = 0; 32 static double selection_box_center_x = 0; 33 static double selection_box_center_y = 0; 34 35 static bool rotate_path = false; 36 static double last_rotate_y; 37 static double rotation = 0; 38 static double last_rotate = 0; 39 40 public double last_skew = 0; 41 42 public signal void objects_rotated (double angle); 43 public signal void objects_resized (double width, double height); 44 45 Text proportional_handle; 46 Text horizontal_handle; 47 48 public ResizeTool (string n) { 49 base (n, t_("Resize and rotate paths")); 50 51 proportional_handle = new Text ("resize_handle", 60); 52 proportional_handle.load_font ("icons.bf"); 53 Theme.text_color (proportional_handle, "Highlighted 1"); 54 55 horizontal_handle = new Text ("resize_handle_horizontal", 60); 56 horizontal_handle.load_font ("icons.bf"); 57 Theme.text_color (horizontal_handle, "Highlighted 1"); 58 59 select_action.connect((self) => { 60 }); 61 62 deselect_action.connect((self) => { 63 }); 64 65 press_action.connect((self, b, x, y) => { 66 Path last_path; 67 Glyph glyph; 68 69 glyph = MainWindow.get_current_glyph (); 70 glyph.store_undo_state (); 71 72 foreach (Path p in glyph.active_paths) { 73 if (is_over_resize_handle (p, x, y)) { 74 resize_path_proportional = true; 75 resized_path = p; 76 last_resize_x = x; 77 last_resize_y = y; 78 return; 79 } 80 81 if (is_over_horizontal_resize_handle (p, x, y)) { 82 resize_width = true; 83 resized_path = p; 84 last_resize_x = x; 85 last_resize_y = y; 86 return; 87 } 88 } 89 90 foreach (Path p in glyph.active_paths) { 91 if (is_over_rotate_handle (p, x, y)) { 92 rotate_path = true; 93 return; 94 } 95 } 96 97 if (glyph.active_paths.size > 0) { 98 last_path = glyph.active_paths.get (glyph.active_paths.size - 1); 99 last_rotate = last_path.rotation; 100 } 101 102 rotation = last_rotate; 103 last_resize_x = x; 104 last_rotate_y = y; 105 106 if (!resize_path_proportional && !resize_width && !rotate_path) { 107 DrawingTools.move_tool.press (b, x, y); 108 } 109 110 move_paths = true; 111 112 update_selection_box (); 113 }); 114 115 release_action.connect((self, b, x, y) => { 116 resize_path_proportional = false; 117 resize_width = false; 118 rotate_path = false; 119 move_paths = false; 120 DrawingTools.move_tool.release (b, x, y); 121 update_selection_box (); 122 GlyphCanvas.redraw (); 123 124 foreach (Path p in MainWindow.get_current_glyph ().active_paths) { 125 p.create_full_stroke (); 126 } 127 }); 128 129 move_action.connect ((self, x, y) => { 130 Glyph glyph; 131 132 if (resize_path_proportional && can_resize (x, y)) { 133 resize_proportional (x, y); 134 update_selection_box (); 135 } 136 137 if (resize_width && can_resize (x, y)) { 138 resize_horizontal (x, y); 139 update_selection_box (); 140 } 141 142 if (rotate_path) { 143 rotate (x, y); 144 update_selection_box (); 145 } 146 147 if (move_paths 148 || rotate_path 149 || resize_path_proportional 150 || resize_width) { 151 152 glyph = MainWindow.get_current_glyph (); 153 154 foreach (Path selected_path in glyph.active_paths) { 155 selected_path.reset_stroke (); 156 } 157 158 GlyphCanvas.redraw (); 159 } 160 161 DrawingTools.move_tool.move (x, y); 162 }); 163 164 draw_action.connect ((self, cr, glyph) => { 165 Text handle; 166 Glyph g = MainWindow.get_current_glyph (); 167 168 if (!rotate_path) { 169 if (!resize_width) { 170 handle = proportional_handle; 171 get_resize_handle_position (out handle.widget_x, out handle.widget_y); 172 173 handle.widget_x -= handle.get_sidebearing_extent () / 2; 174 handle.widget_y -= handle.get_height () / 2; 175 176 handle.draw (cr); 177 } 178 179 if (!resize_path_proportional) { 180 handle = horizontal_handle; 181 182 get_horizontal_reseize_handle_position (out handle.widget_x, 183 out handle.widget_y); 184 185 handle.widget_x -= handle.get_sidebearing_extent () / 2; 186 handle.widget_y -= handle.get_height () / 2; 187 188 handle.draw (cr); 189 } 190 } 191 192 if (!resize_path_proportional && !resize_width 193 && g.active_paths.size > 0) { 194 195 draw_rotate_handle (cr); 196 } 197 198 MoveTool.draw_actions (cr); 199 }); 200 201 key_press_action.connect ((self, keyval) => { 202 DrawingTools.move_tool.key_down (keyval); 203 }); 204 } 205 206 public static void get_resize_handle_position (out double px, out double py) { 207 px = Glyph.reverse_path_coordinate_x (selection_box_center_x + selection_box_width / 2); 208 py = Glyph.reverse_path_coordinate_y (selection_box_center_y + selection_box_height / 2); 209 } 210 211 public static void get_horizontal_reseize_handle_position (out double px, out double py) { 212 px = Glyph.reverse_path_coordinate_x (selection_box_center_x + selection_box_width / 2); 213 px += 40; 214 py = Glyph.reverse_path_coordinate_y (selection_box_center_y); 215 } 216 217 public static double get_rotated_handle_length () { 218 double s, hx, hy; 219 double d; 220 221 s = fmin (selection_box_width, selection_box_height) * 1.1; 222 d = (s / Glyph.ivz ()) / 2; 223 224 hx = cos (rotation) * d; 225 hy = sin (rotation) * d; 226 227 return d; 228 } 229 230 public void signal_objects_rotated () { 231 objects_rotated (rotation * (180 / PI)); 232 } 233 234 public void rotate_selected_paths (double angle, double cx, double cy) { 235 Glyph glyph = MainWindow.get_current_glyph (); 236 double dx, dy, xc2, yc2, w, h; 237 Path last_path; 238 239 foreach (Path p in glyph.active_paths) { 240 p.rotate (angle, cx, cy); 241 } 242 243 MoveTool.get_selection_box_boundaries (out xc2, out yc2, out w, out h); 244 245 dx = -(xc2 - cx); 246 dy = -(yc2 - cy); 247 248 foreach (Path p in glyph.active_paths) { 249 p.move (dx, dy); 250 } 251 252 last_rotate = rotation; 253 254 MoveTool.update_selection_boundaries (); 255 256 if (glyph.active_paths.size > 0) { 257 last_path = glyph.active_paths.get (glyph.active_paths.size - 1); 258 rotation = last_path.rotation; 259 260 if (rotation > PI) { 261 rotation -= 2 * PI; 262 } 263 264 last_rotate = rotation; 265 signal_objects_rotated (); 266 } 267 } 268 269 /** Move rotate handle to pixel x,y. */ 270 void rotate (double x, double y) { 271 double cx, cy, xc, yc, a, b; 272 273 cx = Glyph.reverse_path_coordinate_x (selection_box_center_x); 274 cy = Glyph.reverse_path_coordinate_y (selection_box_center_y); 275 xc = selection_box_center_x; 276 yc = selection_box_center_y; 277 278 a = x - cx; 279 b = y - cy; 280 281 rotation = atan (b / a); 282 283 if (a < 0) { 284 rotation += PI; 285 } 286 287 rotate_selected_paths (rotation - last_rotate, selection_box_center_x, selection_box_center_y); 288 } 289 290 static bool is_over_rotate_handle (Path p, double x, double y) { 291 double cx, cy, hx, hy; 292 double size = 10; 293 bool inx, iny; 294 295 cx = Glyph.reverse_path_coordinate_x (selection_box_center_x); 296 cy = Glyph.reverse_path_coordinate_y (selection_box_center_y); 297 298 hx = cos (rotation) * get_rotated_handle_length (); 299 hy = sin (rotation) * get_rotated_handle_length (); 300 301 inx = x - size * MainWindow.units <= cx + hx - 2.5 <= x + size * MainWindow.units; 302 iny = y - size * MainWindow.units <= cy + hy - 2.5 <= y + size * MainWindow.units; 303 304 return inx && iny; 305 } 306 307 static void draw_rotate_handle (Context cr) { 308 double cx, cy, hx, hy; 309 310 cx = Glyph.reverse_path_coordinate_x (selection_box_center_x); 311 cy = Glyph.reverse_path_coordinate_y (selection_box_center_y); 312 313 cr.save (); 314 Theme.color (cr, "Highlighted 1"); 315 cr.rectangle (cx - 2.5, cy - 2.5, 5, 5); 316 cr.fill (); 317 318 hx = cos (rotation) * get_rotated_handle_length (); 319 hy = sin (rotation) * get_rotated_handle_length (); 320 321 cr.set_line_width (1); 322 cr.move_to (cx, cy); 323 cr.line_to (cx + hx, cy + hy); 324 cr.stroke (); 325 326 Theme.color (cr, "Highlighted 1"); 327 cr.rectangle (cx + hx - 2.5, cy + hy - 2.5, 5, 5); 328 cr.fill (); 329 330 cr.restore (); 331 } 332 333 double get_resize_ratio (double px, double py) { 334 double ratio, x, y, w, h; 335 336 Glyph glyph = MainWindow.get_current_glyph (); 337 glyph.selection_boundaries (out x, out y, out w, out h); 338 339 ratio = 1; 340 341 if (Math.fabs (last_resize_y - py) > Math.fabs (last_resize_x - px)) { 342 ratio = 1 + (Glyph.path_coordinate_y (py) 343 - Glyph.path_coordinate_y (last_resize_y)) / h; 344 } else { 345 ratio = 1 + (Glyph.path_coordinate_x (px) 346 - Glyph.path_coordinate_x (last_resize_x)) / w; 347 } 348 349 return ratio; 350 } 351 352 public void resize_selected_paths (double ratio_x, double ratio_y) { 353 Glyph g = MainWindow.get_current_glyph (); 354 resize_glyph (g, ratio_x, ratio_y, true); 355 } 356 357 public void resize_glyph (Glyph glyph, double ratio_x, 358 double ratio_y, bool selected = true) { 359 360 double resize_pos_x = 0; 361 double resize_pos_y = 0; 362 double selection_minx, selection_miny, dx, dy; 363 364 if (!selected) { 365 glyph.clear_active_paths (); 366 367 foreach (Path path in glyph.get_visible_paths ()) { 368 glyph.add_active_path (null, path); 369 } 370 } 371 372 get_selection_min (out resize_pos_x, out resize_pos_y); 373 374 // resize paths 375 foreach (Path selected_path in glyph.active_paths) { 376 selected_path.resize (ratio_x, ratio_y); 377 selected_path.reset_stroke (); 378 } 379 380 // move paths relative to the updated xmin and xmax 381 get_selection_min (out selection_minx, out selection_miny); 382 dx = resize_pos_x - selection_minx; 383 dy = resize_pos_y - selection_miny; 384 385 foreach (Path selected_path in glyph.active_paths) { 386 selected_path.move (dx, dy); 387 } 388 389 if (glyph.active_paths.size > 0) { 390 update_selection_box (); 391 objects_resized (selection_box_width, selection_box_height); 392 } 393 394 if (!selected) { 395 double w; 396 w = (ratio_x * glyph.get_width () - glyph.get_width ()) / 2.0; 397 glyph.left_limit -= w; 398 glyph.right_limit += w; 399 glyph.clear_active_paths (); 400 glyph.remove_lines (); 401 glyph.add_help_lines (); 402 } 403 } 404 405 void update_selection_box () { 406 MoveTool.update_boundaries_for_selection (); 407 MoveTool.get_selection_box_boundaries (out selection_box_center_x, 408 out selection_box_center_y, out selection_box_width, 409 out selection_box_height); 410 } 411 412 /** Move resize handle to pixel x,y. */ 413 void resize_proportional (double px, double py) { 414 double ratio; 415 416 ratio = get_resize_ratio (px, py); 417 418 if (ratio != 1) { 419 resize_selected_paths (ratio, ratio); 420 last_resize_x = px; 421 last_resize_y = py; 422 } 423 } 424 425 /** Move resize handle to pixel x,y. */ 426 void resize_horizontal (double px, double py) { 427 double ratio, x, y, w, h; 428 429 Glyph glyph = MainWindow.get_current_glyph (); 430 glyph.selection_boundaries (out x, out y, out w, out h); 431 432 ratio = 1 + (Glyph.path_coordinate_x (px) 433 - Glyph.path_coordinate_x (last_resize_x)) / w; 434 435 if (ratio != 1) { 436 resize_selected_paths (ratio, 1); 437 last_resize_x = px; 438 last_resize_y = py; 439 } 440 } 441 442 public void full_height () { 443 double xc, yc, w, h; 444 Glyph glyph = MainWindow.get_current_glyph (); 445 Font font = BirdFont.get_current_font (); 446 447 MoveTool.update_boundaries_for_selection (); 448 MoveTool.get_selection_box_boundaries (out xc, out yc, out w, out h); 449 450 //compute scale 451 double descender = font.base_line - (yc - h / 2); 452 453 if (descender < 0) { 454 descender = 0; 455 } 456 457 double font_height = font.top_position - font.base_line; 458 double scale = font_height / (h - descender); 459 460 resize_selected_paths (scale, scale); 461 PenTool.reset_stroke (); 462 463 MoveTool.update_boundaries_for_selection (); 464 font.touch (); 465 466 MoveTool.get_selection_box_boundaries (out selection_box_center_x, 467 out selection_box_center_y, 468 out selection_box_width, 469 out selection_box_height); 470 471 DrawingTools.move_tool.move_to_baseline (); 472 473 foreach (Path path in glyph.active_paths) { 474 path.move (0, -descender * scale); 475 } 476 477 objects_resized (selection_box_width, selection_box_height); 478 } 479 480 void get_selection_min (out double x, out double y) { 481 Glyph glyph = MainWindow.get_current_glyph (); 482 x = double.MAX; 483 y = double.MAX; 484 foreach (Path p in glyph.active_paths) { 485 if (p.xmin < x) { 486 x = p.xmin; 487 } 488 489 if (p.ymin < y) { 490 y = p.ymin; 491 } 492 } 493 } 494 495 bool can_resize (double x, double y) { 496 Glyph glyph = MainWindow.get_current_glyph (); 497 double h, w; 498 double ratio = get_resize_ratio (x, y); 499 500 foreach (Path selected_path in glyph.active_paths) { 501 h = selected_path.ymax - selected_path.ymin; 502 w = selected_path.xmax - selected_path.xmin; 503 504 if (selected_path.points.size <= 1) { 505 continue; 506 } 507 508 if (h * ratio < 1 || w * ratio < 1) { 509 return false; 510 } 511 } 512 513 return true; 514 } 515 516 bool is_over_resize_handle (Path p, double x, double y) { 517 double handle_x, handle_y; 518 get_resize_handle_position (out handle_x, out handle_y); 519 return Path.distance (handle_x, x, handle_y, y) < 12 * MainWindow.units; 520 } 521 522 bool is_over_horizontal_resize_handle (Path p, double x, double y) { 523 double handle_x, handle_y; 524 get_horizontal_reseize_handle_position (out handle_x, out handle_y); 525 return Path.distance (handle_x, x, handle_y, y) < 12 * MainWindow.units; 526 } 527 528 public void skew (double skew) { 529 Glyph glyph = MainWindow.get_current_glyph (); 530 skew_glyph (glyph, skew, last_skew, true); 531 last_skew = skew; 532 } 533 534 public void skew_glyph (Glyph glyph, double skew, double last_skew, 535 bool selected_paths) { 536 537 double dx, nx, nw, dw, x, y, w, h; 538 double s = (skew - last_skew) / 100.0; 539 540 if (!selected_paths) { 541 glyph.clear_active_paths (); 542 543 foreach (Path path in glyph.get_visible_paths ()) { 544 glyph.add_active_path (null, path); 545 } 546 } 547 548 glyph.selection_boundaries (out x, out y, out w, out h); 549 550 foreach (Path path in glyph.active_paths) { 551 SvgParser.apply_matrix (path, 1, 0, s, 1, 0, 0); 552 path.skew = skew; 553 path.update_region_boundaries (); 554 } 555 556 glyph.selection_boundaries (out nx, out y, out nw, out h); 557 558 dx = -(nx - x); 559 560 foreach (Path p in glyph.active_paths) { 561 p.move (dx, 0); 562 p.reset_stroke (); 563 } 564 565 dw = (nw - w); 566 glyph.right_limit += dw; 567 glyph.remove_lines (); 568 glyph.add_help_lines (); 569 570 if (!selected_paths) { 571 glyph.clear_active_paths (); 572 } 573 } 574 } 575 576 } 577