1 /* ftglue.c: Glue code for compiling the OpenType code from
2 * FreeType 1 using only the public API of FreeType 2
4 * By David Turner, The FreeType Project (www.freetype.org)
6 * This code is explicitely put in the public domain
8 * See ftglue.h for more information.
15 #define LOG(x) ftglue_log x
18 ftglue_log( const char* format
, ... )
22 va_start( ap
, format
);
23 vfprintf( stderr
, format
, ap
);
28 #define LOG(x) do {} while (0)
31 /* only used internally */
33 ftglue_qalloc( FT_Memory memory
,
38 FT_Pointer block
= NULL
;
42 block
= memory
->alloc( memory
, size
);
44 error
= FT_Err_Out_Of_Memory
;
51 #undef QALLOC /* just in case */
52 #define QALLOC(ptr,size) ( (ptr) = ftglue_qalloc( memory, (size), &error ), error != 0 )
57 ftglue_free( memory, _ptr ); \
63 FTGLUE_APIDEF( FT_Pointer
)
64 ftglue_alloc( FT_Memory memory
,
69 FT_Pointer block
= NULL
;
73 block
= memory
->alloc( memory
, size
);
75 error
= FT_Err_Out_Of_Memory
;
77 memset( (char*)block
, 0, (size_t)size
);
85 FTGLUE_APIDEF( FT_Pointer
)
86 ftglue_realloc( FT_Memory memory
,
92 FT_Pointer block2
= NULL
;
95 if ( old_size
== 0 || block
== NULL
)
97 block2
= ftglue_alloc( memory
, new_size
, &error
);
99 else if ( new_size
== 0 )
101 ftglue_free( memory
, block
);
105 block2
= memory
->realloc( memory
, old_size
, new_size
, block
);
106 if ( block2
== NULL
)
107 error
= FT_Err_Out_Of_Memory
;
108 else if ( new_size
> old_size
)
109 memset( (char*)block2
+ old_size
, 0, (size_t)(new_size
- old_size
) );
120 FTGLUE_APIDEF( void )
121 ftglue_free( FT_Memory memory
,
125 memory
->free( memory
, block
);
129 FTGLUE_APIDEF( FT_Long
)
130 ftglue_stream_pos( FT_Stream stream
)
132 LOG(( "ftglue:stream:pos() -> %ld\n", stream
->pos
));
137 FTGLUE_APIDEF( FT_Error
)
138 ftglue_stream_seek( FT_Stream stream
,
146 if ( stream
->read( stream
, pos
, 0, 0 ) )
147 error
= FT_Err_Invalid_Stream_Operation
;
149 else if ( pos
> stream
->size
)
150 error
= FT_Err_Invalid_Stream_Operation
;
152 LOG(( "ftglue:stream:seek(%ld) -> %d\n", pos
, error
));
157 FTGLUE_APIDEF( FT_Error
)
158 ftglue_stream_frame_enter( FT_Stream stream
,
161 FT_Error error
= FT_Err_Ok
;
166 /* allocate the frame in memory */
167 FT_Memory memory
= stream
->memory
;
170 if ( QALLOC( stream
->base
, count
) )
174 read_bytes
= stream
->read( stream
, stream
->pos
,
175 stream
->base
, count
);
176 if ( read_bytes
< count
)
178 FREE( stream
->base
);
179 error
= FT_Err_Invalid_Stream_Operation
;
181 stream
->cursor
= stream
->base
;
182 stream
->limit
= stream
->cursor
+ count
;
183 stream
->pos
+= read_bytes
;
187 /* check current and new position */
188 if ( stream
->pos
>= stream
->size
||
189 stream
->pos
+ count
> stream
->size
)
191 error
= FT_Err_Invalid_Stream_Operation
;
196 stream
->cursor
= stream
->base
+ stream
->pos
;
197 stream
->limit
= stream
->cursor
+ count
;
198 stream
->pos
+= count
;
202 LOG(( "ftglue:stream:frame_enter(%ld) -> %d\n", count
, error
));
207 FTGLUE_APIDEF( void )
208 ftglue_stream_frame_exit( FT_Stream stream
)
212 FT_Memory memory
= stream
->memory
;
214 FREE( stream
->base
);
219 LOG(( "ftglue:stream:frame_exit()\n" ));
223 FTGLUE_APIDEF( FT_Error
)
224 ftglue_face_goto_table( FT_Face face
,
230 LOG(( "ftglue_face_goto_table( %p, %c%c%c%c, %p )\n",
232 (int)((the_tag
>> 24) & 0xFF),
233 (int)((the_tag
>> 16) & 0xFF),
234 (int)((the_tag
>> 8) & 0xFF),
235 (int)(the_tag
& 0xFF),
238 if ( !FT_IS_SFNT(face
) )
240 LOG(( "not a SFNT face !!\n" ));
241 error
= FT_Err_Invalid_Face_Handle
;
245 /* parse the directory table directly, without using
246 * FreeType's built-in data structures
251 if ( face
->num_faces
> 1 )
253 /* deal with TrueType collections */
255 LOG(( ">> This is a TrueType Collection\n" ));
257 if ( FILE_Seek( 12 + face
->face_index
*4 ) ||
261 offset
= GET_ULong();
266 LOG(( "TrueType offset = %ld\n", offset
));
268 if ( FILE_Seek( offset
+4 ) ||
272 count
= GET_UShort();
276 if ( FILE_Seek( offset
+12 ) ||
277 ACCESS_Frame( count
*16 ) )
280 for ( nn
= 0; nn
< count
; nn
++ )
282 FT_ULong tag
= GET_ULong();
283 FT_ULong checksum
= GET_ULong();
284 FT_ULong start
= GET_ULong();
285 FT_ULong size
= GET_ULong();
290 if ( tag
== the_tag
)
292 LOG(( "TrueType table (start: %ld) (size: %ld)\n", start
, size
));
293 error
= ftglue_stream_seek( stream
, start
);
297 error
= TT_Err_Table_Missing
;
304 LOG(( "TrueType error=%d\n", error
));
311 #include "fcaliastail.h"