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.
Move the SVG parser and the SVG rendering code to libsvgbird
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 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 (SvgBird.Object 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 (SvgBird.Object path in glyph.get_visible_objects ()) { 368 glyph.add_active_object (null, path); 369 } 370 } 371 372 get_selection_min (out resize_pos_x, out resize_pos_y); 373 374 // resize paths 375 foreach (SvgBird.Object selected_path in glyph.active_paths) { 376 selected_path.resize (ratio_x, ratio_y); 377 } 378 379 // move paths relative to the updated xmin and xmax 380 get_selection_min (out selection_minx, out selection_miny); 381 dx = resize_pos_x - selection_minx; 382 dy = resize_pos_y - selection_miny; 383 384 foreach (SvgBird.Object selected_path in glyph.active_paths) { 385 selected_path.move (dx, dy); 386 } 387 388 if (glyph.active_paths.size > 0) { 389 update_selection_box (); 390 objects_resized (selection_box_width, selection_box_height); 391 } 392 393 if (!selected) { 394 double w; 395 w = (ratio_x * glyph.get_width () - glyph.get_width ()) / 2.0; 396 glyph.left_limit -= w; 397 glyph.right_limit += w; 398 glyph.clear_active_paths (); 399 glyph.remove_lines (); 400 glyph.add_help_lines (); 401 } 402 } 403 404 void update_selection_box () { 405 MoveTool.update_boundaries_for_selection (); 406 MoveTool.get_selection_box_boundaries (out selection_box_center_x, 407 out selection_box_center_y, out selection_box_width, 408 out selection_box_height); 409 } 410 411 /** Move resize handle to pixel x,y. */ 412 void resize_proportional (double px, double py) { 413 double ratio; 414 415 ratio = get_resize_ratio (px, py); 416 417 if (ratio != 1) { 418 resize_selected_paths (ratio, ratio); 419 last_resize_x = px; 420 last_resize_y = py; 421 } 422 } 423 424 /** Move resize handle to pixel x,y. */ 425 void resize_horizontal (double px, double py) { 426 double ratio, x, y, w, h; 427 428 Glyph glyph = MainWindow.get_current_glyph (); 429 glyph.selection_boundaries (out x, out y, out w, out h); 430 431 ratio = 1 + (Glyph.path_coordinate_x (px) 432 - Glyph.path_coordinate_x (last_resize_x)) / w; 433 434 if (ratio != 1) { 435 resize_selected_paths (ratio, 1); 436 last_resize_x = px; 437 last_resize_y = py; 438 } 439 } 440 441 public void full_height () { 442 double xc, yc, w, h; 443 Glyph glyph = MainWindow.get_current_glyph (); 444 Font font = BirdFont.get_current_font (); 445 446 MoveTool.update_boundaries_for_selection (); 447 MoveTool.get_selection_box_boundaries (out xc, out yc, out w, out h); 448 449 //compute scale 450 double descender = font.base_line - (yc - h / 2); 451 452 if (descender < 0) { 453 descender = 0; 454 } 455 456 double font_height = font.top_position - font.base_line; 457 double scale = font_height / (h - descender); 458 459 resize_selected_paths (scale, scale); 460 PenTool.reset_stroke (); 461 462 MoveTool.update_boundaries_for_selection (); 463 font.touch (); 464 465 MoveTool.get_selection_box_boundaries (out selection_box_center_x, 466 out selection_box_center_y, 467 out selection_box_width, 468 out selection_box_height); 469 470 DrawingTools.move_tool.move_to_baseline (); 471 472 foreach (SvgBird.Object path in glyph.active_paths) { 473 path.move (0, -descender * scale); 474 } 475 476 objects_resized (selection_box_width, selection_box_height); 477 } 478 479 void get_selection_min (out double x, out double y) { 480 Glyph glyph = MainWindow.get_current_glyph (); 481 x = double.MAX; 482 y = double.MAX; 483 foreach (SvgBird.Object p in glyph.active_paths) { 484 if (p.xmin < x) { 485 x = p.xmin; 486 } 487 488 if (p.ymin < y) { 489 y = p.ymin; 490 } 491 } 492 } 493 494 bool can_resize (double x, double y) { 495 Glyph glyph = MainWindow.get_current_glyph (); 496 double h, w; 497 double ratio = get_resize_ratio (x, y); 498 499 foreach (SvgBird.Object selected_path in glyph.active_paths) { 500 h = selected_path.ymax - selected_path.ymin; 501 w = selected_path.xmax - selected_path.xmin; 502 503 if (selected_path.is_empty ()) { // FIXME: test with one point 504 continue; 505 } 506 507 if (h * ratio < 1 || w * ratio < 1) { 508 return false; 509 } 510 } 511 512 return true; 513 } 514 515 bool is_over_resize_handle (SvgBird.Object p, double x, double y) { 516 double handle_x, handle_y; 517 get_resize_handle_position (out handle_x, out handle_y); 518 return Path.distance (handle_x, x, handle_y, y) < 12 * MainWindow.units; 519 } 520 521 bool is_over_horizontal_resize_handle (SvgBird.Object p, double x, double y) { 522 double handle_x, handle_y; 523 get_horizontal_reseize_handle_position (out handle_x, out handle_y); 524 return Path.distance (handle_x, x, handle_y, y) < 12 * MainWindow.units; 525 } 526 527 public void skew (double skew) { 528 Glyph glyph = MainWindow.get_current_glyph (); 529 skew_glyph (glyph, skew, last_skew, true); 530 last_skew = skew; 531 } 532 533 public void skew_glyph (Glyph glyph, double skew, double last_skew, 534 bool selected_paths) { 535 536 double dx, nx, nw, dw, x, y, w, h; 537 double s = (skew - last_skew) / 100.0; 538 539 if (!selected_paths) { 540 glyph.clear_active_paths (); 541 542 foreach (SvgBird.Object path in glyph.get_visible_objects ()) { 543 glyph.add_active_object (null, path); 544 } 545 } 546 547 glyph.selection_boundaries (out x, out y, out w, out h); 548 549 foreach (SvgBird.Object path in glyph.active_paths) { 550 if (path is PathObject) { // FIXME: other objects 551 Path p = ((PathObject) path).get_path (); 552 SvgParser.apply_matrix (p, 1, 0, s, 1, 0, 0); 553 p.skew = skew; 554 path.update_region_boundaries (); 555 } 556 } 557 558 glyph.selection_boundaries (out nx, out y, out nw, out h); 559 560 dx = -(nx - x); 561 562 foreach (SvgBird.Object p in glyph.active_paths) { 563 p.move (dx, 0); 564 } 565 566 dw = (nw - w); 567 glyph.right_limit += dw; 568 glyph.remove_lines (); 569 glyph.add_help_lines (); 570 571 if (!selected_paths) { 572 glyph.clear_active_paths (); 573 } 574 } 575 } 576 577 } 578