Recent

Author Topic: [SOLVED] lcl/include/canvas.inc function TCanvas.TextExtent ARect variable  (Read 2093 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 407
Apparently, ARect declaration can be removed, together with "ARect := Rect(0, 0, 0, 0);"

Code: Pascal  [Select][+][-]
  1. function TCanvas.TextExtent(const Text: string): TSize;
  2. var
  3.   DCIndex: Integer;
  4.   ARect: TRect;
  5.  
  6.   procedure SaveState;
  7.   begin
  8.     if DCIndex <> 0 then exit;
  9.     DCIndex := SaveDC(FHandle);
  10.   end;
  11.  
  12.   procedure RestoreState;
  13.   begin
  14.     if DCIndex = 0 then exit;
  15.     RestoreDC(FHandle, DCIndex);
  16.   end;
  17.  
  18. begin
  19.   Result.cX := 0;
  20.   Result.cY := 0;
  21.   if Text='' then exit;
  22.   RequiredState([csHandleValid, csFontValid]);
  23.  
  24.   DCIndex := 0;
  25.   if Font.IsDefault then
  26.   begin
  27.     SaveState;
  28.     SelectObject(FHandle, OnGetSystemFont());
  29.   end;
  30.  
  31.   ARect := Rect(0, 0, 0, 0);
  32.   GetTextExtentPoint(FHandle, PChar(Text), Length(Text), Result);
  33.  
  34.   RestoreState;
  35. end;
  36.  
« Last Edit: December 15, 2023, 01:29:55 pm by lagprogramming »

Bart

  • Hero Member
  • *****
  • Posts: 5465
    • Bart en Mariska's Webstek
Re: lcl/include/canvas.inc function TCanvas.TextExtent ARect variable
« Reply #1 on: March 10, 2023, 04:18:53 pm »
Please file a bugreport (I assume this is Lazarus trunk?).

Bart

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4541
  • I like bugs.
Re: lcl/include/canvas.inc function TCanvas.TextExtent ARect variable
« Reply #2 on: March 11, 2023, 04:12:08 pm »
I removed it in 79fadc1fa5. Thanks for the hint.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

lagprogramming

  • Sr. Member
  • ****
  • Posts: 407
Re: lcl/include/canvas.inc function TCanvas.TextExtent ARect variable
« Reply #3 on: March 13, 2023, 01:06:57 pm »
I removed it in 79fadc1fa5. Thanks for the hint.

Now the code is better, but looking at it again I've noticed something.
When Font.IsDefault is true, DCIndex will always be zero. This makes the line "if DCIndex <> 0 then exit;" useless at the nested SaveState.
A similar situation can be found at TCanvas.TextRect and TWin32ThemeServices.DrawText. Like function TCanvas.TextExtent, those two routines have nested SaveState and RestoreState procedures with the identical content. I think that after removing "if DCIndex <> 0 then exit;" from these three SaveState nested procedures, lcl developers should verify again to see if the code does what they expect it to do.

As a comparison, I've noticed the usage of TLazCanvas.SaveState in customdrawnproc.pas(RenderWinControl and DrawFormBackground) and also in cusomdrawnwinapi.inc(TCDWidgetSet.DrawFocusRect). No matter what, TLazCanvas.SaveState is always followed by a TLazCanvas.RestoreState. In the three routines that use the nested SaveState and RestoreState procedures, "RestoreDC(DC,DCIndex);" is called only if DCIndex<>0. This means that if the list of saved states was empty at the time of calling SaveDC in SaveState, InxedDC will be zero and the state won't be restored in RestoreState. LCL developers might want to check if that is what they really wanted.

 

TinyPortal © 2005-2018