1 /* sfnt.h
2 
3    Copyright 2002 by Jin-Hwan Cho and Shunsaku Hirata,
4    the dvipdfmx project team <dvipdfmx@project.ktug.or.kr>
5    Copyright 2006-2008 Taco Hoekwater <taco@luatex.org>
6 
7    This file is part of LuaTeX.
8 
9    LuaTeX is free software; you can redistribute it and/or modify it under
10    the terms of the GNU General Public License as published by the Free
11    Software Foundation; either version 2 of the License, or (at your
12    option) any later version.
13 
14    LuaTeX 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 General Public License along
20    with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
21 
22 
23 #ifndef _SFNT_H_
24 #  define _SFNT_H_
25 
26 #  if  HAVE_CONFIG_H
27 #    include <w2c/config.h>
28 #  endif                        /* HAVE_CONFIG_H_ */
29 
30 #  ifndef pdfTeX
31 #    include "mfileio.h"
32 #    include "numbers.h"
33 #    include "pdfobj.h"
34 #  endif
35 
36 #  ifdef XETEX
37 #    include "ft2build.h"
38 #    include FT_FREETYPE_H
39 #    include FT_TRUETYPE_TABLES_H
40 #  endif
41 
42 /* Data Types as described in Apple's TTRefMan */
43 typedef unsigned char BYTE;
44 typedef signed char ICHAR;
45 typedef unsigned short USHORT;
46 typedef signed short SHORT;
47 typedef unsigned long ULONG;
48 typedef signed long LONG;
49 typedef unsigned long Fixed;    /* 16.16-bit signed fixed-point number */
50 typedef short FWord;
51 typedef unsigned short uFWord;
52 typedef short F2Dot14;          /* 16-bit signed fixed number with the low
53                                    14 bits representing fraction. */
54 
55 struct sfnt_table {
56     /* table header */
57     char tag[4];
58     ULONG check_sum;
59     ULONG offset;
60     ULONG length;
61     char *data;                 /* table data */
62 };
63 
64 #  define SFNT_TABLE_REQUIRED (1 << 0)
65 
66 struct sfnt_table_directory {
67     ULONG version;              /* Fixed for Win */
68     USHORT num_tables;
69     USHORT search_range;
70     USHORT entry_selector;
71     USHORT range_shift;
72     USHORT num_kept_tables;     /* number of kept tables */
73     char *flags;                /* keep or omit */
74     struct sfnt_table *tables;
75 };
76 
77 /* sfnt resource */
78 #  define SFNT_TYPE_TRUETYPE   (1 << 0)
79 #  define SFNT_TYPE_OPENTYPE   (1 << 1)
80 #  define SFNT_TYPE_POSTSCRIPT (1 << 2)
81 #  define SFNT_TYPE_TTC        (1 << 4)
82 
83 typedef struct {
84     int type;
85     struct sfnt_table_directory *directory;
86 #  ifdef XETEX
87     FT_Face ft_face;
88     long loc;
89 #  elif defined(pdfTeX)
90     BYTE *buffer;
91     long buflen;
92     long loc;
93 #  else
94     FILE *stream;
95 #  endif
96 } sfnt;
97 
98 /* Convert sfnt "fixed" type to double */
99 #  define fixed(a) ((double)((a)%0x10000L)/(double)(0x10000L) + \
100  (a)/0x10000L - (((a)/0x10000L > 0x7fffL) ? 0x10000L : 0))
101 
102 #  ifdef XETEX
103 UNSIGNED_BYTE ft_unsigned_byte(sfnt * f);
104 SIGNED_BYTE ft_signed_byte(sfnt * f);
105 UNSIGNED_PAIR ft_unsigned_pair(sfnt * f);
106 SIGNED_PAIR ft_signed_pair(sfnt * f);
107 UNSIGNED_QUAD ft_unsigned_quad(sfnt * f);
108 unsigned long ft_read(unsigned char *buf, unsigned long len, sfnt * f);
109 
110 #    define sfnt_get_byte(s)   ((BYTE)   ft_unsigned_byte(s))
111 #    define sfnt_get_char(s)   ((ICHAR)   ft_signed_byte  (s))
112 #    define sfnt_get_ushort(s) ((USHORT) ft_unsigned_pair(s))
113 #    define sfnt_get_short(s)  ((SHORT)  ft_signed_pair  (s))
114 #    define sfnt_get_ulong(s)  ((ULONG)  ft_unsigned_quad(s))
115 #    define sfnt_get_long(s)   ((LONG)   ft_signed_quad  (s))
116 
117 #    define sfnt_seek_set(s,o) (s)->loc = (o)
118 #    define sfnt_read(b,l,s)   ft_read((b), (l), (s))
119 #  elif defined(pdfTeX)
120 BYTE get_unsigned_byte(sfnt * f);
121 ICHAR get_signed_byte(sfnt * f);
122 USHORT get_unsigned_pair(sfnt * f);
123 SHORT get_signed_pair(sfnt * f);
124 ULONG get_unsigned_quad(sfnt * f);
125 int do_sfnt_read(unsigned char *dest, int len, sfnt * f);
126 
127 #    define sfnt_get_byte(s)   ((BYTE)   get_unsigned_byte(s))
128 #    define sfnt_get_char(s)   ((ICHAR)   get_signed_byte  (s))
129 #    define sfnt_get_ushort(s) ((USHORT) get_unsigned_pair(s))
130 #    define sfnt_get_short(s)  ((SHORT)  get_signed_pair  (s))
131 #    define sfnt_get_ulong(s)  ((ULONG)  get_unsigned_quad(s))
132 #    define sfnt_get_long(s)   ((LONG)   get_signed_quad  (s))
133 
134 #    define sfnt_seek_set(s,o) (s)->loc = (o)
135 #    define sfnt_read(b,l,s)   do_sfnt_read((b), (l), (s))
136 #  else
137 /* get_***_*** from numbers.h */
138 #    define sfnt_get_byte(s)   ((BYTE)   get_unsigned_byte((s)->stream))
139 #    define sfnt_get_char(s)   ((ICHAR)  get_signed_byte  ((s)->stream))
140 #    define sfnt_get_ushort(s) ((USHORT) get_unsigned_pair((s)->stream))
141 #    define sfnt_get_short(s)  ((SHORT)  get_signed_pair  ((s)->stream))
142 #    define sfnt_get_ulong(s)  ((ULONG)  get_unsigned_quad((s)->stream))
143 #    define sfnt_get_long(s)   ((LONG)   get_signed_quad  ((s)->stream))
144 
145 #    define sfnt_seek_set(s,o)   seek_absolute((s)->stream, (o))
146 #    define sfnt_read(b,l,s)     fread((b), 1, (l), (s)->stream)
147 #  endif
148 
149 extern int put_big_endian(void *s, LONG q, int n);
150 
151 #  define sfnt_put_ushort(s,v) put_big_endian((s), v, 2);
152 #  define sfnt_put_short(s,v)  put_big_endian((s), v, 2);
153 #  define sfnt_put_ulong(s,v)  put_big_endian((s), v, 4);
154 #  define sfnt_put_long(s,v)   put_big_endian((s), v, 4);
155 
156 #  ifdef XETEX
157 extern sfnt *sfnt_open(FT_Face face, int accept_types);
158 #  elif defined(pdfTeX)
159 extern sfnt *sfnt_open(unsigned char *buffer, int buflen);
160 #  else
161 extern sfnt *sfnt_open(FILE * fp);
162 #  endif
163 extern void sfnt_close(sfnt * sfont);
164 
165 /* table directory */
166 extern int sfnt_read_table_directory(sfnt * sfont, ULONG offset);
167 extern ULONG sfnt_find_table_len(sfnt * sfont, const char *tag);
168 extern ULONG sfnt_find_table_pos(sfnt * sfont, const char *tag);
169 extern ULONG sfnt_locate_table(sfnt * sfont, const char *tag);
170 
171 extern void sfnt_set_table(sfnt * sfont,
172                            const char *tag, void *data, ULONG length);
173 extern int sfnt_require_table(sfnt * sfont, const char *tag, int must_exist);
174 
175 #  ifdef pdfTeX
176 typedef struct {
177     ULONG length;
178     BYTE *data;
179 } pdf_obj;
180 
181 #    define ASSERT(a) assert(a)
182 #    define RELEASE(a) free(a)
183 #    define NEW(a,b) xmalloc((unsigned)((unsigned)(a)*sizeof(b)))
184 #    define RENEW(a,b,c) xrealloc(a, (unsigned)((unsigned)(b)*sizeof(c)))
185 #    define TT_ERROR luatex_fail
186 #    define WARN printf
187 
188 
189 #  endif
190 
191 extern pdf_obj *sfnt_create_FontFile_stream(sfnt * sfont);
192 
193 #endif                          /* _SFNT_H_ */
194