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 ); \
64 ftglue_free( FT_Memory memory,
68 memory->free( memory, block );
71 FTGLUE_APIDEF( FT_Long )
72 ftglue_stream_pos( FT_Stream stream )
74 LOG(( "ftglue:stream:pos() -> %ld\n", stream->pos ));
79 FTGLUE_APIDEF( FT_Error )
80 ftglue_stream_seek( FT_Stream stream,
87 if ( stream->read( stream, pos, 0, 0 ) )
88 error = FT_Err_Invalid_Stream_Operation;
90 else if ( pos < 0 || (FT_ULong) pos > stream->size )
91 error = FT_Err_Invalid_Stream_Operation;
95 LOG(( "ftglue:stream:seek(%ld) -> %d\n", pos, error ));
100 FTGLUE_APIDEF( FT_Error )
101 ftglue_stream_frame_enter( FT_Stream stream,
104 FT_Error error = FT_Err_Ok;
109 /* allocate the frame in memory */
110 FT_Memory memory = stream->memory;
113 if ( QALLOC( stream->base, count ) )
117 read_bytes = stream->read( stream, stream->pos,
118 stream->base, count );
119 if ( read_bytes < count )
121 FREE( stream->base );
122 error = FT_Err_Invalid_Stream_Operation;
124 stream->cursor = stream->base;
125 stream->limit = stream->cursor + count;
126 stream->pos += read_bytes;
130 /* check current and new position */
131 if ( stream->pos >= stream->size ||
132 stream->pos + count > stream->size )
134 error = FT_Err_Invalid_Stream_Operation;
139 stream->cursor = stream->base + stream->pos;
140 stream->limit = stream->cursor + count;
141 stream->pos += count;
145 LOG(( "ftglue:stream:frame_enter(%ld) -> %d\n", count, error ));
150 FTGLUE_APIDEF( void )
151 ftglue_stream_frame_exit( FT_Stream stream )
155 FT_Memory memory = stream->memory;
157 FREE( stream->base );
162 LOG(( "ftglue:stream:frame_exit()\n" ));
166 FTGLUE_APIDEF( FT_Error )
167 ftglue_face_goto_table( FT_Face face,
173 LOG(( "ftglue_face_goto_table( %p, %c%c%c%c, %p )\n",
175 (int)((the_tag >> 24) & 0xFF),
176 (int)((the_tag >> 16) & 0xFF),
177 (int)((the_tag >> 8) & 0xFF),
178 (int)(the_tag & 0xFF),
181 if ( !FT_IS_SFNT(face) )
183 LOG(( "not a SFNT face !!\n" ));
184 error = FT_Err_Invalid_Face_Handle;
188 /* parse the directory table directly, without using
189 * FreeType's built-in data structures
191 FT_ULong offset = 0, sig;
194 if ( FILE_Seek( 0 ) || ACCESS_Frame( 4 ) )
201 if ( sig == FT_MAKE_TAG( 't', 't', 'c', 'f' ) )
203 /* deal with TrueType collections */
205 LOG(( ">> This is a TrueType Collection\n" ));
207 if ( FILE_Seek( 12 + face->face_index*4 ) ||
211 offset = GET_ULong();
216 LOG(( "TrueType offset = %ld\n", offset ));
218 if ( FILE_Seek( offset+4 ) ||
222 count = GET_UShort();
226 if ( FILE_Seek( offset+12 ) ||
227 ACCESS_Frame( count*16 ) )
230 for ( nn = 0; nn < count; nn++ )
232 FT_ULong tag = GET_ULong();
233 FT_ULong checksum = GET_ULong();
234 FT_ULong start = GET_ULong();
235 FT_ULong size = GET_ULong();
240 if ( tag == the_tag )
242 LOG(( "TrueType table (start: %ld) (size: %ld)\n", start, size ));
243 error = ftglue_stream_seek( stream, start );
247 error = FT_Err_Table_Missing;
254 LOG(( "TrueType error=%d\n", error ));
260 #include "fcaliastail.h"