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 the new relative units and ensure that low/high surrogate situations are handled.

Wolthera van Hövell tot Westerflierdc8b8c8a5610
diff --git a/libs/flake/tests/TestSvgText.cpp b/libs/flake/tests/TestSvgText.cpp
index fbb3b05cd5..b1a83f6849 100644
--- a/libs/flake/tests/TestSvgText.cpp
+++ b/libs/flake/tests/TestSvgText.cpp
@@ -2488,9 +2488,9 @@ void TestSvgText::testFontMetrics_data()
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;
+ QTest::addRow("test Deja Vu Sans") << QString("Deja Vu Sans") << 12.0 << true << 768 << 420 << 559 << 384 << 584 << -185 << -181 << 460 << 107 << 369;
+ QTest::addRow("test Baseline test font") << QString("Baseline Test") << 12.0 << true << 768 << 384 << 538 << 384 << 614 << -154 << -154 << 460 << 58 << 269;
+ QTest::addRow("test Baseline test font vertical") << QString("Baseline Test") << 12.0 << false << 768 << 384 << 538 << 384 << 614 << -154 << -154 << 460 << 58 << 269;
}
/**
@@ -2580,6 +2580,81 @@ void TestSvgText::testFontMetrics()
.toLatin1());
}
+void TestSvgText::testFontRelativeUnits_data()
+{
+ QTest::addColumn<QString>("fontFamily");
+ QTest::addColumn<qreal>("parentSize");
+ QTest::addColumn<int>("parentProperty");
+ QTest::addColumn<int>("childProperty");
+ QTest::addColumn<int>("requestedUnit");
+ QTest::addColumn<qreal>("expectedSize");
+
+ QTest::addRow("test font-size em") << "Deja Vu Sans" << 12.0 << int(KoSvgTextProperties::FontSizeId) << int(KoSvgTextProperties::FontSizeId) << int(KoSvgText::CssLengthPercentage::Em) << 6.0;
+ QTest::addRow("test font-size ex") << "Deja Vu Sans" << 12.0 << int(KoSvgTextProperties::FontSizeId) << int(KoSvgTextProperties::FontSizeId) << int(KoSvgText::CssLengthPercentage::Ex) << 3.28125;
+ QTest::addRow("test font-size cap") << "Deja Vu Sans" << 12.0 << int(KoSvgTextProperties::FontSizeId) << int(KoSvgTextProperties::FontSizeId) << int(KoSvgText::CssLengthPercentage::Cap) << 4.36719;
+ QTest::addRow("test font-size ch") << "Deja Vu Sans" << 12.0 << int(KoSvgTextProperties::FontSizeId) << int(KoSvgTextProperties::FontSizeId) << int(KoSvgText::CssLengthPercentage::Ch) << 3.0;
+ QTest::addRow("test font-size ic") << "Deja Vu Sans" << 12.0 << int(KoSvgTextProperties::FontSizeId) << int(KoSvgTextProperties::FontSizeId) << int(KoSvgText::CssLengthPercentage::Ic) << 6.0;
+ QTest::addRow("test font-size lh") << "Deja Vu Sans" << 12.0 << int(KoSvgTextProperties::FontSizeId) << int(KoSvgTextProperties::FontSizeId) << int(KoSvgText::CssLengthPercentage::Lh) << 7.21094;
+
+ QTest::addRow("test line-height lh") << "Deja Vu Sans" << 12.0 << int(KoSvgTextProperties::LineHeightId) << int(KoSvgTextProperties::LineHeightId) << int(KoSvgText::CssLengthPercentage::Lh) << 6.0;
+ QTest::addRow("test line-height font-size lh") << "Deja Vu Sans" << 12.0 << int(KoSvgTextProperties::LineHeightId) << int(KoSvgTextProperties::FontSizeId) << int(KoSvgText::CssLengthPercentage::Lh) << 6.0;
+}
+
+void TestSvgText::testFontRelativeUnits()
+{
+ QFETCH(QString, fontFamily);
+ QFETCH(qreal, parentSize);
+ QFETCH(int, parentProperty);
+ QFETCH(int, childProperty);
+ QFETCH(int, requestedUnit);
+ QFETCH(qreal, expectedSize);
+
+ KoSvgTextProperties props;
+ const QStringList families = {fontFamily};
+ props.setProperty(KoSvgTextProperties::FontFamiliesId, families);
+ KoSvgText::CssLengthPercentage size(parentSize);
+
+ if (KoSvgTextProperties::PropertyId(parentProperty) == KoSvgTextProperties::LineHeightId) {
+ KoSvgText::LineHeightInfo lhInfo;
+ lhInfo.isNormal = false;
+ lhInfo.isNumber = false;
+ lhInfo.length = size;
+ props.setProperty(KoSvgTextProperties::PropertyId(parentProperty), QVariant::fromValue(lhInfo));
+ } else {
+ props.setProperty(KoSvgTextProperties::PropertyId(parentProperty), QVariant::fromValue(size));
+ }
+
+ KoSvgTextProperties childProps;
+ size.value = 0.5;
+ size.unit = KoSvgText::CssLengthPercentage::UnitType(requestedUnit);
+
+ const KoSvgTextProperties::PropertyId testProperty = KoSvgTextProperties::PropertyId(childProperty);
+ if (testProperty == KoSvgTextProperties::LineHeightId) {
+ KoSvgText::LineHeightInfo lhInfo;
+ lhInfo.isNormal = false;
+ lhInfo.isNumber = false;
+ lhInfo.length = size;
+ childProps.setProperty(testProperty, QVariant::fromValue(lhInfo));
+ } else {
+ childProps.setProperty(testProperty, QVariant::fromValue(size));
+ }
+
+ childProps.inheritFrom(props, true);
+ KoSvgText::CssLengthPercentage test;
+ if (testProperty == KoSvgTextProperties::LineHeightId) {
+ test = childProps.propertyOrDefault(testProperty).value<KoSvgText::LineHeightInfo>().length;
+ } else {
+ test = childProps.propertyOrDefault(testProperty).value<KoSvgText::CssLengthPercentage>();
+ }
+
+ // Using fabs(diff) here because qfuzzycompare and qdebug do not give enough info about the precise error.
+ QVERIFY2(fabs(test.value - expectedSize) < 0.00001,
+ QString("Inherited font size not returning as %1, instead %2")
+ .arg(QString::number(expectedSize))
+ .arg(QString::number(test.value))
+ .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 28af97bec2..2af097acbb 100644
--- a/libs/flake/tests/TestSvgText.h
+++ b/libs/flake/tests/TestSvgText.h
@@ -98,6 +98,9 @@ private Q_SLOTS:
void testFontMetrics_data();
void testFontMetrics();
+ void testFontRelativeUnits_data();
+ void testFontRelativeUnits();
+
void testCssShapeParsing();
void testShapeInsideRender();
diff --git a/libs/flake/text/KoSvgTextShape_p_layout.cpp b/libs/flake/text/KoSvgTextShape_p_layout.cpp
index 918b996c26..a172eab9f6 100644
--- a/libs/flake/text/KoSvgTextShape_p_layout.cpp
+++ b/libs/flake/text/KoSvgTextShape_p_layout.cpp
@@ -138,6 +138,23 @@ void KoSvgTextShape::Private::relayout()
}
}
+ const auto getUcs4At = [](const QString &s, int i) -> char32_t {
+ const QChar high = s.at(i);
+ if (!high.isSurrogate()) {
+ return high.unicode();
+ }
+ if (high.isHighSurrogate() && s.length() > i + 1) {
+ const QChar low = s[i + 1];
+ if (low.isLowSurrogate()) {
+ return QChar::surrogateToUcs4(high, low);
+ }
+ }
+ // Don't return U+FFFD replacement character but return the
+ // unpaired surrogate itself, so that if we want to we can draw
+ // a tofu block for it.
+ return high.unicode();
+ };
+
// Whenever the freetype docs talk about a 26.6 floating point unit, they
// mean a 1/64 value.
const qreal ftFontUnit = 64.0;
@@ -433,8 +450,7 @@ void KoSvgTextShape::Private::relayout()
QHash<QChar::Script, KoSvgText::FontMetrics> metricsList;
for (int j=start; j<start+length; j++) {
- //TODO: handle low/high surrogate...
- const QChar::Script currentScript = plainText.at(j).script();
+ const QChar::Script currentScript = QChar::script(getUcs4At(text, j));
if (!metricsList.contains(currentScript)) {
metricsList.insert(currentScript, KoFontRegistry::generateFontMetrics(face, isHorizontal, KoWritingSystemUtils::scriptTagForQCharScript(currentScript)));
}
@@ -487,22 +503,7 @@ void KoSvgTextShape::Private::relayout()
const FT_Int32 faceLoadFlags = KoFontRegistry::loadFlagsForFace(currentGlyph.ftface, isHorizontal, loadFlags);
- const auto getUcs4At = [](const QString &s, int i) -> char32_t {
- const QChar high = s.at(i);
- if (!high.isSurrogate()) {
- return high.unicode();
- }
- if (high.isHighSurrogate() && s.length() > i + 1) {
- const QChar low = s[i + 1];
- if (low.isLowSurrogate()) {
- return QChar::surrogateToUcs4(high, low);
- }
- }
- // Don't return U+FFFD replacement character but return the
- // unpaired surrogate itself, so that if we want to we can draw
- // a tofu block for it.
- return high.unicode();
- };
+
const char32_t codepoint = getUcs4At(text, cluster);
debugFlake << "glyph" << i << "cluster" << cluster << currentGlyph.index << codepoint;