1 /***************************************************************************/
5 /* TrueType GX Font Variation loader */
7 /* Copyright 2004, 2005, 2006, 2007, 2008 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 be 2. */
28 /* The documentation for `gvar' is not intelligible; `cvar' refers you to */
29 /* `gvar' and is thus also incomprehensible. */
31 /* The documentation for `avar' appears correct, but Apple has no fonts */
32 /* with an `avar' table, so it is hard to test. */
34 /* Many thanks to John Jenkins (at Apple) in figuring this out. */
37 /* Apple's `kern' table has some references to tuple indices, but as there */
38 /* is no indication where these indices are defined, nor how to */
39 /* interpolate the kerning values (different tuples have different */
40 /* classes) this issue is ignored. */
42 /***************************************************************************/
46 #include FT_INTERNAL_DEBUG_H
47 #include FT_CONFIG_CONFIG_H
48 #include FT_INTERNAL_STREAM_H
49 #include FT_INTERNAL_SFNT_H
50 #include FT_TRUETYPE_IDS_H
51 #include FT_TRUETYPE_TAGS_H
52 #include FT_MULTIPLE_MASTERS_H
61 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
64 #define FT_Stream_FTell( stream ) \
65 ( (stream)->cursor - (stream)->base )
66 #define FT_Stream_SeekSet( stream, off ) \
67 ( (stream)->cursor = (stream)->base+(off) )
70 /*************************************************************************/
72 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
73 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
74 /* messages during execution. */
77 #define FT_COMPONENT trace_ttgxvar
80 /*************************************************************************/
81 /*************************************************************************/
83 /***** Internal Routines *****/
85 /*************************************************************************/
86 /*************************************************************************/
89 /*************************************************************************/
91 /* The macro ALL_POINTS is used in `ft_var_readpackedpoints'. It */
92 /* indicates that there is a delta for every point without needing to */
93 /* enumerate all of them. */
95 #define ALL_POINTS (FT_UShort*)( -1 )
100 GX_PT_POINTS_ARE_WORDS
= 0x80,
101 GX_PT_POINT_RUN_COUNT_MASK
= 0x7F
105 /*************************************************************************/
108 /* ft_var_readpackedpoints */
111 /* Read a set of points to which the following deltas will apply. */
112 /* Points are packed with a run length encoding. */
115 /* stream :: The data stream. */
118 /* point_cnt :: The number of points read. A zero value means that */
119 /* all points in the glyph will be affected, without */
120 /* enumerating them individually. */
123 /* An array of FT_UShort containing the affected points or the */
124 /* special value ALL_POINTS. */
127 ft_var_readpackedpoints( FT_Stream stream
,
136 FT_Memory memory
= stream
->memory
;
137 FT_Error error
= TT_Err_Ok
;
142 *point_cnt
= n
= FT_GET_BYTE();
146 if ( n
& GX_PT_POINTS_ARE_WORDS
)
147 n
= FT_GET_BYTE() | ( ( n
& GX_PT_POINT_RUN_COUNT_MASK
) << 8 );
149 if ( FT_NEW_ARRAY( points
, n
) )
155 runcnt
= FT_GET_BYTE();
156 if ( runcnt
& GX_PT_POINTS_ARE_WORDS
)
158 runcnt
= runcnt
& GX_PT_POINT_RUN_COUNT_MASK
;
159 first
= points
[i
++] = FT_GET_USHORT();
161 /* first point not included in runcount */
162 for ( j
= 0; j
< runcnt
; ++j
)
163 points
[i
++] = (FT_UShort
)( first
+= FT_GET_USHORT() );
167 first
= points
[i
++] = FT_GET_BYTE();
169 for ( j
= 0; j
< runcnt
; ++j
)
170 points
[i
++] = (FT_UShort
)( first
+= FT_GET_BYTE() );
180 GX_DT_DELTAS_ARE_ZERO
= 0x80,
181 GX_DT_DELTAS_ARE_WORDS
= 0x40,
182 GX_DT_DELTA_RUN_COUNT_MASK
= 0x3F
186 /*************************************************************************/
189 /* ft_var_readpackeddeltas */
192 /* Read a set of deltas. These are packed slightly differently than */
193 /* points. In particular there is no overall count. */
196 /* stream :: The data stream. */
198 /* delta_cnt :: The number of to be read. */
201 /* An array of FT_Short containing the deltas for the affected */
202 /* points. (This only gets the deltas for one dimension. It will */
203 /* generally be called twice, once for x, once for y. When used in */
204 /* cvt table, it will only be called once.) */
207 ft_var_readpackeddeltas( FT_Stream stream
,
214 FT_Memory memory
= stream
->memory
;
215 FT_Error error
= TT_Err_Ok
;
220 if ( FT_NEW_ARRAY( deltas
, delta_cnt
) )
224 while ( i
< delta_cnt
)
226 runcnt
= FT_GET_BYTE();
227 if ( runcnt
& GX_DT_DELTAS_ARE_ZERO
)
229 /* runcnt zeroes get added */
231 j
<= ( runcnt
& GX_DT_DELTA_RUN_COUNT_MASK
) && i
< delta_cnt
;
235 else if ( runcnt
& GX_DT_DELTAS_ARE_WORDS
)
237 /* runcnt shorts from the stack */
239 j
<= ( runcnt
& GX_DT_DELTA_RUN_COUNT_MASK
) && i
< delta_cnt
;
241 deltas
[i
++] = FT_GET_SHORT();
245 /* runcnt signed bytes from the stack */
247 j
<= ( runcnt
& GX_DT_DELTA_RUN_COUNT_MASK
) && i
< delta_cnt
;
249 deltas
[i
++] = FT_GET_CHAR();
252 if ( j
<= ( runcnt
& GX_DT_DELTA_RUN_COUNT_MASK
) )
264 /*************************************************************************/
267 /* ft_var_load_avar */
270 /* Parse the `avar' table if present. It need not be, so we return */
274 /* face :: The font face. */
277 ft_var_load_avar( TT_Face face
)
279 FT_Stream stream
= FT_FACE_STREAM(face
);
280 FT_Memory memory
= stream
->memory
;
281 GX_Blend blend
= face
->blend
;
282 GX_AVarSegment segment
;
283 FT_Error error
= TT_Err_Ok
;
292 blend
->avar_checked
= TRUE
;
293 if ( (error
= face
->goto_table( face
, TTAG_avar
, stream
, &table_len
)) != 0 )
296 if ( FT_FRAME_ENTER( table_len
) )
299 version
= FT_GET_LONG();
300 axisCount
= FT_GET_LONG();
302 if ( version
!= 0x00010000L
||
303 axisCount
!= (FT_Long
)blend
->mmvar
->num_axis
)
306 if ( FT_NEW_ARRAY( blend
->avar_segment
, axisCount
) )
309 segment
= &blend
->avar_segment
[0];
310 for ( i
= 0; i
< axisCount
; ++i
, ++segment
)
312 segment
->pairCount
= FT_GET_USHORT();
313 if ( FT_NEW_ARRAY( segment
->correspondence
, segment
->pairCount
) )
315 /* Failure. Free everything we have done so far. We must do */
316 /* it right now since loading the `avar' table is optional. */
318 for ( j
= i
- 1; j
>= 0; --j
)
319 FT_FREE( blend
->avar_segment
[j
].correspondence
);
321 FT_FREE( blend
->avar_segment
);
322 blend
->avar_segment
= NULL
;
326 for ( j
= 0; j
< segment
->pairCount
; ++j
)
328 segment
->correspondence
[j
].fromCoord
=
329 FT_GET_SHORT() << 2; /* convert to Fixed */
330 segment
->correspondence
[j
].toCoord
=
331 FT_GET_SHORT()<<2; /* convert to Fixed */
340 typedef struct GX_GVar_Head_
344 FT_UShort globalCoordCount
;
345 FT_ULong offsetToCoord
;
346 FT_UShort glyphCount
;
348 FT_ULong offsetToData
;
353 /*************************************************************************/
356 /* ft_var_load_gvar */
359 /* Parses the `gvar' table if present. If `fvar' is there, `gvar' */
360 /* had better be there too. */
363 /* face :: The font face. */
366 /* FreeType error code. 0 means success. */
369 ft_var_load_gvar( TT_Face face
)
371 FT_Stream stream
= FT_FACE_STREAM(face
);
372 FT_Memory memory
= stream
->memory
;
373 GX_Blend blend
= face
->blend
;
378 FT_ULong offsetToData
;
379 GX_GVar_Head gvar_head
;
381 static const FT_Frame_Field gvar_fields
[] =
385 #define FT_STRUCTURE GX_GVar_Head
387 FT_FRAME_START( 20 ),
388 FT_FRAME_LONG ( version
),
389 FT_FRAME_USHORT( axisCount
),
390 FT_FRAME_USHORT( globalCoordCount
),
391 FT_FRAME_ULONG ( offsetToCoord
),
392 FT_FRAME_USHORT( glyphCount
),
393 FT_FRAME_USHORT( flags
),
394 FT_FRAME_ULONG ( offsetToData
),
398 if ( (error
= face
->goto_table( face
, TTAG_gvar
, stream
, &table_len
)) != 0 )
401 gvar_start
= FT_STREAM_POS( );
402 if ( FT_STREAM_READ_FIELDS( gvar_fields
, &gvar_head
) )
405 blend
->tuplecount
= gvar_head
.globalCoordCount
;
406 blend
->gv_glyphcnt
= gvar_head
.glyphCount
;
407 offsetToData
= gvar_start
+ gvar_head
.offsetToData
;
409 if ( gvar_head
.version
!= (FT_Long
)0x00010000L
||
410 gvar_head
.axisCount
!= (FT_UShort
)blend
->mmvar
->num_axis
)
412 error
= TT_Err_Invalid_Table
;
416 if ( FT_NEW_ARRAY( blend
->glyphoffsets
, blend
->gv_glyphcnt
+ 1 ) )
419 if ( gvar_head
.flags
& 1 )
421 /* long offsets (one more offset than glyphs, to mark size of last) */
422 if ( FT_FRAME_ENTER( ( blend
->gv_glyphcnt
+ 1 ) * 4L ) )
425 for ( i
= 0; i
<= blend
->gv_glyphcnt
; ++i
)
426 blend
->glyphoffsets
[i
] = offsetToData
+ FT_GET_LONG();
432 /* short offsets (one more offset than glyphs, to mark size of last) */
433 if ( FT_FRAME_ENTER( ( blend
->gv_glyphcnt
+ 1 ) * 2L ) )
436 for ( i
= 0; i
<= blend
->gv_glyphcnt
; ++i
)
437 blend
->glyphoffsets
[i
] = offsetToData
+ FT_GET_USHORT() * 2;
438 /* XXX: Undocumented: `*2'! */
443 if ( blend
->tuplecount
!= 0 )
445 if ( FT_NEW_ARRAY( blend
->tuplecoords
,
446 gvar_head
.axisCount
* blend
->tuplecount
) )
449 if ( FT_STREAM_SEEK( gvar_start
+ gvar_head
.offsetToCoord
) ||
450 FT_FRAME_ENTER( blend
->tuplecount
* gvar_head
.axisCount
* 2L ) )
453 for ( i
= 0; i
< blend
->tuplecount
; ++i
)
454 for ( j
= 0 ; j
< (FT_UInt
)gvar_head
.axisCount
; ++j
)
455 blend
->tuplecoords
[i
* gvar_head
.axisCount
+ j
] =
456 FT_GET_SHORT() << 2; /* convert to FT_Fixed */
466 /*************************************************************************/
469 /* ft_var_apply_tuple */
472 /* Figure out whether a given tuple (design) applies to the current */
473 /* blend, and if so, what is the scaling factor. */
476 /* blend :: The current blend of the font. */
478 /* tupleIndex :: A flag saying whether this is an intermediate */
481 /* tuple_coords :: The coordinates of the tuple in normalized axis */
484 /* im_start_coords :: The initial coordinates where this tuple starts */
485 /* to apply (for intermediate coordinates). */
487 /* im_end_coords :: The final coordinates after which this tuple no */
488 /* longer applies (for intermediate coordinates). */
491 /* An FT_Fixed value containing the scaling factor. */
494 ft_var_apply_tuple( GX_Blend blend
,
495 FT_UShort tupleIndex
,
496 FT_Fixed
* tuple_coords
,
497 FT_Fixed
* im_start_coords
,
498 FT_Fixed
* im_end_coords
)
506 for ( i
= 0; i
< blend
->num_axis
; ++i
)
508 if ( tuple_coords
[i
] == 0 )
509 /* It's not clear why (for intermediate tuples) we don't need */
510 /* to check against start/end -- the documentation says we don't. */
511 /* Similarly, it's unclear why we don't need to scale along the */
515 else if ( blend
->normalizedcoords
[i
] == 0 ||
516 ( blend
->normalizedcoords
[i
] < 0 && tuple_coords
[i
] > 0 ) ||
517 ( blend
->normalizedcoords
[i
] > 0 && tuple_coords
[i
] < 0 ) )
523 else if ( !( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
) )
524 /* not an intermediate tuple */
525 apply
= FT_MulDiv( apply
,
526 blend
->normalizedcoords
[i
] > 0
527 ? blend
->normalizedcoords
[i
]
528 : -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
] )
540 temp
= FT_MulDiv( blend
->normalizedcoords
[i
] - im_start_coords
[i
],
542 tuple_coords
[i
] - im_start_coords
[i
]);
543 apply
= FT_MulDiv( apply
, temp
, 0x10000L
);
548 temp
= FT_MulDiv( im_end_coords
[i
] - blend
->normalizedcoords
[i
],
550 im_end_coords
[i
] - tuple_coords
[i
] );
551 apply
= FT_MulDiv( apply
, temp
, 0x10000L
);
559 /*************************************************************************/
560 /*************************************************************************/
562 /***** MULTIPLE MASTERS SERVICE FUNCTIONS *****/
564 /*************************************************************************/
565 /*************************************************************************/
568 typedef struct GX_FVar_Head_
571 FT_UShort offsetToData
;
572 FT_UShort countSizePairs
;
575 FT_UShort instanceCount
;
576 FT_UShort instanceSize
;
581 typedef struct fvar_axis_
585 FT_ULong defaultValue
;
593 /*************************************************************************/
599 /* Check that the font's `fvar' table is valid, parse it, and return */
603 /* face :: The font face. */
604 /* TT_Get_MM_Var initializes the blend structure. */
607 /* master :: The `fvar' data (must be freed by caller). */
610 /* FreeType error code. 0 means success. */
612 FT_LOCAL_DEF( FT_Error
)
613 TT_Get_MM_Var( TT_Face face
,
616 FT_Stream stream
= face
->root
.stream
;
617 FT_Memory memory
= face
->root
.memory
;
619 FT_Error error
= TT_Err_Ok
;
623 FT_Fixed
* next_coords
;
624 FT_String
* next_name
;
626 FT_Var_Named_Style
* ns
;
627 GX_FVar_Head fvar_head
;
629 static const FT_Frame_Field fvar_fields
[] =
633 #define FT_STRUCTURE GX_FVar_Head
635 FT_FRAME_START( 16 ),
636 FT_FRAME_LONG ( version
),
637 FT_FRAME_USHORT( offsetToData
),
638 FT_FRAME_USHORT( countSizePairs
),
639 FT_FRAME_USHORT( axisCount
),
640 FT_FRAME_USHORT( axisSize
),
641 FT_FRAME_USHORT( instanceCount
),
642 FT_FRAME_USHORT( instanceSize
),
646 static const FT_Frame_Field fvaraxis_fields
[] =
650 #define FT_STRUCTURE GX_FVar_Axis
652 FT_FRAME_START( 20 ),
653 FT_FRAME_ULONG ( axisTag
),
654 FT_FRAME_ULONG ( minValue
),
655 FT_FRAME_ULONG ( defaultValue
),
656 FT_FRAME_ULONG ( maxValue
),
657 FT_FRAME_USHORT( flags
),
658 FT_FRAME_USHORT( nameID
),
663 if ( face
->blend
== NULL
)
665 /* both `fvar' and `gvar' must be present */
666 if ( (error
= face
->goto_table( face
, TTAG_gvar
,
667 stream
, &table_len
)) != 0 )
670 if ( (error
= face
->goto_table( face
, TTAG_fvar
,
671 stream
, &table_len
)) != 0 )
674 fvar_start
= FT_STREAM_POS( );
676 if ( FT_STREAM_READ_FIELDS( fvar_fields
, &fvar_head
) )
679 if ( fvar_head
.version
!= (FT_Long
)0x00010000L
||
680 fvar_head
.countSizePairs
!= 2 ||
681 fvar_head
.axisSize
!= 20 ||
682 fvar_head
.instanceSize
!= 4 + 4 * fvar_head
.axisCount
||
683 fvar_head
.offsetToData
+ fvar_head
.axisCount
* 20U +
684 fvar_head
.instanceCount
* fvar_head
.instanceSize
> table_len
)
686 error
= TT_Err_Invalid_Table
;
690 if ( FT_NEW( face
->blend
) )
693 /* XXX: TODO - check for overflows */
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 (FT_UInt
)-1; /* 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
= TT_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
= TT_Err_Invalid_Argument
;
884 for ( i
= 0; i
< num_coords
; ++i
)
885 if ( coords
[i
] < -0x00010000L
|| coords
[i
] > 0x00010000L
)
887 error
= TT_Err_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... */
909 i
< num_coords
&& blend
->normalizedcoords
[i
] == coords
[i
];
911 if ( i
== num_coords
)
912 manageCvt
= mcvt_retain
;
914 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
)
1029 normalized
[i
] = -FT_MulDiv( coords
[i
] - a
->def
,
1031 a
->minimum
- a
->def
);
1033 else if ( a
->maximum
== a
->def
)
1037 normalized
[i
] = FT_MulDiv( coords
[i
] - a
->def
,
1039 a
->maximum
- a
->def
);
1043 if ( !blend
->avar_checked
)
1044 ft_var_load_avar( face
);
1046 if ( blend
->avar_segment
!= NULL
)
1048 av
= blend
->avar_segment
;
1049 for ( i
= 0; i
< mmvar
->num_axis
; ++i
, ++av
)
1051 for ( j
= 1; j
< (FT_UInt
)av
->pairCount
; ++j
)
1052 if ( normalized
[i
] < av
->correspondence
[j
].fromCoord
)
1057 normalized
[i
] - av
->correspondence
[j
- 1].fromCoord
,
1059 av
->correspondence
[j
].fromCoord
-
1060 av
->correspondence
[j
- 1].fromCoord
),
1061 av
->correspondence
[j
].toCoord
-
1062 av
->correspondence
[j
- 1].toCoord
,
1064 av
->correspondence
[j
- 1].toCoord
;
1070 error
= TT_Set_MM_Blend( face
, num_coords
, normalized
);
1073 FT_FREE( normalized
);
1078 /*************************************************************************/
1079 /*************************************************************************/
1081 /***** GX VAR PARSING ROUTINES *****/
1083 /*************************************************************************/
1084 /*************************************************************************/
1087 /*************************************************************************/
1090 /* tt_face_vary_cvt */
1093 /* Modify the loaded cvt table according to the `cvar' table and the */
1097 /* face :: A handle to the target face object. */
1100 /* stream :: A handle to the input stream. */
1103 /* FreeType error code. 0 means success. */
1105 /* Most errors are ignored. It is perfectly valid not to have a */
1106 /* `cvar' table even if there is a `gvar' and `fvar' table. */
1108 FT_LOCAL_DEF( FT_Error
)
1109 tt_face_vary_cvt( TT_Face face
,
1113 FT_Memory memory
= stream
->memory
;
1114 FT_ULong table_start
;
1117 FT_ULong offsetToData
;
1120 FT_Fixed
* tuple_coords
= NULL
;
1121 FT_Fixed
* im_start_coords
= NULL
;
1122 FT_Fixed
* im_end_coords
= NULL
;
1123 GX_Blend blend
= face
->blend
;
1124 FT_UInt point_count
;
1125 FT_UShort
* localpoints
;
1129 FT_TRACE2(( "CVAR " ));
1131 if ( blend
== NULL
)
1133 FT_TRACE2(( "no blend specified!\n" ));
1139 if ( face
->cvt
== NULL
)
1141 FT_TRACE2(( "no `cvt ' table!\n" ));
1147 error
= face
->goto_table( face
, TTAG_cvar
, stream
, &table_len
);
1150 FT_TRACE2(( "is missing!\n" ));
1156 if ( FT_FRAME_ENTER( table_len
) )
1162 table_start
= FT_Stream_FTell( stream
);
1163 if ( FT_GET_LONG() != 0x00010000L
)
1165 FT_TRACE2(( "bad table version!\n" ));
1171 if ( FT_NEW_ARRAY( tuple_coords
, blend
->num_axis
) ||
1172 FT_NEW_ARRAY( im_start_coords
, blend
->num_axis
) ||
1173 FT_NEW_ARRAY( im_end_coords
, blend
->num_axis
) )
1176 tupleCount
= FT_GET_USHORT();
1177 offsetToData
= table_start
+ FT_GET_USHORT();
1179 /* The documentation implies there are flags packed into the */
1180 /* tuplecount, but John Jenkins says that shared points don't apply */
1181 /* to `cvar', and no other flags are defined. */
1183 for ( i
= 0; i
< ( tupleCount
& 0xFFF ); ++i
)
1185 FT_UInt tupleDataSize
;
1190 tupleDataSize
= FT_GET_USHORT();
1191 tupleIndex
= FT_GET_USHORT();
1193 /* There is no provision here for a global tuple coordinate section, */
1194 /* so John says. There are no tuple indices, just embedded tuples. */
1196 if ( tupleIndex
& GX_TI_EMBEDDED_TUPLE_COORD
)
1198 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1199 tuple_coords
[j
] = FT_GET_SHORT() << 2; /* convert from */
1200 /* short frac to fixed */
1204 /* skip this tuple; it makes no sense */
1206 if ( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
)
1207 for ( j
= 0; j
< 2 * blend
->num_axis
; ++j
)
1208 (void)FT_GET_SHORT();
1210 offsetToData
+= tupleDataSize
;
1214 if ( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
)
1216 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1217 im_start_coords
[j
] = FT_GET_SHORT() << 2;
1218 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1219 im_end_coords
[j
] = FT_GET_SHORT() << 2;
1222 apply
= ft_var_apply_tuple( blend
,
1223 (FT_UShort
)tupleIndex
,
1227 if ( /* tuple isn't active for our blend */
1229 /* global points not allowed, */
1230 /* if they aren't local, makes no sense */
1231 !( tupleIndex
& GX_TI_PRIVATE_POINT_NUMBERS
) )
1233 offsetToData
+= tupleDataSize
;
1237 here
= FT_Stream_FTell( stream
);
1239 FT_Stream_SeekSet( stream
, offsetToData
);
1241 localpoints
= ft_var_readpackedpoints( stream
, &point_count
);
1242 deltas
= ft_var_readpackeddeltas( stream
,
1243 point_count
== 0 ? face
->cvt_size
1245 if ( localpoints
== NULL
|| deltas
== NULL
)
1246 /* failure, ignore it */;
1248 else if ( localpoints
== ALL_POINTS
)
1250 /* this means that there are deltas for every entry in cvt */
1251 for ( j
= 0; j
< face
->cvt_size
; ++j
)
1252 face
->cvt
[j
] = (FT_Short
)( face
->cvt
[j
] +
1253 FT_MulFix( deltas
[j
], apply
) );
1258 for ( j
= 0; j
< point_count
; ++j
)
1260 int pindex
= localpoints
[j
];
1262 face
->cvt
[pindex
] = (FT_Short
)( face
->cvt
[pindex
] +
1263 FT_MulFix( deltas
[j
], apply
) );
1267 if ( localpoints
!= ALL_POINTS
)
1268 FT_FREE( localpoints
);
1271 offsetToData
+= tupleDataSize
;
1273 FT_Stream_SeekSet( stream
, here
);
1280 FT_FREE( tuple_coords
);
1281 FT_FREE( im_start_coords
);
1282 FT_FREE( im_end_coords
);
1288 /*************************************************************************/
1291 /* TT_Vary_Get_Glyph_Deltas */
1294 /* Load the appropriate deltas for the current glyph. */
1297 /* face :: A handle to the target face object. */
1299 /* glyph_index :: The index of the glyph being modified. */
1301 /* n_points :: The number of the points in the glyph, including */
1302 /* phantom points. */
1305 /* deltas :: The array of points to change. */
1308 /* FreeType error code. 0 means success. */
1310 FT_LOCAL_DEF( FT_Error
)
1311 TT_Vary_Get_Glyph_Deltas( TT_Face face
,
1312 FT_UInt glyph_index
,
1316 FT_Stream stream
= face
->root
.stream
;
1317 FT_Memory memory
= stream
->memory
;
1318 GX_Blend blend
= face
->blend
;
1319 FT_Vector
* delta_xy
;
1322 FT_ULong glyph_start
;
1324 FT_ULong offsetToData
;
1327 FT_Fixed
* tuple_coords
= NULL
;
1328 FT_Fixed
* im_start_coords
= NULL
;
1329 FT_Fixed
* im_end_coords
= NULL
;
1330 FT_UInt point_count
, spoint_count
= 0;
1331 FT_UShort
* sharedpoints
= NULL
;
1332 FT_UShort
* localpoints
= NULL
;
1334 FT_Short
*deltas_x
, *deltas_y
;
1337 if ( !face
->doblend
|| blend
== NULL
)
1338 return TT_Err_Invalid_Argument
;
1340 /* to be freed by the caller */
1341 if ( FT_NEW_ARRAY( delta_xy
, n_points
) )
1345 if ( glyph_index
>= blend
->gv_glyphcnt
||
1346 blend
->glyphoffsets
[glyph_index
] ==
1347 blend
->glyphoffsets
[glyph_index
+ 1] )
1348 return TT_Err_Ok
; /* no variation data for this glyph */
1350 if ( FT_STREAM_SEEK( blend
->glyphoffsets
[glyph_index
] ) ||
1351 FT_FRAME_ENTER( blend
->glyphoffsets
[glyph_index
+ 1] -
1352 blend
->glyphoffsets
[glyph_index
] ) )
1355 glyph_start
= FT_Stream_FTell( stream
);
1357 /* each set of glyph variation data is formatted similarly to `cvar' */
1358 /* (except we get shared points and global tuples) */
1360 if ( FT_NEW_ARRAY( tuple_coords
, blend
->num_axis
) ||
1361 FT_NEW_ARRAY( im_start_coords
, blend
->num_axis
) ||
1362 FT_NEW_ARRAY( im_end_coords
, blend
->num_axis
) )
1365 tupleCount
= FT_GET_USHORT();
1366 offsetToData
= glyph_start
+ FT_GET_USHORT();
1368 if ( tupleCount
& GX_TC_TUPLES_SHARE_POINT_NUMBERS
)
1370 here
= FT_Stream_FTell( stream
);
1372 FT_Stream_SeekSet( stream
, offsetToData
);
1374 sharedpoints
= ft_var_readpackedpoints( stream
, &spoint_count
);
1375 offsetToData
= FT_Stream_FTell( stream
);
1377 FT_Stream_SeekSet( stream
, here
);
1380 for ( i
= 0; i
< ( tupleCount
& GX_TC_TUPLE_COUNT_MASK
); ++i
)
1382 FT_UInt tupleDataSize
;
1387 tupleDataSize
= FT_GET_USHORT();
1388 tupleIndex
= FT_GET_USHORT();
1390 if ( tupleIndex
& GX_TI_EMBEDDED_TUPLE_COORD
)
1392 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1393 tuple_coords
[j
] = FT_GET_SHORT() << 2; /* convert from */
1394 /* short frac to fixed */
1396 else if ( ( tupleIndex
& GX_TI_TUPLE_INDEX_MASK
) >= blend
->tuplecount
)
1398 error
= TT_Err_Invalid_Table
;
1405 &blend
->tuplecoords
[(tupleIndex
& 0xFFF) * blend
->num_axis
],
1406 blend
->num_axis
* sizeof ( FT_Fixed
) );
1409 if ( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
)
1411 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1412 im_start_coords
[j
] = FT_GET_SHORT() << 2;
1413 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1414 im_end_coords
[j
] = FT_GET_SHORT() << 2;
1417 apply
= ft_var_apply_tuple( blend
,
1418 (FT_UShort
)tupleIndex
,
1423 if ( apply
== 0 ) /* tuple isn't active for our blend */
1425 offsetToData
+= tupleDataSize
;
1429 here
= FT_Stream_FTell( stream
);
1431 if ( tupleIndex
& GX_TI_PRIVATE_POINT_NUMBERS
)
1433 FT_Stream_SeekSet( stream
, offsetToData
);
1435 localpoints
= ft_var_readpackedpoints( stream
, &point_count
);
1436 points
= localpoints
;
1440 points
= sharedpoints
;
1441 point_count
= spoint_count
;
1444 deltas_x
= ft_var_readpackeddeltas( stream
,
1445 point_count
== 0 ? n_points
1447 deltas_y
= ft_var_readpackeddeltas( stream
,
1448 point_count
== 0 ? n_points
1451 if ( points
== NULL
|| deltas_y
== NULL
|| deltas_x
== NULL
)
1452 ; /* failure, ignore it */
1454 else if ( points
== ALL_POINTS
)
1456 /* this means that there are deltas for every point in the glyph */
1457 for ( j
= 0; j
< n_points
; ++j
)
1459 delta_xy
[j
].x
+= FT_MulFix( deltas_x
[j
], apply
);
1460 delta_xy
[j
].y
+= FT_MulFix( deltas_y
[j
], apply
);
1466 for ( j
= 0; j
< point_count
; ++j
)
1468 delta_xy
[localpoints
[j
]].x
+= FT_MulFix( deltas_x
[j
], apply
);
1469 delta_xy
[localpoints
[j
]].y
+= FT_MulFix( deltas_y
[j
], apply
);
1473 if ( localpoints
!= ALL_POINTS
)
1474 FT_FREE( localpoints
);
1475 FT_FREE( deltas_x
);
1476 FT_FREE( deltas_y
);
1478 offsetToData
+= tupleDataSize
;
1480 FT_Stream_SeekSet( stream
, here
);
1484 FT_FREE( tuple_coords
);
1485 FT_FREE( im_start_coords
);
1486 FT_FREE( im_end_coords
);
1494 FT_FREE( delta_xy
);
1503 /*************************************************************************/
1509 /* Frees the blend internal data structure. */
1511 FT_LOCAL_DEF( void )
1512 tt_done_blend( FT_Memory memory
,
1515 if ( blend
!= NULL
)
1520 FT_FREE( blend
->normalizedcoords
);
1521 FT_FREE( blend
->mmvar
);
1523 if ( blend
->avar_segment
!= NULL
)
1525 for ( i
= 0; i
< blend
->num_axis
; ++i
)
1526 FT_FREE( blend
->avar_segment
[i
].correspondence
);
1527 FT_FREE( blend
->avar_segment
);
1530 FT_FREE( blend
->tuplecoords
);
1531 FT_FREE( blend
->glyphoffsets
);
1536 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */