Menu

#20 Text overflows bounding rect

2.0
closed
None
2022-03-18
2022-01-05
Pierre Y.
No

Hi Angus,

Image32 is just awesome. I'm close to finish that clone of TSpeedButton, but I found a case when the text overflows its bounding rect :

program TestDrawText;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  System.Types,
  Img32,
  Img32.Vector,
  Img32.Draw,
  Img32.Text,
  Img32.Fmt.PNG;

var
  FontReader: TFontReader;
  FontCache: TFontCache;
  I: TImage32;
  TextRect: TRect;

begin
  try
    FontReader := TFontReader.Create('Segoe UI');
    FontCache := TFontCache.Create(FontReader, 18);

    I := TImage32.Create(200, 200);
    I.Clear(clWhite32);

    TextRect := Rect(0, 0, 150, 200);
    DrawText(I, TextRect, 'This is a test, my friend', taCenter, tvaMiddle, FontCache, clBlack32);

    DrawDashedLine(I, Rectangle(TextRect), [20,10,15,10], nil, 1, clRed32, esPolygon);

    I.SaveToFile('Text.png');

    I.Free;
    FontCache.Free;
    FontReader.Free;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

You will get the image attached. Could it be related to the space character between "my" and "friend", that gets added to the first line instead of being ignored ?

Thank you,

1 Attachments

Discussion

  • Angus Johnson

    Angus Johnson - 2022-01-05

    Hi Pierre.

    First, I'm delighted you're finding Image32 so useful.

    I can see you're using the latest code in the repository and you have indeed found a bug there.
    To fix it ... in the method TFontCache.GetTextOutline in Img32.Text about line 2380 you'll find the following block of code ...

      for i := startLine to endLine do
      begin
        a := tpm.wordListOffsets[i];
        b := tpm.wordListOffsets[i+1] -1;
        //replace the following line
        //spcDx := tpm.justifyDeltas[i];
        //with this ...
        if textAlign = taJustify then
          spcDx := tpm.justifyDeltas[i] else
          spcDx := 0;
        lineWidth := tpm.lineWidths[i];
    

    I'll update the repository shortly (within the next couple of days when I'm back from holiday).

    ps: Would you be happy to write a very bried review - see here? 🤞

     
  • Pierre Y.

    Pierre Y. - 2022-01-05

    Can't believe it was so simple :) Thank you very much Angus.

     
  • Angus Johnson

    Angus Johnson - 2022-03-18
    • status: open --> closed