-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
otf2bdf.c
1596 lines (1425 loc) · 43.8 KB
/
otf2bdf.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 2008 Department of Mathematical Sciences, New Mexico State University
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* DEPARTMENT OF MATHEMATICAL SCIENCES OR NEW MEXICO STATE UNIVERSITY BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#else
#include <stdlib.h>
#include <unistd.h>
#endif
#include <string.h>
#include <ft2build.h>
#include FT_GLYPH_H
#include FT_SFNT_NAMES_H
#include FT_TRUETYPE_TABLES_H
/*
* Include the remapping support.
*/
#include "remap.h"
/**************************************************************************
*
* Macros.
*
**************************************************************************/
/*
* The version of otf2bdf.
*/
#define OTF2BDF_VERSION "3.0"
/*
* Set the default values used to generate a BDF font.
*/
#ifndef DEFAULT_PLATFORM_ID
#define DEFAULT_PLATFORM_ID 3
#endif
#ifndef DEFAULT_ENCODING_ID
#define DEFAULT_ENCODING_ID 1
#endif
#ifndef DEFAULT_POINT_SIZE
#define DEFAULT_POINT_SIZE 12
#endif
#ifndef DEFAULT_RESOLUTION
#define DEFAULT_RESOLUTION 100
#endif
/*
* Used as a fallback for XLFD names where the character set/encoding can not
* be determined.
*/
#ifndef DEFAULT_XLFD_CSET
#define DEFAULT_XLFD_CSET "-FontSpecific-0"
#endif
/*
* nameID macros for getting strings from the OT font.
*/
#define BDFOTF_COPYRIGHT_STRING 0
#define BDFOTF_FAMILY_STRING 1
#define BDFOTF_SUBFAMILY_STRING 2
#define BDFOTF_UNIQUEID_STRING 3
#define BDFOTF_FULLNAME_STRING 4
#define BDFOTF_VENDOR_STRING 5
#define BDFOTF_POSTSCRIPT_STRING 6
#define BDFOTF_TRADEMARK_STRING 7
/*
* String names for the string indexes. Used for error messages.
*/
static char *string_names[] = {
"\"Copyright\"",
"\"Family\"",
"\"SubFamily\"",
"\"Unique ID\"",
"\"Full Name\"",
"\"Vendor\"",
"\"Postscript Name\"",
"\"Trademark\""
};
#if 0
#define TTF_COPYRIGHT 0
#define TTF_TYPEFACE 1
#define TTF_PSNAME 6
#endif
#ifndef MAX
#define MAX(h,i) ((h) > (i) ? (h) : (i))
#endif
#ifndef MIN
#define MIN(l,o) ((l) < (o) ? (l) : (o))
#endif
/**************************************************************************
*
* General globals set from command line.
*
**************************************************************************/
/*
* The program name.
*/
static char *prog;
/*
* The flag indicating whether messages should be printed or not.
*/
static int verbose = 0;
/*
* Flags used when loading glyphs.
*/
static int load_flags = FT_LOAD_DEFAULT;
/*
* The default platform and encoding ID's.
*/
static int pid = DEFAULT_PLATFORM_ID;
static int eid = DEFAULT_ENCODING_ID;
/*
* Default point size and resolutions.
*/
static int point_size = DEFAULT_POINT_SIZE;
static int hres = DEFAULT_RESOLUTION;
static int vres = DEFAULT_RESOLUTION;
/*
* The user supplied foundry name to use in the XLFD name.
*/
static char *foundry_name = 0;
/*
* The user supplied typeface name to use in the XLFD name.
*/
static char *face_name = 0;
/*
* The user supplied weight name to use in the XLFD name.
*/
static char *weight_name = 0;
/*
* The user supplied slant name to use in the XLFD name.
*/
static char *slant_name = 0;
/*
* The user supplied width name to use in the XLFD name.
*/
static char *width_name = 0;
/*
* The user supplied additional style name to use in the XLFD name.
*/
static char *style_name = 0;
/*
* The user supplied spacing (p = proportional, c = character cell,
* m = monospace).
*/
static int spacing = 0;
/*
* The dash character to use in the names retrieved from the font. Default is
* the space.
*/
static int dashchar = ' ';
/*
* Flag, bitmask, and max code for generating a subset of the glyphs in a font.
*/
static int do_subset = 0;
static unsigned short maxcode;
static unsigned long subset[2048];
/*
* The flag that indicates the remapping table should be used to
* reencode the font.
*/
static int do_remap = 0;
/**************************************************************************
*
* Internal globals.
*
**************************************************************************/
/*
* Structure used for calculating the font bounding box as the glyphs are
* generated.
*/
typedef struct {
short minlb;
short maxlb;
short maxrb;
short maxas;
short maxds;
short rbearing;
} bbx_t;
static bbx_t bbx;
/*
* The buffer used to transfer the temporary file to the actual output file.
*/
#define OTF2BDF_IOBUFSIZ 8192
static char iobuf[OTF2BDF_IOBUFSIZ];
/*
* The Units Per Em value used in numerous places.
*/
static FT_UShort upm;
/*
* A flag indicating if a CMap was found or not.
*/
static FT_UShort nocmap;
/*
* The scaling factor needed to compute the SWIDTH (scalable width) value
* for BDF glyphs.
*/
static double swscale;
/**************************************************************************
*
* Platform and encoding table names.
*
**************************************************************************/
static char *platform_names[] = {
"Apple Unicode", "Macintosh", "ISO", "Microsoft", "Unknown"
};
static int nplatform_names = sizeof(platform_names)/sizeof(platform_names[0]);
/*
* Mac encoding names used when creating the BDF XLFD font name.
*/
static char *mac_encodings[] = {
"-MacRoman-0", "-MacJapanese-0", "-MacChinese-0", "-MacKorean-0",
"-MacArabic-0", "-MacHebrew-0", "-MacGreek-0", "-MacRussian-0",
"-MacRSymbol-0", "-MacDevanagari-0", "-MacGurmukhi-0", "-MacGujarati-0",
"-MacOriya-0", "-MacBengali-0", "-MacTamil-0", "-MacTelugu-0",
"-MacKannada-0", "-MacMalayalam-0", "-MacSinhalese-0", "-MacBurmese-0",
"-MacKhmer-0", "-MacThai-0", "-MacLaotian-0", "-MacGeorgian-0",
"-MacArmenian-0", "-MacMaldivian-0", "-MacTibetan-0", "-MacMongolian-0",
"-MacGeez-0", "-MacSlavic-0", "-MacVietnamese-0","-MacSindhi-0",
"-MacUninterp-0"
};
static int nmac_encodings = sizeof(mac_encodings)/sizeof(mac_encodings[0]);
/*
* ISO encoding names used when creating the BDF XLFD font name.
*/
static char *iso_encodings[] = {
"-ASCII-0", "-ISO10646-1", "-ISO8859-1"
};
static int niso_encodings = sizeof(iso_encodings)/sizeof(iso_encodings[0]);
/*
* Microsoft encoding names used when creating the BDF XLFD font name.
*/
static char *ms_encodings[] = {
"-Symbol-0", "-ISO10646-1", "-ShiftJIS-0", "-GB2312.1980-0", "-Big5-0",
"-KSC5601.1987-0", "-KSC5601.1992-0"
};
static int nms_encodings = sizeof(ms_encodings)/sizeof(ms_encodings[0]);
/*
* The propery names for all the XLFD properties.
*/
static char *xlfd_props[] = {
"FOUNDRY",
"FAMILY_NAME",
"WEIGHT_NAME",
"SLANT",
"SETWIDTH_NAME",
"ADD_STYLE_NAME",
"PIXEL_SIZE",
"POINT_SIZE",
"RESOLUTION_X",
"RESOLUTION_Y",
"SPACING",
"AVERAGE_WIDTH",
"CHARSET_REGISTRY",
"CHARSET_ENCODING",
};
/**************************************************************************
*
* Freetype globals.
*
**************************************************************************/
static FT_Library library;
static FT_Face face;
static FT_Size_Metrics imetrics;
static TT_HoriHeader *horizontal;
/**************************************************************************
*
* Freetype related code.
*
**************************************************************************/
/*
* A generic routine to get a name from the OT name table. This routine
* always looks for English language names and checks three possibilities:
* 1. English names with the MS Unicode encoding ID.
* 2. English names with the MS unknown encoding ID.
* 3. English names with the Apple Unicode encoding ID.
*
* The particular name ID mut be provided (e.g. nameID = 0 for copyright
* string, nameID = 6 for Postscript name, nameID = 1 for typeface name.
*
* If the `dash_to_space' flag is non-zero, all dashes (-) in the name will be
* replaced with the character passed.
*
* Returns the number of bytes added.
*/
static int
otf_get_english_string(FT_Face face, int nameID, int dash_to_space,
char *name, int name_size)
{
int j, encid;
FT_UInt i, nrec;
FT_SfntName sfntName;
unsigned char *s;
unsigned short slen;
nrec = FT_Get_Sfnt_Name_Count(face);
for (encid = 1, j = 0; j < 2; j++, encid--) {
/*
* Locate one of the MS English font names.
*/
for (i = 0; i < nrec; i++) {
FT_Get_Sfnt_Name(face, i, &sfntName);
if (sfntName.platform_id == 3 &&
sfntName.encoding_id == encid &&
sfntName.name_id == nameID &&
(sfntName.language_id == 0x0409 ||
sfntName.language_id == 0x0809 ||
sfntName.language_id == 0x0c09 ||
sfntName.language_id == 0x1009 ||
sfntName.language_id == 0x1409 ||
sfntName.language_id == 0x1809)) {
s = sfntName.string;
slen = sfntName.string_len;
break;
}
}
if (i < nrec) {
if (slen >> 1 >= name_size) {
fprintf(stderr, "%s: warning: %s string longer than buffer. Truncating to %d bytes.\n", prog, string_names[nameID], name_size);
slen = name_size << 1;
}
/*
* Found one of the MS English font names. The name is by
* definition encoded in Unicode, so copy every second byte into
* the `name' parameter, assuming there is enough space.
*/
for (i = 1; i < slen; i += 2) {
if (dash_to_space)
*name++ = (s[i] != '-') ? s[i] : ' ';
else if (s[i] == '\r' || s[i] == '\n') {
if (s[i] == '\r' && i + 2 < slen && s[i + 2] == '\n')
i += 2;
*name++ = ' ';
*name++ = ' ';
} else
*name++ = s[i];
}
*name = 0;
return (slen >> 1);
}
}
/*
* No MS English name found, attempt to find an Apple Unicode English
* name.
*/
for (i = 0; i < nrec; i++) {
FT_Get_Sfnt_Name(face, i, &sfntName);
if (sfntName.platform_id == 0 && sfntName.language_id == 0 &&
sfntName.name_id == nameID) {
s = sfntName.string;
slen = sfntName.string_len;
break;
}
}
if (i < nrec) {
if (slen >> 1 >= name_size) {
fprintf(stderr, "%s: warning: %s string longer than buffer. Truncating to %d bytes.\n", prog, string_names[nameID], name_size);
slen = name_size << 1;
}
/*
* Found the Apple Unicode English name. The name is by definition
* encoded in Unicode, so copy every second byte into the `name'
* parameter, assuming there is enough space.
*/
for (i = 1; i < slen; i += 2) {
if (dash_to_space)
*name++ = (s[i] != '-') ? s[i] : ' ';
else if (s[i] == '\r' || s[i] == '\n') {
if (s[i] == '\r' && i + 2 < slen && s[i + 2] == '\n')
i += 2;
*name++ = ' ';
*name++ = ' ';
} else
*name++ = s[i];
}
*name = 0;
return (slen >> 1);
}
return 0;
}
/**************************************************************************
*
* Encoding table related functions.
*
**************************************************************************/
static char *
platform_name(short pid)
{
return (pid < nplatform_names) ?
platform_names[pid] : platform_names[nplatform_names - 1];
}
static char *
encoding_name(short pid, short eid)
{
int nnames;
char **names;
switch (pid) {
case 0: return "-ISO10646-1";
case 1:
nnames = nmac_encodings;
names = mac_encodings;
break;
case 2:
nnames = niso_encodings;
names = iso_encodings;
break;
case 3:
nnames = nms_encodings;
names = ms_encodings;
break;
default: return "-Unknown-0";
}
return (eid < nnames) ? names[eid] : "-Unknown-0";
}
static char *spaces = " ";
static void
print_encoding_table(void)
{
int ncmaps, i, j;
short pid, eid, lasteid;
char *np, *platform, encoding[64];
printf("Encoding tables available in the font:\n\n");
printf("Platform%.*sEncoding\n", 6, spaces);
printf("-------------------------------------------\n");
printf("Default%.*sDefault%.*s(-pid %d -eid %d)\n",
7, spaces, 7, spaces, DEFAULT_PLATFORM_ID, DEFAULT_ENCODING_ID);
ncmaps = face->num_charmaps;
for (lasteid = -1, i = 0; i < ncmaps; i++) {
pid = face->charmaps[i]->platform_id;
eid = face->charmaps[i]->encoding_id;
platform = platform_name(pid);
np = encoding_name(pid, eid);
np++;
for (j = 0; j < 63 && *np != '-'; np++, j++)
encoding[j] = *np;
encoding[j] = 0;
/*
* Typecast the result of 14-strlen(). This returns a size_t on
* some platforms and causes a compilation warning.
*/
printf("%s%.*s%s%.*s(-pid %hd -eid %hd)\n",
platform, (int) (14 - strlen(platform)), spaces,
encoding, (int) (14 - strlen(encoding)), spaces, pid, eid);
}
}
/**************************************************************************
*
* General code.
*
**************************************************************************/
/*
* Create an XLFD name. Assumes there is enough space in the string passed
* to fit a reasonably long XLFD name into, up to the 256 byte maximum.
*/
static void
make_xlfd_name(char *name, int name_size, FT_Long awidth, int ismono)
{
FT_Long i;
FT_ULong val;
char *r, *e;
double dr, dp;
TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
/*
* Default the foundry name to "FreeType" in honor of the project and
* because the foundry name is too difficult to automatically determine
* from the names in TT fonts. But the user can provide his own.
*/
if (foundry_name == 0) {
(void) strcpy(name, "-FreeType");
name += 9;
} else {
*(name++)='-';
strcpy(name,foundry_name);
name+=strlen(foundry_name);
}
/*
* Add the typeface name from the font. The fallback default will be
* "Unknown".
*/
*name++ = '-';
if (face_name == 0) {
if((i = otf_get_english_string(face, BDFOTF_FAMILY_STRING, dashchar,
name, name_size)))
name += i;
else {
(void) strcpy(name, "Unknown");
name += 7;
}
} else {
(void) strcpy(name, face_name);
name += strlen(face_name);
}
/*
* Add the weight name. The default will be "Medium".
*/
if (weight_name != 0) {
sprintf(name, "-%s", weight_name);
name += strlen(weight_name) + 1;
} else {
if (os2->fsSelection & 0x20) {
(void) strcpy(name, "-Bold");
name += 5;
} else {
(void) strcpy(name, "-Medium");
name += 7;
}
}
/*
* Add the slant name. The default will be 'R'.
*/
if (slant_name) {
sprintf(name, "-%s", slant_name);
name += strlen(slant_name) + 1;
} else {
*name++ = '-';
if (os2->fsSelection & 0x01)
*name++ = 'I';
else
*name++ = 'R';
}
/*
* Default the setwidth name to "Normal" but user can specify one.
*/
if (width_name == 0) {
(void) strcpy(name, "-Normal");
name += 7;
} else {
*(name++)='-';
(void) strcpy(name, width_name);
name += strlen(width_name);
}
/*
* Default the additional style name to NULL but user can specify one.
*/
*name++ = '-';
if (style_name != 0) {
(void) strcpy(name, style_name);
name += strlen(style_name);
}
/*
* Determine the pixel size from the point size and resolution.
*/
dr = (double) vres;
dp = (double) (point_size * 10);
val = (unsigned long) (((dp * dr) / 722.7) + 0.5);
/*
* Set the pixel size, point size, and resolution.
*/
sprintf(name, "-%ld-%d-%d-%d", val, point_size * 10, hres, vres);
name += strlen(name);
switch (spacing) {
case 'p': case 'P': spacing = 'P'; break;
case 'm': case 'M': spacing = 'M'; break;
case 'c': case 'C': spacing = 'C'; break;
default: spacing = 0; break;
}
/*
* Set the spacing.
*/
if (!spacing)
spacing = (ismono) ? 'M' : 'P';
*name++ = '-';
*name++ = spacing;
/*
* Add the average width.
*/
sprintf(name, "-%ld", awidth);
name += strlen(name);
/*
* Check to see if the remapping table specified a registry and encoding
* and use those if they both exist.
*/
otf2bdf_remap_charset(&r, &e);
if (r != 0 && e != 0) {
sprintf(name, "-%s-%s", r, e);
return;
}
/*
* If the cmap for the platform and encoding id was not found, or the
* platform id is unknown, assume the character set registry and encoding
* are the XLFD default.
*/
if (nocmap || pid > 3)
(void) strcpy(name, DEFAULT_XLFD_CSET);
else {
/*
* Finally, determine the character set registry and encoding from the
* platform and encoding ID.
*/
switch (pid) {
case 0:
/*
* Apple Unicode platform, so "-Apple-Unicode" is the default.
*/
(void) strcpy(name, "-Apple-Unicode");
break;
case 1:
/*
* Macintosh platform, so choose from the Macintosh encoding
* strings.
*/
if (eid < 0 || eid >= nmac_encodings)
(void) strcpy(name, DEFAULT_XLFD_CSET);
else
(void) strcpy(name, mac_encodings[eid]);
break;
case 2:
/*
* ISO platform, so choose from the ISO encoding strings.
*/
if (eid < 0 || eid >= niso_encodings)
(void) strcpy(name, DEFAULT_XLFD_CSET);
else
(void) strcpy(name, iso_encodings[eid]);
break;
case 3:
/*
* Microsoft platform, so choose from the MS encoding strings.
*/
if (eid < 0 || eid >= nms_encodings)
(void) strcpy(name, DEFAULT_XLFD_CSET);
else
(void) strcpy(name, ms_encodings[eid]);
break;
}
}
}
static int
generate_font(FILE *out, char *iname, char *oname)
{
int eof, ismono, i;
FILE *tmp;
FT_Short x, y, dwidth, swidth;
FT_Short y_off, x_off;
FT_Long sx, sy, ex, ey, wd, ht;
FT_Long code, idx, ng, aw;
FT_UShort remapped_code;
unsigned char *bp;
double dw;
char *xp, xlfd[BUFSIZ];
char *tmpdir, tmpfile[BUFSIZ];
imetrics = face->size->metrics;
horizontal = FT_Get_Sfnt_Table(face, ft_sfnt_hhea);
/*
* Clear the BBX.
*/
memset((char *) &bbx, 0, sizeof(bbx_t));
/*
* Open a temporary file to store the bitmaps in until the exact number
* of bitmaps are known.
*/
if ((tmpdir = getenv("TMPDIR")) == 0)
tmpdir = "/tmp";
sprintf(tmpfile, "%s/otf2bdf%ld", tmpdir, (long) getpid());
if ((tmp = fopen(tmpfile, "w")) == 0) {
fprintf(stderr, "%s: unable to open temporary file '%s'.\n",
prog, tmpfile);
return -1;
}
/*
* Calculate the scale factor for the SWIDTH field.
*/
swscale = ((double) vres) * ((double) point_size);
/*
* Initialize the flag that tracks if the font is monowidth or not and
* initialize the glyph width variable that is used for testing for a
* monowidth font.
*/
wd = 0xffff;
ismono = 1;
for (ng = code = 0, eof = 0, aw = 0; eof != EOF && code < 0xffff; code++) {
/*
* If a remap is indicated, attempt to remap the code. If a remapped
* code is not found, then skip generating the glyph.
*/
remapped_code = (FT_UShort) code;
if (do_remap && !otf2bdf_remap(&remapped_code))
continue;
/*
* If a subset is being generated and the code is greater than the max
* code of the subset, break out of the loop to avoid doing any more
* work.
*/
if (do_subset && remapped_code > maxcode)
break;
/*
* If a subset is being generated and the index is not in the subset
* bitmap, just continue.
*/
if (do_subset &&
!(subset[remapped_code >> 5] & (1 << (remapped_code & 31))))
continue;
if (nocmap) {
if (code >= face->num_glyphs)
/*
* At this point, all the glyphs are done.
*/
break;
idx = code;
} else
idx = FT_Get_Char_Index(face, code);
/*
* If the glyph could not be loaded for some reason, or a subset is
* being generated and the index is not in the subset bitmap, just
* continue.
*/
if (idx <= 0 || FT_Load_Glyph(face, idx, load_flags))
continue;
if (FT_Render_Glyph(face->glyph, FT_RENDER_MODE_MONO))
continue;
/*
* Determine the DWIDTH (device width, or advance width in TT terms)
* and the SWIDTH (scalable width) values.
*/
dwidth = face->glyph->metrics.horiAdvance >> 6;
dw = (double) dwidth;
swidth = (FT_Short) ((dw * 72000.0) / swscale);
/*
* Determine the actual bounding box of the glyph bitmap. Do not
* forget that the glyph is rendered upside down!
*/
sx = sy = 0xffff;
ex = ey = 0;
bp = face->glyph->bitmap.buffer;
for (y = 0; y < face->glyph->bitmap.rows; y++) {
for (x = 0; x < face->glyph->bitmap.width; x++) {
if (bp[x >> 3] & (0x80 >> (x & 7))) {
if (x < sx) sx = x;
if (x > ex) ex = x;
if (y < sy) sy = y;
if (y > ey) ey = y;
}
}
bp += face->glyph->bitmap.pitch;
}
/*
* If the glyph is actually an empty bitmap, set the size to 0 all
* around.
*/
if (sx == 0xffff && sy == 0xffff && ex == 0 && ey == 0)
sx = ex = sy = ey = 0;
else {
/*
* Adjust the end points.
*/
ex++;
ey++;
}
/*
* Increment the number of glyphs generated.
*/
ng++;
/*
* Test to see if the font is going to be monowidth or not by
* comparing the current glyph width against the last one.
*/
if (wd != 0xffff && ismono && (ex - sx) + 1 != wd)
ismono = 0;
/*
* Adjust the font bounding box.
*/
wd = ex - sx;
ht = ey - sy;
x_off = sx + face->glyph->bitmap_left;
y_off = sy + face->glyph->bitmap_top - face->glyph->bitmap.rows;
bbx.maxas = MAX(bbx.maxas, ht + y_off);
bbx.maxds = MAX(bbx.maxds, -y_off);
bbx.rbearing = wd + x_off;
bbx.maxrb = MAX(bbx.maxrb, bbx.rbearing);
bbx.minlb = MIN(bbx.minlb, x_off);
bbx.maxlb = MAX(bbx.maxlb, x_off);
/*
* Add to the average width accumulator.
*/
aw += wd;
/*
* Print the bitmap header.
*/
fprintf(tmp, "STARTCHAR %04lX\nENCODING %ld\n", code,
(long) remapped_code);
fprintf(tmp, "SWIDTH %hd 0\n", swidth);
fprintf(tmp, "DWIDTH %hd 0\n", dwidth);
fprintf(tmp, "BBX %ld %ld %hd %hd\n", wd, ht, x_off, y_off);
/*
* Check for an error return here in case the temporary file system
* fills up or the file is deleted while it is being used.
*/
eof = fprintf(tmp, "BITMAP\n");
bp = face->glyph->bitmap.buffer + (sy * face->glyph->bitmap.pitch);
for (y = 0; eof != EOF && y < ey - sy; y++) {
for (idx = 0, x = 0; eof != EOF && x < ex - sx; x++) {
if (x > 0 && (x & 7) == 0) {
/*
* Print the next byte.
*/
eof = fprintf(tmp, "%02lX", idx & 0xff);
idx = 0;
}
if (bp[(x+sx) >> 3] & (0x80 >> ((x+sx) & 7)))
idx |= (0x80 >> (x & 7));
}
bp += face->glyph->bitmap.pitch;
if (eof != EOF)
/*
* Because of the structure of the loop, the last byte should
* always be printed.
*/
fprintf(tmp, "%02lX\n", idx & 0xff);
}
if (eof != EOF)
fprintf(tmp, "ENDCHAR\n");
}
fclose(tmp);
/*
* If a write error occured, delete the temporary file and issue an error
* message.
*/
if (eof == EOF) {
(void) unlink(tmpfile);
fprintf(stderr, "%s: problem writing to temporary file '%s'.\n",
prog, tmpfile);
return -1;
}
/*
* If no characters were generated, just unlink the temp file and issue a
* warning.
*/
if (ng == 0) {
(void) unlink(tmpfile);
fprintf(stderr, "%s: no glyphs generated from '%s'.\n", prog, iname);
return -1;
}
/*
* Reopen the temporary file so it can be copied to the actual output
* file.
*/
if ((tmp = fopen(tmpfile, "r")) == 0) {
/*
* Unable to open the file for read, so attempt to delete it and issue
* an error message.
*/
(void) unlink(tmpfile);
fprintf(stderr, "%s: unable to open temporary file '%s' for read.\n",
prog, tmpfile);
return -1;
}
/*
* Calculate the average width.
*/
aw = (FT_Long) ((((double) aw / (double) ng) + 0.5) * 10.0);
/*
* Generate the XLFD font name.
*/
make_xlfd_name(xlfd, sizeof(xlfd), aw, ismono);
/*
* Start writing the font out.
*/
fprintf(out, "STARTFONT 2.1\n");
/*
* Add the vanity comments.
*/
fprintf(out, "COMMENT\n");
fprintf(out, "COMMENT Converted from OpenType font \"%s\" by \"%s %s\".\n",
iname, prog, OTF2BDF_VERSION);
fprintf(out, "COMMENT\n");
fprintf(out, "FONT %s\n", xlfd);
fprintf(out, "SIZE %d %d %d\n", point_size, hres, vres);
/*
* Generate the font bounding box.
*/