| 1 | package net.minecraft.client.gui.font;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.font.GlyphBitmap;
|
| 4 | import com.mojang.blaze3d.font.GlyphInfo;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.List;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.gui.font.glyphs.BakedSheetGlyph;
|
| 10 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 11 | import net.minecraft.resources.Identifier;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public class GlyphStitcher implements AutoCloseable {
|
| 16 | private final TextureManager textureManager;
|
| 17 | private final Identifier texturePrefix;
|
| 18 | private final List<FontTexture> textures = new ArrayList<>();
|
| 19 |
|
| 20 | public GlyphStitcher(final TextureManager textureManager, final Identifier texturePrefix) {
|
| 21 | this.textureManager = textureManager;
|
| 22 | this.texturePrefix = texturePrefix;
|
| 23 | }
|
| 24 |
|
| 25 | public void reset() {
|
| 26 | int textureCount = this.textures.size();
|
| 27 | this.textures.clear();
|
| 28 |
|
| 29 | for (int i = 0; i < textureCount; i++) {
|
| 30 | this.textureManager.release(this.textureName(i));
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public void close() {
|
| 36 | this.reset();
|
| 37 | }
|
| 38 |
|
| 39 | public @Nullable BakedSheetGlyph stitch(final GlyphInfo info, final GlyphBitmap glyphBitmap) {
|
| 40 | for (FontTexture texture : this.textures) {
|
| 41 | BakedSheetGlyph glyph = texture.add(info, glyphBitmap);
|
| 42 | if (glyph != null) {
|
| 43 | return glyph;
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | int nextIndex = this.textures.size();
|
| 48 | Identifier name = this.textureName(nextIndex);
|
| 49 | boolean isColored = glyphBitmap.isColored();
|
| 50 | GlyphRenderTypes renderTypes = isColored ? GlyphRenderTypes.createForColorTexture(name) : GlyphRenderTypes.createForGrayscaleTexture(name);
|
| 51 | FontTexture texture = new FontTexture(name::toString, renderTypes, isColored);
|
| 52 | this.textures.add(texture);
|
| 53 | this.textureManager.register(name, texture);
|
| 54 | return texture.add(info, glyphBitmap);
|
| 55 | }
|
| 56 |
|
| 57 | private Identifier textureName(final int index) {
|
| 58 | return this.texturePrefix.withSuffix("/" + index);
|
| 59 | }
|
| 60 | }
|
| 61 |
|