Recent

Author Topic: Bug with drawing to canvas with TextRect  (Read 654 times)

Jonny

  • Jr. Member
  • **
  • Posts: 55
Bug with drawing to canvas with TextRect
« on: January 10, 2025, 03:17:08 pm »
I am having an issue with the TextRect function. Not sure if it is me or a bug in FPC.

Lazarus 4.99 (rev main_4_99-813-g9eb9286e45) FPC 3.3.1 x86_64-linux-qt5
Free Pascal Compiler version 3.3.1-17259-g904c25745c-dirty [2025/01/09] for x86_64

I am drawing rectangles then text to a TBitmap - here is a minimal example:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormShow(Sender: TObject);
  2. var
  3.   bmpGrid: TBitmap;
  4.   cols, rows, boxw, boxh: Integer;
  5.   canvasTextStyle: TTextStyle;
  6.   boxRect: TRect;
  7. begin
  8.   bmpGrid := TBitmap.Create;
  9.   bmpGrid.Width := 1200;
  10.   bmpGrid.Height := 1200;
  11.   with bmpGrid.Canvas do
  12.   begin
  13.     Clear;
  14.     Pen.Width := 1;
  15.     Pen.Style := psInsideframe;
  16.     Font.Color := clWhite;
  17.     canvasTextStyle := TextStyle;
  18.     canvasTextStyle.Opaque := False;
  19.     boxw := bmpGrid.Width div 6;
  20.     boxh := bmpGrid.Height div 6;
  21.     for rows := 0 to 5 do
  22.       for cols := 0 to 5 do
  23.         begin
  24.           Brush.Color := RGBToColor(Random(127),Random(127),Random(127));
  25.           Pen.Color := RGBToColor(Random(127),Random(127),Random(127));
  26.           boxRect := Rect(cols*boxw, rows*boxh, cols*boxw+boxw, rows*boxh+boxh);
  27.           Rectangle(boxRect);
  28.           Inc(boxRect.Left,8);
  29.           Dec(boxRect.Right,8);
  30.           Inc(boxRect.Top,8);
  31.           Dec(boxRect.Bottom,8);
  32.           TextRect(boxRect,0,0,'hello',canvasTextStyle);
  33.         end;
  34.   end;
  35.   bmpGrid.SaveToFile('/tmp/canvas.bmp');
  36. end;
  37.  

The attachments show when compiling to linux gtk2 and qt5 with compiler optimization level set to -O1 and -O2 as per the filename.

You can see varied results. One out if the four seems correct but if I add more draw commands then it fails too.

Is this something that I need to report as a bug (if so then please provide details on the procedure) or am I doing things wrong?
« Last Edit: January 10, 2025, 09:31:00 pm by Jonny »

wp

  • Hero Member
  • *****
  • Posts: 12525
Re: Bug with drawing to canvas with TextRect
« Reply #1 on: January 10, 2025, 05:18:51 pm »
I think, there are two bugs in your code (BTW, it would have been helpful if you had posted a complete compilable project; just recreating it from a code-snippet is always a bit ambiguous):
  • You do not initialize the variable canvasTextStyle. It is a record, and although you assign a value to the Opaque field, all the other fields will contain any random values. So, please always initialize it to the TextStyle of the Canvas before you change something:
Code: Pascal  [Select][+][-]
  1. canvasTextStyle := bmpGrid.Canvas.TextStyle;
  • The x and y coordinates of the TextRect call are relative to the canvas. So, when you set them to zero, the text will be painted in the upper left corner of the canvas. It is probably due to the uninitialized TextStyle that there are cases when your code was working. Correctly do this:
Code: Pascal  [Select][+][-]
  1. bmpGrid.Canvas.TextRect(boxRect, boxRect.Left, boxRect.Top, 'hello', canvasTextStyle)
But note that depending on the values of TextStyle.Alignment and TextStyle.Layout the x and y parameters of some combinations may be ignored (For example when TextStyle.Alignment is taCenter the text is always centered within boxRect no matter what x is).

Jonny

  • Jr. Member
  • **
  • Posts: 55
Re: Bug with drawing to canvas with TextRect
« Reply #2 on: January 10, 2025, 09:41:34 pm »
Quote from: wp
BTW, it would have been helpful if you had posted a complete compilable project; just recreating it from a code-snippet is always a bit ambiguous

My apologies, I did not want to overburden by posting too much code, so I tried to cut it right down to keep it simple. I guess that I trimmed too much. Have just modified it to make it a fully self-contained procedure.

Quote from: wp
You do not initialize the variable canvasTextStyle

Linked to above, I had it in my code but removed it when posting here because it seemed to make no difference (have added it back).

Quote from: wp
The x and y coordinates of the TextRect call are relative to the canvas.

Aha, that was it! I thought that it was relative to the TRect. Thank you for your comprehensive reply, explanation and fix, it is very much appreciated.

 

TinyPortal © 2005-2018