Improve font size selection a bit.

This commit is contained in:
John Preston 2024-05-01 19:09:37 +04:00
parent 08b56a1789
commit 4ddff63a9b

View file

@ -136,71 +136,64 @@ bool LoadCustomFont(const QString &filePath) {
} }
[[nodiscard]] int ComputePixelSize(QFont font, uint32 flags, int size) { [[nodiscard]] int ComputePixelSize(QFont font, uint32 flags, int size) {
constexpr auto kMaxSizeShift = 6; constexpr auto kMaxSizeShift = 8;
const auto family = font.family(); const auto family = font.family();
const auto basic = u"Open Sans"_q; const auto basic = u"Open Sans"_q;
if (family == basic) { if (family == basic) {
return size; return size;
} }
font.setPixelSize(size);
auto copy = font; auto copy = font;
copy.setFamily(basic); copy.setFamily(basic);
const auto basicMetrics = QFontMetricsF(copy); const auto basicMetrics = QFontMetricsF(copy);
const auto desiredHeight = basicMetrics.height();
const auto desiredCap = basicMetrics.capHeight(); //static const auto Test = u"bdfghijklpqtyBDFGHIJKLPQTY1234567890[]{}()"_q;
if (desiredHeight < 1. || desiredCap < 1.) { static const auto Test = u"acemnorsuvwxz"_q;
const auto desired = basicMetrics.tightBoundingRect(Test).height();
//const auto tightAscent = -tightRect.y();
//const auto tightDescent = tightRect.y() + tightRect.height();
if (desired < 1.) {
return size; return size;
} }
font.setPixelSize(size);
const auto currentMetrics = QFontMetricsF(font); const auto currentMetrics = QFontMetricsF(font);
auto currentHeight = currentMetrics.height();
auto currentCap = currentMetrics.capHeight(); auto current = currentMetrics.tightBoundingRect(Test).height();
//const auto tightAscent = -tightRect.y();
//const auto tightDescent = tightRect.y() + tightRect.height();
const auto max = std::min(kMaxSizeShift, size - 1); const auto max = std::min(kMaxSizeShift, size - 1);
if (currentHeight < 1. if (current < 1. || std::abs(current - desired) < 0.2) {
|| currentCap < 1.
|| std::abs(currentCap - desiredCap) < 0.2
|| std::abs(currentHeight - desiredHeight) < 0.2) {
return size; return size;
} else if (currentHeight < desiredHeight) { } else if (current < desired) {
for (auto i = 0; i != max; ++i) { for (auto i = 0; i != max; ++i) {
const auto shift = i + 1; const auto shift = i + 1;
font.setPixelSize(size + shift); font.setPixelSize(size + shift);
const auto metrics = QFontMetricsF(font); const auto metrics = QFontMetricsF(font);
const auto nowHeight = metrics.height(); const auto now = metrics.tightBoundingRect(Test).height();
const auto nowCap = metrics.capHeight(); if (now > desired) {
if (nowHeight > desiredHeight || nowCap > desiredCap) { const auto better = (now - desired) < (desired - current);
const auto heightBetter = (nowHeight - desiredHeight) return better ? (size + shift) : (size + shift - 1);
< (desiredHeight - currentHeight);
const auto capBetter = (nowCap - desiredCap)
< (desiredCap - currentCap);
return (heightBetter && capBetter)
? (size + shift)
: (size + shift - 1);
} }
currentHeight = nowHeight; current = now;
currentCap = nowCap;
} }
return size + kMaxSizeShift; return size + max;
} else { } else {
for (auto i = 0; i != max; ++i) { for (auto i = 0; i != max; ++i) {
const auto shift = i + 1; const auto shift = i + 1;
font.setPixelSize(size - shift); font.setPixelSize(size - shift);
const auto metrics = QFontMetricsF(font); const auto metrics = QFontMetricsF(font);
const auto nowHeight = metrics.height(); const auto now = metrics.tightBoundingRect(Test).height();
const auto nowCap = metrics.capHeight(); if (now < desired) {
if (nowHeight < desiredHeight || nowCap < desiredCap) { const auto better = (desired - now) < (current - desired);
const auto heightBetter = (desiredHeight - nowHeight) return better ? (size - shift) : (size - shift + 1);
< (currentHeight - desiredHeight);
const auto capBetter = (desiredCap - nowCap)
< (currentCap - desiredCap);
return (heightBetter && capBetter)
? (size - shift)
: (size - shift + 1);
} }
currentHeight = nowHeight; current = now;
currentCap = nowCap;
} }
return size - kMaxSizeShift; return size - max;
} }
} }
@ -319,7 +312,7 @@ FontData::FontData(int size, uint32 flags, int family, Font *other)
ascent = int(base::SafeRound(_m.ascent())); ascent = int(base::SafeRound(_m.ascent()));
descent = int(base::SafeRound(_m.descent())); descent = int(base::SafeRound(_m.descent()));
spacew = width(QLatin1Char(' ')); spacew = width(QLatin1Char(' '));
elidew = width("..."); elidew = width(u"..."_q);
} }
Font FontData::bold(bool set) const { Font FontData::bold(bool set) const {