Files
John ChadwickandStefan Schlosser 50ac621430 hiro/qt: Better fix for QFontMetrics::width deprecation
The desired size was probably the boundingRect width and not the advance
width, so the fix from bsdcode is better than mine.

This is backported from ares:
https://github.com/ares-emulator/ares/commit/ffe97222a1aabd325c301e07ba40042ea58ff081

Co-authored-by: Stefan Schlosser <[email protected]>
2025-09-02 18:09:42 +10:00

38 lines
1.0 KiB
C++

#if defined(Hiro_Font)
namespace hiro {
auto pFont::size(const Font& font, const string& text) -> Size {
return pFont::size(pFont::create(font), text);
}
auto pFont::size(const QFont& qtFont, const string& text) -> Size {
QFontMetrics metrics(qtFont);
signed maxWidth = 0;
auto lines = text.split("\n");
for(auto& line : lines) {
maxWidth = max(maxWidth, metrics.boundingRect(QString::fromUtf8(line)).width());
}
return {maxWidth, metrics.height() * (signed)lines.size()};
}
auto pFont::family(const string& family) -> QString {
if(family == Font::Sans ) return "Sans";
if(family == Font::Serif) return "Serif";
if(family == Font::Mono ) return "Liberation Mono";
return family ? QString::fromUtf8(family) : "Sans";
}
auto pFont::create(const Font& font) -> QFont {
QFont qtFont;
qtFont.setFamily(family(font.family()));
qtFont.setPointSize(Application::scale(font.size() ? font.size() : 8));
qtFont.setBold(font.bold());
qtFont.setItalic(font.italic());
return qtFont;
}
}
#endif