1 /***************************************************************************/
5 /* TrueType GX Font Variation loader */
7 /* Copyright 2004, 2005, 2006, 2007 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_
{
343 FT_UShort globalCoordCount
;
344 FT_ULong offsetToCoord
;
345 FT_UShort glyphCount
;
347 FT_ULong offsetToData
;
352 /*************************************************************************/
355 /* ft_var_load_gvar */
358 /* Parses the `gvar' table if present. If `fvar' is there, `gvar' */
359 /* had better be there too. */
362 /* face :: The font face. */
365 /* FreeType error code. 0 means success. */
368 ft_var_load_gvar( TT_Face face
)
370 FT_Stream stream
= FT_FACE_STREAM(face
);
371 FT_Memory memory
= stream
->memory
;
372 GX_Blend blend
= face
->blend
;
377 FT_ULong offsetToData
;
378 GX_GVar_Head gvar_head
;
380 static const FT_Frame_Field gvar_fields
[] =
384 #define FT_STRUCTURE GX_GVar_Head
386 FT_FRAME_START( 20 ),
387 FT_FRAME_LONG ( version
),
388 FT_FRAME_USHORT( axisCount
),
389 FT_FRAME_USHORT( globalCoordCount
),
390 FT_FRAME_ULONG ( offsetToCoord
),
391 FT_FRAME_USHORT( glyphCount
),
392 FT_FRAME_USHORT( flags
),
393 FT_FRAME_ULONG ( offsetToData
),
397 if ( (error
= face
->goto_table( face
, TTAG_gvar
, stream
, &table_len
)) != 0 )
400 gvar_start
= FT_STREAM_POS( );
401 if ( FT_STREAM_READ_FIELDS( gvar_fields
, &gvar_head
) )
404 blend
->tuplecount
= gvar_head
.globalCoordCount
;
405 blend
->gv_glyphcnt
= gvar_head
.glyphCount
;
406 offsetToData
= gvar_start
+ gvar_head
.offsetToData
;
408 if ( gvar_head
.version
!= (FT_Long
)0x00010000L
||
409 gvar_head
.axisCount
!= (FT_UShort
)blend
->mmvar
->num_axis
)
411 error
= TT_Err_Invalid_Table
;
415 if ( FT_NEW_ARRAY( blend
->glyphoffsets
, blend
->gv_glyphcnt
+ 1 ) )
418 if ( gvar_head
.flags
& 1 )
420 /* long offsets (one more offset than glyphs, to mark size of last) */
421 if ( FT_FRAME_ENTER( ( blend
->gv_glyphcnt
+ 1 ) * 4L ) )
424 for ( i
= 0; i
<= blend
->gv_glyphcnt
; ++i
)
425 blend
->glyphoffsets
[i
] = offsetToData
+ FT_GET_LONG();
431 /* short offsets (one more offset than glyphs, to mark size of last) */
432 if ( FT_FRAME_ENTER( ( blend
->gv_glyphcnt
+ 1 ) * 2L ) )
435 for ( i
= 0; i
<= blend
->gv_glyphcnt
; ++i
)
436 blend
->glyphoffsets
[i
] = offsetToData
+ FT_GET_USHORT() * 2;
437 /* XXX: Undocumented: `*2'! */
442 if ( blend
->tuplecount
!= 0 )
444 if ( FT_NEW_ARRAY( blend
->tuplecoords
,
445 gvar_head
.axisCount
* blend
->tuplecount
) )
448 if ( FT_STREAM_SEEK( gvar_start
+ gvar_head
.offsetToCoord
) ||
449 FT_FRAME_ENTER( blend
->tuplecount
* gvar_head
.axisCount
* 2L ) )
452 for ( i
= 0; i
< blend
->tuplecount
; ++i
)
453 for ( j
= 0 ; j
< (FT_UInt
)gvar_head
.axisCount
; ++j
)
454 blend
->tuplecoords
[i
* gvar_head
.axisCount
+ j
] =
455 FT_GET_SHORT() << 2; /* convert to FT_Fixed */
465 /*************************************************************************/
468 /* ft_var_apply_tuple */
471 /* Figure out whether a given tuple (design) applies to the current */
472 /* blend, and if so, what is the scaling factor. */
475 /* blend :: The current blend of the font. */
477 /* tupleIndex :: A flag saying whether this is an intermediate */
480 /* tuple_coords :: The coordinates of the tuple in normalized axis */
483 /* im_start_coords :: The initial coordinates where this tuple starts */
484 /* to apply (for intermediate coordinates). */
486 /* im_end_coords :: The final coordinates after which this tuple no */
487 /* longer applies (for intermediate coordinates). */
490 /* An FT_Fixed value containing the scaling factor. */
493 ft_var_apply_tuple( GX_Blend blend
,
494 FT_UShort tupleIndex
,
495 FT_Fixed
* tuple_coords
,
496 FT_Fixed
* im_start_coords
,
497 FT_Fixed
* im_end_coords
)
505 for ( i
= 0; i
< blend
->num_axis
; ++i
)
507 if ( tuple_coords
[i
] == 0 )
508 /* It's not clear why (for intermediate tuples) we don't need */
509 /* to check against start/end -- the documentation says we don't. */
510 /* Similarly, it's unclear why we don't need to scale along the */
514 else if ( blend
->normalizedcoords
[i
] == 0 ||
515 ( blend
->normalizedcoords
[i
] < 0 && tuple_coords
[i
] > 0 ) ||
516 ( blend
->normalizedcoords
[i
] > 0 && tuple_coords
[i
] < 0 ) )
522 else if ( !( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
) )
523 /* not an intermediate tuple */
524 apply
= FT_MulDiv( apply
,
525 blend
->normalizedcoords
[i
] > 0
526 ? blend
->normalizedcoords
[i
]
527 : -blend
->normalizedcoords
[i
],
530 else if ( blend
->normalizedcoords
[i
] <= im_start_coords
[i
] ||
531 blend
->normalizedcoords
[i
] >= im_end_coords
[i
] )
537 else if ( blend
->normalizedcoords
[i
] < tuple_coords
[i
] )
539 temp
= FT_MulDiv( blend
->normalizedcoords
[i
] - im_start_coords
[i
],
541 tuple_coords
[i
] - im_start_coords
[i
]);
542 apply
= FT_MulDiv( apply
, temp
, 0x10000L
);
547 temp
= FT_MulDiv( im_end_coords
[i
] - blend
->normalizedcoords
[i
],
549 im_end_coords
[i
] - tuple_coords
[i
] );
550 apply
= FT_MulDiv( apply
, temp
, 0x10000L
);
558 /*************************************************************************/
559 /*************************************************************************/
561 /***** MULTIPLE MASTERS SERVICE FUNCTIONS *****/
563 /*************************************************************************/
564 /*************************************************************************/
567 typedef struct GX_FVar_Head_
{
569 FT_UShort offsetToData
;
570 FT_UShort countSizePairs
;
573 FT_UShort instanceCount
;
574 FT_UShort instanceSize
;
579 typedef struct fvar_axis
{
582 FT_ULong defaultValue
;
590 /*************************************************************************/
596 /* Check that the font's `fvar' table is valid, parse it, and return */
600 /* face :: The font face. */
601 /* TT_Get_MM_Var initializes the blend structure. */
604 /* master :: The `fvar' data (must be freed by caller). */
607 /* FreeType error code. 0 means success. */
609 FT_LOCAL_DEF( FT_Error
)
610 TT_Get_MM_Var( TT_Face face
,
613 FT_Stream stream
= face
->root
.stream
;
614 FT_Memory memory
= face
->root
.memory
;
616 FT_Error error
= TT_Err_Ok
;
620 FT_Fixed
* next_coords
;
621 FT_String
* next_name
;
623 FT_Var_Named_Style
* ns
;
624 GX_FVar_Head fvar_head
;
626 static const FT_Frame_Field fvar_fields
[] =
630 #define FT_STRUCTURE GX_FVar_Head
632 FT_FRAME_START( 16 ),
633 FT_FRAME_LONG ( version
),
634 FT_FRAME_USHORT( offsetToData
),
635 FT_FRAME_USHORT( countSizePairs
),
636 FT_FRAME_USHORT( axisCount
),
637 FT_FRAME_USHORT( axisSize
),
638 FT_FRAME_USHORT( instanceCount
),
639 FT_FRAME_USHORT( instanceSize
),
643 static const FT_Frame_Field fvaraxis_fields
[] =
647 #define FT_STRUCTURE GX_FVar_Axis
649 FT_FRAME_START( 20 ),
650 FT_FRAME_ULONG ( axisTag
),
651 FT_FRAME_ULONG ( minValue
),
652 FT_FRAME_ULONG ( defaultValue
),
653 FT_FRAME_ULONG ( maxValue
),
654 FT_FRAME_USHORT( flags
),
655 FT_FRAME_USHORT( nameID
),
660 if ( face
->blend
== NULL
)
662 /* both `fvar' and `gvar' must be present */
663 if ( (error
= face
->goto_table( face
, TTAG_gvar
,
664 stream
, &table_len
)) != 0 )
667 if ( (error
= face
->goto_table( face
, TTAG_fvar
,
668 stream
, &table_len
)) != 0 )
671 fvar_start
= FT_STREAM_POS( );
673 if ( FT_STREAM_READ_FIELDS( fvar_fields
, &fvar_head
) )
676 if ( fvar_head
.version
!= (FT_Long
)0x00010000L
||
677 fvar_head
.countSizePairs
!= 2 ||
678 fvar_head
.axisSize
!= 20 ||
679 fvar_head
.instanceSize
!= 4 + 4 * fvar_head
.axisCount
||
680 fvar_head
.offsetToData
+ fvar_head
.axisCount
* 20U +
681 fvar_head
.instanceCount
* fvar_head
.instanceSize
> table_len
)
683 error
= TT_Err_Invalid_Table
;
687 if ( FT_NEW( face
->blend
) )
690 /* XXX: TODO - check for overflows */
691 face
->blend
->mmvar_len
=
692 sizeof ( FT_MM_Var
) +
693 fvar_head
.axisCount
* sizeof ( FT_Var_Axis
) +
694 fvar_head
.instanceCount
* sizeof ( FT_Var_Named_Style
) +
695 fvar_head
.instanceCount
* fvar_head
.axisCount
* sizeof ( FT_Fixed
) +
696 5 * fvar_head
.axisCount
;
698 if ( FT_ALLOC( mmvar
, face
->blend
->mmvar_len
) )
700 face
->blend
->mmvar
= mmvar
;
705 (FT_UInt
)-1; /* meaningless in this context; each glyph */
706 /* may have a different number of designs */
707 /* (or tuples, as called by Apple) */
708 mmvar
->num_namedstyles
=
709 fvar_head
.instanceCount
;
711 (FT_Var_Axis
*)&(mmvar
[1]);
713 (FT_Var_Named_Style
*)&(mmvar
->axis
[fvar_head
.axisCount
]);
716 (FT_Fixed
*)&(mmvar
->namedstyle
[fvar_head
.instanceCount
]);
717 for ( i
= 0; i
< fvar_head
.instanceCount
; ++i
)
719 mmvar
->namedstyle
[i
].coords
= next_coords
;
720 next_coords
+= fvar_head
.axisCount
;
723 next_name
= (FT_String
*)next_coords
;
724 for ( i
= 0; i
< fvar_head
.axisCount
; ++i
)
726 mmvar
->axis
[i
].name
= next_name
;
730 if ( FT_STREAM_SEEK( fvar_start
+ fvar_head
.offsetToData
) )
734 for ( i
= 0; i
< fvar_head
.axisCount
; ++i
)
736 GX_FVar_Axis axis_rec
;
739 if ( FT_STREAM_READ_FIELDS( fvaraxis_fields
, &axis_rec
) )
741 a
->tag
= axis_rec
.axisTag
;
742 a
->minimum
= axis_rec
.minValue
; /* A Fixed */
743 a
->def
= axis_rec
.defaultValue
; /* A Fixed */
744 a
->maximum
= axis_rec
.maxValue
; /* A Fixed */
745 a
->strid
= axis_rec
.nameID
;
747 a
->name
[0] = (FT_String
)( a
->tag
>> 24 );
748 a
->name
[1] = (FT_String
)( ( a
->tag
>> 16 ) & 0xFF );
749 a
->name
[2] = (FT_String
)( ( a
->tag
>> 8 ) & 0xFF );
750 a
->name
[3] = (FT_String
)( ( a
->tag
) & 0xFF );
756 ns
= mmvar
->namedstyle
;
757 for ( i
= 0; i
< fvar_head
.instanceCount
; ++i
)
759 if ( FT_FRAME_ENTER( 4L + 4L * fvar_head
.axisCount
) )
762 ns
->strid
= FT_GET_USHORT();
763 (void) /* flags = */ FT_GET_USHORT();
765 for ( j
= 0; j
< fvar_head
.axisCount
; ++j
)
766 ns
->coords
[j
] = FT_GET_ULONG(); /* A Fixed */
772 if ( master
!= NULL
)
777 if ( FT_ALLOC( mmvar
, face
->blend
->mmvar_len
) )
779 FT_MEM_COPY( mmvar
, face
->blend
->mmvar
, face
->blend
->mmvar_len
);
782 (FT_Var_Axis
*)&(mmvar
[1]);
784 (FT_Var_Named_Style
*)&(mmvar
->axis
[mmvar
->num_axis
]);
786 (FT_Fixed
*)&(mmvar
->namedstyle
[mmvar
->num_namedstyles
]);
788 for ( n
= 0; n
< mmvar
->num_namedstyles
; ++n
)
790 mmvar
->namedstyle
[n
].coords
= next_coords
;
791 next_coords
+= mmvar
->num_axis
;
795 next_name
= (FT_String
*)next_coords
;
796 for ( n
= 0; n
< mmvar
->num_axis
; ++n
)
800 /* standard PostScript names for some standard apple tags */
801 if ( a
->tag
== TTAG_wght
)
802 a
->name
= (char *)"Weight";
803 else if ( a
->tag
== TTAG_wdth
)
804 a
->name
= (char *)"Width";
805 else if ( a
->tag
== TTAG_opsz
)
806 a
->name
= (char *)"OpticalSize";
807 else if ( a
->tag
== TTAG_slnt
)
808 a
->name
= (char *)"Slant";
822 /*************************************************************************/
825 /* TT_Set_MM_Blend */
828 /* Set the blend (normalized) coordinates for this instance of the */
829 /* font. Check that the `gvar' table is reasonable and does some */
830 /* initial preparation. */
833 /* face :: The font. */
834 /* Initialize the blend structure with `gvar' data. */
837 /* num_coords :: Must be the axis count of the font. */
839 /* coords :: An array of num_coords, each between [-1,1]. */
842 /* FreeType error code. 0 means success. */
844 FT_LOCAL_DEF( FT_Error
)
845 TT_Set_MM_Blend( TT_Face face
,
849 FT_Error error
= TT_Err_Ok
;
853 FT_Memory memory
= face
->root
.memory
;
864 face
->doblend
= FALSE
;
866 if ( face
->blend
== NULL
)
868 if ( (error
= TT_Get_MM_Var( face
, NULL
)) != 0 )
873 mmvar
= blend
->mmvar
;
875 if ( num_coords
!= mmvar
->num_axis
)
877 error
= TT_Err_Invalid_Argument
;
881 for ( i
= 0; i
< num_coords
; ++i
)
882 if ( coords
[i
] < -0x00010000L
|| coords
[i
] > 0x00010000L
)
884 error
= TT_Err_Invalid_Argument
;
888 if ( blend
->glyphoffsets
== NULL
)
889 if ( (error
= ft_var_load_gvar( face
)) != 0 )
892 if ( blend
->normalizedcoords
== NULL
)
894 if ( FT_NEW_ARRAY( blend
->normalizedcoords
, num_coords
) )
897 manageCvt
= mcvt_modify
;
899 /* If we have not set the blend coordinates before this, then the */
900 /* cvt table will still be what we read from the `cvt ' table and */
901 /* we don't need to reload it. We may need to change it though... */
906 i
< num_coords
&& blend
->normalizedcoords
[i
] == coords
[i
];
908 if ( i
== num_coords
)
909 manageCvt
= mcvt_retain
;
911 manageCvt
= mcvt_load
;
913 /* If we don't change the blend coords then we don't need to do */
914 /* anything to the cvt table. It will be correct. Otherwise we */
915 /* no longer have the original cvt (it was modified when we set */
916 /* the blend last time), so we must reload and then modify it. */
919 blend
->num_axis
= num_coords
;
920 FT_MEM_COPY( blend
->normalizedcoords
,
922 num_coords
* sizeof ( FT_Fixed
) );
924 face
->doblend
= TRUE
;
926 if ( face
->cvt
!= NULL
)
931 /* The cvt table has been loaded already; every time we change the */
932 /* blend we may need to reload and remodify the cvt table. */
933 FT_FREE( face
->cvt
);
936 tt_face_load_cvt( face
, face
->root
.stream
);
940 /* The original cvt table is in memory. All we need to do is */
941 /* apply the `cvar' table (if any). */
942 tt_face_vary_cvt( face
, face
->root
.stream
);
946 /* The cvt table is correct for this set of coordinates. */
956 /*************************************************************************/
959 /* TT_Set_Var_Design */
962 /* Set the coordinates for the instance, measured in the user */
963 /* coordinate system. Parse the `avar' table (if present) to convert */
964 /* from user to normalized coordinates. */
967 /* face :: The font face. */
968 /* Initialize the blend struct with `gvar' data. */
971 /* num_coords :: This must be the axis count of the font. */
973 /* coords :: A coordinate array with `num_coords' elements. */
976 /* FreeType error code. 0 means success. */
978 FT_LOCAL_DEF( FT_Error
)
979 TT_Set_Var_Design( TT_Face face
,
983 FT_Error error
= TT_Err_Ok
;
984 FT_Fixed
* normalized
= NULL
;
990 FT_Memory memory
= face
->root
.memory
;
993 if ( face
->blend
== NULL
)
995 if ( (error
= TT_Get_MM_Var( face
, NULL
)) != 0 )
1000 mmvar
= blend
->mmvar
;
1002 if ( num_coords
!= mmvar
->num_axis
)
1004 error
= TT_Err_Invalid_Argument
;
1008 /* Axis normalization is a two stage process. First we normalize */
1009 /* based on the [min,def,max] values for the axis to be [-1,0,1]. */
1010 /* Then, if there's an `avar' table, we renormalize this range. */
1012 if ( FT_NEW_ARRAY( normalized
, mmvar
->num_axis
) )
1016 for ( i
= 0; i
< mmvar
->num_axis
; ++i
, ++a
)
1018 if ( coords
[i
] > a
->maximum
|| coords
[i
] < a
->minimum
)
1020 error
= TT_Err_Invalid_Argument
;
1024 if ( coords
[i
] < a
->def
)
1026 normalized
[i
] = -FT_MulDiv( coords
[i
] - a
->def
,
1028 a
->minimum
- a
->def
);
1030 else if ( a
->maximum
== a
->def
)
1034 normalized
[i
] = FT_MulDiv( coords
[i
] - a
->def
,
1036 a
->maximum
- a
->def
);
1040 if ( !blend
->avar_checked
)
1041 ft_var_load_avar( face
);
1043 if ( blend
->avar_segment
!= NULL
)
1045 av
= blend
->avar_segment
;
1046 for ( i
= 0; i
< mmvar
->num_axis
; ++i
, ++av
)
1048 for ( j
= 1; j
< (FT_UInt
)av
->pairCount
; ++j
)
1049 if ( normalized
[i
] < av
->correspondence
[j
].fromCoord
)
1054 normalized
[i
] - av
->correspondence
[j
- 1].fromCoord
,
1056 av
->correspondence
[j
].fromCoord
-
1057 av
->correspondence
[j
- 1].fromCoord
),
1058 av
->correspondence
[j
].toCoord
-
1059 av
->correspondence
[j
- 1].toCoord
,
1061 av
->correspondence
[j
- 1].toCoord
;
1067 error
= TT_Set_MM_Blend( face
, num_coords
, normalized
);
1070 FT_FREE( normalized
);
1075 /*************************************************************************/
1076 /*************************************************************************/
1078 /***** GX VAR PARSING ROUTINES *****/
1080 /*************************************************************************/
1081 /*************************************************************************/
1084 /*************************************************************************/
1087 /* tt_face_vary_cvt */
1090 /* Modify the loaded cvt table according to the `cvar' table and the */
1094 /* face :: A handle to the target face object. */
1097 /* stream :: A handle to the input stream. */
1100 /* FreeType error code. 0 means success. */
1102 /* Most errors are ignored. It is perfectly valid not to have a */
1103 /* `cvar' table even if there is a `gvar' and `fvar' table. */
1105 FT_LOCAL_DEF( FT_Error
)
1106 tt_face_vary_cvt( TT_Face face
,
1110 FT_Memory memory
= stream
->memory
;
1111 FT_ULong table_start
;
1114 FT_ULong offsetToData
;
1117 FT_Fixed
* tuple_coords
= NULL
;
1118 FT_Fixed
* im_start_coords
= NULL
;
1119 FT_Fixed
* im_end_coords
= NULL
;
1120 GX_Blend blend
= face
->blend
;
1121 FT_UInt point_count
;
1122 FT_UShort
* localpoints
;
1126 FT_TRACE2(( "CVAR " ));
1128 if ( blend
== NULL
)
1130 FT_TRACE2(( "no blend specified!\n" ));
1136 if ( face
->cvt
== NULL
)
1138 FT_TRACE2(( "no `cvt ' table!\n" ));
1144 error
= face
->goto_table( face
, TTAG_cvar
, stream
, &table_len
);
1147 FT_TRACE2(( "is missing!\n" ));
1153 if ( FT_FRAME_ENTER( table_len
) )
1159 table_start
= FT_Stream_FTell( stream
);
1160 if ( FT_GET_LONG() != 0x00010000L
)
1162 FT_TRACE2(( "bad table version!\n" ));
1168 if ( FT_NEW_ARRAY( tuple_coords
, blend
->num_axis
) ||
1169 FT_NEW_ARRAY( im_start_coords
, blend
->num_axis
) ||
1170 FT_NEW_ARRAY( im_end_coords
, blend
->num_axis
) )
1173 tupleCount
= FT_GET_USHORT();
1174 offsetToData
= table_start
+ FT_GET_USHORT();
1176 /* The documentation implies there are flags packed into the */
1177 /* tuplecount, but John Jenkins says that shared points don't apply */
1178 /* to `cvar', and no other flags are defined. */
1180 for ( i
= 0; i
< ( tupleCount
& 0xFFF ); ++i
)
1182 FT_UInt tupleDataSize
;
1187 tupleDataSize
= FT_GET_USHORT();
1188 tupleIndex
= FT_GET_USHORT();
1190 /* There is no provision here for a global tuple coordinate section, */
1191 /* so John says. There are no tuple indices, just embedded tuples. */
1193 if ( tupleIndex
& GX_TI_EMBEDDED_TUPLE_COORD
)
1195 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1196 tuple_coords
[j
] = FT_GET_SHORT() << 2; /* convert from */
1197 /* short frac to fixed */
1201 /* skip this tuple; it makes no sense */
1203 if ( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
)
1204 for ( j
= 0; j
< 2 * blend
->num_axis
; ++j
)
1205 (void)FT_GET_SHORT();
1207 offsetToData
+= tupleDataSize
;
1211 if ( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
)
1213 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1214 im_start_coords
[j
] = FT_GET_SHORT() << 2;
1215 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1216 im_end_coords
[j
] = FT_GET_SHORT() << 2;
1219 apply
= ft_var_apply_tuple( blend
,
1220 (FT_UShort
)tupleIndex
,
1224 if ( /* tuple isn't active for our blend */
1226 /* global points not allowed, */
1227 /* if they aren't local, makes no sense */
1228 !( tupleIndex
& GX_TI_PRIVATE_POINT_NUMBERS
) )
1230 offsetToData
+= tupleDataSize
;
1234 here
= FT_Stream_FTell( stream
);
1236 FT_Stream_SeekSet( stream
, offsetToData
);
1238 localpoints
= ft_var_readpackedpoints( stream
, &point_count
);
1239 deltas
= ft_var_readpackeddeltas( stream
,
1240 point_count
== 0 ? face
->cvt_size
1242 if ( localpoints
== NULL
|| deltas
== NULL
)
1243 /* failure, ignore it */;
1245 else if ( localpoints
== ALL_POINTS
)
1247 /* this means that there are deltas for every entry in cvt */
1248 for ( j
= 0; j
< face
->cvt_size
; ++j
)
1249 face
->cvt
[j
] = (FT_Short
)( face
->cvt
[j
] +
1250 FT_MulFix( deltas
[j
], apply
) );
1255 for ( j
= 0; j
< point_count
; ++j
)
1257 int pindex
= localpoints
[j
];
1259 face
->cvt
[pindex
] = (FT_Short
)( face
->cvt
[pindex
] +
1260 FT_MulFix( deltas
[j
], apply
) );
1264 if ( localpoints
!= ALL_POINTS
)
1265 FT_FREE( localpoints
);
1268 offsetToData
+= tupleDataSize
;
1270 FT_Stream_SeekSet( stream
, here
);
1277 FT_FREE( tuple_coords
);
1278 FT_FREE( im_start_coords
);
1279 FT_FREE( im_end_coords
);
1285 /*************************************************************************/
1288 /* TT_Vary_Get_Glyph_Deltas */
1291 /* Load the appropriate deltas for the current glyph. */
1294 /* face :: A handle to the target face object. */
1296 /* glyph_index :: The index of the glyph being modified. */
1298 /* n_points :: The number of the points in the glyph, including */
1299 /* phantom points. */
1302 /* deltas :: The array of points to change. */
1305 /* FreeType error code. 0 means success. */
1307 FT_LOCAL_DEF( FT_Error
)
1308 TT_Vary_Get_Glyph_Deltas( TT_Face face
,
1309 FT_UInt glyph_index
,
1313 FT_Stream stream
= face
->root
.stream
;
1314 FT_Memory memory
= stream
->memory
;
1315 GX_Blend blend
= face
->blend
;
1316 FT_Vector
* delta_xy
;
1319 FT_ULong glyph_start
;
1321 FT_ULong offsetToData
;
1324 FT_Fixed
* tuple_coords
= NULL
;
1325 FT_Fixed
* im_start_coords
= NULL
;
1326 FT_Fixed
* im_end_coords
= NULL
;
1327 FT_UInt point_count
, spoint_count
= 0;
1328 FT_UShort
* sharedpoints
= NULL
;
1329 FT_UShort
* localpoints
= NULL
;
1331 FT_Short
*deltas_x
, *deltas_y
;
1334 if ( !face
->doblend
|| blend
== NULL
)
1335 return TT_Err_Invalid_Argument
;
1337 /* to be freed by the caller */
1338 if ( FT_NEW_ARRAY( delta_xy
, n_points
) )
1342 if ( glyph_index
>= blend
->gv_glyphcnt
||
1343 blend
->glyphoffsets
[glyph_index
] ==
1344 blend
->glyphoffsets
[glyph_index
+ 1] )
1345 return TT_Err_Ok
; /* no variation data for this glyph */
1347 if ( FT_STREAM_SEEK( blend
->glyphoffsets
[glyph_index
] ) ||
1348 FT_FRAME_ENTER( blend
->glyphoffsets
[glyph_index
+ 1] -
1349 blend
->glyphoffsets
[glyph_index
] ) )
1352 glyph_start
= FT_Stream_FTell( stream
);
1354 /* each set of glyph variation data is formatted similarly to `cvar' */
1355 /* (except we get shared points and global tuples) */
1357 if ( FT_NEW_ARRAY( tuple_coords
, blend
->num_axis
) ||
1358 FT_NEW_ARRAY( im_start_coords
, blend
->num_axis
) ||
1359 FT_NEW_ARRAY( im_end_coords
, blend
->num_axis
) )
1362 tupleCount
= FT_GET_USHORT();
1363 offsetToData
= glyph_start
+ FT_GET_USHORT();
1365 if ( tupleCount
& GX_TC_TUPLES_SHARE_POINT_NUMBERS
)
1367 here
= FT_Stream_FTell( stream
);
1369 FT_Stream_SeekSet( stream
, offsetToData
);
1371 sharedpoints
= ft_var_readpackedpoints( stream
, &spoint_count
);
1372 offsetToData
= FT_Stream_FTell( stream
);
1374 FT_Stream_SeekSet( stream
, here
);
1377 for ( i
= 0; i
< ( tupleCount
& GX_TC_TUPLE_COUNT_MASK
); ++i
)
1379 FT_UInt tupleDataSize
;
1384 tupleDataSize
= FT_GET_USHORT();
1385 tupleIndex
= FT_GET_USHORT();
1387 if ( tupleIndex
& GX_TI_EMBEDDED_TUPLE_COORD
)
1389 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1390 tuple_coords
[j
] = FT_GET_SHORT() << 2; /* convert from */
1391 /* short frac to fixed */
1393 else if ( ( tupleIndex
& GX_TI_TUPLE_INDEX_MASK
) >= blend
->tuplecount
)
1395 error
= TT_Err_Invalid_Table
;
1402 &blend
->tuplecoords
[(tupleIndex
& 0xFFF) * blend
->num_axis
],
1403 blend
->num_axis
* sizeof ( FT_Fixed
) );
1406 if ( tupleIndex
& GX_TI_INTERMEDIATE_TUPLE
)
1408 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1409 im_start_coords
[j
] = FT_GET_SHORT() << 2;
1410 for ( j
= 0; j
< blend
->num_axis
; ++j
)
1411 im_end_coords
[j
] = FT_GET_SHORT() << 2;
1414 apply
= ft_var_apply_tuple( blend
,
1415 (FT_UShort
)tupleIndex
,
1420 if ( apply
== 0 ) /* tuple isn't active for our blend */
1422 offsetToData
+= tupleDataSize
;
1426 here
= FT_Stream_FTell( stream
);
1428 if ( tupleIndex
& GX_TI_PRIVATE_POINT_NUMBERS
)
1430 FT_Stream_SeekSet( stream
, offsetToData
);
1432 localpoints
= ft_var_readpackedpoints( stream
, &point_count
);
1433 points
= localpoints
;
1437 points
= sharedpoints
;
1438 point_count
= spoint_count
;
1441 deltas_x
= ft_var_readpackeddeltas( stream
,
1442 point_count
== 0 ? n_points
1444 deltas_y
= ft_var_readpackeddeltas( stream
,
1445 point_count
== 0 ? n_points
1448 if ( points
== NULL
|| deltas_y
== NULL
|| deltas_x
== NULL
)
1449 ; /* failure, ignore it */
1451 else if ( points
== ALL_POINTS
)
1453 /* this means that there are deltas for every point in the glyph */
1454 for ( j
= 0; j
< n_points
; ++j
)
1456 delta_xy
[j
].x
+= FT_MulFix( deltas_x
[j
], apply
);
1457 delta_xy
[j
].y
+= FT_MulFix( deltas_y
[j
], apply
);
1463 for ( j
= 0; j
< point_count
; ++j
)
1465 delta_xy
[localpoints
[j
]].x
+= FT_MulFix( deltas_x
[j
], apply
);
1466 delta_xy
[localpoints
[j
]].y
+= FT_MulFix( deltas_y
[j
], apply
);
1470 if ( localpoints
!= ALL_POINTS
)
1471 FT_FREE( localpoints
);
1472 FT_FREE( deltas_x
);
1473 FT_FREE( deltas_y
);
1475 offsetToData
+= tupleDataSize
;
1477 FT_Stream_SeekSet( stream
, here
);
1481 FT_FREE( tuple_coords
);
1482 FT_FREE( im_start_coords
);
1483 FT_FREE( im_end_coords
);
1491 FT_FREE( delta_xy
);
1500 /*************************************************************************/
1506 /* Frees the blend internal data structure. */
1508 FT_LOCAL_DEF( void )
1509 tt_done_blend( FT_Memory memory
,
1512 if ( blend
!= NULL
)
1517 FT_FREE( blend
->normalizedcoords
);
1518 FT_FREE( blend
->mmvar
);
1520 if ( blend
->avar_segment
!= NULL
)
1522 for ( i
= 0; i
< blend
->num_axis
; ++i
)
1523 FT_FREE( blend
->avar_segment
[i
].correspondence
);
1524 FT_FREE( blend
->avar_segment
);
1527 FT_FREE( blend
->tuplecoords
);
1528 FT_FREE( blend
->glyphoffsets
);
1533 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */