Line data Source code
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 <boost/static_assert.hpp>
21 :
22 : #include <tools/shl.hxx>
23 :
24 : #include <com/sun/star/i18n/DirectionProperty.hpp>
25 :
26 : #include <i18nlangtag/lang.h>
27 : #include <i18nlangtag/mslangid.hxx>
28 : #include <i18nlangtag/languagetag.hxx>
29 :
30 : #include <svtools/svtools.hrc>
31 : #include <svtools/svtresid.hxx>
32 : #include <svtools/langtab.hxx>
33 : #include <unotools/syslocale.hxx>
34 :
35 :
36 : using namespace ::com::sun::star;
37 :
38 :
39 :
40 0 : SVT_DLLPUBLIC const OUString ApplyLreOrRleEmbedding( const OUString &rText )
41 : {
42 0 : const sal_Int32 nLen = rText.getLength();
43 0 : if (nLen == 0)
44 0 : return OUString();
45 :
46 0 : const sal_Unicode cLRE_Embedding = 0x202A; // the start char of an LRE embedding
47 0 : const sal_Unicode cRLE_Embedding = 0x202B; // the start char of an RLE embedding
48 0 : const sal_Unicode cPopDirectionalFormat = 0x202C; // the unicode PDF (POP_DIRECTIONAL_FORMAT) char that terminates an LRE/RLE embedding
49 :
50 : // check if there are alreay embedding characters at the strings start
51 : // if so change nothing
52 0 : const sal_Unicode cChar = rText[0];
53 0 : if (cChar == cLRE_Embedding || cChar == cRLE_Embedding)
54 0 : return rText;
55 :
56 : // since we only call the function getCharacterDirection
57 : // it does not matter which locale the CharClass is for.
58 : // Thus we can readily make use of SvtSysLocale::GetCharClass()
59 : // which should come at no cost...
60 0 : SvtSysLocale aSysLocale;
61 0 : const CharClass &rCharClass = aSysLocale.GetCharClass();
62 :
63 : // we should look for the first non-neutral LTR or RTL character
64 : // and use that to determine the embedding of the whole text...
65 : // Thus we can avoid to check every character of the text.
66 0 : bool bFound = false;
67 0 : bool bIsRtlText = false;
68 0 : for (sal_uInt16 i = 0; i < nLen && !bFound; ++i)
69 : {
70 0 : sal_Int16 nDirection = rCharClass.getCharacterDirection( rText, i );
71 0 : switch (nDirection)
72 : {
73 : case i18n::DirectionProperty_LEFT_TO_RIGHT :
74 : case i18n::DirectionProperty_LEFT_TO_RIGHT_EMBEDDING :
75 : case i18n::DirectionProperty_LEFT_TO_RIGHT_OVERRIDE :
76 : case i18n::DirectionProperty_EUROPEAN_NUMBER :
77 : case i18n::DirectionProperty_ARABIC_NUMBER : // yes! arabic numbers are written from left to right
78 : {
79 0 : bIsRtlText = false;
80 0 : bFound = true;
81 0 : break;
82 : }
83 :
84 : case i18n::DirectionProperty_RIGHT_TO_LEFT :
85 : case i18n::DirectionProperty_RIGHT_TO_LEFT_ARABIC :
86 : case i18n::DirectionProperty_RIGHT_TO_LEFT_EMBEDDING :
87 : case i18n::DirectionProperty_RIGHT_TO_LEFT_OVERRIDE :
88 : {
89 0 : bIsRtlText = true;
90 0 : bFound = true;
91 0 : break;
92 : }
93 :
94 : default:
95 : {
96 : // nothing to be done, character is considered to be neutral we need to look further ...
97 : }
98 : }
99 : }
100 :
101 0 : sal_Unicode cStart = cLRE_Embedding; // default is to use LRE embedding characters
102 0 : if (bIsRtlText)
103 0 : cStart = cRLE_Embedding; // then use RLE embedding
104 :
105 : // add embedding start and end chars to the text if the direction could be determined
106 0 : OUString aRes( rText );
107 0 : if (bFound)
108 : {
109 0 : aRes = OUString(cStart) + aRes + OUString(cPopDirectionalFormat);
110 : }
111 :
112 0 : return aRes;
113 : }
114 :
115 :
116 :
117 : namespace {
118 : BOOST_STATIC_ASSERT(
119 : 16642 == STR_ARR_SVT_LANGUAGE_TABLE);
120 : // "The value of STR_ARR_SVT_LANGUAGE_TABLE has changed. wizards/com/sun/star/wizards/letter/LocaleCodes.java has this value hard coded, please adapt it to your change."
121 : }
122 :
123 0 : SvtLanguageTable::SvtLanguageTable() :
124 0 : ResStringArray( SvtResId( STR_ARR_SVT_LANGUAGE_TABLE ) )
125 : {
126 0 : }
127 :
128 :
129 :
130 0 : SvtLanguageTable::~SvtLanguageTable()
131 : {
132 0 : }
133 :
134 :
135 :
136 0 : const OUString SvtLanguageTable::GetString( const LanguageType eType, bool bUserInterfaceSelection ) const
137 : {
138 0 : LanguageType eLang = MsLangId::getReplacementForObsoleteLanguage( eType, bUserInterfaceSelection);
139 0 : sal_uInt32 nPos = FindIndex( eLang );
140 :
141 0 : if ( RESARRAY_INDEX_NOTFOUND != nPos && nPos < Count() )
142 0 : return ResStringArray::GetString( nPos );
143 :
144 : //Rather than return a fairly useless "Unknown" name, return a geeky but usable-in-a-pinch lang-tag
145 0 : OUString sLangTag(LanguageTag::convertToBcp47(eType));
146 : SAL_WARN("svtools.misc", "Language: 0x"
147 : << std::hex << eType
148 : << " with unknown name, so returning lang-tag of: "
149 : << sLangTag);
150 0 : return sLangTag;
151 : }
152 :
153 0 : OUString SvtLanguageTable::GetLanguageString( const LanguageType eType )
154 : {
155 0 : static const SvtLanguageTable aLangTable;
156 0 : return aLangTable.GetString( eType );
157 : }
158 :
159 :
160 :
161 0 : LanguageType SvtLanguageTable::GetType( const OUString& rStr ) const
162 : {
163 0 : LanguageType eType = LANGUAGE_DONTKNOW;
164 0 : sal_uInt32 nCount = Count();
165 :
166 0 : for ( sal_uInt32 i = 0; i < nCount; ++i )
167 : {
168 0 : if (ResStringArray::GetString( i ).equals(rStr))
169 : {
170 0 : eType = LanguageType( GetValue( i ) );
171 0 : break;
172 : }
173 : }
174 0 : return eType;
175 : }
176 :
177 :
178 :
179 0 : sal_uInt32 SvtLanguageTable::GetEntryCount() const
180 : {
181 0 : return Count();
182 : }
183 :
184 :
185 :
186 0 : LanguageType SvtLanguageTable::GetTypeAtIndex( sal_uInt32 nIndex ) const
187 : {
188 0 : LanguageType nType = LANGUAGE_DONTKNOW;
189 0 : if (nIndex < Count())
190 0 : nType = LanguageType( GetValue( nIndex ) );
191 0 : return nType;
192 : }
193 :
194 :
195 :
196 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|