Recent

Author Topic: TRect and TextRect Question  (Read 321 times)

Dan3468298

  • Full Member
  • ***
  • Posts: 124
TRect and TextRect Question
« on: July 28, 2020, 02:13:04 am »
When defining a TRect you use cartesian plane coordinates.  But when using TextRect, it ignores TRect Top and left and uses 0 value.  My guess is this is so for when TRect is used on cartesian plane with minus values for graphing.   I am trying to understand the motivations.  Thanks for any input. 
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

wp

  • Hero Member
  • *****
  • Posts: 7392
Re: TRect and TextRect Question
« Reply #1 on: August 01, 2020, 05:58:03 pm »
I don't know what you are talking about. The TextRect method of the canvas?

In this case, the usage of the coordinates depends on the settings for Layout and Alignment in the canvas' TextStyle. Please play with the attached code - I wrote it on Windows but it should work also on Mac.

The rectangle Rect passed to Canvas.TextRect(Rect, x, y, Text, TextStyle) defines the box within which the text is written.
x is used only when TextStyle.Alignment is taLeftJustify, and y is used only when TextStyle.Layout is tlTop. Their origin is the origin of the canvas on which painting occurs. This means that when you want to paint in the top/left corner of the specified Rect you must set x = Rect.Left and y = Rect.Top. So, if you set x=0 and y=0 and have Rect.Left > 0 and Rect.Top > 0 there is probably no output because of clipping (which can be turned off by setting TextRect.Clipping := false).

If you want to paint the text centered at the top of the Rect use TextStyle.Alignment := taCenter, TextStyle.Layout := tlTop, x will be ignored.

If you want to right-align the text within the Rect use TextStyle.Alignment := taRightJustify, TextStyle.Layout := tlTop. x will be ignored.

If you want to right-align the text within the Rect but move it 20 pixels below the top edge use the same TextStyle settings but set y := Rect.Top + 20;

If you want to paint the text centered both vertically and horizontally within Rect use TextStyle.Alignment := taCenter and TextStyle.Layout.tlCenter, both x and y will be ignored. Etc...

Note that TTextStyle is a record which does not have setter procedures; therefore you can assign only a complete record to Canvas.TextStyle. You must use an intermediate variable to change record elements. Do not forget to initialize this record with the current TextStyle settings:
Code: Pascal  [Select][+][-]
  1. var
  2.   ts: TTextStyle;
  3. begin
  4.   ts := Canvas.TextStyle;
  5.   ts.Alignment := taCenter;
  6.   ts.Layout := tlCenter;
  7.   Canvas.TextRect(Rect(10, 10, 490, 390), 0, 0, 'My text', ts);
The temporary TextStyle variable can be omitted in the latter call, but then the temp variable must be re-assigned to canvas.TextStyle:
Code: Pascal  [Select][+][-]
  1. var
  2.   ts: TTextStyle;
  3. begin
  4.   ts := Canvas.TextStyle;
  5.   ts.Alignment := taCenter;
  6.   ts.Layout := tlCenter;
  7.   Canvas.TextStyle := ts;
  8.   Canvas.TextRect(Rect(10, 10, 490, 390), 0, 0, 'My text');
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

circular

  • Hero Member
  • *****
  • Posts: 3405
    • Personal webpage
Re: TRect and TextRect Question
« Reply #2 on: August 02, 2020, 10:26:50 am »
The (x,y) coordinates are redundant in most cases. TextRect could simply use (Left,Top) of rectangle.

Though I can see a case where it makes sense. If you enable clipping and disable word wrap, you can draw some text at location (x,y) and clip it according to any rectangle.
Conscience is the debugger of the mind

Dan3468298

  • Full Member
  • ***
  • Posts: 124
Re: TRect and TextRect Question
« Reply #3 on: August 02, 2020, 07:53:55 pm »
Yes, using TextOut on a Canvas.  Sorry for forgetting those details.   
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

 

TinyPortal © 2005-2018