The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

BackgroundImage.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/BackgroundImage.vala.
Select all glyphs in overview tab
1 /* 2 Copyright (C) 2012, 2013, 2014 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 Cairo; 16 using Math; 17 18 namespace BirdFont { 19 20 public class BackgroundImage { 21 22 public string name = ""; 23 public Gee.ArrayList<BackgroundSelection> selections; 24 25 /** Image position in canvas coordinates. */ 26 public double img_x = 0; 27 public double img_y = 0; 28 29 public double img_scale_x = 1; 30 public double img_scale_y = 1; 31 32 public double img_rotation = 0; 33 private int size = -1; 34 35 public int active_handle = -1; 36 public int selected_handle = -1; 37 38 private ImageSurface? contrast_image = null; 39 private ImageSurface? background_image = null; 40 private ImageSurface? original_image = null; 41 42 private string path; 43 44 public bool high_contrast = false; 45 private Gee.ArrayList<TracedPoint> points = new Gee.ArrayList<TracedPoint> (); 46 private Gee.ArrayList<TracedPoint> start_points = new Gee.ArrayList<TracedPoint> (); 47 48 public signal void updated (); 49 50 public double img_offset_x { 51 get { return img_x + Glyph.xc (); } 52 set { img_x = value - Glyph.xc (); } 53 } 54 55 public double img_offset_y { 56 get { return Glyph.yc () - img_y; } 57 set { img_y = Glyph.yc () - value; } 58 } 59 60 public int size_margin { 61 get { 62 if (unlikely (size == -1)) { 63 size = (int) (Math.sqrt (Math.pow (get_img ().get_height (), 2) 64 + Math.pow (get_img ().get_width (), 2)) + 0.5); 65 } 66 67 return size; 68 } 69 } 70 71 public int margin_left { 72 get { 73 return size_margin - get_img ().get_width (); 74 } 75 } 76 77 public int margin_top { 78 get { 79 return size_margin - get_img ().get_height (); 80 } 81 } 82 83 public double img_middle_x { 84 get { return img_x + (size_margin * img_scale_x) / 2; } 85 set { img_x = value - (size_margin * img_scale_x) / 2; } 86 } 87 88 public double img_middle_y { 89 get { return img_y - (size_margin * img_scale_y) / 2; } 90 set { img_y = value + (size_margin * img_scale_y) / 2; } 91 } 92 93 public BackgroundImage (string file_name) { 94 path = file_name; 95 selections = new Gee.ArrayList<BackgroundSelection> (); 96 } 97 98 public BackgroundImage copy () { 99 BackgroundImage bg = new BackgroundImage (path); 100 101 bg.img_x = img_x; 102 bg.img_y = img_y; 103 104 bg.img_scale_x = img_scale_x; 105 bg.img_scale_y = img_scale_y; 106 bg.img_rotation = img_rotation; 107 108 bg.high_contrast = high_contrast; 109 110 foreach (BackgroundSelection b in selections) { 111 bg.selections.add (b); 112 } 113 114 return bg; 115 } 116 117 public void add_selection (BackgroundSelection bs) { 118 selections.add (bs); 119 } 120 121 public void set_high_contrast (bool t) { 122 high_contrast = t; 123 } 124 125 public double get_margin_width () { 126 return ((size_margin - get_img ().get_width ()) / 2.0); 127 } 128 129 public double get_margin_height () { 130 return ((size_margin - get_img ().get_height ()) / 2.0); 131 } 132 133 public void set_img_offset (double x, double y) { 134 img_offset_x = x; 135 img_offset_y = y; 136 } 137 138 public void set_position (double coordinate_x, double coordinate_y) { 139 img_x = coordinate_x; 140 img_y = coordinate_y; 141 } 142 143 public ImageSurface get_img () { 144 if (!path.has_suffix (".png")) { 145 create_png (); 146 } 147 148 if (background_image == null) { 149 background_image = new ImageSurface.from_png (path); 150 original_image = new ImageSurface.from_png (path); 151 } 152 153 return (!) background_image; 154 } 155 156 public ImageSurface get_original () { 157 if (!path.has_suffix (".png")) { 158 create_png (); 159 } 160 161 if (background_image == null) { 162 background_image = new ImageSurface.from_png (path); 163 original_image = new ImageSurface.from_png (path); 164 } 165 166 return (!) original_image; 167 } 168 169 public bool is_valid () { 170 FileInfo file_info; 171 File file = File.new_for_path (path); 172 173 if (!file.query_exists ()) { 174 return false; 175 } 176 177 try { 178 file_info = file.query_info ("*", FileQueryInfoFlags.NONE); 179 180 if (file_info.get_size () == 0) { 181 return false; 182 } 183 } catch (GLib.Error e) { 184 warning (e.message); 185 return false; 186 } 187 188 return true; 189 } 190 191 public string get_png_base64 () { 192 try { 193 File file = File.new_for_path (path); 194 FileInfo file_info = file.query_info ("*", FileQueryInfoFlags.NONE); 195 uint8[] buffer = new uint8[file_info.get_size ()]; 196 FileInputStream file_stream; 197 DataInputStream png_stream; 198 199 if (!file.query_exists ()) { 200 warning (@"Can't to save image $path, file does not exist."); 201 return ""; 202 } 203 204 if (is_null (buffer)) { 205 warning (@"Can not allocate a buffer of $(file_info.get_size ()) bytes to store $path."); 206 return ""; 207 } 208 209 file_stream = file.read (); 210 211 png_stream = new DataInputStream (file_stream); 212 png_stream.read (buffer); 213 214 return Base64.encode (buffer); 215 } catch (GLib.Error e) { 216 warning (e.message); 217 } 218 219 return ""; 220 221 } 222 223 public void create_background_folders (Font font) { 224 File dir; 225 226 dir = BirdFont.get_settings_directory (); 227 if (!dir.query_exists ()) { 228 DirUtils.create ((!) dir.get_path (), 0755); 229 } 230 231 dir = font.get_backgrounds_folder (); 232 if (!dir.query_exists ()) { 233 DirUtils.create ((!) dir.get_path (), 0755); 234 } 235 236 dir = get_child (font.get_backgrounds_folder (), "parts"); 237 if (!dir.query_exists ()) { 238 DirUtils.create ((!) dir.get_path (), 0755); 239 } 240 } 241 242 public void copy_if_new (File destination) { 243 if (!destination.query_exists ()) { 244 copy_file (destination); 245 } 246 } 247 248 public void copy_file (File destination) { 249 File source; 250 FileInfo info; 251 252 try { 253 if (destination.query_exists ()) { 254 info = destination.query_info ("standard::*", FileQueryInfoFlags.NONE); 255 if (info.get_file_type () == FileType.DIRECTORY) { 256 warning (@"$((!) destination.get_path ()) is a directory."); 257 return; 258 } 259 } 260 261 if (!((!)destination.get_parent ()).query_exists ()) { 262 warning (@"Directory for file $((!) destination.get_path ()) is not created."); 263 return; 264 } 265 266 if (destination.query_exists ()) { 267 warning (@"Image $((!) destination.get_path ()) is already created."); 268 return; 269 } 270 271 source = File.new_for_path (path); 272 source.copy (destination, FileCopyFlags.NONE); 273 } catch (GLib.Error e) { 274 warning (e.message); 275 } 276 } 277 278 public string get_sha1 () { 279 try { 280 File file = File.new_for_path (path); 281 FileInfo file_info; 282 uint8[] buffer; 283 FileInputStream file_stream; 284 DataInputStream png_stream; 285 286 if (!file.query_exists ()) { 287 warning (@"Can't save $path file does not exist."); 288 return ""; 289 } 290 291 file_info = file.query_info ("*", FileQueryInfoFlags.NONE); 292 293 if (file_info.get_size () == 0) { 294 warning (@"length of image $path is zero"); 295 return ""; 296 } 297 298 buffer = new uint8[file_info.get_size ()]; 299 file_stream = file.read (); 300 png_stream = new DataInputStream (file_stream); 301 302 png_stream.read (buffer); 303 304 return Checksum.compute_for_data (ChecksumType.SHA1, buffer); 305 } catch (GLib.Error e) { 306 warning (e.message); 307 } 308 309 return ""; 310 } 311 312 private void create_png () { 313 string file_name = @"$path.png"; 314 Font font = BirdFont.get_current_font (); 315 File folder = font.get_backgrounds_folder (); 316 File original = File.new_for_path (file_name); 317 File png_image = get_child (folder, @"full_$((!)original.get_basename ())"); 318 bool converted; 319 320 if (png_image.query_exists ()) { 321 path = (!) png_image.get_path (); 322 return; 323 } 324 325 if (is_null (path)) { 326 warning ("Background image path is null."); 327 return; 328 } 329 330 converted = MainWindow.native_window.convert_to_png (path.dup (), ((!) png_image.get_path ()).dup ()); 331 332 if (!converted) { 333 warning ("Failed to convert image: $(path)"); 334 return; 335 } 336 337 path = (!) png_image.get_path (); 338 } 339 340 public void set_img_rotation_from_coordinate (double x, double y) { 341 double bcx, bcy; 342 double a, b, c, length; 343 344 bcx = img_middle_x; 345 bcy = img_middle_y; 346 347 a = bcx - x; 348 b = bcy - y; 349 c = a * a + b * b; 350 351 if (c == 0) { 352 return; 353 } 354 355 length = sqrt (fabs (c)); 356 357 if (c < 0) { 358 length = -length; 359 } 360 361 img_rotation = (y > bcy) ? acos (a / length) + PI : -acos (a / length) + PI; 362 363 background_image = null; 364 } 365 366 public void set_img_scale (double xs, double ys) { 367 img_scale_x = xs; 368 img_scale_y = ys; 369 } 370 371 public void reset_scale (Glyph g) { 372 double w, h; 373 374 w = g.get_width (); 375 h = g.get_height (); 376 377 img_scale_x = 1; 378 img_scale_y = 1; 379 380 img_offset_x = g.get_line ("left").pos; 381 img_offset_y = g.get_line ("top").pos; 382 } 383 384 public void draw (Context cr, WidgetAllocation allocation, double view_offset_x, double view_offset_y, double view_zoom) { 385 double scale_x, scale_y; 386 double image_scale_x, image_scale_y; 387 ImageSurface s; 388 Surface st; 389 Context ct; 390 ImageSurface rotated_image; 391 392 if (high_contrast && contrast_image == null) { 393 contrast_image = get_contrast_image (); 394 } 395 396 if (unlikely (get_img ().status () != Cairo.Status.SUCCESS)) { 397 warning (@"Background image is invalid. (\"$path\")\n"); 398 MainWindow.get_current_glyph ().set_background_visible (false); 399 return; 400 } 401 402 rotated_image = (ImageSurface) get_rotated_image (); 403 404 if (contrast_image == null) { 405 s = rotated_image; 406 image_scale_x = img_scale_x; 407 image_scale_y = img_scale_y; 408 } else { 409 s = (!) contrast_image; 410 image_scale_x = img_scale_x * ((double) rotated_image.get_width () / s.get_width ()); 411 image_scale_y = img_scale_y * ((double) rotated_image.get_height () / s.get_height ()); 412 } 413 414 st = new Surface.similar (s, s.get_content (), allocation.width, allocation.height); 415 ct = new Context (st); 416 ct.save (); 417 418 ct.set_source_rgba (1, 1, 1, 1); 419 ct.rectangle (0, 0, allocation.width, allocation.height); 420 ct.fill (); 421 422 // scale both canvas and image at the same time 423 scale_x = view_zoom * image_scale_x; 424 scale_y = view_zoom * image_scale_y; 425 426 ct.scale (scale_x, scale_y); 427 ct.translate (-view_offset_x / image_scale_x, -view_offset_y / image_scale_y); 428 429 ct.set_source_surface (s, img_offset_x / image_scale_x, img_offset_y / image_scale_y); 430 431 ct.paint (); 432 ct.restore (); 433 434 // add it 435 cr.save (); 436 cr.set_source_surface (st, 0, 0); 437 cr.paint (); 438 cr.restore (); 439 } 440 441 public Surface get_rotated_image () { 442 double x, y; 443 double iw, ih; 444 int h, w; 445 double oy, ox; 446 447 Surface o; 448 449 Surface s; 450 Context c; 451 452 Surface sg; 453 Context cg; 454 455 double wc, hc; 456 457 o = get_original (); 458 459 // add margin 460 sg = new Surface.similar (o, o.get_content (), size_margin, size_margin); 461 cg = new Context (sg); 462 463 wc = get_margin_width (); 464 hc = get_margin_height (); 465 466 Theme.color (cg, "Background 1"); 467 cg.rectangle (0, 0, size_margin, size_margin); 468 cg.fill (); 469 470 cg.set_source_surface (get_img (), wc, hc); 471 cg.paint (); 472 473 x = Glyph.reverse_path_coordinate_x (img_offset_x); 474 y = Glyph.reverse_path_coordinate_y (img_offset_y); 475 476 ih = (int) get_img ().get_height (); 477 iw = (int) get_img ().get_width (); 478 479 w = (int) iw; 480 h = (int) ih; 481 482 oy = size_margin; 483 ox = size_margin; 484 485 // rotate image 486 s = new Surface.similar (sg, sg.get_content (), (int) (ox), (int) (oy)); 487 c = new Context (s); 488 489 c.save (); 490 491 Theme.color (c, "Background 1"); 492 c.rectangle (0, 0, size_margin, size_margin); 493 c.fill (); 494 495 c.translate (size_margin * 0.5, size_margin * 0.5); 496 c.rotate (img_rotation); 497 c.translate (-size_margin * 0.5, -size_margin * 0.5); 498 499 c.set_source_surface (cg.get_target (), 0, 0); 500 c.paint (); 501 c.restore (); 502 503 return s; 504 } 505 506 public void handler_release (double nx, double ny) { 507 selected_handle = 0; 508 handler_move (nx, ny); 509 } 510 511 public void handler_press (double nx, double ny) { 512 if (is_over_rotate (nx, ny)) { 513 selected_handle = 2; 514 } else if (is_over_resize (nx, ny)) { 515 selected_handle = 1; 516 } else { 517 selected_handle = 0; 518 } 519 } 520 521 bool is_over_rotate (double nx, double ny) { 522 double x, y, d; 523 524 x = Glyph.reverse_path_coordinate_x (img_middle_x); 525 y = Glyph.reverse_path_coordinate_y (img_middle_y); 526 527 x += cos (img_rotation) * 75; 528 y += sin (img_rotation) * 75; 529 530 d = Path.distance (x, nx, y, ny); 531 532 return d < 15 * MainWindow.units; 533 } 534 535 bool is_over_resize (double nx, double ny) { 536 double x, y, size; 537 bool inx, iny; 538 539 size = 12 * MainWindow.units; 540 541 x = img_middle_x - (img_scale_x * get_img ().get_width () / 2); 542 y = img_middle_y - (img_scale_y * get_img ().get_height () / 2); 543 544 x = Glyph.reverse_path_coordinate_x (x); 545 y = Glyph.reverse_path_coordinate_y (y); 546 547 inx = x - size <= nx <= x + size; 548 iny = y - size <= ny <= y + size; 549 550 return inx && iny; 551 } 552 553 public void handler_move (double nx, double ny) { 554 int prev_handle = active_handle; 555 556 if (is_over_rotate (nx, ny)) { 557 active_handle = 2; 558 } else if (is_over_resize (nx, ny)) { 559 active_handle = 1; 560 } else { 561 active_handle = 0; 562 } 563 564 if (prev_handle != active_handle) { 565 GlyphCanvas.redraw (); 566 } 567 } 568 569 public void draw_handle (Context cr, Glyph g) { 570 draw_resize_handle (cr, g); 571 draw_rotate_handle (cr, g); 572 } 573 574 public void draw_resize_handle (Context cr, Glyph g) { 575 double x, y; 576 cr.save (); 577 578 Theme.color (cr, "Menu Background"); 579 580 x = img_middle_x - (img_scale_x * get_img ().get_width () / 2); 581 y = img_middle_y - (img_scale_y * get_img ().get_height () / 2); 582 583 x = Glyph.reverse_path_coordinate_x (x); 584 y = Glyph.reverse_path_coordinate_y (y); 585 586 draw_handle_triangle (x, y, cr, g, 6); 587 588 cr.restore (); 589 } 590 591 public void draw_rotate_handle (Context cr, Glyph g) { 592 double x, y, hx, hy, x2, y2; 593 594 double ivz = 1.0 / (g.view_zoom); 595 596 cr.save (); 597 598 cr.scale (g.view_zoom, g.view_zoom); 599 600 if (selected_handle == 2) Theme.color (cr, "Highlighted 1"); 601 else if (active_handle == 2) Theme.color (cr, "Default Background"); 602 else Theme.color (cr, "Menu Background"); 603 604 x = img_offset_x - g.view_offset_x + (size_margin / 2) * img_scale_x; 605 y = img_offset_y - g.view_offset_y + (size_margin / 2) * img_scale_y; 606 607 cr.rectangle (x, y, 5 * ivz, 5 * ivz); 608 cr.fill (); 609 610 hx = cos (img_rotation) * 75 * ivz; 611 hy = sin (img_rotation) * 75 * ivz; 612 613 x2 = x + hx; 614 y2 = y + hy; 615 616 cr.rectangle (x2, y2, 5 * ivz, 5 * ivz); 617 cr.fill (); 618 619 cr.set_line_width (ivz); 620 cr.move_to (x + 2.5 * ivz, y + 2.5 * ivz); 621 cr.line_to (x2 + 2.5 * ivz, y2 + 2.5 * ivz); 622 cr.stroke (); 623 624 cr.restore (); 625 } 626 627 void draw_handle_triangle (double x, double y, Context cr, Glyph g, int direction, double s = 1) 628 requires (0 < direction < 8) 629 { 630 double ivz = 1.0 / (g.view_zoom); 631 double size; 632 633 cr.save (); 634 cr.set_line_width (ivz); 635 636 if (selected_handle == 1) Theme.color (cr, "Highlighted 1"); 637 else if (active_handle == 1) Theme.color (cr, "Default Background"); 638 else Theme.color (cr, "Menu Background"); 639 640 size = (8) * s; 641 642 cr.scale (1, 1); 643 cr.new_path (); 644 645 // up + left 646 if (direction == 1) { 647 cr.move_to (x - size, y - size); 648 cr.line_to (x + size, y - size); 649 cr.line_to (x - size, y + size); 650 } 651 652 if (direction == 6) { 653 cr.move_to (x + size, y + size); 654 cr.line_to (x - size, y + size); 655 cr.line_to (x - size, y - size); 656 } 657 658 cr.close_path(); 659 cr.fill (); 660 661 cr.restore (); 662 663 } 664 665 public void update_background () { 666 background_image = null; 667 contrast_image = null; 668 669 GlyphCanvas.redraw (); 670 updated (); 671 } 672 673 ImageSurface get_contrast_image () { 674 ImageSurface s; 675 Context c; 676 677 ImageSurface sg; 678 int scaled_width; 679 680 unowned uchar[] pix_buff; 681 int i, len; 682 double thres; 683 int stride; 684 685 ImageSurface img; 686 ImageSurface ns; 687 688 double trace_resolution = DrawingTools.auto_trace_resolution.get_value (); 689 double threshold = DrawingTools.background_threshold.get_value (); 690 691 thres = (threshold - 0.5) * 255; 692 693 scaled_width = (int) (600 * trace_resolution); 694 695 s = new ImageSurface (Format.RGB24, scaled_width, scaled_width); 696 sg = (ImageSurface) get_rotated_image (); 697 c = new Context (s); 698 699 c.save (); 700 Theme.color (c, "Background 1"); 701 c.rectangle (0, 0, scaled_width, scaled_width); 702 c.fill (); 703 704 c.translate (scaled_width * 0.5, scaled_width * 0.5); 705 c.rotate (img_rotation); 706 c.translate (-scaled_width * 0.5, -scaled_width * 0.5); 707 708 c.scale ((double) scaled_width / sg.get_width (), (double) scaled_width / sg.get_height ()); 709 710 c.set_source_surface (sg, 0, 0); 711 c.paint (); 712 c.restore (); 713 714 img = (ImageSurface) s; 715 pix_buff = img.get_data (); 716 717 len = s.get_height () * s.get_stride (); 718 719 uint8* outline_img = new uint8[len]; 720 721 for (i = 0; i < len - 4; i += 4) { 722 uint8 o = (uint8) ((pix_buff[i] + pix_buff[i + 1] + pix_buff[i + 2]) / 3.0); 723 uint8 bw = (o < thres) ? 0 : 255; 724 outline_img[i] = bw; 725 outline_img[i + 1] = bw; 726 outline_img[i + 2] = bw; 727 outline_img[i + 3] = bw; 728 } 729 730 // fill blur with black 731 stride = s.get_stride (); 732 for (int m = 0; m < 2; m++) { 733 i = stride + 4; 734 while (i < len - 4 - stride) { 735 if ((outline_img[i] == 255 && outline_img[i + 4] == 0 736 && outline_img[i + stride] == 0 && outline_img[i + stride + 4] == 255) 737 || (outline_img[i] == 0 && outline_img[i + 4] == 255 738 && outline_img[i + stride] == 255 && outline_img[i + stride + 4] == 0)) { 739 outline_img[i] = 0; 740 outline_img[i + 4] = 0; 741 outline_img[i + stride] = 0; 742 outline_img[i + stride + 4] = 0; 743 } 744 745 if (outline_img[i] == 255 && outline_img[i + 4] == 0 && outline_img[i + 8] == 255 746 || outline_img[i] == 0 && outline_img[i + 4] == 255 && outline_img[i + 8] == 0) { 747 outline_img[i] = 0; 748 outline_img[i + 4] = 0; 749 outline_img[i + stride] = 0; 750 outline_img[i + stride + 4] = 0; 751 } 752 i += 4; 753 } 754 } 755 756 ns = new ImageSurface.for_data ((uchar[])outline_img, s.get_format (), s.get_width (), s.get_height (), s.get_stride ()); 757 background_image = null; 758 original_image = null; 759 contrast_image = ns; 760 761 return (ImageSurface) ns; 762 } 763 764 public PathList autotrace () { 765 ImageSurface img; 766 int len, w, h, i, s; 767 Path p; 768 PathList pl; 769 uint8* outline_img; 770 771 ImageSurface scaled_image; 772 double scale; 773 774 p = new Path (); 775 pl = new PathList (); 776 777 get_img (); 778 779 if (background_image == null) { 780 return pl; 781 } 782 783 img = (contrast_image == null) ? get_contrast_image () : (!) contrast_image; 784 785 w = img.get_width(); 786 h = img.get_height(); 787 788 if (unlikely (img.status () != Cairo.Status.SUCCESS)) { 789 warning ("Error"); 790 return pl; 791 } 792 793 if (img.get_format () != Format.RGB24) { 794 warning ("Wrong format"); 795 return pl; 796 } 797 798 scaled_image = img; 799 800 img = (ImageSurface) scaled_image; 801 w = img.get_width (); 802 h = img.get_height (); 803 s = img.get_stride (); 804 len = s * h; 805 806 outline_img = (uint8*) img.get_data (); 807 808 start_points = new Gee.ArrayList<TracedPoint> (); 809 points = new Gee.ArrayList<TracedPoint> (); 810 811 int direction_vertical = 4; 812 int direction_horizontal = s; // FIXME: SET AT FIND START 813 int last_move = 0; 814 int pp = 0; 815 816 scale = 1; 817 i = 0; 818 while ((i = find_start_point (outline_img, len, s, i)) != -1) { 819 pp = 0; 820 821 while (4 + s <= i < len - 4 - s) { 822 pp++; 823 if (is_traced (i)) { 824 Path np = generate_path (outline_img, s, w, h, len); 825 826 if (np.points.size >= 3) { 827 if (Path.is_counter (pl, np)) { 828 np.force_direction (Direction.COUNTER_CLOCKWISE); 829 } else { 830 np.force_direction (Direction.CLOCKWISE); 831 } 832 833 pl.add (np); 834 } 835 836 break; 837 } 838 839 if (outline_img[i] == 255 && outline_img[i + 4] == 255 840 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255) { 841 warning ("Lost path"); 842 Path np = generate_path (outline_img, s, w, h, len); 843 pl.add (np); 844 break; 845 } 846 847 if (outline_img[i] == 0 && outline_img[i + 4] == 0 848 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 0) { 849 warning ("Lost path"); 850 Path np = generate_path (outline_img, s, w, h, len); 851 pl.add (np); 852 break; 853 } 854 855 if (outline_img[i] == 0 && outline_img[i + 4] == 255 856 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255) { 857 points.add (new TracedPoint (i)); 858 859 if (last_move == direction_horizontal) { 860 direction_vertical = -s; 861 i += direction_vertical; 862 last_move = direction_vertical; 863 } else { 864 direction_horizontal = -4; 865 i += direction_horizontal; 866 last_move = direction_horizontal; 867 } 868 869 } else if (outline_img[i] == 0 && outline_img[i + 4] == 255 870 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255) { 871 points.add (new TracedPoint (i)); 872 873 if (last_move == direction_horizontal) { 874 direction_vertical = -s; 875 i += direction_vertical; 876 last_move = direction_vertical; 877 } else { 878 direction_horizontal = -4; 879 i += direction_horizontal; 880 last_move = direction_horizontal; 881 } 882 883 } else if (outline_img[i] == 255 && outline_img[i + 4] == 0 884 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255) { 885 points.add (new TracedPoint (i)); 886 887 if (last_move == direction_horizontal) { 888 direction_vertical = -s; 889 i += direction_vertical; 890 last_move = direction_vertical; 891 } else { 892 direction_horizontal = 4; 893 i += direction_horizontal; 894 last_move = direction_horizontal; 895 } 896 897 } else if (outline_img[i] == 0 && outline_img[i + 4] == 0 898 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 0) { 899 points.add (new TracedPoint (i)); 900 901 if (last_move == direction_horizontal) { 902 direction_vertical = s; 903 i += direction_vertical; 904 last_move = direction_vertical; 905 } else { 906 direction_horizontal = -4; 907 i += direction_horizontal; 908 last_move = direction_horizontal; 909 } 910 911 } else if (outline_img[i] == 255 && outline_img[i + 4] == 255 912 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 0) { 913 points.add (new TracedPoint (i)); 914 915 if (last_move == direction_horizontal) { 916 direction_vertical = s; 917 i += direction_vertical; 918 last_move = direction_vertical; 919 } else { 920 direction_horizontal = 4; 921 i += direction_horizontal; 922 last_move = direction_horizontal; 923 } 924 925 } else if (outline_img[i] == 255 && outline_img[i + 4] == 255 926 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255) { 927 points.add (new TracedPoint (i)); 928 929 if (last_move == direction_horizontal) { 930 direction_vertical = s; 931 i += direction_vertical; 932 last_move = direction_vertical; 933 } else { 934 direction_horizontal = -4; 935 i += direction_horizontal; 936 last_move = direction_horizontal; 937 } 938 939 } else if (outline_img[i] == 255 && outline_img[i + 4] == 0 940 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 0) { 941 points.add (new TracedPoint (i)); 942 943 if (last_move == direction_horizontal) { 944 direction_vertical = -s; 945 i += direction_vertical; 946 last_move = direction_vertical; 947 } else { 948 direction_horizontal = -4; 949 i += direction_horizontal; 950 last_move = direction_horizontal; 951 } 952 } else if (outline_img[i] == 0 && outline_img[i + 4] == 255 953 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 0) { 954 points.add (new TracedPoint (i)); 955 956 if (last_move == direction_horizontal) { 957 direction_vertical = -s; 958 i += direction_vertical; 959 last_move = direction_vertical; 960 } else { 961 direction_horizontal = 4; 962 i += direction_horizontal; 963 last_move = direction_horizontal; 964 } 965 } else if (outline_img[i] == 0 && outline_img[i + 4] == 0 966 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255) { 967 points.add (new TracedPoint (i)); 968 969 if (last_move == direction_horizontal) { 970 direction_vertical = s; 971 i += direction_vertical; 972 last_move = direction_vertical; 973 } else { 974 direction_horizontal = 4; 975 i += direction_horizontal; 976 last_move = direction_horizontal; 977 } 978 } else if (outline_img[i] == 255 && outline_img[i + 4] == 255 979 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255) { 980 points.add (new TracedPoint (i)); 981 982 if (last_move == direction_horizontal) { 983 direction_vertical = s; 984 i += direction_vertical; 985 last_move = direction_vertical; 986 } else { 987 direction_horizontal = -4; 988 i += direction_horizontal; 989 last_move = direction_horizontal; 990 } 991 } else if (outline_img[i] == 255 && outline_img[i + 4] == 255 992 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 0) { 993 points.add (new TracedPoint (i)); 994 995 i += direction_horizontal; 996 last_move = direction_horizontal; 997 998 } else if (outline_img[i] == 0 && outline_img[i + 4] == 0 999 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255) { 1000 points.add (new TracedPoint (i)); 1001 1002 i += direction_horizontal; 1003 last_move = direction_horizontal; 1004 } else if (outline_img[i] == 255 && outline_img[i + 4] == 0 1005 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 0) { 1006 points.add (new TracedPoint (i)); 1007 1008 i += direction_vertical; 1009 last_move = direction_vertical; 1010 } else if (outline_img[i] == 0 && outline_img[i + 4] == 255 1011 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255) { 1012 points.add (new TracedPoint (i)); 1013 1014 i += direction_vertical; 1015 last_move = direction_vertical; 1016 } else if ((outline_img[i] == 255 && outline_img[i + 4] == 0 1017 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255) 1018 || (outline_img[i] == 0 && outline_img[i + 4] == 255 1019 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 0)) { 1020 points.add (new TracedPoint (i)); 1021 1022 warning ("Bad edge"); 1023 i += last_move; 1024 1025 } else { 1026 points.add (new TracedPoint (i)); 1027 warning (@"No direction\n $(outline_img[i]) $(outline_img[i + 4])\n $(outline_img[i + s]) $(outline_img[i + s + 4])"); 1028 i += 4; 1029 } 1030 } 1031 } 1032 1033 start_points.clear (); 1034 points.clear (); 1035 1036 return pl; 1037 } 1038 1039 int find_start_point (uint8* outline_img, int len, int s, int start_index) { 1040 // find start point 1041 int i = start_index; 1042 1043 if (i < s + 4) { 1044 i = s + 4; 1045 } 1046 1047 while (i < len - 4 - s) { 1048 if (outline_img[i] == 0 && outline_img[i + 4] == 255 1049 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255 1050 && !has_start_point (i)) { 1051 return i; 1052 } else if (outline_img[i] == 255 && outline_img[i + 4] == 0 1053 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255 1054 && !has_start_point (i)) { 1055 return i; 1056 } else if (outline_img[i] == 255 && outline_img[i + 4] == 255 1057 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255 1058 && !has_start_point (i)) { 1059 return i; 1060 } else if (outline_img[i] == 255 && outline_img[i + 4] == 255 1061 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 0 1062 && !has_start_point (i)) { 1063 return i; 1064 } else if (outline_img[i] == 255 && outline_img[i + 4] == 0 1065 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 0 1066 && !has_start_point (i)) { 1067 return i; 1068 } else if (outline_img[i] == 0 && outline_img[i + 4] == 255 1069 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 0 1070 && !has_start_point (i)) { 1071 return i; 1072 } else if (outline_img[i] == 0 && outline_img[i + 4] == 0 1073 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 0 1074 && !has_start_point (i)) { 1075 return i; 1076 } else if (outline_img[i] == 0 && outline_img[i + 4] == 0 1077 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255 1078 && !has_start_point (i)) { 1079 return i; 1080 } 1081 1082 i +=4; 1083 } 1084 1085 return -1; 1086 } 1087 1088 void find_corner (Path path, int point_index, int end, double points_per_unit, ref double x, ref double y) { 1089 TracedPoint tp0; 1090 double sx = 0; 1091 double sy = 0; 1092 double d = 0; 1093 double mind = double.MAX; 1094 int index = 0; 1095 int pi; 1096 EditPoint ep0, ep1; 1097 double dx, dy; 1098 1099 pi = point_index - 1; 1100 if (pi < 0) { 1101 pi += path.points.size; 1102 } 1103 ep0 = path.points.get (pi); 1104 1105 pi = point_index + 1; 1106 pi %= path.points.size; 1107 ep1 = path.points.get (pi); 1108 1109 Path.find_intersection_handle (ep0.get_left_handle (), ep1.get_right_handle (), out sx, out sy); 1110 1111 dx = x - ep0.x; 1112 dy = y - ep0.y; 1113 1114 sx += 3 * dx; 1115 sy += 3 * dy; 1116 1117 dx = x - ep1.x; 1118 dy = y - ep1.y; 1119 1120 sx += 3 * dx; 1121 sy += 3 * dy; 1122 1123 end += (int) (points_per_unit / 2.0); 1124 for (int i = 0; i < 2 * points_per_unit; i++) { 1125 index = end - i; 1126 1127 if (index < 0) { 1128 index += points.size; 1129 } else { 1130 index %= points.size; 1131 } 1132 1133 tp0 = points.get (index); 1134 1135 d = Path.distance (tp0.x, sx, tp0.y, sy); 1136 if (d < mind) { 1137 mind = d; 1138 x = tp0.x; 1139 y = tp0.y; 1140 } 1141 } 1142 } 1143 1144 Path generate_path (uint8* outline_img, int stride, int w, int h, int length) { 1145 double x, y, np; 1146 int i, index; 1147 double sumx, sumy, points_per_unit; 1148 Path path = new Path (); 1149 Gee.ArrayList<int?> sp = new Gee.ArrayList<int?> (); 1150 double corner; 1151 Gee.ArrayList<TracedPoint> traced = new Gee.ArrayList<TracedPoint> (); 1152 Gee.ArrayList<EditPoint> corners = new Gee.ArrayList<EditPoint> (); 1153 EditPoint ep; 1154 EditPointHandle r, l; 1155 double la, a; 1156 PointSelection ps; 1157 double image_scale_x; 1158 double image_scale_y; 1159 TracedPoint average_point; 1160 int pi; 1161 ImageSurface img; 1162 double simplification = DrawingTools.auto_trace_simplify.get_value (); 1163 1164 img = (contrast_image == null) ? get_contrast_image () : (!) contrast_image; 1165 1166 image_scale_x = ((double) size_margin / img.get_width ()); 1167 image_scale_y = ((double) size_margin / img.get_height ()); 1168 1169 foreach (TracedPoint p in points) { 1170 start_points.add (p); 1171 } 1172 1173 sumx = 0; 1174 sumy = 0; 1175 np = 0; 1176 1177 points_per_unit = 9; 1178 corner = PI / 3.5; 1179 1180 i = 0; 1181 foreach (TracedPoint p in points) { 1182 index = p.index; 1183 x = -w * img_scale_x / 2 + (((index + 4) % stride) / 4) * img_scale_x; 1184 y = h * img_scale_y / 2 + -((index - x * 4) / stride) * img_scale_y; 1185 1186 x *= image_scale_x; 1187 y *= image_scale_y; 1188 1189 x += img_middle_x; 1190 y += img_middle_y; 1191 1192 p.x = x; 1193 p.y = y; 1194 1195 np++; 1196 1197 sumx += x; 1198 sumy += y; 1199 1200 if (np >= points_per_unit) { 1201 average_point = new TracedPoint (-1); 1202 average_point.x = sumx / np; 1203 average_point.y = sumy / np; 1204 traced.add (average_point); 1205 1206 sp.add (i); 1207 1208 np = 0; 1209 sumx = 0; 1210 sumy = 0; 1211 } 1212 1213 i++; 1214 } 1215 1216 if (np != 0) { 1217 average_point = new TracedPoint (-1); 1218 average_point.x = sumx / np; 1219 average_point.y = sumy / np; 1220 traced.add (average_point); 1221 sp.add (i); 1222 } 1223 1224 foreach (TracedPoint avgp in traced) { 1225 ep = new EditPoint (avgp.x, avgp.y); 1226 1227 path.points.add (ep); 1228 1229 if (DrawingTools.point_type == PointType.CUBIC) { 1230 ep.type = PointType.CUBIC; 1231 ep.get_right_handle ().type = PointType.LINE_CUBIC; 1232 ep.get_left_handle ().type = PointType.LINE_CUBIC; 1233 } else { 1234 ep.type = PointType.DOUBLE_CURVE; 1235 ep.get_right_handle ().type = PointType.LINE_DOUBLE_CURVE; 1236 ep.get_left_handle ().type = PointType.LINE_DOUBLE_CURVE; 1237 } 1238 } 1239 1240 path.close (); 1241 path.create_list (); 1242 path.recalculate_linear_handles (); 1243 1244 // Find corners 1245 pi = 0; 1246 for (i = 1; i < sp.size; i += 2) { 1247 return_val_if_fail (0 <= i < path.points.size, path); 1248 ep = path.points.get (i); 1249 1250 pi = i + 2; 1251 pi %= path.points.size; 1252 return_val_if_fail (0 <= pi < path.points.size, path); 1253 l = path.points.get (pi).get_left_handle (); 1254 1255 pi = i - 2; 1256 if (pi < 0) { 1257 pi += path.points.size; 1258 } 1259 return_val_if_fail (0 <= pi < path.points.size, path); 1260 r = path.points.get (pi).get_right_handle (); 1261 1262 la = l.angle - PI; 1263 1264 while (la < 0) { 1265 la += 2 * PI; 1266 } 1267 1268 if (r.angle > (2.0 / 3.0) * PI && la < PI / 2) { 1269 la += 2 * PI; 1270 } else if (la > (2.0 / 3.0) * PI && r.angle < PI / 2) { 1271 la -= 2 * PI; 1272 } 1273 1274 a = r.angle - la; 1275 1276 if (fabs (a) > corner) { // corner 1277 ep.set_tie_handle (false); 1278 find_corner (path, i, (!) sp.get (i), points_per_unit, ref ep.x, ref ep.y); 1279 corners.add (ep); 1280 } else { 1281 ep.set_tie_handle (true); 1282 } 1283 } 1284 1285 path.recalculate_linear_handles (); 1286 path.remove_points_on_points (); 1287 path.create_list (); 1288 foreach (EditPoint e in path.points) { 1289 if (e.tie_handles) { 1290 e.process_tied_handle (); 1291 } 1292 } 1293 1294 if (simplification > 0.01) { 1295 for (i = 0; i < path.points.size; i++) { 1296 ep = path.points.get (i); 1297 ps = new PointSelection (ep, path); 1298 if (corners.index_of (ep) == -1) { 1299 PenTool.remove_point_simplify (ps, simplification); 1300 } 1301 } 1302 } 1303 1304 for (i = 0; i < path.points.size; i++) { 1305 ep = path.points.get (i); 1306 ps = new PointSelection (ep, path); 1307 if (corners.index_of (ep) == -1) { 1308 ep.set_selected (true); 1309 } 1310 } 1311 1312 path = PenTool.simplify (path, true, simplification); 1313 points.clear (); 1314 path.update_region_boundaries (); 1315 1316 return path; 1317 } 1318 1319 bool has_start_point (int i) { 1320 foreach (TracedPoint p in start_points) { 1321 if (p.index == i) { 1322 return true; 1323 } 1324 } 1325 return false; 1326 } 1327 1328 bool is_traced (int i) { 1329 foreach (TracedPoint p in points) { 1330 if (p.index == i) { 1331 return true; 1332 } 1333 } 1334 return false; 1335 } 1336 1337 public void center_in_glyph (Glyph? glyph = null) { 1338 Glyph g; 1339 Font f = BirdFont.get_current_font (); 1340 1341 if (glyph != null) { 1342 g = (!) glyph; 1343 } else { 1344 g = MainWindow.get_current_glyph (); 1345 } 1346 1347 img_middle_x = g.left_limit + (g.right_limit - g.left_limit) / 2; 1348 img_middle_y = f.bottom_position + (f.top_position - f.bottom_position) / 2; 1349 } 1350 1351 class TracedPoint { 1352 public int index; 1353 1354 public double x = 0; 1355 public double y = 0; 1356 1357 public TracedPoint (int index) { 1358 this.index = index; 1359 } 1360 } 1361 } 1362 1363 } 1364