Class PdfType3Font


public class PdfType3Font extends PdfSimpleFont<Type3Font>
Low-level API class for Type 3 fonts.

In Type 3 fonts, glyphs are defined by streams of PDF graphics operators. These streams are associated with character names. A separate encoding entry maps character codes to the appropriate character names for the glyphs.

Note, that this class operates in a special way with glyph space units. In the code when working with fonts, iText expects that 1000 units of glyph-space correspond to 1 unit of text space. For Type3 fonts this is not always the case and depends on FontMatrix. That's why in PdfType3Font the font matrix and all font metrics in glyph space units are "normalized" in such way, that 1 to 1000 relation is preserved. This is done on Type3 font initialization, and is reverted back on font flushing, because the actual content streams of type3 font glyphs are left with original coordinates based on original font matrix. See also ISO-32000-2, 9.2.4 "Glyph positioning and metrics":

"The glyph coordinate system is the space in which an individual character’s glyph is defined. All path coordinates and metrics shall be interpreted in glyph space. For all font types except Type 3, the units of glyph space are one-thousandth of a unit of text space; for a Type 3 font, the transformation from glyph space to text space shall be defined by a font matrix specified in an explicit FontMatrix entry in the font."

Note, that because of this when processing Type3 glyphs content streams either process them completely independent from this class or take this normalization into account.

To be able to be wrapped with this PdfObjectWrapper the PdfObject must be indirect.

  • Field Details

    • FONT_BBOX_LLX

      private static final int FONT_BBOX_LLX
      See Also:
    • FONT_BBOX_LLY

      private static final int FONT_BBOX_LLY
      See Also:
    • FONT_BBOX_URX

      private static final int FONT_BBOX_URX
      See Also:
    • FONT_BBOX_URY

      private static final int FONT_BBOX_URY
      See Also:
    • DEFAULT_FONT_MATRIX

      private static final double[] DEFAULT_FONT_MATRIX
    • fontMatrix

      private double[] fontMatrix
    • glyphSpaceNormalizationFactor

      private double glyphSpaceNormalizationFactor
      Used to normalize font metrics expressed in glyph space units. See PdfType3Font.
  • Constructor Details

    • PdfType3Font

      PdfType3Font(PdfDocument document, boolean colorized)
      Creates a Type 3 font.
      Parameters:
      colorized - defines whether the glyph color is specified in the glyph descriptions in the font.
    • PdfType3Font

      PdfType3Font(PdfDocument document, String fontName, String fontFamily, boolean colorized)
      Creates a Type 3 font.
      Parameters:
      document - the target document of the new font.
      fontName - the PostScript name of the font, shall not be null or empty.
      fontFamily - a preferred font family name.
      colorized - indicates whether the font will be colorized
    • PdfType3Font

      PdfType3Font(PdfDictionary fontDictionary)
      Creates a Type 3 font based on an existing font dictionary, which must be an indirect object.
      Parameters:
      fontDictionary - a dictionary of type /Font, must have an indirect reference.
  • Method Details

    • getFontMatrix

      private double[] getFontMatrix()
      Gets the transformation matrix that defines relation between text and glyph spaces.
      Returns:
      the font matrix
    • setFontName

      public void setFontName(String fontName)
      Sets the PostScript name of the font.
      Parameters:
      fontName - the PostScript name of the font, shall not be null or empty.
    • setFontFamily

      public void setFontFamily(String fontFamily)
      Sets a preferred font family name.
      Parameters:
      fontFamily - a preferred font family name.
    • setFontWeight

      public void setFontWeight(int fontWeight)
      Sets font weight.
      Parameters:
      fontWeight - integer form 100 to 900. See FontWeights.
    • setCapHeight

      public void setCapHeight(int capHeight)
      Sets cap height.
      Parameters:
      capHeight - integer in glyph-space 1000-units
    • setItalicAngle

      public void setItalicAngle(int italicAngle)
      Sets the PostScript italic angle.

      Italic angle in counter-clockwise degrees from the vertical. Zero for upright text, negative for text that leans to the right (forward).

      Parameters:
      italicAngle - in counter-clockwise degrees from the vertical
    • setFontStretch

      public void setFontStretch(String fontWidth)
      Sets font width in css notation (font-stretch property)
      Parameters:
      fontWidth - FontStretches.
    • setPdfFontFlags

      public void setPdfFontFlags(int flags)
      Sets Font descriptor flags.
      Parameters:
      flags - font descriptor flags.
      See Also:
    • getType3Glyph

      public Type3Glyph getType3Glyph(int unicode)
      Returns a Type3Glyph by unicode.
      Parameters:
      unicode - glyph unicode
      Returns:
      Type3Glyph glyph, or null if this font does not contain glyph for the unicode
    • isSubset

      public boolean isSubset()
      Description copied from class: PdfFont
      Indicates if all the glyphs and widths for that particular encoding should be included in the document.
      Overrides:
      isSubset in class PdfFont
      Returns:
      false to include all the glyphs and widths.
    • isEmbedded

      public boolean isEmbedded()
      Overrides:
      isEmbedded in class PdfFont
    • getNumberOfGlyphs

      public int getNumberOfGlyphs()
      Gets count of glyphs in Type 3 font.
      Returns:
      number of glyphs.
    • addGlyph

      public Type3Glyph addGlyph(char c, int wx, int llx, int lly, int urx, int ury)
      Defines a glyph. If the character was already defined it will return the same content
      Parameters:
      c - the character to match this glyph.
      wx - the advance this character will have
      llx - the X lower left corner of the glyph bounding box. If the colorize option is true the value is ignored
      lly - the Y lower left corner of the glyph bounding box. If the colorize option is true the value is ignored
      urx - the X upper right corner of the glyph bounding box. If the colorize option is true the value is ignored
      ury - the Y upper right corner of the glyph bounding box. If the colorize option is true the value is ignored
      Returns:
      a content where the glyph can be defined
    • getGlyph

      public Glyph getGlyph(int unicode)
      Description copied from class: PdfFont
      Get glyph by unicode
      Specified by:
      getGlyph in class PdfFont
      Parameters:
      unicode - a unicode code point
      Returns:
      Glyph if it exists or .NOTDEF if supported, otherwise null.
    • containsGlyph

      public boolean containsGlyph(int unicode)
      Description copied from class: PdfFont
      Check whether font contains glyph with specified unicode.
      Overrides:
      containsGlyph in class PdfFont
      Parameters:
      unicode - a unicode code point
      Returns:
      true if font contains glyph, represented with the unicode code point, otherwise false.
    • flush

      public void flush()
      Description copied from class: PdfFont
      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 PdfFont
    • getFontDescriptor

      protected PdfDictionary getFontDescriptor(String fontName)
      Description copied from class: PdfSimpleFont
      Generates the font descriptor for this font or null if it is one of the 14 built in fonts.
      Overrides:
      getFontDescriptor in class PdfSimpleFont<Type3Font>
      Returns:
      the PdfDictionary containing the font descriptor or null.
    • buildWidthsArray

      protected PdfArray buildWidthsArray(int firstChar, int lastChar)
      Overrides:
      buildWidthsArray in class PdfSimpleFont<Type3Font>
    • addFontStream

      protected void addFontStream(PdfDictionary fontDescriptor)
      Specified by:
      addFontStream in class PdfSimpleFont<Type3Font>
    • getDocument

      protected PdfDocument getDocument()
    • getGlyphSpaceNormalizationFactor

      final double getGlyphSpaceNormalizationFactor()
    • setGlyphSpaceNormalizationFactor

      final void setGlyphSpaceNormalizationFactor(double glyphSpaceNormalizationFactor)
    • addGlyphsFromDifferences

      private void addGlyphsFromDifferences(PdfArray differences, PdfDictionary charProcsDic, int[] widths)
    • getFirstEmptyCode

      private int getFirstEmptyCode()
      Gets the first empty code that could be passed to FontEncoding.addSymbol(int, int)
      Returns:
      code from 1 to 255 or -1 if all slots are busy.
    • addGlyphsFromCharProcs

      private void addGlyphsFromCharProcs(PdfDictionary charProcsDic, int[] widths)
    • flushFontData

      private void flushFontData()
    • readWidths

      private double[] readWidths(PdfDictionary fontDictionary)
    • initializeUsedGlyphs

      private int initializeUsedGlyphs(PdfDictionary fontDictionary)
    • readFontBBox

      private double[] readFontBBox()
    • readFontMatrix

      private double[] readFontMatrix()
    • initializeTypoAscenderDescender

      private void initializeTypoAscenderDescender(double[] fontBBoxRect)
    • initializeFontBBox

      private void initializeFontBBox(double[] fontBBoxRect)
    • normalizeGlyphSpaceUnitsTo1000Units

      private void normalizeGlyphSpaceUnitsTo1000Units(double[] array)
    • normalizeGlyphSpaceUnitsTo1000Units

      private double normalizeGlyphSpaceUnitsTo1000Units(double value)
    • normalize1000UnitsToGlyphSpaceUnits

      private void normalize1000UnitsToGlyphSpaceUnits(double[] array)
    • normalize1000UnitsToGlyphSpaceUnits

      private double normalize1000UnitsToGlyphSpaceUnits(double value)
    • fillFontDescriptor

      private void fillFontDescriptor(PdfDictionary fontDesc)
    • normalizeFirstLastChar

      private int normalizeFirstLastChar(PdfNumber firstLast, int defaultValue)