1/*---------------------------------------------------------------------------*
2 |              PDFlib - A library for generating PDF on the fly             |
3 +---------------------------------------------------------------------------+
4 | Copyright (c) 1997-2004 Thomas Merz and PDFlib GmbH. All rights reserved. |
5 +---------------------------------------------------------------------------+
6 |                                                                           |
7 |    This software is subject to the PDFlib license. It is NOT in the       |
8 |    public domain. Extended versions and commercial licenses are           |
9 |    available, please check http://www.pdflib.com.                         |
10 |                                                                           |
11 *---------------------------------------------------------------------------*/
12
13/* $Id: p_truetype.h 14574 2005-10-29 16:27:43Z bonefish $
14 *
15 * PDFlib TrueType typedefs, structures, and enums
16 *
17 */
18
19typedef unsigned char	tt_byte;
20typedef char	tt_char;
21typedef unsigned short	tt_ushort;
22typedef short	tt_short;
23typedef unsigned int	tt_ulong;
24typedef int	tt_long;
25
26typedef long	tt_fixed;
27typedef short	tt_fword;
28typedef unsigned short	tt_ufword;
29
30#define tt_get_fixed	tt_get_long
31#define tt_get_fword	tt_get_short
32#define tt_get_ufword	tt_get_ushort
33
34#undef	ROUND
35#define ROUND(x)	(((x) < 0) ? ceil((x) - 0.5) : floor((x) + 0.5))
36
37#define TT_XKERNPAIR_CHUNKSIZE  256
38
39#define TT_NUMSTDSTRINGS  391
40
41#define TT_OFFSETTAB_SIZE  12
42
43#define TT_MINUSEDCHARS_SIZE  32
44
45typedef struct
46{
47    char	tag[5];
48    tt_ulong	checksum;
49    tt_ulong	offset;
50    tt_ulong	length;
51} tt_dirent;
52
53typedef enum
54{
55    tt_pfid_apple,
56    tt_pfid_mac,
57    tt_pfid_iso,
58    tt_pfid_win
59} tt_platform_id;
60
61typedef enum
62{
63    tt_wenc_symbol,
64    tt_wenc_text
65} tt_win_encoding_id;
66
67/* format 4 encoding table:
68*/
69typedef struct
70{
71    tt_ushort   encodingID;
72    tt_ushort	format;
73    tt_ushort	length;
74    tt_ushort	version;
75    tt_ushort	segCountX2;	/* segCount * 2		*/
76    tt_ushort	searchRange;
77    tt_ushort	entrySelector;
78    tt_ushort	rangeShift;
79    tt_ushort *	endCount;	/* [segCount]		*/
80    tt_ushort *	startCount;	/* [segCount]		*/
81    tt_short *	idDelta;	/* [segCount]		*/
82    tt_ushort *	idRangeOffs;	/* [segCount]		*/
83    int		numGlyphIds;
84    tt_ushort *	glyphIdArray;
85} tt_cmap4;
86
87/* combined data structure for format 0 and format 6 encoding tables:
88*/
89typedef struct
90{
91    tt_ushort	format;
92    tt_ushort	length;
93    tt_ushort	language;
94    tt_ushort	firstCode;
95    tt_ushort	entryCount;
96    tt_ushort	*glyphIdArray;
97} tt_cmap0_6;
98
99typedef struct
100{
101    tt_ushort	version;
102    tt_ushort	numEncTabs;
103
104    tt_cmap4	*win;
105    tt_cmap0_6	*mac;
106} tt_tab_cmap;
107
108typedef struct
109{
110    tt_fixed	version;
111    tt_fixed	fontRevision;
112    tt_ulong	checkSumAdjustment;
113    tt_ulong	magicNumber;
114    tt_ushort	flags;
115    tt_ushort	unitsPerEm;
116    tt_ulong	created[2];
117    tt_ulong	modified[2];
118    tt_fword	xMin, yMin;
119    tt_fword	xMax, yMax;
120    tt_ushort	macStyle;
121    tt_ushort	lowestRecPPEM;
122    tt_short	fontDirectionHint;
123    tt_short	indexToLocFormat;
124    tt_short	glyphDataFormat;
125} tt_tab_head;
126
127typedef struct
128{
129    tt_fixed	version;
130    tt_fword	ascender;
131    tt_fword	descender;
132    tt_fword	lineGap;
133    tt_ufword	advanceWidthMax;
134    tt_fword	minLeftSideBearing;
135    tt_fword	minRightSideBearing;
136    tt_fword	xMaxExtent;
137    tt_short	caretSlopeRise;
138    tt_short	caretSlopeRun;
139    tt_short	res1;
140    tt_short	res2;
141    tt_short	res3;
142    tt_short	res4;
143    tt_short	res5;
144    tt_short	metricDataFormat;
145    tt_ushort	numberOfHMetrics;
146} tt_tab_hhea;
147
148typedef struct
149{
150    tt_ufword	advanceWidth;
151    tt_fword	lsb;
152} tt_metric;
153
154typedef struct
155{
156    tt_metric *	metrics;
157    tt_fword *	lsbs;
158} tt_tab_hmtx;
159
160typedef struct
161{
162    tt_fixed	version;
163    tt_ushort	numGlyphs;
164    tt_ushort	maxPoints;
165    tt_ushort	maxContours;
166    tt_ushort	maxCompositePoints;
167    tt_ushort	maxCompositeContours;
168    tt_ushort	maxZones;
169    tt_ushort	maxTwilightPoints;
170    tt_ushort	maxStorage;
171    tt_ushort	maxFunctionDefs;
172    tt_ushort	maxInstructionDefs;
173    tt_ushort	maxStackElements;
174    tt_ushort	maxSizeOfInstructions;
175    tt_ushort	maxComponentElements;
176    tt_ushort	maxComponentDepth;
177} tt_tab_maxp;
178
179typedef struct
180{
181    tt_ushort   platform;
182    tt_ushort   language;
183    tt_ushort   namid;
184    tt_ushort   length;
185    tt_ushort   offset;
186} tt_nameref;
187
188typedef struct
189{
190    tt_ushort	format;
191    tt_ushort	numNameRecords;
192    tt_ushort	offsetStrings;
193    tt_nameref *namerecords;
194    char       *englishname4;
195    char       *englishname6;
196} tt_tab_name;
197
198typedef struct
199{
200    tt_ushort	version;
201    tt_short	xAvgCharWidth;
202    tt_ushort	usWeightClass;
203    tt_ushort	usWidthClass;
204    tt_ushort	fsType;			/* tt_short? */
205    tt_short	ySubscriptXSize;
206    tt_short	ySubscriptYSize;
207    tt_short	ySubscriptXOffset;
208    tt_short	ySubscriptYOffset;
209    tt_short	ySuperscriptXSize;
210    tt_short	ySuperscriptYSize;
211    tt_short	ySuperscriptXOffset;
212    tt_short	ySuperscriptYOffset;
213    tt_short	yStrikeoutSize;
214    tt_short	yStrikeoutPosition;
215    tt_ushort	sFamilyClass;		/* ttt_short? */
216    tt_byte	panose[10];
217    tt_ulong	ulUnicodeRange1;
218    tt_ulong	ulUnicodeRange2;
219    tt_ulong	ulUnicodeRange3;
220    tt_ulong	ulUnicodeRange4;
221    tt_char	achVendID[4];
222    tt_ushort	fsSelection;
223    tt_ushort	usFirstCharIndex;
224    tt_ushort	usLastCharIndex;
225
226    /* tt_ushort according to spec; obviously a spec bug.
227    */
228    tt_short	sTypoAscender;
229    tt_short	sTypoDescender;
230    tt_short	sTypoLineGap;
231
232    tt_ushort	usWinAscent;
233    tt_ushort	usWinDescent;
234
235    /* if version >= 1
236    */
237    tt_ulong	ulCodePageRange[2];
238
239    /* if version >= 2 (according to OpenType spec)
240    */
241    tt_short	sxHeight;
242    tt_short	sCapHeight;
243    tt_ushort	usDefaultChar;
244    tt_ushort	usBreakChar;
245    tt_ushort	usMaxContext;
246} tt_tab_OS_2;
247
248typedef struct
249{
250    tt_ushort  sid;
251    tt_byte   *string;
252} tt_string_tab;
253
254typedef struct
255{
256    tt_ulong  	   offset;
257    tt_ulong	   length;
258} tt_tab_CFF_;
259
260typedef struct
261{
262    tt_fixed	formatType;
263    double	italicAngle;
264    tt_fword	underlinePosition;
265    tt_fword	underlineThickness;
266    tt_ulong	isFixedPitch;
267    tt_ulong	minMemType42;
268    tt_ulong	maxMemType42;
269    tt_ulong	minMemType1;
270    tt_ulong	maxMemType1;
271
272} tt_tab_post;
273
274typedef struct
275{
276    tt_ushort   start;
277    tt_ushort   end;
278    tt_ushort   cclass;
279} tt_record_classrange;
280
281typedef struct
282{
283    tt_ushort	left;
284    tt_ushort	right;
285    tt_short	value;
286    tt_short    dominant;
287} tt_xkern_pair;
288
289typedef struct
290{
291    tt_xkern_pair *pairs;
292    int            capacity;
293    int            number;
294} tt_xkern_pair_list;
295
296typedef struct
297{
298    pdc_bool    check;          /* check for fontname */
299    pdc_bool    incore;         /* whole font in-core */
300    tt_byte *	img;		/* in-core TT font file image	*/
301    tt_byte *	end;		/* first byte above image buf	*/
302    tt_byte *	pos;		/* current "file" position	*/
303    pdc_file *	fp;
304
305    int		n_tables;
306    tt_ulong    offset;
307    tt_dirent *	dir;
308
309    /* "required" tables:
310    */
311    tt_tab_cmap *tab_cmap;
312    tt_tab_head *tab_head;
313    tt_tab_hhea *tab_hhea;
314    tt_tab_hmtx *tab_hmtx;
315    tt_tab_maxp *tab_maxp;
316    tt_tab_name *tab_name;
317    tt_tab_post *tab_post;
318    tt_tab_OS_2 *tab_OS_2;
319    tt_tab_CFF_ *tab_CFF_;
320
321    tt_ushort   *GID2Code;     /* [ttf->tab_maxp->numGlyphs] */
322                               /* glyphid: Code = Unicode */
323    int         hasGlyphNames;
324    int         hasEncoding;
325
326
327    const char *filename;       /* font file name */
328    const char *fontname;       /* font API name */
329    char *utf16fontname;        /* UTF-16-BE font name for TTC fonts */
330    int fnamelen;               /* font name length */
331    pdc_font   *font;
332} tt_file;
333
334/* TrueType table names, defined as octal codes */
335#define pdf_str_ttcf    "\164\164\143\146"
336#define pdf_str_bhed    "\142\150\145\144"
337#define pdf_str_CFF_    "\103\106\106\040"
338#define pdf_str_cvt_	"\143\166\164\040"
339#define pdf_str_cmap	"\143\155\141\160"
340#define pdf_str_fpgm	"\146\160\147\155"
341#define pdf_str_glyf	"\147\154\171\146"
342#define pdf_str_GPOS    "\107\120\117\123"
343#define pdf_str_head	"\150\145\141\144"
344#define pdf_str_hhea	"\150\150\145\141"
345#define pdf_str_hmtx	"\150\155\164\170"
346#define pdf_str_kern    "\153\145\162\156"
347#define pdf_str_loca	"\154\157\143\141"
348#define pdf_str_maxp	"\155\141\170\160"
349#define pdf_str_name	"\156\141\155\145"
350#define pdf_str_OS_2	"\117\123\057\062"	/* OS/2 */
351#define pdf_str_post	"\160\157\163\164"
352#define pdf_str_prep	"\160\162\145\160"
353
354/* Windows code page numbers */
355#define TT_CP1252     0 /* Latin 1 */
356#define TT_CP1250     1 /* Latin 2: Eastern Europe */
357#define TT_CP1251     2 /* Cyrillic */
358#define TT_CP1253     3 /* Greek */
359#define TT_CP1254     4 /* Turkish */
360#define TT_CP1255     5 /* Hebrew */
361#define TT_CP1256     6 /* Arabic */
362#define TT_CP1257     7 /* Windows Baltic */
363#define TT_CP1258     8 /* Vietnamese */
364#define TT_CP874     16 /* Thai */
365#define TT_CP932     17 /* JIS/Japan */
366#define TT_CP936     18 /* Chinese: Simplified chars--PRC and Singapore */
367#define TT_CP949     19 /* Korean Wansung */
368#define TT_CP950     20 /* Chinese: Traditional chars--Taiwan and Hong Kong */
369#define TT_CP1361    21 /* Korean Johab */
370#define TT_CP869     48 /* IBM Greek */
371#define TT_CP866     49 /* MS-DOS Russian */
372#define TT_CP865     50 /* MS-DOS Nordic */
373#define TT_CP864     51 /* Arabic */
374#define TT_CP863     52 /* MS-DOS Canadian French */
375#define TT_CP862     53 /* Hebrew */
376#define TT_CP861     54 /* MS-DOS Icelandic */
377#define TT_CP860     55 /* MS-DOS Portuguese */
378#define TT_CP857     56 /* IBM Turkish */
379#define TT_CP855     57 /* IBM Cyrillic; primarily Russian */
380#define TT_CP852     58 /* Latin 2 */
381#define TT_CP775     59 /* MS-DOS Baltic */
382#define TT_CP737     60 /* Greek; former 437 G */
383#define TT_CP708     61 /* Arabic; ASMO 708 */
384#define TT_CP850     62 /* WE/Latin 1 */
385#define TT_CP437     63 /* US */
386
387/* Composite font flags. */
388/* See Reference of OpenType: glyf - Glyf Data */
389#define ARGS_ARE_WORDS       0x001
390#define ARGS_ARE_XY_VALUES   0x002
391#define ROUND_XY_TO_GRID     0x004
392#define WE_HAVE_A_SCALE      0x008
393/* reserved                  0x010 */
394#define MORE_COMPONENTS      0x020
395#define WE_HAVE_AN_XY_SCALE  0x040
396#define WE_HAVE_A_2X2        0x080
397#define WE_HAVE_INSTR        0x100
398#define USE_MY_METRICS       0x200
399
400
401/* Functions */
402
403#define PDF_TT2PDF(v) (int) ROUND(v * 1000.0f / ttf->tab_head->unitsPerEm)
404
405#define TT_ASSERT(p, ttf, cond)         \
406        ((cond) ? (void) 0 : tt_assert(p, ttf))
407
408#define TT_IOCHECK(p, ttf, cond)        \
409        ((cond) ? (void) 0 : tt_error(p, ttf))
410
411tt_file  *pdf_init_tt(PDF *p);
412pdc_bool  pdf_read_offset_tab(PDF *p, tt_file *ttf);
413pdc_bool  pdf_test_tt_font(tt_byte *img, tt_ulong *n_fonts);
414void      pdf_cleanup_tt(PDF *p, tt_file *ttf);
415
416int       tt_tag2idx(tt_file *ttf, char *tag);
417void      tt_get_tab_maxp(PDF *p, tt_file *ttf);
418void      tt_get_tab_head(PDF *p, tt_file *ttf);
419void      tt_get_tab_cmap(PDF *p, tt_file *ttf);
420pdc_bool  tt_get_tab_CFF_(PDF *p, tt_file *ttf);
421void      tt_get_tab_OS_2(PDF *p, tt_file *ttf);
422
423
424
425void	  tt_assert(PDF *p, tt_file *ttf);
426void	  tt_error(PDF *p, tt_file *ttf);
427void      tt_seek(PDF *p, tt_file *ttf, long offset);
428void      tt_read(PDF *p, tt_file *ttf, void *buf, unsigned int nbytes);
429long      tt_tell(PDF *p, tt_file *ttf);
430tt_ushort tt_get_ushort(PDF *p, tt_file *ttf);
431tt_short  tt_get_short(PDF *p, tt_file *ttf);
432tt_ulong  tt_get_ulong3(PDF *p, tt_file *ttf);
433tt_ulong  tt_get_ulong(PDF *p, tt_file *ttf);
434tt_long   tt_get_long(PDF *p, tt_file *ttf);
435tt_ulong  tt_get_offset(PDF *p, tt_file *ttf, tt_byte offsize);
436
437
438