Debian Codegraph @

src/ftglue.c

raw
1 /* ftglue.c: Glue code for compiling the OpenType code from
2 * FreeType 1 using only the public API of FreeType 2
3 *
4 * By David Turner, The FreeType Project (www.freetype.org)
5 *
6 * This code is explicitely put in the public domain
7 *
8 * See ftglue.h for more information.
9 */
10
11 #include "ftglue.h"
12
13 #if 0
14 # include <stdio.h>
15 # define LOG(x) ftglue_log x
16
17 static void
18 ftglue_log( const char* format, ... )
19 {
20 va_list ap;
21
22 va_start( ap, format );
23 vfprintf( stderr, format, ap );
24 va_end( ap );
25 }
26
27 #else
28 # define LOG(x) \
29 do { \
30 } while (0)
31 #endif
32
33 /* only used internally */
34 static FT_Pointer
35 ftglue_qalloc (FT_Memory memory,
36 FT_ULong size,
37 FT_Error *perror)
38 {
39 FT_Error error = 0;
40 FT_Pointer block = NULL;
41
42 if (size > 0) {
43 block = memory->alloc (memory, size);
44 if (!block)
45 error = FT_Err_Out_Of_Memory;
46 }
47
48 *perror = error;
49 return block;
50 }
51
52 #undef QALLOC /* just in case */
53 #define QALLOC(ptr, size) ((ptr) = ftglue_qalloc (memory, (size), &error), error != 0)
54 #define FREE(_ptr) \
55 do { \
56 if ((_ptr)) { \
57 ftglue_free (memory, _ptr); \
58 _ptr = NULL; \
59 } \
60 } while (0)
61
62 static void
63 ftglue_free (FT_Memory memory,
64 FT_Pointer block)
65 {
66 if (block)
67 memory->free (memory, block);
68 }
69
70 FTGLUE_APIDEF (FT_Long)
71 ftglue_stream_pos (FT_Stream stream)
72 {
73 LOG (("ftglue:stream:pos() -> %ld\n", stream->pos));
74 return stream->pos;
75 }
76
77 FTGLUE_APIDEF (FT_Error)
78 ftglue_stream_seek (FT_Stream stream,
79 FT_Long pos)
80 {
81 FT_Error error = 0;
82
83 if (stream->read) {
84 if (stream->read (stream, pos, 0, 0))
85 error = FT_Err_Invalid_Stream_Operation;
86 } else if (pos < 0 || (FT_ULong)pos > stream->size)
87 error = FT_Err_Invalid_Stream_Operation;
88
89 if (!error)
90 stream->pos = pos;
91 LOG (("ftglue:stream:seek(%ld) -> %d\n", pos, error));
92 return error;
93 }
94
95 FTGLUE_APIDEF (FT_Error)
96 ftglue_stream_frame_enter (FT_Stream stream,
97 FT_ULong count)
98 {
99 FT_Error error = FT_Err_Ok;
100 FT_ULong read_bytes;
101
102 if (stream->read) {
103 /* allocate the frame in memory */
104 FT_Memory memory = stream->memory;
105
106 if (QALLOC (stream->base, count))
107 goto Exit;
108
109 /* read it */
110 read_bytes = stream->read (stream, stream->pos,
111 stream->base, count);
112 if (read_bytes < count) {
113 FREE (stream->base);
114 error = FT_Err_Invalid_Stream_Operation;
115 }
116 stream->cursor = stream->base;
117 stream->limit = stream->cursor + count;
118 stream->pos += read_bytes;
119 } else {
120 /* check current and new position */
121 if (stream->pos >= stream->size ||
122 stream->pos + count > stream->size) {
123 error = FT_Err_Invalid_Stream_Operation;
124 goto Exit;
125 }
126
127 /* set cursor */
128 stream->cursor = stream->base + stream->pos;
129 stream->limit = stream->cursor + count;
130 stream->pos += count;
131 }
132
133 Exit:
134 LOG (("ftglue:stream:frame_enter(%ld) -> %d\n", count, error));
135 return error;
136 }
137
138 FTGLUE_APIDEF (void)
139 ftglue_stream_frame_exit (FT_Stream stream)
140 {
141 if (stream->read) {
142 FT_Memory memory = stream->memory;
143
144 FREE (stream->base);
145 }
146 stream->cursor = 0;
147 stream->limit = 0;
148
149 LOG (("ftglue:stream:frame_exit()\n"));
150 }
151
152 FTGLUE_APIDEF (FT_Error)
153 ftglue_face_goto_table (FT_Face face,
154 FT_ULong the_tag,
155 FT_Stream stream,
156 FT_ULong *table_size)
157 {
158 FT_Error error;
159
160 LOG (("ftglue_face_goto_table( %p, %c%c%c%c, %p )\n",
161 face,
162 (int)((the_tag >> 24) & 0xFF),
163 (int)((the_tag >> 16) & 0xFF),
164 (int)((the_tag >> 8) & 0xFF),
165 (int)(the_tag & 0xFF),
166 stream));
167
168 if (!FT_IS_SFNT (face)) {
169 LOG (("not a SFNT face !!\n"));
170 error = FT_Err_Invalid_Face_Handle;
171 } else {
172 /* parse the directory table directly, without using
173 * FreeType's built-in data structures
174 */
175 FT_ULong offset = 0, sig;
176 FT_UInt count, nn;
177
178 if (FILE_Seek (0) || ACCESS_Frame (4))
179 goto Exit;
180
181 sig = GET_Tag4();
182
183 FORGET_Frame();
184
185 if (sig == FT_MAKE_TAG ('t', 't', 'c', 'f')) {
186 /* deal with TrueType collections */
187
188 LOG ((">> This is a TrueType Collection\n"));
189
190 if (FILE_Seek (12 + face->face_index * 4) ||
191 ACCESS_Frame (4))
192 goto Exit;
193
194 offset = GET_ULong();
195
196 FORGET_Frame();
197 }
198
199 LOG (("TrueType offset = %ld\n", offset));
200
201 if (FILE_Seek (offset + 4) ||
202 ACCESS_Frame (2))
203 goto Exit;
204
205 count = GET_UShort();
206
207 FORGET_Frame();
208
209 if (FILE_Seek (offset + 12) ||
210 ACCESS_Frame (count * 16))
211 goto Exit;
212
213 for (nn = 0; nn < count; nn++) {
214 FT_ULong tag = GET_ULong();
215 FT_ULong checksum FC_UNUSED = GET_ULong();
216 FT_ULong start = GET_ULong();
217 FT_ULong size FC_UNUSED = GET_ULong();
218
219 if (tag == the_tag) {
220 LOG (("TrueType table (start: %ld) (size: %ld)\n", start, size));
221 error = ftglue_stream_seek (stream, start);
222 if (table_size)
223 *table_size = size;
224 goto FoundIt;
225 }
226 }
227 error = FT_Err_Table_Missing;
228
229 FoundIt:
230 FORGET_Frame();
231 }
232
233 Exit:
234 LOG (("TrueType error=%d\n", error));
235
236 return error;
237 }
238
239 #undef QALLOC
240 #include "fcaliastail.h"
241 #undef __ftglue__