The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

BirdFontFile.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/BirdFontFile.vala.
Merge branch 'master' of github.com:johanmattssonm/birdfont
1 /* 2 Copyright (C) 2013 2014 2015 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 using B; 15 16 namespace BirdFont { 17 18 /** 19 * BirdFont file format. This class can parse both the old ffi format 20 * and the new bf format. 21 */ 22 class BirdFontFile : GLib.Object { 23 24 Font font; 25 26 public static const int FORMAT_MAJOR = 2; 27 public static const int FORMAT_MINOR = 1; 28 29 public static const int MIN_FORMAT_MAJOR = 0; 30 public static const int MIN_FORMAT_MINOR = 0; 31 32 public BirdFontFile (Font f) { 33 font = f; 34 } 35 36 /** Load a new .bf file. 37 * @param path path to a valid .bf file 38 */ 39 public bool load (string path) { 40 string xml_data; 41 XmlParser parser; 42 bool ok = false; 43 44 try { 45 FileUtils.get_contents(path, out xml_data); 46 47 font.background_images.clear (); 48 font.font_file = path; 49 50 parser = new XmlParser (xml_data); 51 ok = load_xml (parser); 52 } catch (GLib.FileError e) { 53 warning (e.message); 54 } 55 56 return ok; 57 } 58 59 public bool load_part (string bfp_file) { 60 string xml_data; 61 XmlParser parser; 62 bool ok = false; 63 64 try { 65 FileUtils.get_contents(bfp_file, out xml_data); 66 parser = new XmlParser (xml_data); 67 ok = load_xml (parser); 68 } catch (GLib.FileError e) { 69 warning (e.message); 70 } 71 72 return ok; 73 } 74 75 /** Load a new .bf file. 76 * @param xml_data data for a valid .bf file 77 */ 78 public bool load_data (string xml_data) { 79 bool ok; 80 XmlParser parser; 81 82 font.font_file = "typeface.bf"; 83 parser = new XmlParser (xml_data); 84 ok = load_xml (parser); 85 86 return ok; 87 } 88 89 private bool load_xml (XmlParser parser) { 90 bool ok = true; 91 92 create_background_files (parser.get_root_tag ()); 93 ok = parse_file (parser.get_root_tag ()); 94 95 return ok; 96 } 97 98 public bool write_font_file (string path, bool backup = false) { 99 try { 100 DataOutputStream os; 101 File file; 102 103 file = File.new_for_path (path); 104 105 if (file.query_file_type (0) == FileType.DIRECTORY) { 106 warning (@"Can't save font. $path is a directory."); 107 return false; 108 } 109 110 if (file.query_exists ()) { 111 file.delete (); 112 } 113 114 os = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION)); 115 write_root_tag (os); 116 117 // this a backup of another font 118 if (backup) { 119 os.put_string ("\n"); 120 os.put_string (@"<!-- This is a backup of the following font: -->\n"); 121 os.put_string (@"<backup>$((!) font.get_path ())</backup>\n"); 122 } 123 124 os.put_string ("\n"); 125 write_description (os); 126 127 os.put_string ("\n"); 128 write_lines (os); 129 130 os.put_string ("\n"); 131 write_settings (os); 132 133 os.put_string ("\n"); 134 135 font.glyph_cache.for_each ((gc) => { 136 try { 137 write_glyph_collection (gc, os); 138 } catch (GLib.Error e) { 139 warning (e.message); 140 } 141 }); 142 143 os.put_string ("\n"); 144 145 write_images (os); 146 147 os.put_string ("\n"); 148 write_ligatures (os); 149 150 font.glyph_cache.for_each ((gc) => { 151 BackgroundImage bg; 152 153 try { 154 string data; 155 foreach (Glyph g in gc.glyphs) { 156 if (g.get_background_image () != null) { 157 bg = (!) g.get_background_image (); 158 data = bg.get_png_base64 (); 159 160 if (!bg.is_valid ()) { 161 continue; 162 } 163 164 write_image (os, bg.get_sha1 (), data); 165 } 166 } 167 168 foreach (BackgroundImage b in font.background_images) { 169 write_image (os, b.get_sha1 (), b.get_png_base64 ()); 170 } 171 } catch (GLib.Error ef) { 172 warning (@"Failed to save $path \n"); 173 warning (@"$(ef.message) \n"); 174 } 175 }); 176 177 os.put_string ("\n"); 178 write_spacing_classes (os); 179 write_alternates (os); 180 write_kerning (os); 181 write_closing_root_tag (os); 182 183 os.close (); 184 } catch (GLib.Error e) { 185 warning (@"Failed to save $path \n"); 186 warning (@"$(e.message) \n"); 187 return false; 188 } 189 190 return true; 191 } 192 193 public void write_alternates (DataOutputStream os) throws GLib.Error { 194 foreach (Alternate alternate in font.alternates.alternates) { 195 string character = @"$(Font.to_hex (alternate.character))"; 196 string tag = alternate.tag; 197 198 foreach (string alt in alternate.alternates) { 199 os.put_string (@"<alternate "); 200 os.put_string (@"character=\"$character\" "); 201 os.put_string (@"alternate=\"$alt\" "); 202 os.put_string (@"tag=\"$(tag)\" />\n"); 203 } 204 } 205 } 206 207 public void write_images (DataOutputStream os) throws GLib.Error { 208 string glyph_name; 209 210 if (font.background_images.size > 0) { 211 os.put_string (@"<images>\n"); 212 213 foreach (BackgroundImage b in font.background_images) { 214 215 if (b.name == "") { 216 warning ("No name."); 217 } 218 219 os.put_string ("\t<image "); 220 os.put_string (@"name=\"$(b.name)\" "); 221 os.put_string (@"sha1=\"$(b.get_sha1 ())\" "); 222 os.put_string (@"x=\"$(b.img_x)\" "); 223 os.put_string (@"y=\"$(b.img_y)\" "); 224 os.put_string (@"scale_x=\"$(b.img_scale_x)\" "); 225 os.put_string (@"scale_y=\"$(b.img_scale_y)\" "); 226 os.put_string (@"rotation=\"$(b.img_rotation)\" "); 227 os.put_string (">\n"); 228 229 foreach (BackgroundSelection selection in b.selections) { 230 os.put_string ("\t\t<selection "); 231 os.put_string (@"x=\"$(selection.x)\" "); 232 os.put_string (@"y=\"$(selection.y)\" "); 233 os.put_string (@"width=\"$(selection.w)\" "); 234 os.put_string (@"height=\"$(selection.h)\" "); 235 236 if (selection.assigned_glyph != null) { 237 glyph_name = (!) selection.assigned_glyph; 238 os.put_string (@"glyph=\"$(glyph_name)\" "); 239 } 240 241 os.put_string ("/>\n"); 242 } 243 244 os.put_string (@"\t</image>\n"); 245 } 246 247 os.put_string (@"</images>\n"); 248 os.put_string ("\n"); 249 } 250 } 251 252 public void write_image (DataOutputStream os, string sha1, string data) throws GLib.Error { 253 os.put_string (@"<background-image sha1=\""); 254 os.put_string (sha1); 255 os.put_string ("\" "); 256 os.put_string (" data=\""); 257 os.put_string (data); 258 os.put_string ("\" />\n"); 259 } 260 261 public void write_root_tag (DataOutputStream os) throws GLib.Error { 262 os.put_string ("""<?xml version="1.0" encoding="utf-8" standalone="yes"?>"""); 263 os.put_string ("\n"); 264 os.put_string ("<font>\n"); 265 os.put_string (@"<format>$FORMAT_MAJOR.$FORMAT_MINOR</format>\n"); 266 } 267 268 public void write_closing_root_tag (DataOutputStream os) throws GLib.Error { 269 os.put_string ("</font>\n"); 270 } 271 272 public void write_spacing_classes (DataOutputStream os) throws GLib.Error { 273 SpacingData s = font.get_spacing (); 274 275 foreach (SpacingClass sc in s.classes) { 276 os.put_string ("<spacing "); 277 os.put_string ("first=\""); 278 os.put_string (Font.to_hex (sc.first.get_char ())); 279 os.put_string ("\" "); 280 281 os.put_string ("next=\""); 282 os.put_string (Font.to_hex (sc.next.get_char ())); 283 os.put_string ("\" "); 284 285 os.put_string ("/>\n"); 286 } 287 } 288 289 public void write_kerning (DataOutputStream os) throws GLib.Error { 290 uint num_kerning_pairs; 291 string range; 292 KerningClasses classes = font.get_kerning_classes (); 293 294 num_kerning_pairs = classes.classes_first.size; 295 296 for (int i = 0; i < num_kerning_pairs; i++) { 297 range = classes.classes_first.get (i).get_all_ranges (); 298 299 os.put_string ("<kerning "); 300 os.put_string ("left=\""); 301 os.put_string (range); 302 os.put_string ("\" "); 303 304 range = classes.classes_last.get (i).get_all_ranges (); 305 306 os.put_string ("right=\""); 307 os.put_string (range); 308 os.put_string ("\" "); 309 310 os.put_string ("hadjustment=\""); 311 os.put_string (round (classes.classes_kerning.get (i).val)); 312 os.put_string ("\" />\n"); 313 } 314 315 classes.get_single_position_pairs ((l, r, k) => { 316 try { 317 os.put_string ("<kerning "); 318 os.put_string ("left=\""); 319 os.put_string (l); 320 os.put_string ("\" "); 321 322 os.put_string ("right=\""); 323 os.put_string (r); 324 os.put_string ("\" "); 325 326 os.put_string ("hadjustment=\""); 327 os.put_string (round (k)); 328 os.put_string ("\" />\n"); 329 } catch (GLib.Error e) { 330 warning (@"$(e.message) \n"); 331 } 332 }); 333 } 334 335 public void write_settings (DataOutputStream os) throws GLib.Error { 336 foreach (string gv in font.grid_width) { 337 os.put_string (@"<grid width=\"$(gv)\"/>\n"); 338 } 339 340 if (GridTool.sizes.size > 0) { 341 os.put_string ("\n"); 342 } 343 344 os.put_string (@"<background scale=\"$(font.background_scale)\" />\n"); 345 } 346 347 public void write_description (DataOutputStream os) throws GLib.Error { 348 os.put_string (@"<postscript_name>$(Markup.escape_text (font.postscript_name))</postscript_name>\n"); 349 os.put_string (@"<name>$(Markup.escape_text (font.name))</name>\n"); 350 os.put_string (@"<subfamily>$(Markup.escape_text (font.subfamily))</subfamily>\n"); 351 os.put_string (@"<bold>$(font.bold)</bold>\n"); 352 os.put_string (@"<italic>$(font.italic)</italic>\n"); 353 os.put_string (@"<full_name>$(Markup.escape_text (font.full_name))</full_name>\n"); 354 os.put_string (@"<unique_identifier>$(Markup.escape_text (font.unique_identifier))</unique_identifier>\n"); 355 os.put_string (@"<version>$(Markup.escape_text (font.version))</version>\n"); 356 os.put_string (@"<description>$(Markup.escape_text (font.description))</description>\n"); 357 os.put_string (@"<copyright>$(Markup.escape_text (font.copyright))</copyright>\n"); 358 os.put_string (@"<license>$(Markup.escape_text (font.license))</license>\n"); 359 os.put_string (@"<license_url>$(Markup.escape_text (font.license_url))</license_url>\n"); 360 os.put_string (@"<weight>$(font.weight)</weight>\n"); 361 os.put_string (@"<units_per_em>$(font.units_per_em)</units_per_em>\n"); 362 os.put_string (@"<trademark>$(Markup.escape_text (font.trademark))</trademark>\n"); 363 os.put_string (@"<manufacturer>$(Markup.escape_text (font.manufacturer))</manufacturer>\n"); 364 os.put_string (@"<designer>$(Markup.escape_text (font.designer))</designer>\n"); 365 os.put_string (@"<vendor_url>$(Markup.escape_text (font.vendor_url))</vendor_url>\n"); 366 os.put_string (@"<designer_url>$(Markup.escape_text (font.designer_url))</designer_url>\n"); 367 368 } 369 370 public void write_lines (DataOutputStream os) throws GLib.Error { 371 os.put_string ("<horizontal>\n"); 372 os.put_string (@"\t<top_limit>$(round (font.top_limit))</top_limit>\n"); 373 os.put_string (@"\t<top_position>$(round (font.top_position))</top_position>\n"); 374 os.put_string (@"\t<x-height>$(round (font.xheight_position))</x-height>\n"); 375 os.put_string (@"\t<base_line>$(round (font.base_line))</base_line>\n"); 376 os.put_string (@"\t<bottom_position>$(round (font.bottom_position))</bottom_position>\n"); 377 os.put_string (@"\t<bottom_limit>$(round (font.bottom_limit))</bottom_limit>\n"); 378 379 foreach (Line guide in font.custom_guides) { 380 os.put_string (@"\t<custom_guide label=\"$(guide.label)\">$(round (guide.pos))</custom_guide>\n"); 381 } 382 383 os.put_string ("</horizontal>\n"); 384 } 385 386 public void write_glyph_collection_start (GlyphCollection gc, DataOutputStream os) throws GLib.Error { 387 os.put_string ("<collection "); 388 389 if (gc.is_unassigned ()) { 390 os.put_string (@"name=\"$(gc.get_current ().get_name ())\""); 391 } else { 392 os.put_string (@"unicode=\"$(Font.to_hex (gc.get_current ().unichar_code))\""); 393 } 394 395 os.put_string (">\n"); 396 } 397 398 public void write_glyph_collection_end (DataOutputStream os) throws GLib.Error { 399 os.put_string ("</collection>\n"); 400 } 401 402 public void write_selected (GlyphCollection gc, DataOutputStream os) throws GLib.Error { 403 os.put_string (@"\t<selected id=\"$(gc.get_current ().version_id)\"/>\n"); 404 } 405 406 public void write_glyph_collection (GlyphCollection gc, DataOutputStream os) throws GLib.Error { 407 write_glyph_collection_start (gc, os); 408 write_selected (gc, os); 409 foreach (Glyph g in gc.glyphs) { 410 write_glyph (g, os); 411 } 412 write_glyph_collection_end (os); 413 } 414 415 public void write_glyph (Glyph g, DataOutputStream os) throws GLib.Error { 416 os.put_string (@"\t<glyph id=\"$(g.version_id)\" left=\"$(double_to_string (g.left_limit))\" right=\"$(double_to_string (g.right_limit))\">\n"); 417 418 foreach (Layer layer in g.layers.subgroups) { 419 write_layer (layer, os); 420 } 421 422 write_glyph_background (g, os); 423 os.put_string ("\t</glyph>\n"); 424 } 425 426 void write_layer (Layer layer, DataOutputStream os) throws GLib.Error { 427 string data; 428 429 // FIXME: name etc. 430 os.put_string (@"\t\t<layer name= \"$(layer.name)\" visible=\"$(layer.visible)\">\n"); 431 432 foreach (Path p in layer.get_all_paths ().paths) { 433 data = get_point_data (p); 434 if (data != "") { 435 os.put_string (@"\t\t\t<path "); 436 437 if (p.stroke != 0) { 438 os.put_string (@"stroke=\"$(double_to_string (p.stroke))\" "); 439 } 440 441 if (p.line_cap != LineCap.BUTT) { 442 if (p.line_cap == LineCap.ROUND) { 443 os.put_string (@"cap=\"round\" "); 444 } else if (p.line_cap == LineCap.SQUARE) { 445 os.put_string (@"cap=\"square\" "); 446 } 447 } 448 449 if (p.skew != 0) { 450 os.put_string (@"skew=\"$(double_to_string (p.skew))\" "); 451 } 452 453 os.put_string (@"data=\"$(data)\" />\n"); 454 } 455 } 456 457 os.put_string ("\t\t</layer>\n"); 458 } 459 460 public static string double_to_string (double n) { 461 string d = @"$n"; 462 return d.replace (",", "."); 463 } 464 465 /** Get control points in BirdFont format. This function is uses a 466 * cartesian coordinate system with origo in the middle. 467 * 468 * Instructions: 469 * S - Start point for a quadratic path 470 * B - Start point for a cubic path 471 * L - Line with quadratic control points 472 * M - Line with cubic control points 473 * Q - Quadratic Bézier path 474 * D - Two quadratic off curve points 475 * C - Cubic Bézier path 476 * 477 * T - Tie handles for previous curve 478 * 479 * O - Keep open (do not close path) 480 */ 481 public static string get_point_data (Path pl) { 482 StringBuilder data = new StringBuilder (); 483 EditPoint? n = null; 484 EditPoint m; 485 int i = 0; 486 487 if (pl.points.size == 0) { 488 return data.str; 489 } 490 491 if (pl.points.size == 1) { 492 add_start_point (pl.points.get (0), data); 493 data.append (" "); 494 add_next_point (pl.points.get (0), pl.points.get (0), data); 495 496 if (pl.is_open ()) { 497 data.append (" O"); 498 } 499 500 return data.str; 501 } 502 503 if (pl.points.size == 2) { 504 add_start_point (pl.points.get (0), data); 505 data.append (" "); 506 add_next_point (pl.points.get (0), pl.points.get (pl.points.size - 1), data); 507 data.append (" "); 508 add_next_point (pl.points.get (pl.points.size - 1), pl.points.get (0), data); 509 510 if (pl.is_open ()) { 511 data.append (" O"); 512 } 513 514 return data.str; 515 } 516 517 pl.create_list (); 518 519 foreach (EditPoint e in pl.points) { 520 if (i == 0) { 521 add_start_point (e, data); 522 i++; 523 n = e; 524 continue; 525 } 526 527 m = (!) n; 528 data.append (" "); 529 add_next_point (m, e, data); 530 531 n = e; 532 i++; 533 } 534 535 data.append (" "); 536 m = pl.points.get (0); 537 add_next_point ((!) n, m, data); 538 539 if (pl.is_open ()) { 540 data.append (" O"); 541 } 542 543 return data.str; 544 } 545 546 private static void add_start_point (EditPoint e, StringBuilder data) { 547 if (e.type == PointType.CUBIC || e.type == PointType.LINE_CUBIC) { 548 add_cubic_start (e, data); 549 } else { 550 add_quadratic_start (e, data); 551 } 552 } 553 554 private static string round (double d) { 555 char[] b = new char [22]; 556 unowned string s = d.format (b, "%.10f"); 557 string n = s.dup (); 558 559 n = n.replace (",", "."); 560 561 if (n == "-0.0000000000") { 562 n = "0.0000000000"; 563 } 564 565 return n; 566 } 567 568 private static void add_quadratic_start (EditPoint p, StringBuilder data) { 569 string x, y; 570 571 x = round (p.x); 572 y = round (p.y); 573 574 data.append (@"S $(x),$(y)"); 575 } 576 577 private static void add_cubic_start (EditPoint p, StringBuilder data) { 578 string x, y; 579 580 x = round (p.x); 581 y = round (p.y); 582 583 data.append (@"B $(x),$(y)"); 584 } 585 586 private static void add_line_to (EditPoint p, StringBuilder data) { 587 string x, y; 588 589 x = round (p.x); 590 y = round (p.y); 591 592 data.append (@"L $(x),$(y)"); 593 } 594 595 private static void add_cubic_line_to (EditPoint p, StringBuilder data) { 596 string x, y; 597 598 x = round (p.x); 599 y = round (p.y); 600 601 data.append (@"M $(x),$(y)"); 602 } 603 604 private static void add_quadratic (EditPoint start, EditPoint end, StringBuilder data) { 605 EditPointHandle h = start.get_right_handle (); 606 string x0, y0, x1, y1; 607 608 x0 = round (h.x); 609 y0 = round (h.y); 610 x1 = round (end.x); 611 y1 = round (end.y); 612 613 data.append (@"Q $(x0),$(y0) $(x1),$(y1)"); 614 } 615 616 private static void add_double (EditPoint start, EditPoint end, StringBuilder data) { 617 EditPointHandle h1 = start.get_right_handle (); 618 EditPointHandle h2 = end.get_left_handle (); 619 string x0, y0, x1, y1, x2, y2; 620 621 x0 = round (h1.x); 622 y0 = round (h1.y); 623 x1 = round (h2.x); 624 y1 = round (h2.y); 625 x2 = round (end.x); 626 y2 = round (end.y); 627 628 data.append (@"D $(x0),$(y0) $(x1),$(y1) $(x2),$(y2)"); 629 } 630 631 private static void add_cubic (EditPoint start, EditPoint end, StringBuilder data) { 632 EditPointHandle h1 = start.get_right_handle (); 633 EditPointHandle h2 = end.get_left_handle (); 634 string x0, y0, x1, y1, x2, y2; 635 636 x0 = round (h1.x); 637 y0 = round (h1.y); 638 x1 = round (h2.x); 639 y1 = round (h2.y); 640 x2 = round (end.x); 641 y2 = round (end.y); 642 643 data.append (@"C $(x0),$(y0) $(x1),$(y1) $(x2),$(y2)"); 644 } 645 646 private static void add_next_point (EditPoint start, EditPoint end, StringBuilder data) { 647 if (start.right_handle.type == PointType.LINE_QUADRATIC && end.left_handle.type == PointType.LINE_QUADRATIC) { 648 add_line_to (end, data); 649 } else if (start.right_handle.type == PointType.LINE_DOUBLE_CURVE && end.left_handle.type == PointType.LINE_DOUBLE_CURVE) { 650 add_line_to (end, data); 651 } else if (start.right_handle.type == PointType.LINE_CUBIC && end.left_handle.type == PointType.LINE_CUBIC) { 652 add_cubic_line_to (end, data); 653 } else if (end.left_handle.type == PointType.DOUBLE_CURVE || start.right_handle.type == PointType.DOUBLE_CURVE) { 654 add_double (start, end, data); 655 } else if (end.left_handle.type == PointType.QUADRATIC || start.right_handle.type == PointType.QUADRATIC) { 656 add_quadratic (start, end, data); 657 } else if (end.left_handle.type == PointType.CUBIC || start.right_handle.type == PointType.CUBIC) { 658 add_cubic (start, end, data); 659 } else if (start.right_handle.type == PointType.LINE_CUBIC && end.left_handle.type == PointType.LINE_DOUBLE_CURVE) { 660 add_line_to (end, data); 661 } else if (start.right_handle.type == PointType.LINE_DOUBLE_CURVE && end.left_handle.type == PointType.LINE_CUBIC) { 662 add_line_to (end, data); 663 } else { 664 warning (@"Unknown point type. \nStart handle: $(start.right_handle.type) \nStop handle: $(end.left_handle.type)"); 665 add_cubic (start, end, data); 666 } 667 668 if (end.tie_handles) { 669 data.append (" "); 670 data.append (@"T"); 671 } 672 } 673 674 private void write_glyph_background (Glyph g, DataOutputStream os) throws GLib.Error { 675 BackgroundImage? bg; 676 BackgroundImage background_image; 677 double pos_x, pos_y, scale_x, scale_y, rotation; 678 679 bg = g.get_background_image (); 680 681 // FIXME: use the coordinate system 682 if (bg != null) { 683 background_image = (!) bg; 684 685 pos_x = background_image.img_x; 686 pos_y = background_image.img_y; 687 688 scale_x = background_image.img_scale_x; 689 scale_y = background_image.img_scale_y; 690 691 rotation = background_image.img_rotation; 692 693 if (background_image.is_valid ()) { 694 os.put_string (@"\t\t<background sha1=\"$(background_image.get_sha1 ())\" x=\"$pos_x\" y=\"$pos_y\" scale_x=\"$scale_x\" scale_y=\"$scale_y\" rotation=\"$rotation\"/>\n"); 695 } 696 } 697 } 698 699 private bool parse_file (Tag tag) { 700 foreach (Tag t in tag) { 701 // this is a backup file, but with a path to the original file 702 if (t.get_name () == "backup") { 703 font.font_file = t.get_content (); 704 } 705 706 // file format version 707 if (t.get_name () == "format") { 708 parse_format (t); 709 } 710 711 // glyph format 712 if (t.get_name () == "collection") { 713 parse_glyph_collection (t); 714 } 715 716 // horizontal lines in the new format 717 if (t.get_name () == "horizontal") { 718 parse_horizontal_lines (t); 719 } 720 721 // grid buttons 722 if (t.get_name () == "grid") { 723 parse_grid (t); 724 } 725 726 if (t.get_name () == "background") { 727 parse_background (t); 728 } 729 730 if (t.get_name () == "postscript_name") { 731 font.postscript_name = XmlParser.decode (t.get_content ()); 732 } 733 734 if (t.get_name () == "name") { 735 font.name = XmlParser.decode (t.get_content ()); 736 } 737 738 if (t.get_name () == "subfamily") { 739 font.subfamily = XmlParser.decode (t.get_content ()); 740 } 741 742 if (t.get_name () == "bold") { 743 font.bold = bool.parse (t.get_content ()); 744 } 745 746 if (t.get_name () == "italic") { 747 font.italic = bool.parse (t.get_content ()); 748 } 749 750 if (t.get_name () == "full_name") { 751 font.full_name = XmlParser.decode (t.get_content ()); 752 } 753 754 if (t.get_name () == "unique_identifier") { 755 font.unique_identifier = XmlParser.decode (t.get_content ()); 756 } 757 758 if (t.get_name () == "version") { 759 font.version = XmlParser.decode (t.get_content ()); 760 } 761 762 if (t.get_name () == "description") { 763 font.description = XmlParser.decode (t.get_content ()); 764 } 765 766 if (t.get_name () == "copyright") { 767 font.copyright = XmlParser.decode (t.get_content ()); 768 } 769 770 if (t.get_name () == "license") { 771 font.license = XmlParser.decode (t.get_content ()); 772 } 773 774 if (t.get_name () == "license_url") { 775 font.license_url = XmlParser.decode (t.get_content ()); 776 } 777 778 if (t.get_name () == "trademark") { 779 font.trademark = XmlParser.decode (t.get_content ()); 780 } 781 782 if (t.get_name () == "manufacturer") { 783 font.manufacturer = XmlParser.decode (t.get_content ()); 784 } 785 786 if (t.get_name () == "designer") { 787 font.designer = XmlParser.decode (t.get_content ()); 788 } 789 790 if (t.get_name () == "vendor_url") { 791 font.vendor_url = XmlParser.decode (t.get_content ()); 792 } 793 794 if (t.get_name () == "designer_url") { 795 font.designer_url = XmlParser.decode (t.get_content ()); 796 } 797 798 if (t.get_name () == "kerning") { 799 parse_kerning (t); 800 } 801 802 if (t.get_name () == "spacing") { 803 parse_spacing_class (t); 804 } 805 806 if (t.get_name () == "ligature") { 807 parse_ligature (t); 808 } 809 810 if (t.get_name () == "contextual") { 811 parse_contectual_ligature (t); 812 } 813 814 if (t.get_name () == "weight") { 815 font.weight = int.parse (t.get_content ()); 816 } 817 818 if (t.get_name () == "units_per_em") { 819 font.units_per_em = int.parse (t.get_content ()); 820 } 821 822 if (t.get_name () == "images") { 823 parse_images (t); 824 } 825 826 if (t.get_name () == "alternate") { 827 parse_alternate (t); 828 } 829 } 830 831 return true; 832 } 833 834 public void parse_alternate (Tag tag) { 835 unichar character = '\0'; 836 string alt = ""; 837 string alt_tag = ""; 838 839 foreach (Attribute attribute in tag.get_attributes ()) { 840 if (attribute.get_name () == "character") { 841 character = Font.to_unichar (attribute.get_content ()); 842 } 843 844 if (attribute.get_name () == "alternate") { 845 alt = unserialize (attribute.get_content ()); 846 } 847 848 if (attribute.get_name () == "tag") { 849 alt_tag = attribute.get_content (); 850 } 851 } 852 853 font.add_alternate (character, alt, alt_tag); 854 } 855 856 public void parse_format (Tag tag) { 857 string[] v = tag.get_content ().split ("."); 858 859 if (v.length != 2) { 860 warning ("Bad format string."); 861 return; 862 } 863 864 font.format_major = int.parse (v[0]); 865 font.format_major = int.parse (v[1]); 866 } 867 868 public void parse_images (Tag tag) { 869 BackgroundImage? new_img; 870 BackgroundImage img; 871 string name; 872 File img_file; 873 double x, y, scale_x, scale_y, rotation; 874 875 foreach (Tag t in tag) { 876 if (t.get_name () == "image") { 877 name = ""; 878 new_img = null; 879 img_file = get_child (font.get_backgrounds_folder (), "parts"); 880 881 x = 0; 882 y = 0; 883 scale_x = 0; 884 scale_y = 0; 885 rotation = 0; 886 887 foreach (Attribute attr in t.get_attributes ()) { 888 if (attr.get_name () == "sha1") { 889 img_file = get_child (img_file, attr.get_content () + ".png"); 890 891 if (!img_file.query_exists ()) { 892 warning (@"Background file has not been created yet. $((!) img_file.get_path ())"); 893 } 894 895 new_img = new BackgroundImage ((!) img_file.get_path ()); 896 } 897 898 if (attr.get_name () == "name") { 899 name = attr.get_content (); 900 } 901 902 if (attr.get_name () == "x") { 903 x = parse_double (attr.get_content ()); 904 } 905 906 if (attr.get_name () == "y") { 907 y = parse_double (attr.get_content ()); 908 } 909 910 if (attr.get_name () == "scale_x") { 911 scale_x = parse_double (attr.get_content ()); 912 } 913 914 if (attr.get_name () == "scale_y") { 915 scale_y = parse_double (attr.get_content ()); 916 } 917 918 if (attr.get_name () == "rotation") { 919 rotation = parse_double (attr.get_content ()); 920 } 921 } 922 923 if (new_img != null && name != "") { 924 img = (!) new_img; 925 img.name = name; 926 927 Toolbox.background_tools.add_image (img); 928 parse_image_selections (img, t); 929 930 img.img_x = x; 931 img.img_y = y; 932 img.img_scale_x = scale_x; 933 img.img_scale_y = scale_y; 934 img.img_rotation = rotation; 935 } else { 936 warning (@"No image found, name: $name"); 937 } 938 } 939 } 940 } 941 942 private void parse_image_selections (BackgroundImage image, Tag tag) { 943 double x, y, w, h; 944 string? assigned_glyph; 945 BackgroundSelection s; 946 947 foreach (Tag t in tag) { 948 if (t.get_name () == "selection") { 949 950 x = 0; 951 y = 0; 952 w = 0; 953 h = 0; 954 assigned_glyph = null; 955 956 foreach (Attribute attr in t.get_attributes ()) { 957 if (attr.get_name () == "x") { 958 x = parse_double (attr.get_content ()); 959 } 960 961 if (attr.get_name () == "y") { 962 y = parse_double (attr.get_content ()); 963 } 964 965 if (attr.get_name () == "width") { 966 w = parse_double (attr.get_content ()); 967 } 968 969 if (attr.get_name () == "height") { 970 h = parse_double (attr.get_content ()); 971 } 972 973 if (attr.get_name () == "glyph") { 974 assigned_glyph = attr.get_content (); 975 } 976 } 977 978 s = new BackgroundSelection (null, image, x, y, w, h); 979 s.assigned_glyph = assigned_glyph; 980 981 image.selections.add (s); 982 } 983 } 984 } 985 986 private void create_background_files (Tag root) { 987 foreach (Tag child in root) { 988 if (child.get_name () == "name") { 989 font.set_name (child.get_content ()); 990 } 991 992 if (child.get_name () == "background-image") { 993 parse_background_image (child); 994 } 995 } 996 } 997 998 public static string serialize_attribute (string s) { 999 string n = s.replace ("\"", "quote"); 1000 n = n.replace ("&", "ampersand"); 1001 return n; 1002 } 1003 1004 public static string unserialize (string s) { 1005 StringBuilder b; 1006 string r; 1007 r = s.replace ("quote", "\""); 1008 r = r.replace ("ampersand", "&"); 1009 1010 if (s.has_prefix ("U+")) { 1011 b = new StringBuilder (); 1012 b.append_unichar (Font.to_unichar (s)); 1013 r = @"$(b.str)"; 1014 } 1015 1016 return r; 1017 } 1018 1019 public static string serialize_unichar (unichar c) { 1020 return GlyphRange.get_serialized_char (c); 1021 } 1022 1023 private void parse_spacing_class (Tag tag) { 1024 string first, next; 1025 SpacingData spacing = font.get_spacing (); 1026 1027 first = ""; 1028 next = ""; 1029 1030 foreach (Attribute attr in tag.get_attributes ()) { 1031 if (attr.get_name () == "first") { 1032 first = (!) Font.to_unichar (attr.get_content ()).to_string (); 1033 } 1034 1035 if (attr.get_name () == "next") { 1036 next = (!) Font.to_unichar (attr.get_content ()).to_string (); 1037 } 1038 } 1039 1040 spacing.add_class (first, next); 1041 } 1042 1043 private void parse_kerning (Tag tag) { 1044 GlyphRange range_left, range_right; 1045 double hadjustment = 0; 1046 KerningRange kerning_range; 1047 1048 try { 1049 range_left = new GlyphRange (); 1050 range_right = new GlyphRange (); 1051 1052 foreach (Attribute attr in tag.get_attributes ()) { 1053 if (attr.get_name () == "left") { 1054 range_left.parse_ranges (unserialize (attr.get_content ())); 1055 } 1056 1057 if (attr.get_name () == "right") { 1058 range_right.parse_ranges (unserialize (attr.get_content ())); 1059 } 1060 1061 if (attr.get_name () == "hadjustment") { 1062 hadjustment = double.parse (attr.get_content ()); 1063 } 1064 } 1065 1066 if (range_left.get_length () > 1) { 1067 kerning_range = new KerningRange (font); 1068 kerning_range.set_ranges (range_left.get_all_ranges ()); 1069 KerningTools.add_unique_class (kerning_range); 1070 } 1071 1072 if (range_right.get_length () > 1) { 1073 kerning_range = new KerningRange (font); 1074 kerning_range.set_ranges (range_right.get_all_ranges ()); 1075 KerningTools.add_unique_class (kerning_range); 1076 } 1077 1078 font.get_kerning_classes ().set_kerning (range_left, range_right, hadjustment); 1079 1080 } catch (MarkupError e) { 1081 warning (e.message); 1082 } 1083 } 1084 1085 private void parse_background_image (Tag tag) { 1086 string file = ""; 1087 string data = ""; 1088 1089 File img_dir; 1090 File img_file; 1091 FileOutputStream file_stream; 1092 DataOutputStream png_stream; 1093 1094 tag.reparse (); 1095 foreach (Attribute attr in tag.get_attributes ()) { 1096 if (attr.get_name () == "sha1") { 1097 file = attr.get_content (); 1098 } 1099 1100 if (attr.get_name () == "data") { 1101 data = attr.get_content (); 1102 } 1103 } 1104 1105 if (!font.get_backgrounds_folder ().query_exists ()) { 1106 DirUtils.create ((!) font.get_backgrounds_folder ().get_path (), 0755); 1107 } 1108 1109 img_dir = get_child (font.get_backgrounds_folder (), "parts"); 1110 1111 if (!img_dir.query_exists ()) { 1112 DirUtils.create ((!) img_dir.get_path (), 0755); 1113 } 1114 1115 img_file = get_child (img_dir, @"$(file).png"); 1116 1117 if (img_file.query_exists ()) { 1118 return; 1119 } 1120 1121 try { 1122 file_stream = img_file.create (FileCreateFlags.REPLACE_DESTINATION); 1123 png_stream = new DataOutputStream (file_stream); 1124 1125 png_stream.write (Base64.decode (data)); 1126 png_stream.close (); 1127 } catch (GLib.Error e) { 1128 warning (e.message); 1129 } 1130 } 1131 1132 private void parse_background (Tag tag) { 1133 foreach (Attribute attr in tag.get_attributes ()) { 1134 if (attr.get_name () == "scale") { 1135 font.background_scale = attr.get_content (); 1136 } 1137 } 1138 } 1139 1140 private void parse_grid (Tag tag) { 1141 foreach (Attribute attr in tag.get_attributes ()) { 1142 if (attr.get_name () == "width") { 1143 font.grid_width.add (attr.get_content ()); 1144 } 1145 } 1146 } 1147 1148 private void parse_horizontal_lines (Tag tag) { 1149 Line line; 1150 string label; 1151 double position; 1152 1153 foreach (Tag t in tag) { 1154 if (t.get_name () == "top_limit" && t.get_content () != "") { 1155 font.top_limit = parse_double_from_node (t); 1156 } 1157 1158 if (t.get_name () == "top_position" && t.get_content () != "") { 1159 font.top_position = parse_double_from_node (t); 1160 } 1161 1162 if (t.get_name () == "x-height" && t.get_content () != "") { 1163 font.xheight_position = parse_double_from_node (t); 1164 } 1165 1166 if (t.get_name () == "base_line" && t.get_content () != "") { 1167 font.base_line = parse_double_from_node (t); 1168 } 1169 1170 if (t.get_name () == "bottom_position" && t.get_content () != "") { 1171 font.bottom_position = parse_double_from_node (t); 1172 } 1173 1174 if (t.get_name () == "bottom_limit" && t.get_content () != "") { 1175 font.bottom_limit = parse_double_from_node (t); 1176 } 1177 1178 if (t.get_name () == "custom_guide" && t.get_content () != "") { 1179 position = parse_double_from_node (t); 1180 1181 label = ""; 1182 foreach (Attribute attr in t.get_attributes ()) { 1183 if (attr.get_name () == "label") { 1184 label = attr.get_content (); 1185 } 1186 } 1187 1188 line = new Line (label, position); 1189 1190 font.custom_guides.add (line); 1191 } 1192 } 1193 } 1194 1195 private double parse_double_from_node (Tag tag) { 1196 double d; 1197 bool r = double.try_parse (tag.get_content (), out d); 1198 string s; 1199 1200 if (unlikely (!r)) { 1201 s = tag.get_content (); 1202 if (s == "") { 1203 warning (@"No content for node\n"); 1204 } else { 1205 warning (@"Failed to parse double for \"$(tag.get_content ())\"\n"); 1206 } 1207 } 1208 1209 return (r) ? d : 0.0; 1210 } 1211 1212 /** Parse the new glyph format */ 1213 private void parse_glyph_collection (Tag tag) { 1214 unichar unicode = 0; 1215 GlyphCollection gc; 1216 GlyphCollection? current_gc; 1217 bool new_glyph_collection; 1218 StringBuilder b; 1219 string name = ""; 1220 int selected_id = -1; 1221 bool unassigned = false; 1222 1223 foreach (Attribute attribute in tag.get_attributes ()) { 1224 if (attribute.get_name () == "unicode") { 1225 unicode = Font.to_unichar (attribute.get_content ()); 1226 b = new StringBuilder (); 1227 b.append_unichar (unicode); 1228 name = b.str; 1229 1230 if (name == "") { 1231 name = ".null"; 1232 } 1233 1234 unassigned = false; 1235 } 1236 1237 if (attribute.get_name () == "name") { 1238 unicode = '\0'; 1239 name = attribute.get_content (); 1240 unassigned = true; 1241 } 1242 } 1243 1244 current_gc = font.get_glyph_collection_by_name (name); 1245 new_glyph_collection = (current_gc == null); 1246 1247 if (!new_glyph_collection) { 1248 gc = (!) current_gc; 1249 } else { 1250 gc = new GlyphCollection (unicode, name); 1251 } 1252 1253 foreach (Tag t in tag) { 1254 if (t.get_name () == "selected") { 1255 selected_id = parse_selected (t); 1256 gc.set_selected_version (selected_id); 1257 } 1258 } 1259 1260 foreach (Tag t in tag) { 1261 if (t.get_name () == "glyph") { 1262 parse_glyph (t, gc, name, unicode, selected_id, unassigned); 1263 } 1264 } 1265 1266 if (new_glyph_collection) { 1267 font.add_glyph_collection (gc); 1268 } 1269 } 1270 1271 private int parse_selected (Tag tag) { 1272 int id = 1; 1273 bool has_selected_tag = false; 1274 1275 foreach (Attribute attribute in tag.get_attributes ()) { 1276 if (attribute.get_name () == "id") { 1277 id = int.parse (attribute.get_content ()); 1278 has_selected_tag = true; 1279 break; 1280 } 1281 } 1282 1283 if (unlikely (!has_selected_tag)) { 1284 warning ("No selected tag."); 1285 } 1286 1287 return id; 1288 } 1289 1290 public void parse_glyph (Tag tag, GlyphCollection gc, string name, 1291 unichar unicode, int selected_id, bool unassigned) { 1292 Glyph glyph = new Glyph (name, unicode); 1293 Path path; 1294 bool selected = false; 1295 bool has_id = false; 1296 int id = 1; 1297 Layer layer; 1298 1299 foreach (Attribute attr in tag.get_attributes ()) { 1300 if (attr.get_name () == "left") { 1301 glyph.left_limit = double.parse (attr.get_content ()); 1302 } 1303 1304 if (attr.get_name () == "right") { 1305 glyph.right_limit = double.parse (attr.get_content ()); 1306 } 1307 1308 // id is unique within the glyph collection 1309 if (attr.get_name () == "id") { 1310 id = int.parse (attr.get_content ()); 1311 has_id = true; 1312 } 1313 1314 // old way of selecting a glyph in the version list 1315 if (attr.get_name () == "selected") { 1316 selected = bool.parse (attr.get_content ()); 1317 } 1318 } 1319 1320 foreach (Tag t in tag) { 1321 if (t.get_name () == "layer") { 1322 layer = parse_layer (t); 1323 glyph.layers.add_layer (layer); 1324 } 1325 } 1326 1327 // parse paths without layers in old versions of the format 1328 foreach (Tag t in tag) { 1329 if (t.get_name () == "path") { 1330 path = parse_path (t); 1331 glyph.add_path (path); 1332 } 1333 } 1334 1335 foreach (Tag t in tag) { 1336 if (t.get_name () == "background") { 1337 parse_background_scale (glyph, t); 1338 } 1339 } 1340 1341 foreach (Path p in glyph.get_all_paths ()) { 1342 p.reset_stroke (); 1343 } 1344 1345 glyph.version_id = (has_id) ? id : (int) gc.length () + 1; 1346 gc.set_unassigned (unassigned); 1347 1348 gc.insert_glyph (glyph, selected || selected_id == id); 1349 glyph = new Glyph.no_lines (""); 1350 } 1351 1352 Layer parse_layer (Tag tag) { 1353 Layer layer = new Layer (); 1354 Path path; 1355 1356 // FIXME: name etc. 1357 1358 foreach (Attribute a in tag.get_attributes ()) { 1359 if (a.get_name () == "visible") { 1360 layer.visible = bool.parse (a.get_content ()); 1361 } 1362 1363 if (a.get_name () == "name") { 1364 layer.name = a.get_content (); 1365 } 1366 } 1367 1368 foreach (Tag t in tag) { 1369 if (t.get_name () == "path") { 1370 path = parse_path (t); 1371 layer.add_path (path); 1372 } 1373 } 1374 1375 return layer; 1376 } 1377 1378 private Path parse_path (Tag tag) { 1379 Path path = new Path (); 1380 1381 foreach (Attribute attr in tag.get_attributes ()) { 1382 if (attr.get_name () == "data") { 1383 path.point_data = attr.get_content (); 1384 path.control_points = null; 1385 } 1386 } 1387 1388 foreach (Attribute attr in tag.get_attributes ()) { 1389 if (attr.get_name () == "stroke") { 1390 path.set_stroke (double.parse (attr.get_content ())); 1391 } 1392 1393 if (attr.get_name () == "skew") { 1394 path.skew = double.parse (attr.get_content ()); 1395 } 1396 1397 if (attr.get_name () == "cap") { 1398 if (attr.get_content () == "round") { 1399 path.line_cap = LineCap.ROUND; 1400 } else if (attr.get_content () == "square") { 1401 path.line_cap = LineCap.SQUARE; 1402 } 1403 } 1404 } 1405 1406 return path; 1407 } 1408 1409 private static void line (Path path, string px, string py) { 1410 EditPoint ep; 1411 1412 path.add (parse_double (px), parse_double (py)); 1413 ep = path.get_last_point (); 1414 ep.get_right_handle ().type = PointType.LINE_DOUBLE_CURVE; 1415 ep.get_left_handle ().type = PointType.LINE_DOUBLE_CURVE; 1416 ep.type = PointType.LINE_DOUBLE_CURVE; 1417 ep.recalculate_linear_handles (); 1418 } 1419 1420 private static void cubic_line (Path path, string px, string py) { 1421 EditPoint ep; 1422 1423 path.add (parse_double (px), parse_double (py)); 1424 ep = path.points.get (path.points.size - 1); 1425 ep.get_right_handle ().type = PointType.LINE_CUBIC; 1426 ep.type = PointType.LINE_CUBIC; 1427 ep.recalculate_linear_handles (); 1428 } 1429 1430 private static void quadratic (Path path, string px0, string py0, string px1, string py1) { 1431 EditPoint ep1, ep2; 1432 1433 double x0 = parse_double (px0); 1434 double y0 = parse_double (py0); 1435 double x1 = parse_double (px1); 1436 double y1 = parse_double (py1); 1437 1438 if (path.points.size == 0) { 1439 warning ("No point."); 1440 return; 1441 } 1442 1443 ep1 = path.points.get (path.points.size - 1); 1444 ep1.recalculate_linear_handles (); 1445 ep1.get_right_handle ().type = PointType.QUADRATIC; 1446 ep1.get_right_handle ().move_to_coordinate (x0, y0); 1447 ep1.type = PointType.QUADRATIC; 1448 1449 path.add (x1, y1); 1450 1451 ep2 = path.points.get (path.points.size - 1); 1452 ep2.recalculate_linear_handles (); 1453 ep2.get_left_handle ().type = PointType.QUADRATIC; 1454 ep2.get_left_handle ().move_to_coordinate (x0, y0); 1455 ep2.type = PointType.QUADRATIC; 1456 } 1457 1458 private static void cubic (Path path, string px0, string py0, string px1, string py1, string px2, string py2) { 1459 EditPoint ep1, ep2; 1460 1461 double x0 = parse_double (px0); 1462 double y0 = parse_double (py0); 1463 double x1 = parse_double (px1); 1464 double y1 = parse_double (py1); 1465 double x2 = parse_double (px2); 1466 double y2 = parse_double (py2); 1467 1468 double lx, ly; 1469 1470 if (path.points.size == 0) { 1471 warning ("No point"); 1472 return; 1473 } 1474 1475 // start with line handles 1476 ep1 = path.points.get (path.points.size - 1); 1477 ep1.get_right_handle ().type = PointType.LINE_CUBIC; 1478 1479 lx = ep1.x + ((x2 - ep1.x) / 3); 1480 ly = ep1.y + ((y2 - ep1.y) / 3); 1481 1482 ep1.get_right_handle ().move_to_coordinate (lx, ly); 1483 ep1.recalculate_linear_handles (); 1484 1485 // set curve handles 1486 ep1 = path.points.get (path.points.size - 1); 1487 ep1.recalculate_linear_handles (); 1488 ep1.get_right_handle ().type = PointType.CUBIC; 1489 ep1.get_right_handle ().move_to_coordinate (x0, y0); 1490 ep1.type = PointType.CUBIC; 1491 1492 path.add (x2, y2); 1493 1494 ep2 = path.points.get (path.points.size - 1); 1495 ep2.recalculate_linear_handles (); 1496 ep2.get_left_handle ().type = PointType.CUBIC; 1497 ep2.get_left_handle ().move_to_coordinate (x1, y1); 1498 ep2.type = PointType.CUBIC; 1499 1500 ep1.recalculate_linear_handles (); 1501 } 1502 1503 /** Two quadratic off curve points. */ 1504 private static void double_curve (Path path, string px0, string py0, string px1, string py1, string px2, string py2) { 1505 EditPoint ep1, ep2; 1506 1507 double x0 = parse_double (px0); 1508 double y0 = parse_double (py0); 1509 double x1 = parse_double (px1); 1510 double y1 = parse_double (py1); 1511 double x2 = parse_double (px2); 1512 double y2 = parse_double (py2); 1513 1514 double lx, ly; 1515 1516 if (path.points.size == 0) { 1517 warning ("No point"); 1518 return; 1519 } 1520 1521 // start with line handles 1522 ep1 = path.points.get (path.points.size - 1); 1523 ep1.get_right_handle ().type = PointType.LINE_DOUBLE_CURVE; 1524 1525 lx = ep1.x + ((x2 - ep1.x) / 4); 1526 ly = ep1.y + ((y2 - ep1.y) / 4); 1527 1528 ep1.get_right_handle ().move_to_coordinate (lx, ly); 1529 ep1.recalculate_linear_handles (); 1530 1531 // set curve handles 1532 ep1 = path.points.get (path.points.size - 1); 1533 ep1.recalculate_linear_handles (); 1534 ep1.get_right_handle ().type = PointType.DOUBLE_CURVE; 1535 ep1.get_right_handle ().move_to_coordinate (x0, y0); 1536 ep1.type = PointType.DOUBLE_CURVE; 1537 1538 path.add (x2, y2); 1539 1540 ep2 = path.points.get (path.points.size - 1); 1541 ep2.recalculate_linear_handles (); 1542 ep2.get_left_handle ().type = PointType.DOUBLE_CURVE; 1543 ep2.get_left_handle ().move_to_coordinate (x1, y1); 1544 ep2.type = PointType.DOUBLE_CURVE; 1545 1546 ep1.recalculate_linear_handles (); 1547 } 1548 1549 public static void close (Path path) { 1550 EditPoint ep1, ep2; 1551 1552 if (path.points.size < 2) { 1553 warning ("Less than two points in path."); 1554 return; 1555 } 1556 1557 // last point is first 1558 ep1 = path.points.get (path.points.size - 1); 1559 ep2 = path.points.get (0); 1560 1561 path.points.remove_at (path.points.size - 1); 1562 1563 if (ep1.type != PointType.QUADRATIC || ep2.type != PointType.QUADRATIC) { 1564 ep2.tie_handles = ep1.tie_handles; 1565 ep2.left_handle.angle = ep1.left_handle.angle; 1566 ep2.left_handle.length = ep1.left_handle.length; 1567 ep2.left_handle.type = ep1.left_handle.type; 1568 } 1569 1570 path.close (); 1571 } 1572 1573 public static void parse_path_data (string data, Path path) { 1574 string[] d = data.split (" "); 1575 string[] p, p1, p2; 1576 int i = 0; 1577 string instruction = ""; 1578 bool open = false; 1579 1580 if (data == "") { 1581 return; 1582 } 1583 1584 return_val_if_fail (d.length > 1, path); 1585 1586 if (!(d[0] == "S" || d[0] == "B")) { 1587 warning ("No start point."); 1588 return; 1589 } 1590 1591 instruction = d[i++]; 1592 1593 if (instruction == "S") { 1594 p = d[i++].split (","); 1595 return_val_if_fail (p.length == 2, path); 1596 line (path, p[0], p[1]); 1597 } 1598 1599 if (instruction == "B") { 1600 p = d[i++].split (","); 1601 return_val_if_fail (p.length == 2, path); 1602 cubic_line (path, p[0], p[1]); 1603 } 1604 1605 while (i < d.length) { 1606 instruction = d[i++]; 1607 1608 if (instruction == "") { 1609 warning (@"No instruction at index $i."); 1610 return; 1611 } 1612 1613 if (instruction == "L") { 1614 return_val_if_fail (i < d.length, path); 1615 p = d[i++].split (","); 1616 return_val_if_fail (p.length == 2, path); 1617 line (path, p[0], p[1]); 1618 }else if (instruction == "M") { 1619 return_val_if_fail (i < d.length, path); 1620 p = d[i++].split (","); 1621 return_val_if_fail (p.length == 2, path); 1622 cubic_line (path, p[0], p[1]); 1623 } else if (instruction == "Q") { 1624 return_val_if_fail (i + 1 < d.length, path); 1625 1626 p = d[i++].split (","); 1627 p1 = d[i++].split (","); 1628 1629 return_val_if_fail (p.length == 2, path); 1630 return_val_if_fail (p1.length == 2, path); 1631 1632 quadratic (path, p[0], p[1], p1[0], p1[1]); 1633 } else if (instruction == "D") { 1634 return_val_if_fail (i + 2 < d.length, path); 1635 1636 p = d[i++].split (","); 1637 p1 = d[i++].split (","); 1638 p2 = d[i++].split (","); 1639 1640 return_val_if_fail (p.length == 2, path); 1641 return_val_if_fail (p1.length == 2, path); 1642 return_val_if_fail (p2.length == 2, path); 1643 1644 double_curve (path, p[0], p[1], p1[0], p1[1], p2[0], p2[1]); 1645 } else if (instruction == "C") { 1646 return_val_if_fail (i + 2 < d.length, path); 1647 1648 p = d[i++].split (","); 1649 p1 = d[i++].split (","); 1650 p2 = d[i++].split (","); 1651 1652 return_val_if_fail (p.length == 2, path); 1653 return_val_if_fail (p1.length == 2, path); 1654 return_val_if_fail (p2.length == 2, path); 1655 1656 cubic (path, p[0], p[1], p1[0], p1[1], p2[0], p2[1]); 1657 } else if (instruction == "T") { 1658 path.points.get (path.points.size - 1).tie_handles = true; 1659 } else if (instruction == "O") { 1660 open = true; 1661 } else { 1662 warning (@"invalid instruction $instruction"); 1663 return; 1664 } 1665 } 1666 1667 if (!open) { 1668 close (path); 1669 } else { 1670 path.points.remove_at (path.points.size - 1); 1671 1672 if (!path.is_open ()) { 1673 warning ("Closed path."); 1674 } 1675 } 1676 1677 path.update_region_boundaries (); 1678 } 1679 1680 private static double parse_double (string p) { 1681 double d; 1682 if (double.try_parse (p, out d)) { 1683 return d; 1684 } 1685 1686 warning (@"failed to parse $p"); 1687 return 0; 1688 } 1689 1690 private void parse_background_scale (Glyph g, Tag tag) { 1691 BackgroundImage img; 1692 BackgroundImage? new_img = null; 1693 1694 File img_file = get_child (font.get_backgrounds_folder (), "parts"); 1695 1696 foreach (Attribute attr in tag.get_attributes ()) { 1697 if (attr.get_name () == "sha1") { 1698 img_file = get_child (img_file, attr.get_content () + ".png"); 1699 1700 if (!img_file.query_exists ()) { 1701 warning (@"Background file has not been created yet. $((!) img_file.get_path ())"); 1702 } 1703 1704 new_img = new BackgroundImage ((!) img_file.get_path ()); 1705 g.set_background_image ((!) new_img); 1706 } 1707 } 1708 1709 if (unlikely (new_img == null)) { 1710 warning ("No source for image found."); 1711 return; 1712 } 1713 1714 img = (!) new_img; 1715 1716 foreach (Attribute attr in tag.get_attributes ()) { 1717 if (attr.get_name () == "x") { 1718 img.img_x = double.parse (attr.get_content ()); 1719 } 1720 1721 if (attr.get_name () == "y") { 1722 img.img_y = double.parse (attr.get_content ()); 1723 } 1724 1725 if (attr.get_name () == "scale_x") { 1726 img.img_scale_x = double.parse (attr.get_content ()); 1727 } 1728 1729 if (attr.get_name () == "scale_y") { 1730 img.img_scale_y = double.parse (attr.get_content ()); 1731 } 1732 1733 if (attr.get_name () == "rotation") { 1734 img.img_rotation = double.parse (attr.get_content ()); 1735 } 1736 } 1737 1738 img.set_position (img.img_x, img.img_y); 1739 } 1740 1741 public void write_ligatures (DataOutputStream os) { 1742 Ligatures ligatures = font.get_ligatures (); 1743 1744 ligatures.get_ligatures ((subst, liga) => { 1745 try { 1746 string lig = serialize_attribute (liga); 1747 string sequence = serialize_attribute (subst); 1748 os.put_string (@"<ligature sequence=\"$(sequence)\" replacement=\"$(lig)\"/>\n"); 1749 } catch (GLib.IOError e) { 1750 warning (e.message); 1751 } 1752 }); 1753 1754 try { 1755 foreach (ContextualLigature c in ligatures.contextual_ligatures) { 1756 os.put_string (@"<contextual " 1757 + @"ligature=\"$(c.ligatures)\" " 1758 + @"backtrack=\"$(c.backtrack)\" " 1759 + @"input=\"$(c.input)\" " 1760 + @"lookahead=\"$(c.lookahead)\" />\n"); 1761 } 1762 } catch (GLib.Error e) { 1763 warning (e.message); 1764 } 1765 } 1766 1767 public void parse_contectual_ligature (Tag t) { 1768 string ligature = ""; 1769 string backtrack = ""; 1770 string input = ""; 1771 string lookahead = ""; 1772 Ligatures ligatures; 1773 1774 foreach (Attribute a in t.get_attributes ()) { 1775 if (a.get_name () == "ligature") { 1776 ligature = a.get_content (); 1777 } 1778 1779 if (a.get_name () == "backtrack") { 1780 backtrack = a.get_content (); 1781 } 1782 1783 if (a.get_name () == "input") { 1784 input = a.get_content (); 1785 } 1786 1787 if (a.get_name () == "lookahead") { 1788 lookahead = a.get_content (); 1789 } 1790 } 1791 1792 ligatures = font.get_ligatures (); 1793 ligatures.add_contextual_ligature (ligature, backtrack, input, lookahead); 1794 } 1795 1796 public void parse_ligature (Tag t) { 1797 string sequence = ""; 1798 string ligature = ""; 1799 Ligatures ligatures; 1800 1801 foreach (Attribute a in t.get_attributes ()) { 1802 if (a.get_name () == "sequence") { 1803 sequence = a.get_content (); 1804 } 1805 1806 if (a.get_name () == "replacement") { 1807 ligature = a.get_content (); 1808 } 1809 } 1810 1811 ligatures = font.get_ligatures (); 1812 ligatures.add_ligature (sequence, ligature); 1813 } 1814 } 1815 1816 } 1817