From 4b8bd5ec58cb2edfb51bd9ee930beb9c539a8a0b Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 1 Apr 2011 06:48:52 +0200 Subject: Final core and demo changes for jogl merge Core: - Region: Cleanup up constant names - Renderer: Add getRenderType() - TextRenderer: Add cache size limit - JavaFontLoader: Add FIXME 'Add cache size to limit memory usage' - UbuntuFontLoader: Add cache and FIXME 'Add cache size to limit memory usage' - TypecastFont: Add FIXME 'Add cache size to limit memory usage ??' Demos: - Relocated and split (main/listener) for jogl merge - Add 's' for screenshot - Text: - Add 'i' for live editing mode (until CR, backspace supported) --- .../jogamp/graph/curve/opengl/TextRenderer.java | 46 ++++++++++++++++------ 1 file changed, 35 insertions(+), 11 deletions(-) (limited to 'src/com/jogamp/graph/curve/opengl/TextRenderer.java') diff --git a/src/com/jogamp/graph/curve/opengl/TextRenderer.java b/src/com/jogamp/graph/curve/opengl/TextRenderer.java index 83f2c93ca..2bb99d27c 100644 --- a/src/com/jogamp/graph/curve/opengl/TextRenderer.java +++ b/src/com/jogamp/graph/curve/opengl/TextRenderer.java @@ -1,5 +1,6 @@ package com.jogamp.graph.curve.opengl; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -15,8 +16,6 @@ import com.jogamp.graph.geom.Vertex; public abstract class TextRenderer extends Renderer { - protected HashMap strings = new HashMap(); - /** * Create a Hardware accelerated Text Renderer. * @param factory optional Point.Factory for Vertex construction. Default is Vertex.Factory. @@ -58,22 +57,47 @@ public abstract class TextRenderer extends Renderer { GlyphString glyphString = new GlyphString(pointFactory, font.getName(), str); glyphString.createfromFontPath(paths, affineTransform); - glyphString.generateRegion(gl.getContext(), sharpness, st, regionType); + glyphString.generateRegion(gl.getContext(), sharpness, st, renderType); return glyphString; } - protected static String getTextHashCode(Font font, String str, int fontSize) { - // FIXME: use integer hash code - return font.getName() + "." + str.hashCode() + "." + fontSize; - } - public void flushCache() { - Iterator iterator = strings.values().iterator(); + Iterator iterator = stringCacheMap.values().iterator(); while(iterator.hasNext()){ GlyphString glyphString = iterator.next(); glyphString.destroy(); } - strings.clear(); - } + stringCacheMap.clear(); + stringCacheArray.clear(); + } + + public final void setCacheMaxSize(int newSize ) { stringCacheMaxSize = newSize; validateCache(0); } + public final int getCacheMaxSize() { return stringCacheMaxSize; } + public final int getCacheSize() { return stringCacheArray.size(); } + + protected void validateCache(int space) { + while ( getCacheSize() + space > getCacheMaxSize() ) { + String key = stringCacheArray.remove(0); + stringCacheMap.remove(key); + } + } + + protected GlyphString getCachedGlyphString(Font font, String str, int fontSize) { + final String key = font.getName() + "." + str.hashCode() + "." + fontSize; + return stringCacheMap.get(key); + } + + protected void addCachedGlyphString(Font font, String str, int fontSize, GlyphString glyphString) { + final String key = font.getName() + "." + str.hashCode() + "." + fontSize; + validateCache(1); + stringCacheMap.put(key, glyphString); + stringCacheArray.add(stringCacheArray.size(), key); + } + + // Cache is adding at the end of the array + public static final int DEFAULT_CACHE_SIZE = 32; + private HashMap stringCacheMap = new HashMap(DEFAULT_CACHE_SIZE); + private ArrayList stringCacheArray = new ArrayList(DEFAULT_CACHE_SIZE); + private int stringCacheMaxSize = DEFAULT_CACHE_SIZE; // -1 unlimited, 0 off, >0 limited } \ No newline at end of file -- cgit v1.2.3