Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Variable Width Printing on XP

by T.G. Cornholio (Scribe)
on Apr 05, 2005 at 01:39 UTC ( [id://444836]=perlquestion: print w/replies, xml ) Need Help??

T.G. Cornholio has asked for the wisdom of the Perl Monks concerning the following question:

Wise Monks,

I would like to develop a method of printing a given string, using a given font, in a specific amount of space. For example, print the string "Hi there" in Arial font, such that it is exactly 4.5 inches wide on the printed page. Now here's the problem. The space given may entail printing multiple pages. Frequently, this turns into something like having a single letter on each page. In this case I would like to have some kind of marks on the page to indicate where the text on this page ends and the next page begins. Aligning the two marks would recover the original spacing between letters.

Everything I have considered so far seems to boil down to one of two issues:

1. I don't know how to determine the width of a particular letter or word when using a variable pitch font. Thus I don't know how to compute the overall size of the printed phrase.

2. Limited font support. I would like to have as many of the Windows fonts supported as is possible.

I have considered the following methods and concluded the following. If my conclusions are incorrect, it would be great to hear about that also.

Win32::Printer - Problem 1

Generate HTML and let browser print it - From my readings so far, HTML rendering may not give reproducible results across browser/printer combinations (but I could be wrong). Also, font support may not be as extensive as all the fonts available on MS Windows.

Generating PDF - Both #1 and #2 above.

Automating MS Word - problem #1 above.

If you're wondering about the application of all of this, read on.

My father is into woodworking, and he has a pantograph for his router. He would like to say "I have a piece of wood 30 x 40 inches, my phrase is (blah), use font (foo)". Then the program would take over, do the necessary calculations and print a document that he can take out to the shop. The pantograph takes 1 page at a time, thus the need for the ability to align the work across several printed pages so that variable pitch fonts don't come out looking strange on the finished work.

Replies are listed 'Best First'.
Re: Variable Width Printing on XP
by Nevtlathiel (Friar) on Apr 05, 2005 at 07:33 UTC
    Generating PDF - Both #1 and #2 above.

    PDF::Create has a function that will return the width of a string given the string and the font (but doesn't take into account the font size, so you have to multiply by that), but only has very limited support for fonts. Nothing to stop you looking at the code to see how it's done though ;)

Re: Variable Width Printing on XP
by jhourcle (Prior) on Apr 05, 2005 at 02:15 UTC

    You might want to start by taking a look at the specifications for True Type fonts. I've never had a need to go that deep, so I can't offer anything other than what to call it. You might also be able to do some of it in PostScript. (yet another printing language, that I've never had reason to learn.)

Re: Variable Width Printing on XP
by ikegami (Patriarch) on Apr 05, 2005 at 03:29 UTC

      Unfortunately you can't access the .NET methods directly via OLE without creating an intermediate class that is registered for COM interop.

      The Windows API that provides this information is probably found in GDI.

      /J\

        Thre you go. It's GetTextExtentPoint32. Now the OP has to find out how to get the proper Device Context X_X.
Re: Variable Width Printing on XP
by Popcorn Dave (Abbot) on Apr 05, 2005 at 04:14 UTC
    Something you may want to consider is using a graphics program to do what you want, then print it from there. The Gimp is available for windows and there are Perl extensions for that. I've heard that it's possible to automate Photoshop using Perl as well, but I've never seen an example of anyone doing it.

    Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://444836]
Approved by Popcorn Dave
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found