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