summaryrefslogtreecommitdiff
path: root/sw
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
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')
-rw-r--r--sw/CppunitTest_sw_core_txtnode.mk2
-rw-r--r--sw/qa/core/txtnode/justify.cxx112
2 files changed, 114 insertions, 0 deletions
diff --git a/sw/CppunitTest_sw_core_txtnode.mk b/sw/CppunitTest_sw_core_txtnode.mk
index 441e415267b7..4fb34a695656 100644
--- a/sw/CppunitTest_sw_core_txtnode.mk
+++ b/sw/CppunitTest_sw_core_txtnode.mk
@@ -15,6 +15,7 @@ $(eval $(call gb_CppunitTest_use_common_precompiled_header,sw_core_txtnode))
$(eval $(call gb_CppunitTest_add_exception_objects,sw_core_txtnode, \
sw/qa/core/txtnode/txtnode \
+ sw/qa/core/txtnode/justify \
))
$(eval $(call gb_CppunitTest_use_libraries,sw_core_txtnode, \
@@ -44,6 +45,7 @@ $(eval $(call gb_CppunitTest_use_externals,sw_core_txtnode,\
$(eval $(call gb_CppunitTest_set_include,sw_core_txtnode,\
-I$(SRCDIR)/sw/inc \
-I$(SRCDIR)/sw/source/core/inc \
+ -I$(SRCDIR)/sw/source/core/txtnode \
-I$(SRCDIR)/sw/source/uibase/inc \
-I$(SRCDIR)/sw/qa/inc \
$$(INCLUDE) \
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: */