/lib_freetype/freetype/src/truetype/ttgload.c
C | 2589 lines | 1668 code | 513 blank | 408 comment | 262 complexity | c3773bb36b6e64cd54e85ec57d8c189c MD5 | raw file
Possible License(s): Zlib, BSD-3-Clause, CC0-1.0, GPL-3.0, GPL-2.0, CPL-1.0, MPL-2.0-no-copyleft-exception, LGPL-2.0, LGPL-2.1, LGPL-3.0, 0BSD, Cube
Large files files are truncated, but you can click here to view the full file
- /***************************************************************************/
- /* */
- /* ttgload.c */
- /* */
- /* TrueType Glyph Loader (body). */
- /* */
- /* Copyright 1996-2015 by */
- /* David Turner, Robert Wilhelm, and Werner Lemberg. */
- /* */
- /* This file is part of the FreeType project, and may only be used, */
- /* modified, and distributed under the terms of the FreeType project */
- /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
- /* this file you indicate that you have read the license and */
- /* understand and accept it fully. */
- /* */
- /***************************************************************************/
- #include <ft2build.h>
- #include FT_INTERNAL_DEBUG_H
- #include FT_INTERNAL_CALC_H
- #include FT_INTERNAL_STREAM_H
- #include FT_INTERNAL_SFNT_H
- #include FT_TRUETYPE_TAGS_H
- #include FT_OUTLINE_H
- #include FT_TRUETYPE_DRIVER_H
- #include "ttgload.h"
- #include "ttpload.h"
- #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
- #include "ttgxvar.h"
- #endif
- #include "tterrors.h"
- #include "ttsubpix.h"
- /*************************************************************************/
- /* */
- /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
- /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
- /* messages during execution. */
- /* */
- #undef FT_COMPONENT
- #define FT_COMPONENT trace_ttgload
- /*************************************************************************/
- /* */
- /* Composite glyph flags. */
- /* */
- #define ARGS_ARE_WORDS 0x0001
- #define ARGS_ARE_XY_VALUES 0x0002
- #define ROUND_XY_TO_GRID 0x0004
- #define WE_HAVE_A_SCALE 0x0008
- /* reserved 0x0010 */
- #define MORE_COMPONENTS 0x0020
- #define WE_HAVE_AN_XY_SCALE 0x0040
- #define WE_HAVE_A_2X2 0x0080
- #define WE_HAVE_INSTR 0x0100
- #define USE_MY_METRICS 0x0200
- #define OVERLAP_COMPOUND 0x0400
- #define SCALED_COMPONENT_OFFSET 0x0800
- #define UNSCALED_COMPONENT_OFFSET 0x1000
- /*************************************************************************/
- /* */
- /* Return the horizontal metrics in font units for a given glyph. */
- /* */
- FT_LOCAL_DEF( void )
- TT_Get_HMetrics( TT_Face face,
- FT_UInt idx,
- FT_Short* lsb,
- FT_UShort* aw )
- {
- ( (SFNT_Service)face->sfnt )->get_metrics( face, 0, idx, lsb, aw );
- FT_TRACE5(( " advance width (font units): %d\n", *aw ));
- FT_TRACE5(( " left side bearing (font units): %d\n", *lsb ));
- }
- /*************************************************************************/
- /* */
- /* Return the vertical metrics in font units for a given glyph. */
- /* See macro `TT_LOADER_SET_PP' below for explanations. */
- /* */
- FT_LOCAL_DEF( void )
- TT_Get_VMetrics( TT_Face face,
- FT_UInt idx,
- FT_Pos yMax,
- FT_Short* tsb,
- FT_UShort* ah )
- {
- if ( face->vertical_info )
- ( (SFNT_Service)face->sfnt )->get_metrics( face, 1, idx, tsb, ah );
- else if ( face->os2.version != 0xFFFFU )
- {
- *tsb = (FT_Short)( face->os2.sTypoAscender - yMax );
- *ah = (FT_UShort)FT_ABS( face->os2.sTypoAscender -
- face->os2.sTypoDescender );
- }
- else
- {
- *tsb = (FT_Short)( face->horizontal.Ascender - yMax );
- *ah = (FT_UShort)FT_ABS( face->horizontal.Ascender -
- face->horizontal.Descender );
- }
- FT_TRACE5(( " advance height (font units): %d\n", *ah ));
- FT_TRACE5(( " top side bearing (font units): %d\n", *tsb ));
- }
- static FT_Error
- tt_get_metrics( TT_Loader loader,
- FT_UInt glyph_index )
- {
- TT_Face face = (TT_Face)loader->face;
- #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
- TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
- #endif
- FT_Error error;
- FT_Stream stream = loader->stream;
- FT_Short left_bearing = 0, top_bearing = 0;
- FT_UShort advance_width = 0, advance_height = 0;
- /* we must preserve the stream position */
- /* (which gets altered by the metrics functions) */
- FT_ULong pos = FT_STREAM_POS();
- TT_Get_HMetrics( face, glyph_index,
- &left_bearing,
- &advance_width );
- TT_Get_VMetrics( face, glyph_index,
- loader->bbox.yMax,
- &top_bearing,
- &advance_height );
- if ( FT_STREAM_SEEK( pos ) )
- return error;
- loader->left_bearing = left_bearing;
- loader->advance = advance_width;
- loader->top_bearing = top_bearing;
- loader->vadvance = advance_height;
- #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
- if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 &&
- loader->exec )
- {
- loader->exec->sph_tweak_flags = 0;
- /* This may not be the right place for this, but it works... */
- /* Note that we have to unconditionally load the tweaks since */
- /* it is possible that glyphs individually switch ClearType's */
- /* backwards compatibility mode on and off. */
- sph_set_tweaks( loader, glyph_index );
- }
- #endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
- if ( !loader->linear_def )
- {
- loader->linear_def = 1;
- loader->linear = advance_width;
- }
- return FT_Err_Ok;
- }
- #ifdef FT_CONFIG_OPTION_INCREMENTAL
- static void
- tt_get_metrics_incr_overrides( TT_Loader loader,
- FT_UInt glyph_index )
- {
- TT_Face face = (TT_Face)loader->face;
- FT_Short left_bearing = 0, top_bearing = 0;
- FT_UShort advance_width = 0, advance_height = 0;
- /* If this is an incrementally loaded font check whether there are */
- /* overriding metrics for this glyph. */
- if ( face->root.internal->incremental_interface &&
- face->root.internal->incremental_interface->funcs->get_glyph_metrics )
- {
- FT_Incremental_MetricsRec metrics;
- FT_Error error;
- metrics.bearing_x = loader->left_bearing;
- metrics.bearing_y = 0;
- metrics.advance = loader->advance;
- metrics.advance_v = 0;
- error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
- face->root.internal->incremental_interface->object,
- glyph_index, FALSE, &metrics );
- if ( error )
- goto Exit;
- left_bearing = (FT_Short)metrics.bearing_x;
- advance_width = (FT_UShort)metrics.advance;
- #if 0
- /* GWW: Do I do the same for vertical metrics? */
- metrics.bearing_x = 0;
- metrics.bearing_y = loader->top_bearing;
- metrics.advance = loader->vadvance;
- error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
- face->root.internal->incremental_interface->object,
- glyph_index, TRUE, &metrics );
- if ( error )
- goto Exit;
- top_bearing = (FT_Short)metrics.bearing_y;
- advance_height = (FT_UShort)metrics.advance;
- #endif /* 0 */
- loader->left_bearing = left_bearing;
- loader->advance = advance_width;
- loader->top_bearing = top_bearing;
- loader->vadvance = advance_height;
- if ( !loader->linear_def )
- {
- loader->linear_def = 1;
- loader->linear = advance_width;
- }
- }
- Exit:
- return;
- }
- #endif /* FT_CONFIG_OPTION_INCREMENTAL */
- /*************************************************************************/
- /* */
- /* Translates an array of coordinates. */
- /* */
- static void
- translate_array( FT_UInt n,
- FT_Vector* coords,
- FT_Pos delta_x,
- FT_Pos delta_y )
- {
- FT_UInt k;
- if ( delta_x )
- for ( k = 0; k < n; k++ )
- coords[k].x += delta_x;
- if ( delta_y )
- for ( k = 0; k < n; k++ )
- coords[k].y += delta_y;
- }
- /*************************************************************************/
- /* */
- /* The following functions are used by default with TrueType fonts. */
- /* However, they can be replaced by alternatives if we need to support */
- /* TrueType-compressed formats (like MicroType) in the future. */
- /* */
- /*************************************************************************/
- FT_CALLBACK_DEF( FT_Error )
- TT_Access_Glyph_Frame( TT_Loader loader,
- FT_UInt glyph_index,
- FT_ULong offset,
- FT_UInt byte_count )
- {
- FT_Error error;
- FT_Stream stream = loader->stream;
- /* for non-debug mode */
- FT_UNUSED( glyph_index );
- FT_TRACE4(( "Glyph %ld\n", glyph_index ));
- /* the following line sets the `error' variable through macros! */
- if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( byte_count ) )
- return error;
- loader->cursor = stream->cursor;
- loader->limit = stream->limit;
- return FT_Err_Ok;
- }
- FT_CALLBACK_DEF( void )
- TT_Forget_Glyph_Frame( TT_Loader loader )
- {
- FT_Stream stream = loader->stream;
- FT_FRAME_EXIT();
- }
- FT_CALLBACK_DEF( FT_Error )
- TT_Load_Glyph_Header( TT_Loader loader )
- {
- FT_Byte* p = loader->cursor;
- FT_Byte* limit = loader->limit;
- if ( p + 10 > limit )
- return FT_THROW( Invalid_Outline );
- loader->n_contours = FT_NEXT_SHORT( p );
- loader->bbox.xMin = FT_NEXT_SHORT( p );
- loader->bbox.yMin = FT_NEXT_SHORT( p );
- loader->bbox.xMax = FT_NEXT_SHORT( p );
- loader->bbox.yMax = FT_NEXT_SHORT( p );
- FT_TRACE5(( " # of contours: %d\n", loader->n_contours ));
- FT_TRACE5(( " xMin: %4d xMax: %4d\n", loader->bbox.xMin,
- loader->bbox.xMax ));
- FT_TRACE5(( " yMin: %4d yMax: %4d\n", loader->bbox.yMin,
- loader->bbox.yMax ));
- loader->cursor = p;
- return FT_Err_Ok;
- }
- FT_CALLBACK_DEF( FT_Error )
- TT_Load_Simple_Glyph( TT_Loader load )
- {
- FT_Error error;
- FT_Byte* p = load->cursor;
- FT_Byte* limit = load->limit;
- FT_GlyphLoader gloader = load->gloader;
- FT_Int n_contours = load->n_contours;
- FT_Outline* outline;
- FT_UShort n_ins;
- FT_Int n_points;
- FT_ULong tmp;
- FT_Byte *flag, *flag_limit;
- FT_Byte c, count;
- FT_Vector *vec, *vec_limit;
- FT_Pos x;
- FT_Short *cont, *cont_limit, prev_cont;
- FT_Int xy_size = 0;
- /* check that we can add the contours to the glyph */
- error = FT_GLYPHLOADER_CHECK_POINTS( gloader, 0, n_contours );
- if ( error )
- goto Fail;
- /* reading the contours' endpoints & number of points */
- cont = gloader->current.outline.contours;
- cont_limit = cont + n_contours;
- /* check space for contours array + instructions count */
- if ( n_contours >= 0xFFF || p + ( n_contours + 1 ) * 2 > limit )
- goto Invalid_Outline;
- prev_cont = FT_NEXT_SHORT( p );
- if ( n_contours > 0 )
- cont[0] = prev_cont;
- if ( prev_cont < 0 )
- goto Invalid_Outline;
- for ( cont++; cont < cont_limit; cont++ )
- {
- cont[0] = FT_NEXT_SHORT( p );
- if ( cont[0] <= prev_cont )
- {
- /* unordered contours: this is invalid */
- goto Invalid_Outline;
- }
- prev_cont = cont[0];
- }
- n_points = 0;
- if ( n_contours > 0 )
- {
- n_points = cont[-1] + 1;
- if ( n_points < 0 )
- goto Invalid_Outline;
- }
- /* note that we will add four phantom points later */
- error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 );
- if ( error )
- goto Fail;
- /* reading the bytecode instructions */
- load->glyph->control_len = 0;
- load->glyph->control_data = NULL;
- if ( p + 2 > limit )
- goto Invalid_Outline;
- n_ins = FT_NEXT_USHORT( p );
- FT_TRACE5(( " Instructions size: %u\n", n_ins ));
- /* check it */
- if ( ( limit - p ) < n_ins )
- {
- FT_TRACE0(( "TT_Load_Simple_Glyph: instruction count mismatch\n" ));
- error = FT_THROW( Too_Many_Hints );
- goto Fail;
- }
- #ifdef TT_USE_BYTECODE_INTERPRETER
- if ( IS_HINTED( load->load_flags ) )
- {
- /* we don't trust `maxSizeOfInstructions' in the `maxp' table */
- /* and thus update the bytecode array size by ourselves */
- tmp = load->exec->glyphSize;
- error = Update_Max( load->exec->memory,
- &tmp,
- sizeof ( FT_Byte ),
- (void*)&load->exec->glyphIns,
- n_ins );
- load->exec->glyphSize = (FT_UShort)tmp;
- if ( error )
- return error;
- load->glyph->control_len = n_ins;
- load->glyph->control_data = load->exec->glyphIns;
- FT_MEM_COPY( load->exec->glyphIns, p, (FT_Long)n_ins );
- }
- #endif /* TT_USE_BYTECODE_INTERPRETER */
- p += n_ins;
- outline = &gloader->current.outline;
- /* reading the point tags */
- flag = (FT_Byte*)outline->tags;
- flag_limit = flag + n_points;
- FT_ASSERT( flag != NULL );
- while ( flag < flag_limit )
- {
- if ( p + 1 > limit )
- goto Invalid_Outline;
- *flag++ = c = FT_NEXT_BYTE( p );
- if ( c & 8 )
- {
- if ( p + 1 > limit )
- goto Invalid_Outline;
- count = FT_NEXT_BYTE( p );
- if ( flag + (FT_Int)count > flag_limit )
- goto Invalid_Outline;
- for ( ; count > 0; count-- )
- *flag++ = c;
- }
- }
- /* reading the X coordinates */
- vec = outline->points;
- vec_limit = vec + n_points;
- flag = (FT_Byte*)outline->tags;
- x = 0;
- if ( p + xy_size > limit )
- goto Invalid_Outline;
- for ( ; vec < vec_limit; vec++, flag++ )
- {
- FT_Pos y = 0;
- FT_Byte f = *flag;
- if ( f & 2 )
- {
- if ( p + 1 > limit )
- goto Invalid_Outline;
- y = (FT_Pos)FT_NEXT_BYTE( p );
- if ( ( f & 16 ) == 0 )
- y = -y;
- }
- else if ( ( f & 16 ) == 0 )
- {
- if ( p + 2 > limit )
- goto Invalid_Outline;
- y = (FT_Pos)FT_NEXT_SHORT( p );
- }
- x += y;
- vec->x = x;
- /* the cast is for stupid compilers */
- *flag = (FT_Byte)( f & ~( 2 | 16 ) );
- }
- /* reading the Y coordinates */
- vec = gloader->current.outline.points;
- vec_limit = vec + n_points;
- flag = (FT_Byte*)outline->tags;
- x = 0;
- for ( ; vec < vec_limit; vec++, flag++ )
- {
- FT_Pos y = 0;
- FT_Byte f = *flag;
- if ( f & 4 )
- {
- if ( p + 1 > limit )
- goto Invalid_Outline;
- y = (FT_Pos)FT_NEXT_BYTE( p );
- if ( ( f & 32 ) == 0 )
- y = -y;
- }
- else if ( ( f & 32 ) == 0 )
- {
- if ( p + 2 > limit )
- goto Invalid_Outline;
- y = (FT_Pos)FT_NEXT_SHORT( p );
- }
- x += y;
- vec->y = x;
- /* the cast is for stupid compilers */
- *flag = (FT_Byte)( f & FT_CURVE_TAG_ON );
- }
- outline->n_points = (FT_Short)n_points;
- outline->n_contours = (FT_Short)n_contours;
- load->cursor = p;
- Fail:
- return error;
- Invalid_Outline:
- error = FT_THROW( Invalid_Outline );
- goto Fail;
- }
- FT_CALLBACK_DEF( FT_Error )
- TT_Load_Composite_Glyph( TT_Loader loader )
- {
- FT_Error error;
- FT_Byte* p = loader->cursor;
- FT_Byte* limit = loader->limit;
- FT_GlyphLoader gloader = loader->gloader;
- FT_SubGlyph subglyph;
- FT_UInt num_subglyphs;
- num_subglyphs = 0;
- do
- {
- FT_Fixed xx, xy, yy, yx;
- FT_UInt count;
- /* check that we can load a new subglyph */
- error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs + 1 );
- if ( error )
- goto Fail;
- /* check space */
- if ( p + 4 > limit )
- goto Invalid_Composite;
- subglyph = gloader->current.subglyphs + num_subglyphs;
- subglyph->arg1 = subglyph->arg2 = 0;
- subglyph->flags = FT_NEXT_USHORT( p );
- subglyph->index = FT_NEXT_USHORT( p );
- /* check space */
- count = 2;
- if ( subglyph->flags & ARGS_ARE_WORDS )
- count += 2;
- if ( subglyph->flags & WE_HAVE_A_SCALE )
- count += 2;
- else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
- count += 4;
- else if ( subglyph->flags & WE_HAVE_A_2X2 )
- count += 8;
- if ( p + count > limit )
- goto Invalid_Composite;
- /* read arguments */
- if ( subglyph->flags & ARGS_ARE_XY_VALUES )
- {
- if ( subglyph->flags & ARGS_ARE_WORDS )
- {
- subglyph->arg1 = FT_NEXT_SHORT( p );
- subglyph->arg2 = FT_NEXT_SHORT( p );
- }
- else
- {
- subglyph->arg1 = FT_NEXT_CHAR( p );
- subglyph->arg2 = FT_NEXT_CHAR( p );
- }
- }
- else
- {
- if ( subglyph->flags & ARGS_ARE_WORDS )
- {
- subglyph->arg1 = (FT_Int)FT_NEXT_USHORT( p );
- subglyph->arg2 = (FT_Int)FT_NEXT_USHORT( p );
- }
- else
- {
- subglyph->arg1 = (FT_Int)FT_NEXT_BYTE( p );
- subglyph->arg2 = (FT_Int)FT_NEXT_BYTE( p );
- }
- }
- /* read transform */
- xx = yy = 0x10000L;
- xy = yx = 0;
- if ( subglyph->flags & WE_HAVE_A_SCALE )
- {
- xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
- yy = xx;
- }
- else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
- {
- xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
- yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
- }
- else if ( subglyph->flags & WE_HAVE_A_2X2 )
- {
- xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
- yx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
- xy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
- yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
- }
- subglyph->transform.xx = xx;
- subglyph->transform.xy = xy;
- subglyph->transform.yx = yx;
- subglyph->transform.yy = yy;
- num_subglyphs++;
- } while ( subglyph->flags & MORE_COMPONENTS );
- gloader->current.num_subglyphs = num_subglyphs;
- #ifdef TT_USE_BYTECODE_INTERPRETER
- {
- FT_Stream stream = loader->stream;
- /* we must undo the FT_FRAME_ENTER in order to point */
- /* to the composite instructions, if we find some. */
- /* We will process them later. */
- /* */
- loader->ins_pos = (FT_ULong)( FT_STREAM_POS() +
- p - limit );
- }
- #endif
- loader->cursor = p;
- Fail:
- return error;
- Invalid_Composite:
- error = FT_THROW( Invalid_Composite );
- goto Fail;
- }
- FT_LOCAL_DEF( void )
- TT_Init_Glyph_Loading( TT_Face face )
- {
- face->access_glyph_frame = TT_Access_Glyph_Frame;
- face->read_glyph_header = TT_Load_Glyph_Header;
- face->read_simple_glyph = TT_Load_Simple_Glyph;
- face->read_composite_glyph = TT_Load_Composite_Glyph;
- face->forget_glyph_frame = TT_Forget_Glyph_Frame;
- }
- static void
- tt_prepare_zone( TT_GlyphZone zone,
- FT_GlyphLoad load,
- FT_UInt start_point,
- FT_UInt start_contour )
- {
- zone->n_points = (FT_UShort)load->outline.n_points -
- (FT_UShort)start_point;
- zone->n_contours = load->outline.n_contours -
- (FT_Short)start_contour;
- zone->org = load->extra_points + start_point;
- zone->cur = load->outline.points + start_point;
- zone->orus = load->extra_points2 + start_point;
- zone->tags = (FT_Byte*)load->outline.tags + start_point;
- zone->contours = (FT_UShort*)load->outline.contours + start_contour;
- zone->first_point = (FT_UShort)start_point;
- }
- /*************************************************************************/
- /* */
- /* <Function> */
- /* TT_Hint_Glyph */
- /* */
- /* <Description> */
- /* Hint the glyph using the zone prepared by the caller. Note that */
- /* the zone is supposed to include four phantom points. */
- /* */
- static FT_Error
- TT_Hint_Glyph( TT_Loader loader,
- FT_Bool is_composite )
- {
- #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
- TT_Face face = (TT_Face)loader->face;
- TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
- #endif
- TT_GlyphZone zone = &loader->zone;
- #ifdef TT_USE_BYTECODE_INTERPRETER
- FT_Long n_ins;
- #else
- FT_UNUSED( is_composite );
- #endif
- #ifdef TT_USE_BYTECODE_INTERPRETER
- if ( loader->glyph->control_len > 0xFFFFL )
- {
- FT_TRACE1(( "TT_Hint_Glyph: too long instructions" ));
- FT_TRACE1(( " (0x%lx byte) is truncated\n",
- loader->glyph->control_len ));
- }
- n_ins = loader->glyph->control_len;
- /* save original point position in org */
- if ( n_ins > 0 )
- FT_ARRAY_COPY( zone->org, zone->cur, zone->n_points );
- /* Reset graphics state. */
- loader->exec->GS = ((TT_Size)loader->size)->GS;
- /* XXX: UNDOCUMENTED! Hinting instructions of a composite glyph */
- /* completely refer to the (already) hinted subglyphs. */
- if ( is_composite )
- {
- loader->exec->metrics.x_scale = 1 << 16;
- loader->exec->metrics.y_scale = 1 << 16;
- FT_ARRAY_COPY( zone->orus, zone->cur, zone->n_points );
- }
- else
- {
- loader->exec->metrics.x_scale =
- ((TT_Size)loader->size)->metrics.x_scale;
- loader->exec->metrics.y_scale =
- ((TT_Size)loader->size)->metrics.y_scale;
- }
- #endif
- /* round phantom points */
- zone->cur[zone->n_points - 4].x =
- FT_PIX_ROUND( zone->cur[zone->n_points - 4].x );
- zone->cur[zone->n_points - 3].x =
- FT_PIX_ROUND( zone->cur[zone->n_points - 3].x );
- zone->cur[zone->n_points - 2].y =
- FT_PIX_ROUND( zone->cur[zone->n_points - 2].y );
- zone->cur[zone->n_points - 1].y =
- FT_PIX_ROUND( zone->cur[zone->n_points - 1].y );
- #ifdef TT_USE_BYTECODE_INTERPRETER
- if ( n_ins > 0 )
- {
- FT_Error error;
- FT_GlyphLoader gloader = loader->gloader;
- FT_Outline current_outline = gloader->current.outline;
- TT_Set_CodeRange( loader->exec, tt_coderange_glyph,
- loader->exec->glyphIns, n_ins );
- loader->exec->is_composite = is_composite;
- loader->exec->pts = *zone;
- error = TT_Run_Context( loader->exec );
- if ( error && loader->exec->pedantic_hinting )
- return error;
- /* store drop-out mode in bits 5-7; set bit 2 also as a marker */
- current_outline.tags[0] |=
- ( loader->exec->GS.scan_type << 5 ) | FT_CURVE_TAG_HAS_SCANMODE;
- }
- #endif
- /* save glyph phantom points */
- loader->pp1 = zone->cur[zone->n_points - 4];
- loader->pp2 = zone->cur[zone->n_points - 3];
- loader->pp3 = zone->cur[zone->n_points - 2];
- loader->pp4 = zone->cur[zone->n_points - 1];
- #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
- if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
- {
- if ( loader->exec->sph_tweak_flags & SPH_TWEAK_DEEMBOLDEN )
- FT_Outline_EmboldenXY( &loader->gloader->current.outline, -24, 0 );
- else if ( loader->exec->sph_tweak_flags & SPH_TWEAK_EMBOLDEN )
- FT_Outline_EmboldenXY( &loader->gloader->current.outline, 24, 0 );
- }
- #endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
- return FT_Err_Ok;
- }
- /*************************************************************************/
- /* */
- /* <Function> */
- /* TT_Process_Simple_Glyph */
- /* */
- /* <Description> */
- /* Once a simple glyph has been loaded, it needs to be processed. */
- /* Usually, this means scaling and hinting through bytecode */
- /* interpretation. */
- /* */
- static FT_Error
- TT_Process_Simple_Glyph( TT_Loader loader )
- {
- FT_GlyphLoader gloader = loader->gloader;
- FT_Error error = FT_Err_Ok;
- FT_Outline* outline;
- FT_Int n_points;
- outline = &gloader->current.outline;
- n_points = outline->n_points;
- /* set phantom points */
- outline->points[n_points ] = loader->pp1;
- outline->points[n_points + 1] = loader->pp2;
- outline->points[n_points + 2] = loader->pp3;
- outline->points[n_points + 3] = loader->pp4;
- outline->tags[n_points ] = 0;
- outline->tags[n_points + 1] = 0;
- outline->tags[n_points + 2] = 0;
- outline->tags[n_points + 3] = 0;
- n_points += 4;
- #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
- if ( ((TT_Face)loader->face)->doblend )
- {
- /* Deltas apply to the unscaled data. */
- error = TT_Vary_Apply_Glyph_Deltas( (TT_Face)(loader->face),
- loader->glyph_index,
- outline,
- (FT_UInt)n_points );
- if ( error )
- return error;
- }
- #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
- if ( IS_HINTED( loader->load_flags ) )
- {
- tt_prepare_zone( &loader->zone, &gloader->current, 0, 0 );
- FT_ARRAY_COPY( loader->zone.orus, loader->zone.cur,
- loader->zone.n_points + 4 );
- }
- {
- #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
- TT_Face face = (TT_Face)loader->face;
- TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
- FT_String* family = face->root.family_name;
- FT_UInt ppem = loader->size->metrics.x_ppem;
- FT_String* style = face->root.style_name;
- FT_UInt x_scale_factor = 1000;
- #endif
- FT_Vector* vec = outline->points;
- FT_Vector* limit = outline->points + n_points;
- FT_Fixed x_scale = 0; /* pacify compiler */
- FT_Fixed y_scale = 0;
- FT_Bool do_scale = FALSE;
- #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
- if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
- {
- /* scale, but only if enabled and only if TT hinting is being used */
- if ( IS_HINTED( loader->load_flags ) )
- x_scale_factor = sph_test_tweak_x_scaling( face,
- family,
- ppem,
- style,
- loader->glyph_index );
- /* scale the glyph */
- if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ||
- x_scale_factor != 1000 )
- {
- x_scale = FT_MulDiv( ((TT_Size)loader->size)->metrics.x_scale,
- (FT_Long)x_scale_factor, 1000 );
- y_scale = ((TT_Size)loader->size)->metrics.y_scale;
- /* compensate for any scaling by de/emboldening; */
- /* the amount was determined via experimentation */
- if ( x_scale_factor != 1000 && ppem > 11 )
- FT_Outline_EmboldenXY( outline,
- FT_MulFix( 1280 * ppem,
- 1000 - x_scale_factor ),
- 0 );
- do_scale = TRUE;
- }
- }
- else
- #endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
- {
- /* scale the glyph */
- if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
- {
- x_scale = ((TT_Size)loader->size)->metrics.x_scale;
- y_scale = ((TT_Size)loader->size)->metrics.y_scale;
- do_scale = TRUE;
- }
- }
- if ( do_scale )
- {
- for ( ; vec < limit; vec++ )
- {
- vec->x = FT_MulFix( vec->x, x_scale );
- vec->y = FT_MulFix( vec->y, y_scale );
- }
- loader->pp1 = outline->points[n_points - 4];
- loader->pp2 = outline->points[n_points - 3];
- loader->pp3 = outline->points[n_points - 2];
- loader->pp4 = outline->points[n_points - 1];
- }
- }
- if ( IS_HINTED( loader->load_flags ) )
- {
- loader->zone.n_points += 4;
- error = TT_Hint_Glyph( loader, 0 );
- }
- return error;
- }
- /*************************************************************************/
- /* */
- /* <Function> */
- /* TT_Process_Composite_Component */
- /* */
- /* <Description> */
- /* Once a composite component has been loaded, it needs to be */
- /* processed. Usually, this means transforming and translating. */
- /* */
- static FT_Error
- TT_Process_Composite_Component( TT_Loader loader,
- FT_SubGlyph subglyph,
- FT_UInt start_point,
- FT_UInt num_base_points )
- {
- FT_GlyphLoader gloader = loader->gloader;
- FT_Vector* base_vec = gloader->base.outline.points;
- FT_UInt num_points = (FT_UInt)gloader->base.outline.n_points;
- FT_Bool have_scale;
- FT_Pos x, y;
- have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE |
- WE_HAVE_AN_XY_SCALE |
- WE_HAVE_A_2X2 ) );
- /* perform the transform required for this subglyph */
- if ( have_scale )
- {
- FT_UInt i;
- for ( i = num_base_points; i < num_points; i++ )
- FT_Vector_Transform( base_vec + i, &subglyph->transform );
- }
- /* get offset */
- if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
- {
- FT_UInt k = (FT_UInt)subglyph->arg1;
- FT_UInt l = (FT_UInt)subglyph->arg2;
- FT_Vector* p1;
- FT_Vector* p2;
- /* match l-th point of the newly loaded component to the k-th point */
- /* of the previously loaded components. */
- /* change to the point numbers used by our outline */
- k += start_point;
- l += num_base_points;
- if ( k >= num_base_points ||
- l >= num_points )
- return FT_THROW( Invalid_Composite );
- p1 = gloader->base.outline.points + k;
- p2 = gloader->base.outline.points + l;
- x = p1->x - p2->x;
- y = p1->y - p2->y;
- }
- else
- {
- x = subglyph->arg1;
- y = subglyph->arg2;
- if ( !x && !y )
- return FT_Err_Ok;
- /* Use a default value dependent on */
- /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED. This is useful for old */
- /* TT fonts which don't set the xxx_COMPONENT_OFFSET bit. */
- if ( have_scale &&
- #ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED
- !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) )
- #else
- ( subglyph->flags & SCALED_COMPONENT_OFFSET ) )
- #endif
- {
- #if 0
- /*******************************************************************/
- /* */
- /* This algorithm is what Apple documents. But it doesn't work. */
- /* */
- int a = subglyph->transform.xx > 0 ? subglyph->transform.xx
- : -subglyph->transform.xx;
- int b = subglyph->transform.yx > 0 ? subglyph->transform.yx
- : -subglyph->transform.yx;
- int c = subglyph->transform.xy > 0 ? subglyph->transform.xy
- : -subglyph->transform.xy;
- int d = subglyph->transform.yy > 0 ? subglyph->transform.yy
- : -subglyph->transform.yy;
- int m = a > b ? a : b;
- int n = c > d ? c : d;
- if ( a - b <= 33 && a - b >= -33 )
- m *= 2;
- if ( c - d <= 33 && c - d >= -33 )
- n *= 2;
- x = FT_MulFix( x, m );
- y = FT_MulFix( y, n );
- #else /* 1 */
- /*******************************************************************/
- /* */
- /* This algorithm is a guess and works much better than the above. */
- /* */
- FT_Fixed mac_xscale = FT_Hypot( subglyph->transform.xx,
- subglyph->transform.xy );
- FT_Fixed mac_yscale = FT_Hypot( subglyph->transform.yy,
- subglyph->transform.yx );
- x = FT_MulFix( x, mac_xscale );
- y = FT_MulFix( y, mac_yscale );
- #endif /* 1 */
- }
- if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
- {
- FT_Fixed x_scale = ((TT_Size)loader->size)->metrics.x_scale;
- FT_Fixed y_scale = ((TT_Size)loader->size)->metrics.y_scale;
- x = FT_MulFix( x, x_scale );
- y = FT_MulFix( y, y_scale );
- if ( subglyph->flags & ROUND_XY_TO_GRID )
- {
- x = FT_PIX_ROUND( x );
- y = FT_PIX_ROUND( y );
- }
- }
- }
- if ( x || y )
- translate_array( num_points - num_base_points,
- base_vec + num_base_points,
- x, y );
- return FT_Err_Ok;
- }
- /*************************************************************************/
- /* */
- /* <Function> */
- /* TT_Process_Composite_Glyph */
- /* */
- /* <Description> */
- /* This is slightly different from TT_Process_Simple_Glyph, in that */
- /* its sole purpose is to hint the glyph. Thus this function is */
- /* only available when bytecode interpreter is enabled. */
- /* */
- static FT_Error
- TT_Process_Composite_Glyph( TT_Loader loader,
- FT_UInt start_point,
- FT_UInt start_contour )
- {
- FT_Error error;
- FT_Outline* outline;
- FT_UInt i;
- outline = &loader->gloader->base.outline;
- /* make room for phantom points */
- error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader,
- outline->n_points + 4,
- 0 );
- if ( error )
- return error;
- outline->points[outline->n_points ] = loader->pp1;
- outline->points[outline->n_points + 1] = loader->pp2;
- outline->points[outline->n_points + 2] = loader->pp3;
- outline->points[outline->n_points + 3] = loader->pp4;
- outline->tags[outline->n_points ] = 0;
- outline->tags[outline->n_points + 1] = 0;
- outline->tags[outline->n_points + 2] = 0;
- outline->tags[outline->n_points + 3] = 0;
- #ifdef TT_USE_BYTECODE_INTERPRETER
- {
- FT_Stream stream = loader->stream;
- FT_UShort n_ins, max_ins;
- FT_ULong tmp;
- /* TT_Load_Composite_Glyph only gives us the offset of instructions */
- /* so we read them here */
- if ( FT_STREAM_SEEK( loader->ins_pos ) ||
- FT_READ_USHORT( n_ins ) )
- return error;
- FT_TRACE5(( " Instructions size = %d\n", n_ins ));
- /* check it */
- max_ins = ((TT_Face)loader->face)->max_profile.maxSizeOfInstructions;
- if ( n_ins > max_ins )
- {
- /* don't trust `maxSizeOfInstructions'; */
- /* only do a rough safety check */
- if ( (FT_Int)n_ins > loader->byte_len )
- {
- FT_TRACE1(( "TT_Process_Composite_Glyph:"
- " too many instructions (%d) for glyph with length %d\n",
- n_ins, loader->byte_len ));
- return FT_THROW( Too_Many_Hints );
- }
- tmp = loader->exec->glyphSize;
- error = Update_Max( loader->exec->memory,
- &tmp,
- sizeof ( FT_Byte ),
- (void*)&loader->exec->glyphIns,
- n_ins );
- loader->exec->glyphSize = (FT_UShort)tmp;
- if ( error )
- return error;
- }
- else if ( n_ins == 0 )
- return FT_Err_Ok;
- if ( FT_STREAM_READ( loader->exec->glyphIns, n_ins ) )
- return error;
- loader->glyph->control_data = loader->exec->glyphIns;
- loader->glyph->control_len = n_ins;
- }
- #endif
- tt_prepare_zone( &loader->zone, &loader->gloader->base,
- start_point, start_contour );
- /* Some points are likely touched during execution of */
- /* instructions on components. So let's untouch them. */
- for ( i = 0; i < loader->zone.n_points; i++ )
- loader->zone.tags[i] &= ~FT_CURVE_TAG_TOUCH_BOTH;
- loader->zone.n_points += 4;
- return TT_Hint_Glyph( loader, 1 );
- }
- /*
- * Calculate the phantom points
- *
- * Defining the right side bearing (rsb) as
- *
- * rsb = aw - (lsb + xmax - xmin)
- *
- * (with `aw' the advance width, `lsb' the left side bearing, and `xmin'
- * and `xmax' the glyph's minimum and maximum x value), the OpenType
- * specification defines the initial position of horizontal phantom points
- * as
- *
- * pp1 = (round(xmin - lsb), 0) ,
- * pp2 = (round(pp1 + aw), 0) .
- *
- * Note that the rounding to the grid (in the device space) is not
- * documented currently in the specification.
- *
- * However, the specification lacks the precise definition of vertical
- * phantom points. Greg Hitchcock provided the following explanation.
- *
- * - a `vmtx' table is present
- *
- * For any glyph, the minimum and maximum y values (`ymin' and `ymax')
- * are given in the `glyf' table, the top side bearing (tsb) and advance
- * height (ah) are given in the `vmtx' table. The bottom side bearing
- * (bsb) is then calculated as
- *
- * bsb = ah - (tsb + ymax - ymin) ,
- *
- * and the initial position of vertical phantom points is
- *
- * pp3 = (x, round(ymax + tsb)) ,
- * pp4 = (x, round(pp3 - ah)) .
- *
- * See below for value `x'.
- *
- * - no `vmtx' table in the font
- *
- * If there is an `OS/2' table, we set
- *
- * DefaultAscender = sTypoAscender ,
- * DefaultDescender = sTypoDescender ,
- *
- * otherwise we use data from the `hhea' table:
- *
- * DefaultAscender = Ascender ,
- * DefaultDescender = Descender .
- *
- * With these two variables we can now set
- *
- * ah = DefaultAscender - sDefaultDescender ,
- * tsb = DefaultAscender - yMax ,
- *
- * and proceed as if a `vmtx' table was present.
- *
- * Usually we have
- *
- * x = aw / 2 , (1)
- *
- * but there is one compatibility case where it can be set to
- *
- * x = -DefaultDescender -
- * ((DefaultAscender - DefaultDescender - aw) / 2) . (2)
- *
- * and another one with
- *
- * x = 0 . (3)
- *
- * In Windows, the history of those values is quite complicated,
- * depending on the hinting engine (that is, the graphics framework).
- *
- * framework from to formula
- * ----------------------------------------------------------
- * GDI Windows 98 current (1)
- * (Windows 2000 for NT)
- * GDI+ Windows XP Windows 7 (2)
- * GDI+ Windows 8 current (3)
- * DWrite Windows 7 current (3)
- *
- * For simplicity, FreeType uses (1) for grayscale subpixel hinting and
- * (3) for everything else.
- *
- */
- #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
- #define TT_LOADER_SET_PP( loader ) \
- do \
- { \
- FT_Bool subpixel_hinting_ = loader->exec \
- ? loader->exec->subpixel_hinting \
- : 0; \
- FT_Bool grayscale_ = loader->exec \
- ? loader->exec->grayscale \
- : 0; \
- FT_Bool use_aw_2_ = (FT_Bool)( subpixel_hinting_ && \
- grayscale_ ); \
- \
- \
- (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
- (loader)->pp1.y = 0; \
- (loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \
- (loader)->pp2.y = 0; \
- \
- (loader)->pp3.x = use_aw_2_ ? (loader)->advance / 2 : 0; \
- (loader)->pp3.y = (loader)->bbox.yMax + (loader)->top_bearing; \
- (loader)->pp4.x = use_aw_2_ ? (loader)->advance / 2 : 0; \
- (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \
- } while ( 0 )
- #else /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */
- #define TT_LOADER_SET_PP( loader ) \
- do \
- { \
- (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
- (loader)->pp1.y = 0; \
- (loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \
- (loader)->pp2.y = 0; \
- \
- (loader)->pp3.x = 0; \
- (loader)->pp3.y = (loader)->bbox.yMax + (loader)->top_bearing; \
- (loader)->pp4.x = 0; \
- (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \
- } while ( 0 )
- #endif /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */
- /*************************************************************************/
- /* */
- /* <Function> */
- /* load_truetype_glyph */
- /* */
- /* <Description> */
- /* Loads a given truetype glyph. Handles composites and uses a */
- /* TT_Loader object. */
- /* */
- static FT_Error
- load_truetype_glyph( TT_Loader loader,
- FT_UInt glyph_index,
- FT_UInt recurse_count,
- FT_Bool header_only )
- {
- FT_Error error = FT_Err_Ok;
- FT_Fixed x_scale, y_scale;
- FT_ULong offset;
- TT_Face face = (TT_Face)loader->face;
- FT_GlyphLoader gloader = loader->gloader;
- FT_Bool opened_frame = 0;
- #ifdef FT_CONFIG_OPTION_INCREMENTAL
- FT_StreamRec inc_stream;
- FT_Data glyph_data;
- FT_Bool glyph_data_loaded = 0;
- #endif
- /* some fonts have an incorrect value of `maxComponentDepth', */
- /* thus we allow depth 1 to catch the majority of them */
- if ( recurse_count > 1 &&
- recurse_count > face->max_profile.maxComponentDepth )
- {
- error = FT_THROW( Invalid_Composite );
- goto Exit;
- }
- #ifndef FT_CONFIG_OPTION_INCREMENTAL
- /* check glyph index */
- if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
- {
- error = FT_THROW( Invalid_Glyph_Index );
- goto Exit;
- }
- #endif
- loader->glyph_index = glyph_index;
- if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
- {
- x_scale = ((TT_Size)loader->size)->metrics.x_scale;
- y_scale = ((TT_Size)loader->size)->metrics.y_scale;
- }
- else
- {
- x_scale = 0x10000L;
- y_scale = 0x10000L;
- }
- /* Set `offset' to the start of the glyph relative to the start of */
- /* the `glyf' table, and `byte_len' to the length of the glyph in */
- /* bytes. */
- #ifdef FT_CONFIG_OPTION_INCREMENTAL
- /* If we are loading glyph data via the incremental interface, set */
- /* the loader stream to a memory stream reading the data returned */
- /* by the interface. */
- if ( face->root.internal->incremental_interface )
- {
- error = face->root.internal->incremental_interface->funcs->get_glyph_data(
- face->root.internal->incremental_interface->object,
- glyph_index, &glyph_data );
- if ( error )
- goto Exit;
- glyph_data_loaded = 1;
- offset = 0;
- loader->byte_len = glyph_data.length;
- FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) );
- FT_Stream_OpenMemory( &inc_stream,
- glyph_data.pointer,
- (FT_ULong)glyph_data.length );
- loader->stream = &inc_stream;
- }
- else
- #endif /* FT_CONFIG_OPTION_INCREMENTAL */
- offset = tt_face_get_location( face, glyph_index,
- (FT_UInt*)&loader->byte_len );
- if ( loader->byte_len > 0 )
- {
- #ifdef FT_CONFIG_OPTION_INCREMENTAL
- /* for the incremental interface, `glyf_offset' is always zero */
- if ( !loader->glyf_offset &&
- !face->root.internal->incremental_interface )
- #else
- if ( !loader->glyf_offset )
- #endif /* FT_CONFIG_OPTION_INCREMENTAL */
- {
- FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" ));
- error = FT_THROW( Invalid_Table );
- goto Exit;
- }
- error = face->access_glyph_frame( loader, glyph_index,
- loader->glyf_offset + offset,
- (FT_UInt)loader->…
Large files files are truncated, but you can click here to view the full file