Bump version to 6.0-36
[LibreOffice.git] / i18npool / source / breakiterator / xdictionary.cxx
blob5653021fc13a8b920d8aa56a62a60e58064acbae
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 <config_folders.h>
22 #include <osl/file.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>
29 #include <string.h>
30 #include <breakiteratorImpl.hxx>
32 using namespace com::sun::star::i18n;
34 namespace i18npool {
36 #ifdef DICT_JA_ZH_IN_DATAFILE
38 #elif !defined DISABLE_DYNLOADING
40 extern "C" { static void SAL_CALL thisModule() {} }
42 #else
44 extern "C" {
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();
60 #endif
62 xdictionary::xdictionary(const sal_Char *lang) :
63 boundary(),
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 )
75 sUrl += "ja.data";
76 else if( strcmp( lang, "zh" ) == 0 )
77 sUrl += "zh.data";
79 oslFileHandle aFileHandle;
80 sal_uInt64 nFileSize;
81 char *pMapping;
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 );
101 #else
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();
118 #endif
120 for (WordBreakCache & i : cache)
121 i.size = 0;
123 japaneseWordBreak = false;
126 xdictionary::~xdictionary()
128 for (WordBreakCache & i : cache) {
129 if (i.size > 0) {
130 delete [] i.contents;
131 delete [] i.wordboundary;
136 namespace {
137 struct datacache {
138 oslModule mhModule;
139 OString maLang;
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 )
156 data = i.maData;
157 return;
161 // otherwise add to the cache, positive or negative.
162 datacache aEntry;
163 aEntry.maLang = OString( pLang, strlen( pLang ) );
165 #ifdef SAL_DLLPREFIX
166 OUStringBuffer aBuf( sal::static_int_cast<int>(strlen(pLang) + 7 + 6) ); // mostly "lib*.so" (with * == dict_zh)
167 aBuf.append( SAL_DLLPREFIX );
168 #else
169 OUStringBuffer aBuf( sal::static_int_cast<int>(strlen(pLang) + 7 + 4) ); // mostly "*.dll" (with * == dict_zh)
170 #endif
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 );
191 #endif
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;
204 else
205 return exist;
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];
225 if (sLen >= len) {
226 const sal_Unicode *dstr = data.dataArea + data.lenArray[i-1];
227 sal_Int32 pos = 0;
229 while (pos < len && dstr[pos] == str[pos]) { pos++; }
231 if (pos == len)
232 return len + 1;
235 return 0;
240 * c-tor
243 WordBreakCache::WordBreakCache() :
244 length( 0 ),
245 contents( nullptr ),
246 wordboundary( nullptr ),
247 size( 0 )
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;
263 return true;
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;
282 } else {
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;
295 indexUtf16 = pos;
296 while (indexUtf16 > 0)
298 sal_uInt32 ch = rText.iterateCodePoints(&indexUtf16, -1);
299 if (u_isWhitespace(ch) || exists(ch))
300 segBoundary.startPos = indexUtf16;
301 else
302 break;
305 indexUtf16 = pos;
306 while (indexUtf16 < rText.getLength())
308 sal_uInt32 ch = rText.iterateCodePoints(&indexUtf16);
309 if (u_isWhitespace(ch) || exists(ch))
310 segBoundary.endPos = indexUtf16;
311 else
312 break;
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;
324 #define KANJA 1
325 #define KATAKANA 2
326 #define HIRAKANA 3
328 static sal_Int16 JapaneseCharType(sal_Unicode c)
330 if (0x3041 <= c && c <= 0x309e)
331 return HIRAKANA;
332 if ((0x30a1 <= c && c <= 0x30fe) || (0xff65 <= c && c <= 0xff9f))
333 return KATAKANA;
334 return KANJA;
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))
342 return rCache;
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;
350 rCache.size = len;
352 else
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];
357 rCache.length = len;
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) {
365 len = 0;
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]))
368 len ++;
370 if (len == 0) {
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);
376 if (len == 0) {
377 if (!japaneseWordBreak) {
378 len = 1;
379 } else {
380 if (count == 0)
381 type = JapaneseCharType(*str);
382 else if (type != JapaneseCharType(*str))
383 break;
384 count++;
388 if (count)
390 rCache.wordboundary[i+1] = rCache.wordboundary[i] + count;
391 i++;
395 if (len) {
396 rCache.wordboundary[i+1] = rCache.wordboundary[i] + len;
397 i++;
400 rCache.wordboundary[i + 1] = rCache.length + 1;
402 return rCache;
405 Boundary xdictionary::previousWord(const OUString& rText, sal_Int32 anyPos, sal_Int16 wordType)
407 // looking for the first non-whitespace character from anyPos
408 sal_uInt32 ch = 0;
409 if (anyPos > 0)
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();
422 if (anyPos < nLen) {
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);
426 if (anyPos > 0)
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);
441 sal_Int32 i = 0;
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))
452 i--;
455 boundary.endPos = boundary.startPos;
456 boundary.endPos += aCache.wordboundary[i];
457 boundary.startPos += aCache.wordboundary[i-1];
459 } else {
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;
471 else
472 break;
476 return boundary;
481 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */