From 641c8ab365dd3afad8e7082f14f92fd69879a334 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Sun, 7 Jan 2024 14:28:30 +0100 Subject: [PATCH] Rewrote scripts to use inkscape and imagemagick to generate the cards Pipeline: for each file: svg -inkscape-> png the png's are then combined using imagemagick into a new png png -imagemagick-> png --- gendeck.sh | 21 +++++++------ genpreview.sh | 16 ++++++++++ parts/plain/assemble.sh | 37 ++++++++++------------ parts/special/assemble.sh | 65 ++++++++++++++++++++++----------------- 4 files changed, 80 insertions(+), 59 deletions(-) create mode 100755 genpreview.sh diff --git a/gendeck.sh b/gendeck.sh index 49f36bd..3278e0a 100755 --- a/gendeck.sh +++ b/gendeck.sh @@ -1,5 +1,5 @@ #!/bin/sh -# requirements: xq, find +# requirements: find, inkscape, imagemagick function usage { echo "gendeck.sh " @@ -32,8 +32,6 @@ if [ ! "$card1" = "" ] && [ ! "$card1" = "" ] && [ ! "$card1" = "" ] && [ ! "$ca echo "he is trying really hard" echo "grab a cup of tea - this might take a while" - echo ... - rm -rf dist # create build folder @@ -49,13 +47,16 @@ if [ ! "$card1" = "" ] && [ ! "$card1" = "" ] && [ ! "$card1" = "" ] && [ ! "$ca cut -d'/' -f3 | \ cut -d'.' -f1 | \ while read face; do - sh ./parts/plain/assemble.sh $card1 $face > dist/plain/$card1/$face.svg - sh ./parts/plain/assemble.sh $card2 $face > dist/plain/$card2/$face.svg - sh ./parts/plain/assemble.sh $card3 $face > dist/plain/$card3/$face.svg - sh ./parts/plain/assemble.sh $card4 $face > dist/plain/$card4/$face.svg + sh ./parts/plain/assemble.sh $card1 $face dist/plain/$card1/$face.png + echo -n . + sh ./parts/plain/assemble.sh $card2 $face dist/plain/$card2/$face.png + echo -n . + sh ./parts/plain/assemble.sh $card3 $face dist/plain/$card3/$face.png + echo -n . + sh ./parts/plain/assemble.sh $card4 $face dist/plain/$card4/$face.png done - echo ... + echo echo "*sweats*" echo "blahaj is really sorry - but you gotta wait a little longer" @@ -65,11 +66,11 @@ if [ ! "$card1" = "" ] && [ ! "$card1" = "" ] && [ ! "$card1" = "" ] && [ ! "$ca cut -d'/' -f3 | \ cut -d'.' -f1 | \ while read face; do - sh ./parts/special/assemble.sh $face $card1 $card2 $card3 $card4 > dist/special/$face.svg + sh ./parts/special/assemble.sh $face $card1 $card2 $card3 $card4 dist/special/$face.png + echo -n . done echo ... - echo "blahaj finished your deck" echo "you can find the output files in the dist folder" echo "(blahaj sorted them - just for you)" diff --git a/genpreview.sh b/genpreview.sh new file mode 100755 index 0000000..40ae621 --- /dev/null +++ b/genpreview.sh @@ -0,0 +1,16 @@ +#!/bin/sh +# requires: imagemagick + +out="$1" + +function usage { + echo "$0 " + echo "generates a montage png showing off all generated cards in the dist folder" + exit 1 +} + +if [ "$out" = "" ]; then + usage +fi + +montage -geometry +20+20 $(find dist -iname '*.png') $out diff --git a/parts/plain/assemble.sh b/parts/plain/assemble.sh index c2f2dd8..5cb25c6 100755 --- a/parts/plain/assemble.sh +++ b/parts/plain/assemble.sh @@ -1,30 +1,27 @@ #!/bin/sh -# requirements: awk,xq +# requirements: inkscape, imagemagick flag="$1" card="$2" +to="$3" # get svg file content scriptdir=$(dirname $0) -base="$(cat $scriptdir/../flag-base/$flag.svg)" -overlay="$(cat $scriptdir/$card.svg)" +base="$scriptdir/../flag-base/$flag.svg" +overlay="$scriptdir/$card.svg" -# extract the svg dimensions from the xml using xq -base_box=$(echo "$base" | xq -a viewBox -q svg) -overlay_box=$(echo "$overlay" | xq -a viewBox -q svg) +tmpdir="$(mktemp -d)" -# calculate the overlay offset by subtracting -# the overlay witdth from the base width -# and the overlay height from the base height -off_x="$(echo $base_box $overlay_box | awk '{ print ($3 - $7)/2 }')" -off_y="$(echo $base_box $overlay_box | awk '{ print ($4 - $8)/2 }')" +# convert base card to png +basetmp="$tmpdir/base.png" +inkscape \ + --export-filename=$basetmp \ + --export-type=png $base -# get the inner svg from the overlay -ov="$(echo "$overlay" | xq -q 'svg' -n)" -# get the svg content from the base -bg="$(echo "$base" | xq -q 'svg' -n)" -# generate offset glu code to be inserted in the base -glu="$ov" +# convert overlay to png +ovtmp="$tmpdir/ov.png" +inkscape \ + --export-filename=$ovtmp \ + --export-type=png $overlay -echo "$bg" | sed 's/<\/svg>//' -echo "$glu" -echo "" +magick composite -gravity center png:$ovtmp png:$basetmp $to +rm -rf $tmpdir diff --git a/parts/special/assemble.sh b/parts/special/assemble.sh index 012c929..741b6a7 100755 --- a/parts/special/assemble.sh +++ b/parts/special/assemble.sh @@ -1,43 +1,50 @@ #!/bin/sh -# requirements: awk,xq +# requirements: inkscape, imagemagick card="$1" flag1="$2" flag2="$3" flag3="$4" flag4="$5" +to="$6" # get svg file content scriptdir=$(dirname $0) -base="$(cat $scriptdir/$card.svg)" +base="$scriptdir/$card.svg" +ov1="$scriptdir/../flags/$flag1.svg" +ov2="$scriptdir/../flags/$flag2.svg" +ov3="$scriptdir/../flags/$flag3.svg" +ov4="$scriptdir/../flags/$flag4.svg" -# extract the svg dimensions from the xml using xq -base_box=$(echo "$base" | xq -a viewBox -q svg) +tmpdir="$(mktemp -d)" -function gen_ov { - flag="$1" - opx="$2" - opy="$3" +# convert base card to png +basetmp="$tmpdir/base.png" +inkscape \ + --export-filename=$basetmp \ + --export-type=png $base - ov="$(cat $scriptdir/../flags/$flag.svg)" - box="$(echo "$ov" | xq -a viewBox -q svg)" - off_x="$(echo $base_box $box $opx | awk '{ print ($3/2 + $7*$9/2 - $7/2 + $7/16) }')" - off_y="$(echo $base_box $box $opy | awk '{ print ($4/2 + $8*$9/2 - $7/2 + $7/8) }')" - inner="$(echo "$ov" | xq -q 'svg' -n)" - echo "" - echo "$inner" - echo "" -} +# convert overlays to png +tl="$tmpdir/tl.png" +tr="$tmpdir/tr.png" +bl="$tmpdir/bl.png" +br="$tmpdir/br.png" +inkscape \ + --export-filename=$tl \ + --export-type=png $ov1 +inkscape \ + --export-filename=$tr \ + --export-type=png $ov2 +inkscape \ + --export-filename=$bl \ + --export-type=png $ov3 +inkscape \ + --export-filename=$br \ + --export-type=png $ov4 -bg="$(echo "$base" | xq -q 'svg>g' -n)" -c1="$(gen_ov $flag1 '-1' '-1')" -c2="$(gen_ov $flag2 '1' '-1')" -c3="$(gen_ov $flag3 '-1' '1')" -c4="$(gen_ov $flag4 '1' '1')" +mnt="$tmpdir/montage.png" +montage -background none -geometry +0+0 png:$tl png:$tr png:$bl png:$br png:$mnt -# print svg to stdout -echo "$bg" | sed 's/<\/svg>//' -echo "$c1" -echo "$c2" -echo "$c3" -echo "$c4" -echo "" +magick composite \ + -gravity center png:$mnt png:$basetmp $to + +rm -rf $tmpdir -- 2.38.5