summaryrefslogtreecommitdiffstats
path: root/vcl/source/text/ImplLayoutArgs.cxx
blob: 826d288bba6f7bf5fcb88b2f0cbd2be0538df12f (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* -*- 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <ImplLayoutArgs.hxx>

#include <unicode/ubidi.h>
#include <unicode/uchar.h>

#include <algorithm>
#include <memory>
#include <utility>

namespace vcl::text
{
ImplLayoutArgs::ImplLayoutArgs(const OUString& rStr, int nMinCharPos, int nEndCharPos,
                               SalLayoutFlags nFlags, LanguageTag aLanguageTag,
                               vcl::text::TextLayoutCache const* const pLayoutCache)
    : maLanguageTag(std::move(aLanguageTag))
    , mnFlags(nFlags)
    , mrStr(rStr)
    , mnMinCharPos(nMinCharPos)
    , mnEndCharPos(nEndCharPos)
    , m_pTextLayoutCache(pLayoutCache)
    , mpDXArray(nullptr)
    , mpKashidaArray(nullptr)
    , mnLayoutWidth(0)
    , mnOrientation(0)
{
    if (mnFlags & SalLayoutFlags::BiDiStrong)
    {
        // handle strong BiDi mode

        // do not bother to BiDi analyze strong LTR/RTL
        // TODO: can we assume these strings do not have unicode control chars?
        //       if not remove the control characters from the runs
        bool bRTL(mnFlags & SalLayoutFlags::BiDiRtl);
        AddRun(mnMinCharPos, mnEndCharPos, bRTL);
    }
    else
    {
        // handle weak BiDi mode
        UBiDiLevel nLevel = (mnFlags & SalLayoutFlags::BiDiRtl) ? 1 : 0;

        // prepare substring for BiDi analysis
        // TODO: reuse allocated pParaBidi
        UErrorCode rcI18n = U_ZERO_ERROR;
        const int nLength = mnEndCharPos - mnMinCharPos;
        UBiDi* pParaBidi = ubidi_openSized(nLength, 0, &rcI18n);
        if (!pParaBidi)
            return;
        ubidi_setPara(pParaBidi, reinterpret_cast<const UChar*>(mrStr.getStr()) + mnMinCharPos,
                      nLength, nLevel, nullptr, &rcI18n);

        // run BiDi algorithm
        const int nRunCount = ubidi_countRuns(pParaBidi, &rcI18n);
        for (int i = 0; i < nRunCount; ++i)
        {
            int32_t nMinPos, nRunLength;
            const UBiDiDirection nDir = ubidi_getVisualRun(pParaBidi, i, &nMinPos, &nRunLength);
            const int nPos0 = nMinPos + mnMinCharPos;
            const int nPos1 = nPos0 + nRunLength;

            const bool bRTL = (nDir == UBIDI_RTL);
            AddRun(nPos0, nPos1, bRTL);
        }

        // cleanup BiDi engine
        ubidi_close(pParaBidi);
    }

    // prepare calls to GetNextPos/GetNextRun
    maRuns.ResetPos();
}

void ImplLayoutArgs::SetLayoutWidth(double nWidth) { mnLayoutWidth = nWidth; }

void ImplLayoutArgs::SetDXArray(double const* pDXArray) { mpDXArray = pDXArray; }

void ImplLayoutArgs::SetKashidaArray(sal_Bool const* pKashidaArray)
{
    mpKashidaArray = pKashidaArray;
}

void ImplLayoutArgs::SetOrientation(Degree10 nOrientation) { mnOrientation = nOrientation; }

void ImplLayoutArgs::ResetPos() { maRuns.ResetPos(); }

bool ImplLayoutArgs::GetNextPos(int* nCharPos, bool* bRTL)
{
    return maRuns.GetNextPos(nCharPos, bRTL);
}

void ImplLayoutArgs::AddFallbackRun(int nMinRunPos, int nEndRunPos, bool bRTL)
{
    maFallbackRuns.AddRun(nMinRunPos, nEndRunPos, bRTL);
}

bool ImplLayoutArgs::HasFallbackRun() const { return !maFallbackRuns.IsEmpty(); }

static bool IsControlChar(sal_UCS4 cChar)
{
    // C0 control characters
    if ((0x0001 <= cChar) && (cChar <= 0x001F))
        return true;
    // formatting characters
    if ((0x200E <= cChar) && (cChar <= 0x200F))
        return true;
    if ((0x2028 <= cChar) && (cChar <= 0x202E))
        return true;
    // deprecated formatting characters
    if ((0x206A <= cChar) && (cChar <= 0x206F))
        return true;
    if (0x2060 == cChar)
        return true;
    // byte order markers and invalid unicode
    if ((cChar == 0xFEFF) || (cChar == 0xFFFE) || (cChar == 0xFFFF))
        return true;
    // drop null character too, broken documents may contain it (ofz34898-1.doc)
    if (cChar == 0)
        return true;
    return false;
}

// add a run after splitting it up to get rid of control chars
void ImplLayoutArgs::AddRun(int nCharPos0, int nCharPos1, bool bRTL)
{
    SAL_WARN_IF(nCharPos0 > nCharPos1, "vcl", "ImplLayoutArgs::AddRun() nCharPos0>=nCharPos1");

    // remove control characters from runs by splitting them up
    if (!bRTL)
    {
        for (int i = nCharPos0; i < nCharPos1; ++i)
            if (IsControlChar(mrStr[i]))
            {
                // add run until control char
                maRuns.AddRun(nCharPos0, i, bRTL);
                nCharPos0 = i + 1;
            }
    }
    else
    {
        for (int i = nCharPos1; --i >= nCharPos0;)
            if (IsControlChar(mrStr[i]))
            {
                // add run until control char
                maRuns.AddRun(i + 1, nCharPos1, bRTL);
                nCharPos1 = i;
            }
    }

    // add remainder of run
    maRuns.AddRun(nCharPos0, nCharPos1, bRTL);
}

bool ImplLayoutArgs::PrepareFallback(const SalLayoutGlyphsImpl* pGlyphsImpl)
{
    // Generate runs with pre-calculated glyph items instead maFallbackRuns.
    if (pGlyphsImpl != nullptr)
    {
        maRuns.Clear();
        maFallbackRuns.Clear();

        for (auto const& aGlyphItem : *pGlyphsImpl)
        {
            for (int i = aGlyphItem.charPos(); i < aGlyphItem.charPos() + aGlyphItem.charCount();
                 ++i)
                maRuns.AddPos(i, aGlyphItem.IsRTLGlyph());
        }

        return !maRuns.IsEmpty();
    }

    // short circuit if no fallback is needed
    if (maFallbackRuns.IsEmpty())
    {
        maRuns.Clear();
        return false;
    }

    // convert the fallback requests to layout requests
    bool bRTL;
    int nMin, nEnd;

    // get the individual fallback requests
    std::vector<int> aPosVector;
    aPosVector.reserve(mrStr.getLength());
    maFallbackRuns.ResetPos();
    for (; maFallbackRuns.GetRun(&nMin, &nEnd, &bRTL); maFallbackRuns.NextRun())
        for (int i = nMin; i < nEnd; ++i)
            aPosVector.push_back(i);
    maFallbackRuns.Clear();

    // sort the individual fallback requests
    std::sort(aPosVector.begin(), aPosVector.end());

    // adjust fallback runs to have the same order and limits of the original runs
    ImplLayoutRuns aNewRuns;
    maRuns.ResetPos();
    for (; maRuns.GetRun(&nMin, &nEnd, &bRTL); maRuns.NextRun())
    {
        if (!bRTL)
        {
            auto it = std::lower_bound(aPosVector.begin(), aPosVector.end(), nMin);
            for (; (it != aPosVector.end()) && (*it < nEnd); ++it)
                aNewRuns.AddPos(*it, bRTL);
        }
        else
        {
            auto it = std::upper_bound(aPosVector.begin(), aPosVector.end(), nEnd);
            while ((it != aPosVector.begin()) && (*--it >= nMin))
                aNewRuns.AddPos(*it, bRTL);
        }
    }

    maRuns = aNewRuns; // TODO: use vector<>::swap()
    maRuns.ResetPos();
    return true;
}

bool ImplLayoutArgs::GetNextRun(int* nMinRunPos, int* nEndRunPos, bool* bRTL)
{
    bool bValid = maRuns.GetRun(nMinRunPos, nEndRunPos, bRTL);
    maRuns.NextRun();
    return bValid;
}
}

std::ostream& operator<<(std::ostream& s, vcl::text::ImplLayoutArgs const& rArgs)
{
#ifndef SAL_LOG_INFO
    (void)rArgs;
#else
    s << "ImplLayoutArgs{";

    s << "Flags=";
    if (rArgs.mnFlags == SalLayoutFlags::NONE)
        s << 0;
    else
    {
        bool need_or = false;
        s << "{";
#define TEST(x)                                                                                    \
    if (rArgs.mnFlags & SalLayoutFlags::x)                                                         \
    {                                                                                              \
        if (need_or)                                                                               \
            s << "|";                                                                              \
        s << #x;                                                                                   \
        need_or = true;                                                                            \
    }
        TEST(BiDiRtl);
        TEST(BiDiStrong);
        TEST(RightAlign);
        TEST(DisableKerning);
        TEST(KerningAsian);
        TEST(Vertical);
        TEST(DisableLigatures);
        TEST(ForFallback);
#undef TEST
        s << "}";
    }

    const int nLength = rArgs.mrStr.getLength();

    s << ",Length=" << nLength;
    s << ",MinCharPos=" << rArgs.mnMinCharPos;
    s << ",EndCharPos=" << rArgs.mnEndCharPos;

    s << ",Str=\"";
    int lim = nLength;
    if (lim > 10)
        lim = 7;
    for (int i = 0; i < lim; i++)
    {
        if (rArgs.mrStr[i] == '\n')
            s << "\\n";
        else if (rArgs.mrStr[i] < ' ' || (rArgs.mrStr[i] >= 0x7F && rArgs.mrStr[i] <= 0xFF))
            s << "\\0x" << std::hex << std::setw(2) << std::setfill('0')
              << static_cast<int>(rArgs.mrStr[i]) << std::setfill(' ') << std::setw(1) << std::dec;
        else if (rArgs.mrStr[i] < 0x7F)
            s << static_cast<char>(rArgs.mrStr[i]);
        else
            s << "\\u" << std::hex << std::setw(4) << std::setfill('0')
              << static_cast<int>(rArgs.mrStr[i]) << std::setfill(' ') << std::setw(1) << std::dec;
    }
    if (nLength > lim)
        s << "...";
    s << "\"";

    s << ",DXArray=";
    if (rArgs.mpDXArray)
    {
        s << "[";
        int count = rArgs.mnEndCharPos - rArgs.mnMinCharPos;
        lim = count;
        if (lim > 10)
            lim = 7;
        for (int i = 0; i < lim; i++)
        {
            s << rArgs.mpDXArray[i];
            if (i < lim - 1)
                s << ",";
        }
        if (count > lim)
        {
            if (count > lim + 1)
                s << "...";
            s << rArgs.mpDXArray[count - 1];
        }
        s << "]";
    }
    else
        s << "NULL";

    s << ",KashidaArray=";
    if (rArgs.mpKashidaArray)
    {
        s << "[";
        int count = rArgs.mnEndCharPos - rArgs.mnMinCharPos;
        lim = count;
        if (lim > 10)
            lim = 7;
        for (int i = 0; i < lim; i++)
        {
            s << rArgs.mpKashidaArray[i];
            if (i < lim - 1)
                s << ",";
        }
        if (count > lim)
        {
            if (count > lim + 1)
                s << "...";
            s << rArgs.mpKashidaArray[count - 1];
        }
        s << "]";
    }
    else
        s << "NULL";

    s << ",LayoutWidth=" << rArgs.mnLayoutWidth;

    s << "}";

#endif
    return s;
}

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