1 /***************************************************************************/
5 /* TrueType GX Font Variation loader */
7 /* Copyright 2004, 2005, 2006, 2007, 2008, 2009 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*)( -1 )
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
,
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();
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();
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
)
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
)
509 for ( i
= 0; i
< blend
->num_axis
; ++i
)
511 if ( tuple_coords
[i
] == 0 )
512 /* It's not clear why (for intermediate tuples) we don't need */
513 /* to check against start/end -- the documentation says we don't. */
514 /* Similarly, it's unclear why we don't need to scale along the */
518 else if ( blend
->normalizedcoords
[i
] == 0 ||
519 ( blend
->normalizedcoords
[i
] < 0 && tuple_coords
[i
] > 0 ) ||
520 ( blend
->normalizedcoords
[i
] > 0 && tuple_coords
[i
] < 0 ) )
526 else if ( !( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
) )
527 /* not an intermediate tuple */
528 apply
= FT_MulDiv( apply
,
529 blend
->normalizedcoords
[i
] > 0
530 ? blend
->normalizedcoords
[i
]
531 : -blend
->normalizedcoords
[i
],
534 else if ( blend
->normalizedcoords
[i
] <= im_start_coords
[i
] ||
535 blend
->normalizedcoords
[i
] >= im_end_coords
[i
] )
541 else if ( blend
->normalizedcoords
[i
] < tuple_coords
[i
] )
543 temp
= FT_MulDiv( blend
->normalizedcoords
[i
] - im_start_coords
[i
],
545 tuple_coords
[i
] - im_start_coords
[i
]);
546 apply
= FT_MulDiv( apply
, temp
, 0x10000L
);
551 temp
= FT_MulDiv( im_end_coords
[i
] - blend
->normalizedcoords
[i
],
553 im_end_coords
[i
] - tuple_coords
[i
] );
554 apply
= FT_MulDiv( apply
, temp
, 0x10000L
);
562 /*************************************************************************/
563 /*************************************************************************/
565 /***** MULTIPLE MASTERS SERVICE FUNCTIONS *****/
567 /*************************************************************************/
568 /*************************************************************************/
571 typedef struct GX_FVar_Head_
574 FT_UShort offsetToData
;
575 FT_UShort countSizePairs
;
578 FT_UShort instanceCount
;
579 FT_UShort instanceSize
;
584 typedef struct fvar_axis_
588 FT_ULong defaultValue
;
596 /*************************************************************************/
602 /* Check that the font's `fvar' table is valid, parse it, and return */
606 /* face :: The font face. */
607 /* TT_Get_MM_Var initializes the blend structure. */
610 /* master :: The `fvar' data (must be freed by caller). */
613 /* FreeType error code. 0 means success. */
615 FT_LOCAL_DEF( FT_Error
)
616 TT_Get_MM_Var( TT_Face face
,
619 FT_Stream stream
= face
->root
.stream
;
620 FT_Memory memory
= face
->root
.memory
;
622 FT_Error error
= TT_Err_Ok
;
626 FT_Fixed
* next_coords
;
627 FT_String
* next_name
;
629 FT_Var_Named_Style
* ns
;
630 GX_FVar_Head fvar_head
;
632 static const FT_Frame_Field fvar_fields
[] =
636 #define FT_STRUCTURE GX_FVar_Head
638 FT_FRAME_START( 16 ),
639 FT_FRAME_LONG ( version
),
640 FT_FRAME_USHORT( offsetToData
),
641 FT_FRAME_USHORT( countSizePairs
),
642 FT_FRAME_USHORT( axisCount
),
643 FT_FRAME_USHORT( axisSize
),
644 FT_FRAME_USHORT( instanceCount
),
645 FT_FRAME_USHORT( instanceSize
),
649 static const FT_Frame_Field fvaraxis_fields
[] =
653 #define FT_STRUCTURE GX_FVar_Axis
655 FT_FRAME_START( 20 ),
656 FT_FRAME_ULONG ( axisTag
),
657 FT_FRAME_ULONG ( minValue
),
658 FT_FRAME_ULONG ( defaultValue
),
659 FT_FRAME_ULONG ( maxValue
),
660 FT_FRAME_USHORT( flags
),
661 FT_FRAME_USHORT( nameID
),
666 if ( face
->blend
== NULL
)
668 /* both `fvar' and `gvar' must be present */
669 if ( (error
= face
->goto_table( face
, TTAG_gvar
,
670 stream
, &table_len
)) != 0 )
673 if ( (error
= face
->goto_table( face
, TTAG_fvar
,
674 stream
, &table_len
)) != 0 )
677 fvar_start
= FT_STREAM_POS( );
679 if ( FT_STREAM_READ_FIELDS( fvar_fields
, &fvar_head
) )
682 if ( fvar_head
.version
!= (FT_Long
)0x00010000L
||
683 fvar_head
.countSizePairs
!= 2 ||
684 fvar_head
.axisSize
!= 20 ||
685 fvar_head
.instanceSize
!= 4 + 4 * fvar_head
.axisCount
||
686 fvar_head
.offsetToData
+ fvar_head
.axisCount
* 20U +
687 fvar_head
.instanceCount
* fvar_head
.instanceSize
> table_len
)
689 error
= TT_Err_Invalid_Table
;
693 if ( FT_NEW( face
->blend
) )
696 /* XXX: TODO - check for overflows */
697 face
->blend
->mmvar_len
=
698 sizeof ( FT_MM_Var
) +
699 fvar_head
.axisCount
* sizeof ( FT_Var_Axis
) +
700 fvar_head
.instanceCount
* sizeof ( FT_Var_Named_Style
) +
701 fvar_head
.instanceCount
* fvar_head
.axisCount
* sizeof ( FT_Fixed
) +
702 5 * fvar_head
.axisCount
;
704 if ( FT_ALLOC( mmvar
, face
->blend
->mmvar_len
) )
706 face
->blend
->mmvar
= mmvar
;
711 (FT_UInt
)-1; /* meaningless in this context; each glyph */
712 /* may have a different number of designs */
713 /* (or tuples, as called by Apple) */
714 mmvar
->num_namedstyles
=
715 fvar_head
.instanceCount
;
717 (FT_Var_Axis
*)&(mmvar
[1]);
719 (FT_Var_Named_Style
*)&(mmvar
->axis
[fvar_head
.axisCount
]);
722 (FT_Fixed
*)&(mmvar
->namedstyle
[fvar_head
.instanceCount
]);
723 for ( i
= 0; i
< fvar_head
.instanceCount
; ++i
)
725 mmvar
->namedstyle
[i
].coords
= next_coords
;
726 next_coords
+= fvar_head
.axisCount
;
729 next_name
= (FT_String
*)next_coords
;
730 for ( i
= 0; i
< fvar_head
.axisCount
; ++i
)
732 mmvar
->axis
[i
].name
= next_name
;
736 if ( FT_STREAM_SEEK( fvar_start
+ fvar_head
.offsetToData
) )
740 for ( i
= 0; i
< fvar_head
.axisCount
; ++i
)
742 GX_FVar_Axis axis_rec
;
745 if ( FT_STREAM_READ_FIELDS( fvaraxis_fields
, &axis_rec
) )
747 a
->tag
= axis_rec
.axisTag
;
748 a
->minimum
= axis_rec
.minValue
; /* A Fixed */
749 a
->def
= axis_rec
.defaultValue
; /* A Fixed */
750 a
->maximum
= axis_rec
.maxValue
; /* A Fixed */
751 a
->strid
= axis_rec
.nameID
;
753 a
->name
[0] = (FT_String
)( a
->tag
>> 24 );
754 a
->name
[1] = (FT_String
)( ( a
->tag
>> 16 ) & 0xFF );
755 a
->name
[2] = (FT_String
)( ( a
->tag
>> 8 ) & 0xFF );
756 a
->name
[3] = (FT_String
)( ( a
->tag
) & 0xFF );
762 ns
= mmvar
->namedstyle
;
763 for ( i
= 0; i
< fvar_head
.instanceCount
; ++i
, ++ns
)
765 if ( FT_FRAME_ENTER( 4L + 4L * fvar_head
.axisCount
) )
768 ns
->strid
= FT_GET_USHORT();
769 (void) /* flags = */ FT_GET_USHORT();
771 for ( j
= 0; j
< fvar_head
.axisCount
; ++j
)
772 ns
->coords
[j
] = FT_GET_ULONG(); /* A Fixed */
778 if ( master
!= NULL
)
783 if ( FT_ALLOC( mmvar
, face
->blend
->mmvar_len
) )
785 FT_MEM_COPY( mmvar
, face
->blend
->mmvar
, face
->blend
->mmvar_len
);
788 (FT_Var_Axis
*)&(mmvar
[1]);
790 (FT_Var_Named_Style
*)&(mmvar
->axis
[mmvar
->num_axis
]);
792 (FT_Fixed
*)&(mmvar
->namedstyle
[mmvar
->num_namedstyles
]);
794 for ( n
= 0; n
< mmvar
->num_namedstyles
; ++n
)
796 mmvar
->namedstyle
[n
].coords
= next_coords
;
797 next_coords
+= mmvar
->num_axis
;
801 next_name
= (FT_String
*)next_coords
;
802 for ( n
= 0; n
< mmvar
->num_axis
; ++n
)
806 /* standard PostScript names for some standard apple tags */
807 if ( a
->tag
== TTAG_wght
)
808 a
->name
= (char *)"Weight";
809 else if ( a
->tag
== TTAG_wdth
)
810 a
->name
= (char *)"Width";
811 else if ( a
->tag
== TTAG_opsz
)
812 a
->name
= (char *)"OpticalSize";
813 else if ( a
->tag
== TTAG_slnt
)
814 a
->name
= (char *)"Slant";
828 /*************************************************************************/
831 /* TT_Set_MM_Blend */
834 /* Set the blend (normalized) coordinates for this instance of the */
835 /* font. Check that the `gvar' table is reasonable and does some */
836 /* initial preparation. */
839 /* face :: The font. */
840 /* Initialize the blend structure with `gvar' data. */
843 /* num_coords :: Must be the axis count of the font. */
845 /* coords :: An array of num_coords, each between [-1,1]. */
848 /* FreeType error code. 0 means success. */
850 FT_LOCAL_DEF( FT_Error
)
851 TT_Set_MM_Blend( TT_Face face
,
855 FT_Error error
= TT_Err_Ok
;
859 FT_Memory memory
= face
->root
.memory
;
870 face
->doblend
= FALSE
;
872 if ( face
->blend
== NULL
)
874 if ( (error
= TT_Get_MM_Var( face
, NULL
)) != 0 )
879 mmvar
= blend
->mmvar
;
881 if ( num_coords
!= mmvar
->num_axis
)
883 error
= TT_Err_Invalid_Argument
;
887 for ( i
= 0; i
< num_coords
; ++i
)
888 if ( coords
[i
] < -0x00010000L
|| coords
[i
] > 0x00010000L
)
890 error
= TT_Err_Invalid_Argument
;
894 if ( blend
->glyphoffsets
== NULL
)
895 if ( (error
= ft_var_load_gvar( face
)) != 0 )
898 if ( blend
->normalizedcoords
== NULL
)
900 if ( FT_NEW_ARRAY( blend
->normalizedcoords
, num_coords
) )
903 manageCvt
= mcvt_modify
;
905 /* If we have not set the blend coordinates before this, then the */
906 /* cvt table will still be what we read from the `cvt ' table and */
907 /* we don't need to reload it. We may need to change it though... */
911 manageCvt
= mcvt_retain
;
912 for ( i
= 0; i
< num_coords
; ++i
)
914 if ( blend
->normalizedcoords
[i
] != coords
[i
] )
916 manageCvt
= mcvt_load
;
921 /* If we don't change the blend coords then we don't need to do */
922 /* anything to the cvt table. It will be correct. Otherwise we */
923 /* no longer have the original cvt (it was modified when we set */
924 /* the blend last time), so we must reload and then modify it. */
927 blend
->num_axis
= num_coords
;
928 FT_MEM_COPY( blend
->normalizedcoords
,
930 num_coords
* sizeof ( FT_Fixed
) );
932 face
->doblend
= TRUE
;
934 if ( face
->cvt
!= NULL
)
939 /* The cvt table has been loaded already; every time we change the */
940 /* blend we may need to reload and remodify the cvt table. */
941 FT_FREE( face
->cvt
);
944 tt_face_load_cvt( face
, face
->root
.stream
);
948 /* The original cvt table is in memory. All we need to do is */
949 /* apply the `cvar' table (if any). */
950 tt_face_vary_cvt( face
, face
->root
.stream
);
954 /* The cvt table is correct for this set of coordinates. */
964 /*************************************************************************/
967 /* TT_Set_Var_Design */
970 /* Set the coordinates for the instance, measured in the user */
971 /* coordinate system. Parse the `avar' table (if present) to convert */
972 /* from user to normalized coordinates. */
975 /* face :: The font face. */
976 /* Initialize the blend struct with `gvar' data. */
979 /* num_coords :: This must be the axis count of the font. */
981 /* coords :: A coordinate array with `num_coords' elements. */
984 /* FreeType error code. 0 means success. */
986 FT_LOCAL_DEF( FT_Error
)
987 TT_Set_Var_Design( TT_Face face
,
991 FT_Error error
= TT_Err_Ok
;
992 FT_Fixed
* normalized
= NULL
;
998 FT_Memory memory
= face
->root
.memory
;
1001 if ( face
->blend
== NULL
)
1003 if ( (error
= TT_Get_MM_Var( face
, NULL
)) != 0 )
1007 blend
= face
->blend
;
1008 mmvar
= blend
->mmvar
;
1010 if ( num_coords
!= mmvar
->num_axis
)
1012 error
= TT_Err_Invalid_Argument
;
1016 /* Axis normalization is a two stage process. First we normalize */
1017 /* based on the [min,def,max] values for the axis to be [-1,0,1]. */
1018 /* Then, if there's an `avar' table, we renormalize this range. */
1020 if ( FT_NEW_ARRAY( normalized
, mmvar
->num_axis
) )
1024 for ( i
= 0; i
< mmvar
->num_axis
; ++i
, ++a
)
1026 if ( coords
[i
] > a
->maximum
|| coords
[i
] < a
->minimum
)
1028 error
= TT_Err_Invalid_Argument
;
1032 if ( coords
[i
] < a
->def
)
1034 normalized
[i
] = -FT_MulDiv( coords
[i
] - a
->def
,
1036 a
->minimum
- a
->def
);
1038 else if ( a
->maximum
== a
->def
)
1042 normalized
[i
] = FT_MulDiv( coords
[i
] - a
->def
,
1044 a
->maximum
- a
->def
);
1048 if ( !blend
->avar_checked
)
1049 ft_var_load_avar( face
);
1051 if ( blend
->avar_segment
!= NULL
)
1053 av
= blend
->avar_segment
;
1054 for ( i
= 0; i
< mmvar
->num_axis
; ++i
, ++av
)
1056 for ( j
= 1; j
< (FT_UInt
)av
->pairCount
; ++j
)
1057 if ( normalized
[i
] < av
->correspondence
[j
].fromCoord
)
1062 normalized
[i
] - av
->correspondence
[j
- 1].fromCoord
,
1064 av
->correspondence
[j
].fromCoord
-
1065 av
->correspondence
[j
- 1].fromCoord
),
1066 av
->correspondence
[j
].toCoord
-
1067 av
->correspondence
[j
- 1].toCoord
,
1069 av
->correspondence
[j
- 1].toCoord
;
1075 error
= TT_Set_MM_Blend( face
, num_coords
, normalized
);
1078 FT_FREE( normalized
);
1083 /*************************************************************************/
1084 /*************************************************************************/
1086 /***** GX VAR PARSING ROUTINES *****/
1088 /*************************************************************************/
1089 /*************************************************************************/
1092 /*************************************************************************/
1095 /* tt_face_vary_cvt */
1098 /* Modify the loaded cvt table according to the `cvar' table and the */
1102 /* face :: A handle to the target face object. */
1105 /* stream :: A handle to the input stream. */
1108 /* FreeType error code. 0 means success. */
1110 /* Most errors are ignored. It is perfectly valid not to have a */
1111 /* `cvar' table even if there is a `gvar' and `fvar' table. */
1113 FT_LOCAL_DEF( FT_Error
)
1114 tt_face_vary_cvt( TT_Face face
,
1118 FT_Memory memory
= stream
->memory
;
1119 FT_ULong table_start
;
1122 FT_ULong offsetToData
;
1125 FT_Fixed
* tuple_coords
= NULL
;
1126 FT_Fixed
* im_start_coords
= NULL
;
1127 FT_Fixed
* im_end_coords
= NULL
;
1128 GX_Blend blend
= face
->blend
;
1129 FT_UInt point_count
;
1130 FT_UShort
* localpoints
;
1134 FT_TRACE2(( "CVAR " ));
1136 if ( blend
== NULL
)
1138 FT_TRACE2(( "tt_face_vary_cvt: no blend specified\n" ));
1144 if ( face
->cvt
== NULL
)
1146 FT_TRACE2(( "tt_face_vary_cvt: no `cvt ' table\n" ));
1152 error
= face
->goto_table( face
, TTAG_cvar
, stream
, &table_len
);
1155 FT_TRACE2(( "is missing\n" ));
1161 if ( FT_FRAME_ENTER( table_len
) )
1167 table_start
= FT_Stream_FTell( stream
);
1168 if ( FT_GET_LONG() != 0x00010000L
)
1170 FT_TRACE2(( "bad table version\n" ));
1176 if ( FT_NEW_ARRAY( tuple_coords
, blend
->num_axis
) ||
1177 FT_NEW_ARRAY( im_start_coords
, blend
->num_axis
) ||
1178 FT_NEW_ARRAY( im_end_coords
, blend
->num_axis
) )
1181 tupleCount
= FT_GET_USHORT();
1182 offsetToData
= table_start
+ FT_GET_USHORT();
1184 /* The documentation implies there are flags packed into the */
1185 /* tuplecount, but John Jenkins says that shared points don't apply */
1186 /* to `cvar', and no other flags are defined. */
1188 for ( i
= 0; i
< ( tupleCount
& 0xFFF ); ++i
)
1190 FT_UInt tupleDataSize
;
1195 tupleDataSize
= FT_GET_USHORT();
1196 tupleIndex
= FT_GET_USHORT();
1198 /* There is no provision here for a global tuple coordinate section, */
1199 /* so John says. There are no tuple indices, just embedded tuples. */
1201 if ( tupleIndex
& GX_TI_EMBEDDED_TUPLE_COORD
)
1203 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1204 tuple_coords
[j
] = FT_GET_SHORT() << 2; /* convert from */
1205 /* short frac to fixed */
1209 /* skip this tuple; it makes no sense */
1211 if ( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
)
1212 for ( j
= 0; j
< 2 * blend
->num_axis
; ++j
)
1213 (void)FT_GET_SHORT();
1215 offsetToData
+= tupleDataSize
;
1219 if ( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
)
1221 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1222 im_start_coords
[j
] = FT_GET_SHORT() << 2;
1223 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1224 im_end_coords
[j
] = FT_GET_SHORT() << 2;
1227 apply
= ft_var_apply_tuple( blend
,
1228 (FT_UShort
)tupleIndex
,
1232 if ( /* tuple isn't active for our blend */
1234 /* global points not allowed, */
1235 /* if they aren't local, makes no sense */
1236 !( tupleIndex
& GX_TI_PRIVATE_POINT_NUMBERS
) )
1238 offsetToData
+= tupleDataSize
;
1242 here
= FT_Stream_FTell( stream
);
1244 FT_Stream_SeekSet( stream
, offsetToData
);
1246 localpoints
= ft_var_readpackedpoints( stream
, &point_count
);
1247 deltas
= ft_var_readpackeddeltas( stream
,
1248 point_count
== 0 ? face
->cvt_size
1250 if ( localpoints
== NULL
|| deltas
== NULL
)
1251 /* failure, ignore it */;
1253 else if ( localpoints
== ALL_POINTS
)
1255 /* this means that there are deltas for every entry in cvt */
1256 for ( j
= 0; j
< face
->cvt_size
; ++j
)
1257 face
->cvt
[j
] = (FT_Short
)( face
->cvt
[j
] +
1258 FT_MulFix( deltas
[j
], apply
) );
1263 for ( j
= 0; j
< point_count
; ++j
)
1265 int pindex
= localpoints
[j
];
1267 face
->cvt
[pindex
] = (FT_Short
)( face
->cvt
[pindex
] +
1268 FT_MulFix( deltas
[j
], apply
) );
1272 if ( localpoints
!= ALL_POINTS
)
1273 FT_FREE( localpoints
);
1276 offsetToData
+= tupleDataSize
;
1278 FT_Stream_SeekSet( stream
, here
);
1285 FT_FREE( tuple_coords
);
1286 FT_FREE( im_start_coords
);
1287 FT_FREE( im_end_coords
);
1293 /*************************************************************************/
1296 /* TT_Vary_Get_Glyph_Deltas */
1299 /* Load the appropriate deltas for the current glyph. */
1302 /* face :: A handle to the target face object. */
1304 /* glyph_index :: The index of the glyph being modified. */
1306 /* n_points :: The number of the points in the glyph, including */
1307 /* phantom points. */
1310 /* deltas :: The array of points to change. */
1313 /* FreeType error code. 0 means success. */
1315 FT_LOCAL_DEF( FT_Error
)
1316 TT_Vary_Get_Glyph_Deltas( TT_Face face
,
1317 FT_UInt glyph_index
,
1321 FT_Stream stream
= face
->root
.stream
;
1322 FT_Memory memory
= stream
->memory
;
1323 GX_Blend blend
= face
->blend
;
1324 FT_Vector
* delta_xy
;
1327 FT_ULong glyph_start
;
1329 FT_ULong offsetToData
;
1332 FT_Fixed
* tuple_coords
= NULL
;
1333 FT_Fixed
* im_start_coords
= NULL
;
1334 FT_Fixed
* im_end_coords
= NULL
;
1335 FT_UInt point_count
, spoint_count
= 0;
1336 FT_UShort
* sharedpoints
= NULL
;
1337 FT_UShort
* localpoints
= NULL
;
1339 FT_Short
*deltas_x
, *deltas_y
;
1342 if ( !face
->doblend
|| blend
== NULL
)
1343 return TT_Err_Invalid_Argument
;
1345 /* to be freed by the caller */
1346 if ( FT_NEW_ARRAY( delta_xy
, n_points
) )
1350 if ( glyph_index
>= blend
->gv_glyphcnt
||
1351 blend
->glyphoffsets
[glyph_index
] ==
1352 blend
->glyphoffsets
[glyph_index
+ 1] )
1353 return TT_Err_Ok
; /* no variation data for this glyph */
1355 if ( FT_STREAM_SEEK( blend
->glyphoffsets
[glyph_index
] ) ||
1356 FT_FRAME_ENTER( blend
->glyphoffsets
[glyph_index
+ 1] -
1357 blend
->glyphoffsets
[glyph_index
] ) )
1360 glyph_start
= FT_Stream_FTell( stream
);
1362 /* each set of glyph variation data is formatted similarly to `cvar' */
1363 /* (except we get shared points and global tuples) */
1365 if ( FT_NEW_ARRAY( tuple_coords
, blend
->num_axis
) ||
1366 FT_NEW_ARRAY( im_start_coords
, blend
->num_axis
) ||
1367 FT_NEW_ARRAY( im_end_coords
, blend
->num_axis
) )
1370 tupleCount
= FT_GET_USHORT();
1371 offsetToData
= glyph_start
+ FT_GET_USHORT();
1373 if ( tupleCount
& GX_TC_TUPLES_SHARE_POINT_NUMBERS
)
1375 here
= FT_Stream_FTell( stream
);
1377 FT_Stream_SeekSet( stream
, offsetToData
);
1379 sharedpoints
= ft_var_readpackedpoints( stream
, &spoint_count
);
1380 offsetToData
= FT_Stream_FTell( stream
);
1382 FT_Stream_SeekSet( stream
, here
);
1385 for ( i
= 0; i
< ( tupleCount
& GX_TC_TUPLE_COUNT_MASK
); ++i
)
1387 FT_UInt tupleDataSize
;
1392 tupleDataSize
= FT_GET_USHORT();
1393 tupleIndex
= FT_GET_USHORT();
1395 if ( tupleIndex
& GX_TI_EMBEDDED_TUPLE_COORD
)
1397 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1398 tuple_coords
[j
] = FT_GET_SHORT() << 2; /* convert from */
1399 /* short frac to fixed */
1401 else if ( ( tupleIndex
& GX_TI_TUPLE_INDEX_MASK
) >= blend
->tuplecount
)
1403 error
= TT_Err_Invalid_Table
;
1410 &blend
->tuplecoords
[(tupleIndex
& 0xFFF) * blend
->num_axis
],
1411 blend
->num_axis
* sizeof ( FT_Fixed
) );
1414 if ( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
)
1416 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1417 im_start_coords
[j
] = FT_GET_SHORT() << 2;
1418 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1419 im_end_coords
[j
] = FT_GET_SHORT() << 2;
1422 apply
= ft_var_apply_tuple( blend
,
1423 (FT_UShort
)tupleIndex
,
1428 if ( apply
== 0 ) /* tuple isn't active for our blend */
1430 offsetToData
+= tupleDataSize
;
1434 here
= FT_Stream_FTell( stream
);
1436 if ( tupleIndex
& GX_TI_PRIVATE_POINT_NUMBERS
)
1438 FT_Stream_SeekSet( stream
, offsetToData
);
1440 localpoints
= ft_var_readpackedpoints( stream
, &point_count
);
1441 points
= localpoints
;
1445 points
= sharedpoints
;
1446 point_count
= spoint_count
;
1449 deltas_x
= ft_var_readpackeddeltas( stream
,
1450 point_count
== 0 ? n_points
1452 deltas_y
= ft_var_readpackeddeltas( stream
,
1453 point_count
== 0 ? n_points
1456 if ( points
== NULL
|| deltas_y
== NULL
|| deltas_x
== NULL
)
1457 ; /* failure, ignore it */
1459 else if ( points
== ALL_POINTS
)
1461 /* this means that there are deltas for every point in the glyph */
1462 for ( j
= 0; j
< n_points
; ++j
)
1464 delta_xy
[j
].x
+= FT_MulFix( deltas_x
[j
], apply
);
1465 delta_xy
[j
].y
+= FT_MulFix( deltas_y
[j
], apply
);
1471 for ( j
= 0; j
< point_count
; ++j
)
1473 delta_xy
[localpoints
[j
]].x
+= FT_MulFix( deltas_x
[j
], apply
);
1474 delta_xy
[localpoints
[j
]].y
+= FT_MulFix( deltas_y
[j
], apply
);
1478 if ( localpoints
!= ALL_POINTS
)
1479 FT_FREE( localpoints
);
1480 FT_FREE( deltas_x
);
1481 FT_FREE( deltas_y
);
1483 offsetToData
+= tupleDataSize
;
1485 FT_Stream_SeekSet( stream
, here
);
1489 FT_FREE( tuple_coords
);
1490 FT_FREE( im_start_coords
);
1491 FT_FREE( im_end_coords
);
1499 FT_FREE( delta_xy
);
1508 /*************************************************************************/
1514 /* Frees the blend internal data structure. */
1516 FT_LOCAL_DEF( void )
1517 tt_done_blend( FT_Memory memory
,
1520 if ( blend
!= NULL
)
1525 FT_FREE( blend
->normalizedcoords
);
1526 FT_FREE( blend
->mmvar
);
1528 if ( blend
->avar_segment
!= NULL
)
1530 for ( i
= 0; i
< blend
->num_axis
; ++i
)
1531 FT_FREE( blend
->avar_segment
[i
].correspondence
);
1532 FT_FREE( blend
->avar_segment
);
1535 FT_FREE( blend
->tuplecoords
);
1536 FT_FREE( blend
->glyphoffsets
);
1541 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */