Qt: Use bundled fixed-width font where appropriate

Better than trying to juggle the differences between monospaced fonts on
different platforms.
This commit is contained in:
Stenzek
2025-11-15 20:57:01 +10:00
parent 87847ed248
commit 6c3c02f703
5 changed files with 55 additions and 61 deletions

View File

@@ -445,26 +445,12 @@ void DebuggerWindow::setupAdditionalUi()
{
setWindowIcon(QtHost::GetAppIcon());
#ifdef _WIN32
QFont fixedFont;
fixedFont.setFamily(QStringLiteral("Consolas"));
fixedFont.setFixedPitch(true);
fixedFont.setStyleHint(QFont::TypeWriter);
fixedFont.setPointSize(10);
#else
QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
#ifdef __linux__
// Fonts on Linux tend to be wider, so reduce the size.
// Otherwise the memory view gets cut off.
fixedFont.setPointSize(9);
#endif
#endif
m_ui.codeView->setFont(fixedFont);
const QFont& fixed_font = QtHost::GetFixedFont();
m_ui.codeView->setFont(fixed_font);
m_ui.codeView->updateRowHeight();
m_ui.registerView->setFont(fixedFont);
m_ui.memoryView->setFont(fixedFont);
m_ui.stackView->setFont(fixedFont);
m_ui.registerView->setFont(fixed_font);
m_ui.memoryView->setFont(fixed_font);
m_ui.stackView->setFont(fixed_font);
m_ui.codeView->setContextMenuPolicy(Qt::CustomContextMenu);
m_ui.breakpointsWidget->setContextMenuPolicy(Qt::CustomContextMenu);

View File

@@ -185,18 +185,7 @@ void LogWindow::createUi()
m_text->setTextInteractionFlags(Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse);
m_text->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
m_text->setMaximumBlockCount(MAX_LINES);
#if defined(_WIN32)
QFont font("Consolas");
font.setPointSize(10);
#elif defined(__APPLE__)
QFont font("Monaco");
font.setPointSize(11);
#else
QFont font("Monospace");
font.setStyleHint(QFont::TypeWriter);
#endif
m_text->setFont(font);
m_text->setFont(QtHost::GetFixedFont());
setCentralWidget(m_text);
}

View File

@@ -382,42 +382,28 @@ void MemoryEditorWindow::setupAdditionalUi()
{
setWindowIcon(QtHost::GetAppIcon());
#ifdef _WIN32
QFont fixedFont;
fixedFont.setFamily(QStringLiteral("Consolas"));
fixedFont.setFixedPitch(true);
fixedFont.setStyleHint(QFont::TypeWriter);
fixedFont.setPointSize(10);
#else
QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
#ifdef __linux__
// Fonts on Linux tend to be wider, so reduce the size.
// Otherwise the memory view gets cut off.
fixedFont.setPointSize(9);
#endif
#endif
m_ui.memoryView->setFont(fixedFont);
const QFont& fixed_font = QtHost::GetFixedFont();
m_ui.memoryView->setFont(fixed_font);
if (!QtUtils::RestoreWindowGeometry("MemoryEditorWindow", this))
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
// Set minimum width for data inspector.
m_ui.dataInspectorAddress->setFont(fixedFont);
m_ui.dataInspectorUnsignedByte->setFont(fixedFont);
m_ui.dataInspectorSignedByte->setFont(fixedFont);
m_ui.dataInspectorUnsignedHalfword->setFont(fixedFont);
m_ui.dataInspectorSignedHalfword->setFont(fixedFont);
m_ui.dataInspectorUnsignedWord->setFont(fixedFont);
m_ui.dataInspectorSignedWord->setFont(fixedFont);
m_ui.dataInspectorUnsignedDoubleWord->setFont(fixedFont);
m_ui.dataInspectorSignedDoubleWord->setFont(fixedFont);
m_ui.dataInspectorFloat32->setFont(fixedFont);
m_ui.dataInspectorFloat64->setFont(fixedFont);
m_ui.dataInspectorASCIICharacter->setFont(fixedFont);
m_ui.dataInspectorUTF8String->setFont(fixedFont);
m_ui.dataInspectorAddress->setFont(fixed_font);
m_ui.dataInspectorUnsignedByte->setFont(fixed_font);
m_ui.dataInspectorSignedByte->setFont(fixed_font);
m_ui.dataInspectorUnsignedHalfword->setFont(fixed_font);
m_ui.dataInspectorSignedHalfword->setFont(fixed_font);
m_ui.dataInspectorUnsignedWord->setFont(fixed_font);
m_ui.dataInspectorSignedWord->setFont(fixed_font);
m_ui.dataInspectorUnsignedDoubleWord->setFont(fixed_font);
m_ui.dataInspectorSignedDoubleWord->setFont(fixed_font);
m_ui.dataInspectorFloat32->setFont(fixed_font);
m_ui.dataInspectorFloat64->setFont(fixed_font);
m_ui.dataInspectorASCIICharacter->setFont(fixed_font);
m_ui.dataInspectorUTF8String->setFont(fixed_font);
m_ui.dataInspectorSignedDoubleWord->setMinimumWidth(
QFontMetrics(fixedFont).size(0, QStringLiteral("-8888888888888888888888")).width());
QFontMetrics(fixed_font).size(0, QStringLiteral("-8888888888888888888888")).width());
// Default selection.
m_ui.memoryRegionRAM->setChecked(true);

View File

@@ -150,6 +150,9 @@ struct State
QLocale app_locale;
std::once_flag roboto_font_once_flag;
QStringList roboto_font_families;
std::once_flag fixed_font_once_flag;
QStringList fixed_font_families;
QFont fixed_font;
bool batch_mode = false;
bool nogui_mode = false;
bool start_fullscreen_ui = false;
@@ -2781,6 +2784,33 @@ const QStringList& QtHost::GetRobotoFontFamilies()
return s_state.roboto_font_families;
}
const QFont& QtHost::GetFixedFont()
{
std::call_once(s_state.fixed_font_once_flag, []() {
const int font_id = QFontDatabase::addApplicationFont(
QString::fromStdString(Path::Combine(EmuFolders::Resources, "fonts/RobotoMono-VariableFont_wght.ttf")));
if (font_id < 0)
{
ERROR_LOG("Failed to load fixed-width font.");
return;
}
const QStringList families = QFontDatabase::applicationFontFamilies(font_id);
if (families.isEmpty())
{
ERROR_LOG("Failed to get fixed-width font family.");
return;
}
s_state.fixed_font.setFamilies(families);
s_state.fixed_font.setWeight(QFont::Medium);
s_state.fixed_font.setPixelSize(12);
s_state.fixed_font.setHintingPreference(QFont::PreferNoHinting);
});
return s_state.fixed_font;
}
bool Host::ResourceFileExists(std::string_view filename, bool allow_override)
{
const std::string path = QtHost::GetResourcePath(filename, allow_override);

View File

@@ -393,6 +393,9 @@ std::string GetResourcePath(std::string_view name, bool allow_override);
/// Returns the font family for the bundled Roboto font.
const QStringList& GetRobotoFontFamilies();
/// Returns the font for the bundled fixed-width font.
const QFont& GetFixedFont();
/// Returns the base settings interface. Should lock before manipulating.
INISettingsInterface* GetBaseSettingsInterface();