| 1 | package Acme::SaBalTongMun; | ||
| 2 | our $VERSION = '1.100830'; | ||
| 3 | use Moose; | ||
| 4 | # ENCODING: utf-8 | ||
| 5 | # ABSTRACT: make a round robin (사발통문, 沙鉢通文) | ||
| 6 | |||
| 7 | use namespace::autoclean; | ||
| 8 | |||
| 9 | |||
| 10 | use common::sense; | ||
| 11 | use GD; | ||
| 12 | use List::Util qw( max ); | ||
| 13 | |||
| 14 | |||
| 15 | has 'radius' => ( isa => 'Int', is => 'rw', required => 1 ); | ||
| 16 | |||
| 17 | |||
| 18 | has 'font' => ( isa => 'Str', is => 'rw', required => 1 ); | ||
| 19 | |||
| 20 | |||
| 21 | has 'font_size' => ( isa => 'Int', is => 'rw', required => 1 ); | ||
| 22 | |||
| 23 | |||
| 24 | has 'font_charset' => ( isa => 'Str', is => 'rw', default => 'Unicode' ); | ||
| 25 | |||
| 26 | |||
| 27 | has 'color' => ( isa => 'Str', is => 'rw', required => 1 ); | ||
| 28 | |||
| 29 | |||
| 30 | has 'people' => ( isa => 'ArrayRef[Str]', is => 'rw', required => 1 ); | ||
| 31 | |||
| 32 | |||
| 33 | sub generate { | ||
| 34 | my $self = shift; | ||
| 35 | |||
| 36 | my $max_string_width | ||
| 37 | = max( map length, @{$self->people} ) * $self->font_size; | ||
| 38 | |||
| 39 | my $cx = $self->radius + $max_string_width; | ||
| 40 | my $cy = $self->radius + $max_string_width; | ||
| 41 | my $width = ( $self->radius + $max_string_width ) * 2; | ||
| 42 | my $height = ( $self->radius + $max_string_width ) * 2; | ||
| 43 | |||
| 44 | my $angle = ( 2 * 3.14 ) / @{$self->people}; | ||
| 45 | |||
| 46 | # | ||
| 47 | # Make GD::Image | ||
| 48 | # | ||
| 49 | my $image = GD::Image->new($width, $height); | ||
| 50 | |||
| 51 | my $white = $image->colorAllocate( _get_rgb("#FFFFFF") ); | ||
| 52 | my $black = $image->colorAllocate( _get_rgb("#000000") ); | ||
| 53 | |||
| 54 | # make the background transparent and interlaced | ||
| 55 | $image->transparent( _get_rgb("#FFFFFF") ); | ||
| 56 | $image->interlaced('true'); | ||
| 57 | |||
| 58 | my $dest_angle = 0; | ||
| 59 | for my $string ( @{$self->people} ) { | ||
| 60 | my $dest_x = $cx + ( $self->radius * cos $dest_angle ); | ||
| 61 | my $dest_y = $cy + ( $self->radius * sin $dest_angle ); | ||
| 62 | |||
| 63 | my $string_angle = (2 * 3.14) - $dest_angle; | ||
| 64 | |||
| 65 | $image->stringFT( | ||
| 66 | $image->colorAllocate( _get_rgb( $self->color ) ), # fgcolor | ||
| 67 | $self->font, # .ttf path | ||
| 68 | $self->font_size, # point size | ||
| 69 | $string_angle, # rotation angle | ||
| 70 | $dest_x, # X coordinates | ||
| 71 | $dest_y, # Y coordinates | ||
| 72 | $string, | ||
| 73 | { | ||
| 74 | charmap => $self->font_charset, | ||
| 75 | }, | ||
| 76 | ); | ||
| 77 | |||
| 78 | $dest_angle += $angle; | ||
| 79 | } | ||
| 80 | |||
| 81 | return $image; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _get_rgb { map hex, $_[0] =~ m/^#(..)(..)(..)$/ } | ||
| 85 | |||
| 86 | __PACKAGE__->meta->make_immutable; | ||
| 87 | no Moose; | ||
| 88 | 1; | ||
| 89 | |||
| 90 | __END__ |