in reply to Re: Re: How do I replace certain character if condition exists
in thread How do I replace certain character if condition exists

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;

  • Comment on PDF::Create::Extensions (was: Re: How do I replace certain character if condition exists)
  • Select or Download Code

Replies are listed 'Best First'.
Re: PDF::Create::Extensions (was: Re: How do I replace certain character if condition exists)
by peppiv (Curate) on Apr 28, 2003 at 17:32 UTC
    Wow bart! Very cool. Very easy to use. Thanks a million.

    peppiv