/
parsettfbmf.c
1379 lines (1281 loc) · 48.1 KB
/
parsettfbmf.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 */
/*
* 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 <fontforge-config.h>
#include "parsettfbmf.h"
#include "bitmapchar.h"
#include "bvedit.h"
#include "fontforge.h"
#include "gfile.h"
#include "gwidget.h"
#include "mem.h"
#include "splinefill.h"
#include "tottf.h"
#include "ttf.h"
#include "ttfspecial.h"
#include "ustring.h"
#include "utype.h"
#include <locale.h>
#include <math.h>
struct ttfsizehead {
int ppem;
int depth;
int firstglyph, endglyph;
int indexSubTableArrayOffset;
int numIndexSubTables;
int ascent, descent;
};
struct bigmetrics {
int height, width;
int hbearingX, hbearingY, hadvance;
int vbearingX, vbearingY, vadvance; /* Small metrics doesn't use this row */
};
/* ************************************************************************** */
/* *********************** Input --- Reading Bitmaps ************************ */
/* ************************************************************************** */
static void ttfreadbmfglyph(FILE *ttf,struct ttfinfo *info,
int32_t offset, int32_t len, struct bigmetrics *metrics, int imageformat,
int gid, BDFFont *bdf) {
BDFChar *bdfc;
BDFRefChar *ref, *prev = NULL;
struct bigmetrics big;
int i,j,ch,l,p, num;
if ( gid>=bdf->glyphcnt )
return;
if ( len==0 ) /* Missing, but encoded, glyph */
return;
if ( offset!=-1 )
fseek(ttf,info->bitmapdata_start+offset,SEEK_SET);
if ( imageformat==1 || imageformat==2 || imageformat==8 ) {
/* I ignore vertical strikes, so all short metrics are horizontal for me*/
big.height = getc(ttf);
big.width = getc(ttf);
big.hbearingX = (signed char) getc(ttf);
big.hbearingY = (signed char) getc(ttf);
big.hadvance = getc(ttf);
big.vbearingX = 0;
big.vbearingY = 0;
big.vadvance = bdf->pixelsize;
/* Sometimes the index format (e.g. 2) already specifies the metrics */
/* Only use the provided metrics if it hasn't already been set */
if (metrics == NULL)
metrics = &big;
if ( imageformat==8 )
/* pad = */ getc(ttf);
} else if ( imageformat==6 || imageformat==7 || imageformat==9 ) {
big.height = getc(ttf);
big.width = getc(ttf);
big.hbearingX = (signed char) getc(ttf);
big.hbearingY = (signed char) getc(ttf);
big.hadvance = getc(ttf);
big.vbearingX = (signed char) getc(ttf);
big.vbearingY = (signed char) getc(ttf);
big.vadvance = getc(ttf);
metrics = &big;
} else if ( imageformat==5 ) {
/* metrics from EBLC */
/* Do nothing here */
if ( metrics==NULL ) {
LogError(_("Unexpected use of bitmap format 5, no metrics are appearant\n"));
info->bad_embedded_bitmap = true;
/*fseek(ttf,len,SEEK_CUR);*/
return;
}
} else {
/* format 3 is obsolete */
/* format 4 is compressed apple and I'm not supporting it (Nor is MS) */
if ( imageformat==3 && !info->obscomplain ) {
LogError(_("This font contains bitmaps in the obsolete format 3 (And I can't read them)\n") );
info->bad_embedded_bitmap = true;
info->obscomplain = true;
} else if ( imageformat==4 ) {
/* Apple doesn't describe it (fully) in their spec. */
/* MS doesn't support it (and doesn't describe) */
/* Adobe doesn't describe it (and says MS doesn't support it) */
LogError(_("This font contains bitmaps in Apple's compressed format 4 (And I don't support that)\n"));
info->cmpcomplain = true;
} else if ( !info->unkcomplain ) {
LogError(_("This font contains bitmaps in a format %d that I've never heard of\n"), imageformat );
info->bad_embedded_bitmap = true;
info->unkcomplain = true;
}
return;
}
bdfc = chunkalloc(sizeof(BDFChar));
if ( info->chars!=NULL ) {
if ( gid>=info->glyph_cnt || info->chars[gid]==NULL ) {
if ( gid>=info->glyph_cnt ) {
int i;
info->chars = realloc(info->chars,(gid+1)*sizeof(SplineChar *));
for ( i=info->glyph_cnt; i<gid; ++i ) {
info->chars[i] = SplineCharCreate(2);
info->chars[i]->orig_pos = gid;
info->chars[i]->unicodeenc = -1;
info->chars[i]->width = info->chars[i]->vwidth = info->emsize;
}
info->glyph_cnt = gid+1;
}
info->chars[gid] = SplineCharCreate(2);
info->chars[gid]->orig_pos = gid;
info->chars[gid]->unicodeenc = -1;
info->chars[gid]->width = info->chars[gid]->vwidth = info->emsize;
}
bdfc->sc = info->chars[gid];
/* in the case of a cid font orig_pos will be the cid, not the gid */
/* We'll need to rearrange the glyph array to match later. We need */
/* gid order until we've fixed up references */
bdfc->orig_pos = info->chars[gid]->orig_pos;
} else {
bdfc->orig_pos = gid;
}
if ( bdf->glyphs[gid]!=NULL ) /* Shouldn't happen of course */
BDFCharFree(bdf->glyphs[gid]);
bdf->glyphs[gid] = bdfc;
bdfc->width = metrics->hadvance;
bdfc->vwidth = metrics->vadvance;
bdfc->xmin = metrics->hbearingX;
bdfc->xmax = bdfc->xmin+metrics->width-1;
bdfc->ymax = metrics->hbearingY-1;
bdfc->ymin = bdfc->ymax-metrics->height+1;
bdfc->refs = NULL;
bdfc->dependents = NULL;
if ( imageformat == 5 ) bdfc->widthgroup = true;
if ( bdf->clut==NULL ) {
bdfc->bytes_per_line = (metrics->width+7)/8;
bdfc->depth = 1;
} else {
bdfc->bytes_per_line = metrics->width;
bdfc->byte_data = true;
bdfc->depth = bdf->clut->clut_len==4 ? 2 : bdf->clut->clut_len==16 ? 4 : 8;
}
if ( imageformat!=8 && imageformat!=9 )
bdfc->bitmap = calloc(metrics->height*bdfc->bytes_per_line,sizeof(uint8_t));
if ( imageformat==8 || imageformat==9 ) {
/* composite */
num = getushort(ttf);
bdfc->bitmap = calloc( 1,sizeof( uint8_t ));
bdfc->bytes_per_line = 1;
for ( i=0; i<num; ++i ) {
ref = calloc( 1,sizeof( BDFRefChar ));
ref->gid = getushort(ttf);
ref->xoff = (int8_t) getc(ttf);
ref->yoff = (int8_t) getc(ttf);
if ( prev == NULL )
bdfc->refs = ref;
else
prev->next = ref;
prev = ref;
}
} else if ( imageformat==1 || imageformat==6 ) {
/* each row is byte aligned */
if ( bdf->clut==NULL || bdf->clut->clut_len==256 ) {
for ( i=0; i<metrics->height; ++i )
fread(bdfc->bitmap+i*bdfc->bytes_per_line,1,bdfc->bytes_per_line,ttf);
} else if ( bdf->clut->clut_len==4 ) {
for ( i=0; i<metrics->height; ++i ) {
for ( j=0; j<metrics->width; j+=4 ) {
ch = getc(ttf);
bdfc->bitmap[i*bdfc->bytes_per_line+j] = (ch>>6);
if ( j+1 < metrics->width )
bdfc->bitmap[i*bdfc->bytes_per_line+j+1] = (ch>>4)&3;
if ( j+2 < metrics->width )
bdfc->bitmap[i*bdfc->bytes_per_line+j+2] = (ch>>2)&3;
if ( j+3 < metrics->width )
bdfc->bitmap[i*bdfc->bytes_per_line+j+3] = ch&3;
}
}
} else if ( bdf->clut->clut_len==16 ) {
for ( i=0; i<metrics->height; ++i ) {
for ( j=0; j<metrics->width; j+=2 ) {
ch = getc(ttf);
bdfc->bitmap[i*bdfc->bytes_per_line+j] = (ch>>4);
if ( j+1 < metrics->width )
bdfc->bitmap[i*bdfc->bytes_per_line+j+1] = ch&0xf;
}
}
}
} else if ( imageformat==2 || imageformat==5 || imageformat==7 ) {
/* each row is bit aligned, but each glyph is byte aligned */
if ( bdf->clut==NULL ) {
for ( i=0; i<(metrics->height*metrics->width+7)/8; ++i ) {
ch = getc(ttf);
for ( j=0; j<8; ++j ) {
l = (i*8+j)/metrics->width;
p = (i*8+j)%metrics->width;
if ( l<metrics->height && (ch&(1<<(7-j))) )
bdfc->bitmap[l*bdfc->bytes_per_line+(p>>3)] |= (1<<(7-(p&7)));
}
}
} else if ( bdf->clut->clut_len==256 ) {
/* well, yeah, it's bit aligned, but since pixels are bytes, it's byte aligned */
for ( i=0; i<metrics->height; ++i )
fread(bdfc->bitmap+i*bdfc->bytes_per_line,1,bdfc->bytes_per_line,ttf);
} else if ( bdf->clut->clut_len==4 ) {
for ( i=0; i<metrics->height*metrics->width; i+=4 ) {
ch = getc(ttf);
bdfc->bitmap[(i/metrics->width)*bdfc->bytes_per_line+i%metrics->width] = (ch>>6);
if ( i+1 < metrics->width*metrics->height )
bdfc->bitmap[((i+1)/metrics->width)*bdfc->bytes_per_line+(i+1)%metrics->width] = (ch>>4)&3;
if ( i+2 < metrics->width*metrics->height )
bdfc->bitmap[((i+2)/metrics->width)*bdfc->bytes_per_line+(i+2)%metrics->width] = (ch>>2)&3;
if ( i+3 < metrics->width*metrics->height )
bdfc->bitmap[((i+3)/metrics->width)*bdfc->bytes_per_line+(i+3)%metrics->width] = ch&3;
}
} else if ( bdf->clut->clut_len==16 ) {
for ( i=0; i<metrics->height*metrics->width; i+=2 ) {
ch = getc(ttf);
bdfc->bitmap[(i/metrics->width)*bdfc->bytes_per_line+i%metrics->width] = (ch>>4);
if ( i+1 < metrics->width*metrics->height )
bdfc->bitmap[((i+1)/metrics->width)*bdfc->bytes_per_line+(i+1)%metrics->width] = ch&0xf;
}
}
}
ff_progress_next();
}
static void BdfCRefFixup(BDFFont *bdf, int gid, int *warned, struct ttfinfo *info) {
BDFChar *bdfc = bdf->glyphs[gid], *bdfcRefd;
BDFRefChar *head, *prev = NULL, *next;
for ( head=bdfc->refs; head!=NULL; head=next ) {
bdfcRefd = head->gid < bdf->glyphcnt ? bdf->glyphs[head->gid] : NULL;
next = head->next;
if ( bdfcRefd != NULL ) {
head->bdfc = bdfcRefd;
BCMakeDependent( bdfc, bdfcRefd );
prev = head;
/* According to the TTF spec, the xOffset and yOffset values specify */
/* the top-left corner position of the component in the composite. */
/* In our program it is more convenient to manipulate values specified */
/* relatively to the original position of the reference's parent glyph.*/
/* So we have to perform this conversion each time we read or write */
/* an embedded TTF bitmap. */
head->xoff = head->xoff - bdfcRefd->xmin + bdfc->xmin;
head->yoff = bdfc->ymax - bdfcRefd->ymax - head->yoff;
} else {
if ( !*warned ) {
/* Glyphs aren't named yet */
LogError(_("Glyph %d in bitmap strike %d pixels refers to a missing glyph (%d)"),
gid, bdf->pixelsize, head->gid );
info->bad_embedded_bitmap = true;
*warned = true;
}
/* Remove bad reference node from the singly-linked list */
if ( prev == NULL )
bdfc->refs = head->next;
else
prev->next = head->next;
free( head );
}
}
}
static void BdfRefFixup(BDFFont *bdf, struct ttfinfo *info) {
int i;
int warned = false;
BDFChar *bc;
/* Associate each reference with its parent glyph and recalculate */
/* x and y offsets */
for ( i=0; i<bdf->glyphcnt; ++i )
if ( bdf->glyphs[i]!=NULL && bdf->glyphs[i]->refs != NULL )
BdfCRefFixup( bdf,i,&warned,info );
/* When offsets are recalculated, reset the composite glyph bounding */
/* box to zero values */
/* For glyphs which were stored in subtables format 5 compress bitmaps */
/* to the image size (we could not do this before recalculating reference */
/* offsets) */
for ( i=0; i<bdf->glyphcnt; ++i ) if (( bc = bdf->glyphs[i] ) != NULL ) {
if ( bc->refs != NULL ) {
bdf->glyphs[i]->xmin = 0; bdf->glyphs[i]->xmax = 0;
bdf->glyphs[i]->ymin = 0; bdf->glyphs[i]->ymax = 0;
} else if ( bc->widthgroup ) {
BCCompressBitmap( bc );
bc->widthgroup = false;
}
}
}
static void BdfCleanup(BDFFont *bdf,struct ttfinfo *info) {
/* for gulim (and doubtless others) bitmap references do not match */
/* outline glyph references and there are a bunch of extra glyphs */
/* at the end of the list that we can now get rid of */
/* As bitmap references are directly supported now, we no longer */
/* can ignore the extra glyphs in Gulim, and instead create */
/* a faked spline char for each of them. Thus the situation where */
/* info->glyph_cnt is less than bdf->glyphcnt is no longer */
/* possible. */
/* For cid keyed fonts we want to order things by cid rather than */
/* by gid so we may also need to move things around */
int i, cnt;
BDFChar **glyphs, *bdfc;
if ( info->subfonts==NULL ) {
if ( info->glyph_cnt<bdf->glyphcnt ) {
for ( i=info->glyph_cnt ; i<bdf->glyphcnt; ++i )
BDFCharFree(bdf->glyphs[i]);
bdf->glyphs = realloc(bdf->glyphs,info->glyph_cnt*sizeof(BDFChar *));
bdf->glyphcnt = info->glyph_cnt;
}
} else {
cnt = 0;
for ( i=0; i<info->subfontcnt; ++i )
if ( info->subfonts[i]->glyphcnt > cnt )
cnt = info->subfonts[i]->glyphcnt;
glyphs = calloc(cnt,sizeof(BDFChar *));
for ( i=0; i<bdf->glyphcnt; ++i ) if ( (bdfc = bdf->glyphs[i])!=NULL ) {
if ( bdfc->orig_pos<cnt )
glyphs[ bdfc->orig_pos ] = bdfc;
else
BDFCharFree(bdfc);
}
free(bdf->glyphs);
bdf->glyphs = glyphs;
bdf->glyphcnt = cnt;
}
}
static void readttfbitmapfont(FILE *ttf,struct ttfinfo *info,
struct ttfsizehead *head, BDFFont *bdf) {
int i, j, g;
int indexformat, imageformat, size, num, moreoff;
int32_t offset, *glyphoffsets, *glyphs, loc;
int first, last;
struct bigmetrics big;
fseek(ttf,info->bitmaploc_start+head->indexSubTableArrayOffset,SEEK_SET);
for ( j=0; j<head->numIndexSubTables; ++j ) {
first = getushort(ttf);
last = getushort(ttf);
moreoff = getlong(ttf);
if ( last<first ) {
LogError(_("Bad format of subtable %d (of %d) in bloc/EBLC of strike with pixelsize=%d. First=%d, last=%d.\n"),
j, head->numIndexSubTables, bdf->pixelsize, first, last );
info->bad_embedded_bitmap = true;
continue;
}
loc = ftell(ttf);
fseek(ttf,info->bitmaploc_start+head->indexSubTableArrayOffset+moreoff,SEEK_SET);
indexformat=getushort(ttf);
imageformat=getushort(ttf);
offset = getlong(ttf);
switch ( indexformat ) {
case 1: case 3:
glyphoffsets = malloc((last-first+2)*sizeof(int32_t));
for ( i=0; i<(last-first+2); ++i )
glyphoffsets[i] = indexformat==3?getushort(ttf):getlong(ttf);
if ( indexformat==3 && ((last-first)&1) )
getushort(ttf);
for ( i=0; i<=last-first; ++i ) {
if ( info->inuse==NULL || info->inuse[i+first] )
ttfreadbmfglyph(ttf,info,offset+glyphoffsets[i],
glyphoffsets[i+1]-glyphoffsets[i],NULL,
imageformat,i+first,bdf);
}
free(glyphoffsets);
break;
case 2:
size = getlong(ttf);
big.height = getc(ttf);
big.width = getc(ttf);
big.hbearingX = (signed char) getc(ttf);
big.hbearingY = (signed char) getc(ttf);
big.hadvance = getc(ttf);
big.vbearingX = (signed char) getc(ttf);
big.vbearingY = (signed char) getc(ttf);
big.vadvance = getc(ttf);
for ( i=first; i<=last; ++i ) {
if ( info->inuse==NULL || info->inuse[i] ) {
ttfreadbmfglyph(ttf,info,offset,
size,&big,
imageformat,i,bdf);
offset += size;
} else {
fseek(ttf,size,SEEK_CUR);
offset = -1;
}
}
break;
case 4:
num = getlong(ttf);
glyphoffsets = malloc((num+1)*sizeof(int32_t));
glyphs = malloc((num+1)*sizeof(int32_t));
for ( g=0; g<num+1; ++g ) {
glyphs[g] = getushort(ttf);
glyphoffsets[g] = getushort(ttf);
}
for ( i=0, g=0; i<=last-first; ++i, ++g ) {
if ( (info->inuse==NULL || info->inuse[i+first]) && g<num )
ttfreadbmfglyph(ttf,info,offset+glyphoffsets[g],
glyphoffsets[g+1]-glyphoffsets[g],NULL,
imageformat,glyphs[g],bdf);
}
free(glyphoffsets);
free(glyphs);
break;
case 5:
size = getlong(ttf);
big.height = getc(ttf);
big.width = getc(ttf);
big.hbearingX = (signed char) getc(ttf);
big.hbearingY = (signed char) getc(ttf);
big.hadvance = getc(ttf);
big.vbearingX = (signed char) getc(ttf);
big.vbearingY = (signed char) getc(ttf);
big.vadvance = getc(ttf);
num = getlong(ttf);
glyphs = malloc((num+1)*sizeof(int32_t));
for ( g=0; g<num; ++g ) {
glyphs[g] = getushort(ttf);
}
if ( num&1 )
getushort(ttf); /* padding */
for ( i=first, g=0; i<=last; ++i, ++g ) {
if ( (info->inuse==NULL || info->inuse[i+first]) && g<num )
ttfreadbmfglyph(ttf,info,offset,
size,&big,
imageformat,glyphs[g],bdf);
offset = -1;
}
free(glyphs);
break;
default:
LogError(_("Didn't understand index format: %d\n"), indexformat );
info->bad_embedded_bitmap = true;
break;
}
fseek(ttf,loc,SEEK_SET);
}
BdfRefFixup(bdf,info);
BdfCleanup(bdf,info);
}
void TTFLoadBitmaps(FILE *ttf,struct ttfinfo *info,int onlyone) {
int i, cnt, j, k, good, glyphcnt;
int bigval, biggest;
struct ttfsizehead *sizes;
BDFFont *bdf, *last;
const char **choices;
char *sel;
char buf[300];
char *buttons[3];
buttons[0] = _("_Yes");
buttons[1] = _("_No");
buttons[2] = NULL;
fseek(ttf,info->bitmaploc_start,SEEK_SET);
/* version = */ getlong(ttf); /* Had better be 0x00020000, or 2.0 */
cnt = getlong(ttf);
sizes = malloc(cnt*sizeof(struct ttfsizehead));
/* we may not like all the possible bitmaps. Some might be designed for */
/* non-square pixels, others might be color, others might be */
/* vertical data. So only pick out the ones we can use */
bigval = biggest = -1;
for ( i = j = 0; i<cnt; ++i ) {
if ( feof(ttf)) {
IError( "End of file while reading embedded bitmaps" );
break;
}
good = true;
sizes[j].indexSubTableArrayOffset = getlong(ttf);
/* size = */ getlong(ttf);
sizes[j].numIndexSubTables = getlong(ttf);
if ( /* colorRef = */ getlong(ttf)!=0 )
good = false;
sizes[j].ascent = (int8_t)getc(ttf);
sizes[j].descent = (int8_t)getc(ttf);
for ( k=0; k<12-2; ++k ) getc(ttf); /* Horizontal Line Metrics */
for ( k=0; k<12; ++k ) getc(ttf); /* Vertical Line Metrics */
sizes[j].firstglyph = getushort(ttf);
sizes[j].endglyph = getushort(ttf);
sizes[j].ppem = getc(ttf); /* X */
if ( /* ppemY */ getc(ttf) != sizes[j].ppem )
good = false;
if ( (sizes[j].depth = getc(ttf)) != 1 && sizes[j].depth!=2 &&
sizes[j].depth!=4 && sizes[j].depth!=8 )
good = false;
if ( /* flags */ !(getc(ttf)&1) ) /* !Horizontal */
good = false;
if ( good ) {
if ( sizes[j].ppem > bigval ) {
biggest = j;
bigval = sizes[j].ppem;
}
}
j += good;
}
cnt = j;
if ( cnt==0 ) {
free(sizes);
return;
}
/* Ask user which (if any) s/he is interested in */
choices = calloc(cnt+1,sizeof(char *));
sel = calloc(cnt,sizeof(char));
for ( i=0; i<cnt; ++i ) {
if ( sizes[i].depth==1 )
sprintf(buf,"%d", sizes[i].ppem );
else
sprintf(buf,"%d@%d", sizes[i].ppem, sizes[i].depth );
choices[i] = copy(buf);
}
/* When loading a ttf font with only bitmaps, and there's only one strike */
/* then just load that strike. Don't ask */
if ( info->glyphlocations_start==0 && info->cff_start==0 && cnt==1 )
sel[0] = true;
else if ( no_windowing_ui ) {
if ( onlyone ) {
biggest=-1; // We may not find one.
for ( i=1; i<cnt; ++i )
if ( sizes[i].depth==1 && (biggest < 0 || sizes[i].ppem>sizes[biggest].ppem) )
biggest = i;
sel[biggest] = true;
} else {
biggest=-1;
for ( i=0; i<cnt; ++i ) {
sel[i] = true;
if ( sizes[i].depth==1 && (biggest < 0 || sizes[i].ppem>sizes[biggest].ppem) )
biggest = i;
}
}
} else if ( onlyone ) {
biggest=ff_choose(_("Load Bitmap Fonts"), choices,cnt,biggest,
_("Do you want to load the bitmap fonts embedded in this true/open type file?\n(And if so, which)"));
if ( biggest!=-1 ) sel[biggest] = true;
} else {
biggest = ff_choose_multiple(_("Load Bitmap Fonts"), choices,sel,cnt,buttons,
_("Do you want to load the bitmap fonts embedded in this true/open type file?\n(And if so, which)"));
}
for ( i=0; i<cnt; ++i ) free( (unichar_t *) (choices[i]));
free(choices);
if ( biggest<0 ) { /* Cancelled */
free(sizes); free(sel);
return;
}
/* Remove anything not selected */
for ( i=j=0; i<cnt; ++i ) {
if ( sel[i] )
sizes[j++] = sizes[i];
}
cnt = j;
ff_progress_change_stages(3+cnt);
info->bitmaps = last = NULL;
if ( info->subfonts==NULL )
glyphcnt = info->glyph_cnt;
else {
glyphcnt = 0;
for ( i=0; i<info->subfontcnt; ++i )
if ( info->subfonts[i]->glyphcnt > glyphcnt )
glyphcnt = info->subfonts[i]->glyphcnt;
}
for ( i=0; i<cnt; ++i ) {
bdf = calloc(1,sizeof(BDFFont));
/* In cid fonts fontforge stores things by cid rather than gid */
bdf->glyphcnt = glyphcnt;
if ( sizes[i].endglyph > bdf->glyphcnt )
bdf->glyphcnt = sizes[i].endglyph+1; /* Important if we have reference glyphs */
bdf->glyphmax = bdf->glyphcnt;
bdf->glyphs = calloc(bdf->glyphcnt,sizeof(BDFChar *));
bdf->pixelsize = sizes[i].ppem;
if ( sizes[i].ppem == sizes[i].ascent - sizes[i].descent )
bdf->ascent = sizes[i].ascent;
else
bdf->ascent = (info->ascent*bdf->pixelsize + info->emsize/2)/info->emsize;
bdf->descent = bdf->pixelsize - bdf->ascent;
bdf->res = -1;
if ( sizes[i].depth!=1 )
BDFClut(bdf,1<<(sizes[i].depth/2));
if ( last==NULL )
info->bitmaps = bdf;
else
last->next = bdf;
last = bdf;
snprintf(buf,sizeof(buf),_("%d pixel bitmap"), sizes[i].ppem );
ff_progress_change_line2(buf);
readttfbitmapfont(ttf,info,&sizes[i],bdf);
ff_progress_next_stage();
}
free(sizes); free(sel);
ttf_bdf_read(ttf,info);
}
/* ************************************************************************** */
/* *********************** Output --- Writing Bitmaps *********************** */
/* ************************************************************************** */
static BDFChar glyph0, glyph1, glyph2;
static SplineChar sc0, sc1, sc2;
static struct bdfcharlist bl[3];
static struct bdfcharlist *BDFAddDefaultGlyphs(BDFFont *bdf, int format) {
/* when I dump out the glyf table I add 3 glyphs at the start. One is glyph*/
/* 0, the one used when there is no match, and the other two are variants*/
/* on space. These might or might not be in the font. Add the ones that */
/* don't exist */
/* Werner tells me he doesn't want them added. He thinks sparse bitmaps */
/* are reasonable. Hadn't occurred to me. So I wanted complete strikes */
/* Perhaps a better approach is to give them fake bitmaps if we gave them */
/* fake outlines, so look at the spline font not the bitmap font for the */
/* magic glyphs */
int width, w, i, j, bit, blpos=0;
SplineFont *sf = bdf->sf;
int notdefpos = -1, nullpos = -1, nmretpos = -1;
int extracnt = format==ff_otf || format==ff_otfcid ? 1 : 3;
width = 0x7ffff;
for ( i=0; i<bdf->glyphcnt; ++i ) if ( bdf->glyphs[i]!=NULL ) {
if ( width == 0x7ffff )
width = bdf->glyphs[i]->width;
else if ( width!=bdf->glyphs[i]->width ) {
width = 0x7fffe;
break;
}
}
for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL ) {
int ttf_glyph = sf->glyphs[i]->ttf_glyph;
if ( ttf_glyph==0 )
notdefpos = i;
else if ( ttf_glyph==1 )
nullpos = i;
else if ( ttf_glyph==2 )
nmretpos = i;
}
if ( width>0x70000 )
/* Proportional */
width = bdf->pixelsize/3;
memset(bl,0,sizeof(bl));
if ( notdefpos==-1 ) {
sc0.name = ".notdef";
/* sc0.ttf_glyph = 0; */ /* already done */
sc0.parent = sf;
if ( width<4 ) w=4; else w=width;
sc0.width = w*(sf->ascent+sf->descent)/bdf->pixelsize;
sc0.widthset = true;
sc0.vwidth = (sf->ascent+sf->descent);
glyph0.sc = &sc0;
glyph0.width = w;
glyph0.vwidth = bdf->pixelsize;
if ( BDFDepth(bdf)!=1 ) {
glyph0.bytes_per_line = 1; /* Needs to be 1 or BCRegularizeBitmap gets upset */
glyph0.ymax = 1;
glyph0.bitmap = calloc(8,1);
} else {
glyph0.xmin = (w==4)?0:1;
glyph0.xmax = w-1;
glyph0.ymin = 0;
glyph0.ymax = 8*bdf->ascent/10;
glyph0.bytes_per_line = (glyph0.xmax-glyph0.xmin+8)/8;
glyph0.bitmap = calloc((glyph0.ymax-glyph0.ymin+1)*glyph0.bytes_per_line,1);
j = (glyph0.xmax-glyph0.xmin)>>3;
bit = 0x80>>((glyph0.xmax-glyph0.xmin)&7);
for ( i=0; i<=glyph0.ymax; ++i ) {
glyph0.bitmap[i*glyph0.bytes_per_line] |= 0x80;
glyph0.bitmap[i*glyph0.bytes_per_line+j] |= bit;
}
for ( i=0; i<glyph0.xmax-glyph0.xmin; ++i ) {
glyph0.bitmap[i>>3] |= (0x80>>(i&7));
glyph0.bitmap[glyph0.ymax*glyph0.bytes_per_line+(i>>3)] |= (0x80>>(i&7));
}
}
bl[blpos++].bc = &glyph0;
}
if ( extracnt==3 ) {
sc1.ttf_glyph = 1;
sc2.ttf_glyph = 2;
sc1.name = ".null"; sc2.name = "nonmarkingreturn";
sc1.parent = sc2.parent = sf;
sc1.width = sc2.width = width*(sf->ascent+sf->descent)/bdf->pixelsize;
sc1.widthset = sc2.widthset = 1;
sc1.vwidth = sc2.vwidth = (sf->ascent+sf->descent);
glyph1.sc = &sc1; glyph2.sc = &sc2;
/* xmin and so forth are zero, and are already clear */
glyph1.width = glyph2.width = width;
glyph1.vwidth = glyph2.vwidth = bdf->pixelsize;
glyph1.bytes_per_line = glyph2.bytes_per_line = 1; /* Needs to be 1 or BCRegularizeBitmap gets upset */
glyph1.bitmap = glyph2.bitmap = (uint8_t *) "";
glyph1.orig_pos = 1; glyph2.orig_pos = 2;
if ( nullpos==-1 ) {
if ( blpos!=0 )
bl[blpos-1].next = &bl[blpos];
bl[blpos++].bc = &glyph1;
}
if ( nmretpos==-1 ) {
if ( blpos!=0 )
bl[blpos-1].next = &bl[blpos];
bl[blpos++].bc = &glyph2;
}
}
return( blpos==0 ? NULL : &bl[0] );
}
static void BDFCleanupDefaultGlyphs(BDFFont *bdf) {
free(glyph0.bitmap);
glyph0.bitmap = NULL;
}
static void ttfdumpsmallmetrics(FILE *bdat, BDFChar *bc) {
/* dump small metrics */
putc(bc->ymax-bc->ymin+1,bdat); /* height */
putc(bc->xmax-bc->xmin+1,bdat); /* width */
putc(bc->xmin,bdat); /* horiBearingX */
putc(bc->ymax+1,bdat); /* horiBearingY */
putc(bc->width,bdat); /* advance width */
}
static void ttfdumpbigmetrics(FILE *bdat, BDFChar *bc) {
/* dump big metrics */
putc(bc->ymax-bc->ymin+1,bdat); /* height */
putc(bc->xmax-bc->xmin+1,bdat); /* width */
putc(bc->xmin,bdat); /* horiBearingX */
putc(bc->ymax+1,bdat); /* horiBearingY */
putc(bc->width,bdat); /* advance width */
putc(-bc->width/2,bdat); /* vertBearingX */
putc(0,bdat); /* vertBearingY */
putc(bc->vwidth,bdat); /* vert advance width */
}
static int32_t ttfdumpf1_6bchar(FILE *bdat, BDFChar *bc,BDFFont *bdf) {
/* format 1 character dump. small metrics, byte aligned data */
int32_t pos = ftell(bdat);
int r,c,val;
if ( bdf->sf->hasvmetrics )
ttfdumpbigmetrics(bdat,bc);
else
ttfdumpsmallmetrics(bdat,bc);
/* dump image */
for ( r=0; r<=bc->ymax-bc->ymin; ++r ) {
if ( bdf->clut==NULL || bdf->clut->clut_len==256 ) {
for ( c=0; c<bc->bytes_per_line; ++c )
putc(bc->bitmap[r*bc->bytes_per_line+c],bdat);
} else if ( bdf->clut->clut_len==4 ) {
for ( c=0; c<bc->bytes_per_line; c+=2 ) {
val = bc->bitmap[r*bc->bytes_per_line+c]<<4;
if ( c+1<bc->bytes_per_line )
val += bc->bitmap[r*bc->bytes_per_line+c+1];
putc(val,bdat);
}
} else {
for ( c=0; c<bc->bytes_per_line; c+=4 ) {
val = bc->bitmap[r*bc->bytes_per_line+c]<<6;
if ( c+1<bc->bytes_per_line )
val += bc->bitmap[r*bc->bytes_per_line+c+1]<<4;
if ( c+2<bc->bytes_per_line )
val += bc->bitmap[r*bc->bytes_per_line+c+2]<<2;
if ( c+3<bc->bytes_per_line )
val += bc->bitmap[r*bc->bytes_per_line+c+3];
putc(val,bdat);
}
}
}
return( pos );
}
static int32_t ttfdumpf2_7bchar(FILE *bdat, BDFChar *bc, BDFFont *bdf,int do_metrics) {
/* format 2 character dump. small metrics, bit aligned data */
int32_t pos = ftell(bdat);
int r,c,ch,bit,sh;
if ( do_metrics ) {
if ( bdf->sf->hasvmetrics )
ttfdumpbigmetrics(bdat,bc);
else
ttfdumpsmallmetrics(bdat,bc);
}
/* dump image */
ch = 0; bit = 0x80; sh=7;
for ( r=0; r<=bc->ymax-bc->ymin; ++r ) {
for ( c = 0; c<=bc->xmax-bc->xmin; ++c ) {
if ( bdf->clut==NULL ) {
if ( bc->bitmap[r*bc->bytes_per_line+(c>>3)] & (1<<(7-(c&7))) )
ch |= bit;
bit >>= 1;
} else if ( bdf->clut->clut_len==4 ) {
ch |= (bc->bitmap[r*bc->bytes_per_line+c]<<(sh-1));
sh-=2;
bit >>= 2;
} else {
ch |= (bc->bitmap[r*bc->bytes_per_line+c]<<(sh-3));
sh-=4;
bit >>= 4;
}
if ( bit==0 ) {
putc(ch,bdat);
ch = 0;
sh = 7;
bit = 0x80;
}
}
}
if ( bit!=0x80 )
putc(ch,bdat);
return( pos );
}
static int32_t ttfdumpf8_9bchar(FILE *bdat, BDFChar *bc,BDFFont *bdf) {
/* format 8 character dump. small metrics, component data */
int32_t pos = ftell(bdat);
int numc = 0;
BDFRefChar *head;
if ( bdf->sf->hasvmetrics )
ttfdumpbigmetrics(bdat,bc);
else {
ttfdumpsmallmetrics(bdat,bc);
putc( 0,bdat );
}
/* dump component data */
for ( head = bc->refs; head!=NULL; head=head->next )
numc++;
putshort( bdat,numc );
for ( head = bc->refs; head!=NULL; head=head->next ) {
putshort( bdat,head->bdfc->sc->ttf_glyph );
putc( head->bdfc->xmin - bc->xmin + head->xoff,bdat );
putc( bc->ymax - head->bdfc->ymax - head->yoff,bdat );
}
return( pos );
}
struct sbitLineMetrics {
int8_t ascender;
int8_t descender;
uint8_t widthMax;
int8_t caretRise;
int8_t caretRun;
int8_t caretOff;
int8_t minoriginsb;
int8_t minAdvancesb;
int8_t maxbeforebl;
int8_t minafterbl;
int8_t pad1, pad2;
};
struct bitmapSizeTable {
int32_t subtableoffset;
int32_t tablesize;
int32_t numsubtables;
int32_t colorRef;
struct sbitLineMetrics hori, vert;
uint16_t startGlyph;
uint16_t endGlyph;
uint8_t ppemX;
uint8_t ppemY;
uint8_t bitdepth;
int8_t flags;
struct bitmapSizeTable *next;
unsigned int error: 1;
};
struct indexarray {
uint16_t first;
uint16_t last;
uint32_t additionalOffset;
struct indexarray *next;
};
static void FillLineMetrics(struct bitmapSizeTable *size,BDFFont *bdf) {
int i, first;
BDFChar *bc;
memset(&size->hori,0,sizeof(struct sbitLineMetrics));
memset(&size->vert,0,sizeof(struct sbitLineMetrics));
/* I shall ignore vertical metrics */
size->hori.caretRise = 1; /* other caret fields may be 0 */
size->vert.caretRise = 1; /* other caret fields may be 0 */
first = true;
for ( i=0; i<bdf->glyphcnt ; ++i ) if ( (bc=bdf->glyphs[i])!=NULL ) {
if ( first ) {
/* size->hori.ascender = bc->ymax+1;*/
/* size->hori.descender = bc->ymin; */
size->hori.widthMax = bc->xmax-bc->xmin+1;
size->hori.minoriginsb = bc->xmin;
size->hori.minAdvancesb = bc->width-bc->xmax;
size->hori.minafterbl = bc->ymin;
size->hori.maxbeforebl = bc->ymax;
first = false;
} else {
/* if ( bc->ymax+1 > size->hori.ascender ) size->hori.ascender = bc->ymax+1; */
/* if ( bc->ymin < size->hori.descender ) size->hori.descender = bc->ymin; */
if ( bc->xmax-bc->xmin+1 > size->hori.widthMax ) size->hori.widthMax = bc->xmax-bc->xmin+1;
if ( bc->xmin < size->hori.minoriginsb ) size->hori.minoriginsb = bc->xmin;
if ( bc->width-bc->xmax < size->hori.minAdvancesb ) size->hori.minAdvancesb = bc->width-bc->xmax;
if ( bc->ymin < size->hori.minafterbl ) size->hori.minafterbl = bc->ymin;
if ( bc->ymax+1 > size->hori.maxbeforebl ) size->hori.maxbeforebl = bc->ymax+1;
}
}
size->hori.ascender = BdfPropHasInt(bdf,"FONT_ASCENT",bdf->ascent);
size->hori.descender = -BdfPropHasInt(bdf,"FONT_DESCENT",bdf->descent);
/* There are some very cryptic pictures supposed to document the meaning */
/* of the metrics fields. MS and Apple use the same picture. The data */
/* present in font files do not match my understanding of those pictures */
/* But that's ok because different font files contain wildly different */
/* values for the same data, so they don't match each other either */
/* GRRRRR */
/* Apple's ftxvalidator gets unhappy if there are no vertical line metrics*/
/* so even though I've no idea what they should be, I'll include something*/
/* Apple seems to say that the vertical ascender/descender are half the */
/* pixel size (which makes sense), but MS does something else */
size->vert.ascender = bdf->pixelsize/2;
size->vert.descender = -bdf->pixelsize/2;
size->vert.widthMax = bdf->pixelsize;
}
static int HasOutputtableBitmap( BDFChar *bc ) {
int i;
for ( i=0; i<bc->bytes_per_line * ( bc->ymax - bc->ymin + 1 ); i++ ) {
if ( bc->bitmap[i] != 0 )
return( true );
}
return( false );
}
static void BCPreserveAndExpand( BDFChar *bc,IBounds *ib ) {
int bmp_width = ( bc->ymax - bc->ymin + 1 );
bc->backup = calloc( 1,sizeof( BDFFloat ));
bc->backup->bytes_per_line = bc->bytes_per_line;
bc->backup->xmin = bc->xmin; bc->backup->xmax = bc->xmax;
bc->backup->ymin = bc->ymin; bc->backup->ymax = bc->ymax;
bc->backup->bitmap = calloc( bc->bytes_per_line * bmp_width,sizeof( uint8_t ));
memcpy( bc->backup->bitmap,bc->bitmap,bc->bytes_per_line * bmp_width );
BCExpandBitmapToEmBox( bc,ib->minx,ib->miny,ib->maxx,ib->maxy );
}
static void DetectWidthGroups( struct glyphinfo *gi,BDFFont *bdf,int apple ) {
BDFChar *bc, *bc2;
IBounds ib, ib2;
int i, j, cnt, final;
for ( i=0; i<gi->gcnt; ++i ) if ( gi->bygid[i] >= 0 && gi->bygid[i] < bdf->glyphcnt && bdf->glyphs[gi->bygid[i]]!=NULL )