Class PdfFont

Direct Known Subclasses:
PdfSimpleFont, PdfType0Font

public abstract class PdfFont extends PdfObjectWrapper<PdfDictionary>
  • Field Details

    • 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:
    • fontProgram

      protected FontProgram fontProgram
    • EMPTY_BYTES

      protected static final byte[] EMPTY_BYTES
    • notdefGlyphs

      protected Map<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 List subsetRanges
  • Constructor Details

    • PdfFont

      protected PdfFont (PdfDictionary fontDictionary)
    • PdfFont

      protected PdfFont()
  • Method Details

    • 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, otherwise null.
    • 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 (String content)
    • appendGlyphs

      public abstract int appendGlyphs (String text, int from, int to, 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 (String text, int from, 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 (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 String decode (PdfString content)
    • decodeIntoGlyphLine

      public abstract GlyphLine decodeIntoGlyphLine (PdfString characterCodes)
      Decodes sequence of character codes (e.g. from content stream) into a GlyphLine
      Parameters:
      characterCodes - the string which is interpreted as a sequence of character codes. Note, that PdfString 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 (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 added
      characterCodes - the string which is interpreted as a sequence of character codes. Note, that PdfString 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 (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 (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 (String text, float fontSize)
      Gets the width of a String in points.
      Parameters:
      text - the String to get the width of
      fontSize - the font size
      Returns:
      the width in points
    • getDescent

      public int getDescent (String text, float fontSize)
      Gets the descent of a String in points. The descent will always be less than or equal to zero even if all the characters have an higher descent.
      Parameters:
      text - the String to get the descent of
      fontSize - the font size
      Returns:
      the descent in points
    • getDescent

      public int 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 an higher descent.
      Parameters:
      unicode - the char code to get the descent of
      fontSize - the font size
      Returns:
      the descent in points
    • getAscent

      public int getAscent (String text, float fontSize)
      Gets the ascent of a String in points. The ascent will always be greater than or equal to zero even if all the characters have a lower ascent.
      Parameters:
      text - the String to get the ascent of
      fontSize - the font size
      Returns:
      the ascent in points
    • getAscent

      public int 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 of
      fontSize - 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 to true only the glyphs used will be included in the font. When set to false the full font will be included and all subset ranges will be removed.
      Parameters:
      subset - new value of property subset
      See Also:
    • addSubsetRange

      public void addSubsetRange (int[] range)
      Adds a character range when subsetting. The range is an int 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 List<String> splitString (String text, float fontSize, float maxWidth)
    • isBuiltWith

      public boolean isBuiltWith (String fontProgram, String encoding)
      Checks whether the PdfFont 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 program
      encoding - an encoding or CMAP
      Returns:
      true, if the PdfFont was built with the fontProgram and encoding. Otherwise false.
      See Also:
    • flush

      public void flush()
      To manually flush a PdfObject 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 call PdfObjectWrapper.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 class PdfObjectWrapper<PdfDictionary>
    • getFontDescriptor

      protected abstract PdfDictionary getFontDescriptor (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 class PdfObjectWrapper<PdfDictionary>
      Returns:
      true if in the resultant document the object behind the wrapper must be indirect, otherwise false.
    • updateSubsetPrefix

      protected static String updateSubsetPrefix (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)
      Create PdfStream based on fontStreamBytes.
      Parameters:
      fontStreamBytes - original font data, must be not null.
      fontStreamLengths - array to generate Length* 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 if fontStreamBytes is null.
    • toString

      public String toString()
      Overrides:
      toString in class Object