Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "comphelper/processfactory.hxx"
22 : #include "unotools/localedatawrapper.hxx"
23 : #include "unotools/transliterationwrapper.hxx"
24 :
25 : #include "i18npool/languagetag.hxx"
26 :
27 : #include "rtl/ustrbuf.hxx"
28 :
29 : #include "vcl/i18nhelp.hxx"
30 :
31 : #include "com/sun/star/lang/XMultiServiceFactory.hpp"
32 : #include "com/sun/star/i18n/TransliterationModules.hpp"
33 :
34 : using namespace ::com::sun::star;
35 :
36 10 : vcl::I18nHelper::I18nHelper( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext, const LanguageTag& rLanguageTag )
37 : :
38 10 : maLanguageTag( rLanguageTag)
39 : {
40 10 : m_xContext = rxContext;
41 10 : mpLocaleDataWrapper = NULL;
42 10 : mpTransliterationWrapper= NULL;
43 10 : mbTransliterateIgnoreCase = sal_False;
44 10 : }
45 :
46 0 : vcl::I18nHelper::~I18nHelper()
47 : {
48 0 : ImplDestroyWrappers();
49 0 : }
50 :
51 0 : void vcl::I18nHelper::ImplDestroyWrappers()
52 : {
53 0 : delete mpLocaleDataWrapper;
54 0 : mpLocaleDataWrapper = NULL;
55 :
56 0 : delete mpTransliterationWrapper;
57 0 : mpTransliterationWrapper= NULL;
58 0 : }
59 :
60 0 : utl::TransliterationWrapper& vcl::I18nHelper::ImplGetTransliterationWrapper() const
61 : {
62 0 : if ( !mpTransliterationWrapper )
63 : {
64 0 : sal_Int32 nModules = i18n::TransliterationModules_IGNORE_WIDTH;
65 0 : if ( mbTransliterateIgnoreCase )
66 0 : nModules |= i18n::TransliterationModules_IGNORE_CASE;
67 :
68 0 : ((vcl::I18nHelper*)this)->mpTransliterationWrapper = new utl::TransliterationWrapper( m_xContext, (i18n::TransliterationModules)nModules );
69 0 : ((vcl::I18nHelper*)this)->mpTransliterationWrapper->loadModuleIfNeeded( maLanguageTag.getLanguageType() );
70 : }
71 0 : return *mpTransliterationWrapper;
72 : }
73 :
74 236 : LocaleDataWrapper& vcl::I18nHelper::ImplGetLocaleDataWrapper() const
75 : {
76 236 : if ( !mpLocaleDataWrapper )
77 : {
78 10 : ((vcl::I18nHelper*)this)->mpLocaleDataWrapper = new LocaleDataWrapper( m_xContext, maLanguageTag );
79 : }
80 236 : return *mpLocaleDataWrapper;
81 : }
82 :
83 0 : inline bool is_formatting_mark( sal_Unicode c )
84 : {
85 0 : if( (c >= 0x200B) && (c <= 0x200F) ) // BiDi and zero-width-markers
86 0 : return true;
87 0 : if( (c >= 0x2028) && (c <= 0x202E) ) // BiDi and paragraph-markers
88 0 : return true;
89 0 : return false;
90 : }
91 :
92 : /* #i100057# filter formatting marks out of strings before passing them to
93 : the transliteration. The real solution would have been an additional TransliterationModule
94 : to ignore these marks during transliteration; however changin the code in i18npool that actually
95 : implements this could produce unwanted side effects.
96 :
97 : Of course this copying around is not really good, but looking at i18npool, one more time
98 : will not hurt.
99 : */
100 0 : String vcl::I18nHelper::filterFormattingChars( const String& rStr )
101 : {
102 0 : sal_Int32 nUnicodes = rStr.Len();
103 0 : rtl::OUStringBuffer aBuf( nUnicodes );
104 0 : const sal_Unicode* pStr = rStr.GetBuffer();
105 0 : while( nUnicodes-- )
106 : {
107 0 : if( ! is_formatting_mark( *pStr ) )
108 0 : aBuf.append( *pStr );
109 0 : pStr++;
110 : }
111 0 : return aBuf.makeStringAndClear();
112 : }
113 :
114 0 : sal_Int32 vcl::I18nHelper::CompareString( const rtl::OUString& rStr1, const rtl::OUString& rStr2 ) const
115 : {
116 0 : ::osl::Guard< ::osl::Mutex > aGuard( ((vcl::I18nHelper*)this)->maMutex );
117 :
118 0 : if ( mbTransliterateIgnoreCase )
119 : {
120 : // Change mbTransliterateIgnoreCase and destroy the warpper, next call to
121 : // ImplGetTransliterationWrapper() will create a wrapper with the correct bIgnoreCase
122 0 : ((vcl::I18nHelper*)this)->mbTransliterateIgnoreCase = sal_False;
123 0 : delete ((vcl::I18nHelper*)this)->mpTransliterationWrapper;
124 0 : ((vcl::I18nHelper*)this)->mpTransliterationWrapper = NULL;
125 : }
126 :
127 :
128 0 : String aStr1( filterFormattingChars(rStr1) );
129 0 : String aStr2( filterFormattingChars(rStr2) );
130 0 : return ImplGetTransliterationWrapper().compareString( aStr1, aStr2 );
131 : }
132 :
133 0 : sal_Bool vcl::I18nHelper::MatchString( const rtl::OUString& rStr1, const rtl::OUString& rStr2 ) const
134 : {
135 0 : ::osl::Guard< ::osl::Mutex > aGuard( ((vcl::I18nHelper*)this)->maMutex );
136 :
137 0 : if ( !mbTransliterateIgnoreCase )
138 : {
139 : // Change mbTransliterateIgnoreCase and destroy the warpper, next call to
140 : // ImplGetTransliterationWrapper() will create a wrapper with the correct bIgnoreCase
141 0 : ((vcl::I18nHelper*)this)->mbTransliterateIgnoreCase = sal_True;
142 0 : delete ((vcl::I18nHelper*)this)->mpTransliterationWrapper;
143 0 : ((vcl::I18nHelper*)this)->mpTransliterationWrapper = NULL;
144 : }
145 :
146 0 : String aStr1( filterFormattingChars(rStr1) );
147 0 : String aStr2( filterFormattingChars(rStr2) );
148 0 : return ImplGetTransliterationWrapper().isMatch( aStr1, aStr2 );
149 : }
150 :
151 0 : sal_Bool vcl::I18nHelper::MatchMnemonic( const String& rString, sal_Unicode cMnemonicChar ) const
152 : {
153 0 : ::osl::Guard< ::osl::Mutex > aGuard( ((vcl::I18nHelper*)this)->maMutex );
154 :
155 0 : sal_Bool bEqual = sal_False;
156 0 : sal_uInt16 n = rString.Search( '~' );
157 0 : if ( n != STRING_NOTFOUND )
158 : {
159 0 : String aMatchStr( rString, n+1, STRING_LEN ); // not only one char, because of transliteration...
160 0 : bEqual = MatchString( rtl::OUString(cMnemonicChar), aMatchStr );
161 : }
162 0 : return bEqual;
163 : }
164 :
165 :
166 236 : String vcl::I18nHelper::GetNum( long nNumber, sal_uInt16 nDecimals, sal_Bool bUseThousandSep, sal_Bool bTrailingZeros ) const
167 : {
168 236 : return ImplGetLocaleDataWrapper().getNum( nNumber, nDecimals, bUseThousandSep, bTrailingZeros );
169 : }
170 :
171 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|