summaryrefslogtreecommitdiff
path: root/lib/GridFiller/Result/Pango.pm
blob: 504f2a7085b05ed4157a1c43c55052600f85c881 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package GridFiller::Result::Pango; 
use Moose;
use namespace::autoclean;
use GridFiller::Constants ':directions';
use MooseX::Types::Moose qw(Int);
use Cairo;
use Pango;
use Carp;
 
extends 'GridFiller::Result';
 
with 'MooseX::Log::Log4perl';
 
has cell_size => (
    isa => Int,
    is => 'rw',
    default => 20,
);
 
has _width => (
    isa => Int,
    is => 'ro',
    lazy_build => 1,
    clearer => '_reset_w',
);
 
sub _build__width {
    my ($self) = @_;
 
    return $self->cell_size *  @{$self->source_grid->[0]};
}
 
has _height => (
    isa => Int,
    is => 'ro',
    lazy_build => 1,
    clearer => '_reset_h',
);
 
sub _build__height {
    my ($self) = @_;
 
    return $self->cell_size *  @{$self->source_grid};
}
 
has _cairo_s => (
    is => 'ro',
    lazy_build => 1,
    clearer => '_reset_s',
);
 
sub _build__cairo_s {
    my ($self) = @_;
 
    my $cs = Cairo::ImageSurface->create(
        'argb32',
        $self->_width,
        $self->_height,
    );
 
    return $cs;
};
 
has _cairo_c => (
    is => 'ro',
    lazy_build => 1,
    clearer => '_reset_c',
);
 
sub _build__cairo_c {
    my ($self) = @_;
 
    my $cr = Cairo::Context->create($self->_cairo_s);
    $self->_put_squares($cr);
 
    return $cr;
}
 
sub _put_squares {
    my ($self,$cr) = @_;
 
    $self->log->debug(sprintf('putting squares (%dx%d)',$self->_width,$self->_height));
 
    my $size = $self->cell_size;
 
    $cr->rectangle(0,0,$self->_width,$self->_height);
    $cr->set_source_rgb(1,1,1);
    $cr->fill;
 
    my $y=0;
    for my $row (@{$self->source_grid}) {
        my $x=0;
        for my $cell (@$row) {
            if ($cell eq '*') {
                $cr->rectangle($x,$y,$size,$size);
                $cr->set_source_rgb(0,0,0);
                $cr->fill;
            }
            $x+=$size;
        }
        $y+=$size;
    }
 
    return;
}
 
sub place_word_at {
    my ($self$word$x$y$dir) = @_;
 
    $self->log->debug("Placing $word at ${x}:${y} ($dir)");
 
    $self->_cairo_c;
}
 
sub as_png {
    my ($self) = @_;
 
    $self->_cairo_c->show_page;
 
    my $buffer;
    $self->_cairo_s->write_to_png_stream(sub{$buffer.=$_[1]});
    return $buffer;
}
 
sub save_png {
    my ($self,$filename) = @_;
 
    $self->_cairo_c->show_page;
 
    $self->_cairo_s->write_to_png($filename);
    return;
}
 
sub to_string {}
 
after reset => sub {
    my ($self) = @_;
    $self->_reset_w;
    $self->_reset_h;
    $self->_reset_c;
    $self->_reset_s;
};
 
 
1;