Repository navigation
Expand file tree
/
Copy pathDzHTMLText.pas
More file actions
1335 lines (1131 loc) · 33 KB
/
Copy pathDzHTMLText.pas
File metadata and controls
1335 lines (1131 loc) · 33 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
{------------------------------------------------------------------------------
TDzHTMLText component
Developed by Rodrigo Depiné Dalpiaz (digao dalpiaz)
Label with formatting tags support
https://github.com/digao-dalpiaz/DzHTMLText
Please, read the documentation at GitHub link.
Supported Tags:
<A[:abc]></A> - Link
<B></B> - Bold
<I></I> - Italic
<U></U> - Underline
<S></S> - Strike out
<FN:abc></FN> - Font Name
<FS:123></FS> - Font Size
<FC:clColor|$999999></FC> - Font Color
<BC:clColor|$999999></BC> - Background Color
<BR> - Line Break
<L></L> - Align Left
<C></C> - Align Center
<R></R> - Align Right
<T:123> - Tab
<TF:123> - Tab with aligned break
------------------------------------------------------------------------------}
unit DzHTMLText;
{$IFDEF FPC}{$mode delphi}{$ENDIF}
interface
uses
{$IFDEF FPC}
Controls, Classes, Messages, Graphics, Types, FGL, LCLIntf
{$ELSE}
Vcl.Controls, System.Classes, Winapi.Messages,
System.Generics.Collections, Vcl.Graphics, System.Types
{$ENDIF};
type
{$IFDEF FPC}
TObjectList<T> = class(TFPGObjectList<T>);
TList<T> = class(TFPGList<T>);
{$ENDIF}
{DHWord is an object to each word. Will be used to paint event.
The words are separated by space/tag/line break.}
TDHWord = class
private
Rect: TRect;
Text: String;
Group: Integer; //group number
{The group is isolated at each line or tabulation to delimit text align area}
Align: TAlignment;
Font: TFont;
BColor: TColor; //background color
Link: Boolean; //is a link
LinkID: Integer; //link number
{The link number is created sequentially, when reading text links
and works to know the link target, stored on a TStringList, because if
the link was saved here at a work, it will be repeat if has multiple words
per link, spending a lot of unnecessary memory.}
Space: Boolean; //is an space
Hover: Boolean; //the mouse is over the link
public
constructor Create;
destructor Destroy; override;
end;
TDHWordList = class(TObjectList<TDHWord>)
private
procedure Add(Rect: TRect; Text: String; Group: Integer; Align: TAlignment;
Font: TFont; BColor: TColor; Link: Boolean; LinkID: Integer; Space: Boolean);
{$IFDEF FPC}reintroduce;{$ENDIF}
end;
TDzHTMLText = class;
TDHKindStyleLinkProp = (tslpNormal, tslpHover); //kind of link style
{DHStyleLinkProp is a sub-property used at Object Inspector that contains
link formatting when selected and not selected}
TDHStyleLinkProp = class(TPersistent)
private
Lb: TDzHTMLText; //owner
Kind: TDHKindStyleLinkProp;
FFontColor: TColor;
FBackColor: TColor;
FUnderline: Boolean;
procedure SetFontColor(const Value: TColor);
procedure SetBackColor(const Value: TColor);
procedure SetUnderline(const Value: Boolean);
function GetDefaultFontColor: TColor;
function GetStoredFontColor: Boolean;
procedure SetPropsToCanvas(C: TCanvas); //method to use at paint event
function GetStored: Boolean; //GetStored general to use at owner
protected
function GetOwner: TPersistent; override;
public
constructor Create(xLb: TDzHTMLText; xKind: TDHKindStyleLinkProp);
procedure Assign(Source: TPersistent); override;
published
property FontColor: TColor read FFontColor write SetFontColor stored GetStoredFontColor;
property BackColor: TColor read FBackColor write SetBackColor default clNone;
property Underline: Boolean read FUnderline write SetUnderline default False;
end;
TDHLinkData = class
private
FTarget: String;
FText: String;
public
property Target: String read FTarget;
property Text: String read FText;
end;
TDHLinkDataList = class(TObjectList<TDHLinkData>);
TDHEvLink = procedure(Sender: TObject; LinkID: Integer; LinkData: TDHLinkData) of object;
TDHEvLinkClick = procedure(Sender: TObject; LinkID: Integer; LinkData: TDHLinkData; var Handled: Boolean) of object;
TDzHTMLText = class(TGraphicControl)
private
FAbout: String;
LWords: TDHWordList; //word list to paint event
LLinkData: TDHLinkDataList; //list of links info
FText: String;
FAutoWidth: Boolean;
FAutoHeight: Boolean;
FMaxWidth: Integer; //max width when using AutoWidth
//FTransparent: Boolean; //not used because of flickering
FAutoOpenLink: Boolean; //link auto-open with ShellExecute
FLines: Integer; //read-only
FTextWidth: Integer; //read-only
FTextHeight: Integer; //read-only
FStyleLinkNormal, FStyleLinkHover: TDHStyleLinkProp;
FOnLinkEnter, FOnLinkLeave: TDHEvLink;
FOnLinkClick, FOnLinkRightClick: TDHEvLinkClick;
FIsLinkHover: Boolean; //if has a selected link
FSelectedLinkID: Integer; //selected link ID
NoCursorChange: Boolean; //lock CursorChange event
DefaultCursor: TCursor; //default cursor when not over a link
procedure SetText(const Value: String);
procedure SetAutoHeight(const Value: Boolean);
procedure SetAutoWidth(const Value: Boolean);
procedure SetMaxWidth(const Value: Integer);
function GetStoredStyleLink(const Index: Integer): Boolean;
procedure SetStyleLink(const Index: Integer; const Value: TDHStyleLinkProp);
procedure DoPaint;
procedure Rebuild; //rebuild words
procedure BuildAndPaint; //rebuild and repaint
procedure CheckMouse(X, Y: Integer);
procedure SetCursorWithoutChange(C: TCursor); //check links by mouse position
//procedure SetTransparent(const Value: Boolean);
protected
procedure Loaded; override;
procedure Paint; override;
procedure Click; override;
procedure Resize; override;
procedure CMColorchanged(var Message: TMessage); message CM_COLORCHANGED;
procedure CMFontchanged(var Message: TMessage); message CM_FONTCHANGED;
procedure MouseMove(Shift: TShiftState; X: Integer; Y: Integer); override;
procedure CMMouseleave(var Message: TMessage); message CM_MOUSELEAVE;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X: Integer;
Y: Integer); override;
procedure CMCursorchanged(var Message: TMessage); message CM_CURSORCHANGED;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property IsLinkHover: Boolean read FIsLinkHover;
property SelectedLinkID: Integer read FSelectedLinkID;
function GetLinkData(LinkID: Integer): TDHLinkData; //get data by link id
function GetSelectedLinkData: TDHLinkData; //get data of selected link
published
property Align;
property Anchors;
property Color;
property Font;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Visible;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
{$IFDEF DCC}
property OnGesture;
property OnMouseActivate;
{$ENDIF}
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
property Text: String read FText write SetText;
//property Transparent: Boolean read FTransparent write SetTransparent default False;
property AutoWidth: Boolean read FAutoWidth write SetAutoWidth default False;
property AutoHeight: Boolean read FAutoHeight write SetAutoHeight default False;
property MaxWidth: Integer read FMaxWidth write SetMaxWidth default 0;
property StyleLinkNormal: TDHStyleLinkProp index 1 read FStyleLinkNormal write SetStyleLink stored GetStoredStyleLink;
property StyleLinkHover: TDHStyleLinkProp index 2 read FStyleLinkHover write SetStyleLink stored GetStoredStyleLink;
property Lines: Integer read FLines;
property TextWidth: Integer read FTextWidth;
property TextHeight: Integer read FTextHeight;
property OnLinkEnter: TDHEvLink read FOnLinkEnter write FOnLinkEnter;
property OnLinkLeave: TDHEvLink read FOnLinkLeave write FOnLinkLeave;
property OnLinkClick: TDHEvLinkClick read FOnLinkClick write FOnLinkClick;
property OnLinkRightClick: TDHEvLinkClick read FOnLinkRightClick write FOnLinkRightClick;
property AutoOpenLink: Boolean read FAutoOpenLink write FAutoOpenLink default True;
property About: String read FAbout;
end;
procedure Register;
implementation
uses
{$IFDEF FPC}
{$IFDEF MSWINDOWS}Windows, {$ENDIF}SysUtils, LResources
{$ELSE}
System.SysUtils, System.UITypes, Winapi.Windows, Winapi.ShellAPI
{$ENDIF};
procedure Register;
begin
{$IFDEF FPC}{$I DzHTMLText.lrs}{$ENDIF}
RegisterComponents('Digao', [TDzHTMLText]);
end;
//
constructor TDHWord.Create;
begin
inherited;
Font := TFont.Create;
end;
destructor TDHWord.Destroy;
begin
Font.Free;
inherited;
end;
procedure TDHWordList.Add(Rect: TRect; Text: String; Group: Integer; Align: TAlignment;
Font: TFont; BColor: TColor; Link: Boolean; LinkID: Integer; Space: Boolean);
var W: TDHWord;
begin
W := TDHWord.Create;
inherited Add(W);
W.Rect := Rect;
W.Text := Text;
W.Group := Group;
W.Align := Align;
W.Font.Assign(Font);
W.BColor := BColor;
W.Link := Link;
W.LinkID := LinkID;
W.Space := Space;
end;
//
constructor TDzHTMLText.Create(AOwner: TComponent);
begin
inherited;
ControlStyle := ControlStyle + [csOpaque];
//Warning! The use of transparency in the component causes flickering
FAbout := 'Digao Dalpiaz / Version 1.1';
FStyleLinkNormal := TDHStyleLinkProp.Create(Self, tslpNormal);
FStyleLinkHover := TDHStyleLinkProp.Create(Self, tslpHover);
LWords := TDHWordList.Create;
LLinkData := TDHLinkDataList.Create;
FAutoOpenLink := True;
FSelectedLinkID := -1;
DefaultCursor := Cursor;
{$IFDEF FPC}
//Lazarus object starts too small
Width := 200;
Height := 100;
{$ENDIF}
end;
destructor TDzHTMLText.Destroy;
begin
FStyleLinkNormal.Free;
FStyleLinkHover.Free;
LWords.Free;
LLinkData.Free;
inherited;
end;
procedure TDzHTMLText.Loaded;
begin
{Warning! When a component is inserted at design-time, the Loaded
is not fired, because there is nothing to load. The Loaded is only fired
when loading component that already has saved properties on DFM file.}
inherited;
Rebuild;
end;
procedure TDzHTMLText.BuildAndPaint;
begin
//Rebuild words and repaint
Rebuild;
Invalidate;
end;
procedure TDzHTMLText.SetAutoHeight(const Value: Boolean);
begin
if Value<>FAutoHeight then
begin
FAutoHeight := Value;
if Value then Rebuild;
end;
end;
procedure TDzHTMLText.SetAutoWidth(const Value: Boolean);
begin
if Value<>FAutoWidth then
begin
FAutoWidth := Value;
if Value then Rebuild;
end;
end;
procedure TDzHTMLText.SetMaxWidth(const Value: Integer);
begin
if Value<>FMaxWidth then
begin
FMaxWidth := Value;
Rebuild;
end;
end;
procedure TDzHTMLText.SetText(const Value: String);
begin
if Value<>FText then
begin
FText := Value;
BuildAndPaint;
end;
end;
{procedure TDzHTMLText.SetTransparent(const Value: Boolean);
begin
if Value<>FTransparent then
begin
FTransparent := Value;
Invalidate;
end;
end;}
procedure TDzHTMLText.CMColorchanged(var Message: TMessage);
begin
{$IFDEF FPC}if Message.Result=0 then {};{$ENDIF} //avoid unused var warning
Invalidate;
end;
procedure TDzHTMLText.CMFontchanged(var Message: TMessage);
begin
{$IFDEF FPC}if Message.Result=0 then {};{$ENDIF} //avoid unused var warning
BuildAndPaint;
end;
procedure TDzHTMLText.Resize;
begin
//on component creating, there is no parent and the resize is fired,
//so, the canvas is not present at this moment.
if HasParent then
Rebuild;
inherited;
end;
procedure TDzHTMLText.Paint;
begin
inherited;
DoPaint;
end;
procedure TDzHTMLText.DoPaint;
var W: TDHWord;
B: {$IFDEF DCC}Vcl.{$ENDIF}Graphics.TBitmap;
begin
//Using internal bitmap as a buffer to reduce flickering
B := {$IFDEF DCC}Vcl.{$ENDIF}Graphics.TBitmap.Create;
try
B.SetSize(Width, Height);
//if not FTransparent then
//begin
{$IFDEF FPC}
if (Color=clDefault) and (ParentColor) then B.Canvas.Brush.Color := GetColorresolvingParent else
{$ENDIF}
B.Canvas.Brush.Color := Color;
B.Canvas.FillRect(ClientRect);
//end;
if csDesigning in ComponentState then
begin
B.Canvas.Pen.Style := psDot;
B.Canvas.Pen.Color := clBtnShadow;
B.Canvas.Brush.Style := bsClear;
B.Canvas.Rectangle(ClientRect);
end;
for W in LWords do
begin
B.Canvas.Font.Assign(W.Font);
if W.BColor<>clNone then
B.Canvas.Brush.Color := W.BColor
else
B.Canvas.Brush.Style := bsClear;
if W.Link then
begin
if W.Hover then //selected
FStyleLinkHover.SetPropsToCanvas(B.Canvas)
else
FStyleLinkNormal.SetPropsToCanvas(B.Canvas);
end;
DrawText(B.Canvas.Handle,
{$IFDEF FPC}PChar({$ENDIF}W.Text{$IFDEF FPC}){$ENDIF},
-1, W.Rect, DT_NOCLIP or DT_NOPREFIX);
{Using DrawText, because TextOut has not clip option, which causes
bad overload of text when painting using background, oversizing the
text area wildly.}
end;
Canvas.Draw(0, 0, B); //to reduce flickering
finally
B.Free;
end;
end;
function TDzHTMLText.GetLinkData(LinkID: Integer): TDHLinkData;
begin
Result := LLinkData[LinkID];
end;
function TDzHTMLText.GetSelectedLinkData: TDHLinkData;
begin
Result := LLinkData[FSelectedLinkID];
end;
procedure TDzHTMLText.CMCursorchanged(var Message: TMessage);
begin
{$IFDEF FPC}if Message.Result=0 then {};{$ENDIF} //avoid unused var warning
if NoCursorChange then Exit;
DefaultCursor := Cursor; //save default cursor to when link not selected
end;
procedure TDzHTMLText.SetCursorWithoutChange(C: TCursor);
begin
//Set cursor, but without fire cursor change event
NoCursorChange := True;
try
Cursor := C;
finally
NoCursorChange := False;
end;
end;
procedure TDzHTMLText.CheckMouse(X, Y: Integer);
var FoundHover, HasChange, Old: Boolean;
LinkID: Integer;
W: TDHWord;
begin
FoundHover := False;
HasChange := False;
LinkID := -1;
//find the first word, if there is any
for W in LWords do
if W.Link then
begin
if W.Rect.Contains({$IFDEF FPC}Types.{$ENDIF}Point(X, Y)) then //selected
begin
FoundHover := True; //found word of a link selected
LinkID := W.LinkID;
Break;
end;
end;
//set as selected all the words of same link, and unselect another links
for W in LWords do
if W.Link then
begin
Old := W.Hover;
W.Hover := (W.LinkID = LinkID);
if Old<>W.Hover then HasChange := True; //changed
end;
if HasChange then //there is any change
begin
if FoundHover then //enter the link
begin
SetCursorWithoutChange(crHandPoint); //set HandPoint cursor
FIsLinkHover := True;
FSelectedLinkID := LinkID;
if Assigned(FOnLinkEnter) then
FOnLinkEnter(Self, LinkID, LLinkData[LinkID]);
end else
begin //leave the link
SetCursorWithoutChange(DefaultCursor); //back to default cursor
FIsLinkHover := False;
LinkID := FSelectedLinkID; //save to use on OnLinkLeave event
FSelectedLinkID := -1;
if Assigned(FOnLinkLeave) then
FOnLinkLeave(Self, LinkID, LLinkData[LinkID]);
end;
Invalidate;
end;
end;
procedure TDzHTMLText.Click;
var Handled: Boolean;
aTarget: String;
begin
if FIsLinkHover then
begin
Handled := False;
if Assigned(FOnLinkClick) then
FOnLinkClick(Self, FSelectedLinkID, LLinkData[FSelectedLinkID], Handled);
if FAutoOpenLink and not Handled then
begin
aTarget := LLinkData[FSelectedLinkID].FTarget;
{$IFDEF MSWINDOWS}
ShellExecute(0, '', PChar(aTarget), '', '', 0);
{$ELSE}
if aTarget.StartsWith('http://', True)
or aTarget.StartsWith('https://', True)
or aTarget.StartsWith('www.', True)
then
OpenURL(aTarget)
else
OpenDocument(aTarget);
{$ENDIF}
end;
end;
inherited;
end;
procedure TDzHTMLText.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
var Handled: Boolean;
begin
if Button = mbRight then
if IsLinkHover then
if Assigned(FOnLinkRightClick) then
begin
Handled := False;
FOnLinkRightClick(Self, FSelectedLinkID, LLinkData[FSelectedLinkID], Handled);
end;
inherited;
end;
procedure TDzHTMLText.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
CheckMouse(X, Y);
inherited;
end;
procedure TDzHTMLText.CMMouseleave(var Message: TMessage);
begin
//Mouse leaves the component
CheckMouse(-1, -1);
inherited;
end;
//
type
TTokenKind = (
ttInvalid,
ttBold, ttItalic, ttUnderline, ttStrike,
ttFontName, ttFontSize, ttFontColor, ttBackColor,
ttTab, ttTabF, ttSpace,
ttBreak, ttText, ttLink,
ttAlignLeft, ttAlignCenter, ttAlignRight);
TToken = class
Kind: TTokenKind;
TagClose: Boolean;
Text: String;
Value: Integer;
end;
TListToken = class(TObjectList<TToken>)
function GetLinkText(IEnd: Integer): String;
end;
TBuilder = class
private
Lb: TDzHTMLText;
L: TListToken;
LGroupBound: TList<Integer>; //bounds list of the group
{The list of created with the X position of limit where the group ends
to use on text align until the group limit}
CalcWidth, CalcHeight: Integer; //width and height to set at component when using auto
function ProcessTag(Tag: String): Boolean;
procedure AddToken(aKind: TTokenKind; aTagClose: Boolean = False; aText: String = ''; aValue: Integer = 0);
procedure BuildTokens; //create list of tokens
procedure BuildWords; //create list of words
procedure CheckAligns; //realign words
public
constructor Create;
destructor Destroy; override;
end;
constructor TBuilder.Create;
begin
inherited;
L := TListToken.Create;
LGroupBound := TList<Integer>.Create;
end;
destructor TBuilder.Destroy;
begin
L.Free;
LGroupBound.Free;
inherited;
end;
procedure TDzHTMLText.Rebuild;
var B: TBuilder;
begin
if csLoading in ComponentState then Exit;
LWords.Clear; //clean old words
LLinkData.Clear; //clean old links
B := TBuilder.Create;
try
B.Lb := Self;
B.BuildTokens;
B.BuildWords;
B.CheckAligns;
FTextWidth := B.CalcWidth;
FTextHeight := B.CalcHeight;
if FAutoWidth then Width := B.CalcWidth;
if FAutoHeight then Height := B.CalcHeight;
finally
B.Free;
end;
end;
//
function ReplaceForcedChars(A: String): String;
begin
//Allow tag characters at text
A := StringReplace(A, '<', '<', [rfReplaceAll]);
A := StringReplace(A, '>', '>', [rfReplaceAll]);
Result := A;
end;
function ParamToColor(A: String): TColor;
begin
if A.StartsWith('$') then Insert('00', A, 2);
{At HTML, is used Hexadecimal color code with 6 digits, the same used at
this component. However the Delphi works with 8 digits, but the first two
digits are always "00"}
try
Result := StringToColor(A);
except
Result := clNone;
end;
end;
procedure TBuilder.AddToken(aKind: TTokenKind; aTagClose: Boolean = False; aText: String = ''; aValue: Integer = 0);
var T: TToken;
begin
T := TToken.Create;
T.Kind := aKind;
T.TagClose := aTagClose;
T.Text := aText;
T.Value := aValue;
L.Add(T);
end;
function TBuilder.ProcessTag(Tag: String): Boolean;
var TOff, TOn, HasPar: Boolean;
Kind: TTokenKind;
Value: Integer;
A, Par: String;
I: Integer;
begin
//Result=True means valid tag
Result := False;
A := Tag;
TOff := False;
if A.StartsWith('/') then //closing tag
begin
TOff := True;
Delete(A, 1, 1);
end;
TOn := not TOff;
HasPar := False;
Par := '';
I := Pos(':', A); //find parameter
if I>0 then //has parameter
begin
HasPar := True;
Par := A;
Delete(Par, 1, I);
//Par := Copy(A, I+1, Length(A)-I);
A := Copy(A, 1, I-1);
end;
if TOff and HasPar then Exit; //tag closing with parameter
Value := 0;
A := UpperCase(A);
if (A='BR') and TOn and not HasPar then //LINE BREAK
begin
AddToken(ttBreak);
Result := True;
end else
if (A='B') and not HasPar then //BOLD
begin
AddToken(ttBold, TOff);
Result := True;
end else
if (A='I') and not HasPar then //ITALIC
begin
AddToken(ttItalic, TOff);
Result := True;
end else
if (A='U') and not HasPar then //UNDERLINE
begin
AddToken(ttUnderline, TOff);
Result := True;
end else
if (A='S') and not HasPar then //STRIKEOUT
begin
AddToken(ttStrike, TOff);
Result := True;
end else
if A='FN' then //FONT NAME
begin
if TOn and (Par='') then Exit;
AddToken(ttFontName, TOff, Par);
Result := True;
end else
if A='FS' then //FONT SIZE
begin
if TOn then
begin
Value := StrToIntDef(Par, 0);
if Value<=0 then Exit;
end;
AddToken(ttFontSize, TOff, '', Value);
Result := True;
end else
if A='FC' then //FONT COLOR
begin
if TOn then
begin
Value := ParamToColor(Par);
if Value=clNone then Exit;
end;
AddToken(ttFontColor, TOff, '', Value);
Result := True;
end else
if A='BC' then //BACKGROUND COLOR
begin
if TOn then
begin
Value := ParamToColor(Par);
if Value=clNone then Exit;
end;
AddToken(ttBackColor, TOff, '', Value);
Result := True;
end else
if A='A' then //LINK
begin
if TOn and HasPar and (Par='') then Exit;
AddToken(ttLink, TOff, Par);
Result := True;
end else
if (A='L') and not HasPar then //ALIGN LEFT
begin
AddToken(ttAlignLeft, TOff);
Result := True;
end else
if (A='C') and not HasPar then //ALIGN CENTER
begin
AddToken(ttAlignCenter, TOff);
Result := True;
end else
if (A='R') and not HasPar then //ALIGN RIGHT
begin
AddToken(ttAlignRight, TOff);
Result := True;
end else
if ((A='T') or (A='TF')) and TOn then //TAB
begin
Value := StrToIntDef(Par, 0);
if Value<=0 then Exit;
Kind := ttTab;
if A='TF' then Kind := ttTabF;
AddToken(Kind, TOff, '', Value);
Result := True;
end;
end;
procedure TBuilder.BuildTokens;
var Text, A: String;
CharIni: Char;
I, Jump: Integer;
begin
Text := StringReplace(Lb.FText, #13#10, '<BR>', [rfReplaceAll]);
while Text<>'' do
begin
A := Text;
CharIni := A[1];
if CharIni = '<' then //starts with tag opening
begin
Delete(A, 1, 1);
I := Pos('>', A); //find tag closing
if I>0 then
begin
A := Copy(A, 1, I-1);
if not ProcessTag(A) then AddToken(ttInvalid);
Jump := 1+Length(A)+1;
end else
begin
//losted tag opening
AddToken(ttInvalid);
Jump := 1;
end;
end else
if CharIni = '>' then
begin
//losted tag closing
AddToken(ttInvalid);
Jump := 1;
end else
if CharIni = ' ' then //space
begin
AddToken(ttSpace, False, ' ');
Jump := 1;
end else
if CharInSet(CharIni, ['/','\']) then
begin
//this is to break line when using paths
AddToken(ttText, False, CharIni);
Jump := 1;
end else
begin //all the rest is text
I := A.IndexOfAny([' ','<','>','/','\'])+1; //warning: 0-based function!!!
if I=0 then I := Length(A)+1;
Dec(I);
A := Copy(A, 1, I);
AddToken(ttText, False, ReplaceForcedChars(A));
Jump := I;
end;
Delete(Text, 1, Jump);
end;
end;
type
TListStack<T> = class(TList<T>)
procedure AddOrDel(Token: TToken; const XValue: T);
end;
procedure TListStack<T>.AddOrDel(Token: TToken; const XValue: T);
begin
if Token.TagClose then
begin
if Count>1 then
Delete(Count-1);
end else
Add(XValue);
end;
procedure TBuilder.BuildWords;
var C: TCanvas;
X, Y, HighW, HighH, LineCount: Integer;
LastTabF: Boolean; //last tabulation was TabF (with break align)
LastTabF_X: Integer;
procedure DoLineBreak;
begin
if HighH=0 then HighH := C.TextHeight(' '); //line without content
Inc(Y, HighH); //inc biggest height of the line
HighH := 0; //clear line height
if X>HighW then HighW := X; //store width of biggest line
X := 0; //carriage return :)
if LastTabF then X := LastTabF_X; //last line breaks with TabF
LGroupBound.Add(Lb.Width); //add line bound to use in group align
Inc(LineCount);
end;
var
T: TToken;
I: Integer;
Ex: TSize; FS: TFontStyles; PreWidth: Integer;
LinkOn: Boolean;
LinkID: Integer;
BackColor: TColor;
Align: TAlignment;
LBold: TListStack<Boolean>;
LItalic: TListStack<Boolean>;
LUnderline: TListStack<Boolean>;
LStrike: TListStack<Boolean>;
LFontName: TListStack<String>;
LFontSize: TListStack<Integer>;
LFontColor: TListStack<TColor>;
LBackColor: TListStack<TColor>;
LAlign: TListStack<TAlignment>;
LinkData: TDHLinkData;
vBool: Boolean; //Required for Lazarus
begin
C := Lb.Canvas;
C.Font.Assign(Lb.Font);
BackColor := clNone;
Align := taLeftJustify;