| 1 | package net.minecraft.client.gui.font;
|
| 2 |
|
| 3 | import com.google.common.collect.Sets;
|
| 4 | import com.mojang.blaze3d.font.GlyphBitmap;
|
| 5 | import com.mojang.blaze3d.font.GlyphInfo;
|
| 6 | import com.mojang.blaze3d.font.GlyphProvider;
|
| 7 | import com.mojang.blaze3d.font.UnbakedGlyph;
|
| 8 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
| 9 | import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
| 10 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 11 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 12 | import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
| 13 | import it.unimi.dsi.fastutil.ints.IntSet;
|
| 14 | import java.util.ArrayList;
|
| 15 | import java.util.List;
|
| 16 | import java.util.Objects;
|
| 17 | import java.util.Set;
|
| 18 | import java.util.function.IntFunction;
|
| 19 | import java.util.function.Supplier;
|
| 20 | import net.fabricmc.api.EnvType;
|
| 21 | import net.fabricmc.api.Environment;
|
| 22 | import net.minecraft.client.gui.GlyphSource;
|
| 23 | import net.minecraft.client.gui.font.glyphs.BakedGlyph;
|
| 24 | import net.minecraft.client.gui.font.glyphs.EffectGlyph;
|
| 25 | import net.minecraft.client.gui.font.glyphs.SpecialGlyphs;
|
| 26 | import net.minecraft.network.chat.Style;
|
| 27 | import net.minecraft.util.Mth;
|
| 28 | import net.minecraft.util.RandomSource;
|
| 29 | import org.jspecify.annotations.Nullable;
|
| 30 |
|
| 31 | @Environment(EnvType.CLIENT)
|
| 32 | public class FontSet implements AutoCloseable {
|
| 33 | private static final float LARGE_FORWARD_ADVANCE = 32.0F;
|
| 34 | private static final BakedGlyph INVISIBLE_MISSING_GLYPH = new BakedGlyph() {
|
| 35 | @Override
|
| 36 | public GlyphInfo info() {
|
| 37 | return SpecialGlyphs.MISSING;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public TextRenderable.@Nullable Styled createGlyph(
|
| 42 | final float x, final float y, final int color, final int shadowColor, final Style style, final float boldOffset, final float shadowOffset
|
| 43 | ) {
|
| 44 | return null;
|
| 45 | }
|
| 46 | };
|
| 47 | private final GlyphStitcher stitcher;
|
| 48 | private final UnbakedGlyph.Stitcher wrappedStitcher = new UnbakedGlyph.Stitcher() {
|
| 49 | @Override
|
| 50 | public BakedGlyph stitch(final GlyphInfo glyphInfo, final GlyphBitmap glyphBitmap) {
|
| 51 | return Objects.requireNonNullElse(FontSet.this.stitcher.stitch(glyphInfo, glyphBitmap), FontSet.this.missingGlyph);
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public BakedGlyph getMissing() {
|
| 56 | return FontSet.this.missingGlyph;
|
| 57 | }
|
| 58 | };
|
| 59 | private List<GlyphProvider.Conditional> allProviders = List.of();
|
| 60 | private List<GlyphProvider> activeProviders = List.of();
|
| 61 | private final Int2ObjectMap<IntList> glyphsByWidth = new Int2ObjectOpenHashMap<>();
|
| 62 | private final CodepointMap<FontSet.SelectedGlyphs> glyphCache = new CodepointMap<>(FontSet.SelectedGlyphs[]::new, FontSet.SelectedGlyphs[][]::new);
|
| 63 | private final IntFunction<FontSet.SelectedGlyphs> glyphGetter = this::computeGlyphInfo;
|
| 64 | private BakedGlyph missingGlyph = INVISIBLE_MISSING_GLYPH;
|
| 65 | private final Supplier<BakedGlyph> missingGlyphGetter = () -> this.missingGlyph;
|
| 66 | private final FontSet.SelectedGlyphs missingSelectedGlyphs = new FontSet.SelectedGlyphs(this.missingGlyphGetter, this.missingGlyphGetter);
|
| 67 | private @Nullable EffectGlyph whiteGlyph;
|
| 68 | private final GlyphSource anyGlyphs = new FontSet.Source(false);
|
| 69 | private final GlyphSource nonFishyGlyphs = new FontSet.Source(true);
|
| 70 |
|
| 71 | public FontSet(final GlyphStitcher stitcher) {
|
| 72 | this.stitcher = stitcher;
|
| 73 | }
|
| 74 |
|
| 75 | public void reload(final List<GlyphProvider.Conditional> providers, final Set<FontOption> options) {
|
| 76 | this.allProviders = providers;
|
| 77 | this.reload(options);
|
| 78 | }
|
| 79 |
|
| 80 | public void reload(final Set<FontOption> options) {
|
| 81 | this.activeProviders = List.of();
|
| 82 | this.resetTextures();
|
| 83 | this.activeProviders = this.selectProviders(this.allProviders, options);
|
| 84 | }
|
| 85 |
|
| 86 | private void resetTextures() {
|
| 87 | this.stitcher.reset();
|
| 88 | this.glyphCache.clear();
|
| 89 | this.glyphsByWidth.clear();
|
| 90 | this.missingGlyph = Objects.requireNonNull(SpecialGlyphs.MISSING.bake(this.stitcher));
|
| 91 | this.whiteGlyph = SpecialGlyphs.WHITE.bake(this.stitcher);
|
| 92 | }
|
| 93 |
|
| 94 | private List<GlyphProvider> selectProviders(final List<GlyphProvider.Conditional> providers, final Set<FontOption> options) {
|
| 95 | IntSet supportedGlyphs = new IntOpenHashSet();
|
| 96 | List<GlyphProvider> selectedProviders = new ArrayList<>();
|
| 97 |
|
| 98 | for (GlyphProvider.Conditional conditionalProvider : providers) {
|
| 99 | if (conditionalProvider.filter().apply(options)) {
|
| 100 | selectedProviders.add(conditionalProvider.provider());
|
| 101 | supportedGlyphs.addAll(conditionalProvider.provider().getSupportedGlyphs());
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | Set<GlyphProvider> usedProviders = Sets.newHashSet();
|
| 106 | supportedGlyphs.forEach(codepoint -> {
|
| 107 | for (GlyphProvider provider : selectedProviders) {
|
| 108 | UnbakedGlyph glyph = provider.getGlyph(codepoint);
|
| 109 | if (glyph != null) {
|
| 110 | usedProviders.add(provider);
|
| 111 | if (glyph.info() != SpecialGlyphs.MISSING) {
|
| 112 | this.glyphsByWidth.computeIfAbsent(Mth.ceil(glyph.info().getAdvance(false)), w -> new IntArrayList()).add(codepoint);
|
| 113 | }
|
| 114 | break;
|
| 115 | }
|
| 116 | }
|
| 117 | });
|
| 118 | return selectedProviders.stream().filter(usedProviders::contains).toList();
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public void close() {
|
| 123 | this.stitcher.close();
|
| 124 | }
|
| 125 |
|
| 126 | private static boolean hasFishyAdvance(final GlyphInfo glyph) {
|
| 127 | float advance = glyph.getAdvance(false);
|
| 128 | if (!(advance < 0.0F) && !(advance > 32.0F)) {
|
| 129 | float boldAdvance = glyph.getAdvance(true);
|
| 130 | return boldAdvance < 0.0F || boldAdvance > 32.0F;
|
| 131 | } else {
|
| 132 | return true;
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | private FontSet.SelectedGlyphs computeGlyphInfo(final int codepoint) {
|
| 137 | FontSet.DelayedBake firstGlyph = null;
|
| 138 |
|
| 139 | for (GlyphProvider provider : this.activeProviders) {
|
| 140 | UnbakedGlyph glyph = provider.getGlyph(codepoint);
|
| 141 | if (glyph != null) {
|
| 142 | if (firstGlyph == null) {
|
| 143 | firstGlyph = new FontSet.DelayedBake(glyph);
|
| 144 | }
|
| 145 |
|
| 146 | if (!hasFishyAdvance(glyph.info())) {
|
| 147 | if (firstGlyph.unbaked == glyph) {
|
| 148 | return new FontSet.SelectedGlyphs(firstGlyph, firstGlyph);
|
| 149 | }
|
| 150 |
|
| 151 | return new FontSet.SelectedGlyphs(firstGlyph, new FontSet.DelayedBake(glyph));
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | return firstGlyph != null ? new FontSet.SelectedGlyphs(firstGlyph, this.missingGlyphGetter) : this.missingSelectedGlyphs;
|
| 157 | }
|
| 158 |
|
| 159 | private FontSet.SelectedGlyphs getGlyph(final int codepoint) {
|
| 160 | return this.glyphCache.computeIfAbsent(codepoint, this.glyphGetter);
|
| 161 | }
|
| 162 |
|
| 163 | public BakedGlyph getRandomGlyph(final RandomSource random, final int width) {
|
| 164 | IntList chars = this.glyphsByWidth.get(width);
|
| 165 | return chars != null && !chars.isEmpty() ? this.getGlyph(chars.getInt(random.nextInt(chars.size()))).nonFishy().get() : this.missingGlyph;
|
| 166 | }
|
| 167 |
|
| 168 | public EffectGlyph whiteGlyph() {
|
| 169 | return Objects.requireNonNull(this.whiteGlyph);
|
| 170 | }
|
| 171 |
|
| 172 | public GlyphSource source(final boolean nonFishyOnly) {
|
| 173 | return nonFishyOnly ? this.nonFishyGlyphs : this.anyGlyphs;
|
| 174 | }
|
| 175 |
|
| 176 | @Environment(EnvType.CLIENT)
|
| 177 | private class DelayedBake implements Supplier<BakedGlyph> {
|
| 178 | private final UnbakedGlyph unbaked;
|
| 179 | private @Nullable BakedGlyph baked;
|
| 180 |
|
| 181 | private DelayedBake(final UnbakedGlyph unbaked) {
|
| 182 | this.unbaked = unbaked;
|
| 183 | }
|
| 184 |
|
| 185 | public BakedGlyph get() {
|
| 186 | if (this.baked == null) {
|
| 187 | this.baked = this.unbaked.bake(FontSet.this.wrappedStitcher);
|
| 188 | }
|
| 189 |
|
| 190 | return this.baked;
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | @Environment(EnvType.CLIENT)
|
| 195 | private record SelectedGlyphs(Supplier<BakedGlyph> any, Supplier<BakedGlyph> nonFishy) {
|
| 196 | private Supplier<BakedGlyph> select(final boolean filterFishy) {
|
| 197 | return filterFishy ? this.nonFishy : this.any;
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | @Environment(EnvType.CLIENT)
|
| 202 | public class Source implements GlyphSource {
|
| 203 | private final boolean filterFishyGlyphs;
|
| 204 |
|
| 205 | public Source(final boolean filterFishyGlyphs) {
|
| 206 | this.filterFishyGlyphs = filterFishyGlyphs;
|
| 207 | }
|
| 208 |
|
| 209 | @Override
|
| 210 | public BakedGlyph getGlyph(final int codepoint) {
|
| 211 | return FontSet.this.getGlyph(codepoint).select(this.filterFishyGlyphs).get();
|
| 212 | }
|
| 213 |
|
| 214 | @Override
|
| 215 | public BakedGlyph getRandomGlyph(final RandomSource random, final int width) {
|
| 216 | return FontSet.this.getRandomGlyph(random, width);
|
| 217 | }
|
| 218 | }
|
| 219 | }
|
| 220 |
|