JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
GlyphShape.java
Go to the documentation of this file.
1/**
2 * Copyright 2010-2023 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.graph.ui.shapes;
29
30import java.util.List;
31
32import com.jogamp.graph.curve.OutlineShape;
33import com.jogamp.graph.curve.Region;
34import com.jogamp.graph.curve.opengl.GLRegion;
35import com.jogamp.graph.font.Font;
36import com.jogamp.graph.font.Font.Glyph;
37import com.jogamp.graph.ui.GraphShape;
38import com.jogamp.math.Vec3f;
39import com.jogamp.math.geom.AABBox;
40import com.jogamp.math.geom.plane.AffineTransform;
41import com.jogamp.opengl.GL2ES2;
42import com.jogamp.opengl.GLProfile;
43import com.jogamp.opengl.util.texture.TextureSequence;
44
45/**
46 * Representing a single {@link Font.Glyph} as a {@link GraphShape}
47 *
48 * A GlyphShape is represented in font em-size [0..1] unscaled w/ bottom-left origin at 0/0
49 * while preserving an intended position, see {@link #getOrigPos()}.
50 *
51 * Scaling, if any, should be applied via {@link #setScale(float, float, float)} etc.
52 */
53public class GlyphShape extends GraphShape {
54 private final Glyph glyph;
55 private final int regionVertCount;
56 private final int regionIdxCount;
57 private final Vec3f origPos;
58
59 /**
60 * Creates a new GlyphShape
61 * @param renderModes Graph's {@link Region} render modes, see {@link GLRegion#create(GLProfile, int, TextureSequence) create(..)}.
62 * @param glyph the {@link Font.Glyph}
63 * @param x the intended unscaled X position of this Glyph, e.g. if part of a string - otherwise use zero.
64 * @param y the intended unscaled Y position of this Glyph, e.g. if part of a string - otherwise use zero.
65 * @see #processString(List, int, Font, String)
66 */
67 public GlyphShape(final int renderModes, final Glyph glyph, final float x, final float y) {
68 super(renderModes);
69 this.glyph = glyph;
70 this.origPos = new Vec3f(x, y, 0f);
71 if( glyph.isNonContour() ) {
72 setVisible(false);
73 }
74 final int[/*2*/] vertIndexCount = Region.countOutlineShape(glyph.getShape(), new int[2]);
75 regionVertCount = vertIndexCount[0];
76 regionIdxCount = vertIndexCount[1];
77 }
78
79 /**
80 * Creates a new GlyphShape
81 * @param renderModes Graph's {@link Region} render modes, see {@link GLRegion#create(GLProfile, int, TextureSequence) create(..)}.
82 * @param glyph the {@link Font.Glyph}
83 * @param pos the intended unscaled Vec3f position of this Glyph, e.g. if part of a string - otherwise use zero.
84 * @see #processString(List, int, Font, String)
85 */
86 public GlyphShape(final int renderModes, final Glyph glyph, final Vec3f pos) {
87 this(renderModes, glyph, pos.x(), pos.y());
88 }
89
90 /**
91 * Creates a new GlyphShape
92 * @param renderModes Graph's {@link Region} render modes, see {@link GLRegion#create(GLProfile, int, TextureSequence) create(..)}.
93 * @param font the {@link Font} to lookup the symbol's {@link Font.Glyph}
94 * @param codepoint the represented character unicode `codepoint` symbol
95 * @param x the intended unscaled X position of this Glyph, e.g. if part of a string - otherwise use zero.
96 * @param y the intended unscaled Y position of this Glyph, e.g. if part of a string - otherwise use zero.
97 */
98 public GlyphShape(final int renderModes, final Font font, final char codepoint, final float x, final float y) {
99 this(renderModes, font.getGlyph( codepoint ), x, y);
100 }
101
102 /** GlyphShape copy-ctor */
103 public GlyphShape(final GlyphShape orig) {
104 this(orig.renderModes, orig.glyph, orig.origPos);
105 }
106
107 /**
108 * Returns the {@link Font.Glyph} to be rendered.
109 */
110 public Glyph getGlyph() {
111 return glyph;
112 }
113
114 /**
115 * Returns the {@link Font} used to render the text
116 */
117 public Font getFont() {
118 return glyph.getFont();
119 }
120
121 /**
122 * Returns the unscaled original position of this glyph, e.g. if part of a string, otherwise zero.
123 *
124 * Method borrows and returns the internal instance.
125 *
126 * @see #processString(List, int, Font, String)
127 */
128 public Vec3f getOrigPos() { return origPos; }
129 /** Returns {@link Font#getLineHeight()}. */
130 public float getLineHeight() {
131 return glyph.getFont().getLineHeight();
132 }
133
134 /**
135 * Process the given text resulting in a list of {@link GlyphShape}s with stored original position {@link #getOrigX()} and {@link #getOrigY()} each at font em-size [0..1].
136 * @param res storage for resulting {@link GlyphShape}s.
137 * @param renderModes Graph's {@link Region} render modes, see {@link GLRegion#create(GLProfile, int, TextureSequence) create(..)}.
138 * @param font {@link Font} used
139 * @param text text to be represented
140 * @return the bounding box of the given string by taking each glyph's font em-sized [0..1] OutlineShape into account.
141 * @see #getOrigX()
142 * @see #getOrigY()
143 */
144 public static final AABBox processString(final List<GlyphShape> res, final int renderModes,
145 final Font font, final CharSequence text)
146 {
147 final Font.GlyphVisitor fgv = new Font.GlyphVisitor() {
148 @Override
149 public void visit(final Glyph glyph, final AffineTransform t) {
150 if( !glyph.isNonContour() ) {
151 res.add( new GlyphShape(renderModes, glyph, t.getTranslateX(), t.getTranslateY()) );
152 }
153 }
154 };
155 return font.processString(fgv, null, text, new AffineTransform(), new AffineTransform());
156 }
157
158 @Override
159 protected void addShapeToRegion(final GLProfile glp, final GL2ES2 gl) {
160 final OutlineShape shape = glyph.getShape();
161 box.reset();
162 if( null != shape ) {
163 final AABBox sbox = shape.getBounds();
164 final AffineTransform tmp = new AffineTransform();
165 // Enforce bottom-left origin @ 0/0 for good drag-zoom experience,
166 // but keep the underline (decline) intact!
167 tmp.setToTranslation(-sbox.getMinX(), -sbox.getMinY() + glyph.getBounds().getMinY());
169
170 resetGLRegion(glp, gl, null, regionVertCount, regionIdxCount);
171 region.addOutlineShape(shape, tmp, rgbaColor);
172 box.resize(tmp.transform(sbox, new AABBox()));
174 } else {
175 // needs a dummy 'region'
176 resetGLRegion(glp, gl, null, regionVertCount, regionIdxCount);
177 }
178 }
179
180 @Override
181 public String getSubString() {
182 return super.getSubString()+", origPos " + origPos.x() + " / " + origPos.y() + ", cp 0x" + Integer.toHexString(glyph.getCodepoint());
183 }
184}
A Generic shape objects which is defined by a list of Outlines.
final void setSharpness(final float s)
Sets sharpness, defaults to DEFAULT_SHARPNESS.
Abstract Outline shape representation define the method an OutlineShape(s) is bound and rendered.
Definition: Region.java:62
final void addOutlineShape(final OutlineShape shape, final AffineTransform t, final Vec4f rgbaColor)
Add the given OutlineShape to this region with the given optional AffineTransform.
Definition: Region.java:616
static final int[] countOutlineShape(final OutlineShape shape, final int[] vertIndexCount)
Count required number of vertices and indices adding to given int[2] vertIndexCount array.
Definition: Region.java:572
Graph based GLRegion Shape.
Definition: GraphShape.java:55
final void resetGLRegion(final GLProfile glp, final GL2ES2 gl, final TextureSequence colorTexSeq, int vertexCount, int indexCount)
Reset the GLRegion and reserving its buffers to have a free capacity for vertexCount and indexCount e...
final Vec4f rgbaColor
Default base-color w/o color channel, will be modulated w/ pressed- and toggle color.
Definition: Shape.java:261
final Shape setVisible(final boolean v)
Enable (default) or disable this shape's visibility.
Definition: Shape.java:363
final Shape setRotationPivot(final float px, final float py, final float pz)
Set unscaled rotation origin, aka pivot.
Definition: Shape.java:620
Representing a single Font.Glyph as a GraphShape.
Definition: GlyphShape.java:53
Font getFont()
Returns the Font used to render the text.
float getLineHeight()
Returns Font#getLineHeight().
Vec3f getOrigPos()
Returns the unscaled original position of this glyph, e.g.
static final AABBox processString(final List< GlyphShape > res, final int renderModes, final Font font, final CharSequence text)
Process the given text resulting in a list of GlyphShapes with stored original position getOrigX() an...
GlyphShape(final int renderModes, final Glyph glyph, final Vec3f pos)
Creates a new GlyphShape.
Definition: GlyphShape.java:86
GlyphShape(final int renderModes, final Font font, final char codepoint, final float x, final float y)
Creates a new GlyphShape.
Definition: GlyphShape.java:98
Glyph getGlyph()
Returns the Font.Glyph to be rendered.
GlyphShape(final GlyphShape orig)
GlyphShape copy-ctor.
GlyphShape(final int renderModes, final Glyph glyph, final float x, final float y)
Creates a new GlyphShape.
Definition: GlyphShape.java:67
void addShapeToRegion(final GLProfile glp, final GL2ES2 gl)
3D Vector based upon three float components.
Definition: Vec3f.java:37
Axis Aligned Bounding Box.
Definition: AABBox.java:54
final AABBox reset()
Resets this box to the inverse low/high, allowing the next resize(float, float, float) command to hit...
Definition: AABBox.java:123
final AABBox resize(final AABBox newBox)
Resize the AABBox to encapsulate another AABox.
Definition: AABBox.java:274
final Vec3f getCenter()
Returns computed center of this AABBox of getLow() and getHigh().
Definition: AABBox.java:737
final AffineTransform setToTranslation(final float mx, final float my)
final AABBox transform(final AABBox src, final AABBox dst)
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
General purpose Font.Glyph visitor.
Definition: Font.java:298
boolean isNonContour()
Returns true if isWhitespace() or isUndefined().
Font getFont()
Returns the Font owning this Glyph.
char getCodepoint()
Returns this glyph's mapped (unicode) codepoint symbol.
AABBox getBounds(final AABBox dest)
Returns the AABBox in font em-size [0..1], copying into given dest.
Interface wrapper for font implementation.
Definition: Font.java:60
Glyph getGlyph(final String name)
Returns the Glyph mapped to given name.
float getLineHeight()
Returns line height, baseline-to-baseline in em-size [0..1], composed from ‘hhea’ table entries.
AABBox processString(final Font.GlyphVisitor visitor, final AffineTransform transform, final CharSequence string)
Try using processString(GlyphVisitor, AffineTransform, CharSequence, AffineTransform,...