Forum > LCL

lcl/include/ function TCanvas.TextExtent ARect variable


Apparently, ARect declaration can be removed, together with "ARect := Rect(0, 0, 0, 0);"

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TCanvas.TextExtent(const Text: string): TSize;var  DCIndex: Integer;  ARect: TRect;   procedure SaveState;  begin    if DCIndex <> 0 then exit;    DCIndex := SaveDC(FHandle);  end;   procedure RestoreState;  begin    if DCIndex = 0 then exit;    RestoreDC(FHandle, DCIndex);  end; begin  Result.cX := 0;  Result.cY := 0;  if Text='' then exit;  RequiredState([csHandleValid, csFontValid]);   DCIndex := 0;  if Font.IsDefault then  begin    SaveState;    SelectObject(FHandle, OnGetSystemFont());  end;   ARect := Rect(0, 0, 0, 0);  GetTextExtentPoint(FHandle, PChar(Text), Length(Text), Result);   RestoreState;end; 

Please file a bugreport (I assume this is Lazarus trunk?).


I removed it in 79fadc1fa5. Thanks for the hint.


--- Quote from: JuhaManninen on March 11, 2023, 04:12:08 pm ---I removed it in 79fadc1fa5. Thanks for the hint.

--- End quote ---

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 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.


[0] Message Index

Go to full version