PFont

  • java.lang.Object
    • processing.core.PFont
  • All Implemented Interfaces:
    PConstants


    public class PFont
    extends java.lang.Object
    implements PConstants

    Grayscale bitmap font class used by Processing.

    Awful (and by that, I mean awesome) ASCII (non-)art for how this works:

       |
       |                   height is the full used height of the image
       |
       |   ..XX..       }
       |   ..XX..       }
       |   ......       }
       |   XXXX..       }  topExtent (top y is baseline - topExtent)
       |   ..XX..       }
       |   ..XX..       }  dotted areas are where the image data
       |   ..XX..       }  is actually located for the character
       +---XXXXXX----   }  (it extends to the right and down
       |                   for power of two texture sizes)
       ^^^^ leftExtent (amount to move over before drawing the image
    
       ^^^^^^^^^^^^^^ setWidth (width displaced by char)
     
    See Also:
    PApplet.loadFont(String), PApplet.createFont(String, float, boolean, char[]), PGraphics.textFont(PFont)
    • Field Detail

      • CHARSET

        public static char[] CHARSET

        The default Processing character set.

        This is the union of the Mac Roman and Windows ANSI (CP1250) character sets. ISO 8859-1 Latin 1 is Unicode characters 0x80 -> 0xFF, and would seem a good standard, but in practice, most P5 users would rather have characters that they expect from their platform's fonts.

        This is more of an interim solution until a much better font solution can be determined. (i.e. create fonts on the fly from some sort of vector format).

        Not that I expect that to happen.

    • Constructor Detail

      • PFont

        public PFont()
      • PFont

        public PFont(java.awt.Font font,
                     boolean smooth)

        ( begin auto-generated from PFont.xml ) PFont is the font class for Processing. To create a font to use with Processing, select "Create Font..." from the Tools menu. This will create a font in the format Processing requires and also adds it to the current sketch's data directory. Processing displays fonts using the .vlw font format, which uses images for each letter, rather than defining them through vector data. The loadFont() function constructs a new font and textFont() makes a font active. The list() method creates a list of the fonts installed on the computer, which is useful information to use with the createFont() function for dynamically converting fonts into a format to use with Processing. ( end auto-generated )

        Parameters:
        font - font the font object to create from
        smooth - smooth true to enable smoothing/anti-aliasing
      • PFont

        public PFont(java.awt.Font font,
                     boolean smooth,
                     char[] charset)

        Create a new image-based font on the fly. If charset is set to null, the characters will only be created as bitmaps when they're drawn.

        Parameters:
        charset - array of all unicode chars that should be included
      • PFont

        public PFont(java.awt.Font font,
                     boolean smooth,
                     char[] charset,
                     boolean stream,
                     int density)

        Adds an additional parameter that indicates the font came from a file, not a built-in OS font.

      • PFont

        public PFont(java.io.InputStream input)
              throws java.io.IOException
        Parameters:
        input - InputStream
        Throws:
        java.io.IOException
    • Method Detail

      • save

        public void save(java.io.OutputStream output)
                  throws java.io.IOException

        Write this PFont to an OutputStream.

        This is used by the Create Font tool, or whatever anyone else dreams up for messing with fonts themselves.

        It is assumed that the calling class will handle closing the stream when finished.

        Throws:
        java.io.IOException
      • getName

        public java.lang.String getName()
      • getPostScriptName

        public java.lang.String getPostScriptName()
      • setNative

        public void setNative(java.lang.Object font)

        Set the native complement of this font. Might be set internally via the findFont() function, or externally by a deriveFont() call if the font is resized by PGraphicsJava2D.

      • getFont

        @Deprecated
        public java.awt.Font getFont()

        Deprecated. 

        Use the getNative() method instead, which allows library interfaces to be written in a cross-platform fashion for desktop, Android, and others.

      • getNative

        public java.lang.Object getNative()

        Return the native java.awt.Font associated with this PFont (if any).

      • getSize

        public int getSize()

        Return size of this font.

      • getDefaultSize

        public int getDefaultSize()

        Returns the size that will be used when textFont(font) is called. When drawing with 2x pixel density, bitmap fonts in OpenGL need to be created (behind the scenes) at double the requested size. This ensures that they're shown at half on displays (so folks don't have to change their sketch code).

      • isSmooth

        public boolean isSmooth()
      • isStream

        public boolean isStream()
      • setSubsetting

        public void setSubsetting()
      • findNative

        public java.lang.Object findNative()

        Attempt to find the native version of this font. (Public so that it can be used by OpenGL or other renderers.)

      • kern

        public float kern(char a,
                          char b)

        Currently un-implemented for .vlw fonts, but honored for layout in case subclasses use it.

      • ascent

        public float ascent()

        Returns the ascent of this font from the baseline. The value is based on a font of size 1.

      • descent

        public float descent()

        Returns how far this font descends from the baseline. The value is based on a font size of 1.

      • width

        public float width(char c)

        Width of this character for a font of size 1.

      • getGlyphCount

        public int getGlyphCount()
      • getShape

        public PShape getShape(char ch)
      • getShape

        public PShape getShape(char ch,
                               float detail)
      • list

        public static java.lang.String[] list()

        ( begin auto-generated from PFont_list.xml ) Gets a list of the fonts installed on the system. The data is returned as a String array. This list provides the names of each font for input into createFont(), which allows Processing to dynamically format fonts. This function is meant as a tool for programming local applications and is not recommended for use in applets. ( end auto-generated )

        In brief:
        Gets a list of the fonts installed on the system
        Usage:
        application
      • loadFonts

        public static void loadFonts()

        Make an internal list of all installed fonts. This can take a while with a lot of fonts installed, but running it on a separate thread may not help much. As of the commit that's adding this note, loadFonts() will only be called by PFont.list() and when loading a font by name, both of which are occasions when we'd need to block until this was finished anyway. It's also possible that running getAllFonts() on a non-EDT thread could cause graphics system issues. Further, the first fonts are usually loaded at the beginning of a sketch, meaning that sketch startup time will still be affected, even with threading in place. Where we're getting killed on font performance is due to this bug: https://bugs.openjdk.java.net/browse/JDK-8179209

      • findFont

        public static java.awt.Font findFont(java.lang.String name)

        Starting with Java 1.5, Apple broke the ability to specify most fonts. This bug was filed years ago as #4769141 at bugreporter.apple.com. More: Bug 407.
        This function displays a warning when the font is not found and Java's system font is used. See: issue #5481