diff options
Diffstat (limited to 'src')
3 files changed, 70 insertions, 66 deletions
diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java index 714be1998..fb77775ad 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java +++ b/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java @@ -37,8 +37,7 @@ import com.jogamp.opengl.math.geom.AABBox; import com.jogamp.graph.curve.OutlineShape; import com.jogamp.graph.curve.Region; import com.jogamp.graph.font.Font; -import com.jogamp.graph.geom.Vertex; -import com.jogamp.graph.geom.Vertex.Factory; +import com.jogamp.graph.font.Font.Glyph; import com.jogamp.graph.geom.plane.AffineTransform; /** @@ -67,28 +66,53 @@ public class TextRegionUtil { } /** - * Add the string in 3D space w.r.t. the font in font em-size [0..1] at the end of the {@link GLRegion}. + * Add the string in 3D space w.r.t. the font in font em-size [0..1] at the end of the {@link GLRegion} + * while passing the progressed {@link AffineTransform}. * <p> - * The shapes added to the GLRegion are in font em-size [0..1]. + * The shapes added to the GLRegion are in font em-size [0..1], but can be adjusted with the given transform, progressed and passed to the visitor. + * </p> + * <p> + * Origin of rendered text is 0/0 at bottom left. + * </p> + * @param region the {@link GLRegion} sink + * @param font the target {@link Font} + * @param transform optional given transform + * @param str string text + * @param rgbaColor if {@link Region#hasColorChannel()} RGBA color must be passed, otherwise value is ignored. + * @return the bounding box of the given string by taking each glyph's font em-sized [0..1] OutlineShape into account. + */ + public static AABBox addStringToRegion(final Region region, final Font font, final AffineTransform transform, + final CharSequence str, final float[] rgbaColor) { + return addStringToRegion(region, font, transform, str, rgbaColor, new AffineTransform(), new AffineTransform()); + } + + /** + * Add the string in 3D space w.r.t. the font in font em-size [0..1] at the end of the {@link GLRegion} + * while passing the progressed {@link AffineTransform}. + * <p> + * The shapes added to the GLRegion are in font em-size [0..1], but can be adjusted with the given transform, progressed and passed to the visitor. + * </p> + * <p> + * Origin of rendered text is 0/0 at bottom left. * </p> * @param region the {@link GLRegion} sink - * @param vertexFactory vertex impl factory {@link Factory} * @param font the target {@link Font} + * @param transform optional given transform * @param str string text * @param rgbaColor if {@link Region#hasColorChannel()} RGBA color must be passed, otherwise value is ignored. * @param temp1 temporary AffineTransform storage, mandatory * @param temp2 temporary AffineTransform storage, mandatory * @return the bounding box of the given string by taking each glyph's font em-sized [0..1] OutlineShape into account. */ - public static AABBox addStringToRegion(final GLRegion region, final Factory<? extends Vertex> vertexFactory, - final Font font, final CharSequence str, final float[] rgbaColor, + public static AABBox addStringToRegion(final Region region, final Font font, final AffineTransform transform, + final CharSequence str, final float[] rgbaColor, final AffineTransform temp1, final AffineTransform temp2) { final OutlineShape.Visitor visitor = new OutlineShape.Visitor() { @Override public final void visit(final OutlineShape shape, final AffineTransform t) { region.addOutlineShape(shape, t, region.hasColorChannel() ? rgbaColor : null); } }; - return font.processString(visitor, null, str, temp1, temp2); + return font.processString(visitor, transform, str, temp1, temp2); } /** @@ -97,6 +121,9 @@ public class TextRegionUtil { * The shapes added to the GLRegion are in font em-size [0..1]. * </p> * <p> + * Origin of rendered text is 0/0 at bottom left. + * </p> + * <p> * Cached {@link GLRegion}s will be destroyed w/ {@link #clear(GL2ES2)} or to free memory. * </p> * @param gl the current GL state @@ -117,13 +144,15 @@ public class TextRegionUtil { } final int special = 0; GLRegion region = getCachedRegion(font, str, special); + AABBox res; if(null == region) { region = GLRegion.create(renderModes, null); - addStringToRegion(region, renderer.getRenderState().getVertexFactory(), font, str, rgbaColor, tempT1, tempT2); + res = addStringToRegion(region, font, null, str, rgbaColor, tempT1, tempT2); addCachedRegion(gl, font, str, special, region); + } else { + res = new AABBox(); + res.copy(region.getBounds()); } - final AABBox res = new AABBox(); - res.copy(region.getBounds()); region.draw(gl, renderer, sampleCount); return res; } @@ -134,6 +163,9 @@ public class TextRegionUtil { * The shapes added to the GLRegion are in font em-size [0..1]. * </p> * <p> + * Origin of rendered text is 0/0 at bottom left. + * </p> + * <p> * 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(GL2ES2, GLRegion, RegionRenderer, Font, CharSequence, float[], int[])} @@ -155,12 +187,8 @@ public class TextRegionUtil { if(!renderer.isInitialized()){ throw new GLException("TextRendererImpl01: not initialized!"); } - final AffineTransform temp1 = new AffineTransform(); - final AffineTransform temp2 = new AffineTransform(); final GLRegion region = GLRegion.create(renderModes, null); - addStringToRegion(region, renderer.getRenderState().getVertexFactory(), font, str, rgbaColor, temp1, temp2); - final AABBox res = new AABBox(); - res.copy(region.getBounds()); + final AABBox res = addStringToRegion(region, font, null, str, rgbaColor); region.draw(gl, renderer, sampleCount); region.destroy(gl); return res; @@ -172,6 +200,9 @@ public class TextRegionUtil { * <p> * The shapes added to the GLRegion are in font em-size [0..1]. * </p> + * <p> + * Origin of rendered text is 0/0 at bottom left. + * </p> * @param gl the current GL state * @param font {@link Font} to be used * @param str text to be rendered @@ -187,12 +218,8 @@ public class TextRegionUtil { if(!renderer.isInitialized()){ throw new GLException("TextRendererImpl01: not initialized!"); } - final AffineTransform temp1 = new AffineTransform(); - final AffineTransform temp2 = new AffineTransform(); region.clear(gl); - addStringToRegion(region, renderer.getRenderState().getVertexFactory(), font, str, rgbaColor, temp1, temp2); - final AABBox res = new AABBox(); - res.copy(region.getBounds()); + final AABBox res = addStringToRegion(region, font, null, str, rgbaColor); region.draw(gl, renderer, sampleCount); return res; } diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/Label0.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/Label0.java index c30c75959..5cf0f9af5 100644 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/Label0.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/Label0.java @@ -27,8 +27,8 @@ */ package com.jogamp.opengl.test.junit.graph.demos.ui; -import com.jogamp.graph.curve.OutlineShape; import com.jogamp.graph.curve.Region; +import com.jogamp.graph.curve.opengl.TextRegionUtil; import com.jogamp.graph.font.Font; import com.jogamp.graph.geom.plane.AffineTransform; import com.jogamp.opengl.math.geom.AABBox; @@ -37,13 +37,11 @@ public class Label0 { protected Font font; protected String text; protected final float[] rgbaColor; - protected final AABBox box; public Label0(final Font font, final String text, final float[] rgbaColor) { this.font = font; this.text = text; this.rgbaColor = rgbaColor; - this.box = new AABBox(); } public final String getText() { return text; } @@ -67,31 +65,10 @@ public class Label0 { this.font = font; } - public final AABBox getBounds() { return box; } - - private final float[] tmpV3 = new float[3]; - private final AffineTransform tempT1 = new AffineTransform(); - private final AffineTransform tempT2 = new AffineTransform(); - - private final OutlineShape.Visitor shapeVisitor = new OutlineShape.Visitor() { - @Override - public void visit(final OutlineShape shape, final AffineTransform t) { - region.addOutlineShape(shape, t, rgbaColor); - box.resize(shape.getBounds(), t, tmpV3); - } - }; - - private Region region; - - public final AABBox addShapeToRegion(final float pixelSize, final Region region, final AffineTransform tLeft) { - box.reset(); - this.region = region; + public final AABBox addShapeToRegion(final float scale, final Region region, final AffineTransform tLeft) { final AffineTransform t_sxy = new AffineTransform(tLeft); - final AffineTransform tmp = new AffineTransform(); - t_sxy.scale(pixelSize, pixelSize, tmp); - font.processString(shapeVisitor, t_sxy, text, tempT1, tempT2); - this.region = null; - return box; + t_sxy.scale(scale, scale, new AffineTransform()); + return TextRegionUtil.addStringToRegion(region, font, t_sxy, text, rgbaColor); } @Override diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/LabelButton.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/LabelButton.java index db1ec239a..4d62cb2b1 100644 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/LabelButton.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/LabelButton.java @@ -47,7 +47,7 @@ public class LabelButton extends RoundButton { /** {@value} */ public static final float DEFAULT_SPACING_Y = 0.40f; - private static final float DEFAULT_2PASS_LABEL_ZOFFSET = -0.05f; + private static final float DEFAULT_2PASS_LABEL_ZOFFSET = -0.005f; // -0.05f; private final Label0 label; private float spacingX = DEFAULT_SPACING_X; @@ -64,6 +64,8 @@ public class LabelButton extends RoundButton { setToggleOnColorMod(0.85f, 0.85f, 0.85f, 1.0f); } + public Font getFont() { return label.font; } + @Override public void drawShape(final GL2ES2 gl, final RegionRenderer renderer, final int[] sampleCount) { if( false ) { @@ -91,35 +93,33 @@ public class LabelButton extends RoundButton { box.resize(shape.getBounds()); // Precompute text-box size .. guessing pixelSize - final float lPixelSize0 = 10f; final float lw = width * ( 1f - spacingX ) ; final float lh = height * ( 1f - spacingY ) ; - final AABBox lbox0_em = label.font.getMetricBounds(label.text); + final AABBox lbox0_em = label.font.getPointsBounds(null, label.text); final float lsx = lw / lbox0_em.getWidth(); final float lsy = lh / lbox0_em.getHeight(); - final float lPixelSize1 = lsx < lsy ? lsx : lsy; - if( DRAW_DEBUG_BOX ) { - final AABBox lbox0_px = new AABBox(lbox0_em).scale2(lPixelSize0, new float[3]); - System.err.println("RIButton: dim "+width+" x "+height+", spacing "+spacingX+", "+spacingY); - System.err.println("RIButton: net-text "+lw+" x "+lh+" px"); - System.err.println("RIButton: shape "+box+" px"); - System.err.println("RIButton: text "+lbox0_em+" em, "+label.text); - System.err.println("RIButton: text "+lbox0_px+" px, "+label.text); - System.err.println("RIButton: lscale "+lsx+" x "+lsy+": pixelSize "+lPixelSize0+" -> "+lPixelSize1); - } + final float lScale = lsx < lsy ? lsx : lsy; // Setting left-corner transform using text-box in font em-size [0..1] - final AABBox lbox1_s = label.font.getPointsBounds(null, label.text).scale2(lPixelSize1, new float[3]); + final AABBox lbox1_s = new AABBox(lbox0_em).scale2(lScale, new float[3]); // Center text .. (share same center w/ button) final float[] lctr = lbox1_s.getCenter(); final float[] ctr = box.getCenter(); final float[] ltx = new float[] { ctr[0] - lctr[0], ctr[1] - lctr[1], 0f }; - final AABBox lbox2 = label.addShapeToRegion(lPixelSize1, region, tempT1.setToTranslation(ltx[0], ltx[1])); if( DRAW_DEBUG_BOX ) { - System.err.printf("RIButton.0: tleft %f / %f, %f / %f%n", ltx[0], ltx[1], ltx[0] * lPixelSize1, ltx[1] * lPixelSize1); - System.err.printf("RIButton.0: lbox1 %s scaled%n", lbox1_s); - System.err.printf("RIButton.0: lbox2 %s%n", lbox2); + System.err.println("RIButton: dim "+width+" x "+height+", spacing "+spacingX+", "+spacingY); + System.err.println("RIButton: net-text "+lw+" x "+lh); + System.err.println("RIButton: shape "+box); + System.err.println("RIButton: text_em "+lbox0_em+" em, "+label.text); + System.err.println("RIButton: lscale "+lsx+" x "+lsy+" -> "+lScale); + System.err.printf ("RIButton: text_s %s%n", lbox1_s); + System.err.printf ("RIButton: tleft %f / %f, %f / %f%n", ltx[0], ltx[1], ltx[0] * lScale, ltx[1] * lScale); + } + + final AABBox lbox2 = label.addShapeToRegion(lScale, region, tempT1.setToTranslation(ltx[0], ltx[1])); + if( DRAW_DEBUG_BOX ) { + System.err.printf("RIButton.X: lbox2 %s%n", lbox2); } setRotationOrigin( ctr[0], ctr[1], ctr[2]); |