ImageEn, unit iegdiplus

TIECanvas.MeasureText

TIECanvas.MeasureText

Declaration

function MeasureText(Text: WideString; BoundingRect: TRect; Angle: Integer = 0): TSize;
function MeasureText(Text: WideString; Angle: Integer = 0): TSize;

Description

Returns the height and width of text using the current font. If a rect is specified then the width of the text is bounded, which may cause it to wrap.

Note:
MeasureText is called by TextExtent, TextWidth and TextHeight
MeasureText is not accurate when borders are applied (using TextStyling)

GDI+ Method: GdipMeasureString

Example

// Method to draw text within an area of a TIEBitmap
procedure TextOutEx(aBMP: TIEBitmap;
                    X, Y, W, H : Integer; const Text: string;
                    const sFontName: string; iFontSize: Integer; cFontColor: TColor; Style: TFontStyles;
                    Angle: Integer = 0;
                    bAntiAlias: Boolean = True; bAutoEnlarge: Boolean = False);
var
  textSize: TSize;
  mask: TIEMask;
  x1, y1, x2, y2: Integer;
  compCanvas: TIECanvas;
  iAlpha: Integer;
begin
  aBMP.Canvas.Font.Name  := sFontName;
  aBMP.Canvas.Font.Size  := iFontSize;
  aBMP.Canvas.Font.Color := cFontColor;
  aBMP.Canvas.Font.Style := Style;

  textSize := aBMP.IECanvas.MeasureText( Text );

  if Angle <> 0 then
    textSize := IERotatePoint2( textSize.cx, textSize.cy, Angle );

  if bAutoEnlarge and (( aBMP.Width < textSize.cx ) or ( aBMP.Height < textSize.cy )) then
  begin
    iAlpha := 255;
    if aBMP.HasAlphaChannel then
      iAlpha := aBMP.Alpha[0, 0];
    aBMP.Resize( iMax( aBMP.Width, textSize.cx ), iMax( aBMP.Height, textSize.cy ), Background, iAlpha);
  end;

  compCanvas := aBMP.CreateROICanvas(Rect(0, 0, aBMP.Width, aBMP.Height), bAntiAlias, True, bAntiAlias);
  try
    if not bAntiAlias then  // Cannot use together with alpha composting
      compCanvas.TextRendering := ietrTextRenderingHintSingleBitPerPixel;

    compCanvas.Brush.Color := aBMP.Canvas.Font.Color;
    compCanvas.Font.Color  := aBMP.Canvas.Font.Color;
    compCanvas.Font.Name   := aBMP.Canvas.Font.Name;
    compCanvas.Font.Height := aBMP.Canvas.Font.Height;
    compCanvas.Font.Style  := aBMP.Canvas.Font.Style;

    if ( W = 0 ) and ( H = 0 ) then
      compCanvas.DrawText( Text, X, Y, -Angle )
    else
      compCanvas.DrawText( Text, Rect( X, Y, X + W, Y + H ), -Angle );
  finally
    compCanvas.Free();
  end;
end;