Class TTFSubsetter


  • public final class TTFSubsetter
    extends java.lang.Object
    Subsetter for TrueType (TTF) fonts.

    Originally developed by Wolfgang Glas for Sketch.

    • Constructor Summary

      Constructors 
      Constructor Description
      TTFSubsetter​(TrueTypeFont ttf)
      Creates a subsetter for the given font.
      TTFSubsetter​(TrueTypeFont ttf, java.util.List<java.lang.String> tables)
      Creates a subsetter for the given font.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int unicode)
      Add the given character code to the subset.
      void addAll​(java.util.Set<java.lang.Integer> unicodeSet)
      Add the given character codes to the subset.
      private void addCompoundReferences()
      Resolve compound glyph references.
      private byte[] buildCmapTable()  
      private byte[] buildGlyfTable​(long[] newOffsets)  
      private byte[] buildHeadTable()  
      private byte[] buildHheaTable()  
      private byte[] buildHmtxTable()  
      private byte[] buildLocaTable​(long[] newOffsets)  
      private byte[] buildMaxpTable()  
      private byte[] buildNameTable()  
      private byte[] buildOS2Table()  
      private byte[] buildPostTable()  
      private long copyBytes​(java.io.InputStream is, java.io.OutputStream os, long newOffset, long lastOffset, int count)  
      java.util.Map<java.lang.Integer,​java.lang.Integer> getGIDMap()
      Returns the map of new -> old GIDs.
      private int getNewGlyphId​(java.lang.Integer oldGid)  
      private int log2​(int num)  
      void setPrefix​(java.lang.String prefix)
      Sets the prefix to add to the font's PostScript name.
      private boolean shouldCopyNameRecord​(NameRecord nr)  
      private long toUInt32​(byte[] bytes)  
      private long toUInt32​(int high, int low)  
      private long writeFileHeader​(java.io.DataOutputStream out, int nTables)  
      private void writeFixed​(java.io.DataOutputStream out, double f)  
      private void writeLongDateTime​(java.io.DataOutputStream out, java.util.Calendar calendar)  
      private void writeSInt16​(java.io.DataOutputStream out, short i)  
      private void writeTableBody​(java.io.OutputStream os, byte[] bytes)  
      private long writeTableHeader​(java.io.DataOutputStream out, java.lang.String tag, long offset, byte[] bytes)  
      void writeToStream​(java.io.OutputStream os)
      Write the subfont to the given output stream.
      private void writeUint16​(java.io.DataOutputStream out, int i)  
      private void writeUint32​(java.io.DataOutputStream out, long l)  
      private void writeUint8​(java.io.DataOutputStream out, int i)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOG

        private static final org.apache.commons.logging.Log LOG
      • PAD_BUF

        private static final byte[] PAD_BUF
      • unicodeCmap

        private final CmapLookup unicodeCmap
      • uniToGID

        private final java.util.SortedMap<java.lang.Integer,​java.lang.Integer> uniToGID
      • keepTables

        private final java.util.List<java.lang.String> keepTables
      • glyphIds

        private final java.util.SortedSet<java.lang.Integer> glyphIds
      • prefix

        private java.lang.String prefix
      • hasAddedCompoundReferences

        private boolean hasAddedCompoundReferences
    • Constructor Detail

      • TTFSubsetter

        public TTFSubsetter​(TrueTypeFont ttf)
                     throws java.io.IOException
        Creates a subsetter for the given font.
        Parameters:
        ttf - the font to be subset
        Throws:
        java.io.IOException
      • TTFSubsetter

        public TTFSubsetter​(TrueTypeFont ttf,
                            java.util.List<java.lang.String> tables)
                     throws java.io.IOException
        Creates a subsetter for the given font.
        Parameters:
        ttf - the font to be subset
        tables - optional tables to keep if present
        Throws:
        java.io.IOException
    • Method Detail

      • setPrefix

        public void setPrefix​(java.lang.String prefix)
        Sets the prefix to add to the font's PostScript name.
      • add

        public void add​(int unicode)
        Add the given character code to the subset.
        Parameters:
        unicode - character code
      • addAll

        public void addAll​(java.util.Set<java.lang.Integer> unicodeSet)
        Add the given character codes to the subset.
        Parameters:
        unicodeSet - character code set
      • getGIDMap

        public java.util.Map<java.lang.Integer,​java.lang.Integer> getGIDMap()
                                                                           throws java.io.IOException
        Returns the map of new -> old GIDs.
        Throws:
        java.io.IOException
      • writeFileHeader

        private long writeFileHeader​(java.io.DataOutputStream out,
                                     int nTables)
                              throws java.io.IOException
        Parameters:
        out - The data output stream.
        nTables - The number of table.
        Returns:
        The file offset of the first TTF table to write.
        Throws:
        java.io.IOException - Upon errors.
      • writeTableHeader

        private long writeTableHeader​(java.io.DataOutputStream out,
                                      java.lang.String tag,
                                      long offset,
                                      byte[] bytes)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • writeTableBody

        private void writeTableBody​(java.io.OutputStream os,
                                    byte[] bytes)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • buildHeadTable

        private byte[] buildHeadTable()
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • buildHheaTable

        private byte[] buildHheaTable()
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • shouldCopyNameRecord

        private boolean shouldCopyNameRecord​(NameRecord nr)
      • buildNameTable

        private byte[] buildNameTable()
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • buildMaxpTable

        private byte[] buildMaxpTable()
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • buildOS2Table

        private byte[] buildOS2Table()
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • buildLocaTable

        private byte[] buildLocaTable​(long[] newOffsets)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • addCompoundReferences

        private void addCompoundReferences()
                                    throws java.io.IOException
        Resolve compound glyph references.
        Throws:
        java.io.IOException
      • buildGlyfTable

        private byte[] buildGlyfTable​(long[] newOffsets)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • getNewGlyphId

        private int getNewGlyphId​(java.lang.Integer oldGid)
      • buildCmapTable

        private byte[] buildCmapTable()
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • buildPostTable

        private byte[] buildPostTable()
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • buildHmtxTable

        private byte[] buildHmtxTable()
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • copyBytes

        private long copyBytes​(java.io.InputStream is,
                               java.io.OutputStream os,
                               long newOffset,
                               long lastOffset,
                               int count)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • writeToStream

        public void writeToStream​(java.io.OutputStream os)
                           throws java.io.IOException
        Write the subfont to the given output stream.
        Parameters:
        os - the stream used for writing. It will be closed by this method.
        Throws:
        java.io.IOException - if something went wrong.
        java.lang.IllegalStateException - if the subset is empty.
      • writeFixed

        private void writeFixed​(java.io.DataOutputStream out,
                                double f)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • writeUint32

        private void writeUint32​(java.io.DataOutputStream out,
                                 long l)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • writeUint16

        private void writeUint16​(java.io.DataOutputStream out,
                                 int i)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • writeSInt16

        private void writeSInt16​(java.io.DataOutputStream out,
                                 short i)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • writeUint8

        private void writeUint8​(java.io.DataOutputStream out,
                                int i)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • writeLongDateTime

        private void writeLongDateTime​(java.io.DataOutputStream out,
                                       java.util.Calendar calendar)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • toUInt32

        private long toUInt32​(int high,
                              int low)
      • toUInt32

        private long toUInt32​(byte[] bytes)
      • log2

        private int log2​(int num)