mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-18 01:05:12 +00:00
MASSIVE FONT RENDERING FIX, HUGE HUGE HUGE
This commit is contained in:
1 parent
05c27c5d20
commit
be4aa2592a
2 files changed
+168
-48
No files matched your search
@@ -3,11 +3,13 @@ package noppes.npcs.config;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import noppes.npcs.LogWriter;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import java.awt.Font;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.font.GlyphVector;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.text.Bidi;
|
||||
import java.util.ArrayList;
|
||||
@@ -71,6 +73,31 @@ public class StringCache {
|
||||
*/
|
||||
private static final int STRIKETHROUGH_THICKNESS = 2;
|
||||
|
||||
// BetterFonts renders glyphs at 2x internal resolution; this scale converts to GUI coords
|
||||
private static final float RENDER_SCALE = 0.5F;
|
||||
private static final boolean FONT_DEBUG_LOG = Boolean.getBoolean("cnpc.font.debug");
|
||||
private static final boolean FONT_DEBUG_DISABLE_INK_OFFSET = Boolean.getBoolean("cnpc.font.debug.disableInkOffset");
|
||||
|
||||
private static boolean isLineBreak(char c) {
|
||||
return c == '\n' || c == '\r';
|
||||
}
|
||||
|
||||
private static boolean isSpacingDebugGlyph(char c) {
|
||||
return c == '(' || c == '"' || c == 't' || c == '.';
|
||||
}
|
||||
|
||||
private static String debugGlyphLabel(char c) {
|
||||
return c == '"' ? "\\\"" : String.valueOf(c);
|
||||
}
|
||||
|
||||
private static float pixelSnap(float coord) {
|
||||
return (float) Math.floor(coord * 2.0F + 0.5F) * RENDER_SCALE;
|
||||
}
|
||||
|
||||
private int baselineOffset = BASELINE_OFFSET;
|
||||
private int underlineOffset = UNDERLINE_OFFSET;
|
||||
private int strikethroughOffset = STRIKETHROUGH_OFFSET;
|
||||
|
||||
/**
|
||||
* Reference to the unicode.FontRenderer class. Needed for creating GlyphVectors and retrieving glyph texture coordinates.
|
||||
*/
|
||||
@@ -319,31 +346,21 @@ public class StringCache {
|
||||
* the string to which this Glyph object belongs.
|
||||
*/
|
||||
static private class Glyph implements Comparable<Glyph> {
|
||||
/**
|
||||
* The index into the original string (i.e. with color codes) for the character that generated this glyph.
|
||||
*/
|
||||
public int stringIndex;
|
||||
|
||||
/**
|
||||
* Texture ID and position/size of the glyph's pre-rendered image within the cache texture.
|
||||
*/
|
||||
public GlyphCache.Entry texture;
|
||||
|
||||
/**
|
||||
* Glyph's horizontal position (in pixels) relative to the entire string's baseline
|
||||
*/
|
||||
/** Logical horizontal position from getGlyphPosition() — used for spacing/advance */
|
||||
public int x;
|
||||
|
||||
/**
|
||||
* Glyph's vertical position (in pixels) relative to the entire string's baseline
|
||||
*/
|
||||
/** Ink vertical position from getGlyphPixelBounds() — used for texture quad placement */
|
||||
public int y;
|
||||
|
||||
/**
|
||||
* Glyph's horizontal advance (in pixels) used for strikethrough and underline effects
|
||||
*/
|
||||
/** Horizontal advance (logical) used for strikethrough and underline effects */
|
||||
public int advance;
|
||||
|
||||
/** Ink X offset from logical position (inkBoundsX - logicalX) for texture quad placement */
|
||||
public int inkOffsetX;
|
||||
|
||||
/**
|
||||
* Allows arrays of Glyph objects to be sorted. Performs numeric comparison on stringIndex.
|
||||
*
|
||||
@@ -452,6 +469,17 @@ public class StringCache {
|
||||
maxY = g.y;
|
||||
}
|
||||
fontHeight = (int) ((maxY + height) / 2.0F - minY / 2.0F);
|
||||
updateFontMetrics();
|
||||
}
|
||||
|
||||
private void updateFontMetrics() {
|
||||
java.awt.font.LineMetrics lm = glyphCache.getLineMetrics("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
|
||||
float ascent = lm.getAscent();
|
||||
float descent = lm.getDescent();
|
||||
|
||||
baselineOffset = (int) (ascent * RENDER_SCALE + 0.5f);
|
||||
underlineOffset = (int) (descent * 0.25f + 0.5f);
|
||||
strikethroughOffset = -(int) (ascent * 0.35f + 0.5f);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -495,7 +523,7 @@ public class StringCache {
|
||||
Entry entry = cacheString(str);
|
||||
|
||||
/* Adjust the baseline of the string because the startY coordinate in Minecraft is for the top of the string */
|
||||
startY += BASELINE_OFFSET;
|
||||
startY += baselineOffset;
|
||||
|
||||
/* Color currently selected by color code; reapplied to Tessellator instance after glBindTexture() */
|
||||
|
||||
@@ -548,7 +576,7 @@ public class StringCache {
|
||||
/* Select the current glyph's texture information and horizontal layout position within this string */
|
||||
Glyph glyph = entry.glyphs[glyphIndex];
|
||||
GlyphCache.Entry texture = glyph.texture;
|
||||
int glyphX = glyph.x;
|
||||
int glyphX = glyph.x + glyph.inkOffsetX;
|
||||
|
||||
/*
|
||||
* Replace ASCII digits in the string with their respective glyphs; strings differing by digits are only cached once.
|
||||
@@ -557,6 +585,10 @@ public class StringCache {
|
||||
* of the width mismatch.
|
||||
*/
|
||||
char c = str.charAt(glyph.stringIndex);
|
||||
if (isLineBreak(c)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c >= '0' && c <= '9') {
|
||||
int oldWidth = texture.width;
|
||||
texture = digitGlyphs[fontStyle][c - '0'].texture;
|
||||
@@ -580,10 +612,20 @@ public class StringCache {
|
||||
}
|
||||
|
||||
/* The divide by 2.0F is needed to align with the scaled GUI coordinate system; startX/startY are already scaled */
|
||||
float x1 = startX + (glyphX) / 2.0F;
|
||||
float x2 = startX + (glyphX + texture.width) / 2.0F;
|
||||
float y1 = startY + (glyph.y) / 2.0F;
|
||||
float y2 = startY + (glyph.y + texture.height) / 2.0F;
|
||||
if (FONT_DEBUG_DISABLE_INK_OFFSET) {
|
||||
glyphX = glyph.x;
|
||||
}
|
||||
float x1 = pixelSnap(startX + (glyphX) * RENDER_SCALE);
|
||||
float x2 = pixelSnap(startX + (glyphX + texture.width) * RENDER_SCALE);
|
||||
float y1 = pixelSnap(startY + (glyph.y) * RENDER_SCALE);
|
||||
float y2 = pixelSnap(startY + (glyph.y + texture.height) * RENDER_SCALE);
|
||||
|
||||
if (FONT_DEBUG_LOG && isSpacingDebugGlyph(c)) {
|
||||
LogWriter.info("[StringCache glyph-debug] char='" + debugGlyphLabel(c) + "' idx=" + glyph.stringIndex +
|
||||
" x=" + glyph.x + " advance=" + glyph.advance + " texW=" + texture.width +
|
||||
" inkOffsetX=" + glyph.inkOffsetX + " quadX1=" + x1 + " quadX2=" + x2 +
|
||||
" y=" + glyph.y + " tex=" + texture.textureName);
|
||||
}
|
||||
|
||||
tessellator.addVertexWithUV(x1, y1, 0, texture.u1, texture.v1);
|
||||
tessellator.addVertexWithUV(x1, y2, 0, texture.u1, texture.v2);
|
||||
@@ -625,10 +667,10 @@ public class StringCache {
|
||||
/* Draw underline under glyph if the style is enabled */
|
||||
if ((renderStyle & ColorCode.UNDERLINE) != 0) {
|
||||
/* The divide by 2.0F is needed to align with the scaled GUI coordinate system; startX/startY are already scaled */
|
||||
float x1 = startX + (glyph.x - glyphSpace) / 2.0F;
|
||||
float x2 = startX + (glyph.x + glyph.advance) / 2.0F;
|
||||
float y1 = startY + (UNDERLINE_OFFSET) / 2.0F;
|
||||
float y2 = startY + (UNDERLINE_OFFSET + UNDERLINE_THICKNESS) / 2.0F;
|
||||
float x1 = pixelSnap(startX + (glyph.x - glyphSpace) * RENDER_SCALE);
|
||||
float x2 = pixelSnap(startX + (glyph.x + glyph.advance) * RENDER_SCALE);
|
||||
float y1 = pixelSnap(startY + (underlineOffset) * RENDER_SCALE);
|
||||
float y2 = pixelSnap(startY + (underlineOffset + UNDERLINE_THICKNESS) * RENDER_SCALE);
|
||||
|
||||
tessellator.addVertex(x1, y1, 0);
|
||||
tessellator.addVertex(x1, y2, 0);
|
||||
@@ -639,10 +681,10 @@ public class StringCache {
|
||||
/* Draw strikethrough in the middle of glyph if the style is enabled */
|
||||
if ((renderStyle & ColorCode.STRIKETHROUGH) != 0) {
|
||||
/* The divide by 2.0F is needed to align with the scaled GUI coordinate system; startX/startY are already scaled */
|
||||
float x1 = startX + (glyph.x - glyphSpace) / 2.0F;
|
||||
float x2 = startX + (glyph.x + glyph.advance) / 2.0F;
|
||||
float y1 = startY + (STRIKETHROUGH_OFFSET) / 2.0F;
|
||||
float y2 = startY + (STRIKETHROUGH_OFFSET + STRIKETHROUGH_THICKNESS) / 2.0F;
|
||||
float x1 = pixelSnap(startX + (glyph.x - glyphSpace) * RENDER_SCALE);
|
||||
float x2 = pixelSnap(startX + (glyph.x + glyph.advance) * RENDER_SCALE);
|
||||
float y1 = pixelSnap(startY + (strikethroughOffset) * RENDER_SCALE);
|
||||
float y2 = pixelSnap(startY + (strikethroughOffset + STRIKETHROUGH_THICKNESS) * RENDER_SCALE);
|
||||
|
||||
tessellator.addVertex(x1, y1, 0);
|
||||
tessellator.addVertex(x1, y2, 0);
|
||||
@@ -1125,6 +1167,11 @@ public class StringCache {
|
||||
|
||||
/* Break the string up into segments, where each segment can be displayed using a single font */
|
||||
while (start < limit) {
|
||||
if (isLineBreak(text[start])) {
|
||||
start++;
|
||||
continue;
|
||||
}
|
||||
|
||||
Font font = glyphCache.lookupFont(text, start, limit, style);
|
||||
int next = font.canDisplayUpTo(text, start, limit);
|
||||
|
||||
@@ -1177,20 +1224,41 @@ public class StringCache {
|
||||
/* Creating a GlyphVector takes care of all language specific OpenType glyph substitutions and positionings */
|
||||
GlyphVector vector = glyphCache.layoutGlyphVector(font, text, start, limit, layoutFlags);
|
||||
|
||||
/*
|
||||
* Use the same GASP-aware FontRenderContext that was used to rasterize glyphs into the atlas.
|
||||
* Passing null here would use a default FRC that can produce different pixel bounds for hinted
|
||||
* glyphs, causing position mismatches and visible spacing gaps (especially on punctuation).
|
||||
*/
|
||||
java.awt.font.FontRenderContext frc = glyphCache.getFontRenderContext();
|
||||
|
||||
/*
|
||||
* Extract all needed information for each glyph from the GlyphVector so it won't be needed for actual rendering.
|
||||
* Note that initially, glyph.start holds the character index into the stripped text array. But after the entire
|
||||
* string is layed out, this field will be adjusted on every Glyph object to correctly index the original unstripped
|
||||
* string.
|
||||
*
|
||||
* Use getGlyphPosition() (logical font metrics) for horizontal placement and advance calculation.
|
||||
* Use getGlyphPixelBounds() only for the Y position and to compute the ink X offset from the logical X.
|
||||
* This separation ensures proper inter-glyph spacing (from logical metrics) while keeping correct
|
||||
* texture quad alignment (from ink bounds). Under GASP hinting, ink bounds can be tighter than
|
||||
* logical positions, causing glyphs to visually touch or overlap if used for spacing.
|
||||
*/
|
||||
Glyph glyph = null;
|
||||
int numGlyphs = vector.getNumGlyphs();
|
||||
int glyphPadding = glyphCache.getGlyphPadding();
|
||||
for (int index = 0; index < numGlyphs; index++) {
|
||||
Point position = vector.getGlyphPixelBounds(index, null, advance, 0).getLocation();
|
||||
int charIndex = start + vector.getGlyphCharIndex(index);
|
||||
if (charIndex >= start && charIndex < limit && isLineBreak(text[charIndex])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Compute horizontal advance for the previous glyph based on this glyph's position */
|
||||
Point2D logicalPos = vector.getGlyphPosition(index);
|
||||
int logicalX = advance + (int) logicalPos.getX();
|
||||
Rectangle inkBounds = vector.getGlyphPixelBounds(index, frc, advance, 0);
|
||||
|
||||
/* Compute horizontal advance for the previous glyph based on logical positions */
|
||||
if (glyph != null) {
|
||||
glyph.advance = position.x - glyph.x;
|
||||
glyph.advance = logicalX - glyph.x;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1198,10 +1266,11 @@ public class StringCache {
|
||||
* it will be corrected later to account for the color codes that have been stripped out.
|
||||
*/
|
||||
glyph = new Glyph();
|
||||
glyph.stringIndex = start + vector.getGlyphCharIndex(index);
|
||||
glyph.stringIndex = charIndex;
|
||||
glyph.texture = glyphCache.lookupGlyph(font, vector.getGlyphCode(index));
|
||||
glyph.x = position.x;
|
||||
glyph.y = position.y;
|
||||
glyph.x = logicalX;
|
||||
glyph.y = inkBounds.y - glyphPadding;
|
||||
glyph.inkOffsetX = (inkBounds.x - logicalX) - glyphPadding;
|
||||
glyphList.add(glyph);
|
||||
}
|
||||
|
||||
@@ -1215,5 +1284,3 @@ public class StringCache {
|
||||
return advance;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user