1 /****************************************************************************
5 * TrueType GX Font Variation loader
7 * Copyright (C) 2004-2023 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.
19 /**************************************************************************
21 * Apple documents the `fvar', `gvar', `cvar', and `avar' tables at
23 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6[fgca]var.html
25 * The documentation for `gvar' is not intelligible; `cvar' refers you
26 * to `gvar' and is thus also incomprehensible.
28 * The documentation for `avar' appears correct, but Apple has no fonts
29 * with an `avar' table, so it is hard to test.
31 * Many thanks to John Jenkins (at Apple) in figuring this out.
34 * Apple's `kern' table has some references to tuple indices, but as
35 * there is no indication where these indices are defined, nor how to
36 * interpolate the kerning values (different tuples have different
37 * classes) this issue is ignored.
43 #include <freetype/internal/ftdebug.h>
44 #include FT_CONFIG_CONFIG_H
45 #include <freetype/internal/ftcalc.h>
46 #include <freetype/internal/ftstream.h>
47 #include <freetype/internal/sfnt.h>
48 #include <freetype/internal/services/svmetric.h>
49 #include <freetype/tttags.h>
50 #include <freetype/ttnameid.h>
51 #include <freetype/ftmm.h>
52 #include <freetype/ftlist.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 ) \
67 ( (off) < (FT_ULong)( (stream)->limit - (stream)->base ) ) \
68 ? (stream)->base + (off) \
72 /* some macros we need */
73 #define FT_fdot14ToFixed( x ) \
74 ( (FT_Fixed)( (FT_ULong)(x) << 2 ) )
75 #define FT_intToFixed( i ) \
76 ( (FT_Fixed)( (FT_ULong)(i) << 16 ) )
77 #define FT_fdot6ToFixed( i ) \
78 ( (FT_Fixed)( (FT_ULong)(i) << 10 ) )
79 #define FT_fixedToInt( x ) \
80 ( (FT_Short)( ( (x) + 0x8000U ) >> 16 ) )
81 #define FT_fixedToFdot6( x ) \
82 ( (FT_Pos)( ( (x) + 0x200 ) >> 10 ) )
85 /**************************************************************************
87 * The macro FT_COMPONENT is used in trace mode. It is an implicit
88 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
89 * messages during execution.
92 #define FT_COMPONENT ttgxvar
95 /*************************************************************************/
96 /*************************************************************************/
98 /***** Internal Routines *****/
100 /*************************************************************************/
101 /*************************************************************************/
104 /**************************************************************************
106 * The macro ALL_POINTS is used in `ft_var_readpackedpoints'. It
107 * indicates that there is a delta for every point without needing to
108 * enumerate all of them.
111 /* ensure that value `0' has the same width as a pointer */
112 #define ALL_POINTS (FT_UShort*)~(FT_PtrDist)0
115 #define GX_PT_POINTS_ARE_WORDS 0x80U
116 #define GX_PT_POINT_RUN_COUNT_MASK 0x7FU
119 /**************************************************************************
122 * ft_var_readpackedpoints
125 * Read a set of points to which the following deltas will apply.
126 * Points are packed with a run length encoding.
133 * The size of the table holding the data.
137 * The number of points read. A zero value means that
138 * all points in the glyph will be affected, without
139 * enumerating them individually.
142 * An array of FT_UShort containing the affected points or the
143 * special value ALL_POINTS.
146 ft_var_readpackedpoints( FT_Stream stream,
150 FT_UShort *points = NULL;
155 FT_Memory memory = stream->memory;
165 if ( n & GX_PT_POINTS_ARE_WORDS )
167 n &= GX_PT_POINT_RUN_COUNT_MASK;
174 FT_TRACE1(( "ft_var_readpackedpoints: number of points too large\n" ));
178 /* in the nested loops below we increase `i' twice; */
179 /* it is faster to simply allocate one more slot */
180 /* than to add another test within the loop */
181 if ( FT_QNEW_ARRAY( points, n + 1 ) )
190 runcnt = FT_GET_BYTE();
191 if ( runcnt & GX_PT_POINTS_ARE_WORDS )
193 runcnt &= GX_PT_POINT_RUN_COUNT_MASK;
194 first += FT_GET_USHORT();
197 /* first point not included in run count */
198 for ( j = 0; j < runcnt; j++ )
200 first += FT_GET_USHORT();
208 first += FT_GET_BYTE();
211 for ( j = 0; j < runcnt; j++ )
213 first += FT_GET_BYTE();
225 #define GX_DT_DELTAS_ARE_ZERO 0x80U
226 #define GX_DT_DELTAS_ARE_WORDS 0x40U
227 #define GX_DT_DELTA_RUN_COUNT_MASK 0x3FU
230 /**************************************************************************
233 * ft_var_readpackeddeltas
236 * Read a set of deltas. These are packed slightly differently than
237 * points. In particular there is no overall count.
244 * The size of the table holding the data.
247 * The number of deltas to be read.
250 * An array of FT_Fixed containing the deltas for the affected
251 * points. (This only gets the deltas for one dimension. It will
252 * generally be called twice, once for x, once for y. When used in
253 * cvt table, it will only be called once.)
255 * We use FT_Fixed to avoid accumulation errors while summing up all
256 * deltas (the rounding to integer values happens as the very last
260 ft_var_readpackeddeltas( FT_Stream stream,
264 FT_Fixed *deltas = NULL;
268 FT_Memory memory = stream->memory;
272 if ( FT_QNEW_ARRAY( deltas, delta_cnt ) )
278 while ( i < delta_cnt && bytes_used < size )
280 runcnt = FT_GET_BYTE();
281 cnt = runcnt & GX_DT_DELTA_RUN_COUNT_MASK;
285 if ( runcnt & GX_DT_DELTAS_ARE_ZERO )
287 /* `cnt` + 1 zeroes get added */
288 for ( j = 0; j <= cnt && i < delta_cnt; j++ )
291 else if ( runcnt & GX_DT_DELTAS_ARE_WORDS )
293 /* `cnt` + 1 shorts from the stack */
294 bytes_used += 2 * ( cnt + 1 );
295 if ( bytes_used > size )
297 FT_TRACE1(( "ft_var_readpackeddeltas:"
298 " number of short deltas too large\n" ));
302 for ( j = 0; j <= cnt && i < delta_cnt; j++ )
303 deltas[i++] = FT_intToFixed( FT_GET_SHORT() );
307 /* `cnt` + 1 signed bytes from the stack */
308 bytes_used += cnt + 1;
309 if ( bytes_used > size )
311 FT_TRACE1(( "ft_var_readpackeddeltas:"
312 " number of byte deltas too large\n" ));
316 for ( j = 0; j <= cnt && i < delta_cnt; j++ )
317 deltas[i++] = FT_intToFixed( FT_GET_CHAR() );
322 FT_TRACE1(( "ft_var_readpackeddeltas:"
323 " number of deltas too large\n" ));
330 FT_TRACE1(( "ft_var_readpackeddeltas: not enough deltas\n" ));
342 /**************************************************************************
348 * Parse the `avar' table if present. It need not be, so we return
356 ft_var_load_avar( TT_Face face )
359 FT_Stream stream = FT_FACE_STREAM( face );
360 FT_Memory memory = stream->memory;
363 GX_Blend blend = face->blend;
364 GX_AVarSegment segment;
371 #ifndef TT_CONFIG_OPTION_NO_BORING_EXPANSION
372 FT_ULong table_offset;
373 FT_ULong store_offset;
374 FT_ULong axisMap_offset;
378 FT_TRACE2(( "AVAR " ));
380 blend->avar_loaded = TRUE;
381 error = face->goto_table( face, TTAG_avar, stream, &table_len );
384 FT_TRACE2(( "is missing\n" ));
388 #ifndef TT_CONFIG_OPTION_NO_BORING_EXPANSION
389 table_offset = FT_STREAM_POS();
392 if ( FT_FRAME_ENTER( table_len ) )
395 version = FT_GET_LONG();
396 axisCount = FT_GET_LONG();
398 if ( version != 0x00010000L
399 #ifndef TT_CONFIG_OPTION_NO_BORING_EXPANSION
400 && version != 0x00020000L
404 FT_TRACE2(( "bad table version\n" ));
408 FT_TRACE2(( "loaded\n" ));
410 if ( axisCount != (FT_Long)blend->mmvar->num_axis )
412 FT_TRACE2(( "ft_var_load_avar:"
413 " number of axes in `avar' and `fvar'\n" ));
414 FT_TRACE2(( " table are different\n" ));
418 if ( FT_NEW( blend->avar_table ) )
420 table = blend->avar_table;
422 if ( FT_QNEW_ARRAY( table->avar_segment, axisCount ) )
425 segment = &table->avar_segment[0];
426 for ( i = 0; i < axisCount; i++, segment++ )
428 FT_TRACE5(( " axis %d:\n", i ));
430 segment->pairCount = FT_GET_USHORT();
431 if ( (FT_ULong)segment->pairCount * 4 > table_len ||
432 FT_QNEW_ARRAY( segment->correspondence, segment->pairCount ) )
434 /* Failure. Free everything we have done so far. We must do */
435 /* it right now since loading the `avar' table is optional. */
437 for ( j = i - 1; j >= 0; j-- )
438 FT_FREE( table->avar_segment[j].correspondence );
440 FT_FREE( table->avar_segment );
444 for ( j = 0; j < segment->pairCount; j++ )
446 segment->correspondence[j].fromCoord =
447 FT_fdot14ToFixed( FT_GET_SHORT() );
448 segment->correspondence[j].toCoord =
449 FT_fdot14ToFixed( FT_GET_SHORT() );
451 FT_TRACE5(( " mapping %.5f to %.5f\n",
452 (double)segment->correspondence[j].fromCoord / 65536,
453 (double)segment->correspondence[j].toCoord / 65536 ));
459 #ifndef TT_CONFIG_OPTION_NO_BORING_EXPANSION
460 if ( version < 0x00020000L )
463 axisMap_offset = FT_GET_ULONG();
464 store_offset = FT_GET_ULONG();
468 error = tt_var_load_item_variation_store(
470 table_offset + store_offset,
476 if ( axisMap_offset )
478 error = tt_var_load_delta_set_index_mapping(
480 table_offset + axisMap_offset,
495 FT_LOCAL_DEF( FT_Error )
496 tt_var_load_item_variation_store( FT_Face face, /* TT_Face */
498 GX_ItemVarStore itemStore )
500 TT_Face ttface = (TT_Face)face;
501 FT_Stream stream = FT_FACE_STREAM( face );
502 FT_Memory memory = stream->memory;
506 FT_ULong region_offset;
509 FT_UShort axis_count;
510 FT_UInt region_count;
515 GX_Blend blend = ttface->blend;
516 FT_ULong* dataOffsetArray = NULL;
519 if ( FT_STREAM_SEEK( offset ) ||
520 FT_READ_USHORT( format ) )
525 FT_TRACE2(( "tt_var_load_item_variation_store: bad store format %d\n",
527 error = FT_THROW( Invalid_Table );
531 /* read top level fields */
532 if ( FT_READ_ULONG( region_offset ) ||
533 FT_READ_USHORT( data_count ) )
536 /* we need at least one entry in `itemStore->varData' */
539 FT_TRACE2(( "tt_var_load_item_variation_store: missing varData\n" ));
540 error = FT_THROW( Invalid_Table );
544 /* make temporary copy of item variation data offsets; */
545 /* we will parse region list first, then come back */
546 if ( FT_QNEW_ARRAY( dataOffsetArray, data_count ) )
549 for ( i = 0; i < data_count; i++ )
551 if ( FT_READ_ULONG( dataOffsetArray[i] ) )
555 /* parse array of region records (region list) */
556 if ( FT_STREAM_SEEK( offset + region_offset ) )
559 if ( FT_READ_USHORT( axis_count ) ||
560 FT_READ_USHORT( region_count ) )
563 if ( axis_count != (FT_Long)blend->mmvar->num_axis )
565 FT_TRACE2(( "tt_var_load_item_variation_store:"
566 " number of axes in item variation store\n" ));
568 " and `fvar' table are different\n" ));
569 error = FT_THROW( Invalid_Table );
572 itemStore->axisCount = axis_count;
574 /* new constraint in OpenType 1.8.4 */
575 if ( region_count >= 32768U )
577 FT_TRACE2(( "tt_var_load_item_variation_store:"
578 " too many variation region tables\n" ));
579 error = FT_THROW( Invalid_Table );
583 if ( FT_NEW_ARRAY( itemStore->varRegionList, region_count ) )
585 itemStore->regionCount = region_count;
587 for ( i = 0; i < itemStore->regionCount; i++ )
589 GX_AxisCoords axisCoords;
592 if ( FT_NEW_ARRAY( itemStore->varRegionList[i].axisList, axis_count ) )
595 axisCoords = itemStore->varRegionList[i].axisList;
597 for ( j = 0; j < itemStore->axisCount; j++ )
599 FT_Short start, peak, end;
602 if ( FT_READ_SHORT( start ) ||
603 FT_READ_SHORT( peak ) ||
604 FT_READ_SHORT( end ) )
607 axisCoords[j].startCoord = FT_fdot14ToFixed( start );
608 axisCoords[j].peakCoord = FT_fdot14ToFixed( peak );
609 axisCoords[j].endCoord = FT_fdot14ToFixed( end );
613 /* end of region list parse */
615 /* use dataOffsetArray now to parse varData items */
616 if ( FT_NEW_ARRAY( itemStore->varData, data_count ) )
618 itemStore->dataCount = data_count;
620 for ( i = 0; i < data_count; i++ )
622 GX_ItemVarData varData = &itemStore->varData[i];
625 FT_UShort word_delta_count;
626 FT_UInt region_idx_count;
627 FT_UInt per_region_size;
630 if ( FT_STREAM_SEEK( offset + dataOffsetArray[i] ) )
633 if ( FT_READ_USHORT( item_count ) ||
634 FT_READ_USHORT( word_delta_count ) ||
635 FT_READ_USHORT( region_idx_count ) )
638 long_words = !!( word_delta_count & 0x8000 );
639 word_delta_count &= 0x7FFF;
641 /* check some data consistency */
642 if ( word_delta_count > region_idx_count )
644 FT_TRACE2(( "bad short count %d or region count %d\n",
647 error = FT_THROW( Invalid_Table );
651 if ( region_idx_count > itemStore->regionCount )
653 FT_TRACE2(( "inconsistent regionCount %d in varData[%d]\n",
656 error = FT_THROW( Invalid_Table );
660 /* parse region indices */
661 if ( FT_NEW_ARRAY( varData->regionIndices, region_idx_count ) )
663 varData->regionIdxCount = region_idx_count;
664 varData->wordDeltaCount = word_delta_count;
665 varData->longWords = long_words;
667 for ( j = 0; j < varData->regionIdxCount; j++ )
669 if ( FT_READ_USHORT( varData->regionIndices[j] ) )
672 if ( varData->regionIndices[j] >= itemStore->regionCount )
674 FT_TRACE2(( "bad region index %d\n",
675 varData->regionIndices[j] ));
676 error = FT_THROW( Invalid_Table );
681 per_region_size = word_delta_count + region_idx_count;
683 per_region_size *= 2;
685 if ( FT_NEW_ARRAY( varData->deltaSet, per_region_size * item_count ) )
687 if ( FT_Stream_Read( stream,
689 per_region_size * item_count ) )
691 FT_TRACE2(( "deltaSet read failed." ));
692 error = FT_THROW( Invalid_Table );
696 varData->itemCount = item_count;
700 FT_FREE( dataOffsetArray );
706 FT_LOCAL_DEF( FT_Error )
707 tt_var_load_delta_set_index_mapping( FT_Face face, /* TT_Face */
709 GX_DeltaSetIdxMap map,
710 GX_ItemVarStore itemStore,
713 FT_Stream stream = FT_FACE_STREAM( face );
714 FT_Memory memory = stream->memory;
721 FT_UInt innerBitCount;
722 FT_UInt innerIndexMask;
727 if ( FT_STREAM_SEEK( offset ) ||
728 FT_READ_BYTE( format ) ||
729 FT_READ_BYTE( entryFormat ) )
734 if ( FT_READ_USHORT( map->mapCount ) )
737 else if ( format == 1 ) /* new in OpenType 1.9 */
739 if ( FT_READ_ULONG( map->mapCount ) )
744 FT_TRACE2(( "bad map format %d\n", format ));
745 error = FT_THROW( Invalid_Table );
749 if ( entryFormat & 0xC0 )
751 FT_TRACE2(( "bad entry format %d\n", format ));
752 error = FT_THROW( Invalid_Table );
756 /* bytes per entry: 1, 2, 3, or 4 */
757 entrySize = ( ( entryFormat & 0x30 ) >> 4 ) + 1;
758 innerBitCount = ( entryFormat & 0x0F ) + 1;
759 innerIndexMask = ( 1 << innerBitCount ) - 1;
761 /* rough sanity check */
762 if ( map->mapCount * entrySize > table_len )
764 FT_TRACE1(( "tt_var_load_delta_set_index_mapping:"
765 " invalid number of delta-set index mappings\n" ));
766 error = FT_THROW( Invalid_Table );
770 if ( FT_NEW_ARRAY( map->innerIndex, map->mapCount ) )
773 if ( FT_NEW_ARRAY( map->outerIndex, map->mapCount ) )
776 for ( i = 0; i < map->mapCount; i++ )
779 FT_UInt outerIndex, innerIndex;
782 /* read map data one unsigned byte at a time, big endian */
783 for ( j = 0; j < entrySize; j++ )
788 if ( FT_READ_BYTE( data ) )
791 mapData = ( mapData << 8 ) | data;
794 /* new in OpenType 1.8.4 */
795 if ( mapData == 0xFFFFFFFFUL )
797 /* no variation data for this item */
798 map->outerIndex[i] = 0xFFFFU;
799 map->innerIndex[i] = 0xFFFFU;
804 outerIndex = mapData >> innerBitCount;
806 if ( outerIndex >= itemStore->dataCount )
808 FT_TRACE2(( "outerIndex[%ld] == %d out of range\n",
811 error = FT_THROW( Invalid_Table );
815 map->outerIndex[i] = outerIndex;
817 innerIndex = mapData & innerIndexMask;
819 if ( innerIndex >= itemStore->varData[outerIndex].itemCount )
821 FT_TRACE2(( "innerIndex[%ld] == %d out of range\n",
824 error = FT_THROW( Invalid_Table );
828 map->innerIndex[i] = innerIndex;
836 /**************************************************************************
842 * If `vertical' is zero, parse the `HVAR' table and set
843 * `blend->hvar_loaded' to TRUE. On success, `blend->hvar_checked'
846 * If `vertical' is not zero, parse the `VVAR' table and set
847 * `blend->vvar_loaded' to TRUE. On success, `blend->vvar_checked'
850 * Some memory may remain allocated on error; it is always freed in
851 * `tt_done_blend', however.
858 * FreeType error code. 0 means success.
861 ft_var_load_hvvar( TT_Face face,
864 FT_Stream stream = FT_FACE_STREAM( face );
865 FT_Memory memory = stream->memory;
867 GX_Blend blend = face->blend;
872 FT_UShort majorVersion;
874 FT_ULong table_offset;
875 FT_ULong store_offset;
876 FT_ULong widthMap_offset;
881 blend->vvar_loaded = TRUE;
883 FT_TRACE2(( "VVAR " ));
885 error = face->goto_table( face, TTAG_VVAR, stream, &table_len );
889 blend->hvar_loaded = TRUE;
891 FT_TRACE2(( "HVAR " ));
893 error = face->goto_table( face, TTAG_HVAR, stream, &table_len );
898 FT_TRACE2(( "is missing\n" ));
902 table_offset = FT_STREAM_POS();
904 /* skip minor version */
905 if ( FT_READ_USHORT( majorVersion ) ||
906 FT_STREAM_SKIP( 2 ) )
909 if ( majorVersion != 1 )
911 FT_TRACE2(( "bad table version %d\n", majorVersion ));
912 error = FT_THROW( Invalid_Table );
916 if ( FT_READ_ULONG( store_offset ) ||
917 FT_READ_ULONG( widthMap_offset ) )
922 if ( FT_NEW( blend->vvar_table ) )
924 table = blend->vvar_table;
928 if ( FT_NEW( blend->hvar_table ) )
930 table = blend->hvar_table;
933 error = tt_var_load_item_variation_store(
935 table_offset + store_offset,
940 if ( widthMap_offset )
942 error = tt_var_load_delta_set_index_mapping(
944 table_offset + widthMap_offset,
952 FT_TRACE2(( "loaded\n" ));
960 blend->vvar_checked = TRUE;
962 /* FreeType doesn't provide functions to quickly retrieve */
963 /* TSB, BSB, or VORG values; we thus don't have to implement */
964 /* support for those three item variation stores. */
966 face->variation_support |= TT_FACE_FLAG_VAR_VADVANCE;
970 blend->hvar_checked = TRUE;
972 /* FreeType doesn't provide functions to quickly retrieve */
973 /* LSB or RSB values; we thus don't have to implement */
974 /* support for those two item variation stores. */
976 face->variation_support |= TT_FACE_FLAG_VAR_HADVANCE;
984 FT_LOCAL_DEF( FT_ItemVarDelta )
985 tt_var_get_item_delta( FT_Face face, /* TT_Face */
986 GX_ItemVarStore itemStore,
990 TT_Face ttface = (TT_Face)face;
991 FT_Stream stream = FT_FACE_STREAM( face );
992 FT_Memory memory = stream->memory;
993 FT_Error error = FT_Err_Ok;
995 GX_ItemVarData varData;
996 FT_ItemVarDelta* deltaSet = NULL;
997 FT_ItemVarDelta deltaSetStack[16];
999 FT_Fixed* scalars = NULL;
1000 FT_Fixed scalarsStack[16];
1003 FT_ItemVarDelta returnValue = 0;
1004 FT_UInt per_region_size;
1008 if ( !ttface->blend || !ttface->blend->normalizedcoords )
1011 /* OpenType 1.8.4+: No variation data for this item */
1012 /* as indices have special value 0xFFFF. */
1013 if ( outerIndex == 0xFFFF && innerIndex == 0xFFFF )
1016 /* See pseudo code from `Font Variations Overview' */
1017 /* in the OpenType specification. */
1019 if ( outerIndex >= itemStore->dataCount )
1020 return 0; /* Out of range. */
1022 varData = &itemStore->varData[outerIndex];
1024 if ( innerIndex >= varData->itemCount )
1025 return 0; /* Out of range. */
1027 if ( varData->regionIdxCount < 16 )
1029 deltaSet = deltaSetStack;
1030 scalars = scalarsStack;
1034 if ( FT_QNEW_ARRAY( deltaSet, varData->regionIdxCount ) )
1036 if ( FT_QNEW_ARRAY( scalars, varData->regionIdxCount ) )
1040 /* Parse delta set. */
1042 /* Deltas are (word_delta_count + region_idx_count) bytes each */
1043 /* if `longWords` isn't set, and twice as much otherwise. */
1044 per_region_size = varData->wordDeltaCount + varData->regionIdxCount;
1045 if ( varData->longWords )
1046 per_region_size *= 2;
1048 bytes = varData->deltaSet + per_region_size * innerIndex;
1050 if ( varData->longWords )
1052 for ( master = 0; master < varData->wordDeltaCount; master++ )
1053 deltaSet[master] = FT_NEXT_LONG( bytes );
1054 for ( ; master < varData->regionIdxCount; master++ )
1055 deltaSet[master] = FT_NEXT_SHORT( bytes );
1059 for ( master = 0; master < varData->wordDeltaCount; master++ )
1060 deltaSet[master] = FT_NEXT_SHORT( bytes );
1061 for ( ; master < varData->regionIdxCount; master++ )
1062 deltaSet[master] = FT_NEXT_CHAR( bytes );
1065 /* outer loop steps through master designs to be blended */
1066 for ( master = 0; master < varData->regionIdxCount; master++ )
1068 FT_Fixed scalar = 0x10000L;
1069 FT_UInt regionIndex = varData->regionIndices[master];
1071 GX_AxisCoords axis = itemStore->varRegionList[regionIndex].axisList;
1074 /* inner loop steps through axes in this region */
1075 for ( j = 0; j < itemStore->axisCount; j++, axis++ )
1077 /* compute the scalar contribution of this axis; */
1078 /* ignore invalid ranges */
1079 if ( axis->startCoord > axis->peakCoord ||
1080 axis->peakCoord > axis->endCoord )
1083 else if ( axis->startCoord < 0 &&
1084 axis->endCoord > 0 &&
1085 axis->peakCoord != 0 )
1088 /* peak of 0 means ignore this axis */
1089 else if ( axis->peakCoord == 0 )
1092 else if ( ttface->blend->normalizedcoords[j] == axis->peakCoord )
1095 /* ignore this region if coords are out of range */
1096 else if ( ttface->blend->normalizedcoords[j] <= axis->startCoord ||
1097 ttface->blend->normalizedcoords[j] >= axis->endCoord )
1103 /* cumulative product of all the axis scalars */
1104 else if ( ttface->blend->normalizedcoords[j] < axis->peakCoord )
1107 ttface->blend->normalizedcoords[j] - axis->startCoord,
1108 axis->peakCoord - axis->startCoord );
1112 axis->endCoord - ttface->blend->normalizedcoords[j],
1113 axis->endCoord - axis->peakCoord );
1115 } /* per-axis loop */
1117 scalars[master] = scalar;
1119 } /* per-region loop */
1122 /* Compute the scaled delta for this region.
1124 * From: https://docs.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#item-variation-store-header-and-item-variation-data-subtables:
1126 * `Fixed` is a 32-bit (16.16) type and, in the general case, requires
1127 * 32-bit deltas. As described above, the `DeltaSet` record can
1128 * accommodate deltas that are, logically, either 16-bit or 32-bit.
1129 * When scaled deltas are applied to `Fixed` values, the `Fixed` value
1130 * is treated like a 32-bit integer.
1132 * `FT_MulAddFix` internally uses 64-bit precision; it thus can handle
1133 * deltas ranging from small 8-bit to large 32-bit values that are
1134 * applied to 16.16 `FT_Fixed` / OpenType `Fixed` values.
1136 returnValue = FT_MulAddFix( scalars, deltaSet, varData->regionIdxCount );
1139 if ( scalars != scalarsStack )
1141 if ( deltaSet != deltaSetStack )
1142 FT_FREE( deltaSet );
1148 /**************************************************************************
1151 * tt_hvadvance_adjust
1154 * Apply `HVAR' advance width or `VVAR' advance height adjustment of
1162 * If set, handle `VVAR' table.
1169 * Points to width or height value that gets modified.
1172 tt_hvadvance_adjust( TT_Face face,
1177 FT_Error error = FT_Err_Ok;
1178 FT_UInt innerIndex, outerIndex;
1181 GX_HVVarTable table;
1184 if ( !face->doblend || !face->blend )
1189 if ( !face->blend->vvar_loaded )
1191 /* initialize vvar table */
1192 face->blend->vvar_error = ft_var_load_hvvar( face, 1 );
1195 if ( !face->blend->vvar_checked )
1197 error = face->blend->vvar_error;
1201 table = face->blend->vvar_table;
1205 if ( !face->blend->hvar_loaded )
1207 /* initialize hvar table */
1208 face->blend->hvar_error = ft_var_load_hvvar( face, 0 );
1211 if ( !face->blend->hvar_checked )
1213 error = face->blend->hvar_error;
1217 table = face->blend->hvar_table;
1220 /* advance width or height adjustments are always present in an */
1221 /* `HVAR' or `VVAR' table; no need to test for this capability */
1223 if ( table->widthMap.innerIndex )
1225 FT_UInt idx = gindex;
1228 if ( idx >= table->widthMap.mapCount )
1229 idx = table->widthMap.mapCount - 1;
1231 /* trust that HVAR parser has checked indices */
1232 outerIndex = table->widthMap.outerIndex[idx];
1233 innerIndex = table->widthMap.innerIndex[idx];
1237 /* no widthMap data */
1239 innerIndex = gindex;
1242 delta = tt_var_get_item_delta( FT_FACE( face ),
1249 FT_TRACE5(( "%s value %d adjusted by %d unit%s (%s)\n",
1250 vertical ? "vertical height" : "horizontal width",
1253 delta == 1 ? "" : "s",
1254 vertical ? "VVAR" : "HVAR" ));
1256 *avalue = ADD_INT( *avalue, delta );
1264 FT_LOCAL_DEF( FT_Error )
1265 tt_hadvance_adjust( FT_Face face, /* TT_Face */
1269 return tt_hvadvance_adjust( (TT_Face)face, gindex, avalue, 0 );
1273 FT_LOCAL_DEF( FT_Error )
1274 tt_vadvance_adjust( FT_Face face, /* TT_Face */
1278 return tt_hvadvance_adjust( (TT_Face)face, gindex, avalue, 1 );
1282 #define GX_VALUE_SIZE 8
1284 /* all values are FT_Short or FT_UShort entities; */
1285 /* we treat them consistently as FT_Short */
1286 #define GX_VALUE_CASE( tag, dflt ) \
1287 case MVAR_TAG_ ## tag : \
1288 p = (FT_Short*)&face->dflt; \
1291 #define GX_GASP_CASE( idx ) \
1292 case MVAR_TAG_GASP_ ## idx : \
1293 if ( idx < face->gasp.numRanges - 1 ) \
1294 p = (FT_Short*)&face->gasp.gaspRanges[idx].maxPPEM; \
1301 ft_var_get_value_pointer( TT_Face face,
1320 GX_VALUE_CASE( CPHT, os2.sCapHeight );
1321 GX_VALUE_CASE( HASC, os2.sTypoAscender );
1322 GX_VALUE_CASE( HCLA, os2.usWinAscent );
1323 GX_VALUE_CASE( HCLD, os2.usWinDescent );
1324 GX_VALUE_CASE( HCOF, horizontal.caret_Offset );
1325 GX_VALUE_CASE( HCRN, horizontal.caret_Slope_Run );
1326 GX_VALUE_CASE( HCRS, horizontal.caret_Slope_Rise );
1327 GX_VALUE_CASE( HDSC, os2.sTypoDescender );
1328 GX_VALUE_CASE( HLGP, os2.sTypoLineGap );
1329 GX_VALUE_CASE( SBXO, os2.ySubscriptXOffset);
1330 GX_VALUE_CASE( SBXS, os2.ySubscriptXSize );
1331 GX_VALUE_CASE( SBYO, os2.ySubscriptYOffset );
1332 GX_VALUE_CASE( SBYS, os2.ySubscriptYSize );
1333 GX_VALUE_CASE( SPXO, os2.ySuperscriptXOffset );
1334 GX_VALUE_CASE( SPXS, os2.ySuperscriptXSize );
1335 GX_VALUE_CASE( SPYO, os2.ySuperscriptYOffset );
1336 GX_VALUE_CASE( SPYS, os2.ySuperscriptYSize );
1337 GX_VALUE_CASE( STRO, os2.yStrikeoutPosition );
1338 GX_VALUE_CASE( STRS, os2.yStrikeoutSize );
1339 GX_VALUE_CASE( UNDO, postscript.underlinePosition );
1340 GX_VALUE_CASE( UNDS, postscript.underlineThickness );
1341 GX_VALUE_CASE( VASC, vertical.Ascender );
1342 GX_VALUE_CASE( VCOF, vertical.caret_Offset );
1343 GX_VALUE_CASE( VCRN, vertical.caret_Slope_Run );
1344 GX_VALUE_CASE( VCRS, vertical.caret_Slope_Rise );
1345 GX_VALUE_CASE( VDSC, vertical.Descender );
1346 GX_VALUE_CASE( VLGP, vertical.Line_Gap );
1347 GX_VALUE_CASE( XHGT, os2.sxHeight );
1350 /* ignore unknown tag */
1358 /**************************************************************************
1364 * Parse the `MVAR' table.
1366 * Some memory may remain allocated on error; it is always freed in
1367 * `tt_done_blend', however.
1374 ft_var_load_mvar( TT_Face face )
1376 FT_Stream stream = FT_FACE_STREAM( face );
1377 FT_Memory memory = stream->memory;
1379 GX_Blend blend = face->blend;
1380 GX_ItemVarStore itemStore;
1381 GX_Value value, limit;
1384 FT_UShort majorVersion;
1386 FT_ULong table_offset;
1387 FT_UShort store_offset;
1388 FT_ULong records_offset;
1391 FT_TRACE2(( "MVAR " ));
1393 error = face->goto_table( face, TTAG_MVAR, stream, &table_len );
1396 FT_TRACE2(( "is missing\n" ));
1400 table_offset = FT_STREAM_POS();
1402 /* skip minor version */
1403 if ( FT_READ_USHORT( majorVersion ) ||
1404 FT_STREAM_SKIP( 2 ) )
1407 if ( majorVersion != 1 )
1409 FT_TRACE2(( "bad table version %d\n", majorVersion ));
1413 if ( FT_NEW( blend->mvar_table ) )
1416 /* skip reserved entry and value record size */
1417 if ( FT_STREAM_SKIP( 4 ) ||
1418 FT_READ_USHORT( blend->mvar_table->valueCount ) ||
1419 FT_READ_USHORT( store_offset ) )
1422 records_offset = FT_STREAM_POS();
1424 error = tt_var_load_item_variation_store(
1426 table_offset + store_offset,
1427 &blend->mvar_table->itemStore );
1431 if ( FT_NEW_ARRAY( blend->mvar_table->values,
1432 blend->mvar_table->valueCount ) )
1435 if ( FT_STREAM_SEEK( records_offset ) ||
1436 FT_FRAME_ENTER( blend->mvar_table->valueCount * GX_VALUE_SIZE ) )
1439 value = blend->mvar_table->values;
1440 limit = FT_OFFSET( value, blend->mvar_table->valueCount );
1441 itemStore = &blend->mvar_table->itemStore;
1443 for ( ; value < limit; value++ )
1445 value->tag = FT_GET_ULONG();
1446 value->outerIndex = FT_GET_USHORT();
1447 value->innerIndex = FT_GET_USHORT();
1449 /* new in OpenType 1.8.4 */
1450 if ( value->outerIndex == 0xFFFFU && value->innerIndex == 0xFFFFU )
1452 /* no variation data for this item */
1456 if ( value->outerIndex >= itemStore->dataCount ||
1457 value->innerIndex >= itemStore->varData[value->outerIndex]
1460 error = FT_THROW( Invalid_Table );
1470 FT_TRACE2(( "loaded\n" ));
1472 value = blend->mvar_table->values;
1473 limit = FT_OFFSET( value, blend->mvar_table->valueCount );
1475 /* save original values of the data MVAR is going to modify */
1476 for ( ; value < limit; value++ )
1478 FT_Short* p = ft_var_get_value_pointer( face, value->tag );
1482 value->unmodified = *p;
1483 #ifdef FT_DEBUG_LEVEL_TRACE
1485 FT_TRACE1(( "ft_var_load_mvar: Ignoring unknown tag `%c%c%c%c'\n",
1486 (FT_Char)( value->tag >> 24 ),
1487 (FT_Char)( value->tag >> 16 ),
1488 (FT_Char)( value->tag >> 8 ),
1489 (FT_Char)( value->tag ) ));
1493 face->variation_support |= TT_FACE_FLAG_VAR_MVAR;
1498 ft_size_reset_iterator( FT_ListNode node,
1501 FT_Size size = (FT_Size)node->data;
1502 FT_Service_MetricsVariations var = (FT_Service_MetricsVariations)user;
1505 var->size_reset( size );
1511 /**************************************************************************
1517 * Apply `MVAR' table adjustments.
1523 FT_LOCAL_DEF( void )
1524 tt_apply_mvar( FT_Face face ) /* TT_Face */
1526 TT_Face ttface = (TT_Face)face;
1528 GX_Blend blend = ttface->blend;
1529 GX_Value value, limit;
1531 FT_Short mvar_hasc_delta = 0;
1532 FT_Short mvar_hdsc_delta = 0;
1533 FT_Short mvar_hlgp_delta = 0;
1536 if ( !( ttface->variation_support & TT_FACE_FLAG_VAR_MVAR ) )
1539 value = blend->mvar_table->values;
1540 limit = FT_OFFSET( value, blend->mvar_table->valueCount );
1542 for ( ; value < limit; value++ )
1544 FT_Short* p = ft_var_get_value_pointer( ttface, value->tag );
1548 delta = tt_var_get_item_delta( face,
1549 &blend->mvar_table->itemStore,
1551 value->innerIndex );
1555 FT_TRACE5(( "value %c%c%c%c (%d unit%s) adjusted by %d unit%s (MVAR)\n",
1556 (FT_Char)( value->tag >> 24 ),
1557 (FT_Char)( value->tag >> 16 ),
1558 (FT_Char)( value->tag >> 8 ),
1559 (FT_Char)( value->tag ),
1561 value->unmodified == 1 ? "" : "s",
1563 delta == 1 ? "" : "s" ));
1565 /* since we handle both signed and unsigned values as FT_Short, */
1566 /* ensure proper overflow arithmetic */
1567 *p = (FT_Short)( value->unmodified + (FT_Short)delta );
1569 /* Treat hasc, hdsc and hlgp specially, see below. */
1570 if ( value->tag == MVAR_TAG_HASC )
1571 mvar_hasc_delta = (FT_Short)delta;
1572 else if ( value->tag == MVAR_TAG_HDSC )
1573 mvar_hdsc_delta = (FT_Short)delta;
1574 else if ( value->tag == MVAR_TAG_HLGP )
1575 mvar_hlgp_delta = (FT_Short)delta;
1579 /* adjust all derived values */
1581 FT_Service_MetricsVariations var =
1582 (FT_Service_MetricsVariations)ttface->face_var;
1585 * Apply the deltas of hasc, hdsc and hlgp to the FT_Face's ascender,
1586 * descender and height attributes, no matter how they were originally
1589 * (Code that ignores those and accesses the font's metrics values
1590 * directly is already served by the delta application code above.)
1592 * The MVAR table supports variations for both typo and win metrics.
1593 * According to Behdad Esfahbod, the thinking of the working group was
1594 * that no one uses win metrics anymore for setting line metrics (the
1595 * specification even calls these metrics "horizontal clipping
1596 * ascent/descent", probably for their role on the Windows platform in
1597 * computing clipping boxes), and new fonts should use typo metrics, so
1598 * typo deltas should be applied to whatever sfnt_load_face decided the
1599 * line metrics should be.
1601 * Before, the following led to different line metrics between default
1602 * outline and instances, visible when e.g. the default outlines were
1603 * used as the regular face and instances for everything else:
1605 * 1. sfnt_load_face applied the hhea metrics by default.
1606 * 2. This code later applied the typo metrics by default, regardless of
1607 * whether they were actually changed or the font had the OS/2 table's
1608 * fsSelection's bit 7 (USE_TYPO_METRICS) set.
1610 FT_Short current_line_gap = face->height - face->ascender +
1614 face->ascender = face->ascender + mvar_hasc_delta;
1615 face->descender = face->descender + mvar_hdsc_delta;
1616 face->height = face->ascender - face->descender +
1617 current_line_gap + mvar_hlgp_delta;
1619 face->underline_position = ttface->postscript.underlinePosition -
1620 ttface->postscript.underlineThickness / 2;
1621 face->underline_thickness = ttface->postscript.underlineThickness;
1623 /* iterate over all FT_Size objects and call `var->size_reset' */
1624 /* to propagate the metrics changes */
1625 if ( var && var->size_reset )
1626 FT_List_Iterate( &face->sizes_list,
1627 ft_size_reset_iterator,
1633 typedef struct GX_GVar_Head_
1636 FT_UShort axisCount;
1637 FT_UShort globalCoordCount;
1638 FT_ULong offsetToCoord;
1639 FT_UShort glyphCount;
1641 FT_ULong offsetToData;
1646 /**************************************************************************
1652 * Parse the `gvar' table if present. If `fvar' is there, `gvar' had
1653 * better be there too.
1660 * FreeType error code. 0 means success.
1663 ft_var_load_gvar( TT_Face face )
1665 FT_Stream stream = FT_FACE_STREAM( face );
1666 FT_Memory memory = stream->memory;
1667 GX_Blend blend = face->blend;
1671 FT_ULong gvar_start;
1672 FT_ULong offsetToData;
1673 FT_ULong offsets_len;
1674 GX_GVar_Head gvar_head;
1676 static const FT_Frame_Field gvar_fields[] =
1680 #define FT_STRUCTURE GX_GVar_Head
1682 FT_FRAME_START( 20 ),
1683 FT_FRAME_LONG ( version ),
1684 FT_FRAME_USHORT( axisCount ),
1685 FT_FRAME_USHORT( globalCoordCount ),
1686 FT_FRAME_ULONG ( offsetToCoord ),
1687 FT_FRAME_USHORT( glyphCount ),
1688 FT_FRAME_USHORT( flags ),
1689 FT_FRAME_ULONG ( offsetToData ),
1694 FT_TRACE2(( "GVAR " ));
1696 if ( FT_SET_ERROR( face->goto_table( face,
1701 FT_TRACE2(( "is missing\n" ));
1705 gvar_start = FT_STREAM_POS( );
1706 if ( FT_STREAM_READ_FIELDS( gvar_fields, &gvar_head ) )
1709 if ( gvar_head.version != 0x00010000L )
1711 FT_TRACE1(( "bad table version\n" ));
1712 error = FT_THROW( Invalid_Table );
1716 if ( gvar_head.axisCount != (FT_UShort)blend->mmvar->num_axis )
1718 FT_TRACE1(( "ft_var_load_gvar:"
1719 " number of axes in `gvar' and `cvar'\n" ));
1720 FT_TRACE1(( " table are different\n" ));
1721 error = FT_THROW( Invalid_Table );
1725 /* rough sanity check, ignoring offsets */
1726 if ( (FT_ULong)gvar_head.globalCoordCount * gvar_head.axisCount >
1729 FT_TRACE1(( "ft_var_load_gvar:"
1730 " invalid number of global coordinates\n" ));
1731 error = FT_THROW( Invalid_Table );
1735 /* offsets can be either 2 or 4 bytes */
1736 /* (one more offset than glyphs, to mark size of last) */
1737 offsets_len = ( gvar_head.glyphCount + 1 ) *
1738 ( ( gvar_head.flags & 1 ) ? 4L : 2L );
1740 /* rough sanity check */
1741 if (offsets_len > table_len )
1743 FT_TRACE1(( "ft_var_load_gvar: invalid number of glyphs\n" ));
1744 error = FT_THROW( Invalid_Table );
1748 FT_TRACE2(( "loaded\n" ));
1750 blend->gvar_size = table_len;
1751 offsetToData = gvar_start + gvar_head.offsetToData;
1753 FT_TRACE5(( "gvar: there %s %d shared coordinate%s:\n",
1754 gvar_head.globalCoordCount == 1 ? "is" : "are",
1755 gvar_head.globalCoordCount,
1756 gvar_head.globalCoordCount == 1 ? "" : "s" ));
1758 if ( FT_FRAME_ENTER( offsets_len ) )
1761 /* offsets (one more offset than glyphs, to mark size of last) */
1762 if ( FT_QNEW_ARRAY( blend->glyphoffsets, gvar_head.glyphCount + 1 ) )
1765 if ( gvar_head.flags & 1 )
1767 FT_ULong limit = gvar_start + table_len;
1768 FT_ULong max_offset = 0;
1771 for ( i = 0; i <= gvar_head.glyphCount; i++ )
1773 blend->glyphoffsets[i] = offsetToData + FT_GET_ULONG();
1775 if ( max_offset <= blend->glyphoffsets[i] )
1776 max_offset = blend->glyphoffsets[i];
1779 FT_TRACE2(( "ft_var_load_gvar:"
1780 " glyph variation data offset %d not monotonic\n",
1782 blend->glyphoffsets[i] = max_offset;
1785 /* use `<', not `<=' */
1786 if ( limit < blend->glyphoffsets[i] )
1788 FT_TRACE2(( "ft_var_load_gvar:"
1789 " glyph variation data offset %d out of range\n",
1791 blend->glyphoffsets[i] = limit;
1797 FT_ULong limit = gvar_start + table_len;
1798 FT_ULong max_offset = 0;
1801 for ( i = 0; i <= gvar_head.glyphCount; i++ )
1803 blend->glyphoffsets[i] = offsetToData + FT_GET_USHORT() * 2;
1805 if ( max_offset <= blend->glyphoffsets[i] )
1806 max_offset = blend->glyphoffsets[i];
1809 FT_TRACE2(( "ft_var_load_gvar:"
1810 " glyph variation data offset %d not monotonic\n",
1812 blend->glyphoffsets[i] = max_offset;
1815 /* use `<', not `<=' */
1816 if ( limit < blend->glyphoffsets[i] )
1818 FT_TRACE2(( "ft_var_load_gvar:"
1819 " glyph variation data offset %d out of range\n",
1821 blend->glyphoffsets[i] = limit;
1826 blend->gv_glyphcnt = gvar_head.glyphCount;
1830 if ( gvar_head.globalCoordCount != 0 )
1832 if ( FT_STREAM_SEEK( gvar_start + gvar_head.offsetToCoord ) ||
1833 FT_FRAME_ENTER( gvar_head.globalCoordCount *
1834 gvar_head.axisCount * 2L ) )
1836 FT_TRACE2(( "ft_var_load_gvar:"
1837 " glyph variation shared tuples missing\n" ));
1841 if ( FT_QNEW_ARRAY( blend->tuplecoords,
1842 gvar_head.axisCount * gvar_head.globalCoordCount ) )
1845 for ( i = 0; i < gvar_head.globalCoordCount; i++ )
1847 FT_TRACE5(( " [ " ));
1848 for ( j = 0; j < (FT_UInt)gvar_head.axisCount; j++ )
1850 blend->tuplecoords[i * gvar_head.axisCount + j] =
1851 FT_fdot14ToFixed( FT_GET_SHORT() );
1852 FT_TRACE5(( "%.5f ",
1853 (double)blend->tuplecoords[i * gvar_head.axisCount + j] / 65536 ));
1855 FT_TRACE5(( "]\n" ));
1858 blend->tuplecount = gvar_head.globalCoordCount;
1860 FT_TRACE5(( "\n" ));
1872 FT_FREE( blend->glyphoffsets );
1873 blend->gv_glyphcnt = 0;
1878 /**************************************************************************
1881 * ft_var_apply_tuple
1884 * Figure out whether a given tuple (design) applies to the current
1885 * blend, and if so, what is the scaling factor.
1889 * The current blend of the font.
1892 * A flag saying whether this is an intermediate
1896 * The coordinates of the tuple in normalized axis
1899 * im_start_coords ::
1900 * The initial coordinates where this tuple starts
1901 * to apply (for intermediate coordinates).
1904 * The final coordinates after which this tuple no
1905 * longer applies (for intermediate coordinates).
1908 * An FT_Fixed value containing the scaling factor.
1911 ft_var_apply_tuple( GX_Blend blend,
1912 FT_UShort tupleIndex,
1913 FT_Fixed* tuple_coords,
1914 FT_Fixed* im_start_coords,
1915 FT_Fixed* im_end_coords )
1918 FT_Fixed apply = 0x10000L;
1921 for ( i = 0; i < blend->num_axis; i++ )
1923 FT_TRACE6(( " axis %d coordinate %.5f:\n",
1924 i, (double)blend->normalizedcoords[i] / 65536 ));
1926 /* It's not clear why (for intermediate tuples) we don't need */
1927 /* to check against start/end -- the documentation says we don't. */
1928 /* Similarly, it's unclear why we don't need to scale along the */
1931 if ( tuple_coords[i] == 0 )
1933 FT_TRACE6(( " tuple coordinate is zero, ignore\n" ));
1937 if ( blend->normalizedcoords[i] == 0 )
1939 FT_TRACE6(( " axis coordinate is zero, stop\n" ));
1944 if ( blend->normalizedcoords[i] == tuple_coords[i] )
1946 FT_TRACE6(( " tuple coordinate %.5f fits perfectly\n",
1947 (double)tuple_coords[i] / 65536 ));
1948 /* `apply' does not change */
1952 if ( !( tupleIndex & GX_TI_INTERMEDIATE_TUPLE ) )
1954 /* not an intermediate tuple */
1956 if ( blend->normalizedcoords[i] < FT_MIN( 0, tuple_coords[i] ) ||
1957 blend->normalizedcoords[i] > FT_MAX( 0, tuple_coords[i] ) )
1959 FT_TRACE6(( " tuple coordinate %.5f is exceeded, stop\n",
1960 (double)tuple_coords[i] / 65536 ));
1965 FT_TRACE6(( " tuple coordinate %.5f fits\n",
1966 (double)tuple_coords[i] / 65536 ));
1967 apply = FT_MulDiv( apply,
1968 blend->normalizedcoords[i],
1973 /* intermediate tuple */
1975 if ( blend->normalizedcoords[i] <= im_start_coords[i] ||
1976 blend->normalizedcoords[i] >= im_end_coords[i] )
1978 FT_TRACE6(( " intermediate tuple range ]%.5f;%.5f[ is exceeded,"
1980 (double)im_start_coords[i] / 65536,
1981 (double)im_end_coords[i] / 65536 ));
1986 FT_TRACE6(( " intermediate tuple range ]%.5f;%.5f[ fits\n",
1987 (double)im_start_coords[i] / 65536,
1988 (double)im_end_coords[i] / 65536 ));
1989 if ( blend->normalizedcoords[i] < tuple_coords[i] )
1990 apply = FT_MulDiv( apply,
1991 blend->normalizedcoords[i] - im_start_coords[i],
1992 tuple_coords[i] - im_start_coords[i] );
1994 apply = FT_MulDiv( apply,
1995 im_end_coords[i] - blend->normalizedcoords[i],
1996 im_end_coords[i] - tuple_coords[i] );
2000 FT_TRACE6(( " apply factor is %.5f\n", (double)apply / 65536 ));
2006 /* convert from design coordinates to normalized coordinates */
2009 ft_var_to_normalized( TT_Face face,
2012 FT_Fixed* normalized )
2014 FT_Error error = FT_Err_Ok;
2015 FT_Memory memory = face->root.memory;
2023 FT_Fixed* new_normalized = NULL;
2024 FT_Fixed* old_normalized;
2027 blend = face->blend;
2028 mmvar = blend->mmvar;
2030 if ( num_coords > mmvar->num_axis )
2032 FT_TRACE2(( "ft_var_to_normalized:"
2033 " only using first %d of %d coordinates\n",
2034 mmvar->num_axis, num_coords ));
2035 num_coords = mmvar->num_axis;
2038 /* Axis normalization is a two-stage process. First we normalize */
2039 /* based on the [min,def,max] values for the axis to be [-1,0,1]. */
2040 /* Then, if there's an `avar' table, we renormalize this range. */
2043 for ( i = 0; i < num_coords; i++, a++ )
2045 FT_Fixed coord = coords[i];
2048 FT_TRACE5(( " %d: %.5f\n", i, (double)coord / 65536 ));
2049 if ( coord > a->maximum || coord < a->minimum )
2051 FT_TRACE1(( "ft_var_to_normalized: design coordinate %.5f\n",
2052 (double)coord / 65536 ));
2053 FT_TRACE1(( " is out of range [%.5f;%.5f];"
2055 (double)a->minimum / 65536,
2056 (double)a->maximum / 65536 ));
2059 if ( coord > a->def )
2060 normalized[i] = coord >= a->maximum ? 0x10000L :
2061 FT_DivFix( SUB_LONG( coord, a->def ),
2062 SUB_LONG( a->maximum, a->def ) );
2063 else if ( coord < a->def )
2064 normalized[i] = coord <= a->minimum ? -0x10000L :
2065 FT_DivFix( SUB_LONG( coord, a->def ),
2066 SUB_LONG( a->def, a->minimum ) );
2071 FT_TRACE5(( "\n" ));
2073 for ( ; i < mmvar->num_axis; i++ )
2076 if ( blend->avar_table )
2078 GX_AVarTable table = blend->avar_table;
2081 FT_TRACE5(( "normalized design coordinates"
2082 " before applying `avar' data:\n" ));
2084 if ( table->avar_segment )
2086 av = table->avar_segment;
2088 for ( i = 0; i < mmvar->num_axis; i++, av++ )
2090 for ( j = 1; j < (FT_UInt)av->pairCount; j++ )
2092 if ( normalized[i] < av->correspondence[j].fromCoord )
2094 FT_TRACE5(( " %.5f\n", (double)normalized[i] / 65536 ));
2097 FT_MulDiv( normalized[i] - av->correspondence[j - 1].fromCoord,
2098 av->correspondence[j].toCoord -
2099 av->correspondence[j - 1].toCoord,
2100 av->correspondence[j].fromCoord -
2101 av->correspondence[j - 1].fromCoord ) +
2102 av->correspondence[j - 1].toCoord;
2109 if ( table->itemStore.varData )
2111 if ( FT_QNEW_ARRAY( new_normalized, mmvar->num_axis ) )
2114 /* Install our half-normalized coordinates for the next */
2115 /* Item Variation Store to work with. */
2116 old_normalized = face->blend->normalizedcoords;
2117 face->blend->normalizedcoords = normalized;
2119 for ( i = 0; i < mmvar->num_axis; i++ )
2121 FT_Fixed v = normalized[i];
2122 FT_UInt innerIndex = i;
2123 FT_UInt outerIndex = 0;
2127 if ( table->axisMap.innerIndex )
2132 if ( idx >= table->axisMap.mapCount )
2133 idx = table->axisMap.mapCount - 1;
2135 outerIndex = table->axisMap.outerIndex[idx];
2136 innerIndex = table->axisMap.innerIndex[idx];
2139 delta = tt_var_get_item_delta( FT_FACE( face ),
2146 /* Clamp value range. */
2147 v = v >= 0x10000L ? 0x10000 : v;
2148 v = v <= -0x10000L ? -0x10000 : v;
2150 new_normalized[i] = v;
2153 for ( i = 0; i < mmvar->num_axis; i++ )
2155 normalized[i] = new_normalized[i];
2158 face->blend->normalizedcoords = old_normalized;
2160 FT_FREE( new_normalized );
2166 /* convert from normalized coordinates to design coordinates */
2169 ft_var_to_design( TT_Face face,
2181 blend = face->blend;
2184 if ( num_coords > blend->num_axis )
2186 FT_TRACE2(( "ft_var_to_design:"
2187 " only using first %d of %d coordinates\n",
2188 blend->num_axis, num_coords ));
2189 nc = blend->num_axis;
2192 for ( i = 0; i < nc; i++ )
2193 design[i] = coords[i];
2195 for ( ; i < num_coords; i++ )
2198 if ( blend->avar_table && blend->avar_table->avar_segment )
2200 GX_AVarSegment av = blend->avar_table->avar_segment;
2203 FT_TRACE5(( "design coordinates"
2204 " after removing `avar' distortion:\n" ));
2206 for ( i = 0; i < nc; i++, av++ )
2208 for ( j = 1; j < (FT_UInt)av->pairCount; j++ )
2210 if ( design[i] < av->correspondence[j].toCoord )
2213 FT_MulDiv( design[i] - av->correspondence[j - 1].toCoord,
2214 av->correspondence[j].fromCoord -
2215 av->correspondence[j - 1].fromCoord,
2216 av->correspondence[j].toCoord -
2217 av->correspondence[j - 1].toCoord ) +
2218 av->correspondence[j - 1].fromCoord;
2220 FT_TRACE5(( " %.5f\n", (double)design[i] / 65536 ));
2227 mmvar = blend->mmvar;
2230 for ( i = 0; i < nc; i++, a++ )
2232 if ( design[i] < 0 )
2233 design[i] = a->def + FT_MulFix( design[i],
2234 a->def - a->minimum );
2235 else if ( design[i] > 0 )
2236 design[i] = a->def + FT_MulFix( design[i],
2237 a->maximum - a->def );
2244 /*************************************************************************/
2245 /*************************************************************************/
2247 /***** MULTIPLE MASTERS SERVICE FUNCTIONS *****/
2249 /*************************************************************************/
2250 /*************************************************************************/
2253 typedef struct GX_FVar_Head_
2256 FT_UShort offsetToData;
2257 FT_UShort axisCount;
2259 FT_UShort instanceCount;
2260 FT_UShort instanceSize;
2265 typedef struct fvar_axis_
2269 FT_Fixed defaultValue;
2277 /**************************************************************************
2283 * Check that the font's `fvar' table is valid, parse it, and return
2284 * those data. It also loads (and parses) the `MVAR' table, if
2290 * TT_Get_MM_Var initializes the blend structure.
2294 * The `fvar' data (must be freed by caller). Can be NULL,
2295 * which makes this function simply load MM support.
2298 * FreeType error code. 0 means success.
2300 FT_LOCAL_DEF( FT_Error )
2301 TT_Get_MM_Var( FT_Face face, /* TT_Face */
2302 FT_MM_Var* *master )
2304 TT_Face ttface = (TT_Face)face;
2305 FT_Stream stream = FT_FACE_STREAM( face );
2306 FT_Memory memory = FT_FACE_MEMORY( face );
2308 FT_Error error = FT_Err_Ok;
2309 FT_ULong fvar_start = 0;
2311 FT_MM_Var* mmvar = NULL;
2312 FT_Fixed* next_coords;
2314 FT_String* next_name;
2317 FT_Var_Named_Style* ns;
2318 GX_FVar_Head fvar_head = { 0, 0, 0, 0, 0, 0 };
2319 FT_Bool usePsName = 0;
2320 FT_UInt num_instances;
2322 FT_UShort* axis_flags;
2324 FT_Offset mmvar_size;
2325 FT_Offset axis_flags_size;
2326 FT_Offset axis_size;
2327 FT_Offset namedstyle_size;
2328 FT_Offset next_coords_size;
2329 FT_Offset next_name_size;
2333 static const FT_Frame_Field fvar_fields[] =
2337 #define FT_STRUCTURE GX_FVar_Head
2339 FT_FRAME_START( 16 ),
2340 FT_FRAME_LONG ( version ),
2341 FT_FRAME_USHORT ( offsetToData ),
2342 FT_FRAME_SKIP_SHORT,
2343 FT_FRAME_USHORT ( axisCount ),
2344 FT_FRAME_USHORT ( axisSize ),
2345 FT_FRAME_USHORT ( instanceCount ),
2346 FT_FRAME_USHORT ( instanceSize ),
2350 static const FT_Frame_Field fvaraxis_fields[] =
2354 #define FT_STRUCTURE GX_FVar_Axis
2356 FT_FRAME_START( 20 ),
2357 FT_FRAME_ULONG ( axisTag ),
2358 FT_FRAME_LONG ( minValue ),
2359 FT_FRAME_LONG ( defaultValue ),
2360 FT_FRAME_LONG ( maxValue ),
2361 FT_FRAME_USHORT( flags ),
2362 FT_FRAME_USHORT( nameID ),
2366 /* `num_instances` holds the number of all named instances including */
2367 /* the default instance, which might be missing in the table of named */
2368 /* instances (in 'fvar'). This value is validated in `sfobjs.c` and */
2369 /* may be reset to 0 if consistency checks fail. */
2370 num_instances = (FT_UInt)face->style_flags >> 16;
2372 /* read the font data and set up the internal representation */
2373 /* if not already done */
2375 need_init = !ttface->blend;
2379 FT_TRACE2(( "FVAR " ));
2381 if ( FT_SET_ERROR( ttface->goto_table( ttface, TTAG_fvar,
2382 stream, &table_len ) ) )
2384 FT_TRACE1(( "is missing\n" ));
2388 fvar_start = FT_STREAM_POS( );
2390 /* the validity of the `fvar' header data was already checked */
2391 /* in function `sfnt_init_face' */
2392 if ( FT_STREAM_READ_FIELDS( fvar_fields, &fvar_head ) )
2395 /* If `num_instances` is larger, synthetization of the default */
2396 /* instance is required. If `num_instances` is smaller, */
2397 /* however, the value has been reset to 0 in `sfnt_init_face` */
2398 /* (in `sfobjs.c`); in this case we have underallocated `mmvar` */
2400 if ( num_instances < fvar_head.instanceCount )
2402 error = FT_THROW( Invalid_Table );
2406 usePsName = FT_BOOL( fvar_head.instanceSize ==
2407 6 + 4 * fvar_head.axisCount );
2409 FT_TRACE2(( "loaded\n" ));
2411 FT_TRACE5(( "%d variation ax%s\n",
2412 fvar_head.axisCount,
2413 fvar_head.axisCount == 1 ? "is" : "es" ));
2415 if ( FT_NEW( ttface->blend ) )
2418 num_axes = fvar_head.axisCount;
2419 ttface->blend->num_axis = num_axes;
2422 num_axes = ttface->blend->num_axis;
2424 /* prepare storage area for MM data; this cannot overflow */
2425 /* 32-bit arithmetic because of the size limits used in the */
2426 /* `fvar' table validity check in `sfnt_init_face' */
2428 /* the various `*_size' variables, which we also use as */
2429 /* offsets into the `mmvar' array, must be multiples of the */
2430 /* pointer size (except the last one); without such an */
2431 /* alignment there might be runtime errors due to */
2432 /* misaligned addresses */
2434 #define ALIGN_SIZE( n ) \
2435 ( ( (n) + sizeof (void*) - 1 ) & ~( sizeof (void*) - 1 ) )
2437 mmvar_size = ALIGN_SIZE( sizeof ( FT_MM_Var ) );
2438 axis_flags_size = ALIGN_SIZE( num_axes *
2439 sizeof ( FT_UShort ) );
2440 axis_size = ALIGN_SIZE( num_axes *
2441 sizeof ( FT_Var_Axis ) );
2442 namedstyle_size = ALIGN_SIZE( num_instances *
2443 sizeof ( FT_Var_Named_Style ) );
2444 next_coords_size = ALIGN_SIZE( num_instances *
2446 sizeof ( FT_Fixed ) );
2447 next_name_size = num_axes * 5;
2451 ttface->blend->mmvar_len = mmvar_size +
2458 if ( FT_ALLOC( mmvar, ttface->blend->mmvar_len ) )
2460 ttface->blend->mmvar = mmvar;
2462 /* set up pointers and offsets into the `mmvar' array; */
2463 /* the data gets filled in later on */
2467 mmvar->num_designs =
2468 ~0U; /* meaningless in this context; each glyph */
2469 /* may have a different number of designs */
2470 /* (or tuples, as called by Apple) */
2471 mmvar->num_namedstyles =
2474 /* alas, no public field in `FT_Var_Axis' for axis flags */
2476 (FT_UShort*)( (char*)mmvar + mmvar_size );
2478 (FT_Var_Axis*)( (char*)axis_flags + axis_flags_size );
2480 (FT_Var_Named_Style*)( (char*)mmvar->axis + axis_size );
2482 next_coords = (FT_Fixed*)( (char*)mmvar->namedstyle +
2484 for ( i = 0; i < num_instances; i++ )
2486 mmvar->namedstyle[i].coords = next_coords;
2487 next_coords += num_axes;
2490 next_name = (FT_String*)( (char*)mmvar->namedstyle +
2491 namedstyle_size + next_coords_size );
2492 for ( i = 0; i < num_axes; i++ )
2494 mmvar->axis[i].name = next_name;
2498 /* now fill in the data */
2500 if ( FT_STREAM_SEEK( fvar_start + fvar_head.offsetToData ) )
2504 for ( i = 0; i < num_axes; i++ )
2506 GX_FVar_Axis axis_rec;
2508 #ifdef FT_DEBUG_LEVEL_TRACE
2513 if ( FT_STREAM_READ_FIELDS( fvaraxis_fields, &axis_rec ) )
2515 a->tag = axis_rec.axisTag;
2516 a->minimum = axis_rec.minValue;
2517 a->def = axis_rec.defaultValue;
2518 a->maximum = axis_rec.maxValue;
2519 a->strid = axis_rec.nameID;
2521 a->name[0] = (FT_String)( a->tag >> 24 );
2522 a->name[1] = (FT_String)( ( a->tag >> 16 ) & 0xFF );
2523 a->name[2] = (FT_String)( ( a->tag >> 8 ) & 0xFF );
2524 a->name[3] = (FT_String)( ( a->tag ) & 0xFF );
2527 *axis_flags = axis_rec.flags;
2529 if ( a->minimum > a->def ||
2530 a->def > a->maximum )
2532 a->minimum = a->def;
2533 a->maximum = a->def;
2535 #ifdef FT_DEBUG_LEVEL_TRACE
2540 #ifdef FT_DEBUG_LEVEL_TRACE
2542 FT_TRACE5(( " idx tag "
2544 " minimum default maximum flags\n" ));
2545 /* " XXXX.XXXXX XXXX.XXXXX XXXX.XXXXX 0xXXXX" */
2547 FT_TRACE5(( " %3d `%s'"
2548 " %10.5f %10.5f %10.5f 0x%04X%s\n",
2551 (double)a->minimum / 65536,
2552 (double)a->def / 65536,
2553 (double)a->maximum / 65536,
2555 invalid ? " (invalid, disabled)" : "" ));
2562 FT_TRACE5(( "\n" ));
2564 /* named instance coordinates are stored as design coordinates; */
2565 /* we have to convert them to normalized coordinates also */
2566 if ( FT_NEW_ARRAY( ttface->blend->normalized_stylecoords,
2567 num_axes * num_instances ) )
2570 if ( fvar_head.instanceCount && !ttface->blend->avar_loaded )
2572 FT_ULong offset = FT_STREAM_POS();
2575 ft_var_load_avar( ttface );
2577 if ( FT_STREAM_SEEK( offset ) )
2581 FT_TRACE5(( "%d named instance%s\n",
2582 fvar_head.instanceCount,
2583 fvar_head.instanceCount == 1 ? "" : "s" ));
2585 ns = mmvar->namedstyle;
2586 nsc = ttface->blend->normalized_stylecoords;
2587 for ( i = 0; i < fvar_head.instanceCount; i++, ns++ )
2589 /* PostScript names add 2 bytes to the instance record size */
2590 if ( FT_FRAME_ENTER( ( usePsName ? 6L : 4L ) +
2594 ns->strid = FT_GET_USHORT();
2595 (void) /* flags = */ FT_GET_USHORT();
2598 for ( j = 0; j < num_axes; j++, c++ )
2601 /* valid psid values are 6, [256;32767], and 0xFFFF */
2603 ns->psid = FT_GET_USHORT();
2607 #ifdef FT_DEBUG_LEVEL_TRACE
2609 SFNT_Service sfnt = (SFNT_Service)ttface->sfnt;
2611 FT_String* strname = NULL;
2612 FT_String* psname = NULL;
2617 pos = FT_STREAM_POS();
2619 if ( ns->strid != 0xFFFF )
2621 (void)sfnt->get_name( ttface,
2622 (FT_UShort)ns->strid,
2624 if ( strname && !ft_strcmp( strname, ".notdef" ) )
2628 if ( ns->psid != 0xFFFF )
2630 (void)sfnt->get_name( ttface,
2631 (FT_UShort)ns->psid,
2633 if ( psname && !ft_strcmp( psname, ".notdef" ) )
2637 (void)FT_STREAM_SEEK( pos );
2639 FT_TRACE5(( " named instance %d (%s%s%s, %s%s%s)\n",
2641 strname ? "name: `" : "",
2642 strname ? strname : "unnamed",
2644 psname ? "PS name: `" : "",
2645 psname ? psname : "no PS name",
2646 psname ? "'" : "" ));
2651 #endif /* FT_DEBUG_LEVEL_TRACE */
2653 ft_var_to_normalized( ttface, num_axes, ns->coords, nsc );
2659 if ( num_instances != fvar_head.instanceCount )
2661 SFNT_Service sfnt = (SFNT_Service)ttface->sfnt;
2663 FT_Int found, dummy1, dummy2;
2664 FT_UInt strid = ~0U;
2667 /* The default instance is missing in array the */
2668 /* of named instances; try to synthesize an entry. */
2669 /* If this fails, `default_named_instance` remains */
2670 /* at value zero, which doesn't do any harm. */
2671 found = sfnt->get_name_id( ttface,
2672 TT_NAME_ID_TYPOGRAPHIC_SUBFAMILY,
2676 strid = TT_NAME_ID_TYPOGRAPHIC_SUBFAMILY;
2679 found = sfnt->get_name_id( ttface,
2680 TT_NAME_ID_FONT_SUBFAMILY,
2684 strid = TT_NAME_ID_FONT_SUBFAMILY;
2689 found = sfnt->get_name_id( ttface,
2695 FT_TRACE5(( "TT_Get_MM_Var:"
2696 " Adding default instance to named instances\n" ));
2698 /* named instance indices start with value 1 */
2699 ttface->var_default_named_instance = num_instances;
2701 ns = &mmvar->namedstyle[fvar_head.instanceCount];
2704 ns->psid = TT_NAME_ID_PS_NAME;
2708 for ( j = 0; j < num_axes; j++, a++, c++ )
2714 ft_var_load_mvar( ttface );
2717 /* fill the output array if requested */
2724 if ( FT_ALLOC( mmvar, ttface->blend->mmvar_len ) )
2726 FT_MEM_COPY( mmvar, ttface->blend->mmvar, ttface->blend->mmvar_len );
2729 (FT_UShort*)( (char*)mmvar + mmvar_size );
2731 (FT_Var_Axis*)( (char*)axis_flags + axis_flags_size );
2733 (FT_Var_Named_Style*)( (char*)mmvar->axis+ axis_size );
2735 next_coords = (FT_Fixed*)( (char*)mmvar->namedstyle +
2737 for ( n = 0; n < mmvar->num_namedstyles; n++ )
2739 mmvar->namedstyle[n].coords = next_coords;
2740 next_coords += num_axes;
2744 next_name = (FT_String*)( (char*)mmvar->namedstyle +
2745 namedstyle_size + next_coords_size );
2746 for ( n = 0; n < num_axes; n++ )
2748 a->name = next_name;
2750 /* standard PostScript names for some standard apple tags */
2751 if ( a->tag == TTAG_wght )
2752 a->name = (char*)"Weight";
2753 else if ( a->tag == TTAG_wdth )
2754 a->name = (char*)"Width";
2755 else if ( a->tag == TTAG_opsz )
2756 a->name = (char*)"OpticalSize";
2757 else if ( a->tag == TTAG_slnt )
2758 a->name = (char*)"Slant";
2759 else if ( a->tag == TTAG_ital )
2760 a->name = (char*)"Italic";
2775 tt_set_mm_blend( TT_Face face,
2778 FT_Bool set_design_coords )
2780 FT_Error error = FT_Err_Ok;
2785 FT_Bool all_design_coords = FALSE;
2787 FT_Memory memory = face->root.memory;
2798 face->doblend = FALSE;
2802 if ( FT_SET_ERROR( TT_Get_MM_Var( FT_FACE( face ), NULL ) ) )
2806 blend = face->blend;
2807 mmvar = blend->mmvar;
2809 if ( num_coords > mmvar->num_axis )
2811 FT_TRACE2(( "TT_Set_MM_Blend:"
2812 " only using first %d of %d coordinates\n",
2813 mmvar->num_axis, num_coords ));
2814 num_coords = mmvar->num_axis;
2817 FT_TRACE5(( "TT_Set_MM_Blend:\n" ));
2818 FT_TRACE5(( " normalized design coordinates:\n" ));
2820 for ( i = 0; i < num_coords; i++ )
2822 FT_TRACE5(( " %.5f\n", (double)coords[i] / 65536 ));
2823 if ( coords[i] < -0x00010000L || coords[i] > 0x00010000L )
2825 FT_TRACE1(( "TT_Set_MM_Blend: normalized design coordinate %.5f\n",
2826 (double)coords[i] / 65536 ));
2827 FT_TRACE1(( " is out of range [-1;1]\n" ));
2828 error = FT_THROW( Invalid_Argument );
2833 FT_TRACE5(( "\n" ));
2835 if ( !face->is_cff2 && !blend->glyphoffsets )
2837 /* While a missing 'gvar' table is acceptable, for example for */
2838 /* fonts that only vary metrics information or 'COLR' v1 */
2839 /* `PaintVar*` tables, an incorrect SFNT table offset or size */
2840 /* for 'gvar', or an inconsistent 'gvar' table is not. */
2841 error = ft_var_load_gvar( face );
2842 if ( error != FT_Err_Table_Missing && error != FT_Err_Ok )
2847 if ( !blend->coords )
2849 if ( FT_NEW_ARRAY( blend->coords, mmvar->num_axis ) )
2852 /* the first time we have to compute all design coordinates */
2853 all_design_coords = TRUE;
2856 if ( !blend->normalizedcoords )
2858 if ( FT_NEW_ARRAY( blend->normalizedcoords, mmvar->num_axis ) )
2861 manageCvt = mcvt_modify;
2863 /* If we have not set the blend coordinates before this, then the */
2864 /* cvt table will still be what we read from the `cvt ' table and */
2865 /* we don't need to reload it. We may need to change it though... */
2869 FT_Bool have_diff = 0;
2875 manageCvt = mcvt_retain;
2877 for ( i = 0; i < num_coords; i++ )
2879 if ( blend->normalizedcoords[i] != coords[i] )
2881 manageCvt = mcvt_load;
2889 if ( FT_IS_NAMED_INSTANCE( FT_FACE( face ) ) )
2891 FT_UInt instance_index = (FT_UInt)face->root.face_index >> 16;
2894 c = blend->normalizedcoords + i;
2895 n = blend->normalized_stylecoords +
2896 ( instance_index - 1 ) * mmvar->num_axis +
2899 for ( j = i; j < mmvar->num_axis; j++, n++, c++ )
2905 c = blend->normalizedcoords + i;
2906 for ( j = i; j < mmvar->num_axis; j++, c++ )
2912 /* return value -1 indicates `no change' */
2915 face->doblend = TRUE;
2920 for ( ; i < mmvar->num_axis; i++ )
2922 if ( blend->normalizedcoords[i] != 0 )
2924 manageCvt = mcvt_load;
2929 /* If we don't change the blend coords then we don't need to do */
2930 /* anything to the cvt table. It will be correct. Otherwise we */
2931 /* no longer have the original cvt (it was modified when we set */
2932 /* the blend last time), so we must reload and then modify it. */
2935 blend->num_axis = mmvar->num_axis;
2937 FT_MEM_COPY( blend->normalizedcoords,
2939 num_coords * sizeof ( FT_Fixed ) );
2941 if ( set_design_coords )
2942 ft_var_to_design( face,
2943 all_design_coords ? blend->num_axis : num_coords,
2944 blend->normalizedcoords,
2947 face->doblend = TRUE;
2951 switch ( manageCvt )
2954 /* The cvt table has been loaded already; every time we change the */
2955 /* blend we may need to reload and remodify the cvt table. */
2956 FT_FREE( face->cvt );
2958 error = tt_face_load_cvt( face, face->root.stream );
2962 /* The original cvt table is in memory. All we need to do is */
2963 /* apply the `cvar' table (if any). */
2964 error = tt_face_vary_cvt( face, face->root.stream );
2968 /* The cvt table is correct for this set of coordinates. */
2978 /**************************************************************************
2984 * Set the blend (normalized) coordinates for this instance of the
2985 * font. Check that the `gvar' table is reasonable and does some
2986 * initial preparation.
2991 * Initialize the blend structure with `gvar' data.
2995 * The number of available coordinates. If it is
2996 * larger than the number of axes, ignore the excess
2997 * values. If it is smaller than the number of axes,
2998 * use the default value (0) for the remaining axes.
3001 * An array of `num_coords', each between [-1,1].
3004 * FreeType error code. 0 means success, -1 means success and unchanged
3007 FT_LOCAL_DEF( FT_Error )
3008 TT_Set_MM_Blend( FT_Face face, /* TT_Face */
3012 return tt_set_mm_blend( (TT_Face)face, num_coords, coords, 1 );
3016 /**************************************************************************
3022 * Get the blend (normalized) coordinates for this instance of the
3028 * Initialize the blend structure with `gvar' data.
3032 * The number of available coordinates. If it is
3033 * larger than the number of axes, set the excess
3037 * An array of `num_coords', each between [-1,1].
3040 * FreeType error code. 0 means success, -1 means success and unchanged
3043 FT_LOCAL_DEF( FT_Error )
3044 TT_Get_MM_Blend( FT_Face face, /* TT_Face */
3048 TT_Face ttface = (TT_Face)face;
3050 FT_Error error = FT_Err_Ok;
3055 if ( !ttface->blend )
3057 if ( FT_SET_ERROR( TT_Get_MM_Var( face, NULL ) ) )
3061 blend = ttface->blend;
3063 if ( !blend->coords )
3065 /* select default instance coordinates */
3066 /* if no instance is selected yet */
3067 if ( FT_SET_ERROR( tt_set_mm_blend( ttface, 0, NULL, 1 ) ) )
3072 if ( num_coords > blend->num_axis )
3074 FT_TRACE2(( "TT_Get_MM_Blend:"
3075 " only using first %d of %d coordinates\n",
3076 blend->num_axis, num_coords ));
3077 nc = blend->num_axis;
3080 if ( ttface->doblend )
3082 for ( i = 0; i < nc; i++ )
3083 coords[i] = blend->normalizedcoords[i];
3087 for ( i = 0; i < nc; i++ )
3091 for ( ; i < num_coords; i++ )
3098 /**************************************************************************
3104 * Set the coordinates for the instance, measured in the user
3105 * coordinate system. Parse the `avar' table (if present) to convert
3106 * from user to normalized coordinates.
3111 * Initialize the blend struct with `gvar' data.
3115 * The number of available coordinates. If it is
3116 * larger than the number of axes, ignore the excess
3117 * values. If it is smaller than the number of axes,
3118 * use the default values for the remaining axes.
3121 * A coordinate array with `num_coords' elements.
3124 * FreeType error code. 0 means success.
3126 FT_LOCAL_DEF( FT_Error )
3127 TT_Set_Var_Design( FT_Face face, /* TT_Face */
3131 TT_Face ttface = (TT_Face)face;
3132 FT_Error error = FT_Err_Ok;
3136 FT_Memory memory = FT_FACE_MEMORY( face );
3140 FT_Fixed* normalized = NULL;
3142 FT_Bool have_diff = 0;
3145 if ( !ttface->blend )
3147 if ( FT_SET_ERROR( TT_Get_MM_Var( face, NULL ) ) )
3151 blend = ttface->blend;
3152 mmvar = blend->mmvar;
3154 if ( num_coords > mmvar->num_axis )
3156 FT_TRACE2(( "TT_Set_Var_Design:"
3157 " only using first %d of %d coordinates\n",
3158 mmvar->num_axis, num_coords ));
3159 num_coords = mmvar->num_axis;
3162 if ( !blend->coords )
3164 if ( FT_NEW_ARRAY( blend->coords, mmvar->num_axis ) )
3170 for ( i = 0; i < num_coords; i++, n++, c++ )
3179 if ( FT_IS_NAMED_INSTANCE( face ) )
3181 FT_UInt instance_index;
3182 FT_Var_Named_Style* named_style;
3185 instance_index = (FT_UInt)face->face_index >> 16;
3186 named_style = mmvar->namedstyle + instance_index - 1;
3188 n = named_style->coords + num_coords;
3189 for ( ; i < mmvar->num_axis; i++, n++, c++ )
3203 a = mmvar->axis + num_coords;
3204 for ( ; i < mmvar->num_axis; i++, a++, c++ )
3214 /* return value -1 indicates `no change'; */
3215 /* we can exit early if `normalizedcoords' is already computed */
3216 if ( blend->normalizedcoords && !have_diff )
3219 if ( FT_NEW_ARRAY( normalized, mmvar->num_axis ) )
3222 if ( !ttface->blend->avar_loaded )
3223 ft_var_load_avar( ttface );
3225 FT_TRACE5(( "TT_Set_Var_Design:\n" ));
3226 FT_TRACE5(( " normalized design coordinates:\n" ));
3227 ft_var_to_normalized( ttface, num_coords, blend->coords, normalized );
3229 error = tt_set_mm_blend( ttface, mmvar->num_axis, normalized, 0 );
3234 FT_FREE( normalized );
3239 /**************************************************************************
3245 * Get the design coordinates of the currently selected interpolated
3250 * A handle to the source face.
3253 * The number of design coordinates to retrieve. If it
3254 * is larger than the number of axes, set the excess
3259 * The design coordinates array.
3262 * FreeType error code. 0~means success.
3264 FT_LOCAL_DEF( FT_Error )
3265 TT_Get_Var_Design( FT_Face face, /* TT_Face */
3269 TT_Face ttface = (TT_Face)face;
3270 FT_Error error = FT_Err_Ok;
3275 if ( !ttface->blend )
3277 if ( FT_SET_ERROR( TT_Get_MM_Var( face, NULL ) ) )
3281 blend = ttface->blend;
3283 if ( !blend->coords )
3285 /* select default instance coordinates */
3286 /* if no instance is selected yet */
3287 if ( FT_SET_ERROR( tt_set_mm_blend( ttface, 0, NULL, 1 ) ) )
3292 if ( num_coords > blend->num_axis )
3294 FT_TRACE2(( "TT_Get_Var_Design:"
3295 " only using first %d of %d coordinates\n",
3296 blend->num_axis, num_coords ));
3297 nc = blend->num_axis;
3300 if ( ttface->doblend )
3302 for ( i = 0; i < nc; i++ )
3303 coords[i] = blend->coords[i];
3307 for ( i = 0; i < nc; i++ )
3311 for ( ; i < num_coords; i++ )
3318 /**************************************************************************
3321 * TT_Set_Named_Instance
3324 * Set the given named instance, also resetting any further
3329 * A handle to the source face.
3332 * The instance index, starting with value 1.
3333 * Value 0 indicates to not use an instance.
3336 * FreeType error code. 0~means success, -1 means success and unchanged
3339 FT_LOCAL_DEF( FT_Error )
3340 TT_Set_Named_Instance( FT_Face face, /* TT_Face */
3341 FT_UInt instance_index )
3343 TT_Face ttface = (TT_Face)face;
3348 FT_Memory memory = FT_FACE_MEMORY( face );
3350 FT_UInt num_instances;
3353 if ( !ttface->blend )
3355 if ( FT_SET_ERROR( TT_Get_MM_Var( face, NULL ) ) )
3359 blend = ttface->blend;
3360 mmvar = blend->mmvar;
3362 num_instances = (FT_UInt)face->style_flags >> 16;
3364 /* `instance_index' starts with value 1, thus `>' */
3365 if ( instance_index > num_instances )
3367 error = FT_ERR( Invalid_Argument );
3371 if ( instance_index > 0 )
3373 SFNT_Service sfnt = (SFNT_Service)ttface->sfnt;
3375 FT_Var_Named_Style* named_style;
3376 FT_String* style_name;
3379 named_style = mmvar->namedstyle + instance_index - 1;
3381 error = sfnt->get_name( ttface,
3382 (FT_UShort)named_style->strid,
3387 /* set (or replace) style name */
3388 FT_FREE( face->style_name );
3389 face->style_name = style_name;
3391 /* finally, select the named instance */
3392 error = TT_Set_Var_Design( face,
3394 named_style->coords );
3398 /* restore non-VF style name */
3399 FT_FREE( face->style_name );
3400 if ( FT_STRDUP( face->style_name, ttface->non_var_style_name ) )
3402 error = TT_Set_Var_Design( face, 0, NULL );
3410 /**************************************************************************
3413 * TT_Get_Default_Named_Instance
3416 * Get the default named instance.
3420 * A handle to the source face.
3424 * The default named instance index.
3427 * FreeType error code. 0~means success.
3429 FT_LOCAL_DEF( FT_Error )
3430 TT_Get_Default_Named_Instance( FT_Face face,
3431 FT_UInt *instance_index )
3433 TT_Face ttface = (TT_Face)face;
3434 FT_Error error = FT_Err_Ok;
3437 if ( !ttface->blend )
3439 if ( FT_SET_ERROR( TT_Get_MM_Var( face, NULL ) ) )
3443 *instance_index = ttface->var_default_named_instance;
3450 /* This function triggers (lazy) recomputation of the `postscript_name` */
3451 /* field in `TT_Face`. */
3453 FT_LOCAL_DEF( void )
3454 tt_construct_ps_name( FT_Face face )
3456 TT_Face ttface = (TT_Face)face;
3457 FT_Memory memory = FT_FACE_MEMORY( face );
3460 FT_FREE( ttface->postscript_name );
3464 /*************************************************************************/
3465 /*************************************************************************/
3467 /***** GX VAR PARSING ROUTINES *****/
3469 /*************************************************************************/
3470 /*************************************************************************/
3473 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
3476 tt_cvt_ready_iterator( FT_ListNode node,
3479 TT_Size size = (TT_Size)node->data;
3484 size->cvt_ready = -1;
3489 #endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
3493 /**************************************************************************
3499 * Modify the loaded cvt table according to the `cvar' table and the
3504 * A handle to the target face object.
3508 * A handle to the input stream.
3511 * FreeType error code. 0 means success.
3513 * Most errors are ignored. It is perfectly valid not to have a
3514 * `cvar' table even if there is a `gvar' and `fvar' table.
3516 FT_LOCAL_DEF( FT_Error )
3517 tt_face_vary_cvt( TT_Face face,
3520 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
3523 FT_Memory memory = stream->memory;
3525 FT_Face root = &face->root;
3527 FT_ULong table_start;
3531 FT_ULong offsetToData;
3536 FT_Fixed* tuple_coords = NULL;
3537 FT_Fixed* im_start_coords = NULL;
3538 FT_Fixed* im_end_coords = NULL;
3540 GX_Blend blend = face->blend;
3542 FT_UInt point_count;
3543 FT_UInt spoint_count = 0;
3545 FT_UShort* sharedpoints = NULL;
3546 FT_UShort* localpoints = NULL;
3549 FT_Fixed* deltas = NULL;
3550 FT_Fixed* cvt_deltas = NULL;
3553 FT_TRACE2(( "CVAR " ));
3557 FT_TRACE2(( "\n" ));
3558 FT_TRACE2(( "tt_face_vary_cvt: no blend specified\n" ));
3565 FT_TRACE2(( "\n" ));
3566 FT_TRACE2(( "tt_face_vary_cvt: no `cvt ' table\n" ));
3571 error = face->goto_table( face, TTAG_cvar, stream, &table_len );
3574 FT_TRACE2(( "is missing\n" ));
3580 if ( FT_FRAME_ENTER( table_len ) )
3586 table_start = FT_Stream_FTell( stream );
3587 if ( FT_GET_LONG() != 0x00010000L )
3589 FT_TRACE2(( "bad table version\n" ));
3595 FT_TRACE2(( "loaded\n" ));
3597 if ( FT_NEW_ARRAY( tuple_coords, blend->num_axis ) ||
3598 FT_NEW_ARRAY( im_start_coords, blend->num_axis ) ||
3599 FT_NEW_ARRAY( im_end_coords, blend->num_axis ) )
3602 tupleCount = FT_GET_USHORT();
3603 offsetToData = FT_GET_USHORT();
3605 /* rough sanity test */
3606 if ( offsetToData + ( tupleCount & GX_TC_TUPLE_COUNT_MASK ) * 4 >
3609 FT_TRACE2(( "tt_face_vary_cvt:"
3610 " invalid CVT variation array header\n" ));
3612 error = FT_THROW( Invalid_Table );
3616 offsetToData += table_start;
3618 if ( tupleCount & GX_TC_TUPLES_SHARE_POINT_NUMBERS )
3620 here = FT_Stream_FTell( stream );
3622 FT_Stream_SeekSet( stream, offsetToData );
3624 sharedpoints = ft_var_readpackedpoints( stream,
3627 offsetToData = FT_Stream_FTell( stream );
3629 FT_Stream_SeekSet( stream, here );
3632 FT_TRACE5(( "cvar: there %s %d tuple%s:\n",
3633 ( tupleCount & GX_TC_TUPLE_COUNT_MASK ) == 1 ? "is" : "are",
3634 tupleCount & GX_TC_TUPLE_COUNT_MASK,
3635 ( tupleCount & GX_TC_TUPLE_COUNT_MASK ) == 1 ? "" : "s" ));
3637 if ( FT_NEW_ARRAY( cvt_deltas, face->cvt_size ) )
3640 for ( i = 0; i < ( tupleCount & GX_TC_TUPLE_COUNT_MASK ); i++ )
3642 FT_UInt tupleDataSize;
3647 FT_TRACE6(( " tuple %d:\n", i ));
3649 tupleDataSize = FT_GET_USHORT();
3650 tupleIndex = FT_GET_USHORT();
3652 if ( tupleIndex & GX_TI_EMBEDDED_TUPLE_COORD )
3654 for ( j = 0; j < blend->num_axis; j++ )
3655 tuple_coords[j] = FT_fdot14ToFixed( FT_GET_SHORT() );
3657 else if ( ( tupleIndex & GX_TI_TUPLE_INDEX_MASK ) >= blend->tuplecount )
3659 FT_TRACE2(( "tt_face_vary_cvt:"
3660 " invalid tuple index\n" ));
3662 error = FT_THROW( Invalid_Table );
3667 if ( !blend->tuplecoords )
3669 FT_TRACE2(( "tt_face_vary_cvt:"
3670 " no valid tuple coordinates available\n" ));
3672 error = FT_THROW( Invalid_Table );
3678 blend->tuplecoords +
3679 ( tupleIndex & GX_TI_TUPLE_INDEX_MASK ) * blend->num_axis,
3680 blend->num_axis * sizeof ( FT_Fixed ) );
3683 if ( tupleIndex & GX_TI_INTERMEDIATE_TUPLE )
3685 for ( j = 0; j < blend->num_axis; j++ )
3686 im_start_coords[j] = FT_fdot14ToFixed( FT_GET_SHORT() );
3687 for ( j = 0; j < blend->num_axis; j++ )
3688 im_end_coords[j] = FT_fdot14ToFixed( FT_GET_SHORT() );
3691 apply = ft_var_apply_tuple( blend,
3692 (FT_UShort)tupleIndex,
3697 if ( apply == 0 ) /* tuple isn't active for our blend */
3699 offsetToData += tupleDataSize;
3703 here = FT_Stream_FTell( stream );
3705 FT_Stream_SeekSet( stream, offsetToData );
3707 if ( tupleIndex & GX_TI_PRIVATE_POINT_NUMBERS )
3709 localpoints = ft_var_readpackedpoints( stream,
3712 points = localpoints;
3717 points = sharedpoints;
3718 point_count = spoint_count;
3721 deltas = ft_var_readpackeddeltas( stream,
3723 point_count == 0 ? face->cvt_size
3726 if ( !points || !deltas )
3727 ; /* failure, ignore it */
3729 else if ( localpoints == ALL_POINTS )
3731 #ifdef FT_DEBUG_LEVEL_TRACE
3736 FT_TRACE7(( " CVT deltas:\n" ));
3738 /* this means that there are deltas for every entry in cvt */
3739 for ( j = 0; j < face->cvt_size; j++ )
3741 FT_Fixed old_cvt_delta;
3744 old_cvt_delta = cvt_deltas[j];
3745 cvt_deltas[j] = old_cvt_delta + FT_MulFix( deltas[j], apply );
3747 #ifdef FT_DEBUG_LEVEL_TRACE
3748 if ( old_cvt_delta != cvt_deltas[j] )
3750 FT_TRACE7(( " %d: %f -> %f\n",
3752 (double)( FT_fdot6ToFixed( face->cvt[j] ) +
3753 old_cvt_delta ) / 65536,
3754 (double)( FT_fdot6ToFixed( face->cvt[j] ) +
3755 cvt_deltas[j] ) / 65536 ));
3761 #ifdef FT_DEBUG_LEVEL_TRACE
3763 FT_TRACE7(( " none\n" ));
3769 #ifdef FT_DEBUG_LEVEL_TRACE
3774 FT_TRACE7(( " CVT deltas:\n" ));
3776 for ( j = 0; j < point_count; j++ )
3779 FT_Fixed old_cvt_delta;
3783 if ( (FT_ULong)pindex >= face->cvt_size )
3786 old_cvt_delta = cvt_deltas[pindex];
3787 cvt_deltas[pindex] = old_cvt_delta + FT_MulFix( deltas[j], apply );
3789 #ifdef FT_DEBUG_LEVEL_TRACE
3790 if ( old_cvt_delta != cvt_deltas[pindex] )
3792 FT_TRACE7(( " %d: %f -> %f\n",
3794 (double)( FT_fdot6ToFixed( face->cvt[pindex] ) +
3795 old_cvt_delta ) / 65536,
3796 (double)( FT_fdot6ToFixed( face->cvt[pindex] ) +
3797 cvt_deltas[pindex] ) / 65536 ));
3803 #ifdef FT_DEBUG_LEVEL_TRACE
3805 FT_TRACE7(( " none\n" ));
3809 if ( localpoints != ALL_POINTS )
3810 FT_FREE( localpoints );
3813 offsetToData += tupleDataSize;
3815 FT_Stream_SeekSet( stream, here );
3818 FT_TRACE5(( "\n" ));
3820 for ( i = 0; i < face->cvt_size; i++ )
3821 face->cvt[i] += FT_fixedToFdot6( cvt_deltas[i] );
3827 if ( sharedpoints != ALL_POINTS )
3828 FT_FREE( sharedpoints );
3829 FT_FREE( tuple_coords );
3830 FT_FREE( im_start_coords );
3831 FT_FREE( im_end_coords );
3832 FT_FREE( cvt_deltas );
3834 /* iterate over all FT_Size objects and set `cvt_ready' to -1 */
3835 /* to trigger rescaling of all CVT values */
3836 FT_List_Iterate( &root->sizes_list,
3837 tt_cvt_ready_iterator,
3842 #else /* !TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
3845 FT_UNUSED( stream );
3849 #endif /* !TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
3854 /* Shift the original coordinates of all points between indices `p1' */
3855 /* and `p2', using the same difference as given by index `ref'. */
3857 /* modeled after `af_iup_shift' */
3860 tt_delta_shift( int p1,
3863 FT_Vector* in_points,
3864 FT_Vector* out_points )
3870 delta.x = out_points[ref].x - in_points[ref].x;
3871 delta.y = out_points[ref].y - in_points[ref].y;
3873 if ( delta.x == 0 && delta.y == 0 )
3876 for ( p = p1; p < ref; p++ )
3878 out_points[p].x += delta.x;
3879 out_points[p].y += delta.y;
3882 for ( p = ref + 1; p <= p2; p++ )
3884 out_points[p].x += delta.x;
3885 out_points[p].y += delta.y;
3890 /* Interpolate the original coordinates of all points with indices */
3891 /* between `p1' and `p2', using `ref1' and `ref2' as the reference */
3892 /* point indices. */
3894 /* modeled after `af_iup_interp', `_iup_worker_interpolate', and */
3895 /* `Ins_IUP' with spec differences in handling ill-defined cases. */
3897 tt_delta_interpolate( int p1,
3901 FT_Vector* in_points,
3902 FT_Vector* out_points )
3906 FT_Pos out, in1, in2, out1, out2, d1, d2;
3912 /* handle both horizontal and vertical coordinates */
3913 for ( i = 0; i <= 1; i++ )
3915 /* shift array pointers so that we can access `foo.y' as `foo.x' */
3916 in_points = (FT_Vector*)( (FT_Pos*)in_points + i );
3917 out_points = (FT_Vector*)( (FT_Pos*)out_points + i );
3919 if ( in_points[ref1].x > in_points[ref2].x )
3926 in1 = in_points[ref1].x;
3927 in2 = in_points[ref2].x;
3928 out1 = out_points[ref1].x;
3929 out2 = out_points[ref2].x;
3933 /* If the reference points have the same coordinate but different */
3934 /* delta, inferred delta is zero. Otherwise interpolate. */
3935 if ( in1 != in2 || out1 == out2 )
3937 FT_Fixed scale = in1 != in2 ? FT_DivFix( out2 - out1, in2 - in1 )
3941 for ( p = p1; p <= p2; p++ )
3943 out = in_points[p].x;
3947 else if ( out >= in2 )
3950 out = out1 + FT_MulFix( out - in1, scale );
3952 out_points[p].x = out;
3959 /* Interpolate points without delta values, similar to */
3960 /* the `IUP' hinting instruction. */
3962 /* modeled after `Ins_IUP */
3965 tt_interpolate_deltas( FT_Outline* outline,
3966 FT_Vector* out_points,
3967 FT_Vector* in_points,
3968 FT_Bool* has_delta )
3980 /* ignore empty outlines */
3981 if ( !outline->n_contours )
3989 end_point = outline->contours[contour];
3990 first_point = point;
3992 /* search first point that has a delta */
3993 while ( point <= end_point && !has_delta[point] )
3996 if ( point <= end_point )
3998 first_delta = point;
4003 while ( point <= end_point )
4005 /* search next point that has a delta */
4006 /* and interpolate intermediate points */
4007 if ( has_delta[point] )
4009 tt_delta_interpolate( cur_delta + 1,
4021 /* shift contour if we only have a single delta */
4022 if ( cur_delta == first_delta )
4023 tt_delta_shift( first_point,
4030 /* otherwise handle remaining points */
4031 /* at the end and beginning of the contour */
4032 tt_delta_interpolate( cur_delta + 1,
4039 if ( first_delta > 0 )
4040 tt_delta_interpolate( first_point,
4050 } while ( contour < outline->n_contours );
4054 /**************************************************************************
4057 * TT_Vary_Apply_Glyph_Deltas
4060 * Apply the appropriate deltas to the current glyph.
4064 * A handle to the loader object.
4067 * The outline to change, with appended phantom points.
4071 * An array with `n_points' elements that is filled with unrounded
4072 * point coordinates (in 26.6 format).
4075 * FreeType error code. 0 means success.
4077 FT_LOCAL_DEF( FT_Error )
4078 TT_Vary_Apply_Glyph_Deltas( TT_Loader loader,
4079 FT_Outline* outline,
4080 FT_Vector* unrounded )
4083 TT_Face face = loader->face;
4084 FT_Stream stream = face->root.stream;
4085 FT_Memory memory = stream->memory;
4086 FT_UInt glyph_index = loader->glyph_index;
4087 FT_UInt n_points = (FT_UInt)outline->n_points + 4;
4089 FT_Vector* points_org = NULL; /* coordinates in 16.16 format */
4090 FT_Vector* points_out = NULL; /* coordinates in 16.16 format */
4091 FT_Bool* has_delta = NULL;
4093 FT_ULong glyph_start;
4096 FT_ULong offsetToData;
4102 FT_Fixed* tuple_coords = NULL;
4103 FT_Fixed* im_start_coords = NULL;
4104 FT_Fixed* im_end_coords = NULL;
4106 GX_Blend blend = face->blend;
4108 FT_UInt point_count;
4109 FT_UInt spoint_count = 0;
4111 FT_UShort* sharedpoints = NULL;
4112 FT_UShort* localpoints = NULL;
4115 FT_Fixed* deltas_x = NULL;
4116 FT_Fixed* deltas_y = NULL;
4117 FT_Fixed* point_deltas_x = NULL;
4118 FT_Fixed* point_deltas_y = NULL;
4121 if ( !face->doblend || !blend )
4122 return FT_THROW( Invalid_Argument );
4124 for ( i = 0; i < n_points; i++ )
4126 unrounded[i].x = INT_TO_F26DOT6( outline->points[i].x );
4127 unrounded[i].y = INT_TO_F26DOT6( outline->points[i].y );
4130 if ( glyph_index >= blend->gv_glyphcnt ||
4131 blend->glyphoffsets[glyph_index] ==
4132 blend->glyphoffsets[glyph_index + 1] )
4134 FT_TRACE2(( "TT_Vary_Apply_Glyph_Deltas:"
4135 " no variation data for glyph %d\n", glyph_index ));
4139 if ( FT_NEW_ARRAY( points_org, n_points ) ||
4140 FT_NEW_ARRAY( points_out, n_points ) ||
4141 FT_NEW_ARRAY( has_delta, n_points ) )
4144 dataSize = blend->glyphoffsets[glyph_index + 1] -
4145 blend->glyphoffsets[glyph_index];
4147 if ( FT_STREAM_SEEK( blend->glyphoffsets[glyph_index] ) ||
4148 FT_FRAME_ENTER( dataSize ) )
4151 glyph_start = FT_Stream_FTell( stream );
4153 /* each set of glyph variation data is formatted similarly to `cvar' */
4155 if ( FT_NEW_ARRAY( tuple_coords, blend->num_axis ) ||
4156 FT_NEW_ARRAY( im_start_coords, blend->num_axis ) ||
4157 FT_NEW_ARRAY( im_end_coords, blend->num_axis ) )
4160 tupleCount = FT_GET_USHORT();
4161 offsetToData = FT_GET_USHORT();
4163 /* rough sanity test */
4164 if ( offsetToData > dataSize ||
4165 ( tupleCount & GX_TC_TUPLE_COUNT_MASK ) * 4 > dataSize )
4167 FT_TRACE2(( "TT_Vary_Apply_Glyph_Deltas:"
4168 " invalid glyph variation array header\n" ));
4170 error = FT_THROW( Invalid_Table );
4174 offsetToData += glyph_start;
4176 if ( tupleCount & GX_TC_TUPLES_SHARE_POINT_NUMBERS )
4178 here = FT_Stream_FTell( stream );
4180 FT_Stream_SeekSet( stream, offsetToData );
4182 sharedpoints = ft_var_readpackedpoints( stream,
4185 offsetToData = FT_Stream_FTell( stream );
4187 FT_Stream_SeekSet( stream, here );
4190 FT_TRACE5(( "gvar: there %s %d tuple%s:\n",
4191 ( tupleCount & GX_TC_TUPLE_COUNT_MASK ) == 1 ? "is" : "are",
4192 tupleCount & GX_TC_TUPLE_COUNT_MASK,
4193 ( tupleCount & GX_TC_TUPLE_COUNT_MASK ) == 1 ? "" : "s" ));
4195 if ( FT_NEW_ARRAY( point_deltas_x, n_points ) ||
4196 FT_NEW_ARRAY( point_deltas_y, n_points ) )
4199 for ( j = 0; j < n_points; j++ )
4201 points_org[j].x = FT_intToFixed( outline->points[j].x );
4202 points_org[j].y = FT_intToFixed( outline->points[j].y );
4205 for ( i = 0; i < ( tupleCount & GX_TC_TUPLE_COUNT_MASK ); i++ )
4207 FT_UInt tupleDataSize;
4212 FT_TRACE6(( " tuple %d:\n", i ));
4214 tupleDataSize = FT_GET_USHORT();
4215 tupleIndex = FT_GET_USHORT();
4217 if ( tupleIndex & GX_TI_EMBEDDED_TUPLE_COORD )
4219 for ( j = 0; j < blend->num_axis; j++ )
4220 tuple_coords[j] = FT_fdot14ToFixed( FT_GET_SHORT() );
4222 else if ( ( tupleIndex & GX_TI_TUPLE_INDEX_MASK ) >= blend->tuplecount )
4224 FT_TRACE2(( "TT_Vary_Apply_Glyph_Deltas:"
4225 " invalid tuple index\n" ));
4227 error = FT_THROW( Invalid_Table );
4233 blend->tuplecoords +
4234 ( tupleIndex & GX_TI_TUPLE_INDEX_MASK ) * blend->num_axis,
4235 blend->num_axis * sizeof ( FT_Fixed ) );
4237 if ( tupleIndex & GX_TI_INTERMEDIATE_TUPLE )
4239 for ( j = 0; j < blend->num_axis; j++ )
4240 im_start_coords[j] = FT_fdot14ToFixed( FT_GET_SHORT() );
4241 for ( j = 0; j < blend->num_axis; j++ )
4242 im_end_coords[j] = FT_fdot14ToFixed( FT_GET_SHORT() );
4245 apply = ft_var_apply_tuple( blend,
4246 (FT_UShort)tupleIndex,
4251 if ( apply == 0 ) /* tuple isn't active for our blend */
4253 offsetToData += tupleDataSize;
4257 here = FT_Stream_FTell( stream );
4259 FT_Stream_SeekSet( stream, offsetToData );
4261 if ( tupleIndex & GX_TI_PRIVATE_POINT_NUMBERS )
4263 localpoints = ft_var_readpackedpoints( stream,
4266 points = localpoints;
4270 points = sharedpoints;
4271 point_count = spoint_count;
4274 deltas_x = ft_var_readpackeddeltas( stream,
4276 point_count == 0 ? n_points
4278 deltas_y = ft_var_readpackeddeltas( stream,
4280 point_count == 0 ? n_points
4283 if ( !points || !deltas_y || !deltas_x )
4284 ; /* failure, ignore it */
4286 else if ( points == ALL_POINTS )
4288 #ifdef FT_DEBUG_LEVEL_TRACE
4293 FT_TRACE7(( " point deltas:\n" ));
4295 /* this means that there are deltas for every point in the glyph */
4296 for ( j = 0; j < n_points; j++ )
4298 FT_Fixed old_point_delta_x = point_deltas_x[j];
4299 FT_Fixed old_point_delta_y = point_deltas_y[j];
4301 FT_Fixed point_delta_x = FT_MulFix( deltas_x[j], apply );
4302 FT_Fixed point_delta_y = FT_MulFix( deltas_y[j], apply );
4305 point_deltas_x[j] = old_point_delta_x + point_delta_x;
4306 point_deltas_y[j] = old_point_delta_y + point_delta_y;
4308 #ifdef FT_DEBUG_LEVEL_TRACE
4309 if ( point_delta_x || point_delta_y )
4311 FT_TRACE7(( " %d: (%f, %f) -> (%f, %f)\n",
4313 (double)( FT_intToFixed( outline->points[j].x ) +
4314 old_point_delta_x ) / 65536,
4315 (double)( FT_intToFixed( outline->points[j].y ) +
4316 old_point_delta_y ) / 65536,
4317 (double)( FT_intToFixed( outline->points[j].x ) +
4318 point_deltas_x[j] ) / 65536,
4319 (double)( FT_intToFixed( outline->points[j].y ) +
4320 point_deltas_y[j] ) / 65536 ));
4326 #ifdef FT_DEBUG_LEVEL_TRACE
4328 FT_TRACE7(( " none\n" ));
4334 #ifdef FT_DEBUG_LEVEL_TRACE
4339 /* we have to interpolate the missing deltas similar to the */
4340 /* IUP bytecode instruction */
4341 for ( j = 0; j < n_points; j++ )
4343 has_delta[j] = FALSE;
4344 points_out[j] = points_org[j];
4347 for ( j = 0; j < point_count; j++ )
4349 FT_UShort idx = points[j];
4352 if ( idx >= n_points )
4355 has_delta[idx] = TRUE;
4357 points_out[idx].x += FT_MulFix( deltas_x[j], apply );
4358 points_out[idx].y += FT_MulFix( deltas_y[j], apply );
4361 /* no need to handle phantom points here, */
4362 /* since solitary points can't be interpolated */
4363 tt_interpolate_deltas( outline,
4368 FT_TRACE7(( " point deltas:\n" ));
4370 for ( j = 0; j < n_points; j++ )
4372 FT_Fixed old_point_delta_x = point_deltas_x[j];
4373 FT_Fixed old_point_delta_y = point_deltas_y[j];
4375 FT_Pos point_delta_x = points_out[j].x - points_org[j].x;
4376 FT_Pos point_delta_y = points_out[j].y - points_org[j].y;
4379 point_deltas_x[j] = old_point_delta_x + point_delta_x;
4380 point_deltas_y[j] = old_point_delta_y + point_delta_y;
4382 #ifdef FT_DEBUG_LEVEL_TRACE
4383 if ( point_delta_x || point_delta_y )
4385 FT_TRACE7(( " %d: (%f, %f) -> (%f, %f)\n",
4387 (double)( FT_intToFixed( outline->points[j].x ) +
4388 old_point_delta_x ) / 65536,
4389 (double)( FT_intToFixed( outline->points[j].y ) +
4390 old_point_delta_y ) / 65536,
4391 (double)( FT_intToFixed( outline->points[j].x ) +
4392 point_deltas_x[j] ) / 65536,
4393 (double)( FT_intToFixed( outline->points[j].y ) +
4394 point_deltas_y[j] ) / 65536 ));
4400 #ifdef FT_DEBUG_LEVEL_TRACE
4402 FT_TRACE7(( " none\n" ));
4406 if ( localpoints != ALL_POINTS )
4407 FT_FREE( localpoints );
4408 FT_FREE( deltas_x );
4409 FT_FREE( deltas_y );
4411 offsetToData += tupleDataSize;
4413 FT_Stream_SeekSet( stream, here );
4416 FT_TRACE5(( "\n" ));
4418 /* To avoid double adjustment of advance width or height, */
4419 /* do not move phantom points if there is HVAR or VVAR */
4420 /* support, respectively. */
4421 if ( face->variation_support & TT_FACE_FLAG_VAR_HADVANCE )
4423 point_deltas_x[n_points - 4] = 0;
4424 point_deltas_y[n_points - 4] = 0;
4425 point_deltas_x[n_points - 3] = 0;
4426 point_deltas_y[n_points - 3] = 0;
4428 if ( face->variation_support & TT_FACE_FLAG_VAR_VADVANCE )
4430 point_deltas_x[n_points - 2] = 0;
4431 point_deltas_y[n_points - 2] = 0;
4432 point_deltas_x[n_points - 1] = 0;
4433 point_deltas_y[n_points - 1] = 0;
4436 for ( i = 0; i < n_points; i++ )
4438 unrounded[i].x += FT_fixedToFdot6( point_deltas_x[i] );
4439 unrounded[i].y += FT_fixedToFdot6( point_deltas_y[i] );
4441 outline->points[i].x += FT_fixedToInt( point_deltas_x[i] );
4442 outline->points[i].y += FT_fixedToInt( point_deltas_y[i] );
4445 /* To avoid double adjustment of advance width or height, */
4446 /* adjust phantom points only if there is no HVAR or VVAR */
4447 /* support, respectively. */
4448 if ( !( face->variation_support & TT_FACE_FLAG_VAR_HADVANCE ) )
4450 loader->pp1 = outline->points[n_points - 4];
4451 loader->pp2 = outline->points[n_points - 3];
4452 loader->linear = FT_PIX_ROUND( unrounded[n_points - 3].x -
4453 unrounded[n_points - 4].x ) / 64;
4455 if ( !( face->variation_support & TT_FACE_FLAG_VAR_VADVANCE ) )
4457 loader->pp3 = outline->points[n_points - 2];
4458 loader->pp4 = outline->points[n_points - 1];
4459 loader->vadvance = FT_PIX_ROUND( unrounded[n_points - 1].y -
4460 unrounded[n_points - 2].y ) / 64;
4464 FT_FREE( point_deltas_x );
4465 FT_FREE( point_deltas_y );
4468 if ( sharedpoints != ALL_POINTS )
4469 FT_FREE( sharedpoints );
4470 FT_FREE( tuple_coords );
4471 FT_FREE( im_start_coords );
4472 FT_FREE( im_end_coords );
4477 FT_FREE( points_org );
4478 FT_FREE( points_out );
4479 FT_FREE( has_delta );
4485 /**************************************************************************
4491 * An extended internal version of `TT_Get_MM_Blend' that returns
4492 * pointers instead of copying data, without any initialization of
4493 * the MM machinery in case it isn't loaded yet.
4495 FT_LOCAL_DEF( FT_Error )
4496 tt_get_var_blend( FT_Face face, /* TT_Face */
4497 FT_UInt *num_coords,
4499 FT_Fixed* *normalizedcoords,
4500 FT_MM_Var* *mm_var )
4502 TT_Face ttface = (TT_Face)face;
4505 if ( ttface->blend )
4508 *num_coords = ttface->blend->num_axis;
4510 *coords = ttface->blend->coords;
4511 if ( normalizedcoords )
4512 *normalizedcoords = ttface->blend->normalizedcoords;
4514 *mm_var = ttface->blend->mmvar;
4530 FT_LOCAL_DEF( void )
4531 tt_var_done_item_variation_store( FT_Face face,
4532 GX_ItemVarStore itemStore )
4534 FT_Memory memory = FT_FACE_MEMORY( face );
4538 if ( itemStore->varData )
4540 for ( i = 0; i < itemStore->dataCount; i++ )
4542 FT_FREE( itemStore->varData[i].regionIndices );
4543 FT_FREE( itemStore->varData[i].deltaSet );
4546 FT_FREE( itemStore->varData );
4549 if ( itemStore->varRegionList )
4551 for ( i = 0; i < itemStore->regionCount; i++ )
4552 FT_FREE( itemStore->varRegionList[i].axisList );
4554 FT_FREE( itemStore->varRegionList );
4559 FT_LOCAL_DEF( void )
4560 tt_var_done_delta_set_index_map( FT_Face face,
4561 GX_DeltaSetIdxMap deltaSetIdxMap )
4563 FT_Memory memory = FT_FACE_MEMORY( face );
4566 FT_FREE( deltaSetIdxMap->innerIndex );
4567 FT_FREE( deltaSetIdxMap->outerIndex );
4571 /**************************************************************************
4577 * Free the blend internal data structure.
4579 FT_LOCAL_DEF( void )
4580 tt_done_blend( FT_Face face )
4582 TT_Face ttface = (TT_Face)face;
4583 FT_Memory memory = FT_FACE_MEMORY( face );
4584 GX_Blend blend = ttface->blend;
4589 FT_UInt i, num_axes;
4592 /* blend->num_axis might not be set up yet */
4593 num_axes = blend->mmvar->num_axis;
4595 FT_FREE( blend->coords );
4596 FT_FREE( blend->normalizedcoords );
4597 FT_FREE( blend->normalized_stylecoords );
4598 FT_FREE( blend->mmvar );
4600 if ( blend->avar_table )
4602 if ( blend->avar_table->avar_segment )
4604 for ( i = 0; i < num_axes; i++ )
4605 FT_FREE( blend->avar_table->avar_segment[i].correspondence );
4606 FT_FREE( blend->avar_table->avar_segment );
4609 tt_var_done_item_variation_store( face,
4610 &blend->avar_table->itemStore );
4612 tt_var_done_delta_set_index_map( face,
4613 &blend->avar_table->axisMap );
4615 FT_FREE( blend->avar_table );
4618 if ( blend->hvar_table )
4620 tt_var_done_item_variation_store( face,
4621 &blend->hvar_table->itemStore );
4623 tt_var_done_delta_set_index_map( face,
4624 &blend->hvar_table->widthMap );
4625 FT_FREE( blend->hvar_table );
4628 if ( blend->vvar_table )
4630 tt_var_done_item_variation_store( face,
4631 &blend->vvar_table->itemStore );
4633 tt_var_done_delta_set_index_map( face,
4634 &blend->vvar_table->widthMap );
4635 FT_FREE( blend->vvar_table );
4638 if ( blend->mvar_table )
4640 tt_var_done_item_variation_store( face,
4641 &blend->mvar_table->itemStore );
4643 FT_FREE( blend->mvar_table->values );
4644 FT_FREE( blend->mvar_table );
4647 FT_FREE( blend->tuplecoords );
4648 FT_FREE( blend->glyphoffsets );
4653 #else /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */
4655 /* ANSI C doesn't like empty source files */
4656 typedef int tt_gxvar_dummy_;
4658 #endif /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */