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