Top Description Fields Constructors Methods
org.apache.fontbox.ttf

public final Class TTFSubsetter

extends Object
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .DataOutputStream, .EOFException, .IOException, .InputStream, .OutputStream, java.nio.charset.Charset, .StandardCharsets, java.util.Calendar, .HashMap, .Iterator, .LinkedHashMap, .List, .Map, .Map.Entry, .Set, .SortedMap, .SortedSet, .TimeZone, .TreeMap, .TreeSet, org.apache.commons.logging.Log, .LogFactory

Subsetter for TrueType (TTF) fonts.

Originally developed by Wolfgang Glas for Sketch.

Author
Wolfgang Glas

Field Summary

Modifier and TypeField and Description
private final SortedSet<Integer>
private boolean
private final List<String>
private static final Log
private static final byte[]
private String
private static final TimeZone
private final TrueTypeFont
private final CmapLookup
private final SortedMap<Integer, Integer>

Constructor Summary

AccessConstructor and Description
public
TTFSubsetter(TrueTypeFont
the font to be subset
ttf
)

Creates a subsetter for the given font.

public
TTFSubsetter(TrueTypeFont
the font to be subset
ttf
,
List<String>
optional tables to keep if present
tables
)

Creates a subsetter for the given font.

Method Summary

Modifier and TypeMethod and Description
public void
add(int
character code
unicode
)

Add the given character code to the subset.

public void
addAll(Set<Integer>
character code set
unicodeSet
)

Add the given character codes to the subset.

private void
addCompoundReferences()

Resolve compound glyph references.

public void
addGlyphIds(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 long
copyBytes(InputStream is, OutputStream os, long newOffset, long lastOffset, int count)

public Map<Integer, Integer>

Returns:

the GID map
getGIDMap
()

Returns the map of new -> old GIDs.

private int
private int
log2(int num)

public void
setPrefix(String
to be used as prefix for the PostScript name of the font
prefix
)

Sets the prefix to add to the font's PostScript name.

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

private long
toUInt32(byte[] bytes)

private long

Returns:

The file offset of the first TTF table to write.
writeFileHeader
(DataOutputStream
The data output stream.
out
,
int
The number of table.
nTables
)

private void
private void
private void
private void
writeTableBody(OutputStream os, byte[] bytes)

private long
writeTableHeader(DataOutputStream out, String tag, long offset, byte[] bytes)

public void
writeToStream(OutputStream
the stream used for writing. It will be closed by this method.
os
)

Write the subfont to the given output stream.

private void
private void
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

glyphIdsback to summary
private final SortedSet<Integer> glyphIds
hasAddedCompoundReferencesback to summary
private boolean hasAddedCompoundReferences
keepTablesback to summary
private final List<String> keepTables
LOGback to summary
private static final Log LOG
PAD_BUFback to summary
private static final byte[] PAD_BUF
prefixback to summary
private String prefix
TIMEZONE_UTCback to summary
private static final TimeZone TIMEZONE_UTC
ttfback to summary
private final TrueTypeFont ttf
unicodeCmapback to summary
private final CmapLookup unicodeCmap
uniToGIDback to summary
private final SortedMap<Integer, Integer> uniToGID

Constructor Detail

TTFSubsetterback to summary
public TTFSubsetter(TrueTypeFont ttf) throws IOException

Creates a subsetter for the given font.

Parameters
ttf:TrueTypeFont

the font to be subset

Exceptions
IOException:
if there is an error reading the font data
TTFSubsetterback to summary
public TTFSubsetter(TrueTypeFont ttf, List<String> tables) throws IOException

Creates a subsetter for the given font.

Parameters
ttf:TrueTypeFont

the font to be subset

tables:List<String>

optional tables to keep if present

Exceptions
IOException:
if there is an error reading the font data

Method Detail

addback to summary
public void add(int unicode)

Add the given character code to the subset.

Parameters
unicode:int

character code

addAllback to summary
public void addAll(Set<Integer> unicodeSet)

Add the given character codes to the subset.

Parameters
unicodeSet:Set<Integer>

character code set

addCompoundReferencesback to summary
private void addCompoundReferences() throws IOException

Resolve compound glyph references.

addGlyphIdsback to summary
public void addGlyphIds(Set<Integer> allGlyphIds)
buildCmapTableback to summary
private byte[] buildCmapTable() throws IOException
buildGlyfTableback to summary
private byte[] buildGlyfTable(long[] newOffsets) throws IOException
buildHeadTableback to summary
private byte[] buildHeadTable() throws IOException
buildHheaTableback to summary
private byte[] buildHheaTable() throws IOException
buildHmtxTableback to summary
private byte[] buildHmtxTable() throws IOException
buildLocaTableback to summary
private byte[] buildLocaTable(long[] newOffsets) throws IOException
buildMaxpTableback to summary
private byte[] buildMaxpTable() throws IOException
buildNameTableback to summary
private byte[] buildNameTable() throws IOException
buildOS2Tableback to summary
private byte[] buildOS2Table() throws IOException
buildPostTableback to summary
private byte[] buildPostTable() throws IOException
copyBytesback to summary
private long copyBytes(InputStream is, OutputStream os, long newOffset, long lastOffset, int count) throws IOException
getGIDMapback to summary
public Map<Integer, Integer> getGIDMap() throws IOException

Returns the map of new -> old GIDs.

Returns:Map<Integer, Integer>

the GID map

Exceptions
IOException:
if the font data could not be read
getNewGlyphIdback to summary
private int getNewGlyphId(Integer oldGid)
log2back to summary
private int log2(int num)
setPrefixback to summary
public void setPrefix(String prefix)

Sets the prefix to add to the font's PostScript name.

Parameters
prefix:String

to be used as prefix for the PostScript name of the font

shouldCopyNameRecordback to summary
private boolean shouldCopyNameRecord(NameRecord nr)
toUInt32back to summary
private long toUInt32(int high, int low)
toUInt32back to summary
private long toUInt32(byte[] bytes)
writeFileHeaderback to summary
private long writeFileHeader(DataOutputStream out, int nTables) throws IOException
Parameters
out:DataOutputStream

The data output stream.

nTables:int

The number of table.

Returns:long

The file offset of the first TTF table to write.

Exceptions
IOException:
Upon errors.
writeFixedback to summary
private void writeFixed(DataOutputStream out, double f) throws IOException
writeLongDateTimeback to summary
private void writeLongDateTime(DataOutputStream out, Calendar calendar) throws IOException
writeSInt16back to summary
private void writeSInt16(DataOutputStream out, short i) throws IOException
writeTableBodyback to summary
private void writeTableBody(OutputStream os, byte[] bytes) throws IOException
writeTableHeaderback to summary
private long writeTableHeader(DataOutputStream out, String tag, long offset, byte[] bytes) throws IOException
writeToStreamback to summary
public void writeToStream(OutputStream os) throws IOException

Write the subfont to the given output stream.

Parameters
os:OutputStream

the stream used for writing. It will be closed by this method.

Exceptions
IOException:
if something went wrong.
IllegalStateException:
if the subset is empty.
writeUint16back to summary
private void writeUint16(DataOutputStream out, int i) throws IOException
writeUint32back to summary
private void writeUint32(DataOutputStream out, long l) throws IOException
writeUint8back to summary
private void writeUint8(DataOutputStream out, int i) throws IOException