Class Type3Font
- java.lang.Object
-
- org.faceless.pdf2.PDFFont
-
- org.faceless.pdf2.Type3Font
-
- All Implemented Interfaces:
java.lang.Cloneable
public class Type3Font extends PDFFont
A Type3 font is an artificial font made up of shapes drawn directly to the PDF, rather than stored in a font file embedded in the PDF. These are useful when a few custom shapes have to be added to the PDF as glyphs - perhaps extracted from an existing font, or created freehand - and in particular this approach can be used with the
PDFStyle.addBackupFont(org.faceless.pdf2.PDFFont)
method to plug gaps in an existing font's coverage.Here's an example showing how to use a glyph from an existing
Font
. (be aware there is rarely any benefit to cloning an entire font as shown here, and that copyright issues may arise when doing so; it's purely for example):Type3Font font = new Type3Font("MyFont"); String glyphs = "Hello World"; FontRenderContext frc = new FontRenderContext(null, true, true); for (int i=0;i<glyphs.length();i++) { char c = glyphs.charAt(i); if (!font.isDefined(c)) { font.addGlyph(c, f.createGlyphVector(frc, Character.toString(c))); } }
and here's how to create a completely custom glyphType3Font font = new Type3Font("MyFont"); GeneralPath p = new GeneralPath(); p.moveTo(0, 600); p.lineTo(600, 300); p.lineTo(0, 0); p.closePath(); font.add('▶', 600, p); // U+25B6, right pointing triangle
Type3 fonts may contain any number of individual glyphs, but only 255 can be referenced in a PDF from a single instance of this font. The vast majority of methods inherited from
PDFFont
that return font metrics values (eggetAscender()
return 0 or default values as these concepts aren't applicable to Type3 fonts.As of 2.23.4, the full range of codepoints outside the BMP can be addressed.
- Since:
- 2.21
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
addGlyph(int c, int advance, java.awt.Shape shape)
Add a glyph to the font.int
addGlyph(int c, java.awt.font.GlyphVector gv)
Add a glyph to the fontfloat
getAscender()
Get the Ascender for the font (the maximum height above the baseline the font extends), as a proportion of the point size.float
getCapHeight()
Get the Cap-Height of the font - normally the height of an upper-case 'O' characterfloat
getDefaultLeading()
Get the default leading for this font - the preferred distance between two successive baselines of text.java.util.BitSet
getDefinedCodepoints()
Return read-only BitSet containing all the Unicode codepoints defined in this fontfloat
getDescender()
Get the Descender for the font (the maximum height below the baseline the font extends), as a proportion of the point size.int
getSize()
Return the maximum entry in this font, from 0 (for empty) to 255.float
getStrikeoutPosition()
Get the strikeout position, as a proportion of the font size.float
getStrikeoutThickness()
Get the strikeout thickness, as a proportion of the font size.float
getSubscriptPosition()
Get the recommended position of a sub-script version of this font, as a proportion of the sub-scripted font size.float
getSubscriptSize()
Get the recommended size of a super/sub script version of this font, as a proportion of the normal font size.float
getSuperscriptPosition()
Get the recommended position of a super-script version of this font, as a proportion of the sub-scripted font size.float
getUnderlinePosition()
Get the underline position, as a proportion of the font size.float
getUnderlineThickness()
Get the underline thickness, as a proportion of the font size.float
getXHeight()
Get the X-Height of the font - normally the height of a lower-case 'x' character.boolean
isBold()
Return true if the font is boldboolean
isDefined(int codepoint)
Return true if the specified Unicode character is defined in the font.boolean
isEmbedded()
Type3 Fonts are always embeddedboolean
isItalic()
Return true if the font is italicboolean
isMonospace()
Return true if every character has the same width (like Courier), false if every character is potentially a different width (like Times-Roman)boolean
isSerif()
Return true if the font is serifvoid
putLiteral(java.lang.String key, java.lang.String tokens)
Put a literal token sequnce.java.lang.String
toString()
-
Methods inherited from class org.faceless.pdf2.PDFFont
clone, getAvailableFeatures, getBaseName, getBottom, getCharWidth, getCharWidth, getFeature, getKerning, getKerning, getLeft, getMetaData, getRight, getTop, getXMP, hasFeature, isDefined, isHorizontal, isSubset, setFeature, setFeature, setMetaData, versionBold, versionItalic, versionNonBold, versionNonItalic, versionRegular
-
-
-
-
Constructor Detail
-
Type3Font
public Type3Font(Type3Font font)
Create a new Type3 Font that is a copy of the existing Type3 font. This constructor should be used to clone the font if it is being shared across threads.- Parameters:
font
- the original font
-
Type3Font
public Type3Font(java.lang.String name)
Create a new Type3 Font with the specified name- Parameters:
name
- the font name
-
-
Method Detail
-
addGlyph
public int addGlyph(int c, java.awt.font.GlyphVector gv)
Add a glyph to the font- Parameters:
c
- the Unicode Codepoint this glyph maps togv
- the GlyphVector - this should be created from an existingFont
with a size of 1, and represent a single character- Returns:
- the index of the new glyph - the maximum allowed value is 255
- Throws:
java.lang.IllegalStateException
- if the font already has 255 entries
-
addGlyph
public int addGlyph(int c, int advance, java.awt.Shape shape)
Add a glyph to the font.- Parameters:
c
- the Unicode Codepoint this glyph maps toadvance
- the advance, which should be a number between 0 (no advance) and 1000 (for a square glyph)shape
- the glyph outline. This should be in PDF glyph units, which means that (0,0) is the left edge of the baseline, positive Y values are above the baseline, and the glyph is in a box of 1000 x 1000.- Returns:
- the index of the new glyph - the maximum allowed value is 255
- Throws:
java.lang.IllegalStateException
- if the font already has 255 entries
-
isEmbedded
public boolean isEmbedded()
Type3 Fonts are always embedded- Overrides:
isEmbedded
in classPDFFont
-
getSize
public int getSize()
Return the maximum entry in this font, from 0 (for empty) to 255.- Since:
- 2.23.4
-
isItalic
public boolean isItalic()
Description copied from class:PDFFont
Return true if the font is italic
-
isBold
public boolean isBold()
Description copied from class:PDFFont
Return true if the font is bold
-
isSerif
public boolean isSerif()
Description copied from class:PDFFont
Return true if the font is serif
-
isMonospace
public boolean isMonospace()
Description copied from class:PDFFont
Return true if every character has the same width (like Courier), false if every character is potentially a different width (like Times-Roman)- Specified by:
isMonospace
in classPDFFont
-
getAscender
public float getAscender()
Description copied from class:PDFFont
Get the Ascender for the font (the maximum height above the baseline the font extends), as a proportion of the point size. The exact source of of this value is undefined except for OpenTypeFonts, where it comes from the "hhea.ascender" value normally, or the from "OS2.sTypoAscender" flag if the USE_TYPO_METRICS flag is set.- Specified by:
getAscender
in classPDFFont
-
getDescender
public float getDescender()
Description copied from class:PDFFont
Get the Descender for the font (the maximum height below the baseline the font extends), as a proportion of the point size. The returned value is usually negative. The exact source of this value is undefined except for OpenTypeFonts, where it comes from the "hhea.descender" value normally, or the from "OS2.sTypoDescender" flag if the USE_TYPO_METRICS flag is set.- Specified by:
getDescender
in classPDFFont
-
getXHeight
public float getXHeight()
Description copied from class:PDFFont
Get the X-Height of the font - normally the height of a lower-case 'x' character.- Specified by:
getXHeight
in classPDFFont
-
getCapHeight
public float getCapHeight()
Description copied from class:PDFFont
Get the Cap-Height of the font - normally the height of an upper-case 'O' character- Specified by:
getCapHeight
in classPDFFont
-
getDefaultLeading
public float getDefaultLeading()
Description copied from class:PDFFont
Get the default leading for this font - the preferred distance between two successive baselines of text. Values are a ratio of the font size, and are typically between 1 and 1.3
Note that the values of the different spacing-between-lines methods have changed - in versions 1.0.4 and earlier this routine normally returned 1 and the spacing was set by the
PDFStyle.setTextLineSpacing(float)
method. Since 1.1, the values for these two methods are effectively reversed. See the relevant method comments in thePDFStyle
class for more information.- Specified by:
getDefaultLeading
in classPDFFont
-
getUnderlineThickness
public float getUnderlineThickness()
Description copied from class:PDFFont
Get the underline thickness, as a proportion of the font size.- Specified by:
getUnderlineThickness
in classPDFFont
-
getUnderlinePosition
public float getUnderlinePosition()
Description copied from class:PDFFont
Get the underline position, as a proportion of the font size. Like thegetDescender()
method, the returned value is almost always negative, indicating below the baseline. The distance is from the baseline to the center of the underline.- Specified by:
getUnderlinePosition
in classPDFFont
-
getStrikeoutThickness
public float getStrikeoutThickness()
Description copied from class:PDFFont
Get the strikeout thickness, as a proportion of the font size.- Specified by:
getStrikeoutThickness
in classPDFFont
-
getStrikeoutPosition
public float getStrikeoutPosition()
Description copied from class:PDFFont
Get the strikeout position, as a proportion of the font size. The value is the distance from the baseline to the center of the strikeout, and shuold be positive.- Specified by:
getStrikeoutPosition
in classPDFFont
-
getSuperscriptPosition
public float getSuperscriptPosition()
Description copied from class:PDFFont
Get the recommended position of a super-script version of this font, as a proportion of the sub-scripted font size. Value is always positive.
For some fonts (like CJK or barcode fonts) where there is no concept of super or subscript, this value is entirely arbitrary.
- Specified by:
getSuperscriptPosition
in classPDFFont
-
getSubscriptPosition
public float getSubscriptPosition()
Description copied from class:PDFFont
Get the recommended position of a sub-script version of this font, as a proportion of the sub-scripted font size. Value is almost always zero or negative.
For some fonts (like CJK or barcode fonts) where there is no concept of super or subscript, this value is entirely arbitrary.
- Specified by:
getSubscriptPosition
in classPDFFont
-
getSubscriptSize
public float getSubscriptSize()
Description copied from class:PDFFont
Get the recommended size of a super/sub script version of this font, as a proportion of the normal font size. Typical value is around 0.6.
For some fonts (like CJK or barcode fonts) where there is no concept of super or subscript, this value is entirely arbitrary.
- Specified by:
getSubscriptSize
in classPDFFont
-
getDefinedCodepoints
public java.util.BitSet getDefinedCodepoints()
Description copied from class:PDFFont
Return read-only BitSet containing all the Unicode codepoints defined in this font- Specified by:
getDefinedCodepoints
in classPDFFont
-
isDefined
public boolean isDefined(int codepoint)
Description copied from class:PDFFont
Return true if the specified Unicode character is defined in the font. This method is identical toPDFFont.isDefined(char)
but takes anint
, to cater for the new Unicode 4.0 codepoints added in Java 1.5.
-
toString
public java.lang.String toString()
-
putLiteral
public void putLiteral(java.lang.String key, java.lang.String tokens)
Put a literal token sequnce. For debugging- Parameters:
key
- the keytokens
- the token sequence, eg "true" or "/foo" or "[/Foo/Bar]". No refs, just direct objects.
-
-