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