[truetype] Fix `mmvar' array pointers, part 2.

The previous commit was incomplete.

* src/truetype/ttgxvar.c: Properly initialize sub-array offsets for
`master' also.
This commit is contained in:
Werner Lemberg 2017-09-21 11:02:35 +02:00
parent 3b3cb32dd2
commit b19cdc9c81
2 changed files with 76 additions and 58 deletions

View File

@ -1,3 +1,12 @@
2017-09-21 Werner Lemberg <wl@gnu.org>
[truetype] Fix `mmvar' array pointers, part 2.
The previous commit was incomplete.
* src/truetype/ttgxvar.c: Properly initialize sub-array offsets for
`master' also.
2017-09-21 Werner Lemberg <wl@gnu.org> 2017-09-21 Werner Lemberg <wl@gnu.org>
[truetype] Fix `mmvar' array pointers. [truetype] Fix `mmvar' array pointers.

View File

@ -1940,11 +1940,11 @@
TT_Get_MM_Var( TT_Face face, TT_Get_MM_Var( TT_Face face,
FT_MM_Var* *master ) FT_MM_Var* *master )
{ {
FT_Stream stream = face->root.stream; FT_Stream stream = face->root.stream;
FT_Memory memory = face->root.memory; FT_Memory memory = face->root.memory;
FT_ULong table_len; FT_ULong table_len;
FT_Error error = FT_Err_Ok; FT_Error error = FT_Err_Ok;
FT_ULong fvar_start; FT_ULong fvar_start = 0;
FT_UInt i, j; FT_UInt i, j;
FT_MM_Var* mmvar = NULL; FT_MM_Var* mmvar = NULL;
FT_Fixed* next_coords; FT_Fixed* next_coords;
@ -1954,10 +1954,20 @@
FT_Fixed* c; FT_Fixed* c;
FT_Var_Named_Style* ns; FT_Var_Named_Style* ns;
GX_FVar_Head fvar_head; GX_FVar_Head fvar_head;
FT_Bool usePsName; FT_Bool usePsName = 0;
FT_UInt num_instances; FT_UInt num_instances;
FT_UInt num_axes;
FT_UShort* axis_flags; FT_UShort* axis_flags;
FT_Offset mmvar_size;
FT_Offset axis_flags_size;
FT_Offset axis_size;
FT_Offset namedstyle_size;
FT_Offset next_coords_size;
FT_Offset next_name_size;
FT_Bool need_init;
static const FT_Frame_Field fvar_fields[] = static const FT_Frame_Field fvar_fields[] =
{ {
@ -1995,16 +2005,10 @@
/* read the font data and set up the internal representation */ /* read the font data and set up the internal representation */
/* if not already done */ /* if not already done */
if ( !face->blend ) need_init = !face->blend;
if ( need_init )
{ {
FT_Offset mmvar_size;
FT_Offset axis_flags_size;
FT_Offset axis_size;
FT_Offset namedstyle_size;
FT_Offset next_coords_size;
FT_Offset next_name_size;
FT_TRACE2(( "FVAR " )); FT_TRACE2(( "FVAR " ));
/* both `fvar' and `gvar' must be present */ /* both `fvar' and `gvar' must be present */
@ -2045,36 +2049,43 @@
if ( FT_NEW( face->blend ) ) if ( FT_NEW( face->blend ) )
goto Exit; goto Exit;
/* `num_instances' holds the number of all named instances, */ num_axes = fvar_head.axisCount;
/* including the default instance which might be missing */ }
/* in fvar's table of named instances */ else
num_instances = face->root.style_flags >> 16; num_axes = face->blend->num_axis;
/* prepare storage area for MM data; this cannot overflow */ /* `num_instances' holds the number of all named instances, */
/* 32-bit arithmetic because of the size limits used in the */ /* including the default instance which might be missing */
/* `fvar' table validity check in `sfnt_init_face' */ /* in fvar's table of named instances */
num_instances = face->root.style_flags >> 16;
/* the various `*_size' variables, which we also use as */ /* prepare storage area for MM data; this cannot overflow */
/* offsets into the `mmlen' array, must be multiples of the */ /* 32-bit arithmetic because of the size limits used in the */
/* pointer size (except the last one); without such an */ /* `fvar' table validity check in `sfnt_init_face' */
/* alignment there might be runtime errors due to */
/* misaligned addresses */ /* the various `*_size' variables, which we also use as */
/* offsets into the `mmlen' array, must be multiples of the */
/* pointer size (except the last one); without such an */
/* alignment there might be runtime errors due to */
/* misaligned addresses */
#undef ALIGN_SIZE #undef ALIGN_SIZE
#define ALIGN_SIZE( n ) \ #define ALIGN_SIZE( n ) \
( ( (n) + sizeof (void*) - 1 ) & ~( sizeof (void*) - 1 ) ) ( ( (n) + sizeof (void*) - 1 ) & ~( sizeof (void*) - 1 ) )
mmvar_size = ALIGN_SIZE( sizeof ( FT_MM_Var ) ); mmvar_size = ALIGN_SIZE( sizeof ( FT_MM_Var ) );
axis_flags_size = ALIGN_SIZE( fvar_head.axisCount * axis_flags_size = ALIGN_SIZE( num_axes *
sizeof ( FT_UShort ) ); sizeof ( FT_UShort ) );
axis_size = ALIGN_SIZE( fvar_head.axisCount * axis_size = ALIGN_SIZE( num_axes *
sizeof ( FT_Var_Axis ) ); sizeof ( FT_Var_Axis ) );
namedstyle_size = ALIGN_SIZE( num_instances * namedstyle_size = ALIGN_SIZE( num_instances *
sizeof ( FT_Var_Named_Style ) ); sizeof ( FT_Var_Named_Style ) );
next_coords_size = ALIGN_SIZE( num_instances * next_coords_size = ALIGN_SIZE( num_instances *
fvar_head.axisCount * num_axes *
sizeof ( FT_Fixed ) ); sizeof ( FT_Fixed ) );
next_name_size = fvar_head.axisCount * 5; next_name_size = num_axes * 5;
if ( need_init )
{
face->blend->mmvar_len = mmvar_size + face->blend->mmvar_len = mmvar_size +
axis_flags_size + axis_flags_size +
axis_size + axis_size +
@ -2090,7 +2101,7 @@
/* the data gets filled in later on */ /* the data gets filled in later on */
mmvar->num_axis = mmvar->num_axis =
fvar_head.axisCount; num_axes;
mmvar->num_designs = mmvar->num_designs =
~0U; /* meaningless in this context; each glyph */ ~0U; /* meaningless in this context; each glyph */
/* may have a different number of designs */ /* may have a different number of designs */
@ -2111,12 +2122,12 @@
for ( i = 0; i < num_instances; i++ ) for ( i = 0; i < num_instances; i++ )
{ {
mmvar->namedstyle[i].coords = next_coords; mmvar->namedstyle[i].coords = next_coords;
next_coords += fvar_head.axisCount; next_coords += num_axes;
} }
next_name = (FT_String*)( (char*)mmvar->namedstyle + next_name = (FT_String*)( (char*)mmvar->namedstyle +
namedstyle_size + next_coords_size ); namedstyle_size + next_coords_size );
for ( i = 0; i < fvar_head.axisCount; i++ ) for ( i = 0; i < num_axes; i++ )
{ {
mmvar->axis[i].name = next_name; mmvar->axis[i].name = next_name;
next_name += 5; next_name += 5;
@ -2128,7 +2139,7 @@
goto Exit; goto Exit;
a = mmvar->axis; a = mmvar->axis;
for ( i = 0; i < fvar_head.axisCount; i++ ) for ( i = 0; i < num_axes; i++ )
{ {
GX_FVar_Axis axis_rec; GX_FVar_Axis axis_rec;
@ -2178,7 +2189,7 @@
/* named instance coordinates are stored as design coordinates; */ /* named instance coordinates are stored as design coordinates; */
/* we have to convert them to normalized coordinates also */ /* we have to convert them to normalized coordinates also */
if ( FT_NEW_ARRAY( face->blend->normalized_stylecoords, if ( FT_NEW_ARRAY( face->blend->normalized_stylecoords,
fvar_head.axisCount * num_instances ) ) num_axes * num_instances ) )
goto Exit; goto Exit;
if ( fvar_head.instanceCount && !face->blend->avar_loaded ) if ( fvar_head.instanceCount && !face->blend->avar_loaded )
@ -2198,14 +2209,14 @@
{ {
/* PostScript names add 2 bytes to the instance record size */ /* PostScript names add 2 bytes to the instance record size */
if ( FT_FRAME_ENTER( ( usePsName ? 6L : 4L ) + if ( FT_FRAME_ENTER( ( usePsName ? 6L : 4L ) +
4L * fvar_head.axisCount ) ) 4L * num_axes ) )
goto Exit; goto Exit;
ns->strid = FT_GET_USHORT(); ns->strid = FT_GET_USHORT();
(void) /* flags = */ FT_GET_USHORT(); (void) /* flags = */ FT_GET_USHORT();
c = ns->coords; c = ns->coords;
for ( j = 0; j < fvar_head.axisCount; j++, c++ ) for ( j = 0; j < num_axes; j++, c++ )
*c = FT_GET_LONG(); *c = FT_GET_LONG();
/* valid psid values are 6, [256;32767], and 0xFFFF */ /* valid psid values are 6, [256;32767], and 0xFFFF */
@ -2214,11 +2225,8 @@
else else
ns->psid = 0xFFFF; ns->psid = 0xFFFF;
ft_var_to_normalized( face, ft_var_to_normalized( face, num_axes, ns->coords, nsc );
fvar_head.axisCount, nsc += num_axes;
ns->coords,
nsc );
nsc += fvar_head.axisCount;
FT_FRAME_EXIT(); FT_FRAME_EXIT();
} }
@ -2267,7 +2275,7 @@
a = mmvar->axis; a = mmvar->axis;
c = ns->coords; c = ns->coords;
for ( j = 0; j < fvar_head.axisCount; j++, a++, c++ ) for ( j = 0; j < num_axes; j++, a++, c++ )
*c = a->def; *c = a->def;
} }
} }
@ -2288,23 +2296,24 @@
FT_MEM_COPY( mmvar, face->blend->mmvar, face->blend->mmvar_len ); FT_MEM_COPY( mmvar, face->blend->mmvar, face->blend->mmvar_len );
axis_flags = axis_flags =
(FT_UShort*)&( mmvar[1] ); (FT_UShort*)( (char*)mmvar + mmvar_size );
mmvar->axis = mmvar->axis =
(FT_Var_Axis*)&( axis_flags[mmvar->num_axis] ); (FT_Var_Axis*)( (char*)axis_flags + axis_flags_size );
mmvar->namedstyle = mmvar->namedstyle =
(FT_Var_Named_Style*)&( mmvar->axis[mmvar->num_axis] ); (FT_Var_Named_Style*)( (char*)mmvar->axis+ axis_size );
next_coords = next_coords = (FT_Fixed*)( (char*)mmvar->namedstyle +
(FT_Fixed*)&( mmvar->namedstyle[mmvar->num_namedstyles] ); namedstyle_size );
for ( n = 0; n < mmvar->num_namedstyles; n++ ) for ( n = 0; n < mmvar->num_namedstyles; n++ )
{ {
mmvar->namedstyle[n].coords = next_coords; mmvar->namedstyle[n].coords = next_coords;
next_coords += mmvar->num_axis; next_coords += num_axes;
} }
a = mmvar->axis; a = mmvar->axis;
next_name = (FT_String*)next_coords; next_name = (FT_String*)( (char*)mmvar->namedstyle +
for ( n = 0; n < mmvar->num_axis; n++ ) namedstyle_size + next_coords_size );
for ( n = 0; n < num_axes; n++ )
{ {
a->name = next_name; a->name = next_name;