jvcllaz: Fix resolution of html entities in TJvMarkupLabel and TJvMarkupViewer.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8276 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2022-05-07 14:51:41 +00:00
parent 201c83c606
commit e017e82ce5
7 changed files with 50 additions and 14 deletions

View File

@ -24,7 +24,6 @@
</PublishOptions> </PublishOptions>
<RunParams> <RunParams>
<FormatVersion Value="2"/> <FormatVersion Value="2"/>
<Modes Count="0"/>
</RunParams> </RunParams>
<RequiredPackages Count="3"> <RequiredPackages Count="3">
<Item1> <Item1>
@ -62,6 +61,9 @@
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/> <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths> </SearchPaths>
<Linking> <Linking>
<Debugging>
<DebugInfoType Value="dsDwarf2Set"/>
</Debugging>
<Options> <Options>
<Win32> <Win32>
<GraphicApplication Value="True"/> <GraphicApplication Value="True"/>

View File

@ -14,7 +14,7 @@ uses
begin begin
RequireDerivedFormResource := True; RequireDerivedFormResource := True;
Application.Scaled := True; Application.Scaled:=True;
Application.Initialize; Application.Initialize;
Application.CreateForm(TMainForm, MainForm); Application.CreateForm(TMainForm, MainForm);
Application.Run; Application.Run;

View File

@ -7,7 +7,7 @@ object MainForm: TMainForm
ClientHeight = 519 ClientHeight = 519
ClientWidth = 740 ClientWidth = 740
OnCreate = FormCreate OnCreate = FormCreate
LCLVersion = '2.1.0.0' LCLVersion = '2.3.0.0'
inline SynEdit1: TSynEdit inline SynEdit1: TSynEdit
Left = 0 Left = 0
Height = 322 Height = 322
@ -456,6 +456,7 @@ object MainForm: TMainForm
'<font color="blue">blue</font>' '<font color="blue">blue</font>'
'<br><font size="6">small text</font> and <font size="20">large</font> text' '<br><font size="6">small text</font> and <font size="20">large</font> text'
'<br>A different font face: <font face="Times" size="14">Times</font>' '<br>A different font face: <font face="Times" size="14">Times</font>'
'<br>HTML entities: &amp; &trade; &radic;&alpha; &beta; &gamma; &delta; 90&deg;'
'<br>Subscripts and superscripts are not supported:' '<br>Subscripts and superscripts are not supported:'
'10 cm<sup>3</sup> H<sub>2</sub>O ' '10 cm<sup>3</sup> H<sub>2</sub>O '
) )
@ -570,7 +571,7 @@ object MainForm: TMainForm
Left = 4 Left = 4
Height = 19 Height = 19
Top = 0 Top = 0
Width = 99 Width = 97
BorderSpacing.Left = 4 BorderSpacing.Left = 4
BorderSpacing.Bottom = 4 BorderSpacing.Bottom = 4
Caption = 'Markup Viewer' Caption = 'Markup Viewer'
@ -584,10 +585,10 @@ object MainForm: TMainForm
AnchorSideLeft.Side = asrBottom AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = RbMarkupViewer AnchorSideTop.Control = RbMarkupViewer
AnchorSideTop.Side = asrCenter AnchorSideTop.Side = asrCenter
Left = 119 Left = 117
Height = 19 Height = 19
Top = 0 Top = 0
Width = 92 Width = 90
BorderSpacing.Left = 16 BorderSpacing.Left = 16
Caption = 'Markup Label' Caption = 'Markup Label'
OnChange = RbMarkupLabelChange OnChange = RbMarkupLabelChange
@ -598,7 +599,7 @@ object MainForm: TMainForm
object SynHTMLSyn1: TSynHTMLSyn object SynHTMLSyn1: TSynHTMLSyn
DefaultFilter = 'HTML Document (*.htm,*.html)|*.htm;*.html' DefaultFilter = 'HTML Document (*.htm,*.html)|*.htm;*.html'
Enabled = False Enabled = False
left = 456 Left = 456
top = 192 Top = 192
end end
end end

View File

@ -42,7 +42,7 @@ implementation
{ TMainForm } { TMainForm }
procedure TMainForm.FormCreate(Sender: TObject); procedure TMainForm.FormCreate(Sender: TObject);
begin begin
JvMarkupViewer1.Text := SynEdit1.Lines.Text; JvMarkupViewer1.Text := SynEdit1.Lines.Text;
JvMarkupLabel1.Text := SynEdit1.Lines.Text; JvMarkupLabel1.Text := SynEdit1.Lines.Text;
JvMarkupViewer1.Align := alClient; JvMarkupViewer1.Align := alClient;
@ -54,11 +54,13 @@ end;
procedure TMainForm.RbMarkupViewerChange(Sender: TObject); procedure TMainForm.RbMarkupViewerChange(Sender: TObject);
begin begin
JvMarkupViewer1.Visible := RbMarkupViewer.Checked; JvMarkupViewer1.Visible := RbMarkupViewer.Checked;
JvMarkupLabel1.Visible := not RbMarkupViewer.Checked;
end; end;
procedure TMainForm.RbMarkupLabelChange(Sender: TObject); procedure TMainForm.RbMarkupLabelChange(Sender: TObject);
begin begin
JvMarkupLabel1.Visible := RbMarkupLabel.Checked; JvMarkupLabel1.Visible := RbMarkupLabel.Checked;
JvMarkupViewer1.Visible := not RbMarkupLabel.Checked;
end; end;
procedure TMainForm.SynEdit1Change(Sender: TObject); procedure TMainForm.SynEdit1Change(Sender: TObject);

View File

@ -38,6 +38,7 @@ type
TJvHTMLElement = class(TObject) TJvHTMLElement = class(TObject)
private private
FFontSize: Integer; FFontSize: Integer;
FNoEntText: string;
FText: string; FText: string;
FFontName: string; FFontName: string;
FFontStyle: TFontStyles; FFontStyle: TFontStyles;
@ -61,7 +62,9 @@ type
procedure SetBreakLine(const Value: Boolean); procedure SetBreakLine(const Value: Boolean);
public public
procedure Breakup(ACanvas: TCanvas; Available: Integer); procedure Breakup(ACanvas: TCanvas; Available: Integer);
class function FixHTMLEntities(const S: String): String;
property Text: string read FText write SetText; property Text: string read FText write SetText;
property NoEntText: String read FNoEntText;
property SolText: string read FSolText write SetSolText; property SolText: string read FSolText write SetSolText;
property EolText: string read FEolText write SetEolText; property EolText: string read FEolText write SetEolText;
property FontName: string read FFontName write SetFontName; property FontName: string read FFontName write SetFontName;
@ -89,6 +92,9 @@ type
implementation implementation
uses
StrUtils, htmldefs;
//=== { TJvHTMLElement } ===================================================== //=== { TJvHTMLElement } =====================================================
procedure TJvHTMLElement.Breakup(ACanvas: TCanvas; Available: Integer); procedure TJvHTMLElement.Breakup(ACanvas: TCanvas; Available: Integer);
@ -101,7 +107,7 @@ begin
ACanvas.Font.Style := FontStyle; ACanvas.Font.Style := FontStyle;
ACanvas.Font.Color := FontColor; ACanvas.Font.Color := FontColor;
if SolText = '' then if SolText = '' then
S := Text S := FNoEntText
else else
S := EolText; S := EolText;
if ACanvas.TextWidth(S) <= Available then if ACanvas.TextWidth(S) <= Available then
@ -125,6 +131,29 @@ begin
end; end;
end; end;
class function TJvHTMLElement.FixHTMLEntities(const S: String): String;
var
ent: string;
wch: widechar = #0;
p1, p2: Integer;
begin
Result := S;
p1 := pos('&', Result);
while p1 > 0 do begin
p2 := posex(';', Result, p1);
if p2 > 0 then
begin
ent := Copy(Result, p1+1, p2-p1-1);
if ResolveHTMLEntityReference(widestring(ent), wch) then
begin
ent := Copy(Result, p1, p2-p1+1);
Result := StringReplace(Result, ent, UTF8Encode(wch), [rfReplaceAll])
end;
end;
p1 := posex('&', Result, p1+1);
end;
end;
procedure TJvHTMLElement.SetAscent(const Value: Integer); procedure TJvHTMLElement.SetAscent(const Value: Integer);
begin begin
FAscent := Value; FAscent := Value;
@ -173,6 +202,7 @@ end;
procedure TJvHTMLElement.SetText(const Value: string); procedure TJvHTMLElement.SetText(const Value: string);
begin begin
FText := Value; FText := Value;
FNoEntText := FixHTMLEntities(FText);
end; end;
procedure TJvHTMLElement.SetWidth(const Value: Integer); procedure TJvHTMLElement.SetWidth(const Value: Integer);

View File

@ -423,7 +423,7 @@ end;
procedure TJvMarkupLabel.RenderHTML; procedure TJvMarkupLabel.RenderHTML;
var var
R: TRect; R: TRect;
I, C, X, Y: Integer; I, C, X, Y, W: Integer;
ATotalWidth, AClientWidth, ATextWidth, BaseLine: Integer; ATotalWidth, AClientWidth, ATextWidth, BaseLine: Integer;
iSol, iEol, PendingCount, MaxHeight, MaxAscent: Integer; iSol, iEol, PendingCount, MaxHeight, MaxAscent: Integer;
El: TJvHTMLElement; El: TJvHTMLElement;
@ -517,8 +517,9 @@ begin
end; end;
if El.SolText <> '' then if El.SolText <> '' then
begin begin
ATotalWidth := ATotalWidth - Canvas.TextWidth(El.SolText) - 5; W := Canvas.TextWidth(El.SolText);
ATextWidth := ATextWidth + Canvas.TextWidth(El.SolText); ATotalWidth := ATotalWidth - W - 5;
ATextWidth := ATextWidth + W;
if El.EolText = '' then if El.EolText = '' then
begin begin
if I >= C - 1 then if I >= C - 1 then

View File

@ -159,7 +159,7 @@ begin
for I := 0 to C - 1 do for I := 0 to C - 1 do
begin begin
Element := TJvHTMLElement(FElementStack.Items[I]); Element := TJvHTMLElement(FElementStack.Items[I]);
s := Element.Text; s := Element.NoEntText;
Canvas.Font.Name := Element.FontName; Canvas.Font.Name := Element.FontName;
Canvas.Font.Size := Element.FontSize; Canvas.Font.Size := Element.FontSize;
Canvas.Font.Style := Element.FontStyle; Canvas.Font.Style := Element.FontStyle;