1 /****************************************************************************
5 * The FreeType private base classes (body).
7 * Copyright (C) 1996-2023 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.
19 #include <freetype/ftlist.h>
20 #include <freetype/ftoutln.h>
21 #include <freetype/ftfntfmt.h>
22 #include <freetype/otsvg.h>
24 #include <freetype/internal/ftvalid.h>
25 #include <freetype/internal/ftobjs.h>
26 #include <freetype/internal/ftdebug.h>
27 #include <freetype/internal/ftrfork.h>
28 #include <freetype/internal/ftstream.h>
29 #include <freetype/internal/sfnt.h> /* for SFNT_Load_Table_Func */
30 #include <freetype/internal/psaux.h> /* for PS_Driver */
31 #include <freetype/internal/svginterface.h>
33 #include <freetype/tttables.h>
34 #include <freetype/tttags.h>
35 #include <freetype/ttnameid.h>
37 #include <freetype/internal/services/svprop.h>
38 #include <freetype/internal/services/svsfnt.h>
39 #include <freetype/internal/services/svpostnm.h>
40 #include <freetype/internal/services/svgldict.h>
41 #include <freetype/internal/services/svttcmap.h>
42 #include <freetype/internal/services/svkern.h>
43 #include <freetype/internal/services/svtteng.h>
45 #include <freetype/ftdriver.h>
47 #ifdef FT_CONFIG_OPTION_MAC_FONTS
52 #ifdef FT_DEBUG_LEVEL_TRACE
54 #include <freetype/ftbitmap.h>
56 #if defined( _MSC_VER ) /* Visual C++ (and Intel C++) */
57 /* We disable the warning `conversion from XXX to YYY, */
58 /* possible loss of data' in order to compile cleanly with */
59 /* the maximum level of warnings: `md5.c' is non-FreeType */
60 /* code, and it gets used during development builds only. */
61 #pragma warning( push )
62 #pragma warning( disable : 4244 )
65 /* It's easiest to include `md5.c' directly. However, since OpenSSL */
66 /* also provides the same functions, there might be conflicts if */
67 /* both FreeType and OpenSSL are built as static libraries. For */
68 /* this reason, we put the MD5 stuff into the `FT_' namespace. */
69 #define MD5_u32plus FT_MD5_u32plus
70 #define MD5_CTX FT_MD5_CTX
71 #define MD5_Init FT_MD5_Init
72 #define MD5_Update FT_MD5_Update
73 #define MD5_Final FT_MD5_Final
79 #if defined( _MSC_VER )
80 #pragma warning( pop )
83 /* This array must stay in sync with the @FT_Pixel_Mode enumeration */
84 /* (in file `ftimage.h`). */
86 static const char* const pixel_modes[] =
94 "vertical LCD 8-bit bitmap",
95 "BGRA 32-bit color image bitmap",
99 #endif /* FT_DEBUG_LEVEL_TRACE */
102 #define GRID_FIT_METRICS
105 /* forward declaration */
107 ft_open_face_internal( FT_Library library,
108 const FT_Open_Args* args,
111 FT_Bool test_mac_fonts );
114 FT_BASE_DEF( FT_Pointer )
115 ft_service_list_lookup( FT_ServiceDesc service_descriptors,
116 const char* service_id )
118 FT_Pointer result = NULL;
119 FT_ServiceDesc desc = service_descriptors;
122 if ( desc && service_id )
124 for ( ; desc->serv_id != NULL; desc++ )
126 if ( ft_strcmp( desc->serv_id, service_id ) == 0 )
128 result = (FT_Pointer)desc->serv_data;
139 ft_validator_init( FT_Validator valid,
141 const FT_Byte* limit,
142 FT_ValidationLevel level )
145 valid->limit = limit;
146 valid->level = level;
147 valid->error = FT_Err_Ok;
151 FT_BASE_DEF( FT_Int )
152 ft_validator_run( FT_Validator valid )
154 /* This function doesn't work! None should call it. */
162 ft_validator_error( FT_Validator valid,
165 /* since the cast below also disables the compiler's */
166 /* type check, we introduce a dummy variable, which */
167 /* will be optimized away */
168 volatile ft_jmp_buf* jump_buffer = &valid->jump_buffer;
171 valid->error = error;
173 /* throw away volatileness; use `jump_buffer' or the */
174 /* compiler may warn about an unused local variable */
175 ft_longjmp( *(ft_jmp_buf*) jump_buffer, 1 );
179 /*************************************************************************/
180 /*************************************************************************/
181 /*************************************************************************/
184 /**** S T R E A M ****/
187 /*************************************************************************/
188 /*************************************************************************/
189 /*************************************************************************/
192 /* create a new input stream from an FT_Open_Args structure */
194 FT_BASE_DEF( FT_Error )
195 FT_Stream_New( FT_Library library,
196 const FT_Open_Args* args,
201 FT_Stream stream = NULL;
208 return FT_THROW( Invalid_Library_Handle );
211 return FT_THROW( Invalid_Argument );
213 memory = library->memory;
215 ( FT_OPEN_MEMORY | FT_OPEN_STREAM | FT_OPEN_PATHNAME );
217 if ( mode == FT_OPEN_MEMORY )
219 /* create a memory-based stream */
220 if ( FT_NEW( stream ) )
223 FT_Stream_OpenMemory( stream,
224 (const FT_Byte*)args->memory_base,
225 (FT_ULong)args->memory_size );
226 stream->memory = memory;
229 #ifndef FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT
231 else if ( mode == FT_OPEN_PATHNAME )
233 /* create a normal system stream */
234 if ( FT_NEW( stream ) )
237 stream->memory = memory;
238 error = FT_Stream_Open( stream, args->pathname );
242 else if ( ( mode == FT_OPEN_STREAM ) && args->stream )
244 /* use an existing, user-provided stream */
246 /* in this case, we do not need to allocate a new stream object */
247 /* since the caller is responsible for closing it himself */
248 stream = args->stream;
249 stream->memory = memory;
257 error = FT_THROW( Invalid_Argument );
258 if ( ( args->flags & FT_OPEN_STREAM ) && args->stream )
259 FT_Stream_Close( args->stream );
271 FT_Stream_Free( FT_Stream stream,
276 FT_Memory memory = stream->memory;
279 FT_Stream_Close( stream );
287 /**************************************************************************
289 * The macro FT_COMPONENT is used in trace mode. It is an implicit
290 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
291 * messages during execution.
294 #define FT_COMPONENT objs
297 /*************************************************************************/
298 /*************************************************************************/
299 /*************************************************************************/
302 /**** FACE, SIZE & GLYPH SLOT OBJECTS ****/
305 /*************************************************************************/
306 /*************************************************************************/
307 /*************************************************************************/
311 ft_glyphslot_init( FT_GlyphSlot slot )
313 FT_Driver driver = slot->face->driver;
314 FT_Driver_Class clazz = driver->clazz;
315 FT_Memory memory = driver->root.memory;
316 FT_Error error = FT_Err_Ok;
317 FT_Slot_Internal internal = NULL;
320 slot->library = driver->root.library;
322 if ( FT_NEW( internal ) )
325 slot->internal = internal;
327 if ( FT_DRIVER_USES_OUTLINES( driver ) )
328 error = FT_GlyphLoader_New( memory, &internal->loader );
330 if ( !error && clazz->init_slot )
331 error = clazz->init_slot( slot );
333 #ifdef FT_CONFIG_OPTION_SVG
334 /* if SVG table exists, allocate the space in `slot->other` */
335 if ( slot->face->face_flags & FT_FACE_FLAG_SVG )
337 FT_SVG_Document document = NULL;
340 if ( FT_NEW( document ) )
342 slot->other = document;
352 ft_glyphslot_free_bitmap( FT_GlyphSlot slot )
354 if ( slot->internal && ( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) )
356 FT_Memory memory = FT_FACE_MEMORY( slot->face );
359 FT_FREE( slot->bitmap.buffer );
360 slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
364 /* assume that the bitmap buffer was stolen or not */
365 /* allocated from the heap */
366 slot->bitmap.buffer = NULL;
371 /* overflow-resistant presetting of bitmap position and dimensions; */
372 /* also check whether the size is too large for rendering */
373 FT_BASE_DEF( FT_Bool )
374 ft_glyphslot_preset_bitmap( FT_GlyphSlot slot,
376 const FT_Vector* origin )
378 FT_Outline* outline = &slot->outline;
379 FT_Bitmap* bitmap = &slot->bitmap;
381 FT_Pixel_Mode pixel_mode;
386 FT_Pos x_left, y_top;
387 FT_Pos width, height, pitch;
390 if ( slot->format == FT_GLYPH_FORMAT_SVG )
393 SVG_Service svg_service;
396 module = FT_Get_Module( slot->library, "ot-svg" );
397 svg_service = (SVG_Service)module->clazz->module_interface;
399 return (FT_Bool)svg_service->preset_slot( module, slot, FALSE );
401 else if ( slot->format != FT_GLYPH_FORMAT_OUTLINE )
410 /* compute the control box, and grid-fit it, */
411 /* taking into account the origin shift */
412 FT_Outline_Get_CBox( outline, &cbox );
414 /* rough estimate of pixel box */
415 pbox.xMin = ( cbox.xMin >> 6 ) + ( x_shift >> 6 );
416 pbox.yMin = ( cbox.yMin >> 6 ) + ( y_shift >> 6 );
417 pbox.xMax = ( cbox.xMax >> 6 ) + ( x_shift >> 6 );
418 pbox.yMax = ( cbox.yMax >> 6 ) + ( y_shift >> 6 );
420 /* tiny remainder box */
421 cbox.xMin = ( cbox.xMin & 63 ) + ( x_shift & 63 );
422 cbox.yMin = ( cbox.yMin & 63 ) + ( y_shift & 63 );
423 cbox.xMax = ( cbox.xMax & 63 ) + ( x_shift & 63 );
424 cbox.yMax = ( cbox.yMax & 63 ) + ( y_shift & 63 );
428 case FT_RENDER_MODE_MONO:
429 pixel_mode = FT_PIXEL_MODE_MONO;
433 /* undocumented but confirmed: bbox values get rounded; */
434 /* we do asymmetric rounding so that the center of a pixel */
435 /* gets always included */
437 pbox.xMin += ( cbox.xMin + 31 ) >> 6;
438 pbox.xMax += ( cbox.xMax + 32 ) >> 6;
440 /* if the bbox collapsed, we add a pixel based on the total */
441 /* rounding remainder to cover most of the original cbox */
443 if ( pbox.xMin == pbox.xMax )
445 if ( ( ( cbox.xMin + 31 ) & 63 ) - 31 +
446 ( ( cbox.xMax + 32 ) & 63 ) - 32 < 0 )
454 pbox.yMin += ( cbox.yMin + 31 ) >> 6;
455 pbox.yMax += ( cbox.yMax + 32 ) >> 6;
457 if ( pbox.yMin == pbox.yMax )
459 if ( ( ( cbox.yMin + 31 ) & 63 ) - 31 +
460 ( ( cbox.yMax + 32 ) & 63 ) - 32 < 0 )
471 case FT_RENDER_MODE_LCD:
472 pixel_mode = FT_PIXEL_MODE_LCD;
473 ft_lcd_padding( &cbox, slot, mode );
476 case FT_RENDER_MODE_LCD_V:
477 pixel_mode = FT_PIXEL_MODE_LCD_V;
478 ft_lcd_padding( &cbox, slot, mode );
481 case FT_RENDER_MODE_NORMAL:
482 case FT_RENDER_MODE_LIGHT:
484 pixel_mode = FT_PIXEL_MODE_GRAY;
486 pbox.xMin += cbox.xMin >> 6;
487 pbox.yMin += cbox.yMin >> 6;
488 pbox.xMax += ( cbox.xMax + 63 ) >> 6;
489 pbox.yMax += ( cbox.yMax + 63 ) >> 6;
495 width = pbox.xMax - pbox.xMin;
496 height = pbox.yMax - pbox.yMin;
498 switch ( pixel_mode )
500 case FT_PIXEL_MODE_MONO:
501 pitch = ( ( width + 15 ) >> 4 ) << 1;
504 case FT_PIXEL_MODE_LCD:
506 pitch = FT_PAD_CEIL( width, 4 );
509 case FT_PIXEL_MODE_LCD_V:
513 case FT_PIXEL_MODE_GRAY:
518 slot->bitmap_left = (FT_Int)x_left;
519 slot->bitmap_top = (FT_Int)y_top;
521 bitmap->pixel_mode = (unsigned char)pixel_mode;
522 bitmap->num_grays = 256;
523 bitmap->width = (unsigned int)width;
524 bitmap->rows = (unsigned int)height;
525 bitmap->pitch = pitch;
527 if ( pbox.xMin < -0x8000 || pbox.xMax > 0x7FFF ||
528 pbox.yMin < -0x8000 || pbox.yMax > 0x7FFF )
530 FT_TRACE3(( "ft_glyphslot_preset_bitmap: [%ld %ld %ld %ld]\n",
531 pbox.xMin, pbox.yMin, pbox.xMax, pbox.yMax ));
540 ft_glyphslot_set_bitmap( FT_GlyphSlot slot,
543 ft_glyphslot_free_bitmap( slot );
545 slot->bitmap.buffer = buffer;
547 FT_ASSERT( (slot->internal->flags & FT_GLYPH_OWN_BITMAP) == 0 );
551 FT_BASE_DEF( FT_Error )
552 ft_glyphslot_alloc_bitmap( FT_GlyphSlot slot,
555 FT_Memory memory = FT_FACE_MEMORY( slot->face );
559 if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
560 FT_FREE( slot->bitmap.buffer );
562 slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
564 FT_MEM_ALLOC( slot->bitmap.buffer, size );
570 ft_glyphslot_clear( FT_GlyphSlot slot )
572 /* free bitmap if needed */
573 ft_glyphslot_free_bitmap( slot );
575 /* clear all public fields in the glyph slot */
576 slot->glyph_index = 0;
578 FT_ZERO( &slot->metrics );
579 FT_ZERO( &slot->outline );
581 slot->bitmap.width = 0;
582 slot->bitmap.rows = 0;
583 slot->bitmap.pitch = 0;
584 slot->bitmap.pixel_mode = 0;
585 /* `slot->bitmap.buffer' has been handled by ft_glyphslot_free_bitmap */
587 slot->bitmap_left = 0;
588 slot->bitmap_top = 0;
589 slot->num_subglyphs = 0;
590 slot->subglyphs = NULL;
591 slot->control_data = NULL;
592 slot->control_len = 0;
594 #ifndef FT_CONFIG_OPTION_SVG
597 if ( !( slot->face->face_flags & FT_FACE_FLAG_SVG ) )
601 if ( slot->internal->flags & FT_GLYPH_OWN_GZIP_SVG )
603 FT_Memory memory = slot->face->memory;
604 FT_SVG_Document doc = (FT_SVG_Document)slot->other;
607 FT_FREE( doc->svg_document );
608 slot->internal->flags &= ~FT_GLYPH_OWN_GZIP_SVG;
613 slot->format = FT_GLYPH_FORMAT_NONE;
615 slot->linearHoriAdvance = 0;
616 slot->linearVertAdvance = 0;
625 ft_glyphslot_done( FT_GlyphSlot slot )
627 FT_Driver driver = slot->face->driver;
628 FT_Driver_Class clazz = driver->clazz;
629 FT_Memory memory = driver->root.memory;
631 #ifdef FT_CONFIG_OPTION_SVG
632 if ( slot->face->face_flags & FT_FACE_FLAG_SVG )
634 /* Free memory in case SVG was there. */
635 /* `slot->internal` might be NULL in out-of-memory situations. */
636 if ( slot->internal && slot->internal->flags & FT_GLYPH_OWN_GZIP_SVG )
638 FT_SVG_Document doc = (FT_SVG_Document)slot->other;
641 FT_FREE( doc->svg_document );
643 slot->internal->flags &= ~FT_GLYPH_OWN_GZIP_SVG;
646 FT_FREE( slot->other );
650 if ( clazz->done_slot )
651 clazz->done_slot( slot );
653 /* free bitmap buffer if needed */
654 ft_glyphslot_free_bitmap( slot );
656 /* slot->internal might be NULL in out-of-memory situations */
657 if ( slot->internal )
659 /* free glyph loader */
660 if ( FT_DRIVER_USES_OUTLINES( driver ) )
662 FT_GlyphLoader_Done( slot->internal->loader );
663 slot->internal->loader = NULL;
666 FT_FREE( slot->internal );
671 /* documentation is in ftobjs.h */
673 FT_BASE_DEF( FT_Error )
674 FT_New_GlyphSlot( FT_Face face,
675 FT_GlyphSlot *aslot )
679 FT_Driver_Class clazz;
681 FT_GlyphSlot slot = NULL;
685 return FT_THROW( Invalid_Face_Handle );
688 return FT_THROW( Invalid_Argument );
690 driver = face->driver;
691 clazz = driver->clazz;
692 memory = driver->root.memory;
694 FT_TRACE4(( "FT_New_GlyphSlot: Creating new slot object\n" ));
695 if ( !FT_ALLOC( slot, clazz->slot_object_size ) )
699 error = ft_glyphslot_init( slot );
702 ft_glyphslot_done( slot );
707 slot->next = face->glyph;
718 FT_TRACE4(( "FT_New_GlyphSlot: Return 0x%x\n", error ));
724 /* documentation is in ftobjs.h */
727 FT_Done_GlyphSlot( FT_GlyphSlot slot )
731 FT_Driver driver = slot->face->driver;
732 FT_Memory memory = driver->root.memory;
737 /* Remove slot from its parent face's list */
739 cur = slot->face->glyph;
746 slot->face->glyph = cur->next;
748 prev->next = cur->next;
750 /* finalize client-specific data */
751 if ( slot->generic.finalizer )
752 slot->generic.finalizer( slot );
754 ft_glyphslot_done( slot );
765 /* documentation is in freetype.h */
767 FT_EXPORT_DEF( void )
768 FT_Set_Transform( FT_Face face,
772 FT_Face_Internal internal;
778 internal = face->internal;
780 internal->transform_flags = 0;
784 internal->transform_matrix.xx = 0x10000L;
785 internal->transform_matrix.xy = 0;
786 internal->transform_matrix.yx = 0;
787 internal->transform_matrix.yy = 0x10000L;
789 matrix = &internal->transform_matrix;
792 internal->transform_matrix = *matrix;
794 /* set transform_flags bit flag 0 if `matrix' isn't the identity */
795 if ( ( matrix->xy | matrix->yx ) ||
796 matrix->xx != 0x10000L ||
797 matrix->yy != 0x10000L )
798 internal->transform_flags |= 1;
802 internal->transform_delta.x = 0;
803 internal->transform_delta.y = 0;
805 delta = &internal->transform_delta;
808 internal->transform_delta = *delta;
810 /* set transform_flags bit flag 1 if `delta' isn't the null vector */
811 if ( delta->x | delta->y )
812 internal->transform_flags |= 2;
816 /* documentation is in freetype.h */
818 FT_EXPORT_DEF( void )
819 FT_Get_Transform( FT_Face face,
823 FT_Face_Internal internal;
829 internal = face->internal;
832 *matrix = internal->transform_matrix;
835 *delta = internal->transform_delta;
840 ft_lookup_glyph_renderer( FT_GlyphSlot slot );
843 #ifdef GRID_FIT_METRICS
845 ft_glyphslot_grid_fit_metrics( FT_GlyphSlot slot,
848 FT_Glyph_Metrics* metrics = &slot->metrics;
849 FT_Pos right, bottom;
854 metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
855 metrics->horiBearingY = FT_PIX_CEIL_LONG( metrics->horiBearingY );
857 right = FT_PIX_CEIL_LONG( ADD_LONG( metrics->vertBearingX,
859 bottom = FT_PIX_CEIL_LONG( ADD_LONG( metrics->vertBearingY,
862 metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
863 metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
865 metrics->width = SUB_LONG( right,
866 metrics->vertBearingX );
867 metrics->height = SUB_LONG( bottom,
868 metrics->vertBearingY );
872 metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
873 metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
875 right = FT_PIX_CEIL_LONG( ADD_LONG( metrics->horiBearingX,
877 bottom = FT_PIX_FLOOR( SUB_LONG( metrics->horiBearingY,
880 metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
881 metrics->horiBearingY = FT_PIX_CEIL_LONG( metrics->horiBearingY );
883 metrics->width = SUB_LONG( right,
884 metrics->horiBearingX );
885 metrics->height = SUB_LONG( metrics->horiBearingY,
889 metrics->horiAdvance = FT_PIX_ROUND_LONG( metrics->horiAdvance );
890 metrics->vertAdvance = FT_PIX_ROUND_LONG( metrics->vertAdvance );
892 #endif /* GRID_FIT_METRICS */
895 /* documentation is in freetype.h */
897 FT_EXPORT_DEF( FT_Error )
898 FT_Load_Glyph( FT_Face face,
900 FT_Int32 load_flags )
906 FT_Bool autohint = FALSE;
908 TT_Face ttface = (TT_Face)face;
911 if ( !face || !face->size || !face->glyph )
912 return FT_THROW( Invalid_Face_Handle );
914 /* The validity test for `glyph_index' is performed by the */
918 ft_glyphslot_clear( slot );
920 driver = face->driver;
921 library = driver->root.library;
922 hinter = library->auto_hinter;
924 /* undefined scale means no scale */
925 if ( face->size->metrics.x_ppem == 0 ||
926 face->size->metrics.y_ppem == 0 )
927 load_flags |= FT_LOAD_NO_SCALE;
929 /* resolve load flags dependencies */
931 if ( load_flags & FT_LOAD_NO_RECURSE )
932 load_flags |= FT_LOAD_NO_SCALE |
933 FT_LOAD_IGNORE_TRANSFORM;
935 if ( load_flags & FT_LOAD_NO_SCALE )
937 load_flags |= FT_LOAD_NO_HINTING |
940 load_flags &= ~FT_LOAD_RENDER;
943 if ( load_flags & FT_LOAD_BITMAP_METRICS_ONLY )
944 load_flags &= ~FT_LOAD_RENDER;
947 * Determine whether we need to auto-hint or not.
948 * The general rules are:
950 * - Do only auto-hinting if we have
954 * - not a tricky font, and
955 * - no transforms except simple slants and/or rotations by
956 * integer multiples of 90 degrees.
958 * - Then, auto-hint if FT_LOAD_FORCE_AUTOHINT is set or if we don't
959 * have a native font hinter.
961 * - Otherwise, auto-hint for LIGHT hinting mode or if there isn't
962 * any hinting bytecode in the TrueType/OpenType font.
964 * - Exception: The font is `tricky' and requires the native hinter to
969 !( load_flags & FT_LOAD_NO_HINTING ) &&
970 !( load_flags & FT_LOAD_NO_AUTOHINT ) &&
971 FT_IS_SCALABLE( face ) &&
972 !FT_IS_TRICKY( face ) &&
973 ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) ||
974 ( face->internal->transform_matrix.yx == 0 &&
975 face->internal->transform_matrix.xx != 0 ) ||
976 ( face->internal->transform_matrix.xx == 0 &&
977 face->internal->transform_matrix.yx != 0 ) ) )
979 if ( ( load_flags & FT_LOAD_FORCE_AUTOHINT ) ||
980 !FT_DRIVER_HAS_HINTER( driver ) )
984 FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags );
985 FT_Bool is_light_type1;
988 /* only the new Adobe engine (for both CFF and Type 1) is `light'; */
989 /* we use `strstr' to catch both `Type 1' and `CID Type 1' */
991 ft_strstr( FT_Get_Font_Format( face ), "Type 1" ) != NULL &&
992 ((PS_Driver)driver)->hinting_engine == FT_HINTING_ADOBE;
994 /* the check for `num_locations' assures that we actually */
995 /* test for instructions in a TTF and not in a CFF-based OTF */
997 /* since `maxSizeOfInstructions' might be unreliable, we */
998 /* check the size of the `fpgm' and `prep' tables, too -- */
999 /* the assumption is that there don't exist real TTFs where */
1000 /* both `fpgm' and `prep' tables are missing */
1001 if ( ( mode == FT_RENDER_MODE_LIGHT &&
1002 ( !FT_DRIVER_HINTS_LIGHTLY( driver ) &&
1003 !is_light_type1 ) ) ||
1004 ( FT_IS_SFNT( face ) &&
1005 ttface->num_locations &&
1006 ttface->max_profile.maxSizeOfInstructions == 0 &&
1007 ttface->font_program_size == 0 &&
1008 ttface->cvt_program_size == 0 ) )
1015 FT_AutoHinter_Interface hinting;
1018 /* XXX: The use of the `FT_LOAD_XXX_ONLY` flags is not very */
1021 /* try to load SVG documents if available */
1022 if ( ( load_flags & FT_LOAD_NO_SVG ) == 0 &&
1023 FT_HAS_SVG( face ) )
1025 error = driver->clazz->load_glyph( slot, face->size,
1027 load_flags | FT_LOAD_SVG_ONLY );
1029 if ( !error && slot->format == FT_GLYPH_FORMAT_SVG )
1033 /* try to load embedded bitmaps if available */
1034 if ( FT_HAS_FIXED_SIZES( face ) &&
1035 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
1037 error = driver->clazz->load_glyph( slot, face->size,
1039 load_flags | FT_LOAD_SBITS_ONLY );
1041 if ( !error && slot->format == FT_GLYPH_FORMAT_BITMAP )
1046 FT_Face_Internal internal = face->internal;
1047 FT_Int transform_flags = internal->transform_flags;
1050 /* since the auto-hinter calls FT_Load_Glyph by itself, */
1051 /* make sure that glyphs aren't transformed */
1052 internal->transform_flags = 0;
1054 /* load auto-hinted outline */
1055 hinting = (FT_AutoHinter_Interface)hinter->clazz->module_interface;
1057 error = hinting->load_glyph( (FT_AutoHinter)hinter,
1059 glyph_index, load_flags );
1061 internal->transform_flags = transform_flags;
1066 error = driver->clazz->load_glyph( slot,
1073 if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
1075 /* check that the loaded outline is correct */
1076 error = FT_Outline_Check( &slot->outline );
1080 #ifdef GRID_FIT_METRICS
1081 if ( !( load_flags & FT_LOAD_NO_HINTING ) )
1082 ft_glyphslot_grid_fit_metrics(
1084 FT_BOOL( load_flags & FT_LOAD_VERTICAL_LAYOUT ) );
1090 /* compute the advance */
1091 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
1093 slot->advance.x = 0;
1094 slot->advance.y = slot->metrics.vertAdvance;
1098 slot->advance.x = slot->metrics.horiAdvance;
1099 slot->advance.y = 0;
1102 /* compute the linear advance in 16.16 pixels */
1103 if ( ( load_flags & FT_LOAD_LINEAR_DESIGN ) == 0 &&
1104 FT_IS_SCALABLE( face ) )
1106 FT_Size_Metrics* metrics = &face->size->metrics;
1110 slot->linearHoriAdvance = FT_MulDiv( slot->linearHoriAdvance,
1111 metrics->x_scale, 64 );
1113 slot->linearVertAdvance = FT_MulDiv( slot->linearVertAdvance,
1114 metrics->y_scale, 64 );
1117 if ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) == 0 )
1119 FT_Face_Internal internal = face->internal;
1122 /* now, transform the glyph image if needed */
1123 if ( internal->transform_flags )
1126 FT_Renderer renderer = ft_lookup_glyph_renderer( slot );
1130 error = renderer->clazz->transform_glyph(
1132 &internal->transform_matrix,
1133 &internal->transform_delta );
1134 else if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
1136 /* apply `standard' transformation if no renderer is available */
1137 if ( internal->transform_flags & 1 )
1138 FT_Outline_Transform( &slot->outline,
1139 &internal->transform_matrix );
1141 if ( internal->transform_flags & 2 )
1142 FT_Outline_Translate( &slot->outline,
1143 internal->transform_delta.x,
1144 internal->transform_delta.y );
1147 /* transform advance */
1148 FT_Vector_Transform( &slot->advance, &internal->transform_matrix );
1152 slot->glyph_index = glyph_index;
1153 slot->internal->load_flags = load_flags;
1155 /* do we need to render the image or preset the bitmap now? */
1157 ( load_flags & FT_LOAD_NO_SCALE ) == 0 &&
1158 slot->format != FT_GLYPH_FORMAT_BITMAP &&
1159 slot->format != FT_GLYPH_FORMAT_COMPOSITE )
1161 FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags );
1164 if ( mode == FT_RENDER_MODE_NORMAL &&
1165 load_flags & FT_LOAD_MONOCHROME )
1166 mode = FT_RENDER_MODE_MONO;
1168 if ( load_flags & FT_LOAD_RENDER )
1169 error = FT_Render_Glyph( slot, mode );
1171 ft_glyphslot_preset_bitmap( slot, mode, NULL );
1174 #ifdef FT_DEBUG_LEVEL_TRACE
1175 FT_TRACE5(( "FT_Load_Glyph: index %d, flags 0x%x\n",
1176 glyph_index, load_flags ));
1177 FT_TRACE5(( " bitmap %dx%d %s, %s (mode %d)\n",
1180 slot->outline.points ?
1181 slot->bitmap.buffer ? "rendered"
1184 slot->internal->flags & FT_GLYPH_OWN_BITMAP ? "owned"
1186 pixel_modes[slot->bitmap.pixel_mode],
1187 slot->bitmap.pixel_mode ));
1188 FT_TRACE5(( "\n" ));
1189 FT_TRACE5(( " x advance: %f\n", (double)slot->advance.x / 64 ));
1190 FT_TRACE5(( " y advance: %f\n", (double)slot->advance.y / 64 ));
1191 FT_TRACE5(( " linear x advance: %f\n",
1192 (double)slot->linearHoriAdvance / 65536 ));
1193 FT_TRACE5(( " linear y advance: %f\n",
1194 (double)slot->linearVertAdvance / 65536 ));
1197 FT_Glyph_Metrics* metrics = &slot->metrics;
1200 FT_TRACE5(( " metrics:\n" ));
1201 FT_TRACE5(( " width: %f\n", (double)metrics->width / 64 ));
1202 FT_TRACE5(( " height: %f\n", (double)metrics->height / 64 ));
1203 FT_TRACE5(( "\n" ));
1204 FT_TRACE5(( " horiBearingX: %f\n",
1205 (double)metrics->horiBearingX / 64 ));
1206 FT_TRACE5(( " horiBearingY: %f\n",
1207 (double)metrics->horiBearingY / 64 ));
1208 FT_TRACE5(( " horiAdvance: %f\n",
1209 (double)metrics->horiAdvance / 64 ));
1210 FT_TRACE5(( "\n" ));
1211 FT_TRACE5(( " vertBearingX: %f\n",
1212 (double)metrics->vertBearingX / 64 ));
1213 FT_TRACE5(( " vertBearingY: %f\n",
1214 (double)metrics->vertBearingY / 64 ));
1215 FT_TRACE5(( " vertAdvance: %f\n",
1216 (double)metrics->vertAdvance / 64 ));
1225 /* documentation is in freetype.h */
1227 FT_EXPORT_DEF( FT_Error )
1228 FT_Load_Char( FT_Face face,
1230 FT_Int32 load_flags )
1232 FT_UInt glyph_index;
1236 return FT_THROW( Invalid_Face_Handle );
1238 glyph_index = (FT_UInt)char_code;
1239 if ( face->charmap )
1240 glyph_index = FT_Get_Char_Index( face, char_code );
1242 return FT_Load_Glyph( face, glyph_index, load_flags );
1246 /* destructor for sizes list */
1248 destroy_size( FT_Memory memory,
1252 FT_Size size = (FT_Size)size_;
1253 FT_Driver driver = (FT_Driver)driver_;
1256 /* finalize client-specific data */
1257 if ( size->generic.finalizer )
1258 size->generic.finalizer( size );
1260 /* finalize format-specific stuff */
1261 if ( driver->clazz->done_size )
1262 driver->clazz->done_size( size );
1264 FT_FREE( size->internal );
1270 ft_cmap_done_internal( FT_CMap cmap );
1274 destroy_charmaps( FT_Face face,
1283 for ( n = 0; n < face->num_charmaps; n++ )
1285 FT_CMap cmap = FT_CMAP( face->charmaps[n] );
1288 ft_cmap_done_internal( cmap );
1290 face->charmaps[n] = NULL;
1293 FT_FREE( face->charmaps );
1294 face->num_charmaps = 0;
1298 /* destructor for faces list */
1300 destroy_face( FT_Memory memory,
1304 FT_Face face = (FT_Face)face_;
1305 FT_Driver driver = (FT_Driver)driver_;
1306 FT_Driver_Class clazz = driver->clazz;
1309 /* discard auto-hinting data */
1310 if ( face->autohint.finalizer )
1311 face->autohint.finalizer( face->autohint.data );
1313 /* Discard glyph slots for this face. */
1314 /* Beware! FT_Done_GlyphSlot() changes the field `face->glyph' */
1315 while ( face->glyph )
1316 FT_Done_GlyphSlot( face->glyph );
1318 /* discard all sizes for this face */
1319 FT_List_Finalize( &face->sizes_list,
1325 /* now discard client data */
1326 if ( face->generic.finalizer )
1327 face->generic.finalizer( face );
1329 /* discard charmaps */
1330 destroy_charmaps( face, memory );
1332 /* finalize format-specific stuff */
1333 if ( clazz->done_face )
1334 clazz->done_face( face );
1336 /* close the stream for this face if needed */
1339 ( face->face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
1341 face->stream = NULL;
1344 if ( face->internal )
1346 FT_FREE( face->internal );
1353 Destroy_Driver( FT_Driver driver )
1355 FT_List_Finalize( &driver->faces_list,
1357 driver->root.memory,
1362 /**************************************************************************
1365 * find_unicode_charmap
1368 * This function finds a Unicode charmap, if there is one.
1369 * And if there is more than one, it tries to favour the more
1370 * extensive one, i.e., one that supports UCS-4 against those which
1371 * are limited to the BMP (said UCS-2 encoding.)
1373 * This function is called from open_face() (just below), and also
1374 * from FT_Select_Charmap( ..., FT_ENCODING_UNICODE ).
1377 find_unicode_charmap( FT_Face face )
1383 /* caller should have already checked that `face' is valid */
1386 first = face->charmaps;
1389 return FT_THROW( Invalid_CharMap_Handle );
1392 * The original TrueType specification(s) only specified charmap
1393 * formats that are capable of mapping 8 or 16 bit character codes to
1396 * However, recent updates to the Apple and OpenType specifications
1397 * introduced new formats that are capable of mapping 32-bit character
1398 * codes as well. And these are already used on some fonts, mainly to
1399 * map non-BMP Asian ideographs as defined in Unicode.
1401 * For compatibility purposes, these fonts generally come with
1402 * *several* Unicode charmaps:
1404 * - One of them in the "old" 16-bit format, that cannot access
1405 * all glyphs in the font.
1407 * - Another one in the "new" 32-bit format, that can access all
1410 * This function has been written to always favor a 32-bit charmap
1411 * when found. Otherwise, a 16-bit one is returned when found.
1414 /* Since the `interesting' table, with IDs (3,10), is normally the */
1415 /* last one, we loop backwards. This loses with type1 fonts with */
1416 /* non-BMP characters (<.0001%), this wins with .ttf with non-BMP */
1417 /* chars (.01% ?), and this is the same about 99.99% of the time! */
1419 cur = first + face->num_charmaps; /* points after the last one */
1421 for ( ; --cur >= first; )
1423 if ( cur[0]->encoding == FT_ENCODING_UNICODE )
1425 /* XXX If some new encodings to represent UCS-4 are added, */
1426 /* they should be added here. */
1427 if ( ( cur[0]->platform_id == TT_PLATFORM_MICROSOFT &&
1428 cur[0]->encoding_id == TT_MS_ID_UCS_4 ) ||
1429 ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
1430 cur[0]->encoding_id == TT_APPLE_ID_UNICODE_32 ) )
1432 face->charmap = cur[0];
1438 /* We do not have any UCS-4 charmap. */
1439 /* Do the loop again and search for UCS-2 charmaps. */
1440 cur = first + face->num_charmaps;
1442 for ( ; --cur >= first; )
1444 if ( cur[0]->encoding == FT_ENCODING_UNICODE )
1446 face->charmap = cur[0];
1451 return FT_THROW( Invalid_CharMap_Handle );
1455 /**************************************************************************
1458 * find_variant_selector_charmap
1461 * This function finds the variant selector charmap, if there is one.
1462 * There can only be one (platform=0, specific=5, format=14).
1465 find_variant_selector_charmap( FT_Face face )
1472 /* caller should have already checked that `face' is valid */
1475 first = face->charmaps;
1480 end = first + face->num_charmaps; /* points after the last one */
1482 for ( cur = first; cur < end; cur++ )
1484 if ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
1485 cur[0]->encoding_id == TT_APPLE_ID_VARIANT_SELECTOR &&
1486 FT_Get_CMap_Format( cur[0] ) == 14 )
1494 /**************************************************************************
1500 * This function does some work for FT_Open_Face().
1503 open_face( FT_Driver driver,
1505 FT_Bool *anexternal_stream,
1508 FT_Parameter* params,
1512 FT_Driver_Class clazz;
1513 FT_Face face = NULL;
1514 FT_Face_Internal internal = NULL;
1516 FT_Error error, error2;
1519 clazz = driver->clazz;
1520 memory = driver->root.memory;
1522 /* allocate the face object and perform basic initialization */
1523 if ( FT_ALLOC( face, clazz->face_object_size ) )
1526 face->driver = driver;
1527 face->memory = memory;
1528 face->stream = *astream;
1530 /* set the FT_FACE_FLAG_EXTERNAL_STREAM bit for FT_Done_Face */
1531 if ( *anexternal_stream )
1532 face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
1534 if ( FT_NEW( internal ) )
1537 face->internal = internal;
1539 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1544 face->internal->incremental_interface = NULL;
1545 for ( i = 0; i < num_params && !face->internal->incremental_interface;
1547 if ( params[i].tag == FT_PARAM_TAG_INCREMENTAL )
1548 face->internal->incremental_interface =
1549 (FT_Incremental_Interface)params[i].data;
1553 face->internal->random_seed = -1;
1555 if ( clazz->init_face )
1556 error = clazz->init_face( *astream,
1561 /* Stream may have been changed. */
1562 *astream = face->stream;
1563 *anexternal_stream =
1564 ( face->face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0;
1568 /* select Unicode charmap by default */
1569 error2 = find_unicode_charmap( face );
1571 /* if no Unicode charmap can be found, FT_Err_Invalid_CharMap_Handle */
1574 /* no error should happen, but we want to play safe */
1575 if ( error2 && FT_ERR_NEQ( error2, Invalid_CharMap_Handle ) )
1586 destroy_charmaps( face, memory );
1587 if ( clazz->done_face )
1588 clazz->done_face( face );
1589 FT_FREE( internal );
1598 /* there's a Mac-specific extended implementation of FT_New_Face() */
1599 /* in src/base/ftmac.c */
1601 #ifndef FT_MACINTOSH
1603 /* documentation is in freetype.h */
1605 FT_EXPORT_DEF( FT_Error )
1606 FT_New_Face( FT_Library library,
1607 const char* pathname,
1614 /* test for valid `library' and `aface' delayed to `FT_Open_Face' */
1616 return FT_THROW( Invalid_Argument );
1618 args.flags = FT_OPEN_PATHNAME;
1619 args.pathname = (char*)pathname;
1622 return ft_open_face_internal( library, &args, face_index, aface, 1 );
1628 /* documentation is in freetype.h */
1630 FT_EXPORT_DEF( FT_Error )
1631 FT_New_Memory_Face( FT_Library library,
1632 const FT_Byte* file_base,
1640 /* test for valid `library' and `face' delayed to `FT_Open_Face' */
1642 return FT_THROW( Invalid_Argument );
1644 args.flags = FT_OPEN_MEMORY;
1645 args.memory_base = file_base;
1646 args.memory_size = file_size;
1649 return ft_open_face_internal( library, &args, face_index, aface, 1 );
1653 #ifdef FT_CONFIG_OPTION_MAC_FONTS
1655 /* The behavior here is very similar to that in base/ftmac.c, but it */
1656 /* is designed to work on non-mac systems, so no mac specific calls. */
1658 /* We look at the file and determine if it is a mac dfont file or a mac */
1659 /* resource file, or a macbinary file containing a mac resource file. */
1661 /* Unlike ftmac I'm not going to look at a `FOND'. I don't really see */
1662 /* the point, especially since there may be multiple `FOND' resources. */
1663 /* Instead I'll just look for `sfnt' and `POST' resources, ordered as */
1664 /* they occur in the file. */
1666 /* Note that multiple `POST' resources do not mean multiple postscript */
1667 /* fonts; they all get jammed together to make what is essentially a */
1670 /* We aren't interested in `NFNT' or `FONT' bitmap resources. */
1672 /* As soon as we get an `sfnt' load it into memory and pass it off to */
1675 /* If we have a (set of) `POST' resources, massage them into a (memory) */
1676 /* pfb file and pass that to FT_Open_Face. (As with ftmac.c I'm not */
1677 /* going to try to save the kerning info. After all that lives in the */
1678 /* `FOND' which isn't in the file containing the `POST' resources so */
1679 /* we don't really have access to it. */
1682 /* Finalizer for a memory stream; gets called by FT_Done_Face(). */
1683 /* It frees the memory it uses. */
1684 /* From `ftmac.c'. */
1686 memory_stream_close( FT_Stream stream )
1688 FT_Memory memory = (FT_Memory)stream->descriptor.pointer;
1691 FT_FREE( stream->base );
1693 stream->close = NULL;
1698 /* Create a new memory stream from a buffer and a size. */
1699 /* From `ftmac.c'. */
1701 new_memory_stream( FT_Library library,
1704 FT_Stream_CloseFunc close,
1705 FT_Stream *astream )
1709 FT_Stream stream = NULL;
1713 return FT_THROW( Invalid_Library_Handle );
1716 return FT_THROW( Invalid_Argument );
1719 memory = library->memory;
1720 if ( FT_NEW( stream ) )
1723 FT_Stream_OpenMemory( stream, base, size );
1725 stream->descriptor.pointer = memory;
1726 stream->close = close;
1735 /* Create a new FT_Face given a buffer and a driver name. */
1736 /* From `ftmac.c'. */
1737 FT_LOCAL_DEF( FT_Error )
1738 open_face_from_buffer( FT_Library library,
1742 const char* driver_name,
1747 FT_Memory memory = library->memory;
1755 args.driver = FT_Get_Module( library, driver_name );
1759 return FT_THROW( Missing_Module );
1762 args.flags = args.flags | FT_OPEN_DRIVER;
1765 /* `memory_stream_close` also frees the stream object. */
1766 error = new_memory_stream( library,
1769 memory_stream_close,
1777 args.flags |= FT_OPEN_STREAM;
1780 /* At this point, the face index has served its purpose; */
1781 /* whoever calls this function has already used it to */
1782 /* locate the correct font data. We should not propagate */
1783 /* this index to FT_Open_Face() (unless it is negative). */
1785 if ( face_index > 0 )
1786 face_index &= 0x7FFF0000L; /* retain GX data */
1789 return ft_open_face_internal( library, &args, face_index, aface, 0 );
1793 /* Look up `TYP1' or `CID ' table from sfnt table directory. */
1794 /* `offset' and `length' must exclude the binary header in tables. */
1796 /* Type 1 and CID-keyed font drivers should recognize sfnt-wrapped */
1797 /* format too. Here, since we can't expect that the TrueType font */
1798 /* driver is loaded unconditionally, we must parse the font by */
1799 /* ourselves. We are only interested in the name of the table and */
1803 ft_lookup_PS_in_sfnt_stream( FT_Stream stream,
1807 FT_Bool* is_sfnt_cid )
1810 FT_UShort numTables;
1811 FT_Long pstable_index;
1818 *is_sfnt_cid = FALSE;
1820 /* TODO: support for sfnt-wrapped PS/CID in TTC format */
1822 /* version check for 'typ1' (should be ignored?) */
1823 if ( FT_READ_ULONG( tag ) )
1825 if ( tag != TTAG_typ1 )
1826 return FT_THROW( Unknown_File_Format );
1828 if ( FT_READ_USHORT( numTables ) )
1830 if ( FT_STREAM_SKIP( 2 * 3 ) ) /* skip binary search header */
1834 *is_sfnt_cid = FALSE;
1836 for ( i = 0; i < numTables; i++ )
1838 if ( FT_READ_ULONG( tag ) || FT_STREAM_SKIP( 4 ) ||
1839 FT_READ_ULONG( *offset ) || FT_READ_ULONG( *length ) )
1842 if ( tag == TTAG_CID )
1847 *is_sfnt_cid = TRUE;
1848 if ( face_index < 0 )
1851 else if ( tag == TTAG_TYP1 )
1856 *is_sfnt_cid = FALSE;
1857 if ( face_index < 0 )
1860 if ( face_index >= 0 && pstable_index == face_index )
1864 return FT_THROW( Table_Missing );
1868 FT_LOCAL_DEF( FT_Error )
1869 open_face_PS_from_sfnt_stream( FT_Library library,
1873 FT_Parameter *params,
1877 FT_Memory memory = library->memory;
1878 FT_ULong offset, length;
1880 FT_Bool is_sfnt_cid;
1881 FT_Byte* sfnt_ps = NULL;
1883 FT_UNUSED( num_params );
1884 FT_UNUSED( params );
1887 /* ignore GX stuff */
1888 if ( face_index > 0 )
1889 face_index &= 0xFFFFL;
1891 pos = FT_STREAM_POS();
1893 error = ft_lookup_PS_in_sfnt_stream( stream,
1901 if ( offset > stream->size )
1903 FT_TRACE2(( "open_face_PS_from_sfnt_stream: invalid table offset\n" ));
1904 error = FT_THROW( Invalid_Table );
1907 else if ( length > stream->size - offset )
1909 FT_TRACE2(( "open_face_PS_from_sfnt_stream: invalid table length\n" ));
1910 error = FT_THROW( Invalid_Table );
1914 error = FT_Stream_Seek( stream, pos + offset );
1918 if ( FT_QALLOC( sfnt_ps, (FT_Long)length ) )
1921 error = FT_Stream_Read( stream, (FT_Byte *)sfnt_ps, length );
1928 error = open_face_from_buffer( library,
1931 FT_MIN( face_index, 0 ),
1932 is_sfnt_cid ? "t1cid" : "type1",
1939 if ( FT_ERR_EQ( error, Unknown_File_Format ) )
1941 error1 = FT_Stream_Seek( stream, pos );
1951 #ifndef FT_MACINTOSH
1953 /* The resource header says we've got resource_cnt `POST' (type1) */
1954 /* resources in this file. They all need to be coalesced into */
1955 /* one lump which gets passed on to the type1 driver. */
1956 /* Here can be only one PostScript font in a file so face_index */
1957 /* must be 0 (or -1). */
1960 Mac_Read_POST_Resource( FT_Library library,
1963 FT_Long resource_cnt,
1967 FT_Error error = FT_ERR( Cannot_Open_Resource );
1968 FT_Memory memory = library->memory;
1970 FT_Byte* pfb_data = NULL;
1973 FT_ULong pfb_len, pfb_pos, pfb_lenpos;
1974 FT_ULong rlen, temp;
1977 if ( face_index == -1 )
1979 if ( face_index != 0 )
1982 /* Find the length of all the POST resources, concatenated. Assume */
1983 /* worst case (each resource in its own section). */
1985 for ( i = 0; i < resource_cnt; i++ )
1987 error = FT_Stream_Seek( stream, (FT_ULong)offsets[i] );
1990 if ( FT_READ_ULONG( temp ) ) /* actually LONG */
1993 /* FT2 allocator takes signed long buffer length,
1994 * too large value causing overflow should be checked
1996 FT_TRACE4(( " POST fragment #%d: length=0x%08lx"
1997 " total pfb_len=0x%08lx\n",
1998 i, temp, pfb_len + temp + 6 ));
2000 if ( FT_MAC_RFORK_MAX_LEN < temp ||
2001 FT_MAC_RFORK_MAX_LEN - temp < pfb_len + 6 )
2003 FT_TRACE2(( " MacOS resource length cannot exceed"
2005 FT_MAC_RFORK_MAX_LEN ));
2007 error = FT_THROW( Invalid_Offset );
2011 pfb_len += temp + 6;
2014 FT_TRACE2(( " total buffer size to concatenate"
2015 " %ld POST fragments: 0x%08lx\n",
2016 resource_cnt, pfb_len + 2 ));
2018 if ( pfb_len + 2 < 6 )
2020 FT_TRACE2(( " too long fragment length makes"
2021 " pfb_len confused: pfb_len=0x%08lx\n",
2024 error = FT_THROW( Array_Too_Large );
2028 if ( FT_QALLOC( pfb_data, (FT_Long)pfb_len + 2 ) )
2032 pfb_data[1] = 1; /* Ascii section */
2033 pfb_data[2] = 0; /* 4-byte length, fill in later */
2043 for ( i = 0; i < resource_cnt; i++ )
2045 error = FT_Stream_Seek( stream, (FT_ULong)offsets[i] );
2048 if ( FT_READ_ULONG( rlen ) )
2051 /* FT2 allocator takes signed long buffer length,
2052 * too large fragment length causing overflow should be checked
2054 if ( 0x7FFFFFFFUL < rlen )
2056 error = FT_THROW( Invalid_Offset );
2060 if ( FT_READ_USHORT( flags ) )
2063 FT_TRACE3(( "POST fragment[%d]:"
2064 " offsets=0x%08lx, rlen=0x%08lx, flags=0x%04x\n",
2065 i, offsets[i], rlen, flags ));
2067 error = FT_ERR( Array_Too_Large );
2069 /* postpone the check of `rlen longer than buffer' */
2070 /* until `FT_Stream_Read' */
2072 if ( ( flags >> 8 ) == 0 ) /* Comment, should not be loaded */
2074 FT_TRACE3(( " Skip POST fragment #%d because it is a comment\n",
2079 /* the flags are part of the resource, so rlen >= 2, */
2080 /* but some fonts declare rlen = 0 for empty fragment */
2086 if ( ( flags >> 8 ) == type )
2090 FT_TRACE3(( " Write POST fragment #%d header (4-byte) to buffer"
2092 i, (void*)pfb_data, pfb_lenpos ));
2094 if ( pfb_lenpos + 3 > pfb_len + 2 )
2097 pfb_data[pfb_lenpos ] = (FT_Byte)( len );
2098 pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
2099 pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
2100 pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
2102 if ( ( flags >> 8 ) == 5 ) /* End of font mark */
2105 FT_TRACE3(( " Write POST fragment #%d header (6-byte) to buffer"
2107 i, (void*)pfb_data, pfb_pos ));
2109 if ( pfb_pos + 6 > pfb_len + 2 )
2112 pfb_data[pfb_pos++] = 0x80;
2117 pfb_data[pfb_pos++] = (FT_Byte)type;
2118 pfb_lenpos = pfb_pos;
2119 pfb_data[pfb_pos++] = 0; /* 4-byte length, fill in later */
2120 pfb_data[pfb_pos++] = 0;
2121 pfb_data[pfb_pos++] = 0;
2122 pfb_data[pfb_pos++] = 0;
2125 if ( pfb_pos > pfb_len || pfb_pos + rlen > pfb_len )
2128 FT_TRACE3(( " Load POST fragment #%d (%ld byte) to buffer"
2130 i, rlen, (void*)pfb_data, pfb_pos ));
2132 error = FT_Stream_Read( stream, (FT_Byte *)pfb_data + pfb_pos, rlen );
2139 error = FT_ERR( Array_Too_Large );
2141 if ( pfb_pos + 2 > pfb_len + 2 )
2143 pfb_data[pfb_pos++] = 0x80;
2144 pfb_data[pfb_pos++] = 3;
2146 if ( pfb_lenpos + 3 > pfb_len + 2 )
2148 pfb_data[pfb_lenpos ] = (FT_Byte)( len );
2149 pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
2150 pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
2151 pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
2153 return open_face_from_buffer( library,
2161 if ( FT_ERR_EQ( error, Array_Too_Large ) )
2162 FT_TRACE2(( " Abort due to too-short buffer to store"
2163 " all POST fragments\n" ));
2164 else if ( FT_ERR_EQ( error, Invalid_Offset ) )
2165 FT_TRACE2(( " Abort due to invalid offset in a POST fragment\n" ));
2168 error = FT_ERR( Cannot_Open_Resource );
2169 FT_FREE( pfb_data );
2176 /* The resource header says we've got resource_cnt `sfnt' */
2177 /* (TrueType/OpenType) resources in this file. Look through */
2178 /* them for the one indicated by face_index, load it into mem, */
2179 /* pass it on to the truetype driver, and return it. */
2182 Mac_Read_sfnt_Resource( FT_Library library,
2185 FT_Long resource_cnt,
2189 FT_Memory memory = library->memory;
2190 FT_Byte* sfnt_data = NULL;
2192 FT_ULong flag_offset;
2195 FT_Long face_index_in_resource = 0;
2198 if ( face_index < 0 )
2199 face_index = -face_index - 1;
2200 if ( face_index >= resource_cnt )
2201 return FT_THROW( Cannot_Open_Resource );
2203 flag_offset = (FT_ULong)offsets[face_index];
2204 error = FT_Stream_Seek( stream, flag_offset );
2208 if ( FT_READ_ULONG( rlen ) )
2211 return FT_THROW( Cannot_Open_Resource );
2212 if ( rlen > FT_MAC_RFORK_MAX_LEN )
2213 return FT_THROW( Invalid_Offset );
2215 error = open_face_PS_from_sfnt_stream( library,
2223 /* rewind sfnt stream before open_face_PS_from_sfnt_stream() */
2224 error = FT_Stream_Seek( stream, flag_offset + 4 );
2228 if ( FT_QALLOC( sfnt_data, rlen ) )
2230 error = FT_Stream_Read( stream, (FT_Byte *)sfnt_data, rlen );
2233 FT_FREE( sfnt_data );
2237 is_cff = rlen > 4 && !ft_memcmp( sfnt_data, "OTTO", 4 );
2238 error = open_face_from_buffer( library,
2241 face_index_in_resource,
2242 is_cff ? "cff" : "truetype",
2250 /* Check for a valid resource fork header, or a valid dfont */
2251 /* header. In a resource fork the first 16 bytes are repeated */
2252 /* at the location specified by bytes 4-7. In a dfont bytes */
2253 /* 4-7 point to 16 bytes of zeroes instead. */
2256 IsMacResource( FT_Library library,
2258 FT_Long resource_offset,
2262 FT_Memory memory = library->memory;
2264 FT_Long map_offset, rdata_pos;
2265 FT_Long *data_offsets;
2269 error = FT_Raccess_Get_HeaderInfo( library, stream, resource_offset,
2270 &map_offset, &rdata_pos );
2274 /* POST resources must be sorted to concatenate properly */
2275 error = FT_Raccess_Get_DataOffsets( library, stream,
2276 map_offset, rdata_pos,
2278 &data_offsets, &count );
2281 error = Mac_Read_POST_Resource( library, stream, data_offsets, count,
2282 face_index, aface );
2283 FT_FREE( data_offsets );
2284 /* POST exists in an LWFN providing a single face */
2286 (*aface)->num_faces = 1;
2290 /* sfnt resources should not be sorted to preserve the face order by
2292 error = FT_Raccess_Get_DataOffsets( library, stream,
2293 map_offset, rdata_pos,
2295 &data_offsets, &count );
2298 FT_Long face_index_internal = face_index % count;
2301 error = Mac_Read_sfnt_Resource( library, stream, data_offsets, count,
2302 face_index_internal, aface );
2303 FT_FREE( data_offsets );
2305 (*aface)->num_faces = count;
2312 /* Check for a valid macbinary header, and if we find one */
2313 /* check that the (flattened) resource fork in it is valid. */
2316 IsMacBinary( FT_Library library,
2321 unsigned char header[128];
2323 FT_Long dlen, offset;
2327 return FT_THROW( Invalid_Stream_Operation );
2329 error = FT_Stream_Seek( stream, 0 );
2333 error = FT_Stream_Read( stream, (FT_Byte*)header, 128 );
2337 if ( header[ 0] != 0 ||
2343 header[2 + header[1]] != 0 ||
2344 header[0x53] > 0x7F )
2345 return FT_THROW( Unknown_File_Format );
2347 dlen = ( header[0x53] << 24 ) |
2348 ( header[0x54] << 16 ) |
2349 ( header[0x55] << 8 ) |
2352 rlen = ( header[0x57] << 24 ) |
2353 ( header[0x58] << 16 ) |
2354 ( header[0x59] << 8 ) |
2357 offset = 128 + ( ( dlen + 127 ) & ~127 );
2359 return IsMacResource( library, stream, offset, face_index, aface );
2367 load_face_in_embedded_rfork( FT_Library library,
2371 const FT_Open_Args *args )
2375 #define FT_COMPONENT raccess
2377 FT_Memory memory = library->memory;
2378 FT_Error error = FT_ERR( Unknown_File_Format );
2381 char* file_names[FT_RACCESS_N_RULES];
2382 FT_Long offsets[FT_RACCESS_N_RULES];
2383 FT_Error errors[FT_RACCESS_N_RULES];
2384 FT_Bool is_darwin_vfs, vfs_rfork_has_no_font = FALSE; /* not tested */
2387 FT_Stream stream2 = NULL;
2390 FT_Raccess_Guess( library, stream,
2391 args->pathname, file_names, offsets, errors );
2393 for ( i = 0; i < FT_RACCESS_N_RULES; i++ )
2395 is_darwin_vfs = ft_raccess_rule_by_darwin_vfs( library, i );
2396 if ( is_darwin_vfs && vfs_rfork_has_no_font )
2398 FT_TRACE3(( "Skip rule %d: darwin vfs resource fork"
2399 " is already checked and"
2400 " no font is found\n",
2407 FT_TRACE3(( "Error 0x%x has occurred in rule %d\n",
2412 args2.flags = FT_OPEN_PATHNAME;
2413 args2.pathname = file_names[i] ? file_names[i] : args->pathname;
2415 FT_TRACE3(( "Try rule %d: %s (offset=%ld) ...",
2416 i, args2.pathname, offsets[i] ));
2418 error = FT_Stream_New( library, &args2, &stream2 );
2419 if ( is_darwin_vfs && FT_ERR_EQ( error, Cannot_Open_Stream ) )
2420 vfs_rfork_has_no_font = TRUE;
2424 FT_TRACE3(( "failed\n" ));
2428 error = IsMacResource( library, stream2, offsets[i],
2429 face_index, aface );
2430 FT_Stream_Free( stream2, 0 );
2432 FT_TRACE3(( "%s\n", error ? "failed": "successful" ));
2436 else if ( is_darwin_vfs )
2437 vfs_rfork_has_no_font = TRUE;
2440 for (i = 0; i < FT_RACCESS_N_RULES; i++)
2442 if ( file_names[i] )
2443 FT_FREE( file_names[i] );
2446 /* Caller (load_mac_face) requires FT_Err_Unknown_File_Format. */
2448 error = FT_ERR( Unknown_File_Format );
2453 #define FT_COMPONENT objs
2458 /* Check for some macintosh formats without Carbon framework. */
2459 /* Is this a macbinary file? If so look at the resource fork. */
2460 /* Is this a mac dfont file? */
2461 /* Is this an old style resource fork? (in data) */
2462 /* Else call load_face_in_embedded_rfork to try extra rules */
2463 /* (defined in `ftrfork.c'). */
2466 load_mac_face( FT_Library library,
2470 const FT_Open_Args *args )
2476 error = IsMacBinary( library, stream, face_index, aface );
2477 if ( FT_ERR_EQ( error, Unknown_File_Format ) )
2481 #define FT_COMPONENT raccess
2483 #ifdef FT_DEBUG_LEVEL_TRACE
2484 FT_TRACE3(( "Try as dfont: " ));
2485 if ( !( args->flags & FT_OPEN_MEMORY ) )
2486 FT_TRACE3(( "%s ...", args->pathname ));
2489 error = IsMacResource( library, stream, 0, face_index, aface );
2491 FT_TRACE3(( "%s\n", error ? "failed" : "successful" ));
2494 #define FT_COMPONENT objs
2498 if ( ( FT_ERR_EQ( error, Unknown_File_Format ) ||
2499 FT_ERR_EQ( error, Invalid_Stream_Operation ) ) &&
2500 ( args->flags & FT_OPEN_PATHNAME ) )
2501 error = load_face_in_embedded_rfork( library, stream,
2502 face_index, aface, args );
2507 #endif /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
2510 /* documentation is in freetype.h */
2512 FT_EXPORT_DEF( FT_Error )
2513 FT_Open_Face( FT_Library library,
2514 const FT_Open_Args* args,
2518 return ft_open_face_internal( library, args, face_index, aface, 1 );
2523 ft_open_face_internal( FT_Library library,
2524 const FT_Open_Args* args,
2527 FT_Bool test_mac_fonts )
2530 FT_Driver driver = NULL;
2531 FT_Memory memory = NULL;
2532 FT_Stream stream = NULL;
2533 FT_Face face = NULL;
2534 FT_ListNode node = NULL;
2535 FT_Bool external_stream;
2539 #ifndef FT_CONFIG_OPTION_MAC_FONTS
2540 FT_UNUSED( test_mac_fonts );
2544 /* only use lower 31 bits together with sign bit */
2545 if ( face_index > 0 )
2546 face_index &= 0x7FFFFFFFL;
2549 face_index = -face_index;
2550 face_index &= 0x7FFFFFFFL;
2551 face_index = -face_index;
2554 #ifdef FT_DEBUG_LEVEL_TRACE
2555 FT_TRACE3(( "FT_Open_Face: " ));
2556 if ( face_index < 0 )
2557 FT_TRACE3(( "Requesting number of faces and named instances\n"));
2560 FT_TRACE3(( "Requesting face %ld", face_index & 0xFFFFL ));
2561 if ( face_index & 0x7FFF0000L )
2562 FT_TRACE3(( ", named instance %ld", face_index >> 16 ));
2563 FT_TRACE3(( "\n" ));
2567 /* test for valid `library' delayed to `FT_Stream_New' */
2570 return FT_THROW( Invalid_Argument );
2572 external_stream = FT_BOOL( ( args->flags & FT_OPEN_STREAM ) &&
2575 /* create input stream */
2576 error = FT_Stream_New( library, args, &stream );
2580 /* Do this error check after `FT_Stream_New` to ensure that the */
2581 /* 'close' callback is called. */
2582 if ( !aface && face_index >= 0 )
2584 error = FT_THROW( Invalid_Argument );
2588 memory = library->memory;
2590 /* If the font driver is specified in the `args' structure, use */
2591 /* it. Otherwise, we scan the list of registered drivers. */
2592 if ( ( args->flags & FT_OPEN_DRIVER ) && args->driver )
2594 driver = FT_DRIVER( args->driver );
2596 /* not all modules are drivers, so check... */
2597 if ( FT_MODULE_IS_DRIVER( driver ) )
2599 FT_Int num_params = 0;
2600 FT_Parameter* params = NULL;
2603 if ( args->flags & FT_OPEN_PARAMS )
2605 num_params = args->num_params;
2606 params = args->params;
2609 error = open_face( driver, &stream, &external_stream, face_index,
2610 num_params, params, &face );
2615 error = FT_THROW( Invalid_Handle );
2617 FT_Stream_Free( stream, external_stream );
2622 error = FT_ERR( Missing_Module );
2624 /* check each font driver for an appropriate format */
2625 cur = library->modules;
2626 limit = cur + library->num_modules;
2628 for ( ; cur < limit; cur++ )
2630 /* not all modules are font drivers, so check... */
2631 if ( FT_MODULE_IS_DRIVER( cur[0] ) )
2633 FT_Int num_params = 0;
2634 FT_Parameter* params = NULL;
2637 driver = FT_DRIVER( cur[0] );
2639 if ( args->flags & FT_OPEN_PARAMS )
2641 num_params = args->num_params;
2642 params = args->params;
2645 error = open_face( driver, &stream, &external_stream, face_index,
2646 num_params, params, &face );
2650 #ifdef FT_CONFIG_OPTION_MAC_FONTS
2651 if ( test_mac_fonts &&
2652 ft_strcmp( cur[0]->clazz->module_name, "truetype" ) == 0 &&
2653 FT_ERR_EQ( error, Table_Missing ) )
2655 /* TrueType but essential tables are missing */
2656 error = FT_Stream_Seek( stream, 0 );
2660 error = open_face_PS_from_sfnt_stream( library,
2668 FT_Stream_Free( stream, external_stream );
2674 if ( FT_ERR_NEQ( error, Unknown_File_Format ) )
2680 /* If we are on the mac, and we get an */
2681 /* FT_Err_Invalid_Stream_Operation it may be because we have an */
2682 /* empty data fork, so we need to check the resource fork. */
2683 if ( FT_ERR_NEQ( error, Cannot_Open_Stream ) &&
2684 FT_ERR_NEQ( error, Unknown_File_Format ) &&
2685 FT_ERR_NEQ( error, Invalid_Stream_Operation ) )
2688 #if !defined( FT_MACINTOSH ) && defined( FT_CONFIG_OPTION_MAC_FONTS )
2689 if ( test_mac_fonts )
2691 error = load_mac_face( library, stream, face_index, aface, args );
2694 /* We don't want to go to Success here. We've already done */
2695 /* that. On the other hand, if we succeeded we still need to */
2696 /* close this stream (we opened a different stream which */
2697 /* extracted the interesting information out of this stream */
2698 /* here. That stream will still be open and the face will */
2700 FT_Stream_Free( stream, external_stream );
2705 if ( FT_ERR_NEQ( error, Unknown_File_Format ) )
2707 #endif /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
2709 /* no driver is able to handle this format */
2710 error = FT_THROW( Unknown_File_Format );
2713 FT_Stream_Free( stream, external_stream );
2718 FT_TRACE4(( "FT_Open_Face: New face object, adding to list\n" ));
2720 /* add the face object to its driver's list */
2721 if ( FT_QNEW( node ) )
2725 /* don't assume driver is the same as face->driver, so use */
2726 /* face->driver instead. */
2727 FT_List_Add( &face->driver->faces_list, node );
2729 /* now allocate a glyph slot object for the face */
2730 FT_TRACE4(( "FT_Open_Face: Creating glyph slot\n" ));
2732 if ( face_index >= 0 )
2734 error = FT_New_GlyphSlot( face, NULL );
2738 /* finally, allocate a size object for the face */
2743 FT_TRACE4(( "FT_Open_Face: Creating size object\n" ));
2745 error = FT_New_Size( face, &size );
2755 if ( FT_IS_SCALABLE( face ) )
2757 if ( face->height < 0 )
2758 face->height = (FT_Short)-face->height;
2760 if ( !FT_HAS_VERTICAL( face ) )
2761 face->max_advance_height = (FT_Short)face->height;
2764 if ( FT_HAS_FIXED_SIZES( face ) )
2769 for ( i = 0; i < face->num_fixed_sizes; i++ )
2771 FT_Bitmap_Size* bsize = face->available_sizes + i;
2774 if ( bsize->height < 0 )
2775 bsize->height = -bsize->height;
2776 if ( bsize->x_ppem < 0 )
2777 bsize->x_ppem = -bsize->x_ppem;
2778 if ( bsize->y_ppem < 0 )
2779 bsize->y_ppem = -bsize->y_ppem;
2781 /* check whether negation actually has worked */
2782 if ( bsize->height < 0 || bsize->x_ppem < 0 || bsize->y_ppem < 0 )
2784 FT_TRACE0(( "FT_Open_Face:"
2785 " Invalid bitmap dimensions for strike %d,"
2786 " now disabled\n", i ));
2796 /* initialize internal face data */
2798 FT_Face_Internal internal = face->internal;
2801 internal->transform_matrix.xx = 0x10000L;
2802 internal->transform_matrix.xy = 0;
2803 internal->transform_matrix.yx = 0;
2804 internal->transform_matrix.yy = 0x10000L;
2806 internal->transform_delta.x = 0;
2807 internal->transform_delta.y = 0;
2809 internal->refcount = 1;
2811 internal->no_stem_darkening = -1;
2813 #ifdef FT_CONFIG_OPTION_SUBPIXEL_RENDERING
2814 /* Per-face filtering can only be set up by FT_Face_Properties */
2815 internal->lcd_filter_func = NULL;
2822 FT_Done_Face( face );
2828 FT_Done_Face( face ); /* face must be in the driver's list */
2830 destroy_face( memory, face, driver );
2833 #ifdef FT_DEBUG_LEVEL_TRACE
2834 if ( !error && face_index < 0 )
2836 FT_TRACE3(( "FT_Open_Face: The font has %ld face%s\n",
2838 face->num_faces == 1 ? "" : "s" ));
2839 FT_TRACE3(( " and %ld named instance%s for face %ld\n",
2840 face->style_flags >> 16,
2841 ( face->style_flags >> 16 ) == 1 ? "" : "s",
2846 FT_TRACE4(( "FT_Open_Face: Return 0x%x\n", error ));
2852 /* documentation is in freetype.h */
2854 FT_EXPORT_DEF( FT_Error )
2855 FT_Attach_File( FT_Face face,
2856 const char* filepathname )
2861 /* test for valid `face' delayed to `FT_Attach_Stream' */
2863 if ( !filepathname )
2864 return FT_THROW( Invalid_Argument );
2867 open.flags = FT_OPEN_PATHNAME;
2868 open.pathname = (char*)filepathname;
2870 return FT_Attach_Stream( face, &open );
2874 /* documentation is in freetype.h */
2876 FT_EXPORT_DEF( FT_Error )
2877 FT_Attach_Stream( FT_Face face,
2878 const FT_Open_Args* parameters )
2884 FT_Driver_Class clazz;
2887 /* test for valid `parameters' delayed to `FT_Stream_New' */
2890 return FT_THROW( Invalid_Face_Handle );
2892 driver = face->driver;
2894 return FT_THROW( Invalid_Driver_Handle );
2896 error = FT_Stream_New( driver->root.library, parameters, &stream );
2900 /* we implement FT_Attach_Stream in each driver through the */
2901 /* `attach_file' interface */
2903 error = FT_ERR( Unimplemented_Feature );
2904 clazz = driver->clazz;
2905 if ( clazz->attach_file )
2906 error = clazz->attach_file( face, stream );
2908 /* close the attached stream */
2909 FT_Stream_Free( stream,
2910 FT_BOOL( parameters->stream &&
2911 ( parameters->flags & FT_OPEN_STREAM ) ) );
2918 /* documentation is in freetype.h */
2920 FT_EXPORT_DEF( FT_Error )
2921 FT_Reference_Face( FT_Face face )
2924 return FT_THROW( Invalid_Face_Handle );
2926 face->internal->refcount++;
2932 /* documentation is in freetype.h */
2934 FT_EXPORT_DEF( FT_Error )
2935 FT_Done_Face( FT_Face face )
2943 error = FT_ERR( Invalid_Face_Handle );
2944 if ( face && face->driver )
2946 face->internal->refcount--;
2947 if ( face->internal->refcount > 0 )
2951 driver = face->driver;
2952 memory = driver->root.memory;
2954 /* find face in driver's list */
2955 node = FT_List_Find( &driver->faces_list, face );
2958 /* remove face object from the driver's list */
2959 FT_List_Remove( &driver->faces_list, node );
2962 /* now destroy the object proper */
2963 destroy_face( memory, face, driver );
2973 /* documentation is in ftobjs.h */
2975 FT_EXPORT_DEF( FT_Error )
2976 FT_New_Size( FT_Face face,
2982 FT_Driver_Class clazz;
2984 FT_Size size = NULL;
2985 FT_ListNode node = NULL;
2987 FT_Size_Internal internal = NULL;
2991 return FT_THROW( Invalid_Face_Handle );
2994 return FT_THROW( Invalid_Argument );
2996 if ( !face->driver )
2997 return FT_THROW( Invalid_Driver_Handle );
3001 driver = face->driver;
3002 clazz = driver->clazz;
3003 memory = face->memory;
3005 /* Allocate new size object and perform basic initialisation */
3006 if ( FT_ALLOC( size, clazz->size_object_size ) || FT_QNEW( node ) )
3011 if ( FT_NEW( internal ) )
3014 size->internal = internal;
3016 if ( clazz->init_size )
3017 error = clazz->init_size( size );
3019 /* in case of success, add to the face's list */
3024 FT_List_Add( &face->sizes_list, node );
3032 FT_FREE( size->internal );
3040 /* documentation is in ftobjs.h */
3042 FT_EXPORT_DEF( FT_Error )
3043 FT_Done_Size( FT_Size size )
3053 return FT_THROW( Invalid_Size_Handle );
3057 return FT_THROW( Invalid_Face_Handle );
3059 driver = face->driver;
3061 return FT_THROW( Invalid_Driver_Handle );
3063 memory = driver->root.memory;
3066 node = FT_List_Find( &face->sizes_list, size );
3069 FT_List_Remove( &face->sizes_list, node );
3072 if ( face->size == size )
3075 if ( face->sizes_list.head )
3076 face->size = (FT_Size)(face->sizes_list.head->data);
3079 destroy_size( memory, size, driver );
3082 error = FT_THROW( Invalid_Size_Handle );
3088 /* documentation is in ftobjs.h */
3090 FT_BASE_DEF( FT_Error )
3091 FT_Match_Size( FT_Face face,
3092 FT_Size_Request req,
3093 FT_Bool ignore_width,
3094 FT_ULong* size_index )
3100 if ( !FT_HAS_FIXED_SIZES( face ) )
3101 return FT_THROW( Invalid_Face_Handle );
3103 /* FT_Bitmap_Size doesn't provide enough info... */
3104 if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
3105 return FT_THROW( Unimplemented_Feature );
3107 w = FT_REQUEST_WIDTH ( req );
3108 h = FT_REQUEST_HEIGHT( req );
3110 if ( req->width && !req->height )
3112 else if ( !req->width && req->height )
3115 w = FT_PIX_ROUND( w );
3116 h = FT_PIX_ROUND( h );
3119 return FT_THROW( Invalid_Pixel_Size );
3121 for ( i = 0; i < face->num_fixed_sizes; i++ )
3123 FT_Bitmap_Size* bsize = face->available_sizes + i;
3126 if ( h != FT_PIX_ROUND( bsize->y_ppem ) )
3129 if ( w == FT_PIX_ROUND( bsize->x_ppem ) || ignore_width )
3131 FT_TRACE3(( "FT_Match_Size: bitmap strike %d matches\n", i ));
3134 *size_index = (FT_ULong)i;
3140 FT_TRACE3(( "FT_Match_Size: no matching bitmap strike\n" ));
3142 return FT_THROW( Invalid_Pixel_Size );
3146 /* documentation is in ftobjs.h */
3149 ft_synthesize_vertical_metrics( FT_Glyph_Metrics* metrics,
3152 FT_Pos height = metrics->height;
3155 /* compensate for glyph with bbox above/below the baseline */
3156 if ( metrics->horiBearingY < 0 )
3158 if ( height < metrics->horiBearingY )
3159 height = metrics->horiBearingY;
3161 else if ( metrics->horiBearingY > 0 )
3162 height -= metrics->horiBearingY;
3164 /* the factor 1.2 is a heuristical value */
3166 advance = height * 12 / 10;
3168 metrics->vertBearingX = metrics->horiBearingX - metrics->horiAdvance / 2;
3169 metrics->vertBearingY = ( advance - height ) / 2;
3170 metrics->vertAdvance = advance;
3175 ft_recompute_scaled_metrics( FT_Face face,
3176 FT_Size_Metrics* metrics )
3178 /* Compute root ascender, descender, test height, and max_advance */
3180 #ifdef GRID_FIT_METRICS
3181 metrics->ascender = FT_PIX_CEIL( FT_MulFix( face->ascender,
3182 metrics->y_scale ) );
3184 metrics->descender = FT_PIX_FLOOR( FT_MulFix( face->descender,
3185 metrics->y_scale ) );
3187 metrics->height = FT_PIX_ROUND( FT_MulFix( face->height,
3188 metrics->y_scale ) );
3190 metrics->max_advance = FT_PIX_ROUND( FT_MulFix( face->max_advance_width,
3191 metrics->x_scale ) );
3192 #else /* !GRID_FIT_METRICS */
3193 metrics->ascender = FT_MulFix( face->ascender,
3196 metrics->descender = FT_MulFix( face->descender,
3199 metrics->height = FT_MulFix( face->height,
3202 metrics->max_advance = FT_MulFix( face->max_advance_width,
3204 #endif /* !GRID_FIT_METRICS */
3209 FT_Select_Metrics( FT_Face face,
3210 FT_ULong strike_index )
3212 FT_Size_Metrics* metrics;
3213 FT_Bitmap_Size* bsize;
3216 metrics = &face->size->metrics;
3217 bsize = face->available_sizes + strike_index;
3219 metrics->x_ppem = (FT_UShort)( ( bsize->x_ppem + 32 ) >> 6 );
3220 metrics->y_ppem = (FT_UShort)( ( bsize->y_ppem + 32 ) >> 6 );
3222 if ( FT_IS_SCALABLE( face ) )
3224 metrics->x_scale = FT_DivFix( bsize->x_ppem,
3225 face->units_per_EM );
3226 metrics->y_scale = FT_DivFix( bsize->y_ppem,
3227 face->units_per_EM );
3229 ft_recompute_scaled_metrics( face, metrics );
3233 metrics->x_scale = 1L << 16;
3234 metrics->y_scale = 1L << 16;
3235 metrics->ascender = bsize->y_ppem;
3236 metrics->descender = 0;
3237 metrics->height = bsize->height << 6;
3238 metrics->max_advance = bsize->x_ppem;
3243 FT_BASE_DEF( FT_Error )
3244 FT_Request_Metrics( FT_Face face,
3245 FT_Size_Request req )
3247 FT_Error error = FT_Err_Ok;
3249 FT_Size_Metrics* metrics;
3252 metrics = &face->size->metrics;
3254 if ( FT_IS_SCALABLE( face ) )
3256 FT_Long w = 0, h = 0, scaled_w = 0, scaled_h = 0;
3259 switch ( req->type )
3261 case FT_SIZE_REQUEST_TYPE_NOMINAL:
3262 w = h = face->units_per_EM;
3265 case FT_SIZE_REQUEST_TYPE_REAL_DIM:
3266 w = h = face->ascender - face->descender;
3269 case FT_SIZE_REQUEST_TYPE_BBOX:
3270 w = face->bbox.xMax - face->bbox.xMin;
3271 h = face->bbox.yMax - face->bbox.yMin;
3274 case FT_SIZE_REQUEST_TYPE_CELL:
3275 w = face->max_advance_width;
3276 h = face->ascender - face->descender;
3279 case FT_SIZE_REQUEST_TYPE_SCALES:
3280 metrics->x_scale = (FT_Fixed)req->width;
3281 metrics->y_scale = (FT_Fixed)req->height;
3282 if ( !metrics->x_scale )
3283 metrics->x_scale = metrics->y_scale;
3284 else if ( !metrics->y_scale )
3285 metrics->y_scale = metrics->x_scale;
3286 goto Calculate_Ppem;
3288 case FT_SIZE_REQUEST_TYPE_MAX:
3292 /* to be on the safe side */
3299 scaled_w = FT_REQUEST_WIDTH ( req );
3300 scaled_h = FT_REQUEST_HEIGHT( req );
3302 /* determine scales */
3303 if ( req->height || !req->width )
3307 FT_ERROR(( "FT_Request_Metrics: Divide by zero\n" ));
3308 error = FT_ERR( Divide_By_Zero );
3312 metrics->y_scale = FT_DivFix( scaled_h, h );
3319 FT_ERROR(( "FT_Request_Metrics: Divide by zero\n" ));
3320 error = FT_ERR( Divide_By_Zero );
3324 metrics->x_scale = FT_DivFix( scaled_w, w );
3328 metrics->x_scale = metrics->y_scale;
3329 scaled_w = FT_MulDiv( scaled_h, w, h );
3334 metrics->y_scale = metrics->x_scale;
3335 scaled_h = FT_MulDiv( scaled_w, h, w );
3338 if ( req->type == FT_SIZE_REQUEST_TYPE_CELL )
3340 if ( metrics->y_scale > metrics->x_scale )
3341 metrics->y_scale = metrics->x_scale;
3343 metrics->x_scale = metrics->y_scale;
3347 /* calculate the ppems */
3348 if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
3350 scaled_w = FT_MulFix( face->units_per_EM, metrics->x_scale );
3351 scaled_h = FT_MulFix( face->units_per_EM, metrics->y_scale );
3354 scaled_w = ( scaled_w + 32 ) >> 6;
3355 scaled_h = ( scaled_h + 32 ) >> 6;
3356 if ( scaled_w > (FT_Long)FT_USHORT_MAX ||
3357 scaled_h > (FT_Long)FT_USHORT_MAX )
3359 FT_ERROR(( "FT_Request_Metrics: Resulting ppem size too large\n" ));
3360 error = FT_ERR( Invalid_Pixel_Size );
3364 metrics->x_ppem = (FT_UShort)scaled_w;
3365 metrics->y_ppem = (FT_UShort)scaled_h;
3367 ft_recompute_scaled_metrics( face, metrics );
3372 metrics->x_scale = 1L << 16;
3373 metrics->y_scale = 1L << 16;
3381 /* documentation is in freetype.h */
3383 FT_EXPORT_DEF( FT_Error )
3384 FT_Select_Size( FT_Face face,
3385 FT_Int strike_index )
3387 FT_Error error = FT_Err_Ok;
3388 FT_Driver_Class clazz;
3391 if ( !face || !FT_HAS_FIXED_SIZES( face ) )
3392 return FT_THROW( Invalid_Face_Handle );
3394 if ( strike_index < 0 || strike_index >= face->num_fixed_sizes )
3395 return FT_THROW( Invalid_Argument );
3397 clazz = face->driver->clazz;
3399 if ( clazz->select_size )
3401 error = clazz->select_size( face->size, (FT_ULong)strike_index );
3403 FT_TRACE5(( "FT_Select_Size (%s driver):\n",
3404 face->driver->root.clazz->module_name ));
3408 FT_Select_Metrics( face, (FT_ULong)strike_index );
3410 FT_TRACE5(( "FT_Select_Size:\n" ));
3413 #ifdef FT_DEBUG_LEVEL_TRACE
3415 FT_Size_Metrics* metrics = &face->size->metrics;
3418 FT_TRACE5(( " x scale: %ld (%f)\n",
3419 metrics->x_scale, (double)metrics->x_scale / 65536 ));
3420 FT_TRACE5(( " y scale: %ld (%f)\n",
3421 metrics->y_scale, (double)metrics->y_scale / 65536 ));
3422 FT_TRACE5(( " ascender: %f\n",
3423 (double)metrics->ascender / 64 ));
3424 FT_TRACE5(( " descender: %f\n",
3425 (double)metrics->descender / 64 ));
3426 FT_TRACE5(( " height: %f\n",
3427 (double)metrics->height / 64 ));
3428 FT_TRACE5(( " max advance: %f\n",
3429 (double)metrics->max_advance / 64 ));
3430 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
3431 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
3439 /* documentation is in freetype.h */
3441 FT_EXPORT_DEF( FT_Error )
3442 FT_Request_Size( FT_Face face,
3443 FT_Size_Request req )
3446 FT_Driver_Class clazz;
3447 FT_ULong strike_index;
3451 return FT_THROW( Invalid_Face_Handle );
3454 return FT_THROW( Invalid_Size_Handle );
3456 if ( !req || req->width < 0 || req->height < 0 ||
3457 req->type >= FT_SIZE_REQUEST_TYPE_MAX )
3458 return FT_THROW( Invalid_Argument );
3460 /* signal the auto-hinter to recompute its size metrics */
3461 /* (if requested) */
3462 face->size->internal->autohint_metrics.x_scale = 0;
3464 clazz = face->driver->clazz;
3466 if ( clazz->request_size )
3468 error = clazz->request_size( face->size, req );
3470 FT_TRACE5(( "FT_Request_Size (%s driver):\n",
3471 face->driver->root.clazz->module_name ));
3473 else if ( !FT_IS_SCALABLE( face ) && FT_HAS_FIXED_SIZES( face ) )
3476 * The reason that a driver doesn't have `request_size' defined is
3477 * either that the scaling here suffices or that the supported formats
3478 * are bitmap-only and size matching is not implemented.
3480 * In the latter case, a simple size matching is done.
3482 error = FT_Match_Size( face, req, 0, &strike_index );
3486 return FT_Select_Size( face, (FT_Int)strike_index );
3490 error = FT_Request_Metrics( face, req );
3494 FT_TRACE5(( "FT_Request_Size:\n" ));
3497 #ifdef FT_DEBUG_LEVEL_TRACE
3499 FT_Size_Metrics* metrics = &face->size->metrics;
3502 FT_TRACE5(( " x scale: %ld (%f)\n",
3503 metrics->x_scale, (double)metrics->x_scale / 65536 ));
3504 FT_TRACE5(( " y scale: %ld (%f)\n",
3505 metrics->y_scale, (double)metrics->y_scale / 65536 ));
3506 FT_TRACE5(( " ascender: %f\n",
3507 (double)metrics->ascender / 64 ));
3508 FT_TRACE5(( " descender: %f\n",
3509 (double)metrics->descender / 64 ));
3510 FT_TRACE5(( " height: %f\n",
3511 (double)metrics->height / 64 ));
3512 FT_TRACE5(( " max advance: %f\n",
3513 (double)metrics->max_advance / 64 ));
3514 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem ));
3515 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem ));
3524 /* documentation is in freetype.h */
3526 FT_EXPORT_DEF( FT_Error )
3527 FT_Set_Char_Size( FT_Face face,
3528 FT_F26Dot6 char_width,
3529 FT_F26Dot6 char_height,
3530 FT_UInt horz_resolution,
3531 FT_UInt vert_resolution )
3533 FT_Size_RequestRec req;
3536 /* check of `face' delayed to `FT_Request_Size' */
3539 char_width = char_height;
3540 else if ( !char_height )
3541 char_height = char_width;
3543 if ( !horz_resolution )
3544 horz_resolution = vert_resolution;
3545 else if ( !vert_resolution )
3546 vert_resolution = horz_resolution;
3548 if ( char_width < 1 * 64 )
3549 char_width = 1 * 64;
3550 if ( char_height < 1 * 64 )
3551 char_height = 1 * 64;
3553 if ( !horz_resolution )
3554 horz_resolution = vert_resolution = 72;
3556 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
3557 req.width = char_width;
3558 req.height = char_height;
3559 req.horiResolution = horz_resolution;
3560 req.vertResolution = vert_resolution;
3562 return FT_Request_Size( face, &req );
3566 /* documentation is in freetype.h */
3568 FT_EXPORT_DEF( FT_Error )
3569 FT_Set_Pixel_Sizes( FT_Face face,
3570 FT_UInt pixel_width,
3571 FT_UInt pixel_height )
3573 FT_Size_RequestRec req;
3576 /* check of `face' delayed to `FT_Request_Size' */
3578 if ( pixel_width == 0 )
3579 pixel_width = pixel_height;
3580 else if ( pixel_height == 0 )
3581 pixel_height = pixel_width;
3583 if ( pixel_width < 1 )
3585 if ( pixel_height < 1 )
3588 /* use `>=' to avoid potential compiler warning on 16bit platforms */
3589 if ( pixel_width >= 0xFFFFU )
3590 pixel_width = 0xFFFFU;
3591 if ( pixel_height >= 0xFFFFU )
3592 pixel_height = 0xFFFFU;
3594 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
3595 req.width = (FT_Long)( pixel_width << 6 );
3596 req.height = (FT_Long)( pixel_height << 6 );
3597 req.horiResolution = 0;
3598 req.vertResolution = 0;
3600 return FT_Request_Size( face, &req );
3604 /* documentation is in freetype.h */
3606 FT_EXPORT_DEF( FT_Error )
3607 FT_Get_Kerning( FT_Face face,
3609 FT_UInt right_glyph,
3611 FT_Vector *akerning )
3613 FT_Error error = FT_Err_Ok;
3618 return FT_THROW( Invalid_Face_Handle );
3621 return FT_THROW( Invalid_Argument );
3623 driver = face->driver;
3628 if ( driver->clazz->get_kerning )
3630 error = driver->clazz->get_kerning( face,
3636 if ( kern_mode != FT_KERNING_UNSCALED )
3638 akerning->x = FT_MulFix( akerning->x, face->size->metrics.x_scale );
3639 akerning->y = FT_MulFix( akerning->y, face->size->metrics.y_scale );
3641 if ( kern_mode != FT_KERNING_UNFITTED )
3643 FT_Pos orig_x = akerning->x;
3644 FT_Pos orig_y = akerning->y;
3647 /* we scale down kerning values for small ppem values */
3648 /* to avoid that rounding makes them too big. */
3649 /* `25' has been determined heuristically. */
3650 if ( face->size->metrics.x_ppem < 25 )
3651 akerning->x = FT_MulDiv( orig_x,
3652 face->size->metrics.x_ppem, 25 );
3653 if ( face->size->metrics.y_ppem < 25 )
3654 akerning->y = FT_MulDiv( orig_y,
3655 face->size->metrics.y_ppem, 25 );
3657 akerning->x = FT_PIX_ROUND( akerning->x );
3658 akerning->y = FT_PIX_ROUND( akerning->y );
3660 #ifdef FT_DEBUG_LEVEL_TRACE
3662 FT_Pos orig_x_rounded = FT_PIX_ROUND( orig_x );
3663 FT_Pos orig_y_rounded = FT_PIX_ROUND( orig_y );
3666 if ( akerning->x != orig_x_rounded ||
3667 akerning->y != orig_y_rounded )
3668 FT_TRACE5(( "FT_Get_Kerning: horizontal kerning"
3669 " (%ld, %ld) scaled down to (%ld, %ld) pixels\n",
3670 orig_x_rounded / 64, orig_y_rounded / 64,
3671 akerning->x / 64, akerning->y / 64 ));
3683 /* documentation is in freetype.h */
3685 FT_EXPORT_DEF( FT_Error )
3686 FT_Get_Track_Kerning( FT_Face face,
3687 FT_Fixed point_size,
3689 FT_Fixed* akerning )
3691 FT_Service_Kerning service;
3692 FT_Error error = FT_Err_Ok;
3696 return FT_THROW( Invalid_Face_Handle );
3699 return FT_THROW( Invalid_Argument );
3701 FT_FACE_FIND_SERVICE( face, service, KERNING );
3703 return FT_THROW( Unimplemented_Feature );
3705 error = service->get_track( face,
3714 /* documentation is in freetype.h */
3716 FT_EXPORT_DEF( FT_Error )
3717 FT_Select_Charmap( FT_Face face,
3718 FT_Encoding encoding )
3725 return FT_THROW( Invalid_Face_Handle );
3727 /* FT_ENCODING_NONE is a valid encoding for BDF, PCF, and Windows FNT */
3728 if ( encoding == FT_ENCODING_NONE && !face->num_charmaps )
3729 return FT_THROW( Invalid_Argument );
3731 /* FT_ENCODING_UNICODE is special. We try to find the `best' Unicode */
3732 /* charmap available, i.e., one with UCS-4 characters, if possible. */
3734 /* This is done by find_unicode_charmap() above, to share code. */
3735 if ( encoding == FT_ENCODING_UNICODE )
3736 return find_unicode_charmap( face );
3738 cur = face->charmaps;
3740 return FT_THROW( Invalid_CharMap_Handle );
3742 limit = cur + face->num_charmaps;
3744 for ( ; cur < limit; cur++ )
3746 if ( cur[0]->encoding == encoding )
3748 face->charmap = cur[0];
3753 return FT_THROW( Invalid_Argument );
3757 /* documentation is in freetype.h */
3759 FT_EXPORT_DEF( FT_Error )
3760 FT_Set_Charmap( FT_Face face,
3761 FT_CharMap charmap )
3768 return FT_THROW( Invalid_Face_Handle );
3770 cur = face->charmaps;
3771 if ( !cur || !charmap )
3772 return FT_THROW( Invalid_CharMap_Handle );
3774 limit = cur + face->num_charmaps;
3776 for ( ; cur < limit; cur++ )
3778 if ( cur[0] == charmap &&
3779 FT_Get_CMap_Format ( charmap ) != 14 )
3781 face->charmap = cur[0];
3786 return FT_THROW( Invalid_Argument );
3790 /* documentation is in freetype.h */
3792 FT_EXPORT_DEF( FT_Int )
3793 FT_Get_Charmap_Index( FT_CharMap charmap )
3798 if ( !charmap || !charmap->face )
3801 for ( i = 0; i < charmap->face->num_charmaps; i++ )
3802 if ( charmap->face->charmaps[i] == charmap )
3805 FT_ASSERT( i < charmap->face->num_charmaps );
3812 ft_cmap_done_internal( FT_CMap cmap )
3814 FT_CMap_Class clazz = cmap->clazz;
3815 FT_Face face = cmap->charmap.face;
3816 FT_Memory memory = FT_FACE_MEMORY( face );
3820 clazz->done( cmap );
3827 FT_CMap_Done( FT_CMap cmap )
3831 FT_Face face = cmap->charmap.face;
3832 FT_Memory memory = FT_FACE_MEMORY( face );
3837 for ( i = 0; i < face->num_charmaps; i++ )
3839 if ( (FT_CMap)face->charmaps[i] == cmap )
3841 FT_CharMap last_charmap = face->charmaps[face->num_charmaps - 1];
3844 if ( FT_QRENEW_ARRAY( face->charmaps,
3846 face->num_charmaps - 1 ) )
3849 /* remove it from our list of charmaps */
3850 for ( j = i + 1; j < face->num_charmaps; j++ )
3852 if ( j == face->num_charmaps - 1 )
3853 face->charmaps[j - 1] = last_charmap;
3855 face->charmaps[j - 1] = face->charmaps[j];
3858 face->num_charmaps--;
3860 if ( (FT_CMap)face->charmap == cmap )
3861 face->charmap = NULL;
3863 ft_cmap_done_internal( cmap );
3872 FT_BASE_DEF( FT_Error )
3873 FT_CMap_New( FT_CMap_Class clazz,
3874 FT_Pointer init_data,
3881 FT_CMap cmap = NULL;
3884 if ( !clazz || !charmap || !charmap->face )
3885 return FT_THROW( Invalid_Argument );
3887 face = charmap->face;
3888 memory = FT_FACE_MEMORY( face );
3890 if ( !FT_ALLOC( cmap, clazz->size ) )
3892 cmap->charmap = *charmap;
3893 cmap->clazz = clazz;
3897 error = clazz->init( cmap, init_data );
3902 /* add it to our list of charmaps */
3903 if ( FT_QRENEW_ARRAY( face->charmaps,
3905 face->num_charmaps + 1 ) )
3908 face->charmaps[face->num_charmaps++] = (FT_CharMap)cmap;
3918 ft_cmap_done_internal( cmap );
3924 /* documentation is in freetype.h */
3926 FT_EXPORT_DEF( FT_UInt )
3927 FT_Get_Char_Index( FT_Face face,
3933 if ( face && face->charmap )
3935 FT_CMap cmap = FT_CMAP( face->charmap );
3938 if ( charcode > 0xFFFFFFFFUL )
3940 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
3941 FT_TRACE1(( " 0x%lx is truncated\n", charcode ));
3944 result = cmap->clazz->char_index( cmap, (FT_UInt32)charcode );
3945 if ( result >= (FT_UInt)face->num_glyphs )
3953 /* documentation is in freetype.h */
3955 FT_EXPORT_DEF( FT_ULong )
3956 FT_Get_First_Char( FT_Face face,
3959 FT_ULong result = 0;
3963 /* only do something if we have a charmap, and we have glyphs at all */
3964 if ( face && face->charmap && face->num_glyphs )
3966 gindex = FT_Get_Char_Index( face, 0 );
3968 result = FT_Get_Next_Char( face, 0, &gindex );
3978 /* documentation is in freetype.h */
3980 FT_EXPORT_DEF( FT_ULong )
3981 FT_Get_Next_Char( FT_Face face,
3985 FT_ULong result = 0;
3989 if ( face && face->charmap && face->num_glyphs )
3991 FT_UInt32 code = (FT_UInt32)charcode;
3992 FT_CMap cmap = FT_CMAP( face->charmap );
3997 gindex = cmap->clazz->char_next( cmap, &code );
3999 } while ( gindex >= (FT_UInt)face->num_glyphs );
4001 result = ( gindex == 0 ) ? 0 : code;
4011 /* documentation is in freetype.h */
4013 FT_EXPORT_DEF( FT_Error )
4014 FT_Face_Properties( FT_Face face,
4015 FT_UInt num_properties,
4016 FT_Parameter* properties )
4018 FT_Error error = FT_Err_Ok;
4021 if ( num_properties > 0 && !properties )
4023 error = FT_THROW( Invalid_Argument );
4027 for ( ; num_properties > 0; num_properties-- )
4029 if ( properties->tag == FT_PARAM_TAG_STEM_DARKENING )
4031 if ( properties->data )
4033 if ( *( (FT_Bool*)properties->data ) == TRUE )
4034 face->internal->no_stem_darkening = FALSE;
4036 face->internal->no_stem_darkening = TRUE;
4040 /* use module default */
4041 face->internal->no_stem_darkening = -1;
4044 else if ( properties->tag == FT_PARAM_TAG_LCD_FILTER_WEIGHTS )
4046 #ifdef FT_CONFIG_OPTION_SUBPIXEL_RENDERING
4047 if ( properties->data )
4049 ft_memcpy( face->internal->lcd_weights,
4051 FT_LCD_FILTER_FIVE_TAPS );
4052 face->internal->lcd_filter_func = ft_lcd_filter_fir;
4055 error = FT_THROW( Unimplemented_Feature );
4059 else if ( properties->tag == FT_PARAM_TAG_RANDOM_SEED )
4061 if ( properties->data )
4063 face->internal->random_seed = *( (FT_Int32*)properties->data );
4064 if ( face->internal->random_seed < 0 )
4065 face->internal->random_seed = 0;
4069 /* use module default */
4070 face->internal->random_seed = -1;
4075 error = FT_THROW( Invalid_Argument );
4090 /* documentation is in freetype.h */
4092 FT_EXPORT_DEF( FT_UInt )
4093 FT_Face_GetCharVariantIndex( FT_Face face,
4095 FT_ULong variantSelector )
4102 face->charmap->encoding == FT_ENCODING_UNICODE )
4104 FT_CharMap charmap = find_variant_selector_charmap( face );
4105 FT_CMap ucmap = FT_CMAP( face->charmap );
4110 FT_CMap vcmap = FT_CMAP( charmap );
4113 if ( charcode > 0xFFFFFFFFUL )
4115 FT_TRACE1(( "FT_Face_GetCharVariantIndex:"
4116 " too large charcode" ));
4117 FT_TRACE1(( " 0x%lx is truncated\n", charcode ));
4119 if ( variantSelector > 0xFFFFFFFFUL )
4121 FT_TRACE1(( "FT_Face_GetCharVariantIndex:"
4122 " too large variantSelector" ));
4123 FT_TRACE1(( " 0x%lx is truncated\n", variantSelector ));
4126 result = vcmap->clazz->char_var_index( vcmap, ucmap,
4127 (FT_UInt32)charcode,
4128 (FT_UInt32)variantSelector );
4136 /* documentation is in freetype.h */
4138 FT_EXPORT_DEF( FT_Int )
4139 FT_Face_GetCharVariantIsDefault( FT_Face face,
4141 FT_ULong variantSelector )
4148 FT_CharMap charmap = find_variant_selector_charmap( face );
4153 FT_CMap vcmap = FT_CMAP( charmap );
4156 if ( charcode > 0xFFFFFFFFUL )
4158 FT_TRACE1(( "FT_Face_GetCharVariantIsDefault:"
4159 " too large charcode" ));
4160 FT_TRACE1(( " 0x%lx is truncated\n", charcode ));
4162 if ( variantSelector > 0xFFFFFFFFUL )
4164 FT_TRACE1(( "FT_Face_GetCharVariantIsDefault:"
4165 " too large variantSelector" ));
4166 FT_TRACE1(( " 0x%lx is truncated\n", variantSelector ));
4169 result = vcmap->clazz->char_var_default( vcmap,
4170 (FT_UInt32)charcode,
4171 (FT_UInt32)variantSelector );
4179 /* documentation is in freetype.h */
4181 FT_EXPORT_DEF( FT_UInt32* )
4182 FT_Face_GetVariantSelectors( FT_Face face )
4184 FT_UInt32 *result = NULL;
4189 FT_CharMap charmap = find_variant_selector_charmap( face );
4194 FT_CMap vcmap = FT_CMAP( charmap );
4195 FT_Memory memory = FT_FACE_MEMORY( face );
4198 result = vcmap->clazz->variant_list( vcmap, memory );
4206 /* documentation is in freetype.h */
4208 FT_EXPORT_DEF( FT_UInt32* )
4209 FT_Face_GetVariantsOfChar( FT_Face face,
4212 FT_UInt32 *result = NULL;
4217 FT_CharMap charmap = find_variant_selector_charmap( face );
4222 FT_CMap vcmap = FT_CMAP( charmap );
4223 FT_Memory memory = FT_FACE_MEMORY( face );
4226 if ( charcode > 0xFFFFFFFFUL )
4228 FT_TRACE1(( "FT_Face_GetVariantsOfChar: too large charcode" ));
4229 FT_TRACE1(( " 0x%lx is truncated\n", charcode ));
4232 result = vcmap->clazz->charvariant_list( vcmap, memory,
4233 (FT_UInt32)charcode );
4240 /* documentation is in freetype.h */
4242 FT_EXPORT_DEF( FT_UInt32* )
4243 FT_Face_GetCharsOfVariant( FT_Face face,
4244 FT_ULong variantSelector )
4246 FT_UInt32 *result = NULL;
4251 FT_CharMap charmap = find_variant_selector_charmap( face );
4256 FT_CMap vcmap = FT_CMAP( charmap );
4257 FT_Memory memory = FT_FACE_MEMORY( face );
4260 if ( variantSelector > 0xFFFFFFFFUL )
4262 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
4263 FT_TRACE1(( " 0x%lx is truncated\n", variantSelector ));
4266 result = vcmap->clazz->variantchar_list( vcmap, memory,
4267 (FT_UInt32)variantSelector );
4275 /* documentation is in freetype.h */
4277 FT_EXPORT_DEF( FT_UInt )
4278 FT_Get_Name_Index( FT_Face face,
4279 const FT_String* glyph_name )
4285 FT_HAS_GLYPH_NAMES( face ) &&
4288 FT_Service_GlyphDict service;
4291 FT_FACE_LOOKUP_SERVICE( face,
4295 if ( service && service->name_index )
4296 result = service->name_index( face, glyph_name );
4303 /* documentation is in freetype.h */
4305 FT_EXPORT_DEF( FT_Error )
4306 FT_Get_Glyph_Name( FT_Face face,
4307 FT_UInt glyph_index,
4309 FT_UInt buffer_max )
4312 FT_Service_GlyphDict service;
4316 return FT_THROW( Invalid_Face_Handle );
4318 if ( !buffer || buffer_max == 0 )
4319 return FT_THROW( Invalid_Argument );
4321 /* clean up buffer */
4322 ((FT_Byte*)buffer)[0] = '\0';
4324 if ( (FT_Long)glyph_index >= face->num_glyphs )
4325 return FT_THROW( Invalid_Glyph_Index );
4327 if ( !FT_HAS_GLYPH_NAMES( face ) )
4328 return FT_THROW( Invalid_Argument );
4330 FT_FACE_LOOKUP_SERVICE( face, service, GLYPH_DICT );
4331 if ( service && service->get_name )
4332 error = service->get_name( face, glyph_index, buffer, buffer_max );
4334 error = FT_THROW( Invalid_Argument );
4340 /* documentation is in freetype.h */
4342 FT_EXPORT_DEF( const char* )
4343 FT_Get_Postscript_Name( FT_Face face )
4345 const char* result = NULL;
4353 FT_Service_PsFontName service;
4356 FT_FACE_LOOKUP_SERVICE( face,
4358 POSTSCRIPT_FONT_NAME );
4360 if ( service && service->get_ps_font_name )
4361 result = service->get_ps_font_name( face );
4369 /* documentation is in tttables.h */
4371 FT_EXPORT_DEF( void* )
4372 FT_Get_Sfnt_Table( FT_Face face,
4376 FT_Service_SFNT_Table service;
4379 if ( face && FT_IS_SFNT( face ) )
4381 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
4383 table = service->get_table( face, tag );
4390 /* documentation is in tttables.h */
4392 FT_EXPORT_DEF( FT_Error )
4393 FT_Load_Sfnt_Table( FT_Face face,
4399 FT_Service_SFNT_Table service;
4402 if ( !face || !FT_IS_SFNT( face ) )
4403 return FT_THROW( Invalid_Face_Handle );
4405 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
4407 return FT_THROW( Unimplemented_Feature );
4409 return service->load_table( face, tag, offset, buffer, length );
4413 /* documentation is in tttables.h */
4415 FT_EXPORT_DEF( FT_Error )
4416 FT_Sfnt_Table_Info( FT_Face face,
4417 FT_UInt table_index,
4421 FT_Service_SFNT_Table service;
4425 /* test for valid `length' delayed to `service->table_info' */
4427 if ( !face || !FT_IS_SFNT( face ) )
4428 return FT_THROW( Invalid_Face_Handle );
4430 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
4432 return FT_THROW( Unimplemented_Feature );
4434 return service->table_info( face, table_index, tag, &offset, length );
4438 /* documentation is in tttables.h */
4440 FT_EXPORT_DEF( FT_ULong )
4441 FT_Get_CMap_Language_ID( FT_CharMap charmap )
4443 FT_Service_TTCMaps service;
4445 TT_CMapInfo cmap_info;
4448 if ( !charmap || !charmap->face )
4451 face = charmap->face;
4452 FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
4455 if ( service->get_cmap_info( charmap, &cmap_info ))
4458 return cmap_info.language;
4462 /* documentation is in tttables.h */
4464 FT_EXPORT_DEF( FT_Long )
4465 FT_Get_CMap_Format( FT_CharMap charmap )
4467 FT_Service_TTCMaps service;
4469 TT_CMapInfo cmap_info;
4472 if ( !charmap || !charmap->face )
4475 face = charmap->face;
4476 FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
4479 if ( service->get_cmap_info( charmap, &cmap_info ))
4482 return cmap_info.format;
4486 /* documentation is in ftsizes.h */
4488 FT_EXPORT_DEF( FT_Error )
4489 FT_Activate_Size( FT_Size size )
4495 return FT_THROW( Invalid_Size_Handle );
4498 if ( !face || !face->driver )
4499 return FT_THROW( Invalid_Face_Handle );
4501 /* we don't need anything more complex than that; all size objects */
4502 /* are already listed by the face */
4509 /*************************************************************************/
4510 /*************************************************************************/
4511 /*************************************************************************/
4514 /**** R E N D E R E R S ****/
4517 /*************************************************************************/
4518 /*************************************************************************/
4519 /*************************************************************************/
4521 /* lookup a renderer by glyph format in the library's list */
4522 FT_BASE_DEF( FT_Renderer )
4523 FT_Lookup_Renderer( FT_Library library,
4524 FT_Glyph_Format format,
4528 FT_Renderer result = NULL;
4534 cur = library->renderers.head;
4539 cur = (*node)->next;
4545 FT_Renderer renderer = FT_RENDERER( cur->data );
4548 if ( renderer->glyph_format == format )
4565 ft_lookup_glyph_renderer( FT_GlyphSlot slot )
4567 FT_Face face = slot->face;
4568 FT_Library library = FT_FACE_LIBRARY( face );
4569 FT_Renderer result = library->cur_renderer;
4572 if ( !result || result->glyph_format != slot->format )
4573 result = FT_Lookup_Renderer( library, slot->format, 0 );
4580 ft_set_current_renderer( FT_Library library )
4582 FT_Renderer renderer;
4585 renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE, 0 );
4586 library->cur_renderer = renderer;
4591 ft_add_renderer( FT_Module module )
4593 FT_Library library = module->library;
4594 FT_Memory memory = library->memory;
4596 FT_ListNode node = NULL;
4599 if ( FT_QNEW( node ) )
4603 FT_Renderer render = FT_RENDERER( module );
4604 FT_Renderer_Class* clazz = (FT_Renderer_Class*)module->clazz;
4607 render->clazz = clazz;
4608 render->glyph_format = clazz->glyph_format;
4610 /* allocate raster object if needed */
4611 if ( clazz->raster_class && clazz->raster_class->raster_new )
4613 error = clazz->raster_class->raster_new( memory, &render->raster );
4617 render->raster_render = clazz->raster_class->raster_render;
4618 render->render = clazz->render_glyph;
4621 #ifdef FT_CONFIG_OPTION_SVG
4622 if ( clazz->glyph_format == FT_GLYPH_FORMAT_SVG )
4623 render->render = clazz->render_glyph;
4627 node->data = module;
4628 FT_List_Add( &library->renderers, node );
4630 ft_set_current_renderer( library );
4643 ft_remove_renderer( FT_Module module )
4650 library = module->library;
4654 memory = library->memory;
4656 node = FT_List_Find( &library->renderers, module );
4659 FT_Renderer render = FT_RENDERER( module );
4662 /* release raster object, if any */
4663 if ( render->raster )
4664 render->clazz->raster_class->raster_done( render->raster );
4666 /* remove from list */
4667 FT_List_Remove( &library->renderers, node );
4670 ft_set_current_renderer( library );
4675 /* documentation is in ftrender.h */
4677 FT_EXPORT_DEF( FT_Renderer )
4678 FT_Get_Renderer( FT_Library library,
4679 FT_Glyph_Format format )
4681 /* test for valid `library' delayed to `FT_Lookup_Renderer' */
4683 return FT_Lookup_Renderer( library, format, 0 );
4687 /* documentation is in ftrender.h */
4689 FT_EXPORT_DEF( FT_Error )
4690 FT_Set_Renderer( FT_Library library,
4691 FT_Renderer renderer,
4693 FT_Parameter* parameters )
4696 FT_Error error = FT_Err_Ok;
4698 FT_Renderer_SetModeFunc set_mode;
4703 error = FT_THROW( Invalid_Library_Handle );
4709 error = FT_THROW( Invalid_Argument );
4713 if ( num_params > 0 && !parameters )
4715 error = FT_THROW( Invalid_Argument );
4719 node = FT_List_Find( &library->renderers, renderer );
4722 error = FT_THROW( Invalid_Argument );
4726 FT_List_Up( &library->renderers, node );
4728 if ( renderer->glyph_format == FT_GLYPH_FORMAT_OUTLINE )
4729 library->cur_renderer = renderer;
4731 set_mode = renderer->clazz->set_mode;
4733 for ( ; num_params > 0; num_params-- )
4735 error = set_mode( renderer, parameters->tag, parameters->data );
4746 FT_BASE_DEF( FT_Error )
4747 FT_Render_Glyph_Internal( FT_Library library,
4749 FT_Render_Mode render_mode )
4751 FT_Error error = FT_Err_Ok;
4752 FT_Face face = slot->face;
4753 FT_Renderer renderer;
4756 switch ( slot->format )
4759 if ( slot->internal->load_flags & FT_LOAD_COLOR )
4761 FT_LayerIterator iterator;
4763 FT_UInt base_glyph = slot->glyph_index;
4765 FT_Bool have_layers;
4766 FT_UInt glyph_index;
4767 FT_UInt color_index;
4770 /* check whether we have colored glyph layers */
4772 have_layers = FT_Get_Color_Glyph_Layer( face,
4779 error = FT_New_GlyphSlot( face, NULL );
4782 TT_Face ttface = (TT_Face)face;
4783 SFNT_Service sfnt = (SFNT_Service)ttface->sfnt;
4788 FT_Int32 load_flags = slot->internal->load_flags;
4791 /* disable the `FT_LOAD_COLOR' flag to avoid recursion */
4792 /* right here in this function */
4793 load_flags &= ~FT_LOAD_COLOR;
4795 /* render into the new `face->glyph' glyph slot */
4796 load_flags |= FT_LOAD_RENDER;
4798 error = FT_Load_Glyph( face, glyph_index, load_flags );
4802 /* blend new `face->glyph' into old `slot'; */
4803 /* at the first call, `slot' is still empty */
4804 error = sfnt->colr_blend( ttface,
4811 } while ( FT_Get_Color_Glyph_Layer( face,
4818 slot->format = FT_GLYPH_FORMAT_BITMAP;
4820 /* this call also restores `slot' as the glyph slot */
4821 FT_Done_GlyphSlot( face->glyph );
4827 /* Failed to do the colored layer. Draw outline instead. */
4828 slot->format = FT_GLYPH_FORMAT_OUTLINE;
4833 FT_ListNode node = NULL;
4836 /* small shortcut for the very common case */
4837 if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
4839 renderer = library->cur_renderer;
4840 node = library->renderers.head;
4843 renderer = FT_Lookup_Renderer( library, slot->format, &node );
4845 error = FT_ERR( Cannot_Render_Glyph );
4848 error = renderer->render( renderer, slot, render_mode, NULL );
4850 FT_ERR_NEQ( error, Cannot_Render_Glyph ) )
4853 /* FT_Err_Cannot_Render_Glyph is returned if the render mode */
4854 /* is unsupported by the current renderer for this glyph image */
4857 /* now, look for another renderer that supports the same */
4859 renderer = FT_Lookup_Renderer( library, slot->format, &node );
4862 /* it is not an error if we cannot render a bitmap glyph */
4863 if ( FT_ERR_EQ( error, Cannot_Render_Glyph ) &&
4864 slot->format == FT_GLYPH_FORMAT_BITMAP )
4869 #ifdef FT_DEBUG_LEVEL_TRACE
4872 #define FT_COMPONENT checksum
4875 * Computing the MD5 checksum is expensive, unnecessarily distorting a
4876 * possible profiling of FreeType if compiled with tracing support. For
4877 * this reason, we execute the following code only if explicitly
4881 /* we use FT_TRACE3 in this block */
4883 ft_trace_levels[trace_checksum] >= 3 &&
4884 slot->bitmap.buffer )
4890 FT_Bitmap_Init( &bitmap );
4892 /* we convert to a single bitmap format for computing the checksum */
4893 /* this also converts the bitmap flow to `down' (i.e., pitch > 0) */
4894 err = FT_Bitmap_Convert( library, &slot->bitmap, &bitmap, 1 );
4898 unsigned char md5[16];
4899 unsigned long coverage = 0;
4901 int rows = (int)bitmap.rows;
4902 int pitch = bitmap.pitch;
4905 FT_TRACE3(( "FT_Render_Glyph: bitmap %dx%d, %s (mode %d)\n",
4908 pixel_modes[slot->bitmap.pixel_mode],
4909 slot->bitmap.pixel_mode ));
4911 for ( i = 0; i < rows; i++ )
4912 for ( j = 0; j < pitch; j++ )
4913 coverage += bitmap.buffer[i * pitch + j];
4915 FT_TRACE3(( " Total coverage: %lu\n", coverage ));
4918 if ( bitmap.buffer )
4919 MD5_Update( &ctx, bitmap.buffer,
4920 (unsigned long)rows * (unsigned long)pitch );
4921 MD5_Final( md5, &ctx );
4923 FT_TRACE3(( " MD5 checksum: " ));
4924 for ( i = 0; i < 16; i++ )
4925 FT_TRACE3(( "%02X", md5[i] ));
4926 FT_TRACE3(( "\n" ));
4929 FT_Bitmap_Done( library, &bitmap );
4933 * Dump bitmap in Netpbm format (PBM or PGM).
4936 /* we use FT_TRACE7 in this block */
4938 ft_trace_levels[trace_checksum] >= 7 &&
4939 slot->bitmap.buffer )
4941 if ( slot->bitmap.rows < 128U &&
4942 slot->bitmap.width < 128U )
4944 int rows = (int)slot->bitmap.rows;
4945 int width = (int)slot->bitmap.width;
4946 int pitch = slot->bitmap.pitch;
4949 unsigned char* topleft = slot->bitmap.buffer;
4953 topleft -= pitch * ( rows - 1 );
4955 FT_TRACE7(( "Netpbm image: start\n" ));
4956 switch ( slot->bitmap.pixel_mode )
4958 case FT_PIXEL_MODE_MONO:
4959 FT_TRACE7(( "P1 %d %d\n", width, rows ));
4960 for ( i = 0; i < rows; i++ )
4962 for ( j = 0; j < width; )
4963 for ( m = 128; m > 0 && j < width; m >>= 1, j++ )
4965 ( topleft[i * pitch + j / 8] & m ) != 0 ));
4966 FT_TRACE7(( "\n" ));
4971 FT_TRACE7(( "P2 %d %d 255\n", width, rows ));
4972 for ( i = 0; i < rows; i++ )
4974 for ( j = 0; j < width; j += 1 )
4975 FT_TRACE7(( " %3u", topleft[i * pitch + j] ));
4976 FT_TRACE7(( "\n" ));
4979 FT_TRACE7(( "Netpbm image: end\n" ));
4982 FT_TRACE7(( "Netpbm image: too large, omitted\n" ));
4986 #define FT_COMPONENT objs
4988 #endif /* FT_DEBUG_LEVEL_TRACE */
4994 /* documentation is in freetype.h */
4996 FT_EXPORT_DEF( FT_Error )
4997 FT_Render_Glyph( FT_GlyphSlot slot,
4998 FT_Render_Mode render_mode )
5003 if ( !slot || !slot->face )
5004 return FT_THROW( Invalid_Argument );
5006 library = FT_FACE_LIBRARY( slot->face );
5008 return FT_Render_Glyph_Internal( library, slot, render_mode );
5012 /*************************************************************************/
5013 /*************************************************************************/
5014 /*************************************************************************/
5017 /**** M O D U L E S ****/
5020 /*************************************************************************/
5021 /*************************************************************************/
5022 /*************************************************************************/
5025 /**************************************************************************
5031 * Destroys a given module object. For drivers, this also destroys
5036 * A handle to the target driver object.
5039 * The driver _must_ be LOCKED!
5042 Destroy_Module( FT_Module module )
5044 FT_Memory memory = module->memory;
5045 FT_Module_Class* clazz = module->clazz;
5046 FT_Library library = module->library;
5049 if ( library && library->auto_hinter == module )
5050 library->auto_hinter = NULL;
5052 /* if the module is a renderer */
5053 if ( FT_MODULE_IS_RENDERER( module ) )
5054 ft_remove_renderer( module );
5056 /* if the module is a font driver, add some steps */
5057 if ( FT_MODULE_IS_DRIVER( module ) )
5058 Destroy_Driver( FT_DRIVER( module ) );
5060 /* finalize the module object */
5061 if ( clazz->module_done )
5062 clazz->module_done( module );
5069 /* documentation is in ftmodapi.h */
5071 FT_EXPORT_DEF( FT_Error )
5072 FT_Add_Module( FT_Library library,
5073 const FT_Module_Class* clazz )
5077 FT_Module module = NULL;
5081 #define FREETYPE_VER_FIXED ( ( (FT_Long)FREETYPE_MAJOR << 16 ) | \
5085 return FT_THROW( Invalid_Library_Handle );
5088 return FT_THROW( Invalid_Argument );
5090 /* check FreeType version */
5091 if ( clazz->module_requires > FREETYPE_VER_FIXED )
5092 return FT_THROW( Invalid_Version );
5094 /* look for a module with the same name in the library's table */
5095 for ( nn = 0; nn < library->num_modules; nn++ )
5097 module = library->modules[nn];
5098 if ( ft_strcmp( module->clazz->module_name, clazz->module_name ) == 0 )
5100 /* this installed module has the same name, compare their versions */
5101 if ( clazz->module_version <= module->clazz->module_version )
5102 return FT_THROW( Lower_Module_Version );
5104 /* remove the module from our list, then exit the loop to replace */
5105 /* it by our new version.. */
5106 FT_Remove_Module( library, module );
5111 memory = library->memory;
5114 if ( library->num_modules >= FT_MAX_MODULES )
5116 error = FT_THROW( Too_Many_Drivers );
5120 /* allocate module object */
5121 if ( FT_ALLOC( module, clazz->module_size ) )
5124 /* base initialization */
5125 module->library = library;
5126 module->memory = memory;
5127 module->clazz = (FT_Module_Class*)clazz;
5129 /* check whether the module is a renderer - this must be performed */
5130 /* before the normal module initialization */
5131 if ( FT_MODULE_IS_RENDERER( module ) )
5133 /* add to the renderers list */
5134 error = ft_add_renderer( module );
5139 /* is the module a auto-hinter? */
5140 if ( FT_MODULE_IS_HINTER( module ) )
5141 library->auto_hinter = module;
5143 /* if the module is a font driver */
5144 if ( FT_MODULE_IS_DRIVER( module ) )
5146 FT_Driver driver = FT_DRIVER( module );
5149 driver->clazz = (FT_Driver_Class)module->clazz;
5152 if ( clazz->module_init )
5154 error = clazz->module_init( module );
5159 /* add module to the library's table */
5160 library->modules[library->num_modules++] = module;
5166 if ( FT_MODULE_IS_RENDERER( module ) )
5168 FT_Renderer renderer = FT_RENDERER( module );
5171 if ( renderer->clazz &&
5172 renderer->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
5174 renderer->clazz->raster_class->raster_done( renderer->raster );
5182 /* documentation is in ftmodapi.h */
5184 FT_EXPORT_DEF( FT_Module )
5185 FT_Get_Module( FT_Library library,
5186 const char* module_name )
5188 FT_Module result = NULL;
5193 if ( !library || !module_name )
5196 cur = library->modules;
5197 limit = cur + library->num_modules;
5199 for ( ; cur < limit; cur++ )
5200 if ( ft_strcmp( cur[0]->clazz->module_name, module_name ) == 0 )
5210 /* documentation is in ftobjs.h */
5212 FT_BASE_DEF( const void* )
5213 FT_Get_Module_Interface( FT_Library library,
5214 const char* mod_name )
5219 /* test for valid `library' delayed to FT_Get_Module() */
5221 module = FT_Get_Module( library, mod_name );
5223 return module ? module->clazz->module_interface : 0;
5227 FT_BASE_DEF( FT_Pointer )
5228 ft_module_get_service( FT_Module module,
5229 const char* service_id,
5232 FT_Pointer result = NULL;
5237 FT_ASSERT( module->clazz && module->clazz->get_interface );
5239 /* first, look for the service in the module */
5240 if ( module->clazz->get_interface )
5241 result = module->clazz->get_interface( module, service_id );
5243 if ( global && !result )
5245 /* we didn't find it, look in all other modules then */
5246 FT_Library library = module->library;
5247 FT_Module* cur = library->modules;
5248 FT_Module* limit = cur + library->num_modules;
5251 for ( ; cur < limit; cur++ )
5253 if ( cur[0] != module )
5255 FT_ASSERT( cur[0]->clazz );
5257 if ( cur[0]->clazz->get_interface )
5259 result = cur[0]->clazz->get_interface( cur[0], service_id );
5272 /* documentation is in ftmodapi.h */
5274 FT_EXPORT_DEF( FT_Error )
5275 FT_Remove_Module( FT_Library library,
5278 /* try to find the module from the table, then remove it from there */
5281 return FT_THROW( Invalid_Library_Handle );
5285 FT_Module* cur = library->modules;
5286 FT_Module* limit = cur + library->num_modules;
5289 for ( ; cur < limit; cur++ )
5291 if ( cur[0] == module )
5293 /* remove it from the table */
5294 library->num_modules--;
5296 while ( cur < limit )
5303 /* destroy the module */
5304 Destroy_Module( module );
5310 return FT_THROW( Invalid_Driver_Handle );
5315 ft_property_do( FT_Library library,
5316 const FT_String* module_name,
5317 const FT_String* property_name,
5320 FT_Bool value_is_string )
5324 FT_Module_Interface interface;
5326 FT_Service_Properties service;
5328 #ifdef FT_DEBUG_LEVEL_ERROR
5329 const FT_String* set_name = "FT_Property_Set";
5330 const FT_String* get_name = "FT_Property_Get";
5331 const FT_String* func_name = set ? set_name : get_name;
5334 FT_Bool missing_func;
5338 return FT_THROW( Invalid_Library_Handle );
5340 if ( !module_name || !property_name || !value )
5341 return FT_THROW( Invalid_Argument );
5343 cur = library->modules;
5344 limit = cur + library->num_modules;
5347 for ( ; cur < limit; cur++ )
5348 if ( !ft_strcmp( cur[0]->clazz->module_name, module_name ) )
5353 FT_TRACE2(( "%s: can't find module `%s'\n",
5354 func_name, module_name ));
5355 return FT_THROW( Missing_Module );
5358 /* check whether we have a service interface */
5359 if ( !cur[0]->clazz->get_interface )
5361 FT_TRACE2(( "%s: module `%s' doesn't support properties\n",
5362 func_name, module_name ));
5363 return FT_THROW( Unimplemented_Feature );
5366 /* search property service */
5367 interface = cur[0]->clazz->get_interface( cur[0],
5368 FT_SERVICE_ID_PROPERTIES );
5371 FT_TRACE2(( "%s: module `%s' doesn't support properties\n",
5372 func_name, module_name ));
5373 return FT_THROW( Unimplemented_Feature );
5376 service = (FT_Service_Properties)interface;
5379 missing_func = FT_BOOL( !service->set_property );
5381 missing_func = FT_BOOL( !service->get_property );
5385 FT_TRACE2(( "%s: property service of module `%s' is broken\n",
5386 func_name, module_name ));
5387 return FT_THROW( Unimplemented_Feature );
5390 return set ? service->set_property( cur[0],
5394 : service->get_property( cur[0],
5400 /* documentation is in ftmodapi.h */
5402 FT_EXPORT_DEF( FT_Error )
5403 FT_Property_Set( FT_Library library,
5404 const FT_String* module_name,
5405 const FT_String* property_name,
5408 return ft_property_do( library,
5417 /* documentation is in ftmodapi.h */
5419 FT_EXPORT_DEF( FT_Error )
5420 FT_Property_Get( FT_Library library,
5421 const FT_String* module_name,
5422 const FT_String* property_name,
5425 return ft_property_do( library,
5434 #ifdef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES
5436 /* this variant is used for handling the FREETYPE_PROPERTIES */
5437 /* environment variable */
5439 FT_BASE_DEF( FT_Error )
5440 ft_property_string_set( FT_Library library,
5441 const FT_String* module_name,
5442 const FT_String* property_name,
5445 return ft_property_do( library,
5456 /*************************************************************************/
5457 /*************************************************************************/
5458 /*************************************************************************/
5461 /**** L I B R A R Y ****/
5464 /*************************************************************************/
5465 /*************************************************************************/
5466 /*************************************************************************/
5469 /* documentation is in ftmodapi.h */
5471 FT_EXPORT_DEF( FT_Error )
5472 FT_Reference_Library( FT_Library library )
5475 return FT_THROW( Invalid_Library_Handle );
5477 library->refcount++;
5483 /* documentation is in ftmodapi.h */
5485 FT_EXPORT_DEF( FT_Error )
5486 FT_New_Library( FT_Memory memory,
5487 FT_Library *alibrary )
5489 FT_Library library = NULL;
5493 if ( !memory || !alibrary )
5494 return FT_THROW( Invalid_Argument );
5496 #ifndef FT_DEBUG_LOGGING
5497 #ifdef FT_DEBUG_LEVEL_ERROR
5498 /* init debugging support */
5500 #endif /* FT_DEBUG_LEVEL_ERROR */
5501 #endif /* !FT_DEBUG_LOGGING */
5503 /* first of all, allocate the library object */
5504 if ( FT_NEW( library ) )
5507 library->memory = memory;
5509 library->version_major = FREETYPE_MAJOR;
5510 library->version_minor = FREETYPE_MINOR;
5511 library->version_patch = FREETYPE_PATCH;
5513 library->refcount = 1;
5516 *alibrary = library;
5522 /* documentation is in freetype.h */
5524 FT_EXPORT_DEF( void )
5525 FT_Library_Version( FT_Library library,
5537 major = library->version_major;
5538 minor = library->version_minor;
5539 patch = library->version_patch;
5553 /* documentation is in ftmodapi.h */
5555 FT_EXPORT_DEF( FT_Error )
5556 FT_Done_Library( FT_Library library )
5562 return FT_THROW( Invalid_Library_Handle );
5564 library->refcount--;
5565 if ( library->refcount > 0 )
5568 memory = library->memory;
5571 * Close all faces in the library. If we don't do this, we can have
5572 * some subtle memory leaks.
5576 * - the cff font driver uses the pshinter module in cff_size_done
5577 * - if the pshinter module is destroyed before the cff font driver,
5578 * opened FT_Face objects managed by the driver are not properly
5579 * destroyed, resulting in a memory leak
5581 * Some faces are dependent on other faces, like Type42 faces that
5582 * depend on TrueType faces synthesized internally.
5584 * The order of drivers should be specified in driver_name[].
5588 const char* driver_name[] = { "type42", NULL };
5592 m < sizeof ( driver_name ) / sizeof ( driver_name[0] );
5595 for ( n = 0; n < library->num_modules; n++ )
5597 FT_Module module = library->modules[n];
5598 const char* module_name = module->clazz->module_name;
5602 if ( driver_name[m] &&
5603 ft_strcmp( module_name, driver_name[m] ) != 0 )
5606 if ( ( module->clazz->module_flags & FT_MODULE_FONT_DRIVER ) == 0 )
5609 FT_TRACE7(( "FT_Done_Library: close faces for %s\n", module_name ));
5611 faces = &FT_DRIVER( module )->faces_list;
5612 while ( faces->head )
5614 FT_Done_Face( FT_FACE( faces->head->data ) );
5616 FT_TRACE0(( "FT_Done_Library: failed to free some faces\n" ));
5622 /* Close all other modules in the library */
5624 /* XXX Modules are removed in the reversed order so that */
5625 /* type42 module is removed before truetype module. This */
5626 /* avoids double free in some occasions. It is a hack. */
5627 while ( library->num_modules > 0 )
5628 FT_Remove_Module( library,
5629 library->modules[library->num_modules - 1] );
5635 for ( n = 0; n < library->num_modules; n++ )
5637 FT_Module module = library->modules[n];
5642 Destroy_Module( module );
5643 library->modules[n] = NULL;
5656 /* documentation is in ftmodapi.h */
5658 FT_EXPORT_DEF( void )
5659 FT_Set_Debug_Hook( FT_Library library,
5661 FT_DebugHook_Func debug_hook )
5663 if ( library && debug_hook &&
5665 ( sizeof ( library->debug_hooks ) / sizeof ( void* ) ) )
5666 library->debug_hooks[hook_index] = debug_hook;
5670 /* documentation is in ftmodapi.h */
5672 FT_EXPORT_DEF( FT_TrueTypeEngineType )
5673 FT_Get_TrueType_Engine_Type( FT_Library library )
5675 FT_TrueTypeEngineType result = FT_TRUETYPE_ENGINE_TYPE_NONE;
5680 FT_Module module = FT_Get_Module( library, "truetype" );
5685 FT_Service_TrueTypeEngine service;
5688 service = (FT_Service_TrueTypeEngine)
5689 ft_module_get_service( module,
5690 FT_SERVICE_ID_TRUETYPE_ENGINE,
5693 result = service->engine_type;
5701 /* documentation is in freetype.h */
5703 FT_EXPORT_DEF( FT_Error )
5704 FT_Get_SubGlyph_Info( FT_GlyphSlot glyph,
5710 FT_Matrix *p_transform )
5712 FT_Error error = FT_ERR( Invalid_Argument );
5717 glyph->format == FT_GLYPH_FORMAT_COMPOSITE &&
5718 sub_index < glyph->num_subglyphs )
5720 FT_SubGlyph subg = glyph->subglyphs + sub_index;
5723 *p_index = subg->index;
5724 *p_flags = subg->flags;
5725 *p_arg1 = subg->arg1;
5726 *p_arg2 = subg->arg2;
5727 *p_transform = subg->transform;
5736 /* documentation is in freetype.h */
5738 FT_EXPORT_DEF( FT_Bool )
5739 FT_Get_Color_Glyph_Layer( FT_Face face,
5741 FT_UInt *aglyph_index,
5742 FT_UInt *acolor_index,
5743 FT_LayerIterator* iterator )
5753 base_glyph >= (FT_UInt)face->num_glyphs )
5756 if ( !FT_IS_SFNT( face ) )
5759 ttface = (TT_Face)face;
5760 sfnt = (SFNT_Service)ttface->sfnt;
5762 if ( sfnt->get_colr_layer )
5763 return sfnt->get_colr_layer( ttface,
5773 /* documentation is in freetype.h */
5775 FT_EXPORT_DEF( FT_Bool )
5776 FT_Get_Color_Glyph_Paint( FT_Face face,
5778 FT_Color_Root_Transform root_transform,
5779 FT_OpaquePaint* paint )
5785 if ( !face || !paint )
5788 if ( !FT_IS_SFNT( face ) )
5791 ttface = (TT_Face)face;
5792 sfnt = (SFNT_Service)ttface->sfnt;
5794 if ( sfnt->get_colr_layer )
5795 return sfnt->get_colr_glyph_paint( ttface,
5804 /* documentation is in ftcolor.h */
5806 FT_EXPORT_DEF( FT_Bool )
5807 FT_Get_Color_Glyph_ClipBox( FT_Face face,
5809 FT_ClipBox* clip_box )
5815 if ( !face || !clip_box )
5818 if ( !FT_IS_SFNT( face ) )
5821 ttface = (TT_Face)face;
5822 sfnt = (SFNT_Service)ttface->sfnt;
5824 if ( sfnt->get_color_glyph_clipbox )
5825 return sfnt->get_color_glyph_clipbox( ttface,
5833 /* documentation is in freetype.h */
5835 FT_EXPORT_DEF( FT_Bool )
5836 FT_Get_Paint_Layers( FT_Face face,
5837 FT_LayerIterator* layer_iterator,
5838 FT_OpaquePaint* paint )
5844 if ( !face || !paint || !layer_iterator )
5847 if ( !FT_IS_SFNT( face ) )
5850 ttface = (TT_Face)face;
5851 sfnt = (SFNT_Service)ttface->sfnt;
5853 if ( sfnt->get_paint_layers )
5854 return sfnt->get_paint_layers( ttface, layer_iterator, paint );
5860 /* documentation is in freetype.h */
5862 FT_EXPORT_DEF( FT_Bool )
5863 FT_Get_Paint( FT_Face face,
5864 FT_OpaquePaint opaque_paint,
5865 FT_COLR_Paint* paint )
5871 if ( !face || !paint )
5874 if ( !FT_IS_SFNT( face ) )
5877 ttface = (TT_Face)face;
5878 sfnt = (SFNT_Service)ttface->sfnt;
5880 if ( sfnt->get_paint )
5881 return sfnt->get_paint( ttface, opaque_paint, paint );
5887 /* documentation is in freetype.h */
5889 FT_EXPORT_DEF( FT_Bool )
5890 FT_Get_Colorline_Stops ( FT_Face face,
5891 FT_ColorStop * color_stop,
5892 FT_ColorStopIterator *iterator )
5898 if ( !face || !color_stop || !iterator )
5901 if ( !FT_IS_SFNT( face ) )
5904 ttface = (TT_Face)face;
5905 sfnt = (SFNT_Service)ttface->sfnt;
5907 if ( sfnt->get_colorline_stops )
5908 return sfnt->get_colorline_stops ( ttface, color_stop, iterator );