#!/bin/bash
# Burn tests: each SRT x font, three font set-ups. Run in ~/rt/subtitle-boxes.
# A = normal system (fontconfig fallback on), font named via fontsdir + force_style
# B = fontconfig with no system fonts (no fallback): what a bare container or player sees
# C = the fix: fontsdir with a font that has every letter, named in force_style
set -u
cd "$(dirname "$0")"
mkdir -p out frames logs empty-fc
cat > empty-fc/fonts.conf <<'X'
<?xml version="1.0"?><!DOCTYPE fontconfig SYSTEM "fonts.dtd"><fontconfig><cachedir>/tmp/sb-fc-cache</cachedir></fontconfig>
X
run() { # setup lang fontfile family
  local setup=$1 lang=$2 file=$3 fam=$4 tag="$1-$2-${3%%-*}"
  mkdir -p "fd/$tag"; rm -f fd/$tag/*; cp "fonts/$file" "fd/$tag/"
  local env=()
  [ "$setup" = B ] && env=(FONTCONFIG_FILE="$PWD/empty-fc/fonts.conf")
  env "${env[@]}" ffmpeg -hide_banner -loglevel verbose -y -i video.mp4 \
    -vf "subtitles=srt/$lang.srt:fontsdir=fd/$tag:force_style='FontName=$fam,FontSize=28,Outline=2'" \
    -an -c:v libx264 -crf 20 -preset veryfast "out/$tag.mp4" > "logs/$tag.log" 2>&1
  echo "$tag exit=$? fallback_lines=$(grep -c 'fontselect:' logs/$tag.log) noglyph=$(grep -c 'failed to find any fallback' logs/$tag.log)"
  for t in 6 14; do ffmpeg -hide_banner -loglevel error -y -ss $t -i "out/$tag.mp4" -frames:v 1 "frames/$tag-$t.png"; done
}
for L in en vi pl tr cs ru vi-nfd; do
  run A $L BebasNeue-Regular.ttf "Bebas Neue"
  run B $L BebasNeue-Regular.ttf "Bebas Neue"
  run A $L PermanentMarker-Regular.ttf "Permanent Marker"
  run B $L PermanentMarker-Regular.ttf "Permanent Marker"
  run C $L Inter-Bold.ttf "Inter"
  [ "$L" = ru ] && { run A ru BeVietnamPro-Bold.ttf "Be Vietnam Pro"; run B ru BeVietnamPro-Bold.ttf "Be Vietnam Pro"; }
  [ "$L" = tr ] && run B tr TitanOne-Regular.ttf "Titan One"
done
