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