Improved the logic that sets the size of the font textures

This commit is contained in:
codeanticode
2013-05-23 19:10:23 -04:00
parent a2ac452e98
commit 482c8ea2e9
4 changed files with 27 additions and 20 deletions
+21 -14
View File
@@ -26,6 +26,7 @@ package processing.opengl;
import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PFont;
import processing.core.PGraphics;
import processing.core.PImage;
import java.util.HashMap;
@@ -50,8 +51,8 @@ class FontTexture implements PConstants {
protected PGL pgl;
protected boolean is3D;
protected int maxTexWidth;
protected int maxTexHeight;
protected int minSize;
protected int maxSize;
protected int offsetX;
protected int offsetY;
protected int lineHeight;
@@ -62,12 +63,11 @@ class FontTexture implements PConstants {
protected TextureInfo[] glyphTexinfos;
protected HashMap<PFont.Glyph, TextureInfo> texinfoMap;
public FontTexture(PGraphicsOpenGL pg, PFont font, int maxw, int maxh,
boolean is3D) {
public FontTexture(PGraphicsOpenGL pg, PFont font, boolean is3D) {
pgl = PGraphicsOpenGL.pgl;
this.is3D = is3D;
initTexture(pg, font, maxw, maxh);
initTexture(pg, font);
}
@@ -84,13 +84,21 @@ class FontTexture implements PConstants {
}
protected void initTexture(PGraphicsOpenGL pg, PFont font, int w, int h) {
maxTexWidth = w;
maxTexHeight = h;
protected void initTexture(PGraphicsOpenGL pg, PFont font) {
currentTex = -1;
lastTex = -1;
int spow = PGL.nextPowerOfTwo(font.getSize());
minSize = PApplet.min(PGraphicsOpenGL.maxTextureSize,
PApplet.max(PGL.MIN_FONT_TEX_SIZE, spow));
maxSize = PApplet.min(PGraphicsOpenGL.maxTextureSize,
PApplet.max(PGL.MAX_FONT_TEX_SIZE, 2 * spow));
if (maxSize < spow) {
PGraphics.showWarning("The font size is too large to be properly " +
"displayed with OpenGL");
}
addTexture(pg);
offsetX = 0;
@@ -107,15 +115,14 @@ class FontTexture implements PConstants {
int w, h;
boolean resize;
w = maxTexWidth;
if (-1 < currentTex && textures[currentTex].glHeight < maxTexHeight) {
w = maxSize;
if (-1 < currentTex && textures[currentTex].glHeight < maxSize) {
// The height of the current texture is less than the maximum, this
// means we can replace it with a larger texture.
h = PApplet.min(2 * textures[currentTex].glHeight, maxTexHeight);
h = PApplet.min(2 * textures[currentTex].glHeight, maxSize);
resize = true;
} else {
h = PApplet.min(PGraphicsOpenGL.maxTextureSize, PGL.MAX_FONT_TEX_SIZE / 2,
maxTexHeight / 4);
h = minSize;
resize = false;
}
+2 -1
View File
@@ -127,7 +127,8 @@ public class PGL {
*/
protected static final int FLUSH_VERTEX_COUNT = MAX_VERTEX_INDEX1;
/** Maximum dimension of a texture used to hold font data. **/
/** Minimum/maximum dimensions of a texture used to hold font data. **/
protected static final int MIN_FONT_TEX_SIZE = 256;
protected static final int MAX_FONT_TEX_SIZE = 1024;
/** Minimum stroke weight needed to apply the full path stroking
@@ -3278,12 +3278,10 @@ public class PGraphicsOpenGL extends PGraphics {
textTex = pgPrimary.getFontTexture(textFont);
if (textTex == null || textTex.contextIsOutdated()) {
textTex = new FontTexture(pgPrimary, textFont,
PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize),
PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize), is3D());
textTex = new FontTexture(pgPrimary, textFont, is3D());
pgPrimary.setFontTexture(textFont, textTex);
}
textTex.begin();
// Saving style parameters modified by text rendering.
@@ -124,7 +124,8 @@ public class PGL extends processing.opengl.PGL {
*/
protected static final int FLUSH_VERTEX_COUNT = MAX_VERTEX_INDEX1;
/** Maximum dimension of a texture used to hold font data. **/
/** Minimum/maximum dimensions of a texture used to hold font data. **/
protected static final int MIN_FONT_TEX_SIZE = 256;
protected static final int MAX_FONT_TEX_SIZE = 1024;
/** Minimum stroke weight needed to apply the full path stroking