Utils: Introduce StyleHelper::uiFontLineHeight

The "Text tokens" which are defined in Figma have an additional line
height value.

This change adds a getter function for the line heights.

Change-Id: I9513ac2f689292baf6e2c0a52fca8a1ec7f14dc1
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Alessandro Portale
2024-01-28 23:39:17 +01:00
parent 03db2fd6c5
commit 7d659b446d
3 changed files with 12 additions and 2 deletions

View File

@@ -1002,6 +1002,14 @@ QFont StyleHelper::uiFont(UiElement element)
return font; return font;
} }
int StyleHelper::uiFontLineHeight(UiElement element)
{
const UiFontMetrics &metrics = uiFontMetrics(element);
const qreal lineHeightToPixelSizeRatio = qreal(metrics.lineHeight) / metrics.pixelSize;
const QFontInfo fontInfo(uiFont(element));
return qCeil(fontInfo.pixelSize() * lineHeightToPixelSizeRatio);
}
QString StyleHelper::fontToCssProperties(const QFont &font) QString StyleHelper::fontToCssProperties(const QFont &font)
{ {
const QString fontSize = font.pixelSize() != -1 ? QString::number(font.pixelSize()) + "px" const QString fontSize = font.pixelSize() != -1 ? QString::number(font.pixelSize()) + "px"

View File

@@ -114,6 +114,7 @@ QTCREATOR_UTILS_EXPORT QColor toolBarDropShadowColor();
QTCREATOR_UTILS_EXPORT QColor notTooBrightHighlightColor(); QTCREATOR_UTILS_EXPORT QColor notTooBrightHighlightColor();
QTCREATOR_UTILS_EXPORT QFont uiFont(UiElement element); QTCREATOR_UTILS_EXPORT QFont uiFont(UiElement element);
QTCREATOR_UTILS_EXPORT int uiFontLineHeight(UiElement element);
QTCREATOR_UTILS_EXPORT QString fontToCssProperties(const QFont &font); QTCREATOR_UTILS_EXPORT QString fontToCssProperties(const QFont &font);
// Sets the base color and makes sure all top level widgets are updated // Sets the base color and makes sure all top level widgets are updated

View File

@@ -43,8 +43,9 @@ int main(int argc, char *argv[])
sampleLabel->setFont(font); sampleLabel->setFont(font);
sampleLabel->setSizePolicy(uiElementLabel->sizePolicy()); sampleLabel->setSizePolicy(uiElementLabel->sizePolicy());
QFontInfo fontInfo(font); QFontInfo fontInfo(font);
const QString pixelMetrics = QString::fromLatin1("Pixel size: %1") const QString pixelMetrics = QString::fromLatin1("Pixel size: %1, line height: %2")
.arg(fontInfo.pixelSize()); .arg(fontInfo.pixelSize())
.arg(StyleHelper::uiFontLineHeight(uiElement.uiElement));
auto *metricsLabel = new QLabel(pixelMetrics); auto *metricsLabel = new QLabel(pixelMetrics);
fontLabels.addItems({uiElementLabel, sampleLabel, st, font.toString(), metricsLabel, br}); fontLabels.addItems({uiElementLabel, sampleLabel, st, font.toString(), metricsLabel, br});
} }