Games::Bingo::Print - a PDF Generation Class for Games::Bingo


Games-Bingo-Print documentation Contained in the Games-Bingo-Print distribution.

Index


Code Index:

NAME

Top

Games::Bingo::Print - a PDF Generation Class for Games::Bingo

SYNOPSIS

Top

	use Games::Bingo::Print;

	my $bp = Games::Bingo::Print-E<gt>new();

	$bp-E<gt>print_pages(2);

	my $bp = Games::Bingo::Print->new(
		heading  => 'Jimmys bingohalle',
		text     => 'its all in the game!'
		filename => 'jimmys.pdf
	);

VERSION

Top

This documentation describes version 0.03 of Games::Bingo::Print

DESCRIPTION

Top

This is that actual printing class. It generates a PDF file with pages containing bingo cards.

The page contains space for 3 bingo cards, each consisting of 3 rows and 10 columns like this:

So a filled out example card could look like this:

SUBROUTINES/METHODS

Top

new

The constructor

The constructor can take several options, all these are optional.

* heading

The heading on the generated bingo card PDF.

* text

The smaller text on the generated bingo card PDF, the default is the authors name (SEE AUTHOR section below).

* filename

The name of the file containing the generated bingo card PDF, the default is 'bingo.pdf'

If it is not possible to create an object the constructor dies with the diagnostic 'Unable to construct object' and some additional diagnostic depending on the problem, which might relate to third party components used. See DEPENDENCIES.

DIAGNOSTICS

Top

* 'Unable to construct object', a dianostic from the constructor (new) and some additional diagnostic depending on the problem, which might relate to third party components used. See DEPENDENCIES.

CONFIGURATION AND ENVIRONMENT

Top

Games::Bingo::Print requires no special configuration or environment apart from what is listed in the DEPENDENCIES section.

DEPENDENCIES

Top

* Games::Bingo
* Games::Bingo::Card
* PDFLib

INCOMPATIBILITIES

Top

There are no known incompatibilities.

BUGS AND LIMITATIONS

Top

The PDF generator only works with Games::Bingo

BUGREPORTING

Top

Please report issues via CPAN RT:

  http://rt.cpan.org/NoAuth/Bugs.html?Dist=Business-DK-CPR

or by sending mail to

  bug-Business-DK-CPR@rt.cpan.org

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Games::Bingo::Print

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Games-Bingo-Print

* CPAN Ratings

http://cpanratings.perl.org/d/Games-Bingo-Print

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-Bingo-Print

* Search CPAN

http://search.cpan.org/dist/Games-Bingo-Print

TEST

Top

I am currently not able to generate a test coverage report for Games::Bingo::Print.

Perl::Critic tests (t/critic) are enable by settting the environment variable TEST_AUTHOR.

Kwalitee tests (t

SEE ALSO

Top

* bin/bingo_print.pl

TODO

Top

The TODO file contains a complete list for the Games::Bingo::Print class.

AUTHOR

Top

* Jonas B. Nielsen, (jonasbn) <jonasbn@cpan.org>

ACKNOWLEDGEMENTS

Top

* Thanks to Matt Sergeant for suggesting using PDFLib.

LICENSE AND COPYRIGHT

Top


Games-Bingo-Print documentation Contained in the Games-Bingo-Print distribution.

package Games::Bingo::Print;

# $Id: Print.pm 1835 2007-03-17 17:36:20Z jonasbn $

use strict;
use warnings;
use integer;
use Carp qw(croak);
use PDFLib;
use vars qw($VERSION);

use Games::Bingo::Card;

$VERSION = '0.04';

sub new {
    my ( $class, %opts ) = @_;

    my $self = bless {
        text => $opts{text} ? $opts{text} : 'by jonasbn <jonasbn@cpan.org>',
        heading   => $opts{heading}   ? $opts{heading}   : 'Games::Bingo',
        papersize => $opts{papersize} ? $opts{papersize} : 'a4',
    }, $class || ref $class;

    eval {
        $self->{pdf} = PDFLib->new(
            filename => $opts{filename} ? $opts{filename} : 'bingo.pdf',
            papersize => $self->{papersize},
            creator   => 'Games::Bingo::Print',
            author    => 'Jonas B. Nielsen',
            title     => 'Bingo!',
        );
        $self->{pdf}->start_page;
    };

    if ($@) {
        croak 'Unable to construct object - '.$@;
    } else {
        return $self;
    }
}

sub print_pages {
    my ( $self, $pages, $cards ) = @_;

    eval {
        foreach my $i ( 1 .. $pages )
        {

            if ($cards) {
                if ( $cards > 3 ) {
                    $cards = 3;
                }
            } else {
                $cards = 3;
            }

            $self->{pdf}->set_font(
                face => 'Helvetica',
                size => 40,
                bold => 1
            );
            $self->{pdf}->print_boxed(
                $self->{heading},
                mode => 'center',
                'x'  => 0,
                'y'  => 740,
                'w'  => 595,
                'h'  => 50
            );
            $self->{pdf}->set_font(
                face => 'Helvetica',
                size => 12,
                bold => 1
            );
            $self->{pdf}->print_boxed(
                $self->{text},
                'mode' => 'center',
                'x'    => 0,
                'y'    => 685,
                'w'    => 595,
                'h'    => 50
            );

            my $y_start_cordinate = 685;
            my $x_start_cordinate = 30;
            my $size              = 60;
            my $yec;
            my $ysc;
            my $cardsize = $size * 3;

            for ( my $card = 1; $card <= $cards; $card++ ) {

                $ysc = $y_start_cordinate - $cardsize;
                $yec = $ysc + $cardsize;

                $self->_print_card(
                    size              => $size,
                    x_start_cordinate => $x_start_cordinate,
                    y_start_cordinate => $ysc,
                    y_end_cordinate   => $yec,
                );
                $y_start_cordinate = $ysc - 50;
            }
        }
        $self->{pdf}->stroke;
        $self->{pdf}->finish;
    };

    if ($@) {
        carp ('Unable to generate page - '.$@);
        return 0;
    } else {
        return 1;
    }
}

sub _print_card {
    my $self = shift;
    my %args = @_;

    my $p = Games::Bingo::Card->new();
    $p = $p->populate();

    my $ysc  = $args{'y_start_cordinate'};
    my $yec  = $args{'y_end_cordinate'};
    my $xsc  = $args{'x_start_cordinate'};
    my $size = $args{'size'};

    my $y = 3;
    for ( my $ry = $ysc; $ry < $yec; $ry += $size ) {
        my @numbers;
        for ( my $x = 0; $x <= 9; $x++ ) {
            push( @numbers, $p->[ $x - 1 ]->[ $y - 1 ] );
        }
        $self->_print_row(
            size              => $size,
            x_start_cordinate => $xsc,
            y_start_cordinate => $ry,
            x_end_cordinate   => 540,
            numbers           => \@numbers,
        );
        $y--;
    }
    return 1;
}

sub _print_row {
    my $self = shift;
    my %args = @_;

    my $ysc     = $args{'y_start_cordinate'};
    my $xsc     = $args{'x_start_cordinate'};
    my $xec     = $args{'x_end_cordinate'};
    my $size    = $args{'size'};
    my $numbers = $args{'numbers'};

    my $x;
    for ( my $rx = $xsc; $rx <= $xec; $rx += $size ) {
        ++$x;
        my $label = $numbers->[$x] ? $numbers->[$x] : '';

        $self->{pdf}->rect(
            'x' => $rx,
            'y' => $ysc,
            'w' => $size,
            'h' => $size
        );
        $self->{pdf}->stroke;
        $self->{pdf}->set_font(
            face => 'Helvetica',
            size => 40,
            bold => 1
        );
        $self->{pdf}->print_at(
            $label,
            'mode' => 'right',
            'w'    => $size,
            'h'    => $size,
            'x'    => $rx + 8,
            'y'    => $ysc + 13
        );

    }
    return 1;
}

1;

__END__