From 5e06fcfa6139988cce2e88d604ba8e05d5e43dd0 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Wed, 4 Aug 2010 13:09:30 +0200 Subject: Implement QFontMetrics::inFont(uint) which takes UCS-4 encoded char Support unicode characters which are not representable in 16 bits in QFontMetrics::inFont() to enable font selection algorithms for unicode ranges that require surrogates. Task-number: QTBUG-12638 Reviewed-by: Simon Hausmann --- src/gui/text/qfontmetrics.cpp | 32 ++++++++++++++++++++++++++- src/gui/text/qfontmetrics.h | 2 ++ tests/auto/qfontmetrics/qfontmetrics.pro | 2 +- tests/auto/qfontmetrics/testfont.qrc | 5 +++++ tests/auto/qfontmetrics/tst_qfontmetrics.cpp | 22 ++++++++++++++++++ tests/auto/qfontmetrics/ucs4font.ttf | Bin 0 -> 3076 bytes 6 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 tests/auto/qfontmetrics/testfont.qrc create mode 100644 tests/auto/qfontmetrics/ucs4font.ttf diff --git a/src/gui/text/qfontmetrics.cpp b/src/gui/text/qfontmetrics.cpp index d02e841..f2591ce 100644 --- a/src/gui/text/qfontmetrics.cpp +++ b/src/gui/text/qfontmetrics.cpp @@ -443,6 +443,21 @@ bool QFontMetrics::inFont(QChar ch) const } /*! + Returns true if the character encoded in UCS-4/UTF-32 is a valid + character in the font; otherwise returns false. +*/ +bool QFontMetrics::inFontUcs4(uint ucs4) const +{ + const int script = QUnicodeTables::script(ucs4); + QFontEngine *engine = d->engineForScript(script); + Q_ASSERT(engine != 0); + if (engine->type() == QFontEngine::Box) + return false; + QString utf16 = QString::fromUcs4(&ucs4, 1); + return engine->canRender(utf16.data(), utf16.length()); +} + +/*! Returns the left bearing of character \a ch in the font. The left bearing is the right-ward distance of the left-most pixel @@ -1315,6 +1330,21 @@ bool QFontMetricsF::inFont(QChar ch) const } /*! + Returns true if the character encoded in UCS-4/UTF-32 is a valid + character in the font; otherwise returns false. +*/ +bool QFontMetricsF::inFontUcs4(uint ucs4) const +{ + const int script = QUnicodeTables::script(ucs4); + QFontEngine *engine = d->engineForScript(script); + Q_ASSERT(engine != 0); + if (engine->type() == QFontEngine::Box) + return false; + QString utf16 = QString::fromUcs4(&ucs4, 1); + return engine->canRender(utf16.data(), utf16.length()); +} + +/*! Returns the left bearing of character \a ch in the font. The left bearing is the right-ward distance of the left-most pixel @@ -1779,7 +1809,7 @@ qreal QFontMetricsF::lineWidth() const Use the boundingRect() function in combination with QString::left() instead. - + \oldcode QRect rect = boundingRect(text, len); \newcode diff --git a/src/gui/text/qfontmetrics.h b/src/gui/text/qfontmetrics.h index 2518b54..9911ad2 100644 --- a/src/gui/text/qfontmetrics.h +++ b/src/gui/text/qfontmetrics.h @@ -85,6 +85,7 @@ public: int averageCharWidth() const; bool inFont(QChar) const; + bool inFontUcs4(uint ucs4) const; int leftBearing(QChar) const; int rightBearing(QChar) const; @@ -162,6 +163,7 @@ public: qreal averageCharWidth() const; bool inFont(QChar) const; + bool inFontUcs4(uint ucs4) const; qreal leftBearing(QChar) const; qreal rightBearing(QChar) const; diff --git a/tests/auto/qfontmetrics/qfontmetrics.pro b/tests/auto/qfontmetrics/qfontmetrics.pro index 51a7057..c0dc1ab 100644 --- a/tests/auto/qfontmetrics/qfontmetrics.pro +++ b/tests/auto/qfontmetrics/qfontmetrics.pro @@ -1,4 +1,4 @@ load(qttest_p4) SOURCES += tst_qfontmetrics.cpp - +RESOURCES += testfont.qrc diff --git a/tests/auto/qfontmetrics/testfont.qrc b/tests/auto/qfontmetrics/testfont.qrc new file mode 100644 index 0000000..bc0c0b0 --- /dev/null +++ b/tests/auto/qfontmetrics/testfont.qrc @@ -0,0 +1,5 @@ + + + ucs4font.ttf + + diff --git a/tests/auto/qfontmetrics/tst_qfontmetrics.cpp b/tests/auto/qfontmetrics/tst_qfontmetrics.cpp index a22d624..4250415 100644 --- a/tests/auto/qfontmetrics/tst_qfontmetrics.cpp +++ b/tests/auto/qfontmetrics/tst_qfontmetrics.cpp @@ -74,6 +74,7 @@ private slots: void bypassShaping(); void elidedMultiLength(); void elidedMultiLengthF(); + void inFontUcs4(); }; tst_QFontMetrics::tst_QFontMetrics() @@ -266,5 +267,26 @@ void tst_QFontMetrics::elidedMultiLengthF() elidedMultiLength_helper(); } +void tst_QFontMetrics::inFontUcs4() +{ + int id = QFontDatabase::addApplicationFont(":/fonts/ucs4font.ttf"); + QVERIFY(id >= 0); + + QFont font("QtTestUcs4"); + { + QFontMetrics fm(font); + + QVERIFY(fm.inFontUcs4(0x1D7FF)); + } + + { + QFontMetricsF fm(font); + + QVERIFY(fm.inFontUcs4(0x1D7FF)); + } + + QFontDatabase::removeApplicationFont(id); +} + QTEST_MAIN(tst_QFontMetrics) #include "tst_qfontmetrics.moc" diff --git a/tests/auto/qfontmetrics/ucs4font.ttf b/tests/auto/qfontmetrics/ucs4font.ttf new file mode 100644 index 0000000..31b6997 Binary files /dev/null and b/tests/auto/qfontmetrics/ucs4font.ttf differ -- cgit v0.12