| 1 | package net.minecraft.client.gui.font.providers;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.blaze3d.font.GlyphInfo;
|
| 5 | import com.mojang.blaze3d.font.GlyphProvider;
|
| 6 | import com.mojang.blaze3d.font.UnbakedGlyph;
|
| 7 | import com.mojang.datafixers.util.Either;
|
| 8 | import com.mojang.logging.LogUtils;
|
| 9 | import com.mojang.serialization.Codec;
|
| 10 | import com.mojang.serialization.DataResult;
|
| 11 | import com.mojang.serialization.MapCodec;
|
| 12 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 13 | import it.unimi.dsi.fastutil.bytes.ByteArrayList;
|
| 14 | import it.unimi.dsi.fastutil.bytes.ByteList;
|
| 15 | import it.unimi.dsi.fastutil.ints.IntSet;
|
| 16 | import java.io.IOException;
|
| 17 | import java.io.InputStream;
|
| 18 | import java.nio.IntBuffer;
|
| 19 | import java.util.List;
|
| 20 | import java.util.zip.ZipEntry;
|
| 21 | import java.util.zip.ZipInputStream;
|
| 22 | import net.fabricmc.api.EnvType;
|
| 23 | import net.fabricmc.api.Environment;
|
| 24 | import net.minecraft.client.gui.font.CodepointMap;
|
| 25 | import net.minecraft.client.gui.font.glyphs.BakedGlyph;
|
| 26 | import net.minecraft.client.gui.font.providers.UnihexProvider.Glyph.1;
|
| 27 | import net.minecraft.client.gui.font.providers.UnihexProvider.Glyph.2;
|
| 28 | import net.minecraft.resources.Identifier;
|
| 29 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 30 | import net.minecraft.util.ExtraCodecs;
|
| 31 | import net.minecraft.util.FastBufferedInputStream;
|
| 32 | import org.jspecify.annotations.Nullable;
|
| 33 | import org.slf4j.Logger;
|
| 34 |
|
| 35 | @Environment(EnvType.CLIENT)
|
| 36 | public class UnihexProvider implements GlyphProvider {
|
| 37 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 38 | private static final int GLYPH_HEIGHT = 16;
|
| 39 | private static final int DIGITS_PER_BYTE = 2;
|
| 40 | private static final int DIGITS_FOR_WIDTH_8 = 32;
|
| 41 | private static final int DIGITS_FOR_WIDTH_16 = 64;
|
| 42 | private static final int DIGITS_FOR_WIDTH_24 = 96;
|
| 43 | private static final int DIGITS_FOR_WIDTH_32 = 128;
|
| 44 | private final CodepointMap<UnihexProvider.Glyph> glyphs;
|
| 45 |
|
| 46 | private UnihexProvider(final CodepointMap<UnihexProvider.Glyph> glyphs) {
|
| 47 | this.glyphs = glyphs;
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public @Nullable UnbakedGlyph getGlyph(final int codepoint) {
|
| 52 | return this.glyphs.get(codepoint);
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public IntSet getSupportedGlyphs() {
|
| 57 | return this.glyphs.keySet();
|
| 58 | }
|
| 59 |
|
| 60 | @VisibleForTesting
|
| 61 | static void unpackBitsToBytes(final IntBuffer output, final int value, final int left, final int right) {
|
| 62 | int startBit = 32 - left - 1;
|
| 63 | int endBit = 32 - right - 1;
|
| 64 |
|
| 65 | for (int i = startBit; i >= endBit; i--) {
|
| 66 | if (i < 32 && i >= 0) {
|
| 67 | boolean isSet = (value >> i & 1) != 0;
|
| 68 | output.put(isSet ? -1 : 0);
|
| 69 | } else {
|
| 70 | output.put(0);
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | private static void unpackBitsToBytes(final IntBuffer output, final UnihexProvider.LineData data, final int left, final int right) {
|
| 76 | for (int i = 0; i < 16; i++) {
|
| 77 | int line = data.line(i);
|
| 78 | unpackBitsToBytes(output, line, left, right);
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | @VisibleForTesting
|
| 83 | static void readFromStream(final InputStream input, final UnihexProvider.ReaderOutput output) throws IOException {
|
| 84 | int line = 0;
|
| 85 | ByteList buffer = new ByteArrayList(128);
|
| 86 |
|
| 87 | while (true) {
|
| 88 | boolean foundColon = copyUntil(input, buffer, 58);
|
| 89 | int codepointDigitCount = buffer.size();
|
| 90 | if (codepointDigitCount == 0 && !foundColon) {
|
| 91 | return;
|
| 92 | }
|
| 93 |
|
| 94 | if (!foundColon || codepointDigitCount != 4 && codepointDigitCount != 5 && codepointDigitCount != 6) {
|
| 95 | throw new IllegalArgumentException("Invalid entry at line " + line + ": expected 4, 5 or 6 hex digits followed by a colon");
|
| 96 | }
|
| 97 |
|
| 98 | int codepoint = 0;
|
| 99 |
|
| 100 | for (int i = 0; i < codepointDigitCount; i++) {
|
| 101 | codepoint = codepoint << 4 | decodeHex(line, buffer.getByte(i));
|
| 102 | }
|
| 103 |
|
| 104 | buffer.clear();
|
| 105 | copyUntil(input, buffer, 10);
|
| 106 | int dataDigitCount = buffer.size();
|
| 107 |
|
| 108 | UnihexProvider.LineData contents = switch (dataDigitCount) {
|
| 109 | case 32 -> UnihexProvider.ByteContents.read(line, buffer);
|
| 110 | case 64 -> UnihexProvider.ShortContents.read(line, buffer);
|
| 111 | case 96 -> UnihexProvider.IntContents.read24(line, buffer);
|
| 112 | case 128 -> UnihexProvider.IntContents.read32(line, buffer);
|
| 113 | default -> throw new IllegalArgumentException(
|
| 114 | "Invalid entry at line " + line + ": expected hex number describing (8,16,24,32) x 16 bitmap, followed by a new line"
|
| 115 | );
|
| 116 | };
|
| 117 | output.accept(codepoint, contents);
|
| 118 | line++;
|
| 119 | buffer.clear();
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | private static int decodeHex(final int line, final ByteList input, final int index) {
|
| 124 | return decodeHex(line, input.getByte(index));
|
| 125 | }
|
| 126 |
|
| 127 | private static int decodeHex(final int line, final byte b) {
|
| 128 | return switch (b) {
|
| 129 | case 48 -> 0;
|
| 130 | case 49 -> 1;
|
| 131 | case 50 -> 2;
|
| 132 | case 51 -> 3;
|
| 133 | case 52 -> 4;
|
| 134 | case 53 -> 5;
|
| 135 | case 54 -> 6;
|
| 136 | case 55 -> 7;
|
| 137 | case 56 -> 8;
|
| 138 | case 57 -> 9;
|
| 139 | default -> throw new IllegalArgumentException("Invalid entry at line " + line + ": expected hex digit, got " + (char)b);
|
| 140 | case 65 -> 10;
|
| 141 | case 66 -> 11;
|
| 142 | case 67 -> 12;
|
| 143 | case 68 -> 13;
|
| 144 | case 69 -> 14;
|
| 145 | case 70 -> 15;
|
| 146 | };
|
| 147 | }
|
| 148 |
|
| 149 | private static boolean copyUntil(final InputStream input, final ByteList output, final int delimiter) throws IOException {
|
| 150 | while (true) {
|
| 151 | int b = input.read();
|
| 152 | if (b == -1) {
|
| 153 | return false;
|
| 154 | }
|
| 155 |
|
| 156 | if (b == delimiter) {
|
| 157 | return true;
|
| 158 | }
|
| 159 |
|
| 160 | output.add((byte)b);
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | @Environment(EnvType.CLIENT)
|
| 165 | private record ByteContents(byte[] contents) implements UnihexProvider.LineData {
|
| 166 | @Override
|
| 167 | public int line(final int index) {
|
| 168 | return this.contents[index] << 24;
|
| 169 | }
|
| 170 |
|
| 171 | private static UnihexProvider.LineData read(final int line, final ByteList input) {
|
| 172 | byte[] content = new byte[16];
|
| 173 | int pos = 0;
|
| 174 |
|
| 175 | for (int i = 0; i < 16; i++) {
|
| 176 | int n1 = UnihexProvider.decodeHex(line, input, pos++);
|
| 177 | int n0 = UnihexProvider.decodeHex(line, input, pos++);
|
| 178 | byte v = (byte)(n1 << 4 | n0);
|
| 179 | content[i] = v;
|
| 180 | }
|
| 181 |
|
| 182 | return new UnihexProvider.ByteContents(content);
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public int bitWidth() {
|
| 187 | return 8;
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | @Environment(EnvType.CLIENT)
|
| 192 | public static class Definition implements GlyphProviderDefinition {
|
| 193 | public static final MapCodec<UnihexProvider.Definition> CODEC = RecordCodecBuilder.mapCodec(
|
| 194 | i -> i.group(
|
| 195 | Identifier.CODEC.fieldOf("hex_file").forGetter(o -> o.hexFile),
|
| 196 | UnihexProvider.OverrideRange.CODEC.listOf().optionalFieldOf("size_overrides", List.of()).forGetter(o -> o.sizeOverrides)
|
| 197 | )
|
| 198 | .apply(i, UnihexProvider.Definition::new)
|
| 199 | );
|
| 200 | private final Identifier hexFile;
|
| 201 | private final List<UnihexProvider.OverrideRange> sizeOverrides;
|
| 202 |
|
| 203 | private Definition(final Identifier hexFile, final List<UnihexProvider.OverrideRange> sizeOverrides) {
|
| 204 | this.hexFile = hexFile;
|
| 205 | this.sizeOverrides = sizeOverrides;
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public GlyphProviderType type() {
|
| 210 | return GlyphProviderType.UNIHEX;
|
| 211 | }
|
| 212 |
|
| 213 | @Override
|
| 214 | public Either<GlyphProviderDefinition.Loader, GlyphProviderDefinition.Reference> unpack() {
|
| 215 | return Either.left(this::load);
|
| 216 | }
|
| 217 |
|
| 218 | private GlyphProvider load(final ResourceManager resourceManager) throws IOException {
|
| 219 | try (InputStream raw = resourceManager.open(this.hexFile)) {
|
| 220 | return this.loadData(raw);
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | private UnihexProvider loadData(final InputStream zipFile) throws IOException {
|
| 225 | CodepointMap<UnihexProvider.LineData> bits = new CodepointMap<>(UnihexProvider.LineData[]::new, UnihexProvider.LineData[][]::new);
|
| 226 | UnihexProvider.ReaderOutput output = bits::put;
|
| 227 |
|
| 228 | try (ZipInputStream zis = new ZipInputStream(zipFile)) {
|
| 229 | ZipEntry entry;
|
| 230 | while ((entry = zis.getNextEntry()) != null) {
|
| 231 | String name = entry.getName();
|
| 232 | if (name.endsWith(".hex")) {
|
| 233 | UnihexProvider.LOGGER.info("Found {}, loading", name);
|
| 234 | UnihexProvider.readFromStream(new FastBufferedInputStream(zis), output);
|
| 235 | }
|
| 236 | }
|
| 237 |
|
| 238 | CodepointMap<UnihexProvider.Glyph> glyphs = new CodepointMap<>(UnihexProvider.Glyph[]::new, UnihexProvider.Glyph[][]::new);
|
| 239 |
|
| 240 | for (UnihexProvider.OverrideRange sizeOverride : this.sizeOverrides) {
|
| 241 | int from = sizeOverride.from;
|
| 242 | int to = sizeOverride.to;
|
| 243 | UnihexProvider.Dimensions size = sizeOverride.dimensions;
|
| 244 |
|
| 245 | for (int c = from; c <= to; c++) {
|
| 246 | UnihexProvider.LineData codepointBits = bits.remove(c);
|
| 247 | if (codepointBits != null) {
|
| 248 | glyphs.put(c, new UnihexProvider.Glyph(codepointBits, size.left, size.right));
|
| 249 | }
|
| 250 | }
|
| 251 | }
|
| 252 |
|
| 253 | bits.forEach((codepoint, glyphBits) -> {
|
| 254 | int packedSize = glyphBits.calculateWidth();
|
| 255 | int left = UnihexProvider.Dimensions.left(packedSize);
|
| 256 | int right = UnihexProvider.Dimensions.right(packedSize);
|
| 257 | glyphs.put(codepoint, new UnihexProvider.Glyph(glyphBits, left, right));
|
| 258 | });
|
| 259 | return new UnihexProvider(glyphs);
|
| 260 | }
|
| 261 | }
|
| 262 | }
|
| 263 |
|
| 264 | @Environment(EnvType.CLIENT)
|
| 265 | public record Dimensions(int left, int right) {
|
| 266 | public static final MapCodec<UnihexProvider.Dimensions> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 267 | i -> i.group(Codec.INT.fieldOf("left").forGetter(UnihexProvider.Dimensions::left), Codec.INT.fieldOf("right").forGetter(UnihexProvider.Dimensions::right))
|
| 268 | .apply(i, UnihexProvider.Dimensions::new)
|
| 269 | );
|
| 270 | public static final Codec<UnihexProvider.Dimensions> CODEC = MAP_CODEC.codec();
|
| 271 |
|
| 272 | public int pack() {
|
| 273 | return pack(this.left, this.right);
|
| 274 | }
|
| 275 |
|
| 276 | public static int pack(final int left, final int right) {
|
| 277 | return (left & 0xFF) << 8 | right & 0xFF;
|
| 278 | }
|
| 279 |
|
| 280 | public static int left(final int packed) {
|
| 281 | return (byte)(packed >> 8);
|
| 282 | }
|
| 283 |
|
| 284 | public static int right(final int packed) {
|
| 285 | return (byte)packed;
|
| 286 | }
|
| 287 | }
|
| 288 |
|
| 289 | @Environment(EnvType.CLIENT)
|
| 290 | private record Glyph(UnihexProvider.LineData contents, int left, int right) implements UnbakedGlyph {
|
| 291 | public int width() {
|
| 292 | return this.right - this.left + 1;
|
| 293 | }
|
| 294 |
|
| 295 | @Override
|
| 296 | public GlyphInfo info() {
|
| 297 | return new 1(this);
|
| 298 | }
|
| 299 |
|
| 300 | @Override
|
| 301 | public BakedGlyph bake(final UnbakedGlyph.Stitcher stitcher) {
|
| 302 | return stitcher.stitch(this.info(), new 2(this));
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | @Environment(EnvType.CLIENT)
|
| 307 | private record IntContents(int[] contents, int bitWidth) implements UnihexProvider.LineData {
|
| 308 | private static final int SIZE_24 = 24;
|
| 309 |
|
| 310 | @Override
|
| 311 | public int line(final int index) {
|
| 312 | return this.contents[index];
|
| 313 | }
|
| 314 |
|
| 315 | private static UnihexProvider.LineData read24(final int line, final ByteList input) {
|
| 316 | int[] content = new int[16];
|
| 317 | int mask = 0;
|
| 318 | int pos = 0;
|
| 319 |
|
| 320 | for (int i = 0; i < 16; i++) {
|
| 321 | int n5 = UnihexProvider.decodeHex(line, input, pos++);
|
| 322 | int n4 = UnihexProvider.decodeHex(line, input, pos++);
|
| 323 | int n3 = UnihexProvider.decodeHex(line, input, pos++);
|
| 324 | int n2 = UnihexProvider.decodeHex(line, input, pos++);
|
| 325 | int n1 = UnihexProvider.decodeHex(line, input, pos++);
|
| 326 | int n0 = UnihexProvider.decodeHex(line, input, pos++);
|
| 327 | int v = n5 << 20 | n4 << 16 | n3 << 12 | n2 << 8 | n1 << 4 | n0;
|
| 328 | content[i] = v << 8;
|
| 329 | mask |= v;
|
| 330 | }
|
| 331 |
|
| 332 | return new UnihexProvider.IntContents(content, 24);
|
| 333 | }
|
| 334 |
|
| 335 | public static UnihexProvider.LineData read32(final int line, final ByteList input) {
|
| 336 | int[] content = new int[16];
|
| 337 | int mask = 0;
|
| 338 | int pos = 0;
|
| 339 |
|
| 340 | for (int i = 0; i < 16; i++) {
|
| 341 | int n7 = UnihexProvider.decodeHex(line, input, pos++);
|
| 342 | int n6 = UnihexProvider.decodeHex(line, input, pos++);
|
| 343 | int n5 = UnihexProvider.decodeHex(line, input, pos++);
|
| 344 | int n4 = UnihexProvider.decodeHex(line, input, pos++);
|
| 345 | int n3 = UnihexProvider.decodeHex(line, input, pos++);
|
| 346 | int n2 = UnihexProvider.decodeHex(line, input, pos++);
|
| 347 | int n1 = UnihexProvider.decodeHex(line, input, pos++);
|
| 348 | int n0 = UnihexProvider.decodeHex(line, input, pos++);
|
| 349 | int v = n7 << 28 | n6 << 24 | n5 << 20 | n4 << 16 | n3 << 12 | n2 << 8 | n1 << 4 | n0;
|
| 350 | content[i] = v;
|
| 351 | mask |= v;
|
| 352 | }
|
| 353 |
|
| 354 | return new UnihexProvider.IntContents(content, 32);
|
| 355 | }
|
| 356 | }
|
| 357 |
|
| 358 | @Environment(EnvType.CLIENT)
|
| 359 | public interface LineData {
|
| 360 | int line(int index);
|
| 361 |
|
| 362 | int bitWidth();
|
| 363 |
|
| 364 | default int mask() {
|
| 365 | int mask = 0;
|
| 366 |
|
| 367 | for (int i = 0; i < 16; i++) {
|
| 368 | mask |= this.line(i);
|
| 369 | }
|
| 370 |
|
| 371 | return mask;
|
| 372 | }
|
| 373 |
|
| 374 | default int calculateWidth() {
|
| 375 | int mask = this.mask();
|
| 376 | int bitWidth = this.bitWidth();
|
| 377 | int left;
|
| 378 | int right;
|
| 379 | if (mask == 0) {
|
| 380 | left = 0;
|
| 381 | right = bitWidth;
|
| 382 | } else {
|
| 383 | left = Integer.numberOfLeadingZeros(mask);
|
| 384 | right = 32 - Integer.numberOfTrailingZeros(mask) - 1;
|
| 385 | }
|
| 386 |
|
| 387 | return UnihexProvider.Dimensions.pack(left, right);
|
| 388 | }
|
| 389 | }
|
| 390 |
|
| 391 | @Environment(EnvType.CLIENT)
|
| 392 | private record OverrideRange(int from, int to, UnihexProvider.Dimensions dimensions) {
|
| 393 | private static final Codec<UnihexProvider.OverrideRange> RAW_CODEC = RecordCodecBuilder.create(
|
| 394 | i -> i.group(
|
| 395 | ExtraCodecs.CODEPOINT.fieldOf("from").forGetter(UnihexProvider.OverrideRange::from),
|
| 396 | ExtraCodecs.CODEPOINT.fieldOf("to").forGetter(UnihexProvider.OverrideRange::to),
|
| 397 | UnihexProvider.Dimensions.MAP_CODEC.forGetter(UnihexProvider.OverrideRange::dimensions)
|
| 398 | )
|
| 399 | .apply(i, UnihexProvider.OverrideRange::new)
|
| 400 | );
|
| 401 | public static final Codec<UnihexProvider.OverrideRange> CODEC = RAW_CODEC.validate(
|
| 402 | o -> o.from >= o.to ? DataResult.error(() -> "Invalid range: [" + o.from + ";" + o.to + "]") : DataResult.success(o)
|
| 403 | );
|
| 404 | }
|
| 405 |
|
| 406 | @FunctionalInterface
|
| 407 | @Environment(EnvType.CLIENT)
|
| 408 | public interface ReaderOutput {
|
| 409 | void accept(int codepoint, UnihexProvider.LineData glyph);
|
| 410 | }
|
| 411 |
|
| 412 | @Environment(EnvType.CLIENT)
|
| 413 | private record ShortContents(short[] contents) implements UnihexProvider.LineData {
|
| 414 | @Override
|
| 415 | public int line(final int index) {
|
| 416 | return this.contents[index] << 16;
|
| 417 | }
|
| 418 |
|
| 419 | private static UnihexProvider.LineData read(final int line, final ByteList input) {
|
| 420 | short[] content = new short[16];
|
| 421 | int pos = 0;
|
| 422 |
|
| 423 | for (int i = 0; i < 16; i++) {
|
| 424 | int n3 = UnihexProvider.decodeHex(line, input, pos++);
|
| 425 | int n2 = UnihexProvider.decodeHex(line, input, pos++);
|
| 426 | int n1 = UnihexProvider.decodeHex(line, input, pos++);
|
| 427 | int n0 = UnihexProvider.decodeHex(line, input, pos++);
|
| 428 | short v = (short)(n3 << 12 | n2 << 8 | n1 << 4 | n0);
|
| 429 | content[i] = v;
|
| 430 | }
|
| 431 |
|
| 432 | return new UnihexProvider.ShortContents(content);
|
| 433 | }
|
| 434 |
|
| 435 | @Override
|
| 436 | public int bitWidth() {
|
| 437 | return 16;
|
| 438 | }
|
| 439 | }
|
| 440 | }
|
| 441 |
|