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 226 : vcl::I18nHelper::I18nHelper( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext, const LanguageTag& rLanguageTag )
36 : :
37 226 : maLanguageTag( rLanguageTag)
38 : {
39 226 : m_xContext = rxContext;
40 226 : mpLocaleDataWrapper = NULL;
41 226 : mpTransliterationWrapper= NULL;
42 226 : mbTransliterateIgnoreCase = false;
43 226 : }
44 :
45 372 : vcl::I18nHelper::~I18nHelper()
46 : {
47 186 : ImplDestroyWrappers();
48 186 : }
49 :
50 186 : void vcl::I18nHelper::ImplDestroyWrappers()
51 : {
52 186 : delete mpLocaleDataWrapper;
53 186 : mpLocaleDataWrapper = NULL;
54 :
55 186 : delete mpTransliterationWrapper;
56 186 : mpTransliterationWrapper= NULL;
57 186 : }
58 :
59 32862 : utl::TransliterationWrapper& vcl::I18nHelper::ImplGetTransliterationWrapper() const
60 : {
61 32862 : if ( !mpTransliterationWrapper )
62 : {
63 100 : sal_Int32 nModules = i18n::TransliterationModules_IGNORE_WIDTH;
64 100 : if ( mbTransliterateIgnoreCase )
65 100 : nModules |= i18n::TransliterationModules_IGNORE_CASE;
66 :
67 100 : ((vcl::I18nHelper*)this)->mpTransliterationWrapper = new utl::TransliterationWrapper( m_xContext, (i18n::TransliterationModules)nModules );
68 100 : ((vcl::I18nHelper*)this)->mpTransliterationWrapper->loadModuleIfNeeded( maLanguageTag.getLanguageType() );
69 : }
70 32862 : return *mpTransliterationWrapper;
71 : }
72 :
73 4706 : LocaleDataWrapper& vcl::I18nHelper::ImplGetLocaleDataWrapper() const
74 : {
75 4706 : if ( !mpLocaleDataWrapper )
76 : {
77 88 : ((vcl::I18nHelper*)this)->mpLocaleDataWrapper = new LocaleDataWrapper( m_xContext, maLanguageTag );
78 : }
79 4706 : return *mpLocaleDataWrapper;
80 : }
81 :
82 1070228 : inline bool is_formatting_mark( sal_Unicode c )
83 : {
84 1070228 : if( (c >= 0x200B) && (c <= 0x200F) ) // BiDi and zero-width-markers
85 0 : return true;
86 1070228 : if( (c >= 0x2028) && (c <= 0x202E) ) // BiDi and paragraph-markers
87 0 : return true;
88 1070228 : 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 251668 : OUString vcl::I18nHelper::filterFormattingChars( const OUString& rStr )
100 : {
101 251668 : sal_Int32 nUnicodes = rStr.getLength();
102 251668 : OUStringBuffer aBuf( nUnicodes );
103 251668 : const sal_Unicode* pStr = rStr.getStr();
104 1573564 : while( nUnicodes-- )
105 : {
106 1070228 : if( ! is_formatting_mark( *pStr ) )
107 1070228 : aBuf.append( *pStr );
108 1070228 : pStr++;
109 : }
110 251668 : 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 32862 : bool vcl::I18nHelper::MatchString( const OUString& rStr1, const OUString& rStr2 ) const
132 : {
133 32862 : ::osl::Guard< ::osl::Mutex > aGuard( ((vcl::I18nHelper*)this)->maMutex );
134 :
135 32862 : if ( !mbTransliterateIgnoreCase )
136 : {
137 : // Change mbTransliterateIgnoreCase and destroy the warpper, next call to
138 : // ImplGetTransliterationWrapper() will create a wrapper with the correct bIgnoreCase
139 100 : ((vcl::I18nHelper*)this)->mbTransliterateIgnoreCase = true;
140 100 : delete ((vcl::I18nHelper*)this)->mpTransliterationWrapper;
141 100 : ((vcl::I18nHelper*)this)->mpTransliterationWrapper = NULL;
142 : }
143 :
144 65724 : OUString aStr1( filterFormattingChars(rStr1) );
145 65724 : OUString aStr2( filterFormattingChars(rStr2) );
146 65724 : 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 4706 : OUString vcl::I18nHelper::GetNum( long nNumber, sal_uInt16 nDecimals, bool bUseThousandSep, bool bTrailingZeros ) const
164 : {
165 4706 : return ImplGetLocaleDataWrapper().getNum( nNumber, nDecimals, bUseThousandSep, bTrailingZeros );
166 : }
167 :
168 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|