Files
draw_diag/render_diagram.sh
T
wassname bf8b38346d init
2026-01-29 20:19:59 +08:00

102 lines
2.9 KiB
Bash
Executable File

#!/bin/bash
# Diagram rendering script - supports TikZ, Typst/CeTZ, and SVG
set -e
USAGE="Usage: $0 <input_file> [output.png]
Supported formats:
.tex - TikZ/LaTeX diagrams (requires pdflatex + imagemagick/poppler)
.typ - Typst/CeTZ diagrams (requires typst)
.svg - SVG diagrams (requires inkscape or imagemagick)
Examples:
$0 diagram.tex # Creates diagram.png
$0 diagram.typ output.png # Creates output.png
"
if [ $# -lt 1 ]; then
echo "$USAGE"
exit 1
fi
INPUT="$1"
EXT="${INPUT##*.}"
BASENAME="${INPUT%.*}"
# Determine output file
if [ $# -ge 2 ]; then
OUTPUT="$2"
else
OUTPUT="${BASENAME}.png"
fi
echo "Rendering: $INPUT -> $OUTPUT"
case "$EXT" in
tex)
# TikZ/LaTeX workflow
if ! command -v pdflatex &> /dev/null; then
echo "Error: pdflatex not found. Install with:"
echo " sudo apt-get install texlive texlive-pictures"
exit 1
fi
# Compile to PDF
pdflatex -interaction=nonstopmode -output-directory="$(dirname "$INPUT")" "$INPUT" > /dev/null
PDF="${BASENAME}.pdf"
# Convert PDF to PNG
if command -v pdftoppm &> /dev/null; then
# Use poppler (cleaner, better quality)
pdftoppm "$PDF" "${BASENAME}" -png -singlefile -r 300
mv "${BASENAME}.png" "$OUTPUT"
elif command -v convert &> /dev/null; then
# Use ImageMagick
convert -density 300 "$PDF" -quality 90 "$OUTPUT"
else
echo "Error: Need pdftoppm (poppler-utils) or convert (imagemagick)"
echo "Install with: sudo apt-get install poppler-utils"
exit 1
fi
# Cleanup LaTeX aux files
rm -f "${BASENAME}.aux" "${BASENAME}.log" "${BASENAME}.pdf"
echo "Created: $OUTPUT"
;;
typ)
# Typst workflow
if ! command -v typst &> /dev/null; then
echo "Error: typst not found. Install with:"
echo " curl -fsSL https://typst.community/typst-install/install.sh | sh"
echo "Or: cargo install --git https://github.com/typst/typst"
exit 1
fi
# Compile to PNG directly
typst compile "$INPUT" "$OUTPUT" --ppi 300
echo "Created: $OUTPUT"
;;
svg)
# SVG workflow
if command -v inkscape &> /dev/null; then
inkscape "$INPUT" --export-type=png --export-filename="$OUTPUT" --export-dpi=300
elif command -v convert &> /dev/null; then
convert -density 300 -background white -alpha remove "$INPUT" "$OUTPUT"
else
echo "Error: Need inkscape or imagemagick"
echo "Install with: sudo apt-get install inkscape"
exit 1
fi
echo "Created: $OUTPUT"
;;
*)
echo "Error: Unsupported file extension: .$EXT"
echo "$USAGE"
exit 1
;;
esac