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 <config_folders.h>
23 #include <osl/mutex.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <rtl/bootstrap.hxx>
26 #include <com/sun/star/i18n/WordType.hpp>
27 #include <xdictionary.hxx>
28 #include <unicode/uchar.h>
30 #include <breakiteratorImpl.hxx>
32 using namespace com::sun::star::i18n
;
36 #ifdef DICT_JA_ZH_IN_DATAFILE
38 #elif !defined DISABLE_DYNLOADING
40 extern "C" { static void SAL_CALL
thisModule() {} }
46 sal_uInt8
* getExistMark_ja();
47 sal_Int16
* getIndex1_ja();
48 sal_Int32
* getIndex2_ja();
49 sal_Int32
* getLenArray_ja();
50 sal_Unicode
* getDataArea_ja();
52 sal_uInt8
* getExistMark_zh();
53 sal_Int16
* getIndex1_zh();
54 sal_Int32
* getIndex2_zh();
55 sal_Int32
* getLenArray_zh();
56 sal_Unicode
* getDataArea_zh();
62 xdictionary::xdictionary(const sal_Char
*lang
) :
64 japaneseWordBreak( false )
67 #ifdef DICT_JA_ZH_IN_DATAFILE
69 if( strcmp( lang
, "ja" ) == 0 || strcmp( lang
, "zh" ) == 0 )
71 OUString
sUrl( "$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER
"/dict_" );
72 rtl::Bootstrap::expandMacros(sUrl
);
74 if( strcmp( lang
, "ja" ) == 0 )
76 else if( strcmp( lang
, "zh" ) == 0 )
79 oslFileHandle aFileHandle
;
82 if( osl_openFile( sUrl
.pData
, &aFileHandle
, osl_File_OpenFlag_Read
) == osl_File_E_None
&&
83 osl_getFileSize( aFileHandle
, &nFileSize
) == osl_File_E_None
&&
84 osl_mapFile( aFileHandle
, (void **) &pMapping
, nFileSize
, 0, osl_File_MapFlag_RandomAccess
) == osl_File_E_None
)
86 // We have the offsets to the parts of the file at its end, see gendict.cxx
87 sal_Int64
*pEOF
= (sal_Int64
*)(pMapping
+ nFileSize
);
89 data
.existMark
= (sal_uInt8
*) (pMapping
+ pEOF
[-1]);
90 data
.index2
= (sal_Int32
*) (pMapping
+ pEOF
[-2]);
91 data
.index1
= (sal_Int16
*) (pMapping
+ pEOF
[-3]);
92 data
.lenArray
= (sal_Int32
*) (pMapping
+ pEOF
[-4]);
93 data
.dataArea
= (sal_Unicode
*) (pMapping
+ pEOF
[-5]);
97 #elif !defined DISABLE_DYNLOADING
99 initDictionaryData( lang
);
103 if( strcmp( lang
, "ja" ) == 0 ) {
104 data
.existMark
= getExistMark_ja();
105 data
.index1
= getIndex1_ja();
106 data
.index2
= getIndex2_ja();
107 data
.lenArray
= getLenArray_ja();
108 data
.dataArea
= getDataArea_ja();
110 else if( strcmp( lang
, "zh" ) == 0 ) {
111 data
.existMark
= getExistMark_zh();
112 data
.index1
= getIndex1_zh();
113 data
.index2
= getIndex2_zh();
114 data
.lenArray
= getLenArray_zh();
115 data
.dataArea
= getDataArea_zh();
120 for (WordBreakCache
& i
: cache
)
123 japaneseWordBreak
= false;
126 xdictionary::~xdictionary()
128 for (WordBreakCache
& i
: cache
) {
130 delete [] i
.contents
;
131 delete [] i
.wordboundary
;
140 xdictionarydata maData
;
144 #if !defined(DICT_JA_ZH_IN_DATAFILE) && !defined(DISABLE_DYNLOADING)
146 void xdictionary::initDictionaryData(const sal_Char
*pLang
)
148 // Global cache, never released for performance
149 static std::vector
< datacache
> aLoadedCache
;
151 osl::MutexGuard
aGuard( osl::Mutex::getGlobalMutex() );
152 for(datacache
& i
: aLoadedCache
)
154 if( i
.maLang
!= pLang
)
161 // otherwise add to the cache, positive or negative.
163 aEntry
.maLang
= OString( pLang
, strlen( pLang
) );
166 OUStringBuffer
aBuf( sal::static_int_cast
<int>(strlen(pLang
) + 7 + 6) ); // mostly "lib*.so" (with * == dict_zh)
167 aBuf
.append( SAL_DLLPREFIX
);
169 OUStringBuffer
aBuf( sal::static_int_cast
<int>(strlen(pLang
) + 7 + 4) ); // mostly "*.dll" (with * == dict_zh)
171 aBuf
.append( "dict_" ).appendAscii( pLang
).append( SAL_DLLEXTENSION
);
172 aEntry
.mhModule
= osl_loadModuleRelative( &thisModule
, aBuf
.makeStringAndClear().pData
, SAL_LOADMODULE_DEFAULT
);
173 if( aEntry
.mhModule
) {
174 oslGenericFunction func
;
175 func
= osl_getAsciiFunctionSymbol( aEntry
.mhModule
, "getExistMark" );
176 aEntry
.maData
.existMark
= reinterpret_cast<sal_uInt8
const * (*)()>(func
)();
177 func
= osl_getAsciiFunctionSymbol( aEntry
.mhModule
, "getIndex1" );
178 aEntry
.maData
.index1
= reinterpret_cast<sal_Int16
const * (*)()>(func
)();
179 func
= osl_getAsciiFunctionSymbol( aEntry
.mhModule
, "getIndex2" );
180 aEntry
.maData
.index2
= reinterpret_cast<sal_Int32
const * (*)()>(func
)();
181 func
= osl_getAsciiFunctionSymbol( aEntry
.mhModule
, "getLenArray" );
182 aEntry
.maData
.lenArray
= reinterpret_cast<sal_Int32
const * (*)()>(func
)();
183 func
= osl_getAsciiFunctionSymbol( aEntry
.mhModule
, "getDataArea" );
184 aEntry
.maData
.dataArea
= reinterpret_cast<sal_Unicode
const * (*)()>(func
)();
187 data
= aEntry
.maData
;
188 aLoadedCache
.push_back( aEntry
);
193 void xdictionary::setJapaneseWordBreak()
195 japaneseWordBreak
= true;
198 bool xdictionary::exists(const sal_uInt32 c
)
200 // 0x1FFF is the hardcoded limit in gendict for data.existMarks
201 bool exist
= data
.existMark
&& (c
>>3) < 0x1FFF && (data
.existMark
[c
>>3] & (1<<(c
&0x07))) != 0;
202 if (!exist
&& japaneseWordBreak
)
203 return BreakIteratorImpl::getScriptClass(c
) == css::i18n::ScriptType::ASIAN
;
208 sal_Int32
xdictionary::getLongestMatch(const sal_Unicode
* str
, sal_Int32 sLen
)
210 if ( !data
.index1
) return 0;
212 sal_Int16 idx
= data
.index1
[str
[0] >> 8];
214 if (idx
== 0xFF) return 0;
216 idx
= (idx
<<8) | (str
[0]&0xff);
218 sal_uInt32 begin
= data
.index2
[idx
], end
= data
.index2
[idx
+1];
220 if (begin
== 0) return 0;
222 str
++; sLen
--; // first character is not stored in the dictionary
223 for (sal_uInt32 i
= end
; i
> begin
; i
--) {
224 sal_Int32 len
= data
.lenArray
[i
] - data
.lenArray
[i
- 1];
226 const sal_Unicode
*dstr
= data
.dataArea
+ data
.lenArray
[i
-1];
229 while (pos
< len
&& dstr
[pos
] == str
[pos
]) { pos
++; }
243 WordBreakCache::WordBreakCache() :
246 wordboundary( nullptr ),
252 * Compare two unicode string,
255 bool WordBreakCache::equals(const sal_Unicode
* str
, Boundary
const & boundary
)
257 // Different length, different string.
258 if (length
!= boundary
.endPos
- boundary
.startPos
) return false;
260 for (sal_Int32 i
= 0; i
< length
; i
++)
261 if (contents
[i
] != str
[i
+ boundary
.startPos
]) return false;
268 * Retrieve the segment containing the character at pos.
269 * @param pos : Position of the given character.
270 * @return true if CJK.
272 bool xdictionary::seekSegment(const OUString
&rText
, sal_Int32 pos
,
273 Boundary
& segBoundary
)
275 sal_Int32 indexUtf16
;
277 if (segmentCachedString
.pData
!= rText
.pData
) {
278 // Cache the passed text so we can avoid regenerating the segment if it's the same
279 // (pData is refcounted and assigning the OUString references it, which ensures that
280 // the object is the same if we get the same pointer back later)
281 segmentCachedString
= rText
;
283 // If pos is within the cached boundary, use that boundary
284 if (pos
>= segmentCachedBoundary
.startPos
&& pos
<= segmentCachedBoundary
.endPos
) {
285 segBoundary
.startPos
= segmentCachedBoundary
.startPos
;
286 segBoundary
.endPos
= segmentCachedBoundary
.endPos
;
287 indexUtf16
= segmentCachedBoundary
.startPos
;
288 rText
.iterateCodePoints(&indexUtf16
);
289 return segmentCachedBoundary
.endPos
> indexUtf16
;
293 segBoundary
.endPos
= segBoundary
.startPos
= pos
;
296 while (indexUtf16
> 0)
298 sal_uInt32 ch
= rText
.iterateCodePoints(&indexUtf16
, -1);
299 if (u_isWhitespace(ch
) || exists(ch
))
300 segBoundary
.startPos
= indexUtf16
;
306 while (indexUtf16
< rText
.getLength())
308 sal_uInt32 ch
= rText
.iterateCodePoints(&indexUtf16
);
309 if (u_isWhitespace(ch
) || exists(ch
))
310 segBoundary
.endPos
= indexUtf16
;
315 // Cache the calculated boundary
316 segmentCachedBoundary
.startPos
= segBoundary
.startPos
;
317 segmentCachedBoundary
.endPos
= segBoundary
.endPos
;
319 indexUtf16
= segBoundary
.startPos
;
320 rText
.iterateCodePoints(&indexUtf16
);
321 return segBoundary
.endPos
> indexUtf16
;
328 static sal_Int16
JapaneseCharType(sal_Unicode c
)
330 if (0x3041 <= c
&& c
<= 0x309e)
332 if ((0x30a1 <= c
&& c
<= 0x30fe) || (0xff65 <= c
&& c
<= 0xff9f))
337 WordBreakCache
& xdictionary::getCache(const sal_Unicode
*text
, Boundary
const & wordBoundary
)
339 WordBreakCache
& rCache
= cache
[text
[0] & 0x1f];
341 if (rCache
.size
!= 0 && rCache
.equals(text
, wordBoundary
))
344 sal_Int32 len
= wordBoundary
.endPos
- wordBoundary
.startPos
;
346 if (rCache
.size
== 0 || len
> rCache
.size
) {
347 if (rCache
.size
!= 0) {
348 delete [] rCache
.contents
;
349 delete [] rCache
.wordboundary
;
353 rCache
.size
= len
> DEFAULT_SIZE
? len
: DEFAULT_SIZE
;
354 rCache
.contents
= new sal_Unicode
[rCache
.size
+ 1];
355 rCache
.wordboundary
= new sal_Int32
[rCache
.size
+ 2];
358 memcpy(rCache
.contents
, text
+ wordBoundary
.startPos
, len
* sizeof(sal_Unicode
));
359 *(rCache
.contents
+ len
) = 0x0000;
360 // reset the wordboundary in cache
361 memset(rCache
.wordboundary
, '\0', sizeof(sal_Int32
)*(len
+ 2));
363 sal_Int32 i
= 0; // loop variable
364 while (rCache
.wordboundary
[i
] < rCache
.length
) {
366 // look the continuous white space as one word and cache it
367 while (u_isWhitespace((sal_uInt32
)text
[wordBoundary
.startPos
+ rCache
.wordboundary
[i
] + len
]))
371 const sal_Unicode
*str
= text
+ wordBoundary
.startPos
+ rCache
.wordboundary
[i
];
372 sal_Int32 slen
= rCache
.length
- rCache
.wordboundary
[i
];
373 sal_Int16 type
= 0, count
= 0;
374 for (;len
== 0 && slen
> 0; str
++, slen
--) {
375 len
= getLongestMatch(str
, slen
);
377 if (!japaneseWordBreak
) {
381 type
= JapaneseCharType(*str
);
382 else if (type
!= JapaneseCharType(*str
))
390 rCache
.wordboundary
[i
+1] = rCache
.wordboundary
[i
] + count
;
396 rCache
.wordboundary
[i
+1] = rCache
.wordboundary
[i
] + len
;
400 rCache
.wordboundary
[i
+ 1] = rCache
.length
+ 1;
405 Boundary
xdictionary::previousWord(const OUString
& rText
, sal_Int32 anyPos
, sal_Int16 wordType
)
407 // looking for the first non-whitespace character from anyPos
410 rText
.iterateCodePoints(&anyPos
, -1);
412 while (anyPos
> 0 && u_isWhitespace(ch
)) ch
= rText
.iterateCodePoints(&anyPos
, -1);
414 return getWordBoundary(rText
, anyPos
, wordType
, true);
417 Boundary
xdictionary::nextWord(const OUString
& rText
, sal_Int32 anyPos
, sal_Int16 wordType
)
419 boundary
= getWordBoundary(rText
, anyPos
, wordType
, true);
420 anyPos
= boundary
.endPos
;
421 const sal_Int32 nLen
= rText
.getLength();
423 // looking for the first non-whitespace character from anyPos
424 sal_uInt32 ch
= rText
.iterateCodePoints(&anyPos
);
425 while (u_isWhitespace(ch
) && (anyPos
< nLen
)) ch
=rText
.iterateCodePoints(&anyPos
);
427 rText
.iterateCodePoints(&anyPos
, -1);
430 return getWordBoundary(rText
, anyPos
, wordType
, true);
433 Boundary
const & xdictionary::getWordBoundary(const OUString
& rText
, sal_Int32 anyPos
, sal_Int16 wordType
, bool bDirection
)
435 const sal_Unicode
*text
=rText
.getStr();
436 sal_Int32 len
=rText
.getLength();
437 if (anyPos
>= len
|| anyPos
< 0) {
438 boundary
.startPos
= boundary
.endPos
= anyPos
< 0 ? 0 : len
;
439 } else if (seekSegment(rText
, anyPos
, boundary
)) { // character in dict
440 WordBreakCache
& aCache
= getCache(text
, boundary
);
443 while (aCache
.wordboundary
[i
] <= anyPos
- boundary
.startPos
) i
++;
445 sal_Int32 startPos
= aCache
.wordboundary
[i
- 1];
446 // if bDirection is false
447 if (!bDirection
&& startPos
> 0 && startPos
== (anyPos
- boundary
.startPos
))
449 sal_Int32 indexUtf16
= anyPos
-1;
450 sal_uInt32 ch
= rText
.iterateCodePoints(&indexUtf16
);
451 if (u_isWhitespace(ch
))
455 boundary
.endPos
= boundary
.startPos
;
456 boundary
.endPos
+= aCache
.wordboundary
[i
];
457 boundary
.startPos
+= aCache
.wordboundary
[i
-1];
460 boundary
.startPos
= anyPos
;
461 if (anyPos
< len
) rText
.iterateCodePoints(&anyPos
);
462 boundary
.endPos
= anyPos
< len
? anyPos
: len
;
464 if (wordType
== WordType::WORD_COUNT
) {
465 // skip punctuation for word count.
466 while (boundary
.endPos
< len
)
468 sal_Int32 indexUtf16
= boundary
.endPos
;
469 if (u_ispunct(rText
.iterateCodePoints(&indexUtf16
)))
470 boundary
.endPos
= indexUtf16
;
481 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */