could you post your additions to add color? Sounds great :)
Sure. Eventually, I'd like to get it on CPAN, but I'm not sure how long it can still take, so in the meantime, here's my add-on module for
PDF::Create, which I gave the name PDF::Create::Extensions. All you need is this extra file, which adds a few methods to PDF::Create::Page, and they're available as soon as you do
use PDF::Create;
use PDF::Create::Extensions;
You can create two kinds of colour, "linecolor" (for lines) and "fillcolor" (for area fills). For each, you can set the color in the same way, in RGB ("RGB" or "RG": red, green, blue, each value ranging from 0 to 1, less is darker), grayscale ("GRAY", "GREY" or "G", 1 value from 0 (white) to 1 (black); and CMYK ("CMYK" or "K", 4 values from 0 to 1, more is darker), like this:
$page->linecolor(RGB => 0.6, 0.2, 0.2);
The color type is case insensitive.
The second thing you can set, is line width, line connection style ("join": styles "miter", "round", "bevel"), and line end style ("cap": styles "butt", "round", "square" (="projecting square")), like this:
$page->linewidth(0.8, 'join' => 'miter');
See the PDF documentation on what the possibilities represent, or just experiment with these options. Again, the names are case insensitive.
Save the next code as PDF/Create/Extensions.pm somewhere in your @INC
# file PDF/Create/Extensions.pm
package PDF::Create::Extensions;
package PDF::Create::Page::Extensions;
require PDF::Create::Page;
push @PDF::Create::Page::ISA, __PACKAGE__;
use strict;
use Carp;
my %color_mode = (
GRAY => [ G => 1 ],
RGB => [ RG => 3 ],
CMYK => [ K => 4 ],
);
foreach(keys %color_mode) {
$color_mode{uc $color_mode{$_}[0]} ||= $color_mode{$_};
}
$color_mode{GREY} = $color_mode{GRAY};
sub _modecolor {
my $mode = uc shift;
if(my $info = $color_mode{$mode}) {
$#_ = $info->[1] - 1;
return @_, $info->[0];
}
croak "'$mode' is not a valid color mode";
}
sub fillcolor {
my $self = shift;
my @cmd = _modecolor(@_); $_ = lc for $cmd[-1];
$self->{'pdf'}->page_stream($self);
$self->{'pdf'}->add(join ' ', @cmd);
}
sub linecolor {
my $self = shift;
my @cmd = _modecolor(@_); $_ = uc for $cmd[-1];
$self->{'pdf'}->page_stream($self);
$self->{'pdf'}->add(join ' ', @cmd);
}
my %line_styles = (
CAP => [ J => { BUTT => 0, ROUND => 1, 'PROJECTING SQUARE' => 2,
+ SQUARE => 2 }],
JOIN => [ j => { MITER => 0, ROUND => 1, BEVEL => 2 }],
);
sub linewidth {
my $self = shift;
$self->{'pdf'}->page_stream($self);
my @add = (shift, 'w');
while(@_) {
my $key = uc shift;
my $value = uc shift;
if(my $dispatch = $line_styles{$key}) {
if($value =~ /^\d+$/) {
} elsif(exists $dispatch->[1]{$value}) {
$value = $dispatch->[1]{$value};
} else {
croak "'$value' is not a valid parameter for line styl
+e mode '$key'";
}
push @add, $value, $dispatch->[0];
} else {
croak "'$key' is not a valid line style key";
}
}
$self->{'pdf'}->add(join ' ', @add);
}
1;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.