Class FontUtils

java.lang.Object
org.sejda.impl.sambox.util.FontUtils

public final class FontUtils extends Object
Utility to map from Sejda font definition to PDFBox.
Author:
Andrea Vacondio
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Helper for subset fonts.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final List<org.sejda.impl.sambox.component.font.FallbackFontsProvider>
     
    static org.sejda.sambox.pdmodel.font.PDFont
     
    static final String
     
    static final FontResource[]
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    areEncodeDecodeSame(org.sejda.sambox.pdmodel.font.PDFont font, String text)
     
    static void
    assertEncodeDecodeSame(org.sejda.sambox.pdmodel.font.PDFont font, String text)
     
    static double
    calculateBBoxHeight(String text, org.sejda.sambox.pdmodel.font.PDFont font)
     
    static boolean
    canDisplay(String text, org.sejda.sambox.pdmodel.font.PDFont font)
     
    static boolean
    canDisplaySpace(org.sejda.sambox.pdmodel.font.PDFont font)
     
    static void
     
    static void
    clearLoadedFontCache(org.sejda.sambox.pdmodel.PDDocument document)
     
    static String
    decode(org.sejda.sambox.pdmodel.font.PDFont font, byte[] bytes)
     
    static org.sejda.sambox.pdmodel.font.PDFont
    findFontFor(org.sejda.sambox.pdmodel.PDDocument document, String text)
     
    static org.sejda.sambox.pdmodel.font.PDFont
    findFontFor(org.sejda.sambox.pdmodel.PDDocument document, String text, boolean bold, org.sejda.sambox.pdmodel.font.PDFont originalFont)
     
    static org.sejda.sambox.pdmodel.font.PDFont
    fontOrFallback(String text, org.sejda.sambox.pdmodel.font.PDFont font, org.sejda.sambox.pdmodel.PDDocument document)
    Checks the text can be written with the given font, find a fallback font otherwise
    static double
    getSimpleStringWidth(String text, org.sejda.sambox.pdmodel.font.PDFont font, double fontSize)
    Calculates the width of the string using the given font.
    static org.sejda.sambox.pdmodel.font.PDType1Font
    Mapping between Sejda and PDFBox standard type 1 fonts implementation
    static boolean
    isBold(org.sejda.sambox.pdmodel.font.PDFont font)
     
    static boolean
    isItalic(org.sejda.sambox.pdmodel.font.PDFont font)
     
    static boolean
     
    static org.sejda.sambox.pdmodel.font.PDFont
    loadFont(org.sejda.sambox.pdmodel.PDDocument document, FontResource font)
     
    static String
    removeUnsupportedCharacters(String text, org.sejda.sambox.pdmodel.PDDocument doc)
     
    static String
    Removes all unicode whitespace characters from the input string
    static String
    replaceUnsupportedCharacters(String text, org.sejda.sambox.pdmodel.PDDocument doc, String replacement)
     
    resolveFonts(String label, org.sejda.sambox.pdmodel.font.PDFont font, org.sejda.sambox.pdmodel.PDDocument document)
    Supports writing labels which require multiple fonts (eg: mixing thai and english words) Returns a list of text with associated font.
    static List<String>
    resolveTextFragments(String input, org.sejda.sambox.pdmodel.font.PDFont font)
    Splits an input string into multiple fragments, when glyphs with 0 width are detected
    static List<String>
    wrapLines(String rawLabel, org.sejda.sambox.pdmodel.font.PDFont font, float fontSize, double maxWidth, org.sejda.sambox.pdmodel.PDDocument document)
    Wraps the given text on multiple lines, if it does not fit within the given maxWidth It will try to determine if all text can be written with given font and find a fallback for parts that are not supported.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • HELVETICA

      public static org.sejda.sambox.pdmodel.font.PDFont HELVETICA
    • TYPE0FONTS

      public static final FontResource[] TYPE0FONTS
    • FALLBACK_FONTS_PROVIDERS

      public static final List<org.sejda.impl.sambox.component.font.FallbackFontsProvider> FALLBACK_FONTS_PROVIDERS
    • REMARK_FROM_SEJDA_FONT_RESOURCE

      public static final String REMARK_FROM_SEJDA_FONT_RESOURCE
      See Also:
  • Method Details

    • getStandardType1Font

      public static org.sejda.sambox.pdmodel.font.PDType1Font getStandardType1Font(StandardType1Font st1Font)
      Mapping between Sejda and PDFBox standard type 1 fonts implementation
      Parameters:
      st1Font -
      Returns:
      the PDFBox font.
    • fontOrFallback

      public static org.sejda.sambox.pdmodel.font.PDFont fontOrFallback(String text, org.sejda.sambox.pdmodel.font.PDFont font, org.sejda.sambox.pdmodel.PDDocument document)
      Checks the text can be written with the given font, find a fallback font otherwise
    • clearLoadedFontCache

      public static void clearLoadedFontCache()
    • clearLoadedFontCache

      public static void clearLoadedFontCache(org.sejda.sambox.pdmodel.PDDocument document)
    • loadFont

      public static org.sejda.sambox.pdmodel.font.PDFont loadFont(org.sejda.sambox.pdmodel.PDDocument document, FontResource font)
    • findFontFor

      public static org.sejda.sambox.pdmodel.font.PDFont findFontFor(org.sejda.sambox.pdmodel.PDDocument document, String text)
    • findFontFor

      public static org.sejda.sambox.pdmodel.font.PDFont findFontFor(org.sejda.sambox.pdmodel.PDDocument document, String text, boolean bold, org.sejda.sambox.pdmodel.font.PDFont originalFont)
      Parameters:
      document -
      text -
      Returns:
      a font capable of displaying the given string or null
    • isOnlyWhitespace

      public static boolean isOnlyWhitespace(String text)
      Parameters:
      text -
      Returns:
      true if given text contains only unicode whitespace characters
    • removeWhitespace

      public static String removeWhitespace(String text)
      Removes all unicode whitespace characters from the input string
      Parameters:
      text -
      Returns:
      the resulting string
    • canDisplaySpace

      public static boolean canDisplaySpace(org.sejda.sambox.pdmodel.font.PDFont font)
    • canDisplay

      public static boolean canDisplay(String text, org.sejda.sambox.pdmodel.font.PDFont font)
      Returns:
      true if the given font can display the given text. IMPORTANT: Ignores all whitespace in text.
    • calculateBBoxHeight

      public static double calculateBBoxHeight(String text, org.sejda.sambox.pdmodel.font.PDFont font)
    • isBold

      public static boolean isBold(org.sejda.sambox.pdmodel.font.PDFont font)
    • isItalic

      public static boolean isItalic(org.sejda.sambox.pdmodel.font.PDFont font)
    • wrapLines

      public static List<String> wrapLines(String rawLabel, org.sejda.sambox.pdmodel.font.PDFont font, float fontSize, double maxWidth, org.sejda.sambox.pdmodel.PDDocument document) throws TaskIOException
      Wraps the given text on multiple lines, if it does not fit within the given maxWidth It will try to determine if all text can be written with given font and find a fallback for parts that are not supported.
      Throws:
      TaskIOException
    • getSimpleStringWidth

      public static double getSimpleStringWidth(String text, org.sejda.sambox.pdmodel.font.PDFont font, double fontSize) throws IOException
      Calculates the width of the string using the given font. Does not try to find out if the text can actually be written with the given font and find fallback
      Throws:
      IOException
    • resolveFonts

      public static List<TextWithFont> resolveFonts(String label, org.sejda.sambox.pdmodel.font.PDFont font, org.sejda.sambox.pdmodel.PDDocument document)
      Supports writing labels which require multiple fonts (eg: mixing thai and english words) Returns a list of text with associated font.
    • resolveTextFragments

      public static List<String> resolveTextFragments(String input, org.sejda.sambox.pdmodel.font.PDFont font)
      Splits an input string into multiple fragments, when glyphs with 0 width are detected
    • removeUnsupportedCharacters

      public static String removeUnsupportedCharacters(String text, org.sejda.sambox.pdmodel.PDDocument doc)
    • replaceUnsupportedCharacters

      public static String replaceUnsupportedCharacters(String text, org.sejda.sambox.pdmodel.PDDocument doc, String replacement)
    • areEncodeDecodeSame

      public static boolean areEncodeDecodeSame(org.sejda.sambox.pdmodel.font.PDFont font, String text) throws IOException
      Throws:
      IOException
    • assertEncodeDecodeSame

      public static void assertEncodeDecodeSame(org.sejda.sambox.pdmodel.font.PDFont font, String text) throws IOException
      Throws:
      IOException
    • decode

      public static String decode(org.sejda.sambox.pdmodel.font.PDFont font, byte[] bytes) throws IOException
      Throws:
      IOException