| 1 | package net.minecraft.client.gui.font.glyphs;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.font.GlyphBitmap;
|
| 4 | import com.mojang.blaze3d.font.GlyphInfo;
|
| 5 | import com.mojang.blaze3d.platform.NativeImage;
|
| 6 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 7 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 8 | import java.util.function.Supplier;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.client.gui.font.GlyphStitcher;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public enum SpecialGlyphs implements GlyphInfo {
|
| 16 | WHITE(() -> generate(5, 8, (x, y) -> -1)),
|
| 17 | MISSING(() -> {
|
| 18 | int width = 5;
|
| 19 | int height = 8;
|
| 20 | return generate(5, 8, (x, y) -> {
|
| 21 | boolean edge = x == 0 || x + 1 == 5 || y == 0 || y + 1 == 8;
|
| 22 | return edge ? -1 : 0;
|
| 23 | });
|
| 24 | });
|
| 25 |
|
| 26 | private final NativeImage image;
|
| 27 |
|
| 28 | private static NativeImage generate(final int width, final int height, final SpecialGlyphs.PixelProvider pixelProvider) {
|
| 29 | NativeImage result = new NativeImage(NativeImage.Format.RGBA, width, height, false);
|
| 30 |
|
| 31 | for (int y = 0; y < height; y++) {
|
| 32 | for (int x = 0; x < width; x++) {
|
| 33 | result.setPixel(x, y, pixelProvider.getColor(x, y));
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | result.untrack();
|
| 38 | return result;
|
| 39 | }
|
| 40 |
|
| 41 | SpecialGlyphs(final Supplier<NativeImage> image) {
|
| 42 | this.image = image.get();
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public float getAdvance() {
|
| 47 | return this.image.getWidth() + 1;
|
| 48 | }
|
| 49 |
|
| 50 | public @Nullable BakedSheetGlyph bake(final GlyphStitcher stitcher) {
|
| 51 | return stitcher.stitch(this, new GlyphBitmap() {
|
| 52 | @Override
|
| 53 | public int getPixelWidth() {
|
| 54 | return SpecialGlyphs.this.image.getWidth();
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public int getPixelHeight() {
|
| 59 | return SpecialGlyphs.this.image.getHeight();
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public float getOversample() {
|
| 64 | return 1.0F;
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public void upload(final int x, final int y, final GpuTexture texture) {
|
| 69 | RenderSystem.getDevice().createCommandEncoder().writeToTexture(texture, SpecialGlyphs.this.image, 0, 0, x, y);
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public boolean isColored() {
|
| 74 | return true;
|
| 75 | }
|
| 76 | });
|
| 77 | }
|
| 78 |
|
| 79 | @FunctionalInterface
|
| 80 | @Environment(EnvType.CLIENT)
|
| 81 | private interface PixelProvider {
|
| 82 | int getColor(int x, int y);
|
| 83 | }
|
| 84 | }
|
| 85 |
|