1 |
package Frey::Cairo; |
2 |
use Moose; |
3 |
|
4 |
extends 'Frey'; |
5 |
with 'Frey::Web'; |
6 |
#with 'Frey::Storage'; |
7 |
|
8 |
use Cairo; |
9 |
use Font::FreeType; |
10 |
|
11 |
has path => ( |
12 |
is => 'rw', |
13 |
isa => 'Str', |
14 |
required => 1, |
15 |
default => 'var/cairo/telop.png', |
16 |
); |
17 |
|
18 |
has width => ( |
19 |
is => 'rw', |
20 |
isa => 'Int', |
21 |
required => 1, |
22 |
default => 200, |
23 |
); |
24 |
|
25 |
has height => ( |
26 |
is => 'rw', |
27 |
isa => 'Int', |
28 |
required => 1, |
29 |
default => 40, |
30 |
); |
31 |
|
32 |
has font_size => ( |
33 |
is => 'rw', |
34 |
isa => 'Int', |
35 |
required => 1, |
36 |
default => 23, |
37 |
); |
38 |
|
39 |
has x => ( |
40 |
is => 'rw', |
41 |
isa => 'Int', |
42 |
required => 1, |
43 |
default => 20, |
44 |
); |
45 |
|
46 |
has y => ( |
47 |
is => 'rw', |
48 |
isa => 'Int', |
49 |
required => 1, |
50 |
default => 25, |
51 |
); |
52 |
|
53 |
has text => ( |
54 |
is => 'rw', |
55 |
isa => 'Str', |
56 |
required => 1, |
57 |
default => 'Hello, world!', |
58 |
); |
59 |
|
60 |
sub as_markup { |
61 |
my ($self) = @_; |
62 |
|
63 |
my @fonts = ( |
64 |
'/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf', |
65 |
'/usr/share/fonts/truetype/ttf-inconsolata/Inconsolata.otf', |
66 |
'/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf', |
67 |
); |
68 |
|
69 |
my $ttf_path; |
70 |
foreach my $font ( @fonts ) { |
71 |
$ttf_path ||= $font if -e $font; |
72 |
} |
73 |
die "Can't find font $ttf_path" unless -r $ttf_path; |
74 |
|
75 |
my $ft_face = Font::FreeType->new->face($ttf_path); |
76 |
my $cr_face = Cairo::FtFontFace->create($ft_face); |
77 |
|
78 |
my $surface = Cairo::ImageSurface->create('argb32', $self->width, $self->height); |
79 |
|
80 |
my $cr = Cairo::Context->create($surface); |
81 |
$cr->set_font_face($cr_face); |
82 |
$cr->set_font_size( $self->font_size ); |
83 |
$cr->move_to( $self->x, $self->y ); |
84 |
$cr->show_text( $self->text ); |
85 |
$cr->show_page; |
86 |
|
87 |
$self->mkbasepath( $self->path ); |
88 |
$surface->write_to_png( $self->path ); |
89 |
|
90 |
return qq|<img src="/| . $self->path . qq|">|; |
91 |
} |
92 |
|
93 |
1; |