Class TTFSubsetter

java.lang.Object
org.apache.fontbox.ttf.TTFSubsetter

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

Originally developed by Wolfgang Glas for Sketch.

  • Field Details

    • LOG

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

      private static final byte[] PAD_BUF
    • TIMEZONE_UTC

      private static final TimeZone TIMEZONE_UTC
    • ttf

      private final TrueTypeFont ttf
    • unicodeCmap

      private final CmapLookup unicodeCmap
    • uniToGID

      private final SortedMap<Integer,Integer> uniToGID
    • keepTables

      private final List<String> keepTables
    • glyphIds

      private final SortedSet<Integer> glyphIds
    • invisibleGlyphIds

      private final Set<Integer> invisibleGlyphIds
    • prefix

      private String prefix
    • hasAddedCompoundReferences

      private boolean hasAddedCompoundReferences
  • Constructor Details

    • TTFSubsetter

      public TTFSubsetter(TrueTypeFont ttf) throws IOException
      Creates a subsetter for the given font.
      Parameters:
      ttf - the font to be subset
      Throws:
      IOException - if there is an error reading the font data
    • TTFSubsetter

      public TTFSubsetter(TrueTypeFont ttf, List<String> tables) throws IOException
      Creates a subsetter for the given font.
      Parameters:
      ttf - the font to be subset
      tables - optional tables to keep if present
      Throws:
      IOException - if there is an error reading the font data
  • Method Details

    • setPrefix

      public void setPrefix(String prefix)
      Sets the prefix to add to the font's PostScript name.
      Parameters:
      prefix - to be used as prefix for the PostScript name of the font
    • add

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

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

      public void forceInvisible(int unicode)
      Forces the glyph for the specified character code to be zero-width and contour-free, regardless of what the glyph looks like in the original font. Note that the specified character code is not added to the subset unless it is also added separately.
      Parameters:
      unicode - the character code whose glyph should be invisible
    • getGIDMap

      public Map<Integer,Integer> getGIDMap() throws IOException
      Returns the map of new -> old GIDs.
      Returns:
      the GID map
      Throws:
      IOException - if the font data could not be read
    • writeFileHeader

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

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

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

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

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

      private boolean shouldCopyNameRecord(NameRecord nr)
    • buildNameTable

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

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

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

      private byte[] buildLocaTable(long[] newOffsets) throws IOException
      Throws:
      IOException
    • addCompoundReferences

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

      private byte[] buildGlyfTable(long[] newOffsets) throws IOException
      Throws:
      IOException
    • getNewGlyphId

      private int getNewGlyphId(Integer oldGid)
    • buildCmapTable

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

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

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

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

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

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

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

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

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

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

      private void writeLongDateTime(DataOutputStream out, Calendar calendar) throws IOException
      Throws:
      IOException
    • toUInt32

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

      private long toUInt32(byte[] bytes)
    • log2

      private int log2(int num)
    • addGlyphIds

      public void addGlyphIds(Set<Integer> allGlyphIds)