Class BaseFont

java.lang.Object
org.openpdf.text.pdf.BaseFont

public abstract class BaseFont extends Object
Base class for the several font types supported
  • Field Details

    • COURIER

      public static final String COURIER
      This is a possible value of a base 14 type 1 font
      See Also:
    • COURIER_BOLD

      public static final String COURIER_BOLD
      This is a possible value of a base 14 type 1 font
      See Also:
    • COURIER_OBLIQUE

      public static final String COURIER_OBLIQUE
      This is a possible value of a base 14 type 1 font
      See Also:
    • COURIER_BOLDOBLIQUE

      public static final String COURIER_BOLDOBLIQUE
      This is a possible value of a base 14 type 1 font
      See Also:
    • HELVETICA

      public static final String HELVETICA
      This is a possible value of a base 14 type 1 font
      See Also:
    • HELVETICA_BOLD

      public static final String HELVETICA_BOLD
      This is a possible value of a base 14 type 1 font
      See Also:
    • HELVETICA_OBLIQUE

      public static final String HELVETICA_OBLIQUE
      This is a possible value of a base 14 type 1 font
      See Also:
    • HELVETICA_BOLDOBLIQUE

      public static final String HELVETICA_BOLDOBLIQUE
      This is a possible value of a base 14 type 1 font
      See Also:
    • SYMBOL

      public static final String SYMBOL
      This is a possible value of a base 14 type 1 font
      See Also:
    • TIMES_ROMAN

      public static final String TIMES_ROMAN
      This is a possible value of a base 14 type 1 font
      See Also:
    • TIMES_BOLD

      public static final String TIMES_BOLD
      This is a possible value of a base 14 type 1 font
      See Also:
    • TIMES_ITALIC

      public static final String TIMES_ITALIC
      This is a possible value of a base 14 type 1 font
      See Also:
    • TIMES_BOLDITALIC

      public static final String TIMES_BOLDITALIC
      This is a possible value of a base 14 type 1 font
      See Also:
    • ZAPFDINGBATS

      public static final String ZAPFDINGBATS
      This is a possible value of a base 14 type 1 font
      See Also:
    • ASCENT

      public static final int ASCENT
      The maximum height above the baseline reached by glyphs in this font, excluding the height of glyphs for accented characters.
      See Also:
    • CAPHEIGHT

      public static final int CAPHEIGHT
      The y coordinate of the top of flat capital letters, measured from the baseline.
      See Also:
    • DESCENT

      public static final int DESCENT
      The maximum depth below the baseline reached by glyphs in this font. The value is a negative number.
      See Also:
    • ITALICANGLE

      public static final int ITALICANGLE
      The angle, expressed in degrees counterclockwise from the vertical, of the dominant vertical strokes of the font. The value is negative for fonts that slope to the right, as almost all italic fonts do.
      See Also:
    • BBOXLLX

      public static final int BBOXLLX
      The lower left x glyph coordinate.
      See Also:
    • BBOXLLY

      public static final int BBOXLLY
      The lower left y glyph coordinate.
      See Also:
    • BBOXURX

      public static final int BBOXURX
      The upper right x glyph coordinate.
      See Also:
    • BBOXURY

      public static final int BBOXURY
      The upper right y glyph coordinate.
      See Also:
    • AWT_ASCENT

      public static final int AWT_ASCENT
      java.awt.Font property
      See Also:
    • AWT_DESCENT

      public static final int AWT_DESCENT
      java.awt.Font property
      See Also:
    • AWT_LEADING

      public static final int AWT_LEADING
      java.awt.Font property
      See Also:
    • AWT_MAXADVANCE

      public static final int AWT_MAXADVANCE
      java.awt.Font property
      See Also:
    • UNDERLINE_POSITION

      public static final int UNDERLINE_POSITION
      The underline position. Usually a negative value.
      See Also:
    • UNDERLINE_THICKNESS

      public static final int UNDERLINE_THICKNESS
      The underline thickness.
      See Also:
    • FONT_TYPE_T1

      public static final int FONT_TYPE_T1
      The font is Type 1.
      See Also:
    • CP1252

      public static final String CP1252
      A possible encoding.
      See Also:
    • WINANSI

      public static final String WINANSI
      A possible encoding.
      See Also:
    • MACROMAN

      public static final String MACROMAN
      A possible encoding.
      See Also:
    • EMBEDDED

      public static final boolean EMBEDDED
      if the font has to be embedded
      See Also:
    • NOT_EMBEDDED

      public static final boolean NOT_EMBEDDED
      if the font doesn't have to be embedded
      See Also:
    • RESOURCE_PATH

      public static final String RESOURCE_PATH
      The path to the font resources.
      See Also:
    • notdef

      public static final String notdef
      a not defined character in a custom PDF encoding
      See Also:
    • BuiltinFonts14

      protected static final HashMap<String,PdfName> BuiltinFonts14
      list of the 14 built in fonts.
    • fontCache

      protected static ConcurrentHashMap<String,BaseFont> fontCache
      cache for the fonts already used.
    • widths

      protected int[] widths
      table of characters widths for this encoding
    • differences

      protected String[] differences
      encoding names
    • unicodeDifferences

      protected char[] unicodeDifferences
      same as differences but with the unicode codes
    • charBBoxes

      protected int[][] charBBoxes
    • encoding

      protected String encoding
      encoding used with this font
    • embedded

      protected boolean embedded
      true if the font is to be embedded in the PDF
    • compressionLevel

      protected int compressionLevel
      The compression level for the font stream.
      Since:
      2.1.3
    • fontSpecific

      protected boolean fontSpecific
      true if the font must use its built in encoding. In that case the encoding is only used to map a char to the position inside the font, not to the expected char name.
    • forceWidthsOutput

      protected boolean forceWidthsOutput
      Forces the output of the width array. Only matters for the 14 built-in fonts.
    • directTextToByte

      protected boolean directTextToByte
      Converts char directly to byte by casting.
    • subset

      protected boolean subset
      Indicates if all the glyphs and widths for that particular encoding should be included in the document.
    • fastWinansi

      protected boolean fastWinansi
    • specialMap

      protected IntHashtable specialMap
      Custom encodings use this map to key the Unicode character to the single byte code.
  • Constructor Details

    • BaseFont

      protected BaseFont()
      Creates new BaseFont
  • Method Details

    • createFont

      public static BaseFont createFont(String name, String encoding, boolean embedded) throws DocumentException, IOException
      Creates a new font. This font can be one of the 14 built in types, a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or collection) or a CJK font from the Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier appended to the name. These modifiers are: Bold, Italic and BoldItalic. An example would be "STSong-Light,Bold". Note that this modifiers do not work if the font is embedded. Fonts in TrueType collections are addressed by index such as "msgothic.ttc,1". This would get the second font (indexes start at 0), in this case "MS PGothic".

      The fonts are cached and if they already exist they are extracted from the cache, not parsed again.

      Besides the common encodings described by name, custom encodings can also be made. These encodings will only work for the single byte fonts Type1 and TrueType. The encoding string starts with a '#' followed by "simple" or "full". If "simple" there is a decimal for the first character position and then a list of hex values representing the Unicode codes that compose that encoding.
      The "simple" encoding is recommended for TrueType fonts as the "full" encoding risks not matching the character with the right glyph if not done with care.
      The "full" encoding is specially aimed at Type1 fonts where the glyphs have to be described by non standard names like the Tex math fonts. Each group of three elements compose a code position: the one byte code order in decimal or as 'x' (x cannot be the space), the name and the Unicode character used to access the glyph. The space must be assigned to character position 32 otherwise text justification will not work.

      Example for a "simple" encoding that includes the Unicode character space, A, B and ecyrillic:

       "# simple 32 0020 0041 0042 0454"
       

      Example for a "full" encoding for a Type1 Tex font:

       "# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
       

      This method calls:

       createFont(name, encoding, embedded, true, null, null);
       
      Parameters:
      name - the name of the font or its location on file
      encoding - the encoding to be applied to this font
      embedded - true if the font is to be embedded in the PDF
      Returns:
      returns a new font. This font may come from the cache
      Throws:
      DocumentException - the font is invalid
      IOException - the font file could not be read
    • createFont

      public static BaseFont createFont(String name, String encoding, boolean embedded, boolean cached, byte[] ttfAfm, byte[] pfb, boolean noThrow) throws DocumentException, IOException
      Creates a new font. This font can be one of the 14 built in types, a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or collection) or a CJK font from the Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier appended to the name. These modifiers are: Bold, Italic and BoldItalic. An example would be "STSong-Light,Bold". Note that this modifiers do not work if the font is embedded. Fonts in TrueType collections are addressed by index such as "msgothic.ttc,1". This would get the second font (indexes start at 0), in this case "MS PGothic".

      The fonts may or may not be cached depending on the flag cached. If the byte arrays are present the font will be read from them instead of the name. A name is still required to identify the font type.

      Besides the common encodings described by name, custom encodings can also be made. These encodings will only work for the single byte fonts Type1 and TrueType. The encoding string starts with a '#' followed by "simple" or "full". If "simple" there is a decimal for the first character position and then a list of hex values representing the Unicode codes that compose that encoding.
      The "simple" encoding is recommended for TrueType fonts as the "full" encoding risks not matching the character with the right glyph if not done with care.
      The "full" encoding is specially aimed at Type1 fonts where the glyphs have to be described by non standard names like the Tex math fonts. Each group of three elements compose a code position: the one byte code order in decimal or as 'x' (x cannot be the space), the name and the Unicode character used to access the glyph. The space must be assigned to character position 32 otherwise text justification will not work.

      Example for a "simple" encoding that includes the Unicode character space, A, B and ecyrillic:

       "# simple 32 0020 0041 0042 0454"
       

      Example for a "full" encoding for a Type1 Tex font:

       "# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
       
      Parameters:
      name - the name of the font or its location on file
      encoding - the encoding to be applied to this font
      embedded - true if the font is to be embedded in the PDF
      cached - true if the font comes from the cache or is added to the cache if new, false if the font is always created new
      ttfAfm - the true type font or the afm in a byte array
      pfb - the pfb in a byte array
      noThrow - if true will not throw an exception if the font is not recognized and will return null, if false will throw an exception if the font is not recognized. Note that even if true an exception may be thrown in some circumstances. This parameter is useful for FontFactory that may have to check many invalid font names before finding the right one
      Returns:
      returns a new font. This font may come from the cache but only if cached is true, otherwise it will always be created new
      Throws:
      DocumentException - the font is invalid
      IOException - the font file could not be read
      Since:
      2.0.3
    • normalizeEncoding

      protected static String normalizeEncoding(String enc)
      Normalize the encoding names. "winansi" is changed to "Cp1252" and "macroman" is changed to "MacRoman".
      Parameters:
      enc - the encoding to be normalized
      Returns:
      the normalized encoding
    • setResourceProvider

      public static void setResourceProvider(ResourceProvider provider)
      Installs the source of the resources (AFM, glyph list, messages): the class loaders on the JVM, the embedded files in a native executable.
      Parameters:
      provider - the provider, or null to remove it
    • getResourceStream

      public static InputStream getResourceStream(String key)
      Gets the font resources, from the ResourceProvider (see setResourceProvider(ResourceProvider)).
      Parameters:
      key - the full name of the resource
      Returns:
      the InputStream to get the resource or null if not found
    • createEncoding

      protected void createEncoding()
      Creates the widths and the differences arrays
    • getWidth

      public int getWidth(int char1)
      Gets the width of a char in normalized 1000 units.
      Parameters:
      char1 - the unicode char to get the width of
      Returns:
      the width in normalized 1000 units
    • getWidth

      public int getWidth(String text)
      Gets the width of a String in normalized 1000 units.
      Parameters:
      text - the String to get the width of
      Returns:
      the width in normalized 1000 units
    • getWidthPoint

      public float getWidthPoint(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
    • getWidthPoint

      public float getWidthPoint(int char1, float fontSize)
      Gets the width of a char in points.
      Parameters:
      char1 - the char to get the width of
      fontSize - the font size
      Returns:
      the width in points
    • getFontDescriptor

      public abstract float getFontDescriptor(int key, float fontSize)
      Gets the font parameter identified by key. Valid values for key are ASCENT, AWT_ASCENT, CAPHEIGHT, DESCENT, AWT_DESCENT, ITALICANGLE, BBOXLLX, BBOXLLY, BBOXURX and BBOXURY.
      Parameters:
      key - the parameter to be extracted
      fontSize - the font size in points
      Returns:
      the parameter in points
    • getFontType

      public int getFontType()
      Gets the font type. The font types can be: FONT_TYPE_T1, FONT_TYPE_TT, FONT_TYPE_CJK and FONT_TYPE_TTUNI.
      Returns:
      the font type
    • getPostscriptFontName

      public abstract String getPostscriptFontName()
      Gets the postscript font name.
      Returns:
      the postscript font name
    • getCharBBox

      public int[] getCharBBox(int c)
      Gets the smallest box enclosing the character contours. It will return null if the font has not the information or the character has no contours, as in the case of the space, for example. Characters with no contours may also return [0,0,0,0].
      Parameters:
      c - the character to get the contour bounding box from
      Returns:
      an array of four floats with the bounding box in the format [llx,lly,urx,ury] or null
    • getRawCharBBox

      protected abstract int[] getRawCharBBox(int c, String name)
    • createFont

      public static BaseFont createFont(String name, String encoding, boolean embedded, boolean cached, byte[] ttfAfm, byte[] pfb, boolean noThrow, boolean forceRead) throws DocumentException, IOException
      Creates a new font. In this pruned version of OpenPDF only the 14 built in types and the Type1 fonts referred to by an AFM file are supported (no TrueType, no CJK).
      Parameters:
      name - the name of the font or its location on file
      encoding - the encoding to be applied to this font
      embedded - true if the font is to be embedded in the PDF
      cached - true if the font comes from the cache or is added to the cache if new, false if the font is always created new
      ttfAfm - the afm in a byte array
      pfb - the pfb in a byte array
      noThrow - if true will not throw an exception if the font is not recognized and will return null
      forceRead - if true, the full font file will be read and kept in memory
      Returns:
      returns a new font
      Throws:
      DocumentException - the font is invalid
      IOException - the font file could not be read