xref: /aoo42x/main/svtools/source/misc/langtab.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 
31 // include ---------------------------------------------------------------
32 
33 #include <tools/shl.hxx>
34 #include <tools/debug.hxx>
35 
36 //#include <com/sun/star/i18n/XCharacterClassification.hpp>
37 #include <com/sun/star/i18n/DirectionProperty.hpp>
38 
39 #include <i18npool/lang.h>
40 #include <i18npool/mslangid.hxx>
41 
42 #include <svtools/svtools.hrc>
43 #include <svtools/svtdata.hxx>
44 #include <svtools/langtab.hxx>
45 #include <unotools/syslocale.hxx>
46 
47 
48 using namespace ::com::sun::star;
49 
50 //------------------------------------------------------------------------
51 
52 SVT_DLLPUBLIC const String ApplyLreOrRleEmbedding( const String &rText )
53 {
54     const sal_uInt16 nLen = rText.Len();
55     if (nLen == 0)
56         return String();
57 
58     const sal_Unicode cLRE_Embedding  = 0x202A;      // the start char of an LRE embedding
59     const sal_Unicode cRLE_Embedding  = 0x202B;      // the start char of an RLE embedding
60     const sal_Unicode cPopDirectionalFormat = 0x202C;   // the unicode PDF (POP_DIRECTIONAL_FORMAT) char that terminates an LRE/RLE embedding
61 
62     // check if there are alreay embedding characters at the strings start
63     // if so change nothing
64     const sal_Unicode cChar = rText.GetBuffer()[0];
65     if (cChar == cLRE_Embedding || cChar == cRLE_Embedding)
66         return rText;
67 
68     // since we only call the function getCharacterDirection
69     // it does not matter which locale the CharClass is for.
70     // Thus we can readily make use of SvtSysLocale::GetCharClass()
71     // which should come at no cost...
72     SvtSysLocale aSysLocale;
73     const CharClass &rCharClass = aSysLocale.GetCharClass();
74 
75     // we should look for the first non-neutral LTR or RTL character
76     // and use that to determine the embedding of the whole text...
77     // Thus we can avoid to check every character of the text.
78     bool bFound     = false;
79     bool bIsRtlText = false;
80     for (sal_uInt16 i = 0;  i < nLen && !bFound;  ++i)
81     {
82         sal_Int16 nDirection = rCharClass.getCharacterDirection( rText, i );
83         switch (nDirection)
84         {
85             case i18n::DirectionProperty_LEFT_TO_RIGHT :
86             case i18n::DirectionProperty_LEFT_TO_RIGHT_EMBEDDING :
87             case i18n::DirectionProperty_LEFT_TO_RIGHT_OVERRIDE :
88             case i18n::DirectionProperty_EUROPEAN_NUMBER :
89             case i18n::DirectionProperty_ARABIC_NUMBER :        // yes! arabic numbers are written from left to right
90             {
91                 bIsRtlText  = false;
92                 bFound      = true;
93                 break;
94             }
95 
96             case i18n::DirectionProperty_RIGHT_TO_LEFT :
97             case i18n::DirectionProperty_RIGHT_TO_LEFT_ARABIC :
98             case i18n::DirectionProperty_RIGHT_TO_LEFT_EMBEDDING :
99             case i18n::DirectionProperty_RIGHT_TO_LEFT_OVERRIDE :
100             {
101                 bIsRtlText  = true;
102                 bFound      = true;
103                 break;
104             }
105 
106             default:
107             {
108                 // nothing to be done, character is considered to be neutral we need to look further ...
109             }
110         }
111     }
112 
113     sal_Unicode cStart  = cLRE_Embedding;   // default is to use LRE embedding characters
114     if (bIsRtlText)
115         cStart = cRLE_Embedding;            // then use RLE embedding
116 
117     // add embedding start and end chars to the text if the direction could be determined
118     String aRes( rText );
119     if (bFound)
120     {
121         aRes.Insert( cStart, 0 );
122         aRes.Insert( cPopDirectionalFormat );
123     }
124 
125     return aRes;
126 }
127 
128 //------------------------------------------------------------------------
129 
130 SvtLanguageTable::SvtLanguageTable() :
131     ResStringArray( SvtResId( STR_ARR_SVT_LANGUAGE_TABLE ) )
132 {
133 }
134 
135 //------------------------------------------------------------------------
136 
137 SvtLanguageTable::~SvtLanguageTable()
138 {
139 }
140 
141 //------------------------------------------------------------------------
142 
143 const String& SvtLanguageTable::GetString( const LanguageType eType ) const
144 {
145     LanguageType eLang = MsLangId::getReplacementForObsoleteLanguage( eType);
146     sal_uInt32 nPos = FindIndex( eLang );
147 
148 	if ( RESARRAY_INDEX_NOTFOUND != nPos && nPos < Count() )
149 		return ResStringArray::GetString( nPos );
150 	else
151 	{
152         // If we knew what a simple "en" should alias to (en_US?) we could
153         // generally raise an error.
154         OSL_ENSURE(
155             eLang == LANGUAGE_ENGLISH, "language entry not found in resource" );
156 
157         nPos = FindIndex( LANGUAGE_DONTKNOW );
158 
159 		if ( RESARRAY_INDEX_NOTFOUND != nPos && nPos < Count() )
160 			return ResStringArray::GetString( nPos );
161 	}
162     static String aEmptyStr;
163     return aEmptyStr;
164 }
165 
166 String SvtLanguageTable::GetLanguageString( const LanguageType eType )
167 {
168     static const SvtLanguageTable aLangTable;
169 	return aLangTable.GetString( eType );
170 }
171 
172 //------------------------------------------------------------------------
173 
174 LanguageType SvtLanguageTable::GetType( const String& rStr ) const
175 {
176 	LanguageType eType = LANGUAGE_DONTKNOW;
177 	sal_uInt32 nCount = Count();
178 
179 	for ( sal_uInt32 i = 0; i < nCount; ++i )
180     {
181 		if ( rStr == ResStringArray::GetString( i ) )
182 		{
183 			eType = LanguageType( GetValue( i ) );
184 			break;
185 		}
186     }
187 	return eType;
188 }
189 
190 //------------------------------------------------------------------------
191 
192 sal_uInt32 SvtLanguageTable::GetEntryCount() const
193 {
194 	return Count();
195 }
196 
197 //------------------------------------------------------------------------
198 
199 LanguageType SvtLanguageTable::GetTypeAtIndex( sal_uInt32 nIndex ) const
200 {
201 	LanguageType nType = LANGUAGE_DONTKNOW;
202 	if (nIndex < Count())
203 		nType = LanguageType( GetValue( nIndex ) );
204 	return nType;
205 }
206 
207 //------------------------------------------------------------------------
208 
209