Skip to content

Commit

Permalink
Merge pull request #27 from digao-dalpiaz/overall-align
Browse files Browse the repository at this point in the history
Overall align
  • Loading branch information
digao-dalpiaz authored Aug 30, 2020
2 parents 48fc621 + c33ff50 commit e159d3c
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 44 deletions.
169 changes: 126 additions & 43 deletions DzHTMLText.pas
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ TDHStyleLinkProp = class(TPersistent)
TDHEvLink = procedure(Sender: TObject; Link: TDHBaseLink) of object;
TDHEvLinkClick = procedure(Sender: TObject; Link: TDHBaseLink; var Handled: Boolean) of object;

TDHLineVertAlign = (vaTop, vaCenter, vaBottom);
TDHVertAlign = (vaTop, vaCenter, vaBottom);
TDHHorzAlign = (haLeft, haCenter, haRight);

TDHEvRetrieveImgRes = procedure(Sender: TObject; const ResourceName: String; Picture: TPicture; var Handled: Boolean) of object;

Expand Down Expand Up @@ -173,7 +174,9 @@ TDzHTMLText = class(TGraphicControl)

FOnRetrieveImgRes: TDHEvRetrieveImgRes;

FLineVertAlign: TDHLineVertAlign;
FLineVertAlign: TDHVertAlign;
FOverallVertAlign: TDHVertAlign;
FOverallHorzAlign: TDHHorzAlign;
FLineSpacing: Integer;
FListLevelPadding: Integer;

Expand Down Expand Up @@ -208,7 +211,9 @@ TDzHTMLText = class(TGraphicControl)
procedure CheckMouse(X, Y: Integer); //check links by mouse position
procedure SetCursorWithoutChange(C: TCursor);
procedure SetImages(const Value: TCustomImageList);
procedure SetLineVertAlign(const Value: TDHLineVertAlign);
procedure SetLineVertAlign(const Value: TDHVertAlign);
procedure SetOverallVertAlign(const Value: TDHVertAlign);
procedure SetOverallHorzAlign(const Value: TDHHorzAlign);
procedure SetLineSpacing(const Value: Integer);
procedure SetListLevelPadding(const Value: Integer);
//procedure SetTransparent(const Value: Boolean);
Expand Down Expand Up @@ -304,7 +309,9 @@ TDzHTMLText = class(TGraphicControl)

property AutoOpenLink: Boolean read FAutoOpenLink write FAutoOpenLink default True;

property LineVertAlign: TDHLineVertAlign read FLineVertAlign write SetLineVertAlign default vaTop;
property LineVertAlign: TDHVertAlign read FLineVertAlign write SetLineVertAlign default vaTop;
property OverallVertAlign: TDHVertAlign read FOverallVertAlign write SetOverallVertAlign default vaTop;
property OverallHorzAlign: TDHHorzAlign read FOverallHorzAlign write SetOverallHorzAlign default haLeft;
property LineSpacing: Integer read FLineSpacing write SetLineSpacing default 0;
property ListLevelPadding: Integer read FListLevelPadding write SetListLevelPadding default _DEF_LISTLEVELPADDING;

Expand Down Expand Up @@ -452,7 +459,7 @@ constructor TDzHTMLText.Create(AOwner: TComponent);
ControlStyle := ControlStyle + [csOpaque];
//Warning! The use of transparency in the component causes flickering

FAbout := 'Digao Dalpiaz / Version 2.2';
FAbout := 'Digao Dalpiaz / Version 2.3';

FLines := TStringList.Create;
//FLines.TrailingLineBreak := False; -- only supported by Delphi 10.1 and not full funcionally in Lazarus
Expand Down Expand Up @@ -585,7 +592,7 @@ procedure TDzHTMLText.SetText(const Value: String);
FLines.Text := Value;
end;

procedure TDzHTMLText.SetLineVertAlign(const Value: TDHLineVertAlign);
procedure TDzHTMLText.SetLineVertAlign(const Value: TDHVertAlign);
begin
if Value<>FLineVertAlign then
begin
Expand All @@ -595,6 +602,26 @@ procedure TDzHTMLText.SetLineVertAlign(const Value: TDHLineVertAlign);
end;
end;

procedure TDzHTMLText.SetOverallVertAlign(const Value: TDHVertAlign);
begin
if Value<>FOverallVertAlign then
begin
FOverallVertAlign := Value;

BuildAndPaint;
end;
end;

procedure TDzHTMLText.SetOverallHorzAlign(const Value: TDHHorzAlign);
begin
if Value<>FOverallHorzAlign then
begin
FOverallHorzAlign := Value;

BuildAndPaint;
end;
end;

procedure TDzHTMLText.SetLineSpacing(const Value: Integer);
begin
if Value<>FLineSpacing then
Expand Down Expand Up @@ -1331,7 +1358,7 @@ TPreObj_Visual = class(TPreObj)
Group: Integer; //group number
{The group is isolated at each line or tabulation to delimit text horizontal align area}
FixedPos: TFixedPosition;
Align: TAlignment;
Align: TDHHorzAlign;
LineSpace: Integer;
Space: Boolean;
Print: Boolean;
Expand Down Expand Up @@ -1360,7 +1387,7 @@ TTokensProcess = class
Items: TListPreObj;

BackColor: TColor;
Align: TAlignment;
Align: TDHHorzAlign;
LineSpace: Integer;

LBold: TListStack<Boolean>;
Expand All @@ -1371,7 +1398,7 @@ TTokensProcess = class
LFontSize: TListStack<Integer>;
LFontColor: TListStack<TColor>;
LBackColor: TListStack<TColor>;
LAlign: TListStack<TAlignment>;
LAlign: TListStack<TDHHorzAlign>;
LLineSpace: TListStack<Integer>;
LHTMLList: TObjectListStack<THTMLList>;
LSpoilerDet: THTMLSpoilerDetList;
Expand All @@ -1398,8 +1425,10 @@ TTokensProcess = class
procedure DoTab(T: TToken);
procedure DoBreak;

procedure Realign;
procedure DefineVisualRect;
procedure Publish;

procedure CheckAlign(V: TPreObj_Visual);
end;

procedure TBuilder.ProcessTokens;
Expand All @@ -1408,7 +1437,7 @@ procedure TBuilder.ProcessTokens;
P := TTokensProcess.Create(Self);
try
P.Execute;
P.Realign;
P.DefineVisualRect;
P.Publish;
finally
P.Free;
Expand All @@ -1425,7 +1454,7 @@ constructor TTokensProcess.Create(xBuilder: TBuilder);
C.Font.Assign(Lb.Font);

BackColor := clNone;
Align := taLeftJustify;
Align := haLeft;
LineSpace := Lb.FLineSpacing;

Items := TListPreObj.Create;
Expand All @@ -1440,7 +1469,7 @@ constructor TTokensProcess.Create(xBuilder: TBuilder);
LFontSize := TListStack<Integer>.Create;
LFontColor := TListStack<TColor>.Create;
LBackColor := TListStack<TColor>.Create;
LAlign := TListStack<TAlignment>.Create;
LAlign := TListStack<TDHHorzAlign>.Create;
LLineSpace := TListStack<Integer>.Create;

LHTMLList := TObjectListStack<THTMLList>.Create;
Expand Down Expand Up @@ -1562,9 +1591,9 @@ procedure TTokensProcess.DoBackColor(T: TToken);
procedure TTokensProcess.DoAlignment(T: TToken);
begin
case T.Kind of
ttAlignLeft: Align := taLeftJustify;
ttAlignCenter: Align := taCenter;
ttAlignRight: Align := taRightJustify;
ttAlignLeft: Align := haLeft;
ttAlignCenter: Align := haCenter;
ttAlignRight: Align := haRight;
end;
LAlign.AddOrDel(T, Align);
Align := LAlign.Last;
Expand Down Expand Up @@ -1773,7 +1802,7 @@ procedure TTokensProcess.DoBreak;

//

procedure TTokensProcess.Realign;
procedure TTokensProcess.DefineVisualRect;
type TSizes = record
LineHeight, LineSpace, OverallWidth, OverallHeight: Integer;
end;
Expand Down Expand Up @@ -1954,43 +1983,97 @@ type TSizes = record
Lb.FLineCount := LineCount;
end;

procedure TTokensProcess.CheckAlign(V: TPreObj_Visual);
type
TFuncAlignResult = record
Outside, Inside: Integer;
end;

function funcAlignHorz: TFuncAlignResult;
var
B: TGroupBound;
GrpLim: Integer;
begin
B := LGroupBound[V.Group];
if B.Limit = -1 then
begin //group has no limit
if Lb.FAutoWidth or (Lb.FOverallHorzAlign in [haCenter, haRight]) then
GrpLim := Builder.CalcWidth
else
GrpLim := Lb.Width;
end
else GrpLim := B.Limit;

