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
,
88 if ( stream
->read( stream
, pos
, 0, 0 ) )
89 error
= FT_Err_Invalid_Stream_Operation
;
91 else if ( pos
> stream
->size
)
92 error
= FT_Err_Invalid_Stream_Operation
;
94 LOG(( "ftglue:stream:seek(%ld) -> %d\n", pos
, error
));
99 FTGLUE_APIDEF( FT_Error
)
100 ftglue_stream_frame_enter( FT_Stream stream
,
103 FT_Error error
= FT_Err_Ok
;
108 /* allocate the frame in memory */
109 FT_Memory memory
= stream
->memory
;
112 if ( QALLOC( stream
->base
, count
) )
116 read_bytes
= stream
->read( stream
, stream
->pos
,
117 stream
->base
, count
);
118 if ( read_bytes
< count
)
120 FREE( stream
->base
);
121 error
= FT_Err_Invalid_Stream_Operation
;
123 stream
->cursor
= stream
->base
;
124 stream
->limit
= stream
->cursor
+ count
;
125 stream
->pos
+= read_bytes
;
129 /* check current and new position */
130 if ( stream
->pos
>= stream
->size
||
131 stream
->pos
+ count
> stream
->size
)
133 error
= FT_Err_Invalid_Stream_Operation
;
138 stream
->cursor
= stream
->base
+ stream
->pos
;
139 stream
->limit
= stream
->cursor
+ count
;
140 stream
->pos
+= count
;
144 LOG(( "ftglue:stream:frame_enter(%ld) -> %d\n", count
, error
));
149 FTGLUE_APIDEF( void )
150 ftglue_stream_frame_exit( FT_Stream stream
)
154 FT_Memory memory
= stream
->memory
;
156 FREE( stream
->base
);
161 LOG(( "ftglue:stream:frame_exit()\n" ));
165 FTGLUE_APIDEF( FT_Error
)
166 ftglue_face_goto_table( FT_Face face
,
172 LOG(( "ftglue_face_goto_table( %p, %c%c%c%c, %p )\n",
174 (int)((the_tag
>> 24) & 0xFF),
175 (int)((the_tag
>> 16) & 0xFF),
176 (int)((the_tag
>> 8) & 0xFF),
177 (int)(the_tag
& 0xFF),
180 if ( !FT_IS_SFNT(face
) )
182 LOG(( "not a SFNT face !!\n" ));
183 error
= FT_Err_Invalid_Face_Handle
;
187 /* parse the directory table directly, without using
188 * FreeType's built-in data structures
190 FT_ULong offset
= 0, sig
;
193 if ( FILE_Seek( 0 ) || ACCESS_Frame( 4 ) )
200 if ( sig
== FT_MAKE_TAG( 't', 't', 'c', 'f' ) )
202 /* deal with TrueType collections */
204 LOG(( ">> This is a TrueType Collection\n" ));
206 if ( FILE_Seek( 12 + face
->face_index
*4 ) ||
210 offset
= GET_ULong();
215 LOG(( "TrueType offset = %ld\n", offset
));
217 if ( FILE_Seek( offset
+4 ) ||
221 count
= GET_UShort();
225 if ( FILE_Seek( offset
+12 ) ||
226 ACCESS_Frame( count
*16 ) )
229 for ( nn
= 0; nn
< count
; nn
++ )
231 FT_ULong tag
= GET_ULong();
232 FT_ULong checksum
= GET_ULong();
233 FT_ULong start
= GET_ULong();
234 FT_ULong size
= GET_ULong();
239 if ( tag
== the_tag
)
241 LOG(( "TrueType table (start: %ld) (size: %ld)\n", start
, size
));
242 error
= ftglue_stream_seek( stream
, start
);
246 error
= FT_Err_Table_Missing
;
253 LOG(( "TrueType error=%d\n", error
));
260 #include "fcaliastail.h"