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