/
t1load.c
2756 lines (2205 loc) · 79.9 KB
/
t1load.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
*
* t1load.c
*
* Type 1 font loader (body).
*
* Copyright (C) 1996-2024 by
* David Turner, Robert Wilhelm, and Werner Lemberg.
*
* This file is part of the FreeType project, and may only be used,
* modified, and distributed under the terms of the FreeType project
* license, LICENSE.TXT. By continuing to use, modify, or distribute
* this file you indicate that you have read the license and
* understand and accept it fully.
*
*/
/**************************************************************************
*
* This is the new and improved Type 1 data loader for FreeType 2. The
* old loader has several problems: it is slow, complex, difficult to
* maintain, and contains incredible hacks to make it accept some
* ill-formed Type 1 fonts without hiccup-ing. Moreover, about 5% of
* the Type 1 fonts on my machine still aren't loaded correctly by it.
*
* This version is much simpler, much faster and also easier to read and
* maintain by a great order of magnitude. The idea behind it is to
* _not_ try to read the Type 1 token stream with a state machine (i.e.
* a Postscript-like interpreter) but rather to perform simple pattern
* matching.
*
* Indeed, nearly all data definitions follow a simple pattern like
*
* ... /Field <data> ...
*
* where <data> can be a number, a boolean, a string, or an array of
* numbers. There are a few exceptions, namely the encoding, font name,
* charstrings, and subrs; they are handled with a special pattern
* matching routine.
*
* All other common cases are handled very simply. The matching rules
* are defined in the file `t1tokens.h' through the use of several
* macros calls PARSE_XXX. This file is included twice here; the first
* time to generate parsing callback functions, the second time to
* generate a table of keywords (with pointers to the associated
* callback functions).
*
* The function `parse_dict' simply scans *linearly* a given dictionary
* (either the top-level or private one) and calls the appropriate
* callback when it encounters an immediate keyword.
*
* This is by far the fastest way one can find to parse and read all
* data.
*
* This led to tremendous code size reduction. Note that later, the
* glyph loader will also be _greatly_ simplified, and the automatic
* hinter will replace the clumsy `t1hinter'.
*
*/
#include <ft2build.h>
#include <freetype/internal/ftdebug.h>
#include FT_CONFIG_CONFIG_H
#include <freetype/ftmm.h>
#include <freetype/internal/t1types.h>
#include <freetype/internal/ftcalc.h>
#include <freetype/internal/fthash.h>
#include "t1load.h"
#include "t1errors.h"
#ifdef FT_CONFIG_OPTION_INCREMENTAL
#define IS_INCREMENTAL \
FT_BOOL( FT_FACE( face )->internal->incremental_interface )
#else
#define IS_INCREMENTAL 0
#endif
/**************************************************************************
*
* The macro FT_COMPONENT is used in trace mode. It is an implicit
* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
* messages during execution.
*/
#undef FT_COMPONENT
#define FT_COMPONENT t1load
#ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
/*************************************************************************/
/*************************************************************************/
/***** *****/
/***** MULTIPLE MASTERS SUPPORT *****/
/***** *****/
/*************************************************************************/
/*************************************************************************/
static FT_Error
t1_allocate_blend( T1_Face face,
FT_UInt num_designs,
FT_UInt num_axis )
{
PS_Blend blend;
FT_Memory memory = face->root.memory;
FT_Error error = FT_Err_Ok;
blend = face->blend;
if ( !blend )
{
if ( FT_NEW( blend ) )
goto Exit;
blend->num_default_design_vector = 0;
blend->weight_vector = NULL;
blend->default_weight_vector = NULL;
blend->design_pos[0] = NULL;
face->blend = blend;
}
/* allocate design data if needed */
if ( num_designs > 0 )
{
if ( blend->num_designs == 0 )
{
FT_UInt nn;
/* allocate the blend `private' and `font_info' dictionaries */
if ( FT_NEW_ARRAY( blend->font_infos[1], num_designs ) ||
FT_NEW_ARRAY( blend->privates [1], num_designs ) ||
FT_NEW_ARRAY( blend->bboxes [1], num_designs ) )
goto Exit;
blend->font_infos[0] = &face->type1.font_info;
blend->privates [0] = &face->type1.private_dict;
blend->bboxes [0] = &face->type1.font_bbox;
for ( nn = 2; nn <= num_designs; nn++ )
{
blend->font_infos[nn] = blend->font_infos[nn - 1] + 1;
blend->privates [nn] = blend->privates [nn - 1] + 1;
blend->bboxes [nn] = blend->bboxes [nn - 1] + 1;
}
blend->num_designs = num_designs;
}
else if ( blend->num_designs != num_designs )
goto Fail;
}
/* allocate axis data if needed */
if ( num_axis > 0 )
{
if ( blend->num_axis != 0 && blend->num_axis != num_axis )
goto Fail;
blend->num_axis = num_axis;
}
Exit:
return error;
Fail:
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
FT_LOCAL_DEF( FT_Error )
T1_Get_Multi_Master( FT_Face face, /* T1_Face */
FT_Multi_Master* master )
{
T1_Face t1face = (T1_Face)face;
PS_Blend blend = t1face->blend;
FT_UInt n;
FT_Error error;
error = FT_THROW( Invalid_Argument );
if ( blend )
{
master->num_axis = blend->num_axis;
master->num_designs = blend->num_designs;
for ( n = 0; n < blend->num_axis; n++ )
{
FT_MM_Axis* axis = master->axis + n;
PS_DesignMap map = blend->design_map + n;
axis->name = blend->axis_names[n];
axis->minimum = map->design_points[0];
axis->maximum = map->design_points[map->num_points - 1];
}
error = FT_Err_Ok;
}
return error;
}
/**************************************************************************
*
* Given a normalized (blend) coordinate, figure out the design
* coordinate appropriate for that value.
*/
static FT_Fixed
mm_axis_unmap( PS_DesignMap axismap,
FT_Fixed ncv )
{
int j;
if ( ncv <= axismap->blend_points[0] )
return INT_TO_FIXED( axismap->design_points[0] );
for ( j = 1; j < axismap->num_points; j++ )
{
if ( ncv <= axismap->blend_points[j] )
return INT_TO_FIXED( axismap->design_points[j - 1] +
FT_MulDiv( ncv - axismap->blend_points[j - 1],
axismap->design_points[j] -
axismap->design_points[j - 1],
axismap->blend_points[j] -
axismap->blend_points[j - 1] ) );
}
return INT_TO_FIXED( axismap->design_points[axismap->num_points - 1] );
}
/**************************************************************************
*
* Given a vector of weights, one for each design, figure out the
* normalized axis coordinates which gave rise to those weights.
*/
static void
mm_weights_unmap( FT_Fixed* weights,
FT_Fixed* axiscoords,
FT_UInt axis_count )
{
FT_ASSERT( axis_count <= T1_MAX_MM_AXIS );
if ( axis_count == 1 )
axiscoords[0] = weights[1];
else if ( axis_count == 2 )
{
axiscoords[0] = weights[3] + weights[1];
axiscoords[1] = weights[3] + weights[2];
}
else if ( axis_count == 3 )
{
axiscoords[0] = weights[7] + weights[5] + weights[3] + weights[1];
axiscoords[1] = weights[7] + weights[6] + weights[3] + weights[2];
axiscoords[2] = weights[7] + weights[6] + weights[5] + weights[4];
}
else
{
axiscoords[0] = weights[15] + weights[13] + weights[11] + weights[9] +
weights[7] + weights[5] + weights[3] + weights[1];
axiscoords[1] = weights[15] + weights[14] + weights[11] + weights[10] +
weights[7] + weights[6] + weights[3] + weights[2];
axiscoords[2] = weights[15] + weights[14] + weights[13] + weights[12] +
weights[7] + weights[6] + weights[5] + weights[4];
axiscoords[3] = weights[15] + weights[14] + weights[13] + weights[12] +
weights[11] + weights[10] + weights[9] + weights[8];
}
}
/**************************************************************************
*
* Just a wrapper around T1_Get_Multi_Master to support the different
* arguments needed by the GX var distortable fonts.
*/
FT_LOCAL_DEF( FT_Error )
T1_Get_MM_Var( FT_Face face, /* T1_Face */
FT_MM_Var* *master )
{
T1_Face t1face = (T1_Face)face;
FT_Memory memory = FT_FACE_MEMORY( face );
FT_MM_Var *mmvar = NULL;
FT_Multi_Master mmaster;
FT_Error error;
FT_UInt i;
FT_Fixed axiscoords[T1_MAX_MM_AXIS];
PS_Blend blend = t1face->blend;
FT_UShort* axis_flags;
FT_Offset mmvar_size;
FT_Offset axis_flags_size;
FT_Offset axis_size;
error = T1_Get_Multi_Master( face, &mmaster );
if ( error )
goto Exit;
/* the various `*_size' variables, which we also use as */
/* offsets into the `mmvar' array, must be multiples of the */
/* pointer size (except the last one); without such an */
/* alignment there might be runtime errors due to */
/* misaligned addresses */
#undef ALIGN_SIZE
#define ALIGN_SIZE( n ) \
( ( (n) + sizeof (void*) - 1 ) & ~( sizeof (void*) - 1 ) )
mmvar_size = ALIGN_SIZE( sizeof ( FT_MM_Var ) );
axis_flags_size = ALIGN_SIZE( mmaster.num_axis *
sizeof ( FT_UShort ) );
axis_size = mmaster.num_axis * sizeof ( FT_Var_Axis );
if ( FT_QALLOC( mmvar, mmvar_size +
axis_flags_size +
axis_size ) )
goto Exit;
mmvar->num_axis = mmaster.num_axis;
mmvar->num_designs = mmaster.num_designs;
mmvar->num_namedstyles = 0; /* Not supported */
/* while axis flags are meaningless here, we have to provide the array */
/* to make `FT_Get_Var_Axis_Flags' work: the function expects that the */
/* values directly follow the data of `FT_MM_Var' */
axis_flags = (FT_UShort*)( (char*)mmvar + mmvar_size );
FT_ARRAY_ZERO( axis_flags, mmaster.num_axis );
mmvar->axis = (FT_Var_Axis*)( (char*)axis_flags + axis_flags_size );
mmvar->namedstyle = NULL;
for ( i = 0; i < mmaster.num_axis; i++ )
{
mmvar->axis[i].name = mmaster.axis[i].name;
mmvar->axis[i].minimum = INT_TO_FIXED( mmaster.axis[i].minimum );
mmvar->axis[i].maximum = INT_TO_FIXED( mmaster.axis[i].maximum );
mmvar->axis[i].strid = ~0U; /* Does not apply */
mmvar->axis[i].tag = ~0U; /* Does not apply */
if ( !mmvar->axis[i].name )
continue;
if ( ft_strcmp( mmvar->axis[i].name, "Weight" ) == 0 )
mmvar->axis[i].tag = FT_MAKE_TAG( 'w', 'g', 'h', 't' );
else if ( ft_strcmp( mmvar->axis[i].name, "Width" ) == 0 )
mmvar->axis[i].tag = FT_MAKE_TAG( 'w', 'd', 't', 'h' );
else if ( ft_strcmp( mmvar->axis[i].name, "OpticalSize" ) == 0 )
mmvar->axis[i].tag = FT_MAKE_TAG( 'o', 'p', 's', 'z' );
else if ( ft_strcmp( mmvar->axis[i].name, "Slant" ) == 0 )
mmvar->axis[i].tag = FT_MAKE_TAG( 's', 'l', 'n', 't' );
else if ( ft_strcmp( mmvar->axis[i].name, "Italic" ) == 0 )
mmvar->axis[i].tag = FT_MAKE_TAG( 'i', 't', 'a', 'l' );
}
mm_weights_unmap( blend->default_weight_vector,
axiscoords,
blend->num_axis );
for ( i = 0; i < mmaster.num_axis; i++ )
mmvar->axis[i].def = mm_axis_unmap( &blend->design_map[i],
axiscoords[i] );
*master = mmvar;
Exit:
return error;
}
static FT_Error
t1_set_mm_blend( T1_Face face,
FT_UInt num_coords,
FT_Fixed* coords )
{
PS_Blend blend = face->blend;
FT_UInt n, m;
FT_Bool have_diff = 0;
if ( !blend )
return FT_THROW( Invalid_Argument );
if ( num_coords > blend->num_axis )
num_coords = blend->num_axis;
/* recompute the weight vector from the blend coordinates */
for ( n = 0; n < blend->num_designs; n++ )
{
FT_Fixed result = 0x10000L; /* 1.0 fixed */
FT_Fixed factor;
for ( m = 0; m < blend->num_axis; m++ )
{
/* use a default value if we don't have a coordinate */
if ( m >= num_coords )
{
result >>= 1;
continue;
}
/* get current blend axis position */
factor = coords[m];
if ( ( n & ( 1 << m ) ) == 0 )
factor = 0x10000L - factor;
if ( factor <= 0 )
{
result = 0;
break;
}
else if ( factor >= 0x10000L )
continue;
result = FT_MulFix( result, factor );
}
if ( blend->weight_vector[n] != result )
{
blend->weight_vector[n] = result;
have_diff = 1;
}
}
/* return value -1 indicates `no change' */
return have_diff ? FT_Err_Ok : -1;
}
FT_LOCAL_DEF( FT_Error )
T1_Set_MM_Blend( FT_Face face, /* T1_Face */
FT_UInt num_coords,
FT_Fixed* coords )
{
return t1_set_mm_blend( (T1_Face)face, num_coords, coords );
}
FT_LOCAL_DEF( FT_Error )
T1_Get_MM_Blend( FT_Face face, /* T1_Face */
FT_UInt num_coords,
FT_Fixed* coords )
{
T1_Face t1face = (T1_Face)face;
PS_Blend blend = t1face->blend;
FT_Fixed axiscoords[4];
FT_UInt i, nc;
if ( !blend )
return FT_THROW( Invalid_Argument );
mm_weights_unmap( blend->weight_vector,
axiscoords,
blend->num_axis );
nc = num_coords;
if ( num_coords > blend->num_axis )
{
FT_TRACE2(( "T1_Get_MM_Blend: only using first %d of %d coordinates\n",
blend->num_axis, num_coords ));
nc = blend->num_axis;
}
for ( i = 0; i < nc; i++ )
coords[i] = axiscoords[i];
for ( ; i < num_coords; i++ )
coords[i] = 0x8000;
return FT_Err_Ok;
}
FT_LOCAL_DEF( FT_Error )
T1_Set_MM_WeightVector( FT_Face face, /* T1_Face */
FT_UInt len,
FT_Fixed* weightvector )
{
T1_Face t1face = (T1_Face)face;
PS_Blend blend = t1face->blend;
FT_UInt i, n;
if ( !blend )
return FT_THROW( Invalid_Argument );
if ( !len && !weightvector )
{
for ( i = 0; i < blend->num_designs; i++ )
blend->weight_vector[i] = blend->default_weight_vector[i];
}
else
{
if ( !weightvector )
return FT_THROW( Invalid_Argument );
n = len < blend->num_designs ? len : blend->num_designs;
for ( i = 0; i < n; i++ )
blend->weight_vector[i] = weightvector[i];
for ( ; i < blend->num_designs; i++ )
blend->weight_vector[i] = (FT_Fixed)0;
}
return FT_Err_Ok;
}
FT_LOCAL_DEF( FT_Error )
T1_Get_MM_WeightVector( FT_Face face, /* T1_Face */
FT_UInt* len,
FT_Fixed* weightvector )
{
T1_Face t1face = (T1_Face)face;
PS_Blend blend = t1face->blend;
FT_UInt i;
if ( !blend )
return FT_THROW( Invalid_Argument );
if ( *len < blend->num_designs )
{
*len = blend->num_designs;
return FT_THROW( Invalid_Argument );
}
for ( i = 0; i < blend->num_designs; i++ )
weightvector[i] = blend->weight_vector[i];
for ( ; i < *len; i++ )
weightvector[i] = (FT_Fixed)0;
*len = blend->num_designs;
return FT_Err_Ok;
}
FT_LOCAL_DEF( FT_Error )
T1_Set_MM_Design( FT_Face face, /* T1_Face */
FT_UInt num_coords,
FT_Long* coords )
{
T1_Face t1face = (T1_Face)face;
FT_Error error;
PS_Blend blend = t1face->blend;
FT_UInt n;
FT_Fixed final_blends[T1_MAX_MM_DESIGNS];
if ( !blend )
return FT_THROW( Invalid_Argument );
if ( num_coords > blend->num_axis )
num_coords = blend->num_axis;
/* compute the blend coordinates through the blend design map */
for ( n = 0; n < blend->num_axis; n++ )
{
FT_Long design;
FT_Fixed the_blend;
PS_DesignMap map = blend->design_map + n;
FT_Long* designs = map->design_points;
FT_Fixed* blends = map->blend_points;
FT_Int p, before = -1, after = -1;
/* use a default value if we don't have a coordinate */
if ( n < num_coords )
design = coords[n];
else
design = ( designs[map->num_points - 1] - designs[0] ) / 2;
for ( p = 0; p < (FT_Int)map->num_points; p++ )
{
FT_Long p_design = designs[p];
/* exact match? */
if ( design == p_design )
{
the_blend = blends[p];
goto Found;
}
if ( design < p_design )
{
after = p;
break;
}
before = p;
}
/* now interpolate if necessary */
if ( before < 0 )
the_blend = blends[0];
else if ( after < 0 )
the_blend = blends[map->num_points - 1];
else
the_blend = FT_MulDiv( design - designs[before],
blends [after] - blends [before],
designs[after] - designs[before] );
Found:
final_blends[n] = the_blend;
}
error = t1_set_mm_blend( t1face, blend->num_axis, final_blends );
if ( error )
return error;
return FT_Err_Ok;
}
/* MM fonts don't have named instances, so only the design is reset */
FT_LOCAL_DEF( FT_Error )
T1_Reset_MM_Blend( FT_Face face,
FT_UInt instance_index )
{
FT_UNUSED( instance_index );
return T1_Set_MM_Blend( face, 0, NULL );
}
/**************************************************************************
*
* Just a wrapper around T1_Set_MM_Design to support the different
* arguments needed by the GX var distortable fonts.
*/
FT_LOCAL_DEF( FT_Error )
T1_Set_Var_Design( FT_Face face, /* T1_Face */
FT_UInt num_coords,
FT_Fixed* coords )
{
FT_Long lcoords[T1_MAX_MM_AXIS];
FT_UInt i;
if ( num_coords > T1_MAX_MM_AXIS )
num_coords = T1_MAX_MM_AXIS;
for ( i = 0; i < num_coords; i++ )
lcoords[i] = FIXED_TO_INT( coords[i] );
return T1_Set_MM_Design( face, num_coords, lcoords );
}
FT_LOCAL_DEF( FT_Error )
T1_Get_Var_Design( FT_Face face, /* T1_Face */
FT_UInt num_coords,
FT_Fixed* coords )
{
T1_Face t1face = (T1_Face)face;
PS_Blend blend = t1face->blend;
FT_Fixed axiscoords[4];
FT_UInt i, nc;
if ( !blend )
return FT_THROW( Invalid_Argument );
mm_weights_unmap( blend->weight_vector,
axiscoords,
blend->num_axis );
nc = num_coords;
if ( num_coords > blend->num_axis )
{
FT_TRACE2(( "T1_Get_Var_Design:"
" only using first %d of %d coordinates\n",
blend->num_axis, num_coords ));
nc = blend->num_axis;
}
for ( i = 0; i < nc; i++ )
coords[i] = mm_axis_unmap( &blend->design_map[i], axiscoords[i] );
for ( ; i < num_coords; i++ )
coords[i] = 0;
return FT_Err_Ok;
}
FT_LOCAL_DEF( void )
T1_Done_Blend( FT_Face face ) /* T1_Face */
{
T1_Face t1face = (T1_Face)face;
FT_Memory memory = FT_FACE_MEMORY( face );
PS_Blend blend = t1face->blend;
if ( blend )
{
FT_UInt num_designs = blend->num_designs;
FT_UInt num_axis = blend->num_axis;
FT_UInt n;
/* release design pos table */
FT_FREE( blend->design_pos[0] );
for ( n = 1; n < num_designs; n++ )
blend->design_pos[n] = NULL;
/* release blend `private' and `font info' dictionaries */
FT_FREE( blend->privates[1] );
FT_FREE( blend->font_infos[1] );
FT_FREE( blend->bboxes[1] );
for ( n = 0; n < num_designs; n++ )
{
blend->privates [n] = NULL;
blend->font_infos[n] = NULL;
blend->bboxes [n] = NULL;
}
/* release weight vectors */
FT_FREE( blend->weight_vector );
blend->default_weight_vector = NULL;
/* release axis names */
for ( n = 0; n < num_axis; n++ )
FT_FREE( blend->axis_names[n] );
/* release design map */
for ( n = 0; n < num_axis; n++ )
{
PS_DesignMap dmap = blend->design_map + n;
FT_FREE( dmap->design_points );
dmap->num_points = 0;
}
FT_FREE( t1face->blend );
}
}
static void
parse_blend_axis_types( FT_Face face, /* T1_Face */
void* loader_ )
{
T1_Face t1face = (T1_Face)face;
T1_Loader loader = (T1_Loader)loader_;
T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
FT_Int n, num_axis;
FT_Error error = FT_Err_Ok;
PS_Blend blend;
FT_Memory memory = FT_FACE_MEMORY( face );
/* take an array of objects */
T1_ToTokenArray( &loader->parser, axis_tokens,
T1_MAX_MM_AXIS, &num_axis );
if ( num_axis < 0 )
{
error = FT_ERR( Ignore );
goto Exit;
}
if ( num_axis == 0 || num_axis > T1_MAX_MM_AXIS )
{
FT_ERROR(( "parse_blend_axis_types: incorrect number of axes: %d\n",
num_axis ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
/* allocate blend if necessary */
error = t1_allocate_blend( t1face, 0, (FT_UInt)num_axis );
if ( error )
goto Exit;
FT_TRACE4(( " [" ));
blend = t1face->blend;
/* each token is an immediate containing the name of the axis */
for ( n = 0; n < num_axis; n++ )
{
T1_Token token = axis_tokens + n;
FT_Byte* name;
FT_UInt len;
/* skip first slash, if any */
if ( token->start[0] == '/' )
token->start++;
len = (FT_UInt)( token->limit - token->start );
if ( len == 0 )
{
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
FT_TRACE4(( " /%.*s", len, token->start ));
name = (FT_Byte*)blend->axis_names[n];
if ( name )
{
FT_TRACE0(( "parse_blend_axis_types:"
" overwriting axis name `%s' with `%.*s'\n",
name, len, token->start ));
FT_FREE( name );
}
if ( FT_QALLOC( blend->axis_names[n], len + 1 ) )
goto Exit;
name = (FT_Byte*)blend->axis_names[n];
FT_MEM_COPY( name, token->start, len );
name[len] = '\0';
}
FT_TRACE4(( "]\n" ));
Exit:
loader->parser.root.error = error;
}
static void
parse_blend_design_positions( FT_Face face, /* T1_Face */
void* loader_ )
{
T1_Face t1face = (T1_Face)face;
T1_Loader loader = (T1_Loader)loader_;
T1_TokenRec design_tokens[T1_MAX_MM_DESIGNS];
FT_Int num_designs;
FT_Int num_axis = 0; /* make compiler happy */
T1_Parser parser = &loader->parser;
FT_Memory memory = FT_FACE_MEMORY( face );
FT_Error error = FT_Err_Ok;
FT_Fixed* design_pos[T1_MAX_MM_DESIGNS];
design_pos[0] = NULL;
/* get the array of design tokens -- compute number of designs */
T1_ToTokenArray( parser, design_tokens,
T1_MAX_MM_DESIGNS, &num_designs );
if ( num_designs < 0 )
{
error = FT_ERR( Ignore );
goto Exit;
}
if ( num_designs == 0 || num_designs > T1_MAX_MM_DESIGNS )
{
FT_ERROR(( "parse_blend_design_positions:"
" incorrect number of designs: %d\n",
num_designs ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
{
FT_Byte* old_cursor = parser->root.cursor;
FT_Byte* old_limit = parser->root.limit;
FT_Int n, nn;
PS_Blend blend;
FT_TRACE4(( " [" ));
for ( n = 0; n < num_designs; n++ )
{
T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
T1_Token token;
FT_Int axis, n_axis;
/* read axis/coordinates tokens */
token = design_tokens + n;
parser->root.cursor = token->start;
parser->root.limit = token->limit;
T1_ToTokenArray( parser, axis_tokens, T1_MAX_MM_AXIS, &n_axis );
if ( n == 0 )
{
if ( n_axis <= 0 || n_axis > T1_MAX_MM_AXIS )
{
FT_ERROR(( "parse_blend_design_positions:"
" invalid number of axes: %d\n",
n_axis ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
num_axis = n_axis;
error = t1_allocate_blend( t1face,
(FT_UInt)num_designs,
(FT_UInt)num_axis );
if ( error )
goto Exit;
/* allocate a blend design pos table */
if ( FT_QNEW_ARRAY( design_pos[0], num_designs * num_axis ) )
goto Exit;
for ( nn = 1; nn < num_designs; nn++ )
design_pos[nn] = design_pos[0] + num_axis * nn;
}
else if ( n_axis != num_axis )
{
FT_ERROR(( "parse_blend_design_positions: incorrect table\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
/* now read each axis token into the design position */
FT_TRACE4(( " [" )) ;
for ( axis = 0; axis < n_axis; axis++ )
{
T1_Token token2 = axis_tokens + axis;
parser->root.cursor = token2->start;
parser->root.limit = token2->limit;
design_pos[n][axis] = T1_ToFixed( parser, 0 );
FT_TRACE4(( " %f", (double)design_pos[n][axis] / 65536 ));
}
FT_TRACE4(( "]" )) ;
}
FT_TRACE4(( "]\n" ));
loader->parser.root.cursor = old_cursor;
loader->parser.root.limit = old_limit;
/* a valid BlendDesignPosition has been parsed */
blend = t1face->blend;
if ( blend->design_pos[0] )
FT_FREE( blend->design_pos[0] );
for ( n = 0; n < num_designs; n++ )
{
blend->design_pos[n] = design_pos[n];
design_pos[n] = NULL;
}
}
Exit:
FT_FREE( design_pos[0] );
loader->parser.root.error = error;
}
static void
parse_blend_design_map( FT_Face face, /* T1_Face */
void* loader_ )
{
T1_Face t1face = (T1_Face)face;
T1_Loader loader = (T1_Loader)loader_;
FT_Error error = FT_Err_Ok;
T1_Parser parser = &loader->parser;
PS_Blend blend;
T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
FT_Int n, num_axis;
FT_Byte* old_cursor;
FT_Byte* old_limit;
FT_Memory memory = FT_FACE_MEMORY( face );
T1_ToTokenArray( parser, axis_tokens,
T1_MAX_MM_AXIS, &num_axis );
if ( num_axis < 0 )
{
error = FT_ERR( Ignore );
goto Exit;
}
if ( num_axis == 0 || num_axis > T1_MAX_MM_AXIS )
{
FT_ERROR(( "parse_blend_design_map: incorrect number of axes: %d\n",
num_axis ));
error = FT_THROW( Invalid_File_Format );
goto Exit;