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 booleanembeddedtrue if the font is to be embedded in the PDF.protected static byte[]EMPTY_BYTESprotected FontProgramfontProgramprotected booleannewFontfalse, if the font comes from PdfDocument.protected java.util.Map<java.lang.Integer,Glyph>notdefGlyphsstatic intSIMPLE_FONT_MAX_CHAR_CODE_VALUEThe upper bound value for char code.protected booleansubsetIndicates 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 protectedPdfFont()protectedPdfFont(PdfDictionary fontDictionary)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddSubsetRange(int[] range)Adds a character range when subsetting.abstract intappendAnyGlyph(java.lang.String text, int from, java.util.List<Glyph> glyphs)Append any single glyph, even notdef.booleanappendDecodedCodesToGlyphsList(java.util.List<Glyph> list, PdfString characterCodes)Decodes sequence of character codes (e.g.abstract intappendGlyphs(java.lang.String text, int from, int to, java.util.List<Glyph> glyphs)Append all supported glyphs and return number of processed chars.booleancontainsGlyph(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 GlyphLinecreateGlyphLine(java.lang.String content)abstract java.lang.Stringdecode(PdfString content)abstract GlyphLinedecodeIntoGlyphLine(PdfString characterCodes)Decodes sequence of character codes (e.g.voidflush()To manually flush aPdfObjectbehind this wrapper, you have to ensure that this object is added to the document, i.e.floatgetAscent(int unicode, float fontSize)Gets the ascent of a char code in normalized 1000 units.floatgetAscent(java.lang.String text, float fontSize)Gets the ascent of aStringin points.abstract floatgetContentWidth(PdfString content)floatgetDescent(int unicode, float fontSize)Gets the descent of a char code in points.floatgetDescent(java.lang.String text, float fontSize)Gets the descent of aStringin points.protected abstract PdfDictionarygetFontDescriptor(java.lang.String fontName)FontProgramgetFontProgram()abstract GlyphgetGlyph(int unicode)Get glyph by unicodeprotected PdfStreamgetPdfFontStream(byte[] fontStreamBytes, int[] fontStreamLengths)CreatePdfStreambased onfontStreamBytes.intgetWidth(int unicode)Returns the width of a certain character of this font in 1000 normalized units.floatgetWidth(int unicode, float fontSize)Returns the width of a certain character of this font in points.intgetWidth(java.lang.String text)Returns the width of a string of this font in 1000 normalized units.floatgetWidth(java.lang.String text, float fontSize)Gets the width of aStringin points.booleanisBuiltWith(java.lang.String fontProgram, java.lang.String encoding)Checks whether thePdfFontwas built with corresponding fontProgram and encoding or CMAP.booleanisEmbedded()booleanisSubset()Indicates if all the glyphs and widths for that particular encoding should be included in the document.protected booleanisWrappedObjectMustBeIndirect()Defines if the object behind this wrapper must be an indirect object in the resultant document.(package private) booleanmakeObjectIndirect(PdfObject obj)Helper method for making an object indirect, if the object already is indirect.voidsetSubset(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.StringtoString()protected static java.lang.StringupdateSubsetPrefix(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 voidwriteText(GlyphLine text, int from, int to, PdfOutputStream stream)abstract voidwriteText(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:
Glyphif 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, thatPdfStringacts 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
GlyphLinecontaining 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, thatPdfStringacts 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 aStringin points.- Parameters:
text- theStringto 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 aStringin points. The descent will always be less than or equal to zero even if all the characters have a higher descent.- Parameters:
text- theStringto 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 aStringin points. The ascent will always be greater than or equal to zero even if all the characters have a lower ascent.- Parameters:
text- theStringto 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:
falseto 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 totrueonly the glyphs used will be included in the font. When set tofalsethe 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 anintarray 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 thePdfFontwas 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 aPdfObjectbehind 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:
flushin classPdfObjectWrapper<PdfDictionary>
-
getFontDescriptor
protected abstract PdfDictionary getFontDescriptor(java.lang.String fontName)
-
isWrappedObjectMustBeIndirect
protected boolean isWrappedObjectMustBeIndirect()
Description copied from class:PdfObjectWrapperDefines 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:
isWrappedObjectMustBeIndirectin 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)
CreatePdfStreambased 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 iffontStreamBytesisnull.
-
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:
toStringin classjava.lang.Object
-
-