| 1 |
#!/usr/bin/env bash |
| 2 |
set -euo pipefail |
| 3 |
export LANG=C.UTF-8 |
| 4 |
export LC_ALL=C.UTF-8 |
| 5 |
|
| 6 |
if [[ $# -gt 1 ]]; then |
| 7 |
echo "usage: $0 [output.png]" >&2 |
| 8 |
exit 2 |
| 9 |
fi |
| 10 |
|
| 11 |
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 12 |
repo_dir="$(cd "$script_dir/.." && pwd)" |
| 13 |
output="${1:-$repo_dir/site/static/readme-terminal.png}" |
| 14 |
renderer="$script_dir/render-readme-banner.sh" |
| 15 |
font="${CLNKR_README_IMAGE_FONT:-$repo_dir/site/static/fonts/TerminessNerdFontMono-Regular.ttf}" |
| 16 |
|
| 17 |
if ! command -v gum >/dev/null 2>&1; then |
| 18 |
echo "render-readme-banner-png: gum is required" >&2 |
| 19 |
exit 1 |
| 20 |
fi |
| 21 |
|
| 22 |
if command -v magick >/dev/null 2>&1; then |
| 23 |
imagemagick=(magick) |
| 24 |
elif command -v convert >/dev/null 2>&1; then |
| 25 |
imagemagick=(convert) |
| 26 |
else |
| 27 |
echo "render-readme-banner-png: ImageMagick is required" >&2 |
| 28 |
exit 1 |
| 29 |
fi |
| 30 |
|
| 31 |
if [[ ! -f "$font" ]]; then |
| 32 |
echo "render-readme-banner-png: font not found: $font" >&2 |
| 33 |
exit 1 |
| 34 |
fi |
| 35 |
|
| 36 |
esc=$'\e' |
| 37 |
default_fg="#ff9e2c" |
| 38 |
default_bg="#050301" |
| 39 |
fg="$default_fg" |
| 40 |
bg="$default_bg" |
| 41 |
inverse=0 |
| 42 |
scale="${CLNKR_README_IMAGE_SCALE:-2}" |
| 43 |
font_size_base="${CLNKR_README_IMAGE_FONT_SIZE:-18}" |
| 44 |
cell_width_base="${CLNKR_README_IMAGE_CELL_WIDTH:-11}" |
| 45 |
cell_height_base="${CLNKR_README_IMAGE_CELL_HEIGHT:-21}" |
| 46 |
pad_x_base="${CLNKR_README_IMAGE_PAD_X:-24}" |
| 47 |
pad_y_base="${CLNKR_README_IMAGE_PAD_Y:-24}" |
| 48 |
font_size=$((font_size_base * scale)) |
| 49 |
cell_width=$((cell_width_base * scale)) |
| 50 |
cell_height=$((cell_height_base * scale)) |
| 51 |
pad_x=$((pad_x_base * scale)) |
| 52 |
pad_y=$((pad_y_base * scale)) |
| 53 |
|
| 54 |
tmp_dir="$(mktemp -d)" |
| 55 |
trap 'rm -rf "$tmp_dir"' EXIT |
| 56 |
ansi="$tmp_dir/render.ansi" |
| 57 |
mvg="$tmp_dir/render.mvg" |
| 58 |
background_mvg="$tmp_dir/background.mvg" |
| 59 |
text_mvg="$tmp_dir/text.mvg" |
| 60 |
|
| 61 |
"$renderer" >"$ansi" |
| 62 |
|
| 63 |
mvg_escape() { |
| 64 |
local value="$1" |
| 65 |
value="${value//\\/\\\\}" |
| 66 |
value="${value//\'/\\\'}" |
| 67 |
printf '%s' "$value" |
| 68 |
} |
| 69 |
|
| 70 |
hex_color() { |
| 71 |
printf '#%02x%02x%02x' "$1" "$2" "$3" |
| 72 |
} |
| 73 |
|
| 74 |
effective_fg() { |
| 75 |
if (( inverse )); then |
| 76 |
printf '%s' "$bg" |
| 77 |
else |
| 78 |
printf '%s' "$fg" |
| 79 |
fi |
| 80 |
} |
| 81 |
|
| 82 |
effective_bg() { |
| 83 |
if (( inverse )); then |
| 84 |
printf '%s' "$fg" |
| 85 |
else |
| 86 |
printf '%s' "$bg" |
| 87 |
fi |
| 88 |
} |
| 89 |
|
| 90 |
mvg_quote() { |
| 91 |
local value="$1" |
| 92 |
value="${value//\\/\\\\}" |
| 93 |
value="${value//\'/\\\'}" |
| 94 |
printf "'%s'" "$value" |
| 95 |
} |
| 96 |
|
| 97 |
near_black() { |
| 98 |
local color="$1" |
| 99 |
local hex r g b |
| 100 |
|
| 101 |
hex="${color#\#}" |
| 102 |
if [[ "$hex" =~ ^[0-9a-fA-F]{6}$ ]]; then |
| 103 |
r=$((16#${hex:0:2})) |
| 104 |
g=$((16#${hex:2:2})) |
| 105 |
b=$((16#${hex:4:2})) |
| 106 |
(( r <= 6 && g <= 6 && b <= 6 )) |
| 107 |
return |
| 108 |
fi |
| 109 |
|
| 110 |
return 1 |
| 111 |
} |
| 112 |
|
| 113 |
normalize_dark_bg() { |
| 114 |
local color="$1" |
| 115 |
|
| 116 |
if near_black "$color"; then |
| 117 |
printf '%s' "$default_bg" |
| 118 |
return |
| 119 |
fi |
| 120 |
|
| 121 |
printf '%s' "$color" |
| 122 |
} |
| 123 |
|
| 124 |
reset_sgr() { |
| 125 |
fg="$default_fg" |
| 126 |
bg="$default_bg" |
| 127 |
inverse=0 |
| 128 |
} |
| 129 |
|
| 130 |
apply_sgr() { |
| 131 |
local params="$1" |
| 132 |
local -a codes |
| 133 |
local i code mode r g b |
| 134 |
|
| 135 |
[[ -n "$params" ]] || params=0 |
| 136 |
IFS=';' read -r -a codes <<<"$params" |
| 137 |
|
| 138 |
i=0 |
| 139 |
while (( i < ${#codes[@]} )); do |
| 140 |
code="${codes[$i]}" |
| 141 |
[[ -n "$code" ]] || code=0 |
| 142 |
case "$code" in |
| 143 |
0) |
| 144 |
reset_sgr |
| 145 |
;; |
| 146 |
7) |
| 147 |
inverse=1 |
| 148 |
;; |
| 149 |
27) |
| 150 |
inverse=0 |
| 151 |
;; |
| 152 |
38|48) |
| 153 |
mode="${codes[$((i + 1))]:-}" |
| 154 |
if [[ "$mode" == "2" ]]; then |
| 155 |
r="${codes[$((i + 2))]:-0}" |
| 156 |
g="${codes[$((i + 3))]:-0}" |
| 157 |
b="${codes[$((i + 4))]:-0}" |
| 158 |
if [[ "$code" == "38" ]]; then |
| 159 |
fg="$(hex_color "$r" "$g" "$b")" |
| 160 |
else |
| 161 |
bg="$(hex_color "$r" "$g" "$b")" |
| 162 |
fi |
| 163 |
i=$((i + 4)) |
| 164 |
fi |
| 165 |
;; |
| 166 |
esac |
| 167 |
i=$((i + 1)) |
| 168 |
done |
| 169 |
} |
| 170 |
|
| 171 |
draw_cell() { |
| 172 |
local row="$1" |
| 173 |
local col="$2" |
| 174 |
local ch="$3" |
| 175 |
local x y x2 y2 text_y cell_bg cell_fg escaped |
| 176 |
|
| 177 |
x=$((pad_x + col * cell_width)) |
| 178 |
y=$((pad_y + row * cell_height)) |
| 179 |
x2=$((x + cell_width)) |
| 180 |
y2=$((y + cell_height)) |
| 181 |
text_y=$((y + font_size)) |
| 182 |
cell_bg="$(effective_bg)" |
| 183 |
cell_fg="$(effective_fg)" |
| 184 |
cell_bg="$(normalize_dark_bg "$cell_bg")" |
| 185 |
if [[ "$ch" != " " ]] && near_black "$cell_fg" && [[ "$cell_bg" == "$default_bg" ]]; then |
| 186 |
ch=" " |
| 187 |
fi |
| 188 |
printf 'fill %s\nrectangle %d,%d %d,%d\n' \ |
| 189 |
"$(mvg_quote "$cell_bg")" "$x" "$y" "$x2" "$y2" >>"$background_mvg" |
| 190 |
if [[ "$ch" != " " ]]; then |
| 191 |
escaped="$(mvg_escape "$ch")" |
| 192 |
printf 'fill %s\ntext %d,%d %s\n' \ |
| 193 |
"$(mvg_quote "$cell_fg")" "$x" "$text_y" "$(mvg_quote "$escaped")" >>"$text_mvg" |
| 194 |
fi |
| 195 |
} |
| 196 |
|
| 197 |
parse_line() { |
| 198 |
local line="$1" |
| 199 |
local row="$2" |
| 200 |
local col=0 |
| 201 |
local seq params final ch |
| 202 |
|
| 203 |
reset_sgr |
| 204 |
while [[ -n "$line" ]]; do |
| 205 |
if [[ "$line" =~ ^${esc}\[([0-9\;\?]*)([[:alpha:]]) ]]; then |
| 206 |
seq="${BASH_REMATCH[0]}" |
| 207 |
params="${BASH_REMATCH[1]}" |
| 208 |
final="${BASH_REMATCH[2]}" |
| 209 |
line="${line:${#seq}}" |
| 210 |
if [[ "$final" == "m" ]]; then |
| 211 |
apply_sgr "$params" |
| 212 |
fi |
| 213 |
continue |
| 214 |
fi |
| 215 |
|
| 216 |
ch="${line:0:1}" |
| 217 |
line="${line:1}" |
| 218 |
draw_cell "$row" "$col" "$ch" |
| 219 |
col=$((col + 1)) |
| 220 |
done |
| 221 |
} |
| 222 |
|
| 223 |
line_count="$(wc -l <"$ansi" | tr -d ' ')" |
| 224 |
width=$((pad_x * 2 + 61 * cell_width)) |
| 225 |
height=$((pad_y * 2 + line_count * cell_height)) |
| 226 |
|
| 227 |
{ |
| 228 |
printf 'viewbox 0 0 %d %d\n' "$width" "$height" |
| 229 |
printf 'fill %s\nrectangle 0,0 %d,%d\n' "$(mvg_quote "$default_bg")" "$width" "$height" |
| 230 |
printf 'font %s\nfont-size %d\n' "$(mvg_quote "$font")" "$font_size" |
| 231 |
} >"$mvg" |
| 232 |
: >"$background_mvg" |
| 233 |
: >"$text_mvg" |
| 234 |
|
| 235 |
row=0 |
| 236 |
while IFS= read -r line || [[ -n "$line" ]]; do |
| 237 |
parse_line "$line" "$row" |
| 238 |
row=$((row + 1)) |
| 239 |
done <"$ansi" |
| 240 |
|
| 241 |
cat "$background_mvg" "$text_mvg" >>"$mvg" |
| 242 |
|
| 243 |
mkdir -p "$(dirname "$output")" |
| 244 |
"${imagemagick[@]}" "mvg:$mvg" "$output" |
| 245 |
printf 'rendered %s\n' "$output" |