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.
Merge ../birdfont-2.x
1 /* 2 Copyright (C) 2012 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 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 GlyfTable glyf_table; 33 HmtxTable hmtx_table; 34 HheaTable hhea_table; 35 36 public Os2Table (GlyfTable gt, HmtxTable hmtx, HheaTable hhea) { 37 id = "OS/2"; 38 glyf_table = gt; 39 hmtx_table = hmtx; 40 hhea_table = hhea; 41 } 42 43 public override void parse (FontData dis) throws Error { 44 } 45 46 public void process () { 47 process_table (false); 48 } 49 50 public void process_mac () { 51 process_table (true); 52 } 53 54 public void process_table (bool mac) { 55 FontData fd = new FontData (); 56 Font font = OpenFontFormatWriter.get_current_font (); 57 uint16 style = 0; 58 UnicodeRangeBits ranges = new UnicodeRangeBits (); 59 CodePageBits pages = new CodePageBits (); 60 uint32 unicodeRange1, unicodeRange2, unicodeRange3, unicodeRange4; 61 uint32 codepage1, codepage2; 62 int16 win_descent; 63 64 fd.add_u16 (0x0002); // version 65 66 fd.add_16 (hmtx_table.get_average_width ()); // xAvgCharWidth 67 68 fd.add_u16 ((uint16) font.weight); // usWeightClass (400 is normal, 700 is bold) 69 70 fd.add_u16 (5); // usWidthClass (5 is normal) 71 fd.add_u16 (0); // fsType 72 73 //FIXME: 74 fd.add_16 (40); // ySubscriptXSize 75 fd.add_16 (40); // ySubscriptYSize 76 fd.add_16 (40); // ySubscriptXOffset 77 fd.add_16 (40); // ySubscriptYOffset 78 fd.add_16 (40); // ySuperscriptXSize 79 fd.add_16 (40); // ySuperscriptYSize 80 fd.add_16 (40); // ySuperscriptXOffset 81 fd.add_16 (40); // ySuperscriptYOffset 82 fd.add_16 (40); // yStrikeoutSize 83 fd.add_16 (200); // yStrikeoutPosition 84 fd.add_16 (0); // sFamilyClass 85 86 // FIXME: PANOSE 87 // Panose, zero means anything will fit. 88 fd.add (0); 89 fd.add (0); 90 fd.add (0); 91 fd.add (0); 92 fd.add (0); 93 fd.add (0); 94 fd.add (0); 95 fd.add (0); 96 fd.add (0); 97 fd.add (0); 98 99 ranges.get_ranges (font, out unicodeRange1, out unicodeRange2, out unicodeRange3, out unicodeRange4); 100 101 if (mac) { 102 fd.add_u32 (unicodeRange1); // ulUnicodeRange1 Bits 0-31 103 fd.add_u32 (0); // ulUnicodeRange2 Bits 32-63 104 fd.add_u32 (0); // ulUnicodeRange3 Bits 64-95 105 fd.add_u32 (0); // ulUnicodeRange4 Bits 96-127 106 } else { 107 warning(@"unicodeRange1: $unicodeRange1 unicodeRange2: $unicodeRange2 unicodeRange3 $unicodeRange3 unicodeRange4: $unicodeRange4"); 108 fd.add_u32 (unicodeRange1); // ulUnicodeRange1 Bits 0-31 109 fd.add_u32 (unicodeRange2); // ulUnicodeRange2 Bits 32-63 110 fd.add_u32 (unicodeRange3); // ulUnicodeRange3 Bits 64-95 111 fd.add_u32 (unicodeRange4); // ulUnicodeRange4 Bits 96-127 112 } 113 114 fd.add_tag ("Bird"); // VendID 115 116 // fsSelection (1 for italic 0 for upright) 117 118 if (!font.bold && !font.italic) { 119 style |= REGULAR; 120 } 121 122 if (font.bold) { 123 style |= BOLD; 124 } 125 126 if (font.italic) { 127 style |= ITALIC; 128 } 129 130 fd.add_u16 (style); 131 132 fd.add_u16 (glyf_table.get_first_char ()); // usFirstCharIndex 133 fd.add_u16 (glyf_table.get_last_char ()); // usLastCharIndex 134 135 int16 ascender = (int16) rint (font.top_limit * HeadTable.UNITS); 136 int16 descender = (int16) rint (font.bottom_limit * HeadTable.UNITS); 137 138 fd.add_16 (ascender); // sTypoAscender 139 fd.add_16 (descender); // sTypoDescender 140 fd.add_16 (100); // sTypoLineGap 141 142 // usWinAscent 143 fd.add_u16 (ascender); 144 145 // usWinDescent (not like sTypoDescender) 146 win_descent = descender; 147 if (win_descent > 0) { 148 warning (@"usWinDescent is unsigned, can not write $(win_descent) to the field."); 149 fd.add_u16 (0); 150 } else { 151 fd.add_u16 (-win_descent); 152 } 153 154 pages.get_pages (font, out codepage1, out codepage2); 155 156 if (mac) { 157 fd.add_u32 (1); // ulCodePageRange1 Bits 0-31 (this value is only used fontbook) 158 fd.add_u32 (0); // ulCodePageRange2 Bits 32-63 159 } else { 160 fd.add_u32 (codepage1); // ulCodePageRange1 Bits 0-31 (this value is used by Word on Windows) 161 fd.add_u32 (codepage2); // ulCodePageRange2 Bits 32-63 162 } 163 164 int16 xheight = (int16) rint ((font.xheight_position - font.base_line) * HeadTable.UNITS); 165 int16 cap_height = (int16) rint ((font.top_limit - font.base_line) * HeadTable.UNITS); 166 167 fd.add_16 (xheight); // sxHeight 168 fd.add_16 (cap_height); // sCapHeight 169 170 fd.add_16 (0); // usDefaultChar 171 fd.add_16 (0x0020); // usBreakChar, also known as space 172 173 // usMaxContext 174 int16 num_glyphs = 2; // FIXME: add one for each parts of ligatures 175 fd.add_16 (num_glyphs); 176 177 // padding 178 fd.pad (); 179 180 this.font_data = fd; 181 } 182 } 183 184 } 185