JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
FontViewListener01.java
Go to the documentation of this file.
1/**
2 * Copyright 2023-2024 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.opengl.test.junit.graph;
29
30import com.jogamp.graph.font.Font;
31import com.jogamp.graph.font.FontScale;
32import com.jogamp.graph.ui.Group;
33import com.jogamp.graph.ui.Scene;
34import com.jogamp.graph.ui.Shape;
35import com.jogamp.graph.ui.layout.Alignment;
36import com.jogamp.graph.ui.layout.BoxLayout;
37import com.jogamp.graph.ui.layout.Gap;
38import com.jogamp.graph.ui.layout.GridLayout;
39import com.jogamp.graph.ui.shapes.GlyphShape;
40import com.jogamp.math.Vec3f;
41import com.jogamp.math.geom.AABBox;
42import com.jogamp.newt.event.MouseEvent;
43import com.jogamp.newt.opengl.GLWindow;
44import com.jogamp.opengl.GL;
45import com.jogamp.opengl.GL2ES2;
46import com.jogamp.opengl.GLAutoDrawable;
47import com.jogamp.opengl.GLEventListener;
48
49/**
50 * Glyph Grid using GraphUI
51 */
52public class FontViewListener01 implements GLEventListener {
53 private float mmPerCell = 8.0f;
54 private int pixelPerCell = 30; // 1280 x 720 pixel @ 94.268 dpi, 3.7113402 pixel/mm, 8mm -> 29.69 pixel
55 private boolean useDPI = false;
56
57 private final int renderModes;
58 private final char startCharSymbol;
59 private final Font font;
60 private final Scene scene;
61 private Group grid;
62
63 public FontViewListener01(final int renderModes, final int graphAAQuality, final int graphSampleCount, final Font font, final char startCharSymbol) {
64 this.renderModes = renderModes;
65 this.startCharSymbol = startCharSymbol;
66 this.font = font;
67
68 scene = new Scene(graphSampleCount);
69 scene.setClearParams(new float[] { 1f, 1f, 1f, 1f}, GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
70 scene.setPMvCullingEnabled(true);
71 scene.setAAQuality(graphAAQuality);
72 }
73
74 public void setMMPerCell(final float mmPerCell) {
75 this.mmPerCell = mmPerCell;
76 this.useDPI = true;
77 }
78 public void setPixelPerCell(final int pixelPerCell) {
79 this.pixelPerCell = pixelPerCell;
80 this.useDPI = false;
81 }
82
83 public void attachInputListenerTo(final GLWindow window) {
84 scene.attachInputListenerTo(window);
85 }
86
87 public void printScreenOnGLThread(final GLAutoDrawable drawable, final String dir, final String prefix, final String objName, final boolean exportAlpha) {
88 final String fn = font.getFullFamilyName().replace(' ', '_').replace('-', '_');
89 scene.screenshot(true, scene.nextScreenshotFile(null, prefix, renderModes, drawable.getChosenGLCapabilities(), fn));
90 }
91
92 @Override
93 public void init(final GLAutoDrawable drawable) {
94 scene.init(drawable);
95 }
96
97 @Override
98 public void dispose(final GLAutoDrawable drawable) {
99 scene.dispose(drawable);
100 }
101
102 @Override
103 public void display(final GLAutoDrawable drawable) {
104 scene.display(drawable);
105 }
106
107 @Override
108 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
109 scene.reshape(drawable, x, y, width, height);
110 System.err.println("Reshape "+width+" x "+height+", "+scene.getViewport());
111
112 final GL2ES2 gl = drawable.getGL().getGL2ES2();
113
114 if( null != grid ) {
115 scene.removeShape(gl, grid);
116 }
117 final int gridCols, gridRows;
118 if( useDPI && drawable instanceof GLWindow ) {
119 final GLWindow window = (GLWindow)drawable;
120 final float[] ppmm = window.getPixelsPerMM(new float[2]);
121 {
122 final float[] dpi = FontScale.ppmmToPPI( new float[] { ppmm[0], ppmm[1] } );
123 System.err.println("DPI "+dpi[0]+" x "+dpi[1]+", "+ppmm[0]+" x "+ppmm[1]+" pixel/mm");
124
125 final float[] hasSurfacePixelScale1 = window.getCurrentSurfaceScale(new float[2]);
126 System.err.println("HiDPI PixelScale: "+hasSurfacePixelScale1[0]+"x"+hasSurfacePixelScale1[1]+" (has)");
127 System.err.println("mmPerCell "+mmPerCell);
128 }
129 gridCols = (int)( 0.90f * ( width / ppmm[0] ) / mmPerCell );
130 gridRows = (int)( 0.90f * ( height / ppmm[1] ) / mmPerCell );
131 } else {
132 gridCols = (int)( 0.90f * width / pixelPerCell );
133 gridRows = (int)( 0.90f * height / pixelPerCell );
134 }
135 final int cellCount = gridCols * gridRows;
136 final float netGridSize = gridCols > gridRows ? 1f/gridCols : 1f/gridRows;
137 System.err.println("Reshape Grid "+gridCols+" x "+gridRows+", "+cellCount+" cells, netGridSize "+netGridSize);
138
139 grid = new Group(new GridLayout(gridCols, netGridSize, netGridSize, Alignment.FillCenter, new Gap(netGridSize/0.90f*0.10f)));
140 scene.addShape(grid);
141
142 for(int idx=0; idx<Character.MAX_VALUE && grid.getShapeCount() < cellCount ; ++idx) {
143 final char codepoint = (char)(startCharSymbol+idx);
144 final Font.Glyph glyph = font.getGlyph( codepoint );
145 if( glyph.isNonContour() ) {
146 continue;
147 }
148 final GlyphShape glyphShape = new GlyphShape(renderModes, glyph, 0, 0);
149 glyphShape.setColor(0.1f, 0.1f, 0.1f, 1);
150 glyphShape.setDragAndResizable(false);
151 glyphShape.onClicked( (final Shape shape, final Vec3f pos, final MouseEvent e) -> {
152 System.err.println( ((GlyphShape)shape).getGlyph().toString() );
153 });
154 glyphShape.validate(gl);
155
156 // Group each GlyphShape with a border
157 final Group c = new Group( new BoxLayout( 1f, 1f, Alignment.Center) );
158 c.setBorder(0.02f).setBorderColor(0, 0, 0, 1).setInteractive(false);
159 final AABBox gbox = glyphShape.getBounds();
160 glyphShape.move( ( 1f - gbox.getWidth() ) / 2f, ( 1f - gbox.getHeight() ) / 2f, 0f ); // center
161 glyphShape.move( gbox.getLow().mul(-1f) ); // remove bottom-left delta, here glyph underline
162 c.addShape(glyphShape);
163 grid.addShape(c);
164 }
165
166 grid.validate(gl);
167 final AABBox sceneBox = scene.getBounds();
168 final AABBox gridBox = grid.getBounds();
169 final float sx = sceneBox.getWidth() / gridBox.getWidth();
170 final float sy = sceneBox.getHeight() / gridBox.getHeight();
171 final float sxy = Math.min(sx, sy);
172 grid.scale(sxy, sxy, 1f).moveTo(sceneBox.getLow());
173 System.err.println("SceneBox "+sceneBox);
174 System.err.println("GridBox "+gridBox);
175 System.err.println("scale sx "+sx+", sy "+sy+", sxy "+sxy);
176 System.err.println("Grid "+grid);
177 System.err.println("Grid "+grid.getLayout());
178 System.err.println("Grid[0] "+grid.getShapes().get(0));
179 }
180}
Simple static font scale methods for unit conversions.
Definition: FontScale.java:37
static float[] ppmmToPPI(final float[] ppmm)
Converts [1/mm] to [1/inch] in place.
Definition: FontScale.java:105
Group of Shapes, optionally utilizing a Group.Layout.
Definition: Group.java:61
int getShapeCount()
Returns number of Shapes, see getShapes().
Definition: Group.java:216
void addShape(final Shape s)
Adds a Shape.
Definition: Group.java:225
List< Shape > getShapes()
Returns added Shapes.
Definition: Group.java:219
Layout getLayout()
Return current Group.Layout.
Definition: Group.java:150
AABBox getBounds(final PMVMatrix4f pmv, final Shape shape)
Returns AABBox dimension of given Shape from this container's perspective, i.e.
Definition: Group.java:686
GraphUI Scene.
Definition: Scene.java:102
void addShape(final Shape s)
Adds a Shape.
Definition: Scene.java:287
final void setClearParams(final float[] clearColor, final int clearMask)
Sets the clear parameter for glClearColor(..) and glClear(..) to be issued at display(GLAutoDrawable)...
Definition: Scene.java:221
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
Definition: Scene.java:407
Shape removeShape(final Shape s)
Removes given shape, w/o Shape#destroy(GL2ES2, RegionRenderer).
Definition: Scene.java:292
final void setPMvCullingEnabled(final boolean v)
Enable or disable Project-Modelview (PMv) frustum culling per Shape for this container.
Definition: Scene.java:230
int setAAQuality(final int v)
Sets RegionRenderer#setAAQuality(int).
Definition: Scene.java:383
void dispose(final GLAutoDrawable drawable)
Disposes all added Shapes.
Definition: Scene.java:605
final Recti getViewport(final Recti target)
Copies the current int[4] viewport in given target and returns it for chaining.
Definition: Scene.java:768
AABBox getBounds(final PMVMatrix4f pmv, final Shape shape)
Returns AABBox dimension of given Shape from this container's perspective, i.e.
Definition: Scene.java:676
synchronized void attachInputListenerTo(final GLWindow window)
Definition: Scene.java:246
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
Definition: Scene.java:487
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Reshape scene using setupMatrix(PMVMatrix4f, int, int, int, int) using PMVMatrixSetup.
Definition: Scene.java:458
File nextScreenshotFile(final String dir, final String prefix, final int renderModes, final GLCapabilitiesImmutable caps, final String contentDetail)
Return the unique next technical screenshot PNG File instance as follows:
Definition: Scene.java:1434
Generic Shape, potentially using a Graph via GraphShape or other means of representing content.
Definition: Shape.java:87
Shape setColor(final float r, final float g, final float b, final float a)
Set base color.
Definition: Shape.java:1389
final Shape move(final float dtx, final float dty, final float dtz)
Move about scaled distance.
Definition: Shape.java:557
final Shape setInteractive(final boolean v)
Set whether this shape is interactive in general, i.e.
Definition: Shape.java:1711
final void onClicked(final PointerListener l)
Set user callback to be notified when shape is clicked.
Definition: Shape.java:503
final Shape moveTo(final float tx, final float ty, final float tz)
Move to scaled position.
Definition: Shape.java:543
final AABBox getBounds()
Returns the unscaled bounding AABBox for this shape, borrowing internal instance.
Definition: Shape.java:732
final Shape setDragAndResizable(final boolean v)
Set whether this shape is draggable and resizable.
Definition: Shape.java:1801
final Shape validate(final GL2ES2 gl)
Validates the shape's underlying GLRegion.
Definition: Shape.java:850
final Shape scale(final Vec3f s)
Multiply current scale factor by given scale.
Definition: Shape.java:661
final Shape setBorderColor(final float r, final float g, final float b, final float a)
Set border color.
Definition: Shape.java:1489
final Shape setBorder(final float thickness)
Sets the thickness of the border, which is included in getBounds() and is outside of getPadding().
Definition: Shape.java:402
Immutable layout alignment options, including Bit#Fill.
Definition: Alignment.java:35
static final Alignment Center
Bit#CenterHoriz and Bit#CenterVert alignment constant.
Definition: Alignment.java:39
static final Alignment FillCenter
Bit#Fill, Bit#CenterHoriz and Bit#CenterVert alignment constant.
Definition: Alignment.java:45
GraphUI Stack Group.Layout.
Definition: BoxLayout.java:53
GraphUI CSS property Gap, scaled spacing between (grid) cells not belonging to the cell element.
Definition: Gap.java:38
GraphUI Grid Group.Layout.
Definition: GridLayout.java:56
Representing a single Font.Glyph as a GraphShape.
Definition: GlyphShape.java:53
3D Vector based upon three float components.
Definition: Vec3f.java:37
Vec3f mul(final float val)
Returns this * val; creates new vector.
Definition: Vec3f.java:178
Axis Aligned Bounding Box.
Definition: AABBox.java:54
final float getWidth()
Definition: AABBox.java:879
final Vec3f getLow()
Returns the minimum left-bottom-far (xyz) coordinate.
Definition: AABBox.java:140
final float getHeight()
Definition: AABBox.java:883
Pointer event of type PointerType.
Definition: MouseEvent.java:74
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final float[] getPixelsPerMM(final float[] ppmmStore)
Returns the pixels per millimeter of this window's NativeSurface according to the main monitor's curr...
Definition: GLWindow.java:520
final float[] getCurrentSurfaceScale(final float[] result)
Returns the current pixel scale of the associated NativeSurface.
Definition: GLWindow.java:505
void printScreenOnGLThread(final GLAutoDrawable drawable, final String dir, final String prefix, final String objName, final boolean exportAlpha)
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
FontViewListener01(final int renderModes, final int graphAAQuality, final int graphSampleCount, final Font font, final char startCharSymbol)
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
Interface wrapper for font implementation.
Definition: Font.java:60
Glyph getGlyph(final String name)
Returns the Glyph mapped to given name.
String getFullFamilyName()
Shall return the family and subfamily name, separated a dash.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 interface.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
static final int GL_DEPTH_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_BUFFER_BIT" wit...
Definition: GL.java:738