1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/i18n/DirectionProperty.hpp>
22 #include <i18nlangtag/lang.h>
23 #include <i18nlangtag/mslangid.hxx>
24 #include <i18nlangtag/languagetag.hxx>
26 #include <svtools/svtools.hrc>
27 #include <svtools/svtresid.hxx>
28 #include <svtools/langtab.hxx>
29 #include <unotools/syslocale.hxx>
30 #include <tools/resary.hxx>
33 using namespace ::com::sun::star
;
35 class SvtLanguageTableImpl
: public ResStringArray
39 SvtLanguageTableImpl();
40 virtual ~SvtLanguageTableImpl();
42 bool HasType( const LanguageType eType
) const;
43 const OUString
GetString( const LanguageType eType
, bool bUserInterfaceSelection
= false ) const;
44 LanguageType
GetType( const OUString
& rStr
) const;
45 sal_uInt32
GetEntryCount() const;
46 LanguageType
GetTypeAtIndex( sal_uInt32 nIndex
) const;
50 struct theLanguageTable
: public rtl::Static
< SvtLanguageTableImpl
, theLanguageTable
> {};
53 const OUString
ApplyLreOrRleEmbedding( const OUString
&rText
)
55 const sal_Int32 nLen
= rText
.getLength();
59 const sal_Unicode cLRE_Embedding
= 0x202A; // the start char of an LRE embedding
60 const sal_Unicode cRLE_Embedding
= 0x202B; // the start char of an RLE embedding
61 const sal_Unicode cPopDirectionalFormat
= 0x202C; // the unicode PDF (POP_DIRECTIONAL_FORMAT) char that terminates an LRE/RLE embedding
63 // check if there are alreay embedding characters at the strings start
64 // if so change nothing
65 const sal_Unicode cChar
= rText
[0];
66 if (cChar
== cLRE_Embedding
|| cChar
== cRLE_Embedding
)
69 // since we only call the function getCharacterDirection
70 // it does not matter which locale the CharClass is for.
71 // Thus we can readily make use of SvtSysLocale::GetCharClass()
72 // which should come at no cost...
73 SvtSysLocale aSysLocale
;
74 const CharClass
&rCharClass
= aSysLocale
.GetCharClass();
76 // we should look for the first non-neutral LTR or RTL character
77 // and use that to determine the embedding of the whole text...
78 // Thus we can avoid to check every character of the text.
80 bool bIsRtlText
= false;
81 for (sal_uInt16 i
= 0; i
< nLen
&& !bFound
; ++i
)
83 sal_Int16 nDirection
= rCharClass
.getCharacterDirection( rText
, i
);
86 case i18n::DirectionProperty_LEFT_TO_RIGHT
:
87 case i18n::DirectionProperty_LEFT_TO_RIGHT_EMBEDDING
:
88 case i18n::DirectionProperty_LEFT_TO_RIGHT_OVERRIDE
:
89 case i18n::DirectionProperty_EUROPEAN_NUMBER
:
90 case i18n::DirectionProperty_ARABIC_NUMBER
: // yes! arabic numbers are written from left to right
97 case i18n::DirectionProperty_RIGHT_TO_LEFT
:
98 case i18n::DirectionProperty_RIGHT_TO_LEFT_ARABIC
:
99 case i18n::DirectionProperty_RIGHT_TO_LEFT_EMBEDDING
:
100 case i18n::DirectionProperty_RIGHT_TO_LEFT_OVERRIDE
:
109 // nothing to be done, character is considered to be neutral we need to look further ...
114 sal_Unicode cStart
= cLRE_Embedding
; // default is to use LRE embedding characters
116 cStart
= cRLE_Embedding
; // then use RLE embedding
118 // add embedding start and end chars to the text if the direction could be determined
119 OUString
aRes( rText
);
122 aRes
= OUString(cStart
) + aRes
+ OUString(cPopDirectionalFormat
);
128 SvtLanguageTableImpl::SvtLanguageTableImpl() :
129 ResStringArray( SvtResId( STR_ARR_SVT_LANGUAGE_TABLE
) )
133 SvtLanguageTableImpl::~SvtLanguageTableImpl()
138 bool SvtLanguageTableImpl::HasType( const LanguageType eType
) const
140 LanguageType eLang
= MsLangId::getReplacementForObsoleteLanguage( eType
, false);
141 sal_uInt32 nPos
= FindIndex( eLang
);
143 return RESARRAY_INDEX_NOTFOUND
!= nPos
&& nPos
< Count();
146 bool SvtLanguageTable::HasLanguageType( const LanguageType eType
)
148 return theLanguageTable::get().HasType( eType
);
152 const OUString
SvtLanguageTableImpl::GetString( const LanguageType eType
, bool bUserInterfaceSelection
) const
154 LanguageType eLang
= MsLangId::getReplacementForObsoleteLanguage( eType
, bUserInterfaceSelection
);
155 sal_uInt32 nPos
= FindIndex( eLang
);
157 if ( RESARRAY_INDEX_NOTFOUND
!= nPos
&& nPos
< Count() )
158 return ResStringArray::GetString( nPos
);
160 //Rather than return a fairly useless "Unknown" name, return a geeky but usable-in-a-pinch lang-tag
161 OUString
sLangTag(LanguageTag::convertToBcp47(eType
));
162 SAL_WARN("svtools.misc", "Language: 0x"
164 << " with unknown name, so returning lang-tag of: "
167 // And add it to the table if it is an on-the-fly-id, which it usually is,
168 // so it is available in all subsequent language boxes.
169 if (LanguageTag::isOnTheFlyID( eType
))
170 const_cast<SvtLanguageTableImpl
*>(this)->AddItem( sLangTag
, eType
);
175 OUString
SvtLanguageTable::GetLanguageString( const LanguageType eType
)
177 return theLanguageTable::get().GetString( eType
, false );
180 OUString
SvtLanguageTable::GetLanguageString( const LanguageType eType
, bool bUserInterfaceSelection
)
182 return theLanguageTable::get().GetString( eType
, bUserInterfaceSelection
);
187 LanguageType
SvtLanguageTableImpl::GetType( const OUString
& rStr
) const
189 LanguageType eType
= LANGUAGE_DONTKNOW
;
190 sal_uInt32 nCount
= Count();
192 for ( sal_uInt32 i
= 0; i
< nCount
; ++i
)
194 if (ResStringArray::GetString( i
).equals(rStr
))
196 eType
= LanguageType( GetValue( i
) );
203 LanguageType
SvtLanguageTable::GetLanguageType( const OUString
& rStr
)
205 return theLanguageTable::get().GetType( rStr
);
210 sal_uInt32
SvtLanguageTableImpl::GetEntryCount() const
215 sal_uInt32
SvtLanguageTable::GetLanguageEntryCount()
217 return theLanguageTable::get().GetEntryCount();
222 LanguageType
SvtLanguageTableImpl::GetTypeAtIndex( sal_uInt32 nIndex
) const
224 LanguageType nType
= LANGUAGE_DONTKNOW
;
225 if (nIndex
< Count())
226 nType
= LanguageType( GetValue( nIndex
) );
230 LanguageType
SvtLanguageTable::GetLanguageTypeAtIndex( sal_uInt32 nIndex
)
232 return theLanguageTable::get().GetTypeAtIndex( nIndex
);
236 sal_uInt32
SvtLanguageTable::AddLanguageTag( const LanguageTag
& rLanguageTag
, const OUString
& rString
)
238 return theLanguageTable::get().AddItem( (rString
.isEmpty() ? rLanguageTag
.getBcp47() : rString
),
239 rLanguageTag
.getLanguageType());
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */