summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2017-03-09 13:36:22 -0500
committerAndrew Cady <d@jerkface.net>2017-03-09 13:36:22 -0500
commite147ab5b9d0b7df6a689057b9398b0f486cc43b1 (patch)
tree7ae85acf15548bb8a23ab6aaa3f6cdbe8a64f25d
parente047a12209ffd00cd0427e2cd10a03172eb43582 (diff)
new command: text2png
-rwxr-xr-xdot/local/bin/text2png61
1 files changed, 61 insertions, 0 deletions
diff --git a/dot/local/bin/text2png b/dot/local/bin/text2png
new file mode 100755
index 0000000..ada7780
--- /dev/null
+++ b/dot/local/bin/text2png
@@ -0,0 +1,61 @@
1#!/usr/bin/perl
2use warnings;
3use File::Temp qw(tempfile);
4use List::Util qw(max min);
5
6$font = 'FreeMono';
7
8# Approximate dimensions used to determine approximate size. Only the
9# value that produces the higher resolution (for the given input) is used.
10$desired_width = 1296;
11$desired_height = 1296;
12
13# Font size in points (72 points per inch). This does not determine the size of
14# the image, because the image density is defined in terms of it.
15$font_size = 72;
16$margin = $font_size;
17
18$width_chars = 0;
19$height_lines = 0;
20
21###
22
23($tempFH, $tempFN) = tempfile();
24for (<STDIN>) {
25 $width_chars = max((length $_), $width_chars);
26 ++$height_lines;
27 print $tempFH $_;
28}
29
30$PPI = 72; # points per inch
31sub choose_density()
32{
33 my $width_density = $PPI * $desired_width / $width_chars / $font_size;
34 my $height_density = $PPI * $desired_height / $height_lines / $font_size;
35 return max($width_density, $height_density);
36}
37$density = choose_density;
38$character_size = $font_size * $density / $PPI;
39$initial_image_size = sprintf "%ix%i",
40 1.25 * $character_size * $width_chars,
41 1.25 * $character_size * $height_lines;
42$offset = sprintf '+%i+%i', ($character_size) x 2;
43
44$output = 'png:-';
45
46system <<END;
47set -x;
48convert \\
49 -size $initial_image_size \\
50 xc:white \\
51 -font "$font" \\
52 -density $density \\
53 -pointsize $font_size \\
54 -annotate $offset \\
55 "\@$tempFN" \\
56 -trim \\
57 -bordercolor '#FFFFFF' \\
58 -border $margin \\
59 +repage \\
60 $output
61END