Skip to content

Commit 8db144a

Browse files
committed
Improved the logic that sets the size of the font textures
1 parent 593a3a8 commit 8db144a

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

core/src/processing/opengl/FontTexture.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import processing.core.PApplet;
2727
import processing.core.PConstants;
2828
import processing.core.PFont;
29+
import processing.core.PGraphics;
2930
import processing.core.PImage;
3031

3132
import java.util.HashMap;
@@ -50,8 +51,8 @@ class FontTexture implements PConstants {
5051
protected PGL pgl;
5152
protected boolean is3D;
5253

53-
protected int maxTexWidth;
54-
protected int maxTexHeight;
54+
protected int minSize;
55+
protected int maxSize;
5556
protected int offsetX;
5657
protected int offsetY;
5758
protected int lineHeight;
@@ -62,12 +63,11 @@ class FontTexture implements PConstants {
6263
protected TextureInfo[] glyphTexinfos;
6364
protected HashMap<PFont.Glyph, TextureInfo> texinfoMap;
6465

65-
public FontTexture(PGraphicsOpenGL pg, PFont font, int maxw, int maxh,
66-
boolean is3D) {
66+
public FontTexture(PGraphicsOpenGL pg, PFont font, boolean is3D) {
6767
pgl = PGraphicsOpenGL.pgl;
6868
this.is3D = is3D;
6969

70-
initTexture(pg, font, maxw, maxh);
70+
initTexture(pg, font);
7171
}
7272

7373

@@ -84,13 +84,21 @@ protected void dispose() {
8484
}
8585

8686

87-
protected void initTexture(PGraphicsOpenGL pg, PFont font, int w, int h) {
88-
maxTexWidth = w;
89-
maxTexHeight = h;
90-
87+
protected void initTexture(PGraphicsOpenGL pg, PFont font) {
9188
currentTex = -1;
9289
lastTex = -1;
9390

91+
int spow = PGL.nextPowerOfTwo(font.getSize());
92+
minSize = PApplet.min(PGraphicsOpenGL.maxTextureSize,
93+
PApplet.max(PGL.MIN_FONT_TEX_SIZE, spow));
94+
maxSize = PApplet.min(PGraphicsOpenGL.maxTextureSize,
95+
PApplet.max(PGL.MAX_FONT_TEX_SIZE, 2 * spow));
96+
97+
if (maxSize < spow) {
98+
PGraphics.showWarning("The font size is too large to be properly " +
99+
"displayed with OpenGL");
100+
}
101+
94102
addTexture(pg);
95103

96104
offsetX = 0;
@@ -107,15 +115,14 @@ public boolean addTexture(PGraphicsOpenGL pg) {
107115
int w, h;
108116
boolean resize;
109117

110-
w = maxTexWidth;
111-
if (-1 < currentTex && textures[currentTex].glHeight < maxTexHeight) {
118+
w = maxSize;
119+
if (-1 < currentTex && textures[currentTex].glHeight < maxSize) {
112120
// The height of the current texture is less than the maximum, this
113121
// means we can replace it with a larger texture.
114-
h = PApplet.min(2 * textures[currentTex].glHeight, maxTexHeight);
122+
h = PApplet.min(2 * textures[currentTex].glHeight, maxSize);
115123
resize = true;
116124
} else {
117-
h = PApplet.min(PGraphicsOpenGL.maxTextureSize, PGL.MAX_FONT_TEX_SIZE / 2,
118-
maxTexHeight / 4);
125+
h = minSize;
119126
resize = false;
120127
}
121128

core/src/processing/opengl/PGL.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ public class PGL {
127127
*/
128128
protected static final int FLUSH_VERTEX_COUNT = MAX_VERTEX_INDEX1;
129129

130-
/** Maximum dimension of a texture used to hold font data. **/
130+
/** Minimum/maximum dimensions of a texture used to hold font data. **/
131+
protected static final int MIN_FONT_TEX_SIZE = 256;
131132
protected static final int MAX_FONT_TEX_SIZE = 1024;
132133

133134
/** Minimum stroke weight needed to apply the full path stroking

core/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,12 +3278,10 @@ protected void textLineImpl(char buffer[], int start, int stop,
32783278
textTex = pgPrimary.getFontTexture(textFont);
32793279

32803280
if (textTex == null || textTex.contextIsOutdated()) {
3281-
textTex = new FontTexture(pgPrimary, textFont,
3282-
PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize),
3283-
PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize), is3D());
3281+
textTex = new FontTexture(pgPrimary, textFont, is3D());
32843282
pgPrimary.setFontTexture(textFont, textTex);
32853283
}
3286-
3284+
32873285
textTex.begin();
32883286

32893287
// Saving style parameters modified by text rendering.

java/libraries/lwjgl/src/processing/lwjgl/PGL.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ public class PGL extends processing.opengl.PGL {
124124
*/
125125
protected static final int FLUSH_VERTEX_COUNT = MAX_VERTEX_INDEX1;
126126

127-
/** Maximum dimension of a texture used to hold font data. **/
127+
/** Minimum/maximum dimensions of a texture used to hold font data. **/
128+
protected static final int MIN_FONT_TEX_SIZE = 256;
128129
protected static final int MAX_FONT_TEX_SIZE = 1024;
129130

130131
/** Minimum stroke weight needed to apply the full path stroking

0 commit comments

Comments
 (0)