Skip to content

Commit 9f0d384

Browse files
authored
Make GlyphLayout's GlyphRun acquisition overrideable
This PR basically serves as a solution to libgdx#7582 by allowing the user to implement custom pooling mechanic that could for example allow for thread safe use
1 parent 85588b8 commit 9f0d384

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

gdx/src/com/badlogic/gdx/graphics/g2d/GlyphLayout.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public void setText (BitmapFont font, CharSequence str, int start, int end, Colo
172172
runEnded:
173173
{
174174
// Store the run that has ended.
175-
GlyphRun run = glyphRunPool.obtain();
175+
GlyphRun run = obtainRun();
176176
run.x = 0;
177177
run.y = y;
178178
fontData.getGlyphs(run, str, runStart, runEnd, lastGlyph);
@@ -190,14 +190,14 @@ public void setText (BitmapFont font, CharSequence str, int start, int end, Colo
190190
}
191191

192192
if (run.glyphs.size == 0) {
193-
glyphRunPool.free(run);
193+
freeRun(run);
194194
if (lineRun == null) break runEnded; // Otherwise wrap and truncate must still be processed for lineRun.
195195
} else if (lineRun == null) {
196196
lineRun = run;
197197
runs.add(lineRun);
198198
} else {
199199
lineRun.appendRun(run);
200-
glyphRunPool.free(run);
200+
freeRun(run);
201201
}
202202

203203
if (newline || isLastRun) {
@@ -310,7 +310,7 @@ private void truncate (BitmapFontData fontData, GlyphRun run, float targetWidth,
310310
int glyphCount = run.glyphs.size;
311311

312312
// Determine truncate string size.
313-
GlyphRun truncateRun = glyphRunPool.obtain();
313+
GlyphRun truncateRun = obtainRun();
314314
fontData.getGlyphs(truncateRun, truncate, 0, truncate.length(), null);
315315
float truncateWidth = 0;
316316
if (truncateRun.xAdvances.size > 0) {
@@ -357,7 +357,7 @@ private void truncate (BitmapFontData fontData, GlyphRun run, float targetWidth,
357357
run.glyphs.addAll(truncateRun.glyphs);
358358
this.glyphCount += truncate.length();
359359

360-
glyphRunPool.free(truncateRun);
360+
freeRun(truncateRun);
361361
}
362362

363363
/** Breaks a run into two runs at the specified wrapIndex.
@@ -381,7 +381,7 @@ private GlyphRun wrap (BitmapFontData fontData, GlyphRun first, int wrapIndex) {
381381
// The second run will contain the remaining glyph data, so swap instances rather than copying.
382382
GlyphRun second = null;
383383
if (secondStart < glyphCount) {
384-
second = glyphRunPool.obtain();
384+
second = obtainRun();
385385

386386
Array<Glyph> glyphs1 = second.glyphs; // Starts empty.
387387
glyphs1.addAll(glyphs2, 0, firstEnd);
@@ -430,7 +430,7 @@ private GlyphRun wrap (BitmapFontData fontData, GlyphRun first, int wrapIndex) {
430430

431431
if (firstEnd == 0) {
432432
// If the first run is now empty, remove it.
433-
glyphRunPool.free(first);
433+
freeRun(first);
434434
runs.pop();
435435
} else
436436
setLastGlyphXAdvance(fontData, first);
@@ -499,14 +499,29 @@ else if (ch >= 'a' && ch <= 'f')
499499
}
500500

501501
public void reset () {
502-
glyphRunPool.freeAll(runs);
502+
freeAllRuns(runs);
503503
runs.clear();
504504
colors.clear();
505505
glyphCount = 0;
506506
width = 0;
507507
height = 0;
508508
}
509509

510+
/** Obtains a GlyphRun */
511+
protected GlyphRun obtainRun () {
512+
return glyphRunPool.obtain();
513+
}
514+
515+
/** Releases a previously obtained GlyphRun */
516+
protected void freeRun (GlyphRun run) {
517+
glyphRunPool.free(run);
518+
}
519+
520+
/** Releases an array of previously obtained GlyphRuns */
521+
protected void freeAllRuns (Array<GlyphRun> runs) {
522+
glyphRunPool.freeAll(runs);
523+
}
524+
510525
public String toString () {
511526
if (runs.size == 0) return "";
512527
StringBuilder buffer = new StringBuilder(128);
@@ -535,7 +550,7 @@ static public class GlyphRun implements Poolable {
535550

536551
public float x, y, width;
537552

538-
void appendRun (GlyphRun run) {
553+
public void appendRun (GlyphRun run) {
539554
glyphs.addAll(run.glyphs);
540555
// Remove the width of the last glyph. The first xadvance of the appended run has kerning for the last glyph of this run.
541556
if (xAdvances.notEmpty()) xAdvances.size--;

0 commit comments

Comments
 (0)