diff options
author | Sven Gothel <[email protected]> | 2023-02-19 05:12:04 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-02-19 05:12:04 +0100 |
commit | da88b0f2ac0b09082222ce1a387987e8cdd5f21d (patch) | |
tree | 67772067a0b02192a0763b55e5a44c27d56b4660 /src/test/com/jogamp/opengl | |
parent | 3d2b65bd43d5abb1acf59754ee325c25b8c299ff (diff) |
Graph Font.Metrics: Use unchanges sign of (+) ascent, (-) descent and (+) lineGap, document them properly and fix Font.getLineHeightFU()
Diffstat (limited to 'src/test/com/jogamp/opengl')
-rw-r--r-- | src/test/com/jogamp/opengl/test/junit/graph/TestTextRendererNEWT00.java | 285 | ||||
-rw-r--r-- | src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/av/MovieCube.java | 6 |
2 files changed, 288 insertions, 3 deletions
diff --git a/src/test/com/jogamp/opengl/test/junit/graph/TestTextRendererNEWT00.java b/src/test/com/jogamp/opengl/test/junit/graph/TestTextRendererNEWT00.java new file mode 100644 index 000000000..07798e2ee --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/graph/TestTextRendererNEWT00.java @@ -0,0 +1,285 @@ +/** + * Copyright 2011-2023 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package com.jogamp.opengl.test.junit.graph; + +import java.io.File; +import java.io.IOException; +import java.util.Locale; + +import com.jogamp.opengl.GL; +import com.jogamp.opengl.GL2ES2; +import com.jogamp.opengl.GLCapabilities; +import com.jogamp.opengl.GLDrawable; +import com.jogamp.opengl.GLException; +import com.jogamp.opengl.GLProfile; +import com.jogamp.opengl.fixedfunc.GLMatrixFunc; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +import com.jogamp.common.util.IOUtil; +import com.jogamp.graph.curve.Region; +import com.jogamp.graph.curve.opengl.RenderState; +import com.jogamp.graph.curve.opengl.GLRegion; +import com.jogamp.graph.curve.opengl.RegionRenderer; +import com.jogamp.graph.curve.opengl.TextRegionUtil; +import com.jogamp.graph.font.Font; +import com.jogamp.graph.font.FontFactory; +import com.jogamp.graph.font.FontScale; +import com.jogamp.graph.geom.SVertex; +import com.jogamp.opengl.math.geom.AABBox; +import com.jogamp.opengl.test.junit.util.MiscUtils; +import com.jogamp.opengl.test.junit.util.NEWTGLContext; +import com.jogamp.opengl.test.junit.util.UITestCase; +import com.jogamp.opengl.util.GLReadBufferUtil; +import com.jogamp.opengl.util.PMVMatrix; + + +/** + * TestTextRendererNEWT00 Variant + * - No listener, all straight forward + * - Type Rendering via TextRegionUtil + * - Type Rendering vanilla via GLRegion, GLRegion.addOutlineShape(Font.processString()), .. + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestTextRendererNEWT00 extends UITestCase { + static final boolean DEBUG = false; + static final boolean TRACE = false; + static long duration = 100; // ms + static boolean forceES2 = false; + static boolean forceGL3 = false; + static boolean mainRun = false; + static boolean useMSAA = true; + static int win_width = 1024; + static int win_height = 640; + + static Font font; + static float fontSize = 24; // in pixel + + @BeforeClass + public static void setup() throws IOException { + if( null == font ) { + font = FontFactory.get(FontFactory.UBUNTU).getDefault(); + } + } + + static int atoi(final String a) { + try { + return Integer.parseInt(a); + } catch (final Exception ex) { throw new RuntimeException(ex); } + } + + public static void main(final String args[]) throws IOException { + mainRun = true; + for(int i=0; i<args.length; i++) { + if(args[i].equals("-time")) { + i++; + duration = atoi(args[i]); + } else if(args[i].equals("-width")) { + i++; + win_width = atoi(args[i]); + } else if(args[i].equals("-height")) { + i++; + win_height = atoi(args[i]); + } else if(args[i].equals("-noMSAA")) { + useMSAA = false; + } else if(args[i].equals("-es2")) { + forceES2 = true; + } else if(args[i].equals("-gl3")) { + forceGL3 = true; + } else if(args[i].equals("-font")) { + i++; + font = FontFactory.get(IOUtil.getResource(args[i], TestTextRendererNEWT00.class.getClassLoader(), TestTextRendererNEWT00.class).getInputStream(), true); + } else if(args[i].equals("-fontSize")) { + i++; + fontSize = MiscUtils.atof(args[i], fontSize); + } + } + final String tstname = TestTextRendererNEWT00.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } + + static void sleep() { + try { + System.err.println("** new frame ** (sleep: "+duration+"ms)"); + Thread.sleep(duration); + } catch (final InterruptedException ie) {} + } + + @Test + public void test02TextRendererVBAA04() throws InterruptedException, GLException, IOException { + final int renderModes = Region.VBAA_RENDERING_BIT; + final int sampleCount = 4; + final GLProfile glp; + if(forceGL3) { + glp = GLProfile.get(GLProfile.GL3); + } else if(forceES2) { + glp = GLProfile.get(GLProfile.GLES2); + } else { + glp = GLProfile.getGL2ES2(); + } + + final GLCapabilities caps = new GLCapabilities( glp ); + caps.setAlphaBits(4); + System.err.println("Requested: "+caps); + System.err.println("Requested: "+Region.getRenderModeString(renderModes)); + + final NEWTGLContext.WindowContext winctx = NEWTGLContext.createWindow(caps, win_width, win_height, true); + final GLDrawable drawable = winctx.context.getGLDrawable(); + final GL2ES2 gl = winctx.context.getGL().getGL2ES2(); + + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + + System.err.println("Chosen: "+winctx.window.getChosenCapabilities()); + + final RenderState rs = RenderState.createRenderState(SVertex.factory()); + final RegionRenderer renderer = RegionRenderer.create(rs, RegionRenderer.defaultBlendEnable, RegionRenderer.defaultBlendDisable); + rs.setHintMask(RenderState.BITHINT_GLOBAL_DEPTH_TEST_ENABLED); + final TextRegionUtil textRenderUtil = new TextRegionUtil(renderModes); + + // init + gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + renderer.init(gl, 0); + rs.setColorStatic(0.1f, 0.1f, 0.1f, 1.0f); + screenshot = new GLReadBufferUtil(false, false); + + // reshape + gl.glViewport(0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight()); + + // renderer.reshapePerspective(gl, 45.0f, drawable.getWidth(), drawable.getHeight(), 0.1f, 1000.0f); + renderer.reshapeOrtho(drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), 0.1f, 1000.0f); + + final int[] sampleCountIO = { sampleCount }; + // display + gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + { + { + final float[] pixelsPerMM = winctx.window.getPixelsPerMM(new float[2]); + final float[] dpi = FontScale.ppmmToPPI(pixelsPerMM, new float[2]); + final float mmSize = fontSize / pixelsPerMM[1]; + final int unitsPerEM = font.getMetrics().getUnitsPerEM(); + String txt = String.format("Resolution dpiV %.2f, %.2f px/mm", dpi[1], pixelsPerMM[1]); + renderString(drawable, gl, renderer, textRenderUtil, txt, 0, 0, -1000, sampleCountIO); + txt = String.format("Font %s, unitsPerEM %d, size %.2f px %2f mm", font.getFullFamilyName(), unitsPerEM, fontSize, mmSize); + renderString(drawable, gl, renderer, textRenderUtil, txt, 0, -1, -1000, sampleCountIO); + } + { + final AABBox txt_box_em = glyph.getBBox(); + final float full_width_s = full_width_o / txt_box_em.getWidth(); + final float full_height_s = full_height_o / txt_box_em.getHeight(); + final float txt_scale = full_width_s < full_height_s ? full_width_s/2f : full_height_s/2f; + pmv.glPushMatrix(); + pmv.glScalef(txt_scale, txt_scale, 1f); + pmv.glTranslatef(-txt_box_em.getWidth(), 0f, 0f); + { + final GLRegion region = GLRegion.create(renderModes, null); + region.addOutlineShape(glyph.getShape(), null, region.hasColorChannel() ? fg_color : null); + region.draw(gl, renderer, sampleCount); + region.destroy(gl); + } + if( once ) { + final AABBox txt_box_em2 = font.getGlyphShapeBounds(null, text); + System.err.println("XXX: full_width: "+full_width_o+" / "+txt_box_em.getWidth()+" -> "+full_width_s); + System.err.println("XXX: full_height: "+full_height_o+" / "+txt_box_em.getHeight()+" -> "+full_height_s); + System.err.println("XXX: txt_scale: "+txt_scale); + System.err.println("XXX: txt_box_em "+txt_box_em); + System.err.println("XXX: txt_box_e2 "+txt_box_em2); + once = false; + } + } + renderString(drawable, gl, renderer, textRenderUtil, "012345678901234567890123456789", 0, -1, -1000, sampleCountIO); + renderString(drawable, gl, renderer, textRenderUtil, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 0, -1, -1000, sampleCountIO); + renderString(drawable, gl, renderer, textRenderUtil, "Hello World", 0, -1, -1000, sampleCountIO); + renderString(drawable, gl, renderer, textRenderUtil, "4567890123456", 4, -1, -1000,sampleCountIO); + renderString(drawable, gl, renderer, textRenderUtil, "I like JogAmp", 4, -1, -1000, sampleCountIO); + + int c = 0; + renderString(drawable, gl, renderer, textRenderUtil, "GlueGen", c++, -1, -1000, sampleCountIO); + renderString(drawable, gl, renderer, textRenderUtil, "JOAL", c++, -1, -1000, sampleCountIO); + renderString(drawable, gl, renderer, textRenderUtil, "JOGL", c++, -1, -1000, sampleCountIO); + renderString(drawable, gl, renderer, textRenderUtil, "JOCL", c++, -1, -1000, sampleCountIO); + } + gl.glFinish(); + printScreen(renderModes, drawable, gl, false, sampleCount); + drawable.swapBuffers(); + + sleep(); + + // dispose + screenshot.dispose(gl); + renderer.destroy(gl); + + NEWTGLContext.destroyWindow(winctx); + } + private boolean once = true; + + private GLReadBufferUtil screenshot; + int lastRow = -1; + + void renderString(final GLDrawable drawable, final GL2ES2 gl, final RegionRenderer renderer, final TextRegionUtil textRenderUtil, final String text, final int column, int row, final int z0, final int[] sampleCount) { + final int height = drawable.getSurfaceHeight(); + + float dx = 0; + float dy = height; + if(0>row) { + row = lastRow + 1; + } + final AABBox textBox = font.getMetricBounds(text); // em-size + dx += fontSize * font.getAdvanceWidth('X') * column; + dy -= fontSize * textBox.getHeight() * ( row + 1 ); + + final PMVMatrix pmv = renderer.getMatrix(); + pmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW); + pmv.glLoadIdentity(); + pmv.glTranslatef(dx, dy, z0); + pmv.glScalef(fontSize, fontSize, 1.0f); + textRenderUtil.drawString3D(gl, renderer, font, text, null, sampleCount); + + lastRow = row; + } + + private int screenshot_num = 0; + + public void printScreen(final int renderModes, final GLDrawable drawable, final GL gl, final boolean exportAlpha, final int sampleCount) throws GLException, IOException { + final String dir = "./"; + final String objName = getSimpleTestName(".")+"-snap"+screenshot_num; + screenshot_num++; + final String modeS = Region.getRenderModeString(renderModes); + final String bname = String.format((Locale)null, "%s-msaa%02d-fontsz%02.1f-%03dx%03d-%s%04d", objName, + drawable.getChosenGLCapabilities().getNumSamples(), + fontSize, drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), modeS, sampleCount); + final String filename = dir + bname +".png"; + if(screenshot.readPixels(gl, false)) { + screenshot.write(new File(filename)); + } + } + +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/av/MovieCube.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/av/MovieCube.java index f4b0540fa..21cc6b7ef 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/av/MovieCube.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/av/MovieCube.java @@ -203,9 +203,9 @@ public class MovieCube implements GLEventListener { pixelScale = 1.0f / ( pixelSize1 * 20f ); // underlineSize: 'underline' amount of pixel below 0/0 (Note: lineGap is negative) final Font.Metrics metrics = font.getMetrics(); - final float lineGap = pixelSize1 * metrics.getScale( metrics.getLineGapFU() ); - final float descent = pixelSize1 * metrics.getScale( metrics.getDescentFU() ); - underlineSize = descent - lineGap; + final float lineGap = pixelSize1 * metrics.getLineGap(); + final float descent = pixelSize1 * metrics.getDescent(); + underlineSize = lineGap - descent; System.err.println("XXX: dpiH "+dpiH+", fontSize "+fontSize1+", pixelSize "+pixelSize1+", pixelScale "+pixelScale+", fLG "+lineGap+", fDesc "+descent+", underlineSize "+underlineSize); } |