8000 An `<img:index>` tag without a valid image list entry can corrupt the VCL layout · Issue #109 · digao-dalpiaz/DzHTMLText · GitHub
[go: up one dir, main page]

Skip to content

An <img:index> tag without a valid image list entry can corrupt the VCL layout #109

Backlog

Description

@FranmoFR

Environment

DzHTMLText 6.13, Delphi 12 Athens, Windows. VCL runtime regression tests passed on Win32 and Win64. The FMX Win32 package compiled.

Reproduction

Create a TDzHTMLText on a VCL form and leave Images unassigned. Set Text to AB, record TextWidth, then set Text to A<img:0>B and measure again.

Observed and expected behavior

In Delphi 12 Win64, AB measured 15 pixels. Two runs with the image tag measured more than 3 million and 15 million pixels. With AutoWidth enabled, the invalid size can affect the control bounds. An unavailable image should occupy zero space.

Cause and fix

TDHToken_Image.Process fills the local Size only when an image list exists, then scales and queues it unconditionally. The patch skips absent lists, out-of-range indices and empty FMX layer lists. It also invalidates the layout after the assigned list is destroyed. Valid images retain their existing size and DPI calculation.

Verification

The regression covers missing, empty, assigned and destroyed image lists, a valid index and an out-of-range index.

Base revision bfdda0f. The implementation uses Delphi 12 inline variables. Older Delphi versions and Lazarus require compatibility adjustments and were outside this verification.

Patch

diff --git a/Source/Vcl.DHTokenEngine.pas b/Source/Vcl.DHTokenEngine.pas
index 33a091e..f39e621 100644
--- a/Source/Vcl.DHTokenEngine.pas
+++ b/Source/Vcl.DHTokenEngine.pas
@@ -899,38 +899,33 @@ begin
 end;
 
 procedure TDHToken_Image.Process;
-var
-  V: TDHVisualItem_Image;
-  Size: TAnySize;
-  IgnoreScale: Boolean;
 begin
-  IgnoreScale := False;
-
-  {$IFDEF USE_IMGLST}
-  if Assigned(Lb.Images) then
-  begin
-    {$IFDEF FMX}
-    with Lb.Images.Destination[ImageIndex].Layers[0].SourceRect do
-      Size := TAnySize.Create(Width, Height);
-    {$ELSE}
-    Size := TAnySize.Create(Lb.Images.Width, Lb.Images.Height);
-      {$IF Defined(DCC) and (CompilerVersion >= 34)} //Delphi 10.4 Sydney
-      if Lb.Images.IsScaled then IgnoreScale := True; //imagelist already scaled
-      {$ENDIF}
-    {$ENDIF}
-  end;
+ {$IFDEF USE_IMGLST}
+
59B9
 // Pomija obraz bez listy lub bez pasującego wpisu, bez rezerwowania miejsca.
+ if not Assigned(Lb.Images) then Exit;
+ {$IFDEF FMX}
+ if ImageIndex >= Lb.Images.Destination.Count then Exit;
+ var Layers := Lb.Images.Destination[ImageIndex].Layers;
+ if Layers.Count = 0 then Exit;
+ var SourceRect := Layers[0].SourceRect;
+ var Size := TAnySize.Create(SourceRect.Width, SourceRect.Height);
+ {$ELSE}
+ if ImageIndex >= Lb.Images.Count then Exit;
+ var Size := TAnySize.Create(Lb.Images.Width, Lb.Images.Height);
+  {$IF Defined(DCC) and (CompilerVersion >= 34)}
+ if not Lb.Images.IsScaled then
   {$ENDIF}
-
-  if not IgnoreScale then
-  begin
-    Size.Width := Lb.CalcScale(Size.Width);
-    Size.Height := Lb.CalcScale(Size.Height);
-  end;
-
-  V := TDHVisualItem_Image.Create;
-  V.ImageIndex := ImageIndex;
-
-  Builder.AddVisualItemToQueue(V, Size);
+ {$ENDIF}
+ begin
+  Size.Width := Lb.CalcScale(Size.Width);
+  Size.Height := Lb.CalcScale(Size.Height);
+ end;
+
+ // Dodaje tylko obrazy o ustalonym rozmiarze.
+ var VisualItem := TDHVisualItem_Image.Create;
+ VisualItem.ImageIndex := ImageIndex;
+ Builder.AddVisualItemToQueue(VisualItem, Size);
+ {$ENDIF}
 end;
 
 { TDHToken_ImageResource }
diff --git a/Source/Vcl.DzHTMLText.pas b/Source/Vcl.DzHTMLText.pas
index 48fed9d..7ffdd8e 100644
--- a/Source/Vcl.DzHTMLText.pas
+++ b/Source/Vcl.DzHTMLText.pas
@@ -918,7 +918,13 @@ begin
   if Operation = opRemove then
   begin
     {$IFDEF USE_IMGLST}
-    if AComponent = FImages then FImages := nil;
+    // Po usunięciu listy następne odrysowanie ponownie oblicza układ.
+    if AComponent = FImages then
+    begin
+     FImages := nil;
+     FirstRebuild := False;
+     if not (csDestroying in ComponentState) then Modified([mfPaint]);
+    end;
     {$ENDIF}
   end;
 end;
@@ -936,6 +942,8 @@ procedure TDzHTMLText.SetImages(const Value: TCustomImageList);
 begin
   if Value <> FImages then
   begin
+    // Zwalnia powiadomienie po odłączeniu poprzedniej listy.
+    if Assigned(FImages) then FImages.RemoveFreeNotification(Self);
     FImages := Value;
     if FImages <> nil then
       FImages.FreeNotification(Self);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0