diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-07-16 13:44:19 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-08-10 19:43:02 +0200 |
commit | 33f9591e378a2162378d8be5e75a47adf034536b (patch) | |
tree | b3e93e71dd609d213155ddaf333ec912673581d3 /tests/auto/widgets | |
parent | 1a8de144b64fe665d3c0b34e329c9454d97e1523 (diff) |
Implement missing support for 'em' and 'ex' lengths in style sheet
The Qt style sheet reference claimed that Length properties can be
specified in 'em' or 'ex' units, but that was never implemented.
Add the missing implementation. Since the sizes depend on the size of
the font of the current element, we cannot convert the units in the CSS
parser, but have to do so in the QRenderRule constructor, where we can
make a decision about which font to use if the style sheet itself doesn't
specify a font. Fall back to the widget font if possible; otherwise it
will be the application default font.
The implementation translates em into QFontMetrics.height, identical to
what is already done in the QCssParser. This is in line with the CSS
specification, but contradicts our previous documentation which stated
that 'em' means "width of M". Fix the documentation.
Fixes: QTBUG-8096
Pick-to: 6.2
Change-Id: I145e2504ae3b19101a0d0dd63653466b6c2cec1d
Done-with: Cristian Maureira-Fredes <Cristian.Maureira-Fredes@qt.io>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'tests/auto/widgets')
-rw-r--r-- | tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp index 63db23f3b0..272c175042 100644 --- a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp +++ b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp @@ -2352,12 +2352,40 @@ void tst_QStyleSheetStyle::iconSizes_data() smallFont.setPointSizeF(9.0); QFont largeFont; largeFont.setPointSizeF(24.0); + QFont hugeFont; + hugeFont.setPointSizeF(40.0); QTest::addRow("default") << QString() << QFont() << QSize(defaultSize, defaultSize); QTest::addRow("pixels") << "icon-size: 50px" << QFont() << QSize(50, 50); QTest::addRow("points") << "icon-size: 20pt" << QFont() << QSize(15, 15); QTest::addRow("pixels with font") << "icon-size: 50px" << smallFont << QSize(50, 50); QTest::addRow("points with font") << "icon-size: 20pt" << largeFont << QSize(15, 15); + + const QFontMetrics defaultMetrics{QFont()}; + const QFontMetrics smallMetrics(smallFont); + const QFontMetrics largeMetrics(largeFont); + const QFontMetrics hugeMetrics(hugeFont); + QTest::addRow("1em, default font") << "icon-size: 1em" + << QFont() << QSize(defaultMetrics.height(), defaultMetrics.height()); + QTest::addRow("1em, small font") << "icon-size: 1em" + << smallFont << QSize(smallMetrics.height(), smallMetrics.height()); + QTest::addRow("1em, large font") << "icon-size: 1em" + << largeFont << QSize(largeMetrics.height(), largeMetrics.height()); + QTest::addRow("1.5em, lage font") << "icon-size: 1.5em" + << largeFont << QSize(largeMetrics.height(), largeMetrics.height()) * 1.5; + QTest::addRow("2em with styled font") << "font-size: 40pt; icon-size: 2em" + << QFont() << QSize(hugeMetrics.height(), hugeMetrics.height()) * 2; + + QTest::addRow("1ex, default font") << "icon-size: 1ex" + << QFont() << QSize(defaultMetrics.xHeight(), defaultMetrics.xHeight()); + QTest::addRow("1ex, small font") << "icon-size: 1ex" + << smallFont << QSize(smallMetrics.xHeight(), smallMetrics.xHeight()); + QTest::addRow("1ex, large font") << "icon-size: 1ex" + << largeFont << QSize(largeMetrics.xHeight(), largeMetrics.xHeight()); + QTest::addRow("1.5ex, lage font") << "icon-size: 1.5ex" + << largeFont << QSize(largeMetrics.xHeight(), largeMetrics.xHeight()) * 1.5; + QTest::addRow("2ex with styled font") << "font-size: 40pt; icon-size: 2ex" + << QFont() << QSize(hugeMetrics.xHeight(), hugeMetrics.xHeight()) * 2; } void tst_QStyleSheetStyle::iconSizes() |