package imgui; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Helper to build glyph ranges from text/string data. Feed your application strings/characters to it then call buildRanges(). *
* You can use the ImFontGlyphRangesBuilder helper to create glyph ranges based on text input. For example: for a game where your script is known, * if you can feed your entire script to it and only build the characters the game needs. *
* Direct reimplementation of native ImFontGlyphRangesBuilder in java.
* Built with IMGUI_USE_WCHAR32=1: supports full Unicode range U+0000..U+10FFFF.
* buildRanges() emits each range endpoint as two little-endian shorts (32-bit ImWchar layout).
*/
public final class ImFontGlyphRangesBuilder {
private static final int UNICODE_CODEPOINT_MAX = 0x10FFFF;
// have to use type long because values are unsigned integers; sized the same way as native (divide by 8)
private final long[] usedChars = new long[(UNICODE_CODEPOINT_MAX + 1) / 8];
public ImFontGlyphRangesBuilder() {
clear();
}
/**
* Adds single character to resulting ranges (char overload for source compatibility)
*/
public void addChar(final char c) {
setBit(c);
}
/**
* Adds a Unicode codepoint (including SMP, U+10000..U+10FFFF) to resulting ranges
*/
public void addChar(final int codepoint) {
if (codepoint >= 0 && codepoint <= UNICODE_CODEPOINT_MAX) {
setBit(codepoint);
}
}
/**
* Adds all codepoints from the given string to resulting ranges.
* Handles supplementary characters (emoji, etc.) via codePointAt iteration.
*/
public void addText(final String text) {
int i = 0;
while (i < text.length()) {
final int cp = text.codePointAt(i);
addChar(cp);
i += Character.charCount(cp);
}
}
/**
* Copies all given ranges to resulting ranges.
* Expects the WCHAR32 short[] layout: each 32-bit codepoint encoded as two little-endian shorts,
* pairs of codepoints form a [lo, hi] range, terminated by two zero shorts.
* Format: [ lo_lo16, lo_hi16, hi_lo16, hi_hi16, ..., 0, 0 ]
*/
public void addRanges(final short[] ranges) {
for (int i = 0; i + 3 < ranges.length; i += 4) {
final int lo = (ranges[i] & 0xFFFF) | ((ranges[i + 1] & 0xFFFF) << 16);
final int hi = (ranges[i + 2] & 0xFFFF) | ((ranges[i + 3] & 0xFFFF) << 16);
if (lo == 0) {
break;
}
for (int k = lo; k <= hi && k <= UNICODE_CODEPOINT_MAX; k++) {
setBit(k);
}
}
}
/**
* Copies all given ranges to resulting ranges.
* Expects int[] pairs: [ lo, hi, lo, hi, ..., 0 ] (zero-terminated).
*/
public void addRanges(final int[] ranges) {
for (int i = 0; i + 1 < ranges.length; i += 2) {
if (ranges[i] == 0) {
break;
}
for (int k = ranges[i]; k <= ranges[i + 1] && k <= UNICODE_CODEPOINT_MAX; k++) {
setBit(k);
}
}
}
/**
* Builds the final result (ordered ranges with all the unique characters submitted).
* Result is in WCHAR32 short[] layout: each range endpoint is two little-endian shorts,
* terminated by two zero shorts.
* Format: [ lo_lo16, lo_hi16, hi_lo16, hi_hi16, ..., 0, 0 ]
*/
public short[] buildRanges() {
final List