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