diff --git a/lib/Exd/Printer.pm b/lib/Exd/Printer.pm index 70e9f96..0c4188a 100644 --- a/lib/Exd/Printer.pm +++ b/lib/Exd/Printer.pm @@ -15,30 +15,32 @@ use Cairo; use Exd::Utils::String; has device => ( - is => 'ro', + is => 'ro', required => 1, ); has font => ( - is => 'rw', + is => 'rw', default => sub { return 'Noto Sans CJK JP'; }, ); +has current_image => ( is => 'rw', ); + sub qr( $self, $url ) { my $tmpdir = Path::Tiny->tempdir; my $image_file = $tmpdir->child('image.png'); my $qrcode = Imager::QRCode->new( size => 8 ); my $img = $qrcode->plot( $url, {} ); $img->write( file => '' . $image_file ) or die $img->errstr; - $self->image( $image_file ); + $self->image($image_file); $self->print_text( $url, 20 ); } sub image( $self, $image ) { - $image = ''.$image if ref $image && $image->isa('Path::Tiny'); - if (!ref $image) { + $image = '' . $image if ref $image && $image->isa('Path::Tiny'); + if ( !ref $image ) { $image = Exd::Utils::get_gd_image($image); } my $width = $image->width; @@ -68,46 +70,69 @@ sub image( $self, $image ) { my ($alpha) = $image_final->colorAllocateAlpha( 0, 0, 0, 127 ); $image_final->colorDeallocate($alpha); $image_final->colorAllocate( 255, 255, 255 ); - my $tempdir = Path::Tiny->tempdir; - my $image_file_in = $tempdir->child('in.png'); - $image_final->_file(''.$image_file_in); - my $image_file_out = $tempdir->child('out.png'); - die 'Cannot convert' if system (qw/convert/, $image_file_in, qw/-resize 384x -dither FloydSteinberg -remap pattern:gray50 -monochrome/, $image_file_out); - $image_final = Exd::Utils::get_gd_image($image_file_out.''); - my $device = $self->device; - $device->image($image_final); + + if ( !defined $self->current_image ) { + $self->current_image($image_final); + } + else { + my $current_image = $self->current_image; + my $image = $image_final; + my $new_current = GD::Image->new( $current_image->width, + $current_image->height + $image->height ); + $new_current->copy( $current_image, 0, 0, 0, 0, + $current_image->width, $current_image->height ); + $new_current->copy( $image, 0, $current_image->height, 0, 0, + $image->width, $image->height ); + $self->current_image($new_current); + + } } } sub print_text( $self, $text, $font_size ) { - my ($none, $file_code, $line) = caller(0); - die "The bare minimum for font size is 4 at $file_code line $line" if $font_size < 4; - my $font = Pango::FontDescription->from_string( + my ( $none, $file_code, $line ) = caller(0); + die "The bare minimum for font size is 4 at $file_code line $line" + if $font_size < 4; + my $font = Pango::FontDescription->from_string( $self->font . ' ' . ( $font_size - ( 2 * 50 / 30 ) ) ); my $device = $self->device; - my $img = Exd::Utils::String->print_text($text, $font, $font_size); + my $img = Exd::Utils::String->print_text( $text, $font, $font_size ); $self->image($img); } -sub _font_to_weight ($self, $font) { +sub _font_to_weight ( $self, $font ) { my $weight = $font->get_weight; - if ($weight eq 'normal') { + if ( $weight eq 'normal' ) { return 4; } - if ($weight eq 'bold') { + if ( $weight eq 'bold' ) { return 7; } } sub print($self) { - $self->device->print; + my $device = $self->device; + my $image_final = $self->current_image; + my $tempdir = Path::Tiny->tempdir; + my $image_file_in = $tempdir->child('in.png'); + $image_final->_file( '' . $image_file_in ); + my $image_file_out = $tempdir->child('out.png'); + die 'Cannot convert' + if system( + qw/convert/, + $image_file_in, + qw/-resize 384x -dither FloydSteinberg -remap pattern:gray50 -monochrome/, + $image_file_out + ); + $image_final = Exd::Utils::get_gd_image( $image_file_out . '' ); + $device->image($image_final); + $device->print; } -sub print_n_lf($self, $n) { - $self->print_text("\n" x $n, 30); +sub print_n_lf( $self, $n ) { + $self->print_text( "\n" x $n, 30 ); } - sub serialize($self) { my $hash = {%$self}; $hash->{device} = $hash->{device}->serialize;