1 /* Copyright (C) 2001-2012 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied,
8    modified or distributed except as expressly authorized under the terms
9    of the license contained in the file LICENSE in this distribution.
10 
11    Refer to licensing information at http://www.artifex.com or contact
12    Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
13    CA  94903, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 
17 /* The TrueType instruction interpreter interface definition. */
18 
19 #ifndef incl_ttfoutl
20 #define incl_ttfoutl
21 
22 #ifndef TFace_defined
23 #define TFace_defined
24 typedef struct _TFace TFace;
25 #endif
26 
27 #ifndef TInstance_defined
28 #define TInstance_defined
29 typedef struct _TInstance TInstance;
30 #endif
31 
32 #ifndef TExecution_Context_defined
33 #define TExecution_Context_defined
34 typedef struct _TExecution_Context TExecution_Context;
35 #endif
36 
37 #ifndef ttfInterpreter_DEFINED
38 #  define ttfInterpreter_DEFINED
39 typedef struct ttfInterpreter_s ttfInterpreter;
40 #endif
41 
42 /* Define auxiliary data types for the TT interpreter. */
43 
44 typedef struct ttfMemoryDescriptor_s ttfMemoryDescriptor;
45 
46 typedef struct {
47     double a, b, c, d, tx, ty;
48 } FloatMatrix;
49 
50 typedef struct {
51     double x, y;
52 } FloatPoint;
53 
54 #if   ARCH_LOG2_SIZEOF_LONG == 2
55 typedef signed long F26Dot6;
56 #elif ARCH_LOG2_SIZEOF_INT  == 2
57 typedef signed int  F26Dot6;
58 #else
59 #error "No appropriate type for Fixed 26.6 Floats"
60 #endif
61 
62 typedef struct {
63     F26Dot6 x;
64     F26Dot6 y;
65 } F26Dot6Point;
66 
67 /* Define an abstract class for accessing memory managers from the TT interpreter. */
68 typedef struct ttfMemory_s ttfMemory;
69 struct ttfMemory_s {
70     void *(*alloc_bytes)(ttfMemory *, int size,  const char *cname);
71     void *(*alloc_struct)(ttfMemory *, const ttfMemoryDescriptor *,  const char *cname);
72     void (*free)(ttfMemory *, void *p,  const char *cname);
73 } ;
74 
75 typedef struct ttfSubGlyphUsage_s ttfSubGlyphUsage;
76 
77 /* Define a capsule for the TT interpreter. */
78 struct ttfInterpreter_s {
79     TExecution_Context *exec;
80     ttfSubGlyphUsage *usage;
81     int usage_size;
82     int usage_top;
83     int lock;
84     ttfMemory *ttf_memory;
85 };
86 
87 /* Define TT interpreter return codes. */
88 typedef enum {
89     fNoError,
90     fTableNotFound,
91     fNameNotFound,
92     fMemoryError,
93     fUnimplemented,
94     fCMapNotFound,
95     fGlyphNotFound,
96     fBadFontData,
97     fPatented,
98     fBadInstruction
99 } FontError;
100 
101 /* Define an abstract class for accessing TT data from the TT interpreter. */
102 typedef struct ttfReader_s ttfReader;
103 struct ttfReader_s {
104     bool   (*Eof)(ttfReader *);
105     void   (*Read)(ttfReader *, void *p, int n);
106     void   (*Seek)(ttfReader *, int nPos);
107     int    (*Tell)(ttfReader *);
108     bool   (*Error)(ttfReader *);
109     int    (*LoadGlyph)(ttfReader *, int nIndex, const byte **, int *);
110     void   (*ReleaseGlyph)(ttfReader *, int nIndex);
111     int    (*get_metrics)(const ttfReader *ttf, uint glyph_index, bool bVertical,
112                           short *sideBearing, unsigned short *nAdvance);
113 };
114 
115 /* Define an auxiliary structure for ttfFont. */
116 typedef struct {
117     int nPos, nLen;
118 } ttfPtrElem;
119 
120 /* Define a capsule for a TT face.
121    Diue to historical reason the name is some misleading.
122    It should be ttfFace. */
123 #ifndef ttfFont_DEFINED
124 #  define ttfFont_DEFINED
125 typedef struct ttfFont_s ttfFont;
126 #endif
127 struct ttfFont_s {
128     ttfPtrElem t_cvt_;
129     ttfPtrElem t_fpgm;
130     ttfPtrElem t_glyf;
131     ttfPtrElem t_head;
132     ttfPtrElem t_hhea;
133     ttfPtrElem t_hmtx;
134     ttfPtrElem t_vhea;
135     ttfPtrElem t_vmtx;
136     ttfPtrElem t_loca;
137     ttfPtrElem t_maxp;
138     ttfPtrElem t_prep;
139     ttfPtrElem t_cmap;
140     unsigned short nUnitsPerEm;
141     unsigned int nFlags;
142     unsigned int nNumGlyphs;
143     unsigned int nMaxComponents;
144     unsigned int nLongMetricsVert;
145     unsigned int nLongMetricsHorz;
146     unsigned int nIndexToLocFormat;
147     bool    patented;
148     bool    design_grid;
149     TFace *face;
150     TInstance *inst;
151     TExecution_Context  *exec;
152     ttfInterpreter *tti;
153     void (*DebugRepaint)(ttfFont *);
154     int (*DebugPrint)(ttfFont *, const char *s, ...);
155     const gs_memory_t *DebugMem;
156 };
157 
158 void ttfFont__init(ttfFont *this, ttfMemory *mem,
159                    void (*DebugRepaint)(ttfFont *),
160                    int (*DebugPrint)(ttfFont *, const char *s, ...),
161                    const gs_memory_t *);
162 void ttfFont__finit(ttfFont *this);
163 FontError ttfFont__Open(ttfInterpreter *, ttfFont *, ttfReader *r,
164                         unsigned int nTTC, float w, float h,
165                         bool design_grid);
166 
167 /* Define an abstract class for exporting outlines from the TT interpreter. */
168 typedef struct ttfExport_s ttfExport;
169 struct ttfExport_s {
170     bool bPoints, bOutline;
171     void (*MoveTo)(ttfExport *, FloatPoint*);
172     void (*LineTo)(ttfExport *, FloatPoint*);
173     void (*CurveTo)(ttfExport *, FloatPoint*, FloatPoint*, FloatPoint*);
174     void (*Close)(ttfExport *);
175     void (*Point)(ttfExport *, FloatPoint*, bool bOnCurve, bool bNewPath);
176     void (*SetWidth)(ttfExport *, FloatPoint*);
177     void (*DebugPaint)(ttfExport *);
178 };
179 
180 int ttfInterpreter__obtain(ttfMemory *mem, ttfInterpreter **ptti);
181 void ttfInterpreter__release(ttfInterpreter **ptti);
182 
183 /* Define an class for outline description. */
184 typedef struct {
185     bool    bCompound;
186     int     contourCount;
187     uint    pointCount;
188     F26Dot6Point  advance;
189     F26Dot6 sideBearing;
190     F26Dot6   xMinB, yMinB, xMaxB, yMaxB;
191 } ttfGlyphOutline;
192 
193 /* Define an class for generating TT outlines. */
194 typedef struct {
195     bool bOutline;
196     bool bFirst;
197     bool bVertical;
198     int  nPointsTotal;
199     int  nContoursTotal;
200     F26Dot6 ppx, ppy;
201     ttfReader *r;
202     ttfExport *exp;
203     ttfFont *pFont;
204     ttfGlyphOutline out;
205     FloatMatrix post_transform;
206 } ttfOutliner;
207 
208 void ttfOutliner__init(ttfOutliner *, ttfFont *f, ttfReader *r, ttfExport *exp,
209                         bool bOutline, bool bFirst, bool bVertical);
210 FontError ttfOutliner__Outline(ttfOutliner *this, int glyphIndex,
211         float orig_x, float orig_y, FloatMatrix *m1);
212 void ttfOutliner__DrawGlyphOutline(ttfOutliner *this);
213 
214 #endif
215