summaryrefslogtreecommitdiff
path: root/sw/qa/core
diff options
context:
space:
mode:
authorMark Hung <marklm9@gmail.com>2022-05-13 22:43:03 +0800
committerMiklos Vajna <vmiklos@collabora.com>2022-05-23 08:48:04 +0200
commitf94ba53cd446a80f3d07a5d67740129fde831018 (patch)
treed1122aaa927b4c815e8b273c81759c8f2948bb55 /sw/qa/core
parent123bb44f4a23acad6acb0bdaf43705680d8739c9 (diff)
tdf#149017 Justify::SpaceDistribution unit tests.
Cerate new unit test cases for core/txtnode/justify.cxx and Justify namespace. It implements basic utility to convert between KernArray and char width array, and can serve for kern array related tests. Change-Id: I3da172853c6d8aca517247af34b29f712d3b6e62 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134298 Tested-by: Jenkins Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Diffstat (limited to 'sw/qa/core')
-rw-r--r--sw/qa/core/txtnode/justify.cxx112
1 files changed, 112 insertions, 0 deletions
diff --git a/sw/qa/core/txtnode/justify.cxx b/sw/qa/core/txtnode/justify.cxx
new file mode 100644
index 000000000000..46a52f851efc
--- /dev/null
+++ b/sw/qa/core/txtnode/justify.cxx
@@ -0,0 +1,112 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <swmodeltestbase.hxx>
+#include <justify.hxx>
+#include <vector>
+
+namespace
+{
+/// Covers sw/source/core/txtnode/justify fixes.
+class SwCoreJustifyTest : public SwModelTestBase
+{
+};
+
+/// CharWidthArray: store char widths because they are more readable.
+class CharWidthArray
+{
+public:
+ std::vector<sal_Int32> maArray;
+ template <typename... Args>
+ CharWidthArray(Args&&... args)
+ : maArray{ std::forward<Args>(args)... }
+ {
+ }
+ template <typename Function> void InvokeWithKernArray(Function f);
+ void ConvertToKernArray();
+ void ConvertToCharWidths();
+};
+
+inline bool operator==(const CharWidthArray& lhs, const CharWidthArray& rhs)
+{
+ return lhs.maArray == rhs.maArray;
+}
+
+std::ostream& operator<<(std::ostream& rStrm, const CharWidthArray& rCharWidthArray)
+{
+ const std::vector<sal_Int32>& rArray(rCharWidthArray.maArray);
+ sal_Int32 nLen = rArray.size();
+ rStrm << "{ ";
+ for (sal_Int32 i = 0; i < nLen; ++i)
+ {
+ rStrm << rArray[i];
+ rStrm << (i < nLen - 1 ? ", " : " ");
+ }
+ rStrm << "}";
+ return rStrm;
+}
+
+void CharWidthArray::ConvertToKernArray()
+{
+ for (sal_Int32 i = 1; i < sal_Int32(maArray.size()); ++i)
+ maArray[i] += maArray[i - 1];
+}
+
+void CharWidthArray::ConvertToCharWidths()
+{
+ for (sal_Int32 i = maArray.size() - 1; i > 0; --i)
+ maArray[i] -= maArray[i - 1];
+}
+
+/// Convert maArray to kern array values, then invoke the function, and convert it back.
+template <typename Function> void CharWidthArray::InvokeWithKernArray(Function f)
+{
+ ConvertToKernArray();
+ f();
+ ConvertToCharWidths();
+}
+}
+
+CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSpaceDistributionHalfSpace)
+{
+ // Related to: tdf#149017
+ static const OUStringLiteral aText = u"ne del pro";
+ CharWidthArray aActual{ 720, 639, 360, 720, 639, 400, 360, 720, 480, 720 };
+ CharWidthArray aExpected{ 720, 851, 573, 720, 639, 612, 573, 720, 480, 720 };
+
+ aActual.InvokeWithKernArray(
+ [&] { Justify::SpaceDistribution(aActual.maArray, aText, 0, 10, 425, 0, false); });
+ CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
+}
+
+CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSpaceDistributionNoHalfSpace)
+{
+ // Related to: tdf#149017
+ static const OUStringLiteral aText = u"ne del pro";
+ CharWidthArray aActual{ 720, 639, 360, 720, 639, 400, 360, 720, 480, 720 };
+ CharWidthArray aExpected{ 720, 639, 785, 720, 639, 400, 785, 720, 480, 720 };
+
+ aActual.InvokeWithKernArray(
+ [&] { Justify::SpaceDistribution(aActual.maArray, aText, 0, 10, 425, 0, true); });
+ CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
+}
+
+CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSpaceDistributionUnicodeIVS)
+{
+ // Related to: tdf#148594
+ static const OUStringLiteral aText
+ = u"\u9B54\u9AD8\u4E00\U000E01E1\u4E08\u4F55\u9B54\u9AD8\u4E00\U000E01E1";
+ CharWidthArray aActual{ 1600, 1600, 1600, 0, 0, 1600, 1600, 1600, 1600, 1600, 0, 0 };
+ CharWidthArray aExpected{ 1800, 1800, 1800, 0, 0, 1800, 1800, 1800, 1800, 1800, 0, 0 };
+ aActual.InvokeWithKernArray(
+ [&] { Justify::SpaceDistribution(aActual.maArray, aText, 0, 12, 0, 200, false); });
+ CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */