Class Type3Font

  • All Implemented Interfaces:
    java.lang.Cloneable

    public class Type3Font
    extends PDFFont

    A Type3 font is an artificial font made up of shapes drawn directly to the PDF, rather than stored in a font file embedded in the PDF. These are useful when a few custom shapes have to be added to the PDF as glyphs - perhaps extracted from an existing font, or created freehand - and in particular this approach can be used with the PDFStyle.addBackupFont(org.faceless.pdf2.PDFFont) method to plug gaps in an existing font's coverage.

    Here's an example showing how to use a glyph from an existing Font. (be aware there is rarely any benefit to cloning an entire font as shown here, and that copyright issues may arise when doing so; it's purely for example):

     Type3Font font = new Type3Font("MyFont");
     String glyphs = "Hello World";
     FontRenderContext frc = new FontRenderContext(null, true, true);
     for (int i=0;i<glyphs.length();i++) {
         char c = glyphs.charAt(i);
         if (!font.isDefined(c)) {
             font.addGlyph(c, f.createGlyphVector(frc, Character.toString(c)));
         }
     }
     
    and here's how to create a completely custom glyph
     Type3Font font = new Type3Font("MyFont");
     GeneralPath p = new GeneralPath();
     p.moveTo(0, 600);
     p.lineTo(600, 300);
     p.lineTo(0, 0);
     p.closePath();
     font.add('▶', 600, p);  // U+25B6, right pointing triangle
     

    Type3 fonts may contain any number of individual glyphs, but only 255 can be referenced in a PDF from a single instance of this font. The vast majority of methods inherited from PDFFont that return font metrics values (eg getAscender() return 0 or default values as these concepts aren't applicable to Type3 fonts.

    As of 2.23.4, the full range of codepoints outside the BMP can be addressed.

    Since:
    2.21
    • Constructor Detail

      • Type3Font

        public Type3Font​(Type3Font font)
        Create a new Type3 Font that is a copy of the existing Type3 font. This constructor should be used to clone the font if it is being shared across threads.
        Parameters:
        font - the original font
      • Type3Font

        public Type3Font​(java.lang.String name)
        Create a new Type3 Font with the specified name
        Parameters:
        name - the font name
    • Method Detail

      • addGlyph

        public int addGlyph​(int c,
                            java.awt.font.GlyphVector gv)
        Add a glyph to the font
        Parameters:
        c - the Unicode Codepoint this glyph maps to
        gv - the GlyphVector - this should be created from an existing Font with a size of 1, and represent a single character
        Returns:
        the index of the new glyph - the maximum allowed value is 255
        Throws:
        java.lang.IllegalStateException - if the font already has 255 entries
      • addGlyph

        public int addGlyph​(int c,
                            int advance,
                            java.awt.Shape shape)
        Add a glyph to the font.
        Parameters:
        c - the Unicode Codepoint this glyph maps to
        advance - the advance, which should be a number between 0 (no advance) and 1000 (for a square glyph)
        shape - the glyph outline. This should be in PDF glyph units, which means that (0,0) is the left edge of the baseline, positive Y values are above the baseline, and the glyph is in a box of 1000 x 1000.
        Returns:
        the index of the new glyph - the maximum allowed value is 255
        Throws:
        java.lang.IllegalStateException - if the font already has 255 entries
      • isEmbedded

        public boolean isEmbedded()
        Type3 Fonts are always embedded
        Overrides:
        isEmbedded in class PDFFont
      • getSize

        public int getSize()
        Return the maximum entry in this font, from 0 (for empty) to 255.
        Since:
        2.23.4
      • isItalic

        public boolean isItalic()
        Description copied from class: PDFFont
        Return true if the font is italic
        Specified by:
        isItalic in class PDFFont
      • isBold

        public boolean isBold()
        Description copied from class: PDFFont
        Return true if the font is bold
        Specified by:
        isBold in class PDFFont
      • isSerif

        public boolean isSerif()
        Description copied from class: PDFFont
        Return true if the font is serif
        Specified by:
        isSerif in class PDFFont
      • isMonospace

        public boolean isMonospace()
        Description copied from class: PDFFont
        Return true if every character has the same width (like Courier), false if every character is potentially a different width (like Times-Roman)
        Specified by:
        isMonospace in class PDFFont
      • getAscender

        public float getAscender()
        Description copied from class: PDFFont
        Get the Ascender for the font (the maximum height above the baseline the font extends), as a proportion of the point size. The exact source of of this value is undefined except for OpenTypeFonts, where it comes from the "hhea.ascender" value normally, or the from "OS2.sTypoAscender" flag if the USE_TYPO_METRICS flag is set.
        Specified by:
        getAscender in class PDFFont
      • getDescender

        public float getDescender()
        Description copied from class: PDFFont
        Get the Descender for the font (the maximum height below the baseline the font extends), as a proportion of the point size. The returned value is usually negative. The exact source of this value is undefined except for OpenTypeFonts, where it comes from the "hhea.descender" value normally, or the from "OS2.sTypoDescender" flag if the USE_TYPO_METRICS flag is set.
        Specified by:
        getDescender in class PDFFont
      • getXHeight

        public float getXHeight()
        Description copied from class: PDFFont
        Get the X-Height of the font - normally the height of a lower-case 'x' character.
        Specified by:
        getXHeight in class PDFFont
      • getCapHeight

        public float getCapHeight()
        Description copied from class: PDFFont
        Get the Cap-Height of the font - normally the height of an upper-case 'O' character
        Specified by:
        getCapHeight in class PDFFont
      • getDefaultLeading

        public float getDefaultLeading()
        Description copied from class: PDFFont

        Get the default leading for this font - the preferred distance between two successive baselines of text. Values are a ratio of the font size, and are typically between 1 and 1.3

        Note that the values of the different spacing-between-lines methods have changed - in versions 1.0.4 and earlier this routine normally returned 1 and the spacing was set by the PDFStyle.setTextLineSpacing(float) method. Since 1.1, the values for these two methods are effectively reversed. See the relevant method comments in the PDFStyle class for more information.

        Specified by:
        getDefaultLeading in class PDFFont
      • getUnderlineThickness

        public float getUnderlineThickness()
        Description copied from class: PDFFont
        Get the underline thickness, as a proportion of the font size.
        Specified by:
        getUnderlineThickness in class PDFFont
      • getUnderlinePosition

        public float getUnderlinePosition()
        Description copied from class: PDFFont
        Get the underline position, as a proportion of the font size. Like the getDescender() method, the returned value is almost always negative, indicating below the baseline. The distance is from the baseline to the center of the underline.
        Specified by:
        getUnderlinePosition in class PDFFont
      • getStrikeoutThickness

        public float getStrikeoutThickness()
        Description copied from class: PDFFont
        Get the strikeout thickness, as a proportion of the font size.
        Specified by:
        getStrikeoutThickness in class PDFFont
      • getStrikeoutPosition

        public float getStrikeoutPosition()
        Description copied from class: PDFFont
        Get the strikeout position, as a proportion of the font size. The value is the distance from the baseline to the center of the strikeout, and shuold be positive.
        Specified by:
        getStrikeoutPosition in class PDFFont
      • getSuperscriptPosition

        public float getSuperscriptPosition()
        Description copied from class: PDFFont

        Get the recommended position of a super-script version of this font, as a proportion of the sub-scripted font size. Value is always positive.

        For some fonts (like CJK or barcode fonts) where there is no concept of super or subscript, this value is entirely arbitrary.

        Specified by:
        getSuperscriptPosition in class PDFFont
      • getSubscriptPosition

        public float getSubscriptPosition()
        Description copied from class: PDFFont

        Get the recommended position of a sub-script version of this font, as a proportion of the sub-scripted font size. Value is almost always zero or negative.

        For some fonts (like CJK or barcode fonts) where there is no concept of super or subscript, this value is entirely arbitrary.

        Specified by:
        getSubscriptPosition in class PDFFont
      • getSubscriptSize

        public float getSubscriptSize()
        Description copied from class: PDFFont

        Get the recommended size of a super/sub script version of this font, as a proportion of the normal font size. Typical value is around 0.6.

        For some fonts (like CJK or barcode fonts) where there is no concept of super or subscript, this value is entirely arbitrary.

        Specified by:
        getSubscriptSize in class PDFFont
      • getDefinedCodepoints

        public java.util.BitSet getDefinedCodepoints()
        Description copied from class: PDFFont
        Return read-only BitSet containing all the Unicode codepoints defined in this font
        Specified by:
        getDefinedCodepoints in class PDFFont
      • isDefined

        public boolean isDefined​(int codepoint)
        Description copied from class: PDFFont
        Return true if the specified Unicode character is defined in the font. This method is identical to PDFFont.isDefined(char) but takes an int, to cater for the new Unicode 4.0 codepoints added in Java 1.5.
        Overrides:
        isDefined in class PDFFont
        Parameters:
        codepoint - a Unicode codepoint between U+0000 and U+10FFFD
      • toString

        public java.lang.String toString()
      • putLiteral

        public void putLiteral​(java.lang.String key,
                               java.lang.String tokens)
        Put a literal token sequnce. For debugging
        Parameters:
        key - the key
        tokens - the token sequence, eg "true" or "/foo" or "[/Foo/Bar]". No refs, just direct objects.