- java.lang.Object
-
- processing.core.PFont
-
- All Implemented Interfaces:
PConstants
public class PFont extends 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)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classPFont.GlyphA single character, and its visage.
-
Field Summary
Fields Modifier and Type Field Description protected intascentThe ascent of the font.protected int[]asciiA more efficient array lookup for straight ASCII characters.static char[]CHARSETThe default Processing character set.protected intdensityDefault density set to 1 for backwards compatibility with loadFont().protected intdescentThe descent of the font.protected FontfontNative Java version of the font.protected static HashMap<String,Font>fontDifferentprotected static Font[]fontsArray of the native system fonts.protected booleanfontSearchedTrue if already tried to find the native AWT version of this font.protected intglyphCountNumber of character glyphs in this font.protected PFont.Glyph[]glyphsActual glyph data.protected booleanlazyTrue if this font is set to load dynamically.protected Graphics2DlazyGraphicsprotected BufferedImagelazyImageprotected FontMetricslazyMetricsprotected int[]lazySamplesprotected StringnameName of the font as seen by Java when it was created.protected StringpsnamePostscript name of the font that this bitmap was created from.protected intsizeThe original size of the font when it was first createdprotected booleansmoothtrue if smoothing was enabled for this font, used for native implprotected booleanstreamTrue if this font was loaded from an InputStream, rather than by name from the OS.protected booleansubsettingTrue if this font should return 'null' for getFont(), so that the native font will be used to create a subset, but the native version of the font will not be used.protected static StringsystemFontNameThe name of the font that Java uses when a font isn't found.-
Fields inherited from interface processing.core.PConstants
ADD, ALPHA, ALT, AMBIENT, ARC, ARGB, ARROW, BACKSPACE, BASELINE, BEVEL, BEZIER_VERTEX, BLEND, BLUR, BOTTOM, BOX, BREAK, BURN, CENTER, CHATTER, CHORD, CLAMP, CLOSE, CODED, COMPLAINT, CONTROL, CORNER, CORNERS, CROSS, CURVE_VERTEX, CUSTOM, DARKEST, DEG_TO_RAD, DELETE, DIAMETER, DIFFERENCE, DILATE, DIRECTIONAL, DISABLE_ASYNC_SAVEFRAME, DISABLE_BUFFER_READING, DISABLE_DEPTH_MASK, DISABLE_DEPTH_SORT, DISABLE_DEPTH_TEST, DISABLE_KEY_REPEAT, DISABLE_NATIVE_FONTS, DISABLE_OPENGL_ERRORS, DISABLE_OPTIMIZED_STROKE, DISABLE_STROKE_PERSPECTIVE, DISABLE_STROKE_PURE, DISABLE_TEXTURE_MIPMAPS, DODGE, DOWN, DXF, ELLIPSE, ENABLE_ASYNC_SAVEFRAME, ENABLE_BUFFER_READING, ENABLE_DEPTH_MASK, ENABLE_DEPTH_SORT, ENABLE_DEPTH_TEST, ENABLE_KEY_REPEAT, ENABLE_NATIVE_FONTS, ENABLE_OPENGL_ERRORS, ENABLE_OPTIMIZED_STROKE, ENABLE_STROKE_PERSPECTIVE, ENABLE_STROKE_PURE, ENABLE_TEXTURE_MIPMAPS, ENTER, EPSILON, ERODE, ESC, EXCLUSION, FX2D, GIF, GRAY, GROUP, HALF_PI, HAND, HARD_LIGHT, HINT_COUNT, HSB, IMAGE, INVERT, JAVA2D, JPEG, LANDSCAPE, LEFT, LIGHTEST, LINE, LINE_LOOP, LINE_STRIP, LINES, LINUX, MACOSX, MAX_FLOAT, MAX_INT, MIN_FLOAT, MIN_INT, MITER, MODEL, MODELVIEW, MOVE, MULTIPLY, NORMAL, OPAQUE, OPEN, OPENGL, ORTHOGRAPHIC, OTHER, OVERLAY, P2D, P3D, PATH, PDF, PERSPECTIVE, PI, PIE, platformNames, POINT, POINTS, POLYGON, PORTRAIT, POSTERIZE, PROBLEM, PROJECT, PROJECTION, QUAD, QUAD_BEZIER_VERTEX, QUAD_STRIP, QUADRATIC_VERTEX, QUADS, QUARTER_PI, RAD_TO_DEG, RADIUS, RECT, REPEAT, REPLACE, RETURN, RGB, RIGHT, ROUND, SCREEN, SHAPE, SHIFT, SOFT_LIGHT, SPAN, SPHERE, SPOT, SQUARE, SUBTRACT, SVG, TAB, TARGA, TAU, TEXT, THIRD_PI, THRESHOLD, TIFF, TOP, TRIANGLE, TRIANGLE_FAN, TRIANGLE_STRIP, TRIANGLES, TWO_PI, UP, VERTEX, WAIT, WHITESPACE, WINDOWS, X, Y, Z
-
-
Constructor Summary
Constructors Constructor Description PFont()PFont(Font font, boolean smooth)( begin auto-generated from PFont.xml ) PFont is the font class for Processing.PFont(Font font, boolean smooth, char[] charset)Create a new image-based font on the fly.PFont(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(InputStream input)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidaddGlyph(char c)Create a new glyph, and add the character to the current font.floatascent()Returns the ascent of this font from the baseline.floatdescent()Returns how far this font descends from the baseline.static FontfindFont(String name)Starting with Java 1.5, Apple broke the ability to specify most fonts.ObjectfindNative()Attempt to find the native version of this font.intgetDefaultSize()Returns the size that will be used when textFont(font) is called.FontgetFont()Deprecated.PFont.GlyphgetGlyph(char c)PFont.GlyphgetGlyph(int i)intgetGlyphCount()StringgetName()ObjectgetNative()Return the native java.awt.Font associated with this PFont (if any).StringgetPostScriptName()PShapegetShape(char ch)PShapegetShape(char ch, float detail)intgetSize()Return size of this font.protected intindex(char c)Get index for the character.protected intindexActual(char c)protected intindexHunt(int c, int start, int stop)booleanisSmooth()booleanisStream()floatkern(char a, char b)Currently un-implemented for .vlw fonts, but honored for layout in case subclasses use it.static String[]list()( begin auto-generated from PFont_list.xml ) Gets a list of the fonts installed on the system.static voidloadFonts()Make an internal list of all installed fonts.voidsave(OutputStream output)Write this PFont to an OutputStream.voidsetNative(Object font)Set the native complement of this font.voidsetSubsetting()floatwidth(char c)Width of this character for a font of size 1.
-
-
-
Field Detail
-
glyphCount
protected int glyphCount
Number of character glyphs in this font.
-
glyphs
protected PFont.Glyph[] glyphs
Actual glyph data. The length of this array won't necessarily be the same size as glyphCount, in cases where lazy font loading is in use.
-
name
protected String name
Name of the font as seen by Java when it was created. If the font is available, the native version will be used.
-
psname
protected String psname
Postscript name of the font that this bitmap was created from.
-
size
protected int size
The original size of the font when it was first created
-
density
protected int density
Default density set to 1 for backwards compatibility with loadFont().
-
smooth
protected boolean smooth
true if smoothing was enabled for this font, used for native impl
-
ascent
protected int ascent
The ascent of the font. If the 'd' character is present in this PFont, this value is replaced with its pixel height, because the values returned by FontMetrics.getAscent() seem to be terrible.
-
descent
protected int descent
The descent of the font. If the 'p' character is present in this PFont, this value is replaced with its lowest pixel height, because the values returned by FontMetrics.getDescent() are gross.
-
ascii
protected int[] ascii
A more efficient array lookup for straight ASCII characters. For Unicode characters, a QuickSort-style search is used.
-
lazy
protected boolean lazy
True if this font is set to load dynamically. This is the default when createFont() method is called without a character set. Bitmap versions of characters are only created when prompted by an index() call.
-
font
protected Font font
Native Java version of the font. If possible, this allows the PGraphics subclass to just use Java's font rendering stuff in situations where that's faster.
-
stream
protected boolean stream
True if this font was loaded from an InputStream, rather than by name from the OS. It's best to use the native version of a font loaded from a TTF file, since that will ensure that the font is available when the sketch is exported.
-
subsetting
protected boolean subsetting
True if this font should return 'null' for getFont(), so that the native font will be used to create a subset, but the native version of the font will not be used.
-
fontSearched
protected boolean fontSearched
True if already tried to find the native AWT version of this font.
-
systemFontName
protected static String systemFontName
The name of the font that Java uses when a font isn't found. SeefindFont(String)andloadFonts()for more info.
-
fonts
protected static Font[] fonts
Array of the native system fonts. Used to lookup native fonts by their PostScript name. This is a workaround for a several year old Apple Java bug that they can't be bothered to fix.
-
lazyImage
protected BufferedImage lazyImage
-
lazyGraphics
protected Graphics2D lazyGraphics
-
lazyMetrics
protected FontMetrics lazyMetrics
-
lazySamples
protected int[] lazySamples
-
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(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 fromsmooth- smooth true to enable smoothing/anti-aliasing
-
PFont
public PFont(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(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(InputStream input) throws IOException
- Parameters:
input- InputStream- Throws:
IOException
-
-
Method Detail
-
save
public void save(OutputStream output) throws 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:
IOException
-
addGlyph
protected void addGlyph(char c)
Create a new glyph, and add the character to the current font.- Parameters:
c- character to create an image for.
-
getName
public String getName()
-
getPostScriptName
public String getPostScriptName()
-
setNative
public void setNative(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 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 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 Object findNative()
Attempt to find the native version of this font. (Public so that it can be used by OpenGL or other renderers.)
-
getGlyph
public PFont.Glyph getGlyph(char c)
-
index
protected int index(char c)
Get index for the character.- Returns:
- index into arrays or -1 if not found
-
indexActual
protected int indexActual(char c)
-
indexHunt
protected int indexHunt(int c, int start, int stop)
-
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()
-
getGlyph
public PFont.Glyph getGlyph(int i)
-
getShape
public PShape getShape(char ch)
-
getShape
public PShape getShape(char ch, float detail)
-
list
public static 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 )
-
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 Font findFont(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
-
-