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