From 2f8af18ba639989bdbc6f27b8eb547066d91f4dd Mon Sep 17 00:00:00 2001 From: codeanticode Date: Thu, 19 Sep 2013 11:35:03 -0400 Subject: [PATCH] some changes for android compatibility --- core/src/processing/opengl/PGL.java | 22 ++++++++- .../processing/opengl/PGraphicsOpenGL.java | 47 +++++++------------ core/src/processing/opengl/PJOGL.java | 31 +++++++++++- .../lwjgl/src/processing/lwjgl/PLWJGL.java | 26 +++++++++- 4 files changed, 92 insertions(+), 34 deletions(-) diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index f43d19433..6bbab3c9b 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -24,6 +24,7 @@ package processing.opengl; import processing.core.PApplet; + import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -1882,6 +1883,23 @@ public abstract class PGL { } + // TODO: the next three functions shouldn't be here... + + protected int getFontAscent(Object font) { + return 0; + } + + + protected int getFontDescent(Object font) { + return 0; + } + + + protected int getTextWidth(Object font, char buffer[], int start, int stop) { + return 0; + } + + /////////////////////////////////////////////////////////// // Tessellator interface @@ -1912,7 +1930,7 @@ public abstract class PGL { protected String tessError(int err) { return ""; - } + } /////////////////////////////////////////////////////////// @@ -2424,7 +2442,7 @@ public abstract class PGL { public abstract void linkProgram(int program); public abstract void useProgram(int program); public abstract void deleteProgram(int program); - public abstract String glGetActiveAttrib (int program, int index, IntBuffer size, IntBuffer type); + public abstract String getActiveAttrib(int program, int index, IntBuffer size, IntBuffer type); public abstract int getAttribLocation(int program, String name); public abstract void bindAttribLocation(int program, int index, String name); public abstract int getUniformLocation(int program, String name); diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 592f12b71..ba671f35d 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -23,9 +23,6 @@ package processing.opengl; import processing.core.*; - -import java.awt.Font; -import java.awt.FontMetrics; import java.net.URL; import java.nio.*; import java.util.*; @@ -3254,43 +3251,33 @@ public class PGraphicsOpenGL extends PGraphics { @Override public float textAscent() { - if (textFont == null) { - defaultFontOrDeath("textAscent"); - } - Font font = (Font) textFont.getNative(); - if (font != null) { - FontMetrics metrics = parent.getFontMetrics(font); - return metrics.getAscent(); - } - return super.textAscent(); + if (textFont == null) defaultFontOrDeath("textAscent"); + Object font = textFont.getNative(); + float ascent = 0; + if (font != null) ascent = pgl.getFontAscent(font); + if (ascent == 0) ascent = super.textAscent(); + return ascent; } @Override public float textDescent() { - if (textFont == null) { - defaultFontOrDeath("textAscent"); - } - Font font = (Font) textFont.getNative(); - if (font != null) { - FontMetrics metrics = parent.getFontMetrics(font); - return metrics.getDescent(); - } - return super.textDescent(); + if (textFont == null) defaultFontOrDeath("textAscent"); + Object font = textFont.getNative(); + float descent = 0; + if (font != null) descent = pgl.getFontDescent(font); + if (descent == 0) descent = super.textDescent(); + return descent; } @Override protected float textWidthImpl(char buffer[], int start, int stop) { - Font font = (Font) textFont.getNative(); - if (font != null) { - // maybe should use one of the newer/fancier functions for this? - int length = stop - start; - FontMetrics metrics = parent.getFontMetrics(font); - return metrics.charsWidth(buffer, start, length); - } - - return super.textWidthImpl(buffer, start, stop); + Object font = textFont.getNative(); + float twidth = 0; + if (font != null) twidth = pgl.getTextWidth(font, buffer, start, stop); + if (twidth == 0) twidth = super.textWidthImpl(buffer, start, stop); + return twidth; } diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index 01a0dabe7..59092b6a7 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -3,6 +3,7 @@ package processing.opengl; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; +import java.awt.FontMetrics; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.FloatBuffer; @@ -941,6 +942,34 @@ public class PJOGL extends PGL { } + /////////////////////////////////////////////////////////// + + // Utility functions + + + @Override + protected int getFontAscent(Object font) { + FontMetrics metrics = pg.parent.getFontMetrics((Font)font); + return metrics.getAscent(); + } + + + @Override + protected int getFontDescent(Object font) { + FontMetrics metrics = pg.parent.getFontMetrics((Font)font); + return metrics.getDescent(); + } + + + @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); + return metrics.charsWidth(buffer, start, length); + } + + /////////////////////////////////////////////////////////// // Tessellator @@ -1816,7 +1845,7 @@ public class PJOGL extends PGL { } @Override - public String glGetActiveAttrib (int program, int index, IntBuffer size, IntBuffer type) { + public String getActiveAttrib(int program, int index, IntBuffer size, IntBuffer type) { int[] tmp = {0, 0, 0}; byte[] namebuf = new byte[1024]; gl2.glGetActiveAttrib(program, index, 1024, tmp, 0, tmp, 1, tmp, 2, namebuf, 0); diff --git a/java/libraries/lwjgl/src/processing/lwjgl/PLWJGL.java b/java/libraries/lwjgl/src/processing/lwjgl/PLWJGL.java index 477a345f5..7b8c223be 100644 --- a/java/libraries/lwjgl/src/processing/lwjgl/PLWJGL.java +++ b/java/libraries/lwjgl/src/processing/lwjgl/PLWJGL.java @@ -27,6 +27,7 @@ import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.Font; +import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.Shape; import java.awt.font.FontRenderContext; @@ -332,6 +333,29 @@ public class PLWJGL extends PGL { } + @Override + protected int getFontAscent(Object font) { + FontMetrics metrics = pg.parent.getFontMetrics((Font)font); + return metrics.getAscent(); + } + + + @Override + protected int getFontDescent(Object font) { + FontMetrics metrics = pg.parent.getFontMetrics((Font)font); + return metrics.getDescent(); + } + + + @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); + return metrics.charsWidth(buffer, start, length); + } + + /////////////////////////////////////////////////////////// // LWJGL event handling @@ -1598,7 +1622,7 @@ public class PLWJGL extends PGL { GL20.glDeleteProgram(program); } - public String glGetActiveAttrib (int program, int index, IntBuffer size, IntBuffer type) { + public String getActiveAttrib (int program, int index, IntBuffer size, IntBuffer type) { IntBuffer typeTmp = BufferUtils.createIntBuffer(2); String name = GL20.glGetActiveAttrib(program, index, 256, typeTmp); size.put(typeTmp.get(0));