1 /***************************************************************************/
5 /* TrueType GX Font Variation loader */
7 /* Copyright 2004-2012 by */
8 /* David Turner, Robert Wilhelm, Werner Lemberg, and George Williams. */
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. */
16 /***************************************************************************/
19 /*************************************************************************/
21 /* Apple documents the `fvar', `gvar', `cvar', and `avar' tables at */
23 /* http://developer.apple.com/fonts/TTRefMan/RM06/Chap6[fgca]var.html */
25 /* The documentation for `fvar' is inconsistent. At one point it says */
26 /* that `countSizePairs' should be 3, at another point 2. It should */
29 /* The documentation for `gvar' is not intelligible; `cvar' refers you */
30 /* to `gvar' and is thus also incomprehensible. */
32 /* The documentation for `avar' appears correct, but Apple has no fonts */
33 /* with an `avar' table, so it is hard to test. */
35 /* Many thanks to John Jenkins (at Apple) in figuring this out. */
38 /* Apple's `kern' table has some references to tuple indices, but as */
39 /* there is no indication where these indices are defined, nor how to */
40 /* interpolate the kerning values (different tuples have different */
41 /* classes) this issue is ignored. */
43 /*************************************************************************/
47 #include FT_INTERNAL_DEBUG_H
48 #include FT_CONFIG_CONFIG_H
49 #include FT_INTERNAL_STREAM_H
50 #include FT_INTERNAL_SFNT_H
51 #include FT_TRUETYPE_TAGS_H
52 #include FT_MULTIPLE_MASTERS_H
60 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
63 #define FT_Stream_FTell( stream ) \
64 ( (stream)->cursor - (stream)->base )
65 #define FT_Stream_SeekSet( stream, off ) \
66 ( (stream)->cursor = (stream)->base+(off) )
69 /*************************************************************************/
71 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
72 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
73 /* messages during execution. */
76 #define FT_COMPONENT trace_ttgxvar
79 /*************************************************************************/
80 /*************************************************************************/
82 /***** Internal Routines *****/
84 /*************************************************************************/
85 /*************************************************************************/
88 /*************************************************************************/
90 /* The macro ALL_POINTS is used in `ft_var_readpackedpoints'. It */
91 /* indicates that there is a delta for every point without needing to */
92 /* enumerate all of them. */
94 #define ALL_POINTS (FT_UShort*)( ~0 )
97 #define GX_PT_POINTS_ARE_WORDS 0x80
98 #define GX_PT_POINT_RUN_COUNT_MASK 0x7F
101 /*************************************************************************/
104 /* ft_var_readpackedpoints */
107 /* Read a set of points to which the following deltas will apply. */
108 /* Points are packed with a run length encoding. */
111 /* stream :: The data stream. */
114 /* point_cnt :: The number of points read. A zero value means that */
115 /* all points in the glyph will be affected, without */
116 /* enumerating them individually. */
119 /* An array of FT_UShort containing the affected points or the */
120 /* special value ALL_POINTS. */
123 ft_var_readpackedpoints( FT_Stream stream,
126 FT_UShort *points = NULL;
132 FT_Memory memory = stream->memory;
133 FT_Error error = TT_Err_Ok;
138 *point_cnt = n = FT_GET_BYTE();
142 if ( n & GX_PT_POINTS_ARE_WORDS )
143 n = FT_GET_BYTE() | ( ( n & GX_PT_POINT_RUN_COUNT_MASK ) << 8 );
145 if ( FT_NEW_ARRAY( points, n ) )
151 runcnt = FT_GET_BYTE();
152 if ( runcnt & GX_PT_POINTS_ARE_WORDS )
154 runcnt = runcnt & GX_PT_POINT_RUN_COUNT_MASK;
155 first = points[i++] = FT_GET_USHORT();
157 if ( runcnt < 1 || i + runcnt >= n )
160 /* first point not included in runcount */
161 for ( j = 0; j < runcnt; ++j )
162 points[i++] = (FT_UShort)( first += FT_GET_USHORT() );
166 first = points[i++] = FT_GET_BYTE();
168 if ( runcnt < 1 || i + runcnt >= n )
171 for ( j = 0; j < runcnt; ++j )
172 points[i++] = (FT_UShort)( first += FT_GET_BYTE() );
183 GX_DT_DELTAS_ARE_ZERO = 0x80,
184 GX_DT_DELTAS_ARE_WORDS = 0x40,
185 GX_DT_DELTA_RUN_COUNT_MASK = 0x3F
189 /*************************************************************************/
192 /* ft_var_readpackeddeltas */
195 /* Read a set of deltas. These are packed slightly differently than */
196 /* points. In particular there is no overall count. */
199 /* stream :: The data stream. */
201 /* delta_cnt :: The number of to be read. */
204 /* An array of FT_Short containing the deltas for the affected */
205 /* points. (This only gets the deltas for one dimension. It will */
206 /* generally be called twice, once for x, once for y. When used in */
207 /* cvt table, it will only be called once.) */
210 ft_var_readpackeddeltas( FT_Stream stream,
211 FT_Offset delta_cnt )
213 FT_Short *deltas = NULL;
217 FT_Memory memory = stream->memory;
218 FT_Error error = TT_Err_Ok;
223 if ( FT_NEW_ARRAY( deltas, delta_cnt ) )
227 while ( i < delta_cnt )
229 runcnt = FT_GET_BYTE();
230 if ( runcnt & GX_DT_DELTAS_ARE_ZERO )
232 /* runcnt zeroes get added */
234 j <= ( runcnt & GX_DT_DELTA_RUN_COUNT_MASK ) && i < delta_cnt;
238 else if ( runcnt & GX_DT_DELTAS_ARE_WORDS )
240 /* runcnt shorts from the stack */
242 j <= ( runcnt & GX_DT_DELTA_RUN_COUNT_MASK ) && i < delta_cnt;
244 deltas[i++] = FT_GET_SHORT();
248 /* runcnt signed bytes from the stack */
250 j <= ( runcnt & GX_DT_DELTA_RUN_COUNT_MASK ) && i < delta_cnt;
252 deltas[i++] = FT_GET_CHAR();
255 if ( j <= ( runcnt & GX_DT_DELTA_RUN_COUNT_MASK ) )
267 /*************************************************************************/
270 /* ft_var_load_avar */
273 /* Parse the `avar' table if present. It need not be, so we return */
277 /* face :: The font face. */
280 ft_var_load_avar( TT_Face face )
282 FT_Stream stream = FT_FACE_STREAM(face);
283 FT_Memory memory = stream->memory;
284 GX_Blend blend = face->blend;
285 GX_AVarSegment segment;
286 FT_Error error = TT_Err_Ok;
295 blend->avar_checked = TRUE;
296 if ( (error = face->goto_table( face, TTAG_avar, stream, &table_len )) != 0 )
299 if ( FT_FRAME_ENTER( table_len ) )
302 version = FT_GET_LONG();
303 axisCount = FT_GET_LONG();
305 if ( version != 0x00010000L ||
306 axisCount != (FT_Long)blend->mmvar->num_axis )
309 if ( FT_NEW_ARRAY( blend->avar_segment, axisCount ) )
312 segment = &blend->avar_segment[0];
313 for ( i = 0; i < axisCount; ++i, ++segment )
315 segment->pairCount = FT_GET_USHORT();
316 if ( FT_NEW_ARRAY( segment->correspondence, segment->pairCount ) )
318 /* Failure. Free everything we have done so far. We must do */
319 /* it right now since loading the `avar' table is optional. */
321 for ( j = i - 1; j >= 0; --j )
322 FT_FREE( blend->avar_segment[j].correspondence );
324 FT_FREE( blend->avar_segment );
325 blend->avar_segment = NULL;
329 for ( j = 0; j < segment->pairCount; ++j )
331 segment->correspondence[j].fromCoord =
332 FT_GET_SHORT() << 2; /* convert to Fixed */
333 segment->correspondence[j].toCoord =
334 FT_GET_SHORT()<<2; /* convert to Fixed */
343 typedef struct GX_GVar_Head_
347 FT_UShort globalCoordCount;
348 FT_ULong offsetToCoord;
349 FT_UShort glyphCount;
351 FT_ULong offsetToData;
356 /*************************************************************************/
359 /* ft_var_load_gvar */
362 /* Parses the `gvar' table if present. If `fvar' is there, `gvar' */
363 /* had better be there too. */
366 /* face :: The font face. */
369 /* FreeType error code. 0 means success. */
372 ft_var_load_gvar( TT_Face face )
374 FT_Stream stream = FT_FACE_STREAM(face);
375 FT_Memory memory = stream->memory;
376 GX_Blend blend = face->blend;
381 FT_ULong offsetToData;
382 GX_GVar_Head gvar_head;
384 static const FT_Frame_Field gvar_fields[] =
388 #define FT_STRUCTURE GX_GVar_Head
390 FT_FRAME_START( 20 ),
391 FT_FRAME_LONG ( version ),
392 FT_FRAME_USHORT( axisCount ),
393 FT_FRAME_USHORT( globalCoordCount ),
394 FT_FRAME_ULONG ( offsetToCoord ),
395 FT_FRAME_USHORT( glyphCount ),
396 FT_FRAME_USHORT( flags ),
397 FT_FRAME_ULONG ( offsetToData ),
401 if ( (error = face->goto_table( face, TTAG_gvar, stream, &table_len )) != 0 )
404 gvar_start = FT_STREAM_POS( );
405 if ( FT_STREAM_READ_FIELDS( gvar_fields, &gvar_head ) )
408 blend->tuplecount = gvar_head.globalCoordCount;
409 blend->gv_glyphcnt = gvar_head.glyphCount;
410 offsetToData = gvar_start + gvar_head.offsetToData;
412 if ( gvar_head.version != (FT_Long)0x00010000L ||
413 gvar_head.axisCount != (FT_UShort)blend->mmvar->num_axis )
415 error = TT_Err_Invalid_Table;
419 if ( FT_NEW_ARRAY( blend->glyphoffsets, blend->gv_glyphcnt + 1 ) )
422 if ( gvar_head.flags & 1 )
424 /* long offsets (one more offset than glyphs, to mark size of last) */
425 if ( FT_FRAME_ENTER( ( blend->gv_glyphcnt + 1 ) * 4L ) )
428 for ( i = 0; i <= blend->gv_glyphcnt; ++i )
429 blend->glyphoffsets[i] = offsetToData + FT_GET_LONG();
435 /* short offsets (one more offset than glyphs, to mark size of last) */
436 if ( FT_FRAME_ENTER( ( blend->gv_glyphcnt + 1 ) * 2L ) )
439 for ( i = 0; i <= blend->gv_glyphcnt; ++i )
440 blend->glyphoffsets[i] = offsetToData + FT_GET_USHORT() * 2;
441 /* XXX: Undocumented: `*2'! */
446 if ( blend->tuplecount != 0 )
448 if ( FT_NEW_ARRAY( blend->tuplecoords,
449 gvar_head.axisCount * blend->tuplecount ) )
452 if ( FT_STREAM_SEEK( gvar_start + gvar_head.offsetToCoord ) ||
453 FT_FRAME_ENTER( blend->tuplecount * gvar_head.axisCount * 2L ) )
456 for ( i = 0; i < blend->tuplecount; ++i )
457 for ( j = 0 ; j < (FT_UInt)gvar_head.axisCount; ++j )
458 blend->tuplecoords[i * gvar_head.axisCount + j] =
459 FT_GET_SHORT() << 2; /* convert to FT_Fixed */
469 /*************************************************************************/
472 /* ft_var_apply_tuple */
475 /* Figure out whether a given tuple (design) applies to the current */
476 /* blend, and if so, what is the scaling factor. */
479 /* blend :: The current blend of the font. */
481 /* tupleIndex :: A flag saying whether this is an intermediate */
484 /* tuple_coords :: The coordinates of the tuple in normalized axis */
487 /* im_start_coords :: The initial coordinates where this tuple starts */
488 /* to apply (for intermediate coordinates). */
490 /* im_end_coords :: The final coordinates after which this tuple no */
491 /* longer applies (for intermediate coordinates). */
494 /* An FT_Fixed value containing the scaling factor. */
497 ft_var_apply_tuple( GX_Blend blend,
498 FT_UShort tupleIndex,
499 FT_Fixed* tuple_coords,
500 FT_Fixed* im_start_coords,
501 FT_Fixed* im_end_coords )
504 FT_Fixed apply = 0x10000L;
507 for ( i = 0; i < blend->num_axis; ++i )
509 if ( tuple_coords[i] == 0 )
510 /* It's not clear why (for intermediate tuples) we don't need */
511 /* to check against start/end -- the documentation says we don't. */
512 /* Similarly, it's unclear why we don't need to scale along the */
516 else if ( blend->normalizedcoords[i] == 0 ||
517 ( blend->normalizedcoords[i] < 0 && tuple_coords[i] > 0 ) ||
518 ( blend->normalizedcoords[i] > 0 && tuple_coords[i] < 0 ) )
524 else if ( !( tupleIndex & GX_TI_INTERMEDIATE_TUPLE ) )
525 /* not an intermediate tuple */
526 apply = FT_MulFix( apply,
527 blend->normalizedcoords[i] > 0
528 ? blend->normalizedcoords[i]
529 : -blend->normalizedcoords[i] );
531 else if ( blend->normalizedcoords[i] <= im_start_coords[i] ||
532 blend->normalizedcoords[i] >= im_end_coords[i] )
538 else if ( blend->normalizedcoords[i] < tuple_coords[i] )
539 apply = FT_MulDiv( apply,
540 blend->normalizedcoords[i] - im_start_coords[i],
541 tuple_coords[i] - im_start_coords[i] );
544 apply = FT_MulDiv( apply,
545 im_end_coords[i] - blend->normalizedcoords[i],
546 im_end_coords[i] - tuple_coords[i] );
553 /*************************************************************************/
554 /*************************************************************************/
556 /***** MULTIPLE MASTERS SERVICE FUNCTIONS *****/
558 /*************************************************************************/
559 /*************************************************************************/
562 typedef struct GX_FVar_Head_
565 FT_UShort offsetToData;
566 FT_UShort countSizePairs;
569 FT_UShort instanceCount;
570 FT_UShort instanceSize;
575 typedef struct fvar_axis_
579 FT_ULong defaultValue;
587 /*************************************************************************/
593 /* Check that the font's `fvar' table is valid, parse it, and return */
597 /* face :: The font face. */
598 /* TT_Get_MM_Var initializes the blend structure. */
601 /* master :: The `fvar' data (must be freed by caller). */
604 /* FreeType error code. 0 means success. */
606 FT_LOCAL_DEF( FT_Error )
607 TT_Get_MM_Var( TT_Face face,
610 FT_Stream stream = face->root.stream;
611 FT_Memory memory = face->root.memory;
613 FT_Error error = TT_Err_Ok;
616 FT_MM_Var* mmvar = NULL;
617 FT_Fixed* next_coords;
618 FT_String* next_name;
620 FT_Var_Named_Style* ns;
621 GX_FVar_Head fvar_head;
623 static const FT_Frame_Field fvar_fields[] =
627 #define FT_STRUCTURE GX_FVar_Head
629 FT_FRAME_START( 16 ),
630 FT_FRAME_LONG ( version ),
631 FT_FRAME_USHORT( offsetToData ),
632 FT_FRAME_USHORT( countSizePairs ),
633 FT_FRAME_USHORT( axisCount ),
634 FT_FRAME_USHORT( axisSize ),
635 FT_FRAME_USHORT( instanceCount ),
636 FT_FRAME_USHORT( instanceSize ),
640 static const FT_Frame_Field fvaraxis_fields[] =
644 #define FT_STRUCTURE GX_FVar_Axis
646 FT_FRAME_START( 20 ),
647 FT_FRAME_ULONG ( axisTag ),
648 FT_FRAME_ULONG ( minValue ),
649 FT_FRAME_ULONG ( defaultValue ),
650 FT_FRAME_ULONG ( maxValue ),
651 FT_FRAME_USHORT( flags ),
652 FT_FRAME_USHORT( nameID ),
657 if ( face->blend == NULL )
659 /* both `fvar' and `gvar' must be present */
660 if ( (error = face->goto_table( face, TTAG_gvar,
661 stream, &table_len )) != 0 )
664 if ( (error = face->goto_table( face, TTAG_fvar,
665 stream, &table_len )) != 0 )
668 fvar_start = FT_STREAM_POS( );
670 if ( FT_STREAM_READ_FIELDS( fvar_fields, &fvar_head ) )
673 if ( fvar_head.version != (FT_Long)0x00010000L ||
674 fvar_head.countSizePairs != 2 ||
675 fvar_head.axisSize != 20 ||
676 /* axisCount limit implied by 16-bit instanceSize */
677 fvar_head.axisCount > 0x3FFE ||
678 fvar_head.instanceSize != 4 + 4 * fvar_head.axisCount ||
679 /* instanceCount limit implied by limited range of name IDs */
680 fvar_head.instanceCount > 0x7EFF ||
681 fvar_head.offsetToData + fvar_head.axisCount * 20U +
682 fvar_head.instanceCount * fvar_head.instanceSize > table_len )
684 error = TT_Err_Invalid_Table;
688 if ( FT_NEW( face->blend ) )
691 /* cannot overflow 32-bit arithmetic because of limits above */
692 face->blend->mmvar_len =
693 sizeof ( FT_MM_Var ) +
694 fvar_head.axisCount * sizeof ( FT_Var_Axis ) +
695 fvar_head.instanceCount * sizeof ( FT_Var_Named_Style ) +
696 fvar_head.instanceCount * fvar_head.axisCount * sizeof ( FT_Fixed ) +
697 5 * fvar_head.axisCount;
699 if ( FT_ALLOC( mmvar, face->blend->mmvar_len ) )
701 face->blend->mmvar = mmvar;
706 ~0; /* meaningless in this context; each glyph */
707 /* may have a different number of designs */
708 /* (or tuples, as called by Apple) */
709 mmvar->num_namedstyles =
710 fvar_head.instanceCount;
712 (FT_Var_Axis*)&(mmvar[1]);
714 (FT_Var_Named_Style*)&(mmvar->axis[fvar_head.axisCount]);
717 (FT_Fixed*)&(mmvar->namedstyle[fvar_head.instanceCount]);
718 for ( i = 0; i < fvar_head.instanceCount; ++i )
720 mmvar->namedstyle[i].coords = next_coords;
721 next_coords += fvar_head.axisCount;
724 next_name = (FT_String*)next_coords;
725 for ( i = 0; i < fvar_head.axisCount; ++i )
727 mmvar->axis[i].name = next_name;
731 if ( FT_STREAM_SEEK( fvar_start + fvar_head.offsetToData ) )
735 for ( i = 0; i < fvar_head.axisCount; ++i )
737 GX_FVar_Axis axis_rec;
740 if ( FT_STREAM_READ_FIELDS( fvaraxis_fields, &axis_rec ) )
742 a->tag = axis_rec.axisTag;
743 a->minimum = axis_rec.minValue; /* A Fixed */
744 a->def = axis_rec.defaultValue; /* A Fixed */
745 a->maximum = axis_rec.maxValue; /* A Fixed */
746 a->strid = axis_rec.nameID;
748 a->name[0] = (FT_String)( a->tag >> 24 );
749 a->name[1] = (FT_String)( ( a->tag >> 16 ) & 0xFF );
750 a->name[2] = (FT_String)( ( a->tag >> 8 ) & 0xFF );
751 a->name[3] = (FT_String)( ( a->tag ) & 0xFF );
757 ns = mmvar->namedstyle;
758 for ( i = 0; i < fvar_head.instanceCount; ++i, ++ns )
760 if ( FT_FRAME_ENTER( 4L + 4L * fvar_head.axisCount ) )
763 ns->strid = FT_GET_USHORT();
764 (void) /* flags = */ FT_GET_USHORT();
766 for ( j = 0; j < fvar_head.axisCount; ++j )
767 ns->coords[j] = FT_GET_ULONG(); /* A Fixed */
773 if ( master != NULL )
778 if ( FT_ALLOC( mmvar, face->blend->mmvar_len ) )
780 FT_MEM_COPY( mmvar, face->blend->mmvar, face->blend->mmvar_len );
783 (FT_Var_Axis*)&(mmvar[1]);
785 (FT_Var_Named_Style*)&(mmvar->axis[mmvar->num_axis]);
787 (FT_Fixed*)&(mmvar->namedstyle[mmvar->num_namedstyles]);
789 for ( n = 0; n < mmvar->num_namedstyles; ++n )
791 mmvar->namedstyle[n].coords = next_coords;
792 next_coords += mmvar->num_axis;
796 next_name = (FT_String*)next_coords;
797 for ( n = 0; n < mmvar->num_axis; ++n )
801 /* standard PostScript names for some standard apple tags */
802 if ( a->tag == TTAG_wght )
803 a->name = (char *)"Weight";
804 else if ( a->tag == TTAG_wdth )
805 a->name = (char *)"Width";
806 else if ( a->tag == TTAG_opsz )
807 a->name = (char *)"OpticalSize";
808 else if ( a->tag == TTAG_slnt )
809 a->name = (char *)"Slant";
823 /*************************************************************************/
826 /* TT_Set_MM_Blend */
829 /* Set the blend (normalized) coordinates for this instance of the */
830 /* font. Check that the `gvar' table is reasonable and does some */
831 /* initial preparation. */
834 /* face :: The font. */
835 /* Initialize the blend structure with `gvar' data. */
838 /* num_coords :: Must be the axis count of the font. */
840 /* coords :: An array of num_coords, each between [-1,1]. */
843 /* FreeType error code. 0 means success. */
845 FT_LOCAL_DEF( FT_Error )
846 TT_Set_MM_Blend( TT_Face face,
850 FT_Error error = TT_Err_Ok;
854 FT_Memory memory = face->root.memory;
865 face->doblend = FALSE;
867 if ( face->blend == NULL )
869 if ( (error = TT_Get_MM_Var( face, NULL)) != 0 )
874 mmvar = blend->mmvar;
876 if ( num_coords != mmvar->num_axis )
878 error = TT_Err_Invalid_Argument;
882 for ( i = 0; i < num_coords; ++i )
883 if ( coords[i] < -0x00010000L || coords[i] > 0x00010000L )
885 error = TT_Err_Invalid_Argument;
889 if ( blend->glyphoffsets == NULL )
890 if ( (error = ft_var_load_gvar( face )) != 0 )
893 if ( blend->normalizedcoords == NULL )
895 if ( FT_NEW_ARRAY( blend->normalizedcoords, num_coords ) )
898 manageCvt = mcvt_modify;
900 /* If we have not set the blend coordinates before this, then the */
901 /* cvt table will still be what we read from the `cvt ' table and */
902 /* we don't need to reload it. We may need to change it though... */
906 manageCvt = mcvt_retain;
907 for ( i = 0; i < num_coords; ++i )
909 if ( blend->normalizedcoords[i] != coords[i] )
911 manageCvt = mcvt_load;
916 /* If we don't change the blend coords then we don't need to do */
917 /* anything to the cvt table. It will be correct. Otherwise we */
918 /* no longer have the original cvt (it was modified when we set */
919 /* the blend last time), so we must reload and then modify it. */
922 blend->num_axis = num_coords;
923 FT_MEM_COPY( blend->normalizedcoords,
925 num_coords * sizeof ( FT_Fixed ) );
927 face->doblend = TRUE;
929 if ( face->cvt != NULL )
934 /* The cvt table has been loaded already; every time we change the */
935 /* blend we may need to reload and remodify the cvt table. */
936 FT_FREE( face->cvt );
939 tt_face_load_cvt( face, face->root.stream );
943 /* The original cvt table is in memory. All we need to do is */
944 /* apply the `cvar' table (if any). */
945 tt_face_vary_cvt( face, face->root.stream );
949 /* The cvt table is correct for this set of coordinates. */
959 /*************************************************************************/
962 /* TT_Set_Var_Design */
965 /* Set the coordinates for the instance, measured in the user */
966 /* coordinate system. Parse the `avar' table (if present) to convert */
967 /* from user to normalized coordinates. */
970 /* face :: The font face. */
971 /* Initialize the blend struct with `gvar' data. */
974 /* num_coords :: This must be the axis count of the font. */
976 /* coords :: A coordinate array with `num_coords' elements. */
979 /* FreeType error code. 0 means success. */
981 FT_LOCAL_DEF( FT_Error )
982 TT_Set_Var_Design( TT_Face face,
986 FT_Error error = TT_Err_Ok;
987 FT_Fixed* normalized = NULL;
993 FT_Memory memory = face->root.memory;
996 if ( face->blend == NULL )
998 if ( (error = TT_Get_MM_Var( face, NULL )) != 0 )
1002 blend = face->blend;
1003 mmvar = blend->mmvar;
1005 if ( num_coords != mmvar->num_axis )
1007 error = TT_Err_Invalid_Argument;
1011 /* Axis normalization is a two stage process. First we normalize */
1012 /* based on the [min,def,max] values for the axis to be [-1,0,1]. */
1013 /* Then, if there's an `avar' table, we renormalize this range. */
1015 if ( FT_NEW_ARRAY( normalized, mmvar->num_axis ) )
1019 for ( i = 0; i < mmvar->num_axis; ++i, ++a )
1021 if ( coords[i] > a->maximum || coords[i] < a->minimum )
1023 error = TT_Err_Invalid_Argument;
1027 if ( coords[i] < a->def )
1028 normalized[i] = -FT_DivFix( coords[i] - a->def, a->minimum - a->def );
1029 else if ( a->maximum == a->def )
1032 normalized[i] = FT_DivFix( coords[i] - a->def, a->maximum - a->def );
1035 if ( !blend->avar_checked )
1036 ft_var_load_avar( face );
1038 if ( blend->avar_segment != NULL )
1040 av = blend->avar_segment;
1041 for ( i = 0; i < mmvar->num_axis; ++i, ++av )
1043 for ( j = 1; j < (FT_UInt)av->pairCount; ++j )
1044 if ( normalized[i] < av->correspondence[j].fromCoord )
1047 FT_MulDiv( normalized[i] - av->correspondence[j - 1].fromCoord,
1048 av->correspondence[j].toCoord -
1049 av->correspondence[j - 1].toCoord,
1050 av->correspondence[j].fromCoord -
1051 av->correspondence[j - 1].fromCoord ) +
1052 av->correspondence[j - 1].toCoord;
1058 error = TT_Set_MM_Blend( face, num_coords, normalized );
1061 FT_FREE( normalized );
1066 /*************************************************************************/
1067 /*************************************************************************/
1069 /***** GX VAR PARSING ROUTINES *****/
1071 /*************************************************************************/
1072 /*************************************************************************/
1075 /*************************************************************************/
1078 /* tt_face_vary_cvt */
1081 /* Modify the loaded cvt table according to the `cvar' table and the */
1085 /* face :: A handle to the target face object. */
1088 /* stream :: A handle to the input stream. */
1091 /* FreeType error code. 0 means success. */
1093 /* Most errors are ignored. It is perfectly valid not to have a */
1094 /* `cvar' table even if there is a `gvar' and `fvar' table. */
1096 FT_LOCAL_DEF( FT_Error )
1097 tt_face_vary_cvt( TT_Face face,
1101 FT_Memory memory = stream->memory;
1102 FT_ULong table_start;
1105 FT_ULong offsetToData;
1108 FT_Fixed* tuple_coords = NULL;
1109 FT_Fixed* im_start_coords = NULL;
1110 FT_Fixed* im_end_coords = NULL;
1111 GX_Blend blend = face->blend;
1112 FT_UInt point_count;
1113 FT_UShort* localpoints;
1117 FT_TRACE2(( "CVAR " ));
1119 if ( blend == NULL )
1121 FT_TRACE2(( "tt_face_vary_cvt: no blend specified\n" ));
1127 if ( face->cvt == NULL )
1129 FT_TRACE2(( "tt_face_vary_cvt: no `cvt ' table\n" ));
1135 error = face->goto_table( face, TTAG_cvar, stream, &table_len );
1138 FT_TRACE2(( "is missing\n" ));
1144 if ( FT_FRAME_ENTER( table_len ) )
1150 table_start = FT_Stream_FTell( stream );
1151 if ( FT_GET_LONG() != 0x00010000L )
1153 FT_TRACE2(( "bad table version\n" ));
1159 if ( FT_NEW_ARRAY( tuple_coords, blend->num_axis ) ||
1160 FT_NEW_ARRAY( im_start_coords, blend->num_axis ) ||
1161 FT_NEW_ARRAY( im_end_coords, blend->num_axis ) )
1164 tupleCount = FT_GET_USHORT();
1165 offsetToData = table_start + FT_GET_USHORT();
1167 /* The documentation implies there are flags packed into the */
1168 /* tuplecount, but John Jenkins says that shared points don't apply */
1169 /* to `cvar', and no other flags are defined. */
1171 for ( i = 0; i < ( tupleCount & 0xFFF ); ++i )
1173 FT_UInt tupleDataSize;
1178 tupleDataSize = FT_GET_USHORT();
1179 tupleIndex = FT_GET_USHORT();
1181 /* There is no provision here for a global tuple coordinate section, */
1182 /* so John says. There are no tuple indices, just embedded tuples. */
1184 if ( tupleIndex & GX_TI_EMBEDDED_TUPLE_COORD )
1186 for ( j = 0; j < blend->num_axis; ++j )
1187 tuple_coords[j] = FT_GET_SHORT() << 2; /* convert from */
1188 /* short frac to fixed */
1192 /* skip this tuple; it makes no sense */
1194 if ( tupleIndex & GX_TI_INTERMEDIATE_TUPLE )
1195 for ( j = 0; j < 2 * blend->num_axis; ++j )
1196 (void)FT_GET_SHORT();
1198 offsetToData += tupleDataSize;
1202 if ( tupleIndex & GX_TI_INTERMEDIATE_TUPLE )
1204 for ( j = 0; j < blend->num_axis; ++j )
1205 im_start_coords[j] = FT_GET_SHORT() << 2;
1206 for ( j = 0; j < blend->num_axis; ++j )
1207 im_end_coords[j] = FT_GET_SHORT() << 2;
1210 apply = ft_var_apply_tuple( blend,
1211 (FT_UShort)tupleIndex,
1215 if ( /* tuple isn't active for our blend */
1217 /* global points not allowed, */
1218 /* if they aren't local, makes no sense */
1219 !( tupleIndex & GX_TI_PRIVATE_POINT_NUMBERS ) )
1221 offsetToData += tupleDataSize;
1225 here = FT_Stream_FTell( stream );
1227 FT_Stream_SeekSet( stream, offsetToData );
1229 localpoints = ft_var_readpackedpoints( stream, &point_count );
1230 deltas = ft_var_readpackeddeltas( stream,
1231 point_count == 0 ? face->cvt_size
1233 if ( localpoints == NULL || deltas == NULL )
1234 /* failure, ignore it */;
1236 else if ( localpoints == ALL_POINTS )
1238 /* this means that there are deltas for every entry in cvt */
1239 for ( j = 0; j < face->cvt_size; ++j )
1240 face->cvt[j] = (FT_Short)( face->cvt[j] +
1241 FT_MulFix( deltas[j], apply ) );
1246 for ( j = 0; j < point_count; ++j )
1248 int pindex = localpoints[j];
1250 face->cvt[pindex] = (FT_Short)( face->cvt[pindex] +
1251 FT_MulFix( deltas[j], apply ) );
1255 if ( localpoints != ALL_POINTS )
1256 FT_FREE( localpoints );
1259 offsetToData += tupleDataSize;
1261 FT_Stream_SeekSet( stream, here );
1268 FT_FREE( tuple_coords );
1269 FT_FREE( im_start_coords );
1270 FT_FREE( im_end_coords );
1276 /*************************************************************************/
1279 /* TT_Vary_Get_Glyph_Deltas */
1282 /* Load the appropriate deltas for the current glyph. */
1285 /* face :: A handle to the target face object. */
1287 /* glyph_index :: The index of the glyph being modified. */
1289 /* n_points :: The number of the points in the glyph, including */
1290 /* phantom points. */
1293 /* deltas :: The array of points to change. */
1296 /* FreeType error code. 0 means success. */
1298 FT_LOCAL_DEF( FT_Error )
1299 TT_Vary_Get_Glyph_Deltas( TT_Face face,
1300 FT_UInt glyph_index,
1304 FT_Stream stream = face->root.stream;
1305 FT_Memory memory = stream->memory;
1306 GX_Blend blend = face->blend;
1307 FT_Vector* delta_xy = NULL;
1310 FT_ULong glyph_start;
1312 FT_ULong offsetToData;
1315 FT_Fixed* tuple_coords = NULL;
1316 FT_Fixed* im_start_coords = NULL;
1317 FT_Fixed* im_end_coords = NULL;
1318 FT_UInt point_count, spoint_count = 0;
1319 FT_UShort* sharedpoints = NULL;
1320 FT_UShort* localpoints = NULL;
1322 FT_Short *deltas_x, *deltas_y;
1325 if ( !face->doblend || blend == NULL )
1326 return TT_Err_Invalid_Argument;
1328 /* to be freed by the caller */
1329 if ( FT_NEW_ARRAY( delta_xy, n_points ) )
1333 if ( glyph_index >= blend->gv_glyphcnt ||
1334 blend->glyphoffsets[glyph_index] ==
1335 blend->glyphoffsets[glyph_index + 1] )
1336 return TT_Err_Ok; /* no variation data for this glyph */
1338 if ( FT_STREAM_SEEK( blend->glyphoffsets[glyph_index] ) ||
1339 FT_FRAME_ENTER( blend->glyphoffsets[glyph_index + 1] -
1340 blend->glyphoffsets[glyph_index] ) )
1343 glyph_start = FT_Stream_FTell( stream );
1345 /* each set of glyph variation data is formatted similarly to `cvar' */
1346 /* (except we get shared points and global tuples) */
1348 if ( FT_NEW_ARRAY( tuple_coords, blend->num_axis ) ||
1349 FT_NEW_ARRAY( im_start_coords, blend->num_axis ) ||
1350 FT_NEW_ARRAY( im_end_coords, blend->num_axis ) )
1353 tupleCount = FT_GET_USHORT();
1354 offsetToData = glyph_start + FT_GET_USHORT();
1356 if ( tupleCount & GX_TC_TUPLES_SHARE_POINT_NUMBERS )
1358 here = FT_Stream_FTell( stream );
1360 FT_Stream_SeekSet( stream, offsetToData );
1362 sharedpoints = ft_var_readpackedpoints( stream, &spoint_count );
1363 offsetToData = FT_Stream_FTell( stream );
1365 FT_Stream_SeekSet( stream, here );
1368 for ( i = 0; i < ( tupleCount & GX_TC_TUPLE_COUNT_MASK ); ++i )
1370 FT_UInt tupleDataSize;
1375 tupleDataSize = FT_GET_USHORT();
1376 tupleIndex = FT_GET_USHORT();
1378 if ( tupleIndex & GX_TI_EMBEDDED_TUPLE_COORD )
1380 for ( j = 0; j < blend->num_axis; ++j )
1381 tuple_coords[j] = FT_GET_SHORT() << 2; /* convert from */
1382 /* short frac to fixed */
1384 else if ( ( tupleIndex & GX_TI_TUPLE_INDEX_MASK ) >= blend->tuplecount )
1386 error = TT_Err_Invalid_Table;
1393 &blend->tuplecoords[(tupleIndex & 0xFFF) * blend->num_axis],
1394 blend->num_axis * sizeof ( FT_Fixed ) );
1397 if ( tupleIndex & GX_TI_INTERMEDIATE_TUPLE )
1399 for ( j = 0; j < blend->num_axis; ++j )
1400 im_start_coords[j] = FT_GET_SHORT() << 2;
1401 for ( j = 0; j < blend->num_axis; ++j )
1402 im_end_coords[j] = FT_GET_SHORT() << 2;
1405 apply = ft_var_apply_tuple( blend,
1406 (FT_UShort)tupleIndex,
1411 if ( apply == 0 ) /* tuple isn't active for our blend */
1413 offsetToData += tupleDataSize;
1417 here = FT_Stream_FTell( stream );
1419 if ( tupleIndex & GX_TI_PRIVATE_POINT_NUMBERS )
1421 FT_Stream_SeekSet( stream, offsetToData );
1423 localpoints = ft_var_readpackedpoints( stream, &point_count );
1424 points = localpoints;
1428 points = sharedpoints;
1429 point_count = spoint_count;
1432 deltas_x = ft_var_readpackeddeltas( stream,
1433 point_count == 0 ? n_points
1435 deltas_y = ft_var_readpackeddeltas( stream,
1436 point_count == 0 ? n_points
1439 if ( points == NULL || deltas_y == NULL || deltas_x == NULL )
1440 ; /* failure, ignore it */
1442 else if ( points == ALL_POINTS )
1444 /* this means that there are deltas for every point in the glyph */
1445 for ( j = 0; j < n_points; ++j )
1447 delta_xy[j].x += FT_MulFix( deltas_x[j], apply );
1448 delta_xy[j].y += FT_MulFix( deltas_y[j], apply );
1454 for ( j = 0; j < point_count; ++j )
1456 if ( localpoints[j] >= n_points )
1459 delta_xy[localpoints[j]].x += FT_MulFix( deltas_x[j], apply );
1460 delta_xy[localpoints[j]].y += FT_MulFix( deltas_y[j], apply );
1464 if ( localpoints != ALL_POINTS )
1465 FT_FREE( localpoints );
1466 FT_FREE( deltas_x );
1467 FT_FREE( deltas_y );
1469 offsetToData += tupleDataSize;
1471 FT_Stream_SeekSet( stream, here );
1475 FT_FREE( tuple_coords );
1476 FT_FREE( im_start_coords );
1477 FT_FREE( im_end_coords );
1485 FT_FREE( delta_xy );
1494 /*************************************************************************/
1500 /* Frees the blend internal data structure. */
1502 FT_LOCAL_DEF( void )
1503 tt_done_blend( FT_Memory memory,
1506 if ( blend != NULL )
1511 FT_FREE( blend->normalizedcoords );
1512 FT_FREE( blend->mmvar );
1514 if ( blend->avar_segment != NULL )
1516 for ( i = 0; i < blend->num_axis; ++i )
1517 FT_FREE( blend->avar_segment[i].correspondence );
1518 FT_FREE( blend->avar_segment );
1521 FT_FREE( blend->tuplecoords );
1522 FT_FREE( blend->glyphoffsets );
1527 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */