Use filt
[llpp.git] / llppac
blobf879d7f72cd08f7c5dae131d948c63f623a4a977
1 #!/bin/sh
3 set -e
5 CACHEDIR=$HOME/.cache/llpp
7 die() {
8 echo "$1" >&2
9 exit 1
12 executable_p() {
13 command -v "$1" >/dev/null 2>&1
16 trap 'test -n "$casp" && rm -f "$casp"' 0
18 test -d "$CACHEDIR" || die "cache directory '$CACHEDIR' does not exist"
19 while getopts m:t:f opt; do
20 case $opt in
21 m) mime=$OPTARG;;
22 t) type=$OPTARG;;
23 f) force=1;;
24 ?) die "usage: $0 [-m mime/type] [-t filter] [-f] path";;
25 esac
26 done
27 shift $(($OPTIND - 1))
28 test -z "$1" && die "usage $0: path"
30 ft=$(file -L --mime-type -b "$1") || die "$ft"
32 case $ft in
33 application/x-gzip | application/x-compress) dc=zcat;;
34 application/x-xz) dc=xzcat;;
35 application/x-bzip2) dc=bzcat;;
36 *) unset dc || true;;
37 esac
39 filt='"${dc-cat}" "$1" |'
41 if test -z "$type"; then
42 test -z "$dc" || ft=$(file -L --mime-type -bz "$1") || die "$ft"
44 mime=${mime:-$ft}
45 case $mime in
46 application/postscript) type=ps;;
47 application/pdf) type=pdf;;
48 image/vnd.djvu) type=djvu;;
49 text/html) type=html;;
50 application/msword) type=word;;
51 application/vnd.openxmlformats-officedocument.* \
52 | application/vnd.ms-powerpoint \
53 | application/vnd.ms-excel \
54 | application/vnd.oasis.opendocument.*) type=uno;;
55 image/svg+xml) type=svg;;
56 image/png | image/jpeg) test -n "$dc" && type="image" || type="image2";;
57 image/*) type=image;;
58 application/x-dvi) type=dvi;;
59 application/x-font-ttf | application/vnd.ms-opentype) type=font;;
60 *) die "unhandled file type: '$mime'";;
61 esac
64 opts=
66 case $type in
67 ps) conv='ps2pdf - "$casp"';;
68 image2|pdf) test -z "$dc" && exec llpp "$@" || conv='cat >"$casp"';;
69 djvu) conv='djvups - | ps2pdf - "$casp"';;
70 html) {
71 test -n "$dc" && die "can not convert compressed '$mime'"
72 unset filt
73 conv='prince "$1" -o "$casp"'
74 };;
75 word) {
76 if executable_p unoconv && test -z "$dc"; then
77 unset filt
78 conv='unoconv -o "$casp" "$1"'
79 else
80 conv='antiword -m 8859-1.txt -a a4 - >"$casp"'
82 };;
83 uno) {
84 test -n "$dc" && die "can not convert compressed '$mime'"
85 unset filt
86 conv='unoconv -o "$casp" "$1"'
87 };;
88 svg) {
89 executable_p rsvg-convert \
90 && conv='rsvg-convert -f pdf -o "$casp"' \
91 || conv='inkscape -z -A "$casp" -'
92 };;
93 font) {
94 test -z "$2" && die "path to the file with text required"
95 # https://mail.gnome.org/archives/gtk-i18n-list/2010-June/msg00000.html
96 mkdir -p $CACHEDIR/fonts
97 eval $filt cat >$CACHEDIR/fonts/font
98 cd $CACHEDIR/fonts
99 cat >fonts.conf <<EOF
100 <?xml version="1.0"?>
101 <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
102 <fontconfig>
103 <dir>.</dir>
104 <cachedir>.</cachedir>
105 <config></config>
106 </fontconfig>
108 FONTCONFIG_PATH=. pango-view -q "$2" -o font.pdf
109 exec llpp $opts -origin "$@" font.pdf
111 image) conv='convert - pdf:"$casp"';;
112 dvi) {
113 test -n "$dc" && die "can not convert compressed '$mime'"
114 unset filt
115 conv='dvipdf "$1" "$casp"'
117 *) die "unhandled filter type: '$type'";;
118 esac
120 hash=$(md5sum "$1") || die "$hash"
121 casp=$CACHEDIR/${hash%% *}
122 (test -n "$force" -o ! -e "$casp") && eval "$filt" "$conv"
124 exec llpp $opts -origin "$@" "$casp"