/
nowakowskittfinstr.c
5270 lines (4595 loc) · 175 KB
/
nowakowskittfinstr.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
/* Copyright (C) 2000-2012 by
George Williams, Michal Nowakowski & Alexey Kryukov */
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "fontforgevw.h"
#include <math.h>
#include <utype.h>
#include "ttf.h"
#include "splinefont.h"
#include "stemdb.h"
extern int autohint_before_generate;
int instruct_diagonal_stems = 1,
instruct_serif_stems = 1,
instruct_ball_terminals = 1,
interpolate_strong = 1,
interpolate_more_strong = 1, /* not applicable if interpolate_strong==0 */
control_counters = 0;
/* non-optimized instructions will be using a stack of depth 6, allowing
* for easy testing whether the code leaves trash on the stack or not.
*/
#define OPTIMIZE_TTF_INSTRS 1
#if OPTIMIZE_TTF_INSTRS
#define STACK_DEPTH 256
#else
#define STACK_DEPTH 6
#endif
/* define some often used instructions */
#define SVTCA_y (0x00)
#define SVTCA_x (0x01)
#define SRP0 (0x10)
#define SRP1 (0x11)
#define SRP2 (0x12)
#define SZP0 (0x13)
#define SLOOP (0x17)
#define RTG (0x18)
#define SMD (0x1a)
#define DUP (0x20)
#define DEPTH (0x24)
#define CALL (0x2b)
#define MDAP (0x2e)
#define MDAP_rnd (0x2f)
#define IUP_y (0x30)
#define IUP_x (0x31)
#define SHP_rp2 (0x32)
#define SHP_rp1 (0x33)
#define SHPIX (0x38)
#define IP (0x39)
#define ALIGNRP (0x3c)
#define MIAP_rnd (0x3f)
#define ADD (0x60)
#define MUL (0x63)
#define NEG (0x65)
#define SROUND (0x76)
#define FLIPPT (0x80)
#define MDRP_grey (0xc0)
#define MDRP_min_black (0xc9)
#define MDRP_min_white (0xca)
#define MDRP_min_rnd_black (0xcd)
#define MDRP_rp0_rnd_white (0xd6)
#define MDRP_rp0_min_rnd_grey (0xdc)
#define MDRP_rp0_min_rnd_black (0xdd)
#define MIRP_min_black (0xe9)
#define MIRP_min_rnd_black (0xed)
#define MIRP_rp0_min_black (0xf9)
#define MIRP_rp0_min_rnd_black (0xfd)
/******************************************************************************
*
* Low-level routines to add data for PUSHes to bytecode instruction stream.
* pushheader() adds PUSH preamble, then repeating addpoint() adds items.
*
* Numbers larger than 65535 are not supported (according to TrueType spec,
* there can't be more points in a glyph, simple or compound). Negative
* numbers aren't supported, either. So don't use these functions as they
* are - there are higher-level ones further below, that handle things nicely.
*
******************************************************************************/
static uint8 *pushheader(uint8 *instrs, int isword, int tot) {
if ( isword ) {
if ( tot>8 ) {
*instrs++ = 0x41; /* N(next word) Push words */
*instrs++ = tot;
} else
*instrs++ = 0xb8+(tot-1); /* Push Words */
} else {
if ( tot>8 ) {
*instrs++ = 0x40; /* N(next byte) Push bytes */
*instrs++ = tot;
} else
*instrs++ = 0xb0+(tot-1); /* Push bytes */
}
return( instrs );
}
static uint8 *addpoint(uint8 *instrs,int isword,int pt) {
if ( !isword ) {
*instrs++ = pt;
} else {
*instrs++ = pt>>8;
*instrs++ = pt&0xff;
}
return( instrs );
}
/* Exemplary high-level routines to add PUSH-es to bytecode instruction
* stream. They handle negative numbers correctly. As they are used
* in various roles here, some aliases are defined, so that the name
* speaks for itself in the code.
*/
static uint8 *pushpoint(uint8 *instrs,int pt) {
instrs = pushheader(instrs,(pt>255)||(pt<0),1);
return( addpoint(instrs,(pt>255)||(pt<0),pt));
}
#define pushnum(a, b) pushpoint(a, b)
static uint8 *pushpointstem(uint8 *instrs, int pt, int stem) {
int isword = pt>255 || stem>255 || pt<0 || stem<0;
instrs = pushheader(instrs,isword,2);
instrs = addpoint(instrs,isword,pt);
return( addpoint(instrs,isword,stem));
}
#define push2points(a, b, c) pushpointstem(a, b, c)
#define push2nums(a, b, c) pushpointstem(a, b, c)
/* Push a bunch of point numbers (or other numbers) onto the stack.
* TODO!
* Possible strategies:
* - push point by point (poor space efficiency)
* - push all the stock at once (currently used, better, but has
* poor space efficiency in case of a word among several bytes).
* - push bytes and words separately
*/
static uint8 *pushpoints(uint8 *instrs, int ptcnt, const int *pts) {
int i, isword = 0;
for (i=0; i<ptcnt; i++) if (pts[i]>255 || pts[i]<0) isword=1;
/* It's an error to push more than STACK_DEPTH points. */
if (ptcnt > STACK_DEPTH)
IError("Truetype stack overflow will occur.");
if (ptcnt > 255 && !isword) {
instrs = pushpoints(instrs, 255, pts);
ptcnt-=255;
pts+=255;
}
instrs = pushheader(instrs,isword,ptcnt);
for (i=0; i<ptcnt; i++) instrs = addpoint(instrs, isword, pts[i]);
return( instrs );
}
#define pushnums(a, b, c) pushpoints(a, b, c)
/* As we don't have "push F26dot6" command in truetype instructions,
* we need to do this by hand. As we can explicitly push only 16-bit
* quantities, we need to push a F26dot6 value in halves, shift left
* the more significant half and add halves.
*
* There are no checks for overflow!
*/
static uint8 *pushF26Dot6(uint8 *instrs, double num) {
int a, elems[3];
int negative=0;
if (num < 0) {
negative=1;
num*=-1.0;
}
num *= 64;
a = rint(num);
elems[0] = a % 65536;
elems[1] = (int)rint(a / 65536.0) % 65536;
elems[2] = 16384;
if (elems[1]) {
instrs = pushpoints(instrs, 3, elems);
*instrs++ = DUP;
*instrs++ = MUL;
*instrs++ = MUL;
*instrs++ = ADD;
}
else instrs = pushpoint(instrs, elems[0]);
if (negative) *instrs++ = NEG;
return( instrs );
}
/* Compute an EF2Dot14 representation of a floating point number.
* The number must be in range [-2.0 ... 1.0+(2^14-1)/(2^14) = 1.99993896...]
*
* There are no checks for overflow!
*/
static int EF2Dot14(double num) {
return( rint(num*16384) );
}
/* An apparatus for instructing sets of points with given truetype command.
* The command must pop exactly 1 element from the stack and mustn't push any.
* These points must be marked as 'touched' elsewhere! this function only
* generates intructions.
*/
static uint8 *instructpoints(uint8 *instrs, int ptcnt, const int *pts, uint8 command) {
int i, use_sloop;
use_sloop = 0;
use_sloop |= (command == SHP_rp1);
use_sloop |= (command == SHP_rp2);
use_sloop |= (command == SHPIX);
use_sloop |= (command == IP);
use_sloop |= (command == FLIPPT);
use_sloop |= (command == ALIGNRP);
use_sloop = use_sloop && (ptcnt > 3);
instrs = pushpoints(instrs, ptcnt<STACK_DEPTH?ptcnt:STACK_DEPTH-1, pts);
if (use_sloop) {
*instrs++ = DEPTH;
*instrs++ = SLOOP;
*instrs++ = command;
}
else for (i=0; i<(ptcnt<STACK_DEPTH?ptcnt:STACK_DEPTH-1); i++)
*instrs++ = command;
if (ptcnt>=STACK_DEPTH)
instrs=instructpoints(instrs, ptcnt-(STACK_DEPTH-1), pts+(STACK_DEPTH-1), command);
return( instrs );
}
/******************************************************************************
*
* Low-level routines for getting a cvt index for a stem width, assuming there
* are any numbers in cvt. Includes legacy code for importing PS Private into
* CVT.
*
******************************************************************************/
struct ttf_table *SFFindTable(SplineFont *sf,uint32 tag) {
struct ttf_table *tab;
for ( tab=sf->ttf_tables; tab!=NULL && tab->tag!=tag; tab=tab->next );
return( tab );
}
int TTF__getcvtval(SplineFont *sf,int val) {
int i;
struct ttf_table *cvt_tab = SFFindTable(sf,CHR('c','v','t',' '));
if ( cvt_tab==NULL ) {
cvt_tab = chunkalloc(sizeof(struct ttf_table));
cvt_tab->tag = CHR('c','v','t',' ');
cvt_tab->maxlen = 200;
cvt_tab->data = malloc(100*sizeof(short));
cvt_tab->next = sf->ttf_tables;
sf->ttf_tables = cvt_tab;
}
for ( i=0; (int)sizeof(uint16)*i<cvt_tab->len; ++i ) {
int tval = (int16) memushort(cvt_tab->data,cvt_tab->len, sizeof(uint16)*i);
if ( val>=tval-1 && val<=tval+1 )
return( i );
}
if ( (int)sizeof(uint16)*i>=cvt_tab->maxlen ) {
if ( cvt_tab->maxlen==0 ) cvt_tab->maxlen = cvt_tab->len;
cvt_tab->maxlen += 200;
cvt_tab->data = realloc(cvt_tab->data,cvt_tab->maxlen);
}
memputshort(cvt_tab->data,sizeof(uint16)*i,val);
cvt_tab->len += sizeof(uint16);
return( i );
}
/* by default sign is unimportant in the cvt
* For some instructions anyway, but not for MIAP so this routine has
* been broken in two.
*/
int TTF_getcvtval(SplineFont *sf,int val) {
if ( val<0 ) val = -val;
return( TTF__getcvtval(sf,val));
}
/* We are given a stem weight and try to find matching one in CVT.
* If none found, we return -1.
*/
static StdStem *CVTSeekStem(int xdir, GlobalInstrCt *gic, double value, int can_fail) {
StdStem *mainstem = xdir?&(gic->stdvw):&(gic->stdhw);
StdStem *otherstems = xdir?gic->stemsnapv:gic->stemsnaph;
StdStem *closest = NULL;
int otherstemcnt = xdir?gic->stemsnapvcnt:gic->stemsnaphcnt;
int i;
double mindelta=1e20, delta, closestwidth=1e20;
if (mainstem->width == -1)
return NULL;
value = fabs(value);
delta = fabs(mainstem->width - value);
if (delta < mindelta) {
mindelta = delta;
closestwidth = rint(mainstem->width);
closest = mainstem;
}
for (i=0; i<otherstemcnt; i++) {
delta = fabs(otherstems[i].width - value);
if (delta < mindelta) {
mindelta = delta;
closestwidth = otherstems[i].width;
closest = otherstems+i;
}
}
if (mindelta <= gic->fudge)
return closest;
if (value/closestwidth < 1.11 && value/closestwidth > 0.9)
return closest;
if (can_fail)
return NULL;
return closest;
}
/******************************************************************************
******************************************************************************
**
** We need to initialize global instructing context before autoinstructing
** a glyph, because we want to be sure that global hinting tables (cvt, prep,
** fpgm) were (or weren't) properly set up.
**
******************************************************************************
******************************************************************************/
/* Helper routines: read PS private entry and return its contents.
*/
static int GetBlueFuzz(SplineFont *sf) {
char *str, *end;
if ( sf->private==NULL || (str=PSDictHasEntry(sf->private,"BlueFuzz"))==NULL || !isdigit(str[0]) )
return 1;
return strtod(str, &end);
}
/* Return BlueScale as PPEM at which we have to stop suppressing overshoots */
static int GetBlueScale(SplineFont *sf) {
char *str, *end;
double bs;
int result;
if ( sf->private==NULL || (str=PSDictHasEntry(sf->private,"BlueScale"))==NULL )
return 42;
bs = strtod(str, &end);
if (end==str || bs<=0.0) bs=0.039625;
bs*=240;
bs+=0.49;
bs*=300.0/72.0;
result = (int)rint(bs);
if (result>255) result = 255; /* Who would need such blue scale??? */
return result;
}
static real *ParsePSArray(const char *str, int *rescnt) {
char *end;
real d, *results=NULL;
if ((rescnt == NULL) || (str == NULL))
return NULL;
*rescnt = 0;
while (*str)
{
while (!isdigit(*str) && *str!='-' && *str!='+' && *str!='.' && *str!='\0')
++str;
if ( *str=='\0' )
break;
d = strtod(str, &end);
if ( d>=-32768 && d<=32767 ) {
if (*rescnt) {
results = realloc(results, sizeof(real)*(++(*rescnt)));
results[*rescnt-1] = d;
}
else (results = calloc(*rescnt=1, sizeof(real)))[0] = d;
}
str = end;
}
return results;
}
static real *GetNParsePSArray(SplineFont *sf, const char *name, int *rescnt) {
return ParsePSArray(PSDictHasEntry(sf->private, name), rescnt);
}
/* Tell if the two segments, [b1,o1] and [b2,o2] intersect.
* This can be used to determine whether blues or stems overlap.
*/
static int SegmentsOverlap(real b1, real o1, real b2, real o2) {
real t;
if (b1 > o1) {
t = o1;
o1 = b1;
b1 = t;
}
if (b2 > o2) {
t = o2;
o2 = b2;
b2 = t;
}
return !((b2 > o1) || (o2 < b1));
}
/* To be used with qsort() - sorts BlueZone array by base in ascending order.
*/
static int SortBlues(const void *a, const void *b) {
return ((BlueZone *)a)->base > ((BlueZone *)b)->base;
}
/* Import blue data into global instructing context. Include family blues too.
* We assume that blues are needed for family blues to make sense. If there are
* only family blues, we treat them as normal blues. Otherwise, if a family blue
* zone doesn't match any normal blue zone, or if they match perfectly,
* it is ignored.
*/
static void GICImportBlues(GlobalInstrCt *gic) {
int bluecnt = 0;
int i, j, cnt;
real *values;
int HasPSBlues =
(PSDictHasEntry(gic->sf->private, "BlueValues") != NULL) ||
(PSDictHasEntry(gic->sf->private, "OtherBlues") != NULL);
int HasPSFamilyBlues =
(PSDictHasEntry(gic->sf->private, "FamilyBlues") != NULL) ||
(PSDictHasEntry(gic->sf->private, "FamilyOtherBlues") != NULL);
const char *PrimaryBlues = HasPSBlues ? "BlueValues" : "FamilyBlues";
const char *OtherBlues = HasPSBlues ? "OtherBlues" : "FamilyOtherBlues";
if (HasPSBlues || HasPSFamilyBlues){
values = GetNParsePSArray(gic->sf, PrimaryBlues, &cnt);
cnt /= 2;
if (cnt > 7) cnt = 7;
if (values != NULL) {
gic->bluecnt = bluecnt = cnt;
/* First pair is a bottom zone (see Type1 specification). */
gic->blues[0].base = values[1];
gic->blues[0].overshoot = values[0];
gic->blues[0].family_base = strtod("NAN", NULL);
/* Next pairs are top zones (see Type1 specification). */
for (i=1; i<bluecnt; i++) {
gic->blues[i].family_base = strtod("NAN", NULL);
gic->blues[i].base = values[2*i];
gic->blues[i].overshoot = values[2*i+1];
}
free(values);
}
values = GetNParsePSArray(gic->sf, OtherBlues, &cnt);
cnt /= 2;
if (cnt > 5) cnt = 5;
if (values != NULL) {
gic->bluecnt += cnt;
/* All pairs are bottom zones (see Type1 specification). */
for (i=0; i<cnt; i++) {
gic->blues[i+bluecnt].family_base = strtod("NAN", NULL);
gic->blues[i+bluecnt].base = values[2*i+1];
gic->blues[i+bluecnt].overshoot = values[2*i];
}
free(values);
bluecnt += cnt;
}
/* Add family data to blues */
if (HasPSBlues && HasPSFamilyBlues) {
values = GetNParsePSArray(gic->sf, "FamilyBlues", &cnt);
cnt /= 2;
if (cnt > 7) cnt = 7;
if (values != NULL) {
/* First pair is a bottom zone (see Type1 specification). */
for (j=0; j<bluecnt; j++)
if (finite(gic->blues[j].family_base))
continue;
else if (values[1] != gic->blues[j].base &&
SegmentsOverlap(gic->blues[j].base,
gic->blues[j].overshoot,
values[0], values[1]))
gic->blues[j].family_base = values[1];
/* Next pairs are top zones (see Type1 specification). */
for (i=1; i<cnt; i++) {
for (j=0; j<bluecnt; j++)
if (finite(gic->blues[j].family_base))
continue;
else if (values[2*i] != gic->blues[j].base &&
SegmentsOverlap(gic->blues[j].base,
gic->blues[j].overshoot,
values[2*i], values[2*i+1]))
gic->blues[j].family_base = values[2*i];
}
free(values);
}
values = GetNParsePSArray(gic->sf, "FamilyOtherBlues", &cnt);
cnt /= 2;
if (cnt > 5) cnt = 5;
if (values != NULL) {
/* All pairs are bottom zones (see Type1 specification). */
for (i=0; i<cnt; i++) {
for (j=0; j<bluecnt; j++)
if (finite(gic->blues[j].family_base))
continue;
else if (values[2*i+1] != gic->blues[j].base &&
SegmentsOverlap(gic->blues[j].base,
gic->blues[j].overshoot,
values[2*i], values[2*i+1]))
gic->blues[j].family_base = values[2*i+1];
}
free(values);
}
}
}
else if (gic->bd->bluecnt) {
/* If there are no PS private entries, we have */
/* to use FF's quickly guessed fallback blues. */
gic->bluecnt = bluecnt = gic->bd->bluecnt;
for (i=0; i<bluecnt; i++) {
gic->blues[i].family_base = strtod("NAN", NULL);
gic->blues[i].family_cvtindex = -1;
if (gic->bd->blues[i][1] <= 0) {
gic->blues[i].base = gic->bd->blues[i][1];
gic->blues[i].overshoot = gic->bd->blues[i][0];
}
else {
gic->blues[i].base = gic->bd->blues[i][0];
gic->blues[i].overshoot = gic->bd->blues[i][1];
}
}
}
/* 'highest' and 'lowest' are not to be set yet. */
for (i=0; i<gic->bluecnt; i++)
gic->blues[i].highest = gic->blues[i].lowest = -1;
/* I assume ascending order in snap_to_blues(). */
qsort(gic->blues, gic->bluecnt, sizeof(BlueZone), SortBlues);
}
/* To be used with qsort() - sorts StdStem array by width in ascending order.
*/
static int SortStems(const void *a, const void *b) {
return ((StdStem *)a)->width > ((StdStem *)b)->width;
}
/* Import stem data into global instructing context. We deal only with
* horizontal or vertical stems (xdir decides) here. If Std*W is not specified,
* but there exists StemSnap*, we'll make up a fake Std*V as a fallback.
* Subtle manipulations with Std*W's value can result in massive change of
* font appearance at some pixel sizes, because it's used as a base for
* normalization of all other stems.
*/
static void GICImportStems(int xdir, GlobalInstrCt *gic) {
int i, cnt, next;
real *values;
const char *s_StdW = xdir?"StdVW":"StdHW";
const char *s_StemSnap = xdir?"StemSnapV":"StemSnapH";
StdStem *stdw = xdir?&(gic->stdvw):&(gic->stdhw);
StdStem **stemsnap = xdir?&(gic->stemsnapv):&(gic->stemsnaph);
int *stemsnapcnt = xdir?&(gic->stemsnapvcnt):&(gic->stemsnaphcnt);
if ((values = GetNParsePSArray(gic->sf, s_StdW, &cnt)) != NULL) {
stdw->width = *values;
free(values);
}
if ((values = GetNParsePSArray(gic->sf, s_StemSnap, &cnt)) != NULL) {
*stemsnap = (StdStem *)calloc(cnt, sizeof(StdStem));
for (next=i=0; i<cnt; i++)
if (values[i] != gic->stdhw.width)
(*stemsnap)[next++].width = values[i];
if (!next) {
free(*stemsnap);
*stemsnap = NULL;
}
*stemsnapcnt = next;
free(values);
/* I assume ascending order here and in normalize_stems(). */
qsort(*stemsnap, *stemsnapcnt, sizeof(StdStem), SortStems);
}
/* No StdW, but StemSnap exists? */
if (stdw->width == -1 && *stemsnap != NULL) {
cnt = *stemsnapcnt;
i = cnt/2;
stdw->width = (*stemsnap)[i].width;
memmove((*stemsnap)+i, (*stemsnap)+i+1, cnt-i-1);
if (--(*stemsnapcnt) == 0) {
free(*stemsnap);
*stemsnap = NULL;
}
}
}
/* Assign CVT indices to blues and stems in global instructing context. In case
* we can't implant it because of already existent cvt table, reassign the cvt
* indices, picking them from existing cvt table (thus a cvt value can't be
* considered 'horizontal' or 'vertical', and reliable stem normalization is
* thus impossible) and adding some for new values.
*/
static void init_cvt(GlobalInstrCt *gic) {
int i, cvtindex, cvtsize;
struct ttf_table *tab;
uint8 *cvt;
cvtsize = 1;
if (gic->stdhw.width != -1) cvtsize++;
if (gic->stdvw.width != -1) cvtsize++;
cvtsize += gic->stemsnaphcnt;
cvtsize += gic->stemsnapvcnt;
cvtsize += gic->bluecnt * 2; /* possible family blues */
cvt = calloc(cvtsize, cvtsize * sizeof(int16));
cvtindex = 0;
/* Assign cvt indices */
for (i=0; i<gic->bluecnt; i++) {
gic->blues[i].cvtindex = cvtindex;
memputshort(cvt, 2*cvtindex++, rint(gic->blues[i].base));
if (finite(gic->blues[i].family_base)) {
gic->blues[i].family_cvtindex = cvtindex;
memputshort(cvt, 2*cvtindex++, rint(gic->blues[i].family_base));
}
}
if (gic->stdhw.width != -1) {
gic->stdhw.cvtindex = cvtindex;
memputshort(cvt, 2*cvtindex++, rint(gic->stdhw.width));
}
for (i=0; i<gic->stemsnaphcnt; i++) {
gic->stemsnaph[i].cvtindex = cvtindex;
memputshort(cvt, 2*cvtindex++, rint(gic->stemsnaph[i].width));
}
if (gic->stdvw.width != -1) {
gic->stdvw.cvtindex = cvtindex;
memputshort(cvt, 2*cvtindex++, rint(gic->stdvw.width));
}
for (i=0; i<gic->stemsnapvcnt; i++) {
gic->stemsnapv[i].cvtindex = cvtindex;
memputshort(cvt, 2*cvtindex++, rint(gic->stemsnapv[i].width));
}
cvtsize = cvtindex;
cvt = realloc(cvt, cvtsize * sizeof(int16));
/* Try to implant the new cvt table */
gic->cvt_done = 0;
tab = SFFindTable(gic->sf, CHR('c','v','t',' '));
if ( tab==NULL ) {
tab = chunkalloc(sizeof(struct ttf_table));
tab->next = gic->sf->ttf_tables;
gic->sf->ttf_tables = tab;
tab->tag = CHR('c','v','t',' ');
tab->len = tab->maxlen = cvtsize * sizeof(int16);
if (tab->maxlen >256) tab->maxlen = 256;
tab->data = cvt;
gic->cvt_done = 1;
}
else {
if (tab->len >= cvtsize * (int)sizeof(int16) &&
memcmp(cvt, tab->data, cvtsize * sizeof(int16)) == 0)
gic->cvt_done = 1;
free(cvt);
if (!gic->cvt_done) {
ff_post_error(_("Can't insert 'cvt'"),
_("There already exists a 'cvt' table, perhaps legacy. "
"FontForge can use it, but can't make any assumptions on "
"values stored there, so generated instructions will be of "
"lower quality. If legacy hinting is to be scrapped, it is "
"suggested to clear the `cvt` and repeat autoinstructing. "
));
}
}
if (gic->cvt_done)
return;
/* Fallback mode starts here. */
for (i=0; i<gic->bluecnt; i++)
gic->blues[i].cvtindex =
TTF_getcvtval(gic->sf, gic->blues[i].base);
if (gic->stdhw.width != -1)
gic->stdhw.cvtindex =
TTF_getcvtval(gic->sf, gic->stdhw.width);
for (i=0; i<gic->stemsnaphcnt; i++)
gic->stemsnaph[i].cvtindex =
TTF_getcvtval(gic->sf, gic->stemsnaph[i].width);
if (gic->stdvw.width != -1)
gic->stdvw.cvtindex =
TTF_getcvtval(gic->sf, gic->stdvw.width);
for (i=0; i<gic->stemsnapvcnt; i++)
gic->stemsnapv[i].cvtindex =
TTF_getcvtval(gic->sf, gic->stemsnapv[i].width);
}
/* We'll need at least STACK_DEPTH stack levels and a twilight point (and thus
* also a twilight zone). We also currently define some functions in fpgm.
* We must ensure this is indicated in the 'maxp' table.
*
* We also need two storage cells. As we now use SPVFS to set projection
* vector for diagonal hinting, we have to adjust values taken by SPVFS,
* so that diagonals look cleanly in all aspect ratios. Adjustments are
* not trivial to compute, so we do this once (in prep) and store them
* in storage[0] (for X direction) and storage[1] (for Y direction).
*/
static void init_maxp(GlobalInstrCt *gic) {
struct ttf_table *tab = SFFindTable(gic->sf, CHR('m','a','x','p'));
uint16 zones, twpts, store, fdefs, stack;
if ( tab==NULL ) {
tab = chunkalloc(sizeof(struct ttf_table));
tab->next = gic->sf->ttf_tables;
gic->sf->ttf_tables = tab;
tab->tag = CHR('m','a','x','p');
}
if ( tab->len<32 ) {
tab->data = realloc(tab->data,32);
memset(tab->data+tab->len,0,32-tab->len);
tab->len = tab->maxlen = 32;
}
zones = memushort(tab->data, 32, 7*sizeof(uint16));
twpts = memushort(tab->data, 32, 8*sizeof(uint16));
store = memushort(tab->data, 32, 9*sizeof(uint16));
fdefs = memushort(tab->data, 32, 10*sizeof(uint16));
stack = memushort(tab->data, 32, 12*sizeof(uint16));
if (gic->fpgm_done && zones<2) zones=2;
if (gic->fpgm_done && twpts<1) twpts=1;
if (gic->fpgm_done && gic->prep_done && store<2) store=2;
if (gic->fpgm_done && fdefs<22) fdefs=22;
if (stack<STACK_DEPTH) stack=STACK_DEPTH;
memputshort(tab->data, 7*sizeof(uint16), zones);
memputshort(tab->data, 8*sizeof(uint16), twpts);
memputshort(tab->data, 9*sizeof(uint16), store);
memputshort(tab->data,10*sizeof(uint16), fdefs);
memputshort(tab->data,12*sizeof(uint16), stack);
}
/* Other hinting software puts certain actions in FPGM to ease developer's life
* and compress the code. I feel that having a 'standard' library of functions
* could also help FF users.
*
* Caution! This code is heavily relied by autohinting. Any other code should
* be placed below it. It's good to first clear font's hinting tables, then
* autohint it, and then insert user's own code and do the manual hinting of
* glyphs that do need it.
*/
static void init_fpgm(GlobalInstrCt *gic) {
uint8 new_fpgm[] =
{
/* Function 0: position a point within a blue zone (given via cvt).
* Note: in case of successful init of 'cvt' and 'prep' this function
* could be much simpler.
* Syntax: PUSHB_3 point cvt_of_blue 0 CALL
*/
0xb0, // PUSHB_1
0x00, // 0
0x2c, // FDEF
0xb0, // PUSHB_1
0x00, // 0
0x13, // SZP0
0x4b, // MPPEM
0xb0, // PUSHB_1 - under this ppem blues will be specially rounded
GetBlueScale(gic->sf),
0x50, // LT
0x58, // IF
0xb0, // PUSHB_0
0x4a, // 74
0x76, // SROUND - round blues a bit up to grid
0x59, // EIF
0xb0, // PUSHB_1
0x00, // 0
0x23, // SWAP
0x3f, // MIAP[rnd] - blue zone positioned here
0x18, // RTG - round state for overshoots in monochrome mode
0xb0, // PUSHB_1
0x06, // 6
0x2b, // CALL
0x58, // IF
0x3d, // RTDG - round state for overshoots in antialiased mode
0x59, // EIF
0x4b, // MPPEM
0xb0, // PUSHB_1 - under following ppem overshoots will be suppressed
GetBlueScale(gic->sf),
0x50, // LT
0x58, // IF
0x7d, // RDTG - suppress overshoots
0x59, // EIF
0x20, // DUP
0xd4, // MDRP[rp0,rnd,grey]
0xb0, // PUSHB_1
0x01, // 1
0x13, // SZP0
0x2e, // MDAP[no-rnd]
0x18, // RTG
0x2d, // ENDF
/* Function 1: Place given point relatively to previous, maintaining the
* minimum distance. Then call FPGM 12 to check if the point's gridfitted
* position is too far from its original position, and correct it, if necessary.
* Syntax: PUSB_2 point 1 CALL
*/
0xb0, // PUSHB_1
0x01, // 1
0x2c, // FDEF
0x20, // DUP
0xda, // MDRP[rp0,min,white]
0xb0, // PUSHB_1
0x0c, // 12
0x2b, // CALL
0x2d, // ENDF
/* Function 2: Below given ppem, substitute the width with cvt entry.
* Leave the resulting width on the stack. Used as the first step in
* normalizing cvt stems, see normalize_stem().
* Syntax: PUSHX_3 width cvt_index ppem 2 CALL
*/
0xb0, // PUSHB_1
0x02, // 2
0x2c, // FDEF
0x4b, // MPPEM
0x52, // GT
0x58, // IF
0x45, // RCVT
0x23, // SWAP
0x59, // EIF
0x21, // POP
0x2d, // ENDF
/* Function 3: round a stack element as a black distance, respecting
* minimum distance of 1px. This is used for rounding stems after width
* normalization. Often preceeded with SROUND, so finally sets RTG.
* Leaves the rounded width on the stack.
* Syntax: PUSHX_2 width_to_be_rounded 3 CALL
*/
0xb0, // PUSHB_1
0x03, // 3
0x2c, // FDEF
0x69, // ROUND[black]
0x18, // RTG
0x20, // DUP
0xb0, // PUSHB_1
0x40, // 64, that's one pixel as F26Dot6
0x50, // LT
0x58, // IF
0x21, // POP
0xb0, // PUSHB_1
0x40, // 64
0x59, // EIF
0x2d, // ENDF
/* Function 4: Position the second edge of a stem that is not normally
* regularized via cvt (but we snap it to cvt width below given ppem).
* Vertical stems need special round state when not snapped to cvt
* (basically, they are shortened by 0.25px before being rounded).
* Syntax: PUSHX_5 pt cvt_index chg_rp0 ppem 4 CALL
*/
0xb0, // PUSHB_1
0x04, // 4
0x2c, // FDEF
0xb0, // PUSHB_1
0x06, // 6
0x2b, // CALL
0x58, // IF
0x21, // POP
0x23, // SWAP
0x21, // POP
0x7a, // ROFF
0x58, // IF
0xdd, // MDRP[rp0,min,rnd,black]
0x1b, // ELSE
0xcd, // MDRP[min,rnd,black]
0x59, // EIF
0x1b, // ELSE
0x4b, // MPPEM
0x52, // GT
0x58, // IF
0x58, // IF
0xfd, // MIRP[rp0,min,rnd,black]
0x1b, // ELSE
0xed, // MIRP[min,rnd,black]
0x59, // EIF
0x1b, // ELSE
0x23, // SWAP
0x21, // POP
0xb0, // PUSHB_1
0x05, // 5
0x2b, // CALL
0x58, // IF
0xb0, // PUSHB_1
0x46, // 70
0x76, // SROUND
0x59, // EIF
0x58, // IF
0xdd, // MDRP[rp0,min,rnd,black]
0x1b, // ELSE
0xcd, // MDRP[min,rnd,black]
0x59, // EIF
0x59, // EIF
0x59, // EIF
0x18, // RTG
0x2d, // ENDF
/* Function 5: determine if we are hinting vertically. The function
* is crude and it's use is limited to conditions set by SVTCA[].
* Syntax: PUSHB_1 5 CALL; leaves boolean on the stack.
*/
0xb0, // PUSHB_1
0x05, // 5