summaryrefslogtreecommitdiffstats
path: root/sw/qa/core/txtnode/justify.cxx
blob: f8f178543a1f9efa80a4284f1f7e6f06d999042e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* -*- 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 <cstddef>
#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:
    KernArray maArray;
    template <typename... Args> CharWidthArray(Args&&... args)
    {
        for (auto arg : { args... })
            maArray.push_back(arg);
    }
    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 KernArray& 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 (std::size_t i = 1; i < maArray.size(); ++i)
        maArray.adjust(i, maArray[i - 1]);
}

void CharWidthArray::ConvertToCharWidths()
{
    for (sal_Int32 i = maArray.size() - 1; i > 0; --i)
        maArray.adjust(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 constexpr 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(
        [&] { sw::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 constexpr 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(
        [&] { sw::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 constexpr 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(
        [&] { sw::Justify::SpaceDistribution(aActual.maArray, aText, 0, 12, 0, 200, false); });
    CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
}

CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSnapToGrid)
{
    tools::Long nDelta = 0;
    // "曰〈道高一尺化太平〉云云"
    static constexpr OUStringLiteral aText
        = u"\u66f0\u3008\u9053\u9ad8\u4e00\u5c3a\u5316\u592a\u5e73\u3009\u4e91\u4e91";
    CharWidthArray aActual{ 880, 880, 880, 880, 880, 880, 880, 880, 880, 880, 880, 880 };
    CharWidthArray aExpected{
        1360, 1040, 1200, 1200, 1200, 1200, 1200, 1200, 1040, 1360, 1200, 1040
    };
    aActual.InvokeWithKernArray(
        [&] { nDelta = sw::Justify::SnapToGrid(aActual.maArray, aText, 0, 12, 400, false); });
    CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
    CPPUNIT_ASSERT_EQUAL(tools::Long(160), nDelta);
}

CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSnapToGridMixWidth)
{
    // Related to: tdf#149365
    tools::Long nDelta = 0;
    // "中中中ケコサシスセソカケコ" ( mixing fullwidth ideograph and half-width kana )
    static constexpr OUStringLiteral aText
        = u"\u4e2d\u4e2d\u4e2d\uff79\uff7a\uff7b\uff7c\uff7d\uff7e\uff7f\uff76\uff79\uff7a";
    CharWidthArray aActual{ 640, 640, 640, 320, 320, 320, 320, 320, 320, 320, 320, 320, 320 };
    CharWidthArray aExpected{ 800, 800, 760, 400, 400, 400, 400, 400, 400, 400, 400, 400, 360 };
    aActual.InvokeWithKernArray(
        [&] { nDelta = sw::Justify::SnapToGrid(aActual.maArray, aText, 0, 13, 400, false); });
    CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
    CPPUNIT_ASSERT_EQUAL(tools::Long(80), nDelta);
}

CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSnapToGridIVS)
{
    // Related to: tdf#149214
    tools::Long nDelta = 0;
    static constexpr OUStringLiteral aText = u"\u9053\u9ad8\u4e00\U000E01E2\u5c3a\u5316";

    CharWidthArray aActual{ 800, 800, 800, 0, 0, 800, 800 };
    CharWidthArray aExpected{ 800, 800, 800, 0, 0, 800, 800 };
    aActual.InvokeWithKernArray(
        [&] { nDelta = sw::Justify::SnapToGrid(aActual.maArray, aText, 0, 7, 400, false); });
    CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
    CPPUNIT_ASSERT_EQUAL(tools::Long(0), nDelta);
}

CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSnapToGridEdge1)
{
    CharWidthArray aActual{ 640, 640, 640, 640, 640, 640, 320, 960 };
    CharWidthArray aExpected{ 840, 840, 840, 840, 840, 840, 440, 1240 };
    aActual.InvokeWithKernArray(
        [&] { sw::Justify::SnapToGridEdge(aActual.maArray, 8, 400, 40, 0); });
    CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
}

CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSnapToGridEdge2)
{
    CharWidthArray aActual{ 640, 640, 640, 640, 640, 640, 320, 640 };
    CharWidthArray aExpected{ 840, 840, 840, 840, 840, 840, 440, 840 };
    aActual.InvokeWithKernArray(
        [&] { sw::Justify::SnapToGridEdge(aActual.maArray, 8, 100, 40, 80); });
    CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
}

CPPUNIT_TEST_FIXTURE(SwCoreJustifyTest, testSnapToGridEdgeIVS)
{
    CharWidthArray aActual{ 640, 0, 0, 640, 640, 640, 640, 640 };
    CharWidthArray aExpected{ 840, 0, 0, 840, 840, 840, 840, 840 };
    aActual.InvokeWithKernArray(
        [&] { sw::Justify::SnapToGridEdge(aActual.maArray, 8, 400, 40, 0); });
    CPPUNIT_ASSERT_EQUAL(aExpected, aActual);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */