?? formatps.pm
字號:
package HTML::FormatPS;
# $Id: FormatPS.pm,v 1.24 1998/01/06 09:49:15 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-1997 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@oslonett.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.24 $ =~ /(\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");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -