#!/bin/bash us=svg-prep-dxf usage () { cat <&2 "svg-dxf-prep: $*"; exit 1; } badusage () { usage >&2; exit 1; } flatness=0.01 # ^ this number seems to work reasonably well # although maybe it should be adjusted? while true; do case "$1" in --flatness=*) flatness="${1#*=}" ;; -*) badusage ;; *) break ;; esac shift done case "$#" in 2) ;; *) badusage ;; esac source=$1 dest=$2 case "$source" in *.svg) ;; *) badusage ;; esac case "$dest" in *.dxf) ;; *) badusage ;; esac tmpdir="$(dirname "$dest")/.$(basename "$dest").tmpdir" rm -rf -- "$tmpdir" mkdir -- "$tmpdir" case "$tmpdir" in /*) abstmpdir="$tmpdir" ;; *) abstmpdir="$PWD/$tmpdir" ;; esac cp -- "$source" "$tmpdir/t.svg" # startx seems to send "client" stdout/stderr somewhere hopeless exec 4>&2 baseextdir=$(inkscape -x) ourid=uk.org.greenend.chiark.ijackson.swirly.flatten # inkscape doesn't provide a way to specify a per-invocation # extension directory, but it does look in $HOME. # Anyway, we're going to want a fresh HOME for startx. ourxd="$tmpdir"/.config/inkscape/extensions mkdir -p "$ourxd" mkdir -p "$tmpdir/.local/share" # inkscape complains otherwise, wtf # Putting the absolute path in the XML doesn't seem to work: # inkscape complains that this "dependency" (implicit, apparently) # is not satisfied. # I'm not sure why this is, but this does work: ln -s -- "$baseextdir/flatten.py" "$ourxd" xmlstarlet \ ed \ -N ie=http://www.inkscape.org/namespace/inkscape/extension \ -u "/ie:inkscape-extension/ie:_name" -v "Our Flatten Beziers" \ -u "/ie:inkscape-extension/ie:id" -v $ourid \ -d '/ie:inkscape-extension/ie:dependency' \ -u '/ie:inkscape-extension/ie:param[@name="flatness"]' -v $flatness \ "$baseextdir"/flatten.inx \ >"$ourxd"/our-flatten.inx # known bugs with this approach: # - we rely on the .inx filename # - we rely on flatten.py being in the extensions/ directory # and called exactly that # - we drop the dependencies rather than editing them cat <<'END' >"$tmpdir/run-inkscape" #!/bin/sh exec >&4 2>&4 echo printf "running inkscape to transform and clean up..." cd "$HOME" inkscape \ --with-gui \ t.svg \ --verb=ToolNode \ --verb=EditSelectAll \ --verb=SelectionUnGroup \ --verb=EditSelectAll \ --verb=StrokeToPath \ --verb=ToolNode \ --verb=EditSelectAll \ --verb=SelectionUnion \ --verb=EditSelectAll \ --verb=uk.org.greenend.chiark.ijackson.swirly.flatten.noprefs \ --verb=FileSave \ --verb=FileQuit \ echo done. echo END chmod +x "$tmpdir"/run-inkscape cat <