2564b80ff4e37164a9aa558d77e46b88244fa658
[m17n/libotf.git] / src / otf.h
1 /* otf.h -- Header file for libotf (OpenType font library).
2
3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 #ifndef _OTF_H_
25 #define _OTF_H_
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /* Version name of this library.  */
32 #define LIBOTF_VERSION "0.9.10"
33
34 /* Major version number.  */
35 #define LIBOTF_MAJOR_VERSION 0
36 /* Minor version number.  */
37 #define LIBOTF_MINOR_VERSION 9
38 /* Release (i.e. patch level) number.  */
39 #define LIBOTF_RELEASE_NUMBER 10
40
41 /***
42     Table of contents:
43
44     (1) Structures for OTF Layout tables and OTF itself
45     (1-1) Basic types
46     (1-2) "head" table
47     (1-3) "name" table
48     (1-4) "cmap" table
49     (1-5) Structures common to GDEF, GSUB, and GPOS
50     (1-6) "GDEF" table
51     (1-7) Structures for ScriptList, FeatureList, and LookupList
52     (1-8) Structures common to GSUB and GPOS
53     (1-9) "GSUB" table
54     (1-10) "GPOS" table
55     (1-11) Structure for OTF
56
57     (2) API for reading OTF
58     (2-1) OTF_open(), OTF_open_ft_face()
59     (2-2) OTF_close()
60     (2-3) OTF_get_table()
61     (2-4) OTF_check_table()
62
63     (3) API for driving OTF
64     (3-1) Structure for glyph string
65     (3-2) OTF_drive_cmap()
66     (3-3) OTF_drive_gdef()
67     (3-4) OTF_drive_gsub()
68     (3-5) OTF_drive_gpos()
69     (3-6) OTF_drive_tables()
70     (3-7) OTF_get_unicode()
71     (3-8) OTF_drive_gsub_alternate()
72     (3-9) OTF_iterate_on_feature()
73
74     (4) API for error handling
75     (4-1) Error codes
76     (4-2) OTF_perror()
77
78     (5) API miscellaneous
79
80 */
81
82 \f
83 /*** (1) Structures for OTF Layout tables and OTF itself */
84
85 /*** (1-1) Basic types */
86
87 typedef unsigned OTF_Tag;
88 typedef unsigned OTF_GlyphID;
89 typedef unsigned OTF_Offset;
90
91 typedef struct
92 {
93   unsigned high;
94   unsigned low;
95 } OTF_Fixed;
96
97
98 /*** (1-2) "head" table */
99
100 typedef struct
101 {
102   OTF_Fixed TableVersionNumber;
103   OTF_Fixed fontRevision;
104   unsigned checkSumAdjustment;
105   unsigned magicNumber;
106   unsigned flags;
107   int unitsPerEm;
108 } OTF_head;
109
110
111 /*** (1-3) "name" table */
112
113 typedef struct
114 {
115   int platformID;
116   int encodingID;
117   int languageID;
118   int nameID;
119   int length;
120   int offset;
121
122   /* If nonzero, NAME is an ASCII string.  */
123   int ascii;
124   unsigned char *name;
125 } OTF_NameRecord;
126
127 #define OTF_max_nameID 23
128
129 typedef struct
130 {
131   int format;
132   int count;
133   int stringOffset;
134   OTF_NameRecord *nameRecord;
135   char *name[OTF_max_nameID + 1];
136 } OTF_name;
137
138
139 /*** (1-4) "cmap" table */
140
141 typedef struct
142 {
143   unsigned char glyphIdArray[256];
144 } OTF_EncodingSubtable0;
145
146 typedef struct
147 {
148   unsigned firstCode;
149   unsigned entryCount;
150   int idDelta;
151   unsigned idRangeOffset;
152 } OTF_cmapSubHeader;
153
154 typedef struct
155 {
156   unsigned short subHeaderKeys[256];
157   int subHeaderCount;
158   OTF_cmapSubHeader *subHeaders;
159   int glyphIndexCount;
160   OTF_GlyphID *glyphIndexArray;
161 } OTF_EncodingSubtable2;
162
163 typedef struct
164 {
165   unsigned startCount;
166   unsigned endCount;
167   int idDelta;
168   unsigned idRangeOffset;
169 } OTF_cmapSegment;
170
171 typedef struct
172 {
173   unsigned segCountX2;
174   unsigned searchRange;
175   unsigned entrySelector;
176   unsigned rangeShift;
177   OTF_cmapSegment *segments;
178   int GlyphCount;
179   unsigned *glyphIdArray;
180 } OTF_EncodingSubtable4;
181
182 typedef struct
183 {
184   unsigned firstCode;
185   unsigned entryCount;
186   unsigned *glyphIdArray;
187 } OTF_EncodingSubtable6;
188
189 typedef struct
190 {
191   unsigned startCharCode;
192   unsigned endCharCode;
193   unsigned startGlyphID;
194 } OTF_cmapGroup;
195
196 typedef struct
197 {
198   unsigned char is32[8192];
199   unsigned nGroups;
200   OTF_cmapGroup *Groups;
201 } OTF_EncodingSubtable8;
202
203 typedef struct
204 {
205   unsigned startCharCode;
206   unsigned numChars;
207   unsigned *glyphs;
208 } OTF_EncodingSubtable10;
209
210 typedef struct
211 {
212   unsigned nGroups;
213   OTF_cmapGroup *Groups;
214 } OTF_EncodingSubtable12;
215
216 typedef struct
217 {
218   unsigned unicodeValue;
219   unsigned short glyphID;
220 } OTF_UVSMapping;
221
222 typedef struct
223 {
224   unsigned startUnicodeValue;
225   unsigned short additionalCount;
226 } OTF_UnicodeValueRange;
227
228 typedef struct
229 {
230   unsigned varSelector;
231   unsigned defaultUVSOffset;
232   unsigned nonDefaultUVSOffset;
233   /* DefaultUVS */
234   unsigned numUnicodeValueRanges;
235   OTF_UnicodeValueRange *unicodeValueRanges;
236   /* NonDefaultUVS */
237   unsigned numUVSMappings;
238   OTF_UVSMapping *uvsMappings;
239 } OTF_VariationSelectorRecord;
240
241 typedef struct
242 {
243   unsigned nRecords;
244   OTF_VariationSelectorRecord *Records;
245 } OTF_EncodingSubtable14;
246
247 typedef struct
248 {
249   unsigned format;
250   unsigned length;
251   unsigned language;
252   union {
253     OTF_EncodingSubtable0 *f0;
254     OTF_EncodingSubtable2 *f2;
255     OTF_EncodingSubtable4 *f4;
256     OTF_EncodingSubtable6 *f6;
257     OTF_EncodingSubtable8 *f8;
258     OTF_EncodingSubtable10 *f10;
259     OTF_EncodingSubtable12 *f12;
260     OTF_EncodingSubtable14 *f14;
261   }f;
262 } OTF_EncodingSubtable;
263
264 typedef struct
265 {
266   unsigned platformID;
267   unsigned encodingID;
268   unsigned offset;
269   OTF_EncodingSubtable subtable;
270 } OTF_EncodingRecord;
271
272 typedef struct
273 {
274   unsigned version;
275   unsigned numTables;
276   OTF_EncodingRecord *EncodingRecord;
277   /* Mapping table: Unicode->GlyphID (for BMP only) */
278   unsigned short *unicode_table;
279   int max_glyph_id;
280   /* Mapping table: GlyphID->Unicode */
281   unsigned short *decode_table;
282   /* Index of the EncodingRecord for Unicode->GlyphID mapping.
283      -1 means that the font supports only Unicode BMP characters.  */
284   int table_index;
285 } OTF_cmap;
286
287
288 /*** (1-5) Structures common to GDEF, GSUB, GPOS */
289
290 typedef struct
291 {
292   OTF_GlyphID Start;
293   OTF_GlyphID End;
294   unsigned StartCoverageIndex;
295 } OTF_RangeRecord;
296
297 typedef struct
298 {
299   OTF_Offset offset;
300   unsigned CoverageFormat;
301   unsigned Count;
302   union {
303     OTF_GlyphID *GlyphArray;
304     OTF_RangeRecord *RangeRecord;
305   } table;
306 } OTF_Coverage;
307
308 typedef struct
309 {
310   OTF_Offset offset;
311   unsigned StartSize;
312   unsigned EndSize;
313   unsigned DeltaFormat;
314   char *DeltaValue;
315 } OTF_DeviceTable;
316
317 typedef struct
318 {
319   OTF_GlyphID Start;
320   OTF_GlyphID End;
321   unsigned Class;
322 } OTF_ClassRangeRecord;
323
324 typedef struct
325 {
326   OTF_Offset offset;
327   unsigned ClassFormat;
328   union {
329     struct {
330       OTF_GlyphID StartGlyph;
331       unsigned GlyphCount;
332       unsigned *ClassValueArray;
333     } f1;
334     struct {
335       unsigned ClassRangeCount;
336       OTF_ClassRangeRecord *ClassRangeRecord;
337     } f2;
338   } f;
339 } OTF_ClassDef;
340
341
342 /*** (1-6) "GDEF" table */
343
344 typedef struct
345 {
346   OTF_Fixed Version;
347   OTF_Offset GlyphClassDef;
348   OTF_Offset AttachList;
349   OTF_Offset LigCaretList;
350   OTF_Offset MarkAttachClassDef;
351 } OTF_GDEFHeader;
352
353 enum OTF_GlyphClassDef
354   {
355     OTF_GlyphClass0 = 0,
356     OTF_GlyphClassBase = 1,
357     OTF_GlyphClassLigature = 2,
358     OTF_GlyphClassMark = 3,
359     OTF_GlyphClassComponent = 4
360   };
361
362 typedef struct
363 {
364   OTF_Offset offset;
365   unsigned PointCount;
366   unsigned *PointIndex;
367 } OTF_AttachPoint;
368
369 typedef struct
370 {
371   OTF_Coverage Coverage;
372   unsigned GlyphCount;
373   OTF_AttachPoint *AttachPoint;
374 } OTF_AttachList;
375
376 typedef struct
377 {
378   OTF_Offset offset;
379   unsigned CaretValueFormat;    /* 1, 2, or 3 */
380   union {
381     union {
382       int Coordinate;
383     } f1;
384     union {
385       unsigned CaretValuePoint;
386     } f2;
387     union {
388       int Coordinate;
389       OTF_DeviceTable DeviceTable;
390     } f3;
391   } f;
392 } OTF_CaretValue;
393
394 typedef struct
395 {
396   OTF_Offset offset;
397   unsigned CaretCount;
398   OTF_CaretValue *CaretValue;
399 } OTF_LigGlyph;
400
401 typedef struct
402 {
403   OTF_Coverage Coverage;
404   unsigned LigGlyphCount;
405   OTF_LigGlyph *LigGlyph;
406 } OTF_LigCaretList;
407
408 typedef struct
409 {
410   OTF_GDEFHeader header;
411   OTF_ClassDef glyph_class_def;
412   OTF_AttachList attach_list;
413   OTF_LigCaretList lig_caret_list;
414   OTF_ClassDef mark_attach_class_def;
415 } OTF_GDEF;
416
417
418 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList  */
419
420 /*** The structure hierarchy
421
422    ScriptList
423      ScriptRecord[]
424        ScriptTag
425      Script[]
426        DefaultLangSys
427        LangSysRecord[]
428          LangSysTag
429        LangSys[]
430          LookupOrder
431          ReqFeatureIndex
432          FeatureIndex[]
433
434   FeatureList
435     FeatureRecored[]
436       FeatureTag
437     Feature[]
438       FeatureParams
439       LookupListIndex[]
440
441   LookupList
442     LookupOffset[]
443     Lookup[]
444       LookupType
445       LookupFlag
446       SubTableOffset[]
447       SubTable.gsub[] or SubTable.gpos[]
448 */
449
450
451 typedef struct
452 {
453   OTF_Offset LookupOrder;
454   unsigned ReqFeatureIndex;
455   unsigned FeatureCount;
456   unsigned *FeatureIndex;
457 } OTF_LangSys;
458
459 typedef struct
460 {
461   OTF_Tag LangSysTag;
462   OTF_Offset LangSys;
463 } OTF_LangSysRecord;
464
465 typedef struct
466 {
467   OTF_Tag ScriptTag;
468   OTF_Offset offset;
469   OTF_Offset DefaultLangSysOffset;
470   OTF_LangSys DefaultLangSys;
471   unsigned LangSysCount;
472   OTF_LangSysRecord *LangSysRecord;
473   OTF_LangSys *LangSys;
474 } OTF_Script;
475
476 typedef struct
477 {
478   OTF_Offset offset;
479   unsigned ScriptCount;
480   OTF_Script *Script;
481 } OTF_ScriptList;
482
483 typedef struct
484 {
485   OTF_Tag FeatureTag;
486   OTF_Offset offset;
487   OTF_Offset FeatureParams;
488   unsigned LookupCount;
489   unsigned *LookupListIndex;
490 } OTF_Feature;
491
492 typedef struct
493 {
494   OTF_Offset offset;
495   unsigned FeatureCount;
496   OTF_Feature *Feature;
497 } OTF_FeatureList;
498
499 typedef struct OTF_LookupSubTableGSUB OTF_LookupSubTableGSUB;
500 typedef struct OTF_LookupSubTableGPOS OTF_LookupSubTableGPOS;
501
502 enum OTF_LookupFlagBit
503   {
504     OTF_RightToLeft = 0x0001,
505     OTF_IgnoreBaseGlyphs = 0x0002,
506     OTF_IgnoreLigatures = 0x0004,
507     OTF_IgnoreMarks = 0x0008,
508     OTF_Reserved = 0x00F0,
509     OTF_MarkAttachmentType = 0xFF00
510   };
511
512 #define OTF_LookupFlagIgnoreMask \
513   (OTF_IgnoreBaseGlyphs | OTF_IgnoreLigatures | OTF_IgnoreMarks)
514
515 typedef struct
516 {
517   OTF_Offset offset;
518   unsigned LookupType;
519   unsigned LookupFlag;
520   unsigned SubTableCount;
521   OTF_Offset *SubTableOffset;
522   union {
523     OTF_LookupSubTableGSUB *gsub;
524     OTF_LookupSubTableGPOS *gpos;
525   } SubTable;
526 } OTF_Lookup;
527
528 typedef struct
529 {
530   OTF_Offset offset;
531   unsigned LookupCount;
532   OTF_Lookup *Lookup;
533 } OTF_LookupList;
534
535
536 /*** (1-8) Structures common to GSUB and GPOS */
537
538 /* For SubstLookupRecord (GSUB) and PosLookupRecord (GPOS).  */
539
540 typedef struct
541 {
542   unsigned SequenceIndex;
543   unsigned LookupListIndex;
544 } OTF_LookupRecord;
545
546 typedef struct
547 {
548   OTF_Offset offset;
549   unsigned GlyphCount;
550   unsigned LookupCount;
551   OTF_GlyphID *Input;           /* [<GlyphCount> - 1] */
552   OTF_LookupRecord *LookupRecord; /* [<LookupCount>] */
553 } OTF_Rule;
554
555 typedef struct
556 {
557   OTF_Offset offset;
558   unsigned RuleCount;
559   OTF_Rule *Rule;               /* [<RuleCount>] */
560 } OTF_RuleSet;
561
562 typedef struct
563 {
564   OTF_Offset offset;
565   unsigned GlyphCount;
566   unsigned LookupCount;
567   unsigned *Class;              /* [<GlyphCount> - 1] */
568   OTF_LookupRecord *LookupRecord; /* [<LookupCount>] */
569 } OTF_ClassRule;
570
571 typedef struct
572 {
573   OTF_Offset offset;
574   unsigned ClassRuleCnt;
575   OTF_ClassRule *ClassRule;     /* [<ClassRuleCnt>] */
576 } OTF_ClassSet;
577
578 typedef struct
579 {
580   OTF_Offset offset;
581   unsigned BacktrackGlyphCount;
582   OTF_GlyphID *Backtrack;
583   unsigned InputGlyphCount;
584   OTF_GlyphID *Input;
585   unsigned LookaheadGlyphCount;
586   OTF_GlyphID *LookAhead;
587   unsigned LookupCount;
588   OTF_LookupRecord *LookupRecord;
589 } OTF_ChainRule;
590
591 typedef struct
592 {
593   OTF_Offset offset;
594   unsigned ChainRuleCount;
595   OTF_ChainRule *ChainRule;
596 } OTF_ChainRuleSet;
597
598 typedef struct
599 {
600   OTF_Offset offset;
601   unsigned BacktrackGlyphCount;
602   unsigned *Backtrack;
603   unsigned InputGlyphCount;
604   unsigned *Input;
605   unsigned LookaheadGlyphCount;
606   unsigned *LookAhead;
607   unsigned LookupCount;
608   OTF_LookupRecord *LookupRecord;
609 } OTF_ChainClassRule;
610
611 typedef struct
612 {
613   OTF_Offset offset;
614   unsigned ChainClassRuleCnt;
615   OTF_ChainClassRule *ChainClassRule;
616 } OTF_ChainClassSet;
617
618
619 /* Common to OTF_GSUB/GPOS_Context1/2/3.  */
620
621 typedef struct
622 {
623   unsigned RuleSetCount;
624   OTF_RuleSet *RuleSet;         /* [<RuleSetCount>] */
625 } OTF_Context1;
626
627 typedef struct
628 {
629   OTF_ClassDef ClassDef;
630   unsigned ClassSetCnt;
631   OTF_ClassSet *ClassSet;       /* [<ClassSetCnt>] */
632 } OTF_Context2;
633
634 typedef struct
635 {
636   unsigned GlyphCount;
637   unsigned LookupCount;
638   OTF_Coverage *Coverage;       /* [<GlyphCount>] */
639   OTF_LookupRecord *LookupRecord; /* [<LookupCount>] */
640 } OTF_Context3;
641
642
643 /* Common to OTF_GSUB/GPOS_ChainContext1/2/3.  */
644
645 typedef struct
646 {
647   unsigned ChainRuleSetCount;
648   OTF_ChainRuleSet *ChainRuleSet;
649 } OTF_ChainContext1;
650
651 typedef struct
652 {
653   OTF_ClassDef BacktrackClassDef;
654   OTF_ClassDef InputClassDef;
655   OTF_ClassDef LookaheadClassDef;
656   unsigned ChainClassSetCnt;
657   OTF_ChainClassSet *ChainClassSet;
658 } OTF_ChainContext2;
659
660 typedef struct
661 {
662   unsigned BacktrackGlyphCount;
663   OTF_Coverage *Backtrack;
664   unsigned InputGlyphCount;
665   OTF_Coverage *Input;
666   unsigned LookaheadGlyphCount;
667   OTF_Coverage *LookAhead;
668   unsigned LookupCount;
669   OTF_LookupRecord *LookupRecord;
670 } OTF_ChainContext3;
671
672 /* Common to OTF_GSUB/GPOS.  */
673
674 typedef struct
675 {
676   OTF_Fixed Version;
677   OTF_ScriptList ScriptList;
678   OTF_FeatureList FeatureList;
679   OTF_LookupList LookupList;
680 } OTF_GSUB_GPOS;
681
682 /*** (1-9) "GSUB" table */
683
684 typedef struct
685 {
686   int DeltaGlyphID;
687 } OTF_GSUB_Single1;
688
689 typedef struct
690 {
691   unsigned GlyphCount;
692   OTF_GlyphID *Substitute;
693 } OTF_GSUB_Single2;
694
695 typedef struct OTF_Sequence OTF_Sequence;
696
697 typedef struct
698 {
699   unsigned SequenceCount;
700   OTF_Sequence *Sequence;
701 } OTF_GSUB_Multiple1;
702
703 struct OTF_Sequence
704 {
705   OTF_Offset offset;
706   unsigned GlyphCount;
707   OTF_GlyphID *Substitute;
708 };
709
710 typedef struct OTF_AlternateSet OTF_AlternateSet;
711
712 typedef struct
713 {
714   unsigned AlternateSetCount;
715   OTF_AlternateSet *AlternateSet;
716 } OTF_GSUB_Alternate1;
717
718 struct OTF_AlternateSet
719 {
720   OTF_Offset offset;
721   unsigned GlyphCount;
722   OTF_GlyphID *Alternate;
723 };
724
725 typedef struct OTF_LigatureSet OTF_LigatureSet;
726 typedef struct OTF_Ligature OTF_Ligature;
727
728 typedef struct
729 {
730   unsigned LigSetCount;
731   OTF_LigatureSet *LigatureSet;
732 } OTF_GSUB_Ligature1;
733
734 struct OTF_LigatureSet
735 {
736   OTF_Offset offset;
737   unsigned LigatureCount;
738   OTF_Ligature *Ligature;
739 };
740
741 struct OTF_Ligature
742 {
743   OTF_Offset offset;
744   OTF_GlyphID LigGlyph;
745   unsigned CompCount;
746   OTF_GlyphID *Component;
747 };
748
749 typedef OTF_Context1 OTF_GSUB_Context1;
750
751 typedef OTF_Context2 OTF_GSUB_Context2;
752
753 typedef OTF_Context3 OTF_GSUB_Context3;
754
755 typedef OTF_ChainContext1 OTF_GSUB_ChainContext1;
756
757 typedef OTF_ChainContext2 OTF_GSUB_ChainContext2;
758
759 typedef OTF_ChainContext3 OTF_GSUB_ChainContext3;
760
761 typedef struct
762 {
763   unsigned ExtensionLookupType;
764   unsigned ExtensionOffset;
765   OTF_LookupSubTableGSUB *ExtensionSubtable;
766 } OTF_GSUB_Extension1;
767
768 typedef struct
769 {
770   unsigned BacktrackGlyphCount;
771   OTF_Coverage *Backtrack;
772   unsigned LookaheadGlyphCount;
773   OTF_Coverage *LookAhead;
774   unsigned GlyphCount;
775   OTF_GlyphID *Substitute;
776 } OTF_GSUB_ReverseChain1;
777
778 struct OTF_LookupSubTableGSUB
779 {
780   unsigned Format;
781   OTF_Coverage Coverage;
782   union {
783     /* LookupType 1 */
784     OTF_GSUB_Single1 single1;
785     OTF_GSUB_Single2 single2;
786     /* LookupType 2 */
787     OTF_GSUB_Multiple1 multiple1;
788     /* LookupType 3 */
789     OTF_GSUB_Alternate1 alternate1;
790     /* LookupType 4 */
791     OTF_GSUB_Ligature1 ligature1;
792     /* LookupType 5 */
793     OTF_GSUB_Context1 context1;
794     OTF_GSUB_Context2 context2;
795     OTF_GSUB_Context3 context3;
796     /* LookupType 6 */
797     OTF_GSUB_ChainContext1 chain_context1;
798     OTF_GSUB_ChainContext2 chain_context2;
799     OTF_GSUB_ChainContext3 chain_context3;
800     /* LookupType 7 */
801     OTF_GSUB_Extension1 extension1;
802     /* LookupType 8 */
803     OTF_GSUB_ReverseChain1 reverse_chain1;
804   } u;
805 };
806
807 typedef OTF_GSUB_GPOS OTF_GSUB;
808
809 /*** (1-10) "GPOS" table */
810
811 enum OTF_ValueFormat
812   {
813     OTF_XPlacement = 0x0001,
814     OTF_YPlacement = 0x0002,
815     OTF_XAdvance = 0x0004,
816     OTF_YAdvance = 0x0008,
817     OTF_XPlaDevice = 0x0010,
818     OTF_YPlaDevice = 0x0020,
819     OTF_XAdvDevice = 0x0040,
820     OTF_YAdvDevice = 0x0080
821   };
822
823 typedef struct
824 {
825   int XPlacement;
826   int YPlacement;
827   int XAdvance;
828   int YAdvance;
829   OTF_DeviceTable XPlaDevice;
830   OTF_DeviceTable YPlaDevice;
831   OTF_DeviceTable XAdvDevice;
832   OTF_DeviceTable YAdvDevice;
833 } OTF_ValueRecord;
834
835 typedef struct
836 {
837   OTF_Offset offset;
838   unsigned AnchorFormat;
839   int XCoordinate;
840   int YCoordinate;
841   union {
842     struct {
843       unsigned AnchorPoint;
844     } f1;
845     struct {
846       OTF_DeviceTable XDeviceTable;
847       OTF_DeviceTable YDeviceTable;
848     } f2;
849   } f;
850 } OTF_Anchor;
851
852 typedef struct
853 {
854   unsigned Class;
855   OTF_Anchor MarkAnchor;
856 } OTF_MarkRecord;
857
858 typedef struct
859 {
860   OTF_Offset offset;
861   unsigned MarkCount;
862   OTF_MarkRecord *MarkRecord;
863 } OTF_MarkArray;
864
865 typedef struct
866 {
867   unsigned ValueFormat;
868   OTF_ValueRecord Value;
869 } OTF_GPOS_Single1;
870
871 typedef struct
872 {
873   unsigned ValueFormat;
874   unsigned ValueCount;
875   OTF_ValueRecord *Value;       /* [<ValueCount>] */
876 } OTF_GPOS_Single2;
877
878 typedef struct
879 {
880   OTF_GlyphID SecondGlyph;
881   OTF_ValueRecord Value1;
882   OTF_ValueRecord Value2;
883 } OTF_PairValueRecord;
884
885 typedef struct
886 {
887   OTF_Offset offset;
888   unsigned PairValueCount;
889   OTF_PairValueRecord *PairValueRecord;
890 } OTF_PairSet;
891
892 typedef struct
893 {
894   unsigned ValueFormat1;
895   unsigned ValueFormat2;
896   unsigned PairSetCount;
897   OTF_PairSet *PairSet;
898 } OTF_GPOS_Pair1;
899
900 typedef struct
901 {
902   OTF_ValueRecord Value1;
903   OTF_ValueRecord Value2;
904 } OTF_Class2Record;
905
906 typedef struct
907 {
908   OTF_Class2Record *Class2Record;
909 } OTF_Class1Record;
910
911 typedef struct
912 {
913   unsigned ValueFormat1;
914   unsigned ValueFormat2;
915   OTF_ClassDef ClassDef1;
916   OTF_ClassDef ClassDef2;
917   unsigned Class1Count;
918   unsigned Class2Count;
919   OTF_Class1Record *Class1Record; /* size: <Class1Count> */
920 } OTF_GPOS_Pair2;
921
922 typedef struct
923 {
924   OTF_Anchor EntryAnchor;
925   OTF_Anchor ExitAnchor;
926 } OTF_EntryExitRecord;
927
928 typedef struct
929 {
930   unsigned EntryExitCount;
931   OTF_EntryExitRecord *EntryExitRecord;
932 } OTF_GPOS_Cursive1;
933
934 typedef struct
935 {
936   OTF_Anchor *Anchor;
937 } OTF_AnchorRecord;
938
939 typedef struct
940 {
941   OTF_Offset offset;
942   unsigned Count;
943   OTF_AnchorRecord *AnchorRecord;
944 } OTF_AnchorArray;
945
946 typedef struct
947 {
948   OTF_Coverage BaseCoverage;
949   unsigned ClassCount;
950   OTF_MarkArray MarkArray;
951   OTF_AnchorArray BaseArray;
952 } OTF_GPOS_MarkBase1;
953
954 typedef struct
955 {
956   OTF_Anchor *LigatureAnchor; /* [<ClassCount>] */
957 } OTF_ComponentRecord;
958
959 typedef struct
960 {
961   OTF_Offset offset;
962   unsigned ComponentCount;
963   OTF_ComponentRecord *ComponentRecord; /* [<ComponentCount>] */
964 } OTF_LigatureAttach;
965
966 typedef struct
967 {
968   OTF_Offset offset;
969   unsigned LigatureCount;
970   OTF_LigatureAttach *LigatureAttach; /* [<LiagureCount>] */
971 } OTF_LigatureArray;
972
973 typedef struct
974 {
975   OTF_Coverage LigatureCoverage;
976   unsigned ClassCount;
977   OTF_MarkArray MarkArray;
978   OTF_LigatureArray LigatureArray;
979 } OTF_GPOS_MarkLig1;
980
981 typedef struct
982 {
983   OTF_Coverage Mark2Coverage;
984   unsigned ClassCount;
985   OTF_MarkArray Mark1Array;
986   OTF_AnchorArray Mark2Array;
987 } OTF_GPOS_MarkMark1;
988
989 typedef OTF_Context1 OTF_GPOS_Context1;
990
991 typedef OTF_Context2 OTF_GPOS_Context2;
992
993 typedef OTF_Context3 OTF_GPOS_Context3;
994
995 typedef OTF_ChainContext1 OTF_GPOS_ChainContext1;
996
997 typedef OTF_ChainContext2 OTF_GPOS_ChainContext2;
998
999 typedef OTF_ChainContext3 OTF_GPOS_ChainContext3;
1000
1001 typedef struct
1002 {
1003   unsigned ExtensionLookupType;
1004   unsigned ExtensionOffset;
1005   OTF_LookupSubTableGPOS *ExtensionSubtable;
1006 } OTF_GPOS_Extension1;
1007
1008
1009 struct OTF_LookupSubTableGPOS
1010 {
1011   unsigned Format;
1012   OTF_Coverage Coverage;
1013   union {
1014     /* LookupType 1 */
1015     OTF_GPOS_Single1 single1;
1016     OTF_GPOS_Single2 single2;
1017     /* LookupType 2 */
1018     OTF_GPOS_Pair1 pair1;
1019     OTF_GPOS_Pair2 pair2;
1020     /* LookupType 3 */
1021     OTF_GPOS_Cursive1 cursive1;
1022     /* LookupType 4 */
1023     OTF_GPOS_MarkBase1 mark_base1;
1024     /* LookupType 5 */
1025     OTF_GPOS_MarkLig1 mark_lig1;
1026     /* LookupType 6 */
1027     OTF_GPOS_MarkMark1 mark_mark1;
1028     /* LookupType 7 */
1029     OTF_GPOS_Context1 context1;
1030     OTF_GPOS_Context2 context2;
1031     OTF_GPOS_Context3 context3;
1032     /* LookupType 8 */
1033     OTF_GPOS_ChainContext1 chain_context1;
1034     OTF_GPOS_ChainContext2 chain_context2;
1035     OTF_GPOS_ChainContext3 chain_context3;
1036     /* LookupType 9 */
1037     OTF_GPOS_Extension1 extension1;
1038   } u;
1039 };
1040
1041 typedef OTF_GSUB_GPOS OTF_GPOS;
1042
1043 /*** (1-11) Structure for OTF */
1044
1045 typedef struct
1046 {
1047   OTF_Fixed sfnt_version;
1048   unsigned numTables;
1049   unsigned searchRange;
1050   unsigned enterSelector;
1051   unsigned rangeShift;
1052 } OTF_OffsetTable;
1053
1054 typedef struct
1055 {
1056   OTF_Tag tag;
1057   char name[5];
1058   unsigned checkSum;
1059   unsigned offset;
1060   unsigned length;
1061 } OTF_TableDirectory;
1062
1063 typedef struct OTF_InternalData  OTF_InternalData;
1064
1065 typedef struct
1066 {
1067   char *filename;
1068   OTF_OffsetTable offset_table;
1069   OTF_TableDirectory *table_dirs;
1070   OTF_head *head;
1071   OTF_name *name;
1072   OTF_cmap *cmap;
1073   OTF_GDEF *gdef;
1074   OTF_GSUB *gsub;
1075   OTF_GPOS *gpos;
1076   /* The following tables are not yet supported.  */
1077   /* OTF_BASE *base; */
1078   /* OTF_JSTF *jstf; */
1079   OTF_InternalData *internal_data;
1080 } OTF;
1081
1082 \f
1083 /*** (2) API for reading OTF */
1084
1085 /*** (2-1) otf_open () */
1086
1087 /***
1088     Open OpenType font
1089
1090     The OTF_open() function reads the OpenType font file whose name is
1091     $NAME, and return a pointer to the structure of type OTF.
1092
1093     It setups these member of the structure OTF:
1094         filename, offset_table, table_dirs
1095
1096     If the file can't be read or the file contains invalid data, NULL
1097     is returned, and the variable OTF_error is set to one of the
1098     following values.
1099
1100         OTF_ERROR_MEMORY
1101         OTF_ERROR_FILE
1102         OTF_ERROR_TABLE
1103
1104     See also OTF_get_table() and OTF_close().  */
1105
1106 extern OTF *OTF_open (const char *name);
1107
1108 #include <ft2build.h>
1109 #include FT_FREETYPE_H
1110
1111 extern OTF *OTF_open_ft_face (FT_Face face);
1112
1113
1114 /*** (2-2) OTF_close () */
1115
1116 /***
1117     Close OpenType font
1118
1119     The OTF_close() function closes the OpenType font $OTF which must
1120     be what the OTF_open() function returned.
1121
1122     See also OTF_open().  */
1123
1124 extern void OTF_close (OTF *otf);
1125
1126
1127 /*** (2-3) OTF_get_table () */
1128
1129 /***
1130     Get OpenType font table
1131
1132     The OTF_get_table() function setups one of the OTF table specified
1133     by $NAME in the OpenType font $OTF.
1134
1135     $NAME must be one of "head", "name", "cmap", "GDEF", "GSUB", and
1136     "GPOS", and a member of the same name is setup.
1137
1138     If the table is successfully setup, return 0.  Otherwise, return
1139     -1, and set the variable OTF_error to OTF_ERROR_TABLE.
1140
1141     See also OTF_open().  */
1142
1143 extern int OTF_get_table (OTF *otf, const char *name);
1144
1145 /*** (2-4) OTF_check_table () */
1146
1147 /***
1148     Check the existence of OpenType font table
1149
1150     The OTF_check_table() function checks if the the OTF table
1151     specified by $NAME exists in OpenType font $OTF.
1152
1153     If the table exists, return 0, else return -1.
1154
1155     See also OTF_open().  */
1156
1157 extern int OTF_check_table (OTF *otf, const char *name);
1158
1159 /*** (2-5) OTF_get_scripts () */
1160
1161 /***
1162     Get supported scripts.
1163
1164     The OTF_get_scripts() function setups OTF_ScriptList of GSUB (if
1165     $GSUBP is nonzero) or GPOS (if $GSUBP is zero) table of the
1166     OpenType font $OTF.
1167
1168     If the table is successfully setup, return 0.  Otherwise, retrun
1169     -1, and set the variable OTF_error to OTF_ERROR_TABLE.  */
1170
1171 extern int OTF_get_scripts (OTF *otf, int gsubp);
1172
1173 /*** (2-6) OTF_get_features () */
1174
1175 /***
1176     Get supported features.
1177
1178     The OTF_get_features() function setups OTF_FeatureList of GSUB (if
1179     $GSUBP is nonzero) or GPOS (if $GSUBP is zero) table of the
1180     OpenType font $OTF.
1181
1182     If the table is successfully setup, return 0.  Otherwise, retrun
1183     -1, and set the variable OTF_error to OTF_ERROR_TABLE.  */
1184
1185 extern int OTF_get_features (OTF *otf, int gsubp);
1186
1187 /*** (2-7) OTF_check_features  */
1188
1189 /***
1190     Check supported features.
1191
1192     The OTF_check_features() function checks if or not the OpenType
1193     font $OTF has, for $SCRIPT and $LANGUAGE, all features in the
1194     array $FEATURES.  The array size is $N_FEATURES.  If $LANGUAGE is
1195     zero or $OTF doesn't have LangSys for $SCRIPT, the default LangSys
1196     is checked.
1197
1198     If $OTF has all the features, return 1.  Otherwise, return 0.  If
1199     an error occurs, return -1, and set the variable OTF_error to
1200     OTF_ERROR_TABLE.   */
1201
1202 extern int OTF_check_features (OTF *otf, int gsubp,
1203                                OTF_Tag script, OTF_Tag language,
1204                                const OTF_Tag *features, int n_features);
1205
1206 /*** (3) API for driving OTF */
1207
1208 /*** (3-1) Structure for glyph string */
1209
1210 /***
1211     The structure OTF_Glyph contains information about each glyph in
1212     the structure OTF_GlyphString.  */
1213
1214 typedef struct
1215 {
1216   /** The first two members must be set by a clinet before calling the
1217       function OTF_drive_XXX().  **/
1218
1219   /* Character code of the glyph.  The value less than 32 is treated
1220      as a place-holder in a glyph string, and OTF_drive_XXX ()
1221      function just ignore the glyph as if it doesn't exist.  */
1222   int c;
1223
1224   /* Glyph ID of the glyph.  If the value is 0, the library gets a
1225      correct value from the above character code via cmap if such a
1226      glyph is available in the font.
1227
1228      The function OTF_drive_gpos2 may insert a glyph whose glyph_id is
1229      0 but positioning_type is positive.  It is not an actual glyph
1230      but just contains positioning information that should be
1231      accumulated to the positioning information of the previous
1232      glyphs.  */
1233   OTF_GlyphID glyph_id;
1234
1235   /* GlyphClass of the glyph.  The value is extracted from the GDEF
1236      table.  */
1237   enum OTF_GlyphClassDef GlyphClass;
1238
1239   /* MarkAttachClassDef of the glyph.  The value is extracted from the
1240      GDEF table.  */
1241   unsigned MarkAttachClass;
1242
1243   /* Positioning format type of the glyph.  The value specifies how
1244      the glyph positioning information is encoded in the member <f>.
1245      If the value is N, the union member fN, is used.  If the value is
1246      zero, the glyph has no positioning information, i.e. it should be
1247      drawn at the normal position.  */
1248   int positioning_type;
1249   union {
1250     struct {
1251       int from, to;
1252     } index;
1253     struct {
1254       enum OTF_ValueFormat format;
1255       OTF_ValueRecord *value;
1256     } f1;
1257     struct {
1258       enum OTF_ValueFormat format;
1259       OTF_ValueRecord *value;
1260     } f2;
1261     struct {
1262       OTF_Anchor *entry_anchor;
1263       OTF_Anchor *exit_anchor;
1264     } f3;
1265     struct {
1266       OTF_Anchor *mark_anchor;
1267       OTF_Anchor *base_anchor;
1268     } f4;
1269     struct {
1270       OTF_Anchor *mark_anchor;
1271       OTF_Anchor *ligature_anchor;
1272     } f5;
1273     struct {
1274       OTF_Anchor *mark1_anchor;
1275       OTF_Anchor *mark2_anchor;
1276     } f6;
1277   } f;
1278 } OTF_Glyph;
1279
1280 /***
1281     The structure OTF_GlyphString contains an array of glyphs (type
1282     OTF_Glyph).  It is used as arguments of otf_drive_XXX().  */
1283
1284 typedef struct
1285 {
1286   /* How many glyphs are allocated at the memory pointed by the member
1287       <glyphs>.  */
1288   int size;
1289   /* How many glyphs contain valid information.  */
1290   int used;
1291   /* Array of glyphs.  It must be allocated by malloc().  The
1292      functions otf_drive_XXX() may reallocate it and increase the
1293      members <size> and <used>.  */
1294   OTF_Glyph *glyphs;
1295 } OTF_GlyphString;
1296
1297
1298 /*** (3-2) OTF_drive_cmap() */
1299
1300 /***
1301     Process glyph string by Unicode-based cmap table.
1302
1303     The OTF_drive_cmap() function looks up a Unicode-based cmap table
1304     of OpenType font $OTF, and setup the member <glyph_id> of all
1305     glhphs in the glyph string $GSTRING if the value of the member is
1306     not zero.  */
1307
1308 extern int OTF_drive_cmap (OTF *otf, OTF_GlyphString *gstring);
1309
1310 /***
1311     Process glyph string by a specific cmap table.
1312
1313     The OTF_drive_cmap2() function looks up a cmap table (whose
1314     Platform-ID is $PLATFORM_ID an Encoding-ID is $ENCODING_ID) of
1315     OpenType font $OTF, and setup the member <glyph_id> of all glhphs
1316     in the glyph string $GSTRING if the value of the member is not
1317     zero.  */
1318
1319 extern int OTF_drive_cmap2 (OTF *otf, OTF_GlyphString *gstring,
1320                             int platform_id, int encoding_id);
1321
1322
1323 /***
1324     Store variable glyphs of character C in the array CODE.  The array
1325     size must be 256.  The Nth element of CODE is the glyph corresponding
1326     to the variation selector (N + 1).  The return value is the number
1327     of variation glyphs.  */
1328
1329 extern int OTF_get_variation_glyphs (OTF *otf, int c, OTF_GlyphID code[256]);
1330
1331
1332 /*** (3-3) OTF_drive_gdef() */
1333
1334 /***
1335     Process glyph string by GDEF table.
1336
1337     The OTF_drive_gdef() function looks up the GDEF table of OpenType
1338     font $OTF, and setup members <GlyphClass> and <MarkAttachClass> of
1339     all glhphs in the glyph string $GSTRING.  */
1340
1341 extern int OTF_drive_gdef (OTF *otf, OTF_GlyphString *gstring);
1342
1343
1344 /*** (3-4) OTF_drive_gsub() */
1345
1346 /***
1347     Process glyph string by GSUB table.
1348
1349     The OTF_drive_gsub() function looks up the GSUB table of OpenType
1350     font $OTF, and by using features the font has for script $SCRIPT
1351     and language system $LANGSYS, update member <glyphs> of the glyph
1352     string $GSTRING.  It may substitute, delete, insert glyphs in that
1353     array.  $FEATURES is a list of features to apply.  This doesn't
1354     perform a lookup of type 3 (Alternate Substitution).  For that,
1355     use OTF_drive_gsub_alternate().  */
1356
1357 extern int OTF_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
1358                            const char *script, const char *language,
1359                            const char *features);
1360
1361 /*** (3-5) OTF_drive_gpos() */
1362
1363 /***
1364     Process glyph string by GPOS table.
1365
1366     The OTF_drive_gpos() function is deprecated.  Use
1367     OTF_drive_gpos2() instread.  */
1368
1369 extern int OTF_drive_gpos (OTF *otf, OTF_GlyphString *gstring,
1370                            const char *script, const char *language,
1371                            const char *features);
1372
1373 /***
1374     Process glyph string by GPOS table.
1375
1376     The OTF_drive_gpos2() function looks up the GPOS table of $OTF of
1377     OpenType font $OTF, and by using features the font has for script
1378     $SCRIPT and language system $LANGSYS, setup members
1379     <positioning_type> and <f> of all glhphs in the glyph string
1380     $GSTRING.  $FEATURES is a list of features to apply.  */
1381
1382 extern int OTF_drive_gpos2 (OTF *otf, OTF_GlyphString *gstring,
1383                             const char *script, const char *language,
1384                             const char *features);
1385
1386 /*** (3-6) OTF_drive_tables() */
1387
1388 /***
1389     Process glyph string by cmap, GDEF, GSUB, and GPOS tables.
1390
1391     The OTF_drive_tables() function calls OTF_drive_cmap(),
1392     OTF_drive_gdef(), OTF_drive_gsub(), and OTF_drive_gpos() in this
1393     order, and update the glyphs string GSTRING.  */
1394
1395 extern int OTF_drive_tables (OTF *otf, OTF_GlyphString *gstring,
1396                              const char *script, const char *language,
1397                              const char *gsub_features,
1398                              const char *gpos_features);
1399
1400
1401 /*** (3-7) OTF_get_unicode()  */
1402
1403 /***
1404     Return Unicode code point corresponding to the glyph-id CODE.
1405   */
1406
1407 extern int OTF_get_unicode (OTF *otf, OTF_GlyphID code);
1408
1409 /*** (3-8) OTF_drive_gsub_alternate() */
1410
1411 /***
1412     Find alternate glyphs.
1413
1414     This is like OTF_drive_gsub(), but perform only a lookup of type 3
1415     (Alternate Substituion).  */
1416
1417 extern int OTF_drive_gsub_alternate (OTF *otf, OTF_GlyphString *gstring,
1418                                      const char *script, const char *language,
1419                                      const char *features);
1420
1421 /*** (3-9) OTF_iterate_on_feature() */
1422 typedef int (*OTF_Feature_Callback) (OTF *otf, const char *feature,
1423                                      unsigned glyph_id);
1424
1425 extern int OTF_iterate_gsub_feature (OTF *otf, OTF_Feature_Callback callback,
1426                                      const char *script, const char *language,
1427                                      const char *feature);
1428
1429 /*** (4) API for error handling ***/
1430
1431 /*** (4-1) Error codes ***/
1432
1433 /***
1434     Global variable holding an error code.
1435
1436     The variable OTF_error is set to one of OTF_ERROR_XXX macros when
1437     an error is detected in the OTF library.  */
1438 extern int OTF_error;
1439
1440 /***
1441     Memory allocation error
1442
1443     This error indicates that the library couldn't allocate
1444     memory.  */
1445 #define OTF_ERROR_MEMORY        1
1446
1447 /***
1448     File error
1449
1450     This error indicates that the library fails in opening, reading,
1451     or seeking an OTF file.  */
1452 #define OTF_ERROR_FILE          2
1453
1454 /***
1455     Invalid table contents
1456
1457     This error indicates that an OTF file contains invalid data.  */
1458 #define OTF_ERROR_TABLE         3
1459
1460 /***
1461     CMAP driving error
1462
1463     See the function otf_drive_cmap() for more detail.  */
1464 #define OTF_ERROR_CMAP_DRIVE    4
1465
1466 /***
1467     GDEF driving error
1468
1469     See the function OTF_drive_gdef() for more detail.  */
1470 #define OTF_ERROR_GDEF_DRIVE    5
1471
1472 /***
1473     GSUB driving error
1474
1475     See the function OTF_drive_gsub() for more detail.  */
1476 #define OTF_ERROR_GSUB_DRIVE    6
1477
1478 /***
1479     GPOS driving error
1480
1481     See the function OTF_drive_gpos() for more detail.  */
1482 #define OTF_ERROR_GPOS_DRIVE    7
1483
1484 /***
1485     FT_Face access error.
1486
1487     This error indicates that the library fails in accessing Sfnt
1488     tables via FT_Face.  */
1489 #define OTF_ERROR_FT_FACE       8
1490
1491
1492 /*** (4-2) OTF_perror() */
1493
1494 /***
1495     Print an OTF error message
1496
1497     The OTF_perror() function produces a message on the standard error
1498     output, describing the last error encountered during a call to the
1499     OTF library function.  If $PREFIX is not NULL, it is printed
1500     first, followed by a colon and a blank.  Then the message and a
1501     newline.  */
1502
1503 extern void OTF_perror (const char *prefix);
1504
1505
1506 /*** (5) API miscellaneous ***/
1507
1508 /***
1509     Return OTF tag of a specified name string.
1510
1511     The OTF_tag() function returns OTF tag of name $NAME.  If $NAME is
1512     NULL, return 0.  Otherwise, $NAME must be at least 4-byte length.
1513     Only the first 4 characters are took into an account.  */
1514
1515 extern OTF_Tag OTF_tag (const char *name);
1516
1517 /***
1518     Convert OTF tag to name string.
1519
1520     The OTF_tag_name() function converts OTF tag $TAG to a 5-byte name
1521     string (including the terminating NULL), and store it in $NAME.
1522     At least 5-byte space must be at $NAME.  */
1523
1524 extern void OTF_tag_name (OTF_Tag tag, char *name);
1525
1526 #ifdef __cplusplus
1527 }
1528 #endif
1529
1530 #endif /* not _OTF_H_ */