📄 ftobjs.c
字号:
if ( !matrix )
{
internal->transform_matrix.xx = 0x10000L;
internal->transform_matrix.xy = 0;
internal->transform_matrix.yx = 0;
internal->transform_matrix.yy = 0x10000L;
matrix = &internal->transform_matrix;
}
else
internal->transform_matrix = *matrix;
/* set transform_flags bit flag 0 if `matrix' isn't the identity */
if ( ( matrix->xy | matrix->yx ) ||
matrix->xx != 0x10000L ||
matrix->yy != 0x10000L )
internal->transform_flags |= 1;
if ( !delta )
{
internal->transform_delta.x = 0;
internal->transform_delta.y = 0;
delta = &internal->transform_delta;
}
else
internal->transform_delta = *delta;
/* set transform_flags bit flag 1 if `delta' isn't the null vector */
if ( delta->x | delta->y )
internal->transform_flags |= 2;
}
static FT_Renderer
ft_lookup_glyph_renderer( FT_GlyphSlot slot );
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Error )
FT_Load_Glyph( FT_Face face,
FT_UInt glyph_index,
FT_Int32 load_flags )
{
FT_Error error;
FT_Driver driver;
FT_GlyphSlot slot;
FT_Library library;
FT_Bool autohint;
FT_Module hinter;
if ( !face || !face->size || !face->glyph )
return FT_Err_Invalid_Face_Handle;
if ( glyph_index >= (FT_UInt)face->num_glyphs )
return FT_Err_Invalid_Argument;
slot = face->glyph;
ft_glyphslot_clear( slot );
driver = face->driver;
/* if the flag NO_RECURSE is set, we disable hinting and scaling */
if ( load_flags & FT_LOAD_NO_RECURSE )
{
/* disable scaling, hinting, and transformation */
load_flags |= FT_LOAD_NO_SCALE |
FT_LOAD_NO_HINTING |
FT_LOAD_NO_BITMAP |
FT_LOAD_IGNORE_TRANSFORM;
/* disable bitmap rendering */
load_flags &= ~FT_LOAD_RENDER;
}
/* do we need to load the glyph through the auto-hinter? */
library = driver->root.library;
hinter = library->auto_hinter;
autohint =
FT_BOOL( hinter &&
!( load_flags & ( FT_LOAD_NO_SCALE |
FT_LOAD_NO_HINTING |
FT_LOAD_NO_AUTOHINT ) ) &&
FT_DRIVER_IS_SCALABLE( driver ) &&
FT_DRIVER_USES_OUTLINES( driver ) );
if ( autohint )
{
if ( FT_DRIVER_HAS_HINTER( driver ) &&
!( load_flags & FT_LOAD_FORCE_AUTOHINT ) )
autohint = 0;
}
/* don't apply autohinting if glyph is vertically distorted or */
/* mirrored */
if ( autohint && !( face->internal->transform_matrix.yy <= 0 ||
face->internal->transform_matrix.yx != 0 ) )
{
FT_AutoHinter_Service hinting;
/* try to load embedded bitmaps first if available */
/* */
/* XXX: This is really a temporary hack that should disappear */
/* promptly with FreeType 2.1! */
/* */
if ( FT_HAS_FIXED_SIZES( face ) &&
( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
{
error = driver->clazz->load_glyph( slot, face->size,
glyph_index,
load_flags | FT_LOAD_SBITS_ONLY );
if ( !error && slot->format == FT_GLYPH_FORMAT_BITMAP )
goto Load_Ok;
}
/* load auto-hinted outline */
hinting = (FT_AutoHinter_Service)hinter->clazz->module_interface;
error = hinting->load_glyph( (FT_AutoHinter)hinter,
slot, face->size,
glyph_index, load_flags );
}
else
{
error = driver->clazz->load_glyph( slot,
face->size,
glyph_index,
load_flags );
if ( error )
goto Exit;
/* check that the loaded outline is correct */
error = FT_Outline_Check( &slot->outline );
if ( error )
goto Exit;
}
Load_Ok:
/* compute the advance */
if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
{
slot->advance.x = 0;
slot->advance.y = slot->metrics.vertAdvance;
}
else
{
slot->advance.x = slot->metrics.horiAdvance;
slot->advance.y = 0;
}
/* compute the linear advance in 16.16 pixels */
if ( ( load_flags & FT_LOAD_LINEAR_DESIGN ) == 0 &&
( face->face_flags & FT_FACE_FLAG_SCALABLE ) )
{
FT_Size_Metrics* metrics = &face->size->metrics;
slot->linearHoriAdvance = FT_MulDiv( slot->linearHoriAdvance,
metrics->x_scale, 64 );
slot->linearVertAdvance = FT_MulDiv( slot->linearVertAdvance,
metrics->y_scale, 64 );
}
if ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) == 0 )
{
FT_Face_Internal internal = face->internal;
/* now, transform the glyph image if needed */
if ( internal->transform_flags )
{
/* get renderer */
FT_Renderer renderer = ft_lookup_glyph_renderer( slot );
if ( renderer )
error = renderer->clazz->transform_glyph(
renderer, slot,
&internal->transform_matrix,
&internal->transform_delta );
/* transform advance */
FT_Vector_Transform( &slot->advance, &internal->transform_matrix );
}
}
/* do we need to render the image now? */
if ( !error &&
slot->format != FT_GLYPH_FORMAT_BITMAP &&
slot->format != FT_GLYPH_FORMAT_COMPOSITE &&
load_flags & FT_LOAD_RENDER )
{
FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags );
if ( mode == FT_RENDER_MODE_NORMAL &&
(load_flags & FT_LOAD_MONOCHROME ) )
mode = FT_RENDER_MODE_MONO;
error = FT_Render_Glyph( slot, mode );
}
Exit:
return error;
}
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Error )
FT_Load_Char( FT_Face face,
FT_ULong char_code,
FT_Int32 load_flags )
{
FT_UInt glyph_index;
if ( !face )
return FT_Err_Invalid_Face_Handle;
glyph_index = (FT_UInt)char_code;
if ( face->charmap )
glyph_index = FT_Get_Char_Index( face, char_code );
return FT_Load_Glyph( face, glyph_index, load_flags );
}
/* destructor for sizes list */
static void
destroy_size( FT_Memory memory,
FT_Size size,
FT_Driver driver )
{
/* finalize client-specific data */
if ( size->generic.finalizer )
size->generic.finalizer( size );
/* finalize format-specific stuff */
if ( driver->clazz->done_size )
driver->clazz->done_size( size );
FT_FREE( size->internal );
FT_FREE( size );
}
static void
ft_cmap_done_internal( FT_CMap cmap );
static void
destroy_charmaps( FT_Face face,
FT_Memory memory )
{
FT_Int n;
for ( n = 0; n < face->num_charmaps; n++ )
{
FT_CMap cmap = FT_CMAP( face->charmaps[n] );
ft_cmap_done_internal( cmap );
face->charmaps[n] = NULL;
}
FT_FREE( face->charmaps );
face->num_charmaps = 0;
}
/* destructor for faces list */
static void
destroy_face( FT_Memory memory,
FT_Face face,
FT_Driver driver )
{
FT_Driver_Class clazz = driver->clazz;
/* discard auto-hinting data */
if ( face->autohint.finalizer )
face->autohint.finalizer( face->autohint.data );
/* Discard glyph slots for this face. */
/* Beware! FT_Done_GlyphSlot() changes the field `face->glyph' */
while ( face->glyph )
FT_Done_GlyphSlot( face->glyph );
/* discard all sizes for this face */
FT_List_Finalize( &face->sizes_list,
(FT_List_Destructor)destroy_size,
memory,
driver );
face->size = 0;
/* now discard client data */
if ( face->generic.finalizer )
face->generic.finalizer( face );
/* discard charmaps */
destroy_charmaps( face, memory );
/* finalize format-specific stuff */
if ( clazz->done_face )
clazz->done_face( face );
/* close the stream for this face if needed */
FT_Stream_Free(
face->stream,
( face->face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
face->stream = 0;
/* get rid of it */
if ( face->internal )
{
FT_FREE( face->internal );
}
FT_FREE( face );
}
static void
Destroy_Driver( FT_Driver driver )
{
FT_List_Finalize( &driver->faces_list,
(FT_List_Destructor)destroy_face,
driver->root.memory,
driver );
/* check whether we need to drop the driver's glyph loader */
if ( FT_DRIVER_USES_OUTLINES( driver ) )
FT_GlyphLoader_Done( driver->glyph_loader );
}
/*************************************************************************/
/* */
/* <Function> */
/* find_unicode_charmap */
/* */
/* <Description> */
/* This function finds a Unicode charmap, if there is one. */
/* And if there is more than one, it tries to favour the more */
/* extensive one, i.e., one that supports UCS-4 against those which */
/* are limited to the BMP (said UCS-2 encoding.) */
/* */
/* This function is called from open_face() (just below), and also */
/* from FT_Select_Charmap( ..., FT_ENCODING_UNICODE). */
/* */
static FT_Error
find_unicode_charmap( FT_Face face )
{
FT_CharMap* first;
FT_CharMap* cur;
FT_CharMap* unicmap = NULL; /* some UCS-2 map, if we found it */
/* caller should have already checked that `face' is valid */
FT_ASSERT( face );
first = face->charmaps;
if ( !first )
return FT_Err_Invalid_CharMap_Handle;
/*
* The original TrueType specification(s) only specified charmap
* formats that are capable of mapping 8 or 16 bit character codes to
* glyph indices.
*
* However, recent updates to the Apple and OpenType specifications
* introduced new formats that are capable of mapping 32-bit character
* codes as well. And these are already used on some fonts, mainly to
* map non-BMP Asian ideographs as defined in Unicode.
*
* For compatibility purposes, these fonts generally come with
* *several* Unicode charmaps:
*
* - One of them in the "old" 16-bit format, that cannot access
* all glyphs in the font.
*
* - Another one in the "new" 32-bit format, that can access all
* the glyphs.
*
* This function has been written to always favor a 32-bit charmap
* when found. Otherwise, a 16-bit one is returned when found.
*/
/* Since the `interesting' table, with IDs (3,10), is normally the */
/* last one, we loop backwards. This looses with type1 fonts with */
/* non-BMP characters (<.0001%), this wins with .ttf with non-BMP */
/* chars (.01% ?), and this is the same about 99.99% of the time! */
cur = first + face->num_charmaps; /* points after the last one */
for ( ; --cur >= first; )
{
if ( cur[0]->encoding == FT_ENCODING_UNICODE )
{
unicmap = cur; /* record we found a Unicode charmap */
/* XXX If some new encodings to represent UCS-4 are added, */
/* they should be added here. */
if ( ( cur[0]->platform_id == TT_PLATFORM_MICROSOFT &&
cur[0]->encoding_id == TT_MS_ID_UCS_4 ) ||
( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
cur[0]->encoding_id == TT_APPLE_ID_UNICODE_32 ) )
/* Hurray! We found a UCS-4 charmap. We can stop the scan! */
{
face->charmap = cur[0];
return 0;
}
}
}
/* We do not have any UCS-4 charmap. Sigh. */
/* Let's see if we have some other kind of Unicode charmap, though. */
if ( unicmap != NULL )
{
face->charmap = unicmap[0];
return 0;
}
/* Chou blanc! */
return FT_Err_Invalid_CharMap_Handle;
}
/*************************************************************************/
/* */
/* <Function> */
/* open_face */
/* */
/* <Description> */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -