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