#!/usr/bin/env perl

use strict;
use warnings;
use v5.18;
use utf8;
use open qw(:std :utf8);

use Imager;

my $font = Imager::Font->new(
    file => '/usr/local/share/fonts/adobe-source-code-pro/SourceCodePro-Regular.ttf',
    size => 23,
    aa => 0,
    color => "white",
);

my $fontbold = Imager::Font->new(
    file => '/usr/local/share/fonts/adobe-source-code-pro/SourceCodePro-Bold.ttf',
    size => 23,
    aa => 0,
    color => "white",
);

my @letters = (qw(a b c d e f g h i j k l m n o p q r s t u v w x y z
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    1 2 3 4 5 6 7 8 9 0),
    "(", ")", "@", "{", "}", "[", "]", "♫", "♪", "•", "♣", " ", "!", "?", ",", ":", ";",
    "'", "—", "|", "/", "\\", "%", "┘", "┐", "┤", "│", "─", "►", "×", ".", "-", "#", '"',
    ">", "+", "=", "“", "”",
    "¤", "√", "Φ", "§", "●", "~", "*", "∞", "☻", "…", "∩", "♥", "&", "☼", "π","Π", "←", "↓", "→", "↑", "Δ", "«", "»",
    "Ξ", "Θ", "≈", "◊", "^", "○", "‗",
    "┌", "└",
);

my %names = (
    "(" => "lparen",
    ")" => "rparen",
    "@" => "player",
    "{" => "lbrace",
    "}" => "rbrace",
    "[" => "lbracket",
    "]" => "rbracket",
    "♪" => "music1",
    "♫" => "music2",
    "•" => "tick",
    "♣" => "simella",
    " " => "space",
    "!" => "exclamation",
    "?" => "interrogation",
    "," => "comma",
    ":" => "colon",
    ";" => "semicolon",
    "'" => "quote",
    "—" => "longhyphen",
    "-" => "hyphen",
    "|" => "pipe",
    "/" => "slash",
    "\\" => "backslash",
    "%" => "percent",
    "┐" => "boxne",
    "┤" => "boxe",
    "│" => "vbar",
    "┘" => "boxse",
    "┌" => "boxnw",
    "└" => "boxsw",
    "─" => "hbar",
    "►" => "arrow",
    "×" => "times",
    "." => "dot",
    "#" => "hash",
    '"' => "quotes",
    '>' => "gt",
    '=' => "equal",
    '“' => "lquotes",
    '”' => "rquotes",
    '+' => "plus",

    "¤" => "frontier",
    "√" => "hit",
    "Φ" => "magic",
    "§" => "fog",
    "●" => "rock",
    "~" => "tilde",
    "*" => "asterisc",
    "∞" => "kill",
    "☻" => "dreaming",
    "…" => "dots",
    "∩" => "stone",
    "♥" => "heart",
    "&" => "barrel",
    "☼" => "light",
    "π" => "table",
    "Π" => "holedwall",
    "←" => "larrow",
    "↓" => "darrow",
    "→" => "rarrow",
    "↑" => "uarrow",
    "Δ" => "portal",
    "«" => "ldiag",
    "»" => "rdiag",
    "Ξ" => "barrier",
    "Θ" => "window",
    "≈" => "water",
    "◊" => "chasm",
    "^" => "rubble",
    "○" => "nolight",
    "‗" => "queenrock",
);

my $cmd = shift @ARGV // "new";

if ($cmd ne "new" and $cmd ne "all") {
    die "$0: unknown command argument: $cmd";
}

for my $l (@letters) {
    my $name = $names{$l} // $l;
    if ($cmd eq "new" and -f "letter-$name.png") {
        next;
    }
    my $img = Imager->new(xsize => 16, ysize => 24);
    $img->box(filled => 1, color => 'black');
    if ($l eq "#") {
        $img->string(font => $fontbold, text => $l, x => 1, y => 18);
    } else {
        $img->string(font => $font, text => $l, x => 1, y => 18);
    }
    $img->write(file => "letter-$name.png")
        or die "Cannot save letter-$name", $img->errstr;
}

