1 /* Copyright (C) 2001-2019 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.,  1305 Grant Avenue - Suite 200, Novato,
13    CA 94945, 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 /* For memento */
23 #include "malloc_.h"
24 #include "gxfcache.h"
25 
26 typedef struct _TFace TFace;
27 
28 typedef struct _TInstance TInstance;
29 
30 typedef struct _TExecution_Context TExecution_Context;
31 
32 /* Define auxiliary data types for the TT interpreter. */
33 
34 typedef struct ttfMemoryDescriptor_s ttfMemoryDescriptor;
35 
36 typedef struct {
37     double a, b, c, d, tx, ty;
38 } FloatMatrix;
39 
40 typedef struct {
41     double x, y;
42 } FloatPoint;
43 
44 #if   ARCH_LOG2_SIZEOF_LONG == 2
45 typedef signed long F26Dot6;
46 #elif ARCH_LOG2_SIZEOF_INT  == 2
47 typedef signed int  F26Dot6;
48 #else
49 #error "No appropriate type for Fixed 26.6 Floats"
50 #endif
51 
52 typedef struct {
53     F26Dot6 x;
54     F26Dot6 y;
55 } F26Dot6Point;
56 
57 /* Define an abstract class for accessing memory managers from the TT interpreter. */
58 typedef struct ttfMemory_s ttfMemory;
59 struct ttfMemory_s {
60     void *(*alloc_bytes)(ttfMemory *, int size,  const char *cname);
61     void *(*alloc_struct)(ttfMemory *, const ttfMemoryDescriptor *,  const char *cname);
62     void (*free)(ttfMemory *, void *p,  const char *cname);
63 } ;
64 
65 typedef struct ttfSubGlyphUsage_s ttfSubGlyphUsage;
66 
67 /* Define a capsule for the TT interpreter. */
68 struct ttfInterpreter_s {
69     TExecution_Context *exec;
70     ttfSubGlyphUsage *usage;
71     int usage_size;
72     int usage_top;
73     int lock;
74     ttfMemory *ttf_memory;
75 };
76 
77 /* Define TT interpreter return codes. */
78 typedef enum {
79     fNoError,
80     fTableNotFound,
81     fNameNotFound,
82     fMemoryError,
83     fUnimplemented,
84     fCMapNotFound,
85     fGlyphNotFound,
86     fBadFontData,
87     fPatented,
88     fBadInstruction
89 } FontError;
90 
91 /* Define an abstract class for accessing TT data from the TT interpreter. */
92 typedef struct ttfReader_s ttfReader;
93 struct ttfReader_s {
94     bool   (*Eof)(ttfReader *);
95     void   (*Read)(ttfReader *, void *p, int n);
96     void   (*Seek)(ttfReader *, int nPos);
97     int    (*Tell)(ttfReader *);
98     bool   (*Error)(ttfReader *);
99     int    (*LoadGlyph)(ttfReader *, int nIndex, const byte **, int *);
100     void   (*ReleaseGlyph)(ttfReader *, int nIndex);
101     int    (*get_metrics)(const ttfReader *ttf, uint glyph_index, bool bVertical,
102                           short *sideBearing, unsigned short *nAdvance);
103 };
104 
105 /* Define an auxiliary structure for ttfFont. */
106 typedef struct {
107     int nPos, nLen;
108 } ttfPtrElem;
109 
110 /* Define a capsule for a TT face.
111    Diue to historical reason the name is some misleading.
112    It should be ttfFace. */
113 struct ttfFont_s {
114     ttfPtrElem t_cvt_;
115     ttfPtrElem t_fpgm;
116     ttfPtrElem t_glyf;
117     ttfPtrElem t_head;
118     ttfPtrElem t_hhea;
119     ttfPtrElem t_hmtx;
120     ttfPtrElem t_vhea;
121     ttfPtrElem t_vmtx;
122     ttfPtrElem t_loca;
123     ttfPtrElem t_maxp;
124     ttfPtrElem t_prep;
125     ttfPtrElem t_cmap;
126     unsigned short nUnitsPerEm;
127     unsigned int nFlags;
128     unsigned int nNumGlyphs;
129     unsigned int nMaxComponents;
130     unsigned int nLongMetricsVert;
131     unsigned int nLongMetricsHorz;
132     unsigned int nIndexToLocFormat;
133     bool    patented;
134     bool    design_grid;
135     TFace *face;
136     TInstance *inst;
137     TExecution_Context  *exec;
138     ttfInterpreter *tti;
139     void (*DebugRepaint)(ttfFont *);
140     int (*DebugPrint)(ttfFont *, const char *s, ...);
141     const gs_memory_t *DebugMem;
142 };
143 
144 void ttfFont__init(ttfFont *this, ttfMemory *mem,
145                    void (*DebugRepaint)(ttfFont *),
146                    int (*DebugPrint)(ttfFont *, const char *s, ...),
147                    const gs_memory_t *);
148 void ttfFont__finit(ttfFont *this);
149 FontError ttfFont__Open(ttfInterpreter *, ttfFont *, ttfReader *r,
150                         unsigned int nTTC, float w, float h,
151                         bool design_grid);
152 
153 /* Define an abstract class for exporting outlines from the TT interpreter. */
154 typedef struct ttfExport_s ttfExport;
155 struct ttfExport_s {
156     bool bPoints, bOutline;
157     void (*MoveTo)(ttfExport *, FloatPoint*);
158     void (*LineTo)(ttfExport *, FloatPoint*);
159     void (*CurveTo)(ttfExport *, FloatPoint*, FloatPoint*, FloatPoint*);
160     void (*Close)(ttfExport *);
161     void (*Point)(ttfExport *, FloatPoint*, bool bOnCurve, bool bNewPath);
162     void (*SetWidth)(ttfExport *, FloatPoint*);
163     void (*DebugPaint)(ttfExport *);
164 };
165 
166 int ttfInterpreter__obtain(ttfMemory *mem, ttfInterpreter **ptti);
167 void ttfInterpreter__release(ttfInterpreter **ptti);
168 
169 /* Define an class for outline description. */
170 typedef struct {
171     bool    bCompound;
172     int     contourCount;
173     uint    pointCount;
174     F26Dot6Point  advance;
175     F26Dot6 sideBearing;
176     F26Dot6   xMinB, yMinB, xMaxB, yMaxB;
177 } ttfGlyphOutline;
178 
179 /* Define an class for generating TT outlines. */
180 typedef struct {
181     bool bOutline;
182     bool bFirst;
183     bool bVertical;
184     int  nPointsTotal;
185     int  nContoursTotal;
186     F26Dot6 ppx, ppy;
187     ttfReader *r;
188     ttfExport *exp;
189     ttfFont *pFont;
190     ttfGlyphOutline out;
191     FloatMatrix post_transform;
192 } ttfOutliner;
193 
194 void ttfOutliner__init(ttfOutliner *, ttfFont *f, ttfReader *r, ttfExport *exp,
195                         bool bOutline, bool bFirst, bool bVertical);
196 FontError ttfOutliner__Outline(ttfOutliner *this, int glyphIndex,
197         float orig_x, float orig_y, FloatMatrix *m1);
198 void ttfOutliner__DrawGlyphOutline(ttfOutliner *this);
199 
200 #endif
201