| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | App::Basis::ConvertText2::Plugin::Gle | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | my $content = "size 12 8 | ||
| 9 | |||
| 10 | set font texcmr | ||
| 11 | begin graph | ||
| 12 | title "Fruit Production" | ||
| 13 | xtitle "Weekday" | ||
| 14 | ytitle "Production [x100 pound]" | ||
| 15 | xticks off | ||
| 16 | data "test.dat" | ||
| 17 | bar d1,d2 fill navy,skyblue | ||
| 18 | end graph" ; | ||
| 19 | my $params = {} ; | ||
| 20 | my $obj = App::Basis::ConvertText2::Plugin::Gle->new() ; | ||
| 21 | my $out = $obj->process( 'gle', $content, $params) ; | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | convert a gle/glx text string into a PNG, requires gle program http://glx.sourceforge.net/ | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | # ---------------------------------------------------------------------------- | ||
| 30 | |||
| 31 | package App::Basis::ConvertText2::Plugin::Gle; | ||
| 32 | $App::Basis::ConvertText2::Plugin::Gle::VERSION = '0.4'; | ||
| 33 | use 5.10.0; | ||
| 34 | use strict; | ||
| 35 | use warnings; | ||
| 36 | use Path::Tiny; | ||
| 37 | use Moo; | ||
| 38 | use Image::Resize ; | ||
| 39 | use App::Basis; | ||
| 40 | use App::Basis::ConvertText2::Support; | ||
| 41 | use namespace::autoclean; | ||
| 42 | |||
| 43 | has handles => ( | ||
| 44 | is => 'ro', | ||
| 45 | init_arg => undef, | ||
| 46 | default => sub { [qw{gle glx}] } | ||
| 47 | ); | ||
| 48 | |||
| 49 | use constant GLE => "gle"; | ||
| 50 | use constant RES => 28 ; | ||
| 51 | |||
| 52 | # ---------------------------------------------------------------------------- | ||
| 53 | |||
| 54 | =item gle | ||
| 55 | |||
| 56 | create a simple gle image | ||
| 57 | |||
| 58 | parameters | ||
| 59 | data - gle text | ||
| 60 | filename - filename to save the created image as | ||
| 61 | |||
| 62 | hashref params of | ||
| 63 | title - title to use for image alt attribute | ||
| 64 | size - size of image, widthxheight - optional, default 720x512 | ||
| 65 | transparent - flag to set background transparent - optional | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub process { | ||
| 70 | my $self = shift; | ||
| 71 | my ( $tag, $content, $params, $cachedir ) = @_; | ||
| 72 | $params->{size} ||= "720x512"; | ||
| 73 | my ( $x, $y ) = ( $params->{size} =~ /^\s*(\d+)\s*x\s*(\d+)\s*$/ ); | ||
| 74 | |||
| 75 | # strip any ending linefeed | ||
| 76 | chomp $content; | ||
| 77 | return "" if ( !$content ); | ||
| 78 | |||
| 79 | # we can use the cache or process everything ourselves | ||
| 80 | my $sig = create_sig( $content, $params ); | ||
| 81 | my $filename = cachefile( $cachedir, "$sig.png" ); | ||
| 82 | if ( !-f $filename ) { | ||
| 83 | my $glefile = Path::Tiny->tempfile("gleXXXXXXXX"); | ||
| 84 | |||
| 85 | my $res = sprintf( "size %.4f %.4f", $x / RES, $y / RES) ; | ||
| 86 | $content =~ s/size\s+(.*?)\s+(.*?)$//gsm ; | ||
| 87 | $content = "$res\n$content" ; | ||
| 88 | |||
| 89 | path($glefile)->spew_utf8($content); | ||
| 90 | |||
| 91 | my $cmd = GLE . ( $params->{transparent} ? " -transparent" : "" ) . " -output $filename $glefile"; | ||
| 92 | my ( $exit, $stdout, $stderr ) = run_cmd($cmd); | ||
| 93 | if ($exit) { | ||
| 94 | warn "Could not run script " . GLE . " get it from http://glx.sourceforge.net/"; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | my $out; | ||
| 98 | if ( -f $filename ) { | ||
| 99 | |||
| 100 | # create something suitable for the HTML | ||
| 101 | $out = create_img_src( $filename, $params->{title} ); | ||
| 102 | } | ||
| 103 | |||
| 104 | return $out; | ||
| 105 | } | ||
| 106 | |||
| 107 | # ---------------------------------------------------------------------------- | ||
| 108 | |||
| 109 | 1; |