From 183e1bc1868699b99eb9f9c8bf18d646d1120a48 Mon Sep 17 00:00:00 2001
From: Sven Gothel
* Cached {@link GLRegion}s will be destroyed w/ {@link #clear(GL2ES2)} or to free memory.
*
+ * In case of a multisampling region renderer, i.e. {@link Region#VBAA_RENDERING_BIT}, recreating the {@link GLRegion}
+ * is a huge performance impact.
+ * In such case better use {@link #drawString3D(GLRegion, RegionRenderer, GL2ES2, Font, float, CharSequence, int[])}
+ * instead.
+ *
* TrueType Font Specification:
- * http://developer.apple.com/fonts/ttrefman/rm06/Chap6.html
- * http://www.microsoft.com/typography/SpecificationsOverview.mspx
- * http://www.microsoft.com/typography/otspec/
- *
+ *
+ *
+ *
* TrueType Font Table Introduction: - * http://scripts.sil.org/cms/scripts/page.php?item_id=IWS-Chapter08 + *
+ * Misc.: + *
Rasterization https://freddie.witherden.org/pages/font-rasterisation/
http://walon.org/pub/ttf/ttf_glyphs.htm
+ Font Scale Formula: + inch: 25.4 mm + pointSize: [point] = [1/72 inch] + + [1] Scale := pointSize * resolution / ( 72 points per inch * units_per_em ) + [2] PixelSize := pointSize * resolution / ( 72 points per inch ) + [3] Scale := PixelSize / units_per_em + *+ * @param fontSize in point-per-inch + * @param resolution display resolution in dots-per-inch + * @return pixel-per-inch, pixelSize scale factor for font operations. + */ + public float getPixelSize(float fontSize /* points per inch */, float resolution); + public float getAdvanceWidth(int glyphID, float pixelSize); public Metrics getMetrics(); public Glyph getGlyph(char symbol); diff --git a/src/jogl/classes/com/jogamp/graph/font/FontFactory.java b/src/jogl/classes/com/jogamp/graph/font/FontFactory.java index d39bda004..948600a4a 100644 --- a/src/jogl/classes/com/jogamp/graph/font/FontFactory.java +++ b/src/jogl/classes/com/jogamp/graph/font/FontFactory.java @@ -29,10 +29,13 @@ package com.jogamp.graph.font; import java.io.File; import java.io.IOException; +import java.net.URI; import java.net.URLConnection; +import com.jogamp.common.util.IOUtil; import com.jogamp.common.util.PropertyAccess; import com.jogamp.common.util.ReflectionUtil; +import com.jogamp.common.util.cache.TempJarCache; import jogamp.graph.font.FontConstructor; import jogamp.graph.font.JavaFontLoader; @@ -92,6 +95,24 @@ public class FontFactory { return fontConstr.create(conn); } + public static final Font get(final Class> context, final String fname, final boolean useTempJarCache) throws IOException { + URLConnection conn = null; + if( useTempJarCache ) { + try { + final URI uri = TempJarCache.getResource(fname); + conn = null != uri ? uri.toURL().openConnection() : null; + } catch (Exception e) { + throw new IOException(e); + } + } else { + conn = IOUtil.getResource(context, fname); + } + if(null != conn) { + return FontFactory.get ( conn ) ; + } + return null; + } + public static boolean isPrintableChar( char c ) { if( Character.isWhitespace(c) ) { return true; diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java index df3be2e06..11309290d 100644 --- a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java +++ b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java @@ -39,6 +39,7 @@ import jogamp.graph.curve.opengl.shader.AttributeNames; import jogamp.graph.curve.opengl.shader.UniformNames; import com.jogamp.common.nio.Buffers; +import com.jogamp.graph.curve.Region; import com.jogamp.graph.curve.opengl.GLRegion; import com.jogamp.graph.curve.opengl.RegionRenderer; import com.jogamp.graph.curve.opengl.RenderState; @@ -50,6 +51,8 @@ import com.jogamp.opengl.util.PMVMatrix; import com.jogamp.opengl.util.glsl.ShaderState; public class VBORegion2PES2 extends GLRegion { + private static final boolean DEBUG_FBO_1 = false; + private static final boolean DEBUG_FBO_2 = false; private GLArrayDataServer verticeTxtAttr; private GLArrayDataServer texCoordTxtAttr; private GLArrayDataServer indicesTxtBuffer; @@ -62,11 +65,13 @@ public class VBORegion2PES2 extends GLRegion { private final PMVMatrix fboPMVMatrix; GLUniformData mgl_fboPMVMatrix; - private int tex_width_c = 0; - private int tex_height_c = 0; + private int fboWidth = 0; + private int fboHeight = 0; GLUniformData mgl_ActiveTexture; GLUniformData mgl_TextureSize; // if GLSL < 1.30 + final int[] maxTexSize = new int[] { -1 } ; + public VBORegion2PES2(final int renderModes, final int textureUnit) { super(renderModes); final int initialElementCount = 256; @@ -132,7 +137,7 @@ public class VBORegion2PES2 extends GLRegion { st.ownAttribute(verticeTxtAttr, true); st.ownAttribute(texCoordTxtAttr, true); - if(DEBUG_INSTANCE) { + if(Region.DEBUG_INSTANCE) { System.err.println("VBORegion2PES2 Create: " + this); } } @@ -161,27 +166,85 @@ public class VBORegion2PES2 extends GLRegion { // push data 2 GPU .. indicesFbo.seal(gl, true); indicesFbo.enableBuffer(gl, false); + + // trigger renderRegion2FBO ! + fboHeight = 0; + fboWidth = 0; // the buffers were disabled, since due to real/fbo switching and other vbo usage } - final int[] maxTexSize = new int[] { -1 } ; - @Override - protected void drawImpl(final GL2ES2 gl, final RegionRenderer renderer, final int[/*1*/] texWidth) { + protected void drawImpl(final GL2ES2 gl, final RegionRenderer renderer, final int[/*1*/] sampleCount) { final int width = renderer.getWidth(); final int height = renderer.getHeight(); - if(width <=0 || height <= 0 || null==texWidth || texWidth[0] <= 0){ + if(width <=0 || height <= 0 || null==sampleCount || sampleCount[0] <= 0){ renderRegion(gl); } else { if(0 > maxTexSize[0]) { gl.glGetIntegerv(GL.GL_MAX_TEXTURE_SIZE, maxTexSize, 0); } final RenderState rs = renderer.getRenderState(); - if(texWidth[0] != tex_width_c) { - if(texWidth[0] > maxTexSize[0]) { - texWidth[0] = maxTexSize[0]; // clip to max - write-back user value! + final float winWidth, winHeight; + int targetFboWidth, targetFboHeight; + { + // Calculate perspective pixel width/height for FBO, + // considering the sampleCount. + final int[] view = new int[] { 0, 0, width, height }; + final float objZ = 0f; + + final float[] winPosSzMin = new float[3]; + final float[] winPosSzMax = new float[3]; + final PMVMatrix pmv = renderer.getMatrix(); + pmv.gluProject(box.getMinX(), box.getMinY(), objZ, view, 0, winPosSzMin, 0); + pmv.gluProject(box.getMaxX(), box.getMaxY(), objZ, view, 0, winPosSzMax, 0); + winWidth = Math.abs(winPosSzMax[0] - winPosSzMin[0]); + winHeight = Math.abs(winPosSzMax[1] - winPosSzMin[1]); + targetFboWidth = Math.round(winWidth*sampleCount[0]); + targetFboHeight= Math.round(winHeight*sampleCount[0]); + if( DEBUG_FBO_2 ) { + System.err.printf("XXX.MinMax1 min [%.1f, %.1f -> %.3f, %.3f, %.3f]"+ + ", max [%.1f, %.1f -> %.3f, %.3f, %.3f], view[%d, %d] -> [%f x %f]%n", + box.getMinX(), box.getMinY(), winPosSzMin[0], winPosSzMin[1], winPosSzMin[2], + box.getMaxX(), box.getMaxY(), winPosSzMax[0], winPosSzMax[1], winPosSzMax[2], + view[2], view[3], winWidth, winHeight); + System.err.printf("XXX.MinMax1 [%f x %f] * %d = %d x %d%n", winWidth, winHeight, sampleCount[0], targetFboWidth, targetFboHeight); + } + } + final int deltaFboWidth = Math.abs(targetFboWidth-fboWidth); + final int deltaFboHeight = Math.abs(targetFboHeight-fboHeight); + final int maxDeltaFbo, maxLengthFbo; + if( deltaFboWidth >= deltaFboHeight ) { + maxDeltaFbo = deltaFboWidth; + maxLengthFbo = fboWidth > 0 ? fboWidth : 1; + } else { + maxDeltaFbo = deltaFboHeight; + maxLengthFbo = fboHeight > 0 ? fboHeight : 1; + } + final float pctFboDelta = (float)maxDeltaFbo / (float)maxLengthFbo; + if( DEBUG_FBO_2 ) { + System.err.printf("XXX.maxDelta: %d / %d = %.3f%n", maxDeltaFbo, maxLengthFbo, pctFboDelta); + } + if( pctFboDelta > 0.1f ) { // more than 10% ! + if( DEBUG_FBO_1 ) { + System.err.printf("XXX.maxDelta: %d / %d = %.3f%n", maxDeltaFbo, maxLengthFbo, pctFboDelta); + System.err.printf("XXX.Scale %d * [%f x %f]: %dx%d%n", + sampleCount[0], winWidth, winHeight, targetFboWidth, targetFboHeight); + } + final int maxLength = Math.max(targetFboWidth, targetFboHeight); + if( maxLength > maxTexSize[0] ) { + if( targetFboWidth > targetFboHeight ) { + sampleCount[0] = (int)Math.floor(maxTexSize[0] / winWidth); + } else { + sampleCount[0] = (int)Math.floor(maxTexSize[0] / winHeight); + } + targetFboWidth = Math.round(winWidth*sampleCount[0]); + targetFboHeight= Math.round(winHeight*sampleCount[0]); + if( DEBUG_FBO_1 ) { + System.err.printf("XXX.Rescale (MAX): %d * [%f x %f]: %dx%d%n", + sampleCount[0], winWidth, winHeight, targetFboWidth, targetFboHeight); + } } - renderRegion2FBO(gl, rs, texWidth); + renderRegion2FBO(gl, rs, targetFboWidth, targetFboHeight); } // System.out.println("Scale: " + matrix.glGetMatrixf().get(1+4*3) +" " + matrix.glGetMatrixf().get(2+4*3)); renderFBO(gl, rs, width, height); @@ -194,6 +257,8 @@ public class VBORegion2PES2 extends GLRegion { gl.glViewport(0, 0, width, hight); st.uniform(gl, mgl_ActiveTexture); gl.glActiveTexture(GL.GL_TEXTURE0 + mgl_ActiveTexture.intValue()); + setTexSize(gl, st); + fbo.use(gl, texA); verticeFboAttr.enableBuffer(gl, true); texCoordFboAttr.enableBuffer(gl, true); @@ -209,36 +274,39 @@ public class VBORegion2PES2 extends GLRegion { // setback: gl.glActiveTexture(currentActiveTextureEngine[0]); } - private void renderRegion2FBO(final GL2ES2 gl, final RenderState rs, final int[/*1*/] texWidth) { + private void renderRegion2FBO(final GL2ES2 gl, final RenderState rs, final int targetFboWidth, final int targetFboHeight) { final ShaderState st = rs.getShaderState(); - if(0>=texWidth[0]) { - throw new IllegalArgumentException("texWidth must be greater than 0: "+texWidth[0]); - } - - tex_width_c = texWidth[0]; - tex_height_c = (int) ( ( ( tex_width_c * box.getHeight() ) / box.getWidth() ) + 0.5f ); - - // System.out.println("FBO Size: "+texWidth[0]+" -> "+tex_width_c+"x"+tex_height_c); - // System.out.println("FBO Scale: " + m.glGetMatrixf().get(0) +" " + m.glGetMatrixf().get(5)); - - if(null != fbo && fbo.getWidth() != tex_width_c && fbo.getHeight() != tex_height_c ) { - fbo.reset(gl, tex_width_c, tex_height_c); + if( 0 >= targetFboWidth || 0 >= targetFboHeight ) { + throw new IllegalArgumentException("fboSize must be greater than 0: "+targetFboWidth+"x"+targetFboHeight); } if(null == fbo) { + fboWidth = targetFboWidth; + fboHeight = targetFboHeight; fbo = new FBObject(); - fbo.reset(gl, tex_width_c, tex_height_c); + fbo.reset(gl, fboWidth, fboHeight); // FIXME: shall not use bilinear, due to own AA ? However, w/o bilinear result is not smooth texA = fbo.attachTexture2D(gl, 0, true, GL2ES2.GL_LINEAR, GL2ES2.GL_LINEAR, GL2ES2.GL_CLAMP_TO_EDGE, GL2ES2.GL_CLAMP_TO_EDGE); // texA = fbo.attachTexture2D(gl, 0, GL2ES2.GL_NEAREST, GL2ES2.GL_NEAREST, GL2ES2.GL_CLAMP_TO_EDGE, GL2ES2.GL_CLAMP_TO_EDGE); fbo.attachRenderbuffer(gl, Attachment.Type.DEPTH, 24); + if( DEBUG_FBO_1 ) { + System.err.printf("XXX.createFBO: %dx%d%n%s%n", fboWidth, fboHeight, fbo.toString()); + } + } else if( targetFboWidth != fboWidth || targetFboHeight != fboHeight ) { + fbo.reset(gl, targetFboWidth, targetFboHeight); + fbo.bind(gl); + if( DEBUG_FBO_1 ) { + System.err.printf("XXX.resetFBO: %dx%d -> %dx%d%n%s%n", fboWidth, fboHeight, targetFboWidth, targetFboHeight, fbo ); + } + fboWidth = targetFboWidth; + fboHeight = targetFboHeight; } else { fbo.bind(gl); } //render texture - gl.glViewport(0, 0, tex_width_c, tex_height_c); + gl.glViewport(0, 0, fboWidth, fboHeight); st.uniform(gl, mgl_fboPMVMatrix); // use orthogonal matrix gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); @@ -247,15 +315,16 @@ public class VBORegion2PES2 extends GLRegion { fbo.unbind(gl); st.uniform(gl, rs.getPMVMatrix()); // switch back to real PMV matrix - + } + private void setTexSize(final GL2ES2 gl, final ShaderState st) { // if( !gl.isGL3() ) { // GLSL < 1.30 if(null == mgl_TextureSize) { mgl_TextureSize = new GLUniformData(UniformNames.gcu_TextureSize, 2, Buffers.newDirectFloatBuffer(2)); } final FloatBuffer texSize = (FloatBuffer) mgl_TextureSize.getBuffer(); - texSize.put(0, fbo.getWidth()); - texSize.put(1, fbo.getHeight()); + texSize.put(0, fboWidth); + texSize.put(1, fboHeight); st.uniform(gl, mgl_TextureSize); //} } diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegionSPES2.java b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegionSPES2.java index f4c40b0e5..bdf824b6a 100644 --- a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegionSPES2.java +++ b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegionSPES2.java @@ -103,7 +103,7 @@ public class VBORegionSPES2 extends GLRegion { } @Override - protected void drawImpl(final GL2ES2 gl, final RegionRenderer renderer, final int[/*1*/] texWidth) { + protected void drawImpl(final GL2ES2 gl, final RegionRenderer renderer, final int[/*1*/] sampleCount) { verticeAttr.enableBuffer(gl, true); texCoordAttr.enableBuffer(gl, true); indicesBuffer.bindBuffer(gl, true); // keeps VBO binding diff --git a/src/jogl/classes/jogamp/graph/font/UbuntuFontLoader.java b/src/jogl/classes/jogamp/graph/font/UbuntuFontLoader.java index e1e44c92c..c7efe143b 100644 --- a/src/jogl/classes/jogamp/graph/font/UbuntuFontLoader.java +++ b/src/jogl/classes/jogamp/graph/font/UbuntuFontLoader.java @@ -38,9 +38,7 @@ import com.jogamp.graph.font.Font; import com.jogamp.graph.font.FontSet; import com.jogamp.graph.font.FontFactory; -import java.net.MalformedURLException; import java.net.URI; -import java.net.URISyntaxException; import java.net.URLConnection; import java.security.AccessController; import java.security.PrivilegedAction; diff --git a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java index 8dd9ce4d7..ae9c43ec5 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java @@ -221,6 +221,11 @@ class TypecastFont implements Font { return result; } + @Override + public final float getPixelSize(float fontSize /* points per inch */, float resolution) { + return fontSize * resolution / ( 72 /* points per inch */ ); + } + @Override public float getLineHeight(float pixelSize) { final Metrics metrics = getMetrics(); -- cgit v1.2.3