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.
Resize 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 object in MainWindow.get_current_glyph ().active_paths) { 128 if (object is PathObject) { 129 PathObject path = (PathObject) object; 130 Path p = path.get_path (); 131 p.create_full_stroke (); 132 } else { 133 object.transforms.collapse_transforms (); 134 } 135 } 136 }); 137 138 move_action.connect ((self, x, y) => { 139 Glyph glyph; 140 141 if (resize_path_proportional && can_resize (x, y)) { 142 resize_proportional (x, y); 143 } 144 145 if (resize_width && can_resize (x, y)) { 146 resize_horizontal (x, y); 147 } 148 149 if (rotate_path) { 150 rotate (x, y); 151 } 152 153 if (move_paths 154 || rotate_path 155 || resize_path_proportional 156 || resize_width) { 157 158 glyph = MainWindow.get_current_glyph (); 159 GlyphCanvas.redraw (); 160 } 161 162 DrawingTools.move_tool.move (x, y); 163 }); 164 165 draw_action.connect ((self, cr, glyph) => { 166 Text handle; 167 Glyph g = MainWindow.get_current_glyph (); 168 169 if (!move_paths) { 170 if (!rotate_path) { 171 if (!resize_width) { 172 handle = proportional_handle; 173 get_resize_handle_position (out handle.widget_x, out handle.widget_y); 174 175 handle.widget_x -= handle.get_sidebearing_extent () / 2; 176 handle.widget_y -= handle.get_height () / 2; 177 178 handle.draw (cr); 179 } 180 181 if (!resize_path_proportional) { 182 handle = horizontal_handle; 183 184 get_horizontal_reseize_handle_position (out handle.widget_x, 185 out handle.widget_y); 186 187 handle.widget_x -= handle.get_sidebearing_extent () / 2; 188 handle.widget_y -= handle.get_height () / 2; 189 190 handle.draw (cr); 191 } 192 } 193 194 if (!resize_path_proportional && !resize_width 195 && g.active_paths.size > 0) { 196 197 draw_rotate_handle (cr); 198 } 199 } 200 201 MoveTool.draw_actions (cr); 202 }); 203 204 key_press_action.connect ((self, keyval) => { 205 DrawingTools.move_tool.key_down (keyval); 206 }); 207 } 208 209 public static void get_resize_handle_position (out double px, out double py) { 210 px = Glyph.reverse_path_coordinate_x (selection_box_center_x + selection_box_width / 2); 211 py = Glyph.reverse_path_coordinate_y (selection_box_center_y + selection_box_height / 2); 212 } 213 214 public static void get_horizontal_reseize_handle_position (out double px, out double py) { 215 px = Glyph.reverse_path_coordinate_x (selection_box_center_x + selection_box_width / 2); 216 px += 40; 217 py = Glyph.reverse_path_coordinate_y (selection_box_center_y); 218 } 219 220 public static double get_rotated_handle_length () { 221 double s, hx, hy; 222 double d; 223 224 s = fmin (selection_box_width, selection_box_height) * 1.1; 225 d = (s / Glyph.ivz ()) / 2; 226 227 hx = cos (rotation) * d; 228 hy = sin (rotation) * d; 229 230 return d; 231 } 232 233 public void signal_objects_rotated () { 234 objects_rotated (rotation * (180 / PI)); 235 } 236 237 public void rotate_selected_paths (double angle, double cx, double cy) { 238 Glyph glyph = MainWindow.get_current_glyph (); 239 SvgBird.Object last_path; 240 double x, y; 241 242 glyph.layers.update_boundaries_for_object (); 243 244 foreach (SvgBird.Object p in glyph.active_paths) { 245 if (p is EmbeddedSvg) { 246 EmbeddedSvg svg = (EmbeddedSvg) p; 247 x = selection_box_left - svg.x + selection_box_width / 2; 248 y = selection_box_top + svg.y + selection_box_height / 2; 249 p.transforms.rotate (angle, x, y); 250 } else if (p is PathObject) { 251 Path path = ((PathObject) p).get_path (); 252 SvgTransforms transform = new SvgTransforms (); 253 transform.rotate (-angle, selection_box_center_x, selection_box_center_y); 254 Matrix matrix = transform.get_matrix (); 255 path.transform (matrix); 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 if (!selected) { 363 glyph.clear_active_paths (); 364 365 foreach (SvgBird.Object path in glyph.get_visible_objects ()) { 366 glyph.add_active_object (path); 367 } 368 } 369 370 foreach (SvgBird.Object p in glyph.active_paths) { 371 if (p is EmbeddedSvg) { 372 EmbeddedSvg svg = (EmbeddedSvg) p; 373 x = selection_box_left - svg.x; 374 y = selection_box_top + svg.y + selection_box_height; 375 p.transforms.resize (ratio_x, ratio_y, x, y); 376 } else if (p is PathObject) { 377 Path path = ((PathObject) p).get_path (); 378 x = selection_box_center_x - selection_box_width / 2; 379 y = selection_box_center_y - selection_box_height / 2; 380 SvgTransforms transform = new SvgTransforms (); 381 transform.resize (ratio_x, ratio_y, x, y); 382 Matrix matrix = transform.get_matrix (); 383 path.transform (matrix); 384 } 385 } 386 387 if (glyph.active_paths.size > 0) { 388 objects_resized (selection_box_width, selection_box_height); 389 } 390 391 if (!selected) { 392 double w; 393 w = (ratio_x * glyph.get_width () - glyph.get_width ()) / 2.0; 394 glyph.left_limit -= w; 395 glyph.right_limit += w; 396 glyph.clear_active_paths (); 397 glyph.remove_lines (); 398 glyph.add_help_lines (); 399 } 400 401 update_selection_box (); 402 } 403 404 public static 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, out selection_box_left, out selection_box_top); 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 bool can_resize (double x, double y) { 442 Glyph glyph = MainWindow.get_current_glyph (); 443 double h, w; 444 double ratio = get_resize_ratio (x, y); 445 446 foreach (SvgBird.Object selected_path in glyph.active_paths) { 447 h = selected_path.ymax - selected_path.ymin; 448 w = selected_path.xmax - selected_path.xmin; 449 450 if (selected_path.is_empty ()) { // FIXME: test with one point 451 continue; 452 } 453 454 if (h * ratio < 1 || w * ratio < 1) { 455 return false; 456 } 457 } 458 459 return true; 460 } 461 462 bool is_over_resize_handle (SvgBird.Object p, double x, double y) { 463 double handle_x, handle_y; 464 get_resize_handle_position (out handle_x, out handle_y); 465 return Path.distance (handle_x, x, handle_y, y) < 12 * MainWindow.units; 466 } 467 468 bool is_over_horizontal_resize_handle (SvgBird.Object p, double x, double y) { 469 double handle_x, handle_y; 470 get_horizontal_reseize_handle_position (out handle_x, out handle_y); 471 return Path.distance (handle_x, x, handle_y, y) < 12 * MainWindow.units; 472 } 473 474 public void skew (double skew) { 475 Glyph glyph = MainWindow.get_current_glyph (); 476 skew_glyph (glyph, skew, last_skew, true); 477 last_skew = skew; 478 } 479 480 public void skew_glyph (Glyph glyph, double skew, double last_skew, 481 bool selected_paths) { 482 483 double dx, nx, nw, dw, x, y, w, h; 484 double s = (skew - last_skew) / 100.0; 485 486 if (!selected_paths) { 487 glyph.clear_active_paths (); 488 489 foreach (SvgBird.Object path in glyph.get_visible_objects ()) { 490 glyph.add_active_object (path); 491 } 492 } 493 494 glyph.selection_boundaries (out x, out y, out w, out h); 495 496 foreach (SvgBird.Object path in glyph.active_paths) { 497 if (path is PathObject) { // FIXME: other objects 498 Path p = ((PathObject) path).get_path (); 499 SvgParser.apply_matrix (p, 1, 0, s, 1, 0, 0); 500 p.skew = skew; 501 path.update_boundaries_for_object (); 502 } 503 } 504 505 glyph.selection_boundaries (out nx, out y, out nw, out h); 506 507 dx = -(nx - x); 508 509 foreach (SvgBird.Object p in glyph.active_paths) { 510 p.move (dx, 0); 511 } 512 513 dw = (nw - w); 514 glyph.right_limit += dw; 515 glyph.remove_lines (); 516 glyph.add_help_lines (); 517 518 if (!selected_paths) { 519 glyph.clear_active_paths (); 520 } 521 } 522 } 523 524 } 525