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 boolean
private static final org.apache.commons.logging.Log
private static final byte[]
private String
private final TrueTypeFont
private 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 TypeMethodDescriptionvoid
add
(int unicode) Add the given character code to the subset.void
Add the given character codes to the subset.private void
Resolve compound glyph references.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 long
copyBytes
(InputStream is, OutputStream os, long newOffset, long lastOffset, int count) Returns the map of new -> old GIDs.private int
getNewGlyphId
(Integer oldGid) private int
log2
(int num) void
Sets the prefix to add to the font's PostScript name.private boolean
private long
toUInt32
(byte[] bytes) private long
toUInt32
(int high, int low) private long
writeFileHeader
(DataOutputStream out, int nTables) private void
writeFixed
(DataOutputStream out, double f) private void
writeLongDateTime
(DataOutputStream out, Calendar calendar) private void
writeSInt16
(DataOutputStream out, short i) private void
writeTableBody
(OutputStream os, byte[] bytes) private long
writeTableHeader
(DataOutputStream out, String tag, long offset, byte[] bytes) void
Write the subfont to the given output stream.private void
writeUint16
(DataOutputStream out, int i) private void
writeUint32
(DataOutputStream out, long l) private void
writeUint8
(DataOutputStream out, int i)
-
Field Details
-
LOG
private static final org.apache.commons.logging.Log LOG -
PAD_BUF
private static final byte[] PAD_BUF -
ttf
-
unicodeCmap
-
uniToGID
-
keepTables
-
glyphIds
-
prefix
-
hasAddedCompoundReferences
private boolean hasAddedCompoundReferences
-
-
Constructor Details
-
TTFSubsetter
Creates a subsetter for the given font.- Parameters:
ttf
- the font to be subset- Throws:
IOException
-
TTFSubsetter
Creates a subsetter for the given font.- Parameters:
ttf
- the font to be subsettables
- optional tables to keep if present- Throws:
IOException
-
-
Method Details
-
setPrefix
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
Add the given character codes to the subset.- Parameters:
unicodeSet
- character code set
-
getGIDMap
Returns the map of new -> old GIDs.- Throws:
IOException
-
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)
-