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