summaryrefslogtreecommitdiff
path: root/dot/local/bin/text2png
blob: ada778023f52d7a59b03a1c5aaa34100f795375e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/perl
use warnings;
use File::Temp qw(tempfile);
use List::Util qw(max min);

$font = 'FreeMono';

# Approximate dimensions used to determine approximate size. Only the
# value that produces the higher resolution (for the given input) is used.
$desired_width = 1296;
$desired_height = 1296;

# Font size in points (72 points per inch). This does not determine the size of
# the image, because the image density is defined in terms of it.
$font_size = 72;
$margin = $font_size;

$width_chars = 0;
$height_lines = 0;

###

($tempFH, $tempFN) = tempfile();
for (<STDIN>) {
    $width_chars = max((length $_), $width_chars);
    ++$height_lines;
    print $tempFH $_;
}

$PPI = 72; # points per inch
sub choose_density()
{
    my $width_density = $PPI * $desired_width / $width_chars / $font_size;
    my $height_density = $PPI * $desired_height / $height_lines / $font_size;
    return max($width_density, $height_density);
}
$density = choose_density;
$character_size = $font_size * $density / $PPI;
$initial_image_size = sprintf "%ix%i",
    1.25 * $character_size * $width_chars,
    1.25 * $character_size * $height_lines;
$offset = sprintf '+%i+%i', ($character_size) x 2;

$output = 'png:-';

system <<END;
set -x;
convert \\
        -size $initial_image_size \\
        xc:white \\
        -font "$font" \\
        -density $density \\
        -pointsize $font_size \\
        -annotate $offset \\
        "\@$tempFN" \\
        -trim \\
        -bordercolor '#FFFFFF' \\
        -border $margin \\
        +repage \\
        $output
END