/src/libreoffice/i18nutil/source/utility/guessparadirection.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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 | | */ |
9 | | |
10 | | #include <i18nutil/guessparadirection.hxx> |
11 | | #include <i18nutil/unicode.hxx> |
12 | | #include <unicode/uchar.h> |
13 | | #include <unicode/ubidi.h> |
14 | | |
15 | | i18nutil::ParagraphDirection i18nutil::GuessParagraphDirection(const OUString& rText) |
16 | 0 | { |
17 | | // tdf#162120: This algorithm implements Unicode UAX #9 3.3.1 |
18 | | // for automatically determining the paragraph level without |
19 | | // override from a higher-level protocol. |
20 | | // The algorithm has been extended to explicitly communicate |
21 | | // an ambiguous case, rather than defaulting to LTR. |
22 | |
|
23 | 0 | sal_Int32 nBase = 0; |
24 | |
|
25 | 0 | sal_Int32 nIsolateLevel = 0; |
26 | 0 | while (nBase < rText.getLength()) |
27 | 0 | { |
28 | 0 | auto nChar = rText.iterateCodePoints(&nBase); |
29 | 0 | auto nCharDir = u_charDirection(nChar); |
30 | |
|
31 | 0 | switch (nCharDir) |
32 | 0 | { |
33 | 0 | case U_POP_DIRECTIONAL_ISOLATE: |
34 | 0 | nIsolateLevel = std::max(sal_Int32{ 0 }, nIsolateLevel - 1); |
35 | 0 | break; |
36 | | |
37 | 0 | case U_LEFT_TO_RIGHT_ISOLATE: |
38 | 0 | case U_RIGHT_TO_LEFT_ISOLATE: |
39 | 0 | ++nIsolateLevel; |
40 | 0 | break; |
41 | | |
42 | 0 | case U_LEFT_TO_RIGHT: |
43 | 0 | if (nIsolateLevel == 0) |
44 | 0 | { |
45 | 0 | return ParagraphDirection::LeftToRight; |
46 | 0 | } |
47 | 0 | break; |
48 | | |
49 | 0 | case U_RIGHT_TO_LEFT: |
50 | 0 | case U_RIGHT_TO_LEFT_ARABIC: |
51 | 0 | if (nIsolateLevel == 0) |
52 | 0 | { |
53 | 0 | return ParagraphDirection::RightToLeft; |
54 | 0 | } |
55 | 0 | break; |
56 | | |
57 | 0 | default: |
58 | 0 | break; |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | 0 | return ParagraphDirection::Ambiguous; |
63 | 0 | } |
64 | | |
65 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |