1 /* otfopen.c -- OpenType font reader.
2
3 Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H15PRO167
6
7 This file is part of libotf.
8
9 Libotf is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 Libotf 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 Lesser General Public
20 License along with this library, in a file named COPYING; if not,
21 write to the Free Software Foundation, Inc., 59 Temple Place, Suite
22 330, Boston, MA 02111-1307, USA. */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <config.h>
28
29 #include "otf.h"
30 #include "internal.h"
31
32 #include FT_TRUETYPE_TABLES_H
33
34 /***
35 Table of contents (almost parallel to otf.h):
36
37 (0) Stream handler
38
39 (1) Readers for OTF Layout tables and OTF itself
40 (1-1) Basic types and functions
41 (1-2) "head" table
42 (1-3) "name" table
43 (1-4) "cmap" table
44 (1-5) Structures common to GDEF, GSUB, and GPOS
45 (1-6) "GDEF" table
46 (1-7) Structures for ScriptList, FeatureList, and LookupList
47 (1-8) Structures common to GSUB and GPOS
48 (1-9) "GSUB" table
49 (1-10) "GPOS" table
50 (1-11) Structure for OTF
51
52 (2) API for reading OTF
53 (2-1) OTF_open()
54 (2-2) OTF_close()
55 (2-3) OTF_get_table()
56 (2-4) OTF_check_table()
57
58 (5) API miscellaneous
59 */
60
61 int debug_flag = -1;
62
63 static void
set_debug_flag()64 set_debug_flag ()
65 {
66 debug_flag = getenv ("LIBOTF_DEBUG") != NULL;
67 }
68
69
70 /* (0) Stream handler
71
72 Example of typical usage of OTF_Stream.
73
74 {
75 OTF_Stream *stream;
76 OTF_StreamState state;
77 int offset, nbytes;
78
79 OPEN_STREAM (_FILE_NAME_, stream);
80 if (! stream)
81 _ERROR_;
82 SETUP_STREAM (stream, fp, 0, 256, _NAME_);
83 offset = READ_OFFSET (stream);
84 nbytes = READ_ULONG (stream);
85 SETUP_STREAM (stream, fp, offset, nbytes, _NAME2_);
86 ...;
87 CLOSE_STREAM (stream);
88 }
89
90 */
91
92 typedef struct
93 {
94 const char *name;
95 long pos;
96 long bufsize;
97 long allocated;
98 unsigned char *buf;
99 } OTF_Stream;
100
101 typedef long OTF_StreamState;
102
103 static OTF_Stream *
make_stream(const char * name)104 make_stream (const char *name)
105 {
106 OTF_Stream *stream;
107 char *errfmt = "stream creation%s";
108 void *errret = NULL;
109
110 stream = calloc (1, sizeof (OTF_Stream));
111 if (! stream)
112 OTF_ERROR (OTF_ERROR_MEMORY, "");
113 stream->name = name;
114 return stream;
115 }
116
117 static int
setup_stream(OTF_Stream * stream,FILE * fp,long offset,int nbytes)118 setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes)
119 {
120 char *errfmt = "stream setup for %s";
121 int errret = -1;
122
123 stream->pos = 0;
124 if (stream->allocated < nbytes)
125 {
126 unsigned char *buf = malloc (nbytes);
127
128 if (! buf)
129 OTF_ERROR (OTF_ERROR_MEMORY, stream->name);
130 if (stream->buf)
131 free (stream->buf);
132 stream->buf = buf;
133 stream->allocated = nbytes;
134 }
135 stream->bufsize = nbytes;
136 if (fseek (fp, offset, SEEK_SET) < 0)
137 OTF_ERROR (OTF_ERROR_FILE, stream->name);
138 if (fread (stream->buf, 1, nbytes, fp) != nbytes)
139 OTF_ERROR (OTF_ERROR_FILE, stream->name);
140 return 0;
141 }
142
143 static OTF_Stream *
make_stream_from_ft_face(FT_Face face,const char * name)144 make_stream_from_ft_face (FT_Face face, const char *name)
145 {
146 char *errfmt = "FT_Face stream creation for %s";
147 void *errret = NULL;
148 FT_ULong nbytes = 0;
149 unsigned char *buf;
150 OTF_Stream *stream;
151 FT_ULong tag = FT_MAKE_TAG (name[0], name[1], name[2], name[3]);
152
153 if (FT_Load_Sfnt_Table (face, tag, 0, NULL, &nbytes))
154 return NULL;
155 buf = malloc (nbytes);
156 if (! buf)
157 OTF_ERROR (OTF_ERROR_MEMORY, name);
158 if (FT_Load_Sfnt_Table (face, tag, 0, buf, &nbytes))
159 {
160 free (buf);
161 OTF_ERROR (OTF_ERROR_FT_FACE, name);
162 }
163 stream = make_stream (name);
164 if (! stream)
165 return NULL;
166 stream->pos = 0;
167 stream->buf = buf;
168 stream->allocated = nbytes;
169 stream->bufsize = nbytes;
170 return stream;
171 }
172
173 static void
free_stream(OTF_Stream * stream)174 free_stream (OTF_Stream *stream)
175 {
176 if (stream->buf)
177 free (stream->buf);
178 free (stream);
179 }
180
181 #define SAVE_STREAM(stream, state) ((state) = (stream)->pos)
182 #define RESTORE_STREAM(stream, state) ((stream)->pos = (state))
183 #define SEEK_STREAM(stream, offset) ((stream)->pos = (offset))
184
185 #define STREAM_CHECK_SIZE(stream, size) \
186 if ((stream)->pos + (size) > (stream)->bufsize) \
187 { \
188 char *errfmt = "buffer overrun in %s"; \
189 \
190 OTF_ERROR (OTF_ERROR_TABLE, (stream)->name); \
191 return errret; \
192 } \
193 else
194
195
196 #define READ_USHORT(stream, var) \
197 do { \
198 STREAM_CHECK_SIZE ((stream), 2); \
199 (var) = (((stream)->buf[(stream)->pos] << 8) \
200 | (stream)->buf[(stream)->pos + 1]); \
201 (stream)->pos += 2; \
202 } while (0)
203
204 #define READ_SHORT(stream, var) \
205 do { \
206 STREAM_CHECK_SIZE ((stream), 2); \
207 (var) = (short) (((stream)->buf[(stream)->pos] << 8) \
208 | (stream)->buf[(stream)->pos + 1]); \
209 (stream)->pos += 2; \
210 } while (0)
211
212 #define READ_UINT24(stream, var) \
213 do { \
214 STREAM_CHECK_SIZE ((stream), 3); \
215 (var) = (((stream)->buf[(stream)->pos ] << 16) \
216 | ((stream)->buf[(stream)->pos + 1] << 8) \
217 | (stream)->buf[(stream)->pos + 2]); \
218 (stream)->pos += 3; \
219 } while (0)
220
221 #define READ_ULONG(stream, var) \
222 do { \
223 STREAM_CHECK_SIZE ((stream), 4); \
224 (var) = (((stream)->buf[(stream)->pos] << 24) \
225 | ((stream)->buf[(stream)->pos + 1] << 16) \
226 | ((stream)->buf[(stream)->pos + 2] << 8) \
227 | (stream)->buf[(stream)->pos + 3]); \
228 (stream)->pos += 4; \
229 } while (0)
230
231 #define READ_LONG(stream, var) \
232 do { \
233 STREAM_CHECK_SIZE ((stream), 4); \
234 (var) = (int) (((stream)->buf[(stream)->pos] << 24) \
235 | ((stream)->buf[(stream)->pos + 1] << 16) \
236 | ((stream)->buf[(stream)->pos + 2] << 8) \
237 | (stream)->buf[(stream)->pos + 3]); \
238 (stream)->pos += 4; \
239 } while (0)
240
241
242 #define READ_FIXED(stream, fixed) \
243 do { \
244 READ_USHORT ((stream), (fixed).high); \
245 READ_USHORT ((stream), (fixed).low); \
246 } while (0)
247
248
249 #define READ_BYTES(stream, p, nbytes) \
250 do { \
251 STREAM_CHECK_SIZE ((stream), (nbytes)); \
252 memcpy ((p), (stream)->buf + (stream)->pos, (nbytes)); \
253 (stream)->pos += (nbytes); \
254 } while (0)
255
256
257 #define READ_TAG READ_ULONG
258 #define READ_OFFSET READ_USHORT
259 #define READ_UINT16 READ_USHORT
260 #define READ_INT16 READ_SHORT
261 #define READ_GLYPHID READ_USHORT
262
263
264 /*** (1) Structures for OTF Layout tables and OTF itself */
265
266 /*** (1-1) Basic types and functions */
267
268 enum OTF_TableType
269 {
270 OTF_TABLE_TYPE_HEAD,
271 OTF_TABLE_TYPE_NAME,
272 OTF_TABLE_TYPE_CMAP,
273 OTF_TABLE_TYPE_GDEF,
274 OTF_TABLE_TYPE_GSUB,
275 OTF_TABLE_TYPE_GPOS,
276 OTF_TABLE_TYPE_MAX
277 };
278
279 #define OTF_MEMORY_RECORD_SIZE 1024
280
281 struct OTF_MemoryRecord
282 {
283 int used;
284 void *memory[OTF_MEMORY_RECORD_SIZE];
285 struct OTF_MemoryRecord *next;
286 };
287
288 typedef struct OTF_MemoryRecord OTF_MemoryRecord;
289
290 enum OTF_ReaderFlag
291 {
292 OTF_READ_FULL,
293 OTF_READ_SCRIPTS,
294 OTF_READ_FEATURES,
295 OTF_READ_MAX
296 };
297
298 struct _OTF_TableInfo;
299 typedef struct _OTF_TableInfo OTF_TableInfo;
300
301 struct _OTF_TableInfo
302 {
303 /* Points to one of OTF->head, OTF->name, etc. */
304 void **address;
305 /* Function to read one of OTF tables. */
306 void *(*reader) (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag);
307 /* Stream given to <reader>. */
308 OTF_Stream *stream;
309 };
310
311 struct _OTF_ApplicationData
312 {
313 char *id;
314 void *data;
315 void (*freer) (void *data);
316 struct _OTF_ApplicationData *next;
317 };
318
319 typedef struct _OTF_ApplicationData OTF_ApplicationData;
320
321 struct OTF_InternalData
322 {
323 /* Information about each OTF table. */
324 OTF_TableInfo table_info[OTF_TABLE_TYPE_MAX];
325
326 /* Stream used to read the header part of OTF. */
327 OTF_Stream *header_stream;
328
329 /* Records of allocated memories. */
330 OTF_MemoryRecord *memory_record;
331
332 /* Root of application data chain. */
333 OTF_ApplicationData *app_data;
334 };
335
336 static OTF_MemoryRecord *
allocate_memory_record(OTF * otf)337 allocate_memory_record (OTF *otf)
338 {
339 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
340 OTF_MemoryRecord *memrec = malloc (sizeof (OTF_MemoryRecord));
341
342 if (! memrec)
343 return NULL;
344 memrec->used = 0;
345 memrec->next = internal_data->memory_record;
346 internal_data->memory_record = memrec;
347 return memrec;
348 }
349
350 /* Memory allocation macros. */
351
352 #define OTF_MALLOC(p, size, arg) \
353 do { \
354 if (size == 0) \
355 (p) = NULL; \
356 else \
357 { \
358 OTF_MemoryRecord *memrec \
359 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
360 (p) = malloc (sizeof (*(p)) * (size)); \
361 if (! (p) \
362 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
363 && ! (memrec = allocate_memory_record (otf)))) \
364 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
365 memrec->memory[memrec->used++] = (p); \
366 } \
367 } while (0)
368
369
370 #define OTF_CALLOC(p, size, arg) \
371 do { \
372 if (size == 0) \
373 (p) = NULL; \
374 else \
375 { \
376 OTF_MemoryRecord *memrec \
377 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
378 (p) = calloc ((size), sizeof (*(p))); \
379 if (! (p) \
380 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
381 && ! (memrec = allocate_memory_record (otf)))) \
382 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
383 memrec->memory[memrec->used++] = (p); \
384 } \
385 } while (0)
386
387
388 /*** (1-2) "head" table */
389
390 static void *
read_head_table(OTF * otf,OTF_TableInfo * table,enum OTF_ReaderFlag flag)391 read_head_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
392 {
393 OTF_Stream *stream = table->stream;
394 char *errfmt = "head%s";
395 void *errret = NULL;
396 OTF_head *head;
397
398 OTF_CALLOC (head, 1, "");
399 READ_FIXED (stream, head->TableVersionNumber);
400 READ_FIXED (stream, head->fontRevision);
401 READ_ULONG (stream, head->checkSumAdjustment);
402 READ_ULONG (stream, head->magicNumber);
403 READ_USHORT (stream, head->flags);
404 READ_USHORT (stream, head->unitsPerEm);
405
406 *table->address = head;
407 return head;
408 }
409
410
411 /*** (1-3) "name" table */
412
413 static int
read_name(OTF * otf,OTF_Stream * stream,OTF_NameRecord * rec)414 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec)
415 {
416 char errfmt[256];
417 int errret = -1;
418 OTF_StreamState state;
419 int ucs = 0;
420 int ascii = 0;
421 int i;
422
423 sprintf (errfmt, "nameID (%d)%%s", rec->nameID);
424 if (rec->platformID == 0)
425 ucs = (rec->encodingID <= 3) ? 2 : 4;
426 else if (rec->platformID == 1 && rec->encodingID == 0)
427 ascii = 1;
428 else if (rec->platformID == 3)
429 ucs = (rec->encodingID == 1 ? 2
430 : rec->encodingID == 10 ? 4
431 : 0);
432
433 OTF_MALLOC (rec->name, rec->length + 1, "");
434 SAVE_STREAM (stream, state);
435 SEEK_STREAM (stream, stream->pos + rec->offset);
436 READ_BYTES (stream, rec->name, rec->length);
437 RESTORE_STREAM (stream, state);
438 rec->name[rec->length] = 0;
439
440 if (ascii)
441 {
442 rec->ascii = 1;
443 }
444 else if (ucs == 2)
445 {
446 rec->ascii = 1;
447 for (i = 0; i < rec->length / 2; i++)
448 {
449 if (rec->name[i * 2] > 0
450 || rec->name[i * 2 + 1] >= 128)
451 {
452 rec->ascii = 0;
453 break;
454 }
455 }
456 if (rec->ascii)
457 for (i = 0; i < rec->length / 2; i++)
458 rec->name[i] = rec->name[i * 2 + 1];
459 rec->name[i] = 0;
460 }
461 else if (ucs == 4)
462 {
463 rec->ascii = 1;
464 for (i = 0; i < rec->length / 4; i++)
465 {
466 if (rec->name[i * 4] > 0
467 || rec->name[i * 4 + 1] > 0
468 || rec->name[i * 4 + 2] > 0
469 || rec->name[i * 2 + 3] >= 128)
470 {
471 rec->ascii = 0;
472 break;
473 }
474 }
475 if (rec->ascii)
476 for (i = 0; i < rec->length / 4; i++)
477 rec->name[i] = rec->name[i * 4 + 3];
478 rec->name[i] = 0;
479 }
480
481 return 0;
482 }
483
484 static void *
read_name_table(OTF * otf,OTF_TableInfo * table,enum OTF_ReaderFlag flag)485 read_name_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
486 {
487 OTF_Stream *stream = table->stream;
488 char *errfmt = "name%s";
489 void *errret = NULL;
490 OTF_name *name;
491 int i;
492
493 OTF_CALLOC (name, 1, "");
494 READ_USHORT (stream, name->format);
495 READ_USHORT (stream, name->count);
496 READ_USHORT (stream, name->stringOffset);
497 OTF_MALLOC (name->nameRecord, name->count, "");
498 for (i = 0; i < name->count; i++)
499 {
500 OTF_NameRecord *rec = name->nameRecord + i;
501
502 READ_USHORT (stream, rec->platformID);
503 READ_USHORT (stream, rec->encodingID);
504 READ_USHORT (stream, rec->languageID);
505 READ_USHORT (stream, rec->nameID);
506 READ_USHORT (stream, rec->length);
507 READ_USHORT (stream, rec->offset);
508 }
509 for (i = 0; i < name->count; i++)
510 {
511 OTF_NameRecord *rec = name->nameRecord + i;
512 int nameID = rec->nameID;
513
514 read_name (otf, stream, rec);
515
516 if (nameID >= OTF_max_nameID)
517 continue;
518 if (! name->name[nameID]
519 && rec->ascii)
520 name->name[nameID] = (char *) rec->name;
521 }
522
523 *table->address = name;
524 return name;
525 }
526
527
528 /*** (1-4) "cmap" table */
529
530 static OTF_EncodingSubtable14 *
read_cmap_uvs_table(OTF * otf,OTF_Stream * stream,OTF_Offset offset)531 read_cmap_uvs_table (OTF *otf, OTF_Stream *stream, OTF_Offset offset)
532 {
533 OTF_EncodingSubtable14 *sub14;
534 char *errfmt = "cmap-uvs%s";
535 void *errret = NULL;
536 unsigned nRecords;
537 unsigned i,j;
538
539 OTF_MALLOC (sub14, 1, " (EncodingSubtable14)");
540 READ_ULONG (stream, nRecords);
541 sub14->nRecords = nRecords;
542 OTF_MALLOC (sub14->Records, nRecords, "(EncodingSubtable14-Records)");
543 for (i = 0; i < sub14->nRecords; i++)
544 {
545 unsigned varSelector=0, defaultUVSOffset, nonDefaultUVSOffset;
546
547 READ_UINT24 (stream, varSelector);
548 sub14->Records[i].varSelector = varSelector;
549 READ_ULONG (stream, defaultUVSOffset);
550 sub14->Records[i].defaultUVSOffset = defaultUVSOffset;
551 READ_ULONG (stream, nonDefaultUVSOffset);
552 sub14->Records[i].nonDefaultUVSOffset = nonDefaultUVSOffset;
553 }
554 for (i = 0; i < sub14->nRecords; i++)
555 {
556 OTF_VariationSelectorRecord *record = &sub14->Records[i];
557 unsigned defaultUVSOffset = record->defaultUVSOffset;
558 unsigned nonDefaultUVSOffset = record->nonDefaultUVSOffset;
559
560 if (defaultUVSOffset)
561 {
562 unsigned numUnicodeValueRanges;
563
564 SEEK_STREAM (stream, offset+defaultUVSOffset);
565 READ_ULONG (stream, numUnicodeValueRanges);
566 record->numUnicodeValueRanges = numUnicodeValueRanges;
567 OTF_MALLOC (record->unicodeValueRanges,
568 numUnicodeValueRanges,
569 "(EncodingSubtable14-Records-unicodeValueRanges)");
570 for (j = 0; j < numUnicodeValueRanges; j++)
571 {
572 OTF_UnicodeValueRange *unicodeValueRange
573 = &record->unicodeValueRanges[j];
574 unsigned startUnicodeValue;
575 char additionalCount;
576
577 READ_UINT24 (stream, startUnicodeValue);
578 unicodeValueRange->startUnicodeValue=startUnicodeValue;
579 READ_BYTES (stream, &additionalCount, 1);
580 unicodeValueRange->additionalCount
581 = (unsigned short) additionalCount;
582 }
583 }
584 if (nonDefaultUVSOffset)
585 {
586 unsigned numUVSMappings;
587
588 SEEK_STREAM (stream, offset+nonDefaultUVSOffset);
589 READ_ULONG (stream, numUVSMappings);
590 record->numUVSMappings = numUVSMappings;
591 OTF_MALLOC (record->uvsMappings, numUVSMappings,
592 "(EncodingSubtable14-Records-uvsMappings)");
593 for (j = 0; j < numUVSMappings; j++)
594 {
595 OTF_UVSMapping *uvsMapping = &record->uvsMappings[j];
596 unsigned unicodeValue;
597 unsigned short glyphID;
598
599 READ_UINT24 (stream, unicodeValue);
600 uvsMapping->unicodeValue = unicodeValue;
601 READ_USHORT (stream, glyphID);
602 uvsMapping->glyphID = glyphID;
603 }
604 }
605 }
606 return sub14;
607 }
608
609 static void *
read_cmap_table(OTF * otf,OTF_TableInfo * table,enum OTF_ReaderFlag flag)610 read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
611 {
612 OTF_Stream *stream = table->stream;
613 char *errfmt = "cmap%s";
614 void *errret = NULL;
615 OTF_cmap *cmap;
616 int unicode_bmp_index = -1, unicode_full_index = -1;
617 int i;
618
619 OTF_CALLOC (cmap, 1, "");
620 READ_USHORT (stream, cmap->version);
621 READ_USHORT (stream, cmap->numTables);
622 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
623 for (i = 0; i < cmap->numTables; i++)
624 {
625 unsigned platformID, encodingID;
626
627 READ_USHORT (stream, platformID);
628 cmap->EncodingRecord[i].platformID = platformID;
629 READ_USHORT (stream, encodingID);
630 cmap->EncodingRecord[i].encodingID = encodingID;
631 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
632 if (platformID == 0)
633 {
634 if (encodingID <= 3)
635 unicode_bmp_index = i;
636 else
637 unicode_full_index = i;
638 }
639 else if (platformID == 3)
640 {
641 if (encodingID == 1)
642 unicode_bmp_index = i;
643 else if (encodingID == 10)
644 unicode_full_index = i;
645 }
646 }
647 cmap->table_index = unicode_full_index;
648
649 for (i = 0; i < cmap->numTables; i++)
650 {
651 unsigned format;
652
653 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
654 READ_USHORT (stream, format);
655 cmap->EncodingRecord[i].subtable.format = format;
656 if (format == 14)
657 {
658 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
659 cmap->EncodingRecord[i].subtable.language = 0;
660 }
661 else
662 {
663 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
664 if (format == 8 || format == 10 || format == 12)
665 {
666 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
667 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
668 }
669 else
670 {
671 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
672 }
673 }
674 switch (format)
675 {
676 case 0:
677 {
678 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
679 " (EncodingRecord)");
680 READ_BYTES (stream,
681 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
682 256);
683 }
684 break;
685
686 case 2:
687 {
688 OTF_EncodingSubtable2 *sub2;
689 int j, max_key;
690
691 OTF_MALLOC (sub2, 1, " (EncodingSubtable2)");
692 cmap->EncodingRecord[i].subtable.f.f2 = sub2;
693 for (j = 0, max_key = 0; j < 256; j++)
694 {
695 READ_USHORT (stream, sub2->subHeaderKeys[j]);
696 if (max_key < sub2->subHeaderKeys[j])
697 max_key = sub2->subHeaderKeys[j];
698 }
699 max_key += 8;
700 sub2->subHeaderCount = max_key / 8;
701 OTF_MALLOC (sub2->subHeaders, max_key / 8, " (subHeaders)");
702 for (j = 0; j < sub2->subHeaderCount; j++)
703 {
704 READ_USHORT (stream, sub2->subHeaders[j].firstCode);
705 READ_USHORT (stream, sub2->subHeaders[j].entryCount);
706 READ_SHORT (stream, sub2->subHeaders[j].idDelta);
707 READ_USHORT (stream, sub2->subHeaders[j].idRangeOffset);
708 /* Make it offset from sub2->glyphIndexArray. */
709 sub2->subHeaders[j].idRangeOffset -= max_key - (j * 8 + 6);
710 }
711 sub2->glyphIndexCount = (cmap->EncodingRecord[i].subtable.length
712 - 262 - max_key);
713 OTF_MALLOC (sub2->glyphIndexArray, sub2->glyphIndexCount,
714 " (glyphIndexArray)");
715 READ_BYTES (stream, sub2->glyphIndexArray, sub2->glyphIndexCount);
716 }
717 break;
718
719 case 4:
720 {
721 OTF_EncodingSubtable4 *sub4;
722 int segCount;
723 int j;
724 unsigned dummy;
725
726 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
727 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
728 READ_USHORT (stream, sub4->segCountX2);
729 segCount = sub4->segCountX2 / 2;
730 READ_USHORT (stream, sub4->searchRange);
731 READ_USHORT (stream, sub4->entrySelector);
732 READ_USHORT (stream, sub4->rangeShift);
733 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
734 for (j = 0; j < segCount; j++)
735 READ_USHORT (stream, sub4->segments[j].endCount);
736 READ_USHORT (stream, dummy);
737 for (j = 0; j < segCount; j++)
738 READ_USHORT (stream, sub4->segments[j].startCount);
739 for (j = 0; j < segCount; j++)
740 READ_SHORT (stream, sub4->segments[j].idDelta);
741 for (j = 0; j < segCount; j++)
742 {
743 unsigned off;
744 unsigned rest = 2 * (segCount - j);
745
746 READ_USHORT (stream, off);
747 if (off == 0)
748 sub4->segments[j].idRangeOffset = 0xFFFF;
749 else
750 sub4->segments[j].idRangeOffset = (off - rest) / 2;
751 }
752 j = (cmap->EncodingRecord[i].subtable.length
753 - (14 + 2 * (segCount * 4 + 1)));
754 sub4->GlyphCount = j / 2;
755 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
756 for (j = 0; j < sub4->GlyphCount; j++)
757 READ_USHORT (stream, sub4->glyphIdArray[j]);
758 }
759 break;
760
761 case 6:
762 {
763 OTF_EncodingSubtable6 *sub6;
764 int j;
765
766 OTF_MALLOC (sub6, 1, " (EncodingSubtable6)");
767 cmap->EncodingRecord[i].subtable.f.f6 = sub6;
768 READ_USHORT (stream, sub6->firstCode);
769 READ_USHORT (stream, sub6->entryCount);
770 OTF_MALLOC (sub6->glyphIdArray, sub6->entryCount, " (GlyphCount)");
771 for (j = 0; j < sub6->entryCount; j++)
772 READ_USHORT (stream, sub6->glyphIdArray[j]);
773 }
774 break;
775
776 case 8:
777 {
778 OTF_EncodingSubtable8 *sub8;
779 int j;
780
781 OTF_MALLOC (sub8, 1, " (EncodingSubtable8)");
782 cmap->EncodingRecord[i].subtable.f.f8 = sub8;
783 for (j = 0; j < 8192; j++)
784 READ_BYTES (stream, sub8->is32, 8192);
785 READ_ULONG (stream, sub8->nGroups);
786 OTF_MALLOC (sub8->Groups, sub8->nGroups, " (Groups)");
787 for (j = 0; j < sub8->nGroups; j++)
788 {
789 READ_ULONG (stream, sub8->Groups[i].startCharCode);
790 READ_ULONG (stream, sub8->Groups[i].endCharCode);
791 READ_ULONG (stream, sub8->Groups[i].startGlyphID);
792 }
793 }
794 break;
795
796 case 10:
797 {
798 OTF_EncodingSubtable10 *sub10;
799 int j;
800
801 OTF_MALLOC (sub10, 1, " (EncodingSubtable10)");
802 cmap->EncodingRecord[i].subtable.f.f10 = sub10;
803 READ_ULONG (stream, sub10->startCharCode);
804 READ_ULONG (stream, sub10->numChars);
805 OTF_MALLOC (sub10->glyphs, sub10->numChars, " (GlyphCount)");
806 for (j = 0; j < sub10->numChars; j++)
807 READ_USHORT (stream, sub10->glyphs[j]);
808 }
809 break;
810
811 case 12:
812 {
813 OTF_EncodingSubtable12 *sub12;
814 int j;
815
816 OTF_MALLOC (sub12, 1, " (EncodingSubtable12)");
817 cmap->EncodingRecord[i].subtable.f.f12 = sub12;
818 READ_ULONG (stream, sub12->nGroups);
819 OTF_MALLOC (sub12->Groups, sub12->nGroups, " (Groups)");
820 for (j = 0; j < sub12->nGroups; j++)
821 {
822 READ_ULONG (stream, sub12->Groups[j].startCharCode);
823 READ_ULONG (stream, sub12->Groups[j].endCharCode);
824 READ_ULONG (stream, sub12->Groups[j].startGlyphID);
825 }
826 }
827 break;
828
829 case 14:
830 {
831 cmap->EncodingRecord[i].subtable.f.f14
832 = read_cmap_uvs_table (otf, stream,
833 cmap->EncodingRecord[i].offset);
834 break;
835 }
836
837 default:
838 OTF_ERROR (OTF_ERROR_TABLE, " (invalid Subtable format)");
839 }
840 }
841
842 if (unicode_bmp_index >= 0)
843 {
844 OTF_EncodingRecord *rec = cmap->EncodingRecord + unicode_bmp_index;
845 OTF_GlyphID glyph_id, max_glyph_id = 0;
846
847 OTF_CALLOC (cmap->unicode_table, 0x10000, "");
848 switch (rec->subtable.format)
849 {
850 case 4:
851 {
852 OTF_EncodingSubtable4 *sub4 = rec->subtable.f.f4;
853 int segCount = sub4->segCountX2 / 2;
854
855 for (i = 0; i < segCount; i++)
856 {
857 OTF_cmapSegment *seg = sub4->segments + i;
858 int c;
859
860 if (seg->idRangeOffset == 0xFFFF)
861 for (c = seg->startCount; c <= seg->endCount; c++)
862 {
863 glyph_id = (c + seg->idDelta) % 0x10000;
864 cmap->unicode_table[c] = glyph_id;
865 if (glyph_id > max_glyph_id)
866 max_glyph_id = glyph_id;
867 }
868 else
869 for (c = seg->startCount; c <= seg->endCount && c != 0xFFFF;
870 c++)
871 {
872 glyph_id = sub4->glyphIdArray[seg->idRangeOffset
873 + (c - seg->startCount)];
874 cmap->unicode_table[c] = glyph_id;
875 if (glyph_id > max_glyph_id)
876 max_glyph_id = glyph_id;
877 }
878 }
879 }
880 }
881
882 OTF_CALLOC (cmap->decode_table, max_glyph_id + 1, "");
883 for (i = 0; i < 0x10000; i++)
884 if (cmap->unicode_table[i])
885 cmap->decode_table[cmap->unicode_table[i]] = i;
886 cmap->max_glyph_id = max_glyph_id;
887 }
888
889 *table->address = cmap;
890 return cmap;
891 }
892
893
894 /*** (1-5) Structures common to GDEF, GSUB, and GPOS */
895
896 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
897 Glyph-IDs there. If COUNT is negative, read the number of
898 Glyphs-IDs at first. MINUS if nozero is how few the actual
899 Glyph-IDs are in STREAM than COUNT. */
900
901 static int
read_glyph_ids(OTF * otf,OTF_Stream * stream,OTF_GlyphID ** ids,int minus,int count)902 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
903 int minus, int count)
904 {
905 char *errfmt = "GlyphID List%s";
906 int errret = -1;
907 int i;
908
909 if (count < 0)
910 READ_UINT16 (stream, count);
911 if (! count)
912 return 0;
913 OTF_MALLOC (*ids, count, "");
914 for (i = 0; i < count + minus; i++)
915 READ_GLYPHID (stream, (*ids)[i]);
916 return count;
917 }
918
919 static unsigned
read_range_records(OTF * otf,OTF_Stream * stream,OTF_RangeRecord ** record)920 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
921 {
922 char *errfmt = "RangeRecord%s";
923 unsigned errret = 0;
924 unsigned count;
925 int i;
926
927 READ_UINT16 (stream, count);
928 if (! count)
929 return 0;
930 OTF_MALLOC (*record, count, "");
931 for (i = 0; i < count; i++)
932 {
933 READ_GLYPHID (stream, (*record)[i].Start);
934 READ_GLYPHID (stream, (*record)[i].End);
935 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
936 }
937 return count;
938 }
939
940
941 static int
read_coverage(OTF * otf,OTF_Stream * stream,long offset,OTF_Coverage * coverage)942 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
943 OTF_Coverage *coverage)
944 {
945 char *errfmt = "Coverage%s";
946 int errret = -1;
947 OTF_StreamState state;
948 int count;
949
950 READ_OFFSET (stream, coverage->offset);
951 SAVE_STREAM (stream, state);
952 SEEK_STREAM (stream, offset + coverage->offset);
953 READ_UINT16 (stream, coverage->CoverageFormat);
954 if (coverage->CoverageFormat == 1)
955 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
956 else if (coverage->CoverageFormat == 2)
957 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
958 else
959 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
960 if (count < 0)
961 return -1;
962 coverage->Count = (unsigned) count;
963 RESTORE_STREAM (stream, state);
964 return 0;
965 }
966
967 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
968 and store the Coverages there. If COUNT is negative, read the
969 number of Coverages at first. */
970
971 static int
read_coverage_list(OTF * otf,OTF_Stream * stream,long offset,OTF_Coverage ** coverage,int count)972 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
973 OTF_Coverage **coverage, int count)
974 {
975 char *errfmt = "Coverage List%s";
976 int errret = -1;
977 int i;
978
979 if (count < 0)
980 READ_UINT16 (stream, count);
981 if (! count)
982 return 0;
983 OTF_MALLOC (*coverage, count, "");
984 for (i = 0; i < count; i++)
985 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
986 return -1;
987 return count;
988 }
989
990
991 static int
read_class_def_without_offset(OTF * otf,OTF_Stream * stream,OTF_ClassDef * class)992 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
993 OTF_ClassDef *class)
994 {
995 char *errfmt = "ClassDef%s";
996 int errret = -1;
997
998 SEEK_STREAM (stream, class->offset);
999 READ_UINT16 (stream, class->ClassFormat);
1000 if (class->ClassFormat == 1)
1001 {
1002 READ_GLYPHID (stream, class->f.f1.StartGlyph);
1003 class->f.f1.GlyphCount
1004 = (read_glyph_ids
1005 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
1006 if (! class->f.f1.GlyphCount)
1007 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1008 }
1009 else if (class->ClassFormat == 2)
1010 {
1011 class->f.f2.ClassRangeCount
1012 = (read_range_records
1013 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
1014 if (! class->f.f2.ClassRangeCount)
1015 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1016 }
1017 else
1018 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1019 return 0;
1020 }
1021
1022
1023 static int
read_class_def(OTF * otf,OTF_Stream * stream,long offset,OTF_ClassDef * class)1024 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
1025 {
1026 char *errfmt = "ClassDef%s";
1027 int errret = -1;
1028 OTF_StreamState state;
1029
1030 READ_OFFSET (stream, class->offset);
1031 if (! class->offset)
1032 return 0;
1033 SAVE_STREAM (stream, state);
1034 SEEK_STREAM (stream, offset + class->offset);
1035 READ_UINT16 (stream, class->ClassFormat);
1036 if (class->ClassFormat == 1)
1037 {
1038 READ_GLYPHID (stream, class->f.f1.StartGlyph);
1039 class->f.f1.GlyphCount
1040 = read_glyph_ids (otf, stream,
1041 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
1042 0, -1);
1043 }
1044 else if (class->ClassFormat == 2)
1045 {
1046 class->f.f2.ClassRangeCount
1047 = read_range_records (otf, stream,
1048 (OTF_RangeRecord **)
1049 &class->f.f2.ClassRangeRecord);
1050 }
1051 else
1052 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1053
1054 RESTORE_STREAM (stream, state);
1055 return 0;
1056 }
1057
1058
1059 static int
read_device_table(OTF * otf,OTF_Stream * stream,long offset,OTF_DeviceTable * table)1060 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
1061 OTF_DeviceTable *table)
1062 {
1063 char *errfmt = "Device Table%s";
1064 int errret = -1;
1065
1066 int num, i;
1067 unsigned val;
1068 struct {
1069 int int2 : 2;
1070 int int4 : 4;
1071 int int8 : 8;
1072 } intval;
1073
1074 SEEK_STREAM (stream, offset + table->offset);
1075 READ_UINT16 (stream, table->StartSize);
1076 READ_UINT16 (stream, table->EndSize);
1077 READ_UINT16 (stream, table->DeltaFormat);
1078 num = table->EndSize - table->StartSize + 1;
1079 if (num > 0 && table->DeltaFormat >= 1 && table->DeltaFormat <= 3)
1080 {
1081 OTF_MALLOC (table->DeltaValue, num, "");
1082
1083 if (table->DeltaFormat == 1)
1084 for (i = 0; i < num; i++)
1085 {
1086 if ((i % 8) == 0)
1087 READ_UINT16 (stream, val);
1088 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
1089 table->DeltaValue[i] = intval.int2;
1090 }
1091 else if (table->DeltaFormat == 2)
1092 for (i = 0; i < num; i++)
1093 {
1094 if ((i % 4) == 0)
1095 READ_UINT16 (stream, val);
1096 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
1097 table->DeltaValue[i] = intval.int4;
1098 }
1099 else /* (table->DeltaFormat == 3) */
1100 for (i = 0; i < num; i++)
1101 {
1102 if ((i % 2) == 0)
1103 {
1104 READ_UINT16 (stream, val);
1105 intval.int8 = val >> 8;
1106 table->DeltaValue[i] = intval.int8;
1107 }
1108 else
1109 {
1110 intval.int8 = val >> 8;
1111 table->DeltaValue[i] = intval.int8;
1112 }
1113 }
1114 }
1115 else
1116 {
1117 /* Invalid DeltaFormat but several fonts has such values (bug of
1118 fontforge?). So accept it with NULL delta values. */
1119 table->DeltaValue = NULL;
1120 }
1121 return 0;
1122 }
1123
1124
1125 /*** (1-6) "GDEF" table */
1126
1127 static int
read_attach_list(OTF * otf,OTF_Stream * stream,long offset,OTF_AttachList * list)1128 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
1129 OTF_AttachList *list)
1130 {
1131 char *errfmt = "AttachList%s";
1132 int errret = -1;
1133 int i, j;
1134
1135 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1136 return -1;
1137 READ_UINT16 (stream, list->GlyphCount);
1138 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
1139 for (i = 0; i < list->GlyphCount; i++)
1140 READ_OFFSET (stream, list->AttachPoint[i].offset);
1141 for (i = 0; i < list->GlyphCount; i++)
1142 {
1143 int count;
1144
1145 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
1146 READ_UINT16 (stream, count);
1147 list->AttachPoint[i].PointCount = count;
1148 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
1149 for (j = 0; j < count; j++)
1150 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
1151 }
1152 return 0;
1153 }
1154
1155 static int
read_caret_value(OTF * otf,OTF_Stream * stream,long offset,OTF_CaretValue * caret)1156 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
1157 OTF_CaretValue *caret)
1158 {
1159 char *errfmt = "CaretValue%s";
1160 int errret = -1;
1161
1162 SEEK_STREAM (stream, offset + caret->offset);
1163 READ_UINT16 (stream, caret->CaretValueFormat);
1164 if (caret->CaretValueFormat == 1)
1165 READ_INT16 (stream, caret->f.f1.Coordinate);
1166 else if (caret->CaretValueFormat == 2)
1167 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
1168 else if (caret->CaretValueFormat == 3)
1169 {
1170 READ_INT16 (stream, caret->f.f3.Coordinate);
1171 if (read_device_table (otf, stream, offset + caret->offset,
1172 &caret->f.f3.DeviceTable) < 0)
1173 return -1;
1174 }
1175 else
1176 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1177 return 0;
1178 }
1179
1180 static int
read_lig_caret_list(OTF * otf,OTF_Stream * stream,long offset,OTF_LigCaretList * list)1181 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
1182 OTF_LigCaretList *list)
1183 {
1184 char *errfmt = "LigCaretList%s";
1185 int errret = -1;
1186 int i, j;
1187
1188 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1189 return -1;
1190 READ_UINT16 (stream, list->LigGlyphCount);
1191 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
1192 for (i = 0; i < list->LigGlyphCount; i++)
1193 READ_OFFSET (stream, list->LigGlyph[i].offset);
1194 for (i = 0; i < list->LigGlyphCount; i++)
1195 {
1196 int count;
1197
1198 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
1199 READ_UINT16 (stream, count);
1200 list->LigGlyph[i].CaretCount = count;
1201 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
1202 for (j = 0; j < count; j++)
1203 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
1204 for (j = 0; j < count; j++)
1205 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
1206 &list->LigGlyph[i].CaretValue[j]) < 0)
1207 return -1;
1208 }
1209 return 0;
1210 }
1211
1212 static int
read_gdef_header(OTF_Stream * stream,OTF_GDEFHeader * header)1213 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1214 {
1215 int errret = -1;
1216
1217 READ_FIXED (stream, header->Version);
1218 READ_OFFSET (stream, header->GlyphClassDef);
1219 READ_OFFSET (stream, header->AttachList);
1220 READ_OFFSET (stream, header->LigCaretList);
1221 READ_OFFSET (stream, header->MarkAttachClassDef);
1222 return 0;
1223 }
1224
1225 static void *
read_gdef_table(OTF * otf,OTF_TableInfo * table,enum OTF_ReaderFlag flag)1226 read_gdef_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
1227 {
1228 OTF_Stream *stream = table->stream;
1229 char *errfmt = "GDEF%s";
1230 void *errret = NULL;
1231 OTF_GDEF *gdef;
1232
1233 OTF_CALLOC (gdef, 1, "");
1234 if (stream->buf)
1235 {
1236 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1237 if (gdef->header.GlyphClassDef)
1238 {
1239 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1240 read_class_def_without_offset (otf, stream,
1241 &gdef->glyph_class_def);
1242 }
1243 if (gdef->header.AttachList)
1244 read_attach_list (otf, stream, gdef->header.AttachList,
1245 &gdef->attach_list);
1246 if (gdef->header.LigCaretList)
1247 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1248 &gdef->lig_caret_list);
1249 if (gdef->header.MarkAttachClassDef)
1250 {
1251 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1252 read_class_def_without_offset (otf, stream,
1253 &gdef->mark_attach_class_def);
1254 }
1255 }
1256
1257 *table->address = gdef;
1258 return gdef;
1259 }
1260
1261
1262 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList */
1263
1264 static int
read_script_list(OTF * otf,OTF_Stream * stream,long offset,OTF_ScriptList * list)1265 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
1266 OTF_ScriptList *list)
1267 {
1268 char *errfmt = "Script List%s";
1269 int errret = -1;
1270 int i, j, k;
1271
1272 SEEK_STREAM (stream, offset);
1273 READ_USHORT (stream, list->ScriptCount);
1274 OTF_CALLOC (list->Script, list->ScriptCount, "");
1275
1276 for (i = 0; i < list->ScriptCount; i++)
1277 {
1278 READ_TAG (stream, list->Script[i].ScriptTag);
1279 READ_OFFSET (stream, list->Script[i].offset);
1280 }
1281 for (i = 0; i < list->ScriptCount; i++)
1282 {
1283 OTF_Script *script = list->Script + i;
1284 long script_offset = offset + script->offset;
1285
1286 SEEK_STREAM (stream, script_offset);
1287 READ_OFFSET (stream, script->DefaultLangSysOffset);
1288 READ_USHORT (stream, script->LangSysCount);
1289 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
1290 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
1291 for (j = 0; j < script->LangSysCount; j++)
1292 {
1293 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
1294 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
1295 }
1296
1297 if (script->DefaultLangSysOffset)
1298 {
1299 OTF_LangSys *langsys = &script->DefaultLangSys;
1300
1301 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
1302 READ_OFFSET (stream, langsys->LookupOrder);
1303 READ_USHORT (stream, langsys->ReqFeatureIndex);
1304 READ_USHORT (stream, langsys->FeatureCount);
1305 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1306 " (FeatureIndex)");
1307 for (k = 0; k < langsys->FeatureCount; k++)
1308 READ_USHORT (stream, langsys->FeatureIndex[k]);
1309 }
1310
1311 for (j = 0; j < script->LangSysCount; j++)
1312 {
1313 OTF_LangSys *langsys = script->LangSys + j;
1314
1315 SEEK_STREAM (stream,
1316 script_offset + script->LangSysRecord[j].LangSys);
1317 READ_OFFSET (stream, langsys->LookupOrder);
1318 READ_USHORT (stream, langsys->ReqFeatureIndex);
1319 READ_USHORT (stream, langsys->FeatureCount);
1320 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1321 " (FeatureIndex)");
1322 for (k = 0; k < langsys->FeatureCount; k++)
1323 READ_USHORT (stream, langsys->FeatureIndex[k]);
1324 }
1325 }
1326
1327 return 0;
1328 }
1329
1330 static int
read_feature_list(OTF * otf,OTF_Stream * stream,long offset,OTF_FeatureList * list)1331 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
1332 OTF_FeatureList *list)
1333 {
1334 char *errfmt = "Feature List%s";
1335 int errret = -1;
1336 int i, j;
1337
1338 SEEK_STREAM (stream, offset);
1339 READ_UINT16 (stream, list->FeatureCount);
1340 OTF_CALLOC (list->Feature, list->FeatureCount, "");
1341 for (i = 0; i < list->FeatureCount; i++)
1342 {
1343 READ_TAG (stream, list->Feature[i].FeatureTag);
1344 READ_OFFSET (stream, list->Feature[i].offset);
1345 }
1346 for (i = 0; i < list->FeatureCount; i++)
1347 {
1348 OTF_Feature *feature = list->Feature + i;
1349
1350 SEEK_STREAM (stream, offset + feature->offset);
1351 READ_OFFSET (stream, feature->FeatureParams);
1352 READ_UINT16 (stream, feature->LookupCount);
1353 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
1354 " (LookupListIndex)");
1355 for (j = 0; j < feature->LookupCount; j++)
1356 READ_UINT16 (stream, feature->LookupListIndex[j]);
1357 }
1358
1359 return 0;
1360 }
1361
1362 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
1363 long offset, unsigned type,
1364 OTF_LookupSubTableGSUB *subtable);
1365 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1366 long offset, unsigned type,
1367 OTF_LookupSubTableGPOS *subtable);
1368
1369 static int
read_lookup_list(OTF * otf,OTF_Stream * stream,long offset,OTF_LookupList * list,int gsubp)1370 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
1371 OTF_LookupList *list, int gsubp)
1372 {
1373 char *errfmt = "Lookup List%s";
1374 int errret = -1;
1375 int i, j;
1376
1377 SEEK_STREAM (stream, offset);
1378 READ_UINT16 (stream, list->LookupCount);
1379 OTF_CALLOC (list->Lookup, list->LookupCount, "");
1380
1381 for (i = 0; i < list->LookupCount; i++)
1382 READ_OFFSET (stream, list->Lookup[i].offset);
1383 for (i = 0; i < list->LookupCount; i++)
1384 {
1385 OTF_Lookup *lookup = list->Lookup + i;
1386
1387 SEEK_STREAM (stream, offset + lookup->offset);
1388 READ_UINT16 (stream, lookup->LookupType);
1389 READ_UINT16 (stream, lookup->LookupFlag);
1390 READ_UINT16 (stream, lookup->SubTableCount);
1391 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
1392 " (SubTableOffset)");
1393 if (gsubp)
1394 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
1395 " (SubTable)");
1396 else
1397 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
1398 " (SubTable)");
1399 for (j = 0; j < lookup->SubTableCount; j++)
1400 READ_OFFSET (stream, lookup->SubTableOffset[j]);
1401 for (j = 0; j < lookup->SubTableCount; j++)
1402 {
1403 long this_offset
1404 = offset + lookup->offset + lookup->SubTableOffset[j];
1405
1406 if (gsubp
1407 ? read_lookup_subtable_gsub (otf, stream, this_offset,
1408 lookup->LookupType,
1409 lookup->SubTable.gsub + j) < 0
1410 : read_lookup_subtable_gpos (otf, stream, this_offset,
1411 lookup->LookupType,
1412 lookup->SubTable.gpos + j) < 0)
1413 return errret;
1414 }
1415 }
1416
1417 return 0;
1418 }
1419
1420
1421 /*** (1-8) Structures common to GSUB and GPOS */
1422
1423 static int
read_lookup_record_list(OTF * otf,OTF_Stream * stream,OTF_LookupRecord ** record,int count)1424 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
1425 OTF_LookupRecord **record, int count)
1426 {
1427 char *errfmt = "LookupRecord%s";
1428 int errret = -1;
1429 int i;
1430
1431 if (count < 0)
1432 READ_UINT16 (stream, count);
1433 OTF_MALLOC (*record, count, "");
1434 for (i = 0; i < count; i++)
1435 {
1436 READ_UINT16 (stream, (*record)[i].SequenceIndex);
1437 READ_UINT16 (stream, (*record)[i].LookupListIndex);
1438 }
1439 return count;
1440 }
1441
1442 static unsigned
read_rule_list(OTF * otf,OTF_Stream * stream,long offset,OTF_Rule ** rule)1443 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
1444 {
1445 char *errfmt = "List of Rule%s";
1446 unsigned errret = 0;
1447 OTF_StreamState state;
1448 unsigned count;
1449 int i;
1450
1451 READ_UINT16 (stream, count);
1452 if (! count)
1453 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1454 OTF_MALLOC (*rule, count, "");
1455 for (i = 0; i < count; i++)
1456 {
1457 READ_OFFSET (stream, (*rule)[i].offset);
1458 if (! (*rule)[i].offset)
1459 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1460 }
1461 SAVE_STREAM (stream, state);
1462 for (i = 0; i < count; i++)
1463 {
1464 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1465 READ_UINT16 (stream, (*rule)[i].GlyphCount);
1466 if ((*rule)[i].GlyphCount == 0)
1467 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1468 READ_UINT16 (stream, (*rule)[i].LookupCount);
1469 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
1470 (*rule)[i].GlyphCount) < 0)
1471 return errret;
1472 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1473 (*rule)[i].LookupCount) < 0)
1474 return errret;
1475 }
1476 RESTORE_STREAM (stream, state);
1477 return count;
1478 }
1479
1480
1481 static unsigned
read_rule_set_list(OTF * otf,OTF_Stream * stream,long offset,OTF_RuleSet ** set)1482 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1483 OTF_RuleSet **set)
1484 {
1485 char *errfmt = "List of RuleSet%s";
1486 unsigned errret = 0;
1487 OTF_StreamState state;
1488 unsigned count;
1489 int i;
1490
1491 READ_UINT16 (stream, count);
1492 if (! count)
1493 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1494 OTF_MALLOC (*set, count, "");
1495 for (i = 0; i < count; i++)
1496 {
1497 READ_OFFSET (stream, (*set)[i].offset);
1498 if (! (*set)[i].offset)
1499 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1500 }
1501 SAVE_STREAM (stream, state);
1502 for (i = 0; i < count; i++)
1503 {
1504 SEEK_STREAM (stream, offset + (*set)[i].offset);
1505 (*set)[i].RuleCount
1506 = read_rule_list (otf, stream, offset + (*set)[i].offset,
1507 &(*set)[i].Rule);
1508 if (! (*set)[i].RuleCount)
1509 return errret;
1510 }
1511 RESTORE_STREAM (stream, state);
1512 return count;
1513 }
1514
1515 static unsigned
read_class_rule_list(OTF * otf,OTF_Stream * stream,long offset,OTF_ClassRule ** rule)1516 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1517 OTF_ClassRule **rule)
1518 {
1519 char *errfmt = "ClassRule%s";
1520 unsigned errret = 0;
1521 OTF_StreamState state;
1522 unsigned count;
1523 int i;
1524
1525 READ_UINT16 (stream, count);
1526 if (! count)
1527 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1528 OTF_MALLOC (*rule, count, "");
1529 for (i = 0; i < count; i++)
1530 {
1531 READ_OFFSET (stream, (*rule)[i].offset);
1532 if (! (*rule)[i].offset)
1533 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1534 }
1535 SAVE_STREAM (stream, state);
1536 for (i = 0; i < count; i++)
1537 {
1538 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1539 READ_USHORT (stream, (*rule)[i].GlyphCount);
1540 if (! (*rule)[i].GlyphCount)
1541 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1542 READ_USHORT (stream, (*rule)[i].LookupCount);
1543 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
1544 0, (*rule)[i].GlyphCount - 1) < 0)
1545 return errret;
1546 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1547 (*rule)[i].LookupCount) < 0)
1548 return errret;
1549 }
1550 RESTORE_STREAM (stream, state);
1551 return count;
1552 }
1553
1554 static unsigned
read_class_set_list(OTF * otf,OTF_Stream * stream,long offset,OTF_ClassSet ** set)1555 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1556 OTF_ClassSet **set)
1557 {
1558 char *errfmt = "ClassSet%s";
1559 unsigned errret = 0;
1560 OTF_StreamState state;
1561 unsigned count;
1562 int i;
1563
1564 READ_UINT16 (stream, count);
1565 if (! count)
1566 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1567 OTF_CALLOC (*set, count, "");
1568 for (i = 0; i < count; i++)
1569 /* Offset can be zero. */
1570 READ_OFFSET (stream, (*set)[i].offset);
1571 SAVE_STREAM (stream, state);
1572 for (i = 0; i < count; i++)
1573 if ((*set)[i].offset)
1574 {
1575 SEEK_STREAM (stream, offset + (*set)[i].offset);
1576 (*set)[i].ClassRuleCnt
1577 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
1578 &(*set)[i].ClassRule);
1579 if (! (*set)[i].ClassRuleCnt)
1580 return errret;
1581 }
1582 RESTORE_STREAM (stream, state);
1583 return count;
1584 }
1585
1586 static unsigned
read_chain_rule_list(OTF * otf,OTF_Stream * stream,long offset,OTF_ChainRule ** rule)1587 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1588 OTF_ChainRule **rule)
1589 {
1590 char *errfmt = "ChainRule%s";
1591 unsigned errret = 0;
1592 unsigned count;
1593 int i;
1594
1595 READ_UINT16 (stream, count);
1596 if (! count)
1597 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1598 OTF_MALLOC (*rule, count, "");
1599 for (i = 0; i < count; i++)
1600 READ_OFFSET (stream, (*rule)[i].offset);
1601 for (i = 0; i < count; i++)
1602 {
1603 int lookup_record_count;
1604
1605 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1606 (*rule)[i].BacktrackGlyphCount
1607 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
1608 (*rule)[i].InputGlyphCount
1609 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
1610 if (! (*rule)[i].InputGlyphCount)
1611 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1612 (*rule)[i].LookaheadGlyphCount
1613 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
1614 lookup_record_count
1615 = read_lookup_record_list (otf, stream,
1616 &(*rule)[i].LookupRecord, -1);
1617 if (lookup_record_count < 0)
1618 return errret;
1619 (*rule)[i].LookupCount = lookup_record_count;
1620 }
1621 return count;
1622 }
1623
1624
1625 static unsigned
read_chain_rule_set_list(OTF * otf,OTF_Stream * stream,long offset,OTF_ChainRuleSet ** set)1626 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1627 OTF_ChainRuleSet **set)
1628 {
1629 char *errfmt = "ChainRuleSet%s";
1630 unsigned errret = 0;
1631 OTF_StreamState state;
1632 unsigned count;
1633 int i;
1634
1635 READ_UINT16 (stream, count);
1636 if (! count)
1637 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1638 OTF_MALLOC (*set, count, "");
1639 for (i = 0; i < count; i++)
1640 {
1641 READ_OFFSET (stream, (*set)[i].offset);
1642 if (! (*set)[i].offset)
1643 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1644 }
1645 SAVE_STREAM (stream, state);
1646 for (i = 0; i < count; i++)
1647 {
1648 SEEK_STREAM (stream, offset + (*set)[i].offset);
1649 (*set)[i].ChainRuleCount
1650 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1651 &(*set)[i].ChainRule);
1652 if (! (*set)[i].ChainRuleCount)
1653 return errret;
1654 }
1655 RESTORE_STREAM (stream, state);
1656 return count;
1657 }
1658
1659 static unsigned
read_chain_class_rule_list(OTF * otf,OTF_Stream * stream,long offset,OTF_ChainClassRule ** rule)1660 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1661 OTF_ChainClassRule **rule)
1662 {
1663 char *errfmt = "ChainClassRule%s";
1664 unsigned errret = 0;
1665 unsigned count;
1666 int i;
1667
1668 READ_UINT16 (stream, count);
1669 if (! count)
1670 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1671 OTF_MALLOC (*rule, count, "");
1672 for (i = 0; i < count; i++)
1673 {
1674 READ_OFFSET (stream, (*rule)[i].offset);
1675 if (! (*rule)[i].offset)
1676 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1677 }
1678 for (i = 0; i < count; i++)
1679 {
1680 int lookup_record_count;
1681
1682 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1683 (*rule)[i].BacktrackGlyphCount
1684 = read_glyph_ids (otf, stream,
1685 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1686 (*rule)[i].InputGlyphCount
1687 = read_glyph_ids (otf, stream,
1688 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1689 if (! (*rule)[i].InputGlyphCount)
1690 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1691 (*rule)[i].LookaheadGlyphCount
1692 = read_glyph_ids (otf, stream,
1693 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1694 lookup_record_count
1695 = read_lookup_record_list (otf, stream,
1696 &(*rule)[i].LookupRecord, -1);
1697 if (lookup_record_count < 0)
1698 return errret;
1699 (*rule)[i].LookupCount = lookup_record_count;
1700 }
1701 return count;
1702 }
1703
1704 static unsigned
read_chain_class_set_list(OTF * otf,OTF_Stream * stream,long offset,OTF_ChainClassSet ** set)1705 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1706 OTF_ChainClassSet **set)
1707 {
1708 char *errfmt = "ChainClassSet%s";
1709 unsigned errret = 0;
1710 OTF_StreamState state;
1711 unsigned count;
1712 int i;
1713
1714 READ_UINT16 (stream, count);
1715 if (! count)
1716 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1717 OTF_MALLOC (*set, count, "");
1718 for (i = 0; i < count; i++)
1719 /* Offset may be zero. */
1720 READ_OFFSET (stream, (*set)[i].offset);
1721 SAVE_STREAM (stream, state);
1722 for (i = 0; i < count; i++)
1723 if ((*set)[i].offset)
1724 {
1725 SEEK_STREAM (stream, offset + (*set)[i].offset);
1726 (*set)[i].ChainClassRuleCnt
1727 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1728 &(*set)[i].ChainClassRule);
1729 if (! (*set)[i].ChainClassRuleCnt)
1730 return errret;
1731 }
1732 RESTORE_STREAM (stream, state);
1733 return count;
1734 }
1735
1736 static int
read_context1(OTF * otf,OTF_Stream * stream,long offset,OTF_Coverage * coverage,OTF_Context1 * context1)1737 read_context1 (OTF *otf, OTF_Stream *stream, long offset,
1738 OTF_Coverage *coverage,OTF_Context1 *context1)
1739 {
1740 if (read_coverage (otf, stream, offset, coverage) < 0)
1741 return -1;
1742 context1->RuleSetCount
1743 = read_rule_set_list (otf, stream, offset, &context1->RuleSet);
1744 if (! context1->RuleSetCount)
1745 return -1;
1746 return 0;
1747 }
1748
1749 static int
read_context2(OTF * otf,OTF_Stream * stream,long offset,OTF_Coverage * coverage,OTF_Context2 * context2)1750 read_context2 (OTF *otf, OTF_Stream *stream, long offset,
1751 OTF_Coverage *coverage,OTF_Context2 *context2)
1752 {
1753 if (read_coverage (otf, stream, offset, coverage) < 0
1754 || read_class_def (otf, stream, offset, &context2->ClassDef) < 0)
1755 return -1;
1756 context2->ClassSetCnt
1757 = read_class_set_list (otf, stream, offset, &context2->ClassSet);
1758 if (! context2->ClassSetCnt)
1759 return -1;
1760 return 0;
1761 }
1762
1763 static int
read_context3(OTF * otf,OTF_Stream * stream,long offset,OTF_Coverage * coverage,OTF_Context3 * context3)1764 read_context3 (OTF *otf, OTF_Stream *stream, long offset,
1765 OTF_Coverage *coverage,OTF_Context3 *context3)
1766 {
1767 char *errfmt = "Context1%s";
1768 int errret = -1;
1769
1770 READ_USHORT (stream, context3->GlyphCount);
1771 if (context3->GlyphCount < 0)
1772 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1773 READ_USHORT (stream, context3->LookupCount);
1774 if (read_coverage_list (otf, stream, offset, &context3->Coverage,
1775 context3->GlyphCount) < 0)
1776 return errret;
1777 if (read_lookup_record_list (otf, stream, &context3->LookupRecord,
1778 context3->LookupCount) < 0)
1779 return errret;
1780 return 0;
1781 }
1782
1783 static int
read_chain_context1(OTF * otf,OTF_Stream * stream,long offset,OTF_Coverage * coverage,OTF_ChainContext1 * chain_context1)1784 read_chain_context1 (OTF *otf, OTF_Stream *stream, long offset,
1785 OTF_Coverage *coverage, OTF_ChainContext1 *chain_context1)
1786 {
1787 if (read_coverage (otf, stream, offset, coverage) < 0)
1788 return -1;
1789 chain_context1->ChainRuleSetCount
1790 = read_chain_rule_set_list (otf, stream, offset,
1791 &chain_context1->ChainRuleSet);
1792 if (! chain_context1->ChainRuleSetCount)
1793 return -1;
1794 return 0;
1795 }
1796
1797 static int
read_chain_context2(OTF * otf,OTF_Stream * stream,long offset,OTF_Coverage * coverage,OTF_ChainContext2 * chain_context2)1798 read_chain_context2 (OTF *otf, OTF_Stream *stream, long offset,
1799 OTF_Coverage *coverage, OTF_ChainContext2 *chain_context2)
1800 {
1801 if (read_coverage (otf, stream, offset, coverage) < 0
1802 || read_class_def (otf, stream, offset,
1803 &chain_context2->BacktrackClassDef) < 0
1804 || read_class_def (otf, stream, offset,
1805 &chain_context2->InputClassDef) < 0
1806 || read_class_def (otf, stream, offset,
1807 &chain_context2->LookaheadClassDef) < 0)
1808 return -1;
1809 chain_context2->ChainClassSetCnt
1810 = read_chain_class_set_list (otf, stream, offset,
1811 &chain_context2->ChainClassSet);
1812 if (! chain_context2->ChainClassSetCnt)
1813 return -1;
1814 return 0;
1815 }
1816
1817 static int
read_chain_context3(OTF * otf,OTF_Stream * stream,long offset,OTF_Coverage * coverage,OTF_ChainContext3 * chain_context3)1818 read_chain_context3 (OTF *otf, OTF_Stream *stream, long offset,
1819 OTF_Coverage *coverage, OTF_ChainContext3 *chain_context3)
1820 {
1821 int count;
1822
1823 count = read_coverage_list (otf, stream, offset,
1824 &chain_context3->Backtrack, -1);
1825 if (count < 0)
1826 return -1;
1827 chain_context3->BacktrackGlyphCount = (unsigned) count;
1828 count = read_coverage_list (otf, stream, offset,
1829 &chain_context3->Input, -1);
1830 if (count <= 0)
1831 return -1;
1832 chain_context3->InputGlyphCount = (unsigned) count;
1833 *coverage = chain_context3->Input[0];
1834 count = read_coverage_list (otf, stream, offset,
1835 &chain_context3->LookAhead, -1);
1836 chain_context3->LookaheadGlyphCount = (unsigned) count;
1837 count = read_lookup_record_list (otf, stream,
1838 &chain_context3->LookupRecord, -1);
1839 if (count < 0)
1840 return -1;
1841 chain_context3->LookupCount = count;
1842 return 0;
1843 }
1844
1845 static void *
read_gsub_gpos_table(OTF * otf,OTF_TableInfo * table,int gsubp,enum OTF_ReaderFlag flag)1846 read_gsub_gpos_table (OTF *otf, OTF_TableInfo *table, int gsubp,
1847 enum OTF_ReaderFlag flag)
1848 {
1849 OTF_Stream *stream = table->stream;
1850 char *errfmt = gsubp ? "GSUB%s" : "GPOS%s";
1851 void *errret = NULL;
1852 OTF_GSUB_GPOS *gsub_gpos = *table->address;
1853
1854 if (gsub_gpos)
1855 SEEK_STREAM (stream, 10);
1856 else
1857 {
1858 SEEK_STREAM (stream, 0);
1859 OTF_CALLOC (gsub_gpos, 1, "");
1860 READ_FIXED (stream, gsub_gpos->Version);
1861 READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
1862 READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
1863 READ_OFFSET (stream, gsub_gpos->LookupList.offset);
1864 *table->address = gsub_gpos;
1865 }
1866
1867 if (! gsub_gpos->ScriptList.Script
1868 && read_script_list (otf, stream, gsub_gpos->ScriptList.offset,
1869 &gsub_gpos->ScriptList) < 0)
1870 return NULL;
1871 if (flag != OTF_READ_SCRIPTS)
1872 {
1873 if (! gsub_gpos->FeatureList.Feature
1874 && read_feature_list (otf, stream, gsub_gpos->FeatureList.offset,
1875 &gsub_gpos->FeatureList) < 0)
1876 return NULL;
1877 if (flag != OTF_READ_FEATURES)
1878 {
1879 if (! gsub_gpos->LookupList.Lookup
1880 && read_lookup_list (otf, stream, gsub_gpos->LookupList.offset,
1881 &gsub_gpos->LookupList, gsubp) < 0)
1882 return NULL;
1883 }
1884 }
1885
1886 return gsub_gpos;
1887 }
1888
1889
1890 /* (1-9) "GSUB" table */
1891
1892 static unsigned
read_sequence(OTF * otf,OTF_Stream * stream,long offset,OTF_Sequence ** seq)1893 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1894 {
1895 char *errfmt = "Sequence%s";
1896 unsigned errret = 0;
1897 unsigned count;
1898 int i;
1899
1900 READ_UINT16 (stream, count);
1901 if (! count)
1902 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1903 OTF_MALLOC (*seq, count, "");
1904 for (i = 0; i < count; i++)
1905 READ_OFFSET (stream, (*seq)[i].offset);
1906 for (i = 0; i < count; i++)
1907 {
1908 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1909 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1910 &(*seq)[i].Substitute, 0, -1);
1911 if (! (*seq)[i].GlyphCount)
1912 return 0;
1913 }
1914 return count;
1915 }
1916
1917 static int
read_ligature(OTF * otf,OTF_Stream * stream,long offset,OTF_Ligature ** ligature)1918 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1919 OTF_Ligature **ligature)
1920 {
1921 char *errfmt = "Ligature%s";
1922 int errret = -1;
1923 int count;
1924 int i;
1925
1926 READ_UINT16 (stream, count);
1927 if (! count)
1928 return 0;
1929 OTF_MALLOC (*ligature, count, "");
1930 for (i = 0; i < count; i++)
1931 READ_OFFSET (stream, (*ligature)[i].offset);
1932 for (i = 0; i < count; i++)
1933 {
1934 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1935 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1936 (*ligature)[i].CompCount
1937 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1938 if (! (*ligature)[i].CompCount)
1939 return -1;
1940 }
1941 return count;
1942 }
1943
1944 static unsigned
read_ligature_set_list(OTF * otf,OTF_Stream * stream,long offset,OTF_LigatureSet ** ligset)1945 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1946 OTF_LigatureSet **ligset)
1947 {
1948 char *errfmt = "LigatureSet%s";
1949 int errret = 0;
1950 int count;
1951 int i;
1952
1953 READ_UINT16 (stream, count);
1954 if (! count)
1955 return errret;
1956 OTF_MALLOC (*ligset, count, "");
1957 for (i = 0; i < count; i++)
1958 READ_OFFSET (stream, (*ligset)[i].offset);
1959 for (i = 0; i < count; i++)
1960 {
1961 int lig_count;
1962
1963 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1964 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1965 &(*ligset)[i].Ligature);
1966 if (lig_count < 0)
1967 return errret;
1968 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1969 }
1970 return count;
1971 }
1972
1973 static unsigned
read_alternate_set_list(OTF * otf,OTF_Stream * stream,long offset,OTF_AlternateSet ** altset)1974 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1975 OTF_AlternateSet **altset)
1976 {
1977 char *errfmt = "AlternateSet%s";
1978 int errret = 0;
1979 unsigned count;
1980 int i;
1981
1982 READ_UINT16 (stream, count);
1983 if (! count)
1984 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1985 OTF_MALLOC (*altset, count, "");
1986 for (i = 0; i < count; i++)
1987 READ_OFFSET (stream, (*altset)[i].offset);
1988 for (i = 0; i < count; i++)
1989 {
1990 int alt_count;
1991
1992 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1993 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1994 if (alt_count < 0)
1995 return errret;
1996 (*altset)[i].GlyphCount = (unsigned) alt_count;
1997 }
1998 return count;
1999 }
2000
2001 static int
read_reverse_chain1(OTF * otf,OTF_Stream * stream,long offset,OTF_Coverage * coverage,OTF_GSUB_ReverseChain1 * reverse_chain)2002 read_reverse_chain1 (OTF *otf, OTF_Stream *stream, long offset,
2003 OTF_Coverage *coverage,
2004 OTF_GSUB_ReverseChain1 *reverse_chain)
2005 {
2006 int count;
2007
2008 if (read_coverage (otf, stream, offset, coverage) < 0)
2009 return -1;
2010 count = read_coverage_list (otf, stream, offset,
2011 &reverse_chain->Backtrack, -1);
2012 if (count < 0)
2013 return -1;
2014 reverse_chain->BacktrackGlyphCount = (unsigned) count;
2015 count = read_coverage_list (otf, stream, offset,
2016 &reverse_chain->LookAhead, -1);
2017 if (count <= 0)
2018 return -1;
2019 reverse_chain->LookaheadGlyphCount = (unsigned) count;
2020 count = read_glyph_ids (otf, stream, &reverse_chain->Substitute, 0, -1);
2021 if (count <= 0)
2022 return -1;
2023 reverse_chain->GlyphCount = count;
2024 return 0;
2025 }
2026
2027 static int
read_lookup_subtable_gsub(OTF * otf,OTF_Stream * stream,long offset,unsigned type,OTF_LookupSubTableGSUB * subtable)2028 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
2029 unsigned type, OTF_LookupSubTableGSUB *subtable)
2030 {
2031 char errfmt[256];
2032 int errret = -1;
2033
2034 SEEK_STREAM (stream, offset);
2035 READ_USHORT (stream, subtable->Format);
2036 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
2037 switch (type)
2038 {
2039 case 1:
2040 if (subtable->Format == 1)
2041 {
2042 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2043 return -1;
2044 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
2045 }
2046 else if (subtable->Format == 2)
2047 {
2048 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2049 return -1;
2050 subtable->u.single2.GlyphCount
2051 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
2052 0, -1);
2053 if (! subtable->u.single2.GlyphCount)
2054 return -1;
2055 }
2056 else
2057 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2058 break;
2059
2060 case 2:
2061 if (subtable->Format == 1)
2062 {
2063 read_coverage (otf, stream, offset, &subtable->Coverage);
2064 subtable->u.multiple1.SequenceCount
2065 = read_sequence (otf, stream, offset,
2066 &subtable->u.multiple1.Sequence);
2067 }
2068 else
2069 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2070 break;
2071
2072 case 3:
2073 if (subtable->Format == 1)
2074 {
2075 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2076 return -1;
2077 subtable->u.alternate1.AlternateSetCount
2078 = read_alternate_set_list (otf, stream, offset,
2079 &subtable->u.alternate1.AlternateSet);
2080 if (! subtable->u.alternate1.AlternateSetCount)
2081 return -1;
2082 }
2083 else
2084 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2085 break;
2086
2087 case 4:
2088 if (subtable->Format == 1)
2089 {
2090 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2091 return -1;
2092 subtable->u.ligature1.LigSetCount
2093 = read_ligature_set_list (otf, stream, offset,
2094 &subtable->u.ligature1.LigatureSet);
2095 if (! subtable->u.ligature1.LigSetCount)
2096 return -1;
2097 }
2098 else
2099 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2100 break;
2101
2102 case 5:
2103 if (subtable->Format == 1)
2104 {
2105 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2106 &subtable->u.context1) < 0)
2107 return errret;
2108 }
2109 else if (subtable->Format == 2)
2110 {
2111 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2112 &subtable->u.context2) < 0)
2113 return errret;
2114 }
2115 else if (subtable->Format == 3)
2116 {
2117 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2118 &subtable->u.context3) < 0)
2119 return errret;
2120 }
2121 else
2122 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2123 break;
2124
2125 case 6:
2126 if (subtable->Format == 1)
2127 {
2128 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2129 &subtable->u.chain_context1) < 0)
2130 return errret;
2131 }
2132 else if (subtable->Format == 2)
2133 {
2134 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2135 &subtable->u.chain_context2) < 0)
2136 return errret;
2137 }
2138 else if (subtable->Format == 3)
2139 {
2140 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2141 &subtable->u.chain_context3) < 0)
2142 return errret;
2143 }
2144 else
2145 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2146 break;
2147
2148 case 7:
2149 if (subtable->Format == 1)
2150 {
2151 unsigned ex_type;
2152 long ex_offset;
2153 OTF_LookupSubTableGSUB *ex_subtable;
2154
2155 READ_USHORT (stream, ex_type);
2156 READ_ULONG (stream, ex_offset);
2157 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2158 if (read_lookup_subtable_gsub (otf, stream, offset + ex_offset,
2159 ex_type, ex_subtable) < 0)
2160 return errret;
2161 subtable->u.extension1.ExtensionLookupType = ex_type;
2162 subtable->u.extension1.ExtensionOffset = ex_offset;
2163 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2164 }
2165 else
2166 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2167 break;
2168
2169 case 8:
2170 if (subtable->Format == 1)
2171 {
2172 if (read_reverse_chain1 (otf, stream, offset, &subtable->Coverage,
2173 &subtable->u.reverse_chain1) < 0)
2174 return errret;
2175 }
2176 else
2177 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2178 break;
2179
2180 default:
2181 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2182 }
2183 return 0;
2184 }
2185
2186 static void *
read_gsub_table(OTF * otf,OTF_TableInfo * table,enum OTF_ReaderFlag flag)2187 read_gsub_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
2188 {
2189 return read_gsub_gpos_table (otf, table, 1, flag);
2190 }
2191
2192
2193 /* (1-10) "GPOS" table */
2194
2195 static int
read_value_record(OTF * otf,OTF_Stream * stream,long offset,enum OTF_ValueFormat bit,OTF_ValueRecord * value_record)2196 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
2197 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
2198 {
2199 int errret = -1;
2200 OTF_StreamState state;
2201 int size, i;
2202
2203 memset (value_record, 0, sizeof (OTF_ValueRecord));
2204 if (! bit)
2205 return 0;
2206 for (i = 0, size = 0; i < 8; i++)
2207 if (bit & (1 << i))
2208 size += 2;
2209
2210 if (bit & OTF_XPlacement)
2211 READ_INT16 (stream, value_record->XPlacement);
2212 if (bit & OTF_YPlacement)
2213 READ_INT16 (stream, value_record->YPlacement);
2214 if (bit & OTF_XAdvance)
2215 READ_INT16 (stream, value_record->XAdvance);
2216 if (bit & OTF_YAdvance)
2217 READ_INT16 (stream, value_record->YAdvance);
2218 if (bit & OTF_XPlaDevice)
2219 READ_OFFSET (stream, value_record->XPlaDevice.offset);
2220 if (bit & OTF_YPlaDevice)
2221 READ_OFFSET (stream, value_record->YPlaDevice.offset);
2222 if (bit & OTF_XAdvDevice)
2223 READ_OFFSET (stream, value_record->XAdvDevice.offset);
2224 if (bit & OTF_YAdvDevice)
2225 READ_OFFSET (stream, value_record->YAdvDevice.offset);
2226 SAVE_STREAM (stream, state);
2227 if (value_record->XPlaDevice.offset)
2228 {
2229 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
2230 return -1;
2231 }
2232 if (value_record->YPlaDevice.offset)
2233 {
2234 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
2235 return -1;
2236 }
2237 if (value_record->XAdvDevice.offset)
2238 {
2239 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
2240 return -1;
2241 }
2242 if (value_record->YAdvDevice.offset)
2243 {
2244 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
2245 return -1;
2246 }
2247 RESTORE_STREAM (stream, state);
2248 return 0;
2249 }
2250
2251
2252 static int
read_anchor(OTF * otf,OTF_Stream * stream,long offset,OTF_Anchor * anchor)2253 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
2254 {
2255 char *errfmt = "Anchor%s";
2256 int errret = -1;
2257
2258 SEEK_STREAM (stream, offset + anchor->offset);
2259 READ_UINT16 (stream, anchor->AnchorFormat);
2260 READ_INT16 (stream, anchor->XCoordinate);
2261 READ_INT16 (stream, anchor->YCoordinate);
2262 if (anchor->AnchorFormat == 1)
2263 ;
2264 else if (anchor->AnchorFormat == 2)
2265 {
2266 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
2267 }
2268 else if (anchor->AnchorFormat == 3)
2269 {
2270 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
2271 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
2272 if (anchor->f.f2.XDeviceTable.offset)
2273 {
2274 if (read_device_table (otf, stream, offset + anchor->offset,
2275 &anchor->f.f2.XDeviceTable) < 0)
2276 return -1;
2277 }
2278 if (anchor->f.f2.YDeviceTable.offset)
2279 {
2280 if (read_device_table (otf, stream, offset + anchor->offset,
2281 &anchor->f.f2.YDeviceTable) < 0)
2282 return -1;
2283 }
2284 }
2285 else
2286 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
2287
2288 return 0;
2289 }
2290
2291 static int
read_mark_array(OTF * otf,OTF_Stream * stream,long offset,OTF_MarkArray * array)2292 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
2293 OTF_MarkArray *array)
2294 {
2295 char *errfmt = "MarkArray%s";
2296 int errret = -1;
2297 OTF_StreamState state;
2298 int i;
2299
2300 READ_OFFSET (stream, array->offset);
2301 SAVE_STREAM (stream, state);
2302 SEEK_STREAM (stream, offset + array->offset);
2303 READ_UINT16 (stream, array->MarkCount);
2304 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
2305 for (i = 0; i < array->MarkCount; i++)
2306 {
2307 READ_UINT16 (stream, array->MarkRecord[i].Class);
2308 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
2309 }
2310 for (i = 0; i < array->MarkCount; i++)
2311 if (read_anchor (otf, stream, offset + array->offset,
2312 &array->MarkRecord[i].MarkAnchor) < 0)
2313 return -1;;
2314 RESTORE_STREAM (stream, state);
2315 return 0;
2316 }
2317
2318 static int
read_anchor_array(OTF * otf,OTF_Stream * stream,long offset,unsigned ClassCount,OTF_AnchorArray * array)2319 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
2320 unsigned ClassCount, OTF_AnchorArray *array)
2321 {
2322 char *errfmt = "AnchorArray%s";
2323 int errret = -1;
2324 OTF_StreamState state;
2325 int i, j;
2326
2327 READ_OFFSET (stream, array->offset);
2328 SAVE_STREAM (stream, state);
2329 SEEK_STREAM (stream, offset + array->offset);
2330 READ_UINT16 (stream, array->Count);
2331 OTF_MALLOC (array->AnchorRecord, array->Count, "");
2332 for (i = 0; i < array->Count; i++)
2333 {
2334 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
2335 " (AnchorRecord)");
2336 for (j = 0; j < ClassCount; j++)
2337 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
2338 }
2339 for (i = 0; i < array->Count; i++)
2340 for (j = 0; j < ClassCount; j++)
2341 if (array->AnchorRecord[i].Anchor[j].offset > 0
2342 && read_anchor (otf, stream, offset + array->offset,
2343 &array->AnchorRecord[i].Anchor[j]) < 0)
2344 return -1;
2345 RESTORE_STREAM (stream, state);
2346 return 0;
2347 }
2348
2349 static OTF_PairSet *
read_pair_set_list(OTF * otf,OTF_Stream * stream,long offset,unsigned num,enum OTF_ValueFormat bit1,enum OTF_ValueFormat bit2)2350 read_pair_set_list (OTF *otf, OTF_Stream *stream, long offset, unsigned num,
2351 enum OTF_ValueFormat bit1, enum OTF_ValueFormat bit2)
2352 {
2353 char *errfmt = "PairSet%s";
2354 void *errret = NULL;
2355 OTF_StreamState state;
2356 OTF_PairSet *set;
2357 int i, j;
2358
2359 OTF_MALLOC (set, num, "");
2360 for (i = 0; i < num; i++)
2361 READ_OFFSET (stream, set[i].offset);
2362 SAVE_STREAM (stream, state);
2363 for (i = 0; i < num; i++)
2364 {
2365 SEEK_STREAM (stream, offset + set[i].offset);
2366 READ_UINT16 (stream, set[i].PairValueCount);
2367 OTF_MALLOC (set[i].PairValueRecord, set[i].PairValueCount, "");
2368 for (j = 0; j < set[i].PairValueCount; j++)
2369 {
2370 OTF_PairValueRecord *rec = set[i].PairValueRecord + j;
2371
2372 READ_UINT16 (stream, rec->SecondGlyph);
2373 read_value_record (otf, stream, offset, bit1, &rec->Value1);
2374 read_value_record (otf, stream, offset, bit2, &rec->Value2);
2375 }
2376 }
2377 RESTORE_STREAM (stream, state);
2378 return set;
2379 }
2380
2381 static OTF_Class1Record *
read_class1_record_list(OTF * otf,OTF_Stream * stream,long offset,unsigned num1,enum OTF_ValueFormat bit1,unsigned num2,enum OTF_ValueFormat bit2)2382 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
2383 unsigned num1, enum OTF_ValueFormat bit1,
2384 unsigned num2, enum OTF_ValueFormat bit2)
2385 {
2386 char *errfmt = "Class1Record%s";
2387 void *errret = NULL;
2388 OTF_Class1Record *rec;
2389 int i, j;
2390
2391 OTF_MALLOC (rec, num1, "");
2392 for (i = 0; i < num1; i++)
2393 {
2394 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
2395 for (j = 0; j < num2; j++)
2396 {
2397 if (read_value_record (otf, stream, offset,
2398 bit1, &rec[i].Class2Record[j].Value1) < 0
2399 || read_value_record (otf, stream, offset,
2400 bit2, &rec[i].Class2Record[j].Value2) < 0)
2401 return NULL;
2402 }
2403 }
2404 return rec;
2405 }
2406
2407 static unsigned
read_entry_exit_list(OTF * otf,OTF_Stream * stream,long offset,OTF_EntryExitRecord ** rec)2408 read_entry_exit_list (OTF *otf, OTF_Stream *stream, long offset,
2409 OTF_EntryExitRecord **rec)
2410 {
2411 char *errfmt = "EntryExitSet%s";
2412 int errret = 0;
2413 unsigned count;
2414 int i;
2415 OTF_StreamState state;
2416
2417 READ_UINT16 (stream, count);
2418 if (! count)
2419 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
2420 OTF_MALLOC (*rec, count, "");
2421 for (i = 0; i < count; i++)
2422 {
2423 READ_OFFSET (stream, (*rec)[i].EntryAnchor.offset);
2424 READ_OFFSET (stream, (*rec)[i].ExitAnchor.offset);
2425 }
2426 SAVE_STREAM (stream, state);
2427 for (i = 0; i < count; i++)
2428 {
2429 if (read_anchor (otf, stream, offset, &(*rec)[i].EntryAnchor) < 0)
2430 return -1;
2431 if (read_anchor (otf, stream, offset, &(*rec)[i].ExitAnchor) < 0)
2432 return -1;
2433 }
2434 RESTORE_STREAM (stream, state);
2435 return count;
2436 }
2437
2438 static int
read_ligature_attach(OTF * otf,OTF_Stream * stream,long offset,unsigned ClassCount,OTF_LigatureAttach * attach)2439 read_ligature_attach (OTF *otf, OTF_Stream *stream, long offset,
2440 unsigned ClassCount, OTF_LigatureAttach *attach)
2441 {
2442 char *errfmt = "LigatureAttach%s";
2443 int errret = -1;
2444 int i, j;
2445
2446 SEEK_STREAM (stream, offset + attach->offset);
2447 READ_UINT16 (stream, attach->ComponentCount);
2448 OTF_MALLOC (attach->ComponentRecord, attach->ComponentCount, "");
2449 for (i = 0; i < attach->ComponentCount; i++)
2450 {
2451 OTF_MALLOC (attach->ComponentRecord[i].LigatureAnchor, ClassCount,
2452 " (ComponentRecord)");
2453 for (j = 0; j < ClassCount; j++)
2454 READ_OFFSET (stream,
2455 attach->ComponentRecord[i].LigatureAnchor[j].offset);
2456 }
2457 for (i = 0; i < attach->ComponentCount; i++)
2458 for (j = 0; j < ClassCount; j++)
2459 {
2460 if (attach->ComponentRecord[i].LigatureAnchor[j].offset)
2461 {
2462 if (read_anchor (otf, stream, offset + attach->offset,
2463 &attach->ComponentRecord[i].LigatureAnchor[j]) < 0)
2464 return -1;
2465 }
2466 else
2467 attach->ComponentRecord[i].LigatureAnchor[j].AnchorFormat = 0;
2468 }
2469 return 0;
2470 }
2471
2472 static int
read_ligature_array(OTF * otf,OTF_Stream * stream,long offset,unsigned class_count,OTF_LigatureArray * array)2473 read_ligature_array (OTF *otf, OTF_Stream *stream, long offset,
2474 unsigned class_count, OTF_LigatureArray *array)
2475 {
2476 char *errfmt = "LigatureArray%s";
2477 int errret = -1;
2478 OTF_StreamState state;
2479 int i;
2480
2481 READ_OFFSET (stream, array->offset);
2482 SAVE_STREAM (stream, state);
2483 SEEK_STREAM (stream, offset + array->offset);
2484 READ_UINT16 (stream, array->LigatureCount);
2485 OTF_MALLOC (array->LigatureAttach, array->LigatureCount, "");
2486 for (i = 0; i < array->LigatureCount; i++)
2487 READ_OFFSET (stream, array->LigatureAttach[i].offset);
2488 for (i = 0; i < array->LigatureCount; i++)
2489 if (array->LigatureAttach[i].offset > 0
2490 && read_ligature_attach (otf, stream, offset + array->offset,
2491 class_count, array->LigatureAttach + i) < 0)
2492 return -1;
2493 RESTORE_STREAM (stream, state);
2494 return 0;
2495 }
2496
2497 static int
read_lookup_subtable_gpos(OTF * otf,OTF_Stream * stream,long offset,unsigned type,OTF_LookupSubTableGPOS * subtable)2498 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
2499 long offset, unsigned type,
2500 OTF_LookupSubTableGPOS *subtable)
2501 {
2502 char errfmt[256];
2503 int errret = -1;
2504
2505 SEEK_STREAM (stream, offset);
2506 READ_UINT16 (stream, subtable->Format);
2507 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
2508 switch (type)
2509 {
2510 case 1:
2511 if (subtable->Format == 1)
2512 {
2513 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2514 return -1;
2515 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
2516 read_value_record (otf, stream, offset,
2517 subtable->u.single1.ValueFormat,
2518 &subtable->u.single1.Value);
2519 }
2520 else if (subtable->Format == 2)
2521 {
2522 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
2523 int i;
2524
2525 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2526 return -1;
2527 READ_UINT16 (stream, single2->ValueFormat);
2528 READ_UINT16 (stream, single2->ValueCount);
2529 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
2530 for (i = 0; i < single2->ValueCount; i++)
2531 read_value_record (otf, stream, offset, single2->ValueFormat,
2532 single2->Value + i);
2533 }
2534 else
2535 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2536 break;
2537
2538 case 2:
2539 if (subtable->Format == 1)
2540 {
2541 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2542 return -1;
2543 READ_UINT16 (stream, subtable->u.pair1.ValueFormat1);
2544 READ_UINT16 (stream, subtable->u.pair1.ValueFormat2);
2545 READ_UINT16 (stream, subtable->u.pair1.PairSetCount);
2546 subtable->u.pair1.PairSet
2547 = read_pair_set_list (otf, stream, offset,
2548 subtable->u.pair1.PairSetCount,
2549 subtable->u.pair1.ValueFormat1,
2550 subtable->u.pair1.ValueFormat2);
2551 if (! subtable->u.pair1.PairSet)
2552 return -1;
2553 }
2554 else if (subtable->Format == 2)
2555 {
2556 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2557 return -1;
2558 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
2559 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
2560 if (read_class_def (otf, stream, offset,
2561 &subtable->u.pair2.ClassDef1) < 0
2562 || read_class_def (otf, stream, offset,
2563 &subtable->u.pair2.ClassDef2) < 0)
2564 return -1;
2565 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
2566 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
2567 subtable->u.pair2.Class1Record
2568 = read_class1_record_list (otf, stream, offset,
2569 subtable->u.pair2.Class1Count,
2570 subtable->u.pair2.ValueFormat1,
2571 subtable->u.pair2.Class2Count,
2572 subtable->u.pair2.ValueFormat2);
2573 if (! subtable->u.pair2.Class1Record)
2574 return -1;
2575 }
2576 else
2577 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2578 break;
2579
2580 case 3:
2581 if (subtable->Format == 1)
2582 {
2583 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2584 return -1;
2585 subtable->u.cursive1.EntryExitCount
2586 = read_entry_exit_list (otf, stream, offset,
2587 &subtable->u.cursive1.EntryExitRecord);
2588 if (! subtable->u.cursive1.EntryExitCount)
2589 return -1;
2590 }
2591 else
2592 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2593 break;
2594
2595 case 4:
2596 if (subtable->Format == 1)
2597 {
2598 read_coverage (otf, stream, offset, &subtable->Coverage);
2599 read_coverage (otf, stream, offset,
2600 &subtable->u.mark_base1.BaseCoverage);
2601 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2602 read_mark_array (otf, stream, offset,
2603 &subtable->u.mark_base1.MarkArray);
2604 read_anchor_array (otf, stream, offset,
2605 subtable->u.mark_base1.ClassCount,
2606 &subtable->u.mark_base1.BaseArray);
2607 }
2608 else
2609 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2610 break;
2611
2612 case 5:
2613 if (subtable->Format == 1)
2614 {
2615 read_coverage (otf, stream, offset, &subtable->Coverage);
2616 read_coverage (otf, stream, offset,
2617 &subtable->u.mark_lig1.LigatureCoverage);
2618 READ_UINT16 (stream, subtable->u.mark_lig1.ClassCount);
2619 read_mark_array (otf, stream, offset,
2620 &subtable->u.mark_lig1.MarkArray);
2621 read_ligature_array (otf, stream, offset,
2622 subtable->u.mark_lig1.ClassCount,
2623 &subtable->u.mark_lig1.LigatureArray);
2624 }
2625 break;
2626
2627 case 6:
2628 if (subtable->Format == 1)
2629 {
2630 read_coverage (otf, stream, offset, &subtable->Coverage);
2631 read_coverage (otf, stream, offset,
2632 &subtable->u.mark_mark1.Mark2Coverage);
2633 READ_UINT16 (stream, subtable->u.mark_mark1.ClassCount);
2634 read_mark_array (otf, stream, offset,
2635 &subtable->u.mark_mark1.Mark1Array);
2636 read_anchor_array (otf, stream, offset,
2637 subtable->u.mark_mark1.ClassCount,
2638 &subtable->u.mark_mark1.Mark2Array);
2639 }
2640 else
2641 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2642 break;
2643
2644 case 7:
2645 if (subtable->Format == 1)
2646 {
2647 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2648 &subtable->u.context1) < 0)
2649 return errret;
2650 }
2651 else if (subtable->Format == 2)
2652 {
2653 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2654 &subtable->u.context2) < 0)
2655 return errret;
2656 }
2657 else if (subtable->Format == 3)
2658 {
2659 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2660 &subtable->u.context3) < 0)
2661 return errret;
2662 }
2663 else
2664 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2665 break;
2666
2667 case 8:
2668 if (subtable->Format == 1)
2669 {
2670 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2671 &subtable->u.chain_context1) < 0)
2672 return errret;
2673 }
2674 else if (subtable->Format == 2)
2675 {
2676 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2677 &subtable->u.chain_context2) < 0)
2678 return errret;
2679 }
2680 else if (subtable->Format == 3)
2681 {
2682 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2683 &subtable->u.chain_context3) < 0)
2684 return errret;
2685 }
2686 else
2687 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2688 break;
2689
2690 case 9:
2691 if (subtable->Format == 1)
2692 {
2693 unsigned ex_type;
2694 long ex_offset;
2695 OTF_LookupSubTableGPOS *ex_subtable;
2696
2697 READ_USHORT (stream, ex_type);
2698 READ_ULONG (stream, ex_offset);
2699 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2700 if (read_lookup_subtable_gpos (otf, stream, offset + ex_offset,
2701 ex_type, ex_subtable) < 0)
2702 return errret;
2703 subtable->u.extension1.ExtensionLookupType = ex_type;
2704 subtable->u.extension1.ExtensionOffset = ex_offset;
2705 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2706 }
2707 else
2708 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2709 break;
2710
2711 default:
2712 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2713 }
2714 return 0;
2715 }
2716
2717 static void *
read_gpos_table(OTF * otf,OTF_TableInfo * table,enum OTF_ReaderFlag flag)2718 read_gpos_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
2719 {
2720 return read_gsub_gpos_table (otf, table, 0, flag);
2721 }
2722
2723
2724 #if 0
2725 /* BASE */
2726
2727 static OTF_BASE *
2728 read_base_table (OTF_Stream *stream, long offset)
2729 {
2730 OTF_BASE *base;
2731
2732 OTF_MALLOC (base, 1);
2733
2734 return base;
2735 }
2736
2737
2738 /* JSTF */
2739
2740 static OTF_JSTF *
2741 read_jstf_table (OTF_Stream *stream, long offset)
2742 {
2743 OTF_JSTF *jstf;
2744
2745 OTF_MALLOC (jstf, 1);
2746
2747 return jstf;
2748 }
2749 #endif /* 0 */
2750
2751 /*** (1-11) Structure for OTF */
2752
2753 static int
read_offset_table(OTF * otf,OTF_Stream * stream,OTF_OffsetTable * table)2754 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2755 {
2756 int errret = -1;
2757
2758 READ_FIXED (stream, table->sfnt_version);
2759 READ_USHORT (stream, table->numTables);
2760 READ_USHORT (stream, table->searchRange);
2761 READ_USHORT (stream, table->enterSelector);
2762 READ_USHORT (stream, table->rangeShift);
2763 return 0;
2764 }
2765
2766 static OTF_Tag
read_table_directory(OTF_Stream * stream,OTF_TableDirectory * table)2767 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2768 {
2769 int errret = 0;
2770 OTF_Tag tag;
2771
2772 READ_TAG (stream, tag);
2773 table->tag = tag;
2774 table->name[0] = tag >> 24;
2775 table->name[1] = (tag >> 16) & 0xFF;
2776 table->name[2] = (tag >> 8) & 0xFF;
2777 table->name[3] = tag & 0xFF;
2778 table->name[4] = '\0';
2779 READ_ULONG (stream, table->checkSum);
2780 READ_ULONG (stream, table->offset);
2781 READ_ULONG (stream, table->length);
2782 return tag;
2783 }
2784
2785 static int
read_header_part(OTF * otf,FILE * fp,FT_Face face)2786 read_header_part (OTF *otf, FILE *fp, FT_Face face)
2787 {
2788 char *errfmt = "otf header%s";
2789 int errret = -1;
2790 int i;
2791 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2792
2793 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2794 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2795 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2796 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2797 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2798 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2799 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2800 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2801 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2802 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2803 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2804 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2805
2806 if (fp)
2807 {
2808 OTF_Tag head_tag = OTF_tag ("head");
2809 OTF_Tag name_tag = OTF_tag ("name");
2810 OTF_Tag cmap_tag = OTF_tag ("cmap");
2811 OTF_Tag gdef_tag = OTF_tag ("GDEF");
2812 OTF_Tag gsub_tag = OTF_tag ("GSUB");
2813 OTF_Tag gpos_tag = OTF_tag ("GPOS");
2814 OTF_Stream *stream = make_stream ("Offset Table");
2815 unsigned char ttctag[4];
2816
2817 if (! stream)
2818 return -1;
2819 internal_data->header_stream = stream;
2820
2821 /* Size of Offset Table is 12 bytes. Size of TTC header
2822 (including only the an offset of the first font) is 16. */
2823 if (setup_stream (stream, fp, 0, 16) < 0)
2824 return -1;
2825 READ_BYTES (stream, ttctag, 4);
2826 if (memcmp (ttctag, "ttcf", 4) == 0)
2827 {
2828 /* This is a TrueType Collection file. We extract the first font. */
2829 unsigned version, numfonts, offset;
2830 READ_ULONG (stream, version);
2831 READ_ULONG (stream, numfonts);
2832 READ_ULONG (stream, offset); /* Offset of the first font. */
2833 if (setup_stream (stream, fp, offset, 12) < 0)
2834 return -1;
2835 }
2836 else
2837 SEEK_STREAM (stream, 0L);
2838 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2839 return -1;
2840 /* Size of each Table Directory is 16 bytes. */
2841 if (setup_stream (stream, fp, stream->pos,
2842 16 * otf->offset_table.numTables) < 0)
2843 return -1;
2844
2845 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables,
2846 " (OffsetTable)");
2847 for (i = 0; i < otf->offset_table.numTables; i++)
2848 {
2849 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2850 OTF_TableInfo *table_info = NULL;
2851
2852 if (! tag)
2853 return -1;
2854 if (tag == head_tag)
2855 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2856 else if (tag == name_tag)
2857 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2858 else if (tag == cmap_tag)
2859 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2860 else if (tag == gdef_tag)
2861 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2862 else if (tag == gsub_tag)
2863 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2864 else if (tag == gpos_tag)
2865 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2866
2867 if (table_info)
2868 {
2869 table_info->stream = make_stream (otf->table_dirs[i].name);
2870 if (setup_stream (table_info->stream, fp,
2871 otf->table_dirs[i].offset,
2872 otf->table_dirs[i].length) < 0)
2873 return -1;
2874 }
2875 }
2876
2877 internal_data->header_stream = NULL;
2878 free_stream (stream);
2879 }
2880 else
2881 {
2882 OTF_Stream *stream;
2883
2884 internal_data->header_stream = NULL;
2885 if ((stream = make_stream_from_ft_face (face, "head")))
2886 internal_data->table_info[OTF_TABLE_TYPE_HEAD].stream = stream;
2887 if ((stream = make_stream_from_ft_face (face, "name")))
2888 internal_data->table_info[OTF_TABLE_TYPE_NAME].stream = stream;
2889 if ((stream = make_stream_from_ft_face (face, "cmap")))
2890 internal_data->table_info[OTF_TABLE_TYPE_CMAP].stream = stream;
2891 if ((stream = make_stream_from_ft_face (face, "GDEF")))
2892 internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream = stream;
2893 if ((stream = make_stream_from_ft_face (face, "GSUB")))
2894 internal_data->table_info[OTF_TABLE_TYPE_GSUB].stream = stream;
2895 if ((stream = make_stream_from_ft_face (face, "GPOS")))
2896 internal_data->table_info[OTF_TABLE_TYPE_GPOS].stream = stream;
2897 }
2898
2899 if (! internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream)
2900 /* We can simulate the GDEF table. */
2901 internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream
2902 = make_stream ("GDEF");
2903 return 0;
2904 }
2905
2906 static OTF_TableInfo *
get_table_info(OTF * otf,const char * name)2907 get_table_info (OTF *otf, const char *name)
2908 {
2909 char *errfmt = "OTF Table Read%s";
2910 OTF_TableInfo *errret = NULL;
2911 OTF_InternalData *internal_data = otf->internal_data;
2912 OTF_TableInfo *table_info;
2913 OTF_Tag tag = OTF_tag (name);
2914
2915 if (! tag)
2916 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2917
2918 if (tag == OTF_tag ("head"))
2919 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2920 else if (tag == OTF_tag ("name"))
2921 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2922 else if (tag == OTF_tag ("cmap"))
2923 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2924 else if (tag == OTF_tag ("GDEF"))
2925 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2926 else if (tag == OTF_tag ("GSUB"))
2927 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2928 else if (tag == OTF_tag ("GPOS"))
2929 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2930 else
2931 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported table name)");
2932
2933 if (*table_info->address)
2934 /* Already read. */
2935 return table_info;
2936 if (! table_info->stream)
2937 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2938 if (! table_info->reader)
2939 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2940 return table_info;
2941 }
2942
2943
2944
2945 /*** (2) API for reading OTF */
2946
2947 /*** (2-1) OTF_open() */
2948
2949 /* Note: We can't use memory allocation macros in the following
2950 functions because those macros return from the functions before
2951 freeing memory previously allocated. */
2952
2953 OTF *
OTF_open(const char * otf_name)2954 OTF_open (const char *otf_name)
2955 {
2956 FILE *fp;
2957 char *errfmt = "opening otf (%s)";
2958 void *errret = NULL;
2959 OTF *otf;
2960 OTF_InternalData *internal_data;
2961 int len = strlen (otf_name);
2962 const char *ext = otf_name + (len - 4);
2963
2964 if (debug_flag < 0)
2965 set_debug_flag ();
2966
2967 if (len < 4
2968 || ext[0] != '.'
2969 || (strncasecmp (ext + 1, "otf", 3)
2970 && strncasecmp (ext + 1, "ttf", 3)
2971 && strncasecmp (ext + 1, "ttc", 3)))
2972 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2973 fp = fopen (otf_name, "rb");
2974 if (! fp)
2975 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2976 otf = calloc (1, sizeof (OTF));
2977 if (! otf)
2978 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2979 otf->filename = strdup (otf_name);
2980 if (! otf->filename)
2981 {
2982 OTF_close (otf);
2983 fclose (fp);
2984 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2985 }
2986
2987 internal_data = calloc (1, sizeof (OTF_InternalData));
2988 if (! internal_data)
2989 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2990 otf->internal_data = internal_data;
2991 if (! allocate_memory_record (otf))
2992 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2993
2994 /* Here after, all pointers to allocated memory are recorded in
2995 otf->internal_data->memory_record except for what allocated by
2996 the functions allocate_memory_record and make_stream. */
2997
2998 if (read_header_part (otf, fp, NULL) < 0)
2999 {
3000 OTF_close (otf);
3001 fclose (fp);
3002 return NULL;
3003 }
3004
3005 fclose (fp);
3006 return otf;
3007 }
3008
3009 OTF *
OTF_open_ft_face(FT_Face face)3010 OTF_open_ft_face (FT_Face face)
3011 {
3012 char *errfmt = "opening otf from Freetype (%s)";
3013 void *errret = NULL;
3014 OTF *otf;
3015 OTF_InternalData *internal_data;
3016
3017 if (debug_flag < 0)
3018 set_debug_flag ();
3019
3020 if (! FT_IS_SFNT (face))
3021 OTF_ERROR (OTF_ERROR_FILE, (char *) face->family_name);
3022 otf = calloc (1, sizeof (OTF));
3023 if (! otf)
3024 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
3025 otf->filename = NULL;
3026
3027 internal_data = calloc (1, sizeof (OTF_InternalData));
3028 if (! internal_data)
3029 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
3030 otf->internal_data = internal_data;
3031 if (! allocate_memory_record (otf))
3032 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
3033
3034 if (read_header_part (otf, NULL, face) < 0)
3035 {
3036 OTF_close (otf);
3037 return NULL;
3038 }
3039
3040 return otf;
3041 }
3042
3043 /*** (2-2) OTF_close() */
3044
3045 void
OTF_close(OTF * otf)3046 OTF_close (OTF *otf)
3047 {
3048 OTF_InternalData *internal_data = otf->internal_data;
3049 int i;
3050
3051 if (internal_data)
3052 {
3053 OTF_MemoryRecord *memrec = internal_data->memory_record;
3054 OTF_ApplicationData *app_data = internal_data->app_data;
3055
3056 if (internal_data->header_stream)
3057 free_stream (internal_data->header_stream);
3058
3059 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
3060 if (internal_data->table_info[i].stream)
3061 free_stream (internal_data->table_info[i].stream);
3062
3063 for (; app_data; app_data = app_data->next)
3064 if (app_data->data && app_data->freer)
3065 app_data->freer (app_data->data);
3066
3067 while (memrec)
3068 {
3069 OTF_MemoryRecord *next = memrec->next;
3070
3071 for (i = memrec->used - 1; i >= 0; i--)
3072 free (memrec->memory[i]);
3073 free (memrec);
3074 memrec = next;
3075 }
3076
3077 free (internal_data);
3078 }
3079 if (otf->filename)
3080 free (otf->filename);
3081 free (otf);
3082 }
3083
3084 /*** (2-3) OTF_get_table() */
3085
3086 int
OTF_get_table(OTF * otf,const char * name)3087 OTF_get_table (OTF *otf, const char *name)
3088 {
3089 OTF_TableInfo *table_info = get_table_info (otf, name);
3090 void *address;
3091
3092 if (! table_info)
3093 return -1;
3094 if (! table_info->stream)
3095 /* Already fully loaded. */
3096 return 0;
3097
3098 address = (*table_info->reader) (otf, table_info, OTF_READ_FULL);
3099 free_stream (table_info->stream);
3100 table_info->stream = NULL;
3101 if (! address)
3102 {
3103 table_info->reader = NULL;
3104 return -1;
3105 }
3106 return 0;
3107 }
3108
3109 /*** (2-4) OTF_check_table() */
3110
3111 int
OTF_check_table(OTF * otf,const char * name)3112 OTF_check_table (OTF *otf, const char *name)
3113 {
3114 return (get_table_info (otf, name) ? 0 : -1);
3115 }
3116
3117 /*** (2-5) OTF_get_scripts() */
3118
3119 int
OTF_get_scripts(OTF * otf,int gsubp)3120 OTF_get_scripts (OTF *otf, int gsubp)
3121 {
3122 OTF_TableInfo *table_info
3123 = (otf->internal_data->table_info
3124 + (gsubp ? OTF_TABLE_TYPE_GSUB : OTF_TABLE_TYPE_GPOS));
3125 void *address;
3126
3127 if (! table_info->reader)
3128 return -1;
3129 if (! table_info->stream)
3130 /* Already fully loaded. */
3131 return 0;
3132
3133 address = (*table_info->reader) (otf, table_info, OTF_READ_SCRIPTS);
3134 if (! address)
3135 {
3136 table_info->reader = NULL;
3137 return -1;
3138 }
3139 return 0;
3140 }
3141
3142 /*** (2-6) OTF_get_features() */
3143
3144 int
OTF_get_features(OTF * otf,int gsubp)3145 OTF_get_features (OTF *otf, int gsubp)
3146 {
3147 OTF_TableInfo *table_info
3148 = (otf->internal_data->table_info
3149 + (gsubp ? OTF_TABLE_TYPE_GSUB : OTF_TABLE_TYPE_GPOS));
3150 void *address;
3151
3152 if (! table_info->reader)
3153 return -1;
3154 if (! table_info->stream)
3155 {
3156 if (*table_info->address)
3157 /* Already fully loaded. */
3158 return 0;
3159 return -1;
3160 }
3161
3162 address = (*table_info->reader) (otf, table_info, OTF_READ_FEATURES);
3163 if (! address)
3164 {
3165 table_info->reader = NULL;
3166 return -1;
3167 }
3168 return 0;
3169 }
3170
3171 /*** (2-7) OTF_check_features */
3172
3173 int
OTF_check_features(OTF * otf,int gsubp,OTF_Tag script,OTF_Tag language,const OTF_Tag * features,int n_features)3174 OTF_check_features (OTF *otf, int gsubp,
3175 OTF_Tag script, OTF_Tag language, const OTF_Tag *features,
3176 int n_features)
3177 {
3178 OTF_ScriptList *script_list;
3179 OTF_Script *Script = NULL;
3180 OTF_LangSys *LangSys = NULL;
3181 OTF_FeatureList *feature_list;
3182 int i, j;
3183
3184 if (OTF_get_features (otf, gsubp) < 0)
3185 {
3186 if (gsubp ? ! otf->gsub : ! otf->gpos)
3187 return 0;
3188 for (i = 0; i < n_features; i++)
3189 {
3190 OTF_Tag feature = features[i];
3191
3192 if (feature == 0)
3193 continue;
3194 if ((((unsigned) feature) & 0x80000000) == 0)
3195 return -1;
3196 }
3197 }
3198 if (gsubp)
3199 {
3200 script_list = &otf->gsub->ScriptList;
3201 feature_list = &otf->gsub->FeatureList;
3202 }
3203 else
3204 {
3205 script_list = &otf->gpos->ScriptList;
3206 feature_list = &otf->gpos->FeatureList;
3207 }
3208 for (i = 0; i < script_list->ScriptCount && ! Script; i++)
3209 if (script_list->Script[i].ScriptTag == script)
3210 Script = script_list->Script + i;
3211 if (! Script)
3212 return 0;
3213 if (language)
3214 {
3215 for (i = 0; i < Script->LangSysCount && ! LangSys; i++)
3216 if (Script->LangSysRecord[i].LangSysTag == language)
3217 LangSys = Script->LangSys + i;
3218 if (! LangSys)
3219 return 0;
3220 }
3221 else
3222 LangSys = &Script->DefaultLangSys;
3223 for (j = 0; j < n_features; j++)
3224 {
3225 OTF_Tag feature = features[j];
3226 int negate = 0;
3227
3228 if (feature == 0)
3229 continue;
3230 if (((unsigned) feature) & 0x80000000)
3231 {
3232 feature = (OTF_Tag) (((unsigned) feature) & 0x7FFFFFFF);
3233 negate = 1;
3234 }
3235 for (i = 0; i < LangSys->FeatureCount; i++)
3236 if (feature_list->Feature[LangSys->FeatureIndex[i]].FeatureTag
3237 == feature)
3238 {
3239 if (negate)
3240 return 0;
3241 break;
3242 }
3243 if (i == LangSys->FeatureCount)
3244 return 0;
3245 }
3246 return 1;
3247 }
3248
3249
3250 /*** (5) API miscellaneous ***/
3251
3252 OTF_Tag
OTF_tag(const char * name)3253 OTF_tag (const char *name)
3254 {
3255 const unsigned char *p = (unsigned char *) name;
3256
3257 if (! name)
3258 return (OTF_Tag) 0;
3259 return (OTF_Tag) ((p[0] << 24)
3260 | (! p[1] ? 0
3261 : ((p[1] << 16)
3262 | (! p[2] ? 0
3263 : (p[2] << 8) | p[3]))));
3264 }
3265
3266 void
OTF_tag_name(OTF_Tag tag,char * name)3267 OTF_tag_name (OTF_Tag tag, char *name)
3268 {
3269 name[0] = (char) (tag >> 24);
3270 name[1] = (char) ((tag >> 16) & 0xFF);
3271 name[2] = (char) ((tag >> 8) & 0xFF);
3272 name[3] = (char) (tag & 0xFF);
3273 name[4] = '\0';
3274 }
3275
3276 int
OTF_put_data(OTF * otf,char * id,void * data,void (* freer)(void * data))3277 OTF_put_data (OTF *otf, char *id, void *data, void (*freer) (void *data))
3278 {
3279 char *errfmt = "appdata %s";
3280 int errret = -1;
3281 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
3282 OTF_ApplicationData *app_data = internal_data->app_data;
3283 int len = strlen (id) + 1;
3284
3285 for (; app_data; app_data = app_data->next)
3286 if (memcmp (app_data->id, id, len) == 0)
3287 {
3288 if (app_data->data && app_data->freer)
3289 app_data->freer (app_data->data);
3290 break;
3291 }
3292 if (! app_data)
3293 {
3294 OTF_MALLOC (app_data, sizeof (OTF_ApplicationData), id);
3295 app_data->next = internal_data->app_data;
3296 internal_data->app_data = app_data;
3297 OTF_MALLOC (app_data->id, len, id);
3298 memcpy (app_data->id, id, len);
3299 }
3300 app_data->data = data;
3301 app_data->freer = freer;
3302 return 0;
3303 }
3304
3305 void *
OTF_get_data(OTF * otf,char * id)3306 OTF_get_data (OTF *otf, char *id)
3307 {
3308 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
3309 OTF_ApplicationData *app_data = internal_data->app_data;
3310
3311 for (; app_data; app_data = app_data->next)
3312 if (strcmp (app_data->id, id) == 0)
3313 return app_data->data;
3314 return NULL;
3315 }
3316