1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <vcl/svapp.hxx>
22 #include <vcl/settings.hxx>
23 #include <vcl/mnemonic.hxx>
25 #include <vcl/unohelp.hxx>
26 #include <com/sun/star/i18n/XCharacterClassification.hpp>
27 #include <i18nlangtag/languagetag.hxx>
28 #include <i18nlangtag/mslangid.hxx>
29 #include <rtl/character.hxx>
30 #include <sal/log.hxx>
32 using namespace ::com::sun::star
;
34 MnemonicGenerator::MnemonicGenerator(sal_Unicode cMnemonic
)
35 : m_cMnemonic(cMnemonic
)
37 memset( maMnemonics
, 1, sizeof( maMnemonics
) );
40 MnemonicGenerator
& MnemonicGenerator::operator=(MnemonicGenerator
const &) = default; //MSVC2022 workaround
41 MnemonicGenerator::MnemonicGenerator(MnemonicGenerator
const&) = default; //MSVC2022 workaround
43 sal_uInt16
MnemonicGenerator::ImplGetMnemonicIndex( sal_Unicode c
)
45 static sal_uInt16
const aImplMnemonicRangeTab
[MNEMONIC_RANGES
*2] =
47 MNEMONIC_RANGE_1_START
, MNEMONIC_RANGE_1_END
,
48 MNEMONIC_RANGE_2_START
, MNEMONIC_RANGE_2_END
,
49 MNEMONIC_RANGE_3_START
, MNEMONIC_RANGE_3_END
,
50 MNEMONIC_RANGE_4_START
, MNEMONIC_RANGE_4_END
53 sal_uInt16 nMnemonicIndex
= 0;
54 for ( sal_uInt16 i
= 0; i
< MNEMONIC_RANGES
; i
++ )
56 if ( (c
>= aImplMnemonicRangeTab
[i
*2]) &&
57 (c
<= aImplMnemonicRangeTab
[i
*2+1]) )
58 return nMnemonicIndex
+c
-aImplMnemonicRangeTab
[i
*2];
60 nMnemonicIndex
+= aImplMnemonicRangeTab
[i
*2+1]-aImplMnemonicRangeTab
[i
*2];
63 return MNEMONIC_INDEX_NOTFOUND
;
66 sal_Unicode
MnemonicGenerator::ImplFindMnemonic( const OUString
& rKey
)
69 while ( (nIndex
= rKey
.indexOf( m_cMnemonic
, nIndex
)) != -1 )
71 if (nIndex
== rKey
.getLength() - 1) {
72 SAL_WARN("vcl", "key \"" << rKey
<< "\" ends in lone mnemonic prefix");
75 sal_Unicode cMnemonic
= rKey
[ nIndex
+1 ];
76 if ( cMnemonic
!= m_cMnemonic
)
84 void MnemonicGenerator::RegisterMnemonic( const OUString
& rKey
)
86 uno::Reference
< i18n::XCharacterClassification
> xCharClass
= GetCharClass();
88 // Don't crash even when we don't have access to i18n service
89 if ( !xCharClass
.is() )
92 OUString aKey
= xCharClass
->toLower(rKey
, 0, rKey
.getLength(), css::lang::Locale());
94 // If we find a Mnemonic, set the flag. In other case count the
95 // characters, because we need this to set most as possible
97 sal_Unicode cMnemonic
= ImplFindMnemonic( aKey
);
100 sal_uInt16 nMnemonicIndex
= ImplGetMnemonicIndex( cMnemonic
);
101 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
102 maMnemonics
[nMnemonicIndex
] = 0;
106 sal_Int32 nIndex
= 0;
107 sal_Int32 nLen
= aKey
.getLength();
108 while ( nIndex
< nLen
)
110 sal_Unicode c
= aKey
[ nIndex
];
112 sal_uInt16 nMnemonicIndex
= ImplGetMnemonicIndex( c
);
113 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
115 if ( maMnemonics
[nMnemonicIndex
] && (maMnemonics
[nMnemonicIndex
] < 0xFF) )
116 maMnemonics
[nMnemonicIndex
]++;
124 OUString
MnemonicGenerator::CreateMnemonic( const OUString
& _rKey
)
126 if ( _rKey
.isEmpty() || ImplFindMnemonic( _rKey
) )
129 uno::Reference
< i18n::XCharacterClassification
> xCharClass
= GetCharClass();
131 // Don't crash even when we don't have access to i18n service
132 if ( !xCharClass
.is() )
135 OUString aKey
= xCharClass
->toLower(_rKey
, 0, _rKey
.getLength(), css::lang::Locale());
137 bool bChanged
= false;
138 sal_Int32 nLen
= aKey
.getLength();
140 bool bCJK
= MsLangId::isCJK(Application::GetSettings().GetUILanguageTag().getLanguageType());
142 // #107889# in CJK versions ALL strings (even those that contain latin characters)
143 // will get mnemonics in the form: xyz (M)
144 // thus steps 1) and 2) are skipped for CJK locales
146 // #110720#, avoid CJK-style mnemonics for latin-only strings that do not contain useful mnemonic chars
149 bool bLatinOnly
= true;
150 bool bMnemonicIndexFound
= false;
154 for( nIndex
=0; nIndex
< nLen
; nIndex
++ )
157 if ( ((c
>= 0x3000) && (c
<= 0xD7FF)) || // cjk
158 ((c
>= 0xFF61) && (c
<= 0xFFDC)) ) // halfwidth forms
163 if( ImplGetMnemonicIndex( c
) != MNEMONIC_INDEX_NOTFOUND
)
164 bMnemonicIndexFound
= true;
166 if( bLatinOnly
&& !bMnemonicIndexFound
)
170 OUString
rKey(_rKey
);
172 sal_uInt16 nMnemonicIndex
;
174 sal_Int32 nIndex
= 0;
177 // 1) first try the first character of a word
184 if ( ((c
>= 0x3000) && (c
<= 0xD7FF)) || // cjk
185 ((c
>= 0xFF61) && (c
<= 0xFFDC)) ) // halfwidth forms
187 else if ( ((c
>= 0x0030) && (c
<= 0x0039)) || // digits
188 ((c
>= 0x0041) && (c
<= 0x005A)) || // latin capitals
189 ((c
>= 0x0061) && (c
<= 0x007A)) || // latin small
190 ((c
>= 0x0370) && (c
<= 0x037F)) || // greek numeral signs
191 ((c
>= 0x0400) && (c
<= 0x04FF)) ) // cyrillic
195 nMnemonicIndex
= ImplGetMnemonicIndex( c
);
196 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
198 if ( maMnemonics
[nMnemonicIndex
] )
200 maMnemonics
[nMnemonicIndex
] = 0;
201 rKey
= rKey
.replaceAt( nIndex
, 0, rtl::OUStringChar(m_cMnemonic
) );
207 // Search for next word
209 while ( nIndex
< nLen
)
218 while ( nIndex
< nLen
);
220 // 2) search for a unique/uncommon character
223 sal_uInt16 nBestCount
= 0xFFFF;
224 sal_uInt16 nBestMnemonicIndex
= 0;
225 sal_Int32 nBestIndex
= 0;
230 nMnemonicIndex
= ImplGetMnemonicIndex( c
);
231 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
233 if ( maMnemonics
[nMnemonicIndex
] )
235 if ( maMnemonics
[nMnemonicIndex
] < nBestCount
)
237 nBestCount
= maMnemonics
[nMnemonicIndex
];
239 nBestMnemonicIndex
= nMnemonicIndex
;
240 if ( nBestCount
== 2 )
248 while ( nIndex
< nLen
);
250 if ( nBestCount
!= 0xFFFF )
252 maMnemonics
[nBestMnemonicIndex
] = 0;
253 rKey
= rKey
.replaceAt( nBestIndex
, 0, rtl::OUStringChar(m_cMnemonic
) );
261 // 3) Add English Mnemonic for CJK Text
262 if ( !bChanged
&& (nCJK
== 1) && !rKey
.isEmpty() )
264 // Append Ascii Mnemonic
265 for ( c
= MNEMONIC_RANGE_2_START
; c
<= MNEMONIC_RANGE_2_END
; c
++ )
267 nMnemonicIndex
= ImplGetMnemonicIndex(c
);
268 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
270 if ( maMnemonics
[nMnemonicIndex
] )
272 maMnemonics
[nMnemonicIndex
] = 0;
273 OUString aStr
= OUString::Concat("(") + OUStringChar(m_cMnemonic
) +
274 OUStringChar(sal_Unicode(rtl::toAsciiUpperCase(c
))) +
276 nIndex
= rKey
.getLength();
279 if ( ( rKey
[nIndex
-2] == '>' && rKey
[nIndex
-1] == '>' ) ||
280 ( rKey
[nIndex
-2] == 0xFF1E && rKey
[nIndex
-1] == 0xFF1E ) )
285 if ( ( rKey
[nIndex
-3] == '.' && rKey
[nIndex
-2] == '.' && rKey
[nIndex
-1] == '.' ) ||
286 ( rKey
[nIndex
-3] == 0xFF0E && rKey
[nIndex
-2] == 0xFF0E && rKey
[nIndex
-1] == 0xFF0E ) )
291 sal_Unicode cLastChar
= rKey
[ nIndex
-1 ];
292 if ( (cLastChar
== ':') || (cLastChar
== 0xFF1A) ||
293 (cLastChar
== '.') || (cLastChar
== 0xFF0E) ||
294 (cLastChar
== '?') || (cLastChar
== 0xFF1F) ||
298 rKey
= rKey
.replaceAt( nIndex
, 0, aStr
);
308 uno::Reference
< i18n::XCharacterClassification
> const & MnemonicGenerator::GetCharClass()
310 if ( !mxCharClass
.is() )
311 mxCharClass
= vcl::unohelper::CreateCharacterClassification();
315 OUString
MnemonicGenerator::EraseAllMnemonicChars( const OUString
& rStr
)
317 OUString aStr
= rStr
;
318 sal_Int32 nLen
= aStr
.getLength();
323 if ( aStr
[ i
] == '~' )
325 // check for CJK-style mnemonic
326 if( i
> 0 && (i
+2) < nLen
)
328 sal_Unicode c
= sal_Unicode(rtl::toAsciiLowerCase(aStr
[i
+1]));
329 if( aStr
[ i
-1 ] == '(' &&
330 aStr
[ i
+2 ] == ')' &&
331 c
>= MNEMONIC_RANGE_2_START
&& c
<= MNEMONIC_RANGE_2_END
)
333 aStr
= aStr
.replaceAt( i
-1, 4, u
"" );
340 // remove standard mnemonics
341 aStr
= aStr
.replaceAt( i
, 1, u
"" );
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */