From 3edd745d208e554169338eda68f2b66069cc6fd4 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Mon, 17 Nov 2014 18:33:59 -0500 Subject: [PATCH] clean up some font handling --- core/src/processing/opengl/PGL.java | 25 ++++++++------------- core/src/processing/opengl/PJOGL.java | 31 +++++++++++---------------- 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index d897f6947..d05e4652a 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -23,10 +23,6 @@ package processing.opengl; -import processing.core.PApplet; -import processing.core.PGraphics; - -import java.awt.Font; import java.io.IOException; import java.net.URL; import java.nio.Buffer; @@ -37,6 +33,10 @@ import java.nio.IntBuffer; import java.nio.ShortBuffer; import java.util.Arrays; +import processing.core.PApplet; +import processing.core.PGraphics; + + /** * Processing-OpenGL abstraction layer. Needs to be implemented by subclasses * using specific OpenGL-Java bindings. @@ -2105,25 +2105,18 @@ public abstract class PGL { // TODO: the next three functions shouldn't be here... + // Uses 'Object' so that the API can be used w/ Android Typeface objects - protected int getFontAscent(Font font) { - return 0; - } + abstract protected int getFontAscent(Object font); - protected int getFontDescent(Font font) { - return 0; - } + abstract protected int getFontDescent(Object font); - protected int getTextWidth(Font font, char buffer[], int start, int stop) { - return 0; - } + abstract protected int getTextWidth(Object font, char[] buffer, int start, int stop); - protected Font getDerivedFont(Font font, float size) { - return null; - } + abstract protected Object getDerivedFont(Object font, float size); /////////////////////////////////////////////////////////// diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index 988b47846..86a8f536d 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -4,7 +4,6 @@ import java.awt.Canvas; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Shape; -import java.awt.Toolkit; import java.awt.font.FontRenderContext; import java.awt.font.GlyphVector; import java.awt.geom.PathIterator; @@ -1194,32 +1193,29 @@ public class PJOGL extends PGL { @Override - protected int getFontAscent(Font font) { - @SuppressWarnings("deprecation") - FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(font); - return metrics.getAscent(); + protected int getFontAscent(Object font) { + return pg.getFontMetrics((Font) font).getAscent(); } + @Override protected int getFontDescent(Object font) { - FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics((Font)font); -// FontMetrics metrics = pg.parent.getFontMetrics((Font)font); - return metrics.getDescent(); + return pg.getFontMetrics((Font) font).getDescent(); } - protected int getTextWidth(Object font, char buffer[], int start, int stop) { + @Override + protected int getTextWidth(Object font, char[] buffer, int start, int stop) { // maybe should use one of the newer/fancier functions for this? int length = stop - start; -// FontMetrics metrics = pg.parent.getFontMetrics((Font)font); - FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics((Font)font); + FontMetrics metrics = pg.getFontMetrics((Font) font); return metrics.charsWidth(buffer, start, length); } + @Override protected Object getDerivedFont(Object font, float size) { - return ((Font)font).deriveFont(size); - + return ((Font) font).deriveFont(size); } @@ -1388,18 +1384,17 @@ public class PJOGL extends PGL { @Override protected FontOutline createFontOutline(char ch, Object font) { - return new FontOutline(ch, font); + return new FontOutline(ch, (Font) font); } protected class FontOutline implements PGL.FontOutline { PathIterator iter; - public FontOutline(char ch, Object font) { + public FontOutline(char ch, Font font) { char textArray[] = new char[] { ch }; -// Graphics2D graphics = (Graphics2D) pg.parent.frame.getGraphics(); - FontRenderContext frc = pg.getFontRenderContext((Font)font); - GlyphVector gv = ((Font)font).createGlyphVector(frc, textArray); + FontRenderContext frc = pg.getFontRenderContext(font); + GlyphVector gv = font.createGlyphVector(frc, textArray); Shape shp = gv.getOutline(); iter = shp.getPathIterator(null); }