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 .
21 #include <rtl/ustrbuf.hxx>
22 #include <i18nutil/casefolding.hxx>
23 #include <i18nutil/unicode.hxx>
25 #include <comphelper/processfactory.hxx>
26 #include <comphelper/string.hxx>
27 #include <osl/diagnose.h>
31 #include "characterclassificationImpl.hxx"
32 #include "breakiteratorImpl.hxx"
34 #define TRANSLITERATION_ALL
35 #include "transliteration_body.hxx"
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::lang
;
39 using namespace ::rtl
;
41 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
44 Transliteration_body::Transliteration_body()
47 transliterationName
= "Transliteration_body";
48 implementationName
= "com.sun.star.i18n.Transliteration.Transliteration_body";
51 sal_Int16 SAL_CALL
Transliteration_body::getType() throw(RuntimeException
)
53 return TransliterationType::ONE_TO_ONE
;
56 sal_Bool SAL_CALL
Transliteration_body::equals(
57 const OUString
& /*str1*/, sal_Int32
/*pos1*/, sal_Int32
/*nCount1*/, sal_Int32
& /*nMatch1*/,
58 const OUString
& /*str2*/, sal_Int32
/*pos2*/, sal_Int32
/*nCount2*/, sal_Int32
& /*nMatch2*/)
59 throw(RuntimeException
)
61 throw RuntimeException();
64 Sequence
< OUString
> SAL_CALL
65 Transliteration_body::transliterateRange( const OUString
& str1
, const OUString
& str2
)
66 throw( RuntimeException
)
68 Sequence
< OUString
> ostr(2);
75 static sal_uInt8
lcl_getMappingTypeForToggleCase( sal_uInt8 nMappingType
, sal_Unicode cChar
)
77 sal_uInt8 nRes
= nMappingType
;
79 // take care of TOGGLE_CASE transliteration:
80 // nMappingType should not be a combination of flags, thuse we decide now
82 if (nMappingType
== (MappingTypeLowerToUpper
| MappingTypeUpperToLower
))
84 const sal_Int16 nType
= unicode::getUnicodeType( cChar
);
85 if (nType
& 0x02 /* lower case*/)
86 nRes
= MappingTypeLowerToUpper
;
89 // should also work properly for non-upper characters like white spacs, numbers, ...
90 nRes
= MappingTypeUpperToLower
;
99 Transliteration_body::transliterate(
100 const OUString
& inStr
, sal_Int32 startPos
, sal_Int32 nCount
,
101 Sequence
< sal_Int32
>& offset
)
102 throw(RuntimeException
)
105 const sal_Unicode
*in
= inStr
.getStr() + startPos
;
107 // Two different blocks to eliminate the if(useOffset) condition inside the
108 // inner k loop. Yes, on massive use even such small things do count.
111 sal_Int32 nOffCount
= 0, i
;
112 for (i
= 0; i
< nCount
; i
++)
114 // take care of TOGGLE_CASE transliteration:
115 sal_uInt8 nTmpMappingType
= nMappingType
;
116 if (nMappingType
== (MappingTypeLowerToUpper
| MappingTypeUpperToLower
))
117 nTmpMappingType
= lcl_getMappingTypeForToggleCase( nMappingType
, in
[i
] );
119 const Mapping
&map
= casefolding::getValue( in
, i
, nCount
, aLocale
, nTmpMappingType
);
120 nOffCount
+= map
.nmap
;
122 rtl_uString
* pStr
= comphelper::string::rtl_uString_alloc(nOffCount
);
123 sal_Unicode
* out
= pStr
->buffer
;
125 if ( nOffCount
!= offset
.getLength() )
126 offset
.realloc( nOffCount
);
129 sal_Int32
* pArr
= offset
.getArray();
130 for (i
= 0; i
< nCount
; i
++)
132 // take care of TOGGLE_CASE transliteration:
133 sal_uInt8 nTmpMappingType
= nMappingType
;
134 if (nMappingType
== (MappingTypeLowerToUpper
| MappingTypeUpperToLower
))
135 nTmpMappingType
= lcl_getMappingTypeForToggleCase( nMappingType
, in
[i
] );
137 const Mapping
&map
= casefolding::getValue( in
, i
, nCount
, aLocale
, nTmpMappingType
);
138 for (sal_Int32 k
= 0; k
< map
.nmap
; k
++)
140 pArr
[j
] = i
+ startPos
;
141 out
[j
++] = map
.map
[k
];
146 return OUString( pStr
, SAL_NO_ACQUIRE
);
150 // In the simple case of no offset sequence used we can eliminate the
151 // first getValue() loop. We could also assume that most calls result
152 // in identical string lengths, thus using a preallocated
153 // OUStringBuffer could be an easy way to assemble the return string
154 // without too much hassle. However, for single characters the
155 // OUStringBuffer::append() method is quite expensive compared to a
156 // simple array operation, so it pays here to copy the final result
159 // Allocate the max possible buffer. Try to use stack instead of heap,
160 // which would have to be reallocated most times anyways.
161 const sal_Int32 nLocalBuf
= 2048;
162 sal_Unicode aLocalBuf
[ nLocalBuf
* NMAPPINGMAX
], *out
= aLocalBuf
, *pHeapBuf
= NULL
;
163 if ( nCount
> nLocalBuf
)
164 out
= pHeapBuf
= new sal_Unicode
[ nCount
* NMAPPINGMAX
];
167 for ( sal_Int32 i
= 0; i
< nCount
; i
++)
169 // take care of TOGGLE_CASE transliteration:
170 sal_uInt8 nTmpMappingType
= nMappingType
;
171 if (nMappingType
== (MappingTypeLowerToUpper
| MappingTypeUpperToLower
))
172 nTmpMappingType
= lcl_getMappingTypeForToggleCase( nMappingType
, in
[i
] );
174 const Mapping
&map
= casefolding::getValue( in
, i
, nCount
, aLocale
, nTmpMappingType
);
175 for (sal_Int32 k
= 0; k
< map
.nmap
; k
++)
177 out
[j
++] = map
.map
[k
];
181 OUString
aRet( out
, j
);
189 Transliteration_body::transliterateChar2String( sal_Unicode inChar
) throw(RuntimeException
)
191 const Mapping
&map
= casefolding::getValue(&inChar
, 0, 1, aLocale
, nMappingType
);
192 rtl_uString
* pStr
= comphelper::string::rtl_uString_alloc(map
.nmap
);
193 sal_Unicode
* out
= pStr
->buffer
;
196 for (i
= 0; i
< map
.nmap
; i
++)
200 return OUString( pStr
, SAL_NO_ACQUIRE
);
204 Transliteration_body::transliterateChar2Char( sal_Unicode inChar
) throw(MultipleCharsOutputException
, RuntimeException
)
206 const Mapping
&map
= casefolding::getValue(&inChar
, 0, 1, aLocale
, nMappingType
);
208 throw MultipleCharsOutputException();
213 Transliteration_body::folding( const OUString
& inStr
, sal_Int32 startPos
, sal_Int32 nCount
,
214 Sequence
< sal_Int32
>& offset
) throw(RuntimeException
)
216 return this->transliterate(inStr
, startPos
, nCount
, offset
);
219 Transliteration_casemapping::Transliteration_casemapping()
222 transliterationName
= "casemapping(generic)";
223 implementationName
= "com.sun.star.i18n.Transliteration.Transliteration_casemapping";
227 Transliteration_casemapping::setMappingType( const sal_uInt8 rMappingType
, const Locale
& rLocale
)
229 nMappingType
= rMappingType
;
233 Transliteration_u2l::Transliteration_u2l()
235 nMappingType
= MappingTypeUpperToLower
;
236 transliterationName
= "upper_to_lower(generic)";
237 implementationName
= "com.sun.star.i18n.Transliteration.Transliteration_u2l";
240 Transliteration_l2u::Transliteration_l2u()
242 nMappingType
= MappingTypeLowerToUpper
;
243 transliterationName
= "lower_to_upper(generic)";
244 implementationName
= "com.sun.star.i18n.Transliteration.Transliteration_l2u";
247 Transliteration_togglecase::Transliteration_togglecase()
249 // usually nMappingType must NOT be a combiantion of different flages here,
250 // but we take care of that problem in Transliteration_body::transliterate above
251 // before that value is used. There we will decide which of both is to be used on
252 // a per character basis.
253 nMappingType
= MappingTypeLowerToUpper
| MappingTypeUpperToLower
;
254 transliterationName
= "toggle(generic)";
255 implementationName
= "com.sun.star.i18n.Transliteration.Transliteration_togglecase";
258 Transliteration_titlecase::Transliteration_titlecase()
260 nMappingType
= MappingTypeToTitle
;
261 transliterationName
= "title(generic)";
262 implementationName
= "com.sun.star.i18n.Transliteration.Transliteration_titlecase";
265 static rtl::OUString
transliterate_titlecase_Impl(
266 const OUString
& inStr
, sal_Int32 startPos
, sal_Int32 nCount
,
267 const Locale
&rLocale
,
268 Sequence
< sal_Int32
>& offset
)
269 throw(RuntimeException
)
271 const OUString
aText( inStr
.copy( startPos
, nCount
) );
274 if (!aText
.isEmpty())
276 Reference
< XMultiServiceFactory
> xMSF
= ::comphelper::getProcessServiceFactory();
277 CharacterClassificationImpl
aCharClassImpl( xMSF
);
279 // because aCharClassImpl.toTitle does not handle ligatures or ß but will raise
280 // an exception we need to handle the first chara manually...
282 // we don't want to change surrogates by accident, thuse we use proper code point iteration
284 sal_uInt32 cFirstChar
= aText
.iterateCodePoints( &nPos
);
285 OUString
aResolvedLigature( &cFirstChar
, 1 ); //lcl_ResolveLigature( cFirstChar ) );
286 // toUpper can be used to properly resolve ligatures and characters like ß
287 aResolvedLigature
= aCharClassImpl
.toUpper( aResolvedLigature
, 0, aResolvedLigature
.getLength(), rLocale
);
288 // since toTitle will leave all-uppercase text unchanged we first need to
289 // use toLower to bring possible 2nd and following charas in lowercase
290 aResolvedLigature
= aCharClassImpl
.toLower( aResolvedLigature
, 0, aResolvedLigature
.getLength(), rLocale
);
291 sal_Int32 nResolvedLen
= aResolvedLigature
.getLength();
293 // now we can properly use toTitle to get the expected result for the resolved string.
294 // The rest of the text should just become lowercase.
295 aRes
= aCharClassImpl
.toTitle( aResolvedLigature
, 0, nResolvedLen
, rLocale
);
296 aRes
+= aCharClassImpl
.toLower( aText
, 1, aText
.getLength() - 1, rLocale
);
297 offset
.realloc( aRes
.getLength() );
299 sal_Int32
*pOffset
= offset
.getArray();
300 sal_Int32 nLen
= offset
.getLength();
301 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
304 if (i
>= nResolvedLen
)
305 nIdx
= i
- nResolvedLen
+ 1;
309 #if OSL_DEBUG_LEVEL > 1
310 const sal_Int32
*pCOffset
= offset
.getConstArray();
318 // this function expects to be called on a word-by-word basis,
319 // namely that startPos points to the first char of the word
320 rtl::OUString SAL_CALL
Transliteration_titlecase::transliterate(
321 const OUString
& inStr
, sal_Int32 startPos
, sal_Int32 nCount
,
322 Sequence
< sal_Int32
>& offset
)
323 throw(RuntimeException
)
325 return transliterate_titlecase_Impl( inStr
, startPos
, nCount
, aLocale
, offset
);
329 Transliteration_sentencecase::Transliteration_sentencecase()
331 nMappingType
= MappingTypeToTitle
; // though only to be applied to the first word...
332 transliterationName
= "sentence(generic)";
333 implementationName
= "com.sun.star.i18n.Transliteration.Transliteration_sentencecase";
337 // this function expects to be called on a sentence-by-sentence basis,
338 // namely that startPos points to the first word (NOT first char!) in the sentence
339 rtl::OUString SAL_CALL
Transliteration_sentencecase::transliterate(
340 const OUString
& inStr
, sal_Int32 startPos
, sal_Int32 nCount
,
341 Sequence
< sal_Int32
>& offset
)
342 throw(RuntimeException
)
344 return transliterate_titlecase_Impl( inStr
, startPos
, nCount
, aLocale
, offset
);
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */