-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspellutils.pas
More file actions
321 lines (274 loc) · 9.94 KB
/
Copy pathspellutils.pas
File metadata and controls
321 lines (274 loc) · 9.94 KB
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
//-----------------------------------------------------------------------------------
// RichKit Package © 2026 by Alexander Tverskoy
// Licensed under the MIT License
// You may obtain a copy of the License at https://opensource.org/licenses/MIT
//-----------------------------------------------------------------------------------
unit SpellUtils;
{$mode ObjFPC}{$H+}
interface
uses
Classes,
SysUtils,
Graphics,
RichMemo,
{$IFDEF WINDOWS}
WinSpellChecker,
{$ENDIF}
RichSpellChecker,
HunSpellChecker;
type
TSpell = class
public
// Checks the text of a RichMemo for spelling and/or grammar errors and applies wavy underlines (Windows only)
class function WinCheck(ARichMemo: TRichMemo; ASpellChecker: TRichSpellChecker; const ALanguage: string;
AOptions: TSpellCheckOptions = [scoSpelling]; AAddEmptySuggestions: boolean = True): boolean; static;
// Checks the text of a RichMemo using HunSpell engine and applies underlines
class function HunCheck(ARichMemo: TRichMemo; ASpellChecker: TRichSpellChecker; AHunSpellChecker: THunSpellChecker;
AOptions: TSpellCheckOptions = [scoSpelling]; AAddEmptySuggestions: boolean = True): boolean; static;
// Returns a list of all available spell checker language tags in BCP-47 format (Windows only)
class function WinSupportedLanguages: TStrings; static;
// Checks plain text with Windows engine and returns an array of spell error items
class function CheckText(const AText: string; const ALanguage: string; AOptions: TSpellCheckOptions = [scoSpelling];
AAddEmptySuggestions: boolean = True): RichSpellChecker.TSpellErrorArray; static;
// Checks plain text with HunSpell engine and returns an array of spell error items
class function HunCheckText(const AText: string; AHunSpellChecker: THunSpellChecker;
AOptions: TSpellCheckOptions = [scoSpelling]; AAddEmptySuggestions: boolean = True): RichSpellChecker.TSpellErrorArray; static;
// Applies a list of spell errors to the spell checker, replacing all existing underlines
class procedure ApplyErrors(ASpellChecker: TRichSpellChecker; const AErrors: RichSpellChecker.TSpellErrorArray); static;
end;
implementation
class function TSpell.WinCheck(ARichMemo: TRichMemo; ASpellChecker: TRichSpellChecker; const ALanguage: string;
AOptions: TSpellCheckOptions = [scoSpelling]; AAddEmptySuggestions: boolean = True): boolean;
{$IFDEF WINDOWS}
var
WideText: widestring;
Utf8Text: string;
WinErrors: WinSpellChecker.TSpellErrorArray = nil;
SuggestionList: array of string = nil;
i, j: integer;
StartPos, EndPos, ErrorLength: integer;
{$ENDIF}
begin
Result := False;
{$IFDEF WINDOWS}
if not Assigned(ARichMemo) or not Assigned(ASpellChecker) then
Exit;
// If no options selected, clear previous underlines and exit
if AOptions = [] then
begin
ASpellChecker.BeginUpdate;
try
ASpellChecker.Clear;
finally
ASpellChecker.EndUpdate;
end;
Exit;
end;
// Get the text and convert it to WideString for the spell checker
Utf8Text := ARichMemo.Text;
WideText := UTF8Decode(Utf8Text);
// Run the spell check with the specified options
if not CheckSpelling(WideText, ALanguage, WinErrors, AOptions) then
Exit;
// Clear previous underlines
ASpellChecker.BeginUpdate;
try
ASpellChecker.Clear;
// Add errors using UTF-8 character positions
for i := 0 to High(WinErrors) do
begin
StartPos := Utf16ToUtf8CharIndex(Utf8Text, WinErrors[i].Start);
EndPos := Utf16ToUtf8CharIndex(Utf8Text, WinErrors[i].Start + WinErrors[i].Length);
if (StartPos < 0) or (EndPos < StartPos) then
Continue;
ErrorLength := EndPos - StartPos;
// Convert suggestions from WideString to UTF-8 String
SetLength(SuggestionList, Length(WinErrors[i].Suggestions));
for j := 0 to High(WinErrors[i].Suggestions) do
SuggestionList[j] := UTF8Encode(WinErrors[i].Suggestions[j]);
// Add the error using UTF-8 character positions
if AAddEmptySuggestions or (Length(SuggestionList) > 0) then
begin
if WinErrors[i].ErrorType = setComprehensiveSpelling then
ASpellChecker.AddError(StartPos, ErrorLength, 'Comprehensive Spelling error', SuggestionList, clFuchsia)
else
ASpellChecker.AddError(StartPos, ErrorLength, 'Spelling error', SuggestionList, clRed);
end;
end;
Result := True;
finally
ASpellChecker.EndUpdate;
end;
{$ENDIF}
end;
class function TSpell.HunCheck(ARichMemo: TRichMemo; ASpellChecker: TRichSpellChecker; AHunSpellChecker: THunSpellChecker;
AOptions: TSpellCheckOptions = [scoSpelling]; AAddEmptySuggestions: boolean = True): boolean;
var
Utf8Text: string;
HunErrors: HunSpellChecker.TSpellErrorArray = nil;
SuggestionList: array of string = nil;
i, j: integer;
begin
Result := False;
if not Assigned(ARichMemo) or not Assigned(ASpellChecker) or not Assigned(AHunSpellChecker) then
Exit;
// If no options selected, clear previous underlines and exit
if AOptions = [] then
begin
ASpellChecker.BeginUpdate;
try
ASpellChecker.Clear;
finally
ASpellChecker.EndUpdate;
end;
Exit;
end;
// HunSpell only supports spelling, so if scoSpelling is not requested, clear and exit
if not (scoSpelling in AOptions) then
begin
ASpellChecker.BeginUpdate;
try
ASpellChecker.Clear;
finally
ASpellChecker.EndUpdate;
end;
Exit;
end;
Utf8Text := ARichMemo.Text;
// Run HunSpell check
HunErrors := AHunSpellChecker.CheckText(Utf8Text);
ASpellChecker.BeginUpdate;
try
ASpellChecker.Clear;
for i := 0 to High(HunErrors) do
begin
SetLength(SuggestionList, Length(HunErrors[i].Replacements));
for j := 0 to High(HunErrors[i].Replacements) do
SuggestionList[j] := HunErrors[i].Replacements[j];
if AAddEmptySuggestions or (Length(SuggestionList) > 0) then
ASpellChecker.AddError(
HunErrors[i].Offset,
HunErrors[i].Length,
HunErrors[i].Message,
SuggestionList,
HunErrors[i].Color);
end;
Result := True;
finally
ASpellChecker.EndUpdate;
end;
end;
class function TSpell.WinSupportedLanguages: TStrings;
{$IFDEF WINDOWS}
var
Langs: TSupportedLanguages;
i: integer;
LangList: TStringList = nil;
{$ENDIF}
begin
{$IFDEF WINDOWS}
LangList := TStringList.Create;
Langs := GetSupportedSpellCheckerLanguages;
for i := 0 to High(Langs) do
LangList.Add(UTF8Encode(Langs[i]));
Result := LangList;
{$ELSE}
Result := TStringList.Create; // Empty list on non-Windows platforms
{$ENDIF}
end;
class function TSpell.CheckText(const AText: string; const ALanguage: string; AOptions: TSpellCheckOptions = [scoSpelling];
AAddEmptySuggestions: boolean = True): RichSpellChecker.TSpellErrorArray;
{$IFDEF WINDOWS}
var
WideText: widestring;
WinErrors: WinSpellChecker.TSpellErrorArray = nil;
i, j: integer;
ResultIndex: integer;
StartPos, EndPos, ErrorLength: integer;
{$ENDIF}
begin
Result := nil; // Explicit initialization to suppress warning
{$IFDEF WINDOWS}
if AOptions = [] then
Exit;
WideText := UTF8Decode(AText);
if not CheckSpelling(WideText, ALanguage, WinErrors, AOptions) then
Exit;
SetLength(Result, 0); // Start with empty array
ResultIndex := 0;
for i := 0 to High(WinErrors) do
begin
StartPos := Utf16ToUtf8CharIndex(AText, WinErrors[i].Start);
EndPos := Utf16ToUtf8CharIndex(AText, WinErrors[i].Start + WinErrors[i].Length);
if (StartPos < 0) or (EndPos < StartPos) then
Continue;
ErrorLength := EndPos - StartPos;
// If empty suggestions are not allowed and the list is empty, skip this error
if not AAddEmptySuggestions and (Length(WinErrors[i].Suggestions) = 0) then
Continue;
// Increase result array size and fill the new item
Inc(ResultIndex);
SetLength(Result, ResultIndex);
Result[ResultIndex - 1].Offset := StartPos;
Result[ResultIndex - 1].Length := ErrorLength;
if WinErrors[i].ErrorType = setComprehensiveSpelling then
begin
Result[ResultIndex - 1].Message := 'Comprehensive Spelling error';
Result[ResultIndex - 1].Color := clFuchsia;
end
else
begin
Result[ResultIndex - 1].Message := 'Spelling error';
Result[ResultIndex - 1].Color := clRed;
end;
// Copy suggestions from WideString to UTF-8 String
SetLength(Result[ResultIndex - 1].Replacements, Length(WinErrors[i].Suggestions));
for j := 0 to High(WinErrors[i].Suggestions) do
Result[ResultIndex - 1].Replacements[j] := UTF8Encode(WinErrors[i].Suggestions[j]);
end;
{$ENDIF}
end;
class function TSpell.HunCheckText(const AText: string; AHunSpellChecker: THunSpellChecker;
AOptions: TSpellCheckOptions = [scoSpelling]; AAddEmptySuggestions: boolean = True): RichSpellChecker.TSpellErrorArray;
var
HunErrors: HunSpellChecker.TSpellErrorArray = nil;
i: integer;
begin
Result := nil;
if not Assigned(AHunSpellChecker) then Exit;
if AOptions = [] then Exit;
if not (scoSpelling in AOptions) then Exit;
HunErrors := AHunSpellChecker.CheckText(AText);
SetLength(Result, Length(HunErrors));
for i := 0 to High(HunErrors) do
begin
if not AAddEmptySuggestions and (Length(HunErrors[i].Replacements) = 0) then
Continue;
Result[i].Offset := HunErrors[i].Offset;
Result[i].Length := HunErrors[i].Length;
Result[i].Message := HunErrors[i].Message;
Result[i].Replacements := HunErrors[i].Replacements;
Result[i].Color := HunErrors[i].Color;
end;
end;
class procedure TSpell.ApplyErrors(ASpellChecker: TRichSpellChecker; const AErrors: RichSpellChecker.TSpellErrorArray);
var
i: integer;
begin
if not Assigned(ASpellChecker) then
Exit;
ASpellChecker.BeginUpdate;
try
ASpellChecker.Clear;
for i := 0 to High(AErrors) do
ASpellChecker.AddError(
AErrors[i].Offset,
AErrors[i].Length,
AErrors[i].Message,
AErrors[i].Replacements,
AErrors[i].Color);
finally
ASpellChecker.EndUpdate;
end;
end;
end.