Board index » delphi » Newbie: Justification of text using canvas.textout?

Newbie: Justification of text using canvas.textout?

This seems like it should be an easy thing to do, but I want to be able to
right and left justify lines of text I send to the printer using
Canvas.TextOut.  I know I can measure the width of the string I am printing,
but I can't figure out how to dynamically add spaces to the line to line the
text up on the right and left.

Am I going about this the wrong way?  I definitely don't want to use a
third-party reporting tool like ReportSmith.

Thanks,

David Montgomery

 

Re:Newbie: Justification of text using canvas.textout?


Quote
des...@nexus.interealm.com (David Montgomery) wrote:
>This seems like it should be an easy thing to do, but I want to be able to
>right and left justify lines of text I send to the printer using
>Canvas.TextOut.  I know I can measure the width of the string I am printing,
>but I can't figure out how to dynamically add spaces to the line to line the
>text up on the right and left.
>Am I going about this the wrong way?  I definitely don't want to use a
>third-party reporting tool like ReportSmith.

While I would not be at all surprised if there were a better solution to
your justification questions, here's a few lines of code to right-justify a
string, FWIW:

function Spaces(n : byte) : string;
var
  i : byte;
begin
  Result := '';
  for i := 1 to n do
    Result := Result + ' ';
end;

...

function RightJustified(var S : string; Width : byte) : string;
begin
  Result := Spaces( Width - length(S) ) + S;
end;

...

const
  LineWidth = 80;

begin
  String1 := 'This is an example';
  String2 := RightJustified(String1, LineWidth);
end;

---
Andrew Myers           :       --> *{*word*180} Here! * <--
greyj...@macatawa.org  :  (you may be one of the lucky 25)

Re:Newbie: Justification of text using canvas.textout?


In article <4fe6t1$...@spieg.interealm.com>, des...@nexus.interealm.com (David Montgomery) says:

Quote

>This seems like it should be an easy thing to do, but I want to be able to
>right and left justify lines of text I send to the printer using
>Canvas.TextOut.  I know I can measure the width of the string I am printing,
>but I can't figure out how to dynamically add spaces to the line to line the
>text up on the right and left.

>Am I going about this the wrong way?  I definitely don't want to use a
>third-party reporting tool like ReportSmith.

>Thanks,

>David Montgomery

Hello David,
you have several options:
1.What you seem to want to do:
   Add spaces in a loop, testing for the new width, until the string
   is where you want it to be.  Ok, but not receommended.
2.Once you have the width, adjust the x coordinate s.t. x+width is
   at the right edge.  This should be easy.
Good luck!

Jochen

________________________________________________________________________
joch...@berlin.snafu.de                        71270.3...@compuserve.com
I post, therefore I am.

Re:Newbie: Justification of text using canvas.textout?


Don't think in terms of adding spaces to the text!
If the width of your print area is 6 inches, and you wanted to right justify
text that was 4 inches wide, shouldn't you just start printing the line two
inches (6 - 4 = 2) from the left side?

var
  PixelsInInchX,                   {Conversion from inches to Pixels}
  PrintStartPOSX,                {Where to start printing the line from left}
  PrintStartPOSY:  Integer;   {Where to start printing the line from top}    

begin  
  PixelsInInchX := GetDeviceCaps(Printer.Handle,LOGPIXELSX);
  PrintStartPOS := 6*PixelsInInchX -  Canvas.TextWidth(YourStringVar);
  Canvas.TextOut(PrintStartPOSX, PrintStartPOSY, YourStringVar);
end

In article <4fe6t1$...@spieg.interealm.com>,
   des...@nexus.interealm.com (David Montgomery) wrote:

Quote
>This seems like it should be an easy thing to do, but I want to be able to
>right and left justify lines of text I send to the printer using
>Canvas.TextOut. ...

Re:Newbie: Justification of text using canvas.textout?


Quote
>des...@nexus.interealm.com (David Montgomery) wrote:
>>This seems like it should be an easy thing to do, but I want to be able to
>>right and left justify lines of text I send to the printer using
>>Canvas.TextOut.  I know I can measure the width of the string I am printing,
>>but I can't figure out how to dynamically add spaces to the line to line the
>>text up on the right and left.

My approach would be to textout every word separately, calculating
the x,y position, so as to avoid the problem of how many extra
space characters to insert where. If you do extra-space and
x position calculations in floating point, and use Round(floatingX)
in the textout, you will be within 1/2 pixel.

You'll have to decide whether to measure and preserve original
spaces along with adjacent words, and add pixels from there, or
just measure the words and do all the spacing yourself (don't
forget to allow for minimum space when finding word break).

You might want to put extra space after periods that precede space
in the original, or other niceties, if you want to get fancy.

If you are not already doing so, you may want to operate on
paragraphs rather than lines, so you can minimize the amount
of extra space you need to stuff in, and so you can leave
the last line plain left justified.

Quote
>>Am I going about this the wrong way?  I definitely don't want to use a
>>third-party reporting tool like ReportSmith.

Not knowing what the big picture is, it's hard to say. But justifying
a paragraph shouldn't be much harder than describing it in detail.

I'm sure this wheel has been re-invented regularly. If you find
searching for little things faster than coding, you'll probably
find something.

HTH.
Regards,
Bengt Richter

Re:Newbie: Justification of text using canvas.textout?


Quote
des...@nexus.interealm.com (David Montgomery) wrote:
>This seems like it should be an easy thing to do, but I want to be able to
>right and left justify lines of text I send to the printer using
>Canvas.TextOut.  I know I can measure the width of the string I am printing,
>but I can't figure out how to dynamically add spaces to the line to line the
>text up on the right and left.
>Am I going about this the wrong way?  I definitely don't want to use a
>third-party reporting tool like ReportSmith.
>Thanks,
>David Montgomery

There is a help file on the Super Page called Lhelp( I think).
Anyhow, it as a program in it to do just what you want. You can use it
as info to do all kinds of justification by doing pixel offsets(which
is the correct way), setting up columns and so on. You can also turn
it into a unit to make a pretty good text formating and printing
system.
Mike Miller

Re:Newbie: Justification of text using canvas.textout?


Quote
In article <1996Feb13.153016.27...@nosc.mil>, mille...@hqamc.safb.af.mil (Mike Miller) wrote:
>des...@nexus.interealm.com (David Montgomery) wrote:

>>This seems like it should be an easy thing to do, but I want to be able to
>>right and left justify lines of text I send to the printer using
>>Canvas.TextOut.  I know I can measure the width of the string I am printing,
>>but I can't figure out how to dynamically add spaces to the line to line the
>>text up on the right and left.

>>Am I going about this the wrong way?  I definitely don't want to use a
>>third-party reporting tool like ReportSmith.

>>Thanks,

>>David Montgomery
>There is a help file on the Super Page called Lhelp( I think).
>Anyhow, it as a program in it to do just what you want. You can use it
>as info to do all kinds of justification by doing pixel offsets(which
>is the correct way), setting up columns and so on. You can also turn
>it into a unit to make a pretty good text formating and printing
>system.
>Mike Miller

I checked the Delphi Super Page for any file named "Lhelp", or any thing even
close, but with no luck.  Do you happen to know the author's name, or anything
else that could help me find it?

Thanks,

David

Other Threads