LibreOffice Module i18nutil (master) 1
scripttypedetector.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <com/sun/star/i18n/ScriptDirection.hpp>
21#include <com/sun/star/i18n/UnicodeScript.hpp>
23#include <i18nutil/unicode.hxx>
24#include <o3tl/safeint.hxx>
25
26using namespace com::sun::star::i18n;
27
28const sal_Int16 scriptDirection[] = {
29 ScriptDirection::LEFT_TO_RIGHT, // DirectionProperty_LEFT_TO_RIGHT = 0,
30 ScriptDirection::RIGHT_TO_LEFT, // DirectionProperty_RIGHT_TO_LEFT = 1,
31 ScriptDirection::LEFT_TO_RIGHT, // DirectionProperty_EUROPEAN_NUMBER = 2,
32 ScriptDirection::LEFT_TO_RIGHT, // DirectionProperty_EUROPEAN_NUMBER_SEPARATOR = 3,
33 ScriptDirection::LEFT_TO_RIGHT, // DirectionProperty_EUROPEAN_NUMBER_TERMINATOR = 4,
34 ScriptDirection::RIGHT_TO_LEFT, // DirectionProperty_ARABIC_NUMBER = 5,
35 ScriptDirection::NEUTRAL, // DirectionProperty_COMMON_NUMBER_SEPARATOR = 6,
36 ScriptDirection::NEUTRAL, // DirectionProperty_BLOCK_SEPARATOR = 7,
37 ScriptDirection::NEUTRAL, // DirectionProperty_SEGMENT_SEPARATOR = 8,
38 ScriptDirection::NEUTRAL, // DirectionProperty_WHITE_SPACE_NEUTRAL = 9,
39 ScriptDirection::NEUTRAL, // DirectionProperty_OTHER_NEUTRAL = 10,
40 ScriptDirection::LEFT_TO_RIGHT, // DirectionProperty_LEFT_TO_RIGHT_EMBEDDING = 11,
41 ScriptDirection::LEFT_TO_RIGHT, // DirectionProperty_LEFT_TO_RIGHT_OVERRIDE = 12,
42 ScriptDirection::RIGHT_TO_LEFT, // DirectionProperty_RIGHT_TO_LEFT_ARABIC = 13,
43 ScriptDirection::RIGHT_TO_LEFT, // DirectionProperty_RIGHT_TO_LEFT_EMBEDDING = 14,
44 ScriptDirection::RIGHT_TO_LEFT, // DirectionProperty_RIGHT_TO_LEFT_OVERRIDE = 15,
45 ScriptDirection::NEUTRAL, // DirectionProperty_POP_DIRECTIONAL_FORMAT = 16,
46 ScriptDirection::NEUTRAL, // DirectionProperty_DIR_NON_SPACING_MARK = 17,
47 ScriptDirection::NEUTRAL, // DirectionProperty_BOUNDARY_NEUTRAL = 18,
48};
49
50sal_Int16 ScriptTypeDetector::getScriptDirection( std::u16string_view Text, sal_Int32 nPos, sal_Int16 defaultScriptDirection )
51{
53 return (dir == ScriptDirection::NEUTRAL) ? defaultScriptDirection : dir;
54}
55
56// return value '-1' means either the direction on nPos is not same as scriptDirection or nPos is out of range.
57sal_Int32 ScriptTypeDetector::beginOfScriptDirection( std::u16string_view Text, sal_Int32 nPos, sal_Int16 direction )
58{
59 sal_Int32 cPos = nPos;
60
61 if (cPos < static_cast<sal_Int32>(Text.size())) {
62 for (; cPos >= 0; cPos--) {
63 if (direction != getScriptDirection(Text, cPos, direction))
64 break;
65 }
66 }
67 return cPos == nPos ? -1 : cPos + 1;
68}
69
70sal_Int32 ScriptTypeDetector::endOfScriptDirection( std::u16string_view Text, sal_Int32 nPos, sal_Int16 direction )
71{
72 sal_Int32 cPos = nPos;
73 sal_Int32 len = Text.size();
74
75 if (cPos >=0) {
76 for (; cPos < len; cPos++) {
77 if (direction != getScriptDirection(Text, cPos, direction))
78 break;
79 }
80 }
81 return cPos == nPos ? -1 : cPos;
82}
83
84/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static sal_Int32 beginOfScriptDirection(std::u16string_view Text, sal_Int32 nPos, sal_Int16 scriptDirection)
static sal_Int16 getScriptDirection(std::u16string_view Text, sal_Int32 nPos, sal_Int16 defaultScriptDirection)
static sal_Int32 endOfScriptDirection(std::u16string_view Text, sal_Int32 nPos, sal_Int16 scriptDirection)
static sal_uInt8 getUnicodeDirection(const sal_Unicode ch)
Definition: unicode.cxx:176
sal_uInt16 nPos
const sal_Int16 scriptDirection[]