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 .
20 #include <cclass_unicode.hxx>
21 #include <com/sun/star/i18n/UnicodeScript.hpp>
22 #include <com/sun/star/i18n/UnicodeType.hpp>
23 #include <com/sun/star/i18n/KCharacterType.hpp>
24 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
25 #include <unicode/uchar.h>
26 #include <comphelper/string.hxx>
27 #include <cppuhelper/exc_hlp.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <breakiteratorImpl.hxx>
30 #include <rtl/ref.hxx>
32 using namespace ::com::sun::star
;
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::i18n
;
35 using namespace ::com::sun::star::lang
;
39 // class cclass_Unicode
40 // ----------------------------------------------------;
42 cclass_Unicode::cclass_Unicode( const uno::Reference
< XComponentContext
>& rxContext
) :
43 trans( new Transliteration_casemapping() ),
44 m_xContext( rxContext
),
57 cclass_Unicode::~cclass_Unicode() {
63 cclass_Unicode::toUpper( const OUString
& Text
, sal_Int32 nPos
, sal_Int32 nCount
, const Locale
& rLocale
) {
64 sal_Int32 len
= Text
.getLength();
67 if (nCount
+ nPos
> len
)
70 trans
->setMappingType(MappingType::ToUpper
, rLocale
);
71 return trans
->transliterateString2String(Text
, nPos
, nCount
);
75 cclass_Unicode::toLower( const OUString
& Text
, sal_Int32 nPos
, sal_Int32 nCount
, const Locale
& rLocale
) {
76 sal_Int32 len
= Text
.getLength();
79 if (nCount
+ nPos
> len
)
82 trans
->setMappingType(MappingType::ToLower
, rLocale
);
83 return trans
->transliterateString2String(Text
, nPos
, nCount
);
87 cclass_Unicode::toTitle( const OUString
& Text
, sal_Int32 nPos
, sal_Int32 nCount
, const Locale
& rLocale
) {
90 sal_Int32 len
= Text
.getLength();
93 if (nCount
+ nPos
> len
)
96 trans
->setMappingType(MappingType::ToTitle
, rLocale
);
97 rtl_uString
* pStr
= rtl_uString_alloc(nCount
);
98 sal_Unicode
* out
= pStr
->buffer
;
99 rtl::Reference
< BreakIteratorImpl
> xBrk(new BreakIteratorImpl(m_xContext
));
100 Boundary bdy
= xBrk
->getWordBoundary(Text
, nPos
, rLocale
,
101 WordType::ANYWORD_IGNOREWHITESPACES
, true);
102 for (sal_Int32 i
= nPos
; i
< nCount
+ nPos
; i
++, out
++) {
104 bdy
= xBrk
->nextWord(Text
, bdy
.endPos
, rLocale
,
105 WordType::ANYWORD_IGNOREWHITESPACES
);
106 *out
= (i
== bdy
.startPos
) ?
107 trans
->transliterateChar2Char(Text
[i
]) : Text
[i
];
110 return OUString( pStr
, SAL_NO_ACQUIRE
);
112 catch (const RuntimeException
&)
116 catch (const Exception
& e
)
118 uno::Any
a(cppu::getCaughtException());
119 throw lang::WrappedTargetRuntimeException(
120 "wrapped " + a
.getValueTypeName() + ": " + e
.Message
,
121 uno::Reference
<uno::XInterface
>(), a
);
126 cclass_Unicode::getType( const OUString
& Text
, sal_Int32 nPos
) {
127 if ( nPos
< 0 || Text
.getLength() <= nPos
) return 0;
128 return static_cast<sal_Int16
>(u_charType(Text
.iterateCodePoints(&nPos
, 0)));
132 cclass_Unicode::getCharacterDirection( const OUString
& Text
, sal_Int32 nPos
) {
133 if ( nPos
< 0 || Text
.getLength() <= nPos
) return 0;
134 return static_cast<sal_Int16
>(u_charDirection(Text
.iterateCodePoints(&nPos
, 0)));
139 cclass_Unicode::getScript( const OUString
& Text
, sal_Int32 nPos
) {
140 if ( nPos
< 0 || Text
.getLength() <= nPos
) return 0;
141 // ICU Unicode script type UBlockCode starts from 1 for Basic Latin,
142 // while OO.o enum UnicideScript starts from 0.
143 // To map ICU UBlockCode to OO.o UnicodeScript, it needs to shift 1.
144 return static_cast<sal_Int16
>(ublock_getCode(Text
.iterateCodePoints(&nPos
, 0)))-1;
149 cclass_Unicode::getCharType( const OUString
& Text
, sal_Int32
* nPos
, sal_Int32 increment
) {
150 using namespace ::com::sun::star::i18n::KCharacterType
;
152 sal_uInt32 ch
= Text
.iterateCodePoints(nPos
, increment
);
153 switch ( u_charType(ch
) ) {
155 case U_UPPERCASE_LETTER
:
156 return UPPER
|LETTER
|PRINTABLE
|BASE_FORM
;
159 case U_LOWERCASE_LETTER
:
160 return LOWER
|LETTER
|PRINTABLE
|BASE_FORM
;
163 case U_TITLECASE_LETTER
:
164 return TITLE_CASE
|LETTER
|PRINTABLE
|BASE_FORM
;
167 case U_MODIFIER_LETTER
:
168 case U_OTHER_LETTER
:
169 return LETTER
|PRINTABLE
|BASE_FORM
;
172 case U_DECIMAL_DIGIT_NUMBER
:
173 case U_LETTER_NUMBER
:
175 return DIGIT
|PRINTABLE
|BASE_FORM
;
178 case U_NON_SPACING_MARK
:
179 case U_ENCLOSING_MARK
:
180 case U_COMBINING_SPACING_MARK
:
181 return BASE_FORM
|PRINTABLE
;
184 case U_SPACE_SEPARATOR
:
186 case U_DASH_PUNCTUATION
:
187 case U_INITIAL_PUNCTUATION
:
188 case U_FINAL_PUNCTUATION
:
189 case U_CONNECTOR_PUNCTUATION
:
190 case U_OTHER_PUNCTUATION
:
193 case U_CURRENCY_SYMBOL
:
194 case U_MODIFIER_SYMBOL
:
203 case U_LINE_SEPARATOR
:
204 case U_PARAGRAPH_SEPARATOR
:
205 return CONTROL
|PRINTABLE
;
209 return U_GENERAL_OTHER_TYPES
;
214 cclass_Unicode::getCharacterType( const OUString
& Text
, sal_Int32 nPos
, const Locale
& /*rLocale*/ ) {
215 if ( nPos
< 0 || Text
.getLength() <= nPos
) return 0;
216 return getCharType(Text
, &nPos
, 0);
221 cclass_Unicode::getStringType( const OUString
& Text
, sal_Int32 nPos
, sal_Int32 nCount
, const Locale
& /*rLocale*/ ) {
222 if ( nPos
< 0 || Text
.getLength() <= nPos
) return 0;
224 sal_Int32 result
= 0;
226 while (nCount
> 0 && nPos
< Text
.getLength())
228 sal_Int32 nOrigPos
= nPos
;
229 result
|= getCharType(Text
, &nPos
, 1);
230 sal_Int32 nUtf16Units
= nPos
- nOrigPos
;
231 nCount
-= nUtf16Units
;
237 ParseResult SAL_CALL
cclass_Unicode::parseAnyToken(
238 const OUString
& Text
,
240 const Locale
& rLocale
,
241 sal_Int32 startCharTokenType
,
242 const OUString
& userDefinedCharactersStart
,
243 sal_Int32 contCharTokenType
,
244 const OUString
& userDefinedCharactersCont
)
247 if ( Text
.getLength() <= nPos
)
250 setupParserTable( rLocale
,
251 startCharTokenType
, userDefinedCharactersStart
,
252 contCharTokenType
, userDefinedCharactersCont
);
253 parseText( r
, Text
, nPos
);
259 ParseResult SAL_CALL
cclass_Unicode::parsePredefinedToken(
260 sal_Int32 nTokenType
,
261 const OUString
& Text
,
263 const Locale
& rLocale
,
264 sal_Int32 startCharTokenType
,
265 const OUString
& userDefinedCharactersStart
,
266 sal_Int32 contCharTokenType
,
267 const OUString
& userDefinedCharactersCont
)
270 if ( Text
.getLength() <= nPos
)
273 setupParserTable( rLocale
,
274 startCharTokenType
, userDefinedCharactersStart
,
275 contCharTokenType
, userDefinedCharactersCont
);
276 parseText( r
, Text
, nPos
, nTokenType
);
281 OUString SAL_CALL
cclass_Unicode::getImplementationName()
283 return OUString("com.sun.star.i18n.CharacterClassification_Unicode");
286 sal_Bool SAL_CALL
cclass_Unicode::supportsService(const OUString
& rServiceName
)
288 return cppu::supportsService(this, rServiceName
);
291 Sequence
< OUString
> SAL_CALL
cclass_Unicode::getSupportedServiceNames()
293 Sequence
< OUString
> aRet
{ "com.sun.star.i18n.CharacterClassification_Unicode" };
299 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
300 com_sun_star_i18n_CharacterClassification_Unicode_get_implementation(
301 css::uno::XComponentContext
*context
,
302 css::uno::Sequence
<css::uno::Any
> const &)
304 return cppu::acquire(new i18npool::cclass_Unicode(context
));
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */