11 my %sizes; # meme base image sizes
12 my %acros; # meme base acronyms (BLB => bad-luck-brian.jpg)
14 # Are we running as CGI or from the command-line?
15 # TODO better detection
16 my $is_cgi = $ENV{'SCRIPT_FILENAME'};
18 my $script_path = $is_cgi ? dirname($ENV{'SCRIPT_FILENAME'}) : dirname($0);
20 my $sz_fname = $script_path . '/meme-sizes.lst';
22 open FILE, $sz_fname or die $!;
24 while (my $line = <FILE>) {
27 my ($width, $height, $fname) = split(/ /, $line, 3);
28 $sizes{$fname} = [$width, $height];
30 # Find a potential short form (acronym for multiword, no extension otherwise)
33 # remove article for the purpose of the shrotening; we don't care if it's
34 # in the middle of a word because we only care about initials anyway
35 # FIXME this actually fails in the case of XXXthe-XXX, let's care about that
36 # when we actually come across it
40 $acro = join('', map { uc(substr($_, 0, 1)) } split(/-/, $the ));
42 $acro = (split(/\./, $the))[0]
44 if (!defined $acros{$acro}) {
45 $acros{$acro} = $fname;
47 print STDERR "Trying to redefined acronym $acro from $acros{$acro} to ${fname}\n";
51 my %revacros = reverse %acros;
56 # params: img, width, height, text
58 my $svg_template=<<SVG;
59 <?xml version='1.0' encoding='UTF-8'?>
61 xmlns='http://www.w3.org/2000/svg'
62 xmlns:xlink='http://www.w3.org/1999/xlink' version='1.1'
63 viewBox='0 0 %2\$d %3\$d'>
64 <style type="text/css">text{font-family:'Impact';fill:white;stroke:black;stroke-width:2px;text-anchor:middle}</style>
65 <image xlink:href='%1\$s' x='0' y='0'
66 width='%2\$d' height='%3\$d'/>
71 return sprintf($svg_template, @_);
74 # params: y-pos, font-size, text
76 my $txt_template=<<TXT;
77 <text x='50%%' y='%1\$d%%' font-size='%2\$d'
82 return sprintf($txt_template, @_);
85 # routine to actually prepare the SVG.
86 # params: img, sep, text
92 unless (defined $img and defined $sizes{$img}) {
93 return undef unless defined $img and defined $acros{$img};
97 my ($width, $height) = @{$sizes{$img}};
102 foreach (split /\Q$sep\E/) {
107 $divisions = @lines if @lines > $divisions;
109 my $fontsize = int($height/$divisions + 0.5);
110 if ($fontsize > $width/10) {
111 $fontsize = int($width/10 + 0.5) if $fontsize > $width/10;
112 $divisions = int($height/$fontsize + 0.5);
115 my $offset = int(100/$divisions + 0.5);
116 my $fillers = grep { $_ eq '' } @lines;
117 my $real_lines = @lines - $fillers;
118 my $filler_size = $fillers ? int((98 - $offset*$real_lines)/$fillers) : 0;
128 $txt .= fill_txt($dy, $fontsize, $_);
131 return fill_svg($img, $width, $height, $txt);
134 my ($img, $sep, @t); # image, line separator, text lines
136 while (my $q = new CGI::Fast) {
139 $img = $q->param('m');
140 $sep = $q->param('s');
143 ($img, $sep, @t) = @ARGV;
146 $img ||= (keys %sizes)[0];
148 @t = ('TOP TEST//BOTTOM TEST') unless @t;
150 my $svg = make_svg($img, $sep, @t);
154 -type => 'image/svg+xml',
166 print $q->header(-status=>404),
167 $q->start_html("Unknown meme base"),
168 $q->h1("Unknown meme base!");
169 print "<p>Sorry, <tt>'" . encode_entities($img) . "'</tt> is not a known meme base. ".
170 "You want one of the following instead:</p><ul>";
171 my %revacros = reverse %acros;
172 foreach (keys %sizes) {
173 print "<li><tt>" . encode_entities($_) . "</tt>";
174 print " (<tt>" . encode_entities($revacros{$_}) . "</tt>)" if defined $revacros{$_};
178 # foreach (keys %ENV) {
179 # print "<p>$_=$ENV{$_}</p>"
181 print $q->end_html();
184 foreach (keys %sizes) {
186 print STDERR " ($revacros{$_})" if defined $revacros{$_};