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