Package org.apache.fontbox.ttf
Class TTFSubsetter
java.lang.Object
org.apache.fontbox.ttf.TTFSubsetter
Subsetter for TrueType (TTF) fonts.
Originally developed by Wolfgang Glas for Sketch.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate static final org.apache.commons.logging.Logprivate static final byte[]private Stringprivate static final TimeZoneprivate final TrueTypeFontprivate final CmapLookup -
Constructor Summary
ConstructorsConstructorDescriptionTTFSubsetter(TrueTypeFont ttf) Creates a subsetter for the given font.TTFSubsetter(TrueTypeFont ttf, List<String> tables) Creates a subsetter for the given font. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(int unicode) Add the given character code to the subset.voidAdd the given character codes to the subset.private voidResolve compound glyph references.voidaddGlyphIds(Set<Integer> allGlyphIds) private byte[]private byte[]buildGlyfTable(long[] newOffsets) private byte[]private byte[]private byte[]private byte[]buildLocaTable(long[] newOffsets) private byte[]private byte[]private byte[]private byte[]private longcopyBytes(InputStream is, 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.Returns the map of new -> old GIDs.private intgetNewGlyphId(Integer oldGid) private intlog2(int num) voidSets the prefix to add to the font's PostScript name.private booleanprivate longtoUInt32(byte[] bytes) private longtoUInt32(int high, int low) private longwriteFileHeader(DataOutputStream out, int nTables) private voidwriteFixed(DataOutputStream out, double f) private voidwriteLongDateTime(DataOutputStream out, Calendar calendar) private voidwriteSInt16(DataOutputStream out, short i) private voidwriteTableBody(OutputStream os, byte[] bytes) private longwriteTableHeader(DataOutputStream out, String tag, long offset, byte[] bytes) voidWrite the subfont to the given output stream.private voidwriteUint16(DataOutputStream out, int i) private voidwriteUint32(DataOutputStream out, long l) private voidwriteUint8(DataOutputStream out, int i)
-
Field Details
-
LOG
private static final org.apache.commons.logging.Log LOG -
PAD_BUF
private static final byte[] PAD_BUF -
TIMEZONE_UTC
-
ttf
-
unicodeCmap
-
uniToGID
-
keepTables
-
glyphIds
-
invisibleGlyphIds
-
prefix
-
hasAddedCompoundReferences
private boolean hasAddedCompoundReferences
-
-
Constructor Details
-
TTFSubsetter
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
Creates a subsetter for the given font.- Parameters:
ttf- the font to be subsettables- optional tables to keep if present- Throws:
IOException- if there is an error reading the font data
-
-
Method Details
-
setPrefix
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
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
Returns the map of new -> old GIDs.- Returns:
- the GID map
- Throws:
IOException- if the font data could not be read
-
writeFileHeader
- 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
- Throws:
IOException
-
buildHeadTable
- Throws:
IOException
-
buildHheaTable
- Throws:
IOException
-
shouldCopyNameRecord
-
buildNameTable
- Throws:
IOException
-
buildMaxpTable
- Throws:
IOException
-
buildOS2Table
- Throws:
IOException
-
buildLocaTable
- Throws:
IOException
-
addCompoundReferences
Resolve compound glyph references.- Throws:
IOException
-
buildGlyfTable
- Throws:
IOException
-
getNewGlyphId
-
buildCmapTable
- Throws:
IOException
-
buildPostTable
- Throws:
IOException
-
buildHmtxTable
- Throws:
IOException
-
copyBytes
private long copyBytes(InputStream is, OutputStream os, long newOffset, long lastOffset, int count) throws IOException - Throws:
IOException
-
writeToStream
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
- Throws:
IOException
-
writeUint32
- Throws:
IOException
-
writeUint16
- Throws:
IOException
-
writeSInt16
- Throws:
IOException
-
writeUint8
- Throws:
IOException
-
writeLongDateTime
- Throws:
IOException
-
toUInt32
private long toUInt32(int high, int low) -
toUInt32
private long toUInt32(byte[] bytes) -
log2
private int log2(int num) -
addGlyphIds
-