The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

SvgParser.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/SvgParser.vala.
Calculate orientation for paths with two points.
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 Bird; 16 using Math; 17 18 namespace BirdFont { 19 20 public enum SvgFormat { 21 NONE, 22 INKSCAPE, 23 ILLUSTRATOR 24 } 25 26 public class SvgParser { 27 28 SvgFormat format = SvgFormat.ILLUSTRATOR; 29 30 public SvgParser () { 31 } 32 33 public void set_format (SvgFormat f) { 34 format = f; 35 } 36 37 public static void import () { 38 FileChooser fc = new FileChooser (); 39 fc.file_selected.connect ((p) => { 40 string path; 41 42 if (p == null) { 43 return; 44 } 45 46 path = (!) p; 47 import_svg (path); 48 }); 49 50 MainWindow.file_chooser (t_("Import"), fc, FileChooser.LOAD); 51 } 52 53 public static void import_svg_data (string xml_data, SvgFormat format = SvgFormat.NONE) { 54 PathList path_list = new PathList (); 55 Glyph glyph; 56 string[] lines = xml_data.split ("\n"); 57 bool has_format = false; 58 SvgParser parser = new SvgParser (); 59 XmlParser xmlparser; 60 61 foreach (string l in lines) { 62 if (l.index_of ("Illustrator") > -1 || l.index_of ("illustrator") > -1) { 63 parser.set_format (SvgFormat.ILLUSTRATOR); 64 has_format = true; 65 } 66 67 if (l.index_of ("Inkscape") > -1 || l.index_of ("inkscape") > -1) { 68 parser.set_format (SvgFormat.INKSCAPE); 69 has_format = true; 70 } 71 } 72 73 if (format != SvgFormat.NONE) { 74 parser.set_format (format); 75 } 76 77 // parse the file 78 if (!has_format) { 79 warn_if_test ("No format identifier found in SVG parser.\n"); 80 } 81 82 xmlparser = new XmlParser (xml_data); 83 84 path_list = parser.parse_svg_file (xmlparser.get_root_tag ()); 85 86 glyph = MainWindow.get_current_glyph (); 87 foreach (Path p in path_list.paths) { 88 glyph.add_path (p); 89 } 90 91 foreach (Path p in path_list.paths) { 92 glyph.add_active_path (p); 93 p.update_region_boundaries (); 94 } 95 96 glyph.close_path (); 97 } 98 99 public static string replace (string content, string start, string stop, string replacement) { 100 int i_tag = content.index_of (start); 101 int end_tag = content.index_of (stop, i_tag); 102 string c = ""; 103 104 if (i_tag > -1) { 105 c = content.substring (0, i_tag) 106 + replacement 107 + content.substring (end_tag + stop.length); 108 } else { 109 c = content; 110 } 111 112 return c; 113 } 114 115 public static void import_svg (string path) { 116 string svg_data; 117 try { 118 FileUtils.get_contents (path, out svg_data); 119 } catch (GLib.Error e) { 120 warning (e.message); 121 } 122 import_svg_data (svg_data); 123 } 124 125 private PathList parse_svg_file (Tag tag) { 126 Layer pl = new Layer (); 127 128 foreach (Tag t in tag) { 129 130 if (t.get_name () == "g") { 131 parse_layer (t, pl); 132 } 133 134 if (t.get_name () == "switch") { 135 parse_layer (t, pl); 136 } 137 138 if (t.get_name () == "path") { 139 parse_path (t, pl); 140 } 141 142 if (t.get_name () == "polygon") { 143 parse_polygon (t, pl); 144 } 145 } 146 147 return pl.get_all_paths (); 148 } 149 150 private void parse_layer (Tag tag, Layer pl) { 151 Layer layer; 152 153 foreach (Tag t in tag) { 154 if (t.get_name () == "path") { 155 parse_path (t, pl); 156 } 157 158 if (t.get_name () == "g") { 159 layer = new Layer (); 160 parse_layer (t, layer); 161 pl.subgroups.add (layer); 162 } 163 164 if (t.get_name () == "polygon") { 165 parse_polygon (t, pl); 166 } 167 168 if (t.get_name () == "rect") { 169 parse_rect (t, pl); 170 } 171 } 172 173 foreach (Attribute attr in tag.get_attributes ()) { 174 if (attr.get_name () == "transform") { 175 transform (attr.get_content (), pl); 176 } 177 } 178 } 179 180 private void transform (string transform_functions, Layer layer) { 181 transform_paths (transform_functions, layer.paths); 182 transform_subgroups (transform_functions, layer); 183 } 184 185 private void transform_subgroups (string transform_functions, Layer layer) { 186 foreach (Layer subgroup in layer.subgroups) { 187 transform (transform_functions, subgroup); 188 } 189 } 190 191 private void transform_paths (string transform_functions, PathList pl) { 192 string data = transform_functions.dup (); 193 string[] functions; 194 195 // use only a single space as separator 196 while (data.index_of (" ") > -1) { 197 data = data.replace (" ", " "); 198 } 199 200 return_if_fail (data.index_of (")") > -1); 201 202 // add separator 203 data = data.replace (") ", "|"); 204 data = data.replace (")", "|"); 205 functions = data.split ("|"); 206 207 for (int i = functions.length - 1; i >= 0; i--) { 208 if (functions[i].has_prefix ("translate")) { 209 translate (functions[i], pl); 210 } 211 212 if (functions[i].has_prefix ("scale")) { 213 scale (functions[i], pl); 214 } 215 216 if (functions[i].has_prefix ("matrix")) { 217 matrix (functions[i], pl); 218 } 219 220 // TODO: rotate etc. 221 } 222 } 223 224 /** @param path a path in the cartesian coordinate system 225 * The other parameters are in the SVG coordinate system. 226 */ 227 public static void apply_matrix (Path path, double a, double b, double c, 228 double d, double e, double f){ 229 230 double dx, dy; 231 Font font = BirdFont.get_current_font (); 232 Glyph glyph = MainWindow.get_current_glyph (); 233 234 foreach (EditPoint ep in path.points) { 235 apply_matrix_on_handle (ep.get_right_handle (), a, b, c, d, e, f); 236 apply_matrix_on_handle (ep.get_left_handle (), a, b, c, d, e, f); 237 238 ep.independent_y = font.top_position - ep.independent_y; 239 ep.independent_x -= glyph.left_limit; 240 241 dx = a * ep.independent_x + c * ep.independent_y + e; 242 dy = b * ep.independent_x + d * ep.independent_y + f; 243 244 ep.independent_x = dx; 245 ep.independent_y = dy; 246 247 ep.independent_y = font.top_position - ep.independent_y; 248 ep.independent_x += glyph.left_limit; 249 } 250 } 251 252 public static void apply_matrix_on_handle (EditPointHandle h, 253 double a, double b, double c, 254 double d, double e, double f){ 255 256 double dx, dy; 257 Font font = BirdFont.get_current_font (); 258 Glyph glyph = MainWindow.get_current_glyph (); 259 260 h.y = font.top_position - h.y; 261 h.x -= glyph.left_limit; 262 263 dx = a * h.x + c * h.y + e; 264 dy = b * h.x + d * h.y + f; 265 266 h.x = dx; 267 h.y = dy; 268 269 h.y = font.top_position - h.y; 270 h.x += glyph.left_limit; 271 } 272 273 274 private void matrix (string function, PathList pl) { 275 string parameters = get_transform_parameters (function); 276 string[] p = parameters.split (" "); 277 278 if (p.length != 6) { 279 warning ("Expecting six parameters for matrix transformation."); 280 return; 281 } 282 283 foreach (Path path in pl.paths) { 284 apply_matrix (path, parse_double (p[0]), parse_double (p[1]), 285 parse_double (p[2]), parse_double (p[3]), 286 parse_double (p[4]), parse_double (p[5])); 287 } 288 } 289 290 private void scale (string function, PathList pl) { 291 string parameters = get_transform_parameters (function); 292 string[] p = parameters.split (" "); 293 double x, y; 294 295 x = 1; 296 y = 1; 297 298 if (p.length > 0) { 299 x = parse_double (p[0]); 300 } 301 302 if (p.length > 1) { 303 y = parse_double (p[1]); 304 } 305 306 foreach (Path path in pl.paths) { 307 path.scale (-x, y); 308 } 309 } 310 311 private void translate (string function, PathList pl) { 312 string parameters = get_transform_parameters (function); 313 string[] p = parameters.split (" "); 314 double x, y; 315 316 x = 0; 317 y = 0; 318 319 if (p.length > 0) { 320 x = parse_double (p[0]); 321 } 322 323 if (p.length > 1) { 324 y = parse_double (p[1]); 325 } 326 327 foreach (Path path in pl.paths) { 328 path.move (x, -y); 329 } 330 } 331 332 private string get_transform_parameters (string function) { 333 int i; 334 string param = ""; 335 336 i = function.index_of ("("); 337 return_val_if_fail (i != -1, param); 338 param = function.substring (i); 339 340 param = param.replace ("(", ""); 341 param = param.replace ("\n", " "); 342 param = param.replace ("\t", " "); 343 param = param.replace (",", " "); 344 345 while (param.index_of (" ") > -1) { 346 param.replace (" ", " "); 347 } 348 349 return param.strip(); 350 } 351 352 private void parse_rect (Tag tag, Layer pl) { 353 Path p; 354 double x, y, x2, y2; 355 BezierPoints[] bezier_points; 356 Glyph g; 357 PathList npl = new PathList (); 358 359 x = 0; 360 y = 0; 361 x2 = 0; 362 y2 = 0; 363 364 foreach (Attribute attr in tag.get_attributes ()) { 365 if (attr.get_name () == "x") { 366 x = parse_double (attr.get_content ()); 367 } 368 369 if (attr.get_name () == "y") { 370 y = -parse_double (attr.get_content ()); 371 } 372 373 if (attr.get_name () == "width") { 374 x2 = parse_double (attr.get_content ()); 375 } 376 377 if (attr.get_name () == "height") { 378 y2 = -parse_double (attr.get_content ()); 379 } 380 } 381 382 x2 += x; 383 y2 += y; 384 385 bezier_points = new BezierPoints[4]; 386 bezier_points[0] = new BezierPoints (); 387 bezier_points[0].type == 'L'; 388 bezier_points[0].x0 = x; 389 bezier_points[0].y0 = y; 390 391 bezier_points[1] = new BezierPoints (); 392 bezier_points[1].type == 'L'; 393 bezier_points[1].x0 = x2; 394 bezier_points[1].y0 = y; 395 396 bezier_points[2] = new BezierPoints (); 397 bezier_points[2].type == 'L'; 398 bezier_points[2].x0 = x2; 399 bezier_points[2].y0 = y2; 400 401 bezier_points[3] = new BezierPoints (); 402 bezier_points[3].type == 'L'; 403 bezier_points[3].x0 = x; 404 bezier_points[3].y0 = y2; 405 406 g = MainWindow.get_current_glyph (); 407 move_and_resize (bezier_points, 4, false, 1, g); 408 409 p = new Path (); 410 411 p.add (bezier_points[0].x0, bezier_points[0].y0); 412 p.add (bezier_points[1].x0, bezier_points[1].y0); 413 p.add (bezier_points[2].x0, bezier_points[2].y0); 414 p.add (bezier_points[3].x0, bezier_points[3].y0); 415 416 p.close (); 417 p.create_list (); 418 p.recalculate_linear_handles (); 419 420 npl.add (p); 421 422 // FIXME: right layer for other transforms 423 foreach (Attribute attr in tag.get_attributes ()) { 424 if (attr.get_name () == "transform") { 425 transform_paths (attr.get_content (), npl); 426 } 427 } 428 429 pl.paths.append (npl); 430 } 431 432 private void parse_polygon (Tag tag, Layer pl) { 433 Path p; 434 435 foreach (Attribute attr in tag.get_attributes ()) { 436 if (attr.get_name () == "points") { 437 p = parse_polygon_data (attr.get_content ()); 438 pl.paths.add (p); 439 } 440 } 441 } 442 443 private void parse_path (Tag tag, Layer pl) { 444 Glyph glyph = MainWindow.get_current_glyph (); 445 PathList path_list = new PathList (); 446 447 foreach (Attribute attr in tag.get_attributes ()) { 448 if (attr.get_name () == "d") { 449 path_list = parse_svg_data (attr.get_content (), glyph); 450 pl.paths.append (path_list); 451 } 452 } 453 454 // assume the even odd rule is applied and convert the path 455 // to a path using the non-zero rule 456 foreach (Path p1 in pl.paths.paths) { 457 if (Path.is_counter (pl.paths, p1)) { 458 p1.force_direction (Direction.COUNTER_CLOCKWISE); 459 } else { 460 p1.force_direction (Direction.CLOCKWISE); 461 } 462 } 463 464 foreach (Attribute attr in tag.get_attributes ()) { 465 if (attr.get_name () == "transform") { 466 transform_paths (attr.get_content (), path_list); 467 } 468 } 469 } 470 471 public static void create_lines_for_segment (Path path, EditPoint start, EditPoint end) { 472 double x1, x2, x3; 473 double y1, y2, y3; 474 double step_start, step, step_end; 475 476 path.add (start.x, start.y); 477 478 step_start = 0; 479 step = 0.5; 480 step_end = 1; 481 482 while (true) { 483 Path.get_point_for_step (start, end, step_start, out x1, out y1); 484 Path.get_point_for_step (start, end, step, out x2, out y2); 485 Path.get_point_for_step (start, end, step_end, out x3, out y3); 486 487 if (!StrokeTool.is_flat (x1, y1, x2, y2, x3, y3, 1) 488 && step_end - step / 2.0 > step_start 489 && step_end - step / 2.0 > 0.1 490 && step > 0.05) { 491 492 step /= 2.0; 493 494 if (step < 0.05) { 495 step = 0.05; 496 } else { 497 step_end = step_start + 2 * step; 498 } 499 } else { 500 path.add (x3, y3); 501 502 if (step_end + step < 1) { 503 step_start = step_end; 504 step_end += step; 505 } else { 506 break; 507 } 508 } 509 } 510 } 511 512 // FIXME: cache lines in path 513 public static Path get_lines (Path p) { 514 EditPoint start; 515 Path path = new Path (); 516 517 if (p.points.size == 0) { 518 return path; 519 } 520 521 // create a set of straight lines 522 start = p.points.get (p.points.size - 1); 523 524 foreach (EditPoint end in p.points) { 525 create_lines_for_segment (path, start, end); 526 start = end; 527 } 528 529 if (StrokeTool.stroke_selected) { // FIXME: DELETE 530 ((!) BirdFont.get_current_font ().get_glyph ("b")).add_path (path); 531 path.recalculate_linear_handles (); 532 } 533 534 return path; 535 } 536 537 /** Check if a point is inside using the even odd fill rule. 538 * The path should only have straight lines. 539 */ 540 public static bool is_inside (EditPoint point, Path path) { 541 EditPoint prev; 542 bool inside = false; 543 544 if (path.points.size <= 1) { 545 return false; 546 } 547 548 prev = path.get_last_point (); 549 550 foreach (EditPoint start in path.points) { 551 if (start.x == point.x && point.y == start.y) { 552 inside = true; 553 break; 554 } else if ((start.y > point.y) != (prev.y > point.y) 555 && point.x < (prev.x - start.x) * (point.y - start.y) / (prev.y - start.y) + start.x) { 556 inside = !inside; 557 } 558 559 prev = start; 560 } 561 562 return inside; 563 } 564 565 /** Add space as separator to svg data. 566 * @param d svg data 567 */ 568 static string add_separators (string d) { 569 string data = d; 570 571 data = data.replace (",", " "); 572 data = data.replace ("a", " a "); 573 data = data.replace ("A", " A "); 574 data = data.replace ("m", " m "); 575 data = data.replace ("M", " M "); 576 data = data.replace ("h", " h "); 577 data = data.replace ("H", " H "); 578 data = data.replace ("v", " v "); 579 data = data.replace ("V", " V "); 580 data = data.replace ("l", " l "); 581 data = data.replace ("L", " L "); 582 data = data.replace ("q", " q "); 583 data = data.replace ("Q", " Q "); 584 data = data.replace ("c", " c "); 585 data = data.replace ("C", " C "); 586 data = data.replace ("t", " t "); 587 data = data.replace ("T", " T "); 588 data = data.replace ("s", " s "); 589 data = data.replace ("S", " S "); 590 data = data.replace ("zM", " z M "); 591 data = data.replace ("zm", " z m "); 592 data = data.replace ("z", " z "); 593 data = data.replace ("Z", " Z "); 594 data = data.replace ("-", " -"); 595 data = data.replace ("e -", "e-"); // minus can be either separator or a negative exponent 596 data = data.replace ("\t", " "); 597 data = data.replace ("\r\n", " "); 598 data = data.replace ("\n", " "); 599 600 // use only a single space as separator 601 while (data.index_of (" ") > -1) { 602 data = data.replace (" ", " "); 603 } 604 605 return data; 606 } 607 608 public void add_path_to_glyph (string d, Glyph g, bool svg_glyph = false, double units = 1) { 609 PathList p = parse_svg_data (d, g, svg_glyph, units); 610 foreach (Path path in p.paths) { 611 g.add_path (path); 612 } 613 } 614 615 /** 616 * @param d svg data 617 * @param glyph use lines from this glyph but don't add the generated paths 618 * @param svg_glyph parse svg glyph with origo in lower left corner 619 * 620 * @return the new paths 621 */ 622 public PathList parse_svg_data (string d, Glyph glyph, bool svg_glyph = false, double units = 1) { 623 double px = 0; 624 double py = 0; 625 double px2 = 0; 626 double py2 = 0; 627 double cx = 0; 628 double cy = 0; 629 string data; 630 Font font; 631 PathList path_list = new PathList (); 632 BezierPoints[] bezier_points; 633 string[] c; 634 double arc_rx, arc_ry; 635 double arc_rotation; 636 int large_arc; 637 int arc_sweep; 638 double arc_dest_x, arc_dest_y; 639 640 font = BirdFont.get_current_font (); 641 642 data = add_separators (d); 643 c = data.split (" "); 644 bezier_points = new BezierPoints[8 * c.length + 1]; // the arc instruction can use up to eight points 645 646 for (int i = 0; i < 2 * c.length + 1; i++) { 647 bezier_points[i] = new BezierPoints (); 648 } 649 650 int bi = 0; 651 652 // parse path 653 int i = -1; 654 while (++i < c.length && bi < bezier_points.length) { 655 if (c[i] == "m") { 656 while (i + 2 < c.length && is_point (c[i + 1])) { // FIXME: check array bounds 657 bezier_points[bi].type = 'M'; 658 bezier_points[bi].svg_type = 'm'; 659 660 px += parse_double (c[++i]); 661 662 if (svg_glyph) { 663 py += parse_double (c[++i]); 664 } else { 665 py += -parse_double (c[++i]); 666 } 667 668 bezier_points[bi].x0 = px; 669 bezier_points[bi].y0 = py; 670 bi++; 671 } 672 } else if (c[i] == "M") { 673 while (i + 2 < c.length && is_point (c[i + 1])) { 674 bezier_points[bi].type = 'M'; 675 bezier_points[bi].svg_type = 'M'; 676 677 px = parse_double (c[++i]); 678 679 if (svg_glyph) { 680 py = parse_double (c[++i]); 681 } else { 682 py = -parse_double (c[++i]); 683 } 684 685 bezier_points[bi].x0 = px; 686 bezier_points[bi].y0 = py; 687 bi++; 688 } 689 } else if (c[i] == "h") { 690 while (i + 1 < c.length && is_point (c[i + 1])) { 691 bezier_points[bi].type = 'L'; 692 bezier_points[bi].svg_type = 'h'; 693 694 px += parse_double (c[++i]); 695 696 bezier_points[bi].x0 = px; 697 bezier_points[bi].y0 = py; 698 bi++; 699 } 700 } else if (i + 1 < c.length && c[i] == "H") { 701 while (is_point (c[i + 1])) { 702 bezier_points[bi].type = 'L'; 703 bezier_points[bi].svg_type = 'H'; 704 705 px = parse_double (c[++i]); 706 707 bezier_points[bi].x0 = px; 708 bezier_points[bi].y0 = py; 709 bi++; 710 } 711 } else if (c[i] == "v") { 712 while (i + 1 < c.length && is_point (c[i + 1])) { 713 bezier_points[bi].type = 'L'; 714 bezier_points[bi].svg_type = 'v'; 715 716 if (svg_glyph) { 717 py = py + parse_double (c[++i]); 718 } else { 719 py = py - parse_double (c[++i]); 720 } 721 722 bezier_points[bi].x0 = px; 723 bezier_points[bi].y0 = py; 724 bi++; 725 } 726 } else if (i + 1 < c.length && c[i] == "V") { 727 while (is_point (c[i + 1])) { 728 bezier_points[bi].type = 'L'; 729 bezier_points[bi].svg_type = 'V'; 730 731 if (svg_glyph) { 732 py = parse_double (c[++i]); 733 } else { 734 py = -parse_double (c[++i]); 735 } 736 737 bezier_points[bi].x0 = px; 738 bezier_points[bi].y0 = py; 739 bi++; 740 } 741 } else if (c[i] == "l") { 742 while (i + 2 < c.length && is_point (c[i + 1])) { 743 bezier_points[bi].type = 'L'; 744 bezier_points[bi].svg_type = 'l'; 745 746 cx = px + parse_double (c[++i]); 747 748 if (svg_glyph) { 749 cy = py + parse_double (c[++i]); 750 } else { 751 cy = py - parse_double (c[++i]); 752 } 753 754 px = cx; 755 py = cy; 756 757 bezier_points[bi].x0 = cx; 758 bezier_points[bi].y0 = cy; 759 bi++; 760 } 761 } else if (c[i] == "L") { 762 while (i + 2 < c.length && is_point (c[i + 1])) { 763 bezier_points[bi].type = 'L'; 764 bezier_points[bi].svg_type = 'L'; 765 766 cx = parse_double (c[++i]); 767 768 if (svg_glyph) { 769 cy = parse_double (c[++i]); 770 } else { 771 cy = -parse_double (c[++i]); 772 } 773 774 px = cx; 775 py = cy; 776 777 bezier_points[bi].x0 = cx; 778 bezier_points[bi].y0 = cy; 779 bi++; 780 } 781 } else if (c[i] == "c") { 782 while (i + 6 < c.length && is_point (c[i + 1])) { 783 bezier_points[bi].type = 'C'; 784 bezier_points[bi].svg_type = 'C'; 785 786 cx = px + parse_double (c[++i]); 787 788 if (svg_glyph) { 789 cy = py + parse_double (c[++i]); 790 } else { 791 cy = py - parse_double (c[++i]); 792 } 793 794 bezier_points[bi].x0 = cx; 795 bezier_points[bi].y0 = cy; 796 797 cx = px + parse_double (c[++i]); 798 799 if (svg_glyph) { 800 cy = py + parse_double (c[++i]); 801 } else { 802 cy = py - parse_double (c[++i]); 803 } 804 805 px2 = cx; 806 py2 = cy; 807 808 bezier_points[bi].x1 = px2; 809 bezier_points[bi].y1 = py2; 810 811 cx = px + parse_double (c[++i]); 812 813 if (svg_glyph) { 814 cy = py + parse_double (c[++i]); 815 } else { 816 cy = py + -parse_double (c[++i]); 817 } 818 819 bezier_points[bi].x2 = cx; 820 bezier_points[bi].y2 = cy; 821 822 px = cx; 823 py = cy; 824 825 bi++; 826 } 827 } else if (c[i] == "C") { 828 while (i + 6 < c.length && is_point (c[i + 1])) { 829 bezier_points[bi].type = 'C'; 830 bezier_points[bi].svg_type = 'C'; 831 832 cx = parse_double (c[++i]); 833 834 if (svg_glyph) { 835 cy = parse_double (c[++i]); 836 } else { 837 cy = -parse_double (c[++i]); 838 } 839 840 bezier_points[bi].x0 = cx; 841 bezier_points[bi].y0 = cy; 842 843 cx = parse_double (c[++i]); 844 845 if (svg_glyph) { 846 cy = parse_double (c[++i]); 847 } else { 848 cy = -parse_double (c[++i]); 849 } 850 851 px2 = cx; 852 py2 = cy; 853 854 bezier_points[bi].x1 = cx; 855 bezier_points[bi].y1 = cy; 856 857 cx = parse_double (c[++i]); 858 859 if (svg_glyph) { 860 cy = parse_double (c[++i]); 861 } else { 862 cy = -parse_double (c[++i]); 863 } 864 865 bezier_points[bi].x2 = cx; 866 bezier_points[bi].y2 = cy; 867 868 px = cx; 869 py = cy; 870 871 bi++; 872 } 873 } else if (c[i] == "q") { 874 while (i + 4 < c.length && is_point (c[i + 1])) { 875 bezier_points[bi].type = 'Q'; 876 bezier_points[bi].svg_type = 'q'; 877 878 cx = px + parse_double (c[++i]); 879 880 if (svg_glyph) { 881 cy = py + parse_double (c[++i]); 882 } else { 883 cy = py - parse_double (c[++i]); 884 } 885 886 bezier_points[bi].x0 = cx; 887 bezier_points[bi].y0 = cy; 888 889 px2 = cx; 890 py2 = cy; 891 892 cx = px + parse_double (c[++i]); 893 894 if (svg_glyph) { 895 cy = py + parse_double (c[++i]); 896 } else { 897 cy = py - parse_double (c[++i]); 898 } 899 900 bezier_points[bi].x1 = cx; 901 bezier_points[bi].y1 = cy; 902 903 px = cx; 904 py = cy; 905 906 bi++; 907 } 908 } else if (c[i] == "Q") { 909 910 while (i + 4 < c.length && is_point (c[i + 1])) { 911 bezier_points[bi].type = 'Q'; 912 bezier_points[bi].svg_type = 'Q'; 913 914 cx = parse_double (c[++i]); 915 916 if (svg_glyph) { 917 cy = parse_double (c[++i]); 918 } else { 919 cy = -parse_double (c[++i]); 920 } 921 922 bezier_points[bi].x0 = cx; 923 bezier_points[bi].y0 = cy; 924 925 px2 = cx; 926 py2 = cy; 927 928 cx = parse_double (c[++i]); 929 930 if (svg_glyph) { 931 cy = parse_double (c[++i]); 932 } else { 933 cy = -parse_double (c[++i]); 934 } 935 936 px = cx; 937 py = cy; 938 939 bezier_points[bi].x1 = cx; 940 bezier_points[bi].y1 = cy; 941 942 bi++; 943 } 944 } else if (c[i] == "t") { 945 while (i + 2 < c.length && is_point (c[i + 1])) { 946 bezier_points[bi].type = 'Q'; 947 bezier_points[bi].svg_type = 't'; 948 949 // the first point is the reflection 950 cx = 2 * px - px2; 951 cy = 2 * py - py2; // if (svg_glyph) ? 952 953 bezier_points[bi].x0 = cx; 954 bezier_points[bi].y0 = cy; 955 956 px2 = cx; 957 py2 = cy; 958 959 cx = px + parse_double (c[++i]); 960 961 if (svg_glyph) { 962 cy = py + parse_double (c[++i]); 963 } else { 964 cy = py - parse_double (c[++i]); 965 } 966 967 px = cx; 968 py = cy; 969 970 bezier_points[bi].x1 = px; 971 bezier_points[bi].y1 = py; 972 973 bi++; 974 } 975 } else if (c[i] == "T") { 976 while (i + 2 < c.length && is_point (c[i + 1])) { 977 bezier_points[bi].type = 'Q'; 978 bezier_points[bi].svg_type = 'T'; 979 980 // the reflection 981 cx = 2 * px - px2; 982 cy = 2 * py - py2; // if (svg_glyph) ? 983 984 bezier_points[bi].x0 = cx; 985 bezier_points[bi].y0 = cy; 986 987 px2 = cx; 988 py2 = cy; 989 990 cx = parse_double (c[++i]); 991 992 if (svg_glyph) { 993 cy = parse_double (c[++i]); 994 } else { 995 cy = -parse_double (c[++i]); 996 } 997 998 px = cx; 999 py = cy; 1000 1001 bezier_points[bi].x1 = px; 1002 bezier_points[bi].y1 = py; 1003 1004 bi++; 1005 } 1006 } else if (c[i] == "s") { 1007 while (i + 4 < c.length && is_point (c[i + 1])) { 1008 bezier_points[bi].type = 'C'; 1009 bezier_points[bi].svg_type = 's'; 1010 1011 // the first point is the reflection 1012 cx = 2 * px - px2; 1013 cy = 2 * py - py2; // if (svg_glyph) ? 1014 1015 bezier_points[bi].x0 = cx; 1016 bezier_points[bi].y0 = cy; 1017 1018 cx = px + parse_double (c[++i]); 1019 1020 if (svg_glyph) { 1021 cy = py + parse_double (c[++i]); 1022 } else { 1023 cy = py - parse_double (c[++i]); 1024 } 1025 1026 px2 = cx; 1027 py2 = cy; 1028 1029 bezier_points[bi].x1 = px2; 1030 bezier_points[bi].y1 = py2; 1031 1032 cx = px + parse_double (c[++i]); 1033 1034 if (svg_glyph) { 1035 cy = py + parse_double (c[++i]); 1036 } else { 1037 cy = py - parse_double (c[++i]); 1038 } 1039 1040 bezier_points[bi].x2 = cx; 1041 bezier_points[bi].y2 = cy; 1042 1043 px = cx; 1044 py = cy; 1045 1046 bi++; 1047 } 1048 } else if (c[i] == "S") { 1049 while (i + 4 < c.length && is_point (c[i + 1])) { 1050 bezier_points[bi].type = 'C'; 1051 bezier_points[bi].svg_type = 'S'; 1052 1053 // the reflection 1054 cx = 2 * px - px2; 1055 cy = 2 * py - py2; // if (svg_glyph) ? 1056 1057 bezier_points[bi].x0 = cx; 1058 bezier_points[bi].y0 = cy; 1059 1060 // the other two are regular cubic points 1061 cx = parse_double (c[++i]); 1062 1063 if (svg_glyph) { 1064 cy = parse_double (c[++i]); 1065 } else { 1066 cy = -parse_double (c[++i]); 1067 } 1068 1069 px2 = cx; 1070 py2 = cy; 1071 1072 bezier_points[bi].x1 = px2; 1073 bezier_points[bi].y1 = py2; 1074 1075 cx = parse_double (c[++i]); 1076 1077 if (svg_glyph) { 1078 cy = parse_double (c[++i]); 1079 } else { 1080 cy = -parse_double (c[++i]); 1081 } 1082 1083 bezier_points[bi].x2 = cx; 1084 bezier_points[bi].y2 = cy; 1085 1086 px = cx; 1087 py = cy; 1088 1089 bi++; 1090 } 1091 } else if (c[i] == "a") { 1092 while (i + 7 < c.length && is_point (c[i + 1])) { 1093 arc_rx = parse_double (c[++i]); 1094 arc_ry = parse_double (c[++i]); 1095 1096 arc_rotation = parse_double (c[++i]); 1097 large_arc = parse_int (c[++i]); 1098 arc_sweep = parse_int (c[++i]); 1099 1100 cx = px + parse_double (c[++i]); 1101 1102 if (svg_glyph) { 1103 cy = py + parse_double (c[++i]); 1104 } else { 1105 cy = py - parse_double (c[++i]); 1106 } 1107 1108 arc_dest_x = cx; 1109 arc_dest_y = cy; 1110 1111 add_arc_points (bezier_points, ref bi, px, py, arc_rx, arc_ry, arc_rotation, large_arc == 1, arc_sweep == 1, cx, cy); 1112 1113 px = cx; 1114 py = cy; 1115 1116 1117 } 1118 } else if (i + 7 < c.length && c[i] == "A") { 1119 while (is_point (c[i + 1])) { 1120 arc_rx = parse_double (c[++i]); 1121 arc_ry = parse_double (c[++i]); 1122 1123 arc_rotation = parse_double (c[++i]); 1124 large_arc = parse_int (c[++i]); 1125 arc_sweep = parse_int (c[++i]); 1126 1127 cx = parse_double (c[++i]); 1128 1129 if (svg_glyph) { 1130 cy = parse_double (c[++i]); 1131 } else { 1132 cy = -parse_double (c[++i]); 1133 } 1134 1135 arc_dest_x = cx; 1136 arc_dest_y = cy; 1137 1138 add_arc_points (bezier_points, ref bi, px, py, arc_rx, arc_ry, arc_rotation, large_arc == 1, arc_sweep == 1, cx, cy); 1139 1140 px = cx; 1141 py = cy; 1142 1143 1144 } 1145 } else if (c[i] == "z") { 1146 bezier_points[bi].type = 'z'; 1147 bezier_points[bi].svg_type = 'z'; 1148 1149 bi++; 1150 } else if (c[i] == "Z") { 1151 bezier_points[bi].type = 'z'; 1152 bezier_points[bi].svg_type = 'z'; 1153 1154 bi++; 1155 } else if (c[i] == "") { 1156 } else if (c[i] == " ") { 1157 } else { 1158 warning (@"Unknown instruction: $(c[i])"); 1159 } 1160 } 1161 1162 if (bi == 0) { 1163 warning ("No points in path."); 1164 return path_list; 1165 } 1166 1167 // TODO: this code assumes that all paths are closed since stroke has not been implemented yet 1168 if (bezier_points[bi - 1].type != 'z') { 1169 bezier_points[bi].type = 'z'; 1170 bezier_points[bi].svg_type = 'z'; 1171 bi++; 1172 } 1173 1174 move_and_resize (bezier_points, bi, svg_glyph, units, glyph); 1175 1176 if (format == SvgFormat.ILLUSTRATOR) { 1177 path_list = create_paths_illustrator (bezier_points, bi); 1178 } else { 1179 path_list = create_paths_inkscape (bezier_points, bi); 1180 } 1181 1182 // TODO: Find out if it is possible to tie handles. 1183 return path_list; 1184 } 1185 1186 void move_and_resize (BezierPoints[] b, int num_b, bool svg_glyph, double units, Glyph glyph) { 1187 Font font = BirdFont.get_current_font (); 1188 1189 for (int i = 0; i < num_b; i++) { 1190 // resize all points 1191 b[i].x0 *= units; 1192 b[i].y0 *= units; 1193 b[i].x1 *= units; 1194 b[i].y1 *= units; 1195 b[i].x2 *= units; 1196 b[i].y2 *= units; 1197 1198 // move all points 1199 if (svg_glyph) { 1200 b[i].x0 += glyph.left_limit; 1201 b[i].y0 += font.base_line; 1202 b[i].x1 += glyph.left_limit; 1203 b[i].y1 += font.base_line; 1204 b[i].x2 += glyph.left_limit; 1205 b[i].y2 += font.base_line; 1206 } else { 1207 b[i].x0 += glyph.left_limit; 1208 b[i].y0 += font.top_position; 1209 b[i].x1 += glyph.left_limit; 1210 b[i].y1 += font.top_position; 1211 b[i].x2 += glyph.left_limit; 1212 b[i].y2 += font.top_position; 1213 } 1214 } 1215 } 1216 1217 void find_last_handle (int start_index, BezierPoints[] b, int num_b, out double left_x, out double left_y, out PointType last_type) { 1218 BezierPoints last = new BezierPoints (); 1219 1220 left_x = 0; 1221 left_y = 0; 1222 last_type = PointType.NONE; 1223 1224 return_if_fail (b.length != 0); 1225 return_if_fail (b[0].type != 'z'); 1226 return_if_fail (num_b < b.length); 1227 1228 for (int i = start_index; i < num_b; i++) { 1229 switch (b[i].type) { 1230 case 'Q': 1231 break; 1232 case 'C': 1233 break; 1234 case 'z': 1235 if (b[i - 1].type == 'Q') { 1236 return_if_fail (i >= 1); 1237 left_x = b[i - 1].x0; 1238 left_y = b[i - 1].y0; 1239 last_type = PointType.QUADRATIC; 1240 } else if (b[i - 1].type == 'C') { 1241 return_if_fail (i >= 1); 1242 left_x = b[i - 1].x1; 1243 left_y = b[i - 1].y1; 1244 last_type = PointType.CUBIC; 1245 } else if (b[i - 1].type == 'S') { 1246 return_if_fail (i >= 1); 1247 left_x = b[i - 1].x1; 1248 left_y = b[i - 1].y1; 1249 last_type = PointType.CUBIC; 1250 }else if (b[i - 1].type == 'L' || last.type == 'M') { 1251 return_if_fail (i >= 2); // FIXME: -2 can be C or L 1252 left_x = b[i - 2].x0 + (b[i - 1].x0 - b[i - 2].x0) / 3.0; 1253 left_y = b[i - 2].y0 + (b[i - 1].y0 - b[i - 2].y0) / 3.0; 1254 last_type = PointType.LINE_CUBIC; 1255 } else { 1256 warning (@"Unexpected type. $(b[i - 1])\n"); 1257 } 1258 return; 1259 default: 1260 break; 1261 } 1262 1263 last = b[i]; 1264 } 1265 1266 warning ("Expecting z"); 1267 } 1268 1269 PathList create_paths_inkscape (BezierPoints[] b, int num_b) { 1270 double last_x; 1271 double last_y; 1272 PointType last_type; 1273 Path path; 1274 PathList path_list = new PathList (); 1275 EditPoint ep = new EditPoint (); 1276 Gee.ArrayList<EditPoint> smooth_points = new Gee.ArrayList<EditPoint> (); 1277 1278 path = new Path (); 1279 1280 if (num_b == 0) { 1281 warning ("No SVG data"); 1282 return path_list; 1283 } 1284 1285 if (b[0].type != 'M') { 1286 warning ("Path must begin with M or m."); 1287 return path_list; 1288 } 1289 1290 find_last_handle (0, b, num_b, out last_x, out last_y, out last_type); 1291 1292 for (int i = 0; i < num_b; i++) { 1293 if (b[i].type == '\0') { 1294 warning ("Parser error."); 1295 return path_list; 1296 } 1297 1298 if (b[i].type == 'z') { 1299 path.close (); 1300 path.create_list (); 1301 path.recalculate_linear_handles (); 1302 path_list.add (path); 1303 path = new Path (); 1304 1305 if (i + 1 >= num_b) { 1306 break; 1307 } else { 1308 find_last_handle (i + 1, b, num_b, out last_x, out last_y, out last_type); 1309 } 1310 } 1311 1312 return_val_if_fail (i + 1 < num_b, path_list); 1313 1314 if (b[i].type == 'M') { 1315 ep = path.add (b[i].x0, b[i].y0); 1316 ep.set_point_type (PointType.CUBIC); 1317 1318 ep.get_left_handle ().set_point_type (PointType.LINE_CUBIC); 1319 1320 if (i == 0 || (b[i - 1].type == 'z')) { 1321 ep.get_left_handle ().set_point_type (last_type); 1322 ep.get_left_handle ().move_to_coordinate (last_x, last_y); 1323 } else { 1324 if (b[i - 1].type == 'C' || b[i - 1].type == 'S') { 1325 ep.get_left_handle ().set_point_type (PointType.CUBIC); 1326 ep.get_left_handle ().move_to_coordinate (b[i + 1].x1, b[i + 1].y1); 1327 } 1328 1329 if (b[i + 1].type == 'C' || b[i - 1].type == 'S') { 1330 ep.get_right_handle ().set_point_type (PointType.CUBIC); 1331 ep.get_right_handle ().move_to_coordinate (b[i + 1].x0, b[i + 1].y0); 1332 } else if (b[i + 1].type == 'L' || b[i + 1].type == 'M') { 1333 ep.get_right_handle ().set_point_type (PointType.LINE_CUBIC); 1334 } 1335 } 1336 } 1337 1338 if (b[i].type == 'L') { 1339 return_val_if_fail (i != 0, path_list); 1340 1341 ep = path.add (b[i].x0, b[i].y0); 1342 ep.set_point_type (PointType.CUBIC); 1343 ep.get_right_handle ().set_point_type (PointType.LINE_CUBIC); 1344 ep.get_left_handle ().set_point_type (PointType.LINE_CUBIC); 1345 1346 if (b[i + 1].type == 'L' || b[i + 1].type == 'M' || b[i + 1].type == 'z') { 1347 ep.get_right_handle ().set_point_type (PointType.LINE_CUBIC); 1348 } 1349 1350 if (b[i -1].type == 'L' || b[i - 1].type == 'M') { 1351 ep.get_left_handle ().set_point_type (PointType.LINE_CUBIC); 1352 } 1353 } 1354 1355 if (b[i].type == 'Q') { 1356 return_val_if_fail (i != 0, path_list); 1357 1358 ep.set_point_type (PointType.QUADRATIC); 1359 1360 ep.get_right_handle ().set_point_type (PointType.QUADRATIC); 1361 ep.get_right_handle ().move_to_coordinate (b[i].x0, b[i].y0); 1362 1363 if (b[i + 1].type != 'z') { 1364 ep = path.add (b[i].x1, b[i].y1); 1365 1366 ep.get_left_handle ().set_point_type (PointType.QUADRATIC); 1367 ep.get_left_handle ().move_to_coordinate (b[i].x0, b[i].y0); 1368 } 1369 } 1370 1371 if (b[i].type == 'C' || b[i].type == 'S') { 1372 return_val_if_fail (i != 0, path_list); 1373 1374 ep.set_point_type (PointType.CUBIC); 1375 1376 ep.get_right_handle ().set_point_type (PointType.CUBIC); 1377 ep.get_right_handle ().move_to_coordinate (b[i].x0, b[i].y0); 1378 1379 if (b[i].type == 'S') { 1380 smooth_points.add (ep); 1381 } 1382 1383 if (b[i + 1].type != 'z') { 1384 ep = path.add (b[i].x2, b[i].y2); 1385 1386 ep.get_left_handle ().set_point_type (PointType.CUBIC); 1387 ep.get_left_handle ().move_to_coordinate (b[i].x1, b[i].y1); 1388 } 1389 } 1390 } 1391 1392 foreach (EditPoint e in smooth_points) { 1393 e.set_point_type (PointType.LINE_DOUBLE_CURVE); 1394 e.get_right_handle ().set_point_type (PointType.LINE_DOUBLE_CURVE); 1395 e.get_left_handle ().set_point_type (PointType.LINE_DOUBLE_CURVE); 1396 } 1397 1398 foreach (EditPoint e in smooth_points) { 1399 e.recalculate_linear_handles (); 1400 } 1401 1402 1403 for (int i = 0; i < 3; i++) { 1404 foreach (EditPoint e in smooth_points) { 1405 e.set_tie_handle (true); 1406 e.process_tied_handle (); 1407 } 1408 } 1409 1410 if (path.points.size > 0) { 1411 path_list.add (path); 1412 } 1413 1414 foreach (Path p in path_list.paths) { 1415 p.remove_points_on_points (); 1416 } 1417 1418 return path_list; 1419 } 1420 1421 PathList create_paths_illustrator (BezierPoints[] b, int num_b) { 1422 Path path; 1423 PathList path_list = new PathList (); 1424 EditPoint ep; 1425 bool first_point = true; 1426 double first_left_x, first_left_y; 1427 Gee.ArrayList<EditPoint> smooth_points = new Gee.ArrayList<EditPoint> (); 1428 1429 if (num_b > b.length) { 1430 warning ("num_b > b.length: $num_b > $(b.length)"); 1431 return path_list; 1432 } 1433 1434 path = new Path (); 1435 1436 if (num_b <= 1) { 1437 warning ("No SVG data"); 1438 return path_list; 1439 } 1440 1441 first_left_x = 0; 1442 first_left_y = 0; 1443 1444 for (int i = 0; i < num_b; i++) { 1445 1446 if (b[i].type == '\0') { 1447 warning ("Parser error."); 1448 return path_list; 1449 } else if (b[i].type == 'z') { 1450 path.close (); 1451 path.create_list (); 1452 1453 if (b[1].type == 'C' || b[1].type == 'S') { 1454 return_val_if_fail (path.points.size != 0, path_list); 1455 ep = path.points.get (path.points.size - 1); 1456 ep.get_right_handle ().set_point_type (PointType.CUBIC); 1457 ep.get_right_handle ().move_to_coordinate (b[1].x0, b[1].y0); 1458 } 1459 1460 path.recalculate_linear_handles (); 1461 path_list.add (path); 1462 1463 path = new Path (); 1464 first_point = true; 1465 } else if (b[i].type == 'M') { 1466 } else if (b[i].type == 'L') { 1467 1468 if (first_point) { 1469 first_left_x = b[i].x0; 1470 first_left_y = b[i].y0; 1471 } 1472 1473 ep = path.add (b[i].x0, b[i].y0); 1474 ep.set_point_type (PointType.LINE_CUBIC); // TODO: quadratic 1475 ep.get_right_handle ().set_point_type (PointType.LINE_CUBIC); 1476 1477 if (b[i -1].type == 'L' || first_point) { 1478 //ep.get_left_handle ().set_point_type (PointType.LINE_CUBIC); 1479 } 1480 1481 if (b[i + 1].type == 'C' || b[i + 1].type == 'S') { 1482 return_val_if_fail (i + 1 < num_b, path_list); 1483 ep.get_right_handle ().set_point_type (PointType.CUBIC); 1484 ep.get_right_handle ().move_to_coordinate (b[i + 1].x0, b[i + 1].y0); 1485 } 1486 1487 first_point = false; 1488 } else if (b[i].type == 'Q') { 1489 warning ("Illustrator does not support quadratic control points."); 1490 warning (@"$(b[i])\n"); 1491 } else if (b[i].type == 'C' || b[i].type == 'S') { 1492 1493 if (first_point) { 1494 first_left_x = b[i].x0; 1495 first_left_y = b[i].y0; 1496 } 1497 1498 ep = path.add (b[i].x2, b[i].y2); 1499 ep.set_point_type (PointType.CUBIC); 1500 1501 ep.get_right_handle ().set_point_type (PointType.CUBIC); 1502 ep.get_left_handle ().set_point_type (PointType.CUBIC); 1503 1504 ep.get_left_handle ().move_to_coordinate (b[i].x1, b[i].y1); 1505 1506 if (b[i].type == 'S') { 1507 smooth_points.add (ep); 1508 } 1509 1510 if (b[i + 1].type != 'z') { 1511 ep.get_right_handle ().move_to_coordinate (b[i + 1].x0, b[i + 1].y0); 1512 } else { 1513 ep.get_right_handle ().move_to_coordinate (first_left_x, first_left_y); 1514 } 1515 1516 first_point = false; 1517 } else { 1518 warning ("Unknown control point type."); 1519 warning (@"$(b[i])\n"); 1520 } 1521 } 1522 1523 foreach (EditPoint e in smooth_points) { 1524 e.set_point_type (PointType.LINE_DOUBLE_CURVE); 1525 e.get_right_handle ().set_point_type (PointType.LINE_DOUBLE_CURVE); 1526 e.get_left_handle ().set_point_type (PointType.LINE_DOUBLE_CURVE); 1527 } 1528 1529 foreach (EditPoint e in smooth_points) { 1530 e.recalculate_linear_handles (); 1531 } 1532 1533 1534 for (int i = 0; i < 3; i++) { 1535 foreach (EditPoint e in smooth_points) { 1536 e.set_tie_handle (true); 1537 e.process_tied_handle (); 1538 } 1539 } 1540 1541 if (path.points.size > 0) { 1542 warning ("Open path."); 1543 path_list.add (path); 1544 } 1545 1546 foreach (Path p in path_list.paths) { 1547 p.remove_points_on_points (); 1548 } 1549 1550 return path_list; 1551 } 1552 1553 // TODO: implement a default svg parser 1554 1555 static int parse_int (string? s) { 1556 if (is_null (s)) { 1557 warning ("null instead of string"); 1558 return 0; 1559 } 1560 1561 if (!is_point ((!) s)) { 1562 warning (@"Expecting an integer got: $((!) s)"); 1563 return 0; 1564 } 1565 1566 return int.parse ((!) s); 1567 } 1568 1569 static double parse_double (string? s) { 1570 if (is_null (s)) { 1571 warning ("Got null instead of expected string."); 1572 return 0; 1573 } 1574 1575 if (!is_point ((!) s)) { 1576 warning (@"Expecting a double got: $((!) s)"); 1577 return 0; 1578 } 1579 1580 return double.parse ((!) s); 1581 } 1582 1583 static bool is_point (string? s) { 1584 if (s == null) { 1585 warning ("s is null"); 1586 return false; 1587 } 1588 1589 return double.try_parse ((!) s); 1590 } 1591 1592 Path parse_polygon_data (string polygon_points) { 1593 string data = add_separators (polygon_points); 1594 string[] c = data.split (" "); 1595 Path path; 1596 BezierPoints[] bezier_points = new BezierPoints[c.length + 1]; 1597 int bi; 1598 Glyph g; 1599 1600 bi = 0; 1601 for (int i = 0; i < c.length - 1; i += 2) { 1602 if (i + 1 >= c.length) { 1603 warning ("No y value."); 1604 break; 1605 } 1606 1607 if (bi >= bezier_points.length) { 1608 warning ("End of bezier_points"); 1609 break; 1610 } 1611 1612 bezier_points[bi] = new BezierPoints (); 1613 bezier_points[bi].type == 'L'; 1614 bezier_points[bi].x0 = parse_double (c[i]); 1615 bezier_points[bi].y0 = -parse_double (c[i + 1]); 1616 bi++; 1617 } 1618 1619 g = MainWindow.get_current_glyph (); 1620 move_and_resize (bezier_points, bi, false, 1, g); 1621 1622 path = new Path (); 1623 for (int i = 0; i < bi; i++) { 1624 path.add (bezier_points[i].x0, bezier_points[i].y0); 1625 } 1626 1627 path.close (); 1628 path.create_list (); 1629 path.recalculate_linear_handles (); 1630 1631 return path; 1632 } 1633 } 1634 1635 } 1636