Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/build/
/build/
/build-SimpleGraphic
/.idea
2 changes: 2 additions & 0 deletions engine/render.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class r_IRenderer {
virtual int VirtualScreenWidth() = 0;
virtual int VirtualScreenHeight() = 0;
virtual float VirtualScreenScaleFactor() = 0;
virtual void SetDpiScaleOverridePercent(int percent) = 0;
virtual int DpiScaleOverridePercent() const = 0;
virtual int VirtualMap(int properValue) = 0;
virtual int VirtualUnmap(int mappedValue) = 0;

Expand Down
63 changes: 36 additions & 27 deletions engine/render/r_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>

// =======
// Classes
Expand Down Expand Up @@ -144,12 +145,12 @@ int r_font_c::StringWidthInternal(f_fontHeight_s* fh, std::u32string_view str, i
int heightIdx = (int)(std::find(fontHeights, fontHeights + numFontHeight, fh) - fontHeights);
auto tofuFont = FindSmallerFontHeight(height, heightIdx, tofuSizeReduction);

auto measureCodepoint = [](f_fontHeight_s* fh, char32_t cp) {
auto& glyph = fh->Glyph((char)(unsigned char)cp);
auto measureCodepoint = [](f_fontHeight_s* glyphFh, char32_t cp) {
auto& glyph = glyphFh->Glyph((char)(unsigned char)cp);
return glyph.width + glyph.spLeft + glyph.spRight;
};

int width = 0;
float width = 0.0f;
for (size_t idx = 0; idx < str.size();) {
auto ch = str[idx];
int escLen = IsColorEscape(str.substr(idx));
Expand All @@ -158,23 +159,26 @@ int r_font_c::StringWidthInternal(f_fontHeight_s* fh, std::u32string_view str, i
}
else if (ch >= (unsigned)fh->numGlyph) {
auto tofu = BuildTofuString(ch);
for (auto ch : tofu) {
width += measureCodepoint(tofuFont.fh, ch);
for (auto cp : tofu) {
width += measureCodepoint(tofuFont.fh, cp);
width = std::ceil(width);
}
++idx;
}
else if (ch == U'\t') {
auto& glyph = fh->Glyph(' ');
int spWidth = glyph.width + glyph.spLeft + glyph.spRight;
width += (int)(spWidth * 4 * scale);
width += spWidth * 4 * scale;
width = std::ceil(width);
++idx;
}
else {
width += (int)(measureCodepoint(fh, ch) * scale);
width += measureCodepoint(fh, ch) * scale;
width = std::ceil(width);
++idx;
}
}
return width;
return static_cast<int>(width);
}

int r_font_c::StringWidth(int height, std::u32string_view str)
Expand All @@ -187,7 +191,7 @@ int r_font_c::StringWidth(int height, std::u32string_view str)
auto lineEnd = std::find(I, str.end(), U'\n');
if (I != lineEnd) {
std::u32string_view line(&*I, std::distance(I, lineEnd));
int lw = (int)StringWidthInternal(fh, line, height, scale);
int lw = StringWidthInternal(fh, line, height, scale);
max = (std::max)(max, lw);
}
if (lineEnd == str.end()) {
Expand All @@ -203,12 +207,12 @@ size_t r_font_c::StringCursorInternal(f_fontHeight_s* fh, std::u32string_view st
int heightIdx = (int)(std::find(fontHeights, fontHeights + numFontHeight, fh) - fontHeights);
auto tofuFont = FindSmallerFontHeight(height, heightIdx, tofuSizeReduction);

auto measureCodepoint = [](f_fontHeight_s* fh, char32_t cp) {
auto& glyph = fh->Glyph((char)(unsigned char)cp);
auto measureCodepoint = [](f_fontHeight_s* glyphFh, char32_t cp) {
auto& glyph = glyphFh->Glyph((char)(unsigned char)cp);
return glyph.width + glyph.spLeft + glyph.spRight;
};

int x = 0;
float x = 0.0f;
auto I = str.begin();
auto lineEnd = std::find(I, str.end(), U'\n');
while (I != lineEnd) {
Expand All @@ -219,32 +223,34 @@ size_t r_font_c::StringCursorInternal(f_fontHeight_s* fh, std::u32string_view st
}
else if (*I >= (unsigned)fh->numGlyph) {
auto tofu = BuildTofuString(*I);
int tofuWidth = 0;
for (auto ch : tofu) {
tofuWidth += measureCodepoint(tofuFont.fh, ch);
for (auto cp : tofu) {
x += measureCodepoint(tofuFont.fh, cp);
x = std::ceil(x);
if (curX <= x) {
return std::distance(str.begin(), I);
}
}
int halfWidth = (int)(tofuWidth / 2.0f);
x += halfWidth;
if (curX <= x) {
break;
}
x += tofuWidth - halfWidth;
++I;
}
else if (*I == U'\t') {
auto& glyph = fh->Glyph(' ');
int spWidth = glyph.width + glyph.spLeft + glyph.spRight;
int fullWidth = (int)(spWidth * 4 * scale);
int halfWidth = (int)(fullWidth / 2.0f);
float fullWidth = (glyph.width + glyph.spLeft + glyph.spRight) * 4.0f * scale;
float halfWidth = std::ceil(fullWidth / 2.0f);
x += halfWidth;
x = std::ceil(x);
if (curX <= x) {
break;
}
x += fullWidth - halfWidth;
x = std::ceil(x);
if (curX <= x) {
break;
}
++I;
}
else {
x += (int)(measureCodepoint(fh, *I) * scale);
x += measureCodepoint(fh, *I) * scale;
x = std::ceil(x);
if (curX <= x) {
break;
}
Expand Down Expand Up @@ -339,10 +345,10 @@ void r_font_c::DrawTextLine(scp_t pos, int align, int height, col4_t col, std::u

// Calculate the string position
float x = pos[X];
float y = pos[Y];
float y = std::floor(pos[Y]);
if (align != F_LEFT) {
// Calculate the real width of the string
float width = (float)StringWidthInternal(fh, str, height, scale);
float width = StringWidthInternal(fh, str, height, scale);
switch (align) {
case F_CENTRE:
x = floor((renderer->VirtualScreenWidth() - width) / 2.0f + pos[X]);
Expand All @@ -359,6 +365,9 @@ void r_font_c::DrawTextLine(scp_t pos, int align, int height, col4_t col, std::u
}
}

// Snap the starting x position to the pixel grid so the leading glyph isn't blurred.
x = std::round(x);

r_tex_c* curTex{};

auto drawCodepoint = [this, &curTex, &x, y](f_fontHeight_s* fh, int height, float scale, int yShift, char32_t cp) {
Expand Down
21 changes: 17 additions & 4 deletions engine/render/r_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1852,32 +1852,45 @@ int r_renderer_c::DrawStringCursorIndex(int height, int font, const char* str, i
// ==============

int r_renderer_c::VirtualScreenWidth() {
return VirtualMap(sys->video->vid.size[0]);
int const properWidth = apiDpiAware ? sys->video->vid.fbSize[0] : sys->video->vid.size[0];
return VirtualMap(properWidth);
}

int r_renderer_c::VirtualScreenHeight() {
return VirtualMap(sys->video->vid.size[1]);
int const properHeight = apiDpiAware ? sys->video->vid.fbSize[1] : sys->video->vid.size[1];
return VirtualMap(properHeight);
}

float r_renderer_c::VirtualScreenScaleFactor() {
if (apiDpiAware) {
if (dpiScaleOverridePercent > 0) {
return dpiScaleOverridePercent / 100.0f;
}
return sys->video->vid.dpiScale;
}
return 1.0f;
}

void r_renderer_c::SetDpiScaleOverridePercent(int percent) {
dpiScaleOverridePercent = percent;
}

int r_renderer_c::DpiScaleOverridePercent() const {
return dpiScaleOverridePercent;
}

int r_renderer_c::VirtualMap(int properValue) {
if (apiDpiAware) {
return properValue;
}
return (int)(properValue / sys->video->vid.dpiScale);
return static_cast<int>(properValue / sys->video->vid.dpiScale);
}

int r_renderer_c::VirtualUnmap(int mappedValue) {
if (apiDpiAware) {
return mappedValue;
}
return (int)(mappedValue * sys->video->vid.dpiScale);
return static_cast<int>(mappedValue * sys->video->vid.dpiScale);
}

// =====
Expand Down
5 changes: 4 additions & 1 deletion engine/render/r_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class r_renderer_c: public r_IRenderer, public conCmdHandler_c {
int VirtualScreenWidth();
int VirtualScreenHeight();
float VirtualScreenScaleFactor();
void SetDpiScaleOverridePercent(int percent);
int DpiScaleOverridePercent() const;
int VirtualMap(int properValue);
int VirtualUnmap(int mappedValue);

Expand Down Expand Up @@ -126,7 +128,7 @@ class r_renderer_c: public r_IRenderer, public conCmdHandler_c {
PFNGLINSERTEVENTMARKEREXTPROC glInsertEventMarkerEXT = nullptr;
PFNGLPUSHGROUPMARKEREXTPROC glPushGroupMarkerEXT = nullptr;
PFNGLPOPGROUPMARKEREXTPROC glPopGroupMarkerEXT = nullptr;

conVar_c* r_compress = nullptr;
conVar_c* r_screenshotFormat = nullptr;
conVar_c* r_layerDebug = nullptr;
Expand Down Expand Up @@ -173,6 +175,7 @@ class r_renderer_c: public r_IRenderer, public conCmdHandler_c {
};

bool apiDpiAware{};
int dpiScaleOverridePercent = 0;
RenderTarget rttMain[2];
int presentRtt = 0;

Expand Down
Loading
Loading