2012-06-27 21:33:44 +02:00
|
|
|
package MyWrapper;
|
|
|
|
use GD;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
2012-07-25 19:43:19 +02:00
|
|
|
use v5.14;
|
2012-06-27 21:33:44 +02:00
|
|
|
use Moo;
|
|
|
|
use MooX::Options;
|
|
|
|
|
|
|
|
has couleurs => ( is => 'ro', builder => '_build_colors' );
|
|
|
|
|
2012-07-25 02:22:47 +02:00
|
|
|
option distance_entre_mots => ( is => 'rw',
|
|
|
|
doc => 'distance en pixels de chaque mot du message',
|
|
|
|
short => 'dem',
|
|
|
|
format => 'i', default => sub { 5 }
|
|
|
|
);
|
2012-07-25 19:43:19 +02:00
|
|
|
option extends => ( is => 'rw',
|
|
|
|
doc => 'si la taille du message est importante, on peut agrandir le cadre à volonté',
|
|
|
|
short => 'ext'
|
|
|
|
);
|
|
|
|
option progress_bar => ( is => 'rw',
|
|
|
|
doc => 'si vous souhaitez utiliser le script pour gérer une barre de progression, il faut indiquer un pourcentage',
|
|
|
|
format => 'i', short => 'pb'
|
|
|
|
);
|
|
|
|
option debug => ( is => 'rw',
|
|
|
|
doc => 'si vous souhaitez utiliser le script pour gérer une barre de progression',
|
|
|
|
short => 'd'
|
|
|
|
);
|
|
|
|
option couleurs_dispo => ( is => 'rw',
|
|
|
|
doc => 'affiche les couleurs disponibles',
|
|
|
|
short => 'cd'
|
|
|
|
);
|
2012-07-25 02:22:47 +02:00
|
|
|
option output => ( is => 'rw',
|
|
|
|
doc => 'fichier généré : fichier image (ex: image.png)',
|
|
|
|
short => 'o',
|
|
|
|
format => 's', default => sub { 'image.png' }
|
|
|
|
);
|
2012-07-25 01:01:40 +02:00
|
|
|
option message => ( is => 'rw',
|
|
|
|
doc => 'message',
|
|
|
|
short => 'm',
|
|
|
|
format => 's', default => sub { 'message à afficher' }
|
|
|
|
);
|
2012-06-27 21:33:44 +02:00
|
|
|
option couleur_externe => ( is => 'rw',
|
2012-07-25 01:01:40 +02:00
|
|
|
doc => 'couleur externe',
|
|
|
|
short => 'ce',
|
|
|
|
format => 's', default => sub { 'bleu' }
|
|
|
|
);
|
2012-07-25 02:22:47 +02:00
|
|
|
option couleur_contour => ( is => 'rw',
|
|
|
|
doc => 'couleur contour',
|
|
|
|
short => 'cc',
|
|
|
|
format => 's', default => sub { 'noir' }
|
|
|
|
);
|
2012-06-27 21:33:44 +02:00
|
|
|
option couleur_interne => ( is => 'rw',
|
2012-07-25 01:01:40 +02:00
|
|
|
doc => 'couleur interne',
|
|
|
|
short => 'ci',
|
2012-07-25 19:43:19 +02:00
|
|
|
format => 's', default => sub { 'rouge' }
|
2012-07-25 01:01:40 +02:00
|
|
|
);
|
2012-06-27 21:33:44 +02:00
|
|
|
option couleur_background => ( is => 'rw' ,
|
2012-07-25 01:01:40 +02:00
|
|
|
doc => 'couleur du fond (écriture)',
|
|
|
|
short => 'cb',
|
|
|
|
format => 's', default => sub { 'noir' }
|
|
|
|
);
|
|
|
|
option taille_x => ( is => 'rw',
|
|
|
|
doc => 'taille X', format => 'i',
|
|
|
|
short => 'x',
|
|
|
|
default => sub { 150 }
|
|
|
|
);
|
|
|
|
option taille_y => ( is => 'rw',
|
|
|
|
doc => 'taille Y', format => 'i',
|
|
|
|
short => 'y',
|
2012-07-25 19:43:19 +02:00
|
|
|
default => sub { 30 }
|
2012-07-25 01:01:40 +02:00
|
|
|
);
|
2012-06-27 21:33:44 +02:00
|
|
|
sub _build_colors {
|
|
|
|
{
|
|
|
|
bleu => [0, 0, 255],
|
2012-07-27 02:49:04 +02:00
|
|
|
bleuf => [0, 0, 100],
|
2012-06-27 21:33:44 +02:00
|
|
|
rouge => [255, 0, 0],
|
2012-07-27 02:49:04 +02:00
|
|
|
rougef => [100, 0, 0],
|
2012-06-27 21:33:44 +02:00
|
|
|
vert => [0, 255, 0],
|
2012-07-25 20:06:44 +02:00
|
|
|
vertf => [0, 100, 0],
|
2012-07-27 02:49:04 +02:00
|
|
|
gris => [150, 150, 150],
|
|
|
|
grisf => [100, 100, 100],
|
|
|
|
|
2012-06-27 21:33:44 +02:00
|
|
|
noir => [0, 0, 0],
|
2012-07-27 02:49:04 +02:00
|
|
|
blanc => [255, 255, 255]
|
2012-06-27 21:33:44 +02:00
|
|
|
};
|
|
|
|
}
|
2012-07-25 01:01:40 +02:00
|
|
|
sub afficher_couleurs {
|
2012-06-27 21:33:44 +02:00
|
|
|
my ($self) = @_;
|
|
|
|
my %couleurs = %{$self->couleurs};
|
|
|
|
say "couleurs : " . join( ', ' , keys %couleurs) ;
|
|
|
|
while( my ($key , $value) = each %couleurs)
|
|
|
|
{
|
|
|
|
say "La couleur $key est : " . join(', ' , @$value);
|
|
|
|
}
|
2012-07-25 01:01:40 +02:00
|
|
|
}
|
|
|
|
sub afficher_valeurs_actuelles {
|
|
|
|
my ($self) = @_;
|
|
|
|
say "Message : " . $self->message;
|
2012-07-25 19:43:19 +02:00
|
|
|
say "Peut être étendu" if $self->extends;
|
|
|
|
say "Output file : " . $self->output;
|
2012-07-25 02:22:47 +02:00
|
|
|
say "Distance entre mots : " . $self->distance_entre_mots;
|
2012-07-25 01:01:40 +02:00
|
|
|
say "couleur_interne : " . $self->couleur_interne;
|
2012-07-27 02:49:04 +02:00
|
|
|
say "couleur_contour : " . $self->couleur_contour;
|
2012-07-25 01:01:40 +02:00
|
|
|
say "couleur_externe : " . $self->couleur_externe;
|
|
|
|
say "couleur_background : " . $self->couleur_background;
|
|
|
|
say "taille_x : " . $self->taille_x;
|
|
|
|
say "taille_y : " . $self->taille_y;
|
2012-07-25 19:43:19 +02:00
|
|
|
say "taille_x calculée: " . $self->calcul_taille_x;
|
2012-07-25 01:01:40 +02:00
|
|
|
}
|
2012-07-25 19:43:19 +02:00
|
|
|
sub calcul_taille_x {
|
2012-07-25 01:01:40 +02:00
|
|
|
my ($self) = @_;
|
2012-07-25 19:43:19 +02:00
|
|
|
my $taille_x = $self->taille_x;
|
|
|
|
my @mots = split / /, $self->message;
|
|
|
|
if( $self->extends ) {
|
2012-07-27 02:49:04 +02:00
|
|
|
$taille_x += 3 * length $_ for( @mots ) ;
|
2012-07-25 19:43:19 +02:00
|
|
|
}
|
|
|
|
return $taille_x;
|
2012-06-27 21:33:44 +02:00
|
|
|
}
|
2012-07-25 19:43:19 +02:00
|
|
|
sub calcul_taille_y {
|
|
|
|
my ($self) = @_;
|
|
|
|
return $self->taille_y;
|
|
|
|
}
|
|
|
|
sub enregistrement_image {
|
|
|
|
my ($self, $im) = @_;
|
|
|
|
# We record the image file
|
|
|
|
open (DISPLAY,"> " . $self->output ) || die;
|
|
|
|
binmode DISPLAY;
|
|
|
|
print DISPLAY $im->png;
|
|
|
|
close DISPLAY;
|
|
|
|
}
|
2012-07-25 20:06:44 +02:00
|
|
|
sub allocation_des_couleurs {
|
|
|
|
my ($self, $im) = @_;
|
2012-07-25 02:22:47 +02:00
|
|
|
|
2012-07-25 20:06:44 +02:00
|
|
|
my %couleurs = %{$self->couleurs};
|
|
|
|
my %couleurs_allouees;
|
2012-07-25 02:22:47 +02:00
|
|
|
# allocate some colors
|
|
|
|
while( my ($key , $value) = each %couleurs)
|
|
|
|
{
|
|
|
|
#say "La couleur $key est : " . join(', ' , @$value);
|
2012-07-25 20:06:44 +02:00
|
|
|
$couleurs_allouees{$key} = $im->colorAllocate($$value[0], $$value[1], $$value[2]);
|
2012-07-25 02:22:47 +02:00
|
|
|
}
|
2012-07-25 20:06:44 +02:00
|
|
|
return %couleurs_allouees ;
|
|
|
|
}
|
|
|
|
sub options_sortie_image {
|
|
|
|
my ($self, $im) = @_;
|
2012-07-25 02:22:47 +02:00
|
|
|
# make the background transparent and interlaced
|
2012-07-27 02:49:04 +02:00
|
|
|
#$im->transparent($couleurs_allouees{'blanc'});
|
2012-07-25 02:22:47 +02:00
|
|
|
$im->interlaced('true');
|
2012-07-25 20:06:44 +02:00
|
|
|
}
|
2012-07-27 02:49:04 +02:00
|
|
|
sub do_progress_bar{
|
|
|
|
my ($self, $im, $couleurs_allouees, $x, $y) = @_;
|
|
|
|
my $taille_progress_bar = $self->progress_bar * ($x - 1) / 100;
|
|
|
|
my %couleurs_allouees = %$couleurs_allouees;
|
|
|
|
$im->filledRectangle(2, 2, $taille_progress_bar, $y -3, $couleurs_allouees{$self->couleur_interne});
|
|
|
|
}
|
|
|
|
sub do_antipixel_button {
|
|
|
|
my ($self, $im, $couleurs_allouees, $mots, $x, $y) = @_;
|
|
|
|
my %couleurs_allouees = %$couleurs_allouees;
|
|
|
|
my @mots = @$mots;
|
|
|
|
my $current_position = 10;
|
|
|
|
for my $i (@mots) {
|
|
|
|
$im->filledRectangle($current_position - 3, 2, $current_position + length($i) * 6 + 7, $y - 3, $couleurs_allouees{$self->couleur_interne});
|
|
|
|
$im->string(gdMediumBoldFont, $current_position , $y - 0.75 * $y, $i, $couleurs_allouees{'blanc'});
|
|
|
|
$current_position += length($i) * 6 + 15;
|
|
|
|
}
|
|
|
|
}
|
2012-07-25 20:06:44 +02:00
|
|
|
sub do_img {
|
|
|
|
my ($self) = @_;
|
|
|
|
my @mots = split / /, $self->message;
|
|
|
|
my $x = $self->calcul_taille_x;
|
|
|
|
my $y = $self->calcul_taille_y;
|
2012-07-25 02:22:47 +02:00
|
|
|
|
2012-07-25 20:06:44 +02:00
|
|
|
# create a new image
|
|
|
|
my $im = new GD::Image($x,$y);
|
|
|
|
# allocation des couleurs
|
|
|
|
my %couleurs_allouees = $self->allocation_des_couleurs( $im );
|
|
|
|
# options de sortie de l'image (entrelacée etc.)
|
|
|
|
$self->options_sortie_image ( $im );
|
|
|
|
# début de la construction de l'image à proprement dite
|
|
|
|
$im->rectangle(0, 0, $x -1, $y -1, $couleurs_allouees{$self->couleur_contour});
|
|
|
|
$im->filledRectangle(1, 1, $x -2, $y -2, $couleurs_allouees{$self->couleur_background});
|
2012-07-27 02:49:04 +02:00
|
|
|
|
|
|
|
$self->do_progress_bar($im,\%couleurs_allouees,$x, $y) if defined $self->progress_bar;
|
|
|
|
$self->do_antipixel_button($im, \%couleurs_allouees, \@mots, $x, $y) unless defined $self->progress_bar;
|
2012-07-25 02:22:47 +02:00
|
|
|
|
2012-07-25 19:43:19 +02:00
|
|
|
$self->enregistrement_image( $im );
|
|
|
|
}
|
|
|
|
sub mode_debug {
|
|
|
|
my ($self) = @_;
|
|
|
|
say "ENTER DEBUG MODE";
|
|
|
|
$self->afficher_valeurs_actuelles unless $self->couleurs_dispo;
|
|
|
|
$self->afficher_couleurs if $self->couleurs_dispo;
|
|
|
|
}
|
|
|
|
sub run {
|
|
|
|
my ($self) = @_;
|
|
|
|
$self->mode_debug if $self->debug;
|
|
|
|
$self->do_img unless $self->debug;
|
2012-06-27 21:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|