1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
|
package HTML::FormatPS;
# $Id: FormatPS.pm,v 1.25 1998/03/26 20:31:02 aas Exp $
=head1 NAME
HTML::FormatPS - Format HTML as postscript
=head1 SYNOPSIS
require HTML::FormatPS;
$html = parse_htmlfile("test.html");
$formatter = new HTML::FormatPS
FontFamily => 'Helvetica',
PaperSize => 'Letter';
print $formatter->format($html);
=head1 DESCRIPTION
The HTML::FormatPS is a formatter that outputs PostScript code.
Formatting of HTML tables and forms is not implemented.
You might specify the following parameters when constructing the formatter:
=over 4
=item PaperSize
What kind of paper should we format for. The value can be one of
these: A3, A4, A5, B4, B5, Letter, Legal, Executive, Tabloid,
Statement, Folio, 10x14, Quarto.
The default is "A4".
=item PaperWidth
The width of the paper in points. Setting PaperSize also defines this
value.
=item PaperHeight
The height of the paper in points. Setting PaperSize also defines
this value.
=item LeftMargin
The left margin in points.
=item RightMargin
The right margin in points.
=item HorizontalMargin
Both left and right margin at the same time. The default value is 4 cm.
=item TopMargin
The top margin in points.
=item BottomMargin
The bottom margin in points.
=item VerticalMargin
Both top and bottom margin at the same time. The default value is 2 cm.
=item PageNo
The parameter determines if we should put page numbers on the pages.
The default is yes, so you have to set this value to 0 in order to
suppress page numbers.
=item FontFamily
The parameter specifies which family of fonts to use for the formatting.
Legal values are "Courier", "Helvetica" and "Times". The default is
"Times".
=item FontScale
All fontsizes might be scaled by this factor.
=item Leading
How much space between lines. This is a factor of the fontsize used
for that line. Default is 0.1.
=back
=head1 SEE ALSO
L<HTML::Formatter>
=head1 COPYRIGHT
Copyright (c) 1995-1998 Gisle Aas. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 AUTHOR
Gisle Aas <aas@sn.no>
=cut
use Carp;
use strict;
use vars qw(@ISA $VERSION);
require HTML::Formatter;
@ISA = qw(HTML::Formatter);
$VERSION = sprintf("%d.%02d", q$Revision: 1.25 $ =~ /(\d+)\.(\d+)/);
use vars qw(%PaperSizes %FontFamilies @FontSizes %param $DEBUG);
# A few routines that convert lengths into points
sub mm { $_[0] * 72 / 25.4; }
sub in { $_[0] * 72; }
%PaperSizes =
(
A3 => [mm(297), mm(420)],
A4 => [mm(210), mm(297)],
A5 => [mm(148), mm(210)],
B4 => [729, 1032 ],
B5 => [516, 729 ],
Letter => [in(8.5), in(11) ],
Legal => [in(8.5), in(14) ],
Executive => [in(7.5), in(10) ],
Tabloid => [in(11), in(17) ],
Statement => [in(5.5), in(8.5)],
Folio => [in(8.5), in(13) ],
"10x14" => [in(10), in(14) ],
Quarto => [610, 780 ],
);
%FontFamilies =
(
Courier => [qw(Courier
Courier-Bold
Courier-Oblique
Courier-BoldOblique)],
Helvetica => [qw(Helvetica
Helvetica-Bold
Helvetica-Oblique
Helvetica-BoldOblique)],
Times => [qw(Times-Roman
Times-Bold
Times-Italic
Times-BoldItalic)],
);
# size 0 1 2 3 4 5 6 7
@FontSizes = ( 5, 6, 8, 10, 12, 14, 18, 24, 32);
sub BOLD { 0x01; }
sub ITALIC { 0x02; }
%param =
(
papersize => 'papersize',
paperwidth => 'paperwidth',
paperheight => 'paperheigth',
leftmargin => 'lmW',
rightmargin => 'rmW',
horizontalmargin => 'mW',
topmargin => 'tmH',
bottommargin => 'bmH',
verticalmargin => 'mH',
pageno => 'printpageno',
fontfamily => 'family',
fontscale => 'fontscale',
leading => 'leading',
);
sub new
{
my $class = shift;
my $self = $class->SUPER::new(@_);
# Obtained from the <title> element
$self->{title} = "";
# The font ID last sent to the PostScript output (this may be
# temporarily different from the "current font" as read from
# the HTML input). Initially none.
$self->{psfontid} = "";
# Pending horizontal space. A list [ " ", $fontid, $width ],
# or undef if no space is pending.
$self->{hspace} = undef;
$self;
}
sub default_values
{
(
family => "Times",
mH => mm(40),
mW => mm(20),
printpageno => 1,
fontscale => 1,
leading => 0.1,
papersize => 'A4',
paperwidth => mm(210),
paperheight => mm(297),
)
}
sub configure
{
my($self, $hash) = @_;
my($key,$val);
while (($key, $val) = each %$hash) {
$key = lc $key;
croak "Illegal parameter ($key => $val)" unless exists $param{$key};
$key = $param{$key};
{
$key eq "family" && do {
$val = "\u\L$val";
croak "Unknown font family ($val)"
unless exists $FontFamilies{$val};
$self->{family} = $val;
last;
};
$key eq "papersize" && do {
$self->papersize($val) || croak "Unknown papersize ($val)";
last;
};
$self->{$key} = lc $val;
}
}
}
sub papersize
{
my($self, $val) = @_;
$val = "\u\L$val";
my($width, $height) = @{$PaperSizes{$val}};
return 0 unless defined $width;
$self->{papersize} = $val;
$self->{paperwidth} = $width;
$self->{paperheight} = $height;
1;
}
sub fontsize
{
my $self = shift;
my $size = $self->{font_size}[-1];
$size = 8 if $size > 8;
$size = 3 if $size < 0;
$FontSizes[$size] * $self->{fontscale};
}
# Determine the current font and set font-related members.
# If $plain_with_size is given (a number), use a plain font
# of that size. Otherwise, use the font specified by the
# HTML context. Returns the "font ID" of the current font.
sub setfont
{
my($self, $plain_with_size) = @_;
my $index = 0;
my $family = $self->{family} || 'Times';
my $size = $plain_with_size;
unless ($plain_with_size) {
$index |= BOLD if $self->{bold};
$index |= ITALIC if $self->{italic} || $self->{underline};
$family = 'Courier' if $self->{teletype};
$size = $self->fontsize;
}
my $font = $FontFamilies{$family}[$index];
my $font_with_size = "$font-$size";
if ($self->{currentfont} eq $font_with_size) {
return $self->{currentfontid};
}
$self->{currentfont} = $font_with_size;
$self->{pointsize} = $size;
my $fontmod = "Font::Metrics::$font";
$fontmod =~ s/-//g;
my $fontfile = $fontmod . ".pm";
$fontfile =~ s,::,/,g;
require $fontfile;
{
no strict 'refs';
$self->{wx} = \@{ "${fontmod}::wx" };
}
$font = $self->{fonts}{$font_with_size} || do {
my $fontID = "F" . ++$self->{fno};
$self->{fonts}{$font_with_size} = $fontID;
$fontID;
};
$self->{currentfontid} = $font;
return $font;
}
# Construct PostScript code for setting the current font according
# to $fontid, or an empty string if no font change is needed.
# Assumes the return string will always be output as PostScript if
# nonempty, so that our notion of the current PostScript font
# stays in sync with that of the PostScript interpreter.
sub switchfont
{
my($self, $fontid) = @_;
if ($self->{psfontid} eq $fontid) {
return "";
} else {
$self->{psfontid} = $fontid;
return "$fontid SF";
}
}
# Like setfont + switchfont.
sub findfont
{
my($self, $plain_with_size) = @_;
return $self->switchfont($self->setfont($plain_with_size));
}
sub width
{
my $self = shift;
my $w = 0;
my $wx = $self->{wx};
my $sz = $self->{pointsize};
for (unpack("C*", $_[0])) {
$w += $wx->[$_] * $sz;
}
$w;
}
sub begin
{
my $self = shift;
$self->HTML::Formatter::begin;
# Margins is points
$self->{lm} = $self->{lmW} || $self->{mW};
$self->{rm} = $self->{paperwidth} - ($self->{rmW} || $self->{mW});
$self->{tm} = $self->{paperheight} - ($self->{tmH} || $self->{mH});
$self->{bm} = $self->{bmH} || $self->{mH};
# Font setup
$self->{fno} = 0;
$self->{fonts} = {};
$self->{en} = 0.55 * $self->fontsize(3);
# Initial position
$self->{xpos} = $self->{lm}; # top of the current line
$self->{ypos} = $self->{tm};
$self->{pageno} = 1;
$self->{line} = "";
$self->{showstring} = "";
$self->{currentfont} = "";
$self->{prev_currentfont} = "";
$self->{largest_pointsize} = 0;
$self->newpage;
}
sub end
{
my $self = shift;
$self->showline;
$self->endpage if $self->{out};
my $pages = $self->{pageno} - 1;
my @prolog = ();
push(@prolog, "%!PS-Adobe-3.0\n");
#push(@prolog,"%%Title: No title\n"); # should look for the <title> element
push(@prolog, "%%Creator: HTML::FormatPS (libwww-perl)\n");
push(@prolog, "%%CreationDate: " . localtime() . "\n");
push(@prolog, "%%Pages: $pages\n");
push(@prolog, "%%PageOrder: Ascend\n");
push(@prolog, "%%Orientation: Portrait\n");
my($pw, $ph) = map { int($_); } @{$self}{qw(paperwidth paperheight)};
push(@prolog, "%%DocumentMedia: Plain $pw $ph 0 white ()\n");
push(@prolog, "%%DocumentNeededResources: \n");
my($full, %seenfont);
for $full (sort keys %{$self->{fonts}}) {
$full =~ s/-\d+$//;
next if $seenfont{$full}++;
push(@prolog, "%%+ font $full\n");
}
push(@prolog, "%%DocumentSuppliedResources: procset newencode 1.0 0\n");
push(@prolog, "%%+ encoding ISOLatin1Encoding\n");
push(@prolog, "%%EndComments\n");
push(@prolog, <<'EOT');
%%BeginProlog
/S/show load def
/M/moveto load def
/SF/setfont load def
%%BeginResource: encoding ISOLatin1Encoding
systemdict /ISOLatin1Encoding known not {
/ISOLatin1Encoding [
/space /space /space /space /space /space /space /space
/space /space /space /space /space /space /space /space
/space /space /space /space /space /space /space /space
/space /space /space /space /space /space /space /space
/space /exclam /quotedbl /numbersign /dollar /percent /ampersand
/quoteright
/parenleft /parenright /asterisk /plus /comma /minus /period /slash
/zero /one /two /three /four /five /six /seven
/eight /nine /colon /semicolon /less /equal /greater /question
/at /A /B /C /D /E /F /G
/H /I /J /K /L /M /N /O
/P /Q /R /S /T /U /V /W
/X /Y /Z /bracketleft /backslash /bracketright /asciicircum /underscore
/quoteleft /a /b /c /d /e /f /g
/h /i /j /k /l /m /n /o
/p /q /r /s /t /u /v /w
/x /y /z /braceleft /bar /braceright /asciitilde /space
/space /space /space /space /space /space /space /space
/space /space /space /space /space /space /space /space
/dotlessi /grave /acute /circumflex /tilde /macron /breve /dotaccent
/dieresis /space /ring /cedilla /space /hungarumlaut /ogonek /caron
/space /exclamdown /cent /sterling /currency /yen /brokenbar /section
/dieresis /copyright /ordfeminine /guillemotleft /logicalnot /hyphen
/registered /macron
/degree /plusminus /twosuperior /threesuperior /acute /mu /paragraph
/periodcentered
/cedillar /onesuperior /ordmasculine /guillemotright /onequarter
/onehalf /threequarters /questiondown
/Agrave /Aacute /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla
/Egrave /Eacute /Ecircumflex /Edieresis /Igrave /Iacute /Icircumflex
/Idieresis
/Eth /Ntilde /Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply
/Oslash /Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn
/germandbls
/agrave /aacute /acircumflex /atilde /adieresis /aring /ae /ccedilla
/egrave /eacute /ecircumflex /edieresis /igrave /iacute /icircumflex
/idieresis
/eth /ntilde /ograve /oacute /ocircumflex /otilde /odieresis /divide
/oslash /ugrave /uacute /ucircumflex /udieresis /yacute /thorn
/ydieresis
] def
} if
%%EndResource
%%BeginResource: procset newencode 1.0 0
/NE { %def
findfont begin
currentdict dup length dict begin
{ %forall
1 index/FID ne {def} {pop pop} ifelse
} forall
/FontName exch def
/Encoding exch def
currentdict dup
end
end
/FontName get exch definefont pop
} bind def
%%EndResource
%%EndProlog
EOT
push(@prolog, "\n%%BeginSetup\n");
for $full (sort keys %{$self->{fonts}}) {
my $short = $self->{fonts}{$full};
$full =~ s/-(\d+)$//;
my $size = $1;
push(@prolog, "ISOLatin1Encoding/$full-ISO/$full NE\n");
push(@prolog, "/$short/$full-ISO findfont $size scalefont def\n");
}
push(@prolog, "%%EndSetup\n");
$self->collect("\n%%Trailer\n%%EOF\n");
unshift(@{$self->{output}}, @prolog);
}
sub header_start
{
my($self, $level, $node) = @_;
# If we are close enough to be bottom of the page, start a new page
# instead of this:
$self->vspace(1 + (6-$level) * 0.4);
$self->{bold}++;
push(@{$self->{font_size}}, 8 - $level);
1;
}
sub header_end
{
my($self, $level, $node) = @_;
$self->vspace(1);
$self->{bold}--;
pop(@{$self->{font_size}});
1;
}
sub hr_start
{
my $self = shift;
$self->showline;
$self->vspace(0.5);
$self->skip_vspace;
my $lm = $self->{lm};
my $rm = $self->{rm};
my $y = $self->{ypos};
$self->collect(sprintf "newpath %.1f %.1f M %.1f %.1f lineto stroke\n",
$lm, $y, $rm, $y);
$self->vspace(0.5);
}
sub skip_vspace
{
my $self = shift;
if (defined $self->{vspace}) {
$self->showline;
if ($self->{out}) {
$self->{ypos} -= $self->{vspace} * 10 * $self->{fontscale};
if ($self->{ypos} < $self->{bm}) {
$self->newpage;
}
}
$self->{xpos} = $self->{lm};
$self->{vspace} = undef;
$self->{hspace} = undef;
}
}
sub show
{
my $self = shift;
my $str = $self->{showstring};
return unless length $str;
$str =~ s/([\(\)\\])/\\$1/g; # must escape parentesis
$self->{line} .= "($str)S\n";
$self->{showstring} = "";
}
sub showline
{
my $self = shift;
$self->show;
my $line = $self->{line};
return unless length $line;
$self->{ypos} -= $self->{largest_pointsize} || $self->{pointsize};
if ($self->{ypos} < $self->{bm}) {
$self->newpage;
$self->{ypos} -= $self->{pointsize};
# must set current font again
my $font = $self->{prev_currentfont};
if ($font) {
$self->collect("$self->{fonts}{$font} SF\n");
}
}
my $lm = $self->{lm};
my $x = $lm;
if ($self->{center}) {
# Unfortunately, the center attribute is gone when we get here,
# so this code is never activated
my $linewidth = $self->{xpos} - $lm;
$x += ($self->{rm} - $lm - $linewidth) / 2;
}
$self->collect(sprintf "%.1f %.1f M\n", $x, $self->{ypos}); # moveto
$line =~ s/\s\)S$/)S/; # many lines will end with space
$self->collect($line);
if ($self->{bullet}) {
# Putting this behind the first line of the list item
# makes it more likely that we get the right font. We should
# really set the font that we want to use.
my $bullet = $self->{bullet};
if ($bullet eq '*') {
# There is no character that is really suitable. Lets make
# filled cirle ourself.
my $radius = $self->{pointsize} / 4;
$self->collect(sprintf "newpath %.1f %.1f %.1f 0 360 arc fill\n",
$self->{bullet_pos} + $radius,
$self->{ypos} + $radius, $radius);
} else {
$self->collect(sprintf "%.1f %.1f M\n", # moveto
$self->{bullet_pos},
$self->{ypos});
$self->collect("($bullet)S\n");
}
$self->{bullet} = '';
}
$self->{prev_currentfont} = $self->{currentfont};
$self->{largest_pointsize} = 0;
$self->{line} = "";
$self->{xpos} = $lm;
# Additional linespacing
$self->{ypos} -= $self->{leading} * $self->{pointsize};
}
sub endpage
{
my $self = shift;
# End previous page
$self->collect("showpage\n");
$self->{pageno}++;
}
sub newpage
{
my $self = shift;
if ($self->{'out'}) {
$self->endpage;
}
$self->{'out'} = 0;
my $pageno = $self->{pageno};
$self->collect("\n%%Page: $pageno $pageno\n");
# Print area marker (just for debugging)
if ($DEBUG) {
my($llx, $lly, $urx, $ury) = map { sprintf "%.1f", $_}
@{$self}{qw(lm bm rm tm)};
$self->collect("gsave 0.1 setlinewidth\n");
$self->collect("clippath 0.9 setgray fill 1 setgray\n");
$self->collect("$llx $lly moveto $urx $lly lineto $urx $ury lineto $llx $ury lineto closepath fill\n");
$self->collect("grestore\n");
}
# Print page number
if ($self->{printpageno}) {
$self->collect("%% Title and pageno\n");
my $f = $self->findfont(8);
$self->collect("$f\n") if $f;
my $x = $self->{paperwidth};
if ($x) { $x -= 30; } else { $x = 30; }
$self->collect(sprintf "%.1f 30.0 M($pageno)S\n", $x);
$x = $self->{lm};
$self->collect(sprintf "%.1f 30.0 M($self->{title})S\n", $x);
}
$self->collect("\n");
$self->{xpos} = $self->{lm};
$self->{ypos} = $self->{tm};
}
sub out
{
my($self, $text) = @_;
if ($self->{collectingTheTitle}) {
# Both collect and print the title
$text =~ s/([\(\)\\])/\\$1/g; # Escape parens.
$self->{title} .= $text;
return;
}
my $fontid = $self->setfont();
my $w = $self->width($text);
if ($text =~ /^\s*$/) {
$self->{hspace} = [ " ", $fontid, $w ];
return;
}
$self->skip_vspace;
# determine spacing / line breaks needed before text
if ($self->{hspace}) {
my ($stext, $sfont, $swidth) = @{$self->{hspace}};
if ($self->{xpos} + $swidth + $w > $self->{rm}) {
# line break
$self->showline;
} else {
# no line break; output a space
$self->show_with_font($stext, $sfont, $swidth);
}
$self->{hspace} = undef;
}
# output the text
$self->show_with_font($text, $fontid, $w);
}
sub show_with_font {
my ($self, $text, $fontid, $w) = @_;
my $fontps = $self->switchfont($fontid);
if (length $fontps) {
$self->show;
$self->{line} .= "$fontps\n";
}
$self->{xpos} += $w;
$self->{showstring} .= $text;
$self->{largest_pointsize} = $self->{pointsize}
if $self->{largest_pointsize} < $self->{pointsize};
$self->{'out'}++;
}
sub pre_out
{
my($self, $text) = @_;
$self->skip_vspace;
$self->tt_start;
my $font = $self->findfont();
if (length $font) {
$self->show;
$self->{line} .= "$font\n";
}
while ($text =~ s/(.*)\n//) {
$self->{'out'}++;
$self->{showstring} .= $1;
$self->showline;
}
$self->{showstring} .= $text;
$self->tt_end;
}
sub bullet
{
my($self, $bullet) = @_;
$self->{bullet} = $bullet;
$self->{bullet_pos} = $self->{lm};
}
sub adjust_lm
{
my $self = shift;
$self->showline;
$self->{lm} += $_[0] * $self->{en};
}
sub adjust_rm
{
my $self = shift;
$self->showline;
$self->{rm} += $_[0] * $self->{en};
}
sub head_start {
1;
}
sub head_end {
1;
}
sub title_start {
my($self) = @_;
$self->{collectingTheTitle} = 1;
1;
}
sub title_end {
my($self) = @_;
$self->{collectingTheTitle} = 0;
1;
}
1;
|