trivial bug fixes
[swftools.git] / lib / ttf.h
1 /* ttf.h
2    Parser and writer for truetype font files.
3
4    Part of the swftools package.
5    
6    Copyright (c) 2010 Matthias Kramm <kramm@quiss.org> 
7  
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21
22 #ifndef __ttf_h__
23 #define __ttf_h__
24
25 #include "types.h"
26
27 typedef struct _ttf_table {
28     U32 id;
29     struct _ttf_table*prev;
30     struct _ttf_table*next;
31
32     U8*data;
33     int len;
34     int memsize;
35 } ttf_table_t;
36
37 typedef struct _table_maxp {
38     U16 maxPoints;
39     U16 maxContours;
40     U16 maxComponentPoints;
41     U16 maxComponentContours;
42     U16 maxZones;
43     U16 maxTwilightPoints;
44     U16 maxStorage;
45     U16 maxFunctionDefs;
46     U16 maxInstructionDefs;
47     U16 maxStackElements;
48     U16 maxSizeOfInstructions;
49     U16 maxComponentElements;
50     U16 maxComponentDepth;
51 } table_maxp_t;
52
53 typedef struct _table_os2 {
54     S16 xAvgCharWidth;
55     U16 usWeightClass;
56     U16 usWidthClass;
57     U16 ySubscriptXSize;
58     U16 ySubscriptYSize;
59     U16 ySubscriptXOffset;
60     U16 ySubscriptYOffset;
61     U16 ySuperscriptXSize;
62     U16 ySuperscriptYSize;
63     U16 ySuperscriptXOffset;
64     U16 ySuperscriptYOffset;
65     U16 yStrikeoutSize;
66     U16 yStrikeoutPosition;
67     U16 sFamilyClass;
68     U8 panose_FamilyType;
69     U8 panose_SerifStyle;
70     U8 panose_Weight;
71     U8 panose_Proportion;
72     U8 panose_Contrast;
73     U8 panose_StrokeVariation;
74     U8 panose_ArmStyle;
75     U8 panose_Letterform;
76     U8 panose_Midline;
77     U8 panose_XHeight;
78     U32 ulCharRange[4];
79
80     U16 fsSelection;
81     U16 fsFirstCharIndex;
82     U16 fsLastCharIndex;
83
84     S16 sTypoAscender;
85     S16 sTypoDescender;
86     S16 sTypoLineGap;
87     U16 usWinAscent;
88     U16 usWinDescent;
89
90     /* for version >= 0x0001 */
91     U32 ulCodePageRange1;
92     U32 ulCodePageRange2;
93     
94     /* for version >= 0x0002 */
95     S16 sxHeight;
96     S16 sCapHeight;
97     U16 usDefaultChar;
98     U16 usBreakChar;
99     U16 usMaxContext;
100 } table_os2_t;
101
102 typedef struct _table_hea
103 {
104     U16 advanceWidthMax;
105     S16 minLeftSideBearing;
106     S16 minRightSideBearing;
107     S16 xMaxExtent;
108     S16 caretSlopeRise;
109     S16 caretSlopeRun;
110     S16 caretOffset;
111 } table_hea_t;
112
113 #define GLYPH_ON_CURVE 0x01
114 #define GLYPH_CONTOUR_START 0x40
115 #define GLYPH_CONTOUR_END 0x80
116
117 typedef U32 unicode_t;
118
119 typedef struct _ttfpoint {
120     int x,y;
121     U8 flags;
122 } ttfpoint_t;
123 typedef struct _ttfglyph {
124     U16 advance;
125     U16 bearing;
126     S16 xmin,ymin,xmax,ymax;
127     int code_size;
128     U8*code;
129     int num_points;
130     ttfpoint_t*points;
131 } ttfglyph_t;
132
133 typedef struct _table_head {
134     U16 flags;
135     U16 units_per_em;
136     S16 xmin,ymin,xmax,ymax;
137     U16 macStyle;
138     U16 lowest_readable_size;
139     S16 dir_hint;
140 } table_head_t;
141
142 typedef struct _table_post {
143     U16 italic_angle;
144     U16 underline_position;
145     U16 underline_thickness;
146 } table_post_t;
147
148 typedef struct _table_cvt {
149     S16*values;
150     int num;
151 } table_cvt_t;
152
153 typedef struct _table_gasp {
154     int num;
155     struct {
156         U16 size;
157         U16 behaviour;
158     } *records;
159 } table_gasp_t;
160
161 typedef struct _table_code {
162     U8*code;
163     int size;
164 } table_code_t;
165
166 typedef struct _ttf {
167     char*family_name;     /* nameId 1 */
168     char*subfamily_name;  /* nameId 2 */
169     char*font_uid;        /* nameId 3 */
170     char*full_name;       /* nameId 4 */
171     char*version_string;  /* nameId 5 */
172     char*postscript_name; /* nameId 6 */
173
174     ttf_table_t*tables;
175
176     table_head_t*head;
177     table_maxp_t*maxp;
178     table_os2_t*os2;
179     table_hea_t*hea;
180     table_post_t*post;
181     table_cvt_t*cvt;
182     table_gasp_t*gasp;
183     table_code_t*prep;
184     table_code_t*fpgm;
185
186     U16 flags;
187     char is_vertical;
188
189     S16 ascent;
190     S16 descent; // ymin, *not* negative ymin
191     S16 lineGap;
192
193     int num_glyphs;
194     ttfglyph_t*glyphs;
195     
196     int unicode_size;
197     unicode_t*unicode;
198
199     U32 version;
200 } ttf_t;
201
202
203 ttf_t*ttf_new();
204 void ttf_reduce(ttf_t*ttf);
205 ttf_t*ttf_load(void*data, int length);
206 ttf_table_t*ttf_addtable(ttf_t*ttf, U32 tag);
207 void ttf_create_truetype_tables(ttf_t*ttf);
208 void ttf_dump(ttf_t*ttf);
209 void ttf_destroy(ttf_t*ttf);
210 void ttf_save(ttf_t*ttf, const char*filename);
211 void ttf_save_eot(ttf_t*ttf, const char*filename);
212
213 #endif