summaryrefslogtreecommitdiff
path: root/write.c
blob: 79b2c08da52fb19f3bcbf847a7acd4e4a9d02539 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
/*
Copyright (c) 2002-2003 by Juliusz Chroboczek

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 THE
AUTHORS OR COPYRIGHT HOLDERS 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.
*/
/* $XFree86: xc/programs/fonttosfnt/write.c,v 1.4tsi Exp $ */

#if defined(linux) && !defined(_GNU_SOURCE)
/* for fwrite_unlocked and fread_unlocked */
#define _GNU_SOURCE 1
#endif

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include "X11/Xos.h"

#include "fonttosfnt.h"

#if !defined(I_LOVE_POSIX) && \
    defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1
#define DO_FWRITE fwrite_unlocked
#define DO_FREAD fread_unlocked
#else
#define DO_FWRITE fwrite
#define DO_FREAD fread
#endif

static int writeDir(FILE*, FontPtr, int, unsigned*);
static int fixupDir(FILE*, FontPtr, int, int*, int*);
static int fixupChecksum(FILE*, int, int);

static int writeEBDT(FILE*, FontPtr);
static int writeEBLC(FILE*, FontPtr);
static int writeOS2(FILE*, FontPtr);
static int writePCLT(FILE*, FontPtr);
static int writecmap(FILE*, FontPtr);
static int writeglyf(FILE*, FontPtr);
static int writehead(FILE*, FontPtr);
static int writehhea(FILE*, FontPtr);
static int writehmtx(FILE*, FontPtr);
static int writeloca(FILE*, FontPtr);
static int writemaxp(FILE*, FontPtr);
static int writename(FILE*, FontPtr);
static int writepost(FILE*, FontPtr);

static CmapPtr current_cmap = NULL;
static int numglyphs, nummetrics;
static int write_error_occurred, read_error_occurred;

/* floor(log2(x)) */
static int
log2_floor(int x) 
{
    int i, j;

    if(x <= 0)
        abort();

    i = 0;
    j = 1;
    while(2 * j < x) {
        i++;
        j *= 2;
    }
    return i;
}

/* 2 ** floor(log2(x)) */
static int
two_log2_floor(int x)
{
    int j;
    
    if(x <= 0)
        abort();

    j = 1;
    while(2 * j < x) {
        j *= 2;
    }
    return j;
}

static void
write_error(int rc)
{
    /* Real Men program in C and don't use exceptions. */
    if(write_error_occurred)
        return;
    write_error_occurred = 1;
    if(rc < 0)
        perror("Couldn't write");
    else
        fprintf(stderr, "Short write.\n");
}

static void
read_error(int rc)
{
    if(read_error_occurred)
        return;
    read_error_occurred = 1;
    if(rc < 0)
        perror("Couldn't read");
    else
        fprintf(stderr, "Short read.\n");
}

static void
writeBYTE(FILE *out, unsigned char val)
{
    int rc;
    rc = DO_FWRITE(&val, 1, 1, out);
    if(rc != 1) write_error(rc);
}

static void
writeBYTEs(FILE *out, unsigned char *chars, int n)
{
    int rc;
    rc = DO_FWRITE(chars, 1, n, out);
    if(rc != n) write_error(rc);
}

static void
writeCHAR(FILE *out, char val)
{
    int rc;
    rc = DO_FWRITE(&val, 1, 1, out);
    if(rc != 1) write_error(rc);
}

static void
writeCHARs(FILE *out, char *chars, int n)
{
    int rc;
    rc = DO_FWRITE(chars, 1, n, out);
    if(rc != n) write_error(rc);
}

static void
writeUSHORT(FILE *out, unsigned short val)
{
    int rc;
    val = htons(val);
    rc = DO_FWRITE(&val, 2, 1, out);
    if(rc != 1) write_error(rc);
}

static void
writeSHORT(FILE *out, short val)
{
    int rc;
    val = htons(val);
    rc = DO_FWRITE(&val, 2, 1, out);
    if(rc != 1) write_error(rc);
}

static void
writeULONG(FILE *out, unsigned int val)
{
    int rc;
    val = htonl(val);
    rc = DO_FWRITE(&val, 4, 1, out);
    if(rc != 1) write_error(rc);
}

static void
writeLONG(FILE *out, int val)
{
    int rc;
    val = htonl(val);
    rc = DO_FWRITE(&val, 4, 1, out);
    if(rc != 1) write_error(rc);
}

static unsigned
readULONG(FILE *out)
{
    int rc;
    unsigned val;
    rc = DO_FREAD(&val, 4, 1, out);
    if(rc != 1) {
        read_error(rc);
        return 0xDEADBEEF;
    }
    return ntohl(val);
}

