1 /***************************************************************************/
5 /* The FreeType private base classes (body). */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, */
9 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
11 /* This file is part of the FreeType project, and may only be used, */
12 /* modified, and distributed under the terms of the FreeType project */
13 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
14 /* this file you indicate that you have read the license and */
15 /* understand and accept it fully. */
17 /***************************************************************************/
23 #include FT_INTERNAL_VALIDATE_H
24 #include FT_INTERNAL_OBJECTS_H
25 #include FT_INTERNAL_DEBUG_H
26 #include FT_INTERNAL_RFORK_H
27 #include FT_INTERNAL_STREAM_H
28 #include FT_INTERNAL_SFNT_H /* for SFNT_Load_Table_Func */
29 #include FT_TRUETYPE_TABLES_H
30 #include FT_TRUETYPE_TAGS_H
31 #include FT_TRUETYPE_IDS_H
33 #include FT_SERVICE_SFNT_H
34 #include FT_SERVICE_POSTSCRIPT_NAME_H
35 #include FT_SERVICE_GLYPH_DICT_H
36 #include FT_SERVICE_TT_CMAP_H
37 #include FT_SERVICE_KERNING_H
38 #include FT_SERVICE_TRUETYPE_ENGINE_H
40 #ifdef FT_CONFIG_OPTION_MAC_FONTS
44 #define GRID_FIT_METRICS
47 FT_BASE_DEF( FT_Pointer )
48 ft_service_list_lookup( FT_ServiceDesc service_descriptors,
49 const char* service_id )
51 FT_Pointer result = NULL;
52 FT_ServiceDesc desc = service_descriptors;
55 if ( desc && service_id )
57 for ( ; desc->serv_id != NULL; desc++ )
59 if ( ft_strcmp( desc->serv_id, service_id ) == 0 )
61 result = (FT_Pointer)desc->serv_data;
72 ft_validator_init( FT_Validator valid,
75 FT_ValidationLevel level )
80 valid->error = FT_Err_Ok;
85 ft_validator_run( FT_Validator valid )
87 /* This function doesn't work! None should call it. */
95 ft_validator_error( FT_Validator valid,
98 /* since the cast below also disables the compiler's */
99 /* type check, we introduce a dummy variable, which */
100 /* will be optimized away */
101 volatile ft_jmp_buf* jump_buffer = &valid->jump_buffer;
104 valid->error = error;
106 /* throw away volatileness; use `jump_buffer' or the */
107 /* compiler may warn about an unused local variable */
108 ft_longjmp( *(ft_jmp_buf*) jump_buffer, 1 );
112 /*************************************************************************/
113 /*************************************************************************/
114 /*************************************************************************/
117 /**** S T R E A M ****/
120 /*************************************************************************/
121 /*************************************************************************/
122 /*************************************************************************/
125 /* create a new input stream from an FT_Open_Args structure */
127 FT_BASE_DEF( FT_Error )
128 FT_Stream_New( FT_Library library,
129 const FT_Open_Args* args,
140 return FT_Err_Invalid_Library_Handle;
143 return FT_Err_Invalid_Argument;
145 memory = library->memory;
147 if ( FT_NEW( stream ) )
150 stream->memory = memory;
152 if ( args->flags & FT_OPEN_MEMORY )
154 /* create a memory-based stream */
155 FT_Stream_OpenMemory( stream,
156 (const FT_Byte*)args->memory_base,
159 else if ( args->flags & FT_OPEN_PATHNAME )
161 /* create a normal system stream */
162 error = FT_Stream_Open( stream, args->pathname );
163 stream->pathname.pointer = args->pathname;
165 else if ( ( args->flags & FT_OPEN_STREAM ) && args->stream )
167 /* use an existing, user-provided stream */
169 /* in this case, we do not need to allocate a new stream object */
170 /* since the caller is responsible for closing it himself */
172 stream = args->stream;
175 error = FT_Err_Invalid_Argument;
180 stream->memory = memory; /* just to be certain */
190 FT_Stream_Free( FT_Stream stream,
195 FT_Memory memory = stream->memory;
198 FT_Stream_Close( stream );
206 /*************************************************************************/
208 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
209 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
210 /* messages during execution. */
213 #define FT_COMPONENT trace_objs
216 /*************************************************************************/
217 /*************************************************************************/
218 /*************************************************************************/
221 /**** FACE, SIZE & GLYPH SLOT OBJECTS ****/
224 /*************************************************************************/
225 /*************************************************************************/
226 /*************************************************************************/
230 ft_glyphslot_init( FT_GlyphSlot slot )
232 FT_Driver driver = slot->face->driver;
233 FT_Driver_Class clazz = driver->clazz;
234 FT_Memory memory = driver->root.memory;
235 FT_Error error = FT_Err_Ok;
236 FT_Slot_Internal internal = NULL;
239 slot->library = driver->root.library;
241 if ( FT_NEW( internal ) )
244 slot->internal = internal;
246 if ( FT_DRIVER_USES_OUTLINES( driver ) )
247 error = FT_GlyphLoader_New( memory, &internal->loader );
249 if ( !error && clazz->init_slot )
250 error = clazz->init_slot( slot );
258 ft_glyphslot_free_bitmap( FT_GlyphSlot slot )
260 if ( slot->internal && ( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) )
262 FT_Memory memory = FT_FACE_MEMORY( slot->face );
265 FT_FREE( slot->bitmap.buffer );
266 slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
270 /* assume that the bitmap buffer was stolen or not */
271 /* allocated from the heap */
272 slot->bitmap.buffer = NULL;
278 ft_glyphslot_set_bitmap( FT_GlyphSlot slot,
281 ft_glyphslot_free_bitmap( slot );
283 slot->bitmap.buffer = buffer;
285 FT_ASSERT( (slot->internal->flags & FT_GLYPH_OWN_BITMAP) == 0 );
289 FT_BASE_DEF( FT_Error )
290 ft_glyphslot_alloc_bitmap( FT_GlyphSlot slot,
293 FT_Memory memory = FT_FACE_MEMORY( slot->face );
297 if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
298 FT_FREE( slot->bitmap.buffer );
300 slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
302 (void)FT_ALLOC( slot->bitmap.buffer, size );
308 ft_glyphslot_clear( FT_GlyphSlot slot )
310 /* free bitmap if needed */
311 ft_glyphslot_free_bitmap( slot );
313 /* clear all public fields in the glyph slot */
314 FT_ZERO( &slot->metrics );
315 FT_ZERO( &slot->outline );
317 slot->bitmap.width = 0;
318 slot->bitmap.rows = 0;
319 slot->bitmap.pitch = 0;
320 slot->bitmap.pixel_mode = 0;
321 /* `slot->bitmap.buffer' has been handled by ft_glyphslot_free_bitmap */
323 slot->bitmap_left = 0;
324 slot->bitmap_top = 0;
325 slot->num_subglyphs = 0;
327 slot->control_data = 0;
328 slot->control_len = 0;
330 slot->format = FT_GLYPH_FORMAT_NONE;
332 slot->linearHoriAdvance = 0;
333 slot->linearVertAdvance = 0;
340 ft_glyphslot_done( FT_GlyphSlot slot )
342 FT_Driver driver = slot->face->driver;
343 FT_Driver_Class clazz = driver->clazz;
344 FT_Memory memory = driver->root.memory;
347 if ( clazz->done_slot )
348 clazz->done_slot( slot );
350 /* free bitmap buffer if needed */
351 ft_glyphslot_free_bitmap( slot );
353 /* slot->internal might be NULL in out-of-memory situations */
354 if ( slot->internal )
356 /* free glyph loader */
357 if ( FT_DRIVER_USES_OUTLINES( driver ) )
359 FT_GlyphLoader_Done( slot->internal->loader );
360 slot->internal->loader = 0;
363 FT_FREE( slot->internal );
368 /* documentation is in ftobjs.h */
370 FT_BASE_DEF( FT_Error )
371 FT_New_GlyphSlot( FT_Face face,
372 FT_GlyphSlot *aslot )
376 FT_Driver_Class clazz;
381 if ( !face || !face->driver )
382 return FT_Err_Invalid_Argument;
384 driver = face->driver;
385 clazz = driver->clazz;
386 memory = driver->root.memory;
388 FT_TRACE4(( "FT_New_GlyphSlot: Creating new slot object\n" ));
389 if ( !FT_ALLOC( slot, clazz->slot_object_size ) )
393 error = ft_glyphslot_init( slot );
396 ft_glyphslot_done( slot );
401 slot->next = face->glyph;
412 FT_TRACE4(( "FT_New_GlyphSlot: Return %d\n", error ));
417 /* documentation is in ftobjs.h */
420 FT_Done_GlyphSlot( FT_GlyphSlot slot )
424 FT_Driver driver = slot->face->driver;
425 FT_Memory memory = driver->root.memory;
430 /* Remove slot from its parent face's list */
432 cur = slot->face->glyph;
439 slot->face->glyph = cur->next;
441 prev->next = cur->next;
443 ft_glyphslot_done( slot );
454 /* documentation is in freetype.h */
456 FT_EXPORT_DEF( void )
457 FT_Set_Transform( FT_Face face,
461 FT_Face_Internal internal;
467 internal = face->internal;
469 internal->transform_flags = 0;
473 internal->transform_matrix.xx = 0x10000L;
474 internal->transform_matrix.xy = 0;
475 internal->transform_matrix.yx = 0;
476 internal->transform_matrix.yy = 0x10000L;
477 matrix = &internal->transform_matrix;
480 internal->transform_matrix = *matrix;
482 /* set transform_flags bit flag 0 if `matrix' isn't the identity */
483 if ( ( matrix->xy | matrix->yx ) ||
484 matrix->xx != 0x10000L ||
485 matrix->yy != 0x10000L )
486 internal->transform_flags |= 1;
490 internal->transform_delta.x = 0;
491 internal->transform_delta.y = 0;
492 delta = &internal->transform_delta;
495 internal->transform_delta = *delta;
497 /* set transform_flags bit flag 1 if `delta' isn't the null vector */
498 if ( delta->x | delta->y )
499 internal->transform_flags |= 2;
504 ft_lookup_glyph_renderer( FT_GlyphSlot slot );
507 #ifdef GRID_FIT_METRICS
509 ft_glyphslot_grid_fit_metrics( FT_GlyphSlot slot,
512 FT_Glyph_Metrics* metrics = &slot->metrics;
513 FT_Pos right, bottom;
518 metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
519 metrics->horiBearingY = FT_PIX_CEIL ( metrics->horiBearingY );
521 right = FT_PIX_CEIL( metrics->vertBearingX + metrics->width );
522 bottom = FT_PIX_CEIL( metrics->vertBearingY + metrics->height );
524 metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
525 metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
527 metrics->width = right - metrics->vertBearingX;
528 metrics->height = bottom - metrics->vertBearingY;
532 metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
533 metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
535 right = FT_PIX_CEIL ( metrics->horiBearingX + metrics->width );
536 bottom = FT_PIX_FLOOR( metrics->horiBearingY - metrics->height );
538 metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
539 metrics->horiBearingY = FT_PIX_CEIL ( metrics->horiBearingY );
541 metrics->width = right - metrics->horiBearingX;
542 metrics->height = metrics->horiBearingY - bottom;
545 metrics->horiAdvance = FT_PIX_ROUND( metrics->horiAdvance );
546 metrics->vertAdvance = FT_PIX_ROUND( metrics->vertAdvance );
548 #endif /* GRID_FIT_METRICS */
551 /* documentation is in freetype.h */
553 FT_EXPORT_DEF( FT_Error )
554 FT_Load_Glyph( FT_Face face,
556 FT_Int32 load_flags )
562 FT_Bool autohint = FALSE;
566 if ( !face || !face->size || !face->glyph )
567 return FT_Err_Invalid_Face_Handle;
569 /* The validity test for `glyph_index' is performed by the */
573 ft_glyphslot_clear( slot );
575 driver = face->driver;
576 library = driver->root.library;
577 hinter = library->auto_hinter;
579 /* resolve load flags dependencies */
581 if ( load_flags & FT_LOAD_NO_RECURSE )
582 load_flags |= FT_LOAD_NO_SCALE |
583 FT_LOAD_IGNORE_TRANSFORM;
585 if ( load_flags & FT_LOAD_NO_SCALE )
587 load_flags |= FT_LOAD_NO_HINTING |
590 load_flags &= ~FT_LOAD_RENDER;
594 * Determine whether we need to auto-hint or not.
595 * The general rules are:
597 * - Do only auto-hinting if we have a hinter module, a scalable font
598 * format dealing with outlines, and no transforms except simple
599 * slants and/or rotations by integer multiples of 90 degrees.
601 * - Then, auto-hint if FT_LOAD_FORCE_AUTOHINT is set or if we don't
602 * have a native font hinter.
604 * - Otherwise, auto-hint for LIGHT hinting mode.
606 * - Exception: The font is `tricky' and requires the native hinter to
611 !( load_flags & FT_LOAD_NO_HINTING ) &&
612 !( load_flags & FT_LOAD_NO_AUTOHINT ) &&
613 FT_DRIVER_IS_SCALABLE( driver ) &&
614 FT_DRIVER_USES_OUTLINES( driver ) &&
615 !FT_IS_TRICKY( face ) &&
616 ( ( face->internal->transform_matrix.yx == 0 &&
617 face->internal->transform_matrix.xx != 0 ) ||
618 ( face->internal->transform_matrix.xx == 0 &&
619 face->internal->transform_matrix.yx != 0 ) ) )
621 if ( ( load_flags & FT_LOAD_FORCE_AUTOHINT ) ||
622 !FT_DRIVER_HAS_HINTER( driver ) )
626 FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags );
629 if ( mode == FT_RENDER_MODE_LIGHT ||
630 face->internal->ignore_unpatented_hinter )
637 FT_AutoHinter_Service hinting;
640 /* try to load embedded bitmaps first if available */
642 /* XXX: This is really a temporary hack that should disappear */
643 /* promptly with FreeType 2.1! */
645 if ( FT_HAS_FIXED_SIZES( face ) &&
646 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
648 error = driver->clazz->load_glyph( slot, face->size,
650 load_flags | FT_LOAD_SBITS_ONLY );
652 if ( !error && slot->format == FT_GLYPH_FORMAT_BITMAP )
657 FT_Face_Internal internal = face->internal;
658 FT_Int transform_flags = internal->transform_flags;
661 /* since the auto-hinter calls FT_Load_Glyph by itself, */
662 /* make sure that glyphs aren't transformed */
663 internal->transform_flags = 0;
665 /* load auto-hinted outline */
666 hinting = (FT_AutoHinter_Service)hinter->clazz->module_interface;
668 error = hinting->load_glyph( (FT_AutoHinter)hinter,
670 glyph_index, load_flags );
672 internal->transform_flags = transform_flags;
677 error = driver->clazz->load_glyph( slot,
684 if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
686 /* check that the loaded outline is correct */
687 error = FT_Outline_Check( &slot->outline );
691 #ifdef GRID_FIT_METRICS
692 if ( !( load_flags & FT_LOAD_NO_HINTING ) )
693 ft_glyphslot_grid_fit_metrics( slot,
694 FT_BOOL( load_flags & FT_LOAD_VERTICAL_LAYOUT ) );
700 /* compute the advance */
701 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
704 slot->advance.y = slot->metrics.vertAdvance;
708 slot->advance.x = slot->metrics.horiAdvance;
712 /* compute the linear advance in 16.16 pixels */
713 if ( ( load_flags & FT_LOAD_LINEAR_DESIGN ) == 0 &&
714 ( FT_IS_SCALABLE( face ) ) )
716 FT_Size_Metrics* metrics = &face->size->metrics;
720 slot->linearHoriAdvance = FT_MulDiv( slot->linearHoriAdvance,
721 metrics->x_scale, 64 );
723 slot->linearVertAdvance = FT_MulDiv( slot->linearVertAdvance,
724 metrics->y_scale, 64 );
727 if ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) == 0 )
729 FT_Face_Internal internal = face->internal;
732 /* now, transform the glyph image if needed */
733 if ( internal->transform_flags )
736 FT_Renderer renderer = ft_lookup_glyph_renderer( slot );
740 error = renderer->clazz->transform_glyph(
742 &internal->transform_matrix,
743 &internal->transform_delta );
744 else if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
746 /* apply `standard' transformation if no renderer is available */
747 if ( &internal->transform_matrix )
748 FT_Outline_Transform( &slot->outline,
749 &internal->transform_matrix );
751 if ( &internal->transform_delta )
752 FT_Outline_Translate( &slot->outline,
753 internal->transform_delta.x,
754 internal->transform_delta.y );
757 /* transform advance */
758 FT_Vector_Transform( &slot->advance, &internal->transform_matrix );
762 FT_TRACE5(( " x advance: %d\n" , slot->advance.x ));
763 FT_TRACE5(( " y advance: %d\n" , slot->advance.y ));
765 FT_TRACE5(( " linear x advance: %d\n" , slot->linearHoriAdvance ));
766 FT_TRACE5(( " linear y advance: %d\n" , slot->linearVertAdvance ));
768 /* do we need to render the image now? */
770 slot->format != FT_GLYPH_FORMAT_BITMAP &&
771 slot->format != FT_GLYPH_FORMAT_COMPOSITE &&
772 load_flags & FT_LOAD_RENDER )
774 FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags );
777 if ( mode == FT_RENDER_MODE_NORMAL &&
778 (load_flags & FT_LOAD_MONOCHROME ) )
779 mode = FT_RENDER_MODE_MONO;
781 error = FT_Render_Glyph( slot, mode );
789 /* documentation is in freetype.h */
791 FT_EXPORT_DEF( FT_Error )
792 FT_Load_Char( FT_Face face,
794 FT_Int32 load_flags )
800 return FT_Err_Invalid_Face_Handle;
802 glyph_index = (FT_UInt)char_code;
804 glyph_index = FT_Get_Char_Index( face, char_code );
806 return FT_Load_Glyph( face, glyph_index, load_flags );
810 /* destructor for sizes list */
812 destroy_size( FT_Memory memory,
816 /* finalize client-specific data */
817 if ( size->generic.finalizer )
818 size->generic.finalizer( size );
820 /* finalize format-specific stuff */
821 if ( driver->clazz->done_size )
822 driver->clazz->done_size( size );
824 FT_FREE( size->internal );
830 ft_cmap_done_internal( FT_CMap cmap );
834 destroy_charmaps( FT_Face face,
843 for ( n = 0; n < face->num_charmaps; n++ )
845 FT_CMap cmap = FT_CMAP( face->charmaps[n] );
848 ft_cmap_done_internal( cmap );
850 face->charmaps[n] = NULL;
853 FT_FREE( face->charmaps );
854 face->num_charmaps = 0;
858 /* destructor for faces list */
860 destroy_face( FT_Memory memory,
864 FT_Driver_Class clazz = driver->clazz;
867 /* discard auto-hinting data */
868 if ( face->autohint.finalizer )
869 face->autohint.finalizer( face->autohint.data );
871 /* Discard glyph slots for this face. */
872 /* Beware! FT_Done_GlyphSlot() changes the field `face->glyph' */
873 while ( face->glyph )
874 FT_Done_GlyphSlot( face->glyph );
876 /* discard all sizes for this face */
877 FT_List_Finalize( &face->sizes_list,
878 (FT_List_Destructor)destroy_size,
883 /* now discard client data */
884 if ( face->generic.finalizer )
885 face->generic.finalizer( face );
887 /* discard charmaps */
888 destroy_charmaps( face, memory );
890 /* finalize format-specific stuff */
891 if ( clazz->done_face )
892 clazz->done_face( face );
894 /* close the stream for this face if needed */
897 ( face->face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
902 if ( face->internal )
904 FT_FREE( face->internal );
911 Destroy_Driver( FT_Driver driver )
913 FT_List_Finalize( &driver->faces_list,
914 (FT_List_Destructor)destroy_face,
918 /* check whether we need to drop the driver's glyph loader */
919 if ( FT_DRIVER_USES_OUTLINES( driver ) )
920 FT_GlyphLoader_Done( driver->glyph_loader );
924 /*************************************************************************/
927 /* find_unicode_charmap */
930 /* This function finds a Unicode charmap, if there is one. */
931 /* And if there is more than one, it tries to favour the more */
932 /* extensive one, i.e., one that supports UCS-4 against those which */
933 /* are limited to the BMP (said UCS-2 encoding.) */
935 /* This function is called from open_face() (just below), and also */
936 /* from FT_Select_Charmap( ..., FT_ENCODING_UNICODE ). */
939 find_unicode_charmap( FT_Face face )
945 /* caller should have already checked that `face' is valid */
948 first = face->charmaps;
951 return FT_Err_Invalid_CharMap_Handle;
954 * The original TrueType specification(s) only specified charmap
955 * formats that are capable of mapping 8 or 16 bit character codes to
958 * However, recent updates to the Apple and OpenType specifications
959 * introduced new formats that are capable of mapping 32-bit character
960 * codes as well. And these are already used on some fonts, mainly to
961 * map non-BMP Asian ideographs as defined in Unicode.
963 * For compatibility purposes, these fonts generally come with
964 * *several* Unicode charmaps:
966 * - One of them in the "old" 16-bit format, that cannot access
967 * all glyphs in the font.
969 * - Another one in the "new" 32-bit format, that can access all
972 * This function has been written to always favor a 32-bit charmap
973 * when found. Otherwise, a 16-bit one is returned when found.
976 /* Since the `interesting' table, with IDs (3,10), is normally the */
977 /* last one, we loop backwards. This loses with type1 fonts with */
978 /* non-BMP characters (<.0001%), this wins with .ttf with non-BMP */
979 /* chars (.01% ?), and this is the same about 99.99% of the time! */
981 cur = first + face->num_charmaps; /* points after the last one */
983 for ( ; --cur >= first; )
985 if ( cur[0]->encoding == FT_ENCODING_UNICODE )
987 /* XXX If some new encodings to represent UCS-4 are added, */
988 /* they should be added here. */
989 if ( ( cur[0]->platform_id == TT_PLATFORM_MICROSOFT &&
990 cur[0]->encoding_id == TT_MS_ID_UCS_4 ) ||
991 ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
992 cur[0]->encoding_id == TT_APPLE_ID_UNICODE_32 ) )
994 #ifdef FT_MAX_CHARMAP_CACHEABLE
995 if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
997 FT_ERROR(( "find_unicode_charmap: UCS-4 cmap is found "
998 "at too late position (%d)\n", cur - first ));
1002 face->charmap = cur[0];
1008 /* We do not have any UCS-4 charmap. */
1009 /* Do the loop again and search for UCS-2 charmaps. */
1010 cur = first + face->num_charmaps;
1012 for ( ; --cur >= first; )
1014 if ( cur[0]->encoding == FT_ENCODING_UNICODE )
1016 #ifdef FT_MAX_CHARMAP_CACHEABLE
1017 if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1019 FT_ERROR(( "find_unicode_charmap: UCS-2 cmap is found "
1020 "at too late position (%d)\n", cur - first ));
1024 face->charmap = cur[0];
1029 return FT_Err_Invalid_CharMap_Handle;
1033 /*************************************************************************/
1036 /* find_variant_selector_charmap */
1039 /* This function finds the variant selector charmap, if there is one. */
1040 /* There can only be one (platform=0, specific=5, format=14). */
1043 find_variant_selector_charmap( FT_Face face )
1050 /* caller should have already checked that `face' is valid */
1053 first = face->charmaps;
1058 end = first + face->num_charmaps; /* points after the last one */
1060 for ( cur = first; cur < end; ++cur )
1062 if ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
1063 cur[0]->encoding_id == TT_APPLE_ID_VARIANT_SELECTOR &&
1064 FT_Get_CMap_Format( cur[0] ) == 14 )
1065 #ifdef FT_MAX_CHARMAP_CACHEABLE
1066 if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1068 FT_ERROR(( "find_unicode_charmap: UVS cmap is found "
1069 "at too late position (%d)\n", cur - first ));
1080 /*************************************************************************/
1086 /* This function does some work for FT_Open_Face(). */
1089 open_face( FT_Driver driver,
1093 FT_Parameter* params,
1097 FT_Driver_Class clazz;
1099 FT_Error error, error2;
1100 FT_Face_Internal internal = NULL;
1103 clazz = driver->clazz;
1104 memory = driver->root.memory;
1106 /* allocate the face object and perform basic initialization */
1107 if ( FT_ALLOC( face, clazz->face_object_size ) )
1110 if ( FT_NEW( internal ) )
1113 face->internal = internal;
1115 face->driver = driver;
1116 face->memory = memory;
1117 face->stream = stream;
1119 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1124 face->internal->incremental_interface = 0;
1125 for ( i = 0; i < num_params && !face->internal->incremental_interface;
1127 if ( params[i].tag == FT_PARAM_TAG_INCREMENTAL )
1128 face->internal->incremental_interface =
1129 (FT_Incremental_Interface)params[i].data;
1133 if ( clazz->init_face )
1134 error = clazz->init_face( stream,
1142 /* select Unicode charmap by default */
1143 error2 = find_unicode_charmap( face );
1145 /* if no Unicode charmap can be found, FT_Err_Invalid_CharMap_Handle */
1148 /* no error should happen, but we want to play safe */
1149 if ( error2 && error2 != FT_Err_Invalid_CharMap_Handle )
1160 destroy_charmaps( face, memory );
1161 if ( clazz->done_face )
1162 clazz->done_face( face );
1163 FT_FREE( internal );
1172 /* there's a Mac-specific extended implementation of FT_New_Face() */
1173 /* in src/base/ftmac.c */
1175 #if !defined( FT_MACINTOSH ) || defined( DARWIN_NO_CARBON )
1177 /* documentation is in freetype.h */
1179 FT_EXPORT_DEF( FT_Error )
1180 FT_New_Face( FT_Library library,
1181 const char* pathname,
1188 /* test for valid `library' and `aface' delayed to FT_Open_Face() */
1190 return FT_Err_Invalid_Argument;
1192 args.flags = FT_OPEN_PATHNAME;
1193 args.pathname = (char*)pathname;
1196 return FT_Open_Face( library, &args, face_index, aface );
1199 #endif /* defined( FT_MACINTOSH ) && !defined( DARWIN_NO_CARBON ) */
1202 /* documentation is in freetype.h */
1204 FT_EXPORT_DEF( FT_Error )
1205 FT_New_Memory_Face( FT_Library library,
1206 const FT_Byte* file_base,
1214 /* test for valid `library' and `face' delayed to FT_Open_Face() */
1216 return FT_Err_Invalid_Argument;
1218 args.flags = FT_OPEN_MEMORY;
1219 args.memory_base = file_base;
1220 args.memory_size = file_size;
1223 return FT_Open_Face( library, &args, face_index, aface );
1227 #ifdef FT_CONFIG_OPTION_MAC_FONTS
1229 /* The behavior here is very similar to that in base/ftmac.c, but it */
1230 /* is designed to work on non-mac systems, so no mac specific calls. */
1232 /* We look at the file and determine if it is a mac dfont file or a mac */
1233 /* resource file, or a macbinary file containing a mac resource file. */
1235 /* Unlike ftmac I'm not going to look at a `FOND'. I don't really see */
1236 /* the point, especially since there may be multiple `FOND' resources. */
1237 /* Instead I'll just look for `sfnt' and `POST' resources, ordered as */
1238 /* they occur in the file. */
1240 /* Note that multiple `POST' resources do not mean multiple postscript */
1241 /* fonts; they all get jammed together to make what is essentially a */
1244 /* We aren't interested in `NFNT' or `FONT' bitmap resources. */
1246 /* As soon as we get an `sfnt' load it into memory and pass it off to */
1249 /* If we have a (set of) `POST' resources, massage them into a (memory) */
1250 /* pfb file and pass that to FT_Open_Face. (As with ftmac.c I'm not */
1251 /* going to try to save the kerning info. After all that lives in the */
1252 /* `FOND' which isn't in the file containing the `POST' resources so */
1253 /* we don't really have access to it. */
1256 /* Finalizer for a memory stream; gets called by FT_Done_Face(). */
1257 /* It frees the memory it uses. */
1260 memory_stream_close( FT_Stream stream )
1262 FT_Memory memory = stream->memory;
1265 FT_FREE( stream->base );
1273 /* Create a new memory stream from a buffer and a size. */
1276 new_memory_stream( FT_Library library,
1279 FT_Stream_CloseFunc close,
1280 FT_Stream *astream )
1288 return FT_Err_Invalid_Library_Handle;
1291 return FT_Err_Invalid_Argument;
1294 memory = library->memory;
1295 if ( FT_NEW( stream ) )
1298 FT_Stream_OpenMemory( stream, base, size );
1300 stream->close = close;
1309 /* Create a new FT_Face given a buffer and a driver name. */
1311 FT_LOCAL_DEF( FT_Error )
1312 open_face_from_buffer( FT_Library library,
1316 const char* driver_name,
1321 FT_Stream stream = NULL;
1322 FT_Memory memory = library->memory;
1325 error = new_memory_stream( library,
1328 memory_stream_close,
1336 args.flags = FT_OPEN_STREAM;
1337 args.stream = stream;
1340 args.flags = args.flags | FT_OPEN_DRIVER;
1341 args.driver = FT_Get_Module( library, driver_name );
1345 /* At this point, face_index has served its purpose; */
1346 /* whoever calls this function has already used it to */
1347 /* locate the correct font data. We should not propagate */
1348 /* this index to FT_Open_Face() (unless it is negative). */
1350 if ( face_index > 0 )
1354 error = FT_Open_Face( library, &args, face_index, aface );
1356 if ( error == FT_Err_Ok )
1357 (*aface)->face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
1360 FT_Stream_Free( stream, 0 );
1363 FT_Stream_Close( stream );
1372 /* Look up `TYP1' or `CID ' table from sfnt table directory. */
1373 /* `offset' and `length' must exclude the binary header in tables. */
1375 /* Type 1 and CID-keyed font drivers should recognize sfnt-wrapped */
1376 /* format too. Here, since we can't expect that the TrueType font */
1377 /* driver is loaded unconditially, we must parse the font by */
1378 /* ourselves. We are only interested in the name of the table and */
1382 ft_lookup_PS_in_sfnt_stream( FT_Stream stream,
1386 FT_Bool* is_sfnt_cid )
1389 FT_UShort numTables;
1390 FT_Long pstable_index;
1397 *is_sfnt_cid = FALSE;
1399 /* TODO: support for sfnt-wrapped PS/CID in TTC format */
1401 /* version check for 'typ1' (should be ignored?) */
1402 if ( FT_READ_ULONG( tag ) )
1404 if ( tag != TTAG_typ1 )
1405 return FT_Err_Unknown_File_Format;
1407 if ( FT_READ_USHORT( numTables ) )
1409 if ( FT_STREAM_SKIP( 2 * 3 ) ) /* skip binary search header */
1413 *is_sfnt_cid = FALSE;
1415 for ( i = 0; i < numTables; i++ )
1417 if ( FT_READ_ULONG( tag ) || FT_STREAM_SKIP( 4 ) ||
1418 FT_READ_ULONG( *offset ) || FT_READ_ULONG( *length ) )
1421 if ( tag == TTAG_CID )
1426 *is_sfnt_cid = TRUE;
1427 if ( face_index < 0 )
1430 else if ( tag == TTAG_TYP1 )
1435 *is_sfnt_cid = FALSE;
1436 if ( face_index < 0 )
1439 if ( face_index >= 0 && pstable_index == face_index )
1442 return FT_Err_Table_Missing;
1446 FT_LOCAL_DEF( FT_Error )
1447 open_face_PS_from_sfnt_stream( FT_Library library,
1451 FT_Parameter *params,
1455 FT_Memory memory = library->memory;
1456 FT_ULong offset, length;
1458 FT_Bool is_sfnt_cid;
1461 FT_UNUSED( num_params );
1462 FT_UNUSED( params );
1465 pos = FT_Stream_Pos( stream );
1467 error = ft_lookup_PS_in_sfnt_stream( stream,
1475 if ( FT_Stream_Seek( stream, pos + offset ) )
1478 if ( FT_ALLOC( sfnt_ps, (FT_Long)length ) )
1481 error = FT_Stream_Read( stream, (FT_Byte *)sfnt_ps, length );
1485 error = open_face_from_buffer( library,
1488 face_index < 0 ? face_index : 0,
1489 is_sfnt_cid ? "cid" : "type1",
1496 if ( error == FT_Err_Unknown_File_Format )
1498 error1 = FT_Stream_Seek( stream, pos );
1508 #if !defined( FT_MACINTOSH ) || defined( DARWIN_NO_CARBON )
1510 /* The resource header says we've got resource_cnt `POST' (type1) */
1511 /* resources in this file. They all need to be coalesced into */
1512 /* one lump which gets passed on to the type1 driver. */
1513 /* Here can be only one PostScript font in a file so face_index */
1514 /* must be 0 (or -1). */
1517 Mac_Read_POST_Resource( FT_Library library,
1520 FT_Long resource_cnt,
1524 FT_Error error = FT_Err_Cannot_Open_Resource;
1525 FT_Memory memory = library->memory;
1529 FT_Long pfb_len, pfb_pos, pfb_lenpos;
1533 if ( face_index == -1 )
1535 if ( face_index != 0 )
1538 /* Find the length of all the POST resources, concatenated. Assume */
1539 /* worst case (each resource in its own section). */
1541 for ( i = 0; i < resource_cnt; ++i )
1543 error = FT_Stream_Seek( stream, offsets[i] );
1546 if ( FT_READ_LONG( temp ) )
1548 pfb_len += temp + 6;
1551 if ( FT_ALLOC( pfb_data, (FT_Long)pfb_len + 2 ) )
1555 pfb_data[1] = 1; /* Ascii section */
1556 pfb_data[2] = 0; /* 4-byte length, fill in later */
1565 for ( i = 0; i < resource_cnt; ++i )
1567 error = FT_Stream_Seek( stream, offsets[i] );
1570 if ( FT_READ_LONG( rlen ) )
1572 if ( FT_READ_USHORT( flags ) )
1574 FT_TRACE3(( "POST fragment[%d]: offsets=0x%08x, rlen=0x%08x, flags=0x%04x\n",
1575 i, offsets[i], rlen, flags ));
1577 /* postpone the check of rlen longer than buffer until FT_Stream_Read() */
1578 if ( ( flags >> 8 ) == 0 ) /* Comment, should not be loaded */
1581 /* the flags are part of the resource, so rlen >= 2. */
1582 /* but some fonts declare rlen = 0 for empty fragment */
1588 if ( ( flags >> 8 ) == type )
1592 if ( pfb_lenpos + 3 > pfb_len + 2 )
1594 pfb_data[pfb_lenpos ] = (FT_Byte)( len );
1595 pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
1596 pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
1597 pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
1599 if ( ( flags >> 8 ) == 5 ) /* End of font mark */
1602 if ( pfb_pos + 6 > pfb_len + 2 )
1604 pfb_data[pfb_pos++] = 0x80;
1609 pfb_data[pfb_pos++] = (FT_Byte)type;
1610 pfb_lenpos = pfb_pos;
1611 pfb_data[pfb_pos++] = 0; /* 4-byte length, fill in later */
1612 pfb_data[pfb_pos++] = 0;
1613 pfb_data[pfb_pos++] = 0;
1614 pfb_data[pfb_pos++] = 0;
1617 error = FT_Err_Cannot_Open_Resource;
1618 if ( pfb_pos > pfb_len || pfb_pos + rlen > pfb_len )
1621 error = FT_Stream_Read( stream, (FT_Byte *)pfb_data + pfb_pos, rlen );
1627 if ( pfb_pos + 2 > pfb_len + 2 )
1629 pfb_data[pfb_pos++] = 0x80;
1630 pfb_data[pfb_pos++] = 3;
1632 if ( pfb_lenpos + 3 > pfb_len + 2 )
1634 pfb_data[pfb_lenpos ] = (FT_Byte)( len );
1635 pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
1636 pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
1637 pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
1639 return open_face_from_buffer( library,
1647 FT_FREE( pfb_data );
1654 /* The resource header says we've got resource_cnt `sfnt' */
1655 /* (TrueType/OpenType) resources in this file. Look through */
1656 /* them for the one indicated by face_index, load it into mem, */
1657 /* pass it on the the truetype driver and return it. */
1660 Mac_Read_sfnt_Resource( FT_Library library,
1663 FT_Long resource_cnt,
1667 FT_Memory memory = library->memory;
1670 FT_Long flag_offset;
1673 FT_Long face_index_in_resource = 0;
1676 if ( face_index == -1 )
1678 if ( face_index >= resource_cnt )
1679 return FT_Err_Cannot_Open_Resource;
1681 flag_offset = offsets[face_index];
1682 error = FT_Stream_Seek( stream, flag_offset );
1686 if ( FT_READ_LONG( rlen ) )
1689 return FT_Err_Cannot_Open_Resource;
1691 error = open_face_PS_from_sfnt_stream( library,
1699 /* rewind sfnt stream before open_face_PS_from_sfnt_stream() */
1700 if ( FT_Stream_Seek( stream, flag_offset + 4 ) )
1703 if ( FT_ALLOC( sfnt_data, (FT_Long)rlen ) )
1705 error = FT_Stream_Read( stream, (FT_Byte *)sfnt_data, rlen );
1709 is_cff = rlen > 4 && !ft_memcmp( sfnt_data, "OTTO", 4 );
1710 error = open_face_from_buffer( library,
1713 face_index_in_resource,
1714 is_cff ? "cff" : "truetype",
1722 /* Check for a valid resource fork header, or a valid dfont */
1723 /* header. In a resource fork the first 16 bytes are repeated */
1724 /* at the location specified by bytes 4-7. In a dfont bytes */
1725 /* 4-7 point to 16 bytes of zeroes instead. */
1728 IsMacResource( FT_Library library,
1730 FT_Long resource_offset,
1734 FT_Memory memory = library->memory;
1736 FT_Long map_offset, rdara_pos;
1737 FT_Long *data_offsets;
1741 error = FT_Raccess_Get_HeaderInfo( library, stream, resource_offset,
1742 &map_offset, &rdara_pos );
1746 error = FT_Raccess_Get_DataOffsets( library, stream,
1747 map_offset, rdara_pos,
1749 &data_offsets, &count );
1752 error = Mac_Read_POST_Resource( library, stream, data_offsets, count,
1753 face_index, aface );
1754 FT_FREE( data_offsets );
1755 /* POST exists in an LWFN providing a single face */
1757 (*aface)->num_faces = 1;
1761 error = FT_Raccess_Get_DataOffsets( library, stream,
1762 map_offset, rdara_pos,
1764 &data_offsets, &count );
1767 FT_Long face_index_internal = face_index % count;
1770 error = Mac_Read_sfnt_Resource( library, stream, data_offsets, count,
1771 face_index_internal, aface );
1772 FT_FREE( data_offsets );
1774 (*aface)->num_faces = count;
1781 /* Check for a valid macbinary header, and if we find one */
1782 /* check that the (flattened) resource fork in it is valid. */
1785 IsMacBinary( FT_Library library,
1790 unsigned char header[128];
1792 FT_Long dlen, offset;
1795 if ( NULL == stream )
1796 return FT_Err_Invalid_Stream_Operation;
1798 error = FT_Stream_Seek( stream, 0 );
1802 error = FT_Stream_Read( stream, (FT_Byte*)header, 128 );
1806 if ( header[ 0] != 0 ||
1812 header[2 + header[1]] != 0 )
1813 return FT_Err_Unknown_File_Format;
1815 dlen = ( header[0x53] << 24 ) |
1816 ( header[0x54] << 16 ) |
1817 ( header[0x55] << 8 ) |
1820 rlen = ( header[0x57] << 24 ) |
1821 ( header[0x58] << 16 ) |
1822 ( header[0x59] << 8 ) |
1825 offset = 128 + ( ( dlen + 127 ) & ~127 );
1827 return IsMacResource( library, stream, offset, face_index, aface );
1835 load_face_in_embedded_rfork( FT_Library library,
1839 const FT_Open_Args *args )
1843 #define FT_COMPONENT trace_raccess
1845 FT_Memory memory = library->memory;
1846 FT_Error error = FT_Err_Unknown_File_Format;
1849 char * file_names[FT_RACCESS_N_RULES];
1850 FT_Long offsets[FT_RACCESS_N_RULES];
1851 FT_Error errors[FT_RACCESS_N_RULES];
1854 FT_Stream stream2 = 0;
1857 FT_Raccess_Guess( library, stream,
1858 args->pathname, file_names, offsets, errors );
1860 for ( i = 0; i < FT_RACCESS_N_RULES; i++ )
1864 FT_TRACE3(( "Error[%d] has occurred in rule %d\n", errors[i], i ));
1868 args2.flags = FT_OPEN_PATHNAME;
1869 args2.pathname = file_names[i] ? file_names[i] : args->pathname;
1871 FT_TRACE3(( "Try rule %d: %s (offset=%d) ...",
1872 i, args2.pathname, offsets[i] ));
1874 error = FT_Stream_New( library, &args2, &stream2 );
1877 FT_TRACE3(( "failed\n" ));
1881 error = IsMacResource( library, stream2, offsets[i],
1882 face_index, aface );
1883 FT_Stream_Free( stream2, 0 );
1885 FT_TRACE3(( "%s\n", error ? "failed": "successful" ));
1891 for (i = 0; i < FT_RACCESS_N_RULES; i++)
1893 if ( file_names[i] )
1894 FT_FREE( file_names[i] );
1897 /* Caller (load_mac_face) requires FT_Err_Unknown_File_Format. */
1899 error = FT_Err_Unknown_File_Format;
1904 #define FT_COMPONENT trace_objs
1909 /* Check for some macintosh formats without Carbon framework. */
1910 /* Is this a macbinary file? If so look at the resource fork. */
1911 /* Is this a mac dfont file? */
1912 /* Is this an old style resource fork? (in data) */
1913 /* Else call load_face_in_embedded_rfork to try extra rules */
1914 /* (defined in `ftrfork.c'). */
1917 load_mac_face( FT_Library library,
1921 const FT_Open_Args *args )
1927 error = IsMacBinary( library, stream, face_index, aface );
1928 if ( FT_ERROR_BASE( error ) == FT_Err_Unknown_File_Format )
1932 #define FT_COMPONENT trace_raccess
1934 FT_TRACE3(( "Try as dfont: %s ...", args->pathname ));
1936 error = IsMacResource( library, stream, 0, face_index, aface );
1938 FT_TRACE3(( "%s\n", error ? "failed" : "successful" ));
1941 #define FT_COMPONENT trace_objs
1945 if ( ( FT_ERROR_BASE( error ) == FT_Err_Unknown_File_Format ||
1946 FT_ERROR_BASE( error ) == FT_Err_Invalid_Stream_Operation ) &&
1947 ( args->flags & FT_OPEN_PATHNAME ) )
1948 error = load_face_in_embedded_rfork( library, stream,
1949 face_index, aface, args );
1954 #endif /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
1957 /* documentation is in freetype.h */
1959 FT_EXPORT_DEF( FT_Error )
1960 FT_Open_Face( FT_Library library,
1961 const FT_Open_Args* args,
1968 FT_Stream stream = NULL;
1969 FT_Face face = NULL;
1970 FT_ListNode node = NULL;
1971 FT_Bool external_stream;
1976 /* test for valid `library' delayed to */
1977 /* FT_Stream_New() */
1979 if ( ( !aface && face_index >= 0 ) || !args )
1980 return FT_Err_Invalid_Argument;
1982 external_stream = FT_BOOL( ( args->flags & FT_OPEN_STREAM ) &&
1985 /* create input stream */
1986 error = FT_Stream_New( library, args, &stream );
1990 memory = library->memory;
1992 /* If the font driver is specified in the `args' structure, use */
1993 /* it. Otherwise, we scan the list of registered drivers. */
1994 if ( ( args->flags & FT_OPEN_DRIVER ) && args->driver )
1996 driver = FT_DRIVER( args->driver );
1998 /* not all modules are drivers, so check... */
1999 if ( FT_MODULE_IS_DRIVER( driver ) )
2001 FT_Int num_params = 0;
2002 FT_Parameter* params = 0;
2005 if ( args->flags & FT_OPEN_PARAMS )
2007 num_params = args->num_params;
2008 params = args->params;
2011 error = open_face( driver, stream, face_index,
2012 num_params, params, &face );
2017 error = FT_Err_Invalid_Handle;
2019 FT_Stream_Free( stream, external_stream );
2024 /* check each font driver for an appropriate format */
2025 cur = library->modules;
2026 limit = cur + library->num_modules;
2029 for ( ; cur < limit; cur++ )
2031 /* not all modules are font drivers, so check... */
2032 if ( FT_MODULE_IS_DRIVER( cur[0] ) )
2034 FT_Int num_params = 0;
2035 FT_Parameter* params = 0;
2038 driver = FT_DRIVER( cur[0] );
2040 if ( args->flags & FT_OPEN_PARAMS )
2042 num_params = args->num_params;
2043 params = args->params;
2046 error = open_face( driver, stream, face_index,
2047 num_params, params, &face );
2051 #ifdef FT_CONFIG_OPTION_MAC_FONTS
2052 if ( ft_strcmp( cur[0]->clazz->module_name, "truetype" ) == 0 &&
2053 FT_ERROR_BASE( error ) == FT_Err_Table_Missing )
2055 /* TrueType but essential tables are missing */
2056 if ( FT_Stream_Seek( stream, 0 ) )
2059 error = open_face_PS_from_sfnt_stream( library,
2067 FT_Stream_Free( stream, external_stream );
2073 if ( FT_ERROR_BASE( error ) != FT_Err_Unknown_File_Format )
2079 /* If we are on the mac, and we get an FT_Err_Invalid_Stream_Operation */
2080 /* it may be because we have an empty data fork, so we need to check */
2081 /* the resource fork. */
2082 if ( FT_ERROR_BASE( error ) != FT_Err_Cannot_Open_Stream &&
2083 FT_ERROR_BASE( error ) != FT_Err_Unknown_File_Format &&
2084 FT_ERROR_BASE( error ) != FT_Err_Invalid_Stream_Operation )
2087 #if !defined( FT_MACINTOSH ) && defined( FT_CONFIG_OPTION_MAC_FONTS )
2088 error = load_mac_face( library, stream, face_index, aface, args );
2091 /* We don't want to go to Success here. We've already done that. */
2092 /* On the other hand, if we succeeded we still need to close this */
2093 /* stream (we opened a different stream which extracted the */
2094 /* interesting information out of this stream here. That stream */
2095 /* will still be open and the face will point to it). */
2096 FT_Stream_Free( stream, external_stream );
2100 if ( FT_ERROR_BASE( error ) != FT_Err_Unknown_File_Format )
2102 #endif /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
2104 /* no driver is able to handle this format */
2105 error = FT_Err_Unknown_File_Format;
2108 FT_Stream_Free( stream, external_stream );
2113 FT_TRACE4(( "FT_Open_Face: New face object, adding to list\n" ));
2115 /* set the FT_FACE_FLAG_EXTERNAL_STREAM bit for FT_Done_Face */
2116 if ( external_stream )
2117 face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
2119 /* add the face object to its driver's list */
2120 if ( FT_NEW( node ) )
2124 /* don't assume driver is the same as face->driver, so use */
2125 /* face->driver instead. */
2126 FT_List_Add( &face->driver->faces_list, node );
2128 /* now allocate a glyph slot object for the face */
2129 FT_TRACE4(( "FT_Open_Face: Creating glyph slot\n" ));
2131 if ( face_index >= 0 )
2133 error = FT_New_GlyphSlot( face, NULL );
2137 /* finally, allocate a size object for the face */
2142 FT_TRACE4(( "FT_Open_Face: Creating size object\n" ));
2144 error = FT_New_Size( face, &size );
2154 if ( FT_IS_SCALABLE( face ) )
2156 if ( face->height < 0 )
2157 face->height = (FT_Short)-face->height;
2159 if ( !FT_HAS_VERTICAL( face ) )
2160 face->max_advance_height = (FT_Short)face->height;
2163 if ( FT_HAS_FIXED_SIZES( face ) )
2168 for ( i = 0; i < face->num_fixed_sizes; i++ )
2170 FT_Bitmap_Size* bsize = face->available_sizes + i;
2173 if ( bsize->height < 0 )
2174 bsize->height = (FT_Short)-bsize->height;
2175 if ( bsize->x_ppem < 0 )
2176 bsize->x_ppem = (FT_Short)-bsize->x_ppem;
2177 if ( bsize->y_ppem < 0 )
2178 bsize->y_ppem = -bsize->y_ppem;
2182 /* initialize internal face data */
2184 FT_Face_Internal internal = face->internal;
2187 internal->transform_matrix.xx = 0x10000L;
2188 internal->transform_matrix.xy = 0;
2189 internal->transform_matrix.yx = 0;
2190 internal->transform_matrix.yy = 0x10000L;
2192 internal->transform_delta.x = 0;
2193 internal->transform_delta.y = 0;
2195 internal->refcount = 1;
2201 FT_Done_Face( face );
2206 FT_Done_Face( face );
2209 FT_TRACE4(( "FT_Open_Face: Return %d\n", error ));
2215 /* documentation is in freetype.h */
2217 FT_EXPORT_DEF( FT_Error )
2218 FT_Attach_File( FT_Face face,
2219 const char* filepathname )
2224 /* test for valid `face' delayed to FT_Attach_Stream() */
2226 if ( !filepathname )
2227 return FT_Err_Invalid_Argument;
2230 open.flags = FT_OPEN_PATHNAME;
2231 open.pathname = (char*)filepathname;
2233 return FT_Attach_Stream( face, &open );
2237 /* documentation is in freetype.h */
2239 FT_EXPORT_DEF( FT_Error )
2240 FT_Attach_Stream( FT_Face face,
2241 FT_Open_Args* parameters )
2247 FT_Driver_Class clazz;
2250 /* test for valid `parameters' delayed to FT_Stream_New() */
2253 return FT_Err_Invalid_Face_Handle;
2255 driver = face->driver;
2257 return FT_Err_Invalid_Driver_Handle;
2259 error = FT_Stream_New( driver->root.library, parameters, &stream );
2263 /* we implement FT_Attach_Stream in each driver through the */
2264 /* `attach_file' interface */
2266 error = FT_Err_Unimplemented_Feature;
2267 clazz = driver->clazz;
2268 if ( clazz->attach_file )
2269 error = clazz->attach_file( face, stream );
2271 /* close the attached stream */
2272 FT_Stream_Free( stream,
2273 (FT_Bool)( parameters->stream &&
2274 ( parameters->flags & FT_OPEN_STREAM ) ) );
2281 /* documentation is in freetype.h */
2283 FT_EXPORT_DEF( FT_Error )
2284 FT_Reference_Face( FT_Face face )
2286 face->internal->refcount++;
2292 /* documentation is in freetype.h */
2294 FT_EXPORT_DEF( FT_Error )
2295 FT_Done_Face( FT_Face face )
2303 error = FT_Err_Invalid_Face_Handle;
2304 if ( face && face->driver )
2306 face->internal->refcount--;
2307 if ( face->internal->refcount > 0 )
2311 driver = face->driver;
2312 memory = driver->root.memory;
2314 /* find face in driver's list */
2315 node = FT_List_Find( &driver->faces_list, face );
2318 /* remove face object from the driver's list */
2319 FT_List_Remove( &driver->faces_list, node );
2322 /* now destroy the object proper */
2323 destroy_face( memory, face, driver );
2333 /* documentation is in ftobjs.h */
2335 FT_EXPORT_DEF( FT_Error )
2336 FT_New_Size( FT_Face face,
2342 FT_Driver_Class clazz;
2345 FT_ListNode node = 0;
2349 return FT_Err_Invalid_Face_Handle;
2352 return FT_Err_Invalid_Size_Handle;
2354 if ( !face->driver )
2355 return FT_Err_Invalid_Driver_Handle;
2359 driver = face->driver;
2360 clazz = driver->clazz;
2361 memory = face->memory;
2363 /* Allocate new size object and perform basic initialisation */
2364 if ( FT_ALLOC( size, clazz->size_object_size ) || FT_NEW( node ) )
2369 /* for now, do not use any internal fields in size objects */
2372 if ( clazz->init_size )
2373 error = clazz->init_size( size );
2375 /* in case of success, add to the face's list */
2380 FT_List_Add( &face->sizes_list, node );
2394 /* documentation is in ftobjs.h */
2396 FT_EXPORT_DEF( FT_Error )
2397 FT_Done_Size( FT_Size size )
2407 return FT_Err_Invalid_Size_Handle;
2411 return FT_Err_Invalid_Face_Handle;
2413 driver = face->driver;
2415 return FT_Err_Invalid_Driver_Handle;
2417 memory = driver->root.memory;
2420 node = FT_List_Find( &face->sizes_list, size );
2423 FT_List_Remove( &face->sizes_list, node );
2426 if ( face->size == size )
2429 if ( face->sizes_list.head )
2430 face->size = (FT_Size)(face->sizes_list.head->data);
2433 destroy_size( memory, size, driver );
2436 error = FT_Err_Invalid_Size_Handle;
2442 /* documentation is in ftobjs.h */
2444 FT_BASE_DEF( FT_Error )
2445 FT_Match_Size( FT_Face face,
2446 FT_Size_Request req,
2447 FT_Bool ignore_width,
2448 FT_ULong* size_index )
2454 if ( !FT_HAS_FIXED_SIZES( face ) )
2455 return FT_Err_Invalid_Face_Handle;
2457 /* FT_Bitmap_Size doesn't provide enough info... */
2458 if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
2459 return FT_Err_Unimplemented_Feature;
2461 w = FT_REQUEST_WIDTH ( req );
2462 h = FT_REQUEST_HEIGHT( req );
2464 if ( req->width && !req->height )
2466 else if ( !req->width && req->height )
2469 w = FT_PIX_ROUND( w );
2470 h = FT_PIX_ROUND( h );
2472 for ( i = 0; i < face->num_fixed_sizes; i++ )
2474 FT_Bitmap_Size* bsize = face->available_sizes + i;
2477 if ( h != FT_PIX_ROUND( bsize->y_ppem ) )
2480 if ( w == FT_PIX_ROUND( bsize->x_ppem ) || ignore_width )
2483 *size_index = (FT_ULong)i;
2489 return FT_Err_Invalid_Pixel_Size;
2493 /* documentation is in ftobjs.h */
2496 ft_synthesize_vertical_metrics( FT_Glyph_Metrics* metrics,
2499 FT_Pos height = metrics->height;
2502 /* compensate for glyph with bbox above/below the baseline */
2503 if ( metrics->horiBearingY < 0 )
2505 if ( height < metrics->horiBearingY )
2506 height = metrics->horiBearingY;
2508 else if ( metrics->horiBearingY > 0 )
2509 height -= metrics->horiBearingY;
2511 /* the factor 1.2 is a heuristical value */
2513 advance = height * 12 / 10;
2515 metrics->vertBearingX = metrics->horiBearingX - metrics->horiAdvance / 2;
2516 metrics->vertBearingY = ( advance - height ) / 2;
2517 metrics->vertAdvance = advance;
2522 ft_recompute_scaled_metrics( FT_Face face,
2523 FT_Size_Metrics* metrics )
2525 /* Compute root ascender, descender, test height, and max_advance */
2527 #ifdef GRID_FIT_METRICS
2528 metrics->ascender = FT_PIX_CEIL( FT_MulFix( face->ascender,
2529 metrics->y_scale ) );
2531 metrics->descender = FT_PIX_FLOOR( FT_MulFix( face->descender,
2532 metrics->y_scale ) );
2534 metrics->height = FT_PIX_ROUND( FT_MulFix( face->height,
2535 metrics->y_scale ) );
2537 metrics->max_advance = FT_PIX_ROUND( FT_MulFix( face->max_advance_width,
2538 metrics->x_scale ) );
2539 #else /* !GRID_FIT_METRICS */
2540 metrics->ascender = FT_MulFix( face->ascender,
2543 metrics->descender = FT_MulFix( face->descender,
2546 metrics->height = FT_MulFix( face->height,
2549 metrics->max_advance = FT_MulFix( face->max_advance_width,
2551 #endif /* !GRID_FIT_METRICS */
2556 FT_Select_Metrics( FT_Face face,
2557 FT_ULong strike_index )
2559 FT_Size_Metrics* metrics;
2560 FT_Bitmap_Size* bsize;
2563 metrics = &face->size->metrics;
2564 bsize = face->available_sizes + strike_index;
2566 metrics->x_ppem = (FT_UShort)( ( bsize->x_ppem + 32 ) >> 6 );
2567 metrics->y_ppem = (FT_UShort)( ( bsize->y_ppem + 32 ) >> 6 );
2569 if ( FT_IS_SCALABLE( face ) )
2571 metrics->x_scale = FT_DivFix( bsize->x_ppem,
2572 face->units_per_EM );
2573 metrics->y_scale = FT_DivFix( bsize->y_ppem,
2574 face->units_per_EM );
2576 ft_recompute_scaled_metrics( face, metrics );
2580 metrics->x_scale = 1L << 16;
2581 metrics->y_scale = 1L << 16;
2582 metrics->ascender = bsize->y_ppem;
2583 metrics->descender = 0;
2584 metrics->height = bsize->height << 6;
2585 metrics->max_advance = bsize->x_ppem;
2591 FT_Request_Metrics( FT_Face face,
2592 FT_Size_Request req )
2594 FT_Size_Metrics* metrics;
2597 metrics = &face->size->metrics;
2599 if ( FT_IS_SCALABLE( face ) )
2601 FT_Long w = 0, h = 0, scaled_w = 0, scaled_h = 0;
2604 switch ( req->type )
2606 case FT_SIZE_REQUEST_TYPE_NOMINAL:
2607 w = h = face->units_per_EM;
2610 case FT_SIZE_REQUEST_TYPE_REAL_DIM:
2611 w = h = face->ascender - face->descender;
2614 case FT_SIZE_REQUEST_TYPE_BBOX:
2615 w = face->bbox.xMax - face->bbox.xMin;
2616 h = face->bbox.yMax - face->bbox.yMin;
2619 case FT_SIZE_REQUEST_TYPE_CELL:
2620 w = face->max_advance_width;
2621 h = face->ascender - face->descender;
2624 case FT_SIZE_REQUEST_TYPE_SCALES:
2625 metrics->x_scale = (FT_Fixed)req->width;
2626 metrics->y_scale = (FT_Fixed)req->height;
2627 if ( !metrics->x_scale )
2628 metrics->x_scale = metrics->y_scale;
2629 else if ( !metrics->y_scale )
2630 metrics->y_scale = metrics->x_scale;
2631 goto Calculate_Ppem;
2633 case FT_SIZE_REQUEST_TYPE_MAX:
2637 /* to be on the safe side */
2644 scaled_w = FT_REQUEST_WIDTH ( req );
2645 scaled_h = FT_REQUEST_HEIGHT( req );
2647 /* determine scales */
2650 metrics->x_scale = FT_DivFix( scaled_w, w );
2654 metrics->y_scale = FT_DivFix( scaled_h, h );
2656 if ( req->type == FT_SIZE_REQUEST_TYPE_CELL )
2658 if ( metrics->y_scale > metrics->x_scale )
2659 metrics->y_scale = metrics->x_scale;
2661 metrics->x_scale = metrics->y_scale;
2666 metrics->y_scale = metrics->x_scale;
2667 scaled_h = FT_MulDiv( scaled_w, h, w );
2672 metrics->x_scale = metrics->y_scale = FT_DivFix( scaled_h, h );
2673 scaled_w = FT_MulDiv( scaled_h, w, h );
2677 /* calculate the ppems */
2678 if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
2680 scaled_w = FT_MulFix( face->units_per_EM, metrics->x_scale );
2681 scaled_h = FT_MulFix( face->units_per_EM, metrics->y_scale );
2684 metrics->x_ppem = (FT_UShort)( ( scaled_w + 32 ) >> 6 );
2685 metrics->y_ppem = (FT_UShort)( ( scaled_h + 32 ) >> 6 );
2687 ft_recompute_scaled_metrics( face, metrics );
2692 metrics->x_scale = 1L << 16;
2693 metrics->y_scale = 1L << 16;
2698 /* documentation is in freetype.h */
2700 FT_EXPORT_DEF( FT_Error )
2701 FT_Select_Size( FT_Face face,
2702 FT_Int strike_index )
2704 FT_Driver_Class clazz;
2707 if ( !face || !FT_HAS_FIXED_SIZES( face ) )
2708 return FT_Err_Invalid_Face_Handle;
2710 if ( strike_index < 0 || strike_index >= face->num_fixed_sizes )
2711 return FT_Err_Invalid_Argument;
2713 clazz = face->driver->clazz;
2715 if ( clazz->select_size )
2716 return clazz->select_size( face->size, (FT_ULong)strike_index );
2718 FT_Select_Metrics( face, (FT_ULong)strike_index );
2724 /* documentation is in freetype.h */
2726 FT_EXPORT_DEF( FT_Error )
2727 FT_Request_Size( FT_Face face,
2728 FT_Size_Request req )
2730 FT_Driver_Class clazz;
2731 FT_ULong strike_index;
2735 return FT_Err_Invalid_Face_Handle;
2737 if ( !req || req->width < 0 || req->height < 0 ||
2738 req->type >= FT_SIZE_REQUEST_TYPE_MAX )
2739 return FT_Err_Invalid_Argument;
2741 clazz = face->driver->clazz;
2743 if ( clazz->request_size )
2744 return clazz->request_size( face->size, req );
2747 * The reason that a driver doesn't have `request_size' defined is
2748 * either that the scaling here suffices or that the supported formats
2749 * are bitmap-only and size matching is not implemented.
2751 * In the latter case, a simple size matching is done.
2753 if ( !FT_IS_SCALABLE( face ) && FT_HAS_FIXED_SIZES( face ) )
2758 error = FT_Match_Size( face, req, 0, &strike_index );
2762 FT_TRACE3(( "FT_Request_Size: bitmap strike %lu matched\n",
2765 return FT_Select_Size( face, (FT_Int)strike_index );
2768 FT_Request_Metrics( face, req );
2774 /* documentation is in freetype.h */
2776 FT_EXPORT_DEF( FT_Error )
2777 FT_Set_Char_Size( FT_Face face,
2778 FT_F26Dot6 char_width,
2779 FT_F26Dot6 char_height,
2780 FT_UInt horz_resolution,
2781 FT_UInt vert_resolution )
2783 FT_Size_RequestRec req;
2787 char_width = char_height;
2788 else if ( !char_height )
2789 char_height = char_width;
2791 if ( !horz_resolution )
2792 horz_resolution = vert_resolution;
2793 else if ( !vert_resolution )
2794 vert_resolution = horz_resolution;
2796 if ( char_width < 1 * 64 )
2797 char_width = 1 * 64;
2798 if ( char_height < 1 * 64 )
2799 char_height = 1 * 64;
2801 if ( !horz_resolution )
2802 horz_resolution = vert_resolution = 72;
2804 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
2805 req.width = char_width;
2806 req.height = char_height;
2807 req.horiResolution = horz_resolution;
2808 req.vertResolution = vert_resolution;
2810 return FT_Request_Size( face, &req );
2814 /* documentation is in freetype.h */
2816 FT_EXPORT_DEF( FT_Error )
2817 FT_Set_Pixel_Sizes( FT_Face face,
2818 FT_UInt pixel_width,
2819 FT_UInt pixel_height )
2821 FT_Size_RequestRec req;
2824 if ( pixel_width == 0 )
2825 pixel_width = pixel_height;
2826 else if ( pixel_height == 0 )
2827 pixel_height = pixel_width;
2829 if ( pixel_width < 1 )
2831 if ( pixel_height < 1 )
2834 /* use `>=' to avoid potential compiler warning on 16bit platforms */
2835 if ( pixel_width >= 0xFFFFU )
2836 pixel_width = 0xFFFFU;
2837 if ( pixel_height >= 0xFFFFU )
2838 pixel_height = 0xFFFFU;
2840 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
2841 req.width = pixel_width << 6;
2842 req.height = pixel_height << 6;
2843 req.horiResolution = 0;
2844 req.vertResolution = 0;
2846 return FT_Request_Size( face, &req );
2850 /* documentation is in freetype.h */
2852 FT_EXPORT_DEF( FT_Error )
2853 FT_Get_Kerning( FT_Face face,
2855 FT_UInt right_glyph,
2857 FT_Vector *akerning )
2859 FT_Error error = FT_Err_Ok;
2864 return FT_Err_Invalid_Face_Handle;
2867 return FT_Err_Invalid_Argument;
2869 driver = face->driver;
2874 if ( driver->clazz->get_kerning )
2876 error = driver->clazz->get_kerning( face,
2882 if ( kern_mode != FT_KERNING_UNSCALED )
2884 akerning->x = FT_MulFix( akerning->x, face->size->metrics.x_scale );
2885 akerning->y = FT_MulFix( akerning->y, face->size->metrics.y_scale );
2887 if ( kern_mode != FT_KERNING_UNFITTED )
2889 /* we scale down kerning values for small ppem values */
2890 /* to avoid that rounding makes them too big. */
2891 /* `25' has been determined heuristically. */
2892 if ( face->size->metrics.x_ppem < 25 )
2893 akerning->x = FT_MulDiv( akerning->x,
2894 face->size->metrics.x_ppem, 25 );
2895 if ( face->size->metrics.y_ppem < 25 )
2896 akerning->y = FT_MulDiv( akerning->y,
2897 face->size->metrics.y_ppem, 25 );
2899 akerning->x = FT_PIX_ROUND( akerning->x );
2900 akerning->y = FT_PIX_ROUND( akerning->y );
2910 /* documentation is in freetype.h */
2912 FT_EXPORT_DEF( FT_Error )
2913 FT_Get_Track_Kerning( FT_Face face,
2914 FT_Fixed point_size,
2916 FT_Fixed* akerning )
2918 FT_Service_Kerning service;
2919 FT_Error error = FT_Err_Ok;
2923 return FT_Err_Invalid_Face_Handle;
2926 return FT_Err_Invalid_Argument;
2928 FT_FACE_FIND_SERVICE( face, service, KERNING );
2930 return FT_Err_Unimplemented_Feature;
2932 error = service->get_track( face,
2941 /* documentation is in freetype.h */
2943 FT_EXPORT_DEF( FT_Error )
2944 FT_Select_Charmap( FT_Face face,
2945 FT_Encoding encoding )
2952 return FT_Err_Invalid_Face_Handle;
2954 if ( encoding == FT_ENCODING_NONE )
2955 return FT_Err_Invalid_Argument;
2957 /* FT_ENCODING_UNICODE is special. We try to find the `best' Unicode */
2958 /* charmap available, i.e., one with UCS-4 characters, if possible. */
2960 /* This is done by find_unicode_charmap() above, to share code. */
2961 if ( encoding == FT_ENCODING_UNICODE )
2962 return find_unicode_charmap( face );
2964 cur = face->charmaps;
2966 return FT_Err_Invalid_CharMap_Handle;
2968 limit = cur + face->num_charmaps;
2970 for ( ; cur < limit; cur++ )
2972 if ( cur[0]->encoding == encoding )
2974 #ifdef FT_MAX_CHARMAP_CACHEABLE
2975 if ( cur - face->charmaps > FT_MAX_CHARMAP_CACHEABLE )
2977 FT_ERROR(( "FT_Select_Charmap: requested charmap is found (%d), "
2978 "but in too late position to cache\n",
2979 cur - face->charmaps ));
2983 face->charmap = cur[0];
2988 return FT_Err_Invalid_Argument;
2992 /* documentation is in freetype.h */
2994 FT_EXPORT_DEF( FT_Error )
2995 FT_Set_Charmap( FT_Face face,
2996 FT_CharMap charmap )
3003 return FT_Err_Invalid_Face_Handle;
3005 cur = face->charmaps;
3007 return FT_Err_Invalid_CharMap_Handle;
3008 if ( FT_Get_CMap_Format( charmap ) == 14 )
3009 return FT_Err_Invalid_Argument;
3011 limit = cur + face->num_charmaps;
3013 for ( ; cur < limit; cur++ )
3015 if ( cur[0] == charmap )
3017 #ifdef FT_MAX_CHARMAP_CACHEABLE
3018 if ( cur - face->charmaps > FT_MAX_CHARMAP_CACHEABLE )
3020 FT_ERROR(( "FT_Set_Charmap: requested charmap is found (%d), "
3021 "but in too late position to cache\n",
3022 cur - face->charmaps ));
3026 face->charmap = cur[0];
3030 return FT_Err_Invalid_Argument;
3034 /* documentation is in freetype.h */
3036 FT_EXPORT_DEF( FT_Int )
3037 FT_Get_Charmap_Index( FT_CharMap charmap )
3042 if ( !charmap || !charmap->face )
3045 for ( i = 0; i < charmap->face->num_charmaps; i++ )
3046 if ( charmap->face->charmaps[i] == charmap )
3049 FT_ASSERT( i < charmap->face->num_charmaps );
3051 #ifdef FT_MAX_CHARMAP_CACHEABLE
3052 if ( i > FT_MAX_CHARMAP_CACHEABLE )
3054 FT_ERROR(( "FT_Get_Charmap_Index: requested charmap is found (%d), "
3055 "but in too late position to cache\n",
3065 ft_cmap_done_internal( FT_CMap cmap )
3067 FT_CMap_Class clazz = cmap->clazz;
3068 FT_Face face = cmap->charmap.face;
3069 FT_Memory memory = FT_FACE_MEMORY(face);
3073 clazz->done( cmap );
3080 FT_CMap_Done( FT_CMap cmap )
3084 FT_Face face = cmap->charmap.face;
3085 FT_Memory memory = FT_FACE_MEMORY( face );
3090 for ( i = 0; i < face->num_charmaps; i++ )
3092 if ( (FT_CMap)face->charmaps[i] == cmap )
3094 FT_CharMap last_charmap = face->charmaps[face->num_charmaps - 1];
3097 if ( FT_RENEW_ARRAY( face->charmaps,
3099 face->num_charmaps - 1 ) )
3102 /* remove it from our list of charmaps */
3103 for ( j = i + 1; j < face->num_charmaps; j++ )
3105 if ( j == face->num_charmaps - 1 )
3106 face->charmaps[j - 1] = last_charmap;
3108 face->charmaps[j - 1] = face->charmaps[j];
3111 face->num_charmaps--;
3113 if ( (FT_CMap)face->charmap == cmap )
3114 face->charmap = NULL;
3116 ft_cmap_done_internal( cmap );
3125 FT_BASE_DEF( FT_Error )
3126 FT_CMap_New( FT_CMap_Class clazz,
3127 FT_Pointer init_data,
3131 FT_Error error = FT_Err_Ok;
3137 if ( clazz == NULL || charmap == NULL || charmap->face == NULL )
3138 return FT_Err_Invalid_Argument;
3140 face = charmap->face;
3141 memory = FT_FACE_MEMORY( face );
3143 if ( !FT_ALLOC( cmap, clazz->size ) )
3145 cmap->charmap = *charmap;
3146 cmap->clazz = clazz;
3150 error = clazz->init( cmap, init_data );
3155 /* add it to our list of charmaps */
3156 if ( FT_RENEW_ARRAY( face->charmaps,
3158 face->num_charmaps + 1 ) )
3161 face->charmaps[face->num_charmaps++] = (FT_CharMap)cmap;
3171 ft_cmap_done_internal( cmap );
3177 /* documentation is in freetype.h */
3179 FT_EXPORT_DEF( FT_UInt )
3180 FT_Get_Char_Index( FT_Face face,
3186 if ( face && face->charmap )
3188 FT_CMap cmap = FT_CMAP( face->charmap );
3191 if ( charcode > 0xFFFFFFFFUL )
3193 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3194 FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3196 result = cmap->clazz->char_index( cmap, (FT_UInt32)charcode );
3202 /* documentation is in freetype.h */
3204 FT_EXPORT_DEF( FT_ULong )
3205 FT_Get_First_Char( FT_Face face,
3208 FT_ULong result = 0;
3212 if ( face && face->charmap && face->num_glyphs )
3214 gindex = FT_Get_Char_Index( face, 0 );
3215 if ( gindex == 0 || gindex >= (FT_UInt)face->num_glyphs )
3216 result = FT_Get_Next_Char( face, 0, &gindex );
3226 /* documentation is in freetype.h */
3228 FT_EXPORT_DEF( FT_ULong )
3229 FT_Get_Next_Char( FT_Face face,
3233 FT_ULong result = 0;
3237 if ( face && face->charmap && face->num_glyphs )
3239 FT_UInt32 code = (FT_UInt32)charcode;
3240 FT_CMap cmap = FT_CMAP( face->charmap );
3244 gindex = cmap->clazz->char_next( cmap, &code );
3245 } while ( gindex >= (FT_UInt)face->num_glyphs );
3247 result = ( gindex == 0 ) ? 0 : code;
3257 /* documentation is in freetype.h */
3259 FT_EXPORT_DEF( FT_UInt )
3260 FT_Face_GetCharVariantIndex( FT_Face face,
3262 FT_ULong variantSelector )
3267 if ( face && face->charmap &&
3268 face->charmap->encoding == FT_ENCODING_UNICODE )
3270 FT_CharMap charmap = find_variant_selector_charmap( face );
3271 FT_CMap ucmap = FT_CMAP( face->charmap );
3274 if ( charmap != NULL )
3276 FT_CMap vcmap = FT_CMAP( charmap );
3279 if ( charcode > 0xFFFFFFFFUL )
3281 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3282 FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3284 if ( variantSelector > 0xFFFFFFFFUL )
3286 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3287 FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3290 result = vcmap->clazz->char_var_index( vcmap, ucmap,
3291 (FT_UInt32)charcode,
3292 (FT_UInt32)variantSelector );
3300 /* documentation is in freetype.h */
3302 FT_EXPORT_DEF( FT_Int )
3303 FT_Face_GetCharVariantIsDefault( FT_Face face,
3305 FT_ULong variantSelector )
3312 FT_CharMap charmap = find_variant_selector_charmap( face );
3315 if ( charmap != NULL )
3317 FT_CMap vcmap = FT_CMAP( charmap );
3320 if ( charcode > 0xFFFFFFFFUL )
3322 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3323 FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3325 if ( variantSelector > 0xFFFFFFFFUL )
3327 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3328 FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3331 result = vcmap->clazz->char_var_default( vcmap,
3332 (FT_UInt32)charcode,
3333 (FT_UInt32)variantSelector );
3341 /* documentation is in freetype.h */
3343 FT_EXPORT_DEF( FT_UInt32* )
3344 FT_Face_GetVariantSelectors( FT_Face face )
3346 FT_UInt32 *result = NULL;
3351 FT_CharMap charmap = find_variant_selector_charmap( face );
3354 if ( charmap != NULL )
3356 FT_CMap vcmap = FT_CMAP( charmap );
3357 FT_Memory memory = FT_FACE_MEMORY( face );
3360 result = vcmap->clazz->variant_list( vcmap, memory );
3368 /* documentation is in freetype.h */
3370 FT_EXPORT_DEF( FT_UInt32* )
3371 FT_Face_GetVariantsOfChar( FT_Face face,
3374 FT_UInt32 *result = NULL;
3379 FT_CharMap charmap = find_variant_selector_charmap( face );
3382 if ( charmap != NULL )
3384 FT_CMap vcmap = FT_CMAP( charmap );
3385 FT_Memory memory = FT_FACE_MEMORY( face );
3388 if ( charcode > 0xFFFFFFFFUL )
3390 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3391 FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3394 result = vcmap->clazz->charvariant_list( vcmap, memory,
3395 (FT_UInt32)charcode );
3402 /* documentation is in freetype.h */
3404 FT_EXPORT_DEF( FT_UInt32* )
3405 FT_Face_GetCharsOfVariant( FT_Face face,
3406 FT_ULong variantSelector )
3408 FT_UInt32 *result = NULL;
3413 FT_CharMap charmap = find_variant_selector_charmap( face );
3416 if ( charmap != NULL )
3418 FT_CMap vcmap = FT_CMAP( charmap );
3419 FT_Memory memory = FT_FACE_MEMORY( face );
3422 if ( variantSelector > 0xFFFFFFFFUL )
3424 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3425 FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3428 result = vcmap->clazz->variantchar_list( vcmap, memory,
3429 (FT_UInt32)variantSelector );
3437 /* documentation is in freetype.h */
3439 FT_EXPORT_DEF( FT_UInt )
3440 FT_Get_Name_Index( FT_Face face,
3441 FT_String* glyph_name )
3446 if ( face && FT_HAS_GLYPH_NAMES( face ) )
3448 FT_Service_GlyphDict service;
3451 FT_FACE_LOOKUP_SERVICE( face,
3455 if ( service && service->name_index )
3456 result = service->name_index( face, glyph_name );
3463 /* documentation is in freetype.h */
3465 FT_EXPORT_DEF( FT_Error )
3466 FT_Get_Glyph_Name( FT_Face face,
3467 FT_UInt glyph_index,
3469 FT_UInt buffer_max )
3471 FT_Error error = FT_Err_Invalid_Argument;
3474 /* clean up buffer */
3475 if ( buffer && buffer_max > 0 )
3476 ((FT_Byte*)buffer)[0] = 0;
3479 (FT_Long)glyph_index <= face->num_glyphs &&
3480 FT_HAS_GLYPH_NAMES( face ) )
3482 FT_Service_GlyphDict service;
3485 FT_FACE_LOOKUP_SERVICE( face,
3489 if ( service && service->get_name )
3490 error = service->get_name( face, glyph_index, buffer, buffer_max );
3497 /* documentation is in freetype.h */
3499 FT_EXPORT_DEF( const char* )
3500 FT_Get_Postscript_Name( FT_Face face )
3502 const char* result = NULL;
3510 FT_Service_PsFontName service;
3513 FT_FACE_LOOKUP_SERVICE( face,
3515 POSTSCRIPT_FONT_NAME );
3517 if ( service && service->get_ps_font_name )
3518 result = service->get_ps_font_name( face );
3526 /* documentation is in tttables.h */
3528 FT_EXPORT_DEF( void* )
3529 FT_Get_Sfnt_Table( FT_Face face,
3533 FT_Service_SFNT_Table service;
3536 if ( face && FT_IS_SFNT( face ) )
3538 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3539 if ( service != NULL )
3540 table = service->get_table( face, tag );
3547 /* documentation is in tttables.h */
3549 FT_EXPORT_DEF( FT_Error )
3550 FT_Load_Sfnt_Table( FT_Face face,
3556 FT_Service_SFNT_Table service;
3559 if ( !face || !FT_IS_SFNT( face ) )
3560 return FT_Err_Invalid_Face_Handle;
3562 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3563 if ( service == NULL )
3564 return FT_Err_Unimplemented_Feature;
3566 return service->load_table( face, tag, offset, buffer, length );
3570 /* documentation is in tttables.h */
3572 FT_EXPORT_DEF( FT_Error )
3573 FT_Sfnt_Table_Info( FT_Face face,
3574 FT_UInt table_index,
3578 FT_Service_SFNT_Table service;
3582 if ( !face || !FT_IS_SFNT( face ) )
3583 return FT_Err_Invalid_Face_Handle;
3585 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3586 if ( service == NULL )
3587 return FT_Err_Unimplemented_Feature;
3589 return service->table_info( face, table_index, tag, &offset, length );
3593 /* documentation is in tttables.h */
3595 FT_EXPORT_DEF( FT_ULong )
3596 FT_Get_CMap_Language_ID( FT_CharMap charmap )
3598 FT_Service_TTCMaps service;
3600 TT_CMapInfo cmap_info;
3603 if ( !charmap || !charmap->face )
3606 face = charmap->face;
3607 FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
3608 if ( service == NULL )
3610 if ( service->get_cmap_info( charmap, &cmap_info ))
3613 return cmap_info.language;
3617 /* documentation is in tttables.h */
3619 FT_EXPORT_DEF( FT_Long )
3620 FT_Get_CMap_Format( FT_CharMap charmap )
3622 FT_Service_TTCMaps service;
3624 TT_CMapInfo cmap_info;
3627 if ( !charmap || !charmap->face )
3630 face = charmap->face;
3631 FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
3632 if ( service == NULL )
3634 if ( service->get_cmap_info( charmap, &cmap_info ))
3637 return cmap_info.format;
3641 /* documentation is in ftsizes.h */
3643 FT_EXPORT_DEF( FT_Error )
3644 FT_Activate_Size( FT_Size size )
3650 return FT_Err_Invalid_Argument;
3653 if ( face == NULL || face->driver == NULL )
3654 return FT_Err_Invalid_Argument;
3656 /* we don't need anything more complex than that; all size objects */
3657 /* are already listed by the face */
3664 /*************************************************************************/
3665 /*************************************************************************/
3666 /*************************************************************************/
3669 /**** R E N D E R E R S ****/
3672 /*************************************************************************/
3673 /*************************************************************************/
3674 /*************************************************************************/
3676 /* lookup a renderer by glyph format in the library's list */
3677 FT_BASE_DEF( FT_Renderer )
3678 FT_Lookup_Renderer( FT_Library library,
3679 FT_Glyph_Format format,
3683 FT_Renderer result = 0;
3689 cur = library->renderers.head;
3694 cur = (*node)->next;
3700 FT_Renderer renderer = FT_RENDERER( cur->data );
3703 if ( renderer->glyph_format == format )
3720 ft_lookup_glyph_renderer( FT_GlyphSlot slot )
3722 FT_Face face = slot->face;
3723 FT_Library library = FT_FACE_LIBRARY( face );
3724 FT_Renderer result = library->cur_renderer;
3727 if ( !result || result->glyph_format != slot->format )
3728 result = FT_Lookup_Renderer( library, slot->format, 0 );
3735 ft_set_current_renderer( FT_Library library )
3737 FT_Renderer renderer;
3740 renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE, 0 );
3741 library->cur_renderer = renderer;
3746 ft_add_renderer( FT_Module module )
3748 FT_Library library = module->library;
3749 FT_Memory memory = library->memory;
3751 FT_ListNode node = NULL;
3754 if ( FT_NEW( node ) )
3758 FT_Renderer render = FT_RENDERER( module );
3759 FT_Renderer_Class* clazz = (FT_Renderer_Class*)module->clazz;
3762 render->clazz = clazz;
3763 render->glyph_format = clazz->glyph_format;
3765 /* allocate raster object if needed */
3766 if ( clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
3767 clazz->raster_class->raster_new )
3769 error = clazz->raster_class->raster_new( memory, &render->raster );
3773 render->raster_render = clazz->raster_class->raster_render;
3774 render->render = clazz->render_glyph;
3778 node->data = module;
3779 FT_List_Add( &library->renderers, node );
3781 ft_set_current_renderer( library );
3794 ft_remove_renderer( FT_Module module )
3796 FT_Library library = module->library;
3797 FT_Memory memory = library->memory;
3801 node = FT_List_Find( &library->renderers, module );
3804 FT_Renderer render = FT_RENDERER( module );
3807 /* release raster object, if any */
3808 if ( render->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
3810 render->clazz->raster_class->raster_done( render->raster );
3812 /* remove from list */
3813 FT_List_Remove( &library->renderers, node );
3816 ft_set_current_renderer( library );
3821 /* documentation is in ftrender.h */
3823 FT_EXPORT_DEF( FT_Renderer )
3824 FT_Get_Renderer( FT_Library library,
3825 FT_Glyph_Format format )
3827 /* test for valid `library' delayed to FT_Lookup_Renderer() */
3829 return FT_Lookup_Renderer( library, format, 0 );
3833 /* documentation is in ftrender.h */
3835 FT_EXPORT_DEF( FT_Error )
3836 FT_Set_Renderer( FT_Library library,
3837 FT_Renderer renderer,
3839 FT_Parameter* parameters )
3842 FT_Error error = FT_Err_Ok;
3846 return FT_Err_Invalid_Library_Handle;
3849 return FT_Err_Invalid_Argument;
3851 node = FT_List_Find( &library->renderers, renderer );
3854 error = FT_Err_Invalid_Argument;
3858 FT_List_Up( &library->renderers, node );
3860 if ( renderer->glyph_format == FT_GLYPH_FORMAT_OUTLINE )
3861 library->cur_renderer = renderer;
3863 if ( num_params > 0 )
3865 FT_Renderer_SetModeFunc set_mode = renderer->clazz->set_mode;
3868 for ( ; num_params > 0; num_params-- )
3870 error = set_mode( renderer, parameters->tag, parameters->data );
3881 FT_BASE_DEF( FT_Error )
3882 FT_Render_Glyph_Internal( FT_Library library,
3884 FT_Render_Mode render_mode )
3886 FT_Error error = FT_Err_Ok;
3887 FT_Renderer renderer;
3890 /* if it is already a bitmap, no need to do anything */
3891 switch ( slot->format )
3893 case FT_GLYPH_FORMAT_BITMAP: /* already a bitmap, don't do anything */
3898 FT_ListNode node = 0;
3902 /* small shortcut for the very common case */
3903 if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
3905 renderer = library->cur_renderer;
3906 node = library->renderers.head;
3909 renderer = FT_Lookup_Renderer( library, slot->format, &node );
3911 error = FT_Err_Unimplemented_Feature;
3914 error = renderer->render( renderer, slot, render_mode, NULL );
3916 FT_ERROR_BASE( error ) != FT_Err_Cannot_Render_Glyph )
3919 /* FT_Err_Cannot_Render_Glyph is returned if the render mode */
3920 /* is unsupported by the current renderer for this glyph image */
3923 /* now, look for another renderer that supports the same */
3925 renderer = FT_Lookup_Renderer( library, slot->format, &node );
3929 /* if we changed the current renderer for the glyph image format */
3930 /* we need to select it as the next current one */
3931 if ( !error && update && renderer )
3932 FT_Set_Renderer( library, renderer, 0, 0 );
3940 /* documentation is in freetype.h */
3942 FT_EXPORT_DEF( FT_Error )
3943 FT_Render_Glyph( FT_GlyphSlot slot,
3944 FT_Render_Mode render_mode )
3949 if ( !slot || !slot->face )
3950 return FT_Err_Invalid_Argument;
3952 library = FT_FACE_LIBRARY( slot->face );
3954 return FT_Render_Glyph_Internal( library, slot, render_mode );
3958 /*************************************************************************/
3959 /*************************************************************************/
3960 /*************************************************************************/
3963 /**** M O D U L E S ****/
3966 /*************************************************************************/
3967 /*************************************************************************/
3968 /*************************************************************************/
3971 /*************************************************************************/
3974 /* Destroy_Module */
3977 /* Destroys a given module object. For drivers, this also destroys */
3978 /* all child faces. */
3981 /* module :: A handle to the target driver object. */
3984 /* The driver _must_ be LOCKED! */
3987 Destroy_Module( FT_Module module )
3989 FT_Memory memory = module->memory;
3990 FT_Module_Class* clazz = module->clazz;
3991 FT_Library library = module->library;
3994 /* finalize client-data - before anything else */
3995 if ( module->generic.finalizer )
3996 module->generic.finalizer( module );
3998 if ( library && library->auto_hinter == module )
3999 library->auto_hinter = 0;
4001 /* if the module is a renderer */
4002 if ( FT_MODULE_IS_RENDERER( module ) )
4003 ft_remove_renderer( module );
4005 /* if the module is a font driver, add some steps */
4006 if ( FT_MODULE_IS_DRIVER( module ) )
4007 Destroy_Driver( FT_DRIVER( module ) );
4009 /* finalize the module object */
4010 if ( clazz->module_done )
4011 clazz->module_done( module );
4018 /* documentation is in ftmodapi.h */
4020 FT_EXPORT_DEF( FT_Error )
4021 FT_Add_Module( FT_Library library,
4022 const FT_Module_Class* clazz )
4030 #define FREETYPE_VER_FIXED ( ( (FT_Long)FREETYPE_MAJOR << 16 ) | \
4034 return FT_Err_Invalid_Library_Handle;
4037 return FT_Err_Invalid_Argument;
4039 /* check freetype version */
4040 if ( clazz->module_requires > FREETYPE_VER_FIXED )
4041 return FT_Err_Invalid_Version;
4043 /* look for a module with the same name in the library's table */
4044 for ( nn = 0; nn < library->num_modules; nn++ )
4046 module = library->modules[nn];
4047 if ( ft_strcmp( module->clazz->module_name, clazz->module_name ) == 0 )
4049 /* this installed module has the same name, compare their versions */
4050 if ( clazz->module_version <= module->clazz->module_version )
4051 return FT_Err_Lower_Module_Version;
4053 /* remove the module from our list, then exit the loop to replace */
4054 /* it by our new version.. */
4055 FT_Remove_Module( library, module );
4060 memory = library->memory;
4063 if ( library->num_modules >= FT_MAX_MODULES )
4065 error = FT_Err_Too_Many_Drivers;
4069 /* allocate module object */
4070 if ( FT_ALLOC( module, clazz->module_size ) )
4073 /* base initialization */
4074 module->library = library;
4075 module->memory = memory;
4076 module->clazz = (FT_Module_Class*)clazz;
4078 /* check whether the module is a renderer - this must be performed */
4079 /* before the normal module initialization */
4080 if ( FT_MODULE_IS_RENDERER( module ) )
4082 /* add to the renderers list */
4083 error = ft_add_renderer( module );
4088 /* is the module a auto-hinter? */
4089 if ( FT_MODULE_IS_HINTER( module ) )
4090 library->auto_hinter = module;
4092 /* if the module is a font driver */
4093 if ( FT_MODULE_IS_DRIVER( module ) )
4095 /* allocate glyph loader if needed */
4096 FT_Driver driver = FT_DRIVER( module );
4099 driver->clazz = (FT_Driver_Class)module->clazz;
4100 if ( FT_DRIVER_USES_OUTLINES( driver ) )
4102 error = FT_GlyphLoader_New( memory, &driver->glyph_loader );
4108 if ( clazz->module_init )
4110 error = clazz->module_init( module );
4115 /* add module to the library's table */
4116 library->modules[library->num_modules++] = module;
4122 if ( FT_MODULE_IS_DRIVER( module ) )
4124 FT_Driver driver = FT_DRIVER( module );
4127 if ( FT_DRIVER_USES_OUTLINES( driver ) )
4128 FT_GlyphLoader_Done( driver->glyph_loader );
4131 if ( FT_MODULE_IS_RENDERER( module ) )
4133 FT_Renderer renderer = FT_RENDERER( module );
4136 if ( renderer->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
4138 renderer->clazz->raster_class->raster_done( renderer->raster );
4146 /* documentation is in ftmodapi.h */
4148 FT_EXPORT_DEF( FT_Module )
4149 FT_Get_Module( FT_Library library,
4150 const char* module_name )
4152 FT_Module result = 0;
4157 if ( !library || !module_name )
4160 cur = library->modules;
4161 limit = cur + library->num_modules;
4163 for ( ; cur < limit; cur++ )
4164 if ( ft_strcmp( cur[0]->clazz->module_name, module_name ) == 0 )
4174 /* documentation is in ftobjs.h */
4176 FT_BASE_DEF( const void* )
4177 FT_Get_Module_Interface( FT_Library library,
4178 const char* mod_name )
4183 /* test for valid `library' delayed to FT_Get_Module() */
4185 module = FT_Get_Module( library, mod_name );
4187 return module ? module->clazz->module_interface : 0;
4191 FT_BASE_DEF( FT_Pointer )
4192 ft_module_get_service( FT_Module module,
4193 const char* service_id )
4195 FT_Pointer result = NULL;
4199 FT_ASSERT( module->clazz && module->clazz->get_interface );
4201 /* first, look for the service in the module
4203 if ( module->clazz->get_interface )
4204 result = module->clazz->get_interface( module, service_id );
4206 if ( result == NULL )
4208 /* we didn't find it, look in all other modules then
4210 FT_Library library = module->library;
4211 FT_Module* cur = library->modules;
4212 FT_Module* limit = cur + library->num_modules;
4214 for ( ; cur < limit; cur++ )
4216 if ( cur[0] != module )
4218 FT_ASSERT( cur[0]->clazz );
4220 if ( cur[0]->clazz->get_interface )
4222 result = cur[0]->clazz->get_interface( cur[0], service_id );
4223 if ( result != NULL )
4235 /* documentation is in ftmodapi.h */
4237 FT_EXPORT_DEF( FT_Error )
4238 FT_Remove_Module( FT_Library library,
4241 /* try to find the module from the table, then remove it from there */
4244 return FT_Err_Invalid_Library_Handle;
4248 FT_Module* cur = library->modules;
4249 FT_Module* limit = cur + library->num_modules;
4252 for ( ; cur < limit; cur++ )
4254 if ( cur[0] == module )
4256 /* remove it from the table */
4257 library->num_modules--;
4259 while ( cur < limit )
4266 /* destroy the module */
4267 Destroy_Module( module );
4273 return FT_Err_Invalid_Driver_Handle;
4277 /*************************************************************************/
4278 /*************************************************************************/
4279 /*************************************************************************/
4282 /**** L I B R A R Y ****/
4285 /*************************************************************************/
4286 /*************************************************************************/
4287 /*************************************************************************/
4290 /* documentation is in ftmodapi.h */
4292 FT_EXPORT_DEF( FT_Error )
4293 FT_Reference_Library( FT_Library library )
4295 library->refcount++;
4301 /* documentation is in ftmodapi.h */
4303 FT_EXPORT_DEF( FT_Error )
4304 FT_New_Library( FT_Memory memory,
4305 FT_Library *alibrary )
4307 FT_Library library = NULL;
4312 return FT_Err_Invalid_Argument;
4314 #ifdef FT_DEBUG_LEVEL_ERROR
4315 /* init debugging support */
4319 /* first of all, allocate the library object */
4320 if ( FT_NEW( library ) )
4323 library->memory = memory;
4325 #ifdef FT_CONFIG_OPTION_PIC
4326 /* initialize position independent code containers */
4327 error = ft_pic_container_init( library );
4332 /* allocate the render pool */
4333 library->raster_pool_size = FT_RENDER_POOL_SIZE;
4334 #if FT_RENDER_POOL_SIZE > 0
4335 if ( FT_ALLOC( library->raster_pool, FT_RENDER_POOL_SIZE ) )
4339 library->version_major = FREETYPE_MAJOR;
4340 library->version_minor = FREETYPE_MINOR;
4341 library->version_patch = FREETYPE_PATCH;
4343 library->refcount = 1;
4346 *alibrary = library;
4351 #ifdef FT_CONFIG_OPTION_PIC
4352 ft_pic_container_destroy( library );
4359 /* documentation is in freetype.h */
4361 FT_EXPORT_DEF( void )
4362 FT_Library_Version( FT_Library library,
4374 major = library->version_major;
4375 minor = library->version_minor;
4376 patch = library->version_patch;
4390 /* documentation is in ftmodapi.h */
4392 FT_EXPORT_DEF( FT_Error )
4393 FT_Done_Library( FT_Library library )
4399 return FT_Err_Invalid_Library_Handle;
4401 library->refcount--;
4402 if ( library->refcount > 0 )
4405 memory = library->memory;
4407 /* Discard client-data */
4408 if ( library->generic.finalizer )
4409 library->generic.finalizer( library );
4412 * Close all faces in the library. If we don't do this, we can have
4413 * some subtle memory leaks.
4417 * - the cff font driver uses the pshinter module in cff_size_done
4418 * - if the pshinter module is destroyed before the cff font driver,
4419 * opened FT_Face objects managed by the driver are not properly
4420 * destroyed, resulting in a memory leak
4422 * Some faces are dependent on other faces, like Type42 faces that
4423 * depend on TrueType faces synthesized internally.
4425 * The order of drivers should be specified in driver_name[].
4429 const char* driver_name[] = { "type42", NULL };
4433 m < sizeof ( driver_name ) / sizeof ( driver_name[0] );
4436 for ( n = 0; n < library->num_modules; n++ )
4438 FT_Module module = library->modules[n];
4439 const char* module_name = module->clazz->module_name;
4443 if ( driver_name[m] &&
4444 ft_strcmp( module_name, driver_name[m] ) != 0 )
4447 if ( ( module->clazz->module_flags & FT_MODULE_FONT_DRIVER ) == 0 )
4450 FT_TRACE7(( "FT_Done_Library: close faces for %s\n", module_name ));
4452 faces = &FT_DRIVER( module )->faces_list;
4453 while ( faces->head )
4455 FT_Done_Face( FT_FACE( faces->head->data ) );
4457 FT_TRACE0(( "FT_Done_Library: failed to free some faces\n" ));
4463 /* Close all other modules in the library */
4465 /* XXX Modules are removed in the reversed order so that */
4466 /* type42 module is removed before truetype module. This */
4467 /* avoids double free in some occasions. It is a hack. */
4468 while ( library->num_modules > 0 )
4469 FT_Remove_Module( library,
4470 library->modules[library->num_modules - 1] );
4476 for ( n = 0; n < library->num_modules; n++ )
4478 FT_Module module = library->modules[n];
4483 Destroy_Module( module );
4484 library->modules[n] = 0;
4490 /* Destroy raster objects */
4491 FT_FREE( library->raster_pool );
4492 library->raster_pool_size = 0;
4494 #ifdef FT_CONFIG_OPTION_PIC
4495 /* Destroy pic container contents */
4496 ft_pic_container_destroy( library );
4506 /* documentation is in ftmodapi.h */
4508 FT_EXPORT_DEF( void )
4509 FT_Set_Debug_Hook( FT_Library library,
4511 FT_DebugHook_Func debug_hook )
4513 if ( library && debug_hook &&
4515 ( sizeof ( library->debug_hooks ) / sizeof ( void* ) ) )
4516 library->debug_hooks[hook_index] = debug_hook;
4520 /* documentation is in ftmodapi.h */
4522 FT_EXPORT_DEF( FT_TrueTypeEngineType )
4523 FT_Get_TrueType_Engine_Type( FT_Library library )
4525 FT_TrueTypeEngineType result = FT_TRUETYPE_ENGINE_TYPE_NONE;
4530 FT_Module module = FT_Get_Module( library, "truetype" );
4535 FT_Service_TrueTypeEngine service;
4538 service = (FT_Service_TrueTypeEngine)
4539 ft_module_get_service( module,
4540 FT_SERVICE_ID_TRUETYPE_ENGINE );
4542 result = service->engine_type;
4550 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
4552 FT_BASE_DEF( FT_Error )
4553 ft_stub_set_char_sizes( FT_Size size,
4559 FT_Size_RequestRec req;
4560 FT_Driver driver = size->face->driver;
4563 if ( driver->clazz->request_size )
4565 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
4567 req.height = height;
4569 if ( horz_res == 0 )
4570 horz_res = vert_res;
4572 if ( vert_res == 0 )
4573 vert_res = horz_res;
4575 if ( horz_res == 0 )
4576 horz_res = vert_res = 72;
4578 req.horiResolution = horz_res;
4579 req.vertResolution = vert_res;
4581 return driver->clazz->request_size( size, &req );
4588 FT_BASE_DEF( FT_Error )
4589 ft_stub_set_pixel_sizes( FT_Size size,
4593 FT_Size_RequestRec req;
4594 FT_Driver driver = size->face->driver;
4597 if ( driver->clazz->request_size )
4599 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
4600 req.width = width << 6;
4601 req.height = height << 6;
4602 req.horiResolution = 0;
4603 req.vertResolution = 0;
4605 return driver->clazz->request_size( size, &req );
4611 #endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
4614 /* documentation is in freetype.h */
4616 FT_EXPORT_DEF( FT_Error )
4617 FT_Get_SubGlyph_Info( FT_GlyphSlot glyph,
4623 FT_Matrix *p_transform )
4625 FT_Error error = FT_Err_Invalid_Argument;
4630 glyph->format == FT_GLYPH_FORMAT_COMPOSITE &&
4631 sub_index < glyph->num_subglyphs )
4633 FT_SubGlyph subg = glyph->subglyphs + sub_index;
4636 *p_index = subg->index;
4637 *p_flags = subg->flags;
4638 *p_arg1 = subg->arg1;
4639 *p_arg2 = subg->arg2;
4640 *p_transform = subg->transform;