Skip to content

Commit 1cd0624

Browse files
committed
moving font cache inside PGraphicsOpenGL
1 parent 363b460 commit 1cd0624

16 files changed

Lines changed: 157 additions & 143 deletions

File tree

android/core/src/processing/core/PApplet.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3848,7 +3848,7 @@ public PFont createFont(String name, float size,
38483848
AssetManager assets = getBaseContext().getAssets();
38493849
baseFont = Typeface.createFromAsset(assets, name);
38503850
} else {
3851-
baseFont = PFont.findTypeface(name);
3851+
baseFont = (Typeface) PFont.findNative(name);
38523852
}
38533853
return new PFont(baseFont, round(size), smooth, charset);
38543854
}
@@ -8976,8 +8976,8 @@ public boolean isGL() {
89768976
}
89778977

89788978

8979-
public Bitmap getBitmap() {
8980-
return g.getBitmap();
8979+
public Object getNative() {
8980+
return g.getNative();
89818981
}
89828982

89838983

android/core/src/processing/core/PFont.java

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ public class PFont implements PConstants {
118118
/**
119119
* True if this font should return 'null' for getFont(), so that the native
120120
* font will be used to create a subset, but the native version of the font
121-
* will not be used.
121+
* will not be used.
122122
*/
123-
protected boolean subsetting;
124-
123+
protected boolean subsetting;
124+
125125
/**
126126
* True if we've already tried to find the native version of this font.
127127
*/
@@ -133,18 +133,18 @@ public class PFont implements PConstants {
133133
* bug that they can't be bothered to fix.
134134
*/
135135
static protected Typeface[] typefaces;
136-
136+
137137
// objects to handle creation of font characters only as they're needed
138138
Bitmap lazyBitmap;
139139
Canvas lazyCanvas;
140140
Paint lazyPaint;
141141
// FontMetrics lazyMetrics;
142142
int[] lazySamples;
143-
143+
144144
/** for subclasses that need to store metadata about the font */
145-
protected HashMap<PGraphics, Object> cacheMap;
146-
147-
145+
protected HashMap<PGraphics, Object> cacheMap;
146+
147+
148148
public PFont() { } // for subclasses
149149

150150

@@ -212,7 +212,7 @@ public PFont(Typeface font, int size, boolean smooth, char charset[]) {
212212

213213
if (charset == null) {
214214
lazy = true;
215-
215+
216216
} else {
217217
// charset needs to be sorted to make index lookup run more quickly
218218
// http://dev.processing.org/bugs/show_bug.cgi?id=494
@@ -317,7 +317,7 @@ public PFont(InputStream input) throws IOException {
317317
}
318318
}
319319

320-
320+
321321
/**
322322
* Write this PFont to an OutputStream.
323323
* <p>
@@ -408,20 +408,20 @@ public String getName() {
408408
return name;
409409
}
410410

411-
411+
412412
/**
413413
* Return size of this font.
414414
*/
415415
public int getSize() {
416416
return size;
417417
}
418-
418+
419419

420420
public void setSubsetting() {
421421
subsetting = true;
422422
}
423423

424-
424+
425425
public String getPostScriptName() {
426426
return psname;
427427
}
@@ -430,15 +430,15 @@ public String getPostScriptName() {
430430
/**
431431
* Set the native complement of this font.
432432
*/
433-
public void setTypeface(Typeface typeface) {
434-
this.typeface = typeface;
433+
public void setNative(Object typeface) {
434+
this.typeface = (Typeface) typeface;
435435
}
436436

437437

438438
/**
439439
* Return the native Typeface object associated with this PFont (if any).
440440
*/
441-
public Typeface getTypeface() {
441+
public Typeface getNative() {
442442
if (subsetting) {
443443
return null;
444444
}
@@ -450,7 +450,7 @@ public Typeface getTypeface() {
450450
* Attempt to find the native version of this font.
451451
* (Public so that it can be used by OpenGL or other renderers.)
452452
*/
453-
static public Typeface findTypeface(String name) {
453+
static public Object findNative(String name) {
454454
loadTypefaces();
455455
return typefaceMap.get(name);
456456
}
@@ -564,14 +564,14 @@ public float width(char c) {
564564
//////////////////////////////////////////////////////////////
565565

566566
// METADATA REQUIRED BY THE RENDERERS
567-
567+
568568

569569
/**
570570
* Store data of some kind for a renderer that requires extra metadata of
571571
* some kind. Usually this is a renderer-specific representation of the
572572
* font data, for instance a custom OpenGL texture for PGraphicsOpenGL2.
573573
* @param renderer The PGraphics renderer associated to the font
574-
* @param storage The metadata required by the renderer
574+
* @param storage The metadata required by the renderer
575575
*/
576576
public void setCache(PGraphics renderer, Object storage) {
577577
if (cacheMap == null) cacheMap = new HashMap<PGraphics, Object>();
@@ -601,22 +601,22 @@ public void removeCache(PGraphics renderer) {
601601
if (cacheMap != null) {
602602
cacheMap.remove(renderer);
603603
}
604-
}
605-
606-
607-
//////////////////////////////////////////////////////////////
608-
604+
}
605+
606+
607+
//////////////////////////////////////////////////////////////
608+
609609
public int getGlyphCount() {
610610
return glyphCount;
611611
}
612-
612+
613613
public Glyph getGlyph(int i) {
614-
return glyphs[i];
615-
}
616-
614+
return glyphs[i];
615+
}
616+
617617
//////////////////////////////////////////////////////////////
618618

619-
619+
620620
static final char[] EXTRA_CHARS = {
621621
0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
622622
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
@@ -725,7 +725,7 @@ static public void loadTypefaces() {
725725
}
726726
}
727727

728-
728+
729729
/////////////////////////////////////////////////////////////
730730

731731
/**
@@ -736,7 +736,7 @@ public class Glyph {
736736
public int value;
737737
public int height;
738738
public int width;
739-
public int index;
739+
public int index;
740740
public int setWidth;
741741
public int topExtent;
742742
public int leftExtent;

android/core/src/processing/core/PGraphicsAndroid2D.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ public PShape loadShape(String filename) {
11201120
@Override
11211121
public void textFont(PFont which) {
11221122
super.textFont(which);
1123-
fillPaint.setTypeface(which.getTypeface());
1123+
fillPaint.setTypeface(which.getNative());
11241124
}
11251125

11261126

@@ -1151,7 +1151,7 @@ public void textSize(float size) {
11511151
defaultFontOrDeath("textSize", size);
11521152
}
11531153

1154-
Typeface font = textFont.getTypeface();
1154+
Typeface font = textFont.getNative();
11551155
if (font != null) {
11561156
fillPaint.setTextSize(size);
11571157
}
@@ -1184,7 +1184,7 @@ protected void endTextScreenMode() {
11841184
@Override
11851185
protected float textWidthImpl(char buffer[], int start, int stop) {
11861186
// Font font = textFont.getFont();
1187-
Typeface font = textFont.getTypeface();
1187+
Typeface font = textFont.getNative();
11881188
if (font == null) {
11891189
return super.textWidthImpl(buffer, start, stop);
11901190
}
@@ -1217,7 +1217,7 @@ protected float textWidthImpl(char buffer[], int start, int stop) {
12171217
@Override
12181218
protected void textLineImpl(char buffer[], int start, int stop,
12191219
float x, float y) {
1220-
Typeface font = textFont.getTypeface();
1220+
Typeface font = textFont.getNative();
12211221
if (font == null) {
12221222
showWarning("Inefficient font rendering: use createFont() with a TTF/OTF instead of loadFont().");
12231223
//new Exception().printStackTrace(System.out);

android/core/src/processing/core/PImage.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,20 @@ protected void checkAlpha() {
168168
* Construct a new PImage from an Android bitmap. The pixels[] array is not
169169
* initialized, nor is data copied to it, until loadPixels() is called.
170170
*/
171-
public PImage(Bitmap image) {
172-
this.bitmap = image;
173-
this.width = image.getWidth();
174-
this.height = image.getHeight();
171+
public PImage(Object nativeObject) {
172+
Bitmap bitmap = (Bitmap) nativeObject;
173+
this.bitmap = bitmap;
174+
this.width = bitmap.getWidth();
175+
this.height = bitmap.getHeight();
175176
this.pixels = null;
176-
this.format = image.hasAlpha() ? ARGB : RGB;
177+
this.format = bitmap.hasAlpha() ? ARGB : RGB;
177178
}
178179

179180

180181
/**
181-
* Returns a BufferedImage from this PImage.
182+
* Returns the native Bitmap object for this PImage.
182183
*/
183-
// public java.awt.Image getImage() {
184-
// loadPixels();
185-
// int type = (format == RGB) ?
186-
// BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
187-
// BufferedImage image = new BufferedImage(width, height, type);
188-
// WritableRaster wr = image.getRaster();
189-
// wr.setDataElements(0, 0, width, height, pixels);
190-
// return image;
191-
// }
192-
public Bitmap getBitmap() {
184+
public Object getNative() {
193185
return bitmap;
194186
}
195187

android/core/src/processing/opengl/PFontTexture.java renamed to android/core/src/processing/opengl/FontTexture.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* over several textures.
4747
* @author Andres Colubri
4848
*/
49-
class PFontTexture implements PConstants {
49+
class FontTexture implements PConstants {
5050
protected PApplet parent;
5151
protected PGraphicsOpenGL pg;
5252
protected PGL pgl;
@@ -65,7 +65,7 @@ class PFontTexture implements PConstants {
6565
protected TextureInfo[] glyphTexinfos;
6666
protected HashMap<PFont.Glyph, TextureInfo> texinfoMap;
6767

68-
public PFontTexture(PApplet parent, PFont font, int maxw, int maxh,
68+
public FontTexture(PApplet parent, PFont font, int maxw, int maxh,
6969
boolean is3D) {
7070
this.parent = parent;
7171
this.font = font;

android/core/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public class PGraphicsOpenGL extends PGraphics {
4343
/** The renderer currently in use. */
4444
protected static PGraphicsOpenGL pgCurrent = null;
4545

46+
/** Font cache for texture objects. */
47+
protected WeakHashMap<PFont, FontTexture> fontMap =
48+
new WeakHashMap<PFont, FontTexture>();
49+
4650
/** Additional image parameters not covered by the cache. */
4751
protected WeakHashMap<PImage, Object> paramMap =
4852
new WeakHashMap<PImage, Object>();
@@ -353,7 +357,7 @@ public class PGraphicsOpenGL extends PGraphics {
353357
// Text:
354358

355359
/** Font texture of currently selected font. */
356-
PFontTexture textTex;
360+
FontTexture textTex;
357361

358362
// .......................................................
359363

@@ -610,6 +614,24 @@ protected void setFlushMode(int mode) {
610614
}
611615

612616

617+
//////////////////////////////////////////////////////////////
618+
619+
620+
protected void setFontTexture(PFont font, FontTexture fontTexture) {
621+
fontMap.put(font, fontTexture);
622+
}
623+
624+
625+
protected FontTexture getFontTexture(PFont font) {
626+
return fontMap.get(font);
627+
}
628+
629+
630+
protected void removeFontTexture(PFont font) {
631+
fontMap.remove(font);
632+
}
633+
634+
613635
//////////////////////////////////////////////////////////////
614636

615637
/**
@@ -1654,8 +1676,8 @@ public void beginDraw() {
16541676
if (texture != null) {
16551677
// The screen texture should be deleted because it
16561678
// corresponds to the old window size.
1657-
removeCache(pgPrimary);
1658-
removeParams(pgPrimary);
1679+
pgPrimary.removeCache(this);
1680+
pgPrimary.removeParams(this);
16591681
texture = null;
16601682
loadTexture();
16611683
}
@@ -3507,17 +3529,17 @@ protected boolean textModeCheck(int mode) {
35073529
@Override
35083530
protected void textLineImpl(char buffer[], int start, int stop,
35093531
float x, float y) {
3510-
textTex = (PFontTexture)textFont.getCache(pgPrimary);
3532+
textTex = pgPrimary.getFontTexture(textFont);
35113533
if (textTex == null) {
3512-
textTex = new PFontTexture(parent, textFont, maxTextureSize,
3534+
textTex = new FontTexture(parent, textFont, maxTextureSize,
35133535
maxTextureSize, is3D());
3514-
textFont.setCache(pgPrimary, textTex);
3536+
pgPrimary.setFontTexture(textFont, textTex);
35153537
} else {
35163538
if (textTex.contextIsOutdated()) {
3517-
textTex = new PFontTexture(parent, textFont,
3539+
textTex = new FontTexture(parent, textFont,
35183540
PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize),
35193541
PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize), is3D());
3520-
textFont.setCache(pgPrimary, textTex);
3542+
pgPrimary.setFontTexture(textFont, textTex);
35213543
}
35223544
}
35233545
textTex.begin();
@@ -3569,7 +3591,7 @@ protected void textCharImpl(char ch, float x, float y) {
35693591
PFont.Glyph glyph = textFont.getGlyph(ch);
35703592

35713593
if (glyph != null) {
3572-
PFontTexture.TextureInfo tinfo = textTex.getTexInfo(glyph);
3594+
FontTexture.TextureInfo tinfo = textTex.getTexInfo(glyph);
35733595

35743596
if (tinfo == null) {
35753597
// Adding new glyph to the font texture.
@@ -3593,7 +3615,7 @@ protected void textCharImpl(char ch, float x, float y) {
35933615
}
35943616

35953617

3596-
protected void textCharModelImpl(PFontTexture.TextureInfo info,
3618+
protected void textCharModelImpl(FontTexture.TextureInfo info,
35973619
float x0, float y0,
35983620
float x1, float y1) {
35993621
if (textTex.currentTex != info.texIndex) {

android/todo.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
0209 android
2+
A GL android sketch stops running after rotation
3+
A http://code.google.com/p/processing/issues/detail?id=1146
24

35

46
_ inside AndroidPreprocessor

0 commit comments

Comments
 (0)