mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
moving font cache inside PGraphicsOpenGL
This commit is contained in:
@@ -4911,8 +4911,8 @@ public class PApplet extends Applet
|
||||
}
|
||||
|
||||
// For jpeg, gif, and png, load them using createImage(),
|
||||
// because the javax.imageio code was found to be much slower, see
|
||||
// <A HREF="http://dev.processing.org/bugs/show_bug.cgi?id=392">Bug 392</A>.
|
||||
// because the javax.imageio code was found to be much slower.
|
||||
// http://dev.processing.org/bugs/show_bug.cgi?id=392
|
||||
try {
|
||||
if (extension.equals("jpg") || extension.equals("jpeg") ||
|
||||
extension.equals("gif") || extension.equals("png") ||
|
||||
@@ -4949,12 +4949,12 @@ public class PApplet extends Applet
|
||||
if (loadImageFormats != null) {
|
||||
for (int i = 0; i < loadImageFormats.length; i++) {
|
||||
if (extension.equals(loadImageFormats[i])) {
|
||||
PImage image;
|
||||
image = loadImageIO(filename);
|
||||
return loadImageIO(filename);
|
||||
// PImage image = loadImageIO(filename);
|
||||
// if (params != null) {
|
||||
// image.setParams(g, params);
|
||||
// }
|
||||
return image;
|
||||
// return image;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -390,7 +390,7 @@ public class PFont implements PConstants {
|
||||
}
|
||||
// See if there's a native version of this font that can be used,
|
||||
// in case that's of interest later.
|
||||
findFont();
|
||||
findNative();
|
||||
}
|
||||
|
||||
|
||||
@@ -495,15 +495,15 @@ public class PFont implements PConstants {
|
||||
* findFont() function, or externally by a deriveFont() call if the font
|
||||
* is resized by PGraphicsJava2D.
|
||||
*/
|
||||
public void setFont(Font font) {
|
||||
this.font = font;
|
||||
public void setNative(Object font) {
|
||||
this.font = (Font) font;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the native java.awt.Font associated with this PFont (if any).
|
||||
*/
|
||||
public Font getFont() {
|
||||
public Object getNative() {
|
||||
if (subsetting) {
|
||||
return null; // don't return the font for use
|
||||
}
|
||||
@@ -533,7 +533,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.)
|
||||
*/
|
||||
public Font findFont() {
|
||||
public Object findNative() {
|
||||
if (font == null) {
|
||||
if (!fontSearched) {
|
||||
// this font may or may not be installed
|
||||
@@ -664,57 +664,17 @@ 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
|
||||
*/
|
||||
public void setCache(PGraphics renderer, Object storage) {
|
||||
if (cacheMap == null) cacheMap = new HashMap<PGraphics, Object>();
|
||||
cacheMap.put(renderer, storage);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get cache storage data for the specified renderer. Because each renderer
|
||||
* will cache data in different formats, it's necessary to store cache data
|
||||
* keyed by the renderer object. Otherwise, attempting to draw the same
|
||||
* image to both a PGraphicsJava2D and a PGraphicsOpenGL2 will cause errors.
|
||||
* @param renderer The PGraphics renderer associated to the font
|
||||
* @return metadata stored for the specified renderer
|
||||
*/
|
||||
public Object getCache(PGraphics renderer) {
|
||||
if (cacheMap == null) return null;
|
||||
return cacheMap.get(renderer);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove information associated with this renderer from the cache, if any.
|
||||
* @param parent The PGraphics renderer whose cache data should be removed
|
||||
*/
|
||||
public void removeCache(PGraphics renderer) {
|
||||
if (cacheMap != null) {
|
||||
cacheMap.remove(renderer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////
|
||||
|
||||
public int getGlyphCount() {
|
||||
return glyphCount;
|
||||
}
|
||||
|
||||
|
||||
public Glyph getGlyph(int i) {
|
||||
return glyphs[i];
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
|
||||
@@ -3785,7 +3785,7 @@ public class PGraphics extends PImage implements PConstants {
|
||||
textFont = which;
|
||||
if (hints[ENABLE_NATIVE_FONTS]) {
|
||||
//if (which.font == null) {
|
||||
which.findFont();
|
||||
which.findNative();
|
||||
//}
|
||||
}
|
||||
/*
|
||||
|
||||
@@ -1191,7 +1191,7 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
|
||||
defaultFontOrDeath("textAscent");
|
||||
}
|
||||
|
||||
Font font = textFont.getFont();
|
||||
Font font = (Font) textFont.getNative();
|
||||
//if (font != null && (textFont.isStream() || hints[ENABLE_NATIVE_FONTS])) {
|
||||
if (font != null) {
|
||||
FontMetrics metrics = parent.getFontMetrics(font);
|
||||
@@ -1206,7 +1206,7 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
|
||||
if (textFont == null) {
|
||||
defaultFontOrDeath("textAscent");
|
||||
}
|
||||
Font font = textFont.getFont();
|
||||
Font font = (Font) textFont.getNative();
|
||||
//if (font != null && (textFont.isStream() || hints[ENABLE_NATIVE_FONTS])) {
|
||||
if (font != null) {
|
||||
FontMetrics metrics = parent.getFontMetrics(font);
|
||||
@@ -1252,12 +1252,12 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
|
||||
// g2.setFont(textFontNative);
|
||||
// textFontNativeMetrics = g2.getFontMetrics(textFontNative);
|
||||
// }
|
||||
Font font = textFont.getFont();
|
||||
Font font = (Font) textFont.getNative();
|
||||
//if (font != null && (textFont.isStream() || hints[ENABLE_NATIVE_FONTS])) {
|
||||
if (font != null) {
|
||||
Font dfont = font.deriveFont(size);
|
||||
g2.setFont(dfont);
|
||||
textFont.setFont(dfont);
|
||||
textFont.setNative(dfont);
|
||||
}
|
||||
|
||||
// take care of setting the textSize and textLeading vars
|
||||
@@ -1275,7 +1275,7 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
|
||||
|
||||
@Override
|
||||
protected float textWidthImpl(char buffer[], int start, int stop) {
|
||||
Font font = textFont.getFont();
|
||||
Font font = (Font) textFont.getNative();
|
||||
//if (font != null && (textFont.isStream() || hints[ENABLE_NATIVE_FONTS])) {
|
||||
if (font != null) {
|
||||
// maybe should use one of the newer/fancier functions for this?
|
||||
@@ -1328,7 +1328,7 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
|
||||
@Override
|
||||
protected void textLineImpl(char buffer[], int start, int stop,
|
||||
float x, float y) {
|
||||
Font font = textFont.getFont();
|
||||
Font font = (Font) textFont.getNative();
|
||||
// if (font != null && (textFont.isStream() || hints[ENABLE_NATIVE_FONTS])) {
|
||||
if (font != null) {
|
||||
/*
|
||||
@@ -2125,7 +2125,9 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
|
||||
public void copy(PImage src,
|
||||
int sx, int sy, int sw, int sh,
|
||||
int dx, int dy, int dw, int dh) {
|
||||
g2.drawImage(src.getImage(), dx, dy, dx + dw, dy + dh, sx, sy, sx + sw, sy + sh, null);
|
||||
g2.drawImage((Image) src.getNative(),
|
||||
dx, dy, dx + dw, dy + dh,
|
||||
sx, sy, sx + sw, sy + sh, null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -292,9 +292,10 @@ public class PImage implements PConstants, Cloneable {
|
||||
|
||||
|
||||
/**
|
||||
* Returns a BufferedImage from this PImage.
|
||||
* Returns a native BufferedImage from this PImage.
|
||||
*/
|
||||
public java.awt.Image getImage() {
|
||||
// public java.awt.Image getImage() {
|
||||
public Object getNative() {
|
||||
loadPixels();
|
||||
int type = (format == RGB) ?
|
||||
BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
|
||||
|
||||
+2
-2
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
@@ -3508,17 +3530,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();
|
||||
@@ -3570,7 +3592,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.
|
||||
@@ -3594,7 +3616,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) {
|
||||
|
||||
Reference in New Issue
Block a user