diff options
-rwxr-xr-x | dot/local/bin/text2png | 61 |
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 | ||
2 | use warnings; | ||
3 | use File::Temp qw(tempfile); | ||
4 | use 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(); | ||
24 | for (<STDIN>) { | ||
25 | $width_chars = max((length $_), $width_chars); | ||
26 | ++$height_lines; | ||
27 | print $tempFH $_; | ||
28 | } | ||
29 | |||
30 | $PPI = 72; # points per inch | ||
31 | sub 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 | |||
46 | system <<END; | ||
47 | set -x; | ||
48 | convert \\ | ||
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 | ||
61 | END | ||