void
fontMetrics(FontPtr font)
{
    int i, rc;
    double sumAwidth = 0;
    unsigned count = 0;

    font->metrics.maxAwidth = 0;
    font->metrics.maxX = -10000 * TWO_SIXTEENTH;
    font->metrics.maxY = -10000 * TWO_SIXTEENTH;
    font->metrics.minX = 10000 * TWO_SIXTEENTH;
    font->metrics.minY = 10000 * TWO_SIXTEENTH;

    for(i = 0; i < FONT_CODES; i++) {
        int awidth, x0, y0, x1, y1;
        rc = glyphMetrics(font, i, &awidth, &x0, &y0, &x1, &y1);
        if(rc < 0)
            continue;

        if(awidth > font->metrics.maxAwidth) font->metrics.maxAwidth = awidth;
        if(x0 < font->metrics.minX) font->metrics.minX = x0;
        if(y0 < font->metrics.minY) font->metrics.minY = y0;
        if(x1 > font->metrics.maxX) font->metrics.maxX = x1;
        if(y1 > font->metrics.maxY) font->metrics.maxY = y1;

	if(awidth > 0) {
	    sumAwidth += awidth;
	    count++;
	}
    }

    if (count) font->metrics.awidth = sumAwidth / count;

    font->metrics.height = UNDEF /* TODO */;

    if(font->pxMetrics.ascent == UNDEF) {
	font->metrics.ascent = font->metrics.maxY;
	font->pxMetrics.ascent =
	    font->metrics.ascent
	    * font->pxMetrics.height / TWO_SIXTEENTH;
    }
    else
	font->metrics.ascent =
	    font->pxMetrics.ascent
	    * TWO_SIXTEENTH / font->pxMetrics.height;

    if(font->pxMetrics.descent == UNDEF) {
	font->metrics.descent = font->metrics.minY;
	font->pxMetrics.descent =
	    font->metrics.descent
	    * font->pxMetrics.height / TWO_SIXTEENTH;
    }
    else
	font->metrics.descent =
	    font->pxMetrics.descent
	    * TWO_SIXTEENTH / font->pxMetrics.height;

    if(font->pxMetrics.capHeight == UNDEF) {
	if(glyphMetrics(font, 'X', NULL, NULL, NULL, NULL, &font->metrics.capHeight) != 1)
	    font->metrics.capHeight = font->metrics.ascent;
	font->pxMetrics.capHeight =
	    font->metrics.capHeight * font->pxMetrics.height / TWO_SIXTEENTH;
    }
    else
	font->metrics.capHeight =
	    font->pxMetrics.capHeight
	    * TWO_SIXTEENTH / font->pxMetrics.height;

    if(font->pxMetrics.xHeight == UNDEF) {
	if(glyphMetrics(font, 'x', NULL, NULL, NULL, NULL, &font->metrics.xHeight) != 1)
	    font->metrics.xHeight = font->metrics.capHeight * 2 / 3;
	font->pxMetrics.xHeight =
	    font->metrics.xHeight * font->pxMetrics.height / TWO_SIXTEENTH;
    }
    else
	font->metrics.xHeight =
	    font->pxMetrics.xHeight
	    * TWO_SIXTEENTH / font->pxMetrics.height;

    if(font->pxMetrics.underlinePosition == UNDEF)
	font->metrics.underlinePosition = - font->metrics.descent * 2;
    else {
	fprintf(stderr, "Setting underlinePosition. pxMetrics.underlinePosition is %d. height is %d\n",
		font->pxMetrics.underlinePosition, font->pxMetrics.height);
	font->metrics.underlinePosition =
	    font->pxMetrics.underlinePosition
	    * TWO_SIXTEENTH / font->pxMetrics.height;
    }

    if(font->pxMetrics.underlineThickness == UNDEF)
	/* make sure thickness is at least one pixel. */
	/* TODO: this could be refined according to
	 * X Logical Font Description Conventions (xlfd.txt)
	 * by also considering the font weight. */
	font->metrics.underlineThickness =
	    TWO_SIXTEENTH
	    / (font->pxMetrics.height < 9 ? font->pxMetrics.height : 9);
    else
	font->metrics.underlineThickness =
	    font->pxMetrics.underlineThickness
	    * TWO_SIXTEENTH / font->pxMetrics.height;
}

