| 1 | package net.minecraft.client.gui.font.providers;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.font.GlyphInfo;
|
| 4 | import com.mojang.blaze3d.font.GlyphProvider;
|
| 5 | import com.mojang.blaze3d.font.UnbakedGlyph;
|
| 6 | import com.mojang.blaze3d.platform.NativeImage;
|
| 7 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 8 | import com.mojang.datafixers.util.Either;
|
| 9 | import com.mojang.logging.LogUtils;
|
| 10 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 11 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 12 | import com.mojang.serialization.Codec;
|
| 13 | import com.mojang.serialization.DataResult;
|
| 14 | import com.mojang.serialization.MapCodec;
|
| 15 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 16 | import it.unimi.dsi.fastutil.ints.IntSet;
|
| 17 | import it.unimi.dsi.fastutil.ints.IntSets;
|
| 18 | import java.io.IOException;
|
| 19 | import java.io.InputStream;
|
| 20 | import java.nio.ByteBuffer;
|
| 21 | import java.util.ArrayList;
|
| 22 | import java.util.List;
|
| 23 | import net.fabricmc.api.EnvType;
|
| 24 | import net.fabricmc.api.Environment;
|
| 25 | import net.minecraft.client.gui.font.CodepointMap;
|
| 26 | import net.minecraft.client.gui.font.glyphs.BakedGlyph;
|
| 27 | import net.minecraft.client.gui.font.providers.BitmapProvider.Glyph.1;
|
| 28 | import net.minecraft.resources.Identifier;
|
| 29 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 30 | import org.jspecify.annotations.Nullable;
|
| 31 | import org.lwjgl.system.MemoryUtil;
|
| 32 | import org.slf4j.Logger;
|
| 33 |
|
| 34 | @Environment(EnvType.CLIENT)
|
| 35 | public class BitmapProvider implements GlyphProvider {
|
| 36 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 37 | private final BitmapProvider.ImageDataHolder imageData;
|
| 38 | private final CodepointMap<BitmapProvider.Glyph> glyphs;
|
| 39 |
|
| 40 | private BitmapProvider(final BitmapProvider.ImageDataHolder imageData, final CodepointMap<BitmapProvider.Glyph> glyphs) {
|
| 41 | this.imageData = imageData;
|
| 42 | this.glyphs = glyphs;
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public void close() {
|
| 47 | this.imageData.close();
|
| 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 IntSets.unmodifiable(this.glyphs.keySet());
|
| 58 | }
|
| 59 |
|
| 60 | @Environment(EnvType.CLIENT)
|
| 61 | public record Definition(Identifier file, int height, int ascent, int[][] codepointGrid) implements GlyphProviderDefinition {
|
| 62 | private static final Codec<int[][]> CODEPOINT_GRID_CODEC = Codec.STRING.listOf().xmap(input -> {
|
| 63 | int lineCount = input.size();
|
| 64 | int[][] result = new int[lineCount][];
|
| 65 |
|
| 66 | for (int i = 0; i < lineCount; i++) {
|
| 67 | result[i] = input.get(i).codePoints().toArray();
|
| 68 | }
|
| 69 |
|
| 70 | return result;
|
| 71 | }, grid -> {
|
| 72 | List<String> result = new ArrayList<>(grid.length);
|
| 73 |
|
| 74 | for (int[] line : grid) {
|
| 75 | result.add(new String(line, 0, line.length));
|
| 76 | }
|
| 77 |
|
| 78 | return result;
|
| 79 | }).validate(BitmapProvider.Definition::validateDimensions);
|
| 80 | public static final MapCodec<BitmapProvider.Definition> CODEC = RecordCodecBuilder.<BitmapProvider.Definition>mapCodec(
|
| 81 | i -> i.group(
|
| 82 | Identifier.CODEC.fieldOf("file").forGetter(BitmapProvider.Definition::file),
|
| 83 | Codec.INT.optionalFieldOf("height", 8).forGetter(BitmapProvider.Definition::height),
|
| 84 | Codec.INT.fieldOf("ascent").forGetter(BitmapProvider.Definition::ascent),
|
| 85 | CODEPOINT_GRID_CODEC.fieldOf("chars").forGetter(BitmapProvider.Definition::codepointGrid)
|
| 86 | )
|
| 87 | .apply(i, BitmapProvider.Definition::new)
|
| 88 | )
|
| 89 | .validate(BitmapProvider.Definition::validate);
|
| 90 |
|
| 91 | private static DataResult<int[][]> validateDimensions(final int[][] grid) {
|
| 92 | int lineCount = grid.length;
|
| 93 | if (lineCount == 0) {
|
| 94 | return DataResult.error(() -> "Expected to find data in codepoint grid");
|
| 95 | }
|
| 96 |
|
| 97 | int[] firstLine = grid[0];
|
| 98 | int lineWidth = firstLine.length;
|
| 99 | if (lineWidth == 0) {
|
| 100 | return DataResult.error(() -> "Expected to find data in codepoint grid");
|
| 101 | }
|
| 102 |
|
| 103 | for (int i = 1; i < lineCount; i++) {
|
| 104 | int[] line = grid[i];
|
| 105 | if (line.length != lineWidth) {
|
| 106 | return DataResult.error(
|
| 107 | () -> "Lines in codepoint grid have to be the same length (found: " + line.length + " codepoints, expected: " + lineWidth + "), pad with \\u0000"
|
| 108 | );
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | return DataResult.success(grid);
|
| 113 | }
|
| 114 |
|
| 115 | private static DataResult<BitmapProvider.Definition> validate(final BitmapProvider.Definition builder) {
|
| 116 | return builder.ascent > builder.height
|
| 117 | ? DataResult.error(() -> "Ascent " + builder.ascent + " higher than height " + builder.height)
|
| 118 | : DataResult.success(builder);
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public GlyphProviderType type() {
|
| 123 | return GlyphProviderType.BITMAP;
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public Either<GlyphProviderDefinition.Loader, GlyphProviderDefinition.Reference> unpack() {
|
| 128 | return Either.left(this::load);
|
| 129 | }
|
| 130 |
|
| 131 | private GlyphProvider load(final ResourceManager resourceManager) throws IOException {
|
| 132 | Identifier texture = this.file.withPrefix("textures/");
|
| 133 |
|
| 134 | try (InputStream resource = resourceManager.open(texture)) {
|
| 135 | NativeImage image = NativeImage.read(resource);
|
| 136 | int w = image.getWidth();
|
| 137 | int h = image.getHeight();
|
| 138 | int glyphWidth = w / this.codepointGrid[0].length;
|
| 139 | int glyphHeight = h / this.codepointGrid.length;
|
| 140 | float pixelScale = (float)this.height / glyphHeight;
|
| 141 | CodepointMap<BitmapProvider.Glyph> charMap = new CodepointMap<>(BitmapProvider.Glyph[]::new, BitmapProvider.Glyph[][]::new);
|
| 142 | BitmapProvider.ImageDataHolder imageDataHolder = new BitmapProvider.ImageDataHolder(texture, image);
|
| 143 |
|
| 144 | for (int slotY = 0; slotY < this.codepointGrid.length; slotY++) {
|
| 145 | int linePos = 0;
|
| 146 |
|
| 147 | for (int c : this.codepointGrid[slotY]) {
|
| 148 | int slotX = linePos++;
|
| 149 | if (c != 0) {
|
| 150 | int actualGlyphWidth = this.getActualGlyphWidth(image, glyphWidth, glyphHeight, slotX, slotY);
|
| 151 | BitmapProvider.Glyph prev = charMap.put(
|
| 152 | c,
|
| 153 | new BitmapProvider.Glyph(
|
| 154 | pixelScale,
|
| 155 | imageDataHolder,
|
| 156 | slotX * glyphWidth,
|
| 157 | slotY * glyphHeight,
|
| 158 | glyphWidth,
|
| 159 | glyphHeight,
|
| 160 | (int)(0.5 + actualGlyphWidth * pixelScale) + 1,
|
| 161 | this.ascent
|
| 162 | )
|
| 163 | );
|
| 164 | if (prev != null) {
|
| 165 | BitmapProvider.LOGGER.warn("Codepoint '{}' declared multiple times in {}", Integer.toHexString(c), texture);
|
| 166 | }
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | return new BitmapProvider(imageDataHolder, charMap);
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | private int getActualGlyphWidth(final NativeImage image, final int glyphWidth, final int glyphHeight, final int xGlyph, final int yGlyph) {
|
| 176 | int width;
|
| 177 | for (width = glyphWidth - 1; width >= 0; width--) {
|
| 178 | int xPixel = xGlyph * glyphWidth + width;
|
| 179 |
|
| 180 | for (int y = 0; y < glyphHeight; y++) {
|
| 181 | int yPixel = yGlyph * glyphHeight + y;
|
| 182 | if (image.getLuminanceOrAlpha(xPixel, yPixel) != 0) {
|
| 183 | return width + 1;
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | return width + 1;
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | @Environment(EnvType.CLIENT)
|
| 193 | private record Glyph(float scale, BitmapProvider.ImageDataHolder imageData, int offsetX, int offsetY, int width, int height, int advance, int ascent)
|
| 194 | implements UnbakedGlyph {
|
| 195 | @Override
|
| 196 | public GlyphInfo info() {
|
| 197 | return GlyphInfo.simple(this.advance);
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public BakedGlyph bake(final UnbakedGlyph.Stitcher stitcher) {
|
| 202 | return stitcher.stitch(this.info(), new 1(this));
|
| 203 | }
|
| 204 | }
|
| 205 |
|
| 206 | @Environment(EnvType.CLIENT)
|
| 207 | private static class ImageDataHolder implements AutoCloseable {
|
| 208 | private final Identifier identifier;
|
| 209 | private final NativeImage image;
|
| 210 | private @Nullable GpuBuffer gpuBuffer = null;
|
| 211 |
|
| 212 | private ImageDataHolder(final Identifier identifier, final NativeImage image) {
|
| 213 | this.identifier = identifier;
|
| 214 | this.image = image;
|
| 215 | }
|
| 216 |
|
| 217 | private GpuBuffer gpuData() {
|
| 218 | if (this.gpuBuffer == null) {
|
| 219 | ByteBuffer imageBytes = this.image.getPixelBytes();
|
| 220 | this.gpuBuffer = RenderSystem.getDevice().createBuffer(() -> this.identifier + " staging buffer", 22, imageBytes.remaining());
|
| 221 |
|
| 222 | try (GpuBufferSlice.MappedView mappedView = this.gpuBuffer.map(false, true)) {
|
| 223 | MemoryUtil.memCopy(imageBytes, mappedView.data());
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | return this.gpuBuffer;
|
| 228 | }
|
| 229 |
|
| 230 | @Override
|
| 231 | public void close() {
|
| 232 | this.image.close();
|
| 233 | if (this.gpuBuffer != null) {
|
| 234 | this.gpuBuffer.close();
|
| 235 | }
|
| 236 | }
|
| 237 | }
|
| 238 | }
|
| 239 |
|