1 /***************************************************************************/
5 /* The FreeType private base classes (body). */
7 /* Copyright 1996-2011 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
22 #include FT_INTERNAL_VALIDATE_H
23 #include FT_INTERNAL_OBJECTS_H
24 #include FT_INTERNAL_DEBUG_H
25 #include FT_INTERNAL_RFORK_H
26 #include FT_INTERNAL_STREAM_H
27 #include FT_INTERNAL_SFNT_H /* for SFNT_Load_Table_Func */
28 #include FT_TRUETYPE_TABLES_H
29 #include FT_TRUETYPE_TAGS_H
30 #include FT_TRUETYPE_IDS_H
32 #include FT_SERVICE_SFNT_H
33 #include FT_SERVICE_POSTSCRIPT_NAME_H
34 #include FT_SERVICE_GLYPH_DICT_H
35 #include FT_SERVICE_TT_CMAP_H
36 #include FT_SERVICE_KERNING_H
37 #include FT_SERVICE_TRUETYPE_ENGINE_H
39 #ifdef FT_CONFIG_OPTION_MAC_FONTS
43 #define GRID_FIT_METRICS
46 FT_BASE_DEF( FT_Pointer )
47 ft_service_list_lookup( FT_ServiceDesc service_descriptors,
48 const char* service_id )
50 FT_Pointer result = NULL;
51 FT_ServiceDesc desc = service_descriptors;
54 if ( desc && service_id )
56 for ( ; desc->serv_id != NULL; desc++ )
58 if ( ft_strcmp( desc->serv_id, service_id ) == 0 )
60 result = (FT_Pointer)desc->serv_data;
71 ft_validator_init( FT_Validator valid,
74 FT_ValidationLevel level )
79 valid->error = FT_Err_Ok;
84 ft_validator_run( FT_Validator valid )
86 /* This function doesn't work! None should call it. */
94 ft_validator_error( FT_Validator valid,
97 /* since the cast below also disables the compiler's */
98 /* type check, we introduce a dummy variable, which */
99 /* will be optimized away */
100 volatile ft_jmp_buf* jump_buffer = &valid->jump_buffer;
103 valid->error = error;
105 /* throw away volatileness; use `jump_buffer' or the */
106 /* compiler may warn about an unused local variable */
107 ft_longjmp( *(ft_jmp_buf*) jump_buffer, 1 );
111 /*************************************************************************/
112 /*************************************************************************/
113 /*************************************************************************/
116 /**** S T R E A M ****/
119 /*************************************************************************/
120 /*************************************************************************/
121 /*************************************************************************/
124 /* create a new input stream from an FT_Open_Args structure */
126 FT_BASE_DEF( FT_Error )
127 FT_Stream_New( FT_Library library,
128 const FT_Open_Args* args,
133 FT_Stream stream = NULL;
139 return FT_Err_Invalid_Library_Handle;
142 return FT_Err_Invalid_Argument;
144 memory = library->memory;
146 if ( FT_NEW( stream ) )
149 stream->memory = memory;
151 if ( args->flags & FT_OPEN_MEMORY )
153 /* create a memory-based stream */
154 FT_Stream_OpenMemory( stream,
155 (const FT_Byte*)args->memory_base,
159 #ifndef FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT
161 else if ( args->flags & FT_OPEN_PATHNAME )
163 /* create a normal system stream */
164 error = FT_Stream_Open( stream, args->pathname );
165 stream->pathname.pointer = args->pathname;
167 else if ( ( args->flags & FT_OPEN_STREAM ) && args->stream )
169 /* use an existing, user-provided stream */
171 /* in this case, we do not need to allocate a new stream object */
172 /* since the caller is responsible for closing it himself */
174 stream = args->stream;
180 error = FT_Err_Invalid_Argument;
185 stream->memory = memory; /* just to be certain */
195 FT_Stream_Free( FT_Stream stream,
200 FT_Memory memory = stream->memory;
203 FT_Stream_Close( stream );
211 /*************************************************************************/
213 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
214 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
215 /* messages during execution. */
218 #define FT_COMPONENT trace_objs
221 /*************************************************************************/
222 /*************************************************************************/
223 /*************************************************************************/
226 /**** FACE, SIZE & GLYPH SLOT OBJECTS ****/
229 /*************************************************************************/
230 /*************************************************************************/
231 /*************************************************************************/
235 ft_glyphslot_init( FT_GlyphSlot slot )
237 FT_Driver driver = slot->face->driver;
238 FT_Driver_Class clazz = driver->clazz;
239 FT_Memory memory = driver->root.memory;
240 FT_Error error = FT_Err_Ok;
241 FT_Slot_Internal internal = NULL;
244 slot->library = driver->root.library;
246 if ( FT_NEW( internal ) )
249 slot->internal = internal;
251 if ( FT_DRIVER_USES_OUTLINES( driver ) )
252 error = FT_GlyphLoader_New( memory, &internal->loader );
254 if ( !error && clazz->init_slot )
255 error = clazz->init_slot( slot );
263 ft_glyphslot_free_bitmap( FT_GlyphSlot slot )
265 if ( slot->internal && ( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) )
267 FT_Memory memory = FT_FACE_MEMORY( slot->face );
270 FT_FREE( slot->bitmap.buffer );
271 slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
275 /* assume that the bitmap buffer was stolen or not */
276 /* allocated from the heap */
277 slot->bitmap.buffer = NULL;
283 ft_glyphslot_set_bitmap( FT_GlyphSlot slot,
286 ft_glyphslot_free_bitmap( slot );
288 slot->bitmap.buffer = buffer;
290 FT_ASSERT( (slot->internal->flags & FT_GLYPH_OWN_BITMAP) == 0 );
294 FT_BASE_DEF( FT_Error )
295 ft_glyphslot_alloc_bitmap( FT_GlyphSlot slot,
298 FT_Memory memory = FT_FACE_MEMORY( slot->face );
302 if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
303 FT_FREE( slot->bitmap.buffer );
305 slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
307 (void)FT_ALLOC( slot->bitmap.buffer, size );
313 ft_glyphslot_clear( FT_GlyphSlot slot )
315 /* free bitmap if needed */
316 ft_glyphslot_free_bitmap( slot );
318 /* clear all public fields in the glyph slot */
319 FT_ZERO( &slot->metrics );
320 FT_ZERO( &slot->outline );
322 slot->bitmap.width = 0;
323 slot->bitmap.rows = 0;
324 slot->bitmap.pitch = 0;
325 slot->bitmap.pixel_mode = 0;
326 /* `slot->bitmap.buffer' has been handled by ft_glyphslot_free_bitmap */
328 slot->bitmap_left = 0;
329 slot->bitmap_top = 0;
330 slot->num_subglyphs = 0;
332 slot->control_data = 0;
333 slot->control_len = 0;
335 slot->format = FT_GLYPH_FORMAT_NONE;
337 slot->linearHoriAdvance = 0;
338 slot->linearVertAdvance = 0;
345 ft_glyphslot_done( FT_GlyphSlot slot )
347 FT_Driver driver = slot->face->driver;
348 FT_Driver_Class clazz = driver->clazz;
349 FT_Memory memory = driver->root.memory;
352 if ( clazz->done_slot )
353 clazz->done_slot( slot );
355 /* free bitmap buffer if needed */
356 ft_glyphslot_free_bitmap( slot );
358 /* slot->internal might be NULL in out-of-memory situations */
359 if ( slot->internal )
361 /* free glyph loader */
362 if ( FT_DRIVER_USES_OUTLINES( driver ) )
364 FT_GlyphLoader_Done( slot->internal->loader );
365 slot->internal->loader = 0;
368 FT_FREE( slot->internal );
373 /* documentation is in ftobjs.h */
375 FT_BASE_DEF( FT_Error )
376 FT_New_GlyphSlot( FT_Face face,
377 FT_GlyphSlot *aslot )
381 FT_Driver_Class clazz;
383 FT_GlyphSlot slot = NULL;
386 if ( !face || !face->driver )
387 return FT_Err_Invalid_Argument;
389 driver = face->driver;
390 clazz = driver->clazz;
391 memory = driver->root.memory;
393 FT_TRACE4(( "FT_New_GlyphSlot: Creating new slot object\n" ));
394 if ( !FT_ALLOC( slot, clazz->slot_object_size ) )
398 error = ft_glyphslot_init( slot );
401 ft_glyphslot_done( slot );
406 slot->next = face->glyph;
417 FT_TRACE4(( "FT_New_GlyphSlot: Return %d\n", error ));
422 /* documentation is in ftobjs.h */
425 FT_Done_GlyphSlot( FT_GlyphSlot slot )
429 FT_Driver driver = slot->face->driver;
430 FT_Memory memory = driver->root.memory;
435 /* Remove slot from its parent face's list */
437 cur = slot->face->glyph;
444 slot->face->glyph = cur->next;
446 prev->next = cur->next;
448 ft_glyphslot_done( slot );
459 /* documentation is in freetype.h */
461 FT_EXPORT_DEF( void )
462 FT_Set_Transform( FT_Face face,
466 FT_Face_Internal internal;
472 internal = face->internal;
474 internal->transform_flags = 0;
478 internal->transform_matrix.xx = 0x10000L;
479 internal->transform_matrix.xy = 0;
480 internal->transform_matrix.yx = 0;
481 internal->transform_matrix.yy = 0x10000L;
482 matrix = &internal->transform_matrix;
485 internal->transform_matrix = *matrix;
487 /* set transform_flags bit flag 0 if `matrix' isn't the identity */
488 if ( ( matrix->xy | matrix->yx ) ||
489 matrix->xx != 0x10000L ||
490 matrix->yy != 0x10000L )
491 internal->transform_flags |= 1;
495 internal->transform_delta.x = 0;
496 internal->transform_delta.y = 0;
497 delta = &internal->transform_delta;
500 internal->transform_delta = *delta;
502 /* set transform_flags bit flag 1 if `delta' isn't the null vector */
503 if ( delta->x | delta->y )
504 internal->transform_flags |= 2;
509 ft_lookup_glyph_renderer( FT_GlyphSlot slot );
512 #ifdef GRID_FIT_METRICS
514 ft_glyphslot_grid_fit_metrics( FT_GlyphSlot slot,
517 FT_Glyph_Metrics* metrics = &slot->metrics;
518 FT_Pos right, bottom;
523 metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
524 metrics->horiBearingY = FT_PIX_CEIL ( metrics->horiBearingY );
526 right = FT_PIX_CEIL( metrics->vertBearingX + metrics->width );
527 bottom = FT_PIX_CEIL( metrics->vertBearingY + metrics->height );
529 metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
530 metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
532 metrics->width = right - metrics->vertBearingX;
533 metrics->height = bottom - metrics->vertBearingY;
537 metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
538 metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
540 right = FT_PIX_CEIL ( metrics->horiBearingX + metrics->width );
541 bottom = FT_PIX_FLOOR( metrics->horiBearingY - metrics->height );
543 metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
544 metrics->horiBearingY = FT_PIX_CEIL ( metrics->horiBearingY );
546 metrics->width = right - metrics->horiBearingX;
547 metrics->height = metrics->horiBearingY - bottom;
550 metrics->horiAdvance = FT_PIX_ROUND( metrics->horiAdvance );
551 metrics->vertAdvance = FT_PIX_ROUND( metrics->vertAdvance );
553 #endif /* GRID_FIT_METRICS */
556 /* documentation is in freetype.h */
558 FT_EXPORT_DEF( FT_Error )
559 FT_Load_Glyph( FT_Face face,
561 FT_Int32 load_flags )
567 FT_Bool autohint = FALSE;
569 TT_Face ttface = (TT_Face)face;
572 if ( !face || !face->size || !face->glyph )
573 return FT_Err_Invalid_Face_Handle;
575 /* The validity test for `glyph_index' is performed by the */
579 ft_glyphslot_clear( slot );
581 driver = face->driver;
582 library = driver->root.library;
583 hinter = library->auto_hinter;
585 /* resolve load flags dependencies */
587 if ( load_flags & FT_LOAD_NO_RECURSE )
588 load_flags |= FT_LOAD_NO_SCALE |
589 FT_LOAD_IGNORE_TRANSFORM;
591 if ( load_flags & FT_LOAD_NO_SCALE )
593 load_flags |= FT_LOAD_NO_HINTING |
596 load_flags &= ~FT_LOAD_RENDER;
600 * Determine whether we need to auto-hint or not.
601 * The general rules are:
603 * - Do only auto-hinting if we have a hinter module, a scalable font
604 * format dealing with outlines, and no transforms except simple
605 * slants and/or rotations by integer multiples of 90 degrees.
607 * - Then, auto-hint if FT_LOAD_FORCE_AUTOHINT is set or if we don't
608 * have a native font hinter.
610 * - Otherwise, auto-hint for LIGHT hinting mode or if there isn't
611 * any hinting bytecode in the TrueType/OpenType font.
613 * - Exception: The font is `tricky' and requires the native hinter to
618 !( load_flags & FT_LOAD_NO_HINTING ) &&
619 !( load_flags & FT_LOAD_NO_AUTOHINT ) &&
620 FT_DRIVER_IS_SCALABLE( driver ) &&
621 FT_DRIVER_USES_OUTLINES( driver ) &&
622 !FT_IS_TRICKY( face ) &&
623 ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) ||
624 ( face->internal->transform_matrix.yx == 0 &&
625 face->internal->transform_matrix.xx != 0 ) ||
626 ( face->internal->transform_matrix.xx == 0 &&
627 face->internal->transform_matrix.yx != 0 ) ) )
629 if ( ( load_flags & FT_LOAD_FORCE_AUTOHINT ) ||
630 !FT_DRIVER_HAS_HINTER( driver ) )
634 FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags );
637 /* the check for `num_locations' assures that we actually */
638 /* test for instructions in a TTF and not in a CFF-based OTF */
639 if ( mode == FT_RENDER_MODE_LIGHT ||
640 face->internal->ignore_unpatented_hinter ||
641 ( FT_IS_SFNT( face ) &&
642 ttface->num_locations &&
643 ttface->max_profile.maxSizeOfInstructions == 0 ) )
650 FT_AutoHinter_Service hinting;
653 /* try to load embedded bitmaps first if available */
655 /* XXX: This is really a temporary hack that should disappear */
656 /* promptly with FreeType 2.1! */
658 if ( FT_HAS_FIXED_SIZES( face ) &&
659 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
661 error = driver->clazz->load_glyph( slot, face->size,
663 load_flags | FT_LOAD_SBITS_ONLY );
665 if ( !error && slot->format == FT_GLYPH_FORMAT_BITMAP )
670 FT_Face_Internal internal = face->internal;
671 FT_Int transform_flags = internal->transform_flags;
674 /* since the auto-hinter calls FT_Load_Glyph by itself, */
675 /* make sure that glyphs aren't transformed */
676 internal->transform_flags = 0;
678 /* load auto-hinted outline */
679 hinting = (FT_AutoHinter_Service)hinter->clazz->module_interface;
681 error = hinting->load_glyph( (FT_AutoHinter)hinter,
683 glyph_index, load_flags );
685 internal->transform_flags = transform_flags;
690 error = driver->clazz->load_glyph( slot,
697 if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
699 /* check that the loaded outline is correct */
700 error = FT_Outline_Check( &slot->outline );
704 #ifdef GRID_FIT_METRICS
705 if ( !( load_flags & FT_LOAD_NO_HINTING ) )
706 ft_glyphslot_grid_fit_metrics( slot,
707 FT_BOOL( load_flags & FT_LOAD_VERTICAL_LAYOUT ) );
713 /* compute the advance */
714 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
717 slot->advance.y = slot->metrics.vertAdvance;
721 slot->advance.x = slot->metrics.horiAdvance;
725 /* compute the linear advance in 16.16 pixels */
726 if ( ( load_flags & FT_LOAD_LINEAR_DESIGN ) == 0 &&
727 ( FT_IS_SCALABLE( face ) ) )
729 FT_Size_Metrics* metrics = &face->size->metrics;
733 slot->linearHoriAdvance = FT_MulDiv( slot->linearHoriAdvance,
734 metrics->x_scale, 64 );
736 slot->linearVertAdvance = FT_MulDiv( slot->linearVertAdvance,
737 metrics->y_scale, 64 );
740 if ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) == 0 )
742 FT_Face_Internal internal = face->internal;
745 /* now, transform the glyph image if needed */
746 if ( internal->transform_flags )
749 FT_Renderer renderer = ft_lookup_glyph_renderer( slot );
753 error = renderer->clazz->transform_glyph(
755 &internal->transform_matrix,
756 &internal->transform_delta );
757 else if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
759 /* apply `standard' transformation if no renderer is available */
760 if ( internal->transform_flags & 1 )
761 FT_Outline_Transform( &slot->outline,
762 &internal->transform_matrix );
764 if ( internal->transform_flags & 2 )
765 FT_Outline_Translate( &slot->outline,
766 internal->transform_delta.x,
767 internal->transform_delta.y );
770 /* transform advance */
771 FT_Vector_Transform( &slot->advance, &internal->transform_matrix );
775 FT_TRACE5(( " x advance: %d\n" , slot->advance.x ));
776 FT_TRACE5(( " y advance: %d\n" , slot->advance.y ));
778 FT_TRACE5(( " linear x advance: %d\n" , slot->linearHoriAdvance ));
779 FT_TRACE5(( " linear y advance: %d\n" , slot->linearVertAdvance ));
781 /* do we need to render the image now? */
783 slot->format != FT_GLYPH_FORMAT_BITMAP &&
784 slot->format != FT_GLYPH_FORMAT_COMPOSITE &&
785 load_flags & FT_LOAD_RENDER )
787 FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags );
790 if ( mode == FT_RENDER_MODE_NORMAL &&
791 (load_flags & FT_LOAD_MONOCHROME ) )
792 mode = FT_RENDER_MODE_MONO;
794 error = FT_Render_Glyph( slot, mode );
802 /* documentation is in freetype.h */
804 FT_EXPORT_DEF( FT_Error )
805 FT_Load_Char( FT_Face face,
807 FT_Int32 load_flags )
813 return FT_Err_Invalid_Face_Handle;
815 glyph_index = (FT_UInt)char_code;
817 glyph_index = FT_Get_Char_Index( face, char_code );
819 return FT_Load_Glyph( face, glyph_index, load_flags );
823 /* destructor for sizes list */
825 destroy_size( FT_Memory memory,
829 /* finalize client-specific data */
830 if ( size->generic.finalizer )
831 size->generic.finalizer( size );
833 /* finalize format-specific stuff */
834 if ( driver->clazz->done_size )
835 driver->clazz->done_size( size );
837 FT_FREE( size->internal );
843 ft_cmap_done_internal( FT_CMap cmap );
847 destroy_charmaps( FT_Face face,
856 for ( n = 0; n < face->num_charmaps; n++ )
858 FT_CMap cmap = FT_CMAP( face->charmaps[n] );
861 ft_cmap_done_internal( cmap );
863 face->charmaps[n] = NULL;
866 FT_FREE( face->charmaps );
867 face->num_charmaps = 0;
871 /* destructor for faces list */
873 destroy_face( FT_Memory memory,
877 FT_Driver_Class clazz = driver->clazz;
880 /* discard auto-hinting data */
881 if ( face->autohint.finalizer )
882 face->autohint.finalizer( face->autohint.data );
884 /* Discard glyph slots for this face. */
885 /* Beware! FT_Done_GlyphSlot() changes the field `face->glyph' */
886 while ( face->glyph )
887 FT_Done_GlyphSlot( face->glyph );
889 /* discard all sizes for this face */
890 FT_List_Finalize( &face->sizes_list,
891 (FT_List_Destructor)destroy_size,
896 /* now discard client data */
897 if ( face->generic.finalizer )
898 face->generic.finalizer( face );
900 /* discard charmaps */
901 destroy_charmaps( face, memory );
903 /* finalize format-specific stuff */
904 if ( clazz->done_face )
905 clazz->done_face( face );
907 /* close the stream for this face if needed */
910 ( face->face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
915 if ( face->internal )
917 FT_FREE( face->internal );
924 Destroy_Driver( FT_Driver driver )
926 FT_List_Finalize( &driver->faces_list,
927 (FT_List_Destructor)destroy_face,
931 /* check whether we need to drop the driver's glyph loader */
932 if ( FT_DRIVER_USES_OUTLINES( driver ) )
933 FT_GlyphLoader_Done( driver->glyph_loader );
937 /*************************************************************************/
940 /* find_unicode_charmap */
943 /* This function finds a Unicode charmap, if there is one. */
944 /* And if there is more than one, it tries to favour the more */
945 /* extensive one, i.e., one that supports UCS-4 against those which */
946 /* are limited to the BMP (said UCS-2 encoding.) */
948 /* This function is called from open_face() (just below), and also */
949 /* from FT_Select_Charmap( ..., FT_ENCODING_UNICODE ). */
952 find_unicode_charmap( FT_Face face )
958 /* caller should have already checked that `face' is valid */
961 first = face->charmaps;
964 return FT_Err_Invalid_CharMap_Handle;
967 * The original TrueType specification(s) only specified charmap
968 * formats that are capable of mapping 8 or 16 bit character codes to
971 * However, recent updates to the Apple and OpenType specifications
972 * introduced new formats that are capable of mapping 32-bit character
973 * codes as well. And these are already used on some fonts, mainly to
974 * map non-BMP Asian ideographs as defined in Unicode.
976 * For compatibility purposes, these fonts generally come with
977 * *several* Unicode charmaps:
979 * - One of them in the "old" 16-bit format, that cannot access
980 * all glyphs in the font.
982 * - Another one in the "new" 32-bit format, that can access all
985 * This function has been written to always favor a 32-bit charmap
986 * when found. Otherwise, a 16-bit one is returned when found.
989 /* Since the `interesting' table, with IDs (3,10), is normally the */
990 /* last one, we loop backwards. This loses with type1 fonts with */
991 /* non-BMP characters (<.0001%), this wins with .ttf with non-BMP */
992 /* chars (.01% ?), and this is the same about 99.99% of the time! */
994 cur = first + face->num_charmaps; /* points after the last one */
996 for ( ; --cur >= first; )
998 if ( cur[0]->encoding == FT_ENCODING_UNICODE )
1000 /* XXX If some new encodings to represent UCS-4 are added, */
1001 /* they should be added here. */
1002 if ( ( cur[0]->platform_id == TT_PLATFORM_MICROSOFT &&
1003 cur[0]->encoding_id == TT_MS_ID_UCS_4 ) ||
1004 ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
1005 cur[0]->encoding_id == TT_APPLE_ID_UNICODE_32 ) )
1007 #ifdef FT_MAX_CHARMAP_CACHEABLE
1008 if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1010 FT_ERROR(( "find_unicode_charmap: UCS-4 cmap is found "
1011 "at too late position (%d)\n", cur - first ));
1015 face->charmap = cur[0];
1021 /* We do not have any UCS-4 charmap. */
1022 /* Do the loop again and search for UCS-2 charmaps. */
1023 cur = first + face->num_charmaps;
1025 for ( ; --cur >= first; )
1027 if ( cur[0]->encoding == FT_ENCODING_UNICODE )
1029 #ifdef FT_MAX_CHARMAP_CACHEABLE
1030 if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1032 FT_ERROR(( "find_unicode_charmap: UCS-2 cmap is found "
1033 "at too late position (%d)\n", cur - first ));
1037 face->charmap = cur[0];
1042 return FT_Err_Invalid_CharMap_Handle;
1046 /*************************************************************************/
1049 /* find_variant_selector_charmap */
1052 /* This function finds the variant selector charmap, if there is one. */
1053 /* There can only be one (platform=0, specific=5, format=14). */
1056 find_variant_selector_charmap( FT_Face face )
1063 /* caller should have already checked that `face' is valid */
1066 first = face->charmaps;
1071 end = first + face->num_charmaps; /* points after the last one */
1073 for ( cur = first; cur < end; ++cur )
1075 if ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
1076 cur[0]->encoding_id == TT_APPLE_ID_VARIANT_SELECTOR &&
1077 FT_Get_CMap_Format( cur[0] ) == 14 )
1079 #ifdef FT_MAX_CHARMAP_CACHEABLE
1080 if ( cur - first > FT_MAX_CHARMAP_CACHEABLE )
1082 FT_ERROR(( "find_unicode_charmap: UVS cmap is found "
1083 "at too late position (%d)\n", cur - first ));
1095 /*************************************************************************/
1101 /* This function does some work for FT_Open_Face(). */
1104 open_face( FT_Driver driver,
1108 FT_Parameter* params,
1112 FT_Driver_Class clazz;
1114 FT_Error error, error2;
1115 FT_Face_Internal internal = NULL;
1118 clazz = driver->clazz;
1119 memory = driver->root.memory;
1121 /* allocate the face object and perform basic initialization */
1122 if ( FT_ALLOC( face, clazz->face_object_size ) )
1125 if ( FT_NEW( internal ) )
1128 face->internal = internal;
1130 face->driver = driver;
1131 face->memory = memory;
1132 face->stream = stream;
1134 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1139 face->internal->incremental_interface = 0;
1140 for ( i = 0; i < num_params && !face->internal->incremental_interface;
1142 if ( params[i].tag == FT_PARAM_TAG_INCREMENTAL )
1143 face->internal->incremental_interface =
1144 (FT_Incremental_Interface)params[i].data;
1148 if ( clazz->init_face )
1149 error = clazz->init_face( stream,
1157 /* select Unicode charmap by default */
1158 error2 = find_unicode_charmap( face );
1160 /* if no Unicode charmap can be found, FT_Err_Invalid_CharMap_Handle */
1163 /* no error should happen, but we want to play safe */
1164 if ( error2 && error2 != FT_Err_Invalid_CharMap_Handle )
1175 destroy_charmaps( face, memory );
1176 if ( clazz->done_face )
1177 clazz->done_face( face );
1178 FT_FREE( internal );
1187 /* there's a Mac-specific extended implementation of FT_New_Face() */
1188 /* in src/base/ftmac.c */
1190 #if !defined( FT_MACINTOSH ) || defined( DARWIN_NO_CARBON )
1192 /* documentation is in freetype.h */
1194 FT_EXPORT_DEF( FT_Error )
1195 FT_New_Face( FT_Library library,
1196 const char* pathname,
1203 /* test for valid `library' and `aface' delayed to FT_Open_Face() */
1205 return FT_Err_Invalid_Argument;
1207 args.flags = FT_OPEN_PATHNAME;
1208 args.pathname = (char*)pathname;
1211 return FT_Open_Face( library, &args, face_index, aface );
1214 #endif /* defined( FT_MACINTOSH ) && !defined( DARWIN_NO_CARBON ) */
1217 /* documentation is in freetype.h */
1219 FT_EXPORT_DEF( FT_Error )
1220 FT_New_Memory_Face( FT_Library library,
1221 const FT_Byte* file_base,
1229 /* test for valid `library' and `face' delayed to FT_Open_Face() */
1231 return FT_Err_Invalid_Argument;
1233 args.flags = FT_OPEN_MEMORY;
1234 args.memory_base = file_base;
1235 args.memory_size = file_size;
1238 return FT_Open_Face( library, &args, face_index, aface );
1242 #ifdef FT_CONFIG_OPTION_MAC_FONTS
1244 /* The behavior here is very similar to that in base/ftmac.c, but it */
1245 /* is designed to work on non-mac systems, so no mac specific calls. */
1247 /* We look at the file and determine if it is a mac dfont file or a mac */
1248 /* resource file, or a macbinary file containing a mac resource file. */
1250 /* Unlike ftmac I'm not going to look at a `FOND'. I don't really see */
1251 /* the point, especially since there may be multiple `FOND' resources. */
1252 /* Instead I'll just look for `sfnt' and `POST' resources, ordered as */
1253 /* they occur in the file. */
1255 /* Note that multiple `POST' resources do not mean multiple postscript */
1256 /* fonts; they all get jammed together to make what is essentially a */
1259 /* We aren't interested in `NFNT' or `FONT' bitmap resources. */
1261 /* As soon as we get an `sfnt' load it into memory and pass it off to */
1264 /* If we have a (set of) `POST' resources, massage them into a (memory) */
1265 /* pfb file and pass that to FT_Open_Face. (As with ftmac.c I'm not */
1266 /* going to try to save the kerning info. After all that lives in the */
1267 /* `FOND' which isn't in the file containing the `POST' resources so */
1268 /* we don't really have access to it. */
1271 /* Finalizer for a memory stream; gets called by FT_Done_Face(). */
1272 /* It frees the memory it uses. */
1275 memory_stream_close( FT_Stream stream )
1277 FT_Memory memory = stream->memory;
1280 FT_FREE( stream->base );
1288 /* Create a new memory stream from a buffer and a size. */
1291 new_memory_stream( FT_Library library,
1294 FT_Stream_CloseFunc close,
1295 FT_Stream *astream )
1299 FT_Stream stream = NULL;
1303 return FT_Err_Invalid_Library_Handle;
1306 return FT_Err_Invalid_Argument;
1309 memory = library->memory;
1310 if ( FT_NEW( stream ) )
1313 FT_Stream_OpenMemory( stream, base, size );
1315 stream->close = close;
1324 /* Create a new FT_Face given a buffer and a driver name. */
1326 FT_LOCAL_DEF( FT_Error )
1327 open_face_from_buffer( FT_Library library,
1331 const char* driver_name,
1336 FT_Stream stream = NULL;
1337 FT_Memory memory = library->memory;
1340 error = new_memory_stream( library,
1343 memory_stream_close,
1351 args.flags = FT_OPEN_STREAM;
1352 args.stream = stream;
1355 args.flags = args.flags | FT_OPEN_DRIVER;
1356 args.driver = FT_Get_Module( library, driver_name );
1360 /* At this point, face_index has served its purpose; */
1361 /* whoever calls this function has already used it to */
1362 /* locate the correct font data. We should not propagate */
1363 /* this index to FT_Open_Face() (unless it is negative). */
1365 if ( face_index > 0 )
1369 error = FT_Open_Face( library, &args, face_index, aface );
1371 if ( error == FT_Err_Ok )
1372 (*aface)->face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
1375 FT_Stream_Free( stream, 0 );
1378 FT_Stream_Close( stream );
1387 /* Look up `TYP1' or `CID ' table from sfnt table directory. */
1388 /* `offset' and `length' must exclude the binary header in tables. */
1390 /* Type 1 and CID-keyed font drivers should recognize sfnt-wrapped */
1391 /* format too. Here, since we can't expect that the TrueType font */
1392 /* driver is loaded unconditially, we must parse the font by */
1393 /* ourselves. We are only interested in the name of the table and */
1397 ft_lookup_PS_in_sfnt_stream( FT_Stream stream,
1401 FT_Bool* is_sfnt_cid )
1404 FT_UShort numTables;
1405 FT_Long pstable_index;
1412 *is_sfnt_cid = FALSE;
1414 /* TODO: support for sfnt-wrapped PS/CID in TTC format */
1416 /* version check for 'typ1' (should be ignored?) */
1417 if ( FT_READ_ULONG( tag ) )
1419 if ( tag != TTAG_typ1 )
1420 return FT_Err_Unknown_File_Format;
1422 if ( FT_READ_USHORT( numTables ) )
1424 if ( FT_STREAM_SKIP( 2 * 3 ) ) /* skip binary search header */
1428 *is_sfnt_cid = FALSE;
1430 for ( i = 0; i < numTables; i++ )
1432 if ( FT_READ_ULONG( tag ) || FT_STREAM_SKIP( 4 ) ||
1433 FT_READ_ULONG( *offset ) || FT_READ_ULONG( *length ) )
1436 if ( tag == TTAG_CID )
1441 *is_sfnt_cid = TRUE;
1442 if ( face_index < 0 )
1445 else if ( tag == TTAG_TYP1 )
1450 *is_sfnt_cid = FALSE;
1451 if ( face_index < 0 )
1454 if ( face_index >= 0 && pstable_index == face_index )
1457 return FT_Err_Table_Missing;
1461 FT_LOCAL_DEF( FT_Error )
1462 open_face_PS_from_sfnt_stream( FT_Library library,
1466 FT_Parameter *params,
1470 FT_Memory memory = library->memory;
1471 FT_ULong offset, length;
1473 FT_Bool is_sfnt_cid;
1474 FT_Byte* sfnt_ps = NULL;
1476 FT_UNUSED( num_params );
1477 FT_UNUSED( params );
1480 pos = FT_Stream_Pos( stream );
1482 error = ft_lookup_PS_in_sfnt_stream( stream,
1490 if ( FT_Stream_Seek( stream, pos + offset ) )
1493 if ( FT_ALLOC( sfnt_ps, (FT_Long)length ) )
1496 error = FT_Stream_Read( stream, (FT_Byte *)sfnt_ps, length );
1500 error = open_face_from_buffer( library,
1503 face_index < 0 ? face_index : 0,
1504 is_sfnt_cid ? "cid" : "type1",
1511 if ( error == FT_Err_Unknown_File_Format )
1513 error1 = FT_Stream_Seek( stream, pos );
1523 #if !defined( FT_MACINTOSH ) || defined( DARWIN_NO_CARBON )
1525 /* The resource header says we've got resource_cnt `POST' (type1) */
1526 /* resources in this file. They all need to be coalesced into */
1527 /* one lump which gets passed on to the type1 driver. */
1528 /* Here can be only one PostScript font in a file so face_index */
1529 /* must be 0 (or -1). */
1532 Mac_Read_POST_Resource( FT_Library library,
1535 FT_Long resource_cnt,
1539 FT_Error error = FT_Err_Cannot_Open_Resource;
1540 FT_Memory memory = library->memory;
1541 FT_Byte* pfb_data = NULL;
1544 FT_Long pfb_len, pfb_pos, pfb_lenpos;
1548 if ( face_index == -1 )
1550 if ( face_index != 0 )
1553 /* Find the length of all the POST resources, concatenated. Assume */
1554 /* worst case (each resource in its own section). */
1556 for ( i = 0; i < resource_cnt; ++i )
1558 error = FT_Stream_Seek( stream, offsets[i] );
1561 if ( FT_READ_LONG( temp ) )
1563 pfb_len += temp + 6;
1566 if ( FT_ALLOC( pfb_data, (FT_Long)pfb_len + 2 ) )
1570 pfb_data[1] = 1; /* Ascii section */
1571 pfb_data[2] = 0; /* 4-byte length, fill in later */
1580 for ( i = 0; i < resource_cnt; ++i )
1582 error = FT_Stream_Seek( stream, offsets[i] );
1585 if ( FT_READ_LONG( rlen ) )
1587 if ( FT_READ_USHORT( flags ) )
1589 FT_TRACE3(( "POST fragment[%d]: offsets=0x%08x, rlen=0x%08x, flags=0x%04x\n",
1590 i, offsets[i], rlen, flags ));
1592 /* postpone the check of rlen longer than buffer until FT_Stream_Read() */
1593 if ( ( flags >> 8 ) == 0 ) /* Comment, should not be loaded */
1596 /* the flags are part of the resource, so rlen >= 2. */
1597 /* but some fonts declare rlen = 0 for empty fragment */
1603 if ( ( flags >> 8 ) == type )
1607 if ( pfb_lenpos + 3 > pfb_len + 2 )
1609 pfb_data[pfb_lenpos ] = (FT_Byte)( len );
1610 pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
1611 pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
1612 pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
1614 if ( ( flags >> 8 ) == 5 ) /* End of font mark */
1617 if ( pfb_pos + 6 > pfb_len + 2 )
1619 pfb_data[pfb_pos++] = 0x80;
1624 pfb_data[pfb_pos++] = (FT_Byte)type;
1625 pfb_lenpos = pfb_pos;
1626 pfb_data[pfb_pos++] = 0; /* 4-byte length, fill in later */
1627 pfb_data[pfb_pos++] = 0;
1628 pfb_data[pfb_pos++] = 0;
1629 pfb_data[pfb_pos++] = 0;
1632 error = FT_Err_Cannot_Open_Resource;
1633 if ( pfb_pos > pfb_len || pfb_pos + rlen > pfb_len )
1636 error = FT_Stream_Read( stream, (FT_Byte *)pfb_data + pfb_pos, rlen );
1642 if ( pfb_pos + 2 > pfb_len + 2 )
1644 pfb_data[pfb_pos++] = 0x80;
1645 pfb_data[pfb_pos++] = 3;
1647 if ( pfb_lenpos + 3 > pfb_len + 2 )
1649 pfb_data[pfb_lenpos ] = (FT_Byte)( len );
1650 pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
1651 pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
1652 pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
1654 return open_face_from_buffer( library,
1662 FT_FREE( pfb_data );
1669 /* The resource header says we've got resource_cnt `sfnt' */
1670 /* (TrueType/OpenType) resources in this file. Look through */
1671 /* them for the one indicated by face_index, load it into mem, */
1672 /* pass it on the the truetype driver and return it. */
1675 Mac_Read_sfnt_Resource( FT_Library library,
1678 FT_Long resource_cnt,
1682 FT_Memory memory = library->memory;
1683 FT_Byte* sfnt_data = NULL;
1685 FT_Long flag_offset;
1688 FT_Long face_index_in_resource = 0;
1691 if ( face_index == -1 )
1693 if ( face_index >= resource_cnt )
1694 return FT_Err_Cannot_Open_Resource;
1696 flag_offset = offsets[face_index];
1697 error = FT_Stream_Seek( stream, flag_offset );
1701 if ( FT_READ_LONG( rlen ) )
1704 return FT_Err_Cannot_Open_Resource;
1706 error = open_face_PS_from_sfnt_stream( library,
1714 /* rewind sfnt stream before open_face_PS_from_sfnt_stream() */
1715 if ( FT_Stream_Seek( stream, flag_offset + 4 ) )
1718 if ( FT_ALLOC( sfnt_data, (FT_Long)rlen ) )
1720 error = FT_Stream_Read( stream, (FT_Byte *)sfnt_data, rlen );
1724 is_cff = rlen > 4 && !ft_memcmp( sfnt_data, "OTTO", 4 );
1725 error = open_face_from_buffer( library,
1728 face_index_in_resource,
1729 is_cff ? "cff" : "truetype",
1737 /* Check for a valid resource fork header, or a valid dfont */
1738 /* header. In a resource fork the first 16 bytes are repeated */
1739 /* at the location specified by bytes 4-7. In a dfont bytes */
1740 /* 4-7 point to 16 bytes of zeroes instead. */
1743 IsMacResource( FT_Library library,
1745 FT_Long resource_offset,
1749 FT_Memory memory = library->memory;
1751 FT_Long map_offset, rdara_pos;
1752 FT_Long *data_offsets;
1756 error = FT_Raccess_Get_HeaderInfo( library, stream, resource_offset,
1757 &map_offset, &rdara_pos );
1761 error = FT_Raccess_Get_DataOffsets( library, stream,
1762 map_offset, rdara_pos,
1764 &data_offsets, &count );
1767 error = Mac_Read_POST_Resource( library, stream, data_offsets, count,
1768 face_index, aface );
1769 FT_FREE( data_offsets );
1770 /* POST exists in an LWFN providing a single face */
1772 (*aface)->num_faces = 1;
1776 error = FT_Raccess_Get_DataOffsets( library, stream,
1777 map_offset, rdara_pos,
1779 &data_offsets, &count );
1782 FT_Long face_index_internal = face_index % count;
1785 error = Mac_Read_sfnt_Resource( library, stream, data_offsets, count,
1786 face_index_internal, aface );
1787 FT_FREE( data_offsets );
1789 (*aface)->num_faces = count;
1796 /* Check for a valid macbinary header, and if we find one */
1797 /* check that the (flattened) resource fork in it is valid. */
1800 IsMacBinary( FT_Library library,
1805 unsigned char header[128];
1807 FT_Long dlen, offset;
1810 if ( NULL == stream )
1811 return FT_Err_Invalid_Stream_Operation;
1813 error = FT_Stream_Seek( stream, 0 );
1817 error = FT_Stream_Read( stream, (FT_Byte*)header, 128 );
1821 if ( header[ 0] != 0 ||
1827 header[2 + header[1]] != 0 )
1828 return FT_Err_Unknown_File_Format;
1830 dlen = ( header[0x53] << 24 ) |
1831 ( header[0x54] << 16 ) |
1832 ( header[0x55] << 8 ) |
1835 rlen = ( header[0x57] << 24 ) |
1836 ( header[0x58] << 16 ) |
1837 ( header[0x59] << 8 ) |
1840 offset = 128 + ( ( dlen + 127 ) & ~127 );
1842 return IsMacResource( library, stream, offset, face_index, aface );
1850 load_face_in_embedded_rfork( FT_Library library,
1854 const FT_Open_Args *args )
1858 #define FT_COMPONENT trace_raccess
1860 FT_Memory memory = library->memory;
1861 FT_Error error = FT_Err_Unknown_File_Format;
1864 char * file_names[FT_RACCESS_N_RULES];
1865 FT_Long offsets[FT_RACCESS_N_RULES];
1866 FT_Error errors[FT_RACCESS_N_RULES];
1867 FT_Bool is_darwin_vfs, vfs_rfork_has_no_font = FALSE; /* not tested */
1870 FT_Stream stream2 = 0;
1873 FT_Raccess_Guess( library, stream,
1874 args->pathname, file_names, offsets, errors );
1876 for ( i = 0; i < FT_RACCESS_N_RULES; i++ )
1878 is_darwin_vfs = raccess_rule_by_darwin_vfs( i );
1879 if ( is_darwin_vfs && vfs_rfork_has_no_font )
1881 FT_TRACE3(( "Skip rule %d: darwin vfs resource fork"
1882 " is already checked and"
1883 " no font is found\n", i ));
1889 FT_TRACE3(( "Error[%d] has occurred in rule %d\n", errors[i], i ));
1893 args2.flags = FT_OPEN_PATHNAME;
1894 args2.pathname = file_names[i] ? file_names[i] : args->pathname;
1896 FT_TRACE3(( "Try rule %d: %s (offset=%d) ...",
1897 i, args2.pathname, offsets[i] ));
1899 error = FT_Stream_New( library, &args2, &stream2 );
1900 if ( is_darwin_vfs && error == FT_Err_Cannot_Open_Stream )
1901 vfs_rfork_has_no_font = TRUE;
1905 FT_TRACE3(( "failed\n" ));
1909 error = IsMacResource( library, stream2, offsets[i],
1910 face_index, aface );
1911 FT_Stream_Free( stream2, 0 );
1913 FT_TRACE3(( "%s\n", error ? "failed": "successful" ));
1917 else if ( is_darwin_vfs )
1918 vfs_rfork_has_no_font = TRUE;
1921 for (i = 0; i < FT_RACCESS_N_RULES; i++)
1923 if ( file_names[i] )
1924 FT_FREE( file_names[i] );
1927 /* Caller (load_mac_face) requires FT_Err_Unknown_File_Format. */
1929 error = FT_Err_Unknown_File_Format;
1934 #define FT_COMPONENT trace_objs
1939 /* Check for some macintosh formats without Carbon framework. */
1940 /* Is this a macbinary file? If so look at the resource fork. */
1941 /* Is this a mac dfont file? */
1942 /* Is this an old style resource fork? (in data) */
1943 /* Else call load_face_in_embedded_rfork to try extra rules */
1944 /* (defined in `ftrfork.c'). */
1947 load_mac_face( FT_Library library,
1951 const FT_Open_Args *args )
1957 error = IsMacBinary( library, stream, face_index, aface );
1958 if ( FT_ERROR_BASE( error ) == FT_Err_Unknown_File_Format )
1962 #define FT_COMPONENT trace_raccess
1964 FT_TRACE3(( "Try as dfont: %s ...", args->pathname ));
1966 error = IsMacResource( library, stream, 0, face_index, aface );
1968 FT_TRACE3(( "%s\n", error ? "failed" : "successful" ));
1971 #define FT_COMPONENT trace_objs
1975 if ( ( FT_ERROR_BASE( error ) == FT_Err_Unknown_File_Format ||
1976 FT_ERROR_BASE( error ) == FT_Err_Invalid_Stream_Operation ) &&
1977 ( args->flags & FT_OPEN_PATHNAME ) )
1978 error = load_face_in_embedded_rfork( library, stream,
1979 face_index, aface, args );
1984 #endif /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
1987 /* documentation is in freetype.h */
1989 FT_EXPORT_DEF( FT_Error )
1990 FT_Open_Face( FT_Library library,
1991 const FT_Open_Args* args,
1998 FT_Stream stream = NULL;
1999 FT_Face face = NULL;
2000 FT_ListNode node = NULL;
2001 FT_Bool external_stream;
2006 /* test for valid `library' delayed to */
2007 /* FT_Stream_New() */
2009 if ( ( !aface && face_index >= 0 ) || !args )
2010 return FT_Err_Invalid_Argument;
2012 external_stream = FT_BOOL( ( args->flags & FT_OPEN_STREAM ) &&
2015 /* create input stream */
2016 error = FT_Stream_New( library, args, &stream );
2020 memory = library->memory;
2022 /* If the font driver is specified in the `args' structure, use */
2023 /* it. Otherwise, we scan the list of registered drivers. */
2024 if ( ( args->flags & FT_OPEN_DRIVER ) && args->driver )
2026 driver = FT_DRIVER( args->driver );
2028 /* not all modules are drivers, so check... */
2029 if ( FT_MODULE_IS_DRIVER( driver ) )
2031 FT_Int num_params = 0;
2032 FT_Parameter* params = 0;
2035 if ( args->flags & FT_OPEN_PARAMS )
2037 num_params = args->num_params;
2038 params = args->params;
2041 error = open_face( driver, stream, face_index,
2042 num_params, params, &face );
2047 error = FT_Err_Invalid_Handle;
2049 FT_Stream_Free( stream, external_stream );
2054 /* check each font driver for an appropriate format */
2055 cur = library->modules;
2056 limit = cur + library->num_modules;
2059 for ( ; cur < limit; cur++ )
2061 /* not all modules are font drivers, so check... */
2062 if ( FT_MODULE_IS_DRIVER( cur[0] ) )
2064 FT_Int num_params = 0;
2065 FT_Parameter* params = 0;
2068 driver = FT_DRIVER( cur[0] );
2070 if ( args->flags & FT_OPEN_PARAMS )
2072 num_params = args->num_params;
2073 params = args->params;
2076 error = open_face( driver, stream, face_index,
2077 num_params, params, &face );
2081 #ifdef FT_CONFIG_OPTION_MAC_FONTS
2082 if ( ft_strcmp( cur[0]->clazz->module_name, "truetype" ) == 0 &&
2083 FT_ERROR_BASE( error ) == FT_Err_Table_Missing )
2085 /* TrueType but essential tables are missing */
2086 if ( FT_Stream_Seek( stream, 0 ) )
2089 error = open_face_PS_from_sfnt_stream( library,
2097 FT_Stream_Free( stream, external_stream );
2103 if ( FT_ERROR_BASE( error ) != FT_Err_Unknown_File_Format )
2109 /* If we are on the mac, and we get an FT_Err_Invalid_Stream_Operation */
2110 /* it may be because we have an empty data fork, so we need to check */
2111 /* the resource fork. */
2112 if ( FT_ERROR_BASE( error ) != FT_Err_Cannot_Open_Stream &&
2113 FT_ERROR_BASE( error ) != FT_Err_Unknown_File_Format &&
2114 FT_ERROR_BASE( error ) != FT_Err_Invalid_Stream_Operation )
2117 #if !defined( FT_MACINTOSH ) && defined( FT_CONFIG_OPTION_MAC_FONTS )
2118 error = load_mac_face( library, stream, face_index, aface, args );
2121 /* We don't want to go to Success here. We've already done that. */
2122 /* On the other hand, if we succeeded we still need to close this */
2123 /* stream (we opened a different stream which extracted the */
2124 /* interesting information out of this stream here. That stream */
2125 /* will still be open and the face will point to it). */
2126 FT_Stream_Free( stream, external_stream );
2130 if ( FT_ERROR_BASE( error ) != FT_Err_Unknown_File_Format )
2132 #endif /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
2134 /* no driver is able to handle this format */
2135 error = FT_Err_Unknown_File_Format;
2138 FT_Stream_Free( stream, external_stream );
2143 FT_TRACE4(( "FT_Open_Face: New face object, adding to list\n" ));
2145 /* set the FT_FACE_FLAG_EXTERNAL_STREAM bit for FT_Done_Face */
2146 if ( external_stream )
2147 face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
2149 /* add the face object to its driver's list */
2150 if ( FT_NEW( node ) )
2154 /* don't assume driver is the same as face->driver, so use */
2155 /* face->driver instead. */
2156 FT_List_Add( &face->driver->faces_list, node );
2158 /* now allocate a glyph slot object for the face */
2159 FT_TRACE4(( "FT_Open_Face: Creating glyph slot\n" ));
2161 if ( face_index >= 0 )
2163 error = FT_New_GlyphSlot( face, NULL );
2167 /* finally, allocate a size object for the face */
2172 FT_TRACE4(( "FT_Open_Face: Creating size object\n" ));
2174 error = FT_New_Size( face, &size );
2184 if ( FT_IS_SCALABLE( face ) )
2186 if ( face->height < 0 )
2187 face->height = (FT_Short)-face->height;
2189 if ( !FT_HAS_VERTICAL( face ) )
2190 face->max_advance_height = (FT_Short)face->height;
2193 if ( FT_HAS_FIXED_SIZES( face ) )
2198 for ( i = 0; i < face->num_fixed_sizes; i++ )
2200 FT_Bitmap_Size* bsize = face->available_sizes + i;
2203 if ( bsize->height < 0 )
2204 bsize->height = (FT_Short)-bsize->height;
2205 if ( bsize->x_ppem < 0 )
2206 bsize->x_ppem = (FT_Short)-bsize->x_ppem;
2207 if ( bsize->y_ppem < 0 )
2208 bsize->y_ppem = -bsize->y_ppem;
2212 /* initialize internal face data */
2214 FT_Face_Internal internal = face->internal;
2217 internal->transform_matrix.xx = 0x10000L;
2218 internal->transform_matrix.xy = 0;
2219 internal->transform_matrix.yx = 0;
2220 internal->transform_matrix.yy = 0x10000L;
2222 internal->transform_delta.x = 0;
2223 internal->transform_delta.y = 0;
2225 internal->refcount = 1;
2231 FT_Done_Face( face );
2236 FT_Done_Face( face );
2239 FT_TRACE4(( "FT_Open_Face: Return %d\n", error ));
2245 /* documentation is in freetype.h */
2247 FT_EXPORT_DEF( FT_Error )
2248 FT_Attach_File( FT_Face face,
2249 const char* filepathname )
2254 /* test for valid `face' delayed to FT_Attach_Stream() */
2256 if ( !filepathname )
2257 return FT_Err_Invalid_Argument;
2260 open.flags = FT_OPEN_PATHNAME;
2261 open.pathname = (char*)filepathname;
2263 return FT_Attach_Stream( face, &open );
2267 /* documentation is in freetype.h */
2269 FT_EXPORT_DEF( FT_Error )
2270 FT_Attach_Stream( FT_Face face,
2271 FT_Open_Args* parameters )
2277 FT_Driver_Class clazz;
2280 /* test for valid `parameters' delayed to FT_Stream_New() */
2283 return FT_Err_Invalid_Face_Handle;
2285 driver = face->driver;
2287 return FT_Err_Invalid_Driver_Handle;
2289 error = FT_Stream_New( driver->root.library, parameters, &stream );
2293 /* we implement FT_Attach_Stream in each driver through the */
2294 /* `attach_file' interface */
2296 error = FT_Err_Unimplemented_Feature;
2297 clazz = driver->clazz;
2298 if ( clazz->attach_file )
2299 error = clazz->attach_file( face, stream );
2301 /* close the attached stream */
2302 FT_Stream_Free( stream,
2303 (FT_Bool)( parameters->stream &&
2304 ( parameters->flags & FT_OPEN_STREAM ) ) );
2311 /* documentation is in freetype.h */
2313 FT_EXPORT_DEF( FT_Error )
2314 FT_Reference_Face( FT_Face face )
2316 face->internal->refcount++;
2322 /* documentation is in freetype.h */
2324 FT_EXPORT_DEF( FT_Error )
2325 FT_Done_Face( FT_Face face )
2333 error = FT_Err_Invalid_Face_Handle;
2334 if ( face && face->driver )
2336 face->internal->refcount--;
2337 if ( face->internal->refcount > 0 )
2341 driver = face->driver;
2342 memory = driver->root.memory;
2344 /* find face in driver's list */
2345 node = FT_List_Find( &driver->faces_list, face );
2348 /* remove face object from the driver's list */
2349 FT_List_Remove( &driver->faces_list, node );
2352 /* now destroy the object proper */
2353 destroy_face( memory, face, driver );
2363 /* documentation is in ftobjs.h */
2365 FT_EXPORT_DEF( FT_Error )
2366 FT_New_Size( FT_Face face,
2372 FT_Driver_Class clazz;
2375 FT_ListNode node = 0;
2379 return FT_Err_Invalid_Face_Handle;
2382 return FT_Err_Invalid_Size_Handle;
2384 if ( !face->driver )
2385 return FT_Err_Invalid_Driver_Handle;
2389 driver = face->driver;
2390 clazz = driver->clazz;
2391 memory = face->memory;
2393 /* Allocate new size object and perform basic initialisation */
2394 if ( FT_ALLOC( size, clazz->size_object_size ) || FT_NEW( node ) )
2399 /* for now, do not use any internal fields in size objects */
2402 if ( clazz->init_size )
2403 error = clazz->init_size( size );
2405 /* in case of success, add to the face's list */
2410 FT_List_Add( &face->sizes_list, node );
2424 /* documentation is in ftobjs.h */
2426 FT_EXPORT_DEF( FT_Error )
2427 FT_Done_Size( FT_Size size )
2437 return FT_Err_Invalid_Size_Handle;
2441 return FT_Err_Invalid_Face_Handle;
2443 driver = face->driver;
2445 return FT_Err_Invalid_Driver_Handle;
2447 memory = driver->root.memory;
2450 node = FT_List_Find( &face->sizes_list, size );
2453 FT_List_Remove( &face->sizes_list, node );
2456 if ( face->size == size )
2459 if ( face->sizes_list.head )
2460 face->size = (FT_Size)(face->sizes_list.head->data);
2463 destroy_size( memory, size, driver );
2466 error = FT_Err_Invalid_Size_Handle;
2472 /* documentation is in ftobjs.h */
2474 FT_BASE_DEF( FT_Error )
2475 FT_Match_Size( FT_Face face,
2476 FT_Size_Request req,
2477 FT_Bool ignore_width,
2478 FT_ULong* size_index )
2484 if ( !FT_HAS_FIXED_SIZES( face ) )
2485 return FT_Err_Invalid_Face_Handle;
2487 /* FT_Bitmap_Size doesn't provide enough info... */
2488 if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
2489 return FT_Err_Unimplemented_Feature;
2491 w = FT_REQUEST_WIDTH ( req );
2492 h = FT_REQUEST_HEIGHT( req );
2494 if ( req->width && !req->height )
2496 else if ( !req->width && req->height )
2499 w = FT_PIX_ROUND( w );
2500 h = FT_PIX_ROUND( h );
2502 for ( i = 0; i < face->num_fixed_sizes; i++ )
2504 FT_Bitmap_Size* bsize = face->available_sizes + i;
2507 if ( h != FT_PIX_ROUND( bsize->y_ppem ) )
2510 if ( w == FT_PIX_ROUND( bsize->x_ppem ) || ignore_width )
2513 *size_index = (FT_ULong)i;
2519 return FT_Err_Invalid_Pixel_Size;
2523 /* documentation is in ftobjs.h */
2526 ft_synthesize_vertical_metrics( FT_Glyph_Metrics* metrics,
2529 FT_Pos height = metrics->height;
2532 /* compensate for glyph with bbox above/below the baseline */
2533 if ( metrics->horiBearingY < 0 )
2535 if ( height < metrics->horiBearingY )
2536 height = metrics->horiBearingY;
2538 else if ( metrics->horiBearingY > 0 )
2539 height -= metrics->horiBearingY;
2541 /* the factor 1.2 is a heuristical value */
2543 advance = height * 12 / 10;
2545 metrics->vertBearingX = metrics->horiBearingX - metrics->horiAdvance / 2;
2546 metrics->vertBearingY = ( advance - height ) / 2;
2547 metrics->vertAdvance = advance;
2552 ft_recompute_scaled_metrics( FT_Face face,
2553 FT_Size_Metrics* metrics )
2555 /* Compute root ascender, descender, test height, and max_advance */
2557 #ifdef GRID_FIT_METRICS
2558 metrics->ascender = FT_PIX_CEIL( FT_MulFix( face->ascender,
2559 metrics->y_scale ) );
2561 metrics->descender = FT_PIX_FLOOR( FT_MulFix( face->descender,
2562 metrics->y_scale ) );
2564 metrics->height = FT_PIX_ROUND( FT_MulFix( face->height,
2565 metrics->y_scale ) );
2567 metrics->max_advance = FT_PIX_ROUND( FT_MulFix( face->max_advance_width,
2568 metrics->x_scale ) );
2569 #else /* !GRID_FIT_METRICS */
2570 metrics->ascender = FT_MulFix( face->ascender,
2573 metrics->descender = FT_MulFix( face->descender,
2576 metrics->height = FT_MulFix( face->height,
2579 metrics->max_advance = FT_MulFix( face->max_advance_width,
2581 #endif /* !GRID_FIT_METRICS */
2586 FT_Select_Metrics( FT_Face face,
2587 FT_ULong strike_index )
2589 FT_Size_Metrics* metrics;
2590 FT_Bitmap_Size* bsize;
2593 metrics = &face->size->metrics;
2594 bsize = face->available_sizes + strike_index;
2596 metrics->x_ppem = (FT_UShort)( ( bsize->x_ppem + 32 ) >> 6 );
2597 metrics->y_ppem = (FT_UShort)( ( bsize->y_ppem + 32 ) >> 6 );
2599 if ( FT_IS_SCALABLE( face ) )
2601 metrics->x_scale = FT_DivFix( bsize->x_ppem,
2602 face->units_per_EM );
2603 metrics->y_scale = FT_DivFix( bsize->y_ppem,
2604 face->units_per_EM );
2606 ft_recompute_scaled_metrics( face, metrics );
2610 metrics->x_scale = 1L << 16;
2611 metrics->y_scale = 1L << 16;
2612 metrics->ascender = bsize->y_ppem;
2613 metrics->descender = 0;
2614 metrics->height = bsize->height << 6;
2615 metrics->max_advance = bsize->x_ppem;
2618 FT_TRACE5(( "FT_Select_Metrics:\n" ));
2619 FT_TRACE5(( " x scale: %d (%f)\n",
2620 metrics->x_scale, metrics->x_scale / 65536.0 ));
2621 FT_TRACE5(( " y scale: %d (%f)\n",
2622 metrics->y_scale, metrics->y_scale / 65536.0 ));
2623 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 ));
2624 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 ));
2625 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 ));
2626 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 ));
2627 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
2628 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
2633 FT_Request_Metrics( FT_Face face,
2634 FT_Size_Request req )
2636 FT_Size_Metrics* metrics;
2639 metrics = &face->size->metrics;
2641 if ( FT_IS_SCALABLE( face ) )
2643 FT_Long w = 0, h = 0, scaled_w = 0, scaled_h = 0;
2646 switch ( req->type )
2648 case FT_SIZE_REQUEST_TYPE_NOMINAL:
2649 w = h = face->units_per_EM;
2652 case FT_SIZE_REQUEST_TYPE_REAL_DIM:
2653 w = h = face->ascender - face->descender;
2656 case FT_SIZE_REQUEST_TYPE_BBOX:
2657 w = face->bbox.xMax - face->bbox.xMin;
2658 h = face->bbox.yMax - face->bbox.yMin;
2661 case FT_SIZE_REQUEST_TYPE_CELL:
2662 w = face->max_advance_width;
2663 h = face->ascender - face->descender;
2666 case FT_SIZE_REQUEST_TYPE_SCALES:
2667 metrics->x_scale = (FT_Fixed)req->width;
2668 metrics->y_scale = (FT_Fixed)req->height;
2669 if ( !metrics->x_scale )
2670 metrics->x_scale = metrics->y_scale;
2671 else if ( !metrics->y_scale )
2672 metrics->y_scale = metrics->x_scale;
2673 goto Calculate_Ppem;
2675 case FT_SIZE_REQUEST_TYPE_MAX:
2679 /* to be on the safe side */
2686 scaled_w = FT_REQUEST_WIDTH ( req );
2687 scaled_h = FT_REQUEST_HEIGHT( req );
2689 /* determine scales */
2692 metrics->x_scale = FT_DivFix( scaled_w, w );
2696 metrics->y_scale = FT_DivFix( scaled_h, h );
2698 if ( req->type == FT_SIZE_REQUEST_TYPE_CELL )
2700 if ( metrics->y_scale > metrics->x_scale )
2701 metrics->y_scale = metrics->x_scale;
2703 metrics->x_scale = metrics->y_scale;
2708 metrics->y_scale = metrics->x_scale;
2709 scaled_h = FT_MulDiv( scaled_w, h, w );
2714 metrics->x_scale = metrics->y_scale = FT_DivFix( scaled_h, h );
2715 scaled_w = FT_MulDiv( scaled_h, w, h );
2719 /* calculate the ppems */
2720 if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
2722 scaled_w = FT_MulFix( face->units_per_EM, metrics->x_scale );
2723 scaled_h = FT_MulFix( face->units_per_EM, metrics->y_scale );
2726 metrics->x_ppem = (FT_UShort)( ( scaled_w + 32 ) >> 6 );
2727 metrics->y_ppem = (FT_UShort)( ( scaled_h + 32 ) >> 6 );
2729 ft_recompute_scaled_metrics( face, metrics );
2734 metrics->x_scale = 1L << 16;
2735 metrics->y_scale = 1L << 16;
2738 FT_TRACE5(( "FT_Request_Metrics:\n" ));
2739 FT_TRACE5(( " x scale: %d (%f)\n",
2740 metrics->x_scale, metrics->x_scale / 65536.0 ));
2741 FT_TRACE5(( " y scale: %d (%f)\n",
2742 metrics->y_scale, metrics->y_scale / 65536.0 ));
2743 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 ));
2744 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 ));
2745 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 ));
2746 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 ));
2747 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
2748 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
2752 /* documentation is in freetype.h */
2754 FT_EXPORT_DEF( FT_Error )
2755 FT_Select_Size( FT_Face face,
2756 FT_Int strike_index )
2758 FT_Driver_Class clazz;
2761 if ( !face || !FT_HAS_FIXED_SIZES( face ) )
2762 return FT_Err_Invalid_Face_Handle;
2764 if ( strike_index < 0 || strike_index >= face->num_fixed_sizes )
2765 return FT_Err_Invalid_Argument;
2767 clazz = face->driver->clazz;
2769 if ( clazz->select_size )
2774 error = clazz->select_size( face->size, (FT_ULong)strike_index );
2776 #ifdef FT_DEBUG_LEVEL_TRACE
2778 FT_Size_Metrics* metrics = &face->size->metrics;
2781 FT_TRACE5(( "FT_Select_Size (font driver's `select_size'):\n" ));
2782 FT_TRACE5(( " x scale: %d (%f)\n",
2783 metrics->x_scale, metrics->x_scale / 65536.0 ));
2784 FT_TRACE5(( " y scale: %d (%f)\n",
2785 metrics->y_scale, metrics->y_scale / 65536.0 ));
2786 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 ));
2787 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 ));
2788 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 ));
2789 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 ));
2790 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
2791 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
2798 FT_Select_Metrics( face, (FT_ULong)strike_index );
2804 /* documentation is in freetype.h */
2806 FT_EXPORT_DEF( FT_Error )
2807 FT_Request_Size( FT_Face face,
2808 FT_Size_Request req )
2810 FT_Driver_Class clazz;
2811 FT_ULong strike_index;
2815 return FT_Err_Invalid_Face_Handle;
2817 if ( !req || req->width < 0 || req->height < 0 ||
2818 req->type >= FT_SIZE_REQUEST_TYPE_MAX )
2819 return FT_Err_Invalid_Argument;
2821 clazz = face->driver->clazz;
2823 if ( clazz->request_size )
2828 error = clazz->request_size( face->size, req );
2830 #ifdef FT_DEBUG_LEVEL_TRACE
2832 FT_Size_Metrics* metrics = &face->size->metrics;
2835 FT_TRACE5(( "FT_Request_Size (font driver's `request_size'):\n" ));
2836 FT_TRACE5(( " x scale: %d (%f)\n",
2837 metrics->x_scale, metrics->x_scale / 65536.0 ));
2838 FT_TRACE5(( " y scale: %d (%f)\n",
2839 metrics->y_scale, metrics->y_scale / 65536.0 ));
2840 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 ));
2841 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 ));
2842 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 ));
2843 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 ));
2844 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
2845 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
2853 * The reason that a driver doesn't have `request_size' defined is
2854 * either that the scaling here suffices or that the supported formats
2855 * are bitmap-only and size matching is not implemented.
2857 * In the latter case, a simple size matching is done.
2859 if ( !FT_IS_SCALABLE( face ) && FT_HAS_FIXED_SIZES( face ) )
2864 error = FT_Match_Size( face, req, 0, &strike_index );
2868 FT_TRACE3(( "FT_Request_Size: bitmap strike %lu matched\n",
2871 return FT_Select_Size( face, (FT_Int)strike_index );
2874 FT_Request_Metrics( face, req );
2880 /* documentation is in freetype.h */
2882 FT_EXPORT_DEF( FT_Error )
2883 FT_Set_Char_Size( FT_Face face,
2884 FT_F26Dot6 char_width,
2885 FT_F26Dot6 char_height,
2886 FT_UInt horz_resolution,
2887 FT_UInt vert_resolution )
2889 FT_Size_RequestRec req;
2893 char_width = char_height;
2894 else if ( !char_height )
2895 char_height = char_width;
2897 if ( !horz_resolution )
2898 horz_resolution = vert_resolution;
2899 else if ( !vert_resolution )
2900 vert_resolution = horz_resolution;
2902 if ( char_width < 1 * 64 )
2903 char_width = 1 * 64;
2904 if ( char_height < 1 * 64 )
2905 char_height = 1 * 64;
2907 if ( !horz_resolution )
2908 horz_resolution = vert_resolution = 72;
2910 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
2911 req.width = char_width;
2912 req.height = char_height;
2913 req.horiResolution = horz_resolution;
2914 req.vertResolution = vert_resolution;
2916 return FT_Request_Size( face, &req );
2920 /* documentation is in freetype.h */
2922 FT_EXPORT_DEF( FT_Error )
2923 FT_Set_Pixel_Sizes( FT_Face face,
2924 FT_UInt pixel_width,
2925 FT_UInt pixel_height )
2927 FT_Size_RequestRec req;
2930 if ( pixel_width == 0 )
2931 pixel_width = pixel_height;
2932 else if ( pixel_height == 0 )
2933 pixel_height = pixel_width;
2935 if ( pixel_width < 1 )
2937 if ( pixel_height < 1 )
2940 /* use `>=' to avoid potential compiler warning on 16bit platforms */
2941 if ( pixel_width >= 0xFFFFU )
2942 pixel_width = 0xFFFFU;
2943 if ( pixel_height >= 0xFFFFU )
2944 pixel_height = 0xFFFFU;
2946 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
2947 req.width = pixel_width << 6;
2948 req.height = pixel_height << 6;
2949 req.horiResolution = 0;
2950 req.vertResolution = 0;
2952 return FT_Request_Size( face, &req );
2956 /* documentation is in freetype.h */
2958 FT_EXPORT_DEF( FT_Error )
2959 FT_Get_Kerning( FT_Face face,
2961 FT_UInt right_glyph,
2963 FT_Vector *akerning )
2965 FT_Error error = FT_Err_Ok;
2970 return FT_Err_Invalid_Face_Handle;
2973 return FT_Err_Invalid_Argument;
2975 driver = face->driver;
2980 if ( driver->clazz->get_kerning )
2982 error = driver->clazz->get_kerning( face,
2988 if ( kern_mode != FT_KERNING_UNSCALED )
2990 akerning->x = FT_MulFix( akerning->x, face->size->metrics.x_scale );
2991 akerning->y = FT_MulFix( akerning->y, face->size->metrics.y_scale );
2993 if ( kern_mode != FT_KERNING_UNFITTED )
2995 /* we scale down kerning values for small ppem values */
2996 /* to avoid that rounding makes them too big. */
2997 /* `25' has been determined heuristically. */
2998 if ( face->size->metrics.x_ppem < 25 )
2999 akerning->x = FT_MulDiv( akerning->x,
3000 face->size->metrics.x_ppem, 25 );
3001 if ( face->size->metrics.y_ppem < 25 )
3002 akerning->y = FT_MulDiv( akerning->y,
3003 face->size->metrics.y_ppem, 25 );
3005 akerning->x = FT_PIX_ROUND( akerning->x );
3006 akerning->y = FT_PIX_ROUND( akerning->y );
3016 /* documentation is in freetype.h */
3018 FT_EXPORT_DEF( FT_Error )
3019 FT_Get_Track_Kerning( FT_Face face,
3020 FT_Fixed point_size,
3022 FT_Fixed* akerning )
3024 FT_Service_Kerning service;
3025 FT_Error error = FT_Err_Ok;
3029 return FT_Err_Invalid_Face_Handle;
3032 return FT_Err_Invalid_Argument;
3034 FT_FACE_FIND_SERVICE( face, service, KERNING );
3036 return FT_Err_Unimplemented_Feature;
3038 error = service->get_track( face,
3047 /* documentation is in freetype.h */
3049 FT_EXPORT_DEF( FT_Error )
3050 FT_Select_Charmap( FT_Face face,
3051 FT_Encoding encoding )
3058 return FT_Err_Invalid_Face_Handle;
3060 if ( encoding == FT_ENCODING_NONE )
3061 return FT_Err_Invalid_Argument;
3063 /* FT_ENCODING_UNICODE is special. We try to find the `best' Unicode */
3064 /* charmap available, i.e., one with UCS-4 characters, if possible. */
3066 /* This is done by find_unicode_charmap() above, to share code. */
3067 if ( encoding == FT_ENCODING_UNICODE )
3068 return find_unicode_charmap( face );
3070 cur = face->charmaps;
3072 return FT_Err_Invalid_CharMap_Handle;
3074 limit = cur + face->num_charmaps;
3076 for ( ; cur < limit; cur++ )
3078 if ( cur[0]->encoding == encoding )
3080 #ifdef FT_MAX_CHARMAP_CACHEABLE
3081 if ( cur - face->charmaps > FT_MAX_CHARMAP_CACHEABLE )
3083 FT_ERROR(( "FT_Select_Charmap: requested charmap is found (%d), "
3084 "but in too late position to cache\n",
3085 cur - face->charmaps ));
3089 face->charmap = cur[0];
3094 return FT_Err_Invalid_Argument;
3098 /* documentation is in freetype.h */
3100 FT_EXPORT_DEF( FT_Error )
3101 FT_Set_Charmap( FT_Face face,
3102 FT_CharMap charmap )
3109 return FT_Err_Invalid_Face_Handle;
3111 cur = face->charmaps;
3113 return FT_Err_Invalid_CharMap_Handle;
3114 if ( FT_Get_CMap_Format( charmap ) == 14 )
3115 return FT_Err_Invalid_Argument;
3117 limit = cur + face->num_charmaps;
3119 for ( ; cur < limit; cur++ )
3121 if ( cur[0] == charmap )
3123 #ifdef FT_MAX_CHARMAP_CACHEABLE
3124 if ( cur - face->charmaps > FT_MAX_CHARMAP_CACHEABLE )
3126 FT_ERROR(( "FT_Set_Charmap: requested charmap is found (%d), "
3127 "but in too late position to cache\n",
3128 cur - face->charmaps ));
3132 face->charmap = cur[0];
3136 return FT_Err_Invalid_Argument;
3140 /* documentation is in freetype.h */
3142 FT_EXPORT_DEF( FT_Int )
3143 FT_Get_Charmap_Index( FT_CharMap charmap )
3148 if ( !charmap || !charmap->face )
3151 for ( i = 0; i < charmap->face->num_charmaps; i++ )
3152 if ( charmap->face->charmaps[i] == charmap )
3155 FT_ASSERT( i < charmap->face->num_charmaps );
3157 #ifdef FT_MAX_CHARMAP_CACHEABLE
3158 if ( i > FT_MAX_CHARMAP_CACHEABLE )
3160 FT_ERROR(( "FT_Get_Charmap_Index: requested charmap is found (%d), "
3161 "but in too late position to cache\n",
3171 ft_cmap_done_internal( FT_CMap cmap )
3173 FT_CMap_Class clazz = cmap->clazz;
3174 FT_Face face = cmap->charmap.face;
3175 FT_Memory memory = FT_FACE_MEMORY(face);
3179 clazz->done( cmap );
3186 FT_CMap_Done( FT_CMap cmap )
3190 FT_Face face = cmap->charmap.face;
3191 FT_Memory memory = FT_FACE_MEMORY( face );
3196 for ( i = 0; i < face->num_charmaps; i++ )
3198 if ( (FT_CMap)face->charmaps[i] == cmap )
3200 FT_CharMap last_charmap = face->charmaps[face->num_charmaps - 1];
3203 if ( FT_RENEW_ARRAY( face->charmaps,
3205 face->num_charmaps - 1 ) )
3208 /* remove it from our list of charmaps */
3209 for ( j = i + 1; j < face->num_charmaps; j++ )
3211 if ( j == face->num_charmaps - 1 )
3212 face->charmaps[j - 1] = last_charmap;
3214 face->charmaps[j - 1] = face->charmaps[j];
3217 face->num_charmaps--;
3219 if ( (FT_CMap)face->charmap == cmap )
3220 face->charmap = NULL;
3222 ft_cmap_done_internal( cmap );
3231 FT_BASE_DEF( FT_Error )
3232 FT_CMap_New( FT_CMap_Class clazz,
3233 FT_Pointer init_data,
3237 FT_Error error = FT_Err_Ok;
3240 FT_CMap cmap = NULL;
3243 if ( clazz == NULL || charmap == NULL || charmap->face == NULL )
3244 return FT_Err_Invalid_Argument;
3246 face = charmap->face;
3247 memory = FT_FACE_MEMORY( face );
3249 if ( !FT_ALLOC( cmap, clazz->size ) )
3251 cmap->charmap = *charmap;
3252 cmap->clazz = clazz;
3256 error = clazz->init( cmap, init_data );
3261 /* add it to our list of charmaps */
3262 if ( FT_RENEW_ARRAY( face->charmaps,
3264 face->num_charmaps + 1 ) )
3267 face->charmaps[face->num_charmaps++] = (FT_CharMap)cmap;
3277 ft_cmap_done_internal( cmap );
3283 /* documentation is in freetype.h */
3285 FT_EXPORT_DEF( FT_UInt )
3286 FT_Get_Char_Index( FT_Face face,
3292 if ( face && face->charmap )
3294 FT_CMap cmap = FT_CMAP( face->charmap );
3297 if ( charcode > 0xFFFFFFFFUL )
3299 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3300 FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3302 result = cmap->clazz->char_index( cmap, (FT_UInt32)charcode );
3308 /* documentation is in freetype.h */
3310 FT_EXPORT_DEF( FT_ULong )
3311 FT_Get_First_Char( FT_Face face,
3314 FT_ULong result = 0;
3318 if ( face && face->charmap && face->num_glyphs )
3320 gindex = FT_Get_Char_Index( face, 0 );
3321 if ( gindex == 0 || gindex >= (FT_UInt)face->num_glyphs )
3322 result = FT_Get_Next_Char( face, 0, &gindex );
3332 /* documentation is in freetype.h */
3334 FT_EXPORT_DEF( FT_ULong )
3335 FT_Get_Next_Char( FT_Face face,
3339 FT_ULong result = 0;
3343 if ( face && face->charmap && face->num_glyphs )
3345 FT_UInt32 code = (FT_UInt32)charcode;
3346 FT_CMap cmap = FT_CMAP( face->charmap );
3350 gindex = cmap->clazz->char_next( cmap, &code );
3351 } while ( gindex >= (FT_UInt)face->num_glyphs );
3353 result = ( gindex == 0 ) ? 0 : code;
3363 /* documentation is in freetype.h */
3365 FT_EXPORT_DEF( FT_UInt )
3366 FT_Face_GetCharVariantIndex( FT_Face face,
3368 FT_ULong variantSelector )
3373 if ( face && face->charmap &&
3374 face->charmap->encoding == FT_ENCODING_UNICODE )
3376 FT_CharMap charmap = find_variant_selector_charmap( face );
3377 FT_CMap ucmap = FT_CMAP( face->charmap );
3380 if ( charmap != NULL )
3382 FT_CMap vcmap = FT_CMAP( charmap );
3385 if ( charcode > 0xFFFFFFFFUL )
3387 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3388 FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3390 if ( variantSelector > 0xFFFFFFFFUL )
3392 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3393 FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3396 result = vcmap->clazz->char_var_index( vcmap, ucmap,
3397 (FT_UInt32)charcode,
3398 (FT_UInt32)variantSelector );
3406 /* documentation is in freetype.h */
3408 FT_EXPORT_DEF( FT_Int )
3409 FT_Face_GetCharVariantIsDefault( FT_Face face,
3411 FT_ULong variantSelector )
3418 FT_CharMap charmap = find_variant_selector_charmap( face );
3421 if ( charmap != NULL )
3423 FT_CMap vcmap = FT_CMAP( charmap );
3426 if ( charcode > 0xFFFFFFFFUL )
3428 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3429 FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3431 if ( variantSelector > 0xFFFFFFFFUL )
3433 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3434 FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3437 result = vcmap->clazz->char_var_default( vcmap,
3438 (FT_UInt32)charcode,
3439 (FT_UInt32)variantSelector );
3447 /* documentation is in freetype.h */
3449 FT_EXPORT_DEF( FT_UInt32* )
3450 FT_Face_GetVariantSelectors( FT_Face face )
3452 FT_UInt32 *result = NULL;
3457 FT_CharMap charmap = find_variant_selector_charmap( face );
3460 if ( charmap != NULL )
3462 FT_CMap vcmap = FT_CMAP( charmap );
3463 FT_Memory memory = FT_FACE_MEMORY( face );
3466 result = vcmap->clazz->variant_list( vcmap, memory );
3474 /* documentation is in freetype.h */
3476 FT_EXPORT_DEF( FT_UInt32* )
3477 FT_Face_GetVariantsOfChar( FT_Face face,
3480 FT_UInt32 *result = NULL;
3485 FT_CharMap charmap = find_variant_selector_charmap( face );
3488 if ( charmap != NULL )
3490 FT_CMap vcmap = FT_CMAP( charmap );
3491 FT_Memory memory = FT_FACE_MEMORY( face );
3494 if ( charcode > 0xFFFFFFFFUL )
3496 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3497 FT_TRACE1(( " 0x%x is truncated\n", charcode ));
3500 result = vcmap->clazz->charvariant_list( vcmap, memory,
3501 (FT_UInt32)charcode );
3508 /* documentation is in freetype.h */
3510 FT_EXPORT_DEF( FT_UInt32* )
3511 FT_Face_GetCharsOfVariant( FT_Face face,
3512 FT_ULong variantSelector )
3514 FT_UInt32 *result = NULL;
3519 FT_CharMap charmap = find_variant_selector_charmap( face );
3522 if ( charmap != NULL )
3524 FT_CMap vcmap = FT_CMAP( charmap );
3525 FT_Memory memory = FT_FACE_MEMORY( face );
3528 if ( variantSelector > 0xFFFFFFFFUL )
3530 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
3531 FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
3534 result = vcmap->clazz->variantchar_list( vcmap, memory,
3535 (FT_UInt32)variantSelector );
3543 /* documentation is in freetype.h */
3545 FT_EXPORT_DEF( FT_UInt )
3546 FT_Get_Name_Index( FT_Face face,
3547 FT_String* glyph_name )
3552 if ( face && FT_HAS_GLYPH_NAMES( face ) )
3554 FT_Service_GlyphDict service;
3557 FT_FACE_LOOKUP_SERVICE( face,
3561 if ( service && service->name_index )
3562 result = service->name_index( face, glyph_name );
3569 /* documentation is in freetype.h */
3571 FT_EXPORT_DEF( FT_Error )
3572 FT_Get_Glyph_Name( FT_Face face,
3573 FT_UInt glyph_index,
3575 FT_UInt buffer_max )
3577 FT_Error error = FT_Err_Invalid_Argument;
3580 /* clean up buffer */
3581 if ( buffer && buffer_max > 0 )
3582 ((FT_Byte*)buffer)[0] = 0;
3585 (FT_Long)glyph_index <= face->num_glyphs &&
3586 FT_HAS_GLYPH_NAMES( face ) )
3588 FT_Service_GlyphDict service;
3591 FT_FACE_LOOKUP_SERVICE( face,
3595 if ( service && service->get_name )
3596 error = service->get_name( face, glyph_index, buffer, buffer_max );
3603 /* documentation is in freetype.h */
3605 FT_EXPORT_DEF( const char* )
3606 FT_Get_Postscript_Name( FT_Face face )
3608 const char* result = NULL;
3616 FT_Service_PsFontName service;
3619 FT_FACE_LOOKUP_SERVICE( face,
3621 POSTSCRIPT_FONT_NAME );
3623 if ( service && service->get_ps_font_name )
3624 result = service->get_ps_font_name( face );
3632 /* documentation is in tttables.h */
3634 FT_EXPORT_DEF( void* )
3635 FT_Get_Sfnt_Table( FT_Face face,
3639 FT_Service_SFNT_Table service;
3642 if ( face && FT_IS_SFNT( face ) )
3644 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3645 if ( service != NULL )
3646 table = service->get_table( face, tag );
3653 /* documentation is in tttables.h */
3655 FT_EXPORT_DEF( FT_Error )
3656 FT_Load_Sfnt_Table( FT_Face face,
3662 FT_Service_SFNT_Table service;
3665 if ( !face || !FT_IS_SFNT( face ) )
3666 return FT_Err_Invalid_Face_Handle;
3668 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3669 if ( service == NULL )
3670 return FT_Err_Unimplemented_Feature;
3672 return service->load_table( face, tag, offset, buffer, length );
3676 /* documentation is in tttables.h */
3678 FT_EXPORT_DEF( FT_Error )
3679 FT_Sfnt_Table_Info( FT_Face face,
3680 FT_UInt table_index,
3684 FT_Service_SFNT_Table service;
3688 if ( !face || !FT_IS_SFNT( face ) )
3689 return FT_Err_Invalid_Face_Handle;
3691 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
3692 if ( service == NULL )
3693 return FT_Err_Unimplemented_Feature;
3695 return service->table_info( face, table_index, tag, &offset, length );
3699 /* documentation is in tttables.h */
3701 FT_EXPORT_DEF( FT_ULong )
3702 FT_Get_CMap_Language_ID( FT_CharMap charmap )
3704 FT_Service_TTCMaps service;
3706 TT_CMapInfo cmap_info;
3709 if ( !charmap || !charmap->face )
3712 face = charmap->face;
3713 FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
3714 if ( service == NULL )
3716 if ( service->get_cmap_info( charmap, &cmap_info ))
3719 return cmap_info.language;
3723 /* documentation is in tttables.h */
3725 FT_EXPORT_DEF( FT_Long )
3726 FT_Get_CMap_Format( FT_CharMap charmap )
3728 FT_Service_TTCMaps service;
3730 TT_CMapInfo cmap_info;
3733 if ( !charmap || !charmap->face )
3736 face = charmap->face;
3737 FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
3738 if ( service == NULL )
3740 if ( service->get_cmap_info( charmap, &cmap_info ))
3743 return cmap_info.format;
3747 /* documentation is in ftsizes.h */
3749 FT_EXPORT_DEF( FT_Error )
3750 FT_Activate_Size( FT_Size size )
3756 return FT_Err_Invalid_Argument;
3759 if ( face == NULL || face->driver == NULL )
3760 return FT_Err_Invalid_Argument;
3762 /* we don't need anything more complex than that; all size objects */
3763 /* are already listed by the face */
3770 /*************************************************************************/
3771 /*************************************************************************/
3772 /*************************************************************************/
3775 /**** R E N D E R E R S ****/
3778 /*************************************************************************/
3779 /*************************************************************************/
3780 /*************************************************************************/
3782 /* lookup a renderer by glyph format in the library's list */
3783 FT_BASE_DEF( FT_Renderer )
3784 FT_Lookup_Renderer( FT_Library library,
3785 FT_Glyph_Format format,
3789 FT_Renderer result = 0;
3795 cur = library->renderers.head;
3800 cur = (*node)->next;
3806 FT_Renderer renderer = FT_RENDERER( cur->data );
3809 if ( renderer->glyph_format == format )
3826 ft_lookup_glyph_renderer( FT_GlyphSlot slot )
3828 FT_Face face = slot->face;
3829 FT_Library library = FT_FACE_LIBRARY( face );
3830 FT_Renderer result = library->cur_renderer;
3833 if ( !result || result->glyph_format != slot->format )
3834 result = FT_Lookup_Renderer( library, slot->format, 0 );
3841 ft_set_current_renderer( FT_Library library )
3843 FT_Renderer renderer;
3846 renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE, 0 );
3847 library->cur_renderer = renderer;
3852 ft_add_renderer( FT_Module module )
3854 FT_Library library = module->library;
3855 FT_Memory memory = library->memory;
3857 FT_ListNode node = NULL;
3860 if ( FT_NEW( node ) )
3864 FT_Renderer render = FT_RENDERER( module );
3865 FT_Renderer_Class* clazz = (FT_Renderer_Class*)module->clazz;
3868 render->clazz = clazz;
3869 render->glyph_format = clazz->glyph_format;
3871 /* allocate raster object if needed */
3872 if ( clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
3873 clazz->raster_class->raster_new )
3875 error = clazz->raster_class->raster_new( memory, &render->raster );
3879 render->raster_render = clazz->raster_class->raster_render;
3880 render->render = clazz->render_glyph;
3884 node->data = module;
3885 FT_List_Add( &library->renderers, node );
3887 ft_set_current_renderer( library );
3900 ft_remove_renderer( FT_Module module )
3902 FT_Library library = module->library;
3903 FT_Memory memory = library->memory;
3907 node = FT_List_Find( &library->renderers, module );
3910 FT_Renderer render = FT_RENDERER( module );
3913 /* release raster object, if any */
3914 if ( render->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
3916 render->clazz->raster_class->raster_done( render->raster );
3918 /* remove from list */
3919 FT_List_Remove( &library->renderers, node );
3922 ft_set_current_renderer( library );
3927 /* documentation is in ftrender.h */
3929 FT_EXPORT_DEF( FT_Renderer )
3930 FT_Get_Renderer( FT_Library library,
3931 FT_Glyph_Format format )
3933 /* test for valid `library' delayed to FT_Lookup_Renderer() */
3935 return FT_Lookup_Renderer( library, format, 0 );
3939 /* documentation is in ftrender.h */
3941 FT_EXPORT_DEF( FT_Error )
3942 FT_Set_Renderer( FT_Library library,
3943 FT_Renderer renderer,
3945 FT_Parameter* parameters )
3948 FT_Error error = FT_Err_Ok;
3952 return FT_Err_Invalid_Library_Handle;
3955 return FT_Err_Invalid_Argument;
3957 node = FT_List_Find( &library->renderers, renderer );
3960 error = FT_Err_Invalid_Argument;
3964 FT_List_Up( &library->renderers, node );
3966 if ( renderer->glyph_format == FT_GLYPH_FORMAT_OUTLINE )
3967 library->cur_renderer = renderer;
3969 if ( num_params > 0 )
3971 FT_Renderer_SetModeFunc set_mode = renderer->clazz->set_mode;
3974 for ( ; num_params > 0; num_params-- )
3976 error = set_mode( renderer, parameters->tag, parameters->data );
3988 FT_BASE_DEF( FT_Error )
3989 FT_Render_Glyph_Internal( FT_Library library,
3991 FT_Render_Mode render_mode )
3993 FT_Error error = FT_Err_Ok;
3994 FT_Renderer renderer;
3997 /* if it is already a bitmap, no need to do anything */
3998 switch ( slot->format )
4000 case FT_GLYPH_FORMAT_BITMAP: /* already a bitmap, don't do anything */
4005 FT_ListNode node = 0;
4009 /* small shortcut for the very common case */
4010 if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
4012 renderer = library->cur_renderer;
4013 node = library->renderers.head;
4016 renderer = FT_Lookup_Renderer( library, slot->format, &node );
4018 error = FT_Err_Unimplemented_Feature;
4021 error = renderer->render( renderer, slot, render_mode, NULL );
4023 FT_ERROR_BASE( error ) != FT_Err_Cannot_Render_Glyph )
4026 /* FT_Err_Cannot_Render_Glyph is returned if the render mode */
4027 /* is unsupported by the current renderer for this glyph image */
4030 /* now, look for another renderer that supports the same */
4032 renderer = FT_Lookup_Renderer( library, slot->format, &node );
4036 /* if we changed the current renderer for the glyph image format */
4037 /* we need to select it as the next current one */
4038 if ( !error && update && renderer )
4039 FT_Set_Renderer( library, renderer, 0, 0 );
4047 /* documentation is in freetype.h */
4049 FT_EXPORT_DEF( FT_Error )
4050 FT_Render_Glyph( FT_GlyphSlot slot,
4051 FT_Render_Mode render_mode )
4056 if ( !slot || !slot->face )
4057 return FT_Err_Invalid_Argument;
4059 library = FT_FACE_LIBRARY( slot->face );
4061 return FT_Render_Glyph_Internal( library, slot, render_mode );
4065 /*************************************************************************/
4066 /*************************************************************************/
4067 /*************************************************************************/
4070 /**** M O D U L E S ****/
4073 /*************************************************************************/
4074 /*************************************************************************/
4075 /*************************************************************************/
4078 /*************************************************************************/
4081 /* Destroy_Module */
4084 /* Destroys a given module object. For drivers, this also destroys */
4085 /* all child faces. */
4088 /* module :: A handle to the target driver object. */
4091 /* The driver _must_ be LOCKED! */
4094 Destroy_Module( FT_Module module )
4096 FT_Memory memory = module->memory;
4097 FT_Module_Class* clazz = module->clazz;
4098 FT_Library library = module->library;
4101 /* finalize client-data - before anything else */
4102 if ( module->generic.finalizer )
4103 module->generic.finalizer( module );
4105 if ( library && library->auto_hinter == module )
4106 library->auto_hinter = 0;
4108 /* if the module is a renderer */
4109 if ( FT_MODULE_IS_RENDERER( module ) )
4110 ft_remove_renderer( module );
4112 /* if the module is a font driver, add some steps */
4113 if ( FT_MODULE_IS_DRIVER( module ) )
4114 Destroy_Driver( FT_DRIVER( module ) );
4116 /* finalize the module object */
4117 if ( clazz->module_done )
4118 clazz->module_done( module );
4125 /* documentation is in ftmodapi.h */
4127 FT_EXPORT_DEF( FT_Error )
4128 FT_Add_Module( FT_Library library,
4129 const FT_Module_Class* clazz )
4137 #define FREETYPE_VER_FIXED ( ( (FT_Long)FREETYPE_MAJOR << 16 ) | \
4141 return FT_Err_Invalid_Library_Handle;
4144 return FT_Err_Invalid_Argument;
4146 /* check freetype version */
4147 if ( clazz->module_requires > FREETYPE_VER_FIXED )
4148 return FT_Err_Invalid_Version;
4150 /* look for a module with the same name in the library's table */
4151 for ( nn = 0; nn < library->num_modules; nn++ )
4153 module = library->modules[nn];
4154 if ( ft_strcmp( module->clazz->module_name, clazz->module_name ) == 0 )
4156 /* this installed module has the same name, compare their versions */
4157 if ( clazz->module_version <= module->clazz->module_version )
4158 return FT_Err_Lower_Module_Version;
4160 /* remove the module from our list, then exit the loop to replace */
4161 /* it by our new version.. */
4162 FT_Remove_Module( library, module );
4167 memory = library->memory;
4170 if ( library->num_modules >= FT_MAX_MODULES )
4172 error = FT_Err_Too_Many_Drivers;
4176 /* allocate module object */
4177 if ( FT_ALLOC( module, clazz->module_size ) )
4180 /* base initialization */
4181 module->library = library;
4182 module->memory = memory;
4183 module->clazz = (FT_Module_Class*)clazz;
4185 /* check whether the module is a renderer - this must be performed */
4186 /* before the normal module initialization */
4187 if ( FT_MODULE_IS_RENDERER( module ) )
4189 /* add to the renderers list */
4190 error = ft_add_renderer( module );
4195 /* is the module a auto-hinter? */
4196 if ( FT_MODULE_IS_HINTER( module ) )
4197 library->auto_hinter = module;
4199 /* if the module is a font driver */
4200 if ( FT_MODULE_IS_DRIVER( module ) )
4202 /* allocate glyph loader if needed */
4203 FT_Driver driver = FT_DRIVER( module );
4206 driver->clazz = (FT_Driver_Class)module->clazz;
4207 if ( FT_DRIVER_USES_OUTLINES( driver ) )
4209 error = FT_GlyphLoader_New( memory, &driver->glyph_loader );
4215 if ( clazz->module_init )
4217 error = clazz->module_init( module );
4222 /* add module to the library's table */
4223 library->modules[library->num_modules++] = module;
4229 if ( FT_MODULE_IS_DRIVER( module ) )
4231 FT_Driver driver = FT_DRIVER( module );
4234 if ( FT_DRIVER_USES_OUTLINES( driver ) )
4235 FT_GlyphLoader_Done( driver->glyph_loader );
4238 if ( FT_MODULE_IS_RENDERER( module ) )
4240 FT_Renderer renderer = FT_RENDERER( module );
4243 if ( renderer->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
4245 renderer->clazz->raster_class->raster_done( renderer->raster );
4253 /* documentation is in ftmodapi.h */
4255 FT_EXPORT_DEF( FT_Module )
4256 FT_Get_Module( FT_Library library,
4257 const char* module_name )
4259 FT_Module result = 0;
4264 if ( !library || !module_name )
4267 cur = library->modules;
4268 limit = cur + library->num_modules;
4270 for ( ; cur < limit; cur++ )
4271 if ( ft_strcmp( cur[0]->clazz->module_name, module_name ) == 0 )
4281 /* documentation is in ftobjs.h */
4283 FT_BASE_DEF( const void* )
4284 FT_Get_Module_Interface( FT_Library library,
4285 const char* mod_name )
4290 /* test for valid `library' delayed to FT_Get_Module() */
4292 module = FT_Get_Module( library, mod_name );
4294 return module ? module->clazz->module_interface : 0;
4298 FT_BASE_DEF( FT_Pointer )
4299 ft_module_get_service( FT_Module module,
4300 const char* service_id )
4302 FT_Pointer result = NULL;
4306 FT_ASSERT( module->clazz && module->clazz->get_interface );
4308 /* first, look for the service in the module
4310 if ( module->clazz->get_interface )
4311 result = module->clazz->get_interface( module, service_id );
4313 if ( result == NULL )
4315 /* we didn't find it, look in all other modules then
4317 FT_Library library = module->library;
4318 FT_Module* cur = library->modules;
4319 FT_Module* limit = cur + library->num_modules;
4321 for ( ; cur < limit; cur++ )
4323 if ( cur[0] != module )
4325 FT_ASSERT( cur[0]->clazz );
4327 if ( cur[0]->clazz->get_interface )
4329 result = cur[0]->clazz->get_interface( cur[0], service_id );
4330 if ( result != NULL )
4342 /* documentation is in ftmodapi.h */
4344 FT_EXPORT_DEF( FT_Error )
4345 FT_Remove_Module( FT_Library library,
4348 /* try to find the module from the table, then remove it from there */
4351 return FT_Err_Invalid_Library_Handle;
4355 FT_Module* cur = library->modules;
4356 FT_Module* limit = cur + library->num_modules;
4359 for ( ; cur < limit; cur++ )
4361 if ( cur[0] == module )
4363 /* remove it from the table */
4364 library->num_modules--;
4366 while ( cur < limit )
4373 /* destroy the module */
4374 Destroy_Module( module );
4380 return FT_Err_Invalid_Driver_Handle;
4384 /*************************************************************************/
4385 /*************************************************************************/
4386 /*************************************************************************/
4389 /**** L I B R A R Y ****/
4392 /*************************************************************************/
4393 /*************************************************************************/
4394 /*************************************************************************/
4397 /* documentation is in ftmodapi.h */
4399 FT_EXPORT_DEF( FT_Error )
4400 FT_Reference_Library( FT_Library library )
4402 library->refcount++;
4408 /* documentation is in ftmodapi.h */
4410 FT_EXPORT_DEF( FT_Error )
4411 FT_New_Library( FT_Memory memory,
4412 FT_Library *alibrary )
4414 FT_Library library = NULL;
4419 return FT_Err_Invalid_Argument;
4421 #ifdef FT_DEBUG_LEVEL_ERROR
4422 /* init debugging support */
4426 /* first of all, allocate the library object */
4427 if ( FT_NEW( library ) )
4430 library->memory = memory;
4432 #ifdef FT_CONFIG_OPTION_PIC
4433 /* initialize position independent code containers */
4434 error = ft_pic_container_init( library );
4439 /* allocate the render pool */
4440 library->raster_pool_size = FT_RENDER_POOL_SIZE;
4441 #if FT_RENDER_POOL_SIZE > 0
4442 if ( FT_ALLOC( library->raster_pool, FT_RENDER_POOL_SIZE ) )
4446 library->version_major = FREETYPE_MAJOR;
4447 library->version_minor = FREETYPE_MINOR;
4448 library->version_patch = FREETYPE_PATCH;
4450 library->refcount = 1;
4453 *alibrary = library;
4458 #ifdef FT_CONFIG_OPTION_PIC
4459 ft_pic_container_destroy( library );
4466 /* documentation is in freetype.h */
4468 FT_EXPORT_DEF( void )
4469 FT_Library_Version( FT_Library library,
4481 major = library->version_major;
4482 minor = library->version_minor;
4483 patch = library->version_patch;
4497 /* documentation is in ftmodapi.h */
4499 FT_EXPORT_DEF( FT_Error )
4500 FT_Done_Library( FT_Library library )
4506 return FT_Err_Invalid_Library_Handle;
4508 library->refcount--;
4509 if ( library->refcount > 0 )
4512 memory = library->memory;
4514 /* Discard client-data */
4515 if ( library->generic.finalizer )
4516 library->generic.finalizer( library );
4519 * Close all faces in the library. If we don't do this, we can have
4520 * some subtle memory leaks.
4524 * - the cff font driver uses the pshinter module in cff_size_done
4525 * - if the pshinter module is destroyed before the cff font driver,
4526 * opened FT_Face objects managed by the driver are not properly
4527 * destroyed, resulting in a memory leak
4529 * Some faces are dependent on other faces, like Type42 faces that
4530 * depend on TrueType faces synthesized internally.
4532 * The order of drivers should be specified in driver_name[].
4536 const char* driver_name[] = { "type42", NULL };
4540 m < sizeof ( driver_name ) / sizeof ( driver_name[0] );
4543 for ( n = 0; n < library->num_modules; n++ )
4545 FT_Module module = library->modules[n];
4546 const char* module_name = module->clazz->module_name;
4550 if ( driver_name[m] &&
4551 ft_strcmp( module_name, driver_name[m] ) != 0 )
4554 if ( ( module->clazz->module_flags & FT_MODULE_FONT_DRIVER ) == 0 )
4557 FT_TRACE7(( "FT_Done_Library: close faces for %s\n", module_name ));
4559 faces = &FT_DRIVER( module )->faces_list;
4560 while ( faces->head )
4562 FT_Done_Face( FT_FACE( faces->head->data ) );
4564 FT_TRACE0(( "FT_Done_Library: failed to free some faces\n" ));
4570 /* Close all other modules in the library */
4572 /* XXX Modules are removed in the reversed order so that */
4573 /* type42 module is removed before truetype module. This */
4574 /* avoids double free in some occasions. It is a hack. */
4575 while ( library->num_modules > 0 )
4576 FT_Remove_Module( library,
4577 library->modules[library->num_modules - 1] );
4583 for ( n = 0; n < library->num_modules; n++ )
4585 FT_Module module = library->modules[n];
4590 Destroy_Module( module );
4591 library->modules[n] = 0;
4597 /* Destroy raster objects */
4598 FT_FREE( library->raster_pool );
4599 library->raster_pool_size = 0;
4601 #ifdef FT_CONFIG_OPTION_PIC
4602 /* Destroy pic container contents */
4603 ft_pic_container_destroy( library );
4613 /* documentation is in ftmodapi.h */
4615 FT_EXPORT_DEF( void )
4616 FT_Set_Debug_Hook( FT_Library library,
4618 FT_DebugHook_Func debug_hook )
4620 if ( library && debug_hook &&
4622 ( sizeof ( library->debug_hooks ) / sizeof ( void* ) ) )
4623 library->debug_hooks[hook_index] = debug_hook;
4627 /* documentation is in ftmodapi.h */
4629 FT_EXPORT_DEF( FT_TrueTypeEngineType )
4630 FT_Get_TrueType_Engine_Type( FT_Library library )
4632 FT_TrueTypeEngineType result = FT_TRUETYPE_ENGINE_TYPE_NONE;
4637 FT_Module module = FT_Get_Module( library, "truetype" );
4642 FT_Service_TrueTypeEngine service;
4645 service = (FT_Service_TrueTypeEngine)
4646 ft_module_get_service( module,
4647 FT_SERVICE_ID_TRUETYPE_ENGINE );
4649 result = service->engine_type;
4657 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
4659 FT_BASE_DEF( FT_Error )
4660 ft_stub_set_char_sizes( FT_Size size,
4666 FT_Size_RequestRec req;
4667 FT_Driver driver = size->face->driver;
4670 if ( driver->clazz->request_size )
4672 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
4674 req.height = height;
4676 if ( horz_res == 0 )
4677 horz_res = vert_res;
4679 if ( vert_res == 0 )
4680 vert_res = horz_res;
4682 if ( horz_res == 0 )
4683 horz_res = vert_res = 72;
4685 req.horiResolution = horz_res;
4686 req.vertResolution = vert_res;
4688 return driver->clazz->request_size( size, &req );
4695 FT_BASE_DEF( FT_Error )
4696 ft_stub_set_pixel_sizes( FT_Size size,
4700 FT_Size_RequestRec req;
4701 FT_Driver driver = size->face->driver;
4704 if ( driver->clazz->request_size )
4706 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
4707 req.width = width << 6;
4708 req.height = height << 6;
4709 req.horiResolution = 0;
4710 req.vertResolution = 0;
4712 return driver->clazz->request_size( size, &req );
4718 #endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
4721 /* documentation is in freetype.h */
4723 FT_EXPORT_DEF( FT_Error )
4724 FT_Get_SubGlyph_Info( FT_GlyphSlot glyph,
4730 FT_Matrix *p_transform )
4732 FT_Error error = FT_Err_Invalid_Argument;
4737 glyph->format == FT_GLYPH_FORMAT_COMPOSITE &&
4738 sub_index < glyph->num_subglyphs )
4740 FT_SubGlyph subg = glyph->subglyphs + sub_index;
4743 *p_index = subg->index;
4744 *p_flags = subg->flags;
4745 *p_arg1 = subg->arg1;
4746 *p_arg2 = subg->arg2;
4747 *p_transform = subg->transform;