Skip to content

Commit

Permalink
Merge pull request #81 from digao-dalpiaz/reintroduce-tab
Browse files Browse the repository at this point in the history
Reintroduce tab
  • Loading branch information
digao-dalpiaz authored Jan 21, 2024
2 parents 11ed888 + 6b7c805 commit 216f3e5
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CompInstall.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[General]
Name=Digao Dalpiaz - DzHTMLText component
Version=5.1
Version=5.2
DelphiVersions=XE3;XE4;XE5;XE6;XE7;XE8;10;10.1;10.2;10.3;10.4;11
Packages=DzHTMLText_VCL;DzHTMLText_FMX
AddLibrary=1
Expand Down
2 changes: 1 addition & 1 deletion FMX.DHCommon.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
unit FMX.DHCommon;
unit FMX.DHCommon;
{$DEFINE FMX}
{$INCLUDE 'Vcl.DHCommon.pas'}

2 changes: 1 addition & 1 deletion FMX.DHTokenEngine.pas
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
unit FMX.DHTokenEngine;
unit FMX.DHTokenEngine;
{$DEFINE FMX}
{$INCLUDE 'Vcl.DHTokenEngine.pas'}
2 changes: 1 addition & 1 deletion FMX.DzHTMLText.pas
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
unit FMX.DzHTMLText;
unit FMX.DzHTMLText;
{$DEFINE FMX}
{$INCLUDE 'Vcl.DzHTMLText.pas'}
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [Link object](#link-object)
- [Image Tag](#image-tag)
- [Div Tag](#div-tag)
- [Tab Tag](#tab-tag)
- [Literal tag character](#literal-tag-character)
- [Chinese/Japanese/Korean line break](#chinesejapanesekorean-line-break)
- [Auto Scaling](#auto-scaling)
Expand All @@ -33,19 +34,25 @@

## What's New

- 01/12/2024 (Version 5.1)
- 01/21/2024 (Version 5.2)

- Improved VCL x FMX x HTML color notation. Please read Color Notation topic.
- Fixed left margin after List tags (Unordered and Ordered lists).
- Reintroduced `<T>` and `<TF>` tags.
- New PlainText and GeneratePlainText properties (public) - removed HTMLToPlainText method.

<details>
<summary>Click here to view the entire changelog</summary>

- 01/12/2024 (Version 5.1)

- Improved VCL x FMX x HTML color notation. Please read Color Notation topic.

- 01/05/2024 (Version 5.0)

| :exclamation: Component breaking changes |
|----------------------------------------------------|
| Tags `<T>`, `<TF>` and `<FLOAT>` have been removed |
| Please use new tag `<DIV>` |
| :exclamation: Component breaking changes |
|---------------------------------------------------------------------------------------------------|
| Tags `<T>`, `<TF>` and `<FLOAT>` have been removed (`<T>` and `<TF>` reintroduced in version 5.2) |
| Please use new tag `<DIV>` |

- **NEW COMPONENT ENGINE!!!**
- Improved token processing performance
Expand Down Expand Up @@ -413,13 +420,20 @@ This visual component allows you to specify a formatted text in a label, using a
<VALIGN:top|center|bottom></VALIGN> - Aligning content vertically to the line
<OFFSET:[top=123],[bottom=456]></OFFSET> - Content margin spacing
Offset margins are memorized if a new offset tag is specifyed without same parameter name
<T:123> = Tab - left margin offset
<TF:123> = Tab with continuous lines aligned

* COLOR_VALUE - clColor(VCL)|Color(FMX)|$00GGBBRR|#AARRGGBB|#RRGGBB
* When FMX, all sizes (TPixels) use the "." notation as a decimal separator
```
> The tags notation is case-insensitive, so you can use `<B>Text</B>` or `<b>Text</b>`.
Tags must follow the hierarchy as they were opened:
- `<b><i>text</i></b>` = CORRECT
- `<b><i>text</b></i>` = WRONG
![Runtime example](images/runtime_print.png)
## Installing
Expand Down Expand Up @@ -682,6 +696,13 @@ The div tag may be floating, using specific X and Y position, or docked to the c

Please, refer to all possible parameters in [Available tags](#available-tags).

## Tab Tag

There are two **tab** tags you can use:

- `<t:nnn>` = Allow you to positioning text exactly on "nnn" position in pixels starting on the left border of component. If the text wraps to a new line, it will be return aligned at left border of component.
- `<tf:nnn>` = The same as above, but if the text wraps to a new line, it will be aligned in the same position as the first line which the tab started. This tag will produce a better visual text alignment.

## Literal tag character

If you want to display literal special characters in the text, just type the HTML code:
Expand Down
2 changes: 1 addition & 1 deletion Vcl.DHCommon.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{$IFNDEF FMX}unit Vcl.DHCommon;{$ENDIF}
{$IFNDEF FMX}unit Vcl.DHCommon;{$ENDIF}

{$INCLUDE Defines.inc}

Expand Down
68 changes: 60 additions & 8 deletions Vcl.DHTokenEngine.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{$IFNDEF FMX}unit Vcl.DHTokenEngine;{$ENDIF}
{$IFNDEF FMX}unit Vcl.DHTokenEngine;{$ENDIF}

{$INCLUDE Defines.inc}

Expand Down Expand Up @@ -150,12 +150,24 @@ TDHToken_ImageResource = class(TDHTokenSingle)
procedure ReadParam; override;
procedure Process; override;
end;

TDHToken_TabBase = class abstract(TDHTokenSingle)
private
Margin: TPixels;

procedure ReadParam; override;
procedure Process; override;

function IsBreakableToken: Boolean; override;
end;
TDHToken_Tab = class(TDHToken_TabBase);
TDHToken_TabF = class(TDHToken_TabBase);
{$ENDREGION}

{$REGION 'Token block classes'}
TDHToken_Main = class(TDHTokenBlock);

TDHToken_FontStyleItem = class(TDHTokenBlock)
TDHToken_FontStyleItem = class abstract(TDHTokenBlock)
private
Disabled: Boolean;

Expand Down Expand Up @@ -184,7 +196,7 @@ TDHToken_FontSize = class(TDHTokenBlock)
procedure Process; override;
end;

TDHToken_Color = class(TDHTokenBlock)
TDHToken_Color = class abstract(TDHTokenBlock)
private
Color: TAnyColor;

Expand Down Expand Up @@ -257,7 +269,7 @@ TDHToken_Offset = class(TDHTokenBlock)
procedure Process; override;
end;

TDHToken_Spoiler = class(TDHTokenBlock)
TDHToken_Spoiler = class abstract(TDHTokenBlock)
private
Ident: string;
end;
Expand All @@ -274,14 +286,14 @@ TDHToken_SpoilerDetail = class(TDHToken_Spoiler)
procedure Process; override;
end;

TDHToken_SuperOrSubScript = class(TDHTokenBlock)
TDHToken_SuperOrSubScript = class abstract(TDHTokenBlock)
private
procedure Process; override;
end;
TDHToken_Superscript = class(TDHToken_SuperOrSubScript);
TDHToken_Subscript = class(TDHToken_SuperOrSubScript);

TDHToken_List = class(TDHTokenBlock)
TDHToken_List = class abstract(TDHTokenBlock)
private
procedure Process; override;
end;
Expand Down Expand Up @@ -492,12 +504,14 @@ TDHTokenObjectDef = record
end;

const
TOKENS_OBJECTS: array[0..29] of TDHTokenObjectDef = (
TOKENS_OBJECTS: array[0..31] of TDHTokenObjectDef = (
//single
(Ident: 'BR'; Clazz: TDHToken_Break; AllowPar: True; OptionalPar: True), //breakable!
(Ident: 'LINE'; Clazz: TDHToken_Line; AllowPar: True; OptionalPar: True),
(Ident: 'IMG'; Clazz: TDHToken_Image; AllowPar: True),
(Ident: 'IMGRES'; Clazz: TDHToken_ImageResource; AllowPar: True),
(Ident: 'T'; Clazz: TDHToken_Tab; AllowPar: True), //breakable!
(Ident: 'TF'; Clazz: TDHToken_TabF; AllowPar: True), //breakable!

//block
(Ident: 'B'; Clazz: TDHToken_Bold; AllowPar: True; OptionalPar: True),
Expand Down Expand Up @@ -968,6 +982,33 @@ procedure TDHToken_Line.Process;
Builder.AddVisualItemToQueue(V, Size);
end;

{ TDHToken_TabBase }

function TDHToken_TabBase.IsBreakableToken: Boolean;
begin
Result := True;
end;

procedure TDHToken_TabBase.ReadParam;
begin
Margin := Lb.CalcScale(StrToPixels(Param, -1));
ValidParam := Margin>=0;
end;

procedure TDHToken_TabBase.Process;
var
DivMargin: TPixels;
begin
Builder.CurrentDiv.Point.X := Margin;

if Self is TDHToken_TabF then
DivMargin := Margin
else
DivMargin := 0;

Builder.CurrentDiv.LeftMargin := DivMargin;
end;

{ TDHToken_AlignLeft }

procedure TDHToken_AlignLeft.Process;
Expand Down Expand Up @@ -1359,7 +1400,7 @@ function TDHOffsetRec.GetHeight: TPixels;
destructor TDHPreVisualItem.Destroy;
begin
if VisualObject<>nil then VisualObject.Free;
if SelfDiv<>nil then SelfDiv.Free;
if SelfDiv<>nil then SelfDiv.Free;

inherited;
end;
Expand Down Expand Up @@ -1892,6 +1933,9 @@ function TDHBuilder.JumpLine(Continuous: Boolean): TDHDivAreaLine;
begin
EndOfLine; //must always contains lines here

if not Continuous and (Props.List_Level=0) then
CurrentDiv.LeftMargin := 0;

Result := AddNewLineObject(Continuous);
end;

Expand Down Expand Up @@ -2076,11 +2120,14 @@ procedure TDHBuilder.SendObjectsToComponent(DivArea: TDHDivArea);

var
Line: TDHDivAreaLine;
WrPlainText: Boolean;
begin
PDiv := DivArea.GetAbsoluteStartingPos;

ProcessFloatingDivs(True);

WrPlainText := Lb.GeneratePlainText and (DivArea = MainDiv);

for Line in DivArea.Lines do
begin
for Item in Line.Items do
Expand All @@ -2093,9 +2140,14 @@ procedure TDHBuilder.SendObjectsToComponent(DivArea: TDHDivArea);
Item.SelfDiv.FixedSize.Height := Item.Size.Height; //size of div area for align children
end;

if WrPlainText and (Item.VisualObject is TDHVisualItem_Word) then
Lb.PlainText.Append(TDHVisualItem_Word(Item.VisualObject).Text);

CheckAlign(Item, Line, DivArea);
DoItem;
end;

if WrPlainText then Lb.PlainText.Append(sLineBreak);
end;

ProcessFloatingDivs(False);
Expand Down
38 changes: 14 additions & 24 deletions Vcl.DzHTMLText.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{------------------------------------------------------------------------------
{------------------------------------------------------------------------------
TDzHTMLText component
Developed by Rodrigo Depine Dalpiaz (digao dalpiaz)
Label with formatting tags support
Expand Down Expand Up @@ -29,7 +29,7 @@ interface
{$ENDIF}
{$ENDIF};

const DZHTMLTEXT_INTERNAL_VERSION = 707; //Synchronizes TDam component
const DZHTMLTEXT_INTERNAL_VERSION = 708; //Synchronizes TDam component

const _DEF_LISTLEVELPADDING = 20;

Expand Down Expand Up @@ -363,6 +363,9 @@ TDzHTMLText = class(
ParentForm: TCustomForm;
{$ENDIF}

FGeneratePlainText: Boolean;
FPlainText: TStringBuilder;

procedure OnLinesChange(Sender: TObject);
procedure SetLines(const Value: TStrings);
function GetText: string;
Expand Down Expand Up @@ -457,6 +460,9 @@ TDzHTMLText = class(
procedure SetParent(AParent: TWinControl); override;
{$ENDIF}
public
property GeneratePlainText: Boolean read FGeneratePlainText write FGeneratePlainText;
property PlainText: TStringBuilder read FPlainText;

property Spoilers: TDHSpoilerList read LSpoiler;
property LinkRefs: TDHLinkRefList read LLinkRef;

Expand All @@ -481,7 +487,6 @@ TDzHTMLText = class(

class function UnescapeHTMLToText(const aHTML: string): string;
class function EscapeTextToHTML(const aText: string): string;
class function HTMLToPlainText(const aHTML: string): string;
published
property Align;
property Anchors;
Expand Down Expand Up @@ -652,7 +657,7 @@ implementation
{$ENDIF}
{$ENDIF};

const STR_VERSION = '5.1';
const STR_VERSION = '5.2';

const DEFAULT_PPI = 96;

Expand Down Expand Up @@ -797,26 +802,6 @@ class function TDzHTMLText.UnescapeHTMLToText(const aHTML: string): string;

Result := StringReplace(Result, '&amp;', '&', [rfReplaceAll]);
end;

class function TDzHTMLText.HTMLToPlainText(const aHTML: string): string;
var
X, XEnd: Integer;
begin
Result := aHTML;

while True do
begin
X := Pos('<', Result);
if X=0 then Break;

XEnd := PosEx('>', Result, X+1);
if XEnd=0 then Break;

Delete(Result, X, XEnd-X+1);
end;

Result := UnescapeHTMLToText(Result);
end;
{$ENDREGION}

{$REGION 'TDzHTMLText'}
Expand Down Expand Up @@ -847,6 +832,8 @@ constructor TDzHTMLText.Create(AOwner: TComponent);
FOffset := TDHOffset.Create(Self);
FCustomStyles := TDHCustomStyles.Create(Self);

FPlainText := TStringBuilder.Create;

FCursor := crDefault;

{$IFDEF FMX}
Expand Down Expand Up @@ -876,6 +863,7 @@ destructor TDzHTMLText.Destroy;
VisualItems.Free;
LLinkRef.Free;
LSpoiler.Free;
FPlainText.Free;
inherited;
end;

Expand Down Expand Up @@ -1697,6 +1685,8 @@ procedure TDzHTMLText.Rebuild;
VisualItems.Clear; //clean visual items
LLinkRef.Clear; //clean old links

FPlainText.Clear;

B := TDHBuilder.Create(Self,
{$IFDEF FMX}TCanvasManager.MeasureCanvas{$ELSE}Canvas{$ENDIF},
VisualItems, SetTextSizeAndLineCount);
Expand Down

0 comments on commit 216f3e5

Please sign in to comment.