LibreOffice Module i18npool (master)  1
inputsequencechecker.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 <inputsequencechecker.hxx>
21 #include <com/sun/star/i18n/InputSequenceCheckMode.hpp>
22 #include <com/sun/star/uno/XComponentContext.hpp>
24 #include <i18nutil/unicode.hxx>
25 
26 using namespace ::com::sun::star::uno;
27 using namespace ::com::sun::star::i18n;
28 using namespace ::com::sun::star::lang;
29 
30 namespace i18npool {
31 
33 {
34  serviceName = "com.sun.star.i18n.InputSequenceChecker";
35 }
36 
37 InputSequenceCheckerImpl::InputSequenceCheckerImpl(const char *pServiceName)
38  : serviceName(pServiceName)
39 {
40 }
41 
43 {
44 }
45 
46 sal_Bool SAL_CALL
47 InputSequenceCheckerImpl::checkInputSequence(const OUString& Text, sal_Int32 nStartPos,
48  sal_Unicode inputChar, sal_Int16 inputCheckMode)
49 {
50  if (inputCheckMode == InputSequenceCheckMode::PASSTHROUGH)
51  return true;
52 
53  char* language = getLanguageByScripType(Text[nStartPos], inputChar);
54 
55  if (language)
56  return getInputSequenceChecker(language)->checkInputSequence(Text, nStartPos, inputChar, inputCheckMode);
57  else
58  return true; // not a checkable languages.
59 }
60 
61 sal_Int32 SAL_CALL
62 InputSequenceCheckerImpl::correctInputSequence(OUString& Text, sal_Int32 nStartPos,
63  sal_Unicode inputChar, sal_Int16 inputCheckMode)
64 {
65  if (inputCheckMode != InputSequenceCheckMode::PASSTHROUGH) {
66  char* language = getLanguageByScripType(Text[nStartPos], inputChar);
67 
68  if (language)
69  return getInputSequenceChecker(language)->correctInputSequence(Text, nStartPos, inputChar, inputCheckMode);
70  }
71  Text = Text.replaceAt(++nStartPos, 0, OUString(inputChar));
72  return nStartPos;
73 }
74 
76  //{ UnicodeScript_kHebrew, UnicodeScript_kHebrew }, // 10,
77  //{ UnicodeScript_kArabic, UnicodeScript_kArabic }, // 11,
78  { UnicodeScript_kDevanagari, UnicodeScript_kDevanagari, sal_Int16(UnicodeScript_kDevanagari) }, // 14,
79  { UnicodeScript_kThai, UnicodeScript_kThai, sal_Int16(UnicodeScript_kThai) }, // 24,
80 
81  { UnicodeScript_kScriptCount, UnicodeScript_kScriptCount, sal_Int16(UnicodeScript_kScriptCount) } // 88
82 };
83 
84 char*
86 {
87  css::i18n::UnicodeScript type = static_cast<css::i18n::UnicodeScript>(unicode::getUnicodeScriptType( cChar, typeList, sal_Int16(UnicodeScript_kScriptCount) ));
88 
89  if (type != UnicodeScript_kScriptCount &&
90  type == static_cast<css::i18n::UnicodeScript>(unicode::getUnicodeScriptType( nChar, typeList, sal_Int16(UnicodeScript_kScriptCount) ))) {
91  switch(type) {
92  case UnicodeScript_kThai: return const_cast<char*>("th");
93  //case UnicodeScript_kArabic: return (char*)"ar";
94  //case UnicodeScript_kHebrew: return (char*)"he";
95  case UnicodeScript_kDevanagari: return const_cast<char*>("hi");
96  default: break;
97  }
98  }
99  return nullptr;
100 }
101 
102 Reference< XExtendedInputSequenceChecker >&
104 {
105  if (cachedItem && cachedItem->aLanguage == rLanguage) {
106  return cachedItem->xISC;
107  }
108  else {
109  for (const auto& l : lookupTable) {
110  cachedItem = l;
111  if (cachedItem->aLanguage == rLanguage)
112  return cachedItem->xISC;
113  }
114 
115  Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
116  "com.sun.star.i18n.InputSequenceChecker_" +
117  OUString::createFromAscii(rLanguage),
118  m_xContext);
119 
120  if ( xI.is() ) {
121  Reference< XExtendedInputSequenceChecker > xISC( xI, UNO_QUERY );
122  if (xISC.is()) {
123  lookupTable.emplace_back(rLanguage, xISC);
124  cachedItem = lookupTable.back();
125  return cachedItem->xISC;
126  }
127  }
128  }
129  throw RuntimeException();
130 }
131 
132 OUString SAL_CALL
134 {
135  return OUString::createFromAscii(serviceName);
136 }
137 
138 sal_Bool SAL_CALL
139 InputSequenceCheckerImpl::supportsService(const OUString& rServiceName)
140 {
141  return cppu::supportsService(this, rServiceName);
142 }
143 
144 Sequence< OUString > SAL_CALL
146 {
147  Sequence< OUString > aRet { OUString::createFromAscii(serviceName) };
148  return aRet;
149 }
150 
151 }
152 
153 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
155  css::uno::XComponentContext *context,
156  css::uno::Sequence<css::uno::Any> const &)
157 {
158  return cppu::acquire(new i18npool::InputSequenceCheckerImpl(context));
159 }
160 
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_i18n_InputSequenceChecker_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
static sal_Int16 getUnicodeScriptType(const sal_Unicode ch, const ScriptTypeList *typeList, sal_Int16 unknownType=0)
css::uno::Reference< css::uno::XComponentContext > m_xContext
std::optional< lookupTableItem > cachedItem
virtual sal_Bool SAL_CALL checkInputSequence(const OUString &Text, sal_Int32 nStartPos, sal_Unicode inputChar, sal_Int16 inputCheckMode) override
sal_uInt16 sal_Unicode
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
std::vector< lookupTableItem > lookupTable
unsigned char sal_Bool
css::uno::Reference< css::i18n::XExtendedInputSequenceChecker > & getInputSequenceChecker(char const *rLanguage)
Constant values shared between i18npool and, for example, the number formatter.
virtual OUString SAL_CALL getImplementationName() override
InputSequenceCheckerImpl(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
static char * getLanguageByScripType(sal_Unicode cChar, sal_Unicode nChar)
virtual sal_Int32 SAL_CALL correctInputSequence(OUString &Text, sal_Int32 nStartPos, sal_Unicode inputChar, sal_Int16 inputCheckMode) override
ResultType type
Reference< XComponentContext > m_xContext
ScriptTypeList const typeList[]