1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
/**
* Copyright 2014-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.graph.curve.opengl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import com.jogamp.opengl.GL2ES2;
import com.jogamp.opengl.GLException;
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.font.Font.Glyph;
import com.jogamp.graph.geom.plane.AffineTransform;
/**
* Text Type Rendering Utility Class adding the {@link Font.Glyph}s {@link OutlineShape} to a {@link GLRegion}.
* <p>
* {@link OutlineShape}s are all produced in font em-size [0..1].
* </p>
*/
public class TextRegionUtil {
public final int renderModes;
public TextRegionUtil(final int renderModes) {
this.renderModes = renderModes;
}
public static int getCharCount(final String s, final char c) {
final int sz = s.length();
int count = 0;
for(int i=0; i<sz; i++) {
if( s.charAt(i) == c ) {
count++;
}
}
return count;
}
/**
* 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 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 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 Region region, final Font font, final AffineTransform transform,
final CharSequence str, final float[] rgbaColor,
final AffineTransform temp1, final AffineTransform temp2) {
final Font.GlyphVisitor visitor = new Font.GlyphVisitor() {
@Override
public void visit(final char symbol, final Glyph glyph, final AffineTransform t) {
if( glyph.isWhiteSpace() ) {
return;
}
region.addOutlineShape(glyph.getShape(), t, region.hasColorChannel() ? rgbaColor : null);
}
};
return font.processString(visitor, transform, str, temp1, temp2);
}
/**
* Count required number of vertices and indices adding to given int[2] `vertIndexCount` array.
* <p>
* The region's buffer can be either set using {@link Region#setBufferCapacity(int, int)} or grown using {@link Region#growBuffer(int, int)}.
* </p>
* @param region the {@link GLRegion} sink
* @param font the target {@link Font}
* @param str string text
* @param vertIndexCount the int[2] storage where the counted vertices and indices are added, vertices at [0] and indices at [1]
* @see Region#setBufferCapacity(int, int)
* @see Region#growBuffer(int, int)
* @see #drawString3D(GL2ES2, GLRegion, RegionRenderer, Font, CharSequence, float[], int[], AffineTransform, AffineTransform)
*/
public static void countStringRegion(final Region region, final Font font, final CharSequence str, final int[/*2*/] vertIndexCount) {
final Font.GlyphVisitor2 visitor = new Font.GlyphVisitor2() {
@Override
public final void visit(final char symbol, final Font.Glyph glyph) {
region.countOutlineShape(glyph.getShape(), vertIndexCount);
} };
font.processString(visitor, str);
}
/**
* Render the string in 3D space w.r.t. the font int font em-size [0..1] at the end of an internally cached {@link GLRegion}.
* <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>
* <p>
* Cached {@link GLRegion}s will be destroyed w/ {@link #clear(GL2ES2)} or to free memory.
* </p>
* <p>
* The region's buffer size is pre-calculated via {@link GLRegion#create(com.jogamp.opengl.GLProfile, int, com.jogamp.opengl.util.texture.TextureSequence, Font, CharSequence)}
* </p>
* @param gl the current GL state
* @param renderer TODO
* @param font {@link Font} to be used
* @param str text to be rendered
* @param rgbaColor if {@link Region#hasColorChannel()} RGBA color must be passed, otherwise value is ignored.
* @param sampleCount desired multisampling sample count for msaa-rendering.
* The actual used scample-count is written back when msaa-rendering is enabled, otherwise the store is untouched.
* @return the bounding box of the given string from the produced and rendered GLRegion
* @throws Exception if TextRenderer not initialized
*/
public AABBox drawString3D(final GL2ES2 gl,
final RegionRenderer renderer, final Font font, final CharSequence str,
final float[] rgbaColor, final int[/*1*/] sampleCount) {
if( !renderer.isInitialized() ) {
throw new GLException("TextRendererImpl01: not initialized!");
}
GLRegion region = getCachedRegion(font, str);
AABBox res;
if(null == region) {
region = GLRegion.create(gl.getGLProfile(), renderModes, null, font, str);
res = addStringToRegion(region, font, null, str, rgbaColor, tempT1, tempT2);
addCachedRegion(gl, font, str, region);
} else {
res = new AABBox();
res.copy(region.getBounds());
}
region.draw(gl, renderer, sampleCount);
return res;
}
/**
* Try using {@link #drawString3D(GL2ES2, int, RegionRenderer, Font, CharSequence, float[], int[], AffineTransform, AffineTransform)} to reuse {@link AffineTransform} instances.
* <p>
* The region's buffer size is pre-calculated via {@link GLRegion#create(com.jogamp.opengl.GLProfile, int, com.jogamp.opengl.util.texture.TextureSequence, Font, CharSequence)}
* </p>
*/
public static AABBox drawString3D(final GL2ES2 gl, final int renderModes,
final RegionRenderer renderer, final Font font, final CharSequence str,
final float[] rgbaColor, final int[/*1*/] sampleCount) {
return drawString3D(gl, renderModes, renderer, font, str, rgbaColor, sampleCount, new AffineTransform(), new AffineTransform());
}
/**
* Render the string in 3D space w.r.t. the font in font em-size [0..1] at the end of an internally temporary {@link GLRegion}.
* <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>
* <p>
* The region's buffer size is pre-calculated via {@link GLRegion#create(com.jogamp.opengl.GLProfile, int, com.jogamp.opengl.util.texture.TextureSequence, Font, CharSequence)}
* </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[], AffineTransform, AffineTransform)}
* instead.
* </p>
* @param gl the current GL state
* @param renderModes TODO
* @param font {@link Font} to be used
* @param str text to be rendered
* @param rgbaColor if {@link Region#hasColorChannel()} RGBA color must be passed, otherwise value is ignored.
* @param sampleCount desired multisampling sample count for msaa-rendering.
* The actual used scample-count is written back when msaa-rendering is enabled, otherwise the store is untouched.
* @param tmp1 temp {@link AffineTransform} to be reused
* @param tmp2 temp {@link AffineTransform} to be reused
* @throws Exception if TextRenderer not initialized
* @return the bounding box of the given string from the produced and rendered GLRegion
*/
public static AABBox drawString3D(final GL2ES2 gl, final int renderModes,
final RegionRenderer renderer, final Font font, final CharSequence str,
final float[] rgbaColor, final int[/*1*/] sampleCount, final AffineTransform tmp1, final AffineTransform tmp2) {
if(!renderer.isInitialized()){
throw new GLException("TextRendererImpl01: not initialized!");
}
final GLRegion region = GLRegion.create(gl.getGLProfile(), renderModes, null, font, str);
final AABBox res = addStringToRegion(region, font, null, str, rgbaColor, tmp1, tmp2);
region.draw(gl, renderer, sampleCount);
region.destroy(gl);
return res;
}
/**
* Try using {@link #drawString3D(GL2ES2, GLRegion, RegionRenderer, Font, CharSequence, float[], int[], AffineTransform, AffineTransform)} to reuse {@link AffineTransform} instances.
* <p>
* The region's buffer size is pre-calculated via {@link #countStringRegion(Region, Font, CharSequence, int[])}.
* </p>
*/
public static AABBox drawString3D(final GL2ES2 gl, final GLRegion region, final RegionRenderer renderer,
final Font font, final CharSequence str, final float[] rgbaColor, final int[/*1*/] sampleCount) {
return drawString3D(gl, region, renderer, font, str, rgbaColor, sampleCount, new AffineTransform(), new AffineTransform());
}
/**
* Render the string in 3D space w.r.t. the font in font em-size [0..1] at the end of the given {@link GLRegion}.
* <p>
* User might want to {@link GLRegion#clear(GL2ES2)} the region before calling this method.
* </p>
* <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>
* <p>
* The region's buffer size is pre-calculated via {@link #countStringRegion(Region, Font, CharSequence, int[])}.
* </p>
* @param gl the current GL state
* @param region
* @param renderer
* @param font {@link Font} to be used
* @param str text to be rendered
* @param rgbaColor if {@link Region#hasColorChannel()} RGBA color must be passed, otherwise value is ignored.
* @param sampleCount desired multisampling sample count for msaa-rendering.
* The actual used scample-count is written back when msaa-rendering is enabled, otherwise the store is untouched.
* @param tmp1 temp {@link AffineTransform} to be reused
* @param tmp2 temp {@link AffineTransform} to be reused
* @return the bounding box of the given string from the produced and rendered GLRegion
* @throws Exception if TextRenderer not initialized
*/
public static AABBox drawString3D(final GL2ES2 gl, final GLRegion region, final RegionRenderer renderer,
final Font font, final CharSequence str, final float[] rgbaColor,
final int[/*1*/] sampleCount, final AffineTransform tmp1, final AffineTransform tmp2) {
if(!renderer.isInitialized()){
throw new GLException("TextRendererImpl01: not initialized!");
}
final int[] vertIndCount = { 0, 0 };
TextRegionUtil.countStringRegion(region, font, str, vertIndCount);
region.growBuffer(vertIndCount[0], vertIndCount[1]);
final AABBox res = addStringToRegion(region, font, null, str, rgbaColor, tmp1, tmp2);
region.draw(gl, renderer, sampleCount);
return res;
}
/**
* Clear all cached {@link GLRegions} and mapped values.
*/
public void clear(final GL2ES2 gl) {
// fluchCache(gl) already called
final Iterator<GLRegion> iterator = stringCacheMap.values().iterator();
while(iterator.hasNext()){
final GLRegion region = iterator.next();
region.destroy(gl);
}
stringCacheMap.clear();
stringCacheArray.clear();
}
/**
* <p>Sets the cache limit for reusing GlyphString's and their Region.
* Default is {@link #DEFAULT_CACHE_LIMIT}, -1 unlimited, 0 turns cache off, >0 limited </p>
*
* <p>The cache will be validate when the next string rendering happens.</p>
*
* @param newLimit new cache size
*
* @see #DEFAULT_CACHE_LIMIT
*/
public final void setCacheLimit(final int newLimit ) { stringCacheLimit = newLimit; }
/**
* Sets the cache limit, see {@link #setCacheLimit(int)} and validates the cache.
*
* @see #setCacheLimit(int)
*
* @param gl current GL used to remove cached objects if required
* @param newLimit new cache size
*/
public final void setCacheLimit(final GL2ES2 gl, final int newLimit ) { stringCacheLimit = newLimit; validateCache(gl, 0); }
/**
* @return the current cache limit
*/
public final int getCacheLimit() { return stringCacheLimit; }
/**
* @return the current utilized cache size, <= {@link #getCacheLimit()}
*/
public final int getCacheSize() { return stringCacheArray.size(); }
private final void validateCache(final GL2ES2 gl, final int space) {
if ( getCacheLimit() > 0 ) {
while ( getCacheSize() + space > getCacheLimit() ) {
removeCachedRegion(gl, 0);
}
}
}
private final GLRegion getCachedRegion(final Font font, final CharSequence str) {
return stringCacheMap.get(new Key(font, str));
}
private final void addCachedRegion(final GL2ES2 gl, final Font font, final CharSequence str, final GLRegion glyphString) {
if ( 0 != getCacheLimit() ) {
final Key key = new Key(font, str);
final GLRegion oldRegion = stringCacheMap.put(key, glyphString);
if ( null == oldRegion ) {
// new entry ..
validateCache(gl, 1);
stringCacheArray.add(stringCacheArray.size(), key);
} /// else overwrite is nop ..
}
}
private final void removeCachedRegion(final GL2ES2 gl, final Font font, final CharSequence str) {
final Key key = new Key(font, str);
final GLRegion region = stringCacheMap.remove(key);
if(null != region) {
region.destroy(gl);
}
stringCacheArray.remove(key);
}
private final void removeCachedRegion(final GL2ES2 gl, final int idx) {
final Key key = stringCacheArray.remove(idx);
if( null != key ) {
final GLRegion region = stringCacheMap.remove(key);
if(null != region) {
region.destroy(gl);
}
}
}
private class Key {
private final String fontName;
private final CharSequence text;
public final int hash;
public Key(final Font font, final CharSequence text) {
this.fontName = font.getName(Font.NAME_UNIQUNAME);
this.text = text;
// 31 * x == (x << 5) - x
final int lhash = 31 + fontName.hashCode();
this.hash = ((lhash << 5) - lhash) + text.hashCode();
}
@Override
public final int hashCode() { return hash; }
@Override
public final boolean equals(final Object o) {
if( this == o ) { return true; }
if( o instanceof Key ) {
final Key ok = (Key)o;
return ok.fontName.equals(fontName) && ok.text.equals(text);
}
return false;
}
}
/** Default cache limit, see {@link #setCacheLimit(int)} */
public static final int DEFAULT_CACHE_LIMIT = 256;
public final AffineTransform tempT1 = new AffineTransform();
public final AffineTransform tempT2 = new AffineTransform();
private final HashMap<Key, GLRegion> stringCacheMap = new HashMap<Key, GLRegion>(DEFAULT_CACHE_LIMIT);
private final ArrayList<Key> stringCacheArray = new ArrayList<Key>(DEFAULT_CACHE_LIMIT);
private int stringCacheLimit = DEFAULT_CACHE_LIMIT;
}
|