The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

CffTable.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/CffTable.vala.
Fix compile time error
1 /* 2 Copyright (C) 2012, 2013 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 /** Type2, PostScript outlines */ 18 public class CffTable : OtfTable { 19 20 static const uint8 HSTEM = 1; 21 static const uint8 VSTEM = 3; 22 static const uint8 VMOVETO = 4; 23 static const uint8 HLINETO = 6; 24 static const uint8 VLINETO = 7; 25 static const uint8 ENDCHAR = 14; 26 static const uint8 HMOVETO = 22; 27 static const uint8 RMOVETO = 21; 28 29 static const uint8 CHARSET = 15; 30 static const uint8 ENCODING = 16; 31 32 static const uint8 VERSION = 0; 33 34 public int version; 35 36 FontData dis; 37 38 public CffTable () { 39 id = "CFF "; 40 } 41 42 uint32 read_offset (uint size) throws GLib.Error { 43 switch (size) { 44 case 0: 45 warning ("offset size is zero"); 46 return dis.read_byte (); 47 case 1: 48 return dis.read_byte (); 49 case 2: 50 return dis.read_ushort (); 51 case 4: 52 return dis.read_ulong (); 53 default: 54 warn_if_reached (); 55 break; 56 } 57 58 warning ("offset size is zero"); 59 return 0; 60 } 61 62 Gee.ArrayList<uint32> read_index () throws Error { 63 uint32 offset_size, off; 64 int entries; 65 Gee.ArrayList<uint32> offsets = new Gee.ArrayList<uint32> (); 66 67 entries = dis.read_ushort (); 68 printd (@"number of entries $(entries)\n"); 69 70 if (entries == 0) { 71 printd ("skip index"); 72 return offsets; 73 } 74 75 offset_size = dis.read (); 76 printd (@"Offset size $(offset_size)\n"); 77 78 // read the end offset as well 79 for (int i = 0; i <= entries; i++) { 80 off = read_offset (offset_size); 81 printd (@"offset $(off)\n"); 82 offsets.add (off); 83 } 84 85 return offsets; 86 } 87 88 public override void parse (FontData dis) throws Error { 89 uint v1, v2, offset_size, header_size, len; 90 string data; 91 Gee.ArrayList<uint32> offsets, dict_index; 92 int id, val; 93 int off; // offset relative to table position 94 95 dis.seek (offset); 96 this.dis = dis; 97 98 printd ("Parse CFF.\n"); 99 v1 = dis.read (); 100 v2 = dis.read (); 101 printd (@"Version $v1.$v2\n"); 102 header_size = dis.read (); 103 printd (@"Header size $(header_size)\n"); 104 offset_size = dis.read (); 105 printd (@"Offset size $(offset_size)\n"); 106 107 // name index 108 offsets = read_index (); 109 110 // name data 111 for (int i = 0; i < offsets.size - 1; i++) { 112 off = (int) offsets.get (i); 113 len = offsets.get (i + 1) - off; 114 //dis.seek (offset + off + header_size); 115 data = dis.read_string (len); 116 print (@"Found name $data\n"); 117 } 118 119 // dict index 120 print (@"dict index\n"); 121 dict_index = read_index (); 122 123 // dict data 124 id = 0; 125 val = 0; 126 for (int i = 0; i < dict_index.size - 1; i++) { 127 off = (int) offsets.get (i); 128 len = dict_index.get (i + 1) - dict_index.get (i); 129 //dis.seek (offset + off + header_size); 130 131 //for (int j = 0; j < len; j++) { 132 133 if (dis.next_is_operator ()) { 134 id = dis.read (); 135 136 if (id == 12) { 137 id = dis.read (); 138 } else { 139 switch (id) { 140 case 0: 141 version = val; 142 break; 143 default: 144 stderr.printf ("unknown operator"); 145 break; 146 } 147 } 148 } else { 149 val = dis.read_charstring_value (); 150 } 151 152 printd (@"$i: id $(id)\n"); 153 printd (@"val $(val)\n"); 154 //printd (@"B $(dis.read ())\n"); 155 //} 156 } 157 158 // string index 159 read_index (); 160 } 161 162 public void process () throws GLib.Error { 163 FontData fd = new FontData (); 164 string name = "typeface"; 165 166 // header 167 fd.add_byte (1); // format version (1.0) 168 fd.add_byte (0); 169 170 fd.add_byte (4); // header size 171 fd.add_byte (2); // offset field size - ushort 172 173 // name index: 174 fd.add_ushort (1); // number of entries 175 fd.add_byte (2); // offset field size 176 fd.add_ushort (1); // offset 177 fd.add ((uint8) name.length); // length of string 178 fd.add_str (name); 179 180 // top dict index 181 fd.add_ushort (1); // number of entries 182 fd.add_byte (2); // offset field size 183 fd.add_ushort (1); // offset 184 fd.add_ushort (2); // offset 185 186 fd.add_charstring_value (0); 187 fd.add_byte (CHARSET); 188 189 // string index 190 fd.add_byte (0); 191 192 // TODO: glyph gid to cid map 193 fd.add_byte (0); 194 195 fd.pad (); 196 197 this.font_data = fd; 198 } 199 } 200 201 } 202