lib/CairoX/Pager.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package CairoX::Pager;
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 __PACKAGE__->mk_accessors( qw(
6 id
7 surface
8 context
9 config
10 page_spec
11 type
12 ) );
13  
14 use Cairo;
15 use File::Spec;
16  
17 =head1 NAME
18  
19 CairoX::Pager - pager for pdf , image surface backend.
20  
21 =head1 VERSION
22  
23 Version 0.02
24  
25 =cut
26  
27 our $VERSION = '0.02';
28  
29 =head1 DESCRIPTION
30  
31 L<Cairo::PdfSurface> supports pages , but image surface doesn't. this module
32 page both pdf or image for you. for image type surface , we export page to a
33 directory and give them a formatted name on C<finish_page> method. for pdf
34 surface , we create pdf document at start , and call cairo context C<show_page>
35 function to start a new page.
36  
37 * svg , ps type surface are not supported yet.
38  
39 =head1 SYNOPSIS
40  
41 export pages pdf:
42  
43 my $pager = CairoX::Pager->new(
44 pdf => { filename => $filepath },
45 page_spec => { width => , height => },
46 );
47  
48 for ( ... ) {
49 $pager->new_page( );
50  
51 my $surface = $pager->surface(); # get cairo surface
52 my $cr = $pager->context(); # get cairo context
53  
54  
55 # draw something
56  
57  
58 $pager->finish_page( );
59 }
60  
61 $pager->finish();
62  
63 export pages as svg :
64  
65 my $pager = CairoX::Pager->new(
66 svg => {
67 directory => $path,
68 filename_format => "%04d.png",
69 },
70 page_spec => { width => , height => },
71 );
72  
73 export pages as png :
74  
75 my $pager = CairoX::Pager->new(
76 png => {
77 directory => $path,
78 filename_format => "%04d.png",
79 dpi => 600,
80 },
81 page_spec => { width => , height => },
82 );
83  
84 =head1 FUNCTIONS
85  
86 =head2 new
87  
88 =cut
89  
90 sub new {
91 my $class = shift;
92 my %args = @_;
93 my $self = bless {}, $class;
94 $self->SUPER::new;
95  
96 die unless $args{png} or $args{pdf} or $args{svg} or $args{ps} ;
97  
98 die unless $args{page_spec};
99  
100 $self->id( 0 );
101 $self->config( $args{png} || $args{pdf} || $args{svg} || $args{ps} );
102 $self->page_spec( $args{page_spec} );
103 $self->type(
104 defined $args{png} ? 'png' :
105 defined $args{pdf} ? 'pdf' :
106 defined $args{svg} ? 'svg' : undef
107 );
108  
109 if( $self->type eq 'pdf' || $self->type eq 'ps' ) {
110 my $class = 'Cairo::' . ucfirst( $self->type ) . 'Surface';
111 my $surface = $class->create(
112 $self->config->{filename},
113 $self->page_spec->{width},
114 $self->page_spec->{height},
115 );
116  
117 my $context = Cairo::Context->create($surface);
118 $self->surface( $surface );
119 $self->context( $context );
120 $self->fill_white();
121 }
122 return $self;
123 }
124  
125  
126 sub fill_white {
127 my $self = shift;
128 my $context = $self->context;
129 $context->rectangle(
130 0, 0,
131 $self->page_spec->{width},
132 $self->page_spec->{height}
133 );
134 $context->set_source_rgba( 1, 1, 1, 1 );
135 $context->fill;
136 }
137  
138 =head2 current_filename
139  
140 =cut
141  
142 sub current_filename {
143 my $self = shift;
144 if( $self->type eq 'png' ) {
145 return File::Spec->join(
146 $self->config->{directory},
147 sprintf( $self->config->{filename_format} , $self->id )
148 );
149 }
150 elsif( $self->type eq 'pdf' ) {
151 return $self->config->{filename};
152 }
153 }
154  
155 =head2 new_page
156  
157 =cut
158  
159 sub new_page {
160 my $self = shift;
161 $self->id( $self->id + 1 );
162  
163 if( $self->type eq 'png' ) {
164 my $surface = Cairo::ImageSurface->create( 'argb32',
165 $self->page_spec->{width},
166 $self->page_spec->{height},
167 );
168 $self->surface( $surface );
169  
170 my $context = Cairo::Context->create($surface);
171 $self->context( $context );
172  
173 $self->fill_white();
174 }
175 elsif( $self->type eq 'svg' ) {
176 my $surface = Cairo::SvgSurface->create(
177 $self->current_filename ,
178 $self->page_spec->{width},
179 $self->page_spec->{height},
180 );
181 $self->surface( $surface );
182  
183 my $context = Cairo::Context->create($surface);
184 $self->context( $context );
185  
186 $self->fill_white();
187  
188 }
189 }
190  
191  
192 =head2 finish_page
193  
194 =cut
195  
196 sub finish_page {
197 my $self = shift;
198 if( $self->type eq 'png' ) {
199 my $filename = $self->current_filename;
200 $self->surface->write_to_png( $filename );
201 $self->surface->finish; # drop references
202  
203 $self->surface( undef );
204 $self->context( undef );
205  
206 # XXX: resolution option
207 # AIINK::Imager->set_file_res( $filename , $self->config->{dpi} );
208 }
209 elsif( $self->type eq 'svg' ) {
210 $self->surface( undef );
211 $self->context( undef );
212 }
213 elsif( $self->type eq 'pdf' ) {
214 $self->context->show_page();
215 $self->surface->flush();
216 }
217 }
218  
219  
220 =head2 finish
221  
222 =cut
223  
224 sub finish {
225 my $self = shift;
226 if( $self->type eq 'pdf' ) {
227 $self->surface->flush();
228 $self->surface->finish();
229 $self->context( undef );
230 $self->surface( undef );
231 }
232 }
233  
234  
235 =head1 AUTHOR
236  
237 c9s, C<< <cornelius.howl at gmail.com> >>
238  
239 =head1 BUGS
240  
241 Please report any bugs or feature requests to C<bug-cairox-pager at
242 rt.cpan.org>, or through the web interface at
243 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CairoX-Pager>. I will be
244 notified, and then you'll automatically be notified of progress on your bug as
245 I make changes.
246  
247 =head1 SUPPORT
248  
249 You can find documentation for this module with the perldoc command.
250  
251 perldoc CairoX::Pager
252  
253 You can also look for information at:
254  
255 =over 4
256  
257 =item * RT: CPAN's request tracker
258  
259 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CairoX-Pager>
260  
261 =item * AnnoCPAN: Annotated CPAN documentation
262  
263 L<http://annocpan.org/dist/CairoX-Pager>
264  
265 =item * CPAN Ratings
266  
267 L<http://cpanratings.perl.org/d/CairoX-Pager>
268  
269 =item * Search CPAN
270  
271 L<http://search.cpan.org/dist/CairoX-Pager/>
272  
273 =back
274  
275  
276 =head1 ACKNOWLEDGEMENTS
277  
278  
279 =head1 COPYRIGHT & LICENSE
280  
281 Copyright 2009 c9s, all rights reserved.
282  
283 This program is free software; you can redistribute it and/or modify it
284 under the same terms as Perl itself.
285  
286  
287 =cut
288  
289 1; # End of CairoX::Pager
↑ Summary