int 
writeFile(char *filename, FontPtr font)
{
    int rc;
    FILE *out;
    unsigned tables[15];
    int head_position = 0;
    int full_length;
    int (*(table_writers[15]))(FILE*, FontPtr);
    int i, j;
    int offset[15], length[15];
    StrikePtr strike;

    out = fopen(filename, "wb+");
    if(out == NULL)
        return -1;

    current_cmap = makeCmap(font);
    if(current_cmap == NULL) {
        fprintf(stderr, "Couldn't build cmap.\n");
        return -1;
    }

    fontMetrics(font);

    write_error_occurred = 0;
    read_error_occurred = 0;

    if(glyph_flag >= 2) {
        numglyphs = maxIndex(current_cmap) + 1;
        if(metrics_flag >= 2)
            nummetrics = numglyphs - 1;
        else if(metrics_flag >= 1)
            nummetrics = 1;
        else
            nummetrics = 0;
    } else if(glyph_flag == 1) {
        numglyphs = 1;
        nummetrics = (metrics_flag >= 1) ? 1 : 0;
    } else {
        numglyphs = 0;
        nummetrics = 0;
    }

    strike = font->strikes;
    while(strike) {
        strike->indexSubTables = makeIndexSubTables(strike, current_cmap);
        if(!strike->indexSubTables) {
            fprintf(stderr, "Couldn't build indexSubTable.\n");
            return -1;
        }
        strike = strike->next;
    }

    /* These must be sorted lexicographically */
    i = 0;
    tables[i] = makeName("EBDT"); table_writers[i] = writeEBDT; i++;
    tables[i] = makeName("EBLC"); table_writers[i] = writeEBLC; i++;
    tables[i] = makeName("OS/2"); table_writers[i] = writeOS2; i++;
    tables[i] = makeName("PCLT"); table_writers[i] = writePCLT; i++;
    tables[i] = makeName("cmap"); table_writers[i] = writecmap; i++;
    if(numglyphs >= 1) {
        tables[i] = makeName("glyf");
        table_writers[i] = writeglyf; i++;
    }
    tables[i] = makeName("head"); table_writers[i] = writehead; i++;
    tables[i] = makeName("hhea"); table_writers[i] = writehhea; i++;
    if(nummetrics >= 1) {
        tables[i] = makeName("hmtx"); 
        table_writers[i] = writehmtx; i++;
    }
    if(numglyphs >= 1) {
        tables[i] = makeName("loca"); 
        table_writers[i] = writeloca; i++;
    }
    tables[i] = makeName("maxp"); table_writers[i] = writemaxp; i++;
    tables[i] = makeName("name"); table_writers[i] = writename; i++;
    tables[i] = makeName("post"); table_writers[i] = writepost; i++;

    rc = writeDir(out, font, i, tables);
    if(rc < 0)
        goto fail;

    for(j = 0; j < i; j++) {
        offset[j] = ftell(out);
        if(offset[j] < 0) {
            perror("Couldn't compute table offset");
            goto fail;
        }
        if(tables[j] == makeName("head"))
            head_position = offset[j];
        rc = table_writers[j](out, font);
        if(rc < 0 || write_error_occurred || read_error_occurred)
            goto fail;
        length[j] = ftell(out) - offset[j];
        if(length[j] < 0) {
            perror("Couldn't compute table size");
            goto fail;
        }
        if(length[j] % 4 != 0) {
            /* Pad -- recommended by the spec, and assumed by
               computeChecksum. */
            int k;
            for(k = 0; k < (4 - length[j] % 4); k++) {
                /* This must be 0 -- see computeChecksum. */
                writeBYTE(out, 0);
            }
            if(write_error_occurred || read_error_occurred)
                goto fail;
        }
    }

    rc = fixupDir(out, font, i, offset, length);
    if(rc < 0)
        goto fail;

    full_length = ftell(out);
    if(full_length < 0) {
        perror("Couldn't compute file size");
        goto fail;
    }
    while(full_length % 4 != 0) {
        /* pad for computeChecksum */
        writeBYTE(out, 0);
        full_length++;
    }
    if(write_error_occurred || read_error_occurred)
        goto fail;
    rc = fixupChecksum(out, full_length, head_position);
    if(rc < 0)
        goto fail;
    fclose(out);
    return 0;

 fail:
    unlink(filename);
    return -1;
}

static int
writeDir(FILE *out, FontPtr font, int numTables, unsigned *tables)
{
    int i, ti;
    i = 0; ti = 1;
    while(2 * ti < numTables) {
        i++;
        ti = 2 * ti;
    }

    writeULONG(out, 0x10000);   /* version */
    writeUSHORT(out, numTables); /* numTables */
    writeUSHORT(out, 16 * ti);  /* searchRange */
    writeUSHORT(out, i);        /* entrySelector */
    writeUSHORT(out, 16 * (numTables - ti)); /* rangeShift */

    /* see fixupDir */
    for(i = 0; i < numTables; i++) {
        writeULONG(out, tables[i]);
        writeULONG(out, 0xDEADFACE); /* checkSum */
        writeULONG(out, 0xDEADFACE); /* offset */
        writeULONG(out, 0xDEADFACE); /* length */
    }
    return 0;
}

static unsigned
computeChecksum(FILE *out, int offset, int length)
{
    int rc;
    int i;
    unsigned sum = 0;

    if(offset % 4 != 0) {
        fprintf(stderr, "Offset %d is not a multiple of 4\n", offset);
        return ~0;
    }

    rc = fseek(out, offset, SEEK_SET);
    if(rc < 0) {
        perror("Couldn't seek");
        return ~0;
    }

    /* This relies on the fact that we always pad tables with zeroes. */
    for(i = 0; i < length; i += 4) {
        sum += readULONG(out);
    }
    return sum;
}

static int
fixupDir(FILE *out, FontPtr font, int numTables, int *offset, int *length)
{
    int rc, i;
    unsigned sum;

    for(i = 0; i < numTables; i++) {
        sum = computeChecksum(out, offset[i], length[i]);
        rc = fseek(out, 12 + 16 * i + 4, SEEK_SET);
        if(rc != 0) {
            perror("Couldn't seek");
            return -1;
        }
        writeULONG(out, sum);
        writeULONG(out, offset[i]);
        writeULONG(out, length[i]);
    }
    return 0;
}

static int
fixupChecksum(FILE *out, int full_length, int head_position)
{
    int rc, checksum;
    checksum = computeChecksum(out, 0, full_length);
    rc = fseek(out, head_position + 8, SEEK_SET);
    if(rc != 0) {
        perror("Couldn't seek");
        return -1;
    }
    writeULONG(out, 0xB1B0AFBA - checksum); /* checkSumAdjustment */
    return 0;
}

    
static int 
writehead(FILE* out, FontPtr font)
{
    int time_hi = 0;
    unsigned time_lo = 0;

    macTime(&time_hi, &time_lo);

    writeULONG(out, 0x00010000);
    writeULONG(out, 0x00010000); /* fontRevision */
    writeULONG(out, 0);         /* checkSumAdjustment -- filled in later */
    writeULONG(out,0x5F0F3CF5); /* magicNumber */
    writeUSHORT(out, 1);        /* flags */
    writeUSHORT(out, UNITS_PER_EM); /* unitsPerEm */

    writeLONG(out, time_hi);    /* created */
    writeULONG(out, time_lo);
    writeLONG(out, time_hi);    /* modified */
    writeULONG(out, time_lo);

    /* bounding box for all glyphs */
    writeUSHORT(out, FONT_UNITS_FLOOR(font->metrics.minX));
    writeUSHORT(out, FONT_UNITS_FLOOR(font->metrics.minY));
    writeUSHORT(out, FONT_UNITS_CEIL(font->metrics.maxX));
    writeUSHORT(out, FONT_UNITS_CEIL(font->metrics.maxY));

    writeUSHORT(out, font->flags); /* macStyle */
    writeUSHORT(out, 1);        /* lowestRecPPEM */
    writeSHORT(out, 0);         /* fontDirectionHint */
    writeSHORT(out, 0);         /* indexToLocFormat */
    writeSHORT(out, 0);         /* glyphDataFormat */
    return 0;
}

static int
outputRaster(FILE *out, char *raster, int width, int height, int stride,
             int bit_aligned)
{
    int i, j;
    int len = 0;

    if(!bit_aligned || width % 8 == 0) {
        for(i = 0; i < height; i++) {
            writeCHARs(out, raster + i * stride, (width + 7) / 8);
            len += (width + 7) / 8;
        }
    } else {
        int bit = 0;
        unsigned char v = 0;
        for(i = 0; i < height; i++) {
            for(j = 0; j < width; j++) {
                if(BITREF(raster, stride, j, i))
                    v |= 1 << (7 - bit);
                bit++;
                if(bit >= 8) {
                    writeBYTE(out, v);
                    len++;
                    bit = 0;
                    v = 0;
                }
            }
        }
        if(bit > 0) {
            writeBYTE(out, v);
            len++;
        }
    }
    return len;
}

static int 
writeEBDT(FILE* out, FontPtr font)
{
    StrikePtr strike;
    BitmapPtr bitmap;
    IndexSubTablePtr table;
    int i;
    int offset;
    int ebdt_start;

    ebdt_start = ftell(out);

    writeULONG(out, 0x00020000); /* version */
    offset = 4;

    strike = font->strikes;
    while(strike) {
        table = strike->indexSubTables;
        while(table) {
            for(i = table->firstGlyphIndex; i <= table->lastGlyphIndex; i++) {
                bitmap = strikeBitmapIndex(strike, current_cmap, i);
                bitmap->location = offset;
                if(bit_aligned_flag && table->constantMetrics) {
                    /* image format 5 */
                    ;
                } else {
                    /* image format 1 or 2 */
                    writeBYTE(out, bitmap->height);
                    writeBYTE(out, bitmap->width);
                    writeCHAR(out, bitmap->horiBearingX);
                    writeCHAR(out, bitmap->horiBearingY);
                    writeBYTE(out, bitmap->advanceWidth);
                    offset += 5;
                }
                offset += outputRaster(out, 
                                       bitmap->raster, 
                                       bitmap->width, bitmap->height,
                                       bitmap->stride,
                                       bit_aligned_flag);
            }
            table->lastLocation = offset;
            table = table->next;
        }
        strike = strike->next;
    }
    if(ftell(out) != ebdt_start + offset)
        abort();
    return 0;
}

static int 
writeEBLC(FILE* out, FontPtr font)
{
    int i, rc, numstrikes, eblc_start, num, den;
    StrikePtr strike;
    IndexSubTablePtr table;

    degreesToFraction(font->italicAngle, &num, &den);

    numstrikes = 0;
    strike = font->strikes;
    while(strike) {
        numstrikes++;
        strike = strike->next;
    }

    eblc_start = ftell(out);
    
    writeULONG(out, 0x00020000); /* version */
    writeULONG(out, numstrikes); /* numSizes */

    /* bitmapSizeTable */
    strike = font->strikes;
    while(strike) {
        strike->bitmapSizeTableLocation = ftell(out);
        writeULONG(out, 0xDEADFACE); /* indexSubTableArrayOffset */
        writeULONG(out, 0xDEADFACE); /* indexTablesSize */
        writeULONG(out, 0xDEADFACE); /* numberOfIndexSubTables */
        writeULONG(out, 0);     /* colorRef */
	for (i = 0; i <= 1; i++) {
	  writeCHAR(out, font->pxMetrics.ascent);	/* ascender */
	  writeCHAR(out, -font->pxMetrics.descent);	/* descender */
	  writeBYTE(out, strikeMaxWidth(strike));	/* widthMax */
	  writeCHAR(out, num);				/* caretSlopeNumerator */
	  writeCHAR(out, den);				/* caretSlopeDenominator */
	  writeCHAR(out, 0);				/* caretOffset */
	  writeCHAR(out, 0);				/* minOriginSB */
	  writeCHAR(out, 0);				/* minAdvanceSB */
	  writeCHAR(out, 0);				/* maxBeforeBL */
	  writeCHAR(out, 0);				/* minAfterBL */
	  writeCHAR(out, 0);				/* pad1 */
	  writeCHAR(out, 0);				/* pad2 */
	}
        writeUSHORT(out, 0);    /* startGlyphIndex */
        writeUSHORT(out, 0xFFFD); /* endGlyphIndex */
        writeBYTE(out, strike->sizeX); /* ppemX */
        writeBYTE(out, strike->sizeY); /* ppemY */
        writeBYTE(out, 1);      /* bitDepth */
        writeCHAR(out, 1);      /* flags */
        strike = strike->next;
    }

    /* indexSubTableArray, one per strike */
    strike = font->strikes;
    while(strike) {
        int endoffset;
        int numtables = 0;

        strike->indexSubTableLocation = ftell(out);
        table = strike->indexSubTables;
        while(table) {
            table->location = ftell(out);
            writeUSHORT(out, table->firstGlyphIndex);
            writeUSHORT(out, table->lastGlyphIndex);
            writeULONG(out, 0xDEADFACE); /* additionalOffsetToIndexSubtable */
            numtables++;
            table = table->next;
        }
        endoffset = ftell(out);
        rc = fseek(out, strike->bitmapSizeTableLocation, SEEK_SET);
        if(rc != 0) {
            perror("Couldn't seek");
            return -1;
        }
        writeULONG(out, strike->indexSubTableLocation - eblc_start); 
                                              /* indexSubTableArrayOffset */
        writeULONG(out, endoffset - strike->indexSubTableLocation);
                                              /* indexTablesSize */
        writeULONG(out, numtables);           /* numberOfIndexSubTables */
        rc = fseek(out, endoffset, SEEK_SET);
        if(rc != 0) {
            perror("Couldn't seek");
            return -1;
        }
        strike = strike->next;
    }

    /* actual indexSubTables */
    strike = font->strikes;
    while(strike) {
        table = strike->indexSubTables;
        while(table) {
            int location;
            int data_location;
            int short_offsets;
            int offset;

            location = ftell(out);
            rc = fseek(out, table->location + 4, SEEK_SET);
            if(rc != 0) {
                perror("Couldn't seek");
                return -1;
            }
            /* additionalOffsetToIndexSubtable */
            writeULONG(out, location - strike->indexSubTableLocation);
            rc = fseek(out, location, SEEK_SET);
            if(rc != 0) {
                perror("Couldn't seek");
                return -1;
            }
            data_location =
                strikeBitmapIndex(strike, current_cmap,
                                  table->firstGlyphIndex)->location;
            short_offsets = 1;
            for(i = table->firstGlyphIndex; i <= table->lastGlyphIndex; i++) {
                if(strikeBitmapIndex(strike, current_cmap, i)->location -
                   data_location > 0xFFFF) {
                    short_offsets = 0;
                    break;
                }
            }
            /* indexFormat */
            if(table->constantMetrics)
                writeUSHORT(out, 2);
            else if(short_offsets)
                writeUSHORT(out, 3);
            else
                writeUSHORT(out, 1);
            /* imageFormat */
            if(bit_aligned_flag) {
                if(table->constantMetrics)
                    writeUSHORT(out, 5);
                else
                    writeUSHORT(out, 2);
            } else {
                writeUSHORT(out, 1);
            }
            writeULONG(out, data_location);
            if(table->constantMetrics) {
                int size;
                BitmapPtr bitmap = 
                    strikeBitmapIndex(strike, current_cmap, 
                                      table->firstGlyphIndex);

                size = 
                    strikeBitmapIndex(strike, current_cmap, 
                                      table->firstGlyphIndex + 1)->location -
                    bitmap->location;
                writeULONG(out, size); /* imageSize */
                /* bigMetrics */
                writeBYTE(out, bitmap->height);
                writeBYTE(out, bitmap->width);
                writeCHAR(out, bitmap->horiBearingX);
                writeCHAR(out, bitmap->horiBearingY);
                writeBYTE(out, bitmap->advanceWidth);
                writeCHAR(out, bitmap->horiBearingX); /* vertBearingX */
                writeCHAR(out, bitmap->horiBearingY); /* vertBearingY */
                writeBYTE(out, font->metrics.maxAwidth); /* vertAdvance */
            } else {
                for(i = table->firstGlyphIndex; 
                    i <= table->lastGlyphIndex; i++) {
                    offset = 
                        strikeBitmapIndex(strike, current_cmap, i)->location -
                        data_location;
                    if(short_offsets)
                        writeUSHORT(out, offset);
                    else
                        writeULONG(out, offset);
                }
                /* Dummy glyph of size 0 to mark the end of the table */
                if(short_offsets) {
                    writeUSHORT(out, table->lastLocation - data_location);
                    writeUSHORT(out, table->lastLocation - data_location);
                } else {
                    writeULONG(out, table->lastLocation - data_location);
                    writeULONG(out, table->lastLocation - data_location);
                }
            }
            location = ftell(out);
            while(location % 4 != 0) {
                writeCHAR(out, 0);
                location--;
            }
            table = table->next;
        }
        strike = strike->next;
    }
    return 0;
}

