Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Add tests for font metrics retrieval.

Wolthera van Hövell tot Westerflier94acadf0a6e9
diff --git a/libs/flake/tests/TestSvgText.cpp b/libs/flake/tests/TestSvgText.cpp
index 66613816ec..58eb22365b 100644
--- a/libs/flake/tests/TestSvgText.cpp
+++ b/libs/flake/tests/TestSvgText.cpp
@@ -2471,6 +2471,114 @@ void TestSvgText::testTextBaselineAlignment()
t.test_standard("test-text-baseline-alignment", QSize(90, 51), 72.0);
}
+void TestSvgText::testFontMetrics_data()
+{
+ QTest::addColumn<QString>("fontFamily");
+ QTest::addColumn<qreal>("fontSize");
+ QTest::addColumn<bool>("isHorizontal");
+ QTest::addColumn<qint32>("expectedFontSize");
+ QTest::addColumn<qint32>("expectedXHeight");
+ QTest::addColumn<qint32>("expectedCapHeight");
+ QTest::addColumn<qint32>("expectedSpace");
+ QTest::addColumn<qint32>("expectedAscender");
+ QTest::addColumn<qint32>("expectedDescender");
+ QTest::addColumn<qint32>("expectedIdeographic");
+ QTest::addColumn<qint32>("expectedHanging");
+ QTest::addColumn<qint32>("expectedSubscript");
+ QTest::addColumn<qint32>("expectedSuperscript");
+
+ QTest::addRow("test Deja Vu Sans") << QString("Deja Vu Sans") << 12.0 << true << 768 << 420 << 559 << 256 << 584 << -185 << -181 << 460 << 107 << 369;
+ QTest::addRow("test Baseline test font") << QString("Baseline Test") << 12.0 << true << 768 << 384 << 538 << 448 << 614 << -154 << -154 << 460 << 58 << 269;
+ QTest::addRow("test Baseline test font vertical") << QString("Baseline Test") << 12.0 << false << 768 << 384 << 538 << 448 << 614 << -154 << -154 << 460 << 58 << 269;
+}
+
+/**
+ * @brief TestSvgText::testFontMetrics
+ * This tests if we're able to retrieve or fall back on correct fontsize metrics.
+ */
+void TestSvgText::testFontMetrics()
+{
+ QFETCH(QString, fontFamily);
+ QFETCH(qreal, fontSize);
+ QFETCH(bool, isHorizontal);
+
+ KoCSSFontInfo info;
+ info.families = QStringList({fontFamily});
+ info.size = fontSize;
+
+ KoSvgText::FontMetrics metrics = KoFontRegistry::instance()->fontMetricsForCSSValues(info, isHorizontal);
+
+ QFETCH(qint32, expectedFontSize);
+ QVERIFY2(expectedFontSize == metrics.fontSize,
+ QString("Font Size not returning as %1, instead %2")
+ .arg(QString::number(expectedFontSize))
+ .arg(QString::number(metrics.fontSize))
+ .toLatin1());
+
+ QFETCH(qint32, expectedXHeight);
+ QVERIFY2(expectedXHeight == metrics.xHeight,
+ QString("XHeight not returning as %1, instead %2")
+ .arg(QString::number(expectedXHeight))
+ .arg(QString::number(metrics.xHeight))
+ .toLatin1());
+
+ QFETCH(qint32, expectedCapHeight);
+ QVERIFY2(expectedCapHeight == metrics.capHeight,
+ QString("CapHeight not returning as %1, instead %2")
+ .arg(QString::number(expectedCapHeight))
+ .arg(QString::number(metrics.capHeight))
+ .toLatin1());
+
+ QFETCH(qint32, expectedSpace);
+ QVERIFY2(expectedSpace == metrics.spaceAdvance,
+ QString("Space advance not returning as %1, instead %2")
+ .arg(QString::number(expectedSpace))
+ .arg(QString::number(metrics.spaceAdvance))
+ .toLatin1());
+
+ QFETCH(qint32, expectedAscender);
+ QVERIFY2(expectedAscender == metrics.ascender,
+ QString("Ascender not returning as %1, instead %2")
+ .arg(QString::number(expectedAscender))
+ .arg(QString::number(metrics.ascender))
+ .toLatin1());
+
+ QFETCH(qint32, expectedDescender);
+ QVERIFY2(expectedDescender == metrics.descender,
+ QString("Descender not returning as %1, instead %2")
+ .arg(QString::number(expectedDescender))
+ .arg(QString::number(metrics.descender))
+ .toLatin1());
+
+ QFETCH(qint32, expectedIdeographic);
+ QVERIFY2(expectedIdeographic == metrics.ideographicUnderBaseline,
+ QString("Ideographic not returning as %1, instead %2")
+ .arg(QString::number(expectedIdeographic))
+ .arg(QString::number(metrics.ideographicUnderBaseline))
+ .toLatin1());
+
+ QFETCH(qint32, expectedHanging);
+ QVERIFY2(expectedHanging == metrics.hangingBaseline,
+ QString("Hanging not returning as %1, instead %2")
+ .arg(QString::number(expectedHanging))
+ .arg(QString::number(metrics.hangingBaseline))
+ .toLatin1());
+
+ QFETCH(qint32, expectedSubscript);
+ QVERIFY2(expectedSubscript == metrics.subScriptOffset.second,
+ QString("Subscript not returning as %1, instead %2")
+ .arg(QString::number(expectedSubscript))
+ .arg(QString::number(metrics.subScriptOffset.second))
+ .toLatin1());
+
+ QFETCH(qint32, expectedSuperscript);
+ QVERIFY2(expectedSuperscript == metrics.superScriptOffset.second,
+ QString("Superscript not returning as %1, instead %2")
+ .arg(QString::number(expectedSuperscript))
+ .arg(QString::number(metrics.superScriptOffset.second))
+ .toLatin1());
+}
+
/**
* Tests the loading of CSS shapes by comparing the loaded shapes with their reference shapes.
*/
diff --git a/libs/flake/tests/TestSvgText.h b/libs/flake/tests/TestSvgText.h
index 32cdbc66ac..3fb4a2fda9 100644
--- a/libs/flake/tests/TestSvgText.h
+++ b/libs/flake/tests/TestSvgText.h
@@ -95,6 +95,9 @@ private Q_SLOTS:
void testTextBaselineAlignment();
+ void testFontMetrics_data();
+ void testFontMetrics();
+
void testCssShapeParsing();
void testShapeInsideRender();
diff --git a/libs/flake/text/KoFontRegistry.cpp b/libs/flake/text/KoFontRegistry.cpp
index 7dacf0f288..ee2be06354 100644
--- a/libs/flake/text/KoFontRegistry.cpp
+++ b/libs/flake/text/KoFontRegistry.cpp
@@ -768,7 +768,7 @@ KoSvgText::FontMetrics KoFontRegistry::generateFontMetrics(FT_FaceSP face, bool
metrics.isVertical = !isHorizontal;
// Fontsize and advances.
- metrics.fontSize = isHorizontal? face.data()->size->metrics.y_ppem: face.data()->size->metrics.x_ppem;
+ metrics.fontSize = isHorizontal? face.data()->size->metrics.y_ppem*64.0: face.data()->size->metrics.x_ppem*64.0;
if(!FT_Load_Glyph(face.data(), FT_Get_Char_Index(face.data(), ' '), faceLoadFlags)) {
metrics.spaceAdvance = isHorizontal? face.data()->glyph->advance.x: face.data()->glyph->advance.y;
diff --git a/libs/flake/text/KoSvgText.cpp b/libs/flake/text/KoSvgText.cpp
index 0da53834e3..528f68c525 100644
--- a/libs/flake/text/KoSvgText.cpp
+++ b/libs/flake/text/KoSvgText.cpp
@@ -1376,7 +1376,7 @@ void FontMetrics::setMetricsValueByTag(const QLatin1String &tag, int32_t value)
if (tag == "xhgt") {
xHeight = value;
} else if (tag == "cpht") {
- xHeight = value;
+ capHeight = value;
} else if (tag == "sbxo") {
subScriptOffset.first = value;
} else if (tag == "sbyo") {
@@ -1402,7 +1402,7 @@ void FontMetrics::scaleBaselines(const qreal multiplier)
QDebug operator<<(QDebug dbg, const FontMetrics &metrics)
{
- const double ftPixel = 1.0/64.0;
+ const double ftPixel = 1.0;
dbg.nospace() << "FontMetrics(";
dbg.nospace() << "Direction: " << (metrics.isVertical? "Top to bottom. ": "Left to right. ");
dbg.nospace() << "FontSize: " << QString::number(metrics.fontSize*ftPixel) << "px. ";
diff --git a/libs/flake/text/KoSvgText.h b/libs/flake/text/KoSvgText.h
index 594f4ba5bb..5382f03ab1 100644
--- a/libs/flake/text/KoSvgText.h
+++ b/libs/flake/text/KoSvgText.h
@@ -987,32 +987,32 @@ QDebug KRITAFLAKE_EXPORT operator<<(QDebug dbg, const KoSvgText::FontFeatureEast
*/
struct FontMetrics : public boost::equality_comparable<FontMetrics> {
bool isVertical = false; ///< Different fontMetrics count between vertical and horizontal.
- int32_t fontSize; ///< Currently set size, CSS unit 'em'
- int32_t zeroAdvance; ///< Advance of the character '0', CSS Unit 'ch', defaults to 0.5 em in horizontal and 1.0 em in vertical.
- int32_t spaceAdvance;///< Advance of the character ' ', used by tabs.
- int32_t ideographicAdvance; ///< Advance of the character '水' (U+6C34), CSS Unit ic, defaults to 1 em.
+ qint32 fontSize; ///< Currently set size, CSS unit 'em'
+ qint32 zeroAdvance; ///< Advance of the character '0', CSS Unit 'ch', defaults to 0.5 em in horizontal and 1.0 em in vertical.
+ qint32 spaceAdvance;///< Advance of the character ' ', used by tabs.
+ qint32 ideographicAdvance; ///< Advance of the character '水' (U+6C34), CSS Unit ic, defaults to 1 em.
- int32_t xHeight; ///< height of X, defaults to 0.5 fontsize.
- int32_t capHeight; ///< Height of capital letters, defaults to ascender.
- QPair<int32_t, int32_t> subScriptOffset; ///< subscript baseline height, defaults to 1/5th em below alphabetic.
- QPair<int32_t, int32_t> superScriptOffset; ///< superscript baseline height, defaults to 2/3rd about alphabetic.
+ qint32 xHeight; ///< height of X, defaults to 0.5 fontsize.
+ qint32 capHeight; ///< Height of capital letters, defaults to ascender.
+ QPair<qint32, qint32> subScriptOffset; ///< subscript baseline height, defaults to 1/5th em below alphabetic.
+ QPair<qint32, qint32> superScriptOffset; ///< superscript baseline height, defaults to 2/3rd about alphabetic.
- int32_t ascender; ///< distance from origin to top.
- int32_t descender; ///< distance for origin to bottom.
- int32_t lineGap; ///< additional linegap between consequetive lines.
+ qint32 ascender; ///< distance from origin to top.
+ qint32 descender; ///< distance for origin to bottom.
+ qint32 lineGap; ///< additional linegap between consequetive lines.
- int32_t alphabeticBaseline; ///< location of alphabetic baseline from origin.
- int32_t mathematicalBaseline; ///< location of mathematical baseline from origin.
+ qint32 alphabeticBaseline; ///< location of alphabetic baseline from origin.
+ qint32 mathematicalBaseline; ///< location of mathematical baseline from origin.
- int32_t ideographicUnderBaseline; ///< location of ideographic under baseline from origin, may fall back to descender.
- int32_t ideographicCenterBaseline; ///< location of ideographic center baseline from origin,
+ qint32 ideographicUnderBaseline; ///< location of ideographic under baseline from origin, may fall back to descender.
+ qint32 ideographicCenterBaseline; ///< location of ideographic center baseline from origin,
///< default baseline for vertical, centered between over and under.
- int32_t ideographicOverBaseline; ///< location of ideographic over baseline from origin.
+ qint32 ideographicOverBaseline; ///< location of ideographic over baseline from origin.
- int32_t ideographicFaceUnderBaseline; ///< location of ideographic face under baseline, that is, the bottom of the glyphs.
- int32_t ideographicFaceOverBaseline; ///< location of ideographic face over baseline, that is, the top of the glyphs.
+ qint32 ideographicFaceUnderBaseline; ///< location of ideographic face under baseline, that is, the bottom of the glyphs.
+ qint32 ideographicFaceOverBaseline; ///< location of ideographic face over baseline, that is, the top of the glyphs.
- int32_t hangingBaseline; ///< location of the hanging baseline used in north brahmic scripts.
+ qint32 hangingBaseline; ///< location of the hanging baseline used in north brahmic scripts.
bool operator==(const FontMetrics & other) const {
return isVertical == other.isVertical
diff --git a/libs/flake/text/KoSvgTextShape_p_layout.cpp b/libs/flake/text/KoSvgTextShape_p_layout.cpp
index 09baae5581..6b908f0044 100644
--- a/libs/flake/text/KoSvgTextShape_p_layout.cpp
+++ b/libs/flake/text/KoSvgTextShape_p_layout.cpp
@@ -1088,7 +1088,7 @@ void KoSvgTextShape::Private::computeFontMetrics( // NOLINT(readability-function
// Get underline and super/subscripts.
QPointF newSuperScript = QPointF(metrics.superScriptOffset.first * freetypePixelsToPt, metrics.superScriptOffset.second * -freetypePixelsToPt);;
- QPointF newSubScript = QPointF(metrics.subScriptOffset.first * freetypePixelsToPt, metrics.subScriptOffset.second * -freetypePixelsToPt);
+ QPointF newSubScript = QPointF(metrics.subScriptOffset.first * freetypePixelsToPt, metrics.subScriptOffset.second * freetypePixelsToPt);
hb_ot_metrics_get_position_with_fallback(font.data(), HB_OT_METRICS_TAG_UNDERLINE_SIZE, &baseline);
qreal width = baseline;