| 1 | package com.mojang.blaze3d.font;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
| 8 | import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
| 9 | import it.unimi.dsi.fastutil.ints.IntSet;
|
| 10 | import it.unimi.dsi.fastutil.ints.IntSets;
|
| 11 | import java.util.Map;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.client.gui.font.glyphs.EmptyGlyph;
|
| 15 | import net.minecraft.client.gui.font.providers.GlyphProviderDefinition;
|
| 16 | import net.minecraft.client.gui.font.providers.GlyphProviderType;
|
| 17 | import net.minecraft.util.ExtraCodecs;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class SpaceProvider implements GlyphProvider {
|
| 22 | private final Int2ObjectMap<EmptyGlyph> glyphs;
|
| 23 |
|
| 24 | public SpaceProvider(final Map<Integer, Float> advances) {
|
| 25 | this.glyphs = new Int2ObjectOpenHashMap<>(advances.size());
|
| 26 | advances.forEach((codepoint, advance) -> this.glyphs.put(codepoint.intValue(), new EmptyGlyph(advance)));
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public @Nullable UnbakedGlyph getGlyph(final int codepoint) {
|
| 31 | return this.glyphs.get(codepoint);
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public IntSet getSupportedGlyphs() {
|
| 36 | return IntSets.unmodifiable(this.glyphs.keySet());
|
| 37 | }
|
| 38 |
|
| 39 | @Environment(EnvType.CLIENT)
|
| 40 | public record Definition(Map<Integer, Float> advances) implements GlyphProviderDefinition {
|
| 41 | public static final MapCodec<SpaceProvider.Definition> CODEC = RecordCodecBuilder.mapCodec(
|
| 42 | i -> i.group(Codec.unboundedMap(ExtraCodecs.CODEPOINT, Codec.FLOAT).fieldOf("advances").forGetter(SpaceProvider.Definition::advances))
|
| 43 | .apply(i, SpaceProvider.Definition::new)
|
| 44 | );
|
| 45 |
|
| 46 | @Override
|
| 47 | public GlyphProviderType type() {
|
| 48 | return GlyphProviderType.SPACE;
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public Either<GlyphProviderDefinition.Loader, GlyphProviderDefinition.Reference> unpack() {
|
| 53 | GlyphProviderDefinition.Loader loader = resourceManager -> new SpaceProvider(this.advances);
|
| 54 | return Either.left(loader);
|
| 55 | }
|
| 56 | }
|
| 57 | }
|
| 58 |
|