Utils: Add StyleHelper::fontToCssProperties

This function takes a QFont and returns the relevant CSS properties as a
string. Using that CSS string in Qt's HTML supporting widgets ensures
the true to detail HTML rendering of text elements with those CSS
properties.

This change also adds an HTML sample to tst_manual_widgets_uifonts.

Change-Id: I7caf3214854f6ee62ae1211015572c640fc08dc8
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Alessandro Portale
2023-12-07 17:05:47 +01:00
parent 3e62f5d522
commit 608bb8cf3d
3 changed files with 43 additions and 2 deletions

View File

@@ -979,4 +979,24 @@ QFont StyleHelper::uiFont(UiElement element)
return font;
}
QString StyleHelper::fontToCssProperties(const QFont &font)
{
const QString fontSize = font.pixelSize() != -1 ? QString::number(font.pixelSize()) + "px"
: QString::number(font.pointSizeF()) + "pt";
const QString fontStyle = QLatin1String(font.style() == QFont::StyleNormal
? "normal" : font.style() == QFont::StyleItalic
? "italic" : "oblique");
const QString fontShorthand = fontStyle + " " + QString::number(font.weight()) + " "
+ fontSize + " " + font.family();
const QString textDecoration = QLatin1String(font.underline() ? "underline" : "none");
const QString propertyTemplate = "%1: %2";
const QStringList cssProperties = {
propertyTemplate.arg("font").arg(fontShorthand),
propertyTemplate.arg("text-decoration").arg(textDecoration),
propertyTemplate.arg("word-spacing").arg(font.wordSpacing()),
};
const QString fontCssStyle = cssProperties.join("; ");
return fontCssStyle;
}
} // namespace Utils