xref: /trunk/main/svtools/source/misc/langtab.cxx (revision 353566969a66957f00012e88c396ae18ac1cc883)
15900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
35900e8ecSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
45900e8ecSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
55900e8ecSAndrew Rist  * distributed with this work for additional information
65900e8ecSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
75900e8ecSAndrew Rist  * to you under the Apache License, Version 2.0 (the
85900e8ecSAndrew Rist  * "License"); you may not use this file except in compliance
95900e8ecSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
115900e8ecSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
135900e8ecSAndrew Rist  * Unless required by applicable law or agreed to in writing,
145900e8ecSAndrew Rist  * software distributed under the License is distributed on an
155900e8ecSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
165900e8ecSAndrew Rist  * KIND, either express or implied.  See the License for the
175900e8ecSAndrew Rist  * specific language governing permissions and limitations
185900e8ecSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
205900e8ecSAndrew Rist  *************************************************************/
215900e8ecSAndrew Rist 
22cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
23cdf0e10cSrcweir #include "precompiled_svtools.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // include ---------------------------------------------------------------
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <tools/shl.hxx>
28cdf0e10cSrcweir #include <tools/debug.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir //#include <com/sun/star/i18n/XCharacterClassification.hpp>
31cdf0e10cSrcweir #include <com/sun/star/i18n/DirectionProperty.hpp>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <i18npool/lang.h>
34cdf0e10cSrcweir #include <i18npool/mslangid.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <svtools/svtools.hrc>
37cdf0e10cSrcweir #include <svtools/svtdata.hxx>
38cdf0e10cSrcweir #include <svtools/langtab.hxx>
39cdf0e10cSrcweir #include <unotools/syslocale.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace ::com::sun::star;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir //------------------------------------------------------------------------
45cdf0e10cSrcweir 
ApplyLreOrRleEmbedding(const String & rText)46cdf0e10cSrcweir SVT_DLLPUBLIC const String ApplyLreOrRleEmbedding( const String &rText )
47cdf0e10cSrcweir {
48cdf0e10cSrcweir     const sal_uInt16 nLen = rText.Len();
49cdf0e10cSrcweir     if (nLen == 0)
50cdf0e10cSrcweir         return String();
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     const sal_Unicode cLRE_Embedding  = 0x202A;      // the start char of an LRE embedding
53cdf0e10cSrcweir     const sal_Unicode cRLE_Embedding  = 0x202B;      // the start char of an RLE embedding
54cdf0e10cSrcweir     const sal_Unicode cPopDirectionalFormat = 0x202C;   // the unicode PDF (POP_DIRECTIONAL_FORMAT) char that terminates an LRE/RLE embedding
55cdf0e10cSrcweir 
56*35356696Smseidel     // check if there are already embedding characters at the strings start
57cdf0e10cSrcweir     // if so change nothing
58cdf0e10cSrcweir     const sal_Unicode cChar = rText.GetBuffer()[0];
59cdf0e10cSrcweir     if (cChar == cLRE_Embedding || cChar == cRLE_Embedding)
60cdf0e10cSrcweir         return rText;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     // since we only call the function getCharacterDirection
63cdf0e10cSrcweir     // it does not matter which locale the CharClass is for.
64cdf0e10cSrcweir     // Thus we can readily make use of SvtSysLocale::GetCharClass()
65cdf0e10cSrcweir     // which should come at no cost...
66cdf0e10cSrcweir     SvtSysLocale aSysLocale;
67cdf0e10cSrcweir     const CharClass &rCharClass = aSysLocale.GetCharClass();
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     // we should look for the first non-neutral LTR or RTL character
70cdf0e10cSrcweir     // and use that to determine the embedding of the whole text...
71cdf0e10cSrcweir     // Thus we can avoid to check every character of the text.
72cdf0e10cSrcweir     bool bFound     = false;
73cdf0e10cSrcweir     bool bIsRtlText = false;
74cdf0e10cSrcweir     for (sal_uInt16 i = 0;  i < nLen && !bFound;  ++i)
75cdf0e10cSrcweir     {
76cdf0e10cSrcweir         sal_Int16 nDirection = rCharClass.getCharacterDirection( rText, i );
77cdf0e10cSrcweir         switch (nDirection)
78cdf0e10cSrcweir         {
79cdf0e10cSrcweir             case i18n::DirectionProperty_LEFT_TO_RIGHT :
80cdf0e10cSrcweir             case i18n::DirectionProperty_LEFT_TO_RIGHT_EMBEDDING :
81cdf0e10cSrcweir             case i18n::DirectionProperty_LEFT_TO_RIGHT_OVERRIDE :
82cdf0e10cSrcweir             case i18n::DirectionProperty_EUROPEAN_NUMBER :
83cdf0e10cSrcweir             case i18n::DirectionProperty_ARABIC_NUMBER :        // yes! arabic numbers are written from left to right
84cdf0e10cSrcweir             {
85cdf0e10cSrcweir                 bIsRtlText  = false;
86cdf0e10cSrcweir                 bFound      = true;
87cdf0e10cSrcweir                 break;
88cdf0e10cSrcweir             }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir             case i18n::DirectionProperty_RIGHT_TO_LEFT :
91cdf0e10cSrcweir             case i18n::DirectionProperty_RIGHT_TO_LEFT_ARABIC :
92cdf0e10cSrcweir             case i18n::DirectionProperty_RIGHT_TO_LEFT_EMBEDDING :
93cdf0e10cSrcweir             case i18n::DirectionProperty_RIGHT_TO_LEFT_OVERRIDE :
94cdf0e10cSrcweir             {
95cdf0e10cSrcweir                 bIsRtlText  = true;
96cdf0e10cSrcweir                 bFound      = true;
97cdf0e10cSrcweir                 break;
98cdf0e10cSrcweir             }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir             default:
101cdf0e10cSrcweir             {
102cdf0e10cSrcweir                 // nothing to be done, character is considered to be neutral we need to look further ...
103cdf0e10cSrcweir             }
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     sal_Unicode cStart  = cLRE_Embedding; // default is to use LRE embedding characters
108cdf0e10cSrcweir     if (bIsRtlText)
109cdf0e10cSrcweir         cStart = cRLE_Embedding; // then use RLE embedding
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     // add embedding start and end chars to the text if the direction could be determined
112cdf0e10cSrcweir     String aRes( rText );
113cdf0e10cSrcweir     if (bFound)
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         aRes.Insert( cStart, 0 );
116cdf0e10cSrcweir         aRes.Insert( cPopDirectionalFormat );
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     return aRes;
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir //------------------------------------------------------------------------
123cdf0e10cSrcweir 
SvtLanguageTable()124cdf0e10cSrcweir SvtLanguageTable::SvtLanguageTable() :
125cdf0e10cSrcweir     ResStringArray( SvtResId( STR_ARR_SVT_LANGUAGE_TABLE ) )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir //------------------------------------------------------------------------
130cdf0e10cSrcweir 
~SvtLanguageTable()131cdf0e10cSrcweir SvtLanguageTable::~SvtLanguageTable()
132cdf0e10cSrcweir {
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir //------------------------------------------------------------------------
136cdf0e10cSrcweir 
GetString(const LanguageType eType) const137cdf0e10cSrcweir const String& SvtLanguageTable::GetString( const LanguageType eType ) const
138cdf0e10cSrcweir {
139cdf0e10cSrcweir     LanguageType eLang = MsLangId::getReplacementForObsoleteLanguage( eType);
140cdf0e10cSrcweir     sal_uInt32 nPos = FindIndex( eLang );
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     if ( RESARRAY_INDEX_NOTFOUND != nPos && nPos < Count() )
143cdf0e10cSrcweir         return ResStringArray::GetString( nPos );
144cdf0e10cSrcweir     else
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         // If we knew what a simple "en" should alias to (en_US?) we could
147cdf0e10cSrcweir         // generally raise an error.
148cdf0e10cSrcweir         OSL_ENSURE(
149cdf0e10cSrcweir             eLang == LANGUAGE_ENGLISH, "language entry not found in resource" );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir         nPos = FindIndex( LANGUAGE_DONTKNOW );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir         if ( RESARRAY_INDEX_NOTFOUND != nPos && nPos < Count() )
154cdf0e10cSrcweir             return ResStringArray::GetString( nPos );
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir     static String aEmptyStr;
157cdf0e10cSrcweir     return aEmptyStr;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
GetLanguageString(const LanguageType eType)160cdf0e10cSrcweir String SvtLanguageTable::GetLanguageString( const LanguageType eType )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir     static const SvtLanguageTable aLangTable;
163cdf0e10cSrcweir     return aLangTable.GetString( eType );
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir //------------------------------------------------------------------------
167cdf0e10cSrcweir 
GetType(const String & rStr) const168cdf0e10cSrcweir LanguageType SvtLanguageTable::GetType( const String& rStr ) const
169cdf0e10cSrcweir {
170cdf0e10cSrcweir     LanguageType eType = LANGUAGE_DONTKNOW;
171cdf0e10cSrcweir     sal_uInt32 nCount = Count();
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     for ( sal_uInt32 i = 0; i < nCount; ++i )
174cdf0e10cSrcweir     {
175cdf0e10cSrcweir         if ( rStr == ResStringArray::GetString( i ) )
176cdf0e10cSrcweir         {
177cdf0e10cSrcweir             eType = LanguageType( GetValue( i ) );
178cdf0e10cSrcweir             break;
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir     return eType;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir //------------------------------------------------------------------------
185cdf0e10cSrcweir 
GetEntryCount() const186cdf0e10cSrcweir sal_uInt32 SvtLanguageTable::GetEntryCount() const
187cdf0e10cSrcweir {
188cdf0e10cSrcweir     return Count();
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir //------------------------------------------------------------------------
192cdf0e10cSrcweir 
GetTypeAtIndex(sal_uInt32 nIndex) const193cdf0e10cSrcweir LanguageType SvtLanguageTable::GetTypeAtIndex( sal_uInt32 nIndex ) const
194cdf0e10cSrcweir {
195cdf0e10cSrcweir     LanguageType nType = LANGUAGE_DONTKNOW;
196cdf0e10cSrcweir     if (nIndex < Count())
197cdf0e10cSrcweir         nType = LanguageType( GetValue( nIndex ) );
198cdf0e10cSrcweir     return nType;
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir //------------------------------------------------------------------------
202