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.
Rotate paths
1 /* 2 Copyright (C) 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 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 static double selection_box_left = 0; 36 static double selection_box_top = 0; 37 38 static bool rotate_path = false; 39 static double last_rotate_y; 40 static double rotation = 0; 41 static double last_rotate = 0; 42 43 public double last_skew = 0; 44 45 public signal void objects_rotated (double angle); 46 public signal void objects_resized (double width, double height); 47 48 Text proportional_handle; 49 Text horizontal_handle; 50 51 public ResizeTool (string n) { 52 base (n, t_("Resize and rotate paths")); 53 54 proportional_handle = new Text ("resize_handle", 60); 55 proportional_handle.load_font ("icons.bf"); 56 Theme.text_color (proportional_handle, "Highlighted 1"); 57 58 horizontal_handle = new Text ("resize_handle_horizontal", 60); 59 horizontal_handle.load_font ("icons.bf"); 60 Theme.text_color (horizontal_handle, "Highlighted 1"); 61 62 select_action.connect((self) => { 63 }); 64 65 deselect_action.connect((self) => { 66 }); 67 68 press_action.connect((self, b, x, y) => { 69 SvgBird.Object last_path; 70 Glyph glyph; 71 72 glyph = MainWindow.get_current_glyph (); 73 glyph.store_undo_state (); 74 75 foreach (SvgBird.Object p in glyph.active_paths) { 76 if (is_over_resize_handle (p, x, y)) { 77 resize_path_proportional = true; 78 resized_path = p; 79 last_resize_x = x; 80 last_resize_y = y; 81 return; 82 } 83 84 if (is_over_horizontal_resize_handle (p, x, y)) { 85 resize_width = true; 86 resized_path = p; 87 last_resize_x = x; 88 last_resize_y = y; 89 return; 90 } 91 } 92 93 foreach (SvgBird.Object p in glyph.active_paths) { 94 if (is_over_rotate_handle (p, x, y)) { 95 rotate_path = true; 96 return; 97 } 98 } 99 100 if (glyph.active_paths.size > 0) { 101 last_path = glyph.active_paths.get (glyph.active_paths.size - 1); 102 last_rotate = last_path.transforms.rotation; 103 } 104 105 rotation = last_rotate; 106 last_resize_x = x; 107 last_rotate_y = y; 108 109 if (!resize_path_proportional && !resize_width && !rotate_path) { 110 DrawingTools.move_tool.press (b, x, y); 111 } 112 113 move_paths = true; 114 115 update_selection_box (); 116 }); 117 118 release_action.connect((self, b, x, y) => { 119 resize_path_proportional = false; 120 resize_width = false; 121 rotate_path = false; 122 move_paths = false; 123 DrawingTools.move_tool.release (b, x, y); 124 update_selection_box (); 125 GlyphCanvas.redraw (); 126 127 foreach (SvgBird.Object p in MainWindow.get_current_glyph ().active_paths) { 128 if (p is PathObject) { 129 PathObject path = (PathObject) p; 130 path.get_path ().create_full_stroke (); 131 } 132 133 p.transforms.collapse_transforms (); 134 } 135 }); 136 137 move_action.connect ((self, x, y) => { 138 Glyph glyph; 139 140 if (resize_path_proportional && can_resize (x, y)) { 141 resize_proportional (x, y); 142 update_selection_box (); 143 } 144 145 if (resize_width && can_resize (x, y)) { 146 resize_horizontal (x, y); 147 update_selection_box (); 148 } 149 150 if (rotate_path) { 151 rotate (x, y); 152 } 153 154 if (move_paths 155 || rotate_path 156 || resize_path_proportional 157 || resize_width) { 158 159 glyph = MainWindow.get_current_glyph (); 160 161 GlyphCanvas.redraw (); 162 } 163 164 DrawingTools.move_tool.move (x, y); 165 }); 166 167 draw_action.connect ((self, cr, glyph) => { 168 Text handle; 169 Glyph g = MainWindow.get_current_glyph (); 170 171 if (!move_paths) { 172 if (!rotate_path) { 173 if (!resize_width) { 174 handle = proportional_handle; 175 get_resize_handle_position (out handle.widget_x, out handle.widget_y); 176 177 handle.widget_x -= handle.get_sidebearing_extent () / 2; 178 handle.widget_y -= handle.get_height () / 2; 179 180 handle.draw (cr); 181 } 182 183 if (!resize_path_proportional) { 184 handle = horizontal_handle; 185 186 get_horizontal_reseize_handle_position (out handle.widget_x, 187 out handle.widget_y); 188 189 handle.widget_x -= handle.get_sidebearing_extent () / 2; 190 handle.widget_y -= handle.get_height () / 2; 191 192 handle.draw (cr); 193 } 194 } 195 196 if (!resize_path_proportional && !resize_width 197 && g.active_paths.size > 0) { 198 199 draw_rotate_handle (cr); 200 } 201 } 202 203 MoveTool.draw_actions (cr); 204 }); 205 206 key_press_action.connect ((self, keyval) => { 207 DrawingTools.move_tool.key_down (keyval); 208 }); 209 } 210 211 public static void get_resize_handle_position (out double px, out double py) { 212 px = Glyph.reverse_path_coordinate_x (selection_box_center_x + selection_box_width / 2); 213 py = Glyph.reverse_path_coordinate_y (selection_box_center_y + selection_box_height / 2); 214 } 215 216 public static void get_horizontal_reseize_handle_position (out double px, out double py) { 217 px = Glyph.reverse_path_coordinate_x (selection_box_center_x + selection_box_width / 2); 218 px += 40; 219 py = Glyph.reverse_path_coordinate_y (selection_box_center_y); 220 } 221 222 public static double get_rotated_handle_length () { 223 double s, hx, hy; 224 double d; 225 226 s = fmin (selection_box_width, selection_box_height) * 1.1; 227 d = (s / Glyph.ivz ()) / 2; 228 229 hx = cos (rotation) * d; 230 hy = sin (rotation) * d; 231 232 return d; 233 } 234 235 public void signal_objects_rotated () { 236 objects_rotated (rotation * (180 / PI)); 237 } 238 239 public void rotate_selected_paths (double angle, double cx, double cy) { 240 Glyph glyph = MainWindow.get_current_glyph (); 241 SvgBird.Object last_path; 242 double x, y; 243 244 glyph.layers.update_boundaries_for_object (); 245 246 foreach (SvgBird.Object p in glyph.active_paths) { 247 if (p is EmbeddedSvg) { 248 EmbeddedSvg svg = (EmbeddedSvg) p; 249 x = selection_box_left - svg.x + selection_box_width / 2; 250 y = selection_box_top + svg.y + selection_box_height / 2; 251 p.transforms.rotate (angle, x, y); 252 } else { 253 x = selection_box_left + selection_box_width / 2; 254 y = selection_box_top + selection_box_height / 2; 255 p.transforms.rotate (angle, x, y); 256 } 257 } 258 259 last_rotate = rotation; 260 261 if (glyph.active_paths.size > 0) { 262 last_path = glyph.active_paths.get (glyph.active_paths.size - 1); 263 264 if (rotation > PI) { 265 rotation -= 2 * PI; 266 } 267 268 if (last_rotate > PI) { 269 last_rotate -= 2 * PI; 270 } 271 272 signal_objects_rotated (); 273 } 274 } 275 276 /** Move rotate handle to pixel x,y. */ 277 void rotate (double x, double y) { 278 double a, b; 279 280 a = Glyph.path_coordinate_x (x) - selection_box_center_x; 281 b = selection_box_center_y - Glyph.path_coordinate_y (y); 282 283 rotation = atan2 (b, a); 284 285 if (a == 0) { 286 rotation = b > 0 ? PI / 2 : -PI / 2; 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 (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.transforms.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 public static 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, out selection_box_left, out selection_box_top); 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 void get_selection_min (out double x, out double y) { 444 Glyph glyph = MainWindow.get_current_glyph (); 445 x = double.MAX; 446 y = double.MAX; 447 foreach (SvgBird.Object p in glyph.active_paths) { 448 if (p.xmin < x) { 449 x = p.xmin; 450 } 451 452 if (p.ymin < y) { 453 y = p.ymin; 454 } 455 } 456 } 457 458 bool can_resize (double x, double y) { 459 Glyph glyph = MainWindow.get_current_glyph (); 460 double h, w; 461 double ratio = get_resize_ratio (x, y); 462 463 foreach (SvgBird.Object selected_path in glyph.active_paths) { 464 h = selected_path.ymax - selected_path.ymin; 465 w = selected_path.xmax - selected_path.xmin; 466 467 if (selected_path.is_empty ()) { // FIXME: test with one point 468 continue; 469 } 470 471 if (h * ratio < 1 || w * ratio < 1) { 472 return false; 473 } 474 } 475 476 return true; 477 } 478 479 bool is_over_resize_handle (SvgBird.Object p, double x, double y) { 480 double handle_x, handle_y; 481 get_resize_handle_position (out handle_x, out handle_y); 482 return Path.distance (handle_x, x, handle_y, y) < 12 * MainWindow.units; 483 } 484 485 bool is_over_horizontal_resize_handle (SvgBird.Object p, double x, double y) { 486 double handle_x, handle_y; 487 get_horizontal_reseize_handle_position (out handle_x, out handle_y); 488 return Path.distance (handle_x, x, handle_y, y) < 12 * MainWindow.units; 489 } 490 491 public void skew (double skew) { 492 Glyph glyph = MainWindow.get_current_glyph (); 493 skew_glyph (glyph, skew, last_skew, true); 494 last_skew = skew; 495 } 496 497 public void skew_glyph (Glyph glyph, double skew, double last_skew, 498 bool selected_paths) { 499 500 double dx, nx, nw, dw, x, y, w, h; 501 double s = (skew - last_skew) / 100.0; 502 503 if (!selected_paths) { 504 glyph.clear_active_paths (); 505 506 foreach (SvgBird.Object path in glyph.get_visible_objects ()) { 507 glyph.add_active_object (path); 508 } 509 } 510 511 glyph.selection_boundaries (out x, out y, out w, out h); 512 513 foreach (SvgBird.Object path in glyph.active_paths) { 514 if (path is PathObject) { // FIXME: other objects 515 Path p = ((PathObject) path).get_path (); 516 SvgParser.apply_matrix (p, 1, 0, s, 1, 0, 0); 517 p.skew = skew; 518 path.update_boundaries_for_object (); 519 } 520 } 521 522 glyph.selection_boundaries (out nx, out y, out nw, out h); 523 524 dx = -(nx - x); 525 526 foreach (SvgBird.Object p in glyph.active_paths) { 527 p.move (dx, 0); 528 } 529 530 dw = (nw - w); 531 glyph.right_limit += dw; 532 glyph.remove_lines (); 533 glyph.add_help_lines (); 534 535 if (!selected_paths) { 536 glyph.clear_active_paths (); 537 } 538 } 539 } 540 541 } 542