Recent

Author Topic: DrawText clips bottom of text  (Read 1971 times)

jipété

  • Full Member
  • ***
  • Posts: 172
DrawText clips bottom of text
« on: September 09, 2023, 11:29:40 am »
Hi,

found a strange behaviour of DrawText, which clips the last line, see attached image.
img_FormPaint2.png

Problem better displayed if r2 is removed.
img_without-r2.png

Code is coming from https://itecnote.com/tecnote/delphi-calculating-size-of-text-before-drawing-to-a-canvas/, where we can see a nice window
img_SourceDrawText.png
, and adapted for Lazarus with
Code: Pascal  [Select][+][-]
  1. {.$R *.dfm}
  2. {$R *.lfm}
and adding
  LCLIntf, LCLType,
in the uses section (by the way, what to do to have a new project skeleton which always include these two units in the uses declaration ? Thx).

17 in the title bar is coming from :
Code: Pascal  [Select][+][-]
  1.     h := DrawText(Canvas.Handle,
  2.       PChar(S[i]),
  3.       Length(S[i]),
  4.       r,
  5.       Aligns[Odd(i)] or DT_WORDBREAK or DT_CALCRECT);
  6.     Caption := IntToStr(h);
and I don't know what's happens, but the line "Glad to hear that!" is 15 px height...
So the bottom of the "y" in the green top-right text is also broken.

If I enlarge Form1, the second line of the first blue text is concerned, but less than third line when present.

If I reduce Form1.Width, it's a disaster.
img_four-lines.png

(end of text in following post, due to number of images)...
« Last Edit: September 09, 2023, 11:51:50 am by jipété »

jipété

  • Full Member
  • ***
  • Posts: 172
Re: DrawText clips bottom of text
« Reply #1 on: September 09, 2023, 11:32:24 am »
...(end of text)


If I reduce more and more, I lost a line in the big blue text :
img_8lines-only7visible.png

If I remove "or DT_WORDWRAP", al the lines are bottom-clipped,
img_no-wordwrap.png

Even the so simple first code of the page displays a broken text (last word is missing) :
img_FormPaint1.png

Thanks for the help.
Debian Bullseye 11.7 64bits Gtk2 and FPC 3.2.2 Laz 2.2.6

Same problem with Laz 3.0RC1

+++
Codes, in case of http://404
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormPaint(Sender: TObject);
  2. const
  3.   S = 'This is a sample text, I think, is it not?';
  4. var
  5.   r: TRect;
  6. begin
  7.   r := Rect(10, 10, 60, 60);
  8.   DrawText(Canvas.Handle,
  9.     PChar(S),
  10.     Length(S),
  11.     r,
  12.     DT_LEFT or DT_WORDBREAK or DT_CALCRECT);
  13.  
  14.   DrawText(Canvas.Handle,
  15.     PChar(S),
  16.     Length(S),
  17.     r,
  18.     DT_LEFT or DT_WORDBREAK);
  19. end;
  20.  
or
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormPaint(Sender: TObject);
  2. const
  3.   S: array[0..3] of string = ('Hi! How are you?',
  4.     'I am fine, thanks. How are you? How are your kids?',
  5.     'Fine!',
  6.     'Glad to hear that!');
  7.   Colors: array[boolean] of TColor = (clMoneyGreen, clSkyBlue);
  8.   Aligns: array[boolean] of integer = (DT_RIGHT, DT_LEFT);
  9. var
  10.   i, y, MaxWidth, RectWidth: integer;
  11.   r, r2: TRect;
  12. begin
  13.  
  14.   y := 10;
  15.   MaxWidth := ClientWidth div 2;
  16.  
  17.   for i := low(S) to high(S) do
  18.   begin
  19.  
  20.     Canvas.Brush.Color := Colors[Odd(i)];
  21.  
  22.     r := Rect(10, y, MaxWidth, 16);
  23.     DrawText(Canvas.Handle,
  24.       PChar(S[i]),
  25.       Length(S[i]),
  26.       r,
  27.       Aligns[Odd(i)] or DT_WORDBREAK or DT_CALCRECT);
  28.  
  29.     if not Odd(i) then
  30.     begin
  31.       RectWidth := r.Right - r.Left;
  32.       r.Right := ClientWidth - 10;
  33.       r.Left := r.Right - RectWidth;
  34.     end;
  35.  
  36.     r2 := Rect(r.Left - 4, r.Top - 4, r.Right + 4, r.Bottom + 4);
  37.     Canvas.RoundRect(r2, 5, 5);
  38.  
  39.     DrawText(Canvas.Handle,
  40.       PChar(S[i]),
  41.       Length(S[i]),
  42.       r,
  43.       Aligns[Odd(i)] or DT_WORDBREAK);
  44.  
  45.     y := r.Bottom + 10;
  46.  
  47.   end;
  48.  
  49. end;
  50.  
  51. procedure TForm1.FormResize(Sender: TObject);
  52. begin
  53.   Invalidate;
  54. end;
  55.  
« Last Edit: September 09, 2023, 12:18:02 pm by jipété »

paweld

  • Hero Member
  • *****
  • Posts: 1090
Re: DrawText clips bottom of text
« Reply #2 on: September 09, 2023, 11:18:35 pm »
On lazarus trunk and fpc 3.2-fixes it's OK - tested on Windows 10 and Debian 12
Best regards / Pozdrawiam
paweld

jipété

  • Full Member
  • ***
  • Posts: 172
Re: DrawText clips bottom of text
« Reply #3 on: September 10, 2023, 10:02:07 am »
Hello,

Thank you for this nice answer.

For my part, I will therefore quietly wait for the official release of Laz 3.0.
Any idea when this date will arrive?

Have a cool sunday,
Best regards

zeljko

  • Hero Member
  • *****
  • Posts: 1624
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: DrawText clips bottom of text
« Reply #4 on: September 10, 2023, 01:55:48 pm »
This code works fine with gtk2 and gtk3 with trunk lazarus.

jipété

  • Full Member
  • ***
  • Posts: 172
Re: DrawText clips bottom of text
« Reply #5 on: September 11, 2023, 12:38:08 pm »
This code works fine with gtk2 and gtk3 with trunk lazarus.

For my part, I will therefore quietly wait for the official release of Laz 3.0.

jipété

  • Full Member
  • ***
  • Posts: 172
Re: DrawText clips bottom of text
« Reply #6 on: June 20, 2024, 06:26:00 pm »
Hi,
On lazarus trunk and fpc 3.2-fixes it's OK - tested on Windows 10 and Debian 12
This code works fine with gtk2 and gtk3 with trunk lazarus.

Sorry, boys, but still broken with Laz 3.4...
Look at the last line in the big blue rectangle :
« Last Edit: June 20, 2024, 06:27:47 pm by jipété »

 

TinyPortal © 2005-2018