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/KCharacterType.hpp>
22 #include <com/sun/star/i18n/WordType.hpp>
23 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
24 #include <unicode/uchar.h>
25 #include <cppuhelper/exc_hlp.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <breakiteratorImpl.hxx>
28 #include <transliteration_body.hxx>
29 #include <rtl/ref.hxx>
31 using namespace ::com::sun::star
;
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::i18n
;
34 using namespace ::com::sun::star::lang
;
38 // class cclass_Unicode
39 // ----------------------------------------------------;
41 cclass_Unicode::cclass_Unicode( const uno::Reference
< XComponentContext
>& rxContext
) :
42 trans( new Transliteration_casemapping() ),
43 m_xContext( rxContext
),
53 cclass_Unicode::~cclass_Unicode() {
59 cclass_Unicode::toUpper( const OUString
& Text
, sal_Int32 nPos
, sal_Int32 nCount
, const Locale
& rLocale
) {
60 sal_Int32 len
= Text
.getLength();
63 if (nCount
+ nPos
> len
)
66 trans
->setMappingType(MappingType::ToUpper
, rLocale
);
67 return trans
->transliterateString2String(Text
, nPos
, nCount
);
71 cclass_Unicode::toLower( const OUString
& Text
, sal_Int32 nPos
, sal_Int32 nCount
, const Locale
& rLocale
) {
72 sal_Int32 len
= Text
.getLength();
75 if (nCount
+ nPos
> len
)
78 trans
->setMappingType(MappingType::ToLower
, rLocale
);
79 return trans
->transliterateString2String(Text
, nPos
, nCount
);
83 cclass_Unicode::toTitle( const OUString
& Text
, sal_Int32 nPos
, sal_Int32 nCount
, const Locale
& rLocale
) {
86 sal_Int32 len
= Text
.getLength();
89 if (nCount
+ nPos
> len
)
92 trans
->setMappingType(MappingType::ToTitle
, rLocale
);
93 rtl_uString
* pStr
= rtl_uString_alloc(nCount
);
94 sal_Unicode
* out
= pStr
->buffer
;
95 rtl::Reference
< BreakIteratorImpl
> xBrk(new BreakIteratorImpl(m_xContext
));
96 Boundary bdy
= xBrk
->getWordBoundary(Text
, nPos
, rLocale
,
97 WordType::ANYWORD_IGNOREWHITESPACES
, true);
98 for (sal_Int32 i
= nPos
; i
< nCount
+ nPos
; i
++, out
++) {
100 bdy
= xBrk
->nextWord(Text
, bdy
.endPos
, rLocale
,
101 WordType::ANYWORD_IGNOREWHITESPACES
);
102 *out
= (i
== bdy
.startPos
) ?
103 trans
->transliterateChar2Char(Text
[i
]) : Text
[i
];
106 return OUString( pStr
, SAL_NO_ACQUIRE
);
108 catch (const RuntimeException
&)
112 catch (const Exception
& e
)
114 uno::Any
a(cppu::getCaughtException());
115 throw lang::WrappedTargetRuntimeException(
116 "wrapped " + a
.getValueTypeName() + ": " + e
.Message
,
117 uno::Reference
<uno::XInterface
>(), a
);
122 cclass_Unicode::getType( const OUString
& Text
, sal_Int32 nPos
) {
123 if ( nPos
< 0 || Text
.getLength() <= nPos
) return 0;
124 return static_cast<sal_Int16
>(u_charType(Text
.iterateCodePoints(&nPos
, 0)));
128 cclass_Unicode::getCharacterDirection( const OUString
& Text
, sal_Int32 nPos
) {
129 if ( nPos
< 0 || Text
.getLength() <= nPos
) return 0;
130 return static_cast<sal_Int16
>(u_charDirection(Text
.iterateCodePoints(&nPos
, 0)));
135 cclass_Unicode::getScript( const OUString
& Text
, sal_Int32 nPos
) {
136 if ( nPos
< 0 || Text
.getLength() <= nPos
) return 0;
137 // ICU Unicode script type UBlockCode starts from 1 for Basic Latin,
138 // while OO.o enum UnicideScript starts from 0.
139 // To map ICU UBlockCode to OO.o UnicodeScript, it needs to shift 1.
140 return static_cast<sal_Int16
>(ublock_getCode(Text
.iterateCodePoints(&nPos
, 0)))-1;
145 cclass_Unicode::getCharType( const OUString
& Text
, sal_Int32
* nPos
, sal_Int32 increment
) {
146 using namespace ::com::sun::star::i18n::KCharacterType
;
148 sal_uInt32 ch
= Text
.iterateCodePoints(nPos
, increment
);
149 switch ( u_charType(ch
) ) {
151 case U_UPPERCASE_LETTER
:
152 return UPPER
|LETTER
|PRINTABLE
|BASE_FORM
;
155 case U_LOWERCASE_LETTER
:
156 return LOWER
|LETTER
|PRINTABLE
|BASE_FORM
;
159 case U_TITLECASE_LETTER
:
160 return TITLE_CASE
|LETTER
|PRINTABLE
|BASE_FORM
;
163 case U_MODIFIER_LETTER
:
164 case U_OTHER_LETTER
:
165 return LETTER
|PRINTABLE
|BASE_FORM
;
168 case U_DECIMAL_DIGIT_NUMBER
:
169 case U_LETTER_NUMBER
:
171 return DIGIT
|PRINTABLE
|BASE_FORM
;
174 case U_NON_SPACING_MARK
:
175 case U_ENCLOSING_MARK
:
176 case U_COMBINING_SPACING_MARK
:
177 return BASE_FORM
|PRINTABLE
;
180 case U_SPACE_SEPARATOR
:
182 case U_DASH_PUNCTUATION
:
183 case U_INITIAL_PUNCTUATION
:
184 case U_FINAL_PUNCTUATION
:
185 case U_CONNECTOR_PUNCTUATION
:
186 case U_OTHER_PUNCTUATION
:
189 case U_CURRENCY_SYMBOL
:
190 case U_MODIFIER_SYMBOL
:
199 case U_LINE_SEPARATOR
:
200 case U_PARAGRAPH_SEPARATOR
:
201 return CONTROL
|PRINTABLE
;
205 return U_GENERAL_OTHER_TYPES
;
210 cclass_Unicode::getCharacterType( const OUString
& Text
, sal_Int32 nPos
, const Locale
& /*rLocale*/ ) {
211 if ( nPos
< 0 || Text
.getLength() <= nPos
) return 0;
212 return getCharType(Text
, &nPos
, 0);
217 cclass_Unicode::getStringType( const OUString
& Text
, sal_Int32 nPos
, sal_Int32 nCount
, const Locale
& /*rLocale*/ ) {
218 if ( nPos
< 0 || Text
.getLength() <= nPos
) return 0;
220 sal_Int32 result
= 0;
222 while (nCount
> 0 && nPos
< Text
.getLength())
224 sal_Int32 nOrigPos
= nPos
;
225 result
|= getCharType(Text
, &nPos
, 1);
226 sal_Int32 nUtf16Units
= nPos
- nOrigPos
;
227 nCount
-= nUtf16Units
;
233 ParseResult SAL_CALL
cclass_Unicode::parseAnyToken(
234 const OUString
& Text
,
236 const Locale
& rLocale
,
237 sal_Int32 startCharTokenType
,
238 const OUString
& userDefinedCharactersStart
,
239 sal_Int32 contCharTokenType
,
240 const OUString
& userDefinedCharactersCont
)
243 if ( Text
.getLength() <= nPos
)
246 setupParserTable( rLocale
,
247 startCharTokenType
, userDefinedCharactersStart
,
248 contCharTokenType
, userDefinedCharactersCont
);
249 parseText( r
, Text
, nPos
);
255 ParseResult SAL_CALL
cclass_Unicode::parsePredefinedToken(
256 sal_Int32 nTokenType
,
257 const OUString
& Text
,
259 const Locale
& rLocale
,
260 sal_Int32 startCharTokenType
,
261 const OUString
& userDefinedCharactersStart
,
262 sal_Int32 contCharTokenType
,
263 const OUString
& userDefinedCharactersCont
)
266 if ( Text
.getLength() <= nPos
)
269 setupParserTable( rLocale
,
270 startCharTokenType
, userDefinedCharactersStart
,
271 contCharTokenType
, userDefinedCharactersCont
);
272 parseText( r
, Text
, nPos
, nTokenType
);
277 OUString SAL_CALL
cclass_Unicode::getImplementationName()
279 return "com.sun.star.i18n.CharacterClassification_Unicode";
282 sal_Bool SAL_CALL
cclass_Unicode::supportsService(const OUString
& rServiceName
)
284 return cppu::supportsService(this, rServiceName
);
287 Sequence
< OUString
> SAL_CALL
cclass_Unicode::getSupportedServiceNames()
289 return { "com.sun.star.i18n.CharacterClassification_Unicode" };
294 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
295 com_sun_star_i18n_CharacterClassification_Unicode_get_implementation(
296 css::uno::XComponentContext
*context
,
297 css::uno::Sequence
<css::uno::Any
> const &)
299 return cppu::acquire(new i18npool::cclass_Unicode(context
));
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */