gnu.java.awt.font.opentype
Class OpenTypeFont

java.lang.Object
  |
  +--gnu.java.awt.font.opentype.OpenTypeFont
All Implemented Interfaces:
FontDelegate

public final class OpenTypeFont
extends java.lang.Object
implements FontDelegate

A font that takes its data from OpenType or TrueType font tables.

OpenType is an extension of the TrueType font format. In addition to tables for names, kerning or layout, it also stores the shapes of individual glyphs. Three formats are recognized for glyphs: Quadratic splines (classic TrueType), cubic splines (PostScript), and bitmaps.

Author:
Sascha Brawer (brawer@dandelis.ch)
See Also:
Adobe’s OpenType specification, Apple’s TrueType specification

Field Summary
(package private)  java.nio.ByteBuffer buf
          A buffer containing the font data.
private  CharGlyphMap cmap
          A delegate object for mapping Unicode UCS-4 codepoints to glyph IDs.
private  float emsPerUnit
          A factor to convert font units into ems.
private  GlyphNamer glyphNamer
          A delegate object for providing a name for each glyph.
private  java.nio.ByteBuffer nameTable
          The font’s name table, or null if this table has not yet been accessed.
(package private)  int numGlyphs
          The number of glyphs in this font.
private  Scaler scaler
          The scaler to which the actual scaling work is delegated.
(package private)  int[] tableLength
           
(package private)  int[] tableStart
           
(package private)  int[] tableTag
           
(package private) static int TAG_OTTO
           
(package private) static int TAG_SFNT
           
(package private) static int TAG_TRUE
           
(package private) static int TAG_TTCF
           
(package private) static int TAG_ZAPF
           
private  int unitsPerEm
          The number of font units per em.
private  int version
          The version of the font in 16.16 fixed-point encoding, for example 0x00010000 for version 1.0.
 
Constructor Summary
(package private) OpenTypeFont(java.nio.ByteBuffer buf, int offsetTablePosition)
          Constructs an OpenType or TrueType font.
 
Method Summary
 java.awt.font.GlyphVector createGlyphVector(java.awt.Font font, java.awt.font.FontRenderContext frc, java.text.CharacterIterator ci)
          Creates a GlyphVector by mapping each character in a CharacterIterator to the corresponding glyph.
 void getAdvance(int glyphIndex, float pointSize, java.awt.geom.AffineTransform transform, boolean antialias, boolean fractionalMetrics, boolean horizontal, java.awt.geom.Point2D advance)
          Determines the advance width for a glyph.
 float getAscent(float pointSize, java.awt.geom.AffineTransform transform, boolean antialiased, boolean fractionalMetrics, boolean horizontal)
          Determines the distance between the base line and the highest ascender.
private  CharGlyphMap getCharGlyphMap()
           
 float getDescent(float pointSize, java.awt.geom.AffineTransform transform, boolean antialiased, boolean fractionalMetrics, boolean horizontal)
          Determines the distance between the base line and the lowest descender.
 java.lang.String getFamilyName(java.util.Locale locale)
          Returns the name of the family to which this font face belongs, for example “Univers”.
 java.nio.ByteBuffer getFontTable(int tag)
          Creates a view buffer for an OpenType table.
 int getFontTableSize(int tag)
          Returns the size of one of the tables in the font, or -1 if the table does not exist.
 java.lang.String getFullName(java.util.Locale locale)
          Returns the full name of this font face, for example “Univers Light”.
 int getGlyph(int ucs4)
          Looks up a glyph in the font’s cmap tables, without performing any glyph substitution or reordering.
 java.lang.String getGlyphName(int glyphIndex)
          Returns a name for the specified glyph.
 java.awt.geom.GeneralPath getGlyphOutline(int glyph, float pointSize, java.awt.geom.AffineTransform transform, boolean antialias, boolean fractionalMetrics)
          Returns the shape of a glyph.
 int getMissingGlyphCode()
          Returns the index of the glyph which gets displayed if the font cannot map a Unicode code point to a glyph.
private  java.lang.String getName(int name, java.util.Locale locale)
          Extracts a String from the font’s name table.
 int getNumGlyphs()
          Returns the number of glyphs in this font face.
 java.lang.String getPostScriptName()
          Returns the PostScript name of this font face, for example “Univers-Light”.
 java.lang.String getSubFamilyName(java.util.Locale locale)
          Returns the name of this font face inside the family, for example “Light”.
private  int getTableIndex(int tag)
          Determines the index of a table into the offset table.
 int getVersion()
          Returns the version of the font.
(package private) static java.lang.String tagToString(int tag)
          Converts a four-byte tag identifier into a String that can be displayed when debugging this class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TAG_OTTO

static final int TAG_OTTO
See Also:
Constant Field Values

TAG_SFNT

static final int TAG_SFNT
See Also:
Constant Field Values

TAG_TRUE

static final int TAG_TRUE
See Also:
Constant Field Values

TAG_TTCF

static final int TAG_TTCF
See Also:
Constant Field Values

TAG_ZAPF

static final int TAG_ZAPF
See Also:
Constant Field Values

buf

java.nio.ByteBuffer buf
A buffer containing the font data. Note that this may well be an instance of the subclass MappedByteBuffer, in which case the virtual memory subsystem can more efficiently handle requests for font data. This is especially recommended for large font files that contain many glyphs that are rarely accessed.


numGlyphs

final int numGlyphs
The number of glyphs in this font.


tableTag

int[] tableTag

tableStart

int[] tableStart

tableLength

int[] tableLength

version

private int version
The version of the font in 16.16 fixed-point encoding, for example 0x00010000 for version 1.0. There are also two special version IDs used by fonts for Apple Macintosh, namely 'true' (0x74727565) and 'typ1'. OpenType fonts sometimes have 'OTTO' as their version.


unitsPerEm

private int unitsPerEm
The number of font units per em. For fonts with TrueType outlines, this is usually a power of two (such as 2048). For OpenType fonts with PostScript outlines, other values are acceptable (such as 1000).


emsPerUnit

private float emsPerUnit
A factor to convert font units into ems. This value is 1 / unitsPerEm.


scaler

private Scaler scaler
The scaler to which the actual scaling work is delegated.


cmap

private CharGlyphMap cmap
A delegate object for mapping Unicode UCS-4 codepoints to glyph IDs.


glyphNamer

private GlyphNamer glyphNamer
A delegate object for providing a name for each glyph.


nameTable

private java.nio.ByteBuffer nameTable
The font’s name table, or null if this table has not yet been accessed.

Constructor Detail

OpenTypeFont

OpenTypeFont(java.nio.ByteBuffer buf,
             int offsetTablePosition)
       throws java.awt.FontFormatException
Constructs an OpenType or TrueType font.

Parameters:
buf - a buffer with the contents of the font file. It is recommended to use a MappedByteBuffer for very large font files.
offsetTablePosition - the position of the OpenType offset table in the font file. The offset table of most OpenType and TrueType fonts starts at position 0. However, so-called TrueType Collections support multiple OpenType fonts in a single file, which allows sharing some glyphs between fonts. If many glyphs are shared (for example all the Kanji glyphs between multiple Japanese fonts), the space savings can be considerable. In that case, the offset table of each individual font would start at its own position.
Throws:
java.awt.FontFormatException - if the font data is not in OpenType or TrueType format.
Method Detail

getTableIndex

private int getTableIndex(int tag)
Determines the index of a table into the offset table. The result can be used to find the offset and length of a table, as in tableStart[getTableIndex(TAG_NAME)].

Parameters:
tag - the table identifier, for instance OpenType.TAG_NAME.
Returns:
the index of that table into the offset table, or -1 if the font does not contain the table specified by tag.

getFamilyName

public java.lang.String getFamilyName(java.util.Locale locale)
Returns the name of the family to which this font face belongs, for example “Univers”.

Specified by:
getFamilyName in interface FontDelegate
Parameters:
locale - the locale for which to localize the name.
Returns:
the family name.

getSubFamilyName

public java.lang.String getSubFamilyName(java.util.Locale locale)
Returns the name of this font face inside the family, for example “Light”.

Specified by:
getSubFamilyName in interface FontDelegate
Parameters:
locale - the locale for which to localize the name.
Returns:
the name of the face inside its family.

getFullName

public java.lang.String getFullName(java.util.Locale locale)
Returns the full name of this font face, for example “Univers Light”.

Specified by:
getFullName in interface FontDelegate
Parameters:
locale - the locale for which to localize the name.
Returns:
the face name.

getPostScriptName

public java.lang.String getPostScriptName()
Returns the PostScript name of this font face, for example “Univers-Light”.

Specified by:
getPostScriptName in interface FontDelegate
Returns:
the PostScript name, or null if the font does not provide a PostScript name.

getNumGlyphs

public int getNumGlyphs()
Returns the number of glyphs in this font face.

Specified by:
getNumGlyphs in interface FontDelegate

getMissingGlyphCode

public int getMissingGlyphCode()
Returns the index of the glyph which gets displayed if the font cannot map a Unicode code point to a glyph. Many fonts show this glyph as an empty box.

Specified by:
getMissingGlyphCode in interface FontDelegate

getName

private java.lang.String getName(int name,
                                 java.util.Locale locale)
Extracts a String from the font’s name table.

Parameters:
name - the numeric TrueType or OpenType name ID.
locale - the locale for which names shall be localized, or null if the locale does mot matter because the name is known to be language-independent (for example, because it is the PostScript name).

getVersion

public int getVersion()
Returns the version of the font.

Returns:
the version in 16.16 fixed-point encoding, for example 0x00010000 for version 1.0.
See Also:
OpenType.getVersion()

getFontTable

public java.nio.ByteBuffer getFontTable(int tag)
Creates a view buffer for an OpenType table. The caller can access the returned buffer without needing to synchronize access from multiple threads.

Parameters:
tag - the table identifier, for example OpenType.GLYF.
Returns:
a slice of the underlying buffer containing the table, or null if the font does not contain the requested table.

getFontTableSize

public int getFontTableSize(int tag)
Returns the size of one of the tables in the font, or -1 if the table does not exist.


getCharGlyphMap

private CharGlyphMap getCharGlyphMap()

getGlyph

public int getGlyph(int ucs4)
Looks up a glyph in the font’s cmap tables, without performing any glyph substitution or reordering. Because of this limitation, this method cannot be used for script systems that need advanced glyph mapping, such as Arabic, Korean, or even Latin with exotic accents.

It is safe to call this method from any thread.

Parameters:
ucs4 - the Unicode codepoint in the 32-bit Unicode character set UCS-4. Because UTF-16 surrogates do not correspond to a single glyph, it does not make sense to pass them here.
Returns:
the glyph index, or zero if the font does not contain a glyph for the specified codepoint.

createGlyphVector

public java.awt.font.GlyphVector createGlyphVector(java.awt.Font font,
                                                   java.awt.font.FontRenderContext frc,
                                                   java.text.CharacterIterator ci)
Creates a GlyphVector by mapping each character in a CharacterIterator to the corresponding glyph.

The mapping takes only the font’s cmap tables into consideration. No other operations (such as glyph re-ordering, composition, or ligature substitution) are performed. This means that the resulting GlyphVector will not be correct for text in languages that have complex character-to-glyph mappings, such as Arabic, Hebrew, Hindi, or Thai.

Specified by:
createGlyphVector in interface FontDelegate
Parameters:
font - the font object that the created GlyphVector will return when it gets asked for its font. This argument is needed because the public API works with java.awt.Font, not with some private delegate like OpenTypeFont.
frc - the font rendering parameters that are used for measuring glyphs. The exact placement of text slightly depends on device-specific characteristics, for instance the device resolution or anti-aliasing. For this reason, any measurements will only be accurate if the passed FontRenderContext correctly reflects the relevant parameters. Hence, frc should be obtained from the same Graphics2D that will be used for drawing, and any rendering hints should be set to the desired values before obtaining frc.
ci - a CharacterIterator for iterating over the characters to be displayed.

getAdvance

public void getAdvance(int glyphIndex,
                       float pointSize,
                       java.awt.geom.AffineTransform transform,
                       boolean antialias,
                       boolean fractionalMetrics,
                       boolean horizontal,
                       java.awt.geom.Point2D advance)
Determines the advance width for a glyph.

Specified by:
getAdvance in interface FontDelegate
Parameters:
glyphIndex - the glyph whose advance width is to be determined.
pointSize - the point size of the font.
transform - a transform that is applied in addition to scaling to the specified point size. This is often used for scaling according to the device resolution. Those who lack any aesthetic sense may also use the transform to slant or stretch glyphs.
antialias - true for anti-aliased rendering, false for normal rendering. For hinted fonts, this parameter may indeed affect the result.
fractionalMetrics - true for fractional metrics, false for rounding the result to a pixel boundary.
horizontal - true for horizontal line layout, false for vertical line layout.
advance - a point whose x and y fields will hold the advance in each direction. It is possible that both values are non-zero, for example if transform is a rotation, or in the case of Urdu fonts.

getGlyphOutline

public java.awt.geom.GeneralPath getGlyphOutline(int glyph,
                                                 float pointSize,
                                                 java.awt.geom.AffineTransform transform,
                                                 boolean antialias,
                                                 boolean fractionalMetrics)
Returns the shape of a glyph.

Specified by:
getGlyphOutline in interface FontDelegate
Parameters:
pointSize - the point size of the font.
transform - a transform that is applied in addition to scaling to the specified point size. This is often used for scaling according to the device resolution. Those who lack any aesthetic sense may also use the transform to slant or stretch glyphs.
antialias - true for anti-aliased rendering, false for normal rendering. For hinted fonts, this parameter may indeed affect the result.
fractionalMetrics - true for fractional metrics, false for rounding the result to a pixel boundary.
glyph - the glyph whose advance width is to be determined.
Returns:
the scaled and grid-fitted outline of the specified glyph, or null for bitmap fonts.

getGlyphName

public java.lang.String getGlyphName(int glyphIndex)
Returns a name for the specified glyph. This is useful for generating PostScript or PDF files that embed some glyphs of a font.

Names are not unique: Under some rare circumstances, the same name can be returned for different glyphs. It is therefore recommended that printer drivers check whether the same name has already been returned for antoher glyph, and make the name unique by adding the string ".alt" followed by the glyph index.

This situation would occur for an OpenType or TrueType font that has a post table of format 3 and provides a mapping from glyph IDs to Unicode sequences through a Zapf table. If the same sequence of Unicode codepoints leads to different glyphs (depending on contextual position, for example, or on typographic sophistication level), the same name would get synthesized for those glyphs.

Specified by:
getGlyphName in interface FontDelegate
Parameters:
glyphIndex - the glyph whose name the caller wants to retrieve.

getAscent

public float getAscent(float pointSize,
                       java.awt.geom.AffineTransform transform,
                       boolean antialiased,
                       boolean fractionalMetrics,
                       boolean horizontal)
Determines the distance between the base line and the highest ascender.

Specified by:
getAscent in interface FontDelegate
Parameters:
pointSize - the point size of the font.
transform - a transform that is applied in addition to scaling to the specified point size. This is often used for scaling according to the device resolution. Those who lack any aesthetic sense may also use the transform to slant or stretch glyphs.
fractionalMetrics - true for fractional metrics, false for rounding the result to a pixel boundary.
horizontal - true for horizontal line layout, false for vertical line layout.
Returns:
the ascent, which usually is a positive number.

getDescent

public float getDescent(float pointSize,
                        java.awt.geom.AffineTransform transform,
                        boolean antialiased,
                        boolean fractionalMetrics,
                        boolean horizontal)
Determines the distance between the base line and the lowest descender.

Specified by:
getDescent in interface FontDelegate
Parameters:
pointSize - the point size of the font.
transform - a transform that is applied in addition to scaling to the specified point size. This is often used for scaling according to the device resolution. Those who lack any aesthetic sense may also use the transform to slant or stretch glyphs.
fractionalMetrics - true for fractional metrics, false for rounding the result to a pixel boundary.
horizontal - true for horizontal line layout, false for vertical line layout.
Returns:
the descent, which usually is a nagative number.

tagToString

static java.lang.String tagToString(int tag)
Converts a four-byte tag identifier into a String that can be displayed when debugging this class.

Parameters:
tag - the tag as an int.
Returns:
the tag in human-readable form, for example name or glyf.