Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

This is more a 'please advise question', on how to attack a particular problem:

I'm printing (hardcopy) using Win32::Printer. it all works OK except formatting. I'm this case I'm printing the message and address of a picture postcard. I have the address lines in one array and the message text (having a lovely time, weather fine etc) in another.

My problem is that as the message lines are different for each message, and the font type is also changing and NOT fixed width. When I join the appropriate message and address lines, the address 'column' 'wobbles' top to bottom, due to the variations in letter width from the true type fonts (TTF) in the message.

I have tried 'pack' ing out the address lines and various sprintf horrors, but the varying character widths mess it up every time. I recon I can fix this with tabs, but its going to get very messy...

Sooo..., can any monks off up any ideas on how to format this to allow for TTF, in a more elegant way???



Thanks

Eadmund
  • Comment on Formatting Printed column output with variable width Fonts

Replies are listed 'Best First'.
Re: Formatting Printed column output with variable width Fonts
by roboticus (Chancellor) on Aug 10, 2008 at 12:34 UTC

    Since the write() method allows you to specify the starting point of a string, *don't* join the lines. Instead, chop your postcard into two blocks, one for the message, and one for the address. Then write all your message lines in the message box, and write the address to the address box. That way, you can control your left margin without worrying about tabs, blanks, etc.

    You might write a sub to write a block of text, something like this (*untested*):

    # Write message write_text($dc, 1.0, 1.0, 0.2, "Anonymous Monk--", "Am in Hell, having a terrible time.", "Wish *you* were here! (Ha, ha!)", "--roboticus" ); # Where to send it! write_text($dc, 4.0, 2.5, 0.25, "Anonymous Monk", "11 Two-bit lane", "Cyberspace, 11010-0101" ); sub write_text { my $hPrt = shift; my $Left = shift; my $Top = shift; my $NL = shift; for (@_) { $hPrt->write($_, $Left, $Top); $Top += $NL; } }
    --roboticus
Re: Formatting Printed column output with variable width Fonts
by graff (Chancellor) on Aug 10, 2008 at 14:31 UTC
    I've never tried it, but you might want to: Font::TTFMetrics -- there's a portion of the man page that actually talks about how to work out the width of a string in a given font.

    Update: Another thing to consider (might be overkill, but might be the easiest approach in some unexpected sense of "easy"): set up the post card as a PDF, using PDF::Create. I have tried that (though not for post cards), and it's quite usable.

      Thanks for the quick turn around and all the ideas guys.
Re: Formatting Printed column output with variable width Fonts
by wfsp (Abbot) on Aug 10, 2008 at 11:07 UTC
    Perhaps consider using Win32::OLE to create a Word document with a table?