summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2011-10-30 11:21:52 +0100
committerDaniel Friesel <derf@finalrewind.org>2011-10-30 11:21:52 +0100
commit40ec43afcb4f3ef677c27056cb5024f6f5845adc (patch)
tree8bdc727ced546783dc119c612af72e40f4da6f96 /lib
parent2dcc2f578a6bc407276622f0766e09e8d0bcfa88 (diff)
Autocreate Fakedisplay.pm with cached font.png
Diffstat (limited to 'lib')
-rw-r--r--lib/App/VRR/Fakedisplay.pm.PL (renamed from lib/App/VRR/Fakedisplay.pm)164
1 files changed, 90 insertions, 74 deletions
diff --git a/lib/App/VRR/Fakedisplay.pm b/lib/App/VRR/Fakedisplay.pm.PL
index 58edb4a..ebb436d 100644
--- a/lib/App/VRR/Fakedisplay.pm
+++ b/lib/App/VRR/Fakedisplay.pm.PL
@@ -1,3 +1,69 @@
+#!/usr/bin/env perl;
+use strict;
+use warnings;
+use 5.010;
+use utf8;
+
+use GD;
+
+my ($out_file) = @ARGV;
+my $font = GD::Image->new('share/font.png');
+my $black = $font->colorClosest(0, 0, 0);
+
+open(my $out_fh, '>', $out_file) or die("open ${out_file}: $!");
+
+sub write_out {
+ my ($off_x, $off_y, $char) = @_;
+ my $char_w = 0;
+
+ say $out_fh "'${char}' => {";
+ say $out_fh 'matrix => [';
+
+ for my $pos_y ( $off_y .. ($off_y + 10)) {
+ print $out_fh '[ ';
+ for my $pos_x ( $off_x .. ($off_x + 10)) {
+ if ($font->getPixel($pos_x, $pos_y) == $black) {
+
+ if (($pos_x - $off_x) > $char_w) {
+ $char_w = $pos_x - $off_x;
+ }
+
+ print $out_fh '1, ';
+ }
+ else {
+ print $out_fh '0, ';
+ }
+ }
+ say $out_fh ' ],';
+ }
+
+ # spacing (one empty column)
+ $char_w++;
+
+ if ($char eq q{ }) {
+ $char_w = 5;
+ }
+
+ say $out_fh '],';
+ say $out_fh "width => ${char_w},";
+ say $out_fh '},';
+}
+
+sub parse_char_row {
+ my ($off_y, @chars) = @_;
+ my $off_x = 0;
+
+ for my $char (@chars) {
+ write_out($off_x, $off_y, $char);
+ $off_x += 10;
+ }
+
+ return;
+}
+
+
+print $out_fh <<'___CUT___';
+
package App::VRR::Fakedisplay;
use strict;
@@ -14,7 +80,6 @@ sub new {
my ( $class, %opt ) = @_;
my $self = {
- font_file => dist_file( 'App-VRR-Fakedisplay', 'font.png' ),
width => $opt{width} || 140,
height => $opt{height} || 40,
scale => 10,
@@ -22,7 +87,6 @@ sub new {
offset_y => 0,
};
- $self->{font} = GD::Image->new($self->{font_file});
$self->{image} = GD::Image->new($self->{width} * $self->{scale}, $self->{height} * $self->{scale});
$self->{color}->{bg} = $self->{image}->colorAllocate(0, 0, 0);
@@ -31,40 +95,21 @@ sub new {
$self->{image}->filledRectangle(0, 0, ($self->{width} * $self->{scale}) -1,
($self->{height} * $self->{scale}) - 1, $self->{color}->{bg});
- $self->{font_idx} = $self->{font}->colorClosest(0, 0, 0);
+ $self->{font} = {
- return bless( $self, $class );
-}
+___CUT___
-sub locate_char {
- my ($self, $char) = @_;
- my ($x, $y, $w, $h) = (0, 30, 6, 10);
-
- given ($char) {
- when (/[a-z]/) { $y = 10; $x = (ord($char) - 97) * 10 }
- when (/[A-Z]/) { $y = 0; $x = (ord($char) - 65) * 10 }
- when (/[0-9]/) { $y = 20; $x = (ord($char) - 48) * 10 }
-
- when (q{ä}) { $y = 40; $x = 0 }
- when (q{ö}) { $y = 40; $x = 10 }
- when (q{ü}) { $y = 40; $x = 20 }
-
- when (q{ }) { $y = 90; $x = 0 }
- when (q{:}) { $y = 30; $x = 0 }
- when (q{-}) { $y = 30; $x = 10 }
- when (q{.}) { $y = 30; $x = 20 }
- when (q{,}) { $y = 30; $x = 30 }
- when (q{/}) { $y = 30; $x = 40 }
- }
+parse_char_row( 0, 'A' .. 'Z');
+parse_char_row(10, 'a' .. 'z');
+parse_char_row(20, '0' .. '9');
+parse_char_row(30, q{:}, q{-}, q{.}, q{,}, q{/}, q{ });
+parse_char_row(40, qw(ä ö ü));
- given ($char) {
- when (/[WwMm]/) { $w = 8 }
- when (/[BEFkrt ]/) { $w = 5 }
- when (/[il1:]/) { $w = 4 }
- when (/[.,]/) { $w = 3 }
- }
+print $out_fh <<'___CUT___';
+
+ };
- return ($x, $y, $w, $h);
+ return bless( $self, $class );
}
sub draw_at {
@@ -87,12 +132,18 @@ sub draw_at {
}
for my $char (split(qr{}, $text)) {
- my ($x, $y, $w, $h) = $self->locate_char($char);
- for my $pos_x ( $x .. ($x + $w) ) {
- for my $pos_y ( $y .. ($y + $h)) {
- if ($font->getPixel($pos_x, $pos_y) == $font_idx) {
+
+ if (not exists $self->{font}->{$char}) {
+ next;
+ }
+
+ my $w = $self->{font}->{$char}->{width};
+
+ for my $y ( 0 .. 10 ) {
+ for my $x ( 0 .. $w ) {
+ if ($self->{font}->{$char}->{matrix}->[$y]->[$x]) {
$im->filledEllipse(
- ($off_x + $pos_x - $x) * $scale, ($off_y + $pos_y - $y) * $scale,
+ ($off_x + $x) * $scale, ($off_y + $y) * $scale,
$scale, $scale,
$c_fg
);
@@ -129,43 +180,8 @@ sub write_image_to {
return;
}
-
1;
-__END__
-
-=head1 NAME
-
-=head1 SYNOPSIS
-
-=head1 VERSION
-
-version
-
-=head1 DESCRIPTION
-
-=head1 METHODS
-
-=over
-
-=back
-
-=head1 DIAGNOSTICS
-
-=head1 DEPENDENCIES
-
-=over
-
-=back
-
-=head1 BUGS AND LIMITATIONS
-
-=head1 SEE ALSO
-
-=head1 AUTHOR
-
-Copyright (C) 2011 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>
-
-=head1 LICENSE
+___CUT___
- 0. You just DO WHAT THE FUCK YOU WANT TO.
+close($out_fh);