Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / i18npool / source / characterclassification / cclass_unicode.cxx
blob2eb3fdf5f27d58ea731cd131e16a03ea755b9f3f
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 <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;
36 namespace i18npool {
38 // class cclass_Unicode
39 // ----------------------------------------------------;
41 cclass_Unicode::cclass_Unicode( const uno::Reference < XComponentContext >& rxContext ) :
42 trans( new Transliteration_casemapping() ),
43 m_xContext( rxContext ),
44 nStartTypes( 0 ),
45 nContTypes( 0 ),
46 eState( ssGetChar ),
47 cGroupSep( ',' ),
48 cDecimalSep( '.' ),
49 cDecimalSepAlt( 0 )
53 cclass_Unicode::~cclass_Unicode() {
54 destroyParserTable();
58 OUString SAL_CALL
59 cclass_Unicode::toUpper( const OUString& Text, sal_Int32 nPos, sal_Int32 nCount, const Locale& rLocale ) {
60 sal_Int32 len = Text.getLength();
61 if (nPos >= len)
62 return OUString();
63 if (nCount + nPos > len)
64 nCount = len - nPos;
66 trans->setMappingType(MappingType::ToUpper, rLocale);
67 return trans->transliterateString2String(Text, nPos, nCount);
70 OUString SAL_CALL
71 cclass_Unicode::toLower( const OUString& Text, sal_Int32 nPos, sal_Int32 nCount, const Locale& rLocale ) {
72 sal_Int32 len = Text.getLength();
73 if (nPos >= len)
74 return OUString();
75 if (nCount + nPos > len)
76 nCount = len - nPos;
78 trans->setMappingType(MappingType::ToLower, rLocale);
79 return trans->transliterateString2String(Text, nPos, nCount);
82 OUString SAL_CALL
83 cclass_Unicode::toTitle( const OUString& Text, sal_Int32 nPos, sal_Int32 nCount, const Locale& rLocale ) {
84 try
86 sal_Int32 len = Text.getLength();
87 if (nPos >= len)
88 return OUString();
89 if (nCount + nPos > len)
90 nCount = len - nPos;
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++) {
99 if (i >= bdy.endPos)
100 bdy = xBrk->nextWord(Text, bdy.endPos, rLocale,
101 WordType::ANYWORD_IGNOREWHITESPACES);
102 *out = (i == bdy.startPos) ?
103 trans->transliterateChar2Char(Text[i]) : Text[i];
105 *out = 0;
106 return OUString( pStr, SAL_NO_ACQUIRE );
108 catch (const RuntimeException&)
110 throw;
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);
121 sal_Int16 SAL_CALL
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)));
127 sal_Int16 SAL_CALL
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)));
134 sal_Int16 SAL_CALL
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;
144 sal_Int32
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) ) {
150 // Upper
151 case U_UPPERCASE_LETTER :
152 return UPPER|LETTER|PRINTABLE|BASE_FORM;
154 // Lower
155 case U_LOWERCASE_LETTER :
156 return LOWER|LETTER|PRINTABLE|BASE_FORM;
158 // Title
159 case U_TITLECASE_LETTER :
160 return TITLE_CASE|LETTER|PRINTABLE|BASE_FORM;
162 // Letter
163 case U_MODIFIER_LETTER :
164 case U_OTHER_LETTER :
165 return LETTER|PRINTABLE|BASE_FORM;
167 // Digit
168 case U_DECIMAL_DIGIT_NUMBER:
169 case U_LETTER_NUMBER:
170 case U_OTHER_NUMBER:
171 return DIGIT|PRINTABLE|BASE_FORM;
173 // Base
174 case U_NON_SPACING_MARK:
175 case U_ENCLOSING_MARK:
176 case U_COMBINING_SPACING_MARK:
177 return BASE_FORM|PRINTABLE;
179 // Print
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:
188 case U_MATH_SYMBOL:
189 case U_CURRENCY_SYMBOL:
190 case U_MODIFIER_SYMBOL:
191 case U_OTHER_SYMBOL:
192 return PRINTABLE;
194 // Control
195 case U_CONTROL_CHAR:
196 case U_FORMAT_CHAR:
197 return CONTROL;
199 case U_LINE_SEPARATOR:
200 case U_PARAGRAPH_SEPARATOR:
201 return CONTROL|PRINTABLE;
203 // for all others
204 default:
205 return U_GENERAL_OTHER_TYPES;
209 sal_Int32 SAL_CALL
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);
216 sal_Int32 SAL_CALL
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;
230 return result;
233 ParseResult SAL_CALL cclass_Unicode::parseAnyToken(
234 const OUString& Text,
235 sal_Int32 nPos,
236 const Locale& rLocale,
237 sal_Int32 startCharTokenType,
238 const OUString& userDefinedCharactersStart,
239 sal_Int32 contCharTokenType,
240 const OUString& userDefinedCharactersCont )
242 ParseResult r;
243 if ( Text.getLength() <= nPos )
244 return r;
246 setupParserTable( rLocale,
247 startCharTokenType, userDefinedCharactersStart,
248 contCharTokenType, userDefinedCharactersCont );
249 parseText( r, Text, nPos );
251 return r;
255 ParseResult SAL_CALL cclass_Unicode::parsePredefinedToken(
256 sal_Int32 nTokenType,
257 const OUString& Text,
258 sal_Int32 nPos,
259 const Locale& rLocale,
260 sal_Int32 startCharTokenType,
261 const OUString& userDefinedCharactersStart,
262 sal_Int32 contCharTokenType,
263 const OUString& userDefinedCharactersCont )
265 ParseResult r;
266 if ( Text.getLength() <= nPos )
267 return r;
269 setupParserTable( rLocale,
270 startCharTokenType, userDefinedCharactersStart,
271 contCharTokenType, userDefinedCharactersCont );
272 parseText( r, Text, nPos, nTokenType );
274 return r;
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: */