aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/fonts/TTFFile.java
blob: e04387432198bf24aefc8f8037e5d4345b1a3794 (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
/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

package org.apache.fop.fonts;
import java.io.*;
import java.util.Iterator;
import java.util.HashMap;
import java.util.ArrayList;

import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;

/**
 * Reads a TrueType file or a TrueType Collection.
 * The TrueType spec can be found at the Microsoft
 * Typography site: http://www.microsoft.com/truetype/
 */
public class TTFFile {
    static final byte NTABS = 24;
    static final int NMACGLYPHS = 258;
    static final int MAX_CHAR_CODE = 255;
    static final int ENC_BUF_SIZE = 1024;

    private Logger log;

    static String encoding = "WinAnsiEncoding";    // Default encoding
    short firstChar = 0;
    boolean is_embeddable = true;
    boolean hasSerifs = true;
    HashMap dirTabs;                             // Table directory
    HashMap kerningTab;                          // for CIDs
    HashMap ansiKerningTab;                      // For winAnsiEncoding
    ArrayList cmaps;
    ArrayList unicodeMapping;                         //

    int upem;                                      // unitsPerEm from "head" table
    int nhmtx;                                     // Number of horizontal metrics
    int post_format;
    int loca_format;
    long lastLoca = 0;                             // offset to last loca
    int nglyphs;                                   // Number of glyphs in font (read from "maxp" table)
    int nmglyphs;                                  // Used in fixWidths - remove?

    TTFMtxEntry mtx_tab[];                         // Contains glyph data
    int[] mtx_encoded = null;

    String fontName = "";
    String fullName = "";
    String notice = "";
    String familyName = "";
    String subFamilyName = "";

    long italicAngle = 0;
    long isFixedPitch = 0;
    int fontBBox1 = 0;
    int fontBBox2 = 0;
    int fontBBox3 = 0;
    int fontBBox4 = 0;
    int capHeight = 0;
    int underlinePosition = 0;
    int underlineThickness = 0;
    int xHeight = 0;
    int ascender = 0;
    int descender = 0;

    short lastChar = 0;

    int ansiWidth[];
    HashMap ansiIndex;

    public void setLogger(Logger l) {
        log = l;
    }

    /**
     * Position inputstream to position indicated
     * in the dirtab offset + offset
     */
    void seek_tab(FontFileReader in, String name,
                  long offset) throws IOException {
        TTFDirTabEntry dt = (TTFDirTabEntry)dirTabs.get(name);
        if (dt == null) {
            log.error("Dirtab " + name + " not found.");
            return;
        }

        in.seek_set(dt.offset + offset);
    }

    /**
     * Convert from truetype unit to pdf unit based on the
     * unitsPerEm field in the "head" table
     * @param n truetype unit
     * @return pdf unit
     */
    int get_ttf_funit(int n) {
        int ret;
        if (n < 0) {
            long rest1 = n % upem;
            long storrest = 1000 * rest1;
            long ledd2 = rest1 / storrest;
            ret = -((-1000 * n) / upem - (int)ledd2);
        } else {
            ret = (n / upem) * 1000 + ((n % upem) * 1000) / upem;
        }

        return ret;
    }

    /**
     * Read the cmap table,
     * return false if the table is not present or only unsupported
     * tables are present. Currently only unicode cmaps are supported.
     * Set the unicodeIndex in the TTFMtxEntries and fills in the
     * cmaps vector.
     */
    private boolean readCMAP(FontFileReader in) throws IOException {

        unicodeMapping = new ArrayList();

        /**
         * Read CMAP table and correct mtx_tab.index
         */
        int mtxPtr = 0;

        seek_tab(in, "cmap", 2);
        int num_cmap = in.readTTFUShort();    // Number of cmap subtables
        long cmap_unioffset = 0;

        log.info(num_cmap+" cmap tables");

        /*
         * Read offset for all tables
         * We are only interested in the unicode table
         */
        for (int i = 0; i < num_cmap; i++) {
            int cmap_pid = in.readTTFUShort();
            int cmap_eid = in.readTTFUShort();
            long cmap_offset = in.readTTFULong();

            log.debug("Platform ID: "+cmap_pid+
             " Encoding: "+cmap_eid);

            if (cmap_pid == 3 && cmap_eid == 1)
                cmap_unioffset = cmap_offset;
        }

        if (cmap_unioffset <= 0) {
            log.fatalError("Unicode cmap table not present");
            log.fatalError("Unsupported format: Aborting");
            return false;
        }

        // Read unicode cmap
        seek_tab(in, "cmap", cmap_unioffset);
        int cmap_format = in.readTTFUShort();
        int cmap_length = in.readTTFUShort();

        log.info("CMAP format: "+cmap_format);
        if (cmap_format == 4) {
            in.skip(2);    // Skip version number
            int cmap_segCountX2 = in.readTTFUShort();
            int cmap_searchRange = in.readTTFUShort();
            int cmap_entrySelector = in.readTTFUShort();
            int cmap_rangeShift = in.readTTFUShort();


            log.debug("segCountX2   : "+cmap_segCountX2);
            log.debug("searchRange  : "+cmap_searchRange);
            log.debug("entrySelector: "+cmap_entrySelector);
            log.debug("rangeShift   : "+cmap_rangeShift);
             

            int cmap_endCounts[] = new int[cmap_segCountX2 / 2];
            int cmap_startCounts[] = new int[cmap_segCountX2 / 2];
            int cmap_deltas[] = new int[cmap_segCountX2 / 2];
            int cmap_rangeOffsets[] = new int[cmap_segCountX2 / 2];

            for (int i = 0; i < (cmap_segCountX2 / 2); i++) {
                cmap_endCounts[i] = in.readTTFUShort();
            }

            in.skip(2);    // Skip reservedPad

            for (int i = 0; i < (cmap_segCountX2 / 2); i++) {
                cmap_startCounts[i] = in.readTTFUShort();
            }

            for (int i = 0; i < (cmap_segCountX2 / 2); i++) {
                cmap_deltas[i] = in.readTTFShort();
            }

            int startRangeOffset = in.getCurrentPos();

            for (int i = 0; i < (cmap_segCountX2 / 2); i++) {
                cmap_rangeOffsets[i] = in.readTTFUShort();
            }

            int glyphIdArrayOffset = in.getCurrentPos();

            // Insert the unicode id for the glyphs in mtx_tab
            // and fill in the cmaps ArrayList

            for (int i = 0; i < cmap_startCounts.length; i++) {
                
                  log.debug(i+ ": "+cmap_startCounts[i]+
                  " - "+cmap_endCounts[i]);
                 
                for (int j = cmap_startCounts[i]; j <= cmap_endCounts[i];
                        j++) {

                    // Update lastChar
                    if (j < 256 && j > lastChar)
                        lastChar = (short)j;

                    if (mtxPtr < mtx_tab.length) {
                        int glyphIdx;
                        // the last character 65535 = .notdef
                        // may have a range offset
                        if (cmap_rangeOffsets[i] != 0 && j != 65535) {
                            int glyphOffset =
                                glyphIdArrayOffset
                                + ((cmap_rangeOffsets[i] / 2) + (j - cmap_startCounts[i]) + (i) - cmap_segCountX2 / 2)
                                  * 2;
                            in.seek_set(glyphOffset);
                            glyphIdx = (in.readTTFUShort() + cmap_deltas[i])
                                       & 0xffff;

                            unicodeMapping.add(new UnicodeMapping(glyphIdx,
                                    j));
                            mtx_tab[glyphIdx].unicodeIndex.add(new Integer(j));


                            // Also add winAnsiWidth
                            ArrayList v =
                                (ArrayList)ansiIndex.get(new Integer(j));
                            if (v != null) {
                                for (Iterator e = v.listIterator();
                                        e.hasNext(); ) {
                                    Integer aIdx =
                                        (Integer)e.next();
                                    ansiWidth[aIdx.intValue()] =
                                        mtx_tab[glyphIdx].wx;
                                    
                                      log.debug("Added width "+
                                      mtx_tab[glyphIdx].wx +
                                      " uni: " + j +
                                      " ansi: " + aIdx.intValue());
                                     
                                }
                            }
                            
                              log.debug("Idx: "+
                              glyphIdx +
                              " Delta: " + cmap_deltas[i]+
                              " Unicode: " + j +
                              " name: " +
                              mtx_tab[glyphIdx].name);
                             

                        } else {

                            glyphIdx = (j + cmap_deltas[i]) & 0xffff;

                            if (glyphIdx < mtx_tab.length)
                                mtx_tab[glyphIdx].unicodeIndex.add(new Integer(j));
                            else
                                log.debug("Glyph " + glyphIdx
                                                   + " out of range: "
                                                   + mtx_tab.length);

                            unicodeMapping.add(new UnicodeMapping(glyphIdx,
                                    j));
                            if (glyphIdx < mtx_tab.length)
                                mtx_tab[glyphIdx].unicodeIndex.add(new Integer(j));
                            else
                                log.debug("Glyph " + glyphIdx
                                                   + " out of range: "
                                                   + mtx_tab.length);



                            // Also add winAnsiWidth
                            ArrayList v =
                                (ArrayList)ansiIndex.get(new Integer(j));
                            if (v != null) {
                                for (Iterator e = v.listIterator();
                                        e.hasNext(); ) {
                                    Integer aIdx =
                                        (Integer)e.next();
                                    ansiWidth[aIdx.intValue()] =
                                        mtx_tab[glyphIdx].wx;
                                }
                            }

                            
                              log.debug("IIdx: "+
                              mtxPtr +
                              " Delta: " + cmap_deltas[i]+
                              " Unicode: " + j +
                              " name: " +
                              mtx_tab[(j+cmap_deltas[i]) & 0xffff].name);
                            
                        }
                        if (glyphIdx < mtx_tab.length) {
                            if (mtx_tab[glyphIdx].unicodeIndex.size() < 2) {
                                mtxPtr++;
                            }
                        }
                    }
                }
            }
        }
        return true;
    }



    /**
     * Print first char/last char
     */
    private void print_max_min() {
        int min = 255;
        int max = 0;
        for (int i = 0; i < mtx_tab.length; i++) {
            if (mtx_tab[i].index < min)
                min = mtx_tab[i].index;
            if (mtx_tab[i].index > max)
                max = mtx_tab[i].index;
        }
        log.info("Min: " + min);
        log.info("Max: " + max);
    }

    public void readFont(FontFileReader in) throws IOException {
        readFont(in, (String)null);
    }

    /**
     * initialize the ansiWidths array (for winAnsiEncoding)
     * and fill with the missingwidth
     */
    private void initAnsiWidths() {
        ansiWidth = new int[256];
        for (int i = 0; i < 256; i++)
            ansiWidth[i] = mtx_tab[0].wx;

        // Create an index hash to the ansiWidth
        // Can't just index the winAnsiEncoding when inserting widths
        // same char (eg bullet) is repeated more than one place
        ansiIndex = new HashMap();
        for (int i = 32; i < Glyphs.winAnsiEncoding.length; i++) {
            Integer ansi = new Integer(i);
            Integer uni = new Integer((int)Glyphs.winAnsiEncoding[i]);

            ArrayList v = (ArrayList)ansiIndex.get(uni);
            if (v == null) {
                v = new ArrayList();
                ansiIndex.put(uni, v);
            }
            v.add(ansi);
        }
    }


    /**
     * Read the font data
     * If the fontfile is a TrueType Collection (.ttc file)
     * The name of the font to read data for must be supplied,
     * else the name is ignored
     */
    public boolean readFont(FontFileReader in, String name) throws IOException {

        /*
         * Check if TrueType collection, and that the name
         * exists in the collection
         */
        if (!checkTTC(in, name))
            throw new IOException("Failed to read font");

        readDirTabs(in);
        readFontHeader(in);
        getNumGlyphs(in);
        log.info("Number of glyphs in font: " + nglyphs);
        readHorizontalHeader(in);
        readHorizontalMetrics(in);
        initAnsiWidths();
        readPostscript(in);
        readOS2(in);
        readIndexToLocation(in);
        readGlyf(in);
        readName(in);
        readPCLT(in);
        // Read cmap table and fill in ansiwidths
        boolean valid = readCMAP(in);
        if(!valid) {
            return false;
        }
        // Create cmaps for bfentries
        createCMaps();
        // print_max_min();

        readKerning(in);
        return true;
    }

    private void createCMaps() {
        cmaps = new ArrayList();
        TTFCmapEntry tce = new TTFCmapEntry();

        Iterator e = unicodeMapping.listIterator();
        UnicodeMapping um = (UnicodeMapping)e.next();
        UnicodeMapping lastMapping = um;

        tce.unicodeStart = um.uIdx;
        tce.glyphStartIndex = um.gIdx;

        while (e.hasNext()) {
            um = (UnicodeMapping)e.next();
            if (((lastMapping.uIdx + 1) != um.uIdx)
                    || ((lastMapping.gIdx + 1) != um.gIdx)) {
                tce.unicodeEnd = lastMapping.uIdx;
                cmaps.add(tce);

                tce = new TTFCmapEntry();
                tce.unicodeStart = um.uIdx;
                tce.glyphStartIndex = um.gIdx;
            }
            lastMapping = um;
        }

        tce.unicodeEnd = um.uIdx;
        cmaps.add(tce);
    }

    public void printStuff() {
        System.out.println("Font name: " + fontName);
        System.out.println("Full name: " + fullName);
        System.out.println("Family name: " + familyName);
        System.out.println("Subfamily name: " + subFamilyName);
        System.out.println("Notice:    " + notice);
        System.out.println("xHeight:   " + (int)get_ttf_funit(xHeight));
        System.out.println("capheight: " + (int)get_ttf_funit(capHeight));

        int italic = (int)(italicAngle >> 16);
        System.out.println("Italic: " + italic);
        System.out.print("ItalicAngle: " + (short)(italicAngle / 0x10000));
        if ((italicAngle % 0x10000) > 0)
            System.out.print("."
                             + (short)((italicAngle % 0x10000) * 1000)
                               / 0x10000);
        System.out.println();
        System.out.println("Ascender:    " + get_ttf_funit(ascender));
        System.out.println("Descender:   " + get_ttf_funit(descender));
        System.out.println("FontBBox:    [" + (int)get_ttf_funit(fontBBox1)
                           + " " + (int)get_ttf_funit(fontBBox2) + " "
                           + (int)get_ttf_funit(fontBBox3) + " "
                           + (int)get_ttf_funit(fontBBox4) + "]");
    }

    public static void main(String[] args) {
        int level = ConsoleLogger.LEVEL_WARN;
        Logger log = new ConsoleLogger(level);
        try {
            TTFFile ttfFile = new TTFFile();
            ttfFile.setLogger(log);

            FontFileReader reader = new FontFileReader(args[0]);

            String name = null;
            if (args.length >= 2)
                name = args[1];

            ttfFile.readFont(reader, name);
            ttfFile.printStuff();

        } catch (IOException ioe) {
            log.error("problem reading font: " + ioe.toString(), ioe);
        }
    }

    public String getWindowsName() {
        return new String(familyName + "," + subFamilyName);
    }

    public String getPostscriptName() {
        if ("Regular".equals(subFamilyName) || "Roman".equals(subFamilyName))
            return familyName;
        else
            return familyName + "," + subFamilyName;
    }

    public String getFamilyName() {
        return familyName;
    }

    public String getCharSetName() {
        return encoding;
    }

    public int getCapHeight() {
        return (int)get_ttf_funit(capHeight);
    }

    public int getXHeight() {
        return (int)get_ttf_funit(xHeight);
    }

    public int getFlags() {
        int flags = 32;    // Use Adobe Standard charset
        if (italicAngle != 0)
            flags = flags | 64;
        if (isFixedPitch != 0)
            flags = flags | 2;
        if (hasSerifs)
            flags = flags | 1;
        return flags;
    }


    public String getStemV() {
        return "0";
    }

    public String getItalicAngle() {
        String ia = Short.toString((short)(italicAngle / 0x10000));

        // This is the correct italic angle, however only int italic
        // angles are supported at the moment so this is commented out.
        /*
         * if ((italicAngle % 0x10000) > 0 )
         * ia=ia+(comma+Short.toString((short)((short)((italicAngle % 0x10000)*1000)/0x10000)));
         */
        return ia;
    }

    public int[] getFontBBox() {
        int[] fbb = new int[4];
        fbb[0] = (int)get_ttf_funit(fontBBox1);
        fbb[1] = (int)get_ttf_funit(fontBBox2);
        fbb[2] = (int)get_ttf_funit(fontBBox3);
        fbb[3] = (int)get_ttf_funit(fontBBox4);

        return fbb;
    }

    public int getLowerCaseAscent() {
        return (int)get_ttf_funit(ascender);
    }

    public int getLowerCaseDescent() {
        return (int)get_ttf_funit(descender);
    }

    // This is only for WinAnsiEncoding, so the last char is
    // the last char < 256
    public short getLastChar() {
        return lastChar;
    }

    public short getFirstChar() {
        return firstChar;
    }

    public int[] getWidths() {
        int[] wx = new int[mtx_tab.length];
        for (int i = 0; i < wx.length; i++)
            wx[i] = (int)get_ttf_funit(mtx_tab[i].wx);

        return wx;
    }

    public int getCharWidth(int idx) {
        return (int)get_ttf_funit(ansiWidth[idx]);
    }

    public HashMap getKerning() {
        return kerningTab;
    }

    public HashMap getAnsiKerning() {
        return ansiKerningTab;
    }

    public boolean isEmbeddable() {
        return is_embeddable;
    }


    /**
     * Read Table Directory from the current position in the
     * FontFileReader and fill the global HashMap dirTabs
     * with the table name (String) as key and a TTFDirTabEntry
     * as value.
     */
    protected void readDirTabs(FontFileReader in) throws IOException {
        in.skip(4);    // TTF_FIXED_SIZE
        int ntabs = in.readTTFUShort();
        in.skip(6);    // 3xTTF_USHORT_SIZE

        dirTabs = new HashMap();
        TTFDirTabEntry[] pd = new TTFDirTabEntry[ntabs];
        log.debug("Reading " + ntabs + " dir tables");
        for (int i = 0; i < ntabs; i++) {
            pd[i] = new TTFDirTabEntry();
            dirTabs.put(pd[i].read(in), pd[i]);
        }
    }

    /**
     * Read the "head" table, this reads the bounding box and
     * sets the upem (unitsPerEM) variable
     */
    protected void readFontHeader(FontFileReader in) throws IOException {
        seek_tab(in, "head", 2 * 4 + 2 * 4 + 2);
        upem = in.readTTFUShort();

        in.skip(16);

        fontBBox1 = in.readTTFShort();
        fontBBox2 = in.readTTFShort();
        fontBBox3 = in.readTTFShort();
        fontBBox4 = in.readTTFShort();

        in.skip(2 + 2 + 2);

        loca_format = in.readTTFShort();
    }

    /**
     * Read the number of glyphs from the "maxp" table
     */
    protected void getNumGlyphs(FontFileReader in) throws IOException {
        seek_tab(in, "maxp", 4);
        nglyphs = in.readTTFUShort();
    }


    /**
     * Read the "hhea" table to find the ascender and descender and
     * size of "hmtx" table, i.e. a fixed size font might have only
     * one width
     */
    protected void readHorizontalHeader(FontFileReader in)
            throws IOException {
        seek_tab(in, "hhea", 4);
        ascender = in.readTTFShort();    // Use sTypoAscender in "OS/2" table?
        descender = in.readTTFShort();    // Use sTypoDescender in "OS/2" table?

        in.skip(2 + 2 + 3 * 2 + 8 * 2);
        nhmtx = in.readTTFUShort();
        log.debug("Number of horizontal metrics: " + nhmtx);
    }

    /**
     * Read "hmtx" table and put the horizontal metrics
     * in the mtx_tab array. If the number of metrics is less
     * than the number of glyphs (eg fixed size fonts), extend
     * the mtx_tab array and fill in the missing widths
     */
    protected void readHorizontalMetrics(FontFileReader in)
            throws IOException {
        seek_tab(in, "hmtx", 0);

        int mtx_size = (nglyphs > nhmtx) ? nglyphs : nhmtx;
        mtx_tab = new TTFMtxEntry[mtx_size];

        log.debug("*** Widths array: \n");
        for (int i = 0; i < mtx_size; i++)
            mtx_tab[i] = new TTFMtxEntry();
        for (int i = 0; i < nhmtx; i++) {
            mtx_tab[i].wx = in.readTTFUShort();
            mtx_tab[i].lsb = in.readTTFUShort();
            
            log.debug("   width["+i+"] = "+
             get_ttf_funit(mtx_tab[i].wx)+";");
        }

        if (nhmtx < mtx_size) {
            // Fill in the missing widths
            int lastWidth = mtx_tab[nhmtx - 1].wx;
            for (int i = nhmtx; i < mtx_size; i++) {
                mtx_tab[i].wx = lastWidth;
                mtx_tab[i].lsb = in.readTTFUShort();
            }
        }
    }


    /**
     * Read the "post" table
     * containing the postscript names of the glyphs.
     */
    private final void readPostscript(FontFileReader in) throws IOException {
        String[] ps_glyphs_buf;
        int i, k, l;

        seek_tab(in, "post", 0);
        post_format = in.readTTFLong();
        italicAngle = in.readTTFULong();
        underlinePosition = in.readTTFShort();
        underlineThickness = in.readTTFShort();
        isFixedPitch = in.readTTFULong();

        in.skip(4 * 4);

        log.debug("Post format: "+post_format);
        switch (post_format) {
        case 0x00010000:
            log.debug("Postscript format 1");
            for (i = 0; i < Glyphs.mac_glyph_names.length; i++) {
                mtx_tab[i].name = Glyphs.mac_glyph_names[i];
            }
            break;
        case 0x00020000:
            log.debug("Postscript format 2");
            int numGlyphStrings = 0;
            l = in.readTTFUShort();      // Num Glyphs
            // short minIndex=256;
            for (i = 0; i < l; i++) {    // Read indexes
                mtx_tab[i].index = in.readTTFUShort();
                // if (minIndex > mtx_tab[i].index)
                // minIndex=(short)mtx_tab[i].index;

                if (mtx_tab[i].index > 257)
                    numGlyphStrings++;

                    log.debug("Post index: "+mtx_tab[i].index);
            }
            // firstChar=minIndex;
            ps_glyphs_buf = new String[numGlyphStrings];
            log.debug("Reading " + numGlyphStrings +
                      " glyphnames" + ", was n num glyphs="+l);
            for (i = 0; i < ps_glyphs_buf.length; i++) {
                ps_glyphs_buf[i] = in.readTTFString(in.readTTFUByte());
            }

            for (i = 0; i < l; i++) {
                if (mtx_tab[i].index < NMACGLYPHS) {
                    mtx_tab[i].name =
                        Glyphs.mac_glyph_names[mtx_tab[i].index];
                } else {
                    k = mtx_tab[i].index - NMACGLYPHS;

                     log.debug(k+" i="+i+" mtx="+mtx_tab.length+
                      " ps="+ps_glyphs_buf.length);

                    mtx_tab[i].name = ps_glyphs_buf[k];
                }
            }

            break;
        case 0x00030000:
            // Postscript format 3 contains no glyph names
            log.debug("Postscript format 3");
            break;
        default:
            log.error("Unknown Postscript format : " + post_format);
        }
    }


    /**
     * Read the "OS/2" table
     */
    private final void readOS2(FontFileReader in) throws IOException {
        // Check if font is embeddable
        if (dirTabs.get("OS/2") != null) {
            seek_tab(in, "OS/2", 2 * 4);
            int fsType = in.readTTFUShort();
            if (fsType == 2)
                is_embeddable = false;
            else
                is_embeddable = true;
        } else
            is_embeddable = true;
    }

    /**
     * Read the "loca" table
     */
    protected final void readIndexToLocation(FontFileReader in)
            throws IOException {
        seek_tab(in, "loca", 0);
        for (int i = 0; i < nglyphs; i++) {
            mtx_tab[i].offset = (loca_format == 1 ? in.readTTFULong()
                                 : (in.readTTFUShort() << 1));
        }
        lastLoca = (loca_format == 1 ? in.readTTFULong()
                    : (in.readTTFUShort() << 1));
    }

    /**
     * Read the "glyf" table to find the bounding boxes
     */
    private final void readGlyf(FontFileReader in) throws IOException {
        TTFDirTabEntry dirTab = (TTFDirTabEntry)dirTabs.get("glyf");
        for (int i = 0; i < (nglyphs - 1); i++) {
            if (mtx_tab[i].offset != mtx_tab[i + 1].offset) {
                in.seek_set(dirTab.offset + mtx_tab[i].offset);
                in.skip(2);
                mtx_tab[i].bbox[0] = in.readTTFShort();
                mtx_tab[i].bbox[1] = in.readTTFShort();
                mtx_tab[i].bbox[2] = in.readTTFShort();
                mtx_tab[i].bbox[3] = in.readTTFShort();
            } else {
                mtx_tab[i].bbox[0] = mtx_tab[0].bbox[0];
                mtx_tab[i].bbox[1] = mtx_tab[0].bbox[1];
                mtx_tab[i].bbox[2] = mtx_tab[0].bbox[2];
                mtx_tab[i].bbox[3] = mtx_tab[0].bbox[3];
            }
        }


        long n = ((TTFDirTabEntry)dirTabs.get("glyf")).offset;
        for (int i = 0; i < nglyphs; i++) {
            if ((i + 1) >= mtx_tab.length
                    || mtx_tab[i].offset != mtx_tab[i + 1].offset) {
                in.seek_set(n + mtx_tab[i].offset);
                in.skip(2);
                mtx_tab[i].bbox[0] = in.readTTFShort();
                mtx_tab[i].bbox[1] = in.readTTFShort();
                mtx_tab[i].bbox[2] = in.readTTFShort();
                mtx_tab[i].bbox[3] = in.readTTFShort();
            } else {
                mtx_tab[i].bbox[0] = mtx_tab[0].bbox[0];
                mtx_tab[i].bbox[1] = mtx_tab[0].bbox[0];
                mtx_tab[i].bbox[2] = mtx_tab[0].bbox[0];
                mtx_tab[i].bbox[3] = mtx_tab[0].bbox[0];
            }
            log.debug(mtx_tab[i].toString(this));
        }
    }

    /**
     * Read the "name" table
     */
    private final void readName(FontFileReader in) throws IOException {
        int platform_id, encoding_id, language_id;

        seek_tab(in, "name", 2);
        int i = in.getCurrentPos();
        int n = in.readTTFUShort();
        int j = in.readTTFUShort() + i - 2;
        i += 2 * 2;

        while (n-- > 0) {
            // log.debug("Iteration: "+n);
            in.seek_set(i);
            platform_id = in.readTTFUShort();
            encoding_id = in.readTTFUShort();
            language_id = in.readTTFUShort();

            int k = in.readTTFUShort();
            int l = in.readTTFUShort();

            if (((platform_id == 1 || platform_id == 3) && (encoding_id == 0 || encoding_id == 1))
                    && (k == 1 || k == 2 || k == 0 || k == 4 || k == 6)) {
                // if (k==1 || k==2 || k==0 || k==4 || k==6) {
                in.seek_set(j + in.readTTFUShort());
                String txt = in.readTTFString(l);
                // log.debug(platform_id+" "+encoding_id+
                // " "+k+" "+txt);
                switch (k) {
                case 0:
                    notice = txt;
                    break;
                case 1:
                    familyName = txt;
                    break;
                case 2:
                    subFamilyName = txt;
                    break;
                case 4:
                    fullName = txt;
                    break;
                case 6:
                    fontName = txt;
                    break;
                }
                if (!notice.equals("") &&!fullName.equals("")
                        &&!fontName.equals("") &&!familyName.equals("")
                        &&!subFamilyName.equals("")) {
                    break;
                }
            }
            i += 6 * 2;
        }
    }

    /**
     * Read the "PCLT" table to find xHeight and capHeight
     */
    private final void readPCLT(FontFileReader in) throws IOException {
        TTFDirTabEntry dirTab = (TTFDirTabEntry)dirTabs.get("PCLT");
        if (dirTab != null) {
            in.seek_set(dirTab.offset + 4 + 4 + 2);
            xHeight = in.readTTFUShort();
            in.skip(2 * 2);
            capHeight = in.readTTFUShort();
            in.skip(2 + 16 + 8 + 6 + 1 + 1);

            int serifStyle = in.readTTFUByte();
            serifStyle = serifStyle >> 6;
            serifStyle = serifStyle & 3;
            if (serifStyle == 1)
                hasSerifs = false;
            else
                hasSerifs = true;

        } else {
            // Approximate capHeight from height of "H"
            // It's most unlikly that a font misses the PCLT table
            // This also assumes that psocriptnames exists ("H")
            // Should look it up int the cmap (that wouldn't help
            // for charsets without H anyway...)
            for (int i = 0; i < mtx_tab.length; i++) {
                if ("H".equals(mtx_tab[i].name))
                    capHeight = mtx_tab[i].bbox[3] - mtx_tab[i].bbox[1];
            }
        }
    }

    /**
     * Read the kerning table, create a table for both CIDs and
     * winAnsiEncoding
     */
    private final void readKerning(FontFileReader in) throws IOException {
        // Read kerning
        kerningTab = new HashMap();
        ansiKerningTab = new HashMap();
        TTFDirTabEntry dirTab = (TTFDirTabEntry)dirTabs.get("kern");
        if (dirTab != null) {
            seek_tab(in, "kern", 2);
            for (int n = in.readTTFUShort(); n > 0; n--) {
                in.skip(2 * 2);
                int k = in.readTTFUShort();
                if (!((k & 1) != 0) || (k & 2) != 0 || (k & 4) != 0)
                    return;
                if ((k >> 8) != 0)
                    continue;

                k = in.readTTFUShort();
                in.skip(3 * 2);
                while (k-- > 0) {
                    int i = in.readTTFUShort();
                    int j = in.readTTFUShort();
                    int kpx = in.readTTFShort();
                    if (kpx != 0) {
                        // CID table
                        Integer iObj = new Integer(i);
                        HashMap adjTab = (HashMap)kerningTab.get(iObj);
                        if (adjTab == null)
                            adjTab = new HashMap();
                        adjTab.put(new Integer(j),
                                   new Integer((int)get_ttf_funit(kpx)));
                        kerningTab.put(iObj, adjTab);
                    }
                }
            }
            // log.debug(kerningTab.toString());

            // Create winAnsiEncoded kerning table

            for (Iterator ae = kerningTab.keySet().iterator(); ae.hasNext(); ) {
                Integer cidKey = (Integer)ae.next();
                HashMap akpx = new HashMap();
                HashMap ckpx = (HashMap)kerningTab.get(cidKey);

                for (Iterator aee = ckpx.keySet().iterator(); aee.hasNext(); ) {
                    Integer cidKey2 = (Integer)aee.next();
                    Integer kern = (Integer)ckpx.get(cidKey2);

                    for (Iterator uniMap = mtx_tab[cidKey2.intValue()].unicodeIndex.listIterator();
                            uniMap.hasNext(); ) {
                        Integer unicodeKey = (Integer)uniMap.next();
                        Integer[] ansiKeys =
                            unicodeToWinAnsi(unicodeKey.intValue());
                        for (int u = 0; u < ansiKeys.length; u++) {
                            akpx.put(ansiKeys[u], kern);
                        }
                    }
                }

                if (akpx.size() > 0)
                    for (Iterator uniMap = mtx_tab[cidKey.intValue()].unicodeIndex.listIterator();
                            uniMap.hasNext(); ) {
                        Integer unicodeKey = (Integer)uniMap.next();
                        Integer[] ansiKeys =
                            unicodeToWinAnsi(unicodeKey.intValue());
                        for (int u = 0; u < ansiKeys.length; u++) {
                            ansiKerningTab.put(ansiKeys[u], akpx);
                        }
                    }
            }
        }
    }

    /**
     * Return a vector with TTFCmapEntry
     */
    public ArrayList getCMaps() {
        return cmaps;
    }

    /**
     * Check if this is a TrueType collection and that the given
     * name exists in the collection.
     * If it does, set offset in fontfile to the beginning of
     * the Table Directory for that font
     * @ return true if not collection or font name present, false
     * otherwise
     */
    protected final boolean checkTTC(FontFileReader in, String name) throws IOException {
        String tag = in.readTTFString(4);

        if ("ttcf".equals(tag)) {
            // This is a TrueType Collection
            in.skip(4);

            // Read directory offsets
            int numDirectories = (int)in.readTTFULong();
            // int numDirectories=in.readTTFUShort();
            long[] dirOffsets = new long[numDirectories];
            for (int i = 0; i < numDirectories; i++) {
                dirOffsets[i] = in.readTTFULong();
            }

            log.debug("This is a TrueType collection file with"
                                   + numDirectories + " fonts");
            log.debug("Containing the following fonts: ");
            // Read all the directories and name tables to check
            // If the font exists - this is a bit ugly, but...
            boolean found = false;

            // Iterate through all name tables even if font
            // Is found, just to show all the names
            long dirTabOffset = 0;
            for (int i = 0; (i < numDirectories); i++) {
                in.seek_set(dirOffsets[i]);
                readDirTabs(in);

                readName(in);

                if (fullName.equals(name)) {
                    found = true;
                    dirTabOffset = dirOffsets[i];
                    log.debug("* " + fullName);
                } else {
                    log.debug(fullName);
                }

                // Reset names
                notice = "";
                fullName = "";
                familyName = "";
                fontName = "";
                subFamilyName = "";
            }

            in.seek_set(dirTabOffset);
            return found;
        } else {
            in.seek_set(0);
            return true;
        }
    }

    /*
     * Helper classes, they are not very efficient, but that really
     * doesn't matter...
     */
    private Integer[] unicodeToWinAnsi(int unicode) {
        ArrayList ret = new ArrayList();
        for (int i = 32; i < Glyphs.winAnsiEncoding.length; i++)
            if (unicode == Glyphs.winAnsiEncoding[i])
                ret.add(new Integer(i));
        return (Integer[])ret.toArray(new Integer[0]);
    }

}


/**
 * Key-value helper class
 */
class UnicodeMapping {
    int uIdx;
    int gIdx;
    UnicodeMapping(int gIdx, int uIdx) {
        this.uIdx = uIdx;
        this.gIdx = gIdx;
    }

}