-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathd2dFormattedText.pas
More file actions
1360 lines (1231 loc) · 34.7 KB
/
d2dFormattedText.pas
File metadata and controls
1360 lines (1231 loc) · 34.7 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
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
//---------------------------------------------------------------------------
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//---------------------------------------------------------------------------
unit d2dFormattedText;
interface
uses
Classes,
Contnrs,
d2dTypes,
d2dInterfaces,
d2dUtils;
type
Td2dCustomSlice = class;
Td2dLinkSlice = class;
Td2dCustomTextChunk = class;
Td2dLinkActionProc = procedure(const aLinkSlice: Td2dLinkSlice) of object;
Td2dSliceActionProc = procedure(const aSlice: Td2dCustomSlice) of object;
Td2dChunkActionProc = procedure(const aChunk: Td2dCustomTextChunk) of object;
Td2dOnLinkClickEvent = procedure(const aSender: TObject; const aRect: Td2dRect; const aTarget: string) of object;
Td2dCustomTextChunk = class
protected
function pm_GetChunkType: Td2dTextChunkType; virtual;
public
procedure Save(aFiler: Td2dFiler); virtual;
procedure Load(aFiler: Td2dFiler; const aFP: Id2dFontProvider; const aPP: Id2dPictureProvider); virtual; abstract;
property ChunkType: Td2dTextChunkType read pm_GetChunkType;
end;
Td2dTextPara = class
private
f_IsClosed: Boolean;
f_List: TObjectList;
f_ParaType: Td2dTextAlignType;
function pm_GetChunks(Index: Integer): Td2dCustomTextChunk;
function pm_GetCount: Integer;
procedure _DropLink(const aChunk: Td2dCustomTextChunk);
public
constructor Create(aParaType: Td2dTextAlignType);
destructor Destroy; override;
procedure AddPicture(const aPicture: Id2dPicture; const anID: string = '');
procedure AddRawChunk(aChunk: Td2dCustomTextChunk);
procedure AddText(const aText: string; const aFont: Id2dFont; aColor: Td2dColor; aForceNewChunk: Boolean = False);
procedure AddLink(const aText, aTarget: string; const aFont: Id2dFont; aColor, aLinkColor, aHighlightColor: Td2dColor);
procedure ClosePara;
procedure DropLinks;
procedure IterateChunks(anAction: Td2dChunkActionProc);
procedure Save(aFiler: Td2dFiler);
procedure Load(aFiler: Td2dFiler; const aFP: Id2dFontProvider; const aPP: Id2dPictureProvider);
property Chunks[Index: Integer]: Td2dCustomTextChunk read pm_GetChunks;
property Count: Integer read pm_GetCount;
property IsClosed: Boolean read f_IsClosed;
property ParaType: Td2dTextAlignType read f_ParaType write f_ParaType;
end;
Td2dStringChunk = class(Td2dCustomTextChunk)
private
f_Font: Id2dFont;
protected
f_Color: Td2dColor;
f_Text: string;
function pm_GetChunkType: Td2dTextChunkType; override;
public
constructor Create(const aText: string; const aFont: Id2dFont; aColor: Td2dColor);
procedure ConcatText(aText: string);
procedure Save(aFiler: Td2dFiler); override;
procedure Load(aFiler: Td2dFiler; const aFP: Id2dFontProvider; const aPP: Id2dPictureProvider); override;
property Color: Td2dColor read f_Color write f_Color;
property Font: Id2dFont read f_Font write f_Font;
property Text: string read f_Text write f_Text;
end;
Td2dLinkChunk = class(Td2dStringChunk)
private
f_Active: Boolean;
f_HighlightColor: Td2dColor;
f_LinkColor: Td2dColor;
f_Target: string;
protected
function pm_GetChunkType: Td2dTextChunkType; override;
public
constructor Create(const aText, aTarget: string; const aFont: Id2dFont; aColor, aLinkColor, aHighlightColor: Td2dColor);
procedure Save(aFiler: Td2dFiler); override;
procedure Load(aFiler: Td2dFiler; const aFP: Id2dFontProvider; const aPP: Id2dPictureProvider); override;
property Active: Boolean read f_Active write f_Active;
property HighlightColor: Td2dColor read f_HighlightColor write f_HighlightColor;
property LinkColor: Td2dColor read f_LinkColor write f_LinkColor;
property Target: string read f_Target write f_Target;
end;
Td2dPictureChunk = class(Td2dCustomTextChunk)
private
protected
f_Picture: Id2dPicture;
function pm_GetChunkType: Td2dTextChunkType; override;
public
procedure Save(aFiler: Td2dFiler); override;
procedure Load(aFiler: Td2dFiler; const aFP: Id2dFontProvider; const aPP: Id2dPictureProvider); override;
constructor Create(const aPicture: Id2dPicture; const anID: string);
property Picture: Id2dPicture read f_Picture;
end;
Id2dTextAddingTool = interface
['{99660309-19E2-4131-8E67-88EDC936D269}']
procedure AddText(const aText: string);
procedure AddLink(const aText, aTarget: string);
end;
Td2dTextSource = class
private
f_ParaList: TObjectList;
procedure AddTextOrLink(aIsLink: Boolean; const aText, aTarget: string; const aFont: Id2dFont; aColor, aLinkColor,
aHighlightColor: Td2dColor; aParaType: Td2dTextAlignType);
function pm_GetParagraphs(Index: Integer): Td2dTextPara;
function pm_GetCount: Integer;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure Save(aFiler: Td2dFiler);
procedure Load(aFiler: Td2dFiler; const aFP: Id2dFontProvider; const aPP: Id2dPictureProvider);
function AddRawPara(aParaType: Td2dTextAlignType): Td2dTextPara;
procedure AddText(const aText: string; const aFont: Id2dFont; aColor: Td2dColor; aParaType: Td2dTextAlignType);
procedure AddLink(const aText, aTarget: string; const aFont: Id2dFont; aColor, aLinkColor, aHighlightColor: Td2dColor;
aParaType: Td2dTextAlignType);
procedure AddPicture(aPicture: Id2dPicture; aAlign: Td2dTextAlignType);
procedure DropLinks(aFrom: Integer = 0);
procedure IterateChunks(anAction: Td2dChunkActionProc);
property Paragraphs[Index: Integer]: Td2dTextPara read pm_GetParagraphs;
property Count: Integer read pm_GetCount;
end;
// Ñëàéñ - åäèíèöà ôîðìàòèðîâàííîãî òåêñòà
Td2dCustomSlice = class
private
f_Left: Single;
f_Parent: Td2dCustomSlice;
f_Top: Single;
function pm_GetAbsLeft: Single;
function pm_GetAbsTop: Single;
protected
function pm_GetHeight: Single; virtual; abstract;
function pm_GetWidth: Single; virtual; abstract;
procedure DoDraw(X, Y: Single); virtual; abstract;
function pm_GetSliceType: Td2dTextSliceType; virtual;
procedure pm_SetWidth(const Value: Single); virtual; abstract;
public
procedure Draw(X, Y: Single);
property AbsLeft: Single read pm_GetAbsLeft;
property AbsTop: Single read pm_GetAbsTop;
property Height: Single read pm_GetHeight;
property Left: Single read f_Left write f_Left;
property Parent: Td2dCustomSlice read f_Parent write f_Parent;
property SliceType: Td2dTextSliceType read pm_GetSliceType;
property Top: Single read f_Top write f_Top;
property Width: Single read pm_GetWidth write pm_SetWidth;
end;
Td2dUnionSlice = class(Td2dCustomSlice)
private
f_ChList: TObjectList;
f_Height: Single;
f_Parent: Td2dUnionSlice;
f_Width: Single;
function pm_GetChildren(Index: Integer): Td2dCustomSlice;
function pm_GetChildrenCount: Integer;
function pm_GetChList: TObjectList;
protected
function pm_GetHeight: Single; override;
function pm_GetSliceType: Td2dTextSliceType; override;
function pm_GetWidth: Single; override;
procedure pm_SetWidth(const Value: Single); override;
property ChList: TObjectList read pm_GetChList;
public
constructor Create(aWidth: Single);
destructor Destroy; override;
procedure AddChild(aChild: Td2dCustomSlice); virtual;
procedure Clear; virtual;
procedure DeleteFrom(aIndex : Integer); virtual;
procedure DoDraw(X, Y: Single); override;
procedure IterateLeafSlices(anAction: Td2dSliceActionProc; aFrom: Integer = 0);
procedure RecalcHeight;
property Children[Index: Integer]: Td2dCustomSlice read pm_GetChildren;
property ChildrenCount: Integer read pm_GetChildrenCount;
property Parent: Td2dUnionSlice read f_Parent write f_Parent;
end;
Td2dTextSlice = class(Td2dCustomSlice)
private
f_Color: Td2dColor;
f_Size : Td2dPoint;
procedure CalcSize;
protected
f_Font: Id2dFont;
f_Text : string;
procedure DoDraw(X, Y: Single); override;
function pm_GetHeight: Single; override;
function pm_GetSliceType: Td2dTextSliceType; override;
function pm_GetWidth: Single; override;
public
constructor Create(const aText: string; const aFont: Id2dFont; aColor: Td2dColor);
property Color: Td2dColor read f_Color write f_Color;
end;
Td2dPictureSlice = class(Td2dCustomSlice)
private
f_Picture : Id2dPicture;
protected
function pm_GetHeight: Single; override;
function pm_GetSliceType: Td2dTextSliceType; override;
function pm_GetWidth: Single; override;
public
constructor Create(const aPicture: Id2dPicture);
procedure DoDraw(X, Y: Single); override;
end;
Td2dFormatParamsRec = record
rLineSpacing: Single;
rParaSpacing: Single;
end;
// Formatter
Td2dFormatter = class
private
f_Document: Td2dUnionSlice;
f_FormatParams: Td2dFormatParamsRec;
f_LastFormatted: Integer;
f_TextSource: Td2dTextSource;
procedure FormatFrom(aParaIndex: Integer);
function FormatParagraph(aParaIdx: Integer): Td2dUnionSlice;
procedure pm_SetLastFormatted(const Value: Integer);
public
constructor Create(aTS: Td2dTextSource; aDoc: Td2dUnionSlice);
procedure Format;
procedure ReformatAll;
procedure UpdateFormat;
property FormatParams: Td2dFormatParamsRec read f_FormatParams write f_FormatParams;
property LastFormatted: Integer read f_LastFormatted write pm_SetLastFormatted;
end;
Td2dLinkSlice = class(Td2dTextSlice)
private
f_Allowed: Boolean;
f_HighlightColor: Td2dColor;
f_IsActive: Boolean;
f_IsHighlighted: Boolean;
f_LinkColor: Td2dColor;
f_Next: Td2dLinkSlice;
f_Prev: Td2dLinkSlice;
f_Target: string;
protected
procedure DoDraw(X, Y: Single); override;
function pm_GetSliceType: Td2dTextSliceType; override;
public
constructor Create(const aText, aTarget: string; const aFont: Id2dFont; aTextColor, aLinkColor, aHighlightColor: Td2dColor);
function GetLinkText: string;
procedure SpreadHighlight;
property Allowed: Boolean read f_Allowed write f_Allowed;
property HighlightColor: Td2dColor read f_HighlightColor write f_HighlightColor;
property IsActive: Boolean read f_IsActive write f_IsActive;
property IsHighlighted: Boolean read f_IsHighlighted write f_IsHighlighted;
property LinkColor: Td2dColor read f_LinkColor write f_LinkColor;
property Next: Td2dLinkSlice read f_Next write f_Next;
property Prev: Td2dLinkSlice read f_Prev write f_Prev;
property Target: string read f_Target write f_Target;
end;
implementation
uses
SysUtils,
d2dCore,
StrUtils;
type
TChunkCursor = record
rChunk: Integer;
rPos : Integer;
end;
const
csClosedPara = 'Ïàðàãðàô óæå çàêðûò!';
type
Td2dTextChunkClass = class of Td2dCustomTextChunk;
function TextChunkType2Class(const aType: Td2dTextChunkType): Td2dTextChunkClass;
begin
case aType of
ctText: Result := Td2dStringChunk;
ctLink: Result := Td2dLinkChunk;
ctPicture: Result := Td2dPictureChunk;
else
Result := nil;
end;
end;
function d2dLoadChunk(const aFiler: Td2dFiler; const aFP: Id2dFontProvider; const aPP: Id2dPictureProvider): Td2dCustomTextChunk;
var
l_CP: Td2dTextChunkType;
l_ChunkClass: Td2dTextChunkClass;
begin
l_CP := Td2dTextChunkType(aFiler.ReadByte);
l_ChunkClass := TextChunkType2Class(l_CP);
if l_ChunkClass <> nil then
begin
Result := l_ChunkClass.Create;
Result.Load(aFiler, aFP, aPP);
end
else
Result := nil;
end;
function Td2dCustomTextChunk.pm_GetChunkType: Td2dTextChunkType;
begin
Result := ctUndefined;
end;
procedure Td2dCustomTextChunk.Save(aFiler: Td2dFiler);
begin
aFiler.WriteByte(Ord(ChunkType));
end;
constructor Td2dStringChunk.Create(const aText: string; const aFont: Id2dFont; aColor: Td2dColor);
begin
inherited Create;
f_Text := aText;
f_Font := aFont;
f_Color := aColor;
end;
procedure Td2dStringChunk.ConcatText(aText: string);
begin
f_Text := f_Text + aText;
end;
procedure Td2dStringChunk.Load(aFiler: Td2dFiler; const aFP: Id2dFontProvider; const aPP: Id2dPictureProvider);
var
l_FN: string;
begin
l_FN := aFiler.ReadString;
f_Font := aFP.GetByID(l_FN);
f_Color := aFiler.ReadColor;
f_Text := aFiler.ReadString;
end;
function Td2dStringChunk.pm_GetChunkType: Td2dTextChunkType;
begin
Result := ctText;
end;
procedure Td2dStringChunk.Save(aFiler: Td2dFiler);
begin
inherited;
aFiler.WriteString(f_Font.ID);
aFiler.WriteColor(f_Color);
aFiler.WriteString(f_Text);
end;
constructor Td2dTextSource.Create;
begin
inherited;
f_ParaList := TObjectList.Create(True);
end;
destructor Td2dTextSource.Destroy;
begin
f_ParaList.Free;
inherited;
end;
procedure Td2dTextSource.AddLink(const aText, aTarget: string; const aFont: Id2dFont; aColor, aLinkColor,
aHighlightColor: Td2dColor; aParaType: Td2dTextAlignType);
begin
AddTextOrLink(True, aText, aTarget, aFont, aColor, aLinkColor, aHighlightColor, aParaType);
end;
procedure Td2dTextSource.AddPicture(aPicture: Id2dPicture; aAlign: Td2dTextAlignType);
var
// l_Picture: Id2dPicture;
l_Para: Td2dTextPara;
begin
{
if (aWidth = 0) then
aWidth := gD2DE.Texture_GetWidth(aTex) - aTX;
if (aHeight = 0) then
aHeight := gD2DE.Texture_GetHeight(aTex) - aTY;
if (aWidth < 1) or (aHeight < 1) then
Exit;
l_Picture := Id2dPicture.Create(aTex, aTX, aTY, aWidth, aHeight);
l_Picture.SetColor(aColor);
}
if Count > 0 then
begin
l_Para := Paragraphs[Pred(Count)];
if (l_Para.IsClosed) or (l_Para.ParaType <> aAlign) then
l_Para := AddRawPara(aAlign);
end
else
l_Para := AddRawPara(aAlign);
l_Para.AddPicture(aPicture);
end;
function Td2dTextSource.AddRawPara(aParaType: Td2dTextAlignType): Td2dTextPara;
begin
Result := Td2dTextPara.Create(aParaType);
f_ParaList.Add(Result);
end;
procedure Td2dTextSource.AddText(const aText: string; const aFont: Id2dFont; aColor: Td2dColor; aParaType:
Td2dTextAlignType);
begin
AddTextOrLink(False, aText, '', aFont, aColor, 0, 0, aParaType);
end;
procedure Td2dTextSource.AddTextOrLink(aIsLink: Boolean; const aText, aTarget: string; const aFont: Id2dFont; aColor, aLinkColor, aHighlightColor: Td2dColor;
aParaType: Td2dTextAlignType);
var
l_Para: Td2dTextPara;
l_Pos: Integer;
l_SubStr: string;
l_Text : string;
procedure AddToPara(aStr: string; aDoClose: Boolean);
begin
if l_Para = nil then
l_Para := AddRawPara(aParaType);
if aIsLink then
l_Para.AddLink(aStr, aTarget, aFont, aColor, aLinkColor, aHighlightColor)
else
l_Para.AddText(aStr, aFont, aColor);
if aDoClose then
begin
l_Para.ClosePara;
l_Para := nil;
end;
end;
begin
l_Text := aText;
if Count > 0 then
begin
l_Para := Paragraphs[Pred(Count)];
if (l_Para.IsClosed) or (l_Para.ParaType <> aParaType) then
l_Para := nil;
end
else
l_Para := nil;
l_Pos := Pos(#13#10, l_Text);
while l_Pos > 0 do
begin
l_SubStr := Copy(l_Text, 1, l_Pos-1);
AddToPara(l_SubStr, True);
Delete(l_Text, 1, l_Pos+1);
l_Pos := Pos(#13#10, l_Text);
end;
if l_Text <> '' then
AddToPara(l_Text, False);
end;
procedure Td2dTextSource.Clear;
begin
f_ParaList.Clear;
end;
procedure Td2dTextSource.DropLinks(aFrom: Integer = 0);
var
I: Integer;
begin
for I := aFrom to Count - 1 do
Paragraphs[I].DropLinks;
end;
procedure Td2dTextSource.IterateChunks(anAction: Td2dChunkActionProc);
var
I: Integer;
begin
for I := 0 to Count - 1 do
Paragraphs[I].IterateChunks(anAction);
end;
procedure Td2dTextSource.Load(aFiler: Td2dFiler; const aFP: Id2dFontProvider; const aPP: Id2dPictureProvider);
var
I: Integer;
l_Count: Integer;
l_Para: Td2dTextPara;
begin
Clear;
l_Count := aFiler.ReadInteger;
for I := 1 to l_Count do
begin
l_Para := Td2dTextPara.Create(ptLeftAligned);
l_Para.Load(aFiler, aFP, aPP);
f_ParaList.Add(l_Para);
end;
end;
function Td2dTextSource.pm_GetParagraphs(Index: Integer): Td2dTextPara;
begin
Result := Td2dTextPara(f_ParaList[Index]);
end;
function Td2dTextSource.pm_GetCount: Integer;
begin
Result := f_ParaList.Count;
end;
procedure Td2dTextSource.Save(aFiler: Td2dFiler);
var
I: Integer;
begin
aFiler.WriteInteger(Count);
for I := 0 to Count-1 do
Paragraphs[I].Save(aFiler);
end;
constructor Td2dTextPara.Create(aParaType: Td2dTextAlignType);
begin
inherited Create;
f_ParaType := aParaType;
f_List := TObjectList.Create(True);
end;
destructor Td2dTextPara.Destroy;
begin
f_List.Free;
inherited;
end;
procedure Td2dTextPara.AddPicture(const aPicture: Id2dPicture; const anID: string = '');
var
l_Chunk: Td2dPictureChunk;
begin
l_Chunk := Td2dPictureChunk.Create(aPicture, anID);
f_List.Add(l_Chunk)
end;
procedure Td2dTextPara.AddRawChunk(aChunk: Td2dCustomTextChunk);
begin
Assert(not IsClosed, csClosedPara);
f_List.Add(aChunk);
end;
procedure Td2dTextPara.AddText(const aText: string; const aFont: Id2dFont; aColor: Td2dColor; aForceNewChunk: Boolean = False);
var
l_Chunk: Td2dCustomTextChunk;
begin
Assert(not IsClosed, csClosedPara);
if f_List.Count > 0 then
begin
l_Chunk := Td2dCustomTextChunk(f_List[Pred(f_List.Count)]);
if (not aForceNewChunk) and (l_Chunk.ChunkType = ctText) and
(Td2dStringChunk(l_Chunk).Font = aFont) and
(Td2dStringChunk(l_Chunk).Color = aColor) then
begin
Td2dStringChunk(l_Chunk).ConcatText(aText);
Exit;
end;
end;
l_Chunk := Td2dStringChunk.Create(aText, aFont, aColor);
f_List.Add(l_Chunk);
end;
procedure Td2dTextPara.AddLink(const aText, aTarget: string; const aFont: Id2dFont; aColor, aLinkColor, aHighlightColor: Td2dColor);
var
l_Chunk: Td2dCustomTextChunk;
begin
Assert(not IsClosed, csClosedPara);
l_Chunk := Td2dLinkChunk.Create(aText, aTarget, aFont, aColor, aLinkColor, aHighlightColor);
f_List.Add(l_Chunk);
end;
procedure Td2dTextPara.ClosePara;
begin
f_IsClosed := True;
end;
procedure Td2dTextPara.DropLinks;
begin
IterateChunks(_DropLink);
end;
procedure Td2dTextPara._DropLink(const aChunk: Td2dCustomTextChunk);
begin
if aChunk.ChunkType = ctLink then
Td2dLinkChunk(aChunk).Active := False;
end;
procedure Td2dTextPara.IterateChunks(anAction: Td2dChunkActionProc);
var
I: Integer;
begin
for I := 0 to Count - 1 do
anAction(Chunks[I]);
end;
procedure Td2dTextPara.Load(aFiler: Td2dFiler; const aFP: Id2dFontProvider; const aPP: Id2dPictureProvider);
var
I: Integer;
l_Count: Integer;
l_Chunk: Td2dCustomTextChunk;
begin
f_List.Clear;
f_ParaType := Td2dTextAlignType(aFiler.ReadByte);
f_IsClosed := aFiler.ReadBoolean;
l_Count := aFiler.ReadInteger;
for I := 1 to l_Count do
begin
l_Chunk := d2dLoadChunk(aFiler, aFP, aPP); // ôàáðèêà
if l_Chunk <> nil then
f_List.Add(l_Chunk);
end;
end;
function Td2dTextPara.pm_GetChunks(Index: Integer): Td2dCustomTextChunk;
begin
Result := Td2dCustomTextChunk(f_List[Index]);
end;
function Td2dTextPara.pm_GetCount: Integer;
begin
Result := f_List.Count;
end;
procedure Td2dTextPara.Save(aFiler: Td2dFiler);
var
I: Integer;
begin
aFiler.WriteByte(Ord(f_ParaType));
aFiler.WriteBoolean(f_IsClosed);
aFiler.WriteInteger(Count);
for I := 0 to Count-1 do
Chunks[I].Save(aFiler);
end;
constructor Td2dUnionSlice.Create(aWidth: Single);
begin
inherited Create;
f_Width := aWidth;
end;
destructor Td2dUnionSlice.Destroy;
begin
if f_ChList <> nil then
FreeAndNil(f_ChList);
inherited;
end;
procedure Td2dUnionSlice.AddChild(aChild: Td2dCustomSlice);
begin
ChList.Add(aChild);
aChild.Parent := Self;
end;
procedure Td2dUnionSlice.DoDraw(X, Y: Single);
var
I : Integer;
l_Child: Td2dCustomSlice;
begin
for I := 0 to Pred(ChildrenCount) do
begin
l_Child := Children[I];
l_Child.Draw(X, Y);
end;
end;
function Td2dUnionSlice.pm_GetChildren(Index: Integer): Td2dCustomSlice;
begin
Result := Td2dCustomSlice(ChList[Index]);
end;
function Td2dUnionSlice.pm_GetChildrenCount: Integer;
begin
if f_ChList = nil then
Result := 0
else
Result := f_ChList.Count;
end;
function Td2dUnionSlice.pm_GetChList: TObjectList;
begin
if f_ChList = nil then
f_ChList := TObjectList.Create(True);
Result := f_ChList;
end;
function Td2dUnionSlice.pm_GetHeight: Single;
begin
Result := f_Height;
end;
function Td2dUnionSlice.pm_GetWidth: Single;
begin
Result := f_Width;
end;
procedure Td2dUnionSlice.RecalcHeight;
var
I: Integer;
l_Child: Td2dCustomSlice;
l_FarPoint: Single;
begin
f_Height := 0;
for I := 0 to Pred(ChildrenCount) do
begin
l_Child := Children[I];
l_FarPoint := l_Child.Top + l_Child.Height;
if l_FarPoint > f_Height then
f_Height := l_FarPoint;
end;
end;
procedure Td2dUnionSlice.Clear;
var
I: Integer;
begin
if f_ChList <> nil then
begin
for I := 0 to Pred(ChildrenCount) do
begin
if Children[I].SliceType = stUnion then
Td2dUnionSlice(Children[I]).Clear;
end;
f_ChList.Clear;
end;
end;
procedure Td2dUnionSlice.DeleteFrom(aIndex : Integer);
var
I: Integer;
begin
for I := Pred(ChildrenCount) downto aIndex do
ChList.Delete(I);
RecalcHeight;
end;
procedure Td2dUnionSlice.IterateLeafSlices(anAction: Td2dSliceActionProc; aFrom: Integer = 0);
procedure ScanForLinkSlices(const aSlice: Td2dCustomSlice);
var
J: Integer;
l_UC: Td2dUnionSlice;
begin
if aSlice.SliceType = stUnion then
begin
l_UC := Td2dUnionSlice(aSlice);
l_UC.IterateLeafSlices(anAction);
end
else
anAction(aSlice);
end;
var
I: Integer;
begin
for I := aFrom to ChildrenCount-1 do
ScanForLinkSlices(Children[I]);
end;
function Td2dUnionSlice.pm_GetSliceType: Td2dTextSliceType;
begin
Result := stUnion;
end;
procedure Td2dUnionSlice.pm_SetWidth(const Value: Single);
begin
f_Width := Value;
end;
constructor Td2dTextSlice.Create(const aText: string; const aFont: Id2dFont; aColor: Td2dColor);
begin
inherited Create;
f_Text := aText;
f_Font := aFont;
f_Color := aColor;
end;
procedure Td2dTextSlice.CalcSize;
begin
f_Font.CalcSize(f_Text, f_Size);
end;
procedure Td2dTextSlice.DoDraw(X, Y: Single);
begin
f_Font.Color := f_Color;
f_Font.Render(X, Y, f_Text);
//D2DRenderRect(D2DRect(X, Y, X+Width, Y+Height), $FFFF0000);
end;
function Td2dTextSlice.pm_GetHeight: Single;
begin
if f_Size.Y = 0 then
CalcSize;
Result := f_Size.Y;
end;
function Td2dTextSlice.pm_GetSliceType: Td2dTextSliceType;
begin
Result := stText;
end;
function Td2dTextSlice.pm_GetWidth: Single;
begin
if f_Size.X = 0 then
CalcSize;
Result := f_Size.X;
end;
constructor Td2dFormatter.Create(aTS: Td2dTextSource; aDoc: Td2dUnionSlice);
begin
inherited Create;
f_TextSource := aTS;
f_Document := aDoc;
f_LastFormatted := 0;
end;
procedure Td2dFormatter.Format;
begin
FormatFrom(f_LastFormatted);
end;
procedure Td2dFormatter.FormatFrom(aParaIndex: Integer);
var
I : Integer;
l_Para : Td2dUnionSlice;
l_CurTop : Single;
begin
// first, delete paragraphs starting from aParaIndex
f_Document.DeleteFrom(aParaIndex);
if f_Document.ChildrenCount = 0 then
l_CurTop := 0.0
else
with f_Document.Children[Pred(f_Document.ChildrenCount)] do
l_CurTop := Top + Height + f_FormatParams.rParaSpacing;
for I := aParaIndex to Pred(f_TextSource.Count) do
begin
l_Para := FormatParagraph(I);
l_Para.Top := l_CurTop;
l_CurTop := l_CurTop + l_Para.Height + f_FormatParams.rParaSpacing;
f_Document.AddChild(l_Para);
end;
f_Document.RecalcHeight;
end;
function Td2dFormatter.FormatParagraph(aParaIdx: Integer): Td2dUnionSlice;
var
l_Para : Td2dTextPara;
l_ParaSlice : Td2dUnionSlice;
l_Line : Td2dUnionSlice;
l_Start : TChunkCursor;
l_LastBreak : TChunkCursor;
l_LastNewBreak : TChunkCursor;
l_NewBreak : TChunkCursor;
l_IsEnd : Boolean;
l_CurPos: Td2dPoint;
l_Slice : Td2dCustomSlice;
I, J : Integer;
l_LWidth: Single;
l_LastLinkChunk : Td2dLinkChunk;
l_LastLinkSlice : Td2dLinkSlice;
function CalcTextWidth(aChunk: Integer; aFrom, aTo: Integer): Single;
var
l_Str: string;
l_Size: Td2dPoint;
begin
Assert(l_Para.Chunks[aChunk].ChunkType in [ctText, ctLink]);
l_Str := Copy(Td2dStringChunk(l_Para.Chunks[aChunk]).Text, aFrom, aTo - aFrom + 1);
Td2dStringChunk(l_Para.Chunks[aChunk]).Font.CalcSize(l_Str, l_Size);
Result := l_Size.X;
end;
function CalcWidth(aFrom, aTo: TChunkCursor): Single;
var
l_From: TChunkCursor;
begin
Assert((aFrom.rChunk <= aTo.rChunk));
Result := 0;
l_From := aFrom;
while l_From.rChunk < aTo.rChunk do
begin
case l_Para.Chunks[l_From.rChunk].ChunkType of
ctText, ctLink : Result := Result + CalcTextWidth(l_From.rChunk, l_From.rPos, MaxInt);
ctPicture : Result := Result + Td2dPictureChunk(l_Para.Chunks[l_From.rChunk]).Picture.Width;
end;
Inc(l_From.rChunk);
l_From.rPos := 1;
end;
case l_Para.Chunks[l_From.rChunk].ChunkType of
ctText, ctLink : Result := Result + CalcTextWidth(l_From.rChunk, l_From.rPos, aTo.rPos);
ctPicture : Result := Result + Td2dPictureChunk(l_Para.Chunks[l_From.rChunk]).Picture.Width;
end;
end;
function IncChunkCursor(var theCur: TChunkCursor): Boolean;
var
l_Ch: Td2dCustomTextChunk;
l_CC: TChunkCursor;
begin
l_CC := theCur;
l_Ch := l_Para.Chunks[l_CC.rChunk];
case l_Ch.ChunkType of
ctText, ctLink:
begin
Inc(l_CC.rPos);
if l_CC.rPos > Length(Td2dStringChunk(l_Ch).f_Text) then
begin
Inc(l_CC.rChunk);
l_CC.rPos := 1;
end;
end;
ctPicture:
begin
Inc(l_CC.rChunk);
l_CC.rPos := 1;
end;
end;
Result := l_CC.rChunk < l_Para.Count;
if Result then
theCur := l_CC;
end;
function ScanToNextBreak(var theCur: TChunkCursor): Boolean;
var
l_Text: string;
l_Changed: Boolean;
begin
Result := False;
// if current chunk is picture then break would be at the beginning of a next chunk
if l_Para.Chunks[theCur.rChunk].ChunkType = ctPicture then
begin
if Pred(l_Para.Count) = theCur.rChunk then
Result := True
else
begin
Inc(theCur.rChunk);
theCur.rPos := 1;
end;
Exit;
end;
l_Changed := False;
while True do
begin
l_Text := Td2dStringChunk(l_Para.Chunks[theCur.rChunk]).Text;
while theCur.rPos < Length(l_Text) do
begin
l_Changed := True;
Inc(theCur.rPos);
if l_Text[theCur.rPos] in [' ', '-'] then
Exit;
end;
if Pred(l_Para.Count) = theCur.rChunk then
begin
Result := True; // it's
Exit;
end;
if l_Para.Chunks[theCur.rChunk+1].ChunkType = ctPicture then
begin
if not l_Changed then
begin
Inc(theCur.rChunk);
theCur.rPos := 1;
end;
Exit;
end;
Inc(theCur.rChunk);
theCur.rPos := 1;
end;
end;
function CompareCursors(const aCur1, aCur2: TChunkCursor): Integer;
begin
if aCur1.rChunk < aCur2.rChunk then
Result := -1
else
if aCur1.rChunk > aCur2.rChunk then
Result := 1
else
if aCur1.rPos < aCur2.rPos then
Result := -1
else
if aCur1.rPos > aCur2.rPos then
Result := 1
else
Result := 0;
end;