Class FontProvider

java.lang.Object
com.itextpdf.layout.font.FontProvider
Direct Known Subclasses:
BasicFontProvider

public class FontProvider extends Object
Main entry point of font selector logic. Contains reusable FontSet and collection of PdfFonts. FontProvider depends on PdfDocument due to PdfFont, so it cannot be reused for different documents unless reset with reset() or recreated with getFontSet(). In the former case the FontSelectorCache is reused and in the latter it's reinitialised. FontProvider the only end point for creating PdfFont.

It is allowed to use only one FontProvider per document. If additional fonts per element needed, another instance of FontSet can be used. For more details see Property.FONT_SET, getPdfFont(FontInfo, FontSet), createFontSelectorStrategy(List, FontCharacteristics, FontSet).

Note, FontProvider does not close created FontPrograms, because of possible conflicts with FontCache.

  • Field Details

    • defaultFontFamily

      protected final String defaultFontFamily
      The default font-family is used by FontSelector if it's impossible to select a font for all other set font-families
    • pdfFonts

      protected final Map<FontInfo,PdfFont> pdfFonts
  • Constructor Details

    • 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 (String defaultFontFamily)
      Creates a new instance of FontProvider.
      Parameters:
      defaultFontFamily - default font family.
    • FontProvider

      public FontProvider (FontSet fontSet, String defaultFontFamily)
      Creates a new instance of FontProvider.
      Parameters:
      fontSet - predefined set of fonts, could be null.
      defaultFontFamily - default font family.
  • Method Details

    • addFont

      public boolean addFont (FontProgram fontProgram, String encoding, Range unicodeRange)
      Add font to FontSet cache.
      Parameters:
      fontProgram - the font file which will be added to font cache. The FontProgram instances are normally created via FontProgramFactory.
      encoding - font encoding to create PdfFont. Possible values for this argument are the same as for PdfFontFactory.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, String encoding)
      Add font to FontSet cache.
      Parameters:
      fontProgram - the font file which will be added to font cache. The FontProgram instances are normally created via FontProgramFactory.
      encoding - font encoding to create PdfFont. Possible values for this argument are the same as for PdfFontFactory.createFont() family of methods.
      Returns:
      true, if font was successfully added, otherwise false.
    • addFont

      public boolean addFont (FontProgram fontProgram)
      Add font to FontSet cache.
      Parameters:
      fontProgram - the font file which will be added to font cache. The FontProgram instances are normally created via FontProgramFactory.
      Returns:
      true, if font was successfully added, otherwise false.
    • addFont

      public boolean addFont (String fontPath, String encoding, Range unicodeRange)
      Add font to FontSet cache.
      Parameters:
      fontPath - path to the font file to add. Can be a path to file or font name, see FontProgramFactory.createFont(String).
      encoding - font encoding to create PdfFont. Possible values for this argument are the same as for PdfFontFactory.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 (String fontPath, String encoding)
      Add font to FontSet cache.
      Parameters:
      fontPath - path to the font file to add. Can be a path to file or font name, see FontProgramFactory.createFont(String).
      encoding - font encoding to create PdfFont. Possible values for this argument are the same as for PdfFontFactory.createFont() family of methods.
      Returns:
      true, if font was successfully added, otherwise false.
    • addFont

      public boolean addFont (String fontPath)
      Add font to FontSet cache.
      Parameters:
      fontPath - path to the font file to add. Can be a path to file or font name, see FontProgramFactory.createFont(String).
      Returns:
      true, if font was successfully added, otherwise false.
    • addFont

      public boolean addFont (byte[] fontData, String encoding, Range unicodeRange)
      Add font to FontSet cache.
      Parameters:
      fontData - byte content of the font program file.
      encoding - font encoding to create PdfFont. Possible values for this argument are the same as for PdfFontFactory.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, String encoding)
      Add font to FontSet cache.
      Parameters:
      fontData - byte content of the font program file.
      encoding - font encoding to create PdfFont. Possible values for this argument are the same as for PdfFontFactory.createFont() family of methods.
      Returns:
      true, if font was successfully added, otherwise false.
    • addFont

      public boolean addFont (byte[] fontData)
      Add font to FontSet cache.
      Parameters:
      fontData - byte content of the font program file.
      Returns:
      true, if font was successfully added, otherwise false.
    • addDirectory

      public int addDirectory (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 to FontSet cache.
      Returns:
      number of added fonts.
    • addStandardPdfFonts

      public int addStandardPdfFonts()
      Add standard fonts to FontSet cache.
      Returns:
      number of added fonts.
      See Also:
    • getFontSet

      public FontSet getFontSet()
      Gets FontSet.
      Returns:
      the font set
    • getDefaultFontFamily

      public String getDefaultFontFamily()
      Gets the default font-family.
      Returns:
      the default font-family
    • getDefaultEncoding

      public String getDefaultEncoding (FontProgram fontProgram)
      Gets the default encoding for specific font.
      Parameters:
      fontProgram - to get default encoding
      Returns:
      the default encoding
      See Also:
    • 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 (String text, List<String> fontFamilies, FontCharacteristics fc, FontSet additionalFonts)
      Gets the FontSelectorStrategy 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 create FontSelector for sequences of glyphs.
      fc - instance of FontCharacteristics to create FontSelector 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 (String text, List<String> fontFamilies, FontCharacteristics fc)
      Gets the FontSelectorStrategy 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 create FontSelector for sequences of glyphs.
      fc - instance of FontCharacteristics to create FontSelector for sequences of glyphs.
      Returns:
      FontSelectorStrategy instance.
    • getStrategy

      @Deprecated public FontSelectorStrategy getStrategy (String text, List<String> fontFamilies)
      Gets the FontSelectorStrategy 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 create FontSelector for sequences of glyphs.
      Returns:
      FontSelectorStrategy instance.
    • setFontSelectorStrategyFactory

      public void setFontSelectorStrategyFactory (IFontSelectorStrategyFactory factory)
      Sets factory which will be used in createFontSelectorStrategy(List, FontCharacteristics, FontSet) method.
      Parameters:
      factory - the factory which will be used to create font selector strategies
    • createFontSelectorStrategy

      public IFontSelectorStrategy createFontSelectorStrategy (List<String> fontFamilies, FontCharacteristics fc, FontSet additionalFonts)
      Creates the IFontSelectorStrategy 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 create FontSelector for sequences of glyphs.
      fc - instance of FontCharacteristics to create FontSelector 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 (List<String> fontFamilies, FontCharacteristics fc)
      Create FontSelector or get from cache.
      Parameters:
      fontFamilies - target font families.
      fc - instance of FontCharacteristics.
      Returns:
      an instance of FontSelector.
      See Also:
    • getFontSelector

      public final FontSelector getFontSelector (List<String> fontFamilies, FontCharacteristics fc, FontSet additionalFonts)
      Create FontSelector or get from cache.
      Parameters:
      fontFamilies - target font families.
      fc - instance of FontCharacteristics.
      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 FontSelector. 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 (Collection<FontInfo> fonts, List<String> fontFamilies, FontCharacteristics fc)
      Create a new instance of FontSelector. While caching is main responsibility of getFontSelector(List, FontCharacteristics, FontSet). This method just create a new instance of FontSelector.
      Parameters:
      fonts - Set of all available fonts in current context.
      fontFamilies - target font families.
      fc - instance of FontCharacteristics.
      Returns:
      an instance of FontSelector.
    • getPdfFont

      public PdfFont getPdfFont (FontInfo fontInfo)
      Get from cache or create a new instance of PdfFont.
      Parameters:
      fontInfo - font info, to create FontProgram and PdfFont.
      Returns:
      cached or new instance of PdfFont.
    • getPdfFont

      public PdfFont getPdfFont (FontInfo fontInfo, FontSet additionalFonts)
      Get from cache or create a new instance of PdfFont.
      Parameters:
      fontInfo - font info, to create FontProgram and PdfFont.
      additionalFonts - set of additional fonts to consider.
      Returns:
      cached or new instance of PdfFont.
    • reset

      public void reset()
      Resets PdfFont cache. After calling that method FontProvider can be reused with another PdfDocument