diff options
5 files changed, 325 insertions, 33 deletions
diff --git a/src/jogl/classes/com/jogamp/graph/font/Font.java b/src/jogl/classes/com/jogamp/graph/font/Font.java index fdef6a612..20986795b 100644 --- a/src/jogl/classes/com/jogamp/graph/font/Font.java +++ b/src/jogl/classes/com/jogamp/graph/font/Font.java @@ -81,42 +81,50 @@ public interface Font { */ public interface Metrics { /** - * @return ascent in font-units, sourced from `hheaTable' table. + * Distance from baseline of highest ascender, a positive value. + * @return ascent in font-units, sourced from `hhea' table. */ int getAscentFU(); /** - * @return ascent in font em-size [0..1], sourced from `hheaTable' table. + * Distance from baseline of highest ascender, a positive value. + * @return ascent in font em-size [0..1], sourced from `hhea' table. */ float getAscent(); /** - * @return descent in font-units, sourced from `hheaTable' table. + * Distance from baseline of lowest descender, a negative value. + * @return descent in font-units, sourced from `hhea' table. */ int getDescentFU(); /** - * @return descend in font em-size [0..1], sourced from `hheaTable' table. + * Distance from baseline of lowest descender, a negative value. + * @return descend in font em-size [0..1], sourced from `hhea' table. */ float getDescent(); /** - * @return line-gap in font-units, sourced from `hheaTable' table. + * Typographic line gap, a positive value. + * @return line-gap in font-units, sourced from `hhea' table. */ int getLineGapFU(); /** - * @return line-gap in font em-size [0..1], sourced from `hheaTable' table. + * Typographic line gap, a positive value. + * @return line-gap in font em-size [0..1], sourced from `hhea' table. */ float getLineGap(); /** - * @return max-extend in font-units, sourced from `hheaTable' table. + * max(lsb + (xMax-xMin)), a positive value. + * @return max-extend in font-units, sourced from `hhea' table. */ int getMaxExtendFU(); /** - * @return max-extend in font em-size [0..1], sourced from `hheaTable' table. + * max(lsb + (xMax-xMin)), a positive value. + * @return max-extend in font em-size [0..1], sourced from `hhea' table. */ float getMaxExtend(); @@ -203,7 +211,7 @@ public interface Font { /** Return advance in font units, sourced from `hmtx` table. */ int getAdvanceFU(); - /** Return advance in font em-size [0..1] */ + /** Return advance in font em-size [0..1], sourced from `hmtx` table. */ float getAdvance(); /** True if kerning values are horizontal, otherwise vertical */ @@ -258,7 +266,7 @@ public interface Font { int getAdvanceWidthFU(final int glyphID); /** - * Return advance-width of given glyphID in font em-size [0..1] + * Return advance-width of given glyphID in font em-size [0..1], sourced from `hmtx` table. * @param glyphID */ float getAdvanceWidth(final int glyphID); @@ -272,17 +280,20 @@ public interface Font { int getNumGlyphs(); /** - * Return line height in font-units, composed from `hheaTable' table entries. + * Return line height, baseline-to-baseline in font-units, composed from `hhea' table entries. * <pre> - * return abs(lineGap) + abs(descent) abs(ascent); + * return ascent - descent + linegap; * </pre> * or * <pre> - * // lineGap negative value - * // descent positive value - * // ascent negative value - * return -1 * ( lineGap - descent + ascent ); + * // lineGap positive value + * // descent negative value + * // ascent positive value + * return ascent - descent + linegap; * </pre> + * @see Metrics#getAscentFU() + * @see Metrics#getDescentFU() + * @see Metrics#getLineGapFU() */ int getLineHeightFU(); @@ -294,7 +305,7 @@ public interface Font { /** * Returns metric-bounds in font-units. * <p> - * Metric bounds is based on the `hmtx` table's advance of each glyph and `hheaTable' composed line height. + * Metric bounds is based on the `hmtx` table's advance of each glyph and `hhea' composed line height. * </p> * <p> * For accurate layout consider using {@link #getGlyphBoundsFU(CharSequence)}. @@ -307,7 +318,7 @@ public interface Font { /** * Returns metric-bounds in font em-size. * <p> - * Metric bounds is based on the `hmtx` table's advance of each glyph and `hheaTable' composed line height. + * Metric bounds is based on the `hmtx` table's advance of each glyph and `hhea' composed line height. * </p> * <p> * For accurate layout consider using {@link #getGlyphBounds(CharSequence)}. @@ -321,7 +332,7 @@ public interface Font { /** * Returns accurate bounding box by taking each glyph's font em-sized bounding box into account. * <p> - * Glyph bounds is based on each glyph's bounding box and `hheaTable' composed line height. + * Glyph bounds is based on each glyph's bounding box and `hhea' composed line height. * </p> * @param string string text * @return the bounding box of the given string in font em-size [0..1] @@ -334,7 +345,7 @@ public interface Font { /** * Returns accurate bounding box by taking each glyph's font-units sized bounding box into account. * <p> - * Glyph bounds is based on each glyph's bounding box and `hheaTable' composed line height. + * Glyph bounds is based on each glyph's bounding box and `hhea' composed line height. * </p> * @param string string text * @return the bounding box of the given string in font-units [0..1] @@ -345,7 +356,7 @@ public interface Font { /** * Returns accurate bounding box by taking each glyph's font em-sized {@link OutlineShape} into account. * <p> - * Glyph shape bounds is based on each glyph's {@link OutlineShape} and `hheaTable' composed line height. + * Glyph shape bounds is based on each glyph's {@link OutlineShape} and `hhea' composed line height. * </p> * <p> * This method is only exposed to validate the produced {@link OutlineShape} against {@link #getGlyphBounds(CharSequence)}. @@ -362,7 +373,7 @@ public interface Font { /** * Returns accurate bounding box by taking each glyph's font em-sized {@link OutlineShape} into account. * <p> - * Glyph shape bounds is based on each glyph's {@link OutlineShape} and `hheaTable' composed line height. + * Glyph shape bounds is based on each glyph's {@link OutlineShape} and `hhea' composed line height. * </p> * <p> * This method is only exposed to validate the produced {@link OutlineShape} against {@link #getGlyphBounds(CharSequence)}. diff --git a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java index f5358b74b..90cc725e0 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java @@ -253,11 +253,7 @@ class TypecastFont implements Font { @Override public int getLineHeightFU() { final Metrics metrics = getMetrics(); - final int lineGap = metrics.getLineGapFU() ; // negative value! - final int descent = metrics.getDescentFU() ; // positive value! - final int ascent = metrics.getAscentFU() ; // negative value! - final int advanceY = lineGap - descent + ascent; // negative value! - return -advanceY; + return metrics.getAscentFU() - metrics.getDescentFU() + metrics.getLineGapFU(); } @Override diff --git a/src/jogl/classes/jogamp/graph/font/typecast/TypecastHMetrics.java b/src/jogl/classes/jogamp/graph/font/typecast/TypecastHMetrics.java index cae8b2755..a9fd32e9d 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/TypecastHMetrics.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/TypecastHMetrics.java @@ -65,7 +65,7 @@ final class TypecastHMetrics implements Metrics { @Override public int getAscentFU() { - return -hheaTable.getAscender(); // inverted + return hheaTable.getAscender(); } @Override @@ -75,7 +75,7 @@ final class TypecastHMetrics implements Metrics { @Override public int getDescentFU() { - return -hheaTable.getDescender(); // inverted + return hheaTable.getDescender(); } @Override @@ -85,7 +85,7 @@ final class TypecastHMetrics implements Metrics { @Override public int getLineGapFU() { - return -hheaTable.getLineGap(); // inverted + return hheaTable.getLineGap(); } @Override 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); } |