The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Os2Table.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/Os2Table.vala.
Generate mac specific truetype fonts
1 /* 2 Copyright (C) 2012, 2013, 2014 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Math; 16 17 namespace BirdFont { 18 19 public class Os2Table : OtfTable { 20 21 public static const uint16 ITALIC = 1; 22 public static const uint16 UNDERSCORE = 1 << 1; 23 public static const uint16 NEGATIVE = 1 << 2; 24 public static const uint16 OUTLINED = 1 << 3; 25 public static const uint16 STRIKEOUT = 1 << 4; 26 public static const uint16 BOLD = 1 << 5; 27 public static const uint16 REGULAR = 1 << 6; 28 public static const uint16 TYPO_METRICS = 1 << 7; 29 public static const uint16 WWS = 1 << 8; 30 public static const uint16 OBLIQUE = 1 << 9; 31 32 public Os2Table () { 33 id = "OS/2"; 34 } 35 36 public override void parse (FontData dis) throws Error { 37 } 38 39 public void process (GlyfTable glyf_table, HmtxTable hmtx_table) { 40 FontData fd = new FontData (); 41 Font font = OpenFontFormatWriter.get_current_font (); 42 int16 ascender; 43 int16 descender; 44 uint16 style = 0; 45 UnicodeRangeBits ranges = new UnicodeRangeBits (); 46 CodePageBits pages = new CodePageBits (); 47 uint32 unicodeRange1, unicodeRange2, unicodeRange3, unicodeRange4; 48 uint32 codepage1, codepage2; 49 50 fd.add_u16 (0x0004); // version 51 52 fd.add_16 (hmtx_table.get_average_width ()); // xAvgCharWidth 53 54 fd.add_u16 ((uint16) font.weight); // usWeightClass (400 is normal, 700 is bold) 55 56 fd.add_u16 (5); // usWidthClass (5 is normal) 57 fd.add_u16 (0); // fsType 58 59 //FIXME: 60 fd.add_16 (40); // ySubscriptXSize 61 fd.add_16 (40); // ySubscriptYSize 62 fd.add_16 (40); // ySubscriptXOffset 63 fd.add_16 (40); // ySubscriptYOffset 64 fd.add_16 (40); // ySuperscriptXSize 65 fd.add_16 (40); // ySuperscriptYSize 66 fd.add_16 (40); // ySuperscriptXOffset 67 fd.add_16 (40); // ySuperscriptYOffset 68 fd.add_16 (40); // yStrikeoutSize 69 fd.add_16 (200); // yStrikeoutPosition 70 fd.add_16 (0); // sFamilyClass 71 72 // FIXME: PANOSE 73 // Panose, zero means anything will fit. 74 fd.add (0); 75 fd.add (0); 76 fd.add (0); 77 fd.add (0); 78 fd.add (0); 79 fd.add (0); 80 fd.add (0); 81 fd.add (0); 82 fd.add (0); 83 fd.add (0); 84 85 ranges.get_ranges (font, out unicodeRange1, out unicodeRange2, out unicodeRange3, out unicodeRange4); 86 87 fd.add_u32 (unicodeRange1); // ulUnicodeRange1 Bits 0-31 88 fd.add_u32 (unicodeRange2); // ulUnicodeRange2 Bits 32-63 89 fd.add_u32 (unicodeRange3); // ulUnicodeRange3 Bits 64-95 90 fd.add_u32 (unicodeRange4); // ulUnicodeRange4 Bits 96-127 91 92 fd.add_tag ("Bird"); // VendID 93 94 // fsSelection (1 for italic 0 for upright) 95 96 if (!font.bold && !font.italic) { 97 style |= REGULAR; 98 } 99 100 if (font.bold) { 101 style |= BOLD; 102 } 103 104 if (font.italic) { 105 style |= ITALIC; 106 } 107 108 fd.add_u16 (style); 109 110 fd.add_u16 (glyf_table.get_first_char ()); // usFirstCharIndex 111 fd.add_u16 (glyf_table.get_last_char ()); // usLastCharIndex 112 113 ascender = (int16) rint (font.top_limit * HeadTable.UNITS); 114 descender = (int16) rint (font.bottom_limit * HeadTable.UNITS); 115 116 117 fd.add_16 (ascender); // sTypoAscender 118 fd.add_16 (descender); // sTypoDescender 119 fd.add_16 (100); // sTypoLineGap 120 121 fd.add_u16 (ascender); // usWinAscent 122 123 if (descender > 0) { 124 warning (@"usWinDescent is unsigned, can not write $(-descender) to the field."); 125 fd.add_u16 (0); 126 } else { 127 fd.add_u16 (-descender); // usWinDescent (not like sTypoDescender) 128 } 129 130 pages.get_pages (font, out codepage1, out codepage2); 131 fd.add_u32 (codepage1); // ulCodePageRange1 Bits 0-31 132 fd.add_u32 (codepage2); // ulCodePageRange2 Bits 32-63 133 134 fd.add_16 (ascender); // sHeight 135 fd.add_16 (ascender); // sCapHeight 136 137 fd.add_16 (0); // usDefaultChar 138 fd.add_16 (0x0020); // usBreakChar, also known as space 139 140 fd.add_16 (2); // usMaxContext (two, becase the font has kerning but not ligatures). 141 142 // padding 143 fd.pad (); 144 145 this.font_data = fd; 146 } 147 148 } 149 150 } 151