moving font cache inside PGraphicsOpenGL

This commit is contained in:
benfry
2012-09-02 20:07:26 +00:00
parent 720d1ef25c
commit 662fec53e6
16 changed files with 157 additions and 143 deletions
@@ -3848,7 +3848,7 @@ public class PApplet extends Activity implements PConstants, Runnable {
AssetManager assets = getBaseContext().getAssets();
baseFont = Typeface.createFromAsset(assets, name);
} else {
baseFont = PFont.findTypeface(name);
baseFont = (Typeface) PFont.findNative(name);
}
return new PFont(baseFont, round(size), smooth, charset);
}
@@ -8976,8 +8976,8 @@ public class PApplet extends Activity implements PConstants, Runnable {
}
public Bitmap getBitmap() {
return g.getBitmap();
public Object getNative() {
return g.getNative();
}
+31 -31
View File
@@ -118,10 +118,10 @@ public class PFont implements PConstants {
/**
* True if this font should return 'null' for getFont(), so that the native
* font will be used to create a subset, but the native version of the font
* will not be used.
* will not be used.
*/
protected boolean subsetting;
protected boolean subsetting;
/**
* True if we've already tried to find the native version of this font.
*/
@@ -133,18 +133,18 @@ public class PFont implements PConstants {
* bug that they can't be bothered to fix.
*/
static protected Typeface[] typefaces;
// objects to handle creation of font characters only as they're needed
Bitmap lazyBitmap;
Canvas lazyCanvas;
Paint lazyPaint;
// FontMetrics lazyMetrics;
int[] lazySamples;
/** for subclasses that need to store metadata about the font */
protected HashMap<PGraphics, Object> cacheMap;
protected HashMap<PGraphics, Object> cacheMap;
public PFont() { } // for subclasses
@@ -212,7 +212,7 @@ public class PFont implements PConstants {
if (charset == null) {
lazy = true;
} else {
// charset needs to be sorted to make index lookup run more quickly
// http://dev.processing.org/bugs/show_bug.cgi?id=494
@@ -317,7 +317,7 @@ public class PFont implements PConstants {
}
}
/**
* Write this PFont to an OutputStream.
* <p>
@@ -408,20 +408,20 @@ public class PFont implements PConstants {
return name;
}
/**
* Return size of this font.
*/
public int getSize() {
return size;
}
public void setSubsetting() {
subsetting = true;
}
public String getPostScriptName() {
return psname;
}
@@ -430,15 +430,15 @@ public class PFont implements PConstants {
/**
* Set the native complement of this font.
*/
public void setTypeface(Typeface typeface) {
this.typeface = typeface;
public void setNative(Object typeface) {
this.typeface = (Typeface) typeface;
}
/**
* Return the native Typeface object associated with this PFont (if any).
*/
public Typeface getTypeface() {
public Typeface getNative() {
if (subsetting) {
return null;
}
@@ -450,7 +450,7 @@ public class PFont implements PConstants {
* Attempt to find the native version of this font.
* (Public so that it can be used by OpenGL or other renderers.)
*/
static public Typeface findTypeface(String name) {
static public Object findNative(String name) {
loadTypefaces();
return typefaceMap.get(name);
}
@@ -564,14 +564,14 @@ public class PFont implements PConstants {
//////////////////////////////////////////////////////////////
// METADATA REQUIRED BY THE RENDERERS
/**
* Store data of some kind for a renderer that requires extra metadata of
* some kind. Usually this is a renderer-specific representation of the
* font data, for instance a custom OpenGL texture for PGraphicsOpenGL2.
* @param renderer The PGraphics renderer associated to the font
* @param storage The metadata required by the renderer
* @param storage The metadata required by the renderer
*/
public void setCache(PGraphics renderer, Object storage) {
if (cacheMap == null) cacheMap = new HashMap<PGraphics, Object>();
@@ -601,22 +601,22 @@ public class PFont implements PConstants {
if (cacheMap != null) {
cacheMap.remove(renderer);
}
}
//////////////////////////////////////////////////////////////
}
//////////////////////////////////////////////////////////////
public int getGlyphCount() {
return glyphCount;
}
public Glyph getGlyph(int i) {
return glyphs[i];
}
return glyphs[i];
}
//////////////////////////////////////////////////////////////
static final char[] EXTRA_CHARS = {
0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
@@ -725,7 +725,7 @@ public class PFont implements PConstants {
}
}
/////////////////////////////////////////////////////////////
/**
@@ -736,7 +736,7 @@ public class PFont implements PConstants {
public int value;
public int height;
public int width;
public int index;
public int index;
public int setWidth;
public int topExtent;
public int leftExtent;
@@ -1120,7 +1120,7 @@ public class PGraphicsAndroid2D extends PGraphics {
@Override
public void textFont(PFont which) {
super.textFont(which);
fillPaint.setTypeface(which.getTypeface());
fillPaint.setTypeface(which.getNative());
}
@@ -1151,7 +1151,7 @@ public class PGraphicsAndroid2D extends PGraphics {
defaultFontOrDeath("textSize", size);
}
Typeface font = textFont.getTypeface();
Typeface font = textFont.getNative();
if (font != null) {
fillPaint.setTextSize(size);
}
@@ -1184,7 +1184,7 @@ public class PGraphicsAndroid2D extends PGraphics {
@Override
protected float textWidthImpl(char buffer[], int start, int stop) {
// Font font = textFont.getFont();
Typeface font = textFont.getTypeface();
Typeface font = textFont.getNative();
if (font == null) {
return super.textWidthImpl(buffer, start, stop);
}
@@ -1217,7 +1217,7 @@ public class PGraphicsAndroid2D extends PGraphics {
@Override
protected void textLineImpl(char buffer[], int start, int stop,
float x, float y) {
Typeface font = textFont.getTypeface();
Typeface font = textFont.getNative();
if (font == null) {
showWarning("Inefficient font rendering: use createFont() with a TTF/OTF instead of loadFont().");
//new Exception().printStackTrace(System.out);
+8 -16
View File
@@ -168,28 +168,20 @@ public class PImage implements PConstants, Cloneable {
* Construct a new PImage from an Android bitmap. The pixels[] array is not
* initialized, nor is data copied to it, until loadPixels() is called.
*/
public PImage(Bitmap image) {
this.bitmap = image;
this.width = image.getWidth();
this.height = image.getHeight();
public PImage(Object nativeObject) {
Bitmap bitmap = (Bitmap) nativeObject;
this.bitmap = bitmap;
this.width = bitmap.getWidth();
this.height = bitmap.getHeight();
this.pixels = null;
this.format = image.hasAlpha() ? ARGB : RGB;
this.format = bitmap.hasAlpha() ? ARGB : RGB;
}
/**
* Returns a BufferedImage from this PImage.
* Returns the native Bitmap object for this PImage.
*/
// public java.awt.Image getImage() {
// loadPixels();
// int type = (format == RGB) ?
// BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
// BufferedImage image = new BufferedImage(width, height, type);
// WritableRaster wr = image.getRaster();
// wr.setDataElements(0, 0, width, height, pixels);
// return image;
// }
public Bitmap getBitmap() {
public Object getNative() {
return bitmap;
}
@@ -46,7 +46,7 @@ import java.util.HashMap;
* over several textures.
* @author Andres Colubri
*/
class PFontTexture implements PConstants {
class FontTexture implements PConstants {
protected PApplet parent;
protected PGraphicsOpenGL pg;
protected PGL pgl;
@@ -65,7 +65,7 @@ class PFontTexture implements PConstants {
protected TextureInfo[] glyphTexinfos;
protected HashMap<PFont.Glyph, TextureInfo> texinfoMap;
public PFontTexture(PApplet parent, PFont font, int maxw, int maxh,
public FontTexture(PApplet parent, PFont font, int maxw, int maxh,
boolean is3D) {
this.parent = parent;
this.font = font;
@@ -43,6 +43,10 @@ public class PGraphicsOpenGL extends PGraphics {
/** The renderer currently in use. */
protected static PGraphicsOpenGL pgCurrent = null;
/** Font cache for texture objects. */
protected WeakHashMap<PFont, FontTexture> fontMap =
new WeakHashMap<PFont, FontTexture>();
/** Additional image parameters not covered by the cache. */
protected WeakHashMap<PImage, Object> paramMap =
new WeakHashMap<PImage, Object>();
@@ -353,7 +357,7 @@ public class PGraphicsOpenGL extends PGraphics {
// Text:
/** Font texture of currently selected font. */
PFontTexture textTex;
FontTexture textTex;
// .......................................................
@@ -610,6 +614,24 @@ public class PGraphicsOpenGL extends PGraphics {
}
//////////////////////////////////////////////////////////////
protected void setFontTexture(PFont font, FontTexture fontTexture) {
fontMap.put(font, fontTexture);
}
protected FontTexture getFontTexture(PFont font) {
return fontMap.get(font);
}
protected void removeFontTexture(PFont font) {
fontMap.remove(font);
}
//////////////////////////////////////////////////////////////
/**
@@ -1654,8 +1676,8 @@ public class PGraphicsOpenGL extends PGraphics {
if (texture != null) {
// The screen texture should be deleted because it
// corresponds to the old window size.
removeCache(pgPrimary);
removeParams(pgPrimary);
pgPrimary.removeCache(this);
pgPrimary.removeParams(this);
texture = null;
loadTexture();
}
@@ -3507,17 +3529,17 @@ public class PGraphicsOpenGL extends PGraphics {
@Override
protected void textLineImpl(char buffer[], int start, int stop,
float x, float y) {
textTex = (PFontTexture)textFont.getCache(pgPrimary);
textTex = pgPrimary.getFontTexture(textFont);
if (textTex == null) {
textTex = new PFontTexture(parent, textFont, maxTextureSize,
textTex = new FontTexture(parent, textFont, maxTextureSize,
maxTextureSize, is3D());
textFont.setCache(pgPrimary, textTex);
pgPrimary.setFontTexture(textFont, textTex);
} else {
if (textTex.contextIsOutdated()) {
textTex = new PFontTexture(parent, textFont,
textTex = new FontTexture(parent, textFont,
PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize),
PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize), is3D());
textFont.setCache(pgPrimary, textTex);
pgPrimary.setFontTexture(textFont, textTex);
}
}
textTex.begin();
@@ -3569,7 +3591,7 @@ public class PGraphicsOpenGL extends PGraphics {
PFont.Glyph glyph = textFont.getGlyph(ch);
if (glyph != null) {
PFontTexture.TextureInfo tinfo = textTex.getTexInfo(glyph);
FontTexture.TextureInfo tinfo = textTex.getTexInfo(glyph);
if (tinfo == null) {
// Adding new glyph to the font texture.
@@ -3593,7 +3615,7 @@ public class PGraphicsOpenGL extends PGraphics {
}
protected void textCharModelImpl(PFontTexture.TextureInfo info,
protected void textCharModelImpl(FontTexture.TextureInfo info,
float x0, float y0,
float x1, float y1) {
if (textTex.currentTex != info.texIndex) {