Package com.itextpdf.kernel.font
Class PdfFont
- java.lang.Object
-
- com.itextpdf.kernel.pdf.PdfObjectWrapper<PdfDictionary>
-
- com.itextpdf.kernel.font.PdfFont
-
- Direct Known Subclasses:
PdfSimpleFont
,PdfType0Font
public abstract class PdfFont extends PdfObjectWrapper<PdfDictionary>
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
embedded
true if the font is to be embedded in the PDF.protected static byte[]
EMPTY_BYTES
protected FontProgram
fontProgram
protected boolean
newFont
false, if the font comes from PdfDocument.protected java.util.Map<java.lang.Integer,Glyph>
notdefGlyphs
static int
SIMPLE_FONT_MAX_CHAR_CODE_VALUE
The upper bound value for char code.protected boolean
subset
Indicates if all the glyphs and widths for that particular encoding should be included in the document.protected java.util.List<int[]>
subsetRanges
-
Constructor Summary
Constructors Modifier Constructor Description protected
PdfFont()
protected
PdfFont(PdfDictionary fontDictionary)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addSubsetRange(int[] range)
Adds a character range when subsetting.abstract int
appendAnyGlyph(java.lang.String text, int from, java.util.List<Glyph> glyphs)
Append any single glyph, even notdef.boolean
appendDecodedCodesToGlyphsList(java.util.List<Glyph> list, PdfString characterCodes)
Decodes sequence of character codes (e.g.abstract int
appendGlyphs(java.lang.String text, int from, int to, java.util.List<Glyph> glyphs)
Append all supported glyphs and return number of processed chars.boolean
containsGlyph(int unicode)
Check whether font contains glyph with specified unicode.abstract byte[]
convertToBytes(Glyph glyph)
abstract byte[]
convertToBytes(GlyphLine glyphLine)
abstract byte[]
convertToBytes(java.lang.String text)
Converts the text into bytes to be placed in the document.abstract GlyphLine
createGlyphLine(java.lang.String content)
abstract java.lang.String
decode(PdfString content)
abstract GlyphLine
decodeIntoGlyphLine(PdfString characterCodes)
Decodes sequence of character codes (e.g.void
flush()
To manually flush aPdfObject
behind this wrapper, you have to ensure that this object is added to the document, i.e.float
getAscent(int unicode, float fontSize)
Gets the ascent of a char code in normalized 1000 units.float
getAscent(java.lang.String text, float fontSize)
Gets the ascent of aString
in points.abstract float
getContentWidth(PdfString content)
float
getDescent(int unicode, float fontSize)
Gets the descent of a char code in points.float
getDescent(java.lang.String text, float fontSize)
Gets the descent of aString
in points.protected abstract PdfDictionary
getFontDescriptor(java.lang.String fontName)
FontProgram
getFontProgram()
abstract Glyph
getGlyph(int unicode)
Get glyph by unicodeprotected PdfStream
getPdfFontStream(byte[] fontStreamBytes, int[] fontStreamLengths)
CreatePdfStream
based onfontStreamBytes
.int
getWidth(int unicode)
Returns the width of a certain character of this font in 1000 normalized units.float
getWidth(int unicode, float fontSize)
Returns the width of a certain character of this font in points.int
getWidth(java.lang.String text)
Returns the width of a string of this font in 1000 normalized units.float
getWidth(java.lang.String text, float fontSize)
Gets the width of aString
in points.boolean
isBuiltWith(java.lang.String fontProgram, java.lang.String encoding)
Checks whether thePdfFont
was built with corresponding fontProgram and encoding or CMAP.boolean
isEmbedded()
boolean
isSubset()
Indicates if all the glyphs and widths for that particular encoding should be included in the document.protected boolean
isWrappedObjectMustBeIndirect()
Defines if the object behind this wrapper must be an indirect object in the resultant document.(package private) boolean
makeObjectIndirect(PdfObject obj)
Helper method for making an object indirect, if the object already is indirect.void
setSubset(boolean subset)
Indicates if all the glyphs and widths for that particular encoding should be included in the document.java.util.List<java.lang.String>
splitString(java.lang.String text, float fontSize, float maxWidth)
java.lang.String
toString()
protected static java.lang.String
updateSubsetPrefix(java.lang.String fontName, boolean isSubset, boolean isEmbedded)
Adds a unique subset prefix to be added to the font name when the font is embedded and subsetted.abstract void
writeText(GlyphLine text, int from, int to, PdfOutputStream stream)
abstract void
writeText(java.lang.String text, PdfOutputStream stream)
-
Methods inherited from class com.itextpdf.kernel.pdf.PdfObjectWrapper
ensureObjectIsAddedToDocument, ensureUnderlyingObjectHasIndirectReference, getPdfObject, isFlushed, makeIndirect, makeIndirect, markObjectAsIndirect, setForbidRelease, setModified, setPdfObject, unsetForbidRelease
-
-
-
-
Field Detail
-
SIMPLE_FONT_MAX_CHAR_CODE_VALUE
public static final int SIMPLE_FONT_MAX_CHAR_CODE_VALUE
The upper bound value for char code. As for simple fonts char codes are a single byte values, it may vary from 0 to 255.- See Also:
- Constant Field Values
-
fontProgram
protected FontProgram fontProgram
-
EMPTY_BYTES
protected static final byte[] EMPTY_BYTES
-
notdefGlyphs
protected java.util.Map<java.lang.Integer,Glyph> notdefGlyphs
-
newFont
protected boolean newFont
false, if the font comes from PdfDocument.
-
embedded
protected boolean embedded
true if the font is to be embedded in the PDF.
-
subset
protected boolean subset
Indicates if all the glyphs and widths for that particular encoding should be included in the document.
-
subsetRanges
protected java.util.List<int[]> subsetRanges
-
-
Constructor Detail
-
PdfFont
protected PdfFont(PdfDictionary fontDictionary)
-
PdfFont
protected PdfFont()
-
-
Method Detail
-
getGlyph
public abstract Glyph getGlyph(int unicode)
Get glyph by unicode- Parameters:
unicode
- a unicode code point- Returns:
Glyph
if it exists or .NOTDEF if supported, otherwisenull
.
-
containsGlyph
public boolean containsGlyph(int unicode)
Check whether font contains glyph with specified unicode.- Parameters:
unicode
- a unicode code point- Returns:
- true if font contains glyph, represented with the unicode code point, otherwise false.
-
createGlyphLine
public abstract GlyphLine createGlyphLine(java.lang.String content)
-
appendGlyphs
public abstract int appendGlyphs(java.lang.String text, int from, int to, java.util.List<Glyph> glyphs)
Append all supported glyphs and return number of processed chars. Composite font supports surrogate pairs.- Parameters:
text
- String to convert to glyphs.from
- from index of the text.to
- to index of the text.glyphs
- array for a new glyphs, shall not be null.- Returns:
- number of processed chars from text.
-
appendAnyGlyph
public abstract int appendAnyGlyph(java.lang.String text, int from, java.util.List<Glyph> glyphs)
Append any single glyph, even notdef. Returns number of processed chars: 2 in case surrogate pair, otherwise 1.- Parameters:
text
- String to convert to glyphs.from
- from index of the text.glyphs
- array for a new glyph, shall not be null.- Returns:
- number of processed chars: 2 in case surrogate pair, otherwise 1
-
convertToBytes
public abstract byte[] convertToBytes(java.lang.String text)
Converts the text into bytes to be placed in the document. The conversion is done according to the font and the encoding and the characters used are stored.- Parameters:
text
- the text to convert- Returns:
- the conversion
-
convertToBytes
public abstract byte[] convertToBytes(GlyphLine glyphLine)
-
decode
public abstract java.lang.String decode(PdfString content)
-
decodeIntoGlyphLine
public abstract GlyphLine decodeIntoGlyphLine(PdfString characterCodes)
Decodes sequence of character codes (e.g. from content stream) into aGlyphLine
- Parameters:
characterCodes
- the string which is interpreted as a sequence of character codes. Note, thatPdfString
acts as a storage for char code values specific to given font, therefore individual character codes must not be interpreted as code units of the UTF-16 encoding- Returns:
- the
GlyphLine
containing the glyphs encoded by the passed string
-
appendDecodedCodesToGlyphsList
public boolean appendDecodedCodesToGlyphsList(java.util.List<Glyph> list, PdfString characterCodes)
Decodes sequence of character codes (e.g. from content stream) to sequence of glyphs and appends them to the passed list.- Parameters:
list
- the list to the end of which decoded glyphs are to be addedcharacterCodes
- the string which is interpreted as a sequence of character codes. Note, thatPdfString
acts as a storage for char code values specific to given font, therefore individual character codes must not be interpreted as code units of the UTF-16 encoding- Returns:
- true if all codes where successfully decoded, false otherwise
-
getContentWidth
public abstract float getContentWidth(PdfString content)
-
convertToBytes
public abstract byte[] convertToBytes(Glyph glyph)
-
writeText
public abstract void writeText(GlyphLine text, int from, int to, PdfOutputStream stream)
-
writeText
public abstract void writeText(java.lang.String text, PdfOutputStream stream)
-
getWidth
public int getWidth(int unicode)
Returns the width of a certain character of this font in 1000 normalized units.- Parameters:
unicode
- a certain character.- Returns:
- a width in Text Space.
-
getWidth
public float getWidth(int unicode, float fontSize)
Returns the width of a certain character of this font in points.- Parameters:
unicode
- a certain character.fontSize
- the font size.- Returns:
- a width in points.
-
getWidth
public int getWidth(java.lang.String text)
Returns the width of a string of this font in 1000 normalized units.- Parameters:
text
- a string content.- Returns:
- a width of string in Text Space.
-
getWidth
public float getWidth(java.lang.String text, float fontSize)
Gets the width of aString
in points.- Parameters:
text
- theString
to get the width offontSize
- the font size- Returns:
- the width in points
-
getDescent
public float getDescent(java.lang.String text, float fontSize)
Gets the descent of aString
in points. The descent will always be less than or equal to zero even if all the characters have a higher descent.- Parameters:
text
- theString
to get the descent offontSize
- the font size- Returns:
- the descent in points
-
getDescent
public float getDescent(int unicode, float fontSize)
Gets the descent of a char code in points. The descent will always be less than or equal to zero even if all the characters have a higher descent.- Parameters:
unicode
- the char code to get the descent offontSize
- the font size- Returns:
- the descent in points
-
getAscent
public float getAscent(java.lang.String text, float fontSize)
Gets the ascent of aString
in points. The ascent will always be greater than or equal to zero even if all the characters have a lower ascent.- Parameters:
text
- theString
to get the ascent offontSize
- the font size- Returns:
- the ascent in points
-
getAscent
public float getAscent(int unicode, float fontSize)
Gets the ascent of a char code in normalized 1000 units. The ascent will always be greater than or equal to zero even if all the characters have a lower ascent.- Parameters:
unicode
- the char code to get the ascent offontSize
- the font size- Returns:
- the ascent in points
-
getFontProgram
public FontProgram getFontProgram()
-
isEmbedded
public boolean isEmbedded()
-
isSubset
public boolean isSubset()
Indicates if all the glyphs and widths for that particular encoding should be included in the document.- Returns:
false
to include all the glyphs and widths.
-
setSubset
public void setSubset(boolean subset)
Indicates if all the glyphs and widths for that particular encoding should be included in the document. When set totrue
only the glyphs used will be included in the font. When set tofalse
the full font will be included and all subset ranges will be removed.- Parameters:
subset
- new value of property subset- See Also:
addSubsetRange(int[])
-
addSubsetRange
public void addSubsetRange(int[] range)
Adds a character range when subsetting. The range is anint
array where the first element is the start range inclusive and the second element is the end range inclusive. Several ranges are allowed in the same array. Note, #setSubset(true) will be called implicitly therefore this range is an addition to the used glyphs.- Parameters:
range
- the character range
-
splitString
public java.util.List<java.lang.String> splitString(java.lang.String text, float fontSize, float maxWidth)
-
isBuiltWith
public boolean isBuiltWith(java.lang.String fontProgram, java.lang.String encoding)
Checks whether thePdfFont
was built with corresponding fontProgram and encoding or CMAP. Default value is false unless overridden.- Parameters:
fontProgram
- a font name or path to a font programencoding
- an encoding or CMAP- Returns:
- true, if the PdfFont was built with the fontProgram and encoding. Otherwise false.
- See Also:
PdfDocument.findFont(String, String)
,FontProgram.isBuiltWith(String)
,FontEncoding.isBuiltWith(String)
,CMapEncoding.isBuiltWith(String)
-
flush
public void flush()
To manually flush aPdfObject
behind this wrapper, you have to ensure that this object is added to the document, i.e. it has an indirect reference. Basically this means that before flushing you need to explicitly callPdfObjectWrapper.makeIndirect(PdfDocument)
. For example: wrapperInstance.makeIndirect(document).flush(); Note that not every wrapper require this, only those that have such warning in documentation.- Overrides:
flush
in classPdfObjectWrapper<PdfDictionary>
-
getFontDescriptor
protected abstract PdfDictionary getFontDescriptor(java.lang.String fontName)
-
isWrappedObjectMustBeIndirect
protected boolean isWrappedObjectMustBeIndirect()
Description copied from class:PdfObjectWrapper
Defines if the object behind this wrapper must be an indirect object in the resultant document.
If this method returns true it doesn't necessarily mean that object must be in the indirect state at any moment, but rather defines that when the object will be written to the document it will be transformed into indirect object if it's not indirect yet.
Return value of this method shouldn't depend on any logic, it should return always true or false.- Specified by:
isWrappedObjectMustBeIndirect
in classPdfObjectWrapper<PdfDictionary>
- Returns:
- true if in the resultant document the object behind the wrapper must be indirect, otherwise false.
-
updateSubsetPrefix
protected static java.lang.String updateSubsetPrefix(java.lang.String fontName, boolean isSubset, boolean isEmbedded)
Adds a unique subset prefix to be added to the font name when the font is embedded and subsetted.- Parameters:
fontName
- the original font name.isSubset
- denotes whether font in question is subsetted, i.e. only used symbols are kept in it.isEmbedded
- denotes whether font in question is embedded into the PDF document.- Returns:
- the font name prefixed with subset if isSubset and isEmbedded are true, otherwise original font name is returned intact.
-
getPdfFontStream
protected PdfStream getPdfFontStream(byte[] fontStreamBytes, int[] fontStreamLengths)
CreatePdfStream
based onfontStreamBytes
.- Parameters:
fontStreamBytes
- original font data, must be not null.fontStreamLengths
- array to generateLength*
keys, must be not null.- Returns:
- the PdfStream containing the font or
null
, if there is an error reading the font. - Throws:
PdfException
- Method will throw exception iffontStreamBytes
isnull
.
-
makeObjectIndirect
boolean makeObjectIndirect(PdfObject obj)
Helper method for making an object indirect, if the object already is indirect. Useful for FontDescriptor and FontFile to make possible immediate flushing. If there is no PdfDocument, mark the object asMUST_BE_INDIRECT
.- Parameters:
obj
- an object to make indirect.- Returns:
- if current object isn't indirect, returns
false
, otherwisetree
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-