Solved

Wrap text using Image.Picture.Canvas method

Posted on 2006-06-17
3
1,424 Views
Last Modified: 2011-10-03
Hi How can i wrap a paragraph using TextOut Command? or if you have any other good suggestion i'll really appriciate to know.  Here's my procedure below. The line   image.Picture.Bitmap.Canvas.TextOut(112  , 200 , _oTable.fieldbyname('ALLERGIE').asstring); contains a text field which i want to display in a wrap manner.

Thanks in advance

procedure TfDiagnosePatient.DrawPageLabelsPatientRecord2(image:TImage;_oTable:TTable);
var
 x, y  :Integer;
begin
 Try
  x:=0;
  y:=0;

  image.Picture.Bitmap.Canvas.Font.nAME:= 'Arial';
  image.Picture.Bitmap.Canvas.Font.Style:= [fsBold];
  image.Picture.Bitmap.Canvas.Font.Size := 14;
  image.Picture.Bitmap.Canvas.TextOut(16  , 152 , 'More Information');

  image.Picture.Bitmap.Canvas.Font.Size := 10;
  image.Picture.Bitmap.Canvas.Font.Style:= [];
  image.Picture.Bitmap.Canvas.TextOut(32  , 184 , 'Blood Type');
  image.Picture.Bitmap.Canvas.Font.Style:= [fsBold, fsUnderLine];
  image.Picture.Bitmap.Canvas.TextOut(112  , 184 , _oTable.fieldbyname('BLOODTYP').asstring);

  image.Picture.Bitmap.Canvas.Font.nAME:= 'Arial';
  image.Picture.Bitmap.Canvas.Font.Style:= [fsBold];
  image.Picture.Bitmap.Canvas.Font.Size := 12;
  image.Picture.Bitmap.Canvas.TextOut(16  , 224 , 'Allergies');
  image.Picture.Bitmap.Canvas.Font.Style:= [];
  image.Picture.Bitmap.Canvas.Font.Size := 10;
  image.Picture.Bitmap.Canvas.TextOut(112  , 200 , _oTable.fieldbyname('ALLERGIE').asstring);


  image.Picture.Bitmap.Canvas.Font.Style:= [fsBold,fsItalic];
  image.Picture.Bitmap.Canvas.Font.nAME:= 'MS Sans Serif';
  image.Picture.Bitmap.Canvas.Font.Size := 8;
  image.Picture.Bitmap.Canvas.TextOut(16  , 712 , 'Printed on '+formatdatetime('MMMM DD, YYYY  HH:MM:SS am/pm',NOW));
end;
0
Comment
Question by:ryan_sabarre
  • 2
3 Comments
 

Author Comment

by:ryan_sabarre
ID: 16927081
Thanks in advance
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 50 total points
ID: 16931527
Youcan use the "DrawText" method with DT_WORDBREAK
If you don't wish to, or it's too complicated, then here's a simple demo I wrote which does just wordwrapping without using the DrawText method

procedure WordWrapTextOut(canvas: TCanvas; var x, y: integer; S: string; maxwidth, lineheight: integer);

implementation

{$R *.dfm}


procedure TForm1.FormCreate(Sender: TObject);
begin
  image1.picture.bitmap.Width := image1.width;
  image1.picture.Bitmap.Height := image1.height;
  image1.picture.Bitmap.Canvas.Brush.color := clBlack;
  image1.picture.Bitmap.Canvas.Fillrect(RECT(0,0,image1.picture.bitmap.Width,image1.picture.bitmap.Height));
  image1.picture.Bitmap.Canvas.Font.Color := clWhite;
end;

{ This will start drawing text from x, y. If it goes past maxwidth, it will drop to next line.
At the end of this routine, x and y will be at the position just past the end of the text}
procedure WordWrapTextOut(canvas: TCanvas; var x, y: integer; S: string; maxwidth, lineheight: integer);
  const
    delims = ' ,.:;?!';
  var
    textw, texth, tempw: integer;
    firststring, secondstring: string;
    i: integer;
  begin
    firststring := s;
    while firststring <> '' do
    begin
      secondstring := '';
      textw := canvas.TextWidth(firststring);
      while (
              (firststring <> '') and  (textw > maxwidth)
            ) do
      begin
        secondstring := firststring[length(firststring)] + secondstring;
        delete(firststring, length(firststring), 1);
        tempw := canvas.TextWidth(firststring);
        if ( (tempw <= maxwidth) and  (pos(firststring[length(firststring)], delims) > 0) ) then
          textw := tempw;
      end;
// searching for delimiter failed. fall back to wrap at last char
      if firststring = '' then
      begin
        firststring := secondstring;
        secondstring := '';
        textw := canvas.TextWidth(firststring);
        while ( (textw > maxwidth) and (length(firststring) > 0) ) do
        begin
          secondstring := firststring[length(firststring)] + secondstring;
          delete(firststring, length(firststring), 1);
          textw := canvas.TextWidth(firststring);
        end;
      end;
// actually draw some text
      canvas.TextOut(x, y, firststring);
      firststring := secondstring;
      if firststring <> '' then
      begin
        y := y + lineheight;
      end;
    end;
// get the position right in case the user wishes to add some more text to this line
    x := x + textw;
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  x, y: integer;
  lineheight: integer;
begin
  x := 0;
  y := 0;
  lineheight := image1.picture.Bitmap.Canvas.TextHeight('W') + 1; // 1 space between lines
  WordWrapTextOut(image1.picture.Bitmap.Canvas, x, y, 'short line', image1.picture.bitmap.width, lineheight);
  WordWrapTextOut(image1.picture.Bitmap.Canvas, x, y, ' plus this', image1.picture.bitmap.width, lineheight);
// next line
  x := 0;
  y := y + lineheight;
  WordWrapTextOut(image1.picture.Bitmap.Canvas, x, y, 'this is a bigger line which should word wrap', image1.picture.bitmap.width, lineheight);
// next line
  x := 0;
  y := y + lineheight;
  WordWrapTextOut(image1.picture.Bitmap.Canvas, x, y, memo1.lines.text, 200, lineheight);
end;

0
 

Author Comment

by:ryan_sabarre
ID: 16977618
Thanks a lot.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
add combobox item based on numbers 9 154
Downloading email attachments 2 79
URL for downloading Google Chrome for Win XP 2 185
Working with hours 3 65
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question