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 #ifndef INCLUDED_UNOTOOLS_TRANSLITERATIONWRAPPER_HXX
21 #define INCLUDED_UNOTOOLS_TRANSLITERATIONWRAPPER_HXX
22 
23 #include <unotools/unotoolsdllapi.h>
24 #include <rtl/ustring.hxx>
25 #include <i18nlangtag/languagetag.hxx>
26 #include <i18nlangtag/lang.h>
27 #include <com/sun/star/uno/Reference.hxx>
28 
29 namespace com::sun::star::uno { template <typename > class Sequence; }
30 namespace com::sun::star::i18n { class XExtendedTransliteration; }
31 namespace com::sun::star::uno { class XComponentContext; }
32 enum class TransliterationFlags;
33 
34 namespace utl
35 {
36 
37 class UNOTOOLS_DLLPUBLIC TransliterationWrapper
38 {
39     css::uno::Reference< css::i18n::XExtendedTransliteration > xTrans;
40     LanguageTag aLanguageTag;
41     TransliterationFlags nType;
42     mutable bool bFirstCall;
43 
44     TransliterationWrapper( const TransliterationWrapper& ) = delete;
45     TransliterationWrapper& operator=( const TransliterationWrapper& ) = delete;
46 
47     void loadModuleImpl() const;
48     void setLanguageLocaleImpl( LanguageType nLang );
49 
50 public:
51     TransliterationWrapper( const css::uno::Reference< css::uno::XComponentContext > & rxContext,
52                     TransliterationFlags nType );
53 
54     ~TransliterationWrapper();
55 
getType() const56     TransliterationFlags getType() const { return nType; }
57 
58     bool needLanguageForTheMode() const;
59 
60     /** set a new language and load the corresponding transliteration module if
61         needed for the mode set with nType in the ctor */
62     void loadModuleIfNeeded( LanguageType nLang );
63 
64     /** Load the transliteration module specified by rModuleName, which has to
65         be the UNO service implementation name that is expanded to the full UNO
66         service implementation name, for example, "NumToCharKanjiShort_ja_JP"
67         expands to
68         "com.sun.star.i18n.Transliteration.NumToCharKanjiShort_ja_JP".
69         @ATTENTION!
70         This method ignores the mode type set with the constructor and
71         interferes with the loadModuleIfNeeded() method and the transliterate()
72         method that gets a LanguageType passed as parameter.  Using one of
73         those may load a different module and overwrite this setting. Only the
74         transliterate() method that takes no LanguageType parameter may be used
75         for a specific module loaded with this method.  */
76     void loadModuleByImplName( const OUString& rModuleName, LanguageType nLang );
77 
78     /** This transliteration method corresponds with the loadModuleByImplName()
79         method. It relies on a module being loaded and does not try load one.
80         If for any reason the string can't be transliterated the original
81         string is returned.  */
82     OUString transliterate( const OUString& rStr,
83                         sal_Int32 nStart, sal_Int32 nLen ) const;
84 
85     // Wrapper implementations of class Transliteration
86     OUString transliterate( const OUString& rStr, LanguageType nLanguage,
87                         sal_Int32 nStart, sal_Int32 nLen,
88                         css::uno::Sequence <sal_Int32>* pOffset );
89 
90     /** If two strings are equal per this transliteration.
91         Returns the number of matched code points in any case, even if strings
92         are not equal, for example:
93         equals( "a", 0, 1, nMatch1, "aaa", 0, 3, nMatch2 )
94         returns false and nMatch:=1 and nMatch2:=1
95         equals( "aab", 0, 3, nMatch1, "aaa", 0, 3, nMatch2 )
96         returns false and nMatch:=2 and nMatch2:=2
97      */
98     bool equals(
99         const OUString& rStr1, sal_Int32 nPos1, sal_Int32 nCount1, sal_Int32& nMatch1,
100         const OUString& rStr2, sal_Int32 nPos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) const;
101 
102     sal_Int32 compareString( const OUString& rStr1, const OUString& rStr2 ) const;
103 
104     // helpers
105 
106     /** If two strings are really equal as per this translation, and not just
107         one string is matching the start of the other. Use this method instead
108         of compareString()==0 because it is much faster.
109      */
110     bool isEqual( const OUString& rStr1, const OUString& rStr2 ) const;
111 
112     /** If string rStr1 matches the start of string rStr2, i.e. "a" in "aaa"
113      */
114     bool isMatch( const OUString& rStr1, const OUString& rStr2 ) const;
115 
116 };
117 
118 }       // namespace utl
119 
120 #endif
121 
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
123