-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpellers.pas
2599 lines (2468 loc) · 78.6 KB
/
Spellers.pas
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit Spellers;
interface
uses
Windows, Messages, SysUtils, Graphics, Controls, Forms, Dialogs,
ComCtrls, RichEdit, StdCtrls, Math, Langs, Classes, IniFiles;
type
TSpellOption = (spoIgnoreMixedCaps,spoSuggestFromUserDict, spoIgnoreAllCaps, spoIgnoreMixedDigits,
spoIgnoreRomanNumerals, spoFindUncappedSentences,
spoFindMissingSpaces, spoFindRepeatWord, spoFindExtraSpaces,
spoFindSpacesBeforePunc, spoFindSpacesAfterPunc, spoRateSuggestions,
spoFindInitialNumerals);
TSpellOptions = set of TSpellOption;
TSpellerType = (sptMSOffice, sptISpell);
TSpellListObj = class
ISpellCmd,ISpellCharset,ISpellSurrogate,Flag: String;
Language: TLanguage;
SpellerType: TSpellerType;
end;
TUserLanguage = (ulEnglish, ulOwn, ulOther);
TSpellCommand = (scVerifyWord, scVerifyBuffer, scSuggest, scSuggestMore,
scHyphInfo, scWildCard, scAnagram);
TSpellReturnCode = (srNoErrors, srUnknownInputWord, srReturningChangeAlways,
srReturningChangeOnce, srInvalidHyphenation,
srErrorCapitalization, srWordConsideredAbbreviation,
srHyphChangesSpelling, srNoMoreSuggestions,
srMoreInfoThanBufferCouldHold, srNoSentenceStartCap,
srRepeatWord, srExtraSpaces, srMissingSpace,
srInitialNumeral);
TMisspellFont = class(TPersistent)
private
FMspName: TFontName;
FMspColor: TColor;
FMspStyle: TFontStyles;
public
procedure Assign(Source: TPersistent); override;
published
property MspName: TFontName read FMspName write FMspName;
property MspColor: TColor read FMspColor write FMspColor;
property MspStyle: TFontStyles read FMspStyle write FMspStyle;
end;
TMisspellEvent = procedure (Sender: TObject; SRC: TSpellReturnCode;
BufPos, Len: Integer) of object;
TChangeTextEvent = procedure(Sender: TObject; BufPos, Len: Integer;
NewWord: String) of object;
TGetDictEvent = procedure(Sender: TObject; Language: TLanguage;
var Dict: TFileName) of object;
TSpellerDialog2 = class;
TAbstractSpeller = class;
{$IFDEF VER130} { Borland Delphi 5.x }
UTF8String = type string;
{$ENDIF}
{ TSpellChecker }
TSpellChecker = class(TComponent)
private
{ Private declarations }
FMemo,
FBackMemo: TCustomMemo;
FMemoRichEd: Boolean;
FOptions: TSpellOptions;
FLanguage: TLanguage;
FLangOption: TLangOption;
FSpellerType: TSpellerType;
FISpellCmd,
FISpellCharset,
FISpellSurrogate,
FFlag,
FLangName: String;
FActiveLanguage: Boolean;
FDialog: TSpellerDialog2;
FModalDialog: Boolean;
FHTML: Boolean;
FCustomDict: TFileName;
FCaption: TCaption;
FFont: TFont;
FMissFont: TMisspellFont;
Spellers: TList;
CRPos,
TagPos,
LangPos: Integer;
FStartSentence: Boolean;
FSpellStart,
FSpellEnd: Integer;
FSpeller: TAbstractSpeller;
FMisspellStart,
FMisspellLen: Integer;
FMisspellText: String;
FLangSupport: Boolean;
FUnicode: Boolean;
FSRC: TSpellReturnCode;
FFinishMessage: String;
FUserLanguage: TUserLanguage;
FShowFinishMessage: Boolean;
FOnMisspell: TMisspellEvent;
FOnChangeText: TChangeTextEvent;
FOnFinished: TNotifyEvent;
FOnCancel: TNotifyEvent;
FCancelled: Boolean;
FOnGetDict: TGetDictEvent;
protected
{ Protected declarations }
procedure SetLanguage(Value: TLanguage);
procedure SetSpellerType(Value: TSpellerType);
procedure SetUserLanguage(Value: TUserLanguage);
procedure SetFont(Value: TFont);
procedure SetMissFont(Value: TMisspellFont);
procedure ChangeOnce(Word1: String);
procedure Change(Word1: String);
procedure ChangeAlways(Word1: String);
procedure Delete;
procedure Add;
procedure IgnoreAlways;
function OpenLanguage(Value: TLanguage; SpType: TSpellerType): Boolean;
function FindLanguage(Value: TLanguage; SpType: TSpellerType): TAbstractSpeller;
procedure Init;
procedure GetBlock(From: Integer; var StartPos, EndPos: Integer);
function GetMemoLanguage: TLanguage;
procedure GetTag(From: Integer; var Len: Integer);
procedure GetTextRange(Buf: PChar; StartPos, EndPos: Integer; CP: Word);
function SentenceCapitalize(const S: String): String;
procedure ContinueCheck;
procedure FinishCheck;
function GetLineFromPos(Pos: Integer; var LineStart: Integer): String;
procedure GetMemoProperties;
function GetCurrentLanguage: TLanguage;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
class procedure RegisterEditControl(MemoClass: String; Unicode, Multilanguage: Boolean);
procedure Check(Memo: TCustomMemo);
function IsKnownWord(Word: String; Language: TLanguage): Boolean;
procedure AddWord(Word: String; Language: TLanguage);
procedure GetVariants(Word: String; Variants: TStrings; Language: TLanguage);
procedure SetFontDefault; virtual;
property CurrentLanguage: TLanguage read GetCurrentLanguage;
property ISpellCmd: String read FISpellCmd write FISpellCmd;
property ISpellCharset: String read FISpellCharset write FISpellCharset;
property ISpellSurrogate: String read FISpellSurrogate write FISpellSurrogate;
property Flag: String read FFlag write FFlag;
property LangName: String read FLangName write FLangName;
property ActiveLanguage: Boolean read FActiveLanguage write FActiveLanguage;
published
{ Published declarations }
property Language: TLanguage read FLanguage write SetLanguage;
property LangOption: TLangOption read FLangOption write FLangOption
default loLocalized;
property SpellerType: TSpellerType read FSpellerType write SetSpellerType;
property UserLanguage: TUserLanguage read FUserLanguage write SetUserLanguage;
property Options: TSpellOptions read FOptions write FOptions;
property OnMisspelling: TMisspellEvent read FOnMisspell write FOnMisspell;
property OnChangeText: TChangeTextEvent read FOnChangeText write FOnChangeText;
property OnFinished: TNotifyEvent read FOnFinished write FOnFinished;
property OnCancel: TNotifyEvent read FOnCancel write FOnCancel;
property OnGetDictionary: TGetDictEvent read FOnGetDict write FOnGetDict;
property Caption: TCaption read FCaption write FCaption;
property Font: TFont read FFont write SetFont;
property MisspellFont: TMisspellFont read FMissFont write SetMissFont;
property ModalDialog: Boolean read FModalDialog write FModalDialog;
property HTMLSupport: Boolean read FHTML write FHTML default False;
property CustomDict: TFileName read FCustomDict write FCustomDict;
property FinishMessage: String read FFinishMessage write FFinishMessage;
property ShowFinishMessage: Boolean read FShowFinishMessage write FShowFinishMessage;
end;
TAbstractSpeller = class(TObject)
FLanguage: TLanguage;
FSpellerType: TSpellerType;
FISpellCmd,
FISpellCharset,
FISpellSurrogate,
FFlag,
FLangName: String;
FOptions: TSpellOptions;
SpellChecker: TSpellChecker;
FNotActive: Boolean;
constructor Create(Language: TLanguage; Owner: TSpellChecker; Options: TSpellOptions); virtual;
function FindMisspell(Buf: PChar; MaxLen: Integer; var Start, Len: Integer): TSpellReturnCode; virtual; abstract;
function FindNextMisspell(Buf: PChar; MaxLen: Integer; var Start, Len: Integer): TSpellReturnCode; virtual; abstract;
procedure ChangeOnce(Word, NewWord: String); virtual; abstract;
procedure ChangeAlways(Word, NewWord: String); virtual; abstract;
procedure Add(Word: String); virtual; abstract;
procedure IgnoreAlways(Word: String); virtual; abstract;
procedure GetVariants(Word: String; Variants: TStrings); virtual; abstract;
property Language: TLanguage read FLanguage;
property SpellerType: TSpellerType read FSpellerType;
property ISpellCmd: String read FISpellCmd;
property ISpellCharset: String read FISpellCharset;
property ISpellSurrogate: String read FISpellSurrogate;
property Flag: String read FFlag;
property LangName: String read FLangName;
property Options: TSpellOptions read FOptions;
function GetChangeText: String; virtual; abstract;
function GetMisspellText: String; virtual; abstract;
property ChangeText: String read GetChangeText;
property MisspellText: String read GetMisspellText;
property NotActive: Boolean read FNotActive;
end;
TSpellerClass = class of TAbstractSpeller;
ESpellError = class(Exception);
TSpellerDialog2 = class(TForm)
InfoMsg: TLabel;
Misspelling: TRichEdit;
Label2: TLabel;
Variants: TListBox;
ChangeButton: TButton;
ChangeAllButton: TButton;
SkipButton: TButton;
SkipAllButton: TButton;
AddButton: TButton;
CancelButton: TButton;
CancelEdit: TButton;
DelButton: TButton;
StartButton: TButton;
procedure DelButtonClick(Sender: TObject);
procedure SkipButtonClick(Sender: TObject);
procedure SkipAllButtonClick(Sender: TObject);
procedure AddButtonClick(Sender: TObject);
procedure ChangeButtonClick(Sender: TObject);
procedure ChangeAllButtonClick(Sender: TObject);
procedure MisspellingProtectChange(Sender: TObject; StartPos,
EndPos: Integer; var AllowChange: Boolean);
procedure MisspellingChange(Sender: TObject);
procedure CancelEditClick(Sender: TObject);
procedure CancelButtonClick(Sender: TObject);
procedure StartButtonClick(Sender: TObject);
procedure FormDeactivate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
Checker: TSpellChecker;
FShowing: Boolean;
procedure ShowForChange(Msg: TCaption);
procedure ShowForDelete;
procedure ShowForEdit(Msg: TCaption);
procedure ShowMisspelling;
procedure GetHotArea(var SS, SL: Integer);
public
{ Public declarations }
constructor Create(SpellChecker: TSpellChecker); reintroduce; overload;
end;
const
langAfrikaans = TLanguage(1078);
langAlbanian = TLanguage(1052);
langArabic = TLanguage(1025);
langBasque = TLanguage(1069);
langBelgianDutch = TLanguage(2067);
langBelgianFrench = TLanguage(2060);
langBrazilianPortuguese = TLanguage(1046);
langBulgarian = TLanguage(1026);
langByelorussian = TLanguage(1059);
langCatalan = TLanguage(1027);
langCroatian = TLanguage(1050);
langCzech = TLanguage(1029);
langDanish = TLanguage(1030);
langDutch = TLanguage(1043);
langEnglishAUS = TLanguage(3081);
langEnglishCanadian = TLanguage(4105);
langEnglishNewZealand = TLanguage(5129);
langEnglishSouthAfrica = TLanguage(7177);
langEnglishUK = TLanguage(2057);
langEnglishUS = TLanguage(1033);
langEstonian = TLanguage(1061);
langFaeroese = TLanguage(1080);
langFarsi = TLanguage(1065);
langFinnish = TLanguage(1035);
langFinnishSwedish = TLanguage(2077);
langFrench = TLanguage(1036);
langFrenchCanadian = TLanguage(3084);
langGerman = TLanguage(1031);
langGreek = TLanguage(1032);
langHebrew = TLanguage(1037);
langHungarian = TLanguage(1038);
langItalian = TLanguage(1040);
langIcelandic = TLanguage(1039);
langIndonesian = TLanguage(1057);
langJapanese = TLanguage(1041);
langKorean = TLanguage(1042);
langLatvian = TLanguage(1062);
langLithuanian = TLanguage(1063);
langMacedonian = TLanguage(1071);
langMalaysian = TLanguage(1086);
langMexicanSpanish = TLanguage(2058);
langNorwegianBokmol = TLanguage(1044);
langNorwegianNynorsk = TLanguage(2068);
langPolish = TLanguage(1045);
langPortuguese = TLanguage(2070);
langRomanian = TLanguage(1048);
langRussian = TLanguage(1049);
langSerbianCyrillic = TLanguage(3098);
langSerbianLatin = TLanguage(2074);
langSesotho = TLanguage(1072);
langSimplifiedChinese = TLanguage(2052);
langSlovak = TLanguage(1051);
langSlovenian = TLanguage(1060);
langSpanish = TLanguage(1034);
langSpanishModernSort = TLanguage(3082);
langSwedish = TLanguage(1053);
langSwissFrench = TLanguage(4108);
langSwissGerman = TLanguage(2055);
langSwissItalian = TLanguage(2064);
langThai = TLanguage(1054);
langTraditionalChinese = TLanguage(1028);
langTsonga = TLanguage(1073);
langTswana = TLanguage(1074);
langTurkish = TLanguage(1055);
langUkrainian = TLanguage(1058);
langVenda = TLanguage(1075);
langVietnamese = TLanguage(1066);
langXhosa = TLanguage(1076);
langZulu = TLanguage(1077);
var
spOVariants, spODelete, spOChange, spOChangeAll, spOSkip, spOSkipAll, spOAdd,
spOCancel, spOCancelEdit, spONotFound, spOHyphen, spOCaps, spOAbbrev,
spONoSentenceCap, spOExtraSpaces, spOMissingSpace, spOInitialNumeral,
spORepeatedWord, spOFinish, spOFinishCaption, spOCaption, spOError,
spOErrorLoad, spOErrorUnload, spOErrorNoSpellChecker, spOStart: String;
WinNT: Boolean;
function GetSpellLanguages(Languages: TStrings; Option: TLangOption): Integer;
function GetISpellLanguages(Languages: TStrings; Option: TLangOption): Integer;
function SptToStr(Value: TSpellerType): String;
implementation
uses
CSAPI, SpellRes, SpellResDe, Registry, ISpell;
{$R *.DFM}
var
spVariants, spDelete, spChange, spChangeAll, spSkip, spSkipAll,
spAdd, spCancel, spCancelEdit, spNotFound, spHyphen, spCaps,
spAbbrev, spNoSentenceCap, spExtraSpaces, spMissingSpace,
spInitialNumeral, spRepeatedWord, spFinish, spFinishCaption,
spCaption, spError, spErrorLoad, spErrorUnload, spErrorNoSpellChecker,
spStart: String;
type
TMemoClass = class of TCustomMemo;
{TAbstractSpeller}
constructor TAbstractSpeller.Create(Language: TLanguage; Owner: TSpellChecker; Options: TSpellOptions);
begin
inherited Create;
FLanguage:= Language;
FOptions:= Options;
SpellChecker:= Owner;
end;
procedure CheckSR(SR: TSEC);
begin
if (SR<>secNoErrors) then
raise ESpellError.CreateFmt(spError, [SR]);
end;
{TCSAPISpeller}
type
TCSAPISpeller = class(TAbstractSpeller)
private
SpellInstance: THandle;
DLLName: String;
LexName: String;
UserDict: TFileName;
UnkWord: String;
FOptions: TSpellOptions;
SpellVer: TSpellVerFunc;
SpellInit: TSpellInitFunc;
SpellOptions: TSpellOptionsFunc;
SpellCheck: TSpellCheckFunc;
SpellTerminate: TSpellTerminateFunc;
SpellVerifyMdr: TSpellVerifyMdrFunc;
SpellOpenMdr: TSpellOpenMdrFunc;
SpellOpenUdr: TSpellOpenUdrFunc;
SpellAddUdr: TSpellAddUdrFunc;
SpellAddChangeUdr: TSpellAddChangeUdrFunc;
SpellDelUdr: TSpellDelUdrFunc;
SpellClearUdr: TSpellClearUdrFunc;
SpellGetSizeUdr: TSpellGetSizeUdrFunc;
SpellGetListUdr: TSpellGetListUdrFunc;
SpellCloseMdr: TSpellCloseMdrFunc;
SpellCloseUdr: TSpellCloseUdrFunc;
protected
Handle: TSPLID;
SpecChars: TWSC;
Mdrs: TMDRS;
Udr: TUDR;
InputBuffer: TSIB;
ResultBuffer: TSRB;
constructor Create(Language: TLanguage; Owner: TSpellChecker; Options: TSpellOptions); override;
destructor Destroy; override;
function FindMisspell(Buf: PChar; MaxLen: Integer; var Start, Len: Integer): TSpellReturnCode; override;
function FindNextMisspell(Buf: PChar; MaxLen: Integer; var Start, Len: Integer): TSpellReturnCode; override;
procedure ChangeOnce(Word, NewWord: String); override;
procedure ChangeAlways(Word, NewWord: String); override;
procedure Add(Word: String); override;
procedure IgnoreAlways(Word: String); override;
procedure GetVariants(Word: String; Variants: TStrings); override;
function GetChangeText: String; override;
function GetMisspellText: String; override;
end;
{$O-}
constructor TCSAPISpeller.Create(Language: TLanguage; Owner: TSpellChecker; Options: TSpellOptions);
var
UdrRO: Boolean;
NotFound: Boolean;
Registry: TRegistry;
begin
inherited;
FSpellerType := sptMSOffice;
with SpecChars do
begin
bIgnore:= #0;
bHyphenHard:= #45;
bHyphenSoft:= #31;
bHyphenNonBreaking:= #30;
bEmDash:= #151;
bEnDash:= #150;
bEllipsis:= #133;
rgLineBreak:= #11#10;
rgParaBreak:= #13#10;
end;
Registry:= TRegistry.Create;
Registry.RootKey:= HKEY_LOCAL_MACHINE;
try
NotFound:= True;
if Registry.OpenKeyReadOnly(
Format('\SOFTWARE\Microsoft\Shared Tools\Proofing Tools\Spelling\%d\Normal', [FLanguage])) or
Registry.OpenKeyReadOnly(
Format('\SOFTWARE\Microsoft\Shared Tools\Proofing Tools\Spelling\%d\Normal', [1024+(FLanguage mod 1024)]))
then begin
DLLName:= Registry.ReadString('Engine');
LexName:= Registry.ReadString('Dictionary');
NotFound := False;
end;
if not NotFound then begin
if (SpellChecker.CustomDict='') and Registry.OpenKeyReadOnly(
'\SOFTWARE\Microsoft\Shared Tools\Proofing Tools\Custom Dictionaries')
then UserDict:= Registry.ReadString('1')
else begin
UserDict:= SpellChecker.CustomDict;
if (SpellChecker.CustomDict='') then begin
try
Registry.Access := KEY_ALL_ACCESS;
Registry.OpenKey(
'\SOFTWARE\Microsoft\Shared Tools\Proofing Tools\Custom Dictionaries', True)
except on E: Exception do
Registry.Access := KEY_READ;
end;
end;
end;
if Assigned(SpellChecker.FOnGetDict)
then SpellChecker.FOnGetDict(SpellChecker, FLanguage, UserDict);
if UserDict='' then
begin
UserDict:= ExtractFilePath(LexName)+'CUSTOM.DIC';
try
Registry.WriteString('1', UserDict);
Registry.CloseKey;
except end;
end;
end;
finally
Registry.Free;
end;
if NotFound then
begin
FNotActive:= True;
Exit;
end;
try
SpellInstance:= LoadLibrary(PChar(DllName));
except
FNotActive:= True;
raise ESpellError.CreateFmt(spErrorLoad, [DllName]);
end;
try
@SpellVer:= GetProcAddress(SpellInstance, 'SpellVer');
@SpellInit:= GetProcAddress(SpellInstance, 'SpellInit');
@SpellOptions:= GetProcAddress(SpellInstance, 'SpellOptions');
@SpellCheck:= GetProcAddress(SpellInstance, 'SpellCheck');
@SpellTerminate:= GetProcAddress(SpellInstance, 'SpellTerminate');
@SpellVerifyMdr:= GetProcAddress(SpellInstance, 'SpellVerifyMdr');
@SpellOpenMdr:= GetProcAddress(SpellInstance, 'SpellOpenMdr');
@SpellOpenUdr:= GetProcAddress(SpellInstance, 'SpellOpenUdr');
@SpellAddUdr:= GetProcAddress(SpellInstance, 'SpellAddUdr');
@SpellAddChangeUdr:= GetProcAddress(SpellInstance, 'SpellAddChangeUdr');
@SpellDelUdr:= GetProcAddress(SpellInstance, 'SpellDelUdr');
@SpellClearUdr:= GetProcAddress(SpellInstance, 'SpellClearUdr');
@SpellGetSizeUdr:= GetProcAddress(SpellInstance, 'SpellGetSizeUdr');
@SpellGetListUdr:= GetProcAddress(SpellInstance, 'SpellGetListUdr');
@SpellCloseMdr:= GetProcAddress(SpellInstance, 'SpellCloseMdr');
@SpellCloseUdr:= GetProcAddress(SpellInstance, 'SpellCloseUdr');
except
FreeLibrary(SpellInstance);
FNotActive:= True;
raise ESpellError.CreateFmt(spErrorLoad, [DllName]);
end;
FNotActive:= False;
FOptions:= Options;
CheckSR(SpellInit(Handle, SpecChars));
CheckSR(SpellOptions(Handle, Word(FOptions)));
CheckSR(SpellOpenMdr(Handle, PChar(LexName), nil, False, True, FLanguage, Mdrs));
CheckSR(SpellOpenUdr(Handle, PChar(UserDict), True, IgnoreAlwaysProp, Udr, UdrRO));
with InputBuffer do
begin
cMdr:= 1;
cUdr:= 1;
lrgMdr:= @Mdrs.MDR;
lrgUdr:= @Udr;
end;
with ResultBuffer do
begin
cch:= 1024;
lrgsz:= AllocMem(1024);
lrgbRating:= AllocMem(255);
cbRate:= 255;
end;
end;
destructor TCSAPISpeller.Destroy;
var
SR1: TSEC;
begin
if not FNotActive then
begin
FreeMem(ResultBuffer.lrgsz);
FreeMem(ResultBuffer.lrgbRating);
CheckSR(SpellCloseMdr(Handle, Mdrs));
SR1 := SpellCloseUdr(Handle, Udr, True);
if (SR1 <> 33026) then CheckSR(SR1);
CheckSR(SpellTerminate(Handle, True));
try
FreeLibrary(SpellInstance);
except
raise ESpellError.CreateFmt(spErrorUnLoad, [DllName]);
end;
end;
inherited;
end;
function TCSAPISpeller.FindMisspell(Buf: PChar; MaxLen: Integer; var Start, Len: Integer): TSpellReturnCode;
begin
if FNotActive then
begin
Result:= srNoErrors;
Exit;
end;
InputBuffer.cch:= MaxLen;
InputBuffer.lrgch:= Buf;
InputBuffer.wSpellState:= fssStartsSentence;
CheckSR(SpellCheck(handle, sccVerifyBuffer, InputBuffer, ResultBuffer));
Result:= TSpellReturnCode(ResultBuffer.scrs);
if Result<>srNoErrors then
begin
Start:= ResultBuffer.ichError;
Len:= ResultBuffer.cchError;
SetLength(UnkWord, ResultBuffer.cchError);
StrLCopy(@UnkWord[1], InputBuffer.lrgch+ResultBuffer.ichError, ResultBuffer.cchError);
end;
end;
function TCSAPISpeller.FindNextMisspell(Buf: PChar; MaxLen: Integer; var Start, Len: Integer): TSpellReturnCode;
begin
if FNotActive then
begin
Result:= srNoErrors;
Exit;
end;
InputBuffer.cch:= MaxLen;
InputBuffer.lrgch:= Buf;
InputBuffer.wSpellState:= fssIsContinued;
CheckSR(SpellCheck(Handle, sccVerifyBuffer, InputBuffer, ResultBuffer));
Result:= TSpellReturnCode(ResultBuffer.scrs);
if Result<>srNoErrors then
begin
Start:= ResultBuffer.ichError;
Len:= ResultBuffer.cchError;
SetLength(UnkWord, ResultBuffer.cchError);
StrLCopy(@UnkWord[1], InputBuffer.lrgch+ResultBuffer.ichError, ResultBuffer.cchError);
end;
end;
procedure TCSAPISpeller.ChangeOnce(Word, NewWord: String);
begin
if FNotActive then
Exit;
CheckSR(SpellAddChangeUdr(Handle, udrChangeOnce,
PChar(Word), PChar(NewWord)));
end;
procedure TCSAPISpeller.ChangeAlways(Word, NewWord: String);
begin
if FNotActive then
Exit;
CheckSR(SpellAddChangeUdr(Handle, udrChangeAlways,
PChar(Word), PChar(NewWord)));
end;
procedure TCSAPISpeller.Add(Word: String);
begin
if FNotActive then
Exit;
CheckSR(SpellAddUdr(Handle, Udr, PChar(Word)));
end;
procedure TCSAPISpeller.IgnoreAlways(Word: String);
begin
if FNotActive then
Exit;
CheckSR(SpellAddUdr(Handle, udrIgnoreAlways, PChar(Word)));
end;
procedure TCSAPISpeller.GetVariants(Word: String; Variants: TStrings);
var
SIB: TSIB;
SRB: TSRB;
Buf: array[0..2047]of Char;
Ratings: array[0..255]of Byte;
P: PChar;
begin
Variants.Clear;
if FNotActive then
Exit;
with SIB do
begin
cch:= Length(Word);
cMdr:= 1;
cUdr:= 1;
wSpellState:= fssNoStateInfo;
lrgch:= @Word[1];
lrgMdr:= @Mdrs.MDR;
lrgUdr:= @Udr;
end;
with SRB do
begin
cch:= 2047;
lrgsz:= @Buf;
lrgbRating:= @Ratings;
cbRate:= 255;
end;
CheckSR(SpellCheck(Handle, sccSuggest, SIB, SRB));
while SRB.scrs<>scrsNoMoreSuggestions do
begin
P:= SRB.lrgsz;
while P^<>#0 do
begin
if Variants.IndexOf(P)=-1 then
Variants.Add(P);
while P^<>#0 do
Inc(P);
Inc(P);
end;
CheckSR(SpellCheck(Handle, sccSuggestMore, SIB, SRB));
end;
end;
function TCSAPISpeller.GetChangeText: String;
begin
if FNotActive then
Result:= ''
else
Result:= ResultBuffer.lrgsz;
end;
function TCSAPISpeller.GetMisspellText: String;
begin
if FNotActive then
Result:= ''
else
Result:= UnkWord;
end; {TCSAPISpeller}
{$O+}
{TISpeller}
type
TISpeller = class(TAbstractSpeller)
private
FOptions: TSpellOptions;
si_r, si_w, so_r, so_w, se_r, se_w: THandle;
PI: TProcessInformation;
M, Repl,tmpstr: String;
UnkWord: WideString;
ReplData: TStringList;
function SpellCheck (word: String): String;
function Check(Word: WideString): Boolean;
procedure SpellCommand (Word: WideString);
protected
constructor Create(Language: TLanguage; Owner: TSpellChecker; Options: TSpellOptions); override;
destructor Destroy; override;
function FindMisspell(Buf: PChar; MaxLen: Integer; var Start, Len: Integer): TSpellReturnCode; override;
function FindNextMisspell(Buf: PChar; MaxLen: Integer; var Start, Len: Integer): TSpellReturnCode; override;
procedure ChangeOnce(Word, NewWord: String); override;
procedure ChangeAlways(Word, NewWord: String); override;
procedure Add(Word: String); override;
procedure IgnoreAlways(Word: String); override;
procedure GetVariants(Word: String; Variants: TStrings); override;
function GetChangeText: String; override;
function GetMisspellText: String; override;
public
Tbl: TConvTable;
end; {TISpeller}
type
THTMLBracket = (thbTag, thbComment, thbBasic);
const
OpenBracket: array[THTMLBracket] of PChar=('<', '<!--', '<%');
CloseBracket: array[THTMLBracket] of PChar=('>', '-->', '%>');
var
ControlTypes: TStrings;
{TSpellChecker}
constructor TSpellChecker.Create(AOwner: TComponent);
begin
inherited;
FLanguage:= GetSystemDefaultLCID;
FOptions:= [spoIgnoreMixedCaps,spoSuggestFromUserDict, spoIgnoreAllCaps, spoIgnoreMixedDigits,
spoIgnoreRomanNumerals];
FOptions:=[spoIgnoreMixedCaps];
ActiveLanguage := True;
FFont := TFont.Create;
FMissFont := TMisspellFont.Create;
Spellers:= TList.Create;
SetFontDefault;
end;
destructor TSpellChecker.Destroy;
var
I: Integer;
begin
for I:= Spellers.Count-1 downto 0 do
TAbstractSpeller(Spellers.Items[I]).Free;
Spellers.Free;
FFont.Free;
FMissFont.Free;
inherited;
end;
class procedure TSpellChecker.RegisterEditControl(MemoClass: String; Unicode, MultiLanguage: Boolean);
begin
ControlTypes.AddObject(MemoClass, Pointer(Ord(Unicode) or Ord(MultiLanguage)*2));
end;
procedure TSpellChecker.GetMemoProperties;
var
C: TClass;
I: Integer;
begin
C:= FMemo.ClassType;
repeat
for I:= 0 to ControlTypes.Count-1 do
if AnsiCompareText(C.ClassName, ControlTypes[I])=0 then
begin
FLangSupport:= Boolean(Integer(ControlTypes.Objects[I]) shr 1);
FUnicode:= Boolean(Integer(ControlTypes.Objects[I]) and 1);
Exit;
end;
C:= C.ClassParent;
until C=TCustomEdit;
raise ESpellError.CreateFmt('You can''t spell check %s.', [FMemo.Name]);
end;
procedure TSpellChecker.SetLanguage(Value: TLanguage);
begin
FLanguage:= Value;
end;
function TSpellChecker.OpenLanguage(Value: TLanguage; SpType:
TSpellerType): Boolean;
var
Speller: TAbstractSpeller;
begin
Result:= False;
if SpType = sptMSOffice then begin
try
Speller:= TCSAPISpeller.Create(Value, Self, FOptions);
except
try
Speller.Free;
except end;
Exit;
end;
Spellers.Add(Speller);
Result:= True;
end; //if SpellerType = sptMSOffice
if SpType = sptISpell then begin
try
Speller:= TISpeller.Create(Value, Self, FOptions);
except
try
Speller.Free;
except end;
Exit;
end;
Spellers.Add(Speller);
Result:= True;
end; //if SpellerType = sptISpell
end;
function TSpellChecker.FindLanguage(Value: TLanguage; SpType: TSpellerType): TAbstractSpeller;
var
I: Integer;
begin
for I:= 0 to Spellers.Count-1 do
if TAbstractSpeller(Spellers.Items[I]).Language=Value then
if TAbstractSpeller(Spellers.Items[I]).FSpellerType = SpType
then begin
Result:= TAbstractSpeller(Spellers.Items[I]);
Exit;
end;
if OpenLanguage(Value, SpType) then
Result:= TAbstractSpeller(Spellers.Items[Spellers.Count-1])
else
Result:= nil;
end;
function TSpellChecker.IsKnownWord(Word: String; Language: TLanguage): Boolean;
var
Start, Len: Integer;
begin
with FindLanguage(Language, SpellerType) do
Result:= FindMisspell(@Word[1], Length(Word), Start, Len)=srNoErrors;
end;
procedure TSpellChecker.AddWord(Word: String; Language: TLanguage);
begin
with FindLanguage(Language, SpellerType) do
Add(Word);
end;
procedure TSpellChecker.GetVariants(Word: String; Variants: TStrings; Language: TLanguage);
begin
with FindLanguage(Language, SpellerType) do
GetVariants(Word, Variants);
end;
procedure TSpellChecker.GetTag(From: Integer; var Len: Integer);
var
P, PP: PChar;
HTMLTag: THTMLBracket;
S: String;
begin
SetLength(S, FSpellEnd-From);
GetTextRange(@S[1], From, FSpellEnd, 1252);
if StrLComp(@S[1], OpenBracket[thbComment], StrLen(OpenBracket[thbComment]))=0 then
HTMLTag:= thbComment
else if StrLComp(@S[1], OpenBracket[thbBasic], StrLen(OpenBracket[thbBasic]))=0 then
HTMLTag:= thbBasic
else
HTMLTag:= thbTag;
P:= StrPos(@S[1], CloseBracket[HTMLTag])+StrLen(CloseBracket[HTMLTag]);
if HTMLTag<>thbBasic then
begin
PP:= StrScan(@S[2], '<');
if (PP<>nil) and (PP<P) then
begin
GetTag(PP-@S[1]+From, Len);
P:= StrPos(PP+Len, CloseBracket[HTMLTag])+StrLen(CloseBracket[HTMLTag]);
end;
end;
if P=nil then
Len:= Length(S)
else
Len:= P-@S[1];
end;
procedure TSpellChecker.GetTextRange(Buf: PChar; StartPos, EndPos: Integer; CP: Word);
type
{ The declarations of TTextRangeA and TTextRangeW in Richedit.pas are incorrect}
TTextRangeA = record
chrg: TCharRange;
lpstrText: PAnsiChar; {not AnsiChar!}
end;
var
W: WideString;
S: String;
GTL: TGetTextLengthEx;
GT: TGetTextEx;
L: Integer;
begin
GTL.flags:= GTL_DEFAULT;
GTL.codepage:= 1200;
L:= FMemo.Perform(EM_GETTEXTLENGTHEX, Integer(@GTL), 0);
if L>0 then
begin
SetLength(W, L);
GT.cb:= L*2+2;
GT.flags:= GT_DEFAULT;
GT.codepage:= 1200;
GT.lpDefaultChar:= nil;
GT.lpUsedDefChar:= nil;
FMemo.Perform(EM_GETTEXTEX, Integer(@GT), Integer(@W[1]));
WideCharToMultiByte(CP, 0, @W[StartPos+1], EndPos-StartPos, Buf, EndPos-StartPos, nil, nil);
Buf[EndPos-StartPos]:= #0;
end
else
begin
S:= FBackMemo.Text;
StrLCopy(Buf, @S[StartPos+1], EndPos-StartPos);
end;
end;
function TSpellChecker.GetMemoLanguage: TLanguage;
var
CF: TCharFormat2A;
CFW: TCharFormat2W;
begin
if not FLangSupport then
Result:= FLanguage
else if FUnicode then
begin
FillChar(CFW, SizeOf(CFW), 0);
CFW.cbSize:= SizeOf(CFW);
FBackMemo.Perform(EM_GETCHARFORMAT, 1, LongInt(@CFW));
Result:= CFW.lid;
end
else
begin
FillChar(CF, SizeOf(CF), 0);
CF.cbSize:= SizeOf(CF);
FBackMemo.Perform(EM_GETCHARFORMAT, 1, LongInt(@CF));
Result:= CF.lid;
end;
end;
procedure TSpellChecker.GetBlock(From: Integer; var StartPos, EndPos: Integer);
var
L, Lang: TLanguage;
FT: TFindTextA;
FTW: TFindTextW;
C: Char;
P, Len, LP: Integer;
S: String;
Pos: PChar;
begin
P:= From-1;
repeat
Inc(P);
GetTextRange(@C, P, P+1, 1252);
if FHTML and (C='<') then
begin
GetTag(P, Len);
Inc(P, Len-1);
end
else if C=#13 then
FStartSentence:= True
else if not (C in [#10, #13, #11]) then
Break;
until P>=FSpellEnd;
if P<FSpellEnd then
begin
StartPos:= P;
if FUnicode then
begin
if CRPos<=StartPos then
begin
FTW.chrg.cpMin:= StartPos;
FTW.chrg.cpMax:= FSpellEnd;
FTW.lpstrText:= #13;
CRPos:= FBackMemo.Perform(EM_FINDTEXTEX, 1, LongInt(@FTW));
if CRPos=-1 then
CRPos:= FSpellEnd;
end;
if FHTML then
if (TagPos<=StartPos) then
begin
FTW.chrg.cpMin:= StartPos;
FTW.chrg.cpMax:= FSpellEnd;
FTW.lpstrText:= '<';
TagPos:= FBackMemo.Perform(EM_FINDTEXTEX, 1, LongInt(@FTW));
if TagPos=-1 then
TagPos:= FSpellEnd;
end
else