LibreOffice Module vcl (master)  1
mnemonic.cxx
Go to the documentation of this file.
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 <string.h>
21 #include <vcl/svapp.hxx>
22 #include <vcl/settings.hxx>
23 #include <vcl/mnemonic.hxx>
24 
25 #include <vcl/unohelp.hxx>
26 #include <com/sun/star/i18n/XCharacterClassification.hpp>
28 #include <i18nlangtag/mslangid.hxx>
29 #include <rtl/character.hxx>
30 #include <sal/log.hxx>
31 
32 using namespace ::com::sun::star;
33 
35  : m_cMnemonic(cMnemonic)
36 {
37  memset( maMnemonics, 1, sizeof( maMnemonics ) );
38 }
39 
41 {
42  static sal_uInt16 const aImplMnemonicRangeTab[MNEMONIC_RANGES*2] =
43  {
48  };
49 
50  sal_uInt16 nMnemonicIndex = 0;
51  for ( sal_uInt16 i = 0; i < MNEMONIC_RANGES; i++ )
52  {
53  if ( (c >= aImplMnemonicRangeTab[i*2]) &&
54  (c <= aImplMnemonicRangeTab[i*2+1]) )
55  return nMnemonicIndex+c-aImplMnemonicRangeTab[i*2];
56 
57  nMnemonicIndex += aImplMnemonicRangeTab[i*2+1]-aImplMnemonicRangeTab[i*2];
58  }
59 
61 }
62 
64 {
65  sal_Int32 nIndex = 0;
66  while ( (nIndex = rKey.indexOf( m_cMnemonic, nIndex )) != -1 )
67  {
68  if (nIndex == rKey.getLength() - 1) {
69  SAL_WARN("vcl", "key \"" << rKey << "\" ends in lone mnemonic prefix");
70  break;
71  }
72  sal_Unicode cMnemonic = rKey[ nIndex+1 ];
73  if ( cMnemonic != m_cMnemonic )
74  return cMnemonic;
75  nIndex += 2;
76  }
77 
78  return 0;
79 }
80 
81 void MnemonicGenerator::RegisterMnemonic( const OUString& rKey )
82 {
83  uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
84 
85  // Don't crash even when we don't have access to i18n service
86  if ( !xCharClass.is() )
87  return;
88 
89  OUString aKey = xCharClass->toLower(rKey, 0, rKey.getLength(), css::lang::Locale());
90 
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
93  // Mnemonics
94  sal_Unicode cMnemonic = ImplFindMnemonic( aKey );
95  if ( cMnemonic )
96  {
97  sal_uInt16 nMnemonicIndex = ImplGetMnemonicIndex( cMnemonic );
98  if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
99  maMnemonics[nMnemonicIndex] = 0;
100  }
101  else
102  {
103  sal_Int32 nIndex = 0;
104  sal_Int32 nLen = aKey.getLength();
105  while ( nIndex < nLen )
106  {
107  sal_Unicode c = aKey[ nIndex ];
108 
109  sal_uInt16 nMnemonicIndex = ImplGetMnemonicIndex( c );
110  if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
111  {
112  if ( maMnemonics[nMnemonicIndex] && (maMnemonics[nMnemonicIndex] < 0xFF) )
113  maMnemonics[nMnemonicIndex]++;
114  }
115 
116  nIndex++;
117  }
118  }
119 }
120 
121 OUString MnemonicGenerator::CreateMnemonic( const OUString& _rKey )
122 {
123  if ( _rKey.isEmpty() || ImplFindMnemonic( _rKey ) )
124  return _rKey;
125 
126  uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
127 
128  // Don't crash even when we don't have access to i18n service
129  if ( !xCharClass.is() )
130  return _rKey;
131 
132  OUString aKey = xCharClass->toLower(_rKey, 0, _rKey.getLength(), css::lang::Locale());
133 
134  bool bChanged = false;
135  sal_Int32 nLen = aKey.getLength();
136 
137  bool bCJK = MsLangId::isCJK(Application::GetSettings().GetUILanguageTag().getLanguageType());
138 
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
142 
143  // #110720#, avoid CJK-style mnemonics for latin-only strings that do not contain useful mnemonic chars
144  if( bCJK )
145  {
146  bool bLatinOnly = true;
147  bool bMnemonicIndexFound = false;
148  sal_Unicode c;
149  sal_Int32 nIndex;
150 
151  for( nIndex=0; nIndex < nLen; nIndex++ )
152  {
153  c = aKey[ nIndex ];
154  if ( ((c >= 0x3000) && (c <= 0xD7FF)) || // cjk
155  ((c >= 0xFF61) && (c <= 0xFFDC)) ) // halfwidth forms
156  {
157  bLatinOnly = false;
158  break;
159  }
161  bMnemonicIndexFound = true;
162  }
163  if( bLatinOnly && !bMnemonicIndexFound )
164  return _rKey;
165  }
166 
167  OUString rKey(_rKey);
168  int nCJK = 0;
169  sal_uInt16 nMnemonicIndex;
170  sal_Unicode c;
171  sal_Int32 nIndex = 0;
172  if( !bCJK )
173  {
174  // 1) first try the first character of a word
175  do
176  {
177  c = aKey[ nIndex ];
178 
179  if ( nCJK != 2 )
180  {
181  if ( ((c >= 0x3000) && (c <= 0xD7FF)) || // cjk
182  ((c >= 0xFF61) && (c <= 0xFFDC)) ) // halfwidth forms
183  nCJK = 1;
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
189  nCJK = 2;
190  }
191 
192  nMnemonicIndex = ImplGetMnemonicIndex( c );
193  if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
194  {
195  if ( maMnemonics[nMnemonicIndex] )
196  {
197  maMnemonics[nMnemonicIndex] = 0;
198  rKey = rKey.replaceAt( nIndex, 0, OUString(m_cMnemonic) );
199  bChanged = true;
200  break;
201  }
202  }
203 
204  // Search for next word
205  nIndex++;
206  while ( nIndex < nLen )
207  {
208  c = aKey[ nIndex ];
209  if ( c == ' ' )
210  break;
211  nIndex++;
212  }
213  nIndex++;
214  }
215  while ( nIndex < nLen );
216 
217  // 2) search for a unique/uncommon character
218  if ( !bChanged )
219  {
220  sal_uInt16 nBestCount = 0xFFFF;
221  sal_uInt16 nBestMnemonicIndex = 0;
222  sal_Int32 nBestIndex = 0;
223  nIndex = 0;
224  do
225  {
226  c = aKey[ nIndex ];
227  nMnemonicIndex = ImplGetMnemonicIndex( c );
228  if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
229  {
230  if ( maMnemonics[nMnemonicIndex] )
231  {
232  if ( maMnemonics[nMnemonicIndex] < nBestCount )
233  {
234  nBestCount = maMnemonics[nMnemonicIndex];
235  nBestIndex = nIndex;
236  nBestMnemonicIndex = nMnemonicIndex;
237  if ( nBestCount == 2 )
238  break;
239  }
240  }
241  }
242 
243  nIndex++;
244  }
245  while ( nIndex < nLen );
246 
247  if ( nBestCount != 0xFFFF )
248  {
249  maMnemonics[nBestMnemonicIndex] = 0;
250  rKey = rKey.replaceAt( nBestIndex, 0, OUString(m_cMnemonic) );
251  bChanged = true;
252  }
253  }
254  }
255  else
256  nCJK = 1;
257 
258  // 3) Add English Mnemonic for CJK Text
259  if ( !bChanged && (nCJK == 1) && !rKey.isEmpty() )
260  {
261  // Append Ascii Mnemonic
262  for ( c = MNEMONIC_RANGE_2_START; c <= MNEMONIC_RANGE_2_END; c++ )
263  {
264  nMnemonicIndex = ImplGetMnemonicIndex(c);
265  if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
266  {
267  if ( maMnemonics[nMnemonicIndex] )
268  {
269  maMnemonics[nMnemonicIndex] = 0;
270  OUString aStr = OUStringLiteral(u"(") + OUStringChar(m_cMnemonic) +
271  OUStringChar(sal_Unicode(rtl::toAsciiUpperCase(c))) +
272  ")";
273  nIndex = rKey.getLength();
274  if( nIndex >= 2 )
275  {
276  if ( ( rKey[nIndex-2] == '>' && rKey[nIndex-1] == '>' ) ||
277  ( rKey[nIndex-2] == 0xFF1E && rKey[nIndex-1] == 0xFF1E ) )
278  nIndex -= 2;
279  }
280  if( nIndex >= 3 )
281  {
282  if ( ( rKey[nIndex-3] == '.' && rKey[nIndex-2] == '.' && rKey[nIndex-1] == '.' ) ||
283  ( rKey[nIndex-3] == 0xFF0E && rKey[nIndex-2] == 0xFF0E && rKey[nIndex-1] == 0xFF0E ) )
284  nIndex -= 3;
285  }
286  if( nIndex >= 1)
287  {
288  sal_Unicode cLastChar = rKey[ nIndex-1 ];
289  if ( (cLastChar == ':') || (cLastChar == 0xFF1A) ||
290  (cLastChar == '.') || (cLastChar == 0xFF0E) ||
291  (cLastChar == '?') || (cLastChar == 0xFF1F) ||
292  (cLastChar == ' ') )
293  nIndex--;
294  }
295  rKey = rKey.replaceAt( nIndex, 0, aStr );
296  break;
297  }
298  }
299  }
300  }
301 
302  return rKey;
303 }
304 
305 uno::Reference< i18n::XCharacterClassification > const & MnemonicGenerator::GetCharClass()
306 {
307  if ( !mxCharClass.is() )
309  return mxCharClass;
310 }
311 
312 OUString MnemonicGenerator::EraseAllMnemonicChars( const OUString& rStr )
313 {
314  OUString aStr = rStr;
315  sal_Int32 nLen = aStr.getLength();
316  sal_Int32 i = 0;
317 
318  while ( i < nLen )
319  {
320  if ( aStr[ i ] == '~' )
321  {
322  // check for CJK-style mnemonic
323  if( i > 0 && (i+2) < nLen )
324  {
325  sal_Unicode c = sal_Unicode(rtl::toAsciiUpperCase(aStr[i+1]));
326  if( aStr[ i-1 ] == '(' &&
327  aStr[ i+2 ] == ')' &&
329  {
330  aStr = aStr.replaceAt( i-1, 4, "" );
331  nLen -= 4;
332  i--;
333  continue;
334  }
335  }
336 
337  // remove standard mnemonics
338  aStr = aStr.replaceAt( i, 1, "" );
339  nLen--;
340  }
341  else
342  i++;
343  }
344 
345  return aStr;
346 }
347 
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define MNEMONIC_RANGE_4_START
Definition: mnemonic.hxx:40
sal_Int32 nIndex
MnemonicGenerator(sal_Unicode cMnemonic=MNEMONIC_CHAR)
Definition: mnemonic.cxx:34
#define MNEMONIC_RANGE_3_START
Definition: mnemonic.hxx:37
static const AllSettings & GetSettings()
Gets the application's settings.
Definition: svapp.cxx:707
OUString CreateMnemonic(const OUString &rKey)
Definition: mnemonic.cxx:121
sal_Unicode m_cMnemonic
Definition: mnemonic.hxx:54
sal_uInt16 sal_Unicode
css::uno::Reference< css::i18n::XCharacterClassification > mxCharClass
Definition: mnemonic.hxx:57
static bool isCJK(LanguageType nLang)
css::uno::Reference< css::i18n::XCharacterClassification > const & GetCharClass()
Definition: mnemonic.cxx:305
#define MNEMONIC_RANGE_2_START
Definition: mnemonic.hxx:34
int i
float u
#define MNEMONIC_RANGES
Definition: mnemonic.hxx:42
sal_uInt8 maMnemonics[MAX_MNEMONICS]
Definition: mnemonic.hxx:56
#define MNEMONIC_RANGE_1_END
Definition: mnemonic.hxx:32
static SAL_DLLPRIVATE sal_uInt16 ImplGetMnemonicIndex(sal_Unicode c)
Definition: mnemonic.cxx:40
VCL_DLLPUBLIC css::uno::Reference< css::i18n::XCharacterClassification > CreateCharacterClassification()
Definition: unohelp.cxx:43
SAL_DLLPRIVATE sal_Unicode ImplFindMnemonic(const OUString &rKey)
Definition: mnemonic.cxx:63
#define SAL_WARN(area, stream)
#define MNEMONIC_RANGE_1_START
Definition: mnemonic.hxx:31
static OUString EraseAllMnemonicChars(const OUString &rStr)
Definition: mnemonic.cxx:312
#define MNEMONIC_RANGE_2_END
Definition: mnemonic.hxx:35
#define MNEMONIC_INDEX_NOTFOUND
Definition: mnemonic.hxx:49
void RegisterMnemonic(const OUString &rKey)
Definition: mnemonic.cxx:81
#define MNEMONIC_RANGE_4_END
Definition: mnemonic.hxx:41
aStr
#define MNEMONIC_RANGE_3_END
Definition: mnemonic.hxx:38