CI / Build Java (push) Has been cancelled
CI / Build Native (macos-latest - macos) (push) Has been cancelled
CI / Build Native (ubuntu-latest - linux) (push) Has been cancelled
CI / Build Native (ubuntu-latest - windows) (push) Has been cancelled
CI / Archive Natives (push) Has been cancelled
CI / Update Binaries (push) Has been cancelled
CI / Release (push) Has been cancelled
- Enable IMGUI_USE_WCHAR32 in imconfig.h (ImWchar = uint32_t) - Extend ImFontGlyphRangesBuilder, ImFontConfig, ImFontAtlas for 32-bit codepoints - Cross-compile natives for macOS universal, Linux x86_64 (glibc), Windows x86_64 - Publish as org.nnstd.imgui:...:1.90.0-wchar32+mrlokop2 to mvn.mrlokop.dev - Add BUILDING.md with full rebuild and publish instructions Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
140 lines
4.8 KiB
Java
140 lines
4.8 KiB
Java
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().
|
|
* <p>
|
|
* 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.
|
|
* <p>
|
|
* 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<Short> out = new ArrayList<>();
|
|
for (int n = 0; n <= UNICODE_CODEPOINT_MAX; n++) {
|
|
if (getBit(n)) {
|
|
final int lo = n;
|
|
while (n < UNICODE_CODEPOINT_MAX && getBit(n + 1)) {
|
|
n++;
|
|
}
|
|
final int hi = n;
|
|
// emit lo as two LE shorts
|
|
out.add((short) (lo & 0xFFFF));
|
|
out.add((short) ((lo >> 16) & 0xFFFF));
|
|
// emit hi as two LE shorts
|
|
out.add((short) (hi & 0xFFFF));
|
|
out.add((short) ((hi >> 16) & 0xFFFF));
|
|
}
|
|
}
|
|
|
|
// terminator: two zero shorts (= one 32-bit zero ImWchar)
|
|
final short[] result = new short[out.size() + 2];
|
|
for (int i = 0; i < out.size(); i++) {
|
|
result[i] = out.get(i);
|
|
}
|
|
result[result.length - 2] = 0;
|
|
result[result.length - 1] = 0;
|
|
return result;
|
|
}
|
|
|
|
public void clear() {
|
|
Arrays.fill(usedChars, 0L);
|
|
}
|
|
|
|
public void setBit(final int n) {
|
|
final int off = n >> 5;
|
|
final long mask = 1L << (n & 31L);
|
|
usedChars[off] |= mask;
|
|
}
|
|
|
|
public boolean getBit(final int n) {
|
|
final int off = n >> 5;
|
|
final long mask = 1L << (n & 31L);
|
|
return (usedChars[off] & mask) > 0;
|
|
}
|
|
}
|