Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/linguistic/source/misc.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
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 file754
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 .
18
 */
19
20
#include <memory>
21
#include <map>
22
#include <optional>
23
#include <sal/log.hxx>
24
#include <svl/lngmisc.hxx>
25
#include <ucbhelper/content.hxx>
26
#include <i18nlangtag/languagetag.hxx>
27
#include <com/sun/star/beans/XPropertySet.hpp>
28
#include <com/sun/star/beans/XFastPropertySet.hpp>
29
#include <com/sun/star/beans/PropertyValues.hpp>
30
#include <com/sun/star/frame/Desktop.hpp>
31
#include <com/sun/star/frame/XStorable.hpp>
32
#include <com/sun/star/linguistic2/DictionaryType.hpp>
33
#include <com/sun/star/linguistic2/DictionaryList.hpp>
34
#include <com/sun/star/linguistic2/LinguProperties.hpp>
35
#include <com/sun/star/ucb/XCommandEnvironment.hpp>
36
#include <com/sun/star/uno/Sequence.hxx>
37
#include <com/sun/star/uno/Reference.h>
38
#include <comphelper/lok.hxx>
39
#include <comphelper/processfactory.hxx>
40
#include <comphelper/sequence.hxx>
41
#include <unotools/charclass.hxx>
42
#include <unotools/linguprops.hxx>
43
#include <unotools/localedatawrapper.hxx>
44
#include <svtools/strings.hrc>
45
#include <unotools/resmgr.hxx>
46
#include <o3tl/string_view.hxx>
47
48
#include <linguistic/misc.hxx>
49
#include <linguistic/hyphdta.hxx>
50
51
using namespace osl;
52
using namespace com::sun::star;
53
using namespace com::sun::star::beans;
54
using namespace com::sun::star::lang;
55
using namespace com::sun::star::uno;
56
using namespace com::sun::star::i18n;
57
using namespace com::sun::star::linguistic2;
58
59
namespace linguistic
60
{
61
62
//!! multi-thread safe mutex for all platforms !!
63
osl::Mutex & GetLinguMutex()
64
203
{
65
203
    static osl::Mutex SINGLETON;
66
203
    return SINGLETON;
67
203
}
68
69
const LocaleDataWrapper & GetLocaleDataWrapper( LanguageType nLang )
70
0
{
71
0
    static std::optional<LocaleDataWrapper> oLclDtaWrp;
72
0
    if (!oLclDtaWrp || oLclDtaWrp->getLoadedLanguageTag().getLanguageType() != nLang)
73
0
        oLclDtaWrp.emplace(LanguageTag( nLang ));
74
0
    return *oLclDtaWrp;
75
0
}
76
77
LanguageType LinguLocaleToLanguage( const css::lang::Locale& rLocale )
78
186
{
79
186
    if ( rLocale.Language.isEmpty() )
80
4
        return LANGUAGE_NONE;
81
182
    return LanguageTag::convertToLanguageType( rLocale );
82
186
}
83
84
css::lang::Locale LinguLanguageToLocale( LanguageType nLanguage )
85
0
{
86
0
    if (nLanguage == LANGUAGE_NONE)
87
0
        return css::lang::Locale();
88
0
    return LanguageTag::convertToLocale( nLanguage);
89
0
}
90
91
bool LinguIsUnspecified( LanguageType nLanguage )
92
3
{
93
3
    return nLanguage.anyOf(
94
3
         LANGUAGE_NONE,
95
3
         LANGUAGE_UNDETERMINED,
96
3
         LANGUAGE_MULTIPLE);
97
3
}
98
99
// When adding anything keep both LinguIsUnspecified() methods in sync!
100
// For mappings between language code string and LanguageType see
101
// i18nlangtag/source/isolang/isolang.cxx
102
103
bool LinguIsUnspecified( std::u16string_view rBcp47 )
104
0
{
105
0
    if (rBcp47.size() != 3)
106
0
        return false;
107
0
    return rBcp47 == u"zxx" || rBcp47 == u"und" || rBcp47 == u"mul";
108
0
}
109
110
static sal_Int32 Minimum( sal_Int32 n1, sal_Int32 n2, sal_Int32 n3 )
111
0
{
112
0
    return std::min(std::min(n1, n2), n3);
113
0
}
114
115
namespace {
116
117
class IntArray2D
118
{
119
private:
120
    std::unique_ptr<sal_Int32[]>  pData;
121
    int                           n1, n2;
122
123
public:
124
    IntArray2D( int nDim1, int nDim2 );
125
126
    sal_Int32 & Value( int i, int k  );
127
};
128
129
}
130
131
IntArray2D::IntArray2D( int nDim1, int nDim2 )
132
0
{
133
0
    n1 = nDim1;
134
0
    n2 = nDim2;
135
0
    pData.reset( new sal_Int32[n1 * n2] );
136
0
}
137
138
sal_Int32 & IntArray2D::Value( int i, int k  )
139
0
{
140
0
    assert( (0 <= i && i < n1) && "first index out of range" );
141
0
    assert( (0 <= k && k < n2) && "second index out of range" );
142
0
    assert( (i * n2 + k < n1 * n2) && "index out of range" );
143
0
    return pData[ i * n2 + k ];
144
0
}
145
146
sal_Int32 LevDistance( std::u16string_view rTxt1, std::u16string_view rTxt2 )
147
0
{
148
0
    sal_Int32 nLen1 = rTxt1.size();
149
0
    sal_Int32 nLen2 = rTxt2.size();
150
151
0
    if (nLen1 == 0)
152
0
        return nLen2;
153
0
    if (nLen2 == 0)
154
0
        return nLen1;
155
156
0
    IntArray2D aData( nLen1 + 1, nLen2 + 1 );
157
158
0
    sal_Int32 i, k;
159
0
    for (i = 0;  i <= nLen1;  ++i)
160
0
        aData.Value(i, 0) = i;
161
0
    for (k = 0;  k <= nLen2;  ++k)
162
0
        aData.Value(0, k) = k;
163
0
    for (i = 1;  i <= nLen1;  ++i)
164
0
    {
165
0
        for (k = 1;  k <= nLen2;  ++k)
166
0
        {
167
0
            sal_Unicode c1i = rTxt1[i - 1];
168
0
            sal_Unicode c2k = rTxt2[k - 1];
169
0
            sal_Int32 nCost = c1i == c2k ? 0 : 1;
170
0
            sal_Int32 nNew = Minimum( aData.Value(i-1, k  ) + 1,
171
0
                                       aData.Value(i  , k-1) + 1,
172
0
                                       aData.Value(i-1, k-1) + nCost );
173
            // take transposition (exchange with left or right char) in account
174
0
            if (2 < i && 2 < k)
175
0
            {
176
0
                int nT = aData.Value(i-2, k-2) + 1;
177
0
                if (rTxt1[i - 2] != c1i)
178
0
                    ++nT;
179
0
                if (rTxt2[k - 2] != c2k)
180
0
                    ++nT;
181
0
                if (nT < nNew)
182
0
                    nNew = nT;
183
0
            }
184
185
0
            aData.Value(i, k) = nNew;
186
0
        }
187
0
    }
188
0
    sal_Int32 nDist = aData.Value(nLen1, nLen2);
189
0
    return nDist;
190
0
}
191
192
bool IsUseDicList( const PropertyValues &rProperties,
193
        const uno::Reference< XPropertySet > &rxProp )
194
0
{
195
0
    bool bRes = true;
196
197
0
    const PropertyValue *pVal = std::find_if(rProperties.begin(), rProperties.end(),
198
0
        [](const PropertyValue& rVal) { return UPH_IS_USE_DICTIONARY_LIST == rVal.Handle; });
199
200
0
    if (pVal != rProperties.end())
201
0
    {
202
0
        pVal->Value >>= bRes;
203
0
    }
204
0
    else  // no temporary value found in 'rProperties'
205
0
    {
206
0
        uno::Reference< XFastPropertySet > xFast( rxProp, UNO_QUERY );
207
0
        if (xFast.is())
208
0
            xFast->getFastPropertyValue( UPH_IS_USE_DICTIONARY_LIST ) >>= bRes;
209
0
    }
210
211
0
    return bRes;
212
0
}
213
214
bool IsIgnoreControlChars( const PropertyValues &rProperties,
215
        const uno::Reference< XPropertySet > &rxProp )
216
0
{
217
0
    bool bRes = true;
218
219
0
    const PropertyValue *pVal = std::find_if(rProperties.begin(), rProperties.end(),
220
0
        [](const PropertyValue& rVal) { return UPH_IS_IGNORE_CONTROL_CHARACTERS == rVal.Handle; });
221
222
0
    if (pVal != rProperties.end())
223
0
    {
224
0
        pVal->Value >>= bRes;
225
0
    }
226
0
    else  // no temporary value found in 'rProperties'
227
0
    {
228
0
        uno::Reference< XFastPropertySet > xFast( rxProp, UNO_QUERY );
229
0
        if (xFast.is())
230
0
            xFast->getFastPropertyValue( UPH_IS_IGNORE_CONTROL_CHARACTERS ) >>= bRes;
231
0
    }
232
233
0
    return bRes;
234
0
}
235
236
static bool lcl_HasHyphInfo( const uno::Reference<XDictionaryEntry> &xEntry )
237
0
{
238
0
    bool bRes = false;
239
0
    if (xEntry.is())
240
0
    {
241
        // there has to be (at least one) '=' or '[' denoting a hyphenation position
242
        // and it must not be before any character of the word
243
0
        sal_Int32 nIdx = xEntry->getDictionaryWord().indexOf( '=' );
244
0
        if (nIdx == -1)
245
0
            nIdx = xEntry->getDictionaryWord().indexOf( '[' );
246
0
        bRes = nIdx != -1  &&  nIdx != 0;
247
0
    }
248
0
    return bRes;
249
0
}
250
251
uno::Reference< XDictionaryEntry > SearchDicList(
252
        const uno::Reference< XSearchableDictionaryList > &xDicList,
253
        const OUString &rWord, LanguageType nLanguage,
254
        bool bSearchPosDics, bool bSearchSpellEntry,
255
        std::map<LanguageType, std::vector<css::uno::Reference<css::linguistic2::XDictionary>>>& rDictionaryMap )
256
0
{
257
0
    MutexGuard  aGuard( GetLinguMutex() );
258
259
0
    if (!xDicList.is())
260
0
        return uno::Reference< XDictionaryEntry >();
261
262
0
    if (rDictionaryMap.empty())
263
0
    {
264
0
        const uno::Sequence< uno::Reference< XDictionary > >
265
0
                aDics( xDicList->getDictionaries() );
266
0
        for (const uno::Reference<XDictionary>& rxDic : aDics)
267
0
        {
268
0
            assert( rxDic.is() );
269
0
            LanguageType nLang = LinguLocaleToLanguage( rxDic->getLocale() );
270
0
            rDictionaryMap[nLang].push_back(rxDic);
271
0
        }
272
0
    }
273
274
0
    auto lcl_search = [&rDictionaryMap, bSearchPosDics, bSearchSpellEntry, &rWord](LanguageType nLang)
275
0
    {
276
0
        auto it = rDictionaryMap.find(nLang);
277
0
        if (it != rDictionaryMap.end())
278
0
            for (const uno::Reference<XDictionary>& rxDic : it->second)
279
0
            {
280
0
                if ( rxDic->isActive() )
281
0
                {
282
0
                    DictionaryType  eType = rxDic->getDictionaryType();
283
0
                    if (   (!bSearchPosDics  &&  eType == DictionaryType_NEGATIVE)
284
0
                        || ( bSearchPosDics  &&  eType == DictionaryType_POSITIVE))
285
0
                    {
286
0
                        uno::Reference< XDictionaryEntry > xEntry = rxDic->getEntry( rWord );
287
0
                        if ( xEntry.is() && (bSearchSpellEntry || lcl_HasHyphInfo( xEntry )) )
288
0
                            return xEntry;
289
0
                    }
290
0
                }
291
0
            }
292
0
        return uno::Reference< XDictionaryEntry >();
293
0
    };
294
295
0
    uno::Reference< XDictionaryEntry > xEntry;
296
0
    if ((xEntry = lcl_search(nLanguage)))
297
0
        return xEntry;
298
299
    // check the same list as in the LinguIsUnspecified() function
300
0
    if ((xEntry = lcl_search(LANGUAGE_NONE)))
301
0
        return xEntry;
302
0
    if ((xEntry = lcl_search(LANGUAGE_UNDETERMINED)))
303
0
        return xEntry;
304
0
    xEntry = lcl_search(LANGUAGE_MULTIPLE);
305
306
0
    return xEntry;
307
0
}
308
309
bool SaveDictionaries( const uno::Reference< XSearchableDictionaryList > &xDicList )
310
0
{
311
0
    if (!xDicList.is())
312
0
        return true;
313
314
0
    bool bRet = true;
315
316
0
    const Sequence< uno::Reference< XDictionary >  > aDics( xDicList->getDictionaries() );
317
0
    for (const uno::Reference<XDictionary>& rDic : aDics)
318
0
    {
319
0
        try
320
0
        {
321
0
            uno::Reference< frame::XStorable >  xStor( rDic, UNO_QUERY );
322
0
            if (xStor.is())
323
0
            {
324
0
                if (!xStor->isReadonly() && xStor->hasLocation())
325
0
                    xStor->store();
326
0
            }
327
0
        }
328
0
        catch(uno::Exception &)
329
0
        {
330
0
            bRet = false;
331
0
        }
332
0
    }
333
334
0
    return bRet;
335
0
}
336
337
DictionaryError AddEntryToDic(
338
        uno::Reference< XDictionary > const &rxDic,
339
        const OUString &rWord, bool bIsNeg,
340
        const OUString &rRplcTxt,
341
        bool bStripDot )
342
0
{
343
0
    if (!rxDic.is())
344
0
        return DictionaryError::NOT_EXISTS;
345
346
0
    OUString aTmp( rWord );
347
0
    if (bStripDot)
348
0
    {
349
0
        sal_Int32 nLen = rWord.getLength();
350
0
        if (nLen > 0  &&  '.' == rWord[ nLen - 1])
351
0
        {
352
            // remove trailing '.'
353
            // (this is the official way to do this :-( )
354
0
            aTmp = aTmp.copy( 0, nLen - 1 );
355
0
        }
356
0
    }
357
0
    bool bAddOk = rxDic->add( aTmp, bIsNeg, rRplcTxt );
358
359
0
    DictionaryError nRes = DictionaryError::NONE;
360
0
    if (!bAddOk)
361
0
    {
362
0
        if (rxDic->isFull())
363
0
            nRes = DictionaryError::FULL;
364
0
        else
365
0
        {
366
0
            uno::Reference< frame::XStorable >  xStor( rxDic, UNO_QUERY );
367
0
            if (xStor.is() && xStor->isReadonly())
368
0
                nRes = DictionaryError::READONLY;
369
0
            else
370
0
                nRes = DictionaryError::UNKNOWN;
371
0
        }
372
0
    }
373
374
0
    return nRes;
375
0
}
376
377
std::vector< LanguageType >
378
    LocaleSeqToLangVec( uno::Sequence< Locale > const &rLocaleSeq )
379
3
{
380
3
    std::vector< LanguageType >   aLangs;
381
3
    aLangs.reserve(rLocaleSeq.getLength());
382
383
3
    std::transform(rLocaleSeq.begin(), rLocaleSeq.end(), std::back_inserter(aLangs),
384
3
        [](const Locale& rLocale) { return LinguLocaleToLanguage(rLocale); });
385
386
3
    return aLangs;
387
3
}
388
389
uno::Sequence< sal_Int16 >
390
     LocaleSeqToLangSeq( uno::Sequence< Locale > const &rLocaleSeq )
391
0
{
392
0
    std::vector<sal_Int16> aLangs;
393
0
    aLangs.reserve(rLocaleSeq.getLength());
394
395
0
    std::transform(rLocaleSeq.begin(), rLocaleSeq.end(), std::back_inserter(aLangs),
396
0
        [](const Locale& rLocale) { return static_cast<sal_uInt16>(LinguLocaleToLanguage(rLocale)); });
397
398
0
    return comphelper::containerToSequence(aLangs);
399
0
}
400
bool    IsReadOnly( const OUString &rURL, bool *pbExist )
401
0
{
402
0
    bool bRes = false;
403
0
    bool bExists = false;
404
405
0
    if (!rURL.isEmpty())
406
0
    {
407
0
        try
408
0
        {
409
0
            uno::Reference< css::ucb::XCommandEnvironment > xCmdEnv;
410
0
            ::ucbhelper::Content aContent( rURL, xCmdEnv, comphelper::getProcessComponentContext() );
411
412
0
            bExists = aContent.isDocument();
413
0
            if (bExists)
414
0
            {
415
0
                Any aAny( aContent.getPropertyValue( u"IsReadOnly"_ustr ) );
416
0
                aAny >>= bRes;
417
0
            }
418
0
        }
419
0
        catch (Exception &)
420
0
        {
421
0
            bRes = true;
422
0
        }
423
0
    }
424
425
0
    if (pbExist)
426
0
        *pbExist = bExists;
427
0
    return bRes;
428
0
}
429
430
static bool GetAltSpelling( sal_Int16 &rnChgPos, sal_Int16 &rnChgLen, OUString &rRplc,
431
        uno::Reference< XHyphenatedWord > const &rxHyphWord )
432
0
{
433
0
    bool bRes = rxHyphWord->isAlternativeSpelling();
434
0
    if (bRes)
435
0
    {
436
0
        OUString aWord( rxHyphWord->getWord() ),
437
0
                 aHyphenatedWord( rxHyphWord->getHyphenatedWord() );
438
0
        sal_Int16   nHyphenationPos     = rxHyphWord->getHyphenationPos();
439
0
        /*sal_Int16   nHyphenPos          = rxHyphWord->getHyphenPos()*/;
440
0
        const sal_Unicode *pWord    = aWord.getStr(),
441
0
                          *pAltWord = aHyphenatedWord.getStr();
442
443
        // at least char changes directly left or right to the hyphen
444
        // should(!) be handled properly...
445
        //! nHyphenationPos and nHyphenPos differ at most by 1 (see above)
446
        //! Beware: eg "Schiffahrt" in German (pre spelling reform)
447
        //! proves to be a bit nasty (nChgPosLeft and nChgPosRight overlap
448
        //! to an extend.)
449
450
        // find first different char from left
451
0
        sal_Int32   nPosL    = 0,
452
0
                    nAltPosL = 0;
453
0
        for (sal_Int16 i = 0 ;  pWord[ nPosL ] == pAltWord[ nAltPosL ];  nPosL++, nAltPosL++, i++)
454
0
        {
455
            // restrict changes area beginning to the right to
456
            // the char immediately following the hyphen.
457
            //! serves to insert the additional "f" in "Schiffahrt" at
458
            //! position 5 rather than position 6.
459
0
            if (i >= nHyphenationPos + 1)
460
0
                break;
461
0
        }
462
463
        // find first different char from right
464
0
        sal_Int32   nPosR    = aWord.getLength() - 1,
465
0
                    nAltPosR = aHyphenatedWord.getLength() - 1;
466
0
        for ( ;  nPosR >= nPosL  &&  nAltPosR >= nAltPosL
467
0
                    &&  pWord[ nPosR ] == pAltWord[ nAltPosR ];
468
0
                nPosR--, nAltPosR--)
469
0
            ;
470
471
0
        rnChgPos = sal::static_int_cast< sal_Int16 >(nPosL);
472
0
        rnChgLen = sal::static_int_cast< sal_Int16 >(nAltPosR - nPosL);
473
0
        assert( rnChgLen >= 0 && "nChgLen < 0");
474
475
0
        sal_Int32 nTxtStart = nPosL;
476
0
        sal_Int32 nTxtLen   = nAltPosR - nPosL + 1;
477
0
        rRplc = aHyphenatedWord.copy( nTxtStart, nTxtLen );
478
0
    }
479
0
    return bRes;
480
0
}
481
482
static sal_Int16 GetOrigWordPos( std::u16string_view rOrigWord, sal_Int16 nPos )
483
0
{
484
0
    sal_Int32 nLen = rOrigWord.size();
485
0
    sal_Int32 i = -1;
486
0
    while (nPos >= 0  &&  i++ < nLen)
487
0
    {
488
0
        sal_Unicode cChar = rOrigWord[i];
489
0
        bool bSkip = IsHyphen( cChar ) || IsControlChar( cChar );
490
0
        if (!bSkip)
491
0
            --nPos;
492
0
    }
493
0
    return sal::static_int_cast< sal_Int16 >((0 <= i  &&  i < nLen) ? i : -1);
494
0
}
495
496
sal_Int32 GetPosInWordToCheck( std::u16string_view rTxt, sal_Int32 nPos )
497
0
{
498
0
    sal_Int32 nRes = -1;
499
0
    sal_Int32 nLen = rTxt.size();
500
0
    if (0 <= nPos  &&  nPos < nLen)
501
0
    {
502
0
        nRes = 0;
503
0
        for (sal_Int32 i = 0;  i < nPos;  ++i)
504
0
        {
505
0
            sal_Unicode cChar = rTxt[i];
506
0
            bool bSkip = IsHyphen( cChar ) || IsControlChar( cChar );
507
0
            if (!bSkip)
508
0
                ++nRes;
509
0
        }
510
0
    }
511
0
    return nRes;
512
0
}
513
514
rtl::Reference< HyphenatedWord > RebuildHyphensAndControlChars(
515
        const OUString &rOrigWord,
516
        uno::Reference< XHyphenatedWord > const &rxHyphWord )
517
0
{
518
0
    if (rOrigWord.isEmpty() || !rxHyphWord.is())
519
0
        return nullptr;
520
521
0
    sal_Int16    nChgPos = 0,
522
0
             nChgLen = 0;
523
0
    OUString aRplc;
524
0
    bool bAltSpelling = GetAltSpelling( nChgPos, nChgLen, aRplc, rxHyphWord );
525
526
0
    OUString aOrigHyphenatedWord;
527
0
    sal_Int16 nOrigHyphenPos        = -1;
528
0
    sal_Int16 nOrigHyphenationPos   = -1;
529
0
    if (!bAltSpelling)
530
0
    {
531
0
        aOrigHyphenatedWord = rOrigWord;
532
0
        nOrigHyphenPos      = GetOrigWordPos( rOrigWord, rxHyphWord->getHyphenPos() );
533
0
        nOrigHyphenationPos = GetOrigWordPos( rOrigWord, rxHyphWord->getHyphenationPos() );
534
0
    }
535
0
    else
536
0
    {
537
        //! should at least work with the German words
538
        //! B-"u-c-k-er and Sc-hif-fah-rt
539
540
0
        sal_Int16 nPos = GetOrigWordPos( rOrigWord, nChgPos );
541
542
        // get words like Sc-hif-fah-rt to work correct
543
0
        sal_Int16 nHyphenationPos = rxHyphWord->getHyphenationPos();
544
0
        if (nChgPos > nHyphenationPos)
545
0
            --nPos;
546
547
0
        std::u16string_view aLeft = rOrigWord.subView( 0, nPos );
548
0
        std::u16string_view aRight = rOrigWord.subView( nPos ); // FIXME: changes at the right side
549
550
0
        aOrigHyphenatedWord =  aLeft + aRplc + aRight;
551
552
0
        nOrigHyphenPos      = sal::static_int_cast< sal_Int16 >(aLeft.size() +
553
0
                              rxHyphWord->getHyphenPos() - nChgPos);
554
0
        nOrigHyphenationPos = GetOrigWordPos( rOrigWord, nHyphenationPos );
555
0
    }
556
557
0
    if (nOrigHyphenPos != -1  &&  nOrigHyphenationPos != -1)
558
0
    {
559
0
        SAL_INFO( "linguistic", "failed to get nOrigHyphenPos or nOrigHyphenationPos" );
560
0
        return nullptr;
561
0
    }
562
563
0
    LanguageType nLang = LinguLocaleToLanguage( rxHyphWord->getLocale() );
564
0
    return new HyphenatedWord(
565
0
                rOrigWord, nLang, nOrigHyphenationPos,
566
0
                aOrigHyphenatedWord, nOrigHyphenPos );
567
568
0
}
569
570
bool IsUpper( const OUString &rText, sal_Int32 nPos, sal_Int32 nLen, LanguageType nLanguage )
571
0
{
572
0
    assert(nPos >= 0 && nLen > 0);
573
0
    CharClass aCC(( LanguageTag( nLanguage ) ));
574
575
0
    bool bCaseIsAlwaysUppercase = false;
576
0
    const sal_Int32 nEnd = std::min(nPos + nLen, rText.getLength());
577
0
    while (nPos < nEnd)
578
0
    {
579
        // only consider characters that have case-status
580
0
        if (aCC.isAlpha(rText, nPos))
581
0
        {
582
0
            if (aCC.isUpper(rText, nPos))
583
0
                bCaseIsAlwaysUppercase = true;
584
0
            else
585
0
                return false;
586
0
        }
587
0
        rText.iterateCodePoints(&nPos);
588
0
    }
589
590
0
    return bCaseIsAlwaysUppercase;
591
0
}
592
593
CapType capitalType(const OUString& aTerm, CharClass const * pCC)
594
0
{
595
0
        sal_Int32 tlen = aTerm.getLength();
596
0
        if (!pCC || !tlen)
597
0
            return CapType::UNKNOWN;
598
599
0
        sal_Int32 nc = 0;
600
0
        for (sal_Int32 tindex = 0; tindex < tlen; ++tindex)
601
0
        {
602
0
            if (pCC->getCharacterType(aTerm,tindex) &
603
0
               css::i18n::KCharacterType::UPPER) nc++;
604
0
        }
605
606
0
        if (nc == 0)
607
0
            return CapType::NOCAP;
608
0
        if (nc == tlen)
609
0
            return CapType::ALLCAP;
610
0
        if ((nc == 1) && (pCC->getCharacterType(aTerm,0) &
611
0
              css::i18n::KCharacterType::UPPER))
612
0
            return CapType::INITCAP;
613
614
0
        return CapType::MIXED;
615
0
}
616
617
// sorted(!) array of unicode ranges for code points that are exclusively(!) used as numbers
618
// and thus may NOT not be part of names or words like the Chinese/Japanese number characters
619
const sal_uInt32 the_aDigitZeroes [] =
620
{
621
    0x00000030, //0039    ; Decimal # Nd  [10] DIGIT ZERO..DIGIT NINE
622
    0x00000660, //0669    ; Decimal # Nd  [10] ARABIC-INDIC DIGIT ZERO..ARABIC-INDIC DIGIT NINE
623
    0x000006F0, //06F9    ; Decimal # Nd  [10] EXTENDED ARABIC-INDIC DIGIT ZERO..EXTENDED ARABIC-INDIC DIGIT NINE
624
    0x000007C0, //07C9    ; Decimal # Nd  [10] NKO DIGIT ZERO..NKO DIGIT NINE
625
    0x00000966, //096F    ; Decimal # Nd  [10] DEVANAGARI DIGIT ZERO..DEVANAGARI DIGIT NINE
626
    0x000009E6, //09EF    ; Decimal # Nd  [10] BENGALI DIGIT ZERO..BENGALI DIGIT NINE
627
    0x00000A66, //0A6F    ; Decimal # Nd  [10] GURMUKHI DIGIT ZERO..GURMUKHI DIGIT NINE
628
    0x00000AE6, //0AEF    ; Decimal # Nd  [10] GUJARATI DIGIT ZERO..GUJARATI DIGIT NINE
629
    0x00000B66, //0B6F    ; Decimal # Nd  [10] ODIA DIGIT ZERO..ODIA DIGIT NINE
630
    0x00000BE6, //0BEF    ; Decimal # Nd  [10] TAMIL DIGIT ZERO..TAMIL DIGIT NINE
631
    0x00000C66, //0C6F    ; Decimal # Nd  [10] TELUGU DIGIT ZERO..TELUGU DIGIT NINE
632
    0x00000CE6, //0CEF    ; Decimal # Nd  [10] KANNADA DIGIT ZERO..KANNADA DIGIT NINE
633
    0x00000D66, //0D6F    ; Decimal # Nd  [10] MALAYALAM DIGIT ZERO..MALAYALAM DIGIT NINE
634
    0x00000E50, //0E59    ; Decimal # Nd  [10] THAI DIGIT ZERO..THAI DIGIT NINE
635
    0x00000ED0, //0ED9    ; Decimal # Nd  [10] LAO DIGIT ZERO..LAO DIGIT NINE
636
    0x00000F20, //0F29    ; Decimal # Nd  [10] TIBETAN DIGIT ZERO..TIBETAN DIGIT NINE
637
    0x00001040, //1049    ; Decimal # Nd  [10] MYANMAR DIGIT ZERO..MYANMAR DIGIT NINE
638
    0x00001090, //1099    ; Decimal # Nd  [10] MYANMAR SHAN DIGIT ZERO..MYANMAR SHAN DIGIT NINE
639
    0x000017E0, //17E9    ; Decimal # Nd  [10] KHMER DIGIT ZERO..KHMER DIGIT NINE
640
    0x00001810, //1819    ; Decimal # Nd  [10] MONGOLIAN DIGIT ZERO..MONGOLIAN DIGIT NINE
641
    0x00001946, //194F    ; Decimal # Nd  [10] LIMBU DIGIT ZERO..LIMBU DIGIT NINE
642
    0x000019D0, //19D9    ; Decimal # Nd  [10] NEW TAI LUE DIGIT ZERO..NEW TAI LUE DIGIT NINE
643
    0x00001B50, //1B59    ; Decimal # Nd  [10] BALINESE DIGIT ZERO..BALINESE DIGIT NINE
644
    0x00001BB0, //1BB9    ; Decimal # Nd  [10] SUNDANESE DIGIT ZERO..SUNDANESE DIGIT NINE
645
    0x00001C40, //1C49    ; Decimal # Nd  [10] LEPCHA DIGIT ZERO..LEPCHA DIGIT NINE
646
    0x00001C50, //1C59    ; Decimal # Nd  [10] OL CHIKI DIGIT ZERO..OL CHIKI DIGIT NINE
647
    0x0000A620, //A629    ; Decimal # Nd  [10] VAI DIGIT ZERO..VAI DIGIT NINE
648
    0x0000A8D0, //A8D9    ; Decimal # Nd  [10] SAURASHTRA DIGIT ZERO..SAURASHTRA DIGIT NINE
649
    0x0000A900, //A909    ; Decimal # Nd  [10] KAYAH LI DIGIT ZERO..KAYAH LI DIGIT NINE
650
    0x0000AA50, //AA59    ; Decimal # Nd  [10] CHAM DIGIT ZERO..CHAM DIGIT NINE
651
    0x0000FF10, //FF19    ; Decimal # Nd  [10] FULLWIDTH DIGIT ZERO..FULLWIDTH DIGIT NINE
652
    0x000104A0, //104A9   ; Decimal # Nd  [10] OSMANYA DIGIT ZERO..OSMANYA DIGIT NINE
653
    0x0001D7CE  //1D7FF   ; Decimal # Nd  [50] MATHEMATICAL BOLD DIGIT ZERO..MATHEMATICAL MONOSPACE DIGIT NINE
654
};
655
656
bool HasDigits( std::u16string_view rText )
657
0
{
658
0
    const sal_Int32 nLen = rText.size();
659
660
0
    sal_Int32 i = 0;
661
0
    while (i < nLen) // for all characters ...
662
0
    {
663
0
        const sal_uInt32 nCodePoint = o3tl::iterateCodePoints( rText, &i );    // handle unicode surrogates correctly...
664
0
        for (unsigned int nDigitZero : the_aDigitZeroes)   // ... check in all 0..9 ranges
665
0
        {
666
0
            if (nDigitZero > nCodePoint)
667
0
                break;
668
0
            if (/*nDigitZero <= nCodePoint &&*/ nCodePoint <= nDigitZero + 9)
669
0
                return true;
670
0
        }
671
0
    }
672
0
    return false;
673
0
}
674
675
bool IsNumeric( std::u16string_view rText )
676
0
{
677
0
    bool bRes = false;
678
0
    if (!rText.empty())
679
0
    {
680
0
        sal_Int32 nLen = rText.size();
681
0
        bRes = true;
682
0
        for(sal_Int32 i = 0; i < nLen; ++i)
683
0
        {
684
0
            sal_Unicode cChar = rText[ i ];
685
0
            if ( '0' > cChar  ||  cChar > '9' )
686
0
            {
687
0
                bRes = false;
688
0
                break;
689
0
            }
690
0
        }
691
0
    }
692
0
    return bRes;
693
0
}
694
695
uno::Reference< XLinguProperties > GetLinguProperties()
696
3
{
697
3
    return LinguProperties::create( comphelper::getProcessComponentContext() );
698
3
}
699
700
uno::Reference< XSearchableDictionaryList > GetDictionaryList()
701
0
{
702
0
    const uno::Reference< XComponentContext >& xContext( comphelper::getProcessComponentContext() );
703
0
    uno::Reference< XSearchableDictionaryList > xRef;
704
0
    try
705
0
    {
706
0
        xRef = DictionaryList::create(xContext);
707
0
    }
708
0
    catch (const uno::Exception &)
709
0
    {
710
0
        SAL_WARN( "linguistic", "createInstance failed" );
711
0
    }
712
713
0
    return xRef;
714
0
}
715
716
uno::Reference< XDictionary > GetIgnoreAllList()
717
0
{
718
0
    uno::Reference< XDictionary > xRes;
719
0
    uno::Reference< XSearchableDictionaryList > xDL( GetDictionaryList() );
720
0
    if (xDL.is())
721
0
    {
722
0
        const LanguageTag tag = comphelper::LibreOfficeKit::isActive()
723
0
                                    ? LanguageTag(u"en-US"_ustr)
724
0
                                    : SvtSysLocale().GetUILanguageTag();
725
0
        std::locale loc(Translate::Create("svt", tag));
726
0
        xRes = xDL->getDictionaryByName( Translate::get(STR_DESCRIPTION_IGNOREALLLIST, loc) );
727
0
    }
728
0
    return xRes;
729
0
}
730
731
AppExitListener::AppExitListener()
732
0
{
733
    // add object to Desktop EventListeners in order to properly call
734
    // the AtExit function at application exit.
735
0
    const uno::Reference< XComponentContext >& xContext( comphelper::getProcessComponentContext() );
736
737
0
    try
738
0
    {
739
0
        xDesktop = frame::Desktop::create(xContext);
740
0
    }
741
0
    catch (const uno::Exception &)
742
0
    {
743
0
        SAL_WARN( "linguistic", "createInstance failed" );
744
0
    }
745
0
}
746
747
AppExitListener::~AppExitListener()
748
0
{
749
0
}
750
751
void AppExitListener::Activate()
752
0
{
753
0
    if (xDesktop.is())
754
0
        xDesktop->addTerminateListener( this );
755
0
}
756
757
void AppExitListener::Deactivate()
758
0
{
759
0
    if (xDesktop.is())
760
0
        xDesktop->removeTerminateListener( this );
761
0
}
762
763
void SAL_CALL
764
    AppExitListener::disposing( const EventObject& rEvtSource )
765
0
{
766
0
    MutexGuard  aGuard( GetLinguMutex() );
767
768
0
    if (xDesktop.is()  &&  rEvtSource.Source == xDesktop)
769
0
    {
770
0
        xDesktop = nullptr;    //! release reference to desktop
771
0
    }
772
0
}
773
774
void SAL_CALL
775
    AppExitListener::queryTermination( const EventObject& /*rEvtSource*/ )
776
0
{
777
0
}
778
779
void SAL_CALL
780
    AppExitListener::notifyTermination( const EventObject& rEvtSource )
781
0
{
782
0
    MutexGuard  aGuard( GetLinguMutex() );
783
784
0
    if (xDesktop.is()  &&  rEvtSource.Source == xDesktop)
785
0
    {
786
0
        AtExit();
787
0
    }
788
0
}
789
790
}   // namespace linguistic
791
792
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */