line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Convert::ToImage; |
2
|
1
|
|
|
1
|
|
109191
|
use 5.006001; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
36
|
|
3
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
36
|
|
5
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
103
|
|
6
|
1
|
|
|
1
|
|
4
|
use base qw( Image::Magick ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
781
|
|
7
|
|
|
|
|
|
|
use vars qw( $VERSION $LEGAL_ATTRIBUTES ); |
8
|
|
|
|
|
|
|
our $VERSION = sprintf("%d.%02d", q$Revision: 0.00 $ =~ /(\d+)\.(\d+)/); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $LEGAL_ATTRIBUTES = { |
11
|
|
|
|
|
|
|
LINE_HEIGHT => 18, |
12
|
|
|
|
|
|
|
LINE_LENGTH => 100, |
13
|
|
|
|
|
|
|
OUTPUT_FILE => 'www.hjackson.org', |
14
|
|
|
|
|
|
|
IMAGE_EXT => 'png', |
15
|
|
|
|
|
|
|
POINT_SIZE => 12, |
16
|
|
|
|
|
|
|
TEXT => 'http://www.hjackson.org', |
17
|
|
|
|
|
|
|
BG_COLOR => 'white', |
18
|
|
|
|
|
|
|
TEXT_COLOR => 'blue', |
19
|
|
|
|
|
|
|
FONT => 'Bookman-Demi', |
20
|
|
|
|
|
|
|
INPUT_FILE => 'xc:white', |
21
|
|
|
|
|
|
|
MAGICK => undef, |
22
|
|
|
|
|
|
|
LEVEL => 0, |
23
|
|
|
|
|
|
|
FONT => 'Courier', |
24
|
|
|
|
|
|
|
XSKEW => 0, |
25
|
|
|
|
|
|
|
YSKEW => 0, |
26
|
|
|
|
|
|
|
}; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $magick_setup = { |
29
|
|
|
|
|
|
|
size => undef, |
30
|
|
|
|
|
|
|
pointsize => undef, |
31
|
|
|
|
|
|
|
}; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub _init { |
34
|
|
|
|
|
|
|
my ($self, $config) = @_; |
35
|
|
|
|
|
|
|
for my $field ( keys %{ $LEGAL_ATTRIBUTES } ) { |
36
|
|
|
|
|
|
|
my $lc_field = lc $field; |
37
|
|
|
|
|
|
|
no strict "refs"; |
38
|
|
|
|
|
|
|
*$lc_field = sub { |
39
|
|
|
|
|
|
|
my $self = shift; |
40
|
|
|
|
|
|
|
return $self->(uc $field, @_); |
41
|
|
|
|
|
|
|
}; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
while ( my ($key, $val) = each %{ $config }) { |
44
|
|
|
|
|
|
|
$key = lc($key); |
45
|
|
|
|
|
|
|
$self->$key($val); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
unless( ref($self->magick()) ) { |
48
|
|
|
|
|
|
|
my $magick = Image::Magick->new() || die "Unable to create Image::Magick Object $!"; |
49
|
|
|
|
|
|
|
$self->magick( $magick ); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub _get_metrics { |
56
|
|
|
|
|
|
|
my ($self, $filename) = @_; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
if(ref($filename) ne 'GLOB') { |
59
|
|
|
|
|
|
|
my $file = $self->untaint($filename); |
60
|
|
|
|
|
|
|
croak "Possible security problem with filename: $filename" unless($file); |
61
|
|
|
|
|
|
|
open ($filename, "<$file") or die "$!\n"; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $text; |
65
|
|
|
|
|
|
|
my $line_length = 0; |
66
|
|
|
|
|
|
|
my $metrics = { |
67
|
|
|
|
|
|
|
max_line_length => 0, |
68
|
|
|
|
|
|
|
max_line_length_at => 0, |
69
|
|
|
|
|
|
|
linecount => 0, |
70
|
|
|
|
|
|
|
text => 0, |
71
|
|
|
|
|
|
|
}; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
my $line; |
74
|
|
|
|
|
|
|
while(<$filename>) { |
75
|
|
|
|
|
|
|
$text .= $_; |
76
|
|
|
|
|
|
|
$line = $_; |
77
|
|
|
|
|
|
|
$line_length = length($line); |
78
|
|
|
|
|
|
|
if($line_length > $metrics->{max_line_length}) { |
79
|
|
|
|
|
|
|
$metrics->{max_line_length_at} = $metrics->{linecount}; |
80
|
|
|
|
|
|
|
$metrics->{max_line_length} = $line_length; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
$metrics->{linecount}++; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
$metrics->{text} = $text; |
85
|
|
|
|
|
|
|
return $metrics; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub generate { |
89
|
|
|
|
|
|
|
my ($self, $config) = @_; |
90
|
|
|
|
|
|
|
my $metrics = $self->_get_metrics($config->{filename}); |
91
|
|
|
|
|
|
|
my $point_size = $config->{point_size}; |
92
|
|
|
|
|
|
|
my $font = $config->{font}; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# I need to make sure that windows is handled as well |
95
|
|
|
|
|
|
|
my @lines = split( /\n/, $metrics->{text}); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my $size = "size=>'10" . "x" . "20'"; |
98
|
|
|
|
|
|
|
$self->Set(eval $size); |
99
|
|
|
|
|
|
|
$self->Read('xc:white'); |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
my ($x_ppem, $y_ppem, $ascender, $descender, $width, $height, $max_advance) = |
102
|
|
|
|
|
|
|
$self->QueryFontMetrics(text =>$lines[$metrics->{max_line_length_at}], |
103
|
|
|
|
|
|
|
pointsize =>$point_size, |
104
|
|
|
|
|
|
|
font =>$font); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
$width = $width+$max_advance+$x_ppem; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$self->Scale(width=>$width,height=>$height * $metrics->{linecount}); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
my $x = $point_size; |
111
|
|
|
|
|
|
|
my $y = $point_size; |
112
|
|
|
|
|
|
|
$self->Comment("http://www.hjackson.org/"); |
113
|
|
|
|
|
|
|
foreach (@lines) { |
114
|
|
|
|
|
|
|
($x_ppem, $y_ppem, $ascender, $descender, $width, $height, $max_advance) = |
115
|
|
|
|
|
|
|
$self->QueryFontMetrics(text=>$_,pointsize=>$point_size,font=>$font); |
116
|
|
|
|
|
|
|
$self->Annotate( text => $_, |
117
|
|
|
|
|
|
|
font => $font, |
118
|
|
|
|
|
|
|
fill =>'black', |
119
|
|
|
|
|
|
|
align =>'Left' , |
120
|
|
|
|
|
|
|
pointsize=>$point_size, |
121
|
|
|
|
|
|
|
x => $x, |
122
|
|
|
|
|
|
|
y => $y, |
123
|
|
|
|
|
|
|
); |
124
|
|
|
|
|
|
|
$y = $y+$height; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
#$self->Set(compression=>'None'); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
#--------------------------------------------------------- |
133
|
|
|
|
|
|
|
# |
134
|
|
|
|
|
|
|
# |
135
|
|
|
|
|
|
|
# |
136
|
|
|
|
|
|
|
#--------------------------------------------------------- |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub untaint { |
139
|
|
|
|
|
|
|
my ($self, $data) = @_; |
140
|
|
|
|
|
|
|
if ($data =~ /^([-\@\w.]+)$/) { |
141
|
|
|
|
|
|
|
$data = $1; # $data now untainted |
142
|
|
|
|
|
|
|
} else { |
143
|
|
|
|
|
|
|
return undef; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
return $data; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub calculate { |
150
|
|
|
|
|
|
|
my ($self, $config) = @_; |
151
|
|
|
|
|
|
|
my $text = $config->{TEXT}; |
152
|
|
|
|
|
|
|
my $level = $config->{LEVEL}; |
153
|
|
|
|
|
|
|
my $font = $config->{FONT}; |
154
|
|
|
|
|
|
|
my $xskew = $config->{XSKEW}; |
155
|
|
|
|
|
|
|
my $yskew = $config->{YSKEW}; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
my @lines = split (/\n/, $text); |
158
|
|
|
|
|
|
|
my $measure; |
159
|
|
|
|
|
|
|
foreach (@lines) { |
160
|
|
|
|
|
|
|
my $len = length($_); |
161
|
|
|
|
|
|
|
$measure->{LENGTH} = $len unless ($measure->{LENGTH} && $measure->{LENGTH} > $len); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
# Need to handle references to scalars Filhandles and all other sorts of |
164
|
|
|
|
|
|
|
# text that people my want converted. |
165
|
|
|
|
|
|
|
my $nullchar_count = ($text =~ tr[.|/][]); |
166
|
|
|
|
|
|
|
$nullchar_count = $nullchar_count * 5 ; |
167
|
|
|
|
|
|
|
my $point_size = $config->{POINTSIZE}; |
168
|
|
|
|
|
|
|
my $border = ($point_size / 5); |
169
|
|
|
|
|
|
|
my $size = "size=>'10" . "x" . "20'"; |
170
|
|
|
|
|
|
|
$self->Set(eval $size); |
171
|
|
|
|
|
|
|
$self->Read('xc:white'); |
172
|
|
|
|
|
|
|
my ($x_ppem, $y_ppem, $ascender, $descender, $width, $height, $max_advance) = |
173
|
|
|
|
|
|
|
$self->QueryFontMetrics(text=>$text,pointsize=>$point_size,font=>$font); |
174
|
|
|
|
|
|
|
$width = $width+$max_advance+$x_ppem+$nullchar_count; |
175
|
|
|
|
|
|
|
$self->Scale(width=>$width,height=>$height); |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
$self->obfuscate($width, $height, $level, $point_size); |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
my $x = $point_size; |
180
|
|
|
|
|
|
|
my $y = $point_size; |
181
|
|
|
|
|
|
|
my @letters = split (//, $text); |
182
|
|
|
|
|
|
|
$self->Comment("http://www.hjackson.org/"); |
183
|
|
|
|
|
|
|
foreach (@letters) { |
184
|
|
|
|
|
|
|
($x_ppem, $y_ppem, $ascender, $descender, $width, $height, $max_advance) = |
185
|
|
|
|
|
|
|
$self->QueryFontMetrics(text=>$_,pointsize=>$point_size,font=>$font); |
186
|
|
|
|
|
|
|
#warn "letter == $_\n"; |
187
|
|
|
|
|
|
|
$self->Annotate( text => $_, |
188
|
|
|
|
|
|
|
font => $font, |
189
|
|
|
|
|
|
|
fill =>'black', |
190
|
|
|
|
|
|
|
align =>'Left' , |
191
|
|
|
|
|
|
|
pointsize=>$point_size, |
192
|
|
|
|
|
|
|
x => $x, |
193
|
|
|
|
|
|
|
y => $y, |
194
|
|
|
|
|
|
|
skewX =>int(rand($xskew * 3 )), |
195
|
|
|
|
|
|
|
skewY =>int(rand($yskew * 3 )), |
196
|
|
|
|
|
|
|
); |
197
|
|
|
|
|
|
|
$x = $x+$width; |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
$self->Set(compression=>'None'); |
201
|
|
|
|
|
|
|
return $self; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub obfuscate { |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
my ($self, $width, $height, $level, $point_size ) = @_; |
207
|
|
|
|
|
|
|
return $self if ($level eq 0); |
208
|
|
|
|
|
|
|
my @colors = qw(white red black green blue); |
209
|
|
|
|
|
|
|
my $loop = $level * 10 * int($level/2); |
210
|
|
|
|
|
|
|
my @pixel_pos = (); |
211
|
|
|
|
|
|
|
#$self->Blur(); |
212
|
|
|
|
|
|
|
#$self->AddNoise(noise=>'Uniform'); |
213
|
|
|
|
|
|
|
foreach (1 .. $loop) { |
214
|
|
|
|
|
|
|
my $col = int(rand(4)); |
215
|
|
|
|
|
|
|
$pixel_pos[0] = int(rand($width)); |
216
|
|
|
|
|
|
|
$pixel_pos[1] = int(rand($height)); |
217
|
|
|
|
|
|
|
my $string = "'pixel[ $pixel_pos[0], $pixel_pos[1] ]'=>$col"; |
218
|
|
|
|
|
|
|
$self->Set(eval $string); |
219
|
|
|
|
|
|
|
#warn "" . $pixel_pos[0] . "," . $pixel_pos[1] . "\n"; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
#$self->Set('pixel[5,5]'=>'red'); |
223
|
|
|
|
|
|
|
return $self; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
sub calculate_lines { |
227
|
|
|
|
|
|
|
my $self = shift; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
1; |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
__END__ |