1 /***************************************************************************/
5 /* TrueType GX Font Variation loader */
7 /* Copyright 2004-2014 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 (FT_ULong)( (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. */
95 /* ensure that value `0' has the same width as a pointer */
96 #define ALL_POINTS (FT_UShort*)~(FT_PtrDist)0
99 #define GX_PT_POINTS_ARE_WORDS 0x80
100 #define GX_PT_POINT_RUN_COUNT_MASK 0x7F
103 /*************************************************************************/
106 /* ft_var_readpackedpoints */
109 /* Read a set of points to which the following deltas will apply. */
110 /* Points are packed with a run length encoding. */
113 /* stream :: The data stream. */
116 /* point_cnt :: The number of points read. A zero value means that */
117 /* all points in the glyph will be affected, without */
118 /* enumerating them individually. */
121 /* An array of FT_UShort containing the affected points or the */
122 /* special value ALL_POINTS. */
125 ft_var_readpackedpoints( FT_Stream stream
,
128 FT_UShort
*points
= NULL
;
134 FT_Memory memory
= stream
->memory
;
135 FT_Error error
= FT_Err_Ok
;
140 *point_cnt
= n
= FT_GET_BYTE();
144 if ( n
& GX_PT_POINTS_ARE_WORDS
)
145 n
= FT_GET_BYTE() | ( ( n
& GX_PT_POINT_RUN_COUNT_MASK
) << 8 );
147 if ( FT_NEW_ARRAY( points
, n
) )
153 runcnt
= FT_GET_BYTE();
154 if ( runcnt
& GX_PT_POINTS_ARE_WORDS
)
156 runcnt
= runcnt
& GX_PT_POINT_RUN_COUNT_MASK
;
157 first
= points
[i
++] = FT_GET_USHORT();
159 if ( runcnt
< 1 || i
+ runcnt
>= n
)
162 /* first point not included in runcount */
163 for ( j
= 0; j
< runcnt
; ++j
)
164 points
[i
++] = (FT_UShort
)( first
+= FT_GET_USHORT() );
168 first
= points
[i
++] = FT_GET_BYTE();
170 if ( runcnt
< 1 || i
+ runcnt
>= n
)
173 for ( j
= 0; j
< runcnt
; ++j
)
174 points
[i
++] = (FT_UShort
)( first
+= FT_GET_BYTE() );
185 GX_DT_DELTAS_ARE_ZERO
= 0x80,
186 GX_DT_DELTAS_ARE_WORDS
= 0x40,
187 GX_DT_DELTA_RUN_COUNT_MASK
= 0x3F
191 /*************************************************************************/
194 /* ft_var_readpackeddeltas */
197 /* Read a set of deltas. These are packed slightly differently than */
198 /* points. In particular there is no overall count. */
201 /* stream :: The data stream. */
203 /* delta_cnt :: The number of to be read. */
206 /* An array of FT_Short containing the deltas for the affected */
207 /* points. (This only gets the deltas for one dimension. It will */
208 /* generally be called twice, once for x, once for y. When used in */
209 /* cvt table, it will only be called once.) */
212 ft_var_readpackeddeltas( FT_Stream stream
,
213 FT_Offset delta_cnt
)
215 FT_Short
*deltas
= NULL
;
219 FT_Memory memory
= stream
->memory
;
220 FT_Error error
= FT_Err_Ok
;
225 if ( FT_NEW_ARRAY( deltas
, delta_cnt
) )
229 while ( i
< delta_cnt
)
231 runcnt
= FT_GET_BYTE();
232 if ( runcnt
& GX_DT_DELTAS_ARE_ZERO
)
234 /* runcnt zeroes get added */
236 j
<= ( runcnt
& GX_DT_DELTA_RUN_COUNT_MASK
) && i
< delta_cnt
;
240 else if ( runcnt
& GX_DT_DELTAS_ARE_WORDS
)
242 /* runcnt shorts from the stack */
244 j
<= ( runcnt
& GX_DT_DELTA_RUN_COUNT_MASK
) && i
< delta_cnt
;
246 deltas
[i
++] = FT_GET_SHORT();
250 /* runcnt signed bytes from the stack */
252 j
<= ( runcnt
& GX_DT_DELTA_RUN_COUNT_MASK
) && i
< delta_cnt
;
254 deltas
[i
++] = FT_GET_CHAR();
257 if ( j
<= ( runcnt
& GX_DT_DELTA_RUN_COUNT_MASK
) )
269 /*************************************************************************/
272 /* ft_var_load_avar */
275 /* Parse the `avar' table if present. It need not be, so we return */
279 /* face :: The font face. */
282 ft_var_load_avar( TT_Face face
)
284 FT_Stream stream
= FT_FACE_STREAM(face
);
285 FT_Memory memory
= stream
->memory
;
286 GX_Blend blend
= face
->blend
;
287 GX_AVarSegment segment
;
288 FT_Error error
= FT_Err_Ok
;
297 blend
->avar_checked
= TRUE
;
298 if ( (error
= face
->goto_table( face
, TTAG_avar
, stream
, &table_len
)) != 0 )
301 if ( FT_FRAME_ENTER( table_len
) )
304 version
= FT_GET_LONG();
305 axisCount
= FT_GET_LONG();
307 if ( version
!= 0x00010000L
||
308 axisCount
!= (FT_Long
)blend
->mmvar
->num_axis
)
311 if ( FT_NEW_ARRAY( blend
->avar_segment
, axisCount
) )
314 segment
= &blend
->avar_segment
[0];
315 for ( i
= 0; i
< axisCount
; ++i
, ++segment
)
317 segment
->pairCount
= FT_GET_USHORT();
318 if ( FT_NEW_ARRAY( segment
->correspondence
, segment
->pairCount
) )
320 /* Failure. Free everything we have done so far. We must do */
321 /* it right now since loading the `avar' table is optional. */
323 for ( j
= i
- 1; j
>= 0; --j
)
324 FT_FREE( blend
->avar_segment
[j
].correspondence
);
326 FT_FREE( blend
->avar_segment
);
327 blend
->avar_segment
= NULL
;
331 for ( j
= 0; j
< segment
->pairCount
; ++j
)
333 segment
->correspondence
[j
].fromCoord
=
334 FT_GET_SHORT() << 2; /* convert to Fixed */
335 segment
->correspondence
[j
].toCoord
=
336 FT_GET_SHORT()<<2; /* convert to Fixed */
345 typedef struct GX_GVar_Head_
349 FT_UShort globalCoordCount
;
350 FT_ULong offsetToCoord
;
351 FT_UShort glyphCount
;
353 FT_ULong offsetToData
;
358 /*************************************************************************/
361 /* ft_var_load_gvar */
364 /* Parses the `gvar' table if present. If `fvar' is there, `gvar' */
365 /* had better be there too. */
368 /* face :: The font face. */
371 /* FreeType error code. 0 means success. */
374 ft_var_load_gvar( TT_Face face
)
376 FT_Stream stream
= FT_FACE_STREAM(face
);
377 FT_Memory memory
= stream
->memory
;
378 GX_Blend blend
= face
->blend
;
383 FT_ULong offsetToData
;
384 GX_GVar_Head gvar_head
;
386 static const FT_Frame_Field gvar_fields
[] =
390 #define FT_STRUCTURE GX_GVar_Head
392 FT_FRAME_START( 20 ),
393 FT_FRAME_LONG ( version
),
394 FT_FRAME_USHORT( axisCount
),
395 FT_FRAME_USHORT( globalCoordCount
),
396 FT_FRAME_ULONG ( offsetToCoord
),
397 FT_FRAME_USHORT( glyphCount
),
398 FT_FRAME_USHORT( flags
),
399 FT_FRAME_ULONG ( offsetToData
),
403 if ( (error
= face
->goto_table( face
, TTAG_gvar
, stream
, &table_len
)) != 0 )
406 gvar_start
= FT_STREAM_POS( );
407 if ( FT_STREAM_READ_FIELDS( gvar_fields
, &gvar_head
) )
410 blend
->tuplecount
= gvar_head
.globalCoordCount
;
411 blend
->gv_glyphcnt
= gvar_head
.glyphCount
;
412 offsetToData
= gvar_start
+ gvar_head
.offsetToData
;
414 if ( gvar_head
.version
!= (FT_Long
)0x00010000L
||
415 gvar_head
.axisCount
!= (FT_UShort
)blend
->mmvar
->num_axis
)
417 error
= FT_THROW( Invalid_Table
);
421 if ( FT_NEW_ARRAY( blend
->glyphoffsets
, blend
->gv_glyphcnt
+ 1 ) )
424 if ( gvar_head
.flags
& 1 )
426 /* long offsets (one more offset than glyphs, to mark size of last) */
427 if ( FT_FRAME_ENTER( ( blend
->gv_glyphcnt
+ 1 ) * 4L ) )
430 for ( i
= 0; i
<= blend
->gv_glyphcnt
; ++i
)
431 blend
->glyphoffsets
[i
] = offsetToData
+ FT_GET_LONG();
437 /* short offsets (one more offset than glyphs, to mark size of last) */
438 if ( FT_FRAME_ENTER( ( blend
->gv_glyphcnt
+ 1 ) * 2L ) )
441 for ( i
= 0; i
<= blend
->gv_glyphcnt
; ++i
)
442 blend
->glyphoffsets
[i
] = offsetToData
+ FT_GET_USHORT() * 2;
443 /* XXX: Undocumented: `*2'! */
448 if ( blend
->tuplecount
!= 0 )
450 if ( FT_NEW_ARRAY( blend
->tuplecoords
,
451 gvar_head
.axisCount
* blend
->tuplecount
) )
454 if ( FT_STREAM_SEEK( gvar_start
+ gvar_head
.offsetToCoord
) ||
455 FT_FRAME_ENTER( blend
->tuplecount
* gvar_head
.axisCount
* 2L ) )
458 for ( i
= 0; i
< blend
->tuplecount
; ++i
)
459 for ( j
= 0 ; j
< (FT_UInt
)gvar_head
.axisCount
; ++j
)
460 blend
->tuplecoords
[i
* gvar_head
.axisCount
+ j
] =
461 FT_GET_SHORT() << 2; /* convert to FT_Fixed */
471 /*************************************************************************/
474 /* ft_var_apply_tuple */
477 /* Figure out whether a given tuple (design) applies to the current */
478 /* blend, and if so, what is the scaling factor. */
481 /* blend :: The current blend of the font. */
483 /* tupleIndex :: A flag saying whether this is an intermediate */
486 /* tuple_coords :: The coordinates of the tuple in normalized axis */
489 /* im_start_coords :: The initial coordinates where this tuple starts */
490 /* to apply (for intermediate coordinates). */
492 /* im_end_coords :: The final coordinates after which this tuple no */
493 /* longer applies (for intermediate coordinates). */
496 /* An FT_Fixed value containing the scaling factor. */
499 ft_var_apply_tuple( GX_Blend blend
,
500 FT_UShort tupleIndex
,
501 FT_Fixed
* tuple_coords
,
502 FT_Fixed
* im_start_coords
,
503 FT_Fixed
* im_end_coords
)
506 FT_Fixed apply
= 0x10000L
;
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_MulFix( apply
,
529 blend
->normalizedcoords
[i
] > 0
530 ? blend
->normalizedcoords
[i
]
531 : -blend
->normalizedcoords
[i
] );
533 else if ( blend
->normalizedcoords
[i
] <= im_start_coords
[i
] ||
534 blend
->normalizedcoords
[i
] >= im_end_coords
[i
] )
540 else if ( blend
->normalizedcoords
[i
] < tuple_coords
[i
] )
541 apply
= FT_MulDiv( apply
,
542 blend
->normalizedcoords
[i
] - im_start_coords
[i
],
543 tuple_coords
[i
] - im_start_coords
[i
] );
546 apply
= FT_MulDiv( apply
,
547 im_end_coords
[i
] - blend
->normalizedcoords
[i
],
548 im_end_coords
[i
] - tuple_coords
[i
] );
555 /*************************************************************************/
556 /*************************************************************************/
558 /***** MULTIPLE MASTERS SERVICE FUNCTIONS *****/
560 /*************************************************************************/
561 /*************************************************************************/
564 typedef struct GX_FVar_Head_
567 FT_UShort offsetToData
;
568 FT_UShort countSizePairs
;
571 FT_UShort instanceCount
;
572 FT_UShort instanceSize
;
577 typedef struct fvar_axis_
581 FT_ULong defaultValue
;
589 /*************************************************************************/
595 /* Check that the font's `fvar' table is valid, parse it, and return */
599 /* face :: The font face. */
600 /* TT_Get_MM_Var initializes the blend structure. */
603 /* master :: The `fvar' data (must be freed by caller). */
606 /* FreeType error code. 0 means success. */
608 FT_LOCAL_DEF( FT_Error
)
609 TT_Get_MM_Var( TT_Face face
,
612 FT_Stream stream
= face
->root
.stream
;
613 FT_Memory memory
= face
->root
.memory
;
615 FT_Error error
= FT_Err_Ok
;
618 FT_MM_Var
* mmvar
= NULL
;
619 FT_Fixed
* next_coords
;
620 FT_String
* next_name
;
622 FT_Var_Named_Style
* ns
;
623 GX_FVar_Head fvar_head
;
625 static const FT_Frame_Field fvar_fields
[] =
629 #define FT_STRUCTURE GX_FVar_Head
631 FT_FRAME_START( 16 ),
632 FT_FRAME_LONG ( version
),
633 FT_FRAME_USHORT( offsetToData
),
634 FT_FRAME_USHORT( countSizePairs
),
635 FT_FRAME_USHORT( axisCount
),
636 FT_FRAME_USHORT( axisSize
),
637 FT_FRAME_USHORT( instanceCount
),
638 FT_FRAME_USHORT( instanceSize
),
642 static const FT_Frame_Field fvaraxis_fields
[] =
646 #define FT_STRUCTURE GX_FVar_Axis
648 FT_FRAME_START( 20 ),
649 FT_FRAME_ULONG ( axisTag
),
650 FT_FRAME_ULONG ( minValue
),
651 FT_FRAME_ULONG ( defaultValue
),
652 FT_FRAME_ULONG ( maxValue
),
653 FT_FRAME_USHORT( flags
),
654 FT_FRAME_USHORT( nameID
),
659 if ( face
->blend
== NULL
)
661 /* both `fvar' and `gvar' must be present */
662 if ( (error
= face
->goto_table( face
, TTAG_gvar
,
663 stream
, &table_len
)) != 0 )
666 if ( (error
= face
->goto_table( face
, TTAG_fvar
,
667 stream
, &table_len
)) != 0 )
670 fvar_start
= FT_STREAM_POS( );
672 if ( FT_STREAM_READ_FIELDS( fvar_fields
, &fvar_head
) )
675 if ( fvar_head
.version
!= (FT_Long
)0x00010000L
||
676 fvar_head
.countSizePairs
!= 2 ||
677 fvar_head
.axisSize
!= 20 ||
678 /* axisCount limit implied by 16-bit instanceSize */
679 fvar_head
.axisCount
> 0x3FFE ||
680 fvar_head
.instanceSize
!= 4 + 4 * fvar_head
.axisCount
||
681 /* instanceCount limit implied by limited range of name IDs */
682 fvar_head
.instanceCount
> 0x7EFF ||
683 fvar_head
.offsetToData
+ fvar_head
.axisCount
* 20U +
684 fvar_head
.instanceCount
* fvar_head
.instanceSize
> table_len
)
686 error
= FT_THROW( Invalid_Table
);
690 if ( FT_NEW( face
->blend
) )
693 /* cannot overflow 32-bit arithmetic because of limits above */
694 face
->blend
->mmvar_len
=
695 sizeof ( FT_MM_Var
) +
696 fvar_head
.axisCount
* sizeof ( FT_Var_Axis
) +
697 fvar_head
.instanceCount
* sizeof ( FT_Var_Named_Style
) +
698 fvar_head
.instanceCount
* fvar_head
.axisCount
* sizeof ( FT_Fixed
) +
699 5 * fvar_head
.axisCount
;
701 if ( FT_ALLOC( mmvar
, face
->blend
->mmvar_len
) )
703 face
->blend
->mmvar
= mmvar
;
708 ~0U; /* meaningless in this context; each glyph */
709 /* may have a different number of designs */
710 /* (or tuples, as called by Apple) */
711 mmvar
->num_namedstyles
=
712 fvar_head
.instanceCount
;
714 (FT_Var_Axis
*)&(mmvar
[1]);
716 (FT_Var_Named_Style
*)&(mmvar
->axis
[fvar_head
.axisCount
]);
719 (FT_Fixed
*)&(mmvar
->namedstyle
[fvar_head
.instanceCount
]);
720 for ( i
= 0; i
< fvar_head
.instanceCount
; ++i
)
722 mmvar
->namedstyle
[i
].coords
= next_coords
;
723 next_coords
+= fvar_head
.axisCount
;
726 next_name
= (FT_String
*)next_coords
;
727 for ( i
= 0; i
< fvar_head
.axisCount
; ++i
)
729 mmvar
->axis
[i
].name
= next_name
;
733 if ( FT_STREAM_SEEK( fvar_start
+ fvar_head
.offsetToData
) )
737 for ( i
= 0; i
< fvar_head
.axisCount
; ++i
)
739 GX_FVar_Axis axis_rec
;
742 if ( FT_STREAM_READ_FIELDS( fvaraxis_fields
, &axis_rec
) )
744 a
->tag
= axis_rec
.axisTag
;
745 a
->minimum
= axis_rec
.minValue
; /* A Fixed */
746 a
->def
= axis_rec
.defaultValue
; /* A Fixed */
747 a
->maximum
= axis_rec
.maxValue
; /* A Fixed */
748 a
->strid
= axis_rec
.nameID
;
750 a
->name
[0] = (FT_String
)( a
->tag
>> 24 );
751 a
->name
[1] = (FT_String
)( ( a
->tag
>> 16 ) & 0xFF );
752 a
->name
[2] = (FT_String
)( ( a
->tag
>> 8 ) & 0xFF );
753 a
->name
[3] = (FT_String
)( ( a
->tag
) & 0xFF );
759 ns
= mmvar
->namedstyle
;
760 for ( i
= 0; i
< fvar_head
.instanceCount
; ++i
, ++ns
)
762 if ( FT_FRAME_ENTER( 4L + 4L * fvar_head
.axisCount
) )
765 ns
->strid
= FT_GET_USHORT();
766 (void) /* flags = */ FT_GET_USHORT();
768 for ( j
= 0; j
< fvar_head
.axisCount
; ++j
)
769 ns
->coords
[j
] = FT_GET_ULONG(); /* A Fixed */
775 if ( master
!= NULL
)
780 if ( FT_ALLOC( mmvar
, face
->blend
->mmvar_len
) )
782 FT_MEM_COPY( mmvar
, face
->blend
->mmvar
, face
->blend
->mmvar_len
);
785 (FT_Var_Axis
*)&(mmvar
[1]);
787 (FT_Var_Named_Style
*)&(mmvar
->axis
[mmvar
->num_axis
]);
789 (FT_Fixed
*)&(mmvar
->namedstyle
[mmvar
->num_namedstyles
]);
791 for ( n
= 0; n
< mmvar
->num_namedstyles
; ++n
)
793 mmvar
->namedstyle
[n
].coords
= next_coords
;
794 next_coords
+= mmvar
->num_axis
;
798 next_name
= (FT_String
*)next_coords
;
799 for ( n
= 0; n
< mmvar
->num_axis
; ++n
)
803 /* standard PostScript names for some standard apple tags */
804 if ( a
->tag
== TTAG_wght
)
805 a
->name
= (char *)"Weight";
806 else if ( a
->tag
== TTAG_wdth
)
807 a
->name
= (char *)"Width";
808 else if ( a
->tag
== TTAG_opsz
)
809 a
->name
= (char *)"OpticalSize";
810 else if ( a
->tag
== TTAG_slnt
)
811 a
->name
= (char *)"Slant";
825 /*************************************************************************/
828 /* TT_Set_MM_Blend */
831 /* Set the blend (normalized) coordinates for this instance of the */
832 /* font. Check that the `gvar' table is reasonable and does some */
833 /* initial preparation. */
836 /* face :: The font. */
837 /* Initialize the blend structure with `gvar' data. */
840 /* num_coords :: Must be the axis count of the font. */
842 /* coords :: An array of num_coords, each between [-1,1]. */
845 /* FreeType error code. 0 means success. */
847 FT_LOCAL_DEF( FT_Error
)
848 TT_Set_MM_Blend( TT_Face face
,
852 FT_Error error
= FT_Err_Ok
;
856 FT_Memory memory
= face
->root
.memory
;
867 face
->doblend
= FALSE
;
869 if ( face
->blend
== NULL
)
871 if ( (error
= TT_Get_MM_Var( face
, NULL
)) != 0 )
876 mmvar
= blend
->mmvar
;
878 if ( num_coords
!= mmvar
->num_axis
)
880 error
= FT_THROW( Invalid_Argument
);
884 for ( i
= 0; i
< num_coords
; ++i
)
885 if ( coords
[i
] < -0x00010000L
|| coords
[i
] > 0x00010000L
)
887 error
= FT_THROW( Invalid_Argument
);
891 if ( blend
->glyphoffsets
== NULL
)
892 if ( (error
= ft_var_load_gvar( face
)) != 0 )
895 if ( blend
->normalizedcoords
== NULL
)
897 if ( FT_NEW_ARRAY( blend
->normalizedcoords
, num_coords
) )
900 manageCvt
= mcvt_modify
;
902 /* If we have not set the blend coordinates before this, then the */
903 /* cvt table will still be what we read from the `cvt ' table and */
904 /* we don't need to reload it. We may need to change it though... */
908 manageCvt
= mcvt_retain
;
909 for ( i
= 0; i
< num_coords
; ++i
)
911 if ( blend
->normalizedcoords
[i
] != coords
[i
] )
913 manageCvt
= mcvt_load
;
918 /* If we don't change the blend coords then we don't need to do */
919 /* anything to the cvt table. It will be correct. Otherwise we */
920 /* no longer have the original cvt (it was modified when we set */
921 /* the blend last time), so we must reload and then modify it. */
924 blend
->num_axis
= num_coords
;
925 FT_MEM_COPY( blend
->normalizedcoords
,
927 num_coords
* sizeof ( FT_Fixed
) );
929 face
->doblend
= TRUE
;
931 if ( face
->cvt
!= NULL
)
936 /* The cvt table has been loaded already; every time we change the */
937 /* blend we may need to reload and remodify the cvt table. */
938 FT_FREE( face
->cvt
);
941 error
= tt_face_load_cvt( face
, face
->root
.stream
);
945 /* The original cvt table is in memory. All we need to do is */
946 /* apply the `cvar' table (if any). */
947 error
= tt_face_vary_cvt( face
, face
->root
.stream
);
951 /* The cvt table is correct for this set of coordinates. */
961 /*************************************************************************/
964 /* TT_Set_Var_Design */
967 /* Set the coordinates for the instance, measured in the user */
968 /* coordinate system. Parse the `avar' table (if present) to convert */
969 /* from user to normalized coordinates. */
972 /* face :: The font face. */
973 /* Initialize the blend struct with `gvar' data. */
976 /* num_coords :: This must be the axis count of the font. */
978 /* coords :: A coordinate array with `num_coords' elements. */
981 /* FreeType error code. 0 means success. */
983 FT_LOCAL_DEF( FT_Error
)
984 TT_Set_Var_Design( TT_Face face
,
988 FT_Error error
= FT_Err_Ok
;
989 FT_Fixed
* normalized
= NULL
;
995 FT_Memory memory
= face
->root
.memory
;
998 if ( face
->blend
== NULL
)
1000 if ( (error
= TT_Get_MM_Var( face
, NULL
)) != 0 )
1004 blend
= face
->blend
;
1005 mmvar
= blend
->mmvar
;
1007 if ( num_coords
!= mmvar
->num_axis
)
1009 error
= FT_THROW( Invalid_Argument
);
1013 /* Axis normalization is a two stage process. First we normalize */
1014 /* based on the [min,def,max] values for the axis to be [-1,0,1]. */
1015 /* Then, if there's an `avar' table, we renormalize this range. */
1017 if ( FT_NEW_ARRAY( normalized
, mmvar
->num_axis
) )
1021 for ( i
= 0; i
< mmvar
->num_axis
; ++i
, ++a
)
1023 if ( coords
[i
] > a
->maximum
|| coords
[i
] < a
->minimum
)
1025 error
= FT_THROW( Invalid_Argument
);
1029 if ( coords
[i
] < a
->def
)
1030 normalized
[i
] = -FT_DivFix( coords
[i
] - a
->def
, a
->minimum
- a
->def
);
1031 else if ( a
->maximum
== a
->def
)
1034 normalized
[i
] = FT_DivFix( coords
[i
] - a
->def
, a
->maximum
- a
->def
);
1037 if ( !blend
->avar_checked
)
1038 ft_var_load_avar( face
);
1040 if ( blend
->avar_segment
!= NULL
)
1042 av
= blend
->avar_segment
;
1043 for ( i
= 0; i
< mmvar
->num_axis
; ++i
, ++av
)
1045 for ( j
= 1; j
< (FT_UInt
)av
->pairCount
; ++j
)
1046 if ( normalized
[i
] < av
->correspondence
[j
].fromCoord
)
1049 FT_MulDiv( normalized
[i
] - av
->correspondence
[j
- 1].fromCoord
,
1050 av
->correspondence
[j
].toCoord
-
1051 av
->correspondence
[j
- 1].toCoord
,
1052 av
->correspondence
[j
].fromCoord
-
1053 av
->correspondence
[j
- 1].fromCoord
) +
1054 av
->correspondence
[j
- 1].toCoord
;
1060 error
= TT_Set_MM_Blend( face
, num_coords
, normalized
);
1063 FT_FREE( normalized
);
1068 /*************************************************************************/
1069 /*************************************************************************/
1071 /***** GX VAR PARSING ROUTINES *****/
1073 /*************************************************************************/
1074 /*************************************************************************/
1077 /*************************************************************************/
1080 /* tt_face_vary_cvt */
1083 /* Modify the loaded cvt table according to the `cvar' table and the */
1087 /* face :: A handle to the target face object. */
1090 /* stream :: A handle to the input stream. */
1093 /* FreeType error code. 0 means success. */
1095 /* Most errors are ignored. It is perfectly valid not to have a */
1096 /* `cvar' table even if there is a `gvar' and `fvar' table. */
1098 FT_LOCAL_DEF( FT_Error
)
1099 tt_face_vary_cvt( TT_Face face
,
1103 FT_Memory memory
= stream
->memory
;
1104 FT_ULong table_start
;
1107 FT_ULong offsetToData
;
1110 FT_Fixed
* tuple_coords
= NULL
;
1111 FT_Fixed
* im_start_coords
= NULL
;
1112 FT_Fixed
* im_end_coords
= NULL
;
1113 GX_Blend blend
= face
->blend
;
1114 FT_UInt point_count
;
1115 FT_UShort
* localpoints
;
1119 FT_TRACE2(( "CVAR " ));
1121 if ( blend
== NULL
)
1123 FT_TRACE2(( "tt_face_vary_cvt: no blend specified\n" ));
1129 if ( face
->cvt
== NULL
)
1131 FT_TRACE2(( "tt_face_vary_cvt: no `cvt ' table\n" ));
1137 error
= face
->goto_table( face
, TTAG_cvar
, stream
, &table_len
);
1140 FT_TRACE2(( "is missing\n" ));
1146 if ( FT_FRAME_ENTER( table_len
) )
1152 table_start
= FT_Stream_FTell( stream
);
1153 if ( FT_GET_LONG() != 0x00010000L
)
1155 FT_TRACE2(( "bad table version\n" ));
1161 if ( FT_NEW_ARRAY( tuple_coords
, blend
->num_axis
) ||
1162 FT_NEW_ARRAY( im_start_coords
, blend
->num_axis
) ||
1163 FT_NEW_ARRAY( im_end_coords
, blend
->num_axis
) )
1166 tupleCount
= FT_GET_USHORT();
1167 offsetToData
= table_start
+ FT_GET_USHORT();
1169 /* The documentation implies there are flags packed into the */
1170 /* tuplecount, but John Jenkins says that shared points don't apply */
1171 /* to `cvar', and no other flags are defined. */
1173 for ( i
= 0; i
< ( tupleCount
& 0xFFF ); ++i
)
1175 FT_UInt tupleDataSize
;
1180 tupleDataSize
= FT_GET_USHORT();
1181 tupleIndex
= FT_GET_USHORT();
1183 /* There is no provision here for a global tuple coordinate section, */
1184 /* so John says. There are no tuple indices, just embedded tuples. */
1186 if ( tupleIndex
& GX_TI_EMBEDDED_TUPLE_COORD
)
1188 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1189 tuple_coords
[j
] = FT_GET_SHORT() << 2; /* convert from */
1190 /* short frac to fixed */
1194 /* skip this tuple; it makes no sense */
1196 if ( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
)
1197 for ( j
= 0; j
< 2 * blend
->num_axis
; ++j
)
1198 (void)FT_GET_SHORT();
1200 offsetToData
+= tupleDataSize
;
1204 if ( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
)
1206 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1207 im_start_coords
[j
] = FT_GET_SHORT() << 2;
1208 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1209 im_end_coords
[j
] = FT_GET_SHORT() << 2;
1212 apply
= ft_var_apply_tuple( blend
,
1213 (FT_UShort
)tupleIndex
,
1217 if ( /* tuple isn't active for our blend */
1219 /* global points not allowed, */
1220 /* if they aren't local, makes no sense */
1221 !( tupleIndex
& GX_TI_PRIVATE_POINT_NUMBERS
) )
1223 offsetToData
+= tupleDataSize
;
1227 here
= FT_Stream_FTell( stream
);
1229 FT_Stream_SeekSet( stream
, offsetToData
);
1231 localpoints
= ft_var_readpackedpoints( stream
, &point_count
);
1232 deltas
= ft_var_readpackeddeltas( stream
,
1233 point_count
== 0 ? face
->cvt_size
1235 if ( localpoints
== NULL
|| deltas
== NULL
)
1236 /* failure, ignore it */;
1238 else if ( localpoints
== ALL_POINTS
)
1240 /* this means that there are deltas for every entry in cvt */
1241 for ( j
= 0; j
< face
->cvt_size
; ++j
)
1242 face
->cvt
[j
] = (FT_Short
)( face
->cvt
[j
] +
1243 FT_MulFix( deltas
[j
], apply
) );
1248 for ( j
= 0; j
< point_count
; ++j
)
1250 int pindex
= localpoints
[j
];
1252 face
->cvt
[pindex
] = (FT_Short
)( face
->cvt
[pindex
] +
1253 FT_MulFix( deltas
[j
], apply
) );
1257 if ( localpoints
!= ALL_POINTS
)
1258 FT_FREE( localpoints
);
1261 offsetToData
+= tupleDataSize
;
1263 FT_Stream_SeekSet( stream
, here
);
1270 FT_FREE( tuple_coords
);
1271 FT_FREE( im_start_coords
);
1272 FT_FREE( im_end_coords
);
1278 /*************************************************************************/
1281 /* TT_Vary_Get_Glyph_Deltas */
1284 /* Load the appropriate deltas for the current glyph. */
1287 /* face :: A handle to the target face object. */
1289 /* glyph_index :: The index of the glyph being modified. */
1291 /* n_points :: The number of the points in the glyph, including */
1292 /* phantom points. */
1295 /* deltas :: The array of points to change. */
1298 /* FreeType error code. 0 means success. */
1300 FT_LOCAL_DEF( FT_Error
)
1301 TT_Vary_Get_Glyph_Deltas( TT_Face face
,
1302 FT_UInt glyph_index
,
1306 FT_Stream stream
= face
->root
.stream
;
1307 FT_Memory memory
= stream
->memory
;
1308 GX_Blend blend
= face
->blend
;
1309 FT_Vector
* delta_xy
= NULL
;
1312 FT_ULong glyph_start
;
1314 FT_ULong offsetToData
;
1317 FT_Fixed
* tuple_coords
= NULL
;
1318 FT_Fixed
* im_start_coords
= NULL
;
1319 FT_Fixed
* im_end_coords
= NULL
;
1320 FT_UInt point_count
, spoint_count
= 0;
1321 FT_UShort
* sharedpoints
= NULL
;
1322 FT_UShort
* localpoints
= NULL
;
1324 FT_Short
*deltas_x
, *deltas_y
;
1327 if ( !face
->doblend
|| blend
== NULL
)
1328 return FT_THROW( Invalid_Argument
);
1330 /* to be freed by the caller */
1331 if ( FT_NEW_ARRAY( delta_xy
, n_points
) )
1335 if ( glyph_index
>= blend
->gv_glyphcnt
||
1336 blend
->glyphoffsets
[glyph_index
] ==
1337 blend
->glyphoffsets
[glyph_index
+ 1] )
1338 return FT_Err_Ok
; /* no variation data for this glyph */
1340 if ( FT_STREAM_SEEK( blend
->glyphoffsets
[glyph_index
] ) ||
1341 FT_FRAME_ENTER( blend
->glyphoffsets
[glyph_index
+ 1] -
1342 blend
->glyphoffsets
[glyph_index
] ) )
1345 glyph_start
= FT_Stream_FTell( stream
);
1347 /* each set of glyph variation data is formatted similarly to `cvar' */
1348 /* (except we get shared points and global tuples) */
1350 if ( FT_NEW_ARRAY( tuple_coords
, blend
->num_axis
) ||
1351 FT_NEW_ARRAY( im_start_coords
, blend
->num_axis
) ||
1352 FT_NEW_ARRAY( im_end_coords
, blend
->num_axis
) )
1355 tupleCount
= FT_GET_USHORT();
1356 offsetToData
= glyph_start
+ FT_GET_USHORT();
1358 if ( tupleCount
& GX_TC_TUPLES_SHARE_POINT_NUMBERS
)
1360 here
= FT_Stream_FTell( stream
);
1362 FT_Stream_SeekSet( stream
, offsetToData
);
1364 sharedpoints
= ft_var_readpackedpoints( stream
, &spoint_count
);
1365 offsetToData
= FT_Stream_FTell( stream
);
1367 FT_Stream_SeekSet( stream
, here
);
1370 for ( i
= 0; i
< ( tupleCount
& GX_TC_TUPLE_COUNT_MASK
); ++i
)
1372 FT_UInt tupleDataSize
;
1377 tupleDataSize
= FT_GET_USHORT();
1378 tupleIndex
= FT_GET_USHORT();
1380 if ( tupleIndex
& GX_TI_EMBEDDED_TUPLE_COORD
)
1382 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1383 tuple_coords
[j
] = FT_GET_SHORT() << 2; /* convert from */
1384 /* short frac to fixed */
1386 else if ( ( tupleIndex
& GX_TI_TUPLE_INDEX_MASK
) >= blend
->tuplecount
)
1388 error
= FT_THROW( Invalid_Table
);
1395 &blend
->tuplecoords
[(tupleIndex
& 0xFFF) * blend
->num_axis
],
1396 blend
->num_axis
* sizeof ( FT_Fixed
) );
1399 if ( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
)
1401 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1402 im_start_coords
[j
] = FT_GET_SHORT() << 2;
1403 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1404 im_end_coords
[j
] = FT_GET_SHORT() << 2;
1407 apply
= ft_var_apply_tuple( blend
,
1408 (FT_UShort
)tupleIndex
,
1413 if ( apply
== 0 ) /* tuple isn't active for our blend */
1415 offsetToData
+= tupleDataSize
;
1419 here
= FT_Stream_FTell( stream
);
1421 if ( tupleIndex
& GX_TI_PRIVATE_POINT_NUMBERS
)
1423 FT_Stream_SeekSet( stream
, offsetToData
);
1425 localpoints
= ft_var_readpackedpoints( stream
, &point_count
);
1426 points
= localpoints
;
1430 points
= sharedpoints
;
1431 point_count
= spoint_count
;
1434 deltas_x
= ft_var_readpackeddeltas( stream
,
1435 point_count
== 0 ? n_points
1437 deltas_y
= ft_var_readpackeddeltas( stream
,
1438 point_count
== 0 ? n_points
1441 if ( points
== NULL
|| deltas_y
== NULL
|| deltas_x
== NULL
)
1442 ; /* failure, ignore it */
1444 else if ( points
== ALL_POINTS
)
1446 /* this means that there are deltas for every point in the glyph */
1447 for ( j
= 0; j
< n_points
; ++j
)
1449 delta_xy
[j
].x
+= FT_MulFix( deltas_x
[j
], apply
);
1450 delta_xy
[j
].y
+= FT_MulFix( deltas_y
[j
], apply
);
1456 for ( j
= 0; j
< point_count
; ++j
)
1458 if ( localpoints
[j
] >= n_points
)
1461 delta_xy
[localpoints
[j
]].x
+= FT_MulFix( deltas_x
[j
], apply
);
1462 delta_xy
[localpoints
[j
]].y
+= FT_MulFix( deltas_y
[j
], apply
);
1466 if ( localpoints
!= ALL_POINTS
)
1467 FT_FREE( localpoints
);
1468 FT_FREE( deltas_x
);
1469 FT_FREE( deltas_y
);
1471 offsetToData
+= tupleDataSize
;
1473 FT_Stream_SeekSet( stream
, here
);
1477 FT_FREE( tuple_coords
);
1478 FT_FREE( im_start_coords
);
1479 FT_FREE( im_end_coords
);
1487 FT_FREE( delta_xy
);
1496 /*************************************************************************/
1502 /* Frees the blend internal data structure. */
1504 FT_LOCAL_DEF( void )
1505 tt_done_blend( FT_Memory memory
,
1508 if ( blend
!= NULL
)
1513 FT_FREE( blend
->normalizedcoords
);
1514 FT_FREE( blend
->mmvar
);
1516 if ( blend
->avar_segment
!= NULL
)
1518 for ( i
= 0; i
< blend
->num_axis
; ++i
)
1519 FT_FREE( blend
->avar_segment
[i
].correspondence
);
1520 FT_FREE( blend
->avar_segment
);
1523 FT_FREE( blend
->tuplecoords
);
1524 FT_FREE( blend
->glyphoffsets
);
1529 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */