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 <com/sun/star/i18n/DirectionProperty.hpp>
21 :
22 : #include <i18nlangtag/lang.h>
23 : #include <i18nlangtag/mslangid.hxx>
24 : #include <i18nlangtag/languagetag.hxx>
25 :
26 : #include <svtools/svtools.hrc>
27 : #include <svtools/svtresid.hxx>
28 : #include <svtools/langtab.hxx>
29 : #include <unotools/syslocale.hxx>
30 : #include <tools/resary.hxx>
31 :
32 :
33 : using namespace ::com::sun::star;
34 :
35 : class SvtLanguageTableImpl : public ResStringArray
36 : {
37 : public:
38 :
39 : SvtLanguageTableImpl();
40 : virtual ~SvtLanguageTableImpl();
41 :
42 : bool HasType( const LanguageType eType ) const;
43 : const OUString GetString( const LanguageType eType, bool bUserInterfaceSelection = false ) const;
44 : LanguageType GetType( const OUString& rStr ) const;
45 : sal_uInt32 GetEntryCount() const;
46 : LanguageType GetTypeAtIndex( sal_uInt32 nIndex ) const;
47 : };
48 :
49 : namespace {
50 : struct theLanguageTable : public rtl::Static< SvtLanguageTableImpl, theLanguageTable > {};
51 : }
52 :
53 0 : const OUString ApplyLreOrRleEmbedding( const OUString &rText )
54 : {
55 0 : const sal_Int32 nLen = rText.getLength();
56 0 : if (nLen == 0)
57 0 : return OUString();
58 :
59 0 : const sal_Unicode cLRE_Embedding = 0x202A; // the start char of an LRE embedding
60 0 : const sal_Unicode cRLE_Embedding = 0x202B; // the start char of an RLE embedding
61 0 : const sal_Unicode cPopDirectionalFormat = 0x202C; // the unicode PDF (POP_DIRECTIONAL_FORMAT) char that terminates an LRE/RLE embedding
62 :
63 : // check if there are alreay embedding characters at the strings start
64 : // if so change nothing
65 0 : const sal_Unicode cChar = rText[0];
66 0 : if (cChar == cLRE_Embedding || cChar == cRLE_Embedding)
67 0 : return rText;
68 :
69 : // since we only call the function getCharacterDirection
70 : // it does not matter which locale the CharClass is for.
71 : // Thus we can readily make use of SvtSysLocale::GetCharClass()
72 : // which should come at no cost...
73 0 : SvtSysLocale aSysLocale;
74 0 : const CharClass &rCharClass = aSysLocale.GetCharClass();
75 :
76 : // we should look for the first non-neutral LTR or RTL character
77 : // and use that to determine the embedding of the whole text...
78 : // Thus we can avoid to check every character of the text.
79 0 : bool bFound = false;
80 0 : bool bIsRtlText = false;
81 0 : for (sal_uInt16 i = 0; i < nLen && !bFound; ++i)
82 : {
83 0 : sal_Int16 nDirection = rCharClass.getCharacterDirection( rText, i );
84 0 : switch (nDirection)
85 : {
86 : case i18n::DirectionProperty_LEFT_TO_RIGHT :
87 : case i18n::DirectionProperty_LEFT_TO_RIGHT_EMBEDDING :
88 : case i18n::DirectionProperty_LEFT_TO_RIGHT_OVERRIDE :
89 : case i18n::DirectionProperty_EUROPEAN_NUMBER :
90 : case i18n::DirectionProperty_ARABIC_NUMBER : // yes! arabic numbers are written from left to right
91 : {
92 0 : bIsRtlText = false;
93 0 : bFound = true;
94 0 : break;
95 : }
96 :
97 : case i18n::DirectionProperty_RIGHT_TO_LEFT :
98 : case i18n::DirectionProperty_RIGHT_TO_LEFT_ARABIC :
99 : case i18n::DirectionProperty_RIGHT_TO_LEFT_EMBEDDING :
100 : case i18n::DirectionProperty_RIGHT_TO_LEFT_OVERRIDE :
101 : {
102 0 : bIsRtlText = true;
103 0 : bFound = true;
104 0 : break;
105 : }
106 :
107 : default:
108 : {
109 : // nothing to be done, character is considered to be neutral we need to look further ...
110 : }
111 : }
112 : }
113 :
114 0 : sal_Unicode cStart = cLRE_Embedding; // default is to use LRE embedding characters
115 0 : if (bIsRtlText)
116 0 : cStart = cRLE_Embedding; // then use RLE embedding
117 :
118 : // add embedding start and end chars to the text if the direction could be determined
119 0 : OUString aRes( rText );
120 0 : if (bFound)
121 : {
122 0 : aRes = OUString(cStart) + aRes + OUString(cPopDirectionalFormat);
123 : }
124 :
125 0 : return aRes;
126 : }
127 :
128 23 : SvtLanguageTableImpl::SvtLanguageTableImpl() :
129 23 : ResStringArray( SvtResId( STR_ARR_SVT_LANGUAGE_TABLE ) )
130 : {
131 23 : }
132 :
133 23 : SvtLanguageTableImpl::~SvtLanguageTableImpl()
134 : {
135 23 : }
136 :
137 :
138 0 : bool SvtLanguageTableImpl::HasType( const LanguageType eType ) const
139 : {
140 0 : LanguageType eLang = MsLangId::getReplacementForObsoleteLanguage( eType, false);
141 0 : sal_uInt32 nPos = FindIndex( eLang );
142 :
143 0 : return RESARRAY_INDEX_NOTFOUND != nPos && nPos < Count();
144 : }
145 :
146 0 : bool SvtLanguageTable::HasLanguageType( const LanguageType eType )
147 : {
148 0 : return theLanguageTable::get().HasType( eType );
149 : }
150 :
151 :
152 547 : const OUString SvtLanguageTableImpl::GetString( const LanguageType eType, bool bUserInterfaceSelection ) const
153 : {
154 547 : LanguageType eLang = MsLangId::getReplacementForObsoleteLanguage( eType, bUserInterfaceSelection);
155 547 : sal_uInt32 nPos = FindIndex( eLang );
156 :
157 547 : if ( RESARRAY_INDEX_NOTFOUND != nPos && nPos < Count() )
158 547 : return ResStringArray::GetString( nPos );
159 :
160 : //Rather than return a fairly useless "Unknown" name, return a geeky but usable-in-a-pinch lang-tag
161 0 : OUString sLangTag(LanguageTag::convertToBcp47(eType));
162 : SAL_WARN("svtools.misc", "Language: 0x"
163 : << std::hex << eType
164 : << " with unknown name, so returning lang-tag of: "
165 : << sLangTag);
166 :
167 : // And add it to the table if it is an on-the-fly-id, which it usually is,
168 : // so it is available in all subsequent language boxes.
169 0 : if (LanguageTag::isOnTheFlyID( eType))
170 0 : const_cast<SvtLanguageTableImpl*>(this)->AddItem( sLangTag, eType);
171 :
172 0 : return sLangTag;
173 : }
174 :
175 547 : OUString SvtLanguageTable::GetLanguageString( const LanguageType eType )
176 : {
177 547 : return theLanguageTable::get().GetString( eType, false );
178 : }
179 :
180 0 : OUString SvtLanguageTable::GetLanguageString( const LanguageType eType, bool bUserInterfaceSelection )
181 : {
182 0 : return theLanguageTable::get().GetString( eType, bUserInterfaceSelection );
183 : }
184 :
185 :
186 :
187 8 : LanguageType SvtLanguageTableImpl::GetType( const OUString& rStr ) const
188 : {
189 8 : LanguageType eType = LANGUAGE_DONTKNOW;
190 8 : sal_uInt32 nCount = Count();
191 :
192 402 : for ( sal_uInt32 i = 0; i < nCount; ++i )
193 : {
194 402 : if (ResStringArray::GetString( i ).equals(rStr))
195 : {
196 8 : eType = LanguageType( GetValue( i ) );
197 8 : break;
198 : }
199 : }
200 8 : return eType;
201 : }
202 :
203 8 : LanguageType SvtLanguageTable::GetLanguageType( const OUString& rStr )
204 : {
205 8 : return theLanguageTable::get().GetType( rStr );
206 : }
207 :
208 :
209 :
210 0 : sal_uInt32 SvtLanguageTableImpl::GetEntryCount() const
211 : {
212 0 : return Count();
213 : }
214 :
215 0 : sal_uInt32 SvtLanguageTable::GetLanguageEntryCount()
216 : {
217 0 : return theLanguageTable::get().GetEntryCount();
218 : }
219 :
220 :
221 :
222 0 : LanguageType SvtLanguageTableImpl::GetTypeAtIndex( sal_uInt32 nIndex ) const
223 : {
224 0 : LanguageType nType = LANGUAGE_DONTKNOW;
225 0 : if (nIndex < Count())
226 0 : nType = LanguageType( GetValue( nIndex ) );
227 0 : return nType;
228 : }
229 :
230 0 : LanguageType SvtLanguageTable::GetLanguageTypeAtIndex( sal_uInt32 nIndex )
231 : {
232 0 : return theLanguageTable::get().GetTypeAtIndex( nIndex);
233 : }
234 :
235 :
236 0 : sal_uInt32 SvtLanguageTable::AddLanguageTag( const LanguageTag& rLanguageTag, const OUString& rString )
237 : {
238 0 : return theLanguageTable::get().AddItem( (rString.isEmpty() ? rLanguageTag.getBcp47() : rString),
239 0 : rLanguageTag.getLanguageType());
240 : }
241 :
242 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|