| 1 | package net.minecraft.client.gui.font;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.font.GlyphBitmap;
|
| 4 | import com.mojang.blaze3d.font.GlyphInfo;
|
| 5 | import com.mojang.blaze3d.platform.TextureUtil;
|
| 6 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 7 | import com.mojang.renderpearl.api.GpuFormat;
|
| 8 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 9 | import com.mojang.renderpearl.api.textures.FilterMode;
|
| 10 | import java.nio.file.Path;
|
| 11 | import java.util.function.Supplier;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.client.gui.font.glyphs.BakedSheetGlyph;
|
| 15 | import net.minecraft.client.renderer.texture.AbstractTexture;
|
| 16 | import net.minecraft.client.renderer.texture.Dumpable;
|
| 17 | import net.minecraft.client.renderer.texture.DynamicAtlasTree;
|
| 18 | import net.minecraft.client.renderer.texture.DynamicAtlasTreeSlot;
|
| 19 | import net.minecraft.resources.Identifier;
|
| 20 | import net.minecraft.util.ARGB;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class FontTexture extends AbstractTexture implements Dumpable {
|
| 25 | private static final int SIZE = 256;
|
| 26 | private static final int SPACING = 1;
|
| 27 | private final GlyphRenderTypes renderTypes;
|
| 28 | private final boolean colored;
|
| 29 | private final DynamicAtlasTree tree = new DynamicAtlasTree(0, 0, 256, 256);
|
| 30 |
|
| 31 | public FontTexture(final Supplier<String> label, final GlyphRenderTypes renderTypes, final boolean colored) {
|
| 32 | this.colored = colored;
|
| 33 | GpuDevice device = RenderSystem.getDevice();
|
| 34 | this.texture = device.createTexture(label, 7, colored ? GpuFormat.RGBA8_UNORM : GpuFormat.R8_UNORM, 256, 256, 1, 1);
|
| 35 | this.sampler = RenderSystem.getSamplerCache().getRepeat(FilterMode.NEAREST);
|
| 36 | this.textureView = device.createTextureView(this.texture);
|
| 37 | this.renderTypes = renderTypes;
|
| 38 | }
|
| 39 |
|
| 40 | public @Nullable BakedSheetGlyph add(final GlyphInfo info, final GlyphBitmap glyph) {
|
| 41 | if (glyph.isColored() != this.colored) {
|
| 42 | return null;
|
| 43 | } else {
|
| 44 | DynamicAtlasTreeSlot slot = this.tree.insert(glyph.getPixelWidth(), glyph.getPixelHeight(), 1);
|
| 45 | if (slot != null) {
|
| 46 | glyph.upload(slot.x(), slot.y(), this.getTexture());
|
| 47 | float width = 256.0F;
|
| 48 | float height = 256.0F;
|
| 49 | float nudge = 0.01F;
|
| 50 | return new BakedSheetGlyph(
|
| 51 | info,
|
| 52 | this.renderTypes,
|
| 53 | this.getTextureView(),
|
| 54 | (slot.x() + 0.01F) / 256.0F,
|
| 55 | (slot.x() - 0.01F + glyph.getPixelWidth()) / 256.0F,
|
| 56 | (slot.y() + 0.01F) / 256.0F,
|
| 57 | (slot.y() - 0.01F + glyph.getPixelHeight()) / 256.0F,
|
| 58 | glyph.getLeft(),
|
| 59 | glyph.getRight(),
|
| 60 | glyph.getTop(),
|
| 61 | glyph.getBottom()
|
| 62 | );
|
| 63 | } else {
|
| 64 | return null;
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void dumpContents(final Identifier selfId, final Path dir) {
|
| 71 | if (this.texture != null) {
|
| 72 | String outputId = selfId.toDebugFileName();
|
| 73 | TextureUtil.writeAsPNG(dir, outputId, this.texture, 0, argb -> ARGB.alpha(argb) == 0 ? -16777216 : argb);
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|