PostScript

 view release on metacpan or  search on metacpan

TextBlock.pm  view on Meta::CPAN

    $tb->addText(text => "It was a dark and stormy night and Spokane Sam\'s
    Spongepants were thirsty...", %body);


=item numElements()

Returns the number of elements in the text block object. An 'element' is
created each time the addText() method is called.

=item Write( $width, $height, $xoffset, $yoffset )

The Write() method will generate the PostScript code that will render the text
on a page when passed to a PostScript interpreter such as Ghostscript. The
four parameters are expressed in points (1/72 inch) and indicate the width and
height of the box within which the text should be printed, and the x and y
offset of the upper left corner of this box.

Important: PostScript defines the orgin (0,0) as the lower left corner of
the page! This *will* mess you up.

Standard page sizes in points are:

     Paper Size                      Width, Height (in points)
     .........................       .........................
     Letter                          612, 792
     Legal                           612, 1008
     Ledger                          1224, 792
     Tabloid                         792, 1224
     A0                              2384, 3370
     A1                              1684, 2384
     A2                              1191, 1684
     A3                              842, 1191
     A4                              595, 842
     A5                              420, 595
     A6                              297, 420
     A7                              210, 297
     A8                              148, 210
     A9                              105, 148
     B0                              2920, 4127
     B1                              2064, 2920
     B2                              1460, 2064
     B3                              1032, 1460
     B4                              729, 1032
     B5                              516, 729
     B6                              363, 516
     B7                              258, 363
     B8                              181, 258
     B9                              127, 181
     B10                             91, 127
     #10 Envelope                    297, 684
     C5 Envelope                     461, 648
     DL Envelope                     312, 624
     Folio                           595, 935
     Executive                       522, 756

The write() method returns two values: a string consisting of the PostScript
code (suitable for printing to a file), and a TextBlock object containing the
elements (and partial elements) that did not fit within the specified area,
if any. If the entire text block fits with the area, the remainder will be
undef. The remainder can be used to layout multiple pages and columns, etc. in
a similar manner to most modern desktop publishing programs. In general, the
write() method should be called as in the following, which writes the
PostScript code to a file called 'psoutput.ps':

    open OUT, '>psoutput.ps';
    my ($code, $remainder) = $tb->Write(572, 752, 20, 772);
    print OUT $code;

To print an entire text block that spans multiple pages, you could do
something like this:

(add enough text to the text block first..)

    open OUT, '>psoutput.ps';
    my $pages = 1;

    # Create the first page
    #
    my ($code, $remainder) = $tb->Write(572, 752, 20, 772);
    print OUT "%%Page:$pages\n";      # this is required by the Adobe
                                      # Document Structuring Conventions
    print OUT $code;
    print OUT "showpage\n";

    # Print the rest of the pages, if any
    #
    while ($remainder->numElements) {
        $pages++;
        print OUT "%%Page:$pages\n";
        ($code, $remainder) = $remainder->Write(572, 752, 20, 772);
        print OUT $code;
        print OUT "showpage\n";
    }

However, if you use the PostScript::Document module to construct generic
multi-page PostScript documents, you don't have to worry about this.

=head1 A NOTE ABOUT FONT METRICS

The write() method uses the module PostScript::Metrics to determine the width of
each character; widths vary from font to font and character to character.
If you were writing a stright PostScript program, you would let the PostScript
interpreter do this for you, but in the case of this program, we need to know
the width of each character in a font within the Perl script. The PostScript::Metrics
module contains the font metrics (i.e., a list containing the width of each
character in the font) for a bunch of fonts that are listed above under the
description of the addText() method. This set started with the metrics for all
of the default fonts with AFM files that came with GhostScript. It is slowly
growing as more fonts are mapped. To add support for a new font, you must
create the array with the metrics for that font and add it to the PostScript::Metrics
module. For a font with an AFM file, the AFM file can be parsed with Gisle
Aas' Font::AFM module, available on CPAN.

Please send all PostScript::Metrics patches to the author at shawn@as220.org.

=head1 TODO

* better compliance with Adobe's Document Structuring Conventions
* more font metrics descriptions
* make font loading code smarter and more efficient for the interpreter
* support a larger character set



( run in 2.355 seconds using v1.01-cache-2.11-cpan-e1769b4cff6 )