fix font issue
This commit is contained in:
1 parent
e09b3672e6
commit
51c483dd60
1 file changed
+52
-1
@@ -28,16 +28,67 @@
|
|||||||
|
|
||||||
#include "Dpi/DpiGraphics.h"
|
#include "Dpi/DpiGraphics.h"
|
||||||
|
|
||||||
|
#include <QFontDatabase>
|
||||||
#include <QFontMetrics>
|
#include <QFontMetrics>
|
||||||
|
#include <QHash>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
namespace Dpi {
|
namespace Dpi {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
// The original Windows-only UI used 'Times New Roman'/'MS Sans Serif'
|
||||||
|
// (hardcoded in Fonts.txt/.lfm files) - neither exists on Linux/macOS, and
|
||||||
|
// Qt/fontconfig's own substitution doesn't necessarily pick a metrically
|
||||||
|
// similar replacement, which breaks the UI's pixel-tuned absolute layouts
|
||||||
|
// (mismatched character widths cause overlapping text). Prefer a
|
||||||
|
// metric-compatible clone first (same glyph widths as the original font,
|
||||||
|
// so existing layouts still line up), then a widely-available look-alike,
|
||||||
|
// before falling back to Qt's own substitution.
|
||||||
|
QString resolveFontFamily(const QString &requested)
|
||||||
|
{
|
||||||
|
static const QHash<QString, QStringList> substitutes = {
|
||||||
|
{QStringLiteral("times new roman"),
|
||||||
|
{QStringLiteral("Liberation Serif"), QStringLiteral("Tinos"), QStringLiteral("Nimbus Roman No9 L"),
|
||||||
|
QStringLiteral("Nimbus Roman"), QStringLiteral("Times")}},
|
||||||
|
{QStringLiteral("ms sans serif"),
|
||||||
|
{QStringLiteral("Liberation Sans"), QStringLiteral("Arimo"), QStringLiteral("Nimbus Sans L"),
|
||||||
|
QStringLiteral("Nimbus Sans"), QStringLiteral("DejaVu Sans"), QStringLiteral("Arial")}},
|
||||||
|
{QStringLiteral("microsoft sans serif"),
|
||||||
|
{QStringLiteral("Liberation Sans"), QStringLiteral("Arimo"), QStringLiteral("Nimbus Sans L"),
|
||||||
|
QStringLiteral("Nimbus Sans"), QStringLiteral("DejaVu Sans"), QStringLiteral("Arial")}},
|
||||||
|
{QStringLiteral("arial"),
|
||||||
|
{QStringLiteral("Liberation Sans"), QStringLiteral("Arimo"), QStringLiteral("DejaVu Sans")}},
|
||||||
|
};
|
||||||
|
|
||||||
|
const QStringList &installed = QFontDatabase::families();
|
||||||
|
auto isInstalled = [&installed](const QString &family) {
|
||||||
|
return std::any_of(installed.cbegin(), installed.cend(),
|
||||||
|
[&family](const QString &f) { return f.compare(family, Qt::CaseInsensitive) == 0; });
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isInstalled(requested))
|
||||||
|
return requested;
|
||||||
|
|
||||||
|
const auto it = substitutes.constFind(requested.trimmed().toLower());
|
||||||
|
if (it != substitutes.constEnd())
|
||||||
|
for (const QString &candidate : it.value())
|
||||||
|
if (isInstalled(candidate))
|
||||||
|
return candidate;
|
||||||
|
|
||||||
|
// Nothing matched - let Qt's own font-family substitution take over.
|
||||||
|
return requested;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
// ---- Font ----
|
// ---- Font ----
|
||||||
|
|
||||||
Font::Font() : m_color(Qt::black) {}
|
Font::Font() : m_color(Qt::black) {}
|
||||||
|
|
||||||
QString Font::name() const { return m_font.family(); }
|
QString Font::name() const { return m_font.family(); }
|
||||||
void Font::setName(const QString &value) { m_font.setFamily(value); changed(); }
|
void Font::setName(const QString &value) { m_font.setFamily(resolveFontFamily(value)); changed(); }
|
||||||
|
|
||||||
bool Font::bold() const { return m_font.bold(); }
|
bool Font::bold() const { return m_font.bold(); }
|
||||||
void Font::setBold(bool value) { m_font.setBold(value); changed(); }
|
void Font::setBold(bool value) { m_font.setBold(value); changed(); }
|
||||||
|
|||||||
Reference in new issue
Block a user