Result.Outside := GrpLim;
Result.Inside := B.Right;
end;

function funcAlignVert: TFuncAlignResult;
begin
Result.Outside := LLineInfo[V.Line].Height;
Result.Inside := V.Visual.Rect.Height;
end;

function funcOverallAlignHorz: TFuncAlignResult;
begin
Result.Outside := Lb.Width;
Result.Inside := Builder.CalcWidth;
end;

function funcOverallAlignVert: TFuncAlignResult;
begin
Result.Outside := Lb.Height;
Result.Inside := Builder.CalcHeight;
end;

procedure Check(fnIndex: Byte; horz: Boolean; prop: Variant);
var
R: TFuncAlignResult;
P: TPoint;
Offset: Integer;
begin
if prop>0 then //center or right
begin
case fnIndex of
0: R := funcAlignHorz;
1: R := funcAlignVert;
2: R := funcOverallAlignHorz;
3: R := funcOverallAlignVert;
end;

Offset := R.Outside - R.Inside;
if prop=1 then Offset := Offset div 2; //center

P := TPoint.Create(0, 0);
if horz then
P.X := Offset
else
P.Y := Offset;

V.Visual.Rect.Offset(P);
end;
end;

begin
Check(0, True, V.Align);
Check(1, False, Lb.FLineVertAlign);

Check(2, True, Lb.FOverallHorzAlign);
Check(3, False, Lb.FOverallVertAlign);
end;

procedure TTokensProcess.Publish;
var
Z: TPreObj;
V: TPreObj_Visual;
B: TGroupBound;
Offset, GrpLim: Integer;
begin
for Z in Items do
begin
if not (Z is TPreObj_Visual) then Continue;
V := TPreObj_Visual(Z);
if not V.Print then Continue;

//horizontal align
if V.Align in [taCenter, taRightJustify] then
begin
B := LGroupBound[V.Group];
if B.Limit = -1 then
begin //group has no limit
if Lb.FAutoWidth then GrpLim := Builder.CalcWidth else GrpLim := Lb.Width;
end
else GrpLim := B.Limit;

Offset := GrpLim - B.Right;
if V.Align=taCenter then Offset := Offset div 2;

V.Visual.Rect.Offset(Offset, 0);
end;

//vertical align
if Lb.FLineVertAlign in [vaCenter, vaBottom] then
begin
Offset := LLineInfo[V.Line].Height - V.Visual.Rect.Height;
if Lb.FLineVertAlign=vaCenter then Offset := Offset div 2;

V.Visual.Rect.Offset(0, Offset);
end;
CheckAlign(V);

Lb.LVisualItem.Add(V.Visual);
V.Visual := nil;
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

## What's New

- 08/30/2020 (Version 2.3)

- Included overall align properties.

- 08/03/2020

- Fixed Lazarus extra line break (issue #23).
Expand Down Expand Up @@ -238,12 +242,16 @@ This property calls ShellExecute method.
`LineSpacing: Integer` = Specify the default line spacing in overall text. You can use `<LS>` tag to determine line spacing at specific lines.
`LineVertAlign: TDHLineVertAlign (vaTop, vaCenter, vaBottom)` = Allows you to specify the vertical alignment of each element in the line. This property only take effects when the elements have different heights. Default is `vaTop`.
`LineVertAlign: TDHVertAlign (vaTop, vaCenter, vaBottom)` = Allows you to specify the vertical alignment of each element in the line. This property only take effects when the elements have different heights at same line. Default is `vaTop`.
`ListLevelPadding: Integer` = Determines the width of each list level in pixels, when using HTML list tags.
`MaxWidth: Integer` = Specify the maximum width of text, when using AutoWidth property.
`OverallHorzAlign: TDHHorzAlign (haLeft, haCenter, haRight)` = Determines overall text horizontal alignment. This property only take effects if `AutoWidth` is false.
`OverallVertAlign: TDHVertAlign (vaTop, vaCenter, vaBottom)` = Determines overall text vertical alignment. This property only take effects if `AutoHeight` is false.
`StyleLinkNormal: TDHStyleLinkProp` = Properties to format a link when is not selected by mouse.
`StyleLinkHover: TDHStyleLinkProp` = Properties to format a link when is selected by mouse.
Expand Down

0 comments on commit e159d3c

Please sign in to comment.