36#ifdef FT_CONFIG_OPTION_SVG
42#define SVG_TABLE_HEADER_SIZE (10U)
43#define SVG_DOCUMENT_RECORD_SIZE (12U)
44#define SVG_DOCUMENT_LIST_MINIMUM_SIZE (2U + SVG_DOCUMENT_RECORD_SIZE)
45#define SVG_MINIMUM_SIZE (SVG_TABLE_HEADER_SIZE + \
46 SVG_DOCUMENT_LIST_MINIMUM_SIZE)
50#define MAX_SVG_SIZE ( 1 << 26 )
55 FT_UShort num_entries;
57 FT_Byte* svg_doc_list;
72#define FT_COMPONENT ttsvg
75 FT_LOCAL_DEF( FT_Error )
76 tt_face_load_svg( TT_Face face,
80 FT_Memory memory = face->root.memory;
83 FT_Byte* table = NULL;
86 FT_ULong offsetToSVGDocumentList;
89 error = face->goto_table( face, TTAG_SVG, stream, &table_size );
93 if ( table_size < SVG_MINIMUM_SIZE )
96 if ( FT_FRAME_EXTRACT( table_size, table ) )
104 svg->version = FT_NEXT_USHORT( p );
105 offsetToSVGDocumentList = FT_NEXT_ULONG( p );
107 if ( offsetToSVGDocumentList < SVG_TABLE_HEADER_SIZE ||
108 offsetToSVGDocumentList > table_size -
109 SVG_DOCUMENT_LIST_MINIMUM_SIZE )
112 svg->svg_doc_list = (FT_Byte*)( table + offsetToSVGDocumentList );
114 p = svg->svg_doc_list;
115 svg->num_entries = FT_NEXT_USHORT( p );
117 FT_TRACE3((
"version: %d\n", svg->version ));
118 FT_TRACE3((
"number of entries: %d\n", svg->num_entries ));
120 if ( offsetToSVGDocumentList + 2U +
121 svg->num_entries * SVG_DOCUMENT_RECORD_SIZE > table_size )
125 svg->table_size = table_size;
128 face->root.face_flags |= FT_FACE_FLAG_SVG;
133 error = FT_THROW( Invalid_Table );
136 FT_FRAME_RELEASE( table );
145 tt_face_free_svg( TT_Face face )
147 FT_Memory memory = face->root.memory;
148 FT_Stream stream = face->root.stream;
150 Svg* svg = (Svg*)face->svg;
155 FT_FRAME_RELEASE( svg->table );
161 typedef struct Svg_doc_
163 FT_UShort start_glyph_id;
164 FT_UShort end_glyph_id;
173 extract_svg_doc( FT_Byte* stream )
178 doc.start_glyph_id = FT_NEXT_USHORT( stream );
179 doc.end_glyph_id = FT_NEXT_USHORT( stream );
181 doc.offset = FT_NEXT_ULONG( stream );
182 doc.length = FT_NEXT_ULONG( stream );
189 compare_svg_doc( Svg_doc doc,
190 FT_UInt glyph_index )
192 if ( glyph_index < doc.start_glyph_id )
194 else if ( glyph_index > doc.end_glyph_id )
202 find_doc( FT_Byte* document_records,
203 FT_UShort num_entries,
205 FT_ULong *doc_offset,
206 FT_ULong *doc_length,
207 FT_UShort *start_glyph,
208 FT_UShort *end_glyph )
213 Svg_doc mid_doc = { 0, 0, 0, 0 };
216 FT_Bool found = FALSE;
219 FT_UInt start_index = 0;
220 FT_UInt end_index = num_entries - 1;
225 if ( num_entries == 0 )
227 error = FT_THROW( Invalid_Table );
231 start_doc = extract_svg_doc( document_records + start_index * 12 );
232 end_doc = extract_svg_doc( document_records + end_index * 12 );
234 if ( ( compare_svg_doc( start_doc, glyph_index ) == -1 ) ||
235 ( compare_svg_doc( end_doc, glyph_index ) == 1 ) )
237 error = FT_THROW( Invalid_Glyph_Index );
241 while ( start_index <= end_index )
243 i = ( start_index + end_index ) / 2;
244 mid_doc = extract_svg_doc( document_records + i * 12 );
245 comp_res = compare_svg_doc( mid_doc, glyph_index );
250 start_doc = extract_svg_doc( document_records + start_index * 4 );
252 else if ( comp_res == -1 )
255 end_doc = extract_svg_doc( document_records + end_index * 4 );
267 FT_TRACE5((
"SVG glyph not found\n" ));
268 error = FT_THROW( Invalid_Glyph_Index );
272 *doc_offset = mid_doc.offset;
273 *doc_length = mid_doc.length;
275 *start_glyph = mid_doc.start_glyph_id;
276 *end_glyph = mid_doc.end_glyph_id;
285 FT_LOCAL_DEF( FT_Error )
286 tt_face_load_svg_doc( FT_GlyphSlot glyph,
287 FT_UInt glyph_index )
289 FT_Error error = FT_Err_Ok;
290 TT_Face face = (TT_Face)glyph->face;
291 FT_Memory memory = face->root.memory;
292 Svg* svg = (Svg*)face->svg;
300 FT_UShort doc_start_glyph_id;
301 FT_UShort doc_end_glyph_id;
303 FT_SVG_Document svg_document = (FT_SVG_Document)glyph->other;
306 FT_ASSERT( !( svg == NULL ) );
308 doc_list = svg->svg_doc_list;
310 error = find_doc( doc_list + 2, svg->num_entries, glyph_index,
311 &doc_offset, &doc_length,
312 &doc_start_glyph_id, &doc_end_glyph_id );
313 if ( error != FT_Err_Ok )
316 doc_limit = svg->table_size -
317 (FT_ULong)( doc_list - (FT_Byte*)svg->table );
318 if ( doc_offset > doc_limit ||
319 doc_length > doc_limit - doc_offset )
321 error = FT_THROW( Invalid_Table );
325 doc = doc_list + doc_offset;
327 if ( doc_length > 6 &&
332#ifdef FT_CONFIG_OPTION_USE_ZLIB
334 FT_ULong uncomp_size;
335 FT_Byte* uncomp_buffer = NULL;
346 FT_TRACE4((
"SVG document is GZIP compressed\n" ));
347 uncomp_size = (FT_ULong)doc[doc_length - 1] << 24 |
348 (FT_ULong)doc[doc_length - 2] << 16 |
349 (FT_ULong)doc[doc_length - 3] << 8 |
350 (FT_ULong)doc[doc_length - 4];
352 if ( uncomp_size >= MAX_SVG_SIZE )
354 FT_ERROR((
"Uncompressed SVG document too large.\n" ));
355 error = FT_THROW( Array_Too_Large );
359 if ( FT_QALLOC( uncomp_buffer, uncomp_size ) )
362 error = FT_Gzip_Uncompress( memory,
369 FT_FREE( uncomp_buffer );
370 error = FT_THROW( Invalid_Table );
374 glyph->internal->flags |= FT_GLYPH_OWN_GZIP_SVG;
377 doc_length = uncomp_size;
381 error = FT_THROW( Unimplemented_Feature );
387 svg_document->svg_document = doc;
388 svg_document->svg_document_length = doc_length;
390 svg_document->metrics = glyph->face->size->metrics;
391 svg_document->units_per_EM = glyph->face->units_per_EM;
393 svg_document->start_glyph_id = doc_start_glyph_id;
394 svg_document->end_glyph_id = doc_end_glyph_id;
396 svg_document->transform.xx = 0x10000;
397 svg_document->transform.xy = 0;
398 svg_document->transform.yx = 0;
399 svg_document->transform.yy = 0x10000;
401 svg_document->delta.x = 0;
402 svg_document->delta.y = 0;
404 FT_TRACE5((
"start_glyph_id: %d\n", doc_start_glyph_id ));
405 FT_TRACE5((
"end_glyph_id: %d\n", doc_end_glyph_id ));
406 FT_TRACE5((
"svg_document:\n" ));
407 FT_TRACE5((
" %.*s\n", (FT_UInt)doc_length, doc ));
409 glyph->other = svg_document;
418 typedef int tt_svg_dummy_;