static int 
writecmap(FILE* out, FontPtr font)
{
    int rc, cmap_start, cmap_end;
    CmapPtr cmap;
    int segcount;

    segcount = 0;
    cmap = current_cmap;
    while(cmap) {
        segcount++;
        cmap = cmap->next;
    }

    segcount++;                 /* dummy segment to end table */

    cmap_start = ftell(out);

    writeUSHORT(out, 0);        /* version */
    writeUSHORT(out, 1);        /* number of encoding tables */
    writeUSHORT(out, 3);        /* platform ID */
    writeUSHORT(out, (font->flags & FACE_SYMBOL) ? 0 : 1);
                                /* encoding ID */
    writeULONG(out, 12);        /* offset to beginning of subtable */

    /* subtable */
    writeUSHORT(out, 4);        /* format */
    writeUSHORT(out, 0xDEAD);   /* length */
    writeUSHORT(out, 0);        /* language */
    /* How baroque can you get? */
    writeUSHORT(out, segcount * 2); /* segCountX2 */
    writeUSHORT(out, 2 * two_log2_floor(segcount)); /* searchRange */
    writeUSHORT(out, 1 + log2_floor(segcount));   /* entrySelector */
    writeUSHORT(out, 2 * (segcount - two_log2_floor(segcount)));   
                                /* rangeShift */

    cmap = current_cmap;
    while(cmap) {
        writeUSHORT(out, cmap->endCode);
        cmap = cmap->next;
    }
    writeUSHORT(out, 0xFFFF);

    writeUSHORT(out, 0);        /* reservedPad */

    cmap = current_cmap;
    while(cmap) {
        writeUSHORT(out, cmap->startCode);
        cmap = cmap->next;
    }
    writeUSHORT(out, 0xFFFF);

    /* idDelta */
    cmap = current_cmap;
    while(cmap) {
        writeUSHORT(out, (cmap->index - cmap->startCode) & 0xFFFF);
        cmap = cmap->next;
    }
    writeUSHORT(out, 1);

    /* idRangeOffset */
    cmap = current_cmap;
    while(cmap) {
        writeUSHORT(out, 0);
        cmap = cmap->next;
    }
    writeUSHORT(out, 0);

    /* glyphIDArray is empty */

    cmap_end = ftell(out);
    rc = fseek(out, cmap_start + 12 + 2, SEEK_SET);
    if(rc != 0) {
        perror("Couldn't seek");
        return -1;
    }
    writeUSHORT(out, cmap_end - cmap_start - 12); /* length */
    rc = fseek(out, cmap_end, SEEK_SET);
    if(rc != 0) {
        perror("Couldn't seek");
        return -1;
    }
    return 0;
}

static int 
writeglyf(FILE* out, FontPtr font)
{
    return 0;
}

int
writehhea(FILE* out, FontPtr font)
{
    int num, den;
    degreesToFraction(font->italicAngle, &num, &den);

    writeULONG(out, 0x00010000); /* version */
    writeSHORT(out, FONT_UNITS_CEIL(font->metrics.ascent)); /* ascender */
    writeSHORT(out, -FONT_UNITS_CEIL(font->metrics.descent)); /* descender */
    writeSHORT(out, 0);		/* lineGap */
    writeUSHORT(out, FONT_UNITS(font->metrics.maxAwidth)); /* advanceWidthMax */
    /* TODO: the next three are not calculated according to spec, are they ?
     * https://docs.microsoft.com/en-us/typography/opentype/spec/hhea */
    writeSHORT(out, FONT_UNITS_FLOOR(font->metrics.minX)); /* minLeftSideBearing */
    writeSHORT(out, FONT_UNITS_FLOOR(font->metrics.minX)); /* minRightSideBearing */
    writeSHORT(out, FONT_UNITS_CEIL(font->metrics.maxX)); /* xMaxExtent */
    writeSHORT(out, den);       /* caretSlopeRise */
    writeSHORT(out, num);       /* caretSlopeRun */
    writeSHORT(out, 0);         /* reserved */
    writeSHORT(out, 0);         /* reserved */
    writeSHORT(out, 0);         /* reserved */
    writeSHORT(out, 0);         /* reserved */
    writeSHORT(out, 0);         /* reserved */
    writeSHORT(out, 0);         /* metricDataFormat */
    writeSHORT(out, nummetrics); /* numberOfHMetrics */
    return 0;
}

