Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / qa / core / txtnode / justify.cxx
blob6a8afd2b52ef4011dace703578f46e4f4f4c9301
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <swmodeltestbase.hxx>
11 #include <justify.hxx>
12 #include <cstddef>
13 #include <vector>
15 namespace
17 /// Covers sw/source/core/txtnode/justify fixes.
18 class SwCoreJustifyTest : public SwModelTestBase
22 /// CharWidthArray: store char widths because they are more readable.
23 class CharWidthArray
25 public:
26 KernArray maArray;
27 template <typename... Args> CharWidthArray(Args&&... args)
29 for (auto arg : { args... })
30 maArray.push_back(arg);
32 template <typename Function> void InvokeWithKernArray(Function f);
33 void ConvertToKernArray();
34 void ConvertToCharWidths();
37 inline bool operator==(const CharWidthArray& lhs, const CharWidthArray& rhs)
39 return lhs.maArray == rhs.maArray;
42 std::ostream& operator<<(std::ostream& rStrm, const CharWidthArray& rCharWidthArray)
44 const KernArray& rArray(rCharWidthArray.maArray);
45 sal_Int32 nLen = rArray.size();
46 rStrm << "{ ";
47 for (sal_Int32 i = 0; i < nLen; ++i)
49 rStrm << rArray[i];
50 rStrm << (i < nLen - 1 ? ", " : " ");
52 rStrm << "}";
53 return rStrm;
56 void CharWidthArray::ConvertToKernArray()
58 for (std::size_t i = 1; i < maArray.size(); ++i)
59 maArray.adjust(i, maArray[i - 1]);
62 void CharWidthArray::ConvertToCharWidths()
64 for (sal_Int32 i = maArray.size() - 1; i > 0; --i)
65 maArray.adjust(i, -maArray[i - 1]);
68 /// Convert maArray to kern array values, then invoke the function, and convert it back.
69 template <typename Function> void CharWidthArray::InvokeWithKernArray(Function f)
71 ConvertToKernArray();
72 f();
73 ConvertToCharWidths();
77 CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSpaceDistributionHalfSpace)
79 // Related to: tdf#149017
80 static const OUStringLiteral aText = u"ne del pro";
81 CharWidthArray aActual{ 720, 639, 360, 720, 639, 400, 360, 720, 480, 720 };
82 CharWidthArray aExpected{ 720, 851, 573, 720, 639, 612, 573, 720, 480, 720 };
84 aActual.InvokeWithKernArray(
85 [&] { sw::Justify::SpaceDistribution(aActual.maArray, aText, 0, 10, 425, 0, false); });
86 CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
89 CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSpaceDistributionNoHalfSpace)
91 // Related to: tdf#149017
92 static const OUStringLiteral aText = u"ne del pro";
93 CharWidthArray aActual{ 720, 639, 360, 720, 639, 400, 360, 720, 480, 720 };
94 CharWidthArray aExpected{ 720, 639, 785, 720, 639, 400, 785, 720, 480, 720 };
96 aActual.InvokeWithKernArray(
97 [&] { sw::Justify::SpaceDistribution(aActual.maArray, aText, 0, 10, 425, 0, true); });
98 CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
101 CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSpaceDistributionUnicodeIVS)
103 // Related to: tdf#148594
104 static const OUStringLiteral aText
105 = u"\u9B54\u9AD8\u4E00\U000E01E1\u4E08\u4F55\u9B54\u9AD8\u4E00\U000E01E1";
106 CharWidthArray aActual{ 1600, 1600, 1600, 0, 0, 1600, 1600, 1600, 1600, 1600, 0, 0 };
107 CharWidthArray aExpected{ 1800, 1800, 1800, 0, 0, 1800, 1800, 1800, 1800, 1800, 0, 0 };
108 aActual.InvokeWithKernArray(
109 [&] { sw::Justify::SpaceDistribution(aActual.maArray, aText, 0, 12, 0, 200, false); });
110 CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
113 CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSnapToGrid)
115 tools::Long nDelta = 0;
116 // "曰〈道高一尺化太平〉云云"
117 static const OUStringLiteral aText
118 = u"\u66f0\u3008\u9053\u9ad8\u4e00\u5c3a\u5316\u592a\u5e73\u3009\u4e91\u4e91";
119 CharWidthArray aActual{ 880, 880, 880, 880, 880, 880, 880, 880, 880, 880, 880, 880 };
120 CharWidthArray aExpected{
121 1360, 1040, 1200, 1200, 1200, 1200, 1200, 1200, 1040, 1360, 1200, 1040
123 aActual.InvokeWithKernArray(
124 [&] { nDelta = sw::Justify::SnapToGrid(aActual.maArray, aText, 0, 12, 400, false); });
125 CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
126 CPPUNIT_ASSERT_EQUAL(tools::Long(160), nDelta);
129 CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSnapToGridMixWidth)
131 // Related to: tdf#149365
132 tools::Long nDelta = 0;
133 // "中中中ケコサシスセソカケコ" ( mixing fullwidth ideograph and half-width kana )
134 static const OUStringLiteral aText
135 = u"\u4e2d\u4e2d\u4e2d\uff79\uff7a\uff7b\uff7c\uff7d\uff7e\uff7f\uff76\uff79\uff7a";
136 CharWidthArray aActual{ 640, 640, 640, 320, 320, 320, 320, 320, 320, 320, 320, 320, 320 };
137 CharWidthArray aExpected{ 800, 800, 760, 400, 400, 400, 400, 400, 400, 400, 400, 400, 360 };
138 aActual.InvokeWithKernArray(
139 [&] { nDelta = sw::Justify::SnapToGrid(aActual.maArray, aText, 0, 13, 400, false); });
140 CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
141 CPPUNIT_ASSERT_EQUAL(tools::Long(80), nDelta);
144 CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSnapToGridIVS)
146 // Related to: tdf#149214
147 tools::Long nDelta = 0;
148 static const OUStringLiteral aText = u"\u9053\u9ad8\u4e00\U000E01E2\u5c3a\u5316";
150 CharWidthArray aActual{ 800, 800, 800, 0, 0, 800, 800 };
151 CharWidthArray aExpected{ 800, 800, 800, 0, 0, 800, 800 };
152 aActual.InvokeWithKernArray(
153 [&] { nDelta = sw::Justify::SnapToGrid(aActual.maArray, aText, 0, 7, 400, false); });
154 CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
155 CPPUNIT_ASSERT_EQUAL(tools::Long(0), nDelta);
158 CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSnapToGridEdge1)
160 CharWidthArray aActual{ 640, 640, 640, 640, 640, 640, 320, 960 };
161 CharWidthArray aExpected{ 840, 840, 840, 840, 840, 840, 440, 1240 };
162 aActual.InvokeWithKernArray(
163 [&] { sw::Justify::SnapToGridEdge(aActual.maArray, 8, 400, 40, 0); });
164 CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
167 CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSnapToGridEdge2)
169 CharWidthArray aActual{ 640, 640, 640, 640, 640, 640, 320, 640 };
170 CharWidthArray aExpected{ 840, 840, 840, 840, 840, 840, 440, 840 };
171 aActual.InvokeWithKernArray(
172 [&] { sw::Justify::SnapToGridEdge(aActual.maArray, 8, 100, 40, 80); });
173 CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
176 CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSnapToGridEdgeIVS)
178 CharWidthArray aActual{ 640, 0, 0, 640, 640, 640, 640, 640 };
179 CharWidthArray aExpected{ 840, 0, 0, 840, 840, 840, 840, 840 };
180 aActual.InvokeWithKernArray(
181 [&] { sw::Justify::SnapToGridEdge(aActual.maArray, 8, 400, 40, 0); });
182 CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */