The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

PostTable.vala in libbirdfont/OpenFontFormat

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/OpenFontFormat/PostTable.vala.
Merge ../birdfont-2.x
1 /* 2 Copyright (C) 2012 2013 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 15 namespace BirdFont { 16 17 public class PostTable : OtfTable { 18 19 GlyfTable glyf_table; 20 21 Gee.ArrayList<uint16> index = new Gee.ArrayList<uint16> (); 22 Gee.ArrayList<string> names = new Gee.ArrayList<string> (); 23 24 public PostTable (GlyfTable g) { 25 id = "post"; 26 glyf_table = g; 27 } 28 29 // the Macintosh standard order 30 private void add_standard_names () { 31 names.add (".notdef"); 32 names.add (".null"); 33 names.add ("nonmarkingreturn"); 34 names.add ("space"); 35 names.add ("exclam"); 36 names.add ("quotedbl"); 37 names.add ("numbersign"); 38 names.add ("dollar"); 39 names.add ("percent"); 40 names.add ("ampersand"); 41 names.add ("quotesingle"); 42 names.add ("parenleft"); 43 names.add ("parenright"); 44 names.add ("asterisk"); 45 names.add ("plus"); 46 names.add ("comma"); 47 names.add ("hyphen"); 48 names.add ("period"); 49 names.add ("slash"); 50 names.add ("zero"); 51 names.add ("one"); 52 names.add ("two"); 53 names.add ("three"); 54 names.add ("four"); 55 names.add ("five"); 56 names.add ("six"); 57 names.add ("seven"); 58 names.add ("eight"); 59 names.add ("nine"); 60 names.add ("colon"); 61 names.add ("semicolon"); 62 names.add ("less"); 63 names.add ("equal"); 64 names.add ("greater"); 65 names.add ("question"); 66 names.add ("at"); 67 names.add ("A"); 68 names.add ("B"); 69 names.add ("C"); 70 names.add ("D"); 71 names.add ("E"); 72 names.add ("F"); 73 names.add ("G"); 74 names.add ("H"); 75 names.add ("I"); 76 names.add ("J"); 77 names.add ("K"); 78 names.add ("L"); 79 names.add ("M"); 80 names.add ("N"); 81 names.add ("O"); 82 names.add ("P"); 83 names.add ("Q"); 84 names.add ("R"); 85 names.add ("S"); 86 names.add ("T"); 87 names.add ("U"); 88 names.add ("V"); 89 names.add ("W"); 90 names.add ("X"); 91 names.add ("Y"); 92 names.add ("Z"); 93 names.add ("bracketleft"); 94 names.add ("backslash"); 95 names.add ("bracketright"); 96 names.add ("asciicircum"); 97 names.add ("underscore"); 98 names.add ("grave"); 99 names.add ("a"); 100 names.add ("b"); 101 names.add ("c"); 102 names.add ("d"); 103 names.add ("e"); 104 names.add ("f"); 105 names.add ("g"); 106 names.add ("h"); 107 names.add ("i"); 108 names.add ("j"); 109 names.add ("k"); 110 names.add ("l"); 111 names.add ("m"); 112 names.add ("n"); 113 names.add ("o"); 114 names.add ("p"); 115 names.add ("q"); 116 names.add ("r"); 117 names.add ("s"); 118 names.add ("t"); 119 names.add ("u"); 120 names.add ("v"); 121 names.add ("w"); 122 names.add ("x"); 123 names.add ("y"); 124 names.add ("z"); 125 names.add ("braceleft"); 126 names.add ("bar"); 127 names.add ("braceright"); 128 names.add ("asciitilde"); 129 names.add ("Adieresis"); 130 names.add ("Aring"); 131 names.add ("Ccedilla"); 132 names.add ("Eacute"); 133 names.add ("Ntilde"); 134 names.add ("Odieresis"); 135 names.add ("Udieresis"); 136 names.add ("aacute"); 137 names.add ("agrave"); 138 names.add ("acircumflex"); 139 names.add ("adieresis"); 140 names.add ("atilde"); 141 names.add ("aring"); 142 names.add ("ccedilla"); 143 names.add ("eacute"); 144 names.add ("egrave"); 145 names.add ("ecircumflex"); 146 names.add ("edieresis"); 147 names.add ("iacute"); 148 names.add ("igrave"); 149 names.add ("icircumflex"); 150 names.add ("idieresis"); 151 names.add ("ntilde"); 152 names.add ("oacute"); 153 names.add ("ograve"); 154 names.add ("ocircumflex"); 155 names.add ("odieresis"); 156 names.add ("otilde"); 157 names.add ("uacute"); 158 names.add ("ugrave"); 159 names.add ("ucircumflex"); 160 names.add ("udieresis"); 161 names.add ("dagger"); 162 names.add ("degree"); 163 names.add ("cent"); 164 names.add ("sterling"); 165 names.add ("section"); 166 names.add ("bullet"); 167 names.add ("paragraph"); 168 names.add ("germandbls"); 169 names.add ("registered"); 170 names.add ("copyright"); 171 names.add ("trademark"); 172 names.add ("acute"); 173 names.add ("dieresis"); 174 names.add ("notequal"); 175 names.add ("AE"); 176 names.add ("Oslash"); 177 names.add ("infinity"); 178 names.add ("plusminus"); 179 names.add ("lessequal"); 180 names.add ("greaterequal"); 181 names.add ("yen"); 182 names.add ("mu"); 183 names.add ("partialdiff"); 184 names.add ("summation"); 185 names.add ("product"); 186 names.add ("pi"); 187 names.add ("integral"); 188 names.add ("ordfeminine"); 189 names.add ("ordmasculine"); 190 names.add ("Omega"); 191 names.add ("ae"); 192 names.add ("oslash"); 193 names.add ("questiondown"); 194 names.add ("exclamdown"); 195 names.add ("logicalnot"); 196 names.add ("radical"); 197 names.add ("florin"); 198 names.add ("approxequal"); 199 names.add ("Delta"); 200 names.add ("guillemotleft"); 201 names.add ("guillemotright"); 202 names.add ("ellipsis"); 203 names.add ("nonbreakingspace"); 204 names.add ("Agrave"); 205 names.add ("Atilde"); 206 names.add ("Otilde"); 207 names.add ("OE"); 208 names.add ("oe"); 209 names.add ("endash"); 210 names.add ("emdash"); 211 names.add ("quotedblleft"); 212 names.add ("quotedblright"); 213 names.add ("quoteleft"); 214 names.add ("quoteright"); 215 names.add ("divide"); 216 names.add ("lozenge"); 217 names.add ("ydieresis"); 218 names.add ("Ydieresis"); 219 names.add ("fraction"); 220 names.add ("currency"); 221 names.add ("guilsinglleft"); 222 names.add ("guilsinglright"); 223 names.add ("fi"); 224 names.add ("fl"); 225 names.add ("daggerdbl"); 226 names.add ("periodcentered"); 227 names.add ("quotesinglbase"); 228 names.add ("quotedblbase"); 229 names.add ("perthousand"); 230 names.add ("Acircumflex"); 231 names.add ("Ecircumflex"); 232 names.add ("Aacute"); 233 names.add ("Edieresis"); 234 names.add ("Egrave"); 235 names.add ("Iacute"); 236 names.add ("Icircumflex"); 237 names.add ("Idieresis"); 238 names.add ("Igrave"); 239 names.add ("Oacute"); 240 names.add ("Ocircumflex"); 241 names.add ("apple"); 242 names.add ("Ograve"); 243 names.add ("Uacute"); 244 names.add ("Ucircumflex"); 245 names.add ("Ugrave"); 246 names.add ("dotlessi"); 247 names.add ("circumflex"); 248 names.add ("tilde"); 249 names.add ("macron"); 250 names.add ("breve"); 251 names.add ("dotaccent"); 252 names.add ("ring"); 253 names.add ("cedilla"); 254 names.add ("hungarumlaut"); 255 names.add ("ogonek"); 256 names.add ("caron"); 257 names.add ("Lslash"); 258 names.add ("lslash"); 259 names.add ("Scaron"); 260 names.add ("scaron"); 261 names.add ("Zcaron"); 262 names.add ("zcaron"); 263 names.add ("brokenbar"); 264 names.add ("Eth"); 265 names.add ("eth"); 266 names.add ("Yacute"); 267 names.add ("yacute"); 268 names.add ("Thorn"); 269 names.add ("thorn"); 270 names.add ("minus"); 271 names.add ("multiply"); 272 names.add ("onesuperior"); 273 names.add ("twosuperior"); 274 names.add ("threesuperior"); 275 names.add ("onehalf"); 276 names.add ("onequarter"); 277 names.add ("threequarters"); 278 names.add ("franc"); 279 names.add ("Gbreve"); 280 names.add ("gbreve"); 281 names.add ("Idotaccent"); 282 names.add ("Scedilla"); 283 names.add ("scedilla"); 284 names.add ("Cacute"); 285 names.add ("cacute"); 286 names.add ("Ccaron"); 287 names.add ("ccaron"); 288 names.add ("dcroat"); 289 } 290 291 // mapping with char code to standard order 292 int get_standard_index (unichar c) { 293 switch (c) { 294 // entry 0 is the .notdef 295 296 case '\0': 297 return 1; 298 299 case '\r': 300 return 2; 301 302 case ' ': // space 303 return 3; 304 305 case '!': 306 return 4; 307 308 case '"': 309 return 5; 310 311 case '#': 312 return 6; 313 314 case '$': 315 return 7; 316 317 case '%': 318 return 8; 319 320 case '&': 321 return 9; 322 323 case '\'': 324 return 10; 325 326 case '(': 327 return 11; 328 329 case ')': 330 return 12; 331 332 case '*': 333 return 13; 334 335 case '+': 336 return 14; 337 338 case ',': 339 return 15; 340 341 case '-': 342 return 16; 343 344 case '.': 345 return 17; 346 347 case '/': 348 return 18; 349 350 case '0': 351 return 19; 352 353 case '1': 354 return 20; 355 356 case '2': 357 return 21; 358 359 case '3': 360 return 22; 361 362 case '4': 363 return 23; 364 365 case '5': 366 return 24; 367 368 case '6': 369 return 25; 370 371 case '7': 372 return 26; 373 374 case '8': 375 return 27; 376 377 case '9': 378 return 28; 379 380 case ':': 381 return 29; 382 383 case ';': 384 return 30; 385 386 case '<': 387 return 31; 388 389 case '=': 390 return 32; 391 392 case '>': 393 return 33; 394 395 case '?': 396 return 34; 397 398 case '@': 399 return 35; 400 401 case 'A': 402 return 36; 403 404 case 'B': 405 return 37; 406 407 case 'C': 408 return 38; 409 410 case 'D': 411 return 39; 412 413 case 'E': 414 return 40; 415 416 case 'F': 417 return 41; 418 419 case 'G': 420 return 42; 421 422 case 'H': 423 return 43; 424 425 case 'I': 426 return 44; 427 428 case 'J': 429 return 45; 430 431 case 'K': 432 return 46; 433 434 case 'L': 435 return 47; 436 437 case 'M': 438 return 48; 439 440 case 'N': 441 return 49; 442 443 case 'O': 444 return 50; 445 446 case 'P': 447 return 51; 448 449 case 'Q': 450 return 52; 451 452 case 'R': 453 return 53; 454 455 case 'S': 456 return 54; 457 458 case 'T': 459 return 55; 460 461 case 'U': 462 return 56; 463 464 case 'V': 465 return 57; 466 467 case 'W': 468 return 58; 469 470 case 'X': 471 return 59; 472 473 case 'Y': 474 return 60; 475 476 case 'Z': 477 return 61; 478 479 case '[': 480 return 62; 481 482 case '\\': 483 return 63; 484 485 case ']': 486 return 64; 487 488 case '^': 489 return 65; 490 491 case '_': 492 return 66; 493 494 case '`': 495 return 67; 496 497 case 'a': 498 return 68; 499 500 case 'b': 501 return 69; 502 503 case 'c': 504 return 70; 505 506 case 'd': 507 return 71; 508 509 case 'e': 510 return 72; 511 512 case 'f': 513 return 73; 514 515 case 'g': 516 return 74; 517 518 case 'h': 519 return 75; 520 521 case 'i': 522 return 76; 523 524 case 'j': 525 return 77; 526 527 case 'k': 528 return 78; 529 530 case 'l': 531 return 79; 532 533 case 'm': 534 return 80; 535 536 case 'n': 537 return 81; 538 539 case 'o': 540 return 82; 541 542 case 'p': 543 return 83; 544 545 case 'q': 546 return 84; 547 548 case 'r': 549 return 85; 550 551 case 's': 552 return 86; 553 554 case 't': 555 return 87; 556 557 case 'u': 558 return 88; 559 560 case 'v': 561 return 89; 562 563 case 'w': 564 return 90; 565 566 case 'x': 567 return 91; 568 569 case 'y': 570 return 92; 571 572 case 'z': 573 return 93; 574 575 case '{': 576 return 94; 577 578 case '|': 579 return 95; 580 581 case '}': 582 return 96; 583 584 case '~': 585 return 97; 586 587 case 'Ä': 588 return 98; 589 590 case 'Å': 591 return 99; 592 593 case 'Ç': 594 return 100; 595 596 case 'É': 597 return 101; 598 599 case 'Ñ': 600 return 102; 601 602 case 'Ö': 603 return 103; 604 605 case 'Ü': 606 return 104; 607 608 case 'á': 609 return 105; 610 611 case 'à': 612 return 106; 613 614 case 'â': 615 return 107; 616 617 case 'ä': 618 return 108; 619 620 case 'ã': 621 return 109; 622 623 case 'å': 624 return 110; 625 626 case 'ç': 627 return 111; 628 629 case 'é': 630 return 112; 631 632 case 'è': 633 return 113; 634 635 case 'ê': 636 return 114; 637 638 case 'ë': 639 return 115; 640 641 case 'í': 642 return 116; 643 644 case 'ì': 645 return 117; 646 647 case 'î': 648 return 118; 649 650 case 'ï': 651 return 119; 652 653 case 'ñ': 654 return 120; 655 656 case 'ó': 657 return 121; 658 659 case 'ò': 660 return 122; 661 662 case 'ô': 663 return 123; 664 665 case 'ö': 666 return 124; 667 668 case 'õ': 669 return 125; 670 671 case 'ú': 672 return 126; 673 674 case 'ù': 675 return 127; 676 677 case 'û': 678 return 128; 679 680 case 'ü': 681 return 129; 682 683 case '†': 684 return 130; 685 686 case '°': 687 return 131; 688 689 case '¢': 690 return 132; 691 692 case '£': 693 return 133; 694 695 case '§': 696 return 134; 697 698 case '•': 699 return 135; 700 701 case '¶': 702 return 136; 703 704 case 'ß': 705 return 137; 706 707 case '®': 708 return 138; 709 710 case '©': 711 return 139; 712 713 case '™': 714 return 140; 715 716 case '´': 717 return 141; 718 719 case '¨': 720 return 142; 721 722 case '≠': 723 return 143; 724 725 case 'Æ': 726 return 144; 727 728 case 'Ø': 729 return 145; 730 731 case '∞': 732 return 146; 733 734 case '±': 735 return 147; 736 737 case '≤': 738 return 148; 739 740 case '≥': 741 return 149; 742 743 case '¥': 744 return 150; 745 746 case 'µ': 747 return 151; 748 749 case '∂': 750 return 152; 751 752 case '∑': 753 return 153; 754 755 case '∏': 756 return 154; 757 758 case 'π': 759 return 155; 760 761 case '∫': 762 return 156; 763 764 case 'ª': 765 return 157; 766 767 case 'º': 768 return 158; 769 770 case 'Ω': 771 return 159; 772 773 case 'æ': 774 return 160; 775 776 case 'ø': 777 return 161; 778 779 case '¿': 780 return 162; 781 782 case '¡': 783 return 163; 784 785 case '¬': 786 return 164; 787 788 case '√': 789 return 165; 790 791 case 'ƒ': 792 return 166; 793 794 case '≈': 795 return 167; 796 797 case '∆': 798 return 168; 799 800 case '«': 801 return 169; 802 803 case '»': 804 return 170; 805 806 case '…': 807 return 171; 808 809 case ' ': // non breaking space 810 return 172; 811 812 case 'À': 813 return 173; 814 815 case 'Ã': 816 return 174; 817 818 case 'Õ': 819 return 175; 820 821 case 'Œ': 822 return 176; 823 824 case 'œ': 825 return 177; 826 827 case '–': 828 return 178; 829 830 case '—': 831 return 179; 832 833 case '“': 834 return 180; 835 836 case '”': 837 return 181; 838 839 case '‘': 840 return 182; 841 842 case '’': 843 return 183; 844 845 case '÷': 846 return 184; 847 848 case '◊': 849 return 185; 850 851 case 'ÿ': 852 return 186; 853 854 case 'Ÿ': 855 return 187; 856 857 case '⁄': 858 return 188; 859 860 case '¤': 861 return 189; 862 863 case '‹': 864 return 190; 865 866 case '›': 867 return 191; 868 869 case 'fi': 870 return 192; 871 872 case 'fl': 873 return 193; 874 875 case '‡': 876 return 194; 877 878 case '·': 879 return 195; 880 881 case '‚': 882 return 196; 883 884 case '„': 885 return 197; 886 887 case '‰': 888 return 198; 889 890 case 'Â': 891 return 199; 892 893 case 'Ê': 894 return 200; 895 896 case 'Á': 897 return 201; 898 899 case 'Ë': 900 return 202; 901 902 case 'È': 903 return 203; 904 905 case 'Í': 906 return 204; 907 908 case 'Î': 909 return 205; 910 911 case 'Ï': 912 return 206; 913 914 case 'Ì': 915 return 207; 916 917 case 'Ó': 918 return 208; 919 920 case 'Ô': 921 return 209; 922 923 // Machintosh apple goes here 924 // return 210; 925 926 case 'Ò': 927 return 211; 928 929 case 'Ú': 930 return 212; 931 932 case 'Û': 933 return 213; 934 935 case 'Ù': 936 return 214; 937 938 case 'ı': 939 return 215; 940 941 case 'ˆ': 942 return 216; 943 944 case '˜': 945 return 217; 946 947 case '¯': 948 return 218; 949 950 case '˘': 951 return 219; 952 953 case '˙': 954 return 220; 955 956 case '˚': 957 return 221; 958 959 case '¸': 960 return 222; 961 962 case '˝': 963 return 223; 964 965 case '˛': 966 return 224; 967 968 case 'ˇ': 969 return 225; 970 971 case 'Ł': 972 return 226; 973 974 case 'ł': 975 return 227; 976 977 case 'Š': 978 return 228; 979 980 case 'š': 981 return 229; 982 983 case 'Ž': 984 return 230; 985 986 case 'ž': 987 return 231; 988 989 case '¦': 990 return 232; 991 992 case 'Ð': 993 return 233; 994 995 case 'ð': 996 return 234; 997 998 case 'Ý': 999 return 235; 1000 1001 case 'ý': 1002 return 236; 1003 1004 case 'Þ': 1005 return 237; 1006 1007 case 'þ': 1008 return 238; 1009 1010 case '−': 1011 return 239; 1012 1013 case '×': 1014 return 240; 1015 1016 case '¹': 1017 return 241; 1018 1019 case '²': 1020 return 242; 1021 1022 case '³': 1023 return 243; 1024 1025 case '½': 1026 return 244; 1027 1028 case '¼': 1029 return 245; 1030 1031 case '¾': 1032 return 246; 1033 1034 case '₣': 1035 return 247; 1036 1037 case 'Ğ': 1038 return 248; 1039 1040 case 'ğ': 1041 return 249; 1042 1043 case 'İ': 1044 return 250; 1045 1046 case 'Ş': 1047 return 251; 1048 1049 case 'ş': 1050 return 252; 1051 1052 case 'Ć': 1053 return 253; 1054 1055 case 'ć': 1056 return 254; 1057 1058 case 'Č': 1059 return 255; 1060 1061 case 'č': 1062 return 256; 1063 1064 case 'đ': 1065 return 257; 1066 } 1067 1068 return 0; 1069 } 1070 1071 public void process () throws Error { 1072 FontData fd = new FontData (); 1073 string n; 1074 int name_index; 1075 GlyphCollection gc; 1076 Glyph g; 1077 string ps_name; 1078 StringBuilder name; 1079 1080 fd.add_fixed (0x00020000); // Version 1081 fd.add_fixed (0x00000000); // italicAngle 1082 1083 fd.add_short (-2); // underlinePosition 1084 fd.add_short (1); // underlineThickness 1085 1086 fd.add_ulong (0); // non zero for monospaced font 1087 1088 // mem boundaries may be omitted 1089 fd.add_ulong (0); // min mem for type 42 1090 fd.add_ulong (0); // max mem for type 42 1091 1092 fd.add_ulong (0); // min mem for Type1 1093 fd.add_ulong (0); // max mem for Type1 1094 1095 fd.add_ushort ((uint16) glyf_table.glyphs.size); 1096 1097 // this part of the spec is so weird 1098 1099 fd.add_ushort ((uint16) 0); // first index is .notdef 1100 index.add (0); 1101 1102 assert (names.size == 0); 1103 add_standard_names (); 1104 1105 print ("Adding post names\n"); 1106 1107 for (int i = 1; i < glyf_table.glyphs.size; i++) { 1108 gc = glyf_table.glyphs.get (i); 1109 g = gc.get_current (); 1110 name_index = get_standard_index (g.unichar_code); 1111 1112 if (name_index != 0 && !gc.is_unassigned ()) { 1113 fd.add_ushort ((uint16) name_index); // use standard name 1114 } else { 1115 printd (@"Adding non standard postscript name $(gc.get_name ())\n"); 1116 1117 name_index = (int) names.size; // use font specific name 1118 fd.add_ushort ((uint16) name_index); 1119 1120 name = new StringBuilder (); 1121 if (gc.is_unassigned ()) { 1122 name.append (g.get_name ()); 1123 } else { 1124 unichar c = gc.get_unicode_character (); 1125 if (c < 0xFFFF) { 1126 name.printf ("uni%04x", c); 1127 } else { 1128 name.printf ("u%05x", c); 1129 } 1130 } 1131 1132 ps_name = create_ps_name (name.str); 1133 names.add (ps_name); 1134 } 1135 1136 this.index.add ((uint16) name_index); 1137 } 1138 1139 for (int i = 258; i < names.size; i++) { 1140 n = (!) names.get (i); 1141 1142 if (n.length > 0xFF) { 1143 warning (@"too long name for glyph $n"); 1144 continue; 1145 } 1146 1147 fd.add ((uint8) n.length); 1148 fd.add_str (n); 1149 } 1150 1151 fd.pad (); 1152 1153 this.font_data = fd; 1154 } 1155 1156 string create_ps_name (string name) { 1157 string valid_name = NameTable.name_validation (name, false, 0xFF); 1158 1159 if (valid_name.char_count () == 1) { 1160 warning (@"Too short name: $valid_name generated from $name"); 1161 valid_name = add_suffix (valid_name); 1162 } 1163 1164 if (names.index_of (valid_name) > -1) { 1165 valid_name = add_suffix (valid_name); 1166 } 1167 1168 return valid_name; 1169 } 1170 1171 string add_suffix (string valid_name) { 1172 int i = 2; 1173 string s; 1174 1175 s = valid_name + @"_$i"; 1176 while (names.index_of (s) > -1) { 1177 i++; 1178 s = valid_name + @"_$i"; 1179 } 1180 1181 return s; 1182 } 1183 } 1184 1185 } 1186