diff options
Diffstat (limited to 'tests/auto/gui/text/qfontmetrics/tst_qfontmetrics.cpp')
-rw-r--r-- | tests/auto/gui/text/qfontmetrics/tst_qfontmetrics.cpp | 136 |
1 files changed, 107 insertions, 29 deletions
diff --git a/tests/auto/gui/text/qfontmetrics/tst_qfontmetrics.cpp b/tests/auto/gui/text/qfontmetrics/tst_qfontmetrics.cpp index c78475a76f..bad33ab0a4 100644 --- a/tests/auto/gui/text/qfontmetrics/tst_qfontmetrics.cpp +++ b/tests/auto/gui/text/qfontmetrics/tst_qfontmetrics.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> @@ -43,6 +18,7 @@ private slots: void same(); void metrics(); void boundingRect(); + void boundingRect2(); void elidedText_data(); void elidedText(); void veryNarrowElidedText(); @@ -56,6 +32,11 @@ private slots: void leadingBelowLine(); void elidedMetrics(); void zeroWidthMetrics(); + void verticalMetrics_data(); + void verticalMetrics(); + void largeText_data(); + void largeText(); // QTBUG-123339 + void typoLineMetrics(); }; void tst_QFontMetrics::same() @@ -149,6 +130,21 @@ void tst_QFontMetrics::boundingRect() QVERIFY(r.top() < 0); } +void tst_QFontMetrics::boundingRect2() +{ + QFont f; + f.setPixelSize(16); + QFontMetricsF fm(f); + QString str("AVAVAVA vvvvvvvvvv fffffffff file"); + QRectF br = fm.boundingRect(str); + QRectF tbr = fm.tightBoundingRect(str); + qreal advance = fm.horizontalAdvance(str); + // Bounding rect plus bearings should be similar to advance + qreal bearings = fm.leftBearing(QChar('A')) + fm.rightBearing(QChar('e')); + QVERIFY(qAbs(br.width() + bearings - advance) < fm.averageCharWidth()/2.0); + QVERIFY(qAbs(tbr.width() + bearings - advance) < fm.averageCharWidth()/2.0); +} + void tst_QFontMetrics::elidedText_data() { QTest::addColumn<QFont>("font"); @@ -268,7 +264,7 @@ void tst_QFontMetrics::inFontUcs4() glyphs.glyphs[0] = 0; QVERIFY(engine->stringToCMap(string.constData(), string.size(), &glyphs, &glyphs.numGlyphs, - QFontEngine::GlyphIndicesOnly)); + QFontEngine::GlyphIndicesOnly) > 0); QCOMPARE(glyphs.numGlyphs, 1); QCOMPARE(glyphs.glyphs[0], uint(1)); } @@ -280,7 +276,7 @@ void tst_QFontMetrics::inFontUcs4() glyphs.glyphs[0] = 0; QVERIFY(engine->stringToCMap(string.constData(), string.size(), &glyphs, &glyphs.numGlyphs, - QFontEngine::GlyphIndicesOnly)); + QFontEngine::GlyphIndicesOnly) >= 0); QVERIFY(glyphs.glyphs[0] != 1); } } @@ -374,6 +370,88 @@ void tst_QFontMetrics::zeroWidthMetrics() QCOMPARE(fm.horizontalAdvance(string3), fm.horizontalAdvance(string4)); QCOMPARE(fm.boundingRect(string1).width(), fm.boundingRect(string2).width()); QCOMPARE(fm.boundingRect(string3).width(), fm.boundingRect(string4).width()); + QCOMPARE(fm.tightBoundingRect(string1).width(), fm.tightBoundingRect(string2).width()); + QCOMPARE(fm.tightBoundingRect(string3).width(), fm.tightBoundingRect(string4).width()); +} + +void tst_QFontMetrics::verticalMetrics_data() +{ + QTest::addColumn<QFont>("font"); + QStringList families = QFontDatabase::families(); + for (const QString &family : families) { + QFont font(family); + QTest::newRow(family.toUtf8()) << font; + } +} + +void tst_QFontMetrics::verticalMetrics() +{ + QFETCH(QFont, font); + QFontMetrics fm(font); + QVERIFY(fm.ascent() != 0 || fm.descent() != 0); +} + +void tst_QFontMetrics::largeText_data() +{ + QTest::addColumn<qsizetype>("size"); + for (int i = 1; i < 20; ++i) { + qsizetype size = qsizetype(1) << i; + QByteArray rowText = QByteArray::number(size); + QTest::newRow(rowText.constData()) << size; + } +} + +void tst_QFontMetrics::largeText() +{ + QFont font; + QFontMetrics fm(font); + QFETCH(qsizetype, size); + QString string(size, QLatin1Char('A')); + QRect boundingRect = fm.boundingRect(string); + QVERIFY(boundingRect.isValid()); +} + +void tst_QFontMetrics::typoLineMetrics() +{ + QString testFont = QFINDTESTDATA("fonts/testfont_linemetrics.otf"); + QVERIFY(!testFont.isEmpty()); + + int id = QFontDatabase::addApplicationFont(testFont); + QVERIFY(id >= 0); + + { + auto cleanup = qScopeGuard([&id] { + if (id >= 0) + QFontDatabase::removeApplicationFont(id); + }); + + QImage img(100, 100, QImage::Format_ARGB32); + img.setDevicePixelRatio(1.0); + QFont font(QFontDatabase::applicationFontFamilies(id).at(0), &img); + font.setPixelSize(18); + + const qreal unitsPerEm = 1000.0; + + QFontMetrics defaultFm(font); + const int defaultAscent = defaultFm.ascent(); + const int defaultDescent = defaultFm.descent(); + const int defaultLeading = defaultFm.leading(); + + QCOMPARE(defaultAscent, qRound(1234.0 / unitsPerEm * font.pixelSize())); + QCOMPARE(defaultDescent, qRound(5678.0 / unitsPerEm * font.pixelSize())); + QCOMPARE(defaultLeading, 0.0); + + font.setStyleStrategy(QFont::PreferTypoLineMetrics); + const QFontMetrics typoFm(font); + + const int typoAscent = typoFm.ascent(); + const int typoDescent = typoFm.descent(); + const int typoLeading = typoFm.leading(); + + QCOMPARE(typoAscent, qRound(2000.0 / unitsPerEm * font.pixelSize())); + QCOMPARE(typoDescent, qRound(3000.0 / unitsPerEm * font.pixelSize())); + QCOMPARE(typoLeading, qRound(1000.0 / unitsPerEm * font.pixelSize())); + } } QTEST_MAIN(tst_QFontMetrics) |