14 #include <QFontMetricsF>
20 , m_fontMetrics(m_font)
21 , m_initialFontMetrics(m_fontMetrics)
22 , m_scalingFactor(1.0)
33 if (m_font.pixelSize() == -1)
36 m_font.setPixelSize(qRound(m_font.pixelSize() *
scalingFactor));
37 m_fontMetrics = QFontMetricsF(m_font);
38 m_initialFontMetrics = m_fontMetrics;
44 m_scalingFactor *= factor;
46 if (m_font.pixelSize() == -1) {
47 if (factor > 0.99 && factor < 1.000)
48 m_font.setPointSizeF(m_font.pointSizeF() - 0.1);
50 m_font.setPointSizeF(m_font.pointSizeF() * factor);
52 if (m_font.pixelSize() > 2 && factor > 0.99 && factor < 1.000)
53 m_font.setPixelSize(m_font.pixelSize() - 1);
55 m_font.setPixelSize(
int(m_font.pixelSize() * factor));
58 qDebug() <<
" applyAdditionalScalingFactor" << factor <<
"combined factor:" << m_scalingFactor <<
"pointSize:" << m_font.pointSizeF() <<
"pixelSize:" << m_font.pixelSize();
60 m_fontMetrics = QFontMetricsF(m_font);
65 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
66 return fm.width(text);
68 return fm.size(Qt::TextSingleLine, text).width();
80 qDebug() <<
" FontScaler::setFactorForHeight" << wantedHeight;
82 Q_ASSERT(wantedHeight > 0);
83 qreal height = m_fontMetrics.height();
86 while (height > wantedHeight && height > 3.0 ) {
87 const qreal factor = wantedHeight / height;
89 qreal prevHeight = height;
90 height = m_fontMetrics.height();
92 qDebug() <<
" FontScaler: height=" << height << factor << m_scalingFactor;
94 if (++iterations > 10 && height == prevHeight) {
99 if (m_font.pixelSize() == 1) {
108 qDebug() <<
" FontScaler::setFactorForWidth" << wantedFactor;
114 const qreal wantedWidth = initialWidth * wantedFactor;
117 qDebug() <<
" FontScaler: sampleText with initialFontMetrics:" << initialWidth <<
"with current fontMetrics:" << width <<
"wanted:" << wantedWidth;
122 while (width > wantedWidth) {
123 qreal factor = wantedWidth / width;
125 qreal prevWidth = width;
128 qDebug() <<
" FontScaler: width=" << width <<
"factor=" << factor <<
"m_scalingFactor=" << m_scalingFactor;
130 if (++iterations > 10 && width == prevWidth) {
static qreal textWidthForMetrics(const QFontMetricsF &fm, const QString &text)
qreal scalingFactor() const
qreal textWidth(const QString &text) const
void applyAdditionalScalingFactor(qreal factor)
void setFactorForWidth(qreal wantedFactor, const QString &sampleText)
void setFactorForHeight(qreal wantedHeight)
FontScaler(const QFont &initialFont)
void setFontAndScalingFactor(const QFont &font, qreal scalingFactor)