1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <spellcheckcontext.hxx>
#include <svl/sharedstring.hxx>
#include <editeng/eeitem.hxx>
#include <editeng/langitem.hxx>
#include <editeng/unolingu.hxx>
#include <scitems.hxx>
#include <document.hxx>
#include <cellvalue.hxx>
#include <editutil.hxx>
#include <dpobject.hxx>
#include <com/sun/star/linguistic2/XSpellChecker1.hpp>
#include <o3tl/hash_combine.hxx>
#include <unordered_map>
using namespace css;
using sc::SpellCheckContext;
class SpellCheckContext::SpellCheckCache
{
struct CellPos
{
struct Hash
{
size_t operator() (const CellPos& rPos) const
{
std::size_t seed = 0;
o3tl::hash_combine(seed, rPos.mnCol);
o3tl::hash_combine(seed, rPos.mnRow);
return seed;
}
};
SCCOL mnCol;
SCROW mnRow;
CellPos(SCCOL nCol, SCROW nRow) : mnCol(nCol), mnRow(nRow) {}
bool operator== (const CellPos& r) const
{
return mnCol == r.mnCol && mnRow == r.mnRow;
}
};
typedef std::vector<editeng::MisspellRanges> MisspellType;
typedef std::unordered_map<CellPos, std::unique_ptr<MisspellType>, CellPos::Hash> CellMapType;
typedef std::unordered_map<const rtl_uString*, std::unique_ptr<MisspellType>> SharedStringMapType;
typedef std::unordered_map<CellPos, LanguageType, CellPos::Hash> CellLangMapType;
SharedStringMapType maStringMisspells;
CellMapType maEditTextMisspells;
CellLangMapType maCellLanguages;
LanguageType meDefCellLanguage;
public:
SpellCheckCache(LanguageType eDefaultCellLanguage) : meDefCellLanguage(eDefaultCellLanguage)
{
}
bool query(SCCOL nCol, SCROW nRow, const ScRefCellValue& rCell, MisspellType*& rpRanges) const
{
CellType eType = rCell.getType();
if (eType == CELLTYPE_STRING)
{
SharedStringMapType::const_iterator it = maStringMisspells.find(rCell.getSharedString()->getData());
if (it == maStringMisspells.end())
return false; // Not available
rpRanges = it->second.get();
return true;
}
if (eType == CELLTYPE_EDIT)
{
CellMapType::const_iterator it = maEditTextMisspells.find(CellPos(nCol, nRow));
if (it == maEditTextMisspells.end())
return false; // Not available
rpRanges = it->second.get();
return true;
}
rpRanges = nullptr;
return true;
}
void set(SCCOL nCol, SCROW nRow, const ScRefCellValue& rCell, std::unique_ptr<MisspellType> pRanges)
{
CellType eType = rCell.getType();
if (eType == CELLTYPE_STRING)
maStringMisspells.insert_or_assign(rCell.getSharedString()->getData(), std::move(pRanges));
else if (eType == CELLTYPE_EDIT)
maEditTextMisspells.insert_or_assign(CellPos(nCol, nRow), std::move(pRanges));
}
LanguageType getLanguage(SCCOL nCol, SCROW nRow) const
{
CellLangMapType::const_iterator it = maCellLanguages.find(CellPos(nCol, nRow));
if (it == maCellLanguages.end())
return meDefCellLanguage;
return it->second;
}
void setLanguage(LanguageType eCellLang, SCCOL nCol, SCROW nRow)
{
if (eCellLang == meDefCellLanguage)
maCellLanguages.erase(CellPos(nCol, nRow));
else
maCellLanguages.insert_or_assign(CellPos(nCol, nRow), eCellLang);
}
void clear(LanguageType eDefaultCellLanguage)
{
maStringMisspells.clear();
maEditTextMisspells.clear();
maCellLanguages.clear();
meDefCellLanguage = eDefaultCellLanguage;
}
void clearEditTextMap()
{
maEditTextMisspells.clear();
}
};
struct SpellCheckContext::SpellCheckStatus
{
bool mbModified;
SpellCheckStatus() : mbModified(false) {};
DECL_LINK( EventHdl, EditStatus&, void );
};
IMPL_LINK(SpellCheckContext::SpellCheckStatus, EventHdl, EditStatus&, rStatus, void)
{
EditStatusFlags nStatus = rStatus.GetStatusWord();
if (nStatus & EditStatusFlags::WRONGWORDCHANGED)
mbModified = true;
}
struct SpellCheckContext::SpellCheckResult
{
SCCOL mnCol;
SCROW mnRow;
const std::vector<editeng::MisspellRanges>* pRanges;
SpellCheckResult() : mnCol(-1), mnRow(-1), pRanges(nullptr) {}
void set(SCCOL nCol, SCROW nRow, const std::vector<editeng::MisspellRanges>* pMisspells)
{
mnCol = nCol;
mnRow = nRow;
pRanges = pMisspells;
}
const std::vector<editeng::MisspellRanges>* query(SCCOL nCol, SCROW nRow) const
{
assert(mnCol == nCol);
assert(mnRow == nRow);
(void)nCol;
(void)nRow;
return pRanges;
}
void clear()
{
mnCol = -1;
mnRow = -1;
pRanges = nullptr;
}
};
SpellCheckContext::SpellCheckContext(ScDocument* pDocument, SCTAB nTab) :
pDoc(pDocument),
mnTab(nTab),
meLanguage(ScGlobal::GetEditDefaultLanguage())
{
// defer init of engine and cache till the first query/set
}
SpellCheckContext::~SpellCheckContext()
{
}
void SpellCheckContext::dispose()
{
mpEngine.reset();
mpCache.reset();
pDoc = nullptr;
}
void SpellCheckContext::setTabNo(SCTAB nTab)
{
if (mnTab == nTab)
return;
mnTab = nTab;
reset();
}
bool SpellCheckContext::isMisspelled(SCCOL nCol, SCROW nRow) const
{
const_cast<SpellCheckContext*>(this)->ensureResults(nCol, nRow);
return mpResult->query(nCol, nRow);
}
const std::vector<editeng::MisspellRanges>* SpellCheckContext::getMisspellRanges(
SCCOL nCol, SCROW nRow ) const
{
const_cast<SpellCheckContext*>(this)->ensureResults(nCol, nRow);
return mpResult->query(nCol, nRow);
}
void SpellCheckContext::setMisspellRanges(
SCCOL nCol, SCROW nRow, const std::vector<editeng::MisspellRanges>* pRanges )
{
if (!mpEngine || !mpCache)
reset();
ScRefCellValue aCell(*pDoc, ScAddress(nCol, nRow, mnTab));
CellType eType = aCell.getType();
if (eType != CELLTYPE_STRING && eType != CELLTYPE_EDIT)
return;
typedef std::vector<editeng::MisspellRanges> MisspellType;
std::unique_ptr<MisspellType> pMisspells(pRanges ? new MisspellType(*pRanges) : nullptr);
mpCache->set(nCol, nRow, aCell, std::move(pMisspells));
}
void SpellCheckContext::reset()
{
meLanguage = ScGlobal::GetEditDefaultLanguage();
resetCache();
mpEngine.reset();
mpStatus.reset();
}
void SpellCheckContext::resetForContentChange()
{
resetCache(true /* bContentChangeOnly */);
}
void SpellCheckContext::ensureResults(SCCOL nCol, SCROW nRow)
{
if (!mpEngine || !mpCache ||
ScGlobal::GetEditDefaultLanguage() != meLanguage)
{
reset();
setup();
}
// perhaps compute the pivot rangelist once in some pivot-table change handler ?
if (pDoc->HasPivotTable())
{
if (ScDPCollection* pDPs = pDoc->GetDPCollection())
{
ScRangeList aPivotRanges = pDPs->GetAllTableRanges(mnTab);
if (aPivotRanges.Contains(ScAddress(nCol, nRow, mnTab))) // Don't spell check within pivot tables
{
mpResult->set(nCol, nRow, nullptr);
return;
}
}
}
ScRefCellValue aCell(*pDoc, ScAddress(nCol, nRow, mnTab));
CellType eType = aCell.getType();
if (eType != CELLTYPE_STRING && eType != CELLTYPE_EDIT)
{
// No spell-check required.
mpResult->set(nCol, nRow, nullptr);
return;
}
// Cell content is either shared-string or EditTextObject
// For spell-checking, we currently only use the primary
// language; not CJK nor CTL.
const ScPatternAttr* pPattern = pDoc->GetPattern(nCol, nRow, mnTab);
LanguageType eCellLang = pPattern->GetItem(ATTR_FONT_LANGUAGE).GetValue();
if (eCellLang == LANGUAGE_SYSTEM)
eCellLang = meLanguage; // never use SYSTEM for spelling
if (eCellLang == LANGUAGE_NONE)
{
mpResult->set(nCol, nRow, nullptr); // No need to spell check this cell.
return;
}
typedef std::vector<editeng::MisspellRanges> MisspellType;
LanguageType eCachedCellLang = mpCache->getLanguage(nCol, nRow);
if (eCellLang != eCachedCellLang)
mpCache->setLanguage(eCellLang, nCol, nRow);
else
{
MisspellType* pRanges = nullptr;
bool bFound = mpCache->query(nCol, nRow, aCell, pRanges);
if (bFound)
{
// Cache hit.
mpResult->set(nCol, nRow, pRanges);
return;
}
}
// Cache miss, the cell needs spell-check..
if (eType == CELLTYPE_STRING)
mpEngine->SetText(aCell.getSharedString()->getString());
else
mpEngine->SetText(*aCell.getEditText());
// it has to happen after we set text
mpEngine->SetDefaultItem(SvxLanguageItem(eCellLang, EE_CHAR_LANGUAGE));
mpStatus->mbModified = false;
mpEngine->CompleteOnlineSpelling();
std::unique_ptr<MisspellType> pRanges;
if (mpStatus->mbModified)
{
pRanges.reset(new MisspellType);
mpEngine->GetAllMisspellRanges(*pRanges);
if (pRanges->empty())
pRanges.reset(nullptr);
}
// else : No change in status for EditStatusFlags::WRONGWORDCHANGED => no spell errors (which is the default status).
mpResult->set(nCol, nRow, pRanges.get());
mpCache->set(nCol, nRow, aCell, std::move(pRanges));
}
void SpellCheckContext::resetCache(bool bContentChangeOnly)
{
if (!mpResult)
mpResult.reset(new SpellCheckResult());
else
mpResult->clear();
if (!mpCache)
mpCache.reset(new SpellCheckCache(meLanguage));
else if (bContentChangeOnly)
mpCache->clearEditTextMap();
else
mpCache->clear(meLanguage);
}
void SpellCheckContext::setup()
{
mpEngine.reset(new ScTabEditEngine(pDoc));
mpStatus.reset(new SpellCheckStatus());
mpEngine->SetControlWord(
mpEngine->GetControlWord() | (EEControlBits::ONLINESPELLING | EEControlBits::ALLOWBIGOBJS));
mpEngine->SetStatusEventHdl(LINK(mpStatus.get(), SpellCheckStatus, EventHdl));
// Delimiters here like in inputhdl.cxx !!!
mpEngine->SetWordDelimiters(
ScEditUtil::ModifyDelimiters(mpEngine->GetWordDelimiters()));
uno::Reference<linguistic2::XSpellChecker1> xXSpellChecker1(LinguMgr::GetSpellChecker());
mpEngine->SetSpeller(xXSpellChecker1);
mpEngine->SetDefaultLanguage(meLanguage);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|