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