static int 
writehmtx(FILE* out, FontPtr font)
{
    int rc, i;

    for(i = 0; i <= numglyphs; i++) {
        int code, width, lsb;
        code = findCode(current_cmap, i);
        if(code < 0)
            rc = -1;
        else
            rc = glyphMetrics(font, code, &width, &lsb, NULL, NULL, NULL);
        if(rc < 0) {
            width = UNITS_PER_EM / 3;
            lsb = 0;
        }
        if(i < nummetrics) {
            writeSHORT(out, FONT_UNITS(width));
            writeSHORT(out, FONT_UNITS(lsb));
        } else {
            writeSHORT(out, FONT_UNITS(lsb));
        }
    }
    return 0;
}

static int 
writeloca(FILE* out, FontPtr font)
{
    int i;

    /* All glyphs undefined -- loca table is empty, so offset 0 */
    for(i = 0; i < numglyphs; i++) {
        writeSHORT(out, 0);
    }
    writeSHORT(out, 0);
    return 0;
}

static int 
writemaxp(FILE* out, FontPtr font)
{
    writeLONG(out, 0x00010000); /* version */
    writeUSHORT(out, numglyphs); /* numGlyphs */
    writeUSHORT(out, 0);        /* maxPoints */
    writeUSHORT(out, 0);        /* maxContours */
    writeUSHORT(out, 0);        /* maxCompositePoints */
    writeUSHORT(out, 0);        /* maxCompositeContours */
    writeUSHORT(out, 1);        /* maxZones */
    writeUSHORT(out, 0);        /* maxTwilightPoints */
    writeUSHORT(out, 0);        /* maxStorage */
    writeUSHORT(out, 0);        /* maxFunctionDefs */
    writeUSHORT(out, 0);        /* maxInstructionDefs */
    writeUSHORT(out, 0);        /* maxStackElements */
    writeUSHORT(out, 0);        /* maxSizeOfInstructions */
    writeUSHORT(out, 0);        /* maxComponentElements */
    writeUSHORT(out, 0);        /* maxComponentDepth */
    return 0;
}

static int 
writename(FILE* out, FontPtr font)
{
    int i;
    int offset;

    writeUSHORT(out, 0);        /* format selector */
    writeUSHORT(out, font->numNames);
    writeUSHORT(out, 6 + font->numNames * 12); /* offset to string storage */
    offset = 0;
    for(i = 0; i < font->numNames; i++) {
        writeUSHORT(out, 3);    /* platform id -- Microsoft */
        writeUSHORT(out, 1);    /* encoding -- Unicode */
        writeUSHORT(out, 0x409); /* language id -- American English */
        writeUSHORT(out, font->names[i].nid); /* name id */
        writeUSHORT(out, font->names[i].size); /* string length */
        writeUSHORT(out, offset); /* string offset */
        offset += font->names[i].size;
    }
    for(i = 0; i < font->numNames; i++)
        writeCHARs(out, font->names[i].value, font->names[i].size);
    return 0;
}

static int 
writepost(FILE* out, FontPtr font)
{
    int i, rc, previous_width, width, fixed_pitch;

    fixed_pitch = 1;
    previous_width = -1;
    for(i = 0; i < FONT_CODES; i++) {
        rc = glyphMetrics(font, i, &width, NULL, NULL, NULL, NULL);
        if(rc < 0)
            continue;
        if(previous_width >= 0) {
            if(width != previous_width) {
                fixed_pitch = 0;
                break;
            }
        }
        previous_width = width;
    }
    
    writeULONG(out, 0x00030000); /* FormatType */
    writeULONG(out, font->italicAngle); /* italicAngle */
    writeSHORT(out, FONT_UNITS(font->metrics.underlinePosition));
    writeSHORT(out, FONT_UNITS(font->metrics.underlineThickness));
    writeULONG(out, fixed_pitch); /* isFixedPitch */
    writeULONG(out, 0);         /* minMemType42 */
    writeULONG(out, 0);         /* maxMemType42 */
    writeULONG(out, 0);         /* minMemType1 */
    writeULONG(out, 0);         /* maxMemType1 */
    return 0;
}

static int 
writeOS2(FILE* out, FontPtr font)
{
    int i;

    writeUSHORT(out, 5); /* version */
    writeSHORT(out, FONT_UNITS(font->metrics.awidth)); /* xAvgCharWidth; */
    writeUSHORT(out, font->weight);  /* usWeightClass; */
    writeUSHORT(out, font->width); /* usWidthClass; */
    writeSHORT(out, 0);         /* fsType; */
    writeSHORT(out, UNITS_PER_EM / 5); /* ySubscriptXSize; */
    writeSHORT(out, UNITS_PER_EM / 5); /* ySubscriptYSize; */
    writeSHORT(out, 0);         /* ySubscriptXOffset; */
    writeSHORT(out, UNITS_PER_EM / 5); /* ySubscriptYOffset; */
    writeSHORT(out, UNITS_PER_EM / 5); /* ySuperscriptXSize; */
    writeSHORT(out, UNITS_PER_EM / 5); /* ySuperscriptYSize; */
    writeSHORT(out, 0);         /* ySuperscriptXOffset; */
    writeSHORT(out, UNITS_PER_EM / 5); /* ySuperscriptYOffset; */
    writeSHORT(out, FONT_UNITS(font->metrics.underlineThickness));
    /* yStrikeoutSize; */
    writeSHORT(out, UNITS_PER_EM / 4); /* yStrikeoutPosition; */
    writeSHORT(out, 0);         /* sFamilyClass; */
    for(i = 0; i < 10; i++)
        writeBYTE(out, 0);      /* panose; */
    writeULONG(out, 0xFFFF);    /* ulUnicodeRange1; */
    writeULONG(out, 0xFFFF);    /* ulUnicodeRange2; */
    writeULONG(out, 0x03FF);    /* ulUnicodeRange3; */
    writeULONG(out, 0U);        /* ulUnicodeRange4; */
    writeULONG(out, font->foundry); /* achVendID[4]; */
    i = 0;
    if (font->flags & FACE_ITALIC)
	i |= 1 << 0;
    if (font->flags & FACE_BOLD)
	i |= 1 << 5;
    if (!i)
	i |= 1 << 6;
#ifndef NO_TYPO_METRICS
    i |= 1 << 7; /* USE_TYPO_METRICS instead usWin metrics for line spacing. */
#endif
    writeUSHORT(out, i);	/* fsSelection; */
    writeUSHORT(out, 0x20);     /* usFirstCharIndex; */
    writeUSHORT(out, 0xFFFD);   /* usLastCharIndex; */
    writeUSHORT(out, FONT_UNITS_CEIL(font->metrics.ascent)); /* sTypoAscender; */
    writeUSHORT(out, -FONT_UNITS_CEIL(font->metrics.descent)); /* sTypoDescender; */
    writeUSHORT(out, 0);	/* sTypoLineGap; */
#ifdef NO_TYPO_METRICS
    writeUSHORT(out, FONT_UNITS_CEIL(font->metrics.ascent)); /* usWinAscent; */
    writeUSHORT(out, FONT_UNITS_CEIL(font->metrics.descent)); /* usWinDescent; */
#else
    writeUSHORT(out, FONT_UNITS_CEIL(font->metrics.maxY)); /* usWinAscent; */
    writeUSHORT(out, -FONT_UNITS_FLOOR(font->metrics.minY)); /* usWinDescent; */
#endif
    writeULONG(out, 3);						/* ulCodePageRange1; */
    writeULONG(out, 0);         				/* ulCodePageRange2; */
    writeSHORT(out, FONT_UNITS_CEIL(font->metrics.xHeight));	/* sxHeight; */
    writeSHORT(out, FONT_UNITS_CEIL(font->metrics.capHeight));	/* sCapHeight; */
    writeUSHORT(out, 0); 					/* usDefaultChar; */
    writeUSHORT(out, 20); 					/* usBreakChar; */
    writeUSHORT(out, 0); 					/* usMaxContext; */
    writeUSHORT(out, 0); 					/* usLowerOpticalPointSize; */
    writeUSHORT(out, 0xffff); 					/* usUpperOpticalPointSize; */
    return 0;
}

static int 
writePCLT(FILE* out, FontPtr font)
{
    char name[16] = "X11 font        ";
    char filename[6] = "X11R00";
    unsigned char charComplement[8] = 
        {0xFF, 0xFF, 0xFF, 0xFF, 0x0B, 0xFF, 0xFF, 0xFE};
    int style, w, strokeWeight, widthType;

    style = 0;
    if(font->flags & FACE_ITALIC)
        style = 1;

    w = (font->weight + 50) / 100;
    if(w < 5)
        strokeWeight = w - 6;
    else if(w == 5)
        strokeWeight = 0;
    else
        strokeWeight = w - 4;

    if(font->width <= 2)
        widthType = -3;
    else if(font->width <= 4)
        widthType = -2;
    else if(font->width <= 6)
        widthType = 0;
    else if(font->width <= 7)
        widthType = 2;
    else
        widthType = 3;

    writeULONG(out, 0x00010000); /* version */
    writeULONG(out, 0);         /* FontNumber */
    writeUSHORT(out, FONT_UNITS(font->metrics.maxAwidth)); /* pitch */
    writeUSHORT(out, FONT_UNITS(font->metrics.xHeight));    /* xHeight */
    writeUSHORT(out, style);    /* style */
    writeUSHORT(out, 6 << 12);  /* TypeFamily */
    writeUSHORT(out, FONT_UNITS(font->metrics.xHeight)); /* CapHeight */
    writeUSHORT(out, 0);        /* SymbolSet */
    writeCHARs(out, name, 16);  /* TypeFace */
    writeBYTEs(out, charComplement, 8); /* CharacterComplement */
    writeCHARs(out, filename, 6); /* FileName */
    writeCHAR(out, strokeWeight); /* StrokeWeight */
    writeCHAR(out, widthType);  /* WidthType */
    writeCHAR(out, 1 << 6);     /* SerifStyle */
    writeCHAR(out, 0);          /* Reserved */
    return 0;
}