CairoX::Pager - pager for pdf , image surface backend.


CairoX-Pager documentation Contained in the CairoX-Pager distribution.

Index


Code Index:

NAME

Top

CairoX::Pager - pager for pdf , image surface backend.

VERSION

Top

Version 0.02

DESCRIPTION

Top

Cairo::PdfSurface supports pages , but image surface doesn't. this module page both pdf or image for you. for image type surface , we export page to a directory and give them a formatted name on finish_page method. for pdf surface , we create pdf document at start , and call cairo context show_page function to start a new page.

* svg , ps type surface are not supported yet.

SYNOPSIS

Top

export pages pdf:

    my $pager = CairoX::Pager->new(
        pdf => { filename => $filepath },
        page_spec => { width =>  , height => },
    );

    for ( ... ) {
        $pager->new_page( );

        my $surface = $pager->surface();   # get cairo surface 
        my $cr = $pager->context();    # get cairo context




        # draw something




        $pager->finish_page( );
    }

    $pager->finish();

export pages as svg :

    my $pager = CairoX::Pager->new( 
        svg => { 
            directory => $path,
            filename_format => "%04d.png",
        },
        page_spec => { width =>  , height => },
    );

export pages as png :

    my $pager = CairoX::Pager->new( 
        png => { 
            directory => $path,
            filename_format => "%04d.png",
            dpi => 600,
        },
        page_spec => { width =>  , height => },
    );

FUNCTIONS

Top

new

current_filename

new_page

finish_page

finish

AUTHOR

Top

c9s, <cornelius.howl at gmail.com>

BUGS

Top

Please report any bugs or feature requests to bug-cairox-pager at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CairoX-Pager. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

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

    perldoc CairoX::Pager

You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=CairoX-Pager

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/CairoX-Pager

* CPAN Ratings

http://cpanratings.perl.org/d/CairoX-Pager

* Search CPAN

http://search.cpan.org/dist/CairoX-Pager/

ACKNOWLEDGEMENTS

Top

COPYRIGHT & LICENSE

Top


CairoX-Pager documentation Contained in the CairoX-Pager distribution.
package CairoX::Pager;
use warnings;
use strict;
use base qw(Class::Accessor::Fast);
__PACKAGE__->mk_accessors( qw(
    id
    surface 
    context 
    config 
    page_spec 
    type
) );

use Cairo;
use File::Spec;

our $VERSION = '0.02';

sub new {
    my $class = shift;
    my %args = @_;
    my $self = bless {}, $class;
    $self->SUPER::new;

    die unless $args{png} or $args{pdf} or $args{svg} or $args{ps} ;

    die unless $args{page_spec};

    $self->id( 0 );
    $self->config( $args{png} || $args{pdf} || $args{svg} || $args{ps} );
    $self->page_spec(  $args{page_spec} );
    $self->type( 
        defined $args{png} ? 'png' : 
            defined $args{pdf} ? 'pdf' :
                defined $args{svg} ? 'svg' : undef 
    );

    if( $self->type eq 'pdf' || $self->type eq 'ps' ) {
        my $class = 'Cairo::' . ucfirst( $self->type ) . 'Surface';
        my $surface = $class->create(
            $self->config->{filename},
            $self->page_spec->{width},
            $self->page_spec->{height},
        );

        my $context = Cairo::Context->create($surface);
        $self->surface( $surface );
        $self->context( $context );
        $self->fill_white();
    }
    return $self;
}


sub fill_white {
    my $self = shift;
    my $context = $self->context;
    $context->rectangle( 
        0, 0,
        $self->page_spec->{width},
        $self->page_spec->{height}
    );
    $context->set_source_rgba( 1, 1, 1, 1 );
    $context->fill;
}

sub current_filename {
    my $self = shift;
    if( $self->type eq 'png' ) {
        return File::Spec->join(
                    $self->config->{directory},
                    sprintf( $self->config->{filename_format} , $self->id ) 
        );
    }
    elsif( $self->type eq 'pdf' ) {
        return $self->config->{filename};
    }
}

sub new_page {
    my $self = shift;
    $self->id(  $self->id + 1 );

    if( $self->type eq 'png' ) {
        my $surface = Cairo::ImageSurface->create( 'argb32',
            $self->page_spec->{width},
            $self->page_spec->{height},
        );
        $self->surface( $surface );

        my $context = Cairo::Context->create($surface);
        $self->context( $context );

        $self->fill_white();
    }
    elsif( $self->type eq 'svg' ) {
        my $surface = Cairo::SvgSurface->create( 
            $self->current_filename ,
            $self->page_spec->{width},
            $self->page_spec->{height},
        );
        $self->surface( $surface );

        my $context = Cairo::Context->create($surface);
        $self->context( $context );

        $self->fill_white();

    }
}


sub finish_page {
    my $self = shift;
    if( $self->type eq 'png' ) {
        my $filename = $self->current_filename;
        $self->surface->write_to_png( $filename );
        $self->surface->finish;  # drop references

        $self->surface( undef );
        $self->context( undef );

        # XXX: resolution option
        # AIINK::Imager->set_file_res( $filename , $self->config->{dpi} );
    }
    elsif( $self->type eq 'svg' ) {
        $self->surface( undef );
        $self->context( undef );
    }
    elsif( $self->type eq 'pdf' ) {
        $self->context->show_page();
        $self->surface->flush();
    }
}


sub finish {
    my $self = shift;
    if( $self->type eq 'pdf' ) {
        $self->surface->flush();
        $self->surface->finish();
        $self->context( undef );
        $self->surface( undef );
    }
}


1; # End of CairoX::Pager