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