Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b0aeaef

Browse files
committed
Add font smoothing thanks to @Maschell
It seems smoothER, but there may be room for more improvement
1 parent e33c989 commit b0aeaef

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

src/gui/FreeTypeGX.cpp

100644100755
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "FreeTypeGX.h"
2424
#include "video/CVideo.h"
2525
#include "video/shaders/Texture2DShader.h"
26+
#include "utils/logger.h"
2627

2728
using namespace std;
2829

@@ -369,7 +370,7 @@ int16_t FreeTypeGX::getStyleOffsetHeight(int16_t format, uint16_t pixelSize)
369370
* @return The number of characters printed.
370371
*/
371372

372-
uint16_t FreeTypeGX::drawText(CVideo *video, int16_t x, int16_t y, int16_t z, const wchar_t *text, int16_t pixelSize, const glm::vec4 & color, uint16_t textStyle, uint16_t textWidth, const float &textBlur, const float &colorBlurIntensity, const glm::vec4 & blurColor)
373+
uint16_t FreeTypeGX::drawText(CVideo *video, int16_t x, int16_t y, int16_t z, const wchar_t *text, int16_t pixelSize, const glm::vec4 & color, uint16_t textStyle, uint16_t textWidth, const float &textBlur, const float & colorBlurIntensity, const glm::vec4 & blurColor, const float & internalRenderingScale)
373374
{
374375
if (!text)
375376
return 0;
@@ -389,7 +390,6 @@ uint16_t FreeTypeGX::drawText(CVideo *video, int16_t x, int16_t y, int16_t z, co
389390
}
390391

391392
int i = 0;
392-
393393
while (text[i])
394394
{
395395
ftgxCharData* glyphData = cacheGlyphData(text[i], pixelSize);
@@ -400,9 +400,9 @@ uint16_t FreeTypeGX::drawText(CVideo *video, int16_t x, int16_t y, int16_t z, co
400400
{
401401
FT_Get_Kerning(ftFace, fontData[pixelSize].ftgxCharMap[text[i - 1]].glyphIndex, glyphData->glyphIndex, FT_KERNING_DEFAULT, &pairDelta);
402402
x_pos += (pairDelta.x >> 6);
403-
}
404403

405-
copyTextureToFramebuffer(video, glyphData->texture, x_pos + glyphData->renderOffsetX + x_offset, y + glyphData->renderOffsetY - y_offset, z, color, textBlur, colorBlurIntensity, blurColor);
404+
}
405+
copyTextureToFramebuffer(video, glyphData->texture,x_pos + glyphData->renderOffsetX + x_offset, y + glyphData->renderOffsetY - y_offset, z, color, textBlur, colorBlurIntensity, blurColor,internalRenderingScale);
406406

407407
x_pos += glyphData->glyphAdvanceX;
408408
++printed;
@@ -547,13 +547,13 @@ void FreeTypeGX::getOffset(const wchar_t *text, int16_t pixelSize, uint16_t widt
547547
* @param screenY The screen Y coordinate at which to output the rendered texture.
548548
* @param color Color to apply to the texture.
549549
*/
550-
void FreeTypeGX::copyTextureToFramebuffer(CVideo *pVideo, GX2Texture *texture, int16_t x, int16_t y, int16_t z, const glm::vec4 & color, const float & defaultBlur, const float & blurIntensity, const glm::vec4 & blurColor)
550+
void FreeTypeGX::copyTextureToFramebuffer(CVideo *pVideo, GX2Texture *texture, int16_t x, int16_t y, int16_t z, const glm::vec4 & color, const float & defaultBlur, const float & blurIntensity, const glm::vec4 & blurColor, const float & internalRenderingScale)
551551
{
552552
static const f32 imageAngle = 0.0f;
553-
static const f32 blurScale = 2.0f;
553+
static const f32 blurScale = (2.0f/ (internalRenderingScale));
554554

555-
f32 offsetLeft = 2.0f * ((f32)x + 0.5f * (f32)texture->surface.width) * (f32)pVideo->getWidthScaleFactor();
556-
f32 offsetTop = 2.0f * ((f32)y - 0.5f * (f32)texture->surface.height) * (f32)pVideo->getHeightScaleFactor();
555+
f32 offsetLeft = blurScale * ((f32)x + 0.5f * (f32)texture->surface.width) * (f32)pVideo->getWidthScaleFactor();
556+
f32 offsetTop = blurScale * ((f32)y - 0.5f * (f32)texture->surface.height) * (f32)pVideo->getHeightScaleFactor();
557557

558558
f32 widthScale = blurScale * (f32)texture->surface.width * pVideo->getWidthScaleFactor();
559559
f32 heightScale = blurScale * (f32)texture->surface.height * pVideo->getHeightScaleFactor();

src/gui/FreeTypeGX.h

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ class FreeTypeGX
134134
uint16_t cacheGlyphDataComplete(int16_t pixelSize);
135135
void loadGlyphData(FT_Bitmap *bmp, ftgxCharData *charData);
136136

137-
void copyTextureToFramebuffer(CVideo * pVideo, GX2Texture *tex, int16_t screenX, int16_t screenY, int16_t screenZ, const glm::vec4 & color, const float &textBlur, const float &colorBlurIntensity, const glm::vec4 & blurColor);
137+
void copyTextureToFramebuffer(CVideo * pVideo, GX2Texture *tex, int16_t screenX, int16_t screenY, int16_t screenZ, const glm::vec4 & color, const float &textBlur, const float &colorBlurIntensity, const glm::vec4 & blurColor, const float & internalRenderingScale);
138138

139139
public:
140140
FreeTypeGX(const uint8_t* fontBuffer, FT_Long bufferSize, bool lastFace = false);
141141
~FreeTypeGX();
142142

143143
uint16_t drawText(CVideo * pVideo, int16_t x, int16_t y, int16_t z, const wchar_t *text, int16_t pixelSize, const glm::vec4 & color,
144-
uint16_t textStyling, uint16_t textWidth, const float &textBlur, const float &colorBlurIntensity, const glm::vec4 & blurColor);
144+
uint16_t textStyling, uint16_t textWidth, const float &textBlur, const float &colorBlurIntensity, const glm::vec4 & blurColor, const float & internalRenderingScale);
145145

146146
uint16_t getWidth(const wchar_t *text, int16_t pixelSize);
147147
uint16_t getCharWidth(const wchar_t wChar, int16_t pixelSize, const wchar_t prevChar = 0x0000);

src/gui/GuiText.cpp

100644100755
Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
FreeTypeGX * GuiText::presentFont = NULL;
2222
int GuiText::presetSize = 28;
23+
int GuiText::presetInternalRenderingScale = 2.0f; //Lets render the font at the doubled size. This make it even smoother!
2324
int GuiText::presetMaxWidth = 0xFFFF;
2425
int GuiText::presetAlignment = ALIGN_CENTER | ALIGN_MIDDLE;
2526
GX2ColorF32 GuiText::presetColor = (GX2ColorF32){ 1.0f, 1.0f, 1.0f, 1.0f };
@@ -52,6 +53,7 @@ GuiText::GuiText()
5253
blurGlowIntensity = 0.0f;
5354
blurAlpha = 0.0f;
5455
blurGlowColor = glm::vec4(0.0f);
56+
internalRenderingScale = presetInternalRenderingScale;
5557
}
5658

5759
GuiText::GuiText(const char * t, int s, const glm::vec4 & c)
@@ -74,6 +76,7 @@ GuiText::GuiText(const char * t, int s, const glm::vec4 & c)
7476
blurGlowIntensity = 0.0f;
7577
blurAlpha = 0.0f;
7678
blurGlowColor = glm::vec4(0.0f);
79+
internalRenderingScale = presetInternalRenderingScale;
7780

7881
if(t)
7982
{
@@ -105,6 +108,7 @@ GuiText::GuiText(const wchar_t * t, int s, const glm::vec4 & c)
105108
blurGlowIntensity = 0.0f;
106109
blurAlpha = 0.0f;
107110
blurGlowColor = glm::vec4(0.0f);
111+
internalRenderingScale = presetInternalRenderingScale;
108112

109113
if(t)
110114
{
@@ -141,6 +145,7 @@ GuiText::GuiText(const char * t)
141145
blurGlowIntensity = 0.0f;
142146
blurAlpha = 0.0f;
143147
blurGlowColor = glm::vec4(0.0f);
148+
internalRenderingScale = presetInternalRenderingScale;
144149

145150
if(t)
146151
{
@@ -530,16 +535,23 @@ void GuiText::draw(CVideo *pVideo)
530535

531536
color[3] = getAlpha();
532537
blurGlowColor[3] = blurAlpha * getAlpha();
533-
int newSize = size * getScale();
538+
539+
float finalRenderingScale = 2.0f * internalRenderingScale;
540+
541+
int newSize = size * getScale() * finalRenderingScale;
542+
int normal_size = size * getScale();
534543

535544
if(newSize != currentSize)
536545
{
537-
currentSize = newSize;
546+
currentSize = normal_size;
538547

539548
if(text)
540-
textWidth = font->getWidth(text, currentSize);
549+
textWidth = font->getWidth(text, normal_size);
541550
}
542551

552+
f32 x_pos = getCenterX() * finalRenderingScale;
553+
f32 y_pos = getCenterY() * finalRenderingScale;
554+
543555
if(maxWidth > 0 && maxWidth <= textWidth)
544556
{
545557
if(wrapMode == DOTTED) // text dotted
@@ -552,23 +564,25 @@ void GuiText::draw(CVideo *pVideo)
552564
textDynWidth.resize(textDyn.size());
553565

554566
for(u32 i = 0; i < textDynWidth.size(); i++)
555-
textDynWidth[i] = font->getWidth(textDyn[i], currentSize);
567+
textDynWidth[i] = font->getWidth(textDyn[i], newSize);
556568
}
557569

570+
558571
if(textDyn.size() > 0)
559-
font->drawText(pVideo, getCenterX(), getCenterY(), getDepth(), textDyn[textDyn.size()-1], currentSize, color, alignment, textDynWidth[textDyn.size()-1], defaultBlur, blurGlowIntensity, blurGlowColor);
572+
font->drawText(pVideo, x_pos, y_pos, getDepth(), textDyn[textDyn.size()-1], newSize, color, alignment, textDynWidth[textDyn.size()-1], defaultBlur, blurGlowIntensity, blurGlowColor,finalRenderingScale);
560573
}
561574

562575
else if(wrapMode == SCROLL_HORIZONTAL)
563576
{
564577
scrollText(pVideo->getFrameCount());
565578

566579
if(textDyn.size() > 0)
567-
font->drawText(pVideo, getCenterX(), getCenterY(), getDepth(), textDyn[textDyn.size()-1], currentSize, color, alignment, maxWidth, defaultBlur, blurGlowIntensity, blurGlowColor);
580+
font->drawText(pVideo, x_pos, y_pos, getDepth(), textDyn[textDyn.size()-1], newSize, color, alignment, maxWidth*finalRenderingScale, defaultBlur, blurGlowIntensity, blurGlowColor,finalRenderingScale);
581+
568582
}
569583
else if(wrapMode == WRAP)
570584
{
571-
int lineheight = currentSize + 6;
585+
int lineheight = newSize + 6;
572586
int yoffset = 0;
573587
int voffset = 0;
574588

@@ -580,21 +594,22 @@ void GuiText::draw(CVideo *pVideo)
580594
textDynWidth.resize(textDyn.size());
581595

582596
for(u32 i = 0; i < textDynWidth.size(); i++)
583-
textDynWidth[i] = font->getWidth(textDyn[i], currentSize);
597+
textDynWidth[i] = font->getWidth(textDyn[i], newSize);
584598
}
585599

586600
if(alignment & ALIGN_MIDDLE)
587601
voffset = (lineheight * (textDyn.size()-1)) >> 1;
588602

589603
for(u32 i = 0; i < textDyn.size(); i++)
590604
{
591-
font->drawText(pVideo, getCenterX(), getCenterY() + voffset + yoffset, getDepth(), textDyn[i], currentSize, color, alignment, textDynWidth[i], defaultBlur, blurGlowIntensity, blurGlowColor);
605+
font->drawText(pVideo, x_pos, y_pos + voffset + yoffset, getDepth(), textDyn[i], newSize, color, alignment, textDynWidth[i], defaultBlur, blurGlowIntensity, blurGlowColor,finalRenderingScale);
592606
yoffset -= lineheight;
593607
}
594608
}
595609
}
596610
else
597611
{
598-
font->drawText(pVideo, getCenterX(), getCenterY(), getDepth(), text, currentSize, color, alignment, textWidth, defaultBlur, blurGlowIntensity, blurGlowColor);
612+
uint16_t newtextWidth = font->getWidth(text, newSize);
613+
font->drawText(pVideo, x_pos, y_pos, getDepth(), text, newSize, color, alignment, newtextWidth, defaultBlur, blurGlowIntensity, blurGlowColor,finalRenderingScale);
599614
}
600615
}

src/gui/GuiText.h

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class GuiText : public GuiElement
104104
static FreeTypeGX * presentFont;
105105
static int presetSize;
106106
static int presetMaxWidth;
107+
static int presetInternalRenderingScale;
107108
static int presetAlignment;
108109
static GX2ColorF32 presetColor;
109110

@@ -134,6 +135,7 @@ class GuiText : public GuiElement
134135
float blurGlowIntensity;
135136
float blurAlpha;
136137
glm::vec4 blurGlowColor;
138+
float internalRenderingScale;
137139
};
138140

139141
#endif

0 commit comments

Comments
 (0)