Various font improvements and fixes (#2165)

This commit is contained in:
Death Fuel authored and GitHub committed 2026-09-29 14:58:53 -07:00
1 parent 1f274a64ea
commit 9081f4c3e8
15 files changed
+211 -198

No files matched your search

@@ -16,7 +16,6 @@ import com.gtnewhorizons.angelica.glsm.hooks.GLSMConfig;
import com.gtnewhorizons.angelica.glsm.streaming.PersistentStreamingBuffer;
import com.gtnewhorizons.angelica.glsm.streaming.StreamingUploader;
import com.gtnewhorizons.angelica.hudcaching.HUDCaching;
import com.gtnewhorizons.angelica.mixins.interfaces.FontRendererAccessor;
import com.gtnewhorizons.angelica.rendering.tesr.BatchDrawDefaults;
import com.gtnewhorizons.angelica.rendering.tesr.ModelPartBatcher;
import com.gtnewhorizons.angelica.rendering.tesr.TesrBatchRenderer;
@@ -83,6 +82,7 @@ public class BatchingFontRenderer {
private final int AAMode;
private final int AAStrength;
private final int fontBrightness;
private final int alphaTestRefLocation;
private final int mvpMatrixLocation;
private final int lightmapLocation;
@@ -126,10 +126,10 @@ public class BatchingFontRenderer {
FontProviderMC.get(this.isSGA).charWidth = this.charWidth;
FontProviderMC.get(this.isSGA).locationFontTexture = this.locationFontTexture;
//noinspection deprecation
fontShaderId = FontAAShader.getProgram().getProgramId();
AAMode = GLStateManager.glGetUniformLocation(fontShaderId, "aaMode");
AAStrength = GLStateManager.glGetUniformLocation(fontShaderId, "strength");
fontBrightness = GLStateManager.glGetUniformLocation(fontShaderId, "fontBrightness");
alphaTestRefLocation = GLStateManager.glGetUniformLocation(fontShaderId, "alphaTestRef");
mvpMatrixLocation = GLStateManager.glGetUniformLocation(fontShaderId, "u_MVPMatrix");
lightmapLocation = GLStateManager.glGetUniformLocation(fontShaderId, "u_Lightmap");
@@ -144,8 +144,6 @@ public class BatchingFontRenderer {
// === Batched rendering
private static final int INITIAL_BATCH_SIZE = 2048;
private static final ResourceLocation DUMMY_RESOURCE_LOCATION = new ResourceLocation("angelica$dummy",
"this is invalid!");
// Layout in data:
// [v, v, t, t, c, c, c, c, tb, tb, tb, tb]
@@ -402,11 +400,11 @@ public class BatchingFontRenderer {
private static final int LAYER_BACKGROUND = -1;
private static final int LAYER_DEFAULT = 0;
private void pushDrawCmd(int startIdx, int idxCount, ResourceLocation texture, boolean isUnicode) {
private void pushDrawCmd(int startIdx, int idxCount, int texture, boolean isUnicode) {
pushDrawCmd(startIdx, idxCount, texture, isUnicode, LAYER_DEFAULT);
}
private void pushDrawCmd(int startIdx, int idxCount, ResourceLocation texture, boolean isUnicode, int layer) {
private void pushDrawCmd(int startIdx, int idxCount, int texture, boolean isUnicode, int layer) {
arenaOwner = this;
// Never coalesce into a command below the watermark - it belongs to a sealed segment.
if (batchCommands.size() > batchSealedEnd) {
@@ -433,7 +431,7 @@ public class BatchingFontRenderer {
public int startVtx;
public int idxCount;
public boolean isUnicode;
public ResourceLocation texture;
public int texture;
/**
* Sorted ahead of the texture, so lower layers draw first. Sorting by texture
* means submission order is not kept, so anything belonging under the glyphs
@@ -441,11 +439,11 @@ public class BatchingFontRenderer {
*/
public int layer;
public void reset(int startVtx, int vtxCount, ResourceLocation texture, boolean isUnicode) {
public void reset(int startVtx, int vtxCount, int texture, boolean isUnicode) {
reset(startVtx, vtxCount, texture, isUnicode, LAYER_DEFAULT);
}
public void reset(int startVtx, int vtxCount, ResourceLocation texture, boolean isUnicode, int layer) {
public void reset(int startVtx, int vtxCount, int texture, boolean isUnicode, int layer) {
this.layer = layer;
this.startVtx = startVtx;
this.idxCount = vtxCount;
@@ -458,8 +456,7 @@ public class BatchingFontRenderer {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (FontDrawCmd) obj;
return this.startVtx == that.startVtx && this.idxCount == that.idxCount && Objects.equals(this.texture,
that.texture);
return this.startVtx == that.startVtx && this.idxCount == that.idxCount && this.texture == that.texture;
}
@Override
@@ -483,10 +480,8 @@ public class BatchingFontRenderer {
public static final Comparator<FontDrawCmd> DRAW_ORDER_COMPARATOR =
Comparator.comparingInt((FontDrawCmd fdc) -> fdc.layer)
.thenComparing(fdc -> fdc.texture,
Comparator.nullsLast(Comparator.comparing(ResourceLocation::getResourceDomain)
.thenComparing(ResourceLocation::getResourcePath)))
.thenComparing(fdc -> fdc.startVtx);
.thenComparingInt(fdc -> fdc.texture == 0 ? Integer.MAX_VALUE : fdc.texture)
.thenComparingInt(fdc -> fdc.startVtx);
}
/**
@@ -599,7 +594,7 @@ public class BatchingFontRenderer {
deferredIdxPos = 0;
truncateBatchToWatermark();
arenaOwner = null;
flushLastTexture = null;
flushLastTexture = 0;
}
private void deferBatch() {
@@ -704,7 +699,7 @@ public class BatchingFontRenderer {
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0);
deferredSegments.get(0).owner.setupFontDrawState();
GLStateManager.glDepthMask(false);
flushLastTexture = DUMMY_RESOURCE_LOCATION;
flushLastTexture = -1;
resetFlushLightmap();
try (MemoryStack stack = stackPush()) {
final FloatBuffer mvpBuf = stack.mallocFloat(16);
@@ -769,6 +764,7 @@ public class BatchingFontRenderer {
private static final Matrix4f scratchModelView = new Matrix4f();
private int fontAAModeLast = -1;
private int fontAAStrengthLast = -1;
private float fontBrightnessLast = -1;
private void flushBatch() {
final boolean locked = GLStateManager.acquireDrawLock();
@@ -827,7 +823,7 @@ public class BatchingFontRenderer {
final int d = GLStateManager.pushState(StateSet.FONT);
try {
setupFontDrawState();
flushLastTexture = DUMMY_RESOURCE_LOCATION;
flushLastTexture = -1;
resetFlushLightmap();
try (MemoryStack stack = stackPush()) {
final FloatBuffer mvpBuf = stack.mallocFloat(16);
@@ -874,6 +870,10 @@ public class BatchingFontRenderer {
fontAAStrengthLast = FontConfig.fontAAStrength;
GLStateManager.glUniform1f(AAStrength, FontConfig.fontAAStrength / 120.f);
}
if (FontConfig.fontBrightness != fontBrightnessLast) {
fontBrightnessLast = FontConfig.fontBrightness;
GLStateManager.glUniform1f(fontBrightness, 0.625f * FontConfig.fontBrightness + 0.1875f);
}
GLStateManager.glUniform1f(alphaTestRefLocation, GLStateManager.getAlphaState().getReference());
if (lightmapSamplerProgram != fontShaderId) {
GLStateManager.glUniform1i(lightmapSamplerLocation, LIGHTMAP_TEX_UNIT);
@@ -913,7 +913,7 @@ public class BatchingFontRenderer {
}
}
private static ResourceLocation flushLastTexture;
private static int flushLastTexture;
private static float flushLastLightmapU;
private static float flushLastLightmapV;
@@ -950,14 +950,14 @@ public class BatchingFontRenderer {
private static void drawCommands(FontDrawCmd[] cmdsData, int from, int to, BatchingFontRenderer owner) {
for (int i = from; i < to; i++) {
final FontDrawCmd cmd = cmdsData[i];
if (!Objects.equals(flushLastTexture, cmd.texture)) {
if (flushLastTexture == null) {
if (flushLastTexture != cmd.texture) {
if (flushLastTexture == 0) {
GLStateManager.glEnable(GL11.GL_TEXTURE_2D);
} else if (cmd.texture == null) {
} else if (cmd.texture == 0) {
GLStateManager.glDisable(GL11.GL_TEXTURE_2D);
}
if (cmd.texture != null) {
((FontRendererAccessor) owner.underlying).angelica$bindTexture(cmd.texture);
if (cmd.texture != 0) {
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, cmd.texture);
}
flushLastTexture = cmd.texture;
}
@@ -1028,21 +1028,21 @@ public class BatchingFontRenderer {
private static void emitRangeThroughPipeline(BatchingFontRenderer owner, FontDrawCmd[] cmdsData, int from, int to,
int packedLight, float normalX, float normalY, float normalZ) {
final Tessellator tessellator = Tessellator.instance;
ResourceLocation lastTexture = DUMMY_RESOURCE_LOCATION;
int lastTexture = -1;
boolean drawing = false;
try {
for (int i = from; i < to; i++) {
final FontDrawCmd cmd = cmdsData[i];
if (!Objects.equals(lastTexture, cmd.texture)) {
if (lastTexture != cmd.texture) {
if (drawing) {
tessellator.draw();
drawing = false;
}
if (cmd.texture == null) {
if (cmd.texture == 0) {
GLStateManager.disableTexture();
} else {
GLStateManager.enableTexture();
((FontRendererAccessor) owner.underlying).angelica$bindTexture(cmd.texture);
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, cmd.texture);
}
lastTexture = cmd.texture;
}
@@ -1257,7 +1257,7 @@ public class BatchingFontRenderer {
if (curUnderline && underlineStartX != underlineEndX) {
final int ulIdx = idxWriterIndex;
pushUntexRect(underlineStartX, underlineY, underlineEndX - underlineStartX, glyphScaleY, curColor);
pushDrawCmd(ulIdx, 6, null, false);
pushDrawCmd(ulIdx, 6, 0, false);
underlineStartX = underlineEndX;
}
if (curStrikethrough && strikethroughStartX != strikethroughEndX) {
@@ -1268,7 +1268,7 @@ public class BatchingFontRenderer {
strikethroughEndX - strikethroughStartX,
glyphScaleY,
curColor);
pushDrawCmd(ulIdx, 6, null, false);
pushDrawCmd(ulIdx, 6, 0, false);
strikethroughStartX = strikethroughEndX;
}
@@ -1442,11 +1442,11 @@ public class BatchingFontRenderer {
final float glyphW = fontProvider.getGlyphW(chr) * glyphScaleX;
final float uSz = fontProvider.getUSize(chr);
final float vSz = fontProvider.getVSize(chr);
final float itOff = curItalic ? 1.0F : 0.0F; // italic offset
final float itOff = curItalic ? glyphScaleX : 0.0F; // italic offset
final float shadowOffset = fontProvider.getShadowOffset();
final int shadowCopies = FontConfig.shadowCopies;
final int boldCopies = FontConfig.boldCopies;
final ResourceLocation texture = fontProvider.getTexture(chr);
final int texture = fontProvider.getTexture(chr);
// Wave: Y offset via sine wave
float renderY = heightNorth;
@@ -1477,7 +1477,7 @@ public class BatchingFontRenderer {
if (glyphBackground != 0) {
final int bgIdx = idxWriterIndex;
pushUntexRect(renderX, renderY, glyphW, heightSouth, glyphBackground);
pushDrawCmd(bgIdx, 6, null, false, LAYER_BACKGROUND);
pushDrawCmd(bgIdx, 6, 0, false, LAYER_BACKGROUND);
}
// After the background, so this command covers only what is counted below.
@@ -1531,13 +1531,13 @@ public class BatchingFontRenderer {
if (perGlyphColor && curUnderline && underlineStartX != underlineEndX) {
final int ulIdx = idxWriterIndex;
pushUntexRect(underlineStartX, underlineY, underlineEndX - underlineStartX, glyphScaleY, glyphColor);
pushDrawCmd(ulIdx, 6, null, false);
pushDrawCmd(ulIdx, 6, 0, false);
underlineStartX = underlineEndX;
}
if (perGlyphColor && curStrikethrough && strikethroughStartX != strikethroughEndX) {
final int stIdx = idxWriterIndex;
pushUntexRect(strikethroughStartX, strikethroughY, strikethroughEndX - strikethroughStartX, glyphScaleY, glyphColor);
pushDrawCmd(stIdx, 6, null, false);
pushDrawCmd(stIdx, 6, 0, false);
strikethroughStartX = strikethroughEndX;
}
}
@@ -1545,7 +1545,7 @@ public class BatchingFontRenderer {
if (curUnderline && underlineStartX != underlineEndX) {
final int ulIdx = idxWriterIndex;
pushUntexRect(underlineStartX, underlineY, underlineEndX - underlineStartX, glyphScaleY, curColor);
pushDrawCmd(ulIdx, 6, null, false);
pushDrawCmd(ulIdx, 6, 0, false);
}
if (curStrikethrough && strikethroughStartX != strikethroughEndX) {
final int ulIdx = idxWriterIndex;
@@ -1555,7 +1555,7 @@ public class BatchingFontRenderer {
strikethroughEndX - strikethroughStartX,
glyphScaleY,
curColor);
pushDrawCmd(ulIdx, 6, null, false);
pushDrawCmd(ulIdx, 6, 0, false);
}
} finally {
@@ -1,7 +1,5 @@
package com.gtnewhorizons.angelica.client.font;
import net.minecraft.util.ResourceLocation;
public interface FontProvider {
/** Tries before {@link #getRandomReplacement} gives up and leaves the character alone. */
@@ -20,6 +18,6 @@ public interface FontProvider {
float getUSize(char chr);
float getVSize(char chr);
float getShadowOffset();
ResourceLocation getTexture(char chr);
int getTexture(char chr);
float getYScaleMultiplier();
}
@@ -1,34 +1,36 @@
package com.gtnewhorizons.angelica.client.font;
import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memAddress0;
import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memAllocInt;
import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memFree;
import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutInt;
import com.gtnewhorizons.angelica.config.FontConfig;
import com.gtnewhorizons.angelica.glsm.GLStateManager;
import jss.util.RandomXoshiro256StarStar;
import lombok.Setter;
import lombok.Value;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.ITextureObject;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.renderer.texture.TextureUtil;
import net.minecraft.util.ResourceLocation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import javax.imageio.ImageIO;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferInt;
import java.awt.image.Raster;
import java.nio.IntBuffer;
import java.util.Objects;
import static com.gtnewhorizons.angelica.client.font.FontStrategist.getFontName;
public final class FontProviderCustom implements FontProvider {
public static final Logger LOGGER = LogManager.getLogger("Angelica");
public static final String FONT_DIR = "fonts/custom/";
static final int ATLAS_SIZE = 128;
static final int ATLAS_COUNT = 512;
private final byte id; // 0 - primary font, 1 - fallback font
@@ -52,7 +54,7 @@ public final class FontProviderCustom implements FontProvider {
}
int fontPos = -1;
for (int i = 0; i < availableFonts.length; i++) {
if (Objects.equals(myFontName, availableFonts[i].getFontName())) {
if (Objects.equals(myFontName, getFontName(availableFonts[i]))) {
fontPos = i;
break;
}
@@ -75,45 +77,15 @@ public final class FontProviderCustom implements FontProvider {
this.currentFontQuality = FontConfig.customFontQuality;
this.font = FontStrategist.getAvailableFonts()[fontID].deriveFont(this.currentFontQuality);
File[] files = new File(getFontDir()).listFiles();
if (files != null) {
for (File f : files) {
if (!Files.isSymbolicLink(f.toPath())) {
f.delete();
}
}
}
TextureManager tm = Minecraft.getMinecraft().getTextureManager();
Map mapTextureObjects = tm.mapTextureObjects;
for (int i = 0; i < ATLAS_COUNT; i++) {
ResourceLocation key = new ResourceLocation(getAtlasResourceName(i));
if (mapTextureObjects.containsKey(key)) {
ITextureObject obj = (ITextureObject) mapTextureObjects.get(key);
TextureUtil.deleteTexture(obj.getGlTextureId());
mapTextureObjects.remove(key);
for (FontAtlas atlas : fontAtlases) {
if (atlas != null && atlas.texture != 0) {
GLStateManager.glDeleteTextures(atlas.texture);
}
}
this.fontAtlases = new FontAtlas[ATLAS_COUNT];
}
private String getFontDir() {
return FONT_DIR + "f" + this.id + "/";
}
private String getAtlasFilename(int atlasId) {
return "f" + this.id + "p" + atlasId;
}
String getAtlasResourceName(int atlasId) {
return "minecraft:angelica_c" + getAtlasFilename(atlasId);
}
String getAtlasFullPath(int atlasId) {
return getFontDir() + getAtlasFilename(atlasId) + ".png";
}
@Value
private class GlyphData {
float uStart;
@@ -125,9 +97,8 @@ public final class FontProviderCustom implements FontProvider {
}
private class FontAtlas {
GlyphData[] glyphData = new GlyphData[ATLAS_SIZE];
private ResourceLocation texture;
private int texture;
private final int id;
FontAtlas(int id) {
@@ -214,13 +185,46 @@ public final class FontProviderCustom implements FontProvider {
tileX++;
}
g2d.dispose();
try {
Files.createDirectories(Paths.get(getFontDir()));
ImageIO.write(image, "png", new File(getAtlasFullPath(this.id)));
} catch (IOException e) {
e.printStackTrace();
int id = GLStateManager.glGenTextures();
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, id);
GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
final int width = image.getWidth();
final int height = image.getHeight();
final IntBuffer pixelBuffer = memAllocInt(width * height);
long ptr = memAddress0(pixelBuffer);
if (image.getRaster().getDataBuffer() instanceof DataBufferInt dataBufferInt) {
final int[] pixelValues = dataBufferInt.getData();
for (int i : pixelValues) {
memPutInt(ptr, i);
ptr += 4;
}
pixelBuffer.limit(pixelValues.length);
} else {
Raster raster = image.getRaster();
ColorModel colorModel = image.getColorModel();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Object pixel = raster.getDataElements(x, y, null);
memPutInt(ptr, colorModel.getRGB(pixel));
ptr += 4;
}
}
pixelBuffer.limit(image.getHeight() * image.getWidth());
}
this.texture = new ResourceLocation(getAtlasResourceName(this.id));
GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, image.getWidth(), image.getHeight(), 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, pixelBuffer);
memFree(pixelBuffer);
this.texture = id;
}
}
@@ -303,7 +307,7 @@ public final class FontProviderCustom implements FontProvider {
}
@Override
public ResourceLocation getTexture(char chr) {
public int getTexture(char chr) {
return getAtlas(chr).texture;
}
@@ -3,6 +3,10 @@ package com.gtnewhorizons.angelica.client.font;
import com.gtnewhorizons.angelica.config.FontConfig;
import it.unimi.dsi.fastutil.chars.Char2ShortOpenHashMap;
import jss.util.RandomXoshiro256StarStar;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.ITextureObject;
import net.minecraft.client.renderer.texture.SimpleTexture;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.util.ResourceLocation;
public final class FontProviderMC implements FontProvider {
@@ -109,8 +113,15 @@ public final class FontProviderMC implements FontProvider {
}
@Override
public ResourceLocation getTexture(char chr) {
return locationFontTexture;
public int getTexture(char chr) {
final TextureManager tm = Minecraft.getMinecraft().getTextureManager();
ITextureObject tex = tm.getTexture(locationFontTexture);
if (tex == null) {
tex = new SimpleTexture(locationFontTexture);
tm.loadTexture(locationFontTexture, tex);
tex = tm.getTexture(locationFontTexture);
}
return tex.getGlTextureId();
}
@Override
@@ -3,6 +3,9 @@ package com.gtnewhorizons.angelica.client.font;
import com.gtnewhorizons.angelica.config.FontConfig;
import jss.util.RandomXoshiro256StarStar;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.ITextureObject;
import net.minecraft.client.renderer.texture.SimpleTexture;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.resources.IReloadableResourceManager;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
@@ -79,12 +82,12 @@ public final class FontProviderUnicode implements FontProvider, IResourceManager
@Override
public float getUStart(char chr) {
final float startColumnF = (float)((this.glyphWidth[chr] >>> 4) & 15);
return ((float) (chr % 16 * 16) + startColumnF + 0.21f) / 256.0f;
return ((float) (chr % 16 * 16) + startColumnF + 0.01f) / 256.0f;
}
@Override
public float getVStart(char chr) {
return ((float) ((chr & 255) / 16 * 16) + 0.21f) / 256.0f;
return ((float) ((chr & 255) / 16 * 16) + 0.01f) / 256.0f;
}
@Override
@@ -113,12 +116,12 @@ public final class FontProviderUnicode implements FontProvider, IResourceManager
final float startColumnF = (float) startColumn;
final float endColumnF = (float) (endColumn + 1);
final float chrWidth = endColumnF - startColumnF - 0.02F;
return (chrWidth - 0.42f) / 256.0f;
return (chrWidth - 0.02f) / 256.0f;
}
@Override
public float getVSize(char chr) {
return (16.0f - 0.42f) / 256.0f;
return (16.0f - 0.02f) / 256.0f;
}
@Override
@@ -127,9 +130,17 @@ public final class FontProviderUnicode implements FontProvider, IResourceManager
}
@Override
public ResourceLocation getTexture(char chr) {
public int getTexture(char chr) {
final int uniPage = chr / 256;
return getUnicodePageLocation(uniPage);
ResourceLocation rl = getUnicodePageLocation(uniPage);
final TextureManager tm = Minecraft.getMinecraft().getTextureManager();
ITextureObject tex = tm.getTexture(rl);
if (tex == null) {
tex = new SimpleTexture(rl);
tm.loadTexture(rl, tex);
tex = tm.getTexture(rl);
}
return tex.getGlTextureId();
}
@Override
@@ -2,14 +2,11 @@ package com.gtnewhorizons.angelica.client.font;
import com.google.common.collect.HashMultiset;
import com.gtnewhorizons.angelica.config.FontConfig;
import com.gtnewhorizons.angelica.mixins.interfaces.ResourceAccessor;
import cpw.mods.fml.client.SplashProgress;
import cpw.mods.fml.common.versioning.DefaultArtifactVersion;
import lombok.Getter;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.resources.DefaultResourcePack;
import net.minecraft.client.resources.SimpleReloadableResourceManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -25,12 +22,15 @@ import java.nio.charset.StandardCharsets;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
public class FontStrategist {
@Getter
private static final Font[] availableFonts;
private static final Map<Font,String> fontNameMap;
public static final Logger LOGGER = LogManager.getLogger("Angelica");
private static final boolean isSafeToUseAwtEnvironmentData = checkIsSafeToUseAwtEnvironmentData();
@@ -74,20 +74,13 @@ public class FontStrategist {
loadBundledFonts(fontSet);
availableFonts = fontSet.values().stream().sorted(Comparator.comparing(Font::getFontName)).toArray(Font[]::new);
fontNameMap = fontSet.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
availableFonts = fontSet.values().stream().sorted(Comparator.comparing(fontNameMap::get)).toArray(Font[]::new);
}
// create and add the resource pack that provides fonts
HashMap<String, File> packMap = new HashMap<>();
for (int i = 0; i < FontProviderCustom.ATLAS_COUNT; i++) {
packMap.put(FontProviderCustom.getPrimary().getAtlasResourceName(i), new File(FontProviderCustom.getPrimary().getAtlasFullPath(i)));
packMap.put(FontProviderCustom.getFallback().getAtlasResourceName(i), new File(FontProviderCustom.getFallback().getAtlasFullPath(i)));
}
Minecraft mc = Minecraft.getMinecraft();
DefaultResourcePack fontResourcePack = new DefaultResourcePack(packMap);
((ResourceAccessor) mc).angelica$getDefaultResourcePacks().add(fontResourcePack);
((SimpleReloadableResourceManager) mc.getResourceManager()).reloadResourcePack(fontResourcePack);
// reduces allocations
public static String getFontName(Font font) {
return fontNameMap.get(font);
}
private static boolean checkIsSafeToUseAwtEnvironmentData() {
@@ -181,10 +174,11 @@ public class FontStrategist {
FontProviderCustom.getPrimary().setFont(null);
FontProviderCustom.getFallback().setFont(null);
for (int i = 0; i < availableFonts.length; i++) {
if (Objects.equals(FontConfig.customFontNamePrimary, availableFonts[i].getFontName())) {
String fontName = getFontName(availableFonts[i]);
if (Objects.equals(FontConfig.customFontNamePrimary, fontName)) {
FontProviderCustom.getPrimary().reloadFont(i);
}
if (Objects.equals(FontConfig.customFontNameFallback, availableFonts[i].getFontName())) {
if (Objects.equals(FontConfig.customFontNameFallback, fontName)) {
FontProviderCustom.getFallback().reloadFont(i);
}
}
@@ -21,6 +21,8 @@ import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static com.gtnewhorizons.angelica.client.font.FontStrategist.getFontName;
public class FontConfigScreen extends GuiScreen {
private static final Font[] availableFonts = FontStrategist.getAvailableFonts();
@@ -52,11 +54,12 @@ public class FontConfigScreen extends GuiScreen {
SliderClone.Option optShadowCopies = new SliderClone.Option(1, 8, 1);
SliderClone.Option optBoldCopies = new SliderClone.Option(1, 8, 1);
SliderClone.Option optGlyphAspect = new SliderClone.Option(-1, 1, 0.05f);
SliderClone.Option optGlyphScale = new SliderClone.Option(0.1f, 3, 0.05f);
SliderClone.Option optGlyphScale = new SliderClone.Option(0.25f, 2, 0.01f);
SliderClone.Option optWhitespaceScale = new SliderClone.Option(0.1f, 3, 0.05f);
SliderClone.Option optGlyphSpacing = new SliderClone.Option(-2f, 2f, 0.05f);
SliderClone.Option optFontAAMode = new SliderClone.Option(0, 2, 1);
SliderClone.Option optFontAAStrength = new SliderClone.Option(1, 24, 1);
SliderClone.Option optFontBrightness = new SliderClone.Option(0, 1, 0.05f);
SliderClone.Option optCustomFontScale = new SliderClone.Option(0.1f, 3, 0.05f);
SliderClone.Option optShadowOffsetUC = new SliderClone.Option(0, 2, 0.05f);
@@ -69,10 +72,11 @@ public class FontConfigScreen extends GuiScreen {
this.currentFallbackFontName = FontConfig.customFontNameFallback;
this.displayedFonts = new ArrayList<>(Arrays.asList(availableFonts));
for (int i = 0; i < availableFonts.length; i++) {
if (Objects.equals(this.currentPrimaryFontName, availableFonts[i].getFontName())) {
String fontName = getFontName(availableFonts[i]);
if (Objects.equals(this.currentPrimaryFontName, fontName)) {
selectedPrimaryFontListPos = i;
}
if (Objects.equals(this.currentFallbackFontName, availableFonts[i].getFontName())) {
if (Objects.equals(this.currentFallbackFontName, fontName)) {
selectedFallbackFontListPos = i;
}
}
@@ -217,6 +221,16 @@ public class FontConfigScreen extends GuiScreen {
.formatString("%.0f")
.build()
);
sliders.add(new SliderClone.SliderCloneBuilder()
.width(sliderWidth)
.height(sliderHeight)
.option(optFontBrightness)
.initialValue(FontConfig.fontBrightness)
.setter(value -> FontConfig.fontBrightness = value)
.langKey("options.angelica.fontconfig.font_brightness")
.formatString("%3.2f")
.build()
);
sliders.add(new SliderClone.SliderCloneBuilder()
.width(sliderWidth)
.height(sliderHeight)
@@ -278,11 +292,11 @@ public class FontConfigScreen extends GuiScreen {
int pos;
pos = selectedPrimaryFontListPos;
if (pos >= 0 && pos < displayedFonts.size()) {
FontConfig.customFontNamePrimary = displayedFonts.get(pos).getFontName();
FontConfig.customFontNamePrimary = getFontName(displayedFonts.get(pos));
}
pos = selectedFallbackFontListPos;
if (pos >= 0 && pos < displayedFonts.size()) {
FontConfig.customFontNameFallback = displayedFonts.get(pos).getFontName();
FontConfig.customFontNameFallback = getFontName(displayedFonts.get(pos));
}
FontStrategist.reloadCustomFontProviders();
@@ -338,9 +352,9 @@ public class FontConfigScreen extends GuiScreen {
};
}
private float lastMouseX = 0;
private float lastMouseY = 0;
private long lastStillTime = 0;
private int lastMouseX = 0;
private int lastMouseY = 0;
private long lastMovedTime = -1;
@Override
public void drawScreen(int mouseX, int mouseY, float delta) {
drawBackground(0);
@@ -366,8 +380,9 @@ public class FontConfigScreen extends GuiScreen {
this.searchBox.yPosition + this.searchBox.height / 2 - 4, 0xFFFFFF);
}
drawCenteredString(this.fontRendererObj, this.title, (int) (this.width * 0.5), 8, 0xFFFFFF);
drawCenteredString(this.fontRendererObj, I18n.format("options.angelica.fontconfig.currentfonts",
FontConfig.customFontNamePrimary, FontConfig.customFontNameFallback), (int) (this.width * 0.5), this.height - 92, 0xFFFFFF);
final int fontInfoTextY = 92;
String currentFonts = I18n.format("options.angelica.fontconfig.currentfonts", FontConfig.customFontNamePrimary, FontConfig.customFontNameFallback);
drawCenteredString(this.fontRendererObj, currentFonts, (int) (this.width * 0.5), this.height - fontInfoTextY, 0xFFFFFF);
if (this.testAreaInfo == null) {
this.testAreaInfo = this.fontRendererObj.listFormattedStringToWidth(
@@ -398,6 +413,18 @@ public class FontConfigScreen extends GuiScreen {
super.drawScreen(mouseX, mouseY, delta);
// tooltip code, can return
long timeMillis = System.currentTimeMillis();
if (mouseX != lastMouseX || mouseY != lastMouseY) {
lastMovedTime = timeMillis;
lastMouseX = mouseX;
lastMouseY = mouseY;
}
if (lastMovedTime + 500L > timeMillis) { return; }
for (GuiButton guiButton : buttonList) {
if (!(guiButton instanceof SliderClone slider)) { continue; }
final int top = slider.yPosition;
@@ -405,20 +432,18 @@ public class FontConfigScreen extends GuiScreen {
final int left = slider.xPosition;
final int right = slider.xPosition + slider.width;
if (mouseY < top || mouseY >= bot || mouseX < left || mouseX >= right) { continue; }
if (mouseX == lastMouseX && mouseY == lastMouseY) {
if (lastStillTime == 0) {
lastStillTime = System.currentTimeMillis();
}
if (lastStillTime + 500L < System.currentTimeMillis()) {
displayTooltip(mouseX, mouseY, slider.tooltipKey);
}
} else {
lastStillTime = 0;
}
break;
displayTooltip(mouseX, mouseY, slider.tooltipKey);
return;
}
int fontInfoTextWidth = this.fontRendererObj.getStringWidth(currentFonts);
int top = this.height - fontInfoTextY - 8;
int bot = this.height - fontInfoTextY + 16;
int left = this.width / 2 - fontInfoTextWidth / 2 - 8;
int right = this.width / 2 + fontInfoTextWidth / 2 + 8;
if (top <= mouseY && mouseY < bot && left <= mouseX && mouseX < right) {
displayTooltip(mouseX, mouseY, "options.angelica.fontconfig.font_selection_info");
}
lastMouseX = mouseX;
lastMouseY = mouseY;
}
private void displayTooltip(int x, int y, String langKey) {
@@ -477,16 +502,16 @@ public class FontConfigScreen extends GuiScreen {
if (search == null || search.isEmpty()) {
results = new ArrayList<>(Arrays.asList(availableFonts));
} else {
results = Arrays.stream(availableFonts).filter((font -> font.getFontName().toLowerCase().contains(search))).collect(Collectors.toCollection(ArrayList::new));
results = Arrays.stream(availableFonts).filter((font -> getFontName(font).toLowerCase().contains(search))).collect(Collectors.toCollection(ArrayList::new));
}
selectedPrimaryFontListPos = -1;
selectedFallbackFontListPos = -1;
for (int i = 0; i < results.size(); i++) {
if (Objects.equals(currentPrimaryFontName, results.get(i).getFontName())) {
if (Objects.equals(currentPrimaryFontName, getFontName(results.get(i)))) {
selectedPrimaryFontListPos = i;
}
if (Objects.equals(currentFallbackFontName, results.get(i).getFontName())) {
if (Objects.equals(currentFallbackFontName, getFontName(results.get(i)))) {
selectedFallbackFontListPos = i;
}
}
@@ -520,10 +545,10 @@ public class FontConfigScreen extends GuiScreen {
protected void onElemClicked(int index, boolean rightClick) {
if (!rightClick) {
selectedPrimaryFontListPos = index;
currentPrimaryFontName = displayedFonts.get(index).getFontName();
currentPrimaryFontName = getFontName(displayedFonts.get(index));
} else {
selectedFallbackFontListPos = index;
currentFallbackFontName = displayedFonts.get(index).getFontName();
currentFallbackFontName = getFontName(displayedFonts.get(index));
}
applyChanges(false);
}
@@ -566,7 +591,7 @@ public class FontConfigScreen extends GuiScreen {
if (index == selectedFallbackFontListPos) {
color &= 0x55ffff;
}
drawCenteredString(fontRendererObj, displayedFonts.get(index).getFontName(), this.width / 2, y + 1, color);
drawCenteredString(fontRendererObj, getFontName(displayedFonts.get(index)), this.width / 2, y + 1, color);
}
}
}
@@ -64,10 +64,15 @@ public class FontConfig {
public static int fontAAMode;
@Config.Comment("Affects font antialiasing sample spacing. Higher values increase blur.")
@Config.DefaultInt(7)
@Config.DefaultInt(6)
@Config.RangeInt(min = 1, max = 24)
public static int fontAAStrength;
@Config.Comment("Affects the thickness of antialiased fonts. Can improve visibility of thin text.")
@Config.DefaultFloat(0.6f)
@Config.RangeFloat(min = 0, max = 1)
public static float fontBrightness;
@Config.Comment("Custom font scale multiplier, for bugfixes.")
@Config.DefaultFloat(1.5F)
@Config.RangeFloat(min = 0.1F, max = 3F)
@@ -150,7 +150,6 @@ public enum Mixins implements IMixins {
.addClientMixins(
"angelica.fontrenderer.MixinGuiIngameForge"
, "angelica.fontrenderer.MixinFontRenderer"
, "angelica.fontrenderer.MixinMCResourceAccessor"
)
),
@@ -1,13 +1,10 @@
package com.gtnewhorizons.angelica.mixins.interfaces;
import com.gtnewhorizons.angelica.client.font.BatchingFontRenderer;
import net.minecraft.util.ResourceLocation;
public interface FontRendererAccessor {
int angelica$drawStringBatched(String text, int x, int y, int argb, boolean dropShadow);
BatchingFontRenderer angelica$getBatcher();
void angelica$bindTexture(ResourceLocation location);
}
@@ -1,7 +0,0 @@
package com.gtnewhorizons.angelica.mixins.interfaces;
import java.util.List;
public interface ResourceAccessor {
List angelica$getDefaultResourcePacks();
}