Package com.itextpdf.layout.font
Class FontProvider
- java.lang.Object
-
- com.itextpdf.layout.font.FontProvider
-
- Direct Known Subclasses:
BasicFontProvider
public class FontProvider extends java.lang.Object
Main entry point of font selector logic. Contains reusableFontSet
and collection ofPdfFont
s. FontProvider depends onPdfDocument
due toPdfFont
, so it cannot be reused for different documents unless reset withreset()
or recreated withgetFontSet()
. In the former case theFontSelectorCache
is reused and in the latter it's reinitialised. FontProvider the only end point for creatingPdfFont
.It is allowed to use only one
FontProvider
per document. If additional fonts per element needed, another instance ofFontSet
can be used. For more details seeProperty.FONT_SET
,getPdfFont(FontInfo, FontSet)
,createFontSelectorStrategy(List, FontCharacteristics, FontSet)
.Note, FontProvider does not close created
FontProgram
s, because of possible conflicts withFontCache
.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
DEFAULT_FONT_FAMILY
protected java.lang.String
defaultFontFamily
The default font-family is used byFontSelector
if it's impossible to select a font for all other set font-familiesprivate FontSelectorCache
fontSelectorCache
private IFontSelectorStrategyFactory
fontSelectorStrategyFactory
private FontSet
fontSet
protected java.util.Map<FontInfo,PdfFont>
pdfFonts
-
Constructor Summary
Constructors Constructor Description FontProvider()
Creates a new instance of FontProvider.FontProvider(FontSet fontSet)
Creates a new instance of FontProvider.FontProvider(FontSet fontSet, java.lang.String defaultFontFamily)
Creates a new instance of FontProvider.FontProvider(java.lang.String defaultFontFamily)
Creates a new instance of FontProvider.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description int
addDirectory(java.lang.String dir)
Add all the fonts from a directory.boolean
addFont(byte[] fontData)
Add font toFontSet
cache.boolean
addFont(byte[] fontData, java.lang.String encoding)
Add font toFontSet
cache.boolean
addFont(byte[] fontData, java.lang.String encoding, Range unicodeRange)
Add font toFontSet
cache.boolean
addFont(FontProgram fontProgram)
Add font toFontSet
cache.boolean
addFont(FontProgram fontProgram, java.lang.String encoding)
Add font toFontSet
cache.boolean
addFont(FontProgram fontProgram, java.lang.String encoding, Range unicodeRange)
Add font toFontSet
cache.boolean
addFont(java.lang.String fontPath)
Add font toFontSet
cache.boolean
addFont(java.lang.String fontPath, java.lang.String encoding)
Add font toFontSet
cache.boolean
addFont(java.lang.String fontPath, java.lang.String encoding, Range unicodeRange)
Add font toFontSet
cache.int
addStandardPdfFonts()
Add standard fonts toFontSet
cache.int
addSystemFonts()
Add all fonts from system directories toFontSet
cache.protected FontSelector
createFontSelector(java.util.Collection<FontInfo> fonts, java.util.List<java.lang.String> fontFamilies, FontCharacteristics fc)
Create a new instance ofFontSelector
.IFontSelectorStrategy
createFontSelectorStrategy(java.util.List<java.lang.String> fontFamilies, FontCharacteristics fc, FontSet additionalFonts)
Creates theIFontSelectorStrategy
to split text into sequences of glyphs, already tied to the fonts which contain them.boolean
getDefaultCacheFlag()
The method is used to determine whether the font should be cached or not by default.boolean
getDefaultEmbeddingFlag()
The method is used to determine whether the font should be embedded or not by default.java.lang.String
getDefaultEncoding(FontProgram fontProgram)
Gets the default encoding for specific font.java.lang.String
getDefaultFontFamily()
Gets the default font-family.FontSelector
getFontSelector(java.util.List<java.lang.String> fontFamilies, FontCharacteristics fc)
CreateFontSelector
or get from cache.FontSelector
getFontSelector(java.util.List<java.lang.String> fontFamilies, FontCharacteristics fc, FontSet additionalFonts)
CreateFontSelector
or get from cache.FontSet
getFontSet()
GetsFontSet
.PdfFont
getPdfFont(FontInfo fontInfo)
Get from cache or create a new instance ofPdfFont
.PdfFont
getPdfFont(FontInfo fontInfo, FontSet additionalFonts)
Get from cache or create a new instance ofPdfFont
.FontSelectorStrategy
getStrategy(java.lang.String text, java.util.List<java.lang.String> fontFamilies)
Deprecated.FontSelectorStrategy
getStrategy(java.lang.String text, java.util.List<java.lang.String> fontFamilies, FontCharacteristics fc)
Deprecated.FontSelectorStrategy
getStrategy(java.lang.String text, java.util.List<java.lang.String> fontFamilies, FontCharacteristics fc, FontSet additionalFonts)
Deprecated.void
reset()
ResetsPdfFont cache
.void
setFontSelectorStrategyFactory(IFontSelectorStrategyFactory factory)
Sets factory which will be used increateFontSelectorStrategy(List, FontCharacteristics, FontSet)
method.
-
-
-
Field Detail
-
DEFAULT_FONT_FAMILY
private static final java.lang.String DEFAULT_FONT_FAMILY
- See Also:
- Constant Field Values
-
fontSet
private final FontSet fontSet
-
fontSelectorCache
private final FontSelectorCache fontSelectorCache
-
defaultFontFamily
protected final java.lang.String defaultFontFamily
The default font-family is used byFontSelector
if it's impossible to select a font for all other set font-families
-
fontSelectorStrategyFactory
private IFontSelectorStrategyFactory fontSelectorStrategyFactory
-
-
Constructor Detail
-
FontProvider
public FontProvider(FontSet fontSet)
Creates a new instance of FontProvider.- Parameters:
fontSet
- predefined set of fonts, could be null.
-
FontProvider
public FontProvider()
Creates a new instance of FontProvider.
-
FontProvider
public FontProvider(java.lang.String defaultFontFamily)
Creates a new instance of FontProvider.- Parameters:
defaultFontFamily
- default font family.
-
FontProvider
public FontProvider(FontSet fontSet, java.lang.String defaultFontFamily)
Creates a new instance of FontProvider.- Parameters:
fontSet
- predefined set of fonts, could be null.defaultFontFamily
- default font family.
-
-
Method Detail
-
addFont
public boolean addFont(FontProgram fontProgram, java.lang.String encoding, Range unicodeRange)
Add font toFontSet
cache.- Parameters:
fontProgram
- the font file which will be added to font cache. TheFontProgram
instances are normally created viaFontProgramFactory
.encoding
- font encoding to createPdfFont
. Possible values for this argument are the same as forPdfFontFactory.createFont()
family of methods.unicodeRange
- sets the specific range of characters to be used from the font.- Returns:
- true, if font was successfully added, otherwise false.
-
addFont
public boolean addFont(FontProgram fontProgram, java.lang.String encoding)
Add font toFontSet
cache.- Parameters:
fontProgram
- the font file which will be added to font cache. TheFontProgram
instances are normally created viaFontProgramFactory
.encoding
- font encoding to createPdfFont
. Possible values for this argument are the same as forPdfFontFactory.createFont()
family of methods.- Returns:
- true, if font was successfully added, otherwise false.
-
addFont
public boolean addFont(FontProgram fontProgram)
Add font toFontSet
cache.- Parameters:
fontProgram
- the font file which will be added to font cache. TheFontProgram
instances are normally created viaFontProgramFactory
.- Returns:
- true, if font was successfully added, otherwise false.
-
addFont
public boolean addFont(java.lang.String fontPath, java.lang.String encoding, Range unicodeRange)
Add font toFontSet
cache.- Parameters:
fontPath
- path to the font file to add. Can be a path to file or font name, seeFontProgramFactory.createFont(String)
.encoding
- font encoding to createPdfFont
. Possible values for this argument are the same as forPdfFontFactory.createFont()
family of methods.unicodeRange
- sets the specific range of characters to be used from the font.- Returns:
- true, if font was successfully added, otherwise false.
-
addFont
public boolean addFont(java.lang.String fontPath, java.lang.String encoding)
Add font toFontSet
cache.- Parameters:
fontPath
- path to the font file to add. Can be a path to file or font name, seeFontProgramFactory.createFont(String)
.encoding
- font encoding to createPdfFont
. Possible values for this argument are the same as forPdfFontFactory.createFont()
family of methods.- Returns:
- true, if font was successfully added, otherwise false.
-
addFont
public boolean addFont(java.lang.String fontPath)
Add font toFontSet
cache.- Parameters:
fontPath
- path to the font file to add. Can be a path to file or font name, seeFontProgramFactory.createFont(String)
.- Returns:
- true, if font was successfully added, otherwise false.
-
addFont
public boolean addFont(byte[] fontData, java.lang.String encoding, Range unicodeRange)
Add font toFontSet
cache.- Parameters:
fontData
- byte content of the font program file.encoding
- font encoding to createPdfFont
. Possible values for this argument are the same as forPdfFontFactory.createFont()
family of methods.unicodeRange
- sets the specific range of characters to be used from the font.- Returns:
- true, if font was successfully added, otherwise false.
-
addFont
public boolean addFont(byte[] fontData, java.lang.String encoding)
Add font toFontSet
cache.- Parameters:
fontData
- byte content of the font program file.encoding
- font encoding to createPdfFont
. Possible values for this argument are the same as forPdfFontFactory.createFont()
family of methods.- Returns:
- true, if font was successfully added, otherwise false.
-
addFont
public boolean addFont(byte[] fontData)
Add font toFontSet
cache.- Parameters:
fontData
- byte content of the font program file.- Returns:
- true, if font was successfully added, otherwise false.
-
addDirectory
public int addDirectory(java.lang.String dir)
Add all the fonts from a directory.- Parameters:
dir
- path to directory.- Returns:
- number of added fonts.
-
addSystemFonts
public int addSystemFonts()
Add all fonts from system directories toFontSet
cache.- Returns:
- number of added fonts.
-
addStandardPdfFonts
public int addStandardPdfFonts()
Add standard fonts toFontSet
cache.- Returns:
- number of added fonts.
- See Also:
StandardFonts
-
getDefaultFontFamily
public java.lang.String getDefaultFontFamily()
Gets the default font-family.- Returns:
- the default font-family
-
getDefaultEncoding
public java.lang.String getDefaultEncoding(FontProgram fontProgram)
Gets the default encoding for specific font.- Parameters:
fontProgram
- to get default encoding- Returns:
- the default encoding
- See Also:
PdfEncodings
-
getDefaultCacheFlag
public boolean getDefaultCacheFlag()
The method is used to determine whether the font should be cached or not by default.NOTE: This method can be overridden to customize behaviour.
- Returns:
- the default cache flag
-
getDefaultEmbeddingFlag
public boolean getDefaultEmbeddingFlag()
The method is used to determine whether the font should be embedded or not by default.NOTE: This method can be overridden to customize behaviour.
- Returns:
- the default embedding flag
-
getStrategy
@Deprecated public FontSelectorStrategy getStrategy(java.lang.String text, java.util.List<java.lang.String> fontFamilies, FontCharacteristics fc, FontSet additionalFonts)
Deprecated.Gets theFontSelectorStrategy
to split specified text into sequences of glyphs, already tied to the fonts which contain them. The fonts can be taken from the added fonts to the font provider and are chosen based on font-families list and desired font characteristics.- Parameters:
text
- for splitting into sequences of glyphs.fontFamilies
- target font families to createFontSelector
for sequences of glyphs.fc
- instance ofFontCharacteristics
to createFontSelector
for sequences of glyphs.additionalFonts
- set which provides fonts additionally to the fonts added to font provider. Combined set of font provider fonts and additional fonts is used when choosing a single font for a sequence of glyphs. Additional fonts will only be used for the given font selector strategy instance and will not be otherwise preserved in font provider.- Returns:
FontSelectorStrategy
instance.
-
getStrategy
@Deprecated public FontSelectorStrategy getStrategy(java.lang.String text, java.util.List<java.lang.String> fontFamilies, FontCharacteristics fc)
Deprecated.Gets theFontSelectorStrategy
to split specified text into sequences of glyphs, already tied to the fonts which contain them. The fonts can be taken from the added fonts to the font provider and are chosen based on font-families list and desired font characteristics.- Parameters:
text
- for splitting into sequences of glyphs.fontFamilies
- target font families to createFontSelector
for sequences of glyphs.fc
- instance ofFontCharacteristics
to createFontSelector
for sequences of glyphs.- Returns:
FontSelectorStrategy
instance.
-
getStrategy
@Deprecated public FontSelectorStrategy getStrategy(java.lang.String text, java.util.List<java.lang.String> fontFamilies)
Deprecated.Gets theFontSelectorStrategy
to split specified text into sequences of glyphs, already tied to the fonts which contain them. The fonts can be taken from the added fonts to the font provider and are chosen based on font-families list and desired font characteristics.- Parameters:
text
- for splitting into sequences of glyphs.fontFamilies
- target font families to createFontSelector
for sequences of glyphs.- Returns:
FontSelectorStrategy
instance.
-
setFontSelectorStrategyFactory
public void setFontSelectorStrategyFactory(IFontSelectorStrategyFactory factory)
Sets factory which will be used increateFontSelectorStrategy(List, FontCharacteristics, FontSet)
method.- Parameters:
factory
- the factory which will be used to create font selector strategies
-
createFontSelectorStrategy
public IFontSelectorStrategy createFontSelectorStrategy(java.util.List<java.lang.String> fontFamilies, FontCharacteristics fc, FontSet additionalFonts)
Creates theIFontSelectorStrategy
to split text into sequences of glyphs, already tied to the fonts which contain them. The fonts can be taken from the added fonts to the font provider and are chosen based on font-families list and desired font characteristics.- Parameters:
fontFamilies
- target font families to createFontSelector
for sequences of glyphs.fc
- instance ofFontCharacteristics
to createFontSelector
for sequences of glyphs.additionalFonts
- set which provides fonts additionally to the fonts added to font provider. Combined set of font provider fonts and additional fonts is used when choosing a single font for a sequence of glyphs. Additional fonts will only be used for the given font selector strategy instance and will not be otherwise preserved in font provider.- Returns:
IFontSelectorStrategy
instance
-
getFontSelector
public final FontSelector getFontSelector(java.util.List<java.lang.String> fontFamilies, FontCharacteristics fc)
CreateFontSelector
or get from cache.- Parameters:
fontFamilies
- target font families.fc
- instance ofFontCharacteristics
.- Returns:
- an instance of
FontSelector
. - See Also:
createFontSelector(Collection, List, FontCharacteristics)
,getFontSelector(List, FontCharacteristics, FontSet)
-
getFontSelector
public final FontSelector getFontSelector(java.util.List<java.lang.String> fontFamilies, FontCharacteristics fc, FontSet additionalFonts)
CreateFontSelector
or get from cache.- Parameters:
fontFamilies
- target font families.fc
- instance ofFontCharacteristics
.additionalFonts
- set which provides fonts additionally to the fonts added to font provider. Combined set of font provider fonts and additional fonts is used when choosing a single font forFontSelector
. Additional fonts will only be used for the given font selector strategy instance and will not be otherwise preserved in font provider.- Returns:
- an instance of
FontSelector
. - See Also:
}
-
createFontSelector
protected FontSelector createFontSelector(java.util.Collection<FontInfo> fonts, java.util.List<java.lang.String> fontFamilies, FontCharacteristics fc)
Create a new instance ofFontSelector
. While caching is main responsibility ofgetFontSelector(List, FontCharacteristics, FontSet)
. This method just create a new instance ofFontSelector
.- Parameters:
fonts
- Set of all available fonts in current context.fontFamilies
- target font families.fc
- instance ofFontCharacteristics
.- Returns:
- an instance of
FontSelector
.
-
getPdfFont
public PdfFont getPdfFont(FontInfo fontInfo)
Get from cache or create a new instance ofPdfFont
.- Parameters:
fontInfo
- font info, to createFontProgram
andPdfFont
.- Returns:
- cached or new instance of
PdfFont
.
-
getPdfFont
public PdfFont getPdfFont(FontInfo fontInfo, FontSet additionalFonts)
Get from cache or create a new instance ofPdfFont
.- Parameters:
fontInfo
- font info, to createFontProgram
andPdfFont
.additionalFonts
- set of additional fonts to consider.- Returns:
- cached or new instance of
PdfFont
.
-
reset
public void reset()
-
-