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 sal_uInt16
MnemonicGenerator::ImplGetMnemonicIndex( sal_Unicode c
)
42 static sal_uInt16
const aImplMnemonicRangeTab
[MNEMONIC_RANGES
*2] =
44 MNEMONIC_RANGE_1_START
, MNEMONIC_RANGE_1_END
,
45 MNEMONIC_RANGE_2_START
, MNEMONIC_RANGE_2_END
,
46 MNEMONIC_RANGE_3_START
, MNEMONIC_RANGE_3_END
,
47 MNEMONIC_RANGE_4_START
, MNEMONIC_RANGE_4_END
50 sal_uInt16 nMnemonicIndex
= 0;
51 for ( sal_uInt16 i
= 0; i
< MNEMONIC_RANGES
; i
++ )
53 if ( (c
>= aImplMnemonicRangeTab
[i
*2]) &&
54 (c
<= aImplMnemonicRangeTab
[i
*2+1]) )
55 return nMnemonicIndex
+c
-aImplMnemonicRangeTab
[i
*2];
57 nMnemonicIndex
+= aImplMnemonicRangeTab
[i
*2+1]-aImplMnemonicRangeTab
[i
*2];
60 return MNEMONIC_INDEX_NOTFOUND
;
63 sal_Unicode
MnemonicGenerator::ImplFindMnemonic( const OUString
& rKey
)
66 while ( (nIndex
= rKey
.indexOf( m_cMnemonic
, nIndex
)) != -1 )
68 if (nIndex
== rKey
.getLength() - 1) {
69 SAL_WARN("vcl", "key \"" << rKey
<< "\" ends in lone mnemonic prefix");
72 sal_Unicode cMnemonic
= rKey
[ nIndex
+1 ];
73 if ( cMnemonic
!= m_cMnemonic
)
81 void MnemonicGenerator::RegisterMnemonic( const OUString
& rKey
)
83 uno::Reference
< i18n::XCharacterClassification
> xCharClass
= GetCharClass();
85 // Don't crash even when we don't have access to i18n service
86 if ( !xCharClass
.is() )
89 OUString aKey
= xCharClass
->toLower(rKey
, 0, rKey
.getLength(), css::lang::Locale());
91 // If we find a Mnemonic, set the flag. In other case count the
92 // characters, because we need this to set most as possible
94 sal_Unicode cMnemonic
= ImplFindMnemonic( aKey
);
97 sal_uInt16 nMnemonicIndex
= ImplGetMnemonicIndex( cMnemonic
);
98 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
99 maMnemonics
[nMnemonicIndex
] = 0;
103 sal_Int32 nIndex
= 0;
104 sal_Int32 nLen
= aKey
.getLength();
105 while ( nIndex
< nLen
)
107 sal_Unicode c
= aKey
[ nIndex
];
109 sal_uInt16 nMnemonicIndex
= ImplGetMnemonicIndex( c
);
110 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
112 if ( maMnemonics
[nMnemonicIndex
] && (maMnemonics
[nMnemonicIndex
] < 0xFF) )
113 maMnemonics
[nMnemonicIndex
]++;
121 OUString
MnemonicGenerator::CreateMnemonic( const OUString
& _rKey
)
123 if ( _rKey
.isEmpty() || ImplFindMnemonic( _rKey
) )
126 uno::Reference
< i18n::XCharacterClassification
> xCharClass
= GetCharClass();
128 // Don't crash even when we don't have access to i18n service
129 if ( !xCharClass
.is() )
132 OUString aKey
= xCharClass
->toLower(_rKey
, 0, _rKey
.getLength(), css::lang::Locale());
134 bool bChanged
= false;
135 sal_Int32 nLen
= aKey
.getLength();
137 bool bCJK
= MsLangId::isCJK(Application::GetSettings().GetUILanguageTag().getLanguageType());
139 // #107889# in CJK versions ALL strings (even those that contain latin characters)
140 // will get mnemonics in the form: xyz (M)
141 // thus steps 1) and 2) are skipped for CJK locales
143 // #110720#, avoid CJK-style mnemonics for latin-only strings that do not contain useful mnemonic chars
146 bool bLatinOnly
= true;
147 bool bMnemonicIndexFound
= false;
151 for( nIndex
=0; nIndex
< nLen
; nIndex
++ )
154 if ( ((c
>= 0x3000) && (c
<= 0xD7FF)) || // cjk
155 ((c
>= 0xFF61) && (c
<= 0xFFDC)) ) // halfwidth forms
160 if( ImplGetMnemonicIndex( c
) != MNEMONIC_INDEX_NOTFOUND
)
161 bMnemonicIndexFound
= true;
163 if( bLatinOnly
&& !bMnemonicIndexFound
)
167 OUString
rKey(_rKey
);
169 sal_uInt16 nMnemonicIndex
;
171 sal_Int32 nIndex
= 0;
174 // 1) first try the first character of a word
181 if ( ((c
>= 0x3000) && (c
<= 0xD7FF)) || // cjk
182 ((c
>= 0xFF61) && (c
<= 0xFFDC)) ) // halfwidth forms
184 else if ( ((c
>= 0x0030) && (c
<= 0x0039)) || // digits
185 ((c
>= 0x0041) && (c
<= 0x005A)) || // latin capitals
186 ((c
>= 0x0061) && (c
<= 0x007A)) || // latin small
187 ((c
>= 0x0370) && (c
<= 0x037F)) || // greek numeral signs
188 ((c
>= 0x0400) && (c
<= 0x04FF)) ) // cyrillic
192 nMnemonicIndex
= ImplGetMnemonicIndex( c
);
193 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
195 if ( maMnemonics
[nMnemonicIndex
] )
197 maMnemonics
[nMnemonicIndex
] = 0;
198 rKey
= rKey
.replaceAt( nIndex
, 0, OUString(m_cMnemonic
) );
204 // Search for next word
206 while ( nIndex
< nLen
)
215 while ( nIndex
< nLen
);
217 // 2) search for a unique/uncommon character
220 sal_uInt16 nBestCount
= 0xFFFF;
221 sal_uInt16 nBestMnemonicIndex
= 0;
222 sal_Int32 nBestIndex
= 0;
227 nMnemonicIndex
= ImplGetMnemonicIndex( c
);
228 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
230 if ( maMnemonics
[nMnemonicIndex
] )
232 if ( maMnemonics
[nMnemonicIndex
] < nBestCount
)
234 nBestCount
= maMnemonics
[nMnemonicIndex
];
236 nBestMnemonicIndex
= nMnemonicIndex
;
237 if ( nBestCount
== 2 )
245 while ( nIndex
< nLen
);
247 if ( nBestCount
!= 0xFFFF )
249 maMnemonics
[nBestMnemonicIndex
] = 0;
250 rKey
= rKey
.replaceAt( nBestIndex
, 0, OUString(m_cMnemonic
) );
258 // 3) Add English Mnemonic for CJK Text
259 if ( !bChanged
&& (nCJK
== 1) && !rKey
.isEmpty() )
261 // Append Ascii Mnemonic
262 for ( c
= MNEMONIC_RANGE_2_START
; c
<= MNEMONIC_RANGE_2_END
; c
++ )
264 nMnemonicIndex
= ImplGetMnemonicIndex(c
);
265 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
267 if ( maMnemonics
[nMnemonicIndex
] )
269 maMnemonics
[nMnemonicIndex
] = 0;
270 OUString aStr
= OUStringLiteral(u
"(") + OUStringChar(m_cMnemonic
) +
271 OUStringChar(sal_Unicode(rtl::toAsciiUpperCase(c
))) +
273 nIndex
= rKey
.getLength();
276 if ( ( rKey
[nIndex
-2] == '>' && rKey
[nIndex
-1] == '>' ) ||
277 ( rKey
[nIndex
-2] == 0xFF1E && rKey
[nIndex
-1] == 0xFF1E ) )
282 if ( ( rKey
[nIndex
-3] == '.' && rKey
[nIndex
-2] == '.' && rKey
[nIndex
-1] == '.' ) ||
283 ( rKey
[nIndex
-3] == 0xFF0E && rKey
[nIndex
-2] == 0xFF0E && rKey
[nIndex
-1] == 0xFF0E ) )
288 sal_Unicode cLastChar
= rKey
[ nIndex
-1 ];
289 if ( (cLastChar
== ':') || (cLastChar
== 0xFF1A) ||
290 (cLastChar
== '.') || (cLastChar
== 0xFF0E) ||
291 (cLastChar
== '?') || (cLastChar
== 0xFF1F) ||
295 rKey
= rKey
.replaceAt( nIndex
, 0, aStr
);
305 uno::Reference
< i18n::XCharacterClassification
> const & MnemonicGenerator::GetCharClass()
307 if ( !mxCharClass
.is() )
308 mxCharClass
= vcl::unohelper::CreateCharacterClassification();
312 OUString
MnemonicGenerator::EraseAllMnemonicChars( const OUString
& rStr
)
314 OUString aStr
= rStr
;
315 sal_Int32 nLen
= aStr
.getLength();
320 if ( aStr
[ i
] == '~' )
322 // check for CJK-style mnemonic
323 if( i
> 0 && (i
+2) < nLen
)
325 sal_Unicode c
= sal_Unicode(rtl::toAsciiUpperCase(aStr
[i
+1]));
326 if( aStr
[ i
-1 ] == '(' &&
327 aStr
[ i
+2 ] == ')' &&
328 c
>= MNEMONIC_RANGE_2_START
&& c
<= MNEMONIC_RANGE_2_END
)
330 aStr
= aStr
.replaceAt( i
-1, 4, "" );
337 // remove standard mnemonics
338 aStr
= aStr
.replaceAt( i
, 1, "" );
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */