Fix font rendering for color and formatting codes (#1017)

* Fix "Colormatic" splash text rendering as single color

Signed-off-by: Ayush Thoren <ayushthoren@gmail.com>

* Use per-vertex coloring in a single batch

Signed-off-by: Ayush Thoren <ayushthoren@gmail.com>

* Fix font rendering for color and formatting codes

Signed-off-by: Ayush Thoren <ayushthoren@gmail.com>

---------

Signed-off-by: Ayush Thoren <ayushthoren@gmail.com>
This commit is contained in:
Ayush Thoren
2026-03-21 21:33:35 -07:00
committed by GitHub
parent 250accd40b
commit be7e2ca91d
2 changed files with 160 additions and 145 deletions

View File

@@ -45,46 +45,46 @@ Font::Font(Options *options, const wstring& name, Textures* textures, bool enfor
random = new Random(); random = new Random();
// Load the image // Load the image
BufferedImage *img = textures->readImage(textureLocation->getTexture(), name); BufferedImage *img = textures->readImage(textureLocation->getTexture(), name);
/* - 4J - TODO /* - 4J - TODO
try { try {
img = ImageIO.read(Textures.class.getResourceAsStream(name)); img = ImageIO.read(Textures.class.getResourceAsStream(name));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
*/ */
int w = img->getWidth(); int w = img->getWidth();
int h = img->getHeight(); int h = img->getHeight();
intArray rawPixels(w * h); intArray rawPixels(w * h);
img->getRGB(0, 0, w, h, rawPixels, 0, w); img->getRGB(0, 0, w, h, rawPixels, 0, w);
for (int i = 0; i < charC; i++) for (int i = 0; i < charC; i++)
{ {
int xt = i % m_cols; int xt = i % m_cols;
int yt = i / m_cols; int yt = i / m_cols;
int x = 7; int x = 7;
for (; x >= 0; x--) for (; x >= 0; x--)
{ {
int xPixel = xt * 8 + x; int xPixel = xt * 8 + x;
bool emptyColumn = true; bool emptyColumn = true;
for (int y = 0; y < 8 && emptyColumn; y++) for (int y = 0; y < 8 && emptyColumn; y++)
{ {
int yPixel = (yt * 8 + y) * w; int yPixel = (yt * 8 + y) * w;
bool emptyPixel = (rawPixels[xPixel + yPixel] >> 24) == 0; // Check the alpha value bool emptyPixel = (rawPixels[xPixel + yPixel] >> 24) == 0; // Check the alpha value
if (!emptyPixel) emptyColumn = false; if (!emptyPixel) emptyColumn = false;
} }
if (!emptyColumn) if (!emptyColumn)
{ {
break; break;
} }
} }
if (i == ' ') x = 4 - 2; if (i == ' ') x = 4 - 2;
charWidths[i] = x + 2; charWidths[i] = x + 2;
} }
delete img; delete img;
@@ -130,6 +130,7 @@ Font::~Font()
} }
#endif #endif
// Legacy helper used by renderCharacter() only.
void Font::renderStyleLine(float x0, float y0, float x1, float y1) void Font::renderStyleLine(float x0, float y0, float x1, float y1)
{ {
Tesselator* t = Tesselator::getInstance(); Tesselator* t = Tesselator::getInstance();
@@ -146,7 +147,20 @@ void Font::renderStyleLine(float x0, float y0, float x1, float y1)
t->end(); t->end();
} }
void Font::addCharacterQuad(wchar_t c) void Font::addSolidQuad(float x0, float y0, float x1, float y1)
{
Tesselator *t = Tesselator::getInstance();
t->tex(0.0f, 0.0f);
t->vertex(x0, y1, 0.0f);
t->tex(0.0f, 0.0f);
t->vertex(x1, y1, 0.0f);
t->tex(0.0f, 0.0f);
t->vertex(x1, y0, 0.0f);
t->tex(0.0f, 0.0f);
t->vertex(x0, y0, 0.0f);
}
void Font::emitCharacterGeometry(wchar_t c)
{ {
float xOff = c % m_cols * m_charWidth; float xOff = c % m_cols * m_charWidth;
float yOff = c / m_cols * m_charHeight; // was m_charWidth — wrong when glyphs aren't square float yOff = c / m_cols * m_charHeight; // was m_charWidth — wrong when glyphs aren't square
@@ -180,52 +194,45 @@ void Font::addCharacterQuad(wchar_t c)
t->tex(xOff / fontWidth, yOff / fontHeight); t->tex(xOff / fontWidth, yOff / fontHeight);
t->vertex(x0 + dx, y0, 0.0f); t->vertex(x0 + dx, y0, 0.0f);
} }
xPos += static_cast<float>(charWidths[c]);
} }
void Font::addCharacterQuad(wchar_t c)
{
float height = m_charHeight - .01f;
float x0 = xPos;
float y0 = yPos;
float y1 = yPos + height;
float advance = static_cast<float>(charWidths[c]);
emitCharacterGeometry(c);
if (m_underline)
{
addSolidQuad(x0, y1 - 1.0f, xPos + advance, y1);
}
if (m_strikethrough)
{
float mid = y0 + height * 0.5f;
addSolidQuad(x0, mid - 0.5f, xPos + advance, mid + 0.5f);
}
xPos += advance;
}
// Legacy helper used by drawLiteral() only.
void Font::renderCharacter(wchar_t c) void Font::renderCharacter(wchar_t c)
{ {
float xOff = c % m_cols * m_charWidth;
float yOff = c / m_cols * m_charHeight; // was m_charWidth — wrong when glyphs aren't square
float width = charWidths[c] - .01f;
float height = m_charHeight - .01f; float height = m_charHeight - .01f;
float x0 = xPos;
float fontWidth = m_cols * m_charWidth; float y0 = yPos;
float fontHeight = m_rows * m_charHeight; float y1 = yPos + height;
const float shear = m_italic ? (height * 0.25f) : 0.0f;
float x0 = xPos, x1 = xPos + width + shear;
float y0 = yPos, y1 = yPos + height;
Tesselator *t = Tesselator::getInstance(); Tesselator *t = Tesselator::getInstance();
t->begin(); t->begin();
t->tex(xOff / fontWidth, (yOff + 7.99f) / fontHeight); emitCharacterGeometry(c);
t->vertex(x0, y1, 0.0f);
t->tex((xOff + width) / fontWidth, (yOff + 7.99f) / fontHeight);
t->vertex(x1, y1, 0.0f);
t->tex((xOff + width) / fontWidth, yOff / fontHeight);
t->vertex(x1, y0, 0.0f);
t->tex(xOff / fontWidth, yOff / fontHeight);
t->vertex(x0, y0, 0.0f);
t->end(); t->end();
if (m_bold)
{
float dx = 1.0f;
t->begin();
t->tex(xOff / fontWidth, (yOff + 7.99f) / fontHeight);
t->vertex(x0 + dx, y1, 0.0f);
t->tex((xOff + width) / fontWidth, (yOff + 7.99f) / fontHeight);
t->vertex(x1 + dx, y1, 0.0f);
t->tex((xOff + width) / fontWidth, yOff / fontHeight);
t->vertex(x1 + dx, y0, 0.0f);
t->tex(xOff / fontWidth, yOff / fontHeight);
t->vertex(x0 + dx, y0, 0.0f);
t->end();
}
if (m_underline) if (m_underline)
renderStyleLine(x0, y1 - 1.0f, xPos + static_cast<float>(charWidths[c]), y1); renderStyleLine(x0, y1 - 1.0f, xPos + static_cast<float>(charWidths[c]), y1);
@@ -240,8 +247,8 @@ void Font::renderCharacter(wchar_t c)
void Font::drawShadow(const wstring& str, int x, int y, int color) void Font::drawShadow(const wstring& str, int x, int y, int color)
{ {
draw(str, x + 1, y + 1, color, true); draw(str, x + 1, y + 1, color, true);
draw(str, x, y, color, false); draw(str, x, y, color, false);
} }
void Font::drawShadowLiteral(const wstring& str, int x, int y, int color) void Font::drawShadowLiteral(const wstring& str, int x, int y, int color)
@@ -289,7 +296,7 @@ static bool isSectionFormatCode(wchar_t ca)
return l == L'l' || l == L'o' || l == L'n' || l == L'm' || l == L'r' || l == L'k'; return l == L'l' || l == L'o' || l == L'n' || l == L'm' || l == L'r' || l == L'k';
} }
void Font::draw(const wstring &str, bool dropShadow) void Font::draw(const wstring &str, bool dropShadow, int initialColor)
{ {
// Bind the texture // Bind the texture
textures->bindTexture(m_textureLocation); textures->bindTexture(m_textureLocation);
@@ -297,8 +304,11 @@ void Font::draw(const wstring &str, bool dropShadow)
m_bold = m_italic = m_underline = m_strikethrough = false; m_bold = m_italic = m_underline = m_strikethrough = false;
wstring cleanStr = sanitize(str); wstring cleanStr = sanitize(str);
int currentColor = initialColor;
Tesselator *t = Tesselator::getInstance(); Tesselator *t = Tesselator::getInstance();
t->begin(); t->begin();
t->color(currentColor & 0x00ffffff, (currentColor >> 24) & 255);
for (int i = 0; i < static_cast<int>(cleanStr.length()); ++i) for (int i = 0; i < static_cast<int>(cleanStr.length()); ++i)
{ {
@@ -310,10 +320,8 @@ void Font::draw(const wstring &str, bool dropShadow)
wchar_t ca = cleanStr[i+1]; wchar_t ca = cleanStr[i+1];
if (!isSectionFormatCode(ca)) if (!isSectionFormatCode(ca))
{ {
t->end(); addCharacterQuad(167);
renderCharacter(167); addCharacterQuad(ca);
renderCharacter(ca);
t->begin();
i += 1; i += 1;
continue; continue;
} }
@@ -329,7 +337,12 @@ void Font::draw(const wstring &str, bool dropShadow)
else if (l == L'o') m_italic = true; else if (l == L'o') m_italic = true;
else if (l == L'n') m_underline = true; else if (l == L'n') m_underline = true;
else if (l == L'm') m_strikethrough = true; else if (l == L'm') m_strikethrough = true;
else if (l == L'r') m_bold = m_italic = m_underline = m_strikethrough = noise = false; else if (l == L'r')
{
m_bold = m_italic = m_underline = m_strikethrough = noise = false;
currentColor = initialColor;
t->color(currentColor & 0x00ffffff, (currentColor >> 24) & 255);
}
else if (l == L'k') noise = true; else if (l == L'k') noise = true;
} }
else else
@@ -337,8 +350,8 @@ void Font::draw(const wstring &str, bool dropShadow)
noise = false; noise = false;
if (colorN < 0 || colorN > 15) colorN = 15; if (colorN < 0 || colorN > 15) colorN = 15;
if (dropShadow) colorN += 16; if (dropShadow) colorN += 16;
int color = colors[colorN]; currentColor = (initialColor & 0xff000000) | colors[colorN];
glColor3f((color >> 16) / 255.0F, ((color >> 8) & 255) / 255.0F, (color & 255) / 255.0F); t->color(currentColor & 0x00ffffff, (currentColor >> 24) & 255);
} }
i += 1; i += 1;
continue; continue;
@@ -371,11 +384,11 @@ void Font::draw(const wstring& str, int x, int y, int color, bool dropShadow)
if (dropShadow) // divide RGB by 4, preserve alpha if (dropShadow) // divide RGB by 4, preserve alpha
color = (color & 0xfcfcfc) >> 2 | (color & (-1 << 24)); color = (color & 0xfcfcfc) >> 2 | (color & (-1 << 24));
glColor4f((color >> 16 & 255) / 255.0F, (color >> 8 & 255) / 255.0F, (color & 255) / 255.0F, (color >> 24 & 255) / 255.0F); glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
xPos = x; xPos = x;
yPos = y; yPos = y;
draw(str, dropShadow); draw(str, dropShadow, color);
} }
} }
@@ -422,9 +435,9 @@ wstring Font::sanitize(const wstring& str)
{ {
wstring sb = str; wstring sb = str;
for (unsigned int i = 0; i < sb.length(); i++) for (unsigned int i = 0; i < sb.length(); i++)
{ {
if (CharacterExists(sb[i])) if (CharacterExists(sb[i]))
{ {
sb[i] = MapCharacter(sb[i]); sb[i] = MapCharacter(sb[i]);
} }
@@ -433,8 +446,8 @@ wstring Font::sanitize(const wstring& str)
// If this character isn't supported, just show the first character (empty square box character) // If this character isn't supported, just show the first character (empty square box character)
sb[i] = 0; sb[i] = 0;
} }
} }
return sb; return sb;
} }
int Font::MapCharacter(wchar_t c) int Font::MapCharacter(wchar_t c)
@@ -487,95 +500,95 @@ void Font::drawWordWrap(const wstring &string, int x, int y, int w, int col, boo
void Font::drawWordWrapInternal(const wstring& string, int x, int y, int w, int col, bool darken, int h) void Font::drawWordWrapInternal(const wstring& string, int x, int y, int w, int col, bool darken, int h)
{ {
vector<wstring>lines = stringSplit(string,L'\n'); vector<wstring>lines = stringSplit(string,L'\n');
if (lines.size() > 1) if (lines.size() > 1)
{ {
for ( auto& it : lines ) for ( auto& it : lines )
{ {
// 4J Stu - Don't draw text that will be partially cutoff/overlap something it shouldn't // 4J Stu - Don't draw text that will be partially cutoff/overlap something it shouldn't
if( (y + this->wordWrapHeight(it, w)) > h) break; if( (y + this->wordWrapHeight(it, w)) > h) break;
drawWordWrapInternal(it, x, y, w, col, h); drawWordWrapInternal(it, x, y, w, col, h);
y += this->wordWrapHeight(it, w); y += this->wordWrapHeight(it, w);
} }
return; return;
} }
vector<wstring> words = stringSplit(string,L' '); vector<wstring> words = stringSplit(string,L' ');
unsigned int pos = 0; unsigned int pos = 0;
while (pos < words.size()) while (pos < words.size())
{ {
wstring line = words[pos++] + L" "; wstring line = words[pos++] + L" ";
while (pos < words.size() && width(line + words[pos]) < w) while (pos < words.size() && width(line + words[pos]) < w)
{ {
line += words[pos++] + L" "; line += words[pos++] + L" ";
} }
while (width(line) > w) while (width(line) > w)
{ {
int l = 0; int l = 0;
while (width(line.substr(0, l + 1)) <= w) while (width(line.substr(0, l + 1)) <= w)
{ {
l++; l++;
} }
if (trimString(line.substr(0, l)).length() > 0) if (trimString(line.substr(0, l)).length() > 0)
{ {
draw(line.substr(0, l), x, y, col); draw(line.substr(0, l), x, y, col);
y += 8; y += 8;
} }
line = line.substr(l); line = line.substr(l);
// 4J Stu - Don't draw text that will be partially cutoff/overlap something it shouldn't // 4J Stu - Don't draw text that will be partially cutoff/overlap something it shouldn't
if( (y + 8) > h) break; if( (y + 8) > h) break;
} }
// 4J Stu - Don't draw text that will be partially cutoff/overlap something it shouldn't // 4J Stu - Don't draw text that will be partially cutoff/overlap something it shouldn't
if (trimString(line).length() > 0 && !( (y + 8) > h) ) if (trimString(line).length() > 0 && !( (y + 8) > h) )
{ {
draw(line, x, y, col); draw(line, x, y, col);
y += 8; y += 8;
} }
} }
} }
int Font::wordWrapHeight(const wstring& string, int w) int Font::wordWrapHeight(const wstring& string, int w)
{ {
vector<wstring> lines = stringSplit(string,L'\n'); vector<wstring> lines = stringSplit(string,L'\n');
if (lines.size() > 1) if (lines.size() > 1)
{ {
int h = 0; int h = 0;
for ( auto& it : lines ) for ( auto& it : lines )
{ {
h += this->wordWrapHeight(it, w); h += this->wordWrapHeight(it, w);
} }
return h; return h;
} }
vector<wstring> words = stringSplit(string,L' '); vector<wstring> words = stringSplit(string,L' ');
unsigned int pos = 0; unsigned int pos = 0;
int y = 0; int y = 0;
while (pos < words.size()) while (pos < words.size())
{ {
wstring line = words[pos++] + L" "; wstring line = words[pos++] + L" ";
while (pos < words.size() && width(line + words[pos]) < w) while (pos < words.size() && width(line + words[pos]) < w)
{ {
line += words[pos++] + L" "; line += words[pos++] + L" ";
} }
while (width(line) > w) while (width(line) > w)
{ {
int l = 0; int l = 0;
while (width(line.substr(0, l + 1)) <= w) while (width(line.substr(0, l + 1)) <= w)
{ {
l++; l++;
} }
if (trimString(line.substr(0, l)).length() > 0) if (trimString(line.substr(0, l)).length() > 0)
{ {
y += 8; y += 8;
} }
line = line.substr(l); line = line.substr(l);
} }
if (trimString(line).length() > 0) { if (trimString(line).length() > 0) {
y += 8; y += 8;
} }
} }
if (y < 8) y += 8; if (y < 8) y += 8;
return y; return y;
} }

View File

@@ -38,7 +38,7 @@ private:
std::map<int, int> m_charMap; std::map<int, int> m_charMap;
public: public:
Font(Options *options, const wstring& name, Textures* textures, bool enforceUnicode, ResourceLocation *textureLocation, int cols, int rows, int charWidth, int charHeight, unsigned short charMap[] = nullptr); Font(Options *options, const wstring& name, Textures* textures, bool enforceUnicode, ResourceLocation *textureLocation, int cols, int rows, int charWidth, int charHeight, unsigned short charMap[] = nullptr);
#ifndef _XBOX #ifndef _XBOX
// 4J Stu - This dtor clashes with one in xui! We never delete these anyway so take it out for now. Can go back when we have got rid of XUI // 4J Stu - This dtor clashes with one in xui! We never delete these anyway so take it out for now. Can go back when we have got rid of XUI
~Font(); ~Font();
@@ -48,6 +48,8 @@ public:
private: private:
void renderCharacter(wchar_t c); // 4J added void renderCharacter(wchar_t c); // 4J added
void addCharacterQuad(wchar_t c); void addCharacterQuad(wchar_t c);
void addSolidQuad(float x0, float y0, float x1, float y1);
void emitCharacterGeometry(wchar_t c);
void renderStyleLine(float x0, float y0, float x1, float y1); // solid line for underline/strikethrough void renderStyleLine(float x0, float y0, float x1, float y1); // solid line for underline/strikethrough
public: public:
@@ -65,7 +67,7 @@ public:
private: private:
wstring reorderBidi(const wstring &str); wstring reorderBidi(const wstring &str);
void draw(const wstring &str, bool dropShadow); void draw(const wstring &str, bool dropShadow, int baseColor);
void draw(const wstring& str, int x, int y, int color, bool dropShadow); void draw(const wstring& str, int x, int y, int color, bool dropShadow);
void drawLiteral(const wstring& str, int x, int y, int color); // no § parsing void drawLiteral(const wstring& str, int x, int y, int color); // no § parsing
int MapCharacter(wchar_t c); // 4J added int MapCharacter(wchar_t c); // 4J added