blib/lib/CairoX/Sweet.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod n/a totaltotal 0.0% 0 / 142 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use 5.14.0;
2 use strict;
3 use warnings;
4 use CairoX::Sweet::Standard;
5  
6 # PODNAME: CairoX::Sweet
7  
8 class CairoX::Sweet using Moose {
9  
10 our $VERSION = '0.0108'; # VERSION
11 # ABSTRACT: Wraps Cairo for easier drawing
12  
13 use Cairo;
14 use Type::Utils qw/enum/;
15 use Types::Path::Tiny qw/AbsPath/;
16 use CairoX::Sweet::Color;
17 use CairoX::Sweet::Path;
18 use CairoX::Sweet::Core::CurveTo;
19 use CairoX::Sweet::Core::LineTo;
20 use CairoX::Sweet::Core::MoveTo;
21  
22 has surface_format => (
23 is => 'ro',
24 isa => Str,
25 );
26 has width => (
27 is => 'ro',
28 isa => Int,
29 required => 1,
30 );
31 has height => (
32 is => 'ro',
33 isa => Int,
34 required => 1,
35 );
36 has background_color => (
37 is => 'ro',
38 isa => Maybe[Color],
39 predicate => 1,
40 coerce => 1,
41 );
42 has filename => (
43 is => 'rw',
44 isa => Maybe[AbsPath],
45 coerce => 1,
46 );
47 has c => (
48 is => 'ro',
49 isa => CairoContext,
50 );
51 has surface => (
52 is => 'ro',
53 isa => CairoSurface,
54 );
55  
56 around BUILDARGS($orig: $self, Int $width, Int $height, @args) {
57 my %args = @args;
58  
59 $args{'width'} = $width;
60 $args{'height'} = $height;
61 $args{'surface_format'} = 'argb32' if !exists $args{'surface_format'};
62 $args{'surface'} = Cairo::ImageSurface->create($args{'surface_format'}, $args{'width'}, $args{'height'}) if !exists $args{'surface'};
63 $args{'c'} = $args{'context'} // $args{'c'} // Cairo::Context->create($args{'surface'});
64 delete $args{'context'};
65  
66 $self->$orig(%args);
67 }
68  
69 method BUILD {
70 if($self->has_background_color) {
71 $self->c->rectangle(0, 0, $self->width, $self->height);
72 $self->c->set_source_rgb($self->background_color->color);
73 $self->c->fill;
74 }
75 }
76 method new_svg($class: Int $width!, Int $height!, AbsPath $filename! does coerce, @args) {
77 my %args = @args;
78 $args{'surface'} = Cairo::SvgSurface->create($filename, $width, $height);
79 $args{'filename'} = $filename;
80 return $class->new($width, $height, %args);
81 }
82  
83 method add_path(Path $path, Bool :$close = 0
84 ) {
85 if($path->has_background_color) {
86 foreach my $command ($path->all_commands) {
87 my $method = $command->method;
88 $self->c->$method($command->out);
89 }
90 $self->c->set_source_rgb($path->background_color->color);
91 $self->c->fill;
92 }
93 if(($path->has_background_color && $path->has_color) || !$path->has_background_color) {
94 foreach my $command ($path->all_commands) {
95 my $method = $command->method;
96 $self->c->$method($command->out);
97 }
98 $self->c->set_line_cap($path->cap);
99 $self->c->set_line_join($path->join);
100 $self->c->set_source_rgb($path->color->color) if $path->has_color;
101 $self->c->set_line_width($path->width) if $path->has_width;
102 $self->c->close_path if $close;
103 $self->c->stroke;
104 }
105 }
106  
107 method add_text(Num :$x, Num :$y, Color :$color does coerce, Str :$text!, ArrayRef[Str] :$font_face = [], Num :$font_size, Enum[qw/normal italic oblique/] :$slant = 'normal', Enum[qw/normal bold/] :$weight = 'normal') {
108 $self->c->select_font_face(@{ $font_face }, $slant, $weight) if scalar @$font_face;
109 $self->c->set_font_size($font_size) if defined $font_size;
110 $self->c->set_source_rgb($color->color) if defined $color;
111 $self->c->move_to($x, $y) if defined $x && defined $y;
112  
113 $self->c->show_text($text);
114 $self->c->fill;
115  
116 }
117  
118 }
119  
120 1;
121  
122 __END__
123  
124 =pod
125  
126 =encoding utf-8
127  
128 =head1 NAME
129  
130 CairoX::Sweet - Wraps Cairo for easier drawing
131  
132  
133  
134 =begin HTML
135  
136 <p><img src="https://img.shields.io/badge/perl-5.14+-brightgreen.svg" alt="Requires Perl 5.14+" /> <a href="https://travis-ci.org/Csson/p5-CairoX-Sweet"><img src="https://api.travis-ci.org/Csson/p5-CairoX-Sweet.svg?branch=master" alt="Travis status" /></a></p>
137  
138 =end HTML
139  
140  
141 =begin markdown
142  
143 ![Requires Perl 5.14+](https://img.shields.io/badge/perl-5.14+-brightgreen.svg) [![Travis status](https://api.travis-ci.org/Csson/p5-CairoX-Sweet.svg?branch=master)](https://travis-ci.org/Csson/p5-CairoX-Sweet)
144  
145 =end markdown
146  
147 =head1 VERSION
148  
149 Version 0.0108, released 2015-03-21.
150  
151 =head1 SYNOPSIS
152  
153 use CairoX::Sweet;
154  
155 my $c = CairoX::Sweet->new(500, 500, '#ffffff');
156 my $path = CairoX::Sweet::Path->new(start => [35, 50], color => '#8855bb', width => 10, cap => 'round', join => 'round');
157  
158 $path->add_relative_line(qw/
159 20 -5
160 10 0
161 30 -20
162 -50 0
163 /);
164 $c->add_path($path, close => 1);
165  
166 $c->surface->write_to_png('a_short_line.png');
167  
168 =head1 DESCRIPTION
169  
170 CairoX::Sweet is a wrapper around L<Cairo> which uses the great L<cairo|http://www.cairographics.org/> graphics library.
171  
172 For some use cases the standard api is a little verbose, the goal of this distribution is to reduce that.
173  
174 =head1 CONSTRUCTORS
175  
176 =head2 new($width, $height, %args)
177  
178 my $c = CairoX::Sweet->new(700, 500);
179 my $c = CairoX::Sweet->new(700, 500, surface_format => 'argb32');
180  
181 Both of these does this:
182  
183 my $surface = Cairo::ImageSurface->create('argb32', 700, 500);
184 my $context = Cairo::Context->create($surface);
185  
186 Use this constructor if you want to save the output as a png file later.
187  
188 =head2 new_svg($width, $height, $filename, %args)
189  
190 my $c = CairoX::Sweet->new_svg(700, 500, 'output.svg');
191  
192 Does this:
193  
194 Cairo::SvgSurface->create($filename, $width, $height);
195  
196 Use this constructor if you want to save the output as an svg file later
197  
198 =head1 ATTRIBUTES
199  
200 =head2 width
201  
202 =head2 height
203  
204 my $c = CairoX::Sweet->new(700, 500);
205  
206 Integers. Required. The width and height of the surface.
207  
208 =head2 background_color
209  
210 my $c1 = CairoX::Sweet->new(..., background_color => '#83f9e2');
211 my $c2 = CairoX::Sweet->new(..., background_color => [220, 75, 230]);
212 my $c3 = CairoX::Sweet->new(..., background_color => [0.2, 0.4, 0.6]);
213  
214 String or array reference of numbers. Optional. The starting background color.
215  
216 You can pick one of three ways to give a color:
217  
218 =over 4
219  
220 =item *
221  
222 As a hexadecimal string
223  
224 =item *
225  
226 As an array reference giving a number each for red, green and blue (between 0 and 255).
227  
228 =item *
229  
230 As an array reference in fractions between 0 and 1. This is the C<cairo> style. B<Note:> A color given as C<[1, 1, 1]> is interpreted as (nearly) black, and not white as C<cairo> would do.
231  
232 =back
233  
234 =head2 filename
235  
236 my $c = CairoX::Sweet->new_svg(..., filename => 'mysvg.svg');
237  
238 String or a L<Path::Tiny> path. Required for L<new_svg|/"new_svg($width, $height, $filename, %args)">.
239  
240 =head1 METHODS
241  
242 =head2 add_path()
243  
244 Adds a L<CairoX::Sweet::Path> to the C<cairo> object.
245  
246 $c->add_path($path, close => 1);
247  
248 C<$path>
249  
250 The L<CairoX::Sweet::Path> to add. Required.
251  
252 C<close => Bool>
253  
254 Boolean. Optional named parameter, defaults to C<0>. If positive, will call C<close_path|http://cairographics.org/manual/cairo-Paths.html#cairo-close-path>
255 on the C<$path> (connects the two end points of the C<$path>).
256  
257 =head2 add_text()
258  
259 Adds a string to the C<cairo> object. Takes only named parameters.
260  
261 $c->add_text( text => "The text to add",
262 color => '#444444',
263 x => 37,
264 y => 115,
265 font_face => ['courier'],
266 font_size => 13
267 weight => 'bold',
268 );
269  
270 C<text => 'the text'>
271  
272 String. The only required parameter. The text to add.
273  
274 C<x>
275  
276 C<y>
277  
278 Integers. The position to start at.
279  
280 C<color>
281  
282 A color. The color of the text. See L</"background_color"> for more information on colors.
283  
284 C<font_face>
285  
286 An array reference of font faces. There is no list of available font faces.
287  
288 C<font_size>
289  
290 Number. The size of the text. Default is C<10>.
291  
292 C<weight>
293  
294 One of C<normal> or C<bold>.
295  
296 C<slant>
297  
298 One of C<normal>, C<italic> or C<oblique>.
299  
300 =head2 c()
301  
302 Returns the L<Cairo> object itself.
303  
304 $c->c->set_source_rgb(0.4, 0.3, 0.2);
305 $c->c->set_line_cap('round');
306 $c->c->arc(150, 200, 35, 0, 3.1415);
307 $c->c->stroke;
308  
309 =head2 surface()
310  
311 Return the L<Cairo surface|https://metacpan.org/pod/Cairo#Surfaces>.
312  
313 $c->surface->write_to_png('a_short_line.png');
314  
315 =head1 STABILITY
316  
317 Since this is an early release things might break, but hopefully not without good cause.
318  
319 =head1 PERFORMANCE
320  
321 Using both L<Moose> and L<Moops> adds a little startup cost. If you need performance it is recommended to use L<Cairo> directly.
322  
323 =head1 SEE ALSO
324  
325 =over 4
326  
327 =item *
328  
329 L<CairoX::Sweet::Path>
330  
331 =item *
332  
333 L<Cairo>
334  
335 =back
336  
337 =head1 SOURCE
338  
339 L<https://github.com/Csson/p5-CairoX-Sweet>
340  
341 =head1 HOMEPAGE
342  
343 L<https://metacpan.org/release/CairoX-Sweet>
344  
345 =head1 AUTHOR
346  
347 Erik Carlsson <info@code301.com>
348  
349 =head1 COPYRIGHT AND LICENSE
350  
351 This software is copyright (c) 2015 by Erik Carlsson <info@code301.com>.
352  
353 This is free software; you can redistribute it and/or modify it under
354 the same terms as the Perl 5 programming language system itself.
355  
356 =cut