From a9af69148173f64e39ed6b1be1944a014ffb0d6c Mon Sep 17 00:00:00 2001 From: Werner Lemberg Date: Tue, 25 Sep 2018 09:10:09 +0200 Subject: [PATCH] Fix handing of `FT_Bool'. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit we had code like (FT_Bool)( globals->glyph_styles[gindex] & 0x8000) Since `FT_Bool' is defined to be an `unsigned char', the code evaluated to something like (unsigned char)( 0x8532 & 0x8000) which in turn expanded to (unsigned char)( 0x8000) and finally yielded 0x00 – i.e., false – not as expected. Problem reported and analyzed by Tony Smith . * include/freetype/fttypes.h (FT_BOOL): Add a comparison against zero so that we always have a Boolean expression. */*: Replace castings to `FT_Bool' with calls to `FT_BOOL' where possible. --- ChangeLog | 27 +++++++++++++++++++++++++++ include/freetype/fttypes.h | 2 +- src/autofit/afcjk.c | 2 +- src/autofit/afglobal.c | 4 ++-- src/autofit/afhints.c | 2 +- src/autofit/aflatin.c | 8 ++++---- src/autofit/aflatin2.c | 8 ++++---- src/base/ftobjs.c | 13 +++++++------ src/cff/cffgload.c | 2 +- src/cid/cidgload.c | 3 +-- src/pfr/pfrobjs.c | 4 ++-- src/psaux/cffdecode.c | 4 ++-- src/psaux/psfont.c | 4 ++-- src/psaux/pshints.c | 19 ++++++++----------- src/psaux/psintrp.c | 4 ++-- src/psaux/psread.c | 2 +- src/truetype/ttgload.c | 2 +- src/type1/t1gload.c | 11 +++++------ src/type1/t1load.c | 2 +- 19 files changed, 73 insertions(+), 50 deletions(-) diff --git a/ChangeLog b/ChangeLog index d53f4dfe9..a0a0b5cf4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,30 @@ +2018-09-25 Werner Lemberg + + Fix handing of `FT_Bool'. + + Before this commit we had code like + + (FT_Bool)( globals->glyph_styles[gindex] & 0x8000) + + Since `FT_Bool' is defined to be an `unsigned char', the code + evaluated to something like + + (unsigned char)( 0x8532 & 0x8000) + + which in turn expanded to + + (unsigned char)( 0x8000) + + and finally yielded 0x00 – i.e., false – not as expected. + + Problem reported and analyzed by Tony Smith . + + * include/freetype/fttypes.h (FT_BOOL): Add a comparison against + zero so that we always have a Boolean expression. + + */*: Replace castings to `FT_Bool' with calls to `FT_BOOL' where + possible. + 2018-09-23 Alexei Podtelezhnikov [bdf] Speed up charmap access. diff --git a/include/freetype/fttypes.h b/include/freetype/fttypes.h index df57986b5..b20879b62 100644 --- a/include/freetype/fttypes.h +++ b/include/freetype/fttypes.h @@ -588,7 +588,7 @@ FT_BEGIN_HEADER #define FT_IS_EMPTY( list ) ( (list).head == 0 ) -#define FT_BOOL( x ) ( (FT_Bool)( x ) ) +#define FT_BOOL( x ) ( (FT_Bool)( (x) != 0 ) ) /* concatenate C tokens */ #define FT_ERR_XCAT( x, y ) x ## y diff --git a/src/autofit/afcjk.c b/src/autofit/afcjk.c index ef02ed02f..d64eeea2a 100644 --- a/src/autofit/afcjk.c +++ b/src/autofit/afcjk.c @@ -1198,7 +1198,7 @@ /* check for links -- if seg->serif is set, then seg->link must */ /* be ignored */ - is_serif = (FT_Bool)( seg->serif && seg->serif->edge != edge ); + is_serif = FT_BOOL( seg->serif && seg->serif->edge != edge ); if ( seg->link || is_serif ) { diff --git a/src/autofit/afglobal.c b/src/autofit/afglobal.c index f40b5af51..b81de406a 100644 --- a/src/autofit/afglobal.c +++ b/src/autofit/afglobal.c @@ -489,9 +489,9 @@ FT_UInt gindex ) { if ( gindex < (FT_ULong)globals->glyph_count ) - return (FT_Bool)( globals->glyph_styles[gindex] & AF_DIGIT ); + return FT_BOOL( globals->glyph_styles[gindex] & AF_DIGIT ); - return (FT_Bool)0; + return FT_BOOL( 0 ); } diff --git a/src/autofit/afhints.c b/src/autofit/afhints.c index 1e8a57399..601d30f7e 100644 --- a/src/autofit/afhints.c +++ b/src/autofit/afhints.c @@ -549,7 +549,7 @@ *offset = ( dim == AF_DIMENSION_HORZ ) ? seg->first->fx : seg->first->fy; if ( seg->edge ) - *is_blue = (FT_Bool)( seg->edge->blue_edge != 0 ); + *is_blue = FT_BOOL( seg->edge->blue_edge ); else *is_blue = FALSE; diff --git a/src/autofit/aflatin.c b/src/autofit/aflatin.c index 21f666953..e2101b5e5 100644 --- a/src/autofit/aflatin.c +++ b/src/autofit/aflatin.c @@ -1307,7 +1307,7 @@ /* an extra-light axis corresponds to a standard width that is */ /* smaller than 5/8 pixels */ axis->extra_light = - (FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 ); + FT_BOOL( FT_MulFix( axis->standard_width, scale ) < 32 + 8 ); #ifdef FT_DEBUG_LEVEL_TRACE if ( axis->extra_light ) @@ -2328,9 +2328,9 @@ /* check for links -- if seg->serif is set, then seg->link must */ /* be ignored */ - is_serif = (FT_Bool)( seg->serif && - seg->serif->edge && - seg->serif->edge != edge ); + is_serif = FT_BOOL( seg->serif && + seg->serif->edge && + seg->serif->edge != edge ); if ( ( seg->link && seg->link->edge ) || is_serif ) { diff --git a/src/autofit/aflatin2.c b/src/autofit/aflatin2.c index 25767084a..0cfe26ef3 100644 --- a/src/autofit/aflatin2.c +++ b/src/autofit/aflatin2.c @@ -632,7 +632,7 @@ /* an extra-light axis corresponds to a standard width that is */ /* smaller than 5/8 pixels */ axis->extra_light = - (FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 ); + FT_BOOL( FT_MulFix( axis->standard_width, scale ) < 32 + 8 ); if ( dim == AF_DIMENSION_VERT ) { @@ -1302,9 +1302,9 @@ /* check for links -- if seg->serif is set, then seg->link must */ /* be ignored */ - is_serif = (FT_Bool)( seg->serif && - seg->serif->edge && - seg->serif->edge != edge ); + is_serif = FT_BOOL( seg->serif && + seg->serif->edge && + seg->serif->edge != edge ); if ( ( seg->link && seg->link->edge ) || is_serif ) { diff --git a/src/base/ftobjs.c b/src/base/ftobjs.c index ee865a8fb..9062e5e64 100644 --- a/src/base/ftobjs.c +++ b/src/base/ftobjs.c @@ -962,8 +962,9 @@ #ifdef GRID_FIT_METRICS if ( !( load_flags & FT_LOAD_NO_HINTING ) ) - ft_glyphslot_grid_fit_metrics( slot, - FT_BOOL( load_flags & FT_LOAD_VERTICAL_LAYOUT ) ); + ft_glyphslot_grid_fit_metrics( + slot, + FT_BOOL( load_flags & FT_LOAD_VERTICAL_LAYOUT ) ); #endif } } @@ -2736,8 +2737,8 @@ /* close the attached stream */ FT_Stream_Free( stream, - (FT_Bool)( parameters->stream && - ( parameters->flags & FT_OPEN_STREAM ) ) ); + FT_BOOL( parameters->stream && + ( parameters->flags & FT_OPEN_STREAM ) ) ); Exit: return error; @@ -5154,9 +5155,9 @@ service = (FT_Service_Properties)interface; if ( set ) - missing_func = (FT_Bool)( !service->set_property ); + missing_func = FT_BOOL( !service->set_property ); else - missing_func = (FT_Bool)( !service->get_property ); + missing_func = FT_BOOL( !service->get_property ); if ( missing_func ) { diff --git a/src/cff/cffgload.c b/src/cff/cffgload.c index c4d7ddbed..755ad13dd 100644 --- a/src/cff/cffgload.c +++ b/src/cff/cffgload.c @@ -414,7 +414,7 @@ decoder.width_only = TRUE; decoder.builder.no_recurse = - (FT_Bool)( load_flags & FT_LOAD_NO_RECURSE ); + FT_BOOL( load_flags & FT_LOAD_NO_RECURSE ); /* now load the unscaled outline */ error = cff_get_glyph_data( face, glyph_index, diff --git a/src/cid/cidgload.c b/src/cid/cidgload.c index 36ccee544..d683a6805 100644 --- a/src/cid/cidgload.c +++ b/src/cid/cidgload.c @@ -393,8 +393,7 @@ must_finish_decoder = TRUE; /* set up the decoder */ - decoder.builder.no_recurse = FT_BOOL( - ( ( load_flags & FT_LOAD_NO_RECURSE ) != 0 ) ); + decoder.builder.no_recurse = FT_BOOL( load_flags & FT_LOAD_NO_RECURSE ); error = cid_load_glyph( &decoder, glyph_index ); if ( error ) diff --git a/src/pfr/pfrobjs.c b/src/pfr/pfrobjs.c index c906c90bc..45c71b240 100644 --- a/src/pfr/pfrobjs.c +++ b/src/pfr/pfrobjs.c @@ -122,7 +122,7 @@ stream, (FT_UInt)( face_index & 0xFFFF ), face->header.log_dir_offset, - FT_BOOL( face->header.phy_font_max_size_high != 0 ) ); + FT_BOOL( face->header.phy_font_max_size_high ) ); if ( error ) goto Exit; @@ -370,7 +370,7 @@ FT_Bool scaling; - scaling = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 ); + scaling = FT_BOOL( !( load_flags & FT_LOAD_NO_SCALE ) ); /* copy outline data */ *outline = slot->glyph.loader->base.outline; diff --git a/src/psaux/cffdecode.c b/src/psaux/cffdecode.c index 1641ccd71..0576ca6ee 100644 --- a/src/psaux/cffdecode.c +++ b/src/psaux/cffdecode.c @@ -1568,7 +1568,7 @@ x = ADD_LONG( x, args[0] ); y = ADD_LONG( y, args[1] ); cff_builder_add_point( builder, x, y, - (FT_Bool)( count == 3 ) ); + FT_BOOL( count == 3 ) ); args += 2; } @@ -1606,7 +1606,7 @@ x = ADD_LONG( x, args[0] ); y = ADD_LONG( y, args[1] ); cff_builder_add_point( builder, x, y, - (FT_Bool)( count == 4 || count == 1 ) ); + FT_BOOL( count == 4 || count == 1 ) ); args += 2; } diff --git a/src/psaux/psfont.c b/src/psaux/psfont.c index a7e743d7f..bb5faa38f 100644 --- a/src/psaux/psfont.c +++ b/src/psaux/psfont.c @@ -331,7 +331,7 @@ } /* copy hinted flag on each call */ - font->hinted = (FT_Bool)( font->renderingFlags & CF2_FlagsHinted ); + font->hinted = FT_BOOL( font->renderingFlags & CF2_FlagsHinted ); /* determine if transform has changed; */ /* include Fontmatrix but ignore translation */ @@ -366,7 +366,7 @@ if ( font->stemDarkened != ( font->renderingFlags & CF2_FlagsDarkened ) ) { font->stemDarkened = - (FT_Bool)( font->renderingFlags & CF2_FlagsDarkened ); + FT_BOOL( font->renderingFlags & CF2_FlagsDarkened ); /* blue zones depend on darkened flag */ needExtraSetup = TRUE; diff --git a/src/psaux/pshints.c b/src/psaux/pshints.c index 1965b2cd7..816c3cc3b 100644 --- a/src/psaux/pshints.c +++ b/src/psaux/pshints.c @@ -217,52 +217,49 @@ FT_LOCAL_DEF( FT_Bool ) cf2_hint_isValid( const CF2_Hint hint ) { - return (FT_Bool)( hint->flags != 0 ); + return FT_BOOL( hint->flags ); } static FT_Bool cf2_hint_isPair( const CF2_Hint hint ) { - return (FT_Bool)( ( hint->flags & - ( CF2_PairBottom | CF2_PairTop ) ) != 0 ); + return FT_BOOL( hint->flags & ( CF2_PairBottom | CF2_PairTop ) ); } static FT_Bool cf2_hint_isPairTop( const CF2_Hint hint ) { - return (FT_Bool)( ( hint->flags & CF2_PairTop ) != 0 ); + return FT_BOOL( hint->flags & CF2_PairTop ); } FT_LOCAL_DEF( FT_Bool ) cf2_hint_isTop( const CF2_Hint hint ) { - return (FT_Bool)( ( hint->flags & - ( CF2_PairTop | CF2_GhostTop ) ) != 0 ); + return FT_BOOL( hint->flags & ( CF2_PairTop | CF2_GhostTop ) ); } FT_LOCAL_DEF( FT_Bool ) cf2_hint_isBottom( const CF2_Hint hint ) { - return (FT_Bool)( ( hint->flags & - ( CF2_PairBottom | CF2_GhostBottom ) ) != 0 ); + return FT_BOOL( hint->flags & ( CF2_PairBottom | CF2_GhostBottom ) ); } static FT_Bool cf2_hint_isLocked( const CF2_Hint hint ) { - return (FT_Bool)( ( hint->flags & CF2_Locked ) != 0 ); + return FT_BOOL( hint->flags & CF2_Locked ); } static FT_Bool cf2_hint_isSynthetic( const CF2_Hint hint ) { - return (FT_Bool)( ( hint->flags & CF2_Synthetic ) != 0 ); + return FT_BOOL( hint->flags & CF2_Synthetic ); } @@ -497,7 +494,7 @@ { move = moveDown; /* true if non-optimum move */ - saveEdge = (FT_Bool)( moveUp < -moveDown ); + saveEdge = FT_BOOL( moveUp < -moveDown ); } else { diff --git a/src/psaux/psintrp.c b/src/psaux/psintrp.c index b63bcd4e8..712e8782d 100644 --- a/src/psaux/psintrp.c +++ b/src/psaux/psintrp.c @@ -287,7 +287,7 @@ { CF2_UInt i; CF2_UInt count = cf2_stack_count( opStack ); - FT_Bool hasWidthArg = (FT_Bool)( count & 1 ); + FT_Bool hasWidthArg = FT_BOOL( count & 1 ); /* variable accumulates delta values from operand stack */ CF2_Fixed position = hintOffset; @@ -364,7 +364,7 @@ if ( doConditionalLastRead ) { - FT_Bool lastIsX = (FT_Bool)( + FT_Bool lastIsX = FT_BOOL( cf2_fixedAbs( SUB_INT32( vals[10], *curX ) ) > cf2_fixedAbs( SUB_INT32( vals[11], *curY ) ) ); CF2_Fixed lastVal = cf2_stack_getReal( opStack, idx ); diff --git a/src/psaux/psread.c b/src/psaux/psread.c index 6f3a361e0..86bfc03c6 100644 --- a/src/psaux/psread.c +++ b/src/psaux/psread.c @@ -105,7 +105,7 @@ FT_LOCAL_DEF( FT_Bool ) cf2_buf_isEnd( CF2_Buffer buf ) { - return (FT_Bool)( buf->ptr >= buf->end ); + return FT_BOOL( buf->ptr >= buf->end ); } diff --git a/src/truetype/ttgload.c b/src/truetype/ttgload.c index d54626ddd..f2d20bd57 100644 --- a/src/truetype/ttgload.c +++ b/src/truetype/ttgload.c @@ -1479,7 +1479,7 @@ } #endif - use_aw_2 = (FT_Bool)( subpixel_hinting && grayscale ); + use_aw_2 = FT_BOOL( subpixel_hinting && grayscale ); loader->pp1.x = loader->bbox.xMin - loader->left_bearing; loader->pp1.y = 0; diff --git a/src/type1/t1gload.c b/src/type1/t1gload.c index e1b10ddba..75062ee83 100644 --- a/src/type1/t1gload.c +++ b/src/type1/t1gload.c @@ -402,9 +402,9 @@ t1glyph->outline.n_points = 0; t1glyph->outline.n_contours = 0; - hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 && - ( load_flags & FT_LOAD_NO_HINTING ) == 0 ); - scaled = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 ); + hinting = FT_BOOL( !( load_flags & FT_LOAD_NO_SCALE ) && + !( load_flags & FT_LOAD_NO_HINTING ) ); + scaled = FT_BOOL( !( load_flags & FT_LOAD_NO_SCALE ) ); glyph->hint = hinting; glyph->scaled = scaled; @@ -416,7 +416,7 @@ t1glyph, (FT_Byte**)type1->glyph_names, face->blend, - FT_BOOL( hinting ), + hinting, FT_LOAD_TARGET_MODE( load_flags ), T1_Parse_Glyph ); if ( error ) @@ -424,8 +424,7 @@ must_finish_decoder = TRUE; - decoder.builder.no_recurse = FT_BOOL( - ( load_flags & FT_LOAD_NO_RECURSE ) != 0 ); + decoder.builder.no_recurse = FT_BOOL( load_flags & FT_LOAD_NO_RECURSE ); decoder.num_subrs = type1->num_subrs; decoder.subrs = type1->subrs; diff --git a/src/type1/t1load.c b/src/type1/t1load.c index b16389ab7..b33739b08 100644 --- a/src/type1/t1load.c +++ b/src/type1/t1load.c @@ -73,7 +73,7 @@ #ifdef FT_CONFIG_OPTION_INCREMENTAL -#define IS_INCREMENTAL (FT_Bool)( face->root.internal->incremental_interface != 0 ) +#define IS_INCREMENTAL FT_BOOL( face->root.internal->incremental_interface ) #else #define IS_INCREMENTAL 0 #endif