Skip to content

Commit

Permalink
Scale emote height based on chat line spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinrobot committed Nov 26, 2023
1 parent 1024cbf commit f38d70f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import net.minecraft.client.font.FontStorage;
import net.minecraft.client.font.Glyph;
import net.minecraft.client.font.GlyphRenderer;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.texture.TextureManager;
import net.minecraft.util.Identifier;

import java.util.List;

public abstract class CustomFontStorage extends FontStorage {
public static final float GLYPH_HEIGHT = TextRenderer.ARABIC_SHAPING_LETTERS_SHAPE;

private final Identifier identifier;
private final TextureManager textureManager;
private final GlyphRenderer blankGlyphRenderer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

public class EmoteFontStorage extends CustomFontStorage {
public static final Identifier IDENTIFIER = new Identifier("mcemote.fonts", "emotes");
public static final float GLYPH_HEIGHT = 8;

private final EmotesManager emotesManager;
private final Map<Integer, Future<AnimatedGlyph>> framesCache = new HashMap<>();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.vinrobot.mcemote.client.mixin;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.FontStorage;
import net.minecraft.client.font.Glyph;
import net.minecraft.client.font.GlyphRenderer;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.option.GameOptions;
import net.minecraft.client.render.VertexConsumer;
import net.vinrobot.mcemote.client.font.NativeImageGlyph;
import org.joml.Matrix4f;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import static net.vinrobot.mcemote.client.font.CustomFontStorage.GLYPH_HEIGHT;

@Mixin(TextRenderer.Drawer.class)
public abstract class TextRendererDrawerMixin {
@Accessor("x")
abstract void setX(float x);

@Unique
private volatile Glyph localGlyph = null; // Used to catch the local variable in TextRenderer.Drawer#accept()

@Redirect(method = "accept", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/font/FontStorage;getGlyph(IZ)Lnet/minecraft/client/font/Glyph;"))
private Glyph acceptGetGlyph(final FontStorage instance, final int codePoint, final boolean validateAdvance) {
return this.localGlyph = instance.getGlyph(codePoint, validateAdvance);
}

@Redirect(method = "accept", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/font/TextRenderer;drawGlyph(Lnet/minecraft/client/font/GlyphRenderer;ZZFFFLorg/joml/Matrix4f;Lnet/minecraft/client/render/VertexConsumer;FFFFI)V"))
private void acceptDrawGlyph(final TextRenderer instance, final GlyphRenderer glyphRenderer, final boolean bold, final boolean italic, final float weight, final float x, final float y, final Matrix4f matrix, final VertexConsumer vertexConsumer, final float red, final float green, final float blue, final float alpha, final int light) {
if (this.localGlyph instanceof NativeImageGlyph glyph) {
final GameOptions options = MinecraftClient.getInstance().options;
final float lineSpacing = options.getChatLineSpacing().getValue().floatValue();
final float scale = (lineSpacing + 1);

final float newX = x / scale;
// Align the glyph to the top of the line
final float newY = (y - (lineSpacing * GLYPH_HEIGHT) / 2) / scale;

matrix.scale(scale);
instance.drawGlyph(glyphRenderer, bold, italic, weight, newX, newY, matrix, vertexConsumer, red, green, blue, alpha, light);
matrix.scale(1 / scale);

// Fix the spacing between glyphs
this.setX(x + glyph.getAdvance(bold) * lineSpacing);
} else {
instance.drawGlyph(glyphRenderer, bold, italic, weight, x, y, matrix, vertexConsumer, red, green, blue, alpha, light);
}
}
}
2 changes: 1 addition & 1 deletion src/client/resources/mcemote.client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"client": [
"ChatHudMixin",
"FontManagerMixin",
"MinecraftEmoteClientMixin"
"TextRendererDrawerMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/mcemote.accesswidener
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
accessWidener v1 named

accessible class net/minecraft/client/font/FontManager$ProviderIndex
accessible class net/minecraft/client/font/TextRenderer$Drawer

accessible method net/minecraft/client/font/FontStorage getGlyphRenderer (Lnet/minecraft/client/font/RenderableGlyph;)Lnet/minecraft/client/font/GlyphRenderer;
accessible method net/minecraft/client/font/TextRenderer drawGlyph (Lnet/minecraft/client/font/GlyphRenderer;ZZFFFLorg/joml/Matrix4f;Lnet/minecraft/client/render/VertexConsumer;FFFFI)V

0 comments on commit f38d70f

Please sign in to comment.