Improve macOS Retina UI clarity and bundle HD assets

This commit is contained in:
shen
2026-09-30 16:13:31 -07:00
parent 905f23f135
commit d1f8d3dddd
26 changed files with 505 additions and 134 deletions
+48 -11
View File
@@ -1,5 +1,5 @@
// 40250 EterLib/GrpFontTexture.cpp, verbatim over the GDI emulation in platform/Win32Gdi.cpp. The
// A4R4G4B4 page textures are CGraphicImageTexture memory textures the Godot canvas samples.
// Page textures are memory textures consumed by the native UI renderer.
#include "EterLib/StdAfx.h"
#include "EterLib/GrpText.h"
#include "EterBase/Stl.h"
@@ -8,6 +8,8 @@
#include "UIRenderCommands.h"
#include <unordered_map>
#include <unordered_set>
#include <string>
// PORT: HiDPI glyph pages (UISetFontOversample). With a scale s > 1 the page DIB and texture are
// s times larger and each glyph is drawn with an s-times font into the s-scaled cell, while the
@@ -15,22 +17,25 @@
// 1x values: text layout is unchanged, only the texels get finer.
namespace {
int g_font_oversample = 1;
bool g_font_oversample_latched = false;
std::unordered_set<CGraphicFontTexture*> g_font_textures;
// The 1x HFONT a CGraphicFontTexture measures with -> its s-times twin that draws.
std::unordered_map<HFONT, HFONT> g_oversampled_fonts;
int FontOversample()
{
g_font_oversample_latched = true;
return g_font_oversample;
}
} // namespace
void UISetFontOversample(int scale)
{
if (g_font_oversample_latched)
return;
g_font_oversample = scale < 1 ? 1 : (scale > 4 ? 4 : scale);
scale = scale < 1 ? 1 : (scale > 4 ? 4 : scale);
if (scale == g_font_oversample) return;
g_font_oversample = scale;
// The host calls this between AppFrame steps, when the script thread is suspended.
const auto textures = g_font_textures;
for (auto* texture : textures)
if (!texture->RefreshRasterScale()) TraceError("HiDPI font page rebuild failed");
}
int UIFontOversample()
@@ -64,6 +69,7 @@ bool CGraphicFontTexture::IsEmpty() const
void CGraphicFontTexture::Destroy()
{
g_font_textures.erase(this);
HDC hDC = m_dib.GetDCHandle();
if (hDC)
SelectObject(hDC, m_hFontOld);
@@ -139,10 +145,29 @@ bool CGraphicFontTexture::Create(const char* c_szFontName, int fontSize, bool bI
if (!AppendTexture())
return false;
g_font_textures.insert(this);
return true;
}
bool CGraphicFontTexture::RefreshRasterScale()
{
const std::string name(m_fontName);
const int size = m_fontSize;
const bool italic = m_bItalic;
// Text instances cache pointers into this map. Keep its nodes alive throughout
// Create/Destroy and fill the same nodes with the new page indices and UVs.
TCharacterInfomationMap characters;
characters.swap(m_charInfoMap);
if (!Create(name.c_str(), size, italic)) {
m_charInfoMap.swap(characters);
return false;
}
m_charInfoMap.swap(characters);
for (const auto& character : m_charInfoMap)
if (!UpdateCharacterInfomation(character.first)) return false;
return UpdateTexture();
}
HFONT CGraphicFontTexture::GetFont(WORD codePage)
@@ -195,7 +220,9 @@ bool CGraphicFontTexture::AppendTexture()
{
CGraphicImageTexture * pNewTexture = new CGraphicImageTexture;
if (!pNewTexture->Create(m_dib.GetWidth(), m_dib.GetHeight(), D3DFMT_A4R4G4B4))
// PORT: retain all 256 coverage levels on Retina pages; 1x keeps the original format.
if (!pNewTexture->Create(m_dib.GetWidth(), m_dib.GetHeight(),
FontOversample() > 1 ? D3DFMT_A8R8G8B8 : D3DFMT_A4R4G4B4))
{
delete pNewTexture;
return false;
@@ -220,6 +247,16 @@ bool CGraphicFontTexture::UpdateTexture()
WORD* pwDst;
int pitch;
if (FontOversample() > 1) {
DWORD* dst;
if (!pFontTexture->Lock(&pitch, (void**)&dst)) return false;
const DWORD* src = (const DWORD*)m_dib.GetPointer();
for (int y=0; y<m_dib.GetHeight(); ++y)
std::memcpy((char*)dst + y*pitch, src + y*m_dib.GetWidth(), m_dib.GetWidth()*sizeof(DWORD));
pFontTexture->Unlock();
return true;
}
if (!pFontTexture->Lock(&pitch, (void**)&pwDst))
return false;
@@ -308,7 +345,7 @@ CGraphicFontTexture::TCharacterInfomation* CGraphicFontTexture::UpdateCharacterI
if (hi != g_oversampled_fonts.end())
{
// Draw with the s-times font into the s-scaled cell and keep its anti-aliasing as
// A4R4G4B4 white with coverage alpha (1x keeps 40250's bilevel threshold below).
// A8R8G8B8 white with coverage alpha (1x keeps 40250's bilevel threshold below).
SelectObject(hDC, hi->second);
TextOutW(hDC, m_x * s, m_y * s, &keyValue, 1);
SelectObject(hDC, hFont);
@@ -318,8 +355,8 @@ CGraphicFontTexture::TCharacterInfomation* CGraphicFontTexture::UpdateCharacterI
for (int y = 0; y < size.cy * s; ++y, pdwRow += nDIBWidth)
for (int x = 0; x < size.cx * s; ++x)
{
const DWORD alpha = ((pdwRow[x] & 0xff) * 15 + 127) / 255;
pdwRow[x] = alpha ? ((alpha << 12) | 0x0fff) : 0;
const DWORD alpha = pdwRow[x] & 0xff;
pdwRow[x] = alpha ? ((alpha << 24) | 0x00ffffff) : 0;
}
}
else
+2 -2
View File
@@ -83,7 +83,7 @@ void UIRenderRestoreClip();
// HiDPI text: CGraphicFontTexture rasterises its glyph pages at this multiple of the logical font
// size (the swapchain's pixels per UI pixel) while keeping the 40250 glyph metrics and page UVs in
// logical pixels. 1 (the default) is the verbatim 40250 path. Latched when the first font page is
// created, so set it before the game creates fonts.
// logical pixels. 1 (the default) is the verbatim 40250 path. Changing the scale between
// AppFrame steps rebuilds existing pages without invalidating cached glyph pointers.
void UISetFontOversample(int scale);
int UIFontOversample();