Class PDType0Font

    • Constructor Summary

      Constructors 
      Constructor Description
      PDType0Font​(COSDictionary fontDictionary)
      Constructor for reading a Type0 font from a PDF file.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addToSubset​(int codePoint)
      Adds the given Unicode point to the subset.
      int codeToCID​(int code)
      Returns the CID for the given character code.
      int codeToGID​(int code)
      Returns the GID for the given character code.
      protected byte[] encode​(int unicode)
      Encodes the given Unicode code point for use in a PDF content stream.
      float getAverageFontWidth()
      This will get the average font width for all characters.
      java.lang.String getBaseFont()
      Returns the PostScript name of the font.
      org.apache.fontbox.util.BoundingBox getBoundingBox()
      Returns the font's bounding box.
      org.apache.fontbox.cmap.CMap getCMap()
      Returns the font's CMap.
      org.apache.fontbox.cmap.CMap getCMapUCS2()
      Returns the font's UCS2 CMap, only present this font uses a predefined CMap.
      PDCIDFont getDescendantFont()
      Returns the descendant font.
      Vector getDisplacement​(int code)
      Returns the displacement vector (w0, w1) in text space, for the given character.
      PDFontDescriptor getFontDescriptor()
      Returns the font descriptor, may be null.
      Matrix getFontMatrix()
      Returns the font matrix, which represents the transformation from glyph space to text space.
      float getHeight​(int code)
      Returns the height of the given character, in glyph space.
      java.lang.String getName()
      Returns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".
      java.awt.geom.GeneralPath getPath​(int code)
      Returns the glyph path for the given character code.
      Vector getPositionVector​(int code)
      Returns the position vector (v), in text space, for the given character.
      protected float getStandard14Width​(int code)
      Returns the glyph width from the AFM if this is a Standard 14 font.
      float getWidth​(int code)
      Returns the advance width of the given character, in glyph space.
      float getWidthFromFont​(int code)
      Returns the width of a glyph in the embedded font file.
      boolean hasExplicitWidth​(int code)
      Returns true if the Font dictionary specifies an explicit width for the given glyph.
      boolean hasGlyph​(int code)
      Returns true if this font contains a glyph for the given character code.
      boolean isDamaged()
      Returns true if the embedded font file is damaged.
      boolean isEmbedded()
      Returns true if the font file is embedded in the PDF.
      boolean isStandard14()
      Returns true if this font is one of the "Standard 14" fonts and receives special handling.
      boolean isVertical()
      Returns true if the font uses vertical writing mode.
      static PDType0Font load​(PDDocument doc, java.io.File file)
      Loads a TTF to be embedded into a document as a Type 0 font.
      static PDType0Font load​(PDDocument doc, java.io.InputStream input)
      Loads a TTF to be embedded into a document as a Type 0 font.
      static PDType0Font load​(PDDocument doc, java.io.InputStream input, boolean embedSubset)
      Loads a TTF to be embedded into a document as a Type 0 font.
      static PDType0Font load​(PDDocument doc, org.apache.fontbox.ttf.TrueTypeFont ttf, boolean embedSubset)
      Loads a TTF to be embedded into a document as a Type 0 font.
      static PDType0Font loadVertical​(PDDocument doc, java.io.File file)
      Loads a TTF to be embedded into a document as a vertical Type 0 font.
      static PDType0Font loadVertical​(PDDocument doc, java.io.InputStream input)
      Loads a TTF to be embedded into a document as a vertical Type 0 font.
      static PDType0Font loadVertical​(PDDocument doc, java.io.InputStream input, boolean embedSubset)
      Loads a TTF to be embedded into a document as a vertical Type 0 font.
      static PDType0Font loadVertical​(PDDocument doc, org.apache.fontbox.ttf.TrueTypeFont ttf, boolean embedSubset)
      Loads a TTF to be embedded into a document as a vertical Type 0 font.
      int readCode​(java.io.InputStream in)
      Reads a character code from a content stream string.
      void subset()
      Replaces this font with a subset containing only the given Unicode characters.
      java.lang.String toString()  
      java.lang.String toUnicode​(int code)
      Returns the Unicode character sequence which corresponds to the given character code.
      boolean willBeSubset()
      Returns true if this font will be subset when embedded.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • PDType0Font

        public PDType0Font​(COSDictionary fontDictionary)
                    throws java.io.IOException
        Constructor for reading a Type0 font from a PDF file.
        Parameters:
        fontDictionary - The font dictionary according to the PDF specification.
        Throws:
        java.io.IOException - if the descendant font is missing.
    • Method Detail

      • load

        public static PDType0Font load​(PDDocument doc,
                                       java.io.File file)
                                throws java.io.IOException
        Loads a TTF to be embedded into a document as a Type 0 font.
        Parameters:
        doc - The PDF document that will hold the embedded font.
        file - A TrueType font.
        Returns:
        A Type0 font with a CIDFontType2 descendant.
        Throws:
        java.io.IOException - If there is an error reading the font file.
      • load

        public static PDType0Font load​(PDDocument doc,
                                       java.io.InputStream input)
                                throws java.io.IOException
        Loads a TTF to be embedded into a document as a Type 0 font.
        Parameters:
        doc - The PDF document that will hold the embedded font.
        input - A TrueType font.
        Returns:
        A Type0 font with a CIDFontType2 descendant.
        Throws:
        java.io.IOException - If there is an error reading the font stream.
      • load

        public static PDType0Font load​(PDDocument doc,
                                       java.io.InputStream input,
                                       boolean embedSubset)
                                throws java.io.IOException
        Loads a TTF to be embedded into a document as a Type 0 font.
        Parameters:
        doc - The PDF document that will hold the embedded font.
        input - A TrueType font.
        embedSubset - True if the font will be subset before embedding
        Returns:
        A Type0 font with a CIDFontType2 descendant.
        Throws:
        java.io.IOException - If there is an error reading the font stream.
      • load

        public static PDType0Font load​(PDDocument doc,
                                       org.apache.fontbox.ttf.TrueTypeFont ttf,
                                       boolean embedSubset)
                                throws java.io.IOException
        Loads a TTF to be embedded into a document as a Type 0 font.
        Parameters:
        doc - The PDF document that will hold the embedded font.
        ttf - A TrueType font.
        embedSubset - True if the font will be subset before embedding
        Returns:
        A Type0 font with a CIDFontType2 descendant.
        Throws:
        java.io.IOException - If there is an error reading the font stream.
      • loadVertical

        public static PDType0Font loadVertical​(PDDocument doc,
                                               java.io.File file)
                                        throws java.io.IOException
        Loads a TTF to be embedded into a document as a vertical Type 0 font.
        Parameters:
        doc - The PDF document that will hold the embedded font.
        file - A TrueType font.
        Returns:
        A Type0 font with a CIDFontType2 descendant.
        Throws:
        java.io.IOException - If there is an error reading the font file.
      • loadVertical

        public static PDType0Font loadVertical​(PDDocument doc,
                                               java.io.InputStream input)
                                        throws java.io.IOException
        Loads a TTF to be embedded into a document as a vertical Type 0 font.
        Parameters:
        doc - The PDF document that will hold the embedded font.
        input - A TrueType font.
        Returns:
        A Type0 font with a CIDFontType2 descendant.
        Throws:
        java.io.IOException - If there is an error reading the font stream.
      • loadVertical

        public static PDType0Font loadVertical​(PDDocument doc,
                                               java.io.InputStream input,
                                               boolean embedSubset)
                                        throws java.io.IOException
        Loads a TTF to be embedded into a document as a vertical Type 0 font.
        Parameters:
        doc - The PDF document that will hold the embedded font.
        input - A TrueType font.
        embedSubset - True if the font will be subset before embedding
        Returns:
        A Type0 font with a CIDFontType2 descendant.
        Throws:
        java.io.IOException - If there is an error reading the font stream.
      • loadVertical

        public static PDType0Font loadVertical​(PDDocument doc,
                                               org.apache.fontbox.ttf.TrueTypeFont ttf,
                                               boolean embedSubset)
                                        throws java.io.IOException
        Loads a TTF to be embedded into a document as a vertical Type 0 font.
        Parameters:
        doc - The PDF document that will hold the embedded font.
        ttf - A TrueType font.
        embedSubset - True if the font will be subset before embedding
        Returns:
        A Type0 font with a CIDFontType2 descendant.
        Throws:
        java.io.IOException - If there is an error reading the font stream.
      • addToSubset

        public void addToSubset​(int codePoint)
        Description copied from class: PDFont
        Adds the given Unicode point to the subset.
        Specified by:
        addToSubset in class PDFont
        Parameters:
        codePoint - Unicode code point
      • subset

        public void subset()
                    throws java.io.IOException
        Description copied from class: PDFont
        Replaces this font with a subset containing only the given Unicode characters.
        Specified by:
        subset in class PDFont
        Throws:
        java.io.IOException - if the subset could not be written
      • willBeSubset

        public boolean willBeSubset()
        Description copied from class: PDFont
        Returns true if this font will be subset when embedded.
        Specified by:
        willBeSubset in class PDFont
      • getBaseFont

        public java.lang.String getBaseFont()
        Returns the PostScript name of the font.
      • getDescendantFont

        public PDCIDFont getDescendantFont()
        Returns the descendant font.
      • getCMap

        public org.apache.fontbox.cmap.CMap getCMap()
        Returns the font's CMap.
      • getCMapUCS2

        public org.apache.fontbox.cmap.CMap getCMapUCS2()
        Returns the font's UCS2 CMap, only present this font uses a predefined CMap.
      • isVertical

        public boolean isVertical()
        Description copied from class: PDFont
        Returns true if the font uses vertical writing mode.
        Specified by:
        isVertical in class PDFont
      • getHeight

        public float getHeight​(int code)
                        throws java.io.IOException
        Description copied from interface: PDFontLike
        Returns the height of the given character, in glyph space. This can be expensive to calculate. Results are only approximate.

        Warning: This method is deprecated in PDFBox 2.0 because there is no meaningful value which it can return. The PDFontLike.getWidth(int) method returns the advance width of a glyph, but there is no corresponding advance height. The logical height of a character is the same for every character in a font, so if you want that, retrieve the font bbox's height. Otherwise if you want the visual bounds of the glyph then call getPath(..) on the appropriate PDFont subclass to retrieve the glyph outline as a GeneralPath.

        Specified by:
        getHeight in interface PDFontLike
        Specified by:
        getHeight in class PDFont
        Parameters:
        code - character code
        Throws:
        java.io.IOException
      • encode

        protected byte[] encode​(int unicode)
                         throws java.io.IOException
        Description copied from class: PDFont
        Encodes the given Unicode code point for use in a PDF content stream. Content streams use a multi-byte encoding with 1 to 4 bytes.

        This method is called when embedding text in PDFs and when filling in fields.

        Specified by:
        encode in class PDFont
        Parameters:
        unicode - Unicode code point.
        Returns:
        Array of 1 to 4 PDF content stream bytes.
        Throws:
        java.io.IOException - If the text could not be encoded.
      • hasExplicitWidth

        public boolean hasExplicitWidth​(int code)
                                 throws java.io.IOException
        Description copied from interface: PDFontLike
        Returns true if the Font dictionary specifies an explicit width for the given glyph. This includes Width, W but not default widths entries.
        Specified by:
        hasExplicitWidth in interface PDFontLike
        Parameters:
        code - character code
        Throws:
        java.io.IOException - if the font could not be read
      • getAverageFontWidth

        public float getAverageFontWidth()
        Description copied from class: PDFont
        This will get the average font width for all characters.
        Specified by:
        getAverageFontWidth in interface PDFontLike
        Overrides:
        getAverageFontWidth in class PDFont
        Returns:
        The width is in 1000 unit of text space, ie 333 or 777
      • getPositionVector

        public Vector getPositionVector​(int code)
        Description copied from interface: PDFontLike
        Returns the position vector (v), in text space, for the given character. This represents the position of vertical origin relative to horizontal origin, for horizontal writing it will always be (0, 0). For vertical writing both x and y are set.
        Specified by:
        getPositionVector in interface PDFontLike
        Overrides:
        getPositionVector in class PDFont
        Parameters:
        code - character code
        Returns:
        position vector
      • getDisplacement

        public Vector getDisplacement​(int code)
                               throws java.io.IOException
        Description copied from class: PDFont
        Returns the displacement vector (w0, w1) in text space, for the given character. For horizontal text only the x component is used, for vertical text only the y component.
        Overrides:
        getDisplacement in class PDFont
        Parameters:
        code - character code
        Returns:
        displacement vector
        Throws:
        java.io.IOException
      • getWidth

        public float getWidth​(int code)
                       throws java.io.IOException
        Description copied from interface: PDFontLike
        Returns the advance width of the given character, in glyph space.

        If you want the visual bounds of the glyph then call getPath(..) on the appropriate PDFont subclass to retrieve the glyph outline as a GeneralPath instead.

        Specified by:
        getWidth in interface PDFontLike
        Overrides:
        getWidth in class PDFont
        Parameters:
        code - character code
        Throws:
        java.io.IOException
      • getStandard14Width

        protected float getStandard14Width​(int code)
        Description copied from class: PDFont
        Returns the glyph width from the AFM if this is a Standard 14 font.
        Specified by:
        getStandard14Width in class PDFont
        Parameters:
        code - character code
        Returns:
        width in 1/1000 text space
      • getWidthFromFont

        public float getWidthFromFont​(int code)
                               throws java.io.IOException
        Description copied from interface: PDFontLike
        Returns the width of a glyph in the embedded font file.
        Specified by:
        getWidthFromFont in interface PDFontLike
        Specified by:
        getWidthFromFont in class PDFont
        Parameters:
        code - character code
        Returns:
        width in glyph space
        Throws:
        java.io.IOException - if the font could not be read
      • isEmbedded

        public boolean isEmbedded()
        Description copied from interface: PDFontLike
        Returns true if the font file is embedded in the PDF.
        Specified by:
        isEmbedded in interface PDFontLike
        Specified by:
        isEmbedded in class PDFont
      • toUnicode

        public java.lang.String toUnicode​(int code)
                                   throws java.io.IOException
        Description copied from class: PDFont
        Returns the Unicode character sequence which corresponds to the given character code.
        Overrides:
        toUnicode in class PDFont
        Parameters:
        code - character code
        Returns:
        Unicode character(s)
        Throws:
        java.io.IOException
      • getName

        public java.lang.String getName()
        Description copied from interface: PDFontLike
        Returns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".
        Specified by:
        getName in interface PDFontLike
        Specified by:
        getName in class PDFont
      • getBoundingBox

        public org.apache.fontbox.util.BoundingBox getBoundingBox()
                                                           throws java.io.IOException
        Description copied from interface: PDFontLike
        Returns the font's bounding box.
        Specified by:
        getBoundingBox in interface PDFontLike
        Specified by:
        getBoundingBox in class PDFont
        Throws:
        java.io.IOException
      • readCode

        public int readCode​(java.io.InputStream in)
                     throws java.io.IOException
        Description copied from class: PDFont
        Reads a character code from a content stream string. Codes may be up to 4 bytes long.
        Specified by:
        readCode in class PDFont
        Parameters:
        in - string stream
        Returns:
        character code
        Throws:
        java.io.IOException - if the CMap or stream cannot be read
      • codeToCID

        public int codeToCID​(int code)
        Returns the CID for the given character code. If not found then CID 0 is returned.
        Parameters:
        code - character code
        Returns:
        CID
      • codeToGID

        public int codeToGID​(int code)
                      throws java.io.IOException
        Returns the GID for the given character code.
        Parameters:
        code - character code
        Returns:
        GID
        Throws:
        java.io.IOException
      • isStandard14

        public boolean isStandard14()
        Description copied from class: PDFont
        Returns true if this font is one of the "Standard 14" fonts and receives special handling.
        Overrides:
        isStandard14 in class PDFont
      • isDamaged

        public boolean isDamaged()
        Description copied from interface: PDFontLike
        Returns true if the embedded font file is damaged.
        Specified by:
        isDamaged in interface PDFontLike
        Specified by:
        isDamaged in class PDFont
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class PDFont
      • getPath

        public java.awt.geom.GeneralPath getPath​(int code)
                                          throws java.io.IOException
        Description copied from interface: PDVectorFont
        Returns the glyph path for the given character code.
        Specified by:
        getPath in interface PDVectorFont
        Parameters:
        code - character code in a PDF. Not to be confused with unicode.
        Throws:
        java.io.IOException - if the font could not be read
      • hasGlyph

        public boolean hasGlyph​(int code)
                         throws java.io.IOException
        Description copied from interface: PDVectorFont
        Returns true if this font contains a glyph for the given character code.
        Specified by:
        hasGlyph in interface PDVectorFont
        Parameters:
        code - character code in a PDF. Not to be confused with unicode.
        Throws:
        java.io.IOException