nss: upgrade to release 3.73
[LibreOffice.git] / i18npool / source / inputchecker / inputsequencechecker.cxx
blobf27ed5c9e2664c2502082a7dfa09c21a85c68d1b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <inputsequencechecker.hxx>
21 #include <com/sun/star/i18n/InputSequenceCheckMode.hpp>
22 #include <com/sun/star/uno/XComponentContext.hpp>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <i18nutil/unicode.hxx>
26 using namespace ::com::sun::star::uno;
27 using namespace ::com::sun::star::i18n;
28 using namespace ::com::sun::star::lang;
30 namespace i18npool {
32 InputSequenceCheckerImpl::InputSequenceCheckerImpl( const Reference < XComponentContext >& rxContext ) : m_xContext( rxContext )
34 serviceName = "com.sun.star.i18n.InputSequenceChecker";
35 cachedItem = nullptr;
38 InputSequenceCheckerImpl::InputSequenceCheckerImpl(const char *pServiceName)
39 : serviceName(pServiceName)
40 , cachedItem(nullptr)
44 InputSequenceCheckerImpl::~InputSequenceCheckerImpl()
48 sal_Bool SAL_CALL
49 InputSequenceCheckerImpl::checkInputSequence(const OUString& Text, sal_Int32 nStartPos,
50 sal_Unicode inputChar, sal_Int16 inputCheckMode)
52 if (inputCheckMode == InputSequenceCheckMode::PASSTHROUGH)
53 return true;
55 char* language = getLanguageByScripType(Text[nStartPos], inputChar);
57 if (language)
58 return getInputSequenceChecker(language)->checkInputSequence(Text, nStartPos, inputChar, inputCheckMode);
59 else
60 return true; // not a checkable languages.
63 sal_Int32 SAL_CALL
64 InputSequenceCheckerImpl::correctInputSequence(OUString& Text, sal_Int32 nStartPos,
65 sal_Unicode inputChar, sal_Int16 inputCheckMode)
67 if (inputCheckMode != InputSequenceCheckMode::PASSTHROUGH) {
68 char* language = getLanguageByScripType(Text[nStartPos], inputChar);
70 if (language)
71 return getInputSequenceChecker(language)->correctInputSequence(Text, nStartPos, inputChar, inputCheckMode);
73 Text = Text.replaceAt(++nStartPos, 0, OUString(inputChar));
74 return nStartPos;
77 ScriptTypeList const typeList[] = {
78 //{ UnicodeScript_kHebrew, UnicodeScript_kHebrew }, // 10,
79 //{ UnicodeScript_kArabic, UnicodeScript_kArabic }, // 11,
80 { UnicodeScript_kDevanagari, UnicodeScript_kDevanagari, sal_Int16(UnicodeScript_kDevanagari) }, // 14,
81 { UnicodeScript_kThai, UnicodeScript_kThai, sal_Int16(UnicodeScript_kThai) }, // 24,
83 { UnicodeScript_kScriptCount, UnicodeScript_kScriptCount, sal_Int16(UnicodeScript_kScriptCount) } // 88
86 char*
87 InputSequenceCheckerImpl::getLanguageByScripType(sal_Unicode cChar, sal_Unicode nChar)
89 css::i18n::UnicodeScript type = static_cast<css::i18n::UnicodeScript>(unicode::getUnicodeScriptType( cChar, typeList, sal_Int16(UnicodeScript_kScriptCount) ));
91 if (type != UnicodeScript_kScriptCount &&
92 type == static_cast<css::i18n::UnicodeScript>(unicode::getUnicodeScriptType( nChar, typeList, sal_Int16(UnicodeScript_kScriptCount) ))) {
93 switch(type) {
94 case UnicodeScript_kThai: return const_cast<char*>("th");
95 //case UnicodeScript_kArabic: return (char*)"ar";
96 //case UnicodeScript_kHebrew: return (char*)"he";
97 case UnicodeScript_kDevanagari: return const_cast<char*>("hi");
98 default: break;
101 return nullptr;
104 Reference< XExtendedInputSequenceChecker >&
105 InputSequenceCheckerImpl::getInputSequenceChecker(char const * rLanguage)
107 if (cachedItem && cachedItem->aLanguage == rLanguage) {
108 return cachedItem->xISC;
110 else {
111 for (const auto& l : lookupTable) {
112 cachedItem = l.get();
113 if (cachedItem->aLanguage == rLanguage)
114 return cachedItem->xISC;
117 Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
118 "com.sun.star.i18n.InputSequenceChecker_" +
119 OUString::createFromAscii(rLanguage),
120 m_xContext);
122 if ( xI.is() ) {
123 Reference< XExtendedInputSequenceChecker > xISC( xI, UNO_QUERY );
124 if (xISC.is()) {
125 lookupTable.emplace_back(new lookupTableItem(rLanguage, xISC));
126 cachedItem = lookupTable.back().get();
127 return cachedItem->xISC;
131 throw RuntimeException();
134 OUString SAL_CALL
135 InputSequenceCheckerImpl::getImplementationName()
137 return OUString::createFromAscii(serviceName);
140 sal_Bool SAL_CALL
141 InputSequenceCheckerImpl::supportsService(const OUString& rServiceName)
143 return cppu::supportsService(this, rServiceName);
146 Sequence< OUString > SAL_CALL
147 InputSequenceCheckerImpl::getSupportedServiceNames()
149 Sequence< OUString > aRet { OUString::createFromAscii(serviceName) };
150 return aRet;
155 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
156 com_sun_star_i18n_InputSequenceChecker_get_implementation(
157 css::uno::XComponentContext *context,
158 css::uno::Sequence<css::uno::Any> const &)
160 return cppu::acquire(new i18npool::InputSequenceCheckerImpl(context));
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */