Debian Codegraph @

texk/web2c/luatexdir/font/sfnt.c

raw
1 /*
2
3 Copyright 2002 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team <dvipdfmx@@project.ktug.or.kr>
5 Copyright 2006-2008 Taco Hoekwater <taco@@luatex.org>
6
7 This file is part of LuaTeX.
8
9 LuaTeX is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your
12 option) any later version.
13
14 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
21
22 */
23
24 /*tex
25
26 The code below is originally based on |dvipdfmx-0.13.2c|:
27
28 */
29
30 #include "ptexlib.h"
31
32 #if HAVE_CONFIG_H
33 # include <w2c/config.h>
34 #endif
35
36 #include <string.h>
37
38 #include "font/sfnt.h"
39
40 /*tex
41
42 Types:
43
44 \starttabulate[||||]
45 \NC \type {true} \NC 0x74727565 \NC (Mac) TrueType \NC \NR
46 \NC \type {typ1| \NC 0x74797031 \NC (Mac) PostScript font housed in a sfnt wrapper \NC \NR
47 \NC \NC 0x00010000 \NC TrueType (Win)/OpenType \NC \NR
48 \NC \type {OTTO} \NC \NC PostScript CFF font with OpenType wrapper \NC \NR
49 \NC \type {ttcf} \NC \NC TrueType Collection \NC \NR
50 \stoptabulate
51
52 */
53
54 #define SFNT_TRUETYPE 0x00010000UL
55 #define SFNT_MAC_TRUE 0x74727565UL
56 #define SFNT_OPENTYPE 0x00010000UL
57 #define SFNT_POSTSCRIPT 0x4f54544fUL
58 #define SFNT_TTC 0x74746366UL
59
60 sfnt *sfnt_open(unsigned char *buff, int buflen)
61 {
62 sfnt *sfont;
63 ULONG type;
64 sfont = xmalloc(sizeof(sfnt));
65 sfont->loc = 0;
66 sfont->buffer = buff;
67 sfont->buflen = buflen;
68 type = sfnt_get_ulong(sfont);
69 if (type == SFNT_TRUETYPE || type == SFNT_MAC_TRUE) {
70 sfont->type = SFNT_TYPE_TRUETYPE;
71 } else if (type == SFNT_OPENTYPE) {
72 sfont->type = SFNT_TYPE_OPENTYPE;
73 } else if (type == SFNT_POSTSCRIPT) {
74 sfont->type = SFNT_TYPE_POSTSCRIPT;
75 } else if (type == SFNT_TTC) {
76 sfont->type = SFNT_TYPE_TTC;
77 }
78 sfont->loc = 0;
79 sfont->directory = NULL;
80 return sfont;
81 }
82
83 static void release_directory(struct sfnt_table_directory *td)
84 {
85 long i;
86 if (td) {
87 if (td->tables) {
88 for (i = 0; i < td->num_tables; i++) {
89 if (td->tables[i].data)
90 RELEASE(td->tables[i].data);
91 }
92 RELEASE(td->tables);
93 }
94 if (td->flags)
95 RELEASE(td->flags);
96 RELEASE(td);
97 }
98 return;
99 }
100
101 void sfnt_close(sfnt * sfont)
102 {
103 if (sfont) {
104 if (sfont->directory)
105 release_directory(sfont->directory);
106 RELEASE(sfont);
107 }
108 return;
109 }
110
111 int put_big_endian(void *s, LONG q, int n)
112 {
113 int i;
114 char *p;
115 p = (char *) s;
116 for (i = n - 1; i >= 0; i--) {
117 p[i] = (char) (q & 0xff);
118 q >>= 8;
119 }
120 return n;
121 }
122
123 /*tex
124
125 Convert four-byte number to big endianess in a machine independent way.
126
127 */
128
129 static void convert_tag(char *tag, unsigned long u_tag)
130 {
131 int i;
132 for (i = 3; i >= 0; i--) {
133 tag[i] = (char) (u_tag % 256);
134 u_tag /= 256;
135 }
136 return;
137 }
138
139 /*tex
140
141 Computes the max power of $|2 <= n$:
142
143 */
144
145 static unsigned max2floor(unsigned n)
146 {
147 int val = 1;
148 while (n > 1) {
149 n /= 2;
150 val *= 2;
151 }
152 return (unsigned) val;
153 }
154
155 /*tex
156
157 Computes the log2 of the max power of $2 <= n$
158
159 */
160
161 static unsigned log2floor(unsigned n)
162 {
163 unsigned val = 0;
164 while (n > 1) {
165 n /= 2;
166 val++;
167 }
168 return val;
169 }
170
171 static ULONG sfnt_calc_checksum(void *data, ULONG length)
172 {
173 ULONG chksum = 0;
174 BYTE *p, *endptr;
175 ULONG count = 0;
176 p = (BYTE *) data;
177 endptr = p + length;
178 while (p < endptr) {
179 chksum = chksum + (ULONG) (p[0] << (8 * (3 - count)));
180 count = ((count + 1) & 3);
181 p++;
182 }
183 return chksum;
184 }
185
186 static int find_table_index(struct sfnt_table_directory *td, const char *tag)
187 {
188 int idx;
189 if (!td)
190 return -1;
191 for (idx = 0; idx < td->num_tables; idx++) {
192 if (!memcmp(td->tables[idx].tag, tag, 4))
193 return idx;
194 }
195 return -1;
196 }
197
198 void sfnt_set_table(sfnt * sfont, const char *tag, void *data, ULONG length)
199 {
200 struct sfnt_table_directory *td;
201 int idx;
202 td = sfont->directory;
203 idx = find_table_index(td, tag);
204 if (idx < 0) {
205 idx = td->num_tables;
206 td->num_tables++;
207 td->tables = RENEW(td->tables, td->num_tables, struct sfnt_table);
208 memcpy(td->tables[idx].tag, tag, 4);
209 }
210 td->tables[idx].check_sum = sfnt_calc_checksum(data, length);
211 td->tables[idx].offset = 0L;
212 td->tables[idx].length = length;
213 td->tables[idx].data = data;
214 return;
215 }
216
217 ULONG sfnt_find_table_len(sfnt * sfont, const char *tag)
218 {
219 ULONG length;
220 struct sfnt_table_directory *td;
221 int idx;
222 td = sfont->directory;
223 idx = find_table_index(td, tag);
224 if (idx < 0)
225 length = 0;
226 else {
227 length = td->tables[idx].length;
228 }
229 return length;
230 }
231
232 ULONG sfnt_find_table_pos(sfnt * sfont, const char *tag)
233 {
234 ULONG offset;
235 struct sfnt_table_directory *td;
236 int idx;
237 td = sfont->directory;
238 idx = find_table_index(td, tag);
239 if (idx < 0)
240 offset = 0;
241 else {
242 offset = td->tables[idx].offset;
243 }
244 return offset;
245 }
246
247 ULONG sfnt_locate_table(sfnt * sfont, const char *tag)
248 {
249 ULONG offset;
250 offset = sfnt_find_table_pos(sfont, tag);
251 if (offset == 0) {
252 formatted_error("ttf","%s table not found",tag);
253 }
254 sfnt_seek_set(sfont, (long) offset);
255 return offset;
256 }
257
258 int sfnt_read_table_directory(sfnt * sfont, ULONG offset)
259 {
260 struct sfnt_table_directory *td;
261 unsigned long i, u_tag;
262 if (sfont->directory)
263 release_directory(sfont->directory);
264 sfont->directory = td = NEW(1, struct sfnt_table_directory);
265 sfnt_seek_set(sfont, (long) offset);
266 td->version = sfnt_get_ulong(sfont);
267 td->num_tables = sfnt_get_ushort(sfont);
268 td->search_range = sfnt_get_ushort(sfont);
269 td->entry_selector = sfnt_get_ushort(sfont);
270 td->range_shift = sfnt_get_ushort(sfont);
271 td->flags = NEW(td->num_tables, char);
272 td->tables = NEW(td->num_tables, struct sfnt_table);
273 for (i = 0; i < td->num_tables; i++) {
274 u_tag = sfnt_get_ulong(sfont);
275 convert_tag(td->tables[i].tag, u_tag);
276 td->tables[i].check_sum = sfnt_get_ulong(sfont);
277 td->tables[i].offset = sfnt_get_ulong(sfont);
278 td->tables[i].length = sfnt_get_ulong(sfont);
279 td->tables[i].data = NULL;
280 td->flags[i] = 0;
281 }
282 td->num_kept_tables = 0;
283 return 0;
284 }
285
286 int sfnt_require_table(sfnt * sfont, const char *tag, int must_exist)
287 {
288 struct sfnt_table_directory *td;
289 int idx;
290 td = sfont->directory;
291 idx = find_table_index(td, tag);
292 if (idx < 0) {
293 if (must_exist)
294 return -1;
295 } else {
296 td->flags[idx] |= SFNT_TABLE_REQUIRED;
297 td->num_kept_tables++;
298 }
299 return 0;
300 }
301
302 /*tex
303
304 All tables begin on four byte boundries, and pad any remaining space between
305 tables with zeros. Entries in the Table Directory must be sorted in ascending
306 order by tag The head table contains checksum of the whole font file. To
307 compute: first set it to 0, sum the entire font as ULONG, then store
308 0xB1B0AFBA - sum.
309
310 */
311
312 # include "font/luatexfont.h"
313 # undef MIN
314 # define MIN(a, b) (((a) < (b)) ? (a) : (b))
315 # define STREAM_COMPRESS
316
317 static unsigned char wbuf[1024], padbytes[4] = { 0, 0, 0, 0 };
318
319 pdf_obj *sfnt_create_FontFile_stream(sfnt * sfont)
320 {
321 pdf_obj *stream;
322 struct sfnt_table_directory *td;
323 long offset, nb_read, length;
324 int i, sr;
325 char *p;
326 stream = pdf_new_stream(STREAM_COMPRESS);
327 td = sfont->directory;
328 p = (char *) wbuf;
329 p += sfnt_put_ulong(p, (LONG) td->version);
330 p += sfnt_put_ushort(p, td->num_kept_tables);
331 sr = (int) (max2floor(td->num_kept_tables) * 16);
332 p += sfnt_put_ushort(p, sr);
333 p += sfnt_put_ushort(p, log2floor(td->num_kept_tables));
334 p += sfnt_put_ushort(p, td->num_kept_tables * 16 - sr);
335 pdf_add_stream(stream, wbuf, 12);
336 /*tex Compute start of actual tables (after headers). */
337 offset = 12 + 16 * td->num_kept_tables;
338 for (i = 0; i < td->num_tables; i++) {
339 /*tex This table must exist in |FontFile|: */
340 if (td->flags[i] & SFNT_TABLE_REQUIRED) {
341 if ((offset % 4) != 0) {
342 offset += 4 - (offset % 4);
343 }
344 p = (char *) wbuf;
345 memcpy(p, td->tables[i].tag, 4);
346 p += 4;
347 p += sfnt_put_ulong(p, (LONG) td->tables[i].check_sum);
348 p += sfnt_put_ulong(p, offset);
349 p += sfnt_put_ulong(p, (LONG) td->tables[i].length);
350 pdf_add_stream(stream, wbuf, 16);
351 offset = (long) (offset + (long) td->tables[i].length);
352 }
353 }
354 offset = 12 + 16 * td->num_kept_tables;
355 for (i = 0; i < td->num_tables; i++) {
356 if (td->flags[i] & SFNT_TABLE_REQUIRED) {
357 if ((offset % 4) != 0) {
358 length = 4 - (offset % 4);
359 pdf_add_stream(stream, padbytes, length);
360 offset += length;
361 }
362 if (!td->tables[i].data) {
363 if (!sfont->buffer) {
364 pdf_release_obj(stream);
365 normal_error("ttf","file not opened or already closed");
366 return NULL;
367 }
368 length = (long) td->tables[i].length;
369 sfnt_seek_set(sfont, (long) td->tables[i].offset);
370 while (length > 0) {
371 nb_read = sfnt_read(wbuf, (int) MIN(length, 1024), sfont);
372 if (nb_read < 0) {
373 pdf_release_obj(stream);
374 normal_error("ttf","reading file failed");
375 return NULL;
376 } else if (nb_read > 0) {
377 pdf_add_stream(stream, wbuf, nb_read);
378 }
379 length -= nb_read;
380 }
381 } else {
382 pdf_add_stream(stream,
383 (unsigned char *) td->tables[i].data,
384 (long) td->tables[i].length);
385 RELEASE(td->tables[i].data);
386 td->tables[i].data = NULL;
387 }
388 /*tex Set offset for next table. */
389 offset = (long) (offset + (long) td->tables[i].length);
390 }
391 }
392 return stream;
393 }