Recent

Author Topic: Advance text rendering on canvas  (Read 2065 times)

stoffman

  • Jr. Member
  • **
  • Posts: 67
Advance text rendering on canvas
« on: July 11, 2021, 11:24:59 am »
Hi,

I'm familiar with TextOut. But I'm looking for something along the lines of Win32 DrawTextEx (That provides alignment, word wrapping and some other niceties) that is also cross platform. It would be great if it also supports different colors (TextOut/DrawTextEx uses the current pen color)   

Any suggestions ?

https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-drawtextexa

Thanks,

wp

  • Hero Member
  • *****
  • Posts: 11922
Re: Advance text rendering on canvas
« Reply #1 on: July 11, 2021, 11:31:21 am »
AFAIK, DrawTextEx is not available in LCLIntf, in contrast to DrawText. The basic DrawText functionality is also available in the TCanvas.TextRect method which respects the TextStyle record of TCanvas.
Code: Pascal  [Select][+][-]
  1. type
  2.   TTextStyle = packed record
  3.     Alignment : TAlignment;  // TextRect Only: horizontal alignment
  4.  
  5.     Layout    : TTextLayout; // TextRect Only: vertical alignment
  6.  
  7.     SingleLine: boolean;     // If WordBreak is false then process #13, #10 as
  8.                              // standard chars and perform no Line breaking.
  9.  
  10.     Clipping  : boolean;     // TextRect Only: Clip Text to passed Rectangle
  11.  
  12.     ExpandTabs: boolean;     // Replace #9 by apropriate amount of spaces (default is usually 8)
  13.  
  14.     ShowPrefix: boolean;     // TextRect Only: Process first single '&' per
  15.                              //    line as an underscore and draw '&&' as '&'
  16.  
  17.     Wordbreak : boolean;     // TextRect Only: If line of text is too long
  18.                              //    too fit between left and right boundaries
  19.                              //    try to break into multiple lines between
  20.                              //    words
  21.                              //    See also EndEllipsis.
  22.  
  23.     Opaque    : boolean;     // TextRect: Fills background with current Brush
  24.                              // TextOut : Fills background with current
  25.                              //            foreground color
  26.  
  27.     SystemFont: Boolean;     // Use the system font instead of Canvas Font
  28.    
  29.     RightToLeft: Boolean;    //For RightToLeft text reading (Text Direction)
  30.  
  31.     EndEllipsis: Boolean;    // TextRect Only: If line of text is too long
  32.                              //    to fit between left and right boundaries
  33.                              //    truncates the text and adds "..."
  34.                              //    If Wordbreak is set as well, Workbreak will
  35.                              //    dominate.
  36.   end;
  37.  
  38.   TCanvas = class(TFPCustomCanvas)
  39.   ...
  40.     procedure TextRect(const ARect: TRect; X, Y: integer; const Text: string);
  41.     procedure TextRect(ARect: TRect; X, Y: integer; const Text: string;
  42.                        const Style: TTextStyle); virtual;
  43.   end;
« Last Edit: July 11, 2021, 11:55:03 am by wp »

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Advance text rendering on canvas
« Reply #2 on: July 11, 2021, 02:07:18 pm »
it's unfortunate the canvas.TextRect(… did not define the TRECT as a VAR because you can not use that function if all you want to do is simply calculate the rectangle without painting.


However, all is not lost..

 the LCLINTF unit which makes cross platform calls does have the DrawText which accepts the same parameters as the windows counter part..

 So all one needs to do is just include the unit.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018