Package org.apache.fontbox.ttf
Class TTFSubsetter
- java.lang.Object
-
- org.apache.fontbox.ttf.TTFSubsetter
-
public final class TTFSubsetter extends java.lang.ObjectSubsetter for TrueType (TTF) fonts.Originally developed by Wolfgang Glas for Sketch.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.SortedSet<java.lang.Integer>glyphIdsprivate booleanhasAddedCompoundReferencesprivate java.util.Set<java.lang.Integer>invisibleGlyphIdsprivate java.util.List<java.lang.String>keepTablesprivate static org.apache.commons.logging.LogLOGprivate static byte[]PAD_BUFprivate java.lang.Stringprefixprivate static java.util.TimeZoneTIMEZONE_UTCprivate TrueTypeFontttfprivate CmapLookupunicodeCmapprivate java.util.SortedMap<java.lang.Integer,java.lang.Integer>uniToGID
-
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 voidadd(int unicode)Add the given character code to the subset.voidaddAll(java.util.Set<java.lang.Integer> unicodeSet)Add the given character codes to the subset.private voidaddCompoundReferences()Resolve compound glyph references.voidaddGlyphIds(java.util.Set<java.lang.Integer> allGlyphIds)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 longcopyBytes(java.io.InputStream is, java.io.OutputStream os, long newOffset, long lastOffset, int count)voidforceInvisible(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.java.util.Map<java.lang.Integer,java.lang.Integer>getGIDMap()Returns the map of new -> old GIDs.private intgetNewGlyphId(java.lang.Integer oldGid)private intlog2(int num)voidsetPrefix(java.lang.String prefix)Sets the prefix to add to the font's PostScript name.private booleanshouldCopyNameRecord(NameRecord nr)private longtoUInt32(byte[] bytes)private longtoUInt32(int high, int low)private longwriteFileHeader(java.io.DataOutputStream out, int nTables)private voidwriteFixed(java.io.DataOutputStream out, double f)private voidwriteLongDateTime(java.io.DataOutputStream out, java.util.Calendar calendar)private voidwriteSInt16(java.io.DataOutputStream out, short i)private voidwriteTableBody(java.io.OutputStream os, byte[] bytes)private longwriteTableHeader(java.io.DataOutputStream out, java.lang.String tag, long offset, byte[] bytes)voidwriteToStream(java.io.OutputStream os)Write the subfont to the given output stream.private voidwriteUint16(java.io.DataOutputStream out, int i)private voidwriteUint32(java.io.DataOutputStream out, long l)private voidwriteUint8(java.io.DataOutputStream out, int i)
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
PAD_BUF
private static final byte[] PAD_BUF
-
TIMEZONE_UTC
private static final java.util.TimeZone TIMEZONE_UTC
-
ttf
private final TrueTypeFont ttf
-
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
-
invisibleGlyphIds
private final java.util.Set<java.lang.Integer> invisibleGlyphIds
-
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- if there is an error reading the font data
-
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 subsettables- optional tables to keep if present- Throws:
java.io.IOException- if there is an error reading the font data
-
-
Method Detail
-
setPrefix
public void setPrefix(java.lang.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(java.util.Set<java.lang.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 alsoaddedseparately.- Parameters:
unicode- the character code whose glyph should be invisible
-
getGIDMap
public java.util.Map<java.lang.Integer,java.lang.Integer> getGIDMap() throws java.io.IOExceptionReturns the map of new -> old GIDs.- Returns:
- the GID map
- Throws:
java.io.IOException- if the font data could not be read
-
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.IOExceptionResolve 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.IOExceptionWrite 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)
-
addGlyphIds
public void addGlyphIds(java.util.Set<java.lang.Integer> allGlyphIds)
-
-