FontData: added support for UCS4 encoded CMAP tables
authorMatthias Mann
Sun Jan 22 13:26:46 2012 +0100 (3 months ago)
changeset 451cee127b8e37d
parent 450 43eb45ff2147
child 452 e4c7ee79a3ce
FontData: added support for UCS4 encoded CMAP tables
src/de/matthiasmann/twlthemeeditor/fontgen/FontData.java
     1.1 --- a/src/de/matthiasmann/twlthemeeditor/fontgen/FontData.java	Sun Jan 22 13:18:01 2012 +0100
     1.2 +++ b/src/de/matthiasmann/twlthemeeditor/fontgen/FontData.java	Sun Jan 22 13:26:46 2012 +0100
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2008-2010, Matthias Mann
     1.6 + * Copyright (c) 2008-2012, Matthias Mann
     1.7   *
     1.8   * All rights reserved.
     1.9   *
    1.10 @@ -154,7 +154,7 @@
    1.11  
    1.12                  upem = readUPEM(headSection);
    1.13                  postScriptName = readNAME(nameSection);
    1.14 -                readCMAP(cmapSection, glyphToUnicode);
    1.15 +                readCMAP(cmapSection);
    1.16  
    1.17                  if(kernSection != null) {
    1.18                      readKERN(kernSection);
    1.19 @@ -209,24 +209,40 @@
    1.20          return Math.round((units * size) / upem);
    1.21      }
    1.22  
    1.23 -    private void readCMAP(byte[] cmapSection, IntMap<int[]> glyphToUnicode) throws IOException {
    1.24 +    private void addGlyphCodePoint(int glyphIdx, int unicode) {
    1.25 +        int[] codepoints = glyphToUnicode.get(glyphIdx);
    1.26 +        if(codepoints == null) {
    1.27 +            codepoints = new int[] { unicode };
    1.28 +        } else {
    1.29 +            int len = codepoints.length;
    1.30 +            codepoints = Arrays.copyOf(codepoints, len+1);
    1.31 +            codepoints[len] = unicode;
    1.32 +        }
    1.33 +        glyphToUnicode.put(glyphIdx, codepoints);
    1.34 +        defined.set(unicode);
    1.35 +    }
    1.36 +    
    1.37 +    private void readCMAP(byte[] cmapSection) throws IOException {
    1.38          int numCMap = readUShort(cmapSection, 2);
    1.39 -        int cmapUniOffset = 0;
    1.40  
    1.41          for(int i=0 ; i<numCMap ; i++) {
    1.42              int cmapPID = readUShort(cmapSection, i*8 + 4);
    1.43              int cmapEID = readUShort(cmapSection, i*8 + 6);
    1.44  
    1.45 -            if (cmapPID == 3 && cmapEID == 1) {
    1.46 -                cmapUniOffset = readInt(cmapSection, i*8 + 8);
    1.47 -                break;
    1.48 +            if(cmapPID == 3 && cmapEID == 1) {
    1.49 +                readCMAP_USC2(cmapSection, readInt(cmapSection, i*8 + 8));
    1.50 +                return;
    1.51 +            }
    1.52 +            if(cmapPID == 3 && cmapEID == 10) {
    1.53 +                readCMAP_USC4(cmapSection, readInt(cmapSection, i*8 + 8));
    1.54 +                return;
    1.55              }
    1.56          }
    1.57 -
    1.58 -        if(cmapUniOffset == 0) {
    1.59 -            throw new IOException("No unicode mapping table found");
    1.60 -        }
    1.61 -
    1.62 +        
    1.63 +        throw new IOException("No unicode mapping table found");
    1.64 +    }
    1.65 +    
    1.66 +    private void readCMAP_USC2(byte[] cmapSection, int cmapUniOffset) throws IOException {
    1.67          int cmapFormat = readUShort(cmapSection, cmapUniOffset);
    1.68          if (cmapFormat != 4) {
    1.69              throw new IOException("Unsupported unicode table format: " + cmapFormat);
    1.70 @@ -261,20 +277,30 @@
    1.71                  }
    1.72  
    1.73                  if (glyphIdx != 0) {
    1.74 -                    int[] codepoints = glyphToUnicode.get(glyphIdx);
    1.75 -                    if(codepoints == null) {
    1.76 -                        codepoints = new int[] { unicode };
    1.77 -                    } else {
    1.78 -                        int len = codepoints.length;
    1.79 -                        codepoints = Arrays.copyOf(codepoints, len+1);
    1.80 -                        codepoints[len] = unicode;
    1.81 -                    }
    1.82 -                    glyphToUnicode.put(glyphIdx, codepoints);
    1.83 -                    defined.set(unicode);
    1.84 +                    addGlyphCodePoint(glyphIdx, unicode);
    1.85                  }
    1.86              }
    1.87          }
    1.88      }
    1.89 +    
    1.90 +    private void readCMAP_USC4(byte[] cmapSection, int cmapUniOffset) throws IOException {
    1.91 +        int cmapFormat = readUShort(cmapSection, cmapUniOffset);
    1.92 +        if (cmapFormat != 12) {
    1.93 +            throw new IOException("Unsupported unicode table format: " + cmapFormat);
    1.94 +        }
    1.95 +
    1.96 +        int nGroups = readInt(cmapSection, cmapUniOffset + 12);
    1.97 +
    1.98 +        for (int group=0 ; group<nGroups ; group++) {
    1.99 +            int startCharCode = readUShort(cmapSection, cmapUniOffset + 16 + group*12);
   1.100 +            int endCharCode   = readUShort(cmapSection, cmapUniOffset + 16 + group*12 + 4);
   1.101 +            int startGlyphID  = readShort (cmapSection, cmapUniOffset + 16 + group*12 + 8);
   1.102 +
   1.103 +            for(int i=startCharCode ; i<=endCharCode ; i++) {
   1.104 +                addGlyphCodePoint(startGlyphID+(i-startCharCode), i);
   1.105 +            }
   1.106 +        }
   1.107 +    }
   1.108  
   1.109      private void readKERN(byte[] kernSection) {
   1.110          int version = readUShort(kernSection, 0);