• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3#
4# Copyright (c) 2015-2018 Samuel Thibault <samuel.thibault@ens-lyon.org>
5#
6# Permission is hereby granted, free of charge, to any person obtaining a copy
7# of this software and associated documentation files (the "Software"), to deal
8# in the Software without restriction, including without limitation the rights
9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10# copies of the Software, and to permit persons to whom the Software is
11# furnished to do so, subject to the following conditions:
12#
13#
14# The above copyright notice and this permission notice shall be included in
15# all copies or substantial portions of the Software.
16#
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24# THE SOFTWARE.
25#
26
27# Make sure we have enough options
28if [ $# != 5 -a $# != 6 ]; then
29  echo "ERROR: $0 jobid user name nb options [filename]" >&2
30  exit 1
31fi
32
33NB=$4
34OPTIONS=$5
35FILE=$6
36
37. @CUPS_DATADIR@/braille/cups-braille.sh
38
39####################
40# Liblouis options #
41####################
42LIBLOUIS_CONFIG=""
43
44echo "DEBUG: Input content type: $CONTENT_TYPE" >&2
45
46# FIXME CONTENT_TYPE contains original document, not document passed as parameter ?!!
47
48setupTextRendering() {
49  # Default rendering without translation: just reformat paragraphs
50  RENDER_CALL="fmt -$TEXTWIDTH"
51
52  #  Tool to be used for the conversion
53  case $CONTENT_TYPE in
54    text/plain)
55      CONVERT=""
56      ;;
57    text/html)
58      CONVERT=""
59      RENDER_CALL="lynx -width=$TEXTWIDTH -dump -stdin"
60      checkTool lynx lynx "translating html files"
61      ;;
62    application/msword)
63      CONVERT="antiword -"
64      checkTool antiword antiword "translating MS-Word doc files"
65      ;;
66    application/vnd.openxmlformats-officedocument.wordprocessingml.document)
67      CONVERT="docx2txt"
68      checkTool docx2txt docx2txt "translating MS-Word docx files"
69      ;;
70    text/rtf|application/rtf)
71      CONVERT="rtf2txt /dev/stdin"
72      checkTool rtf2txt rtf2txt "translating RTF files"
73      ;;
74    application/pdf|application/vnd.cups-pdf-banner)
75      CONVERT="pdftotext -raw - -"
76      checkTool pdftotext poppler "translating PDF files"
77      ;;
78    *)
79      echo "ERROR: unsupported content type $CONTENT_TYPE" >&2
80      exit 1
81      ;;
82  esac
83}
84
85# sometimes we can't filter directly from stdin or the original file because the
86# tools need to seek within the file (e.g. unzip), or spaces in the path pose
87# problem. This can be called in such case to dump the original content to a
88# fresh file
89dumptofile() {
90  ORIGFILE="$FILE"
91  FILE=$(mktemp "${TMPDIR:-/tmp}/texttobrf.tmp.XXXXXX")
92  trap -- 'rm -f "$FILE"' EXIT
93  if [ -n "$ORIGFILE" ]
94  then
95    cat "$ORIGFILE" > "$FILE"
96  else
97    cat > "$FILE"
98  fi
99}
100
101#  Selected braille table
102if [ -n "$LIBLOUIS_TABLES" ]
103then
104  if type file2brl > /dev/null
105  then
106    # Good, we can use liblouisutdml
107    case $CONTENT_TYPE in
108      text/plain)
109	LIBLOUIS_TOOL="file2brl"
110	CONVERT=""
111	;;
112      text/html)
113	LIBLOUIS_TOOL="file2brl -t"
114	CONVERT=""
115	;;
116      text/xml|application/xml|application/xhtml+xml|application/sgml)
117	LIBLOUIS_TOOL="file2brl"
118	CONVERT=""
119	;;
120      application/msword)
121	LIBLOUIS_TOOL="file2brl"
122	CONVERT="antiword -x db -"
123	checkTool antiword antiword "translating MS-Word doc files"
124	;;
125      application/vnd.oasis.opendocument*)
126	LIBLOUIS_TOOL="file2brl"
127	dumptofile
128	CONVERT="unzip -p $FILE content.xml"
129	CHARSET=utf-8
130	checkTool unzip unzip "translating LibreOffice/OpenOffice OpenDocument files"
131	;;
132      application/vnd.openxmlformats-officedocument*)
133	LIBLOUIS_TOOL="file2brl"
134	dumptofile
135	CONVERT="unzip -p $FILE word/document.xml"
136	CHARSET=utf-8
137	checkTool unzip unzip "translating MS-Word docx files"
138	;;
139      text/rtf|application/rtf)
140	LIBLOUIS_TOOL="file2brl"
141	CONVERT="rtf2xml /dev/stdin"
142	checkTool rtf2xml rtf2xml "translating RTF files"
143	;;
144      application/pdf|application/vnd.cups-pdf-banner)
145	LIBLOUIS_TOOL="file2brl -p"
146	CONVERT="pdftotext -raw - -"
147	CHARSET=utf-8
148	checkTool pdftotext poppler "translating PDF files"
149	;;
150      *)
151	echo "ERROR: unsupported content type $CONTENT_TYPE" >&2
152	exit 1
153	;;
154    esac
155
156    #
157    # text encoding
158    #
159    if [ "$CHARSET" = utf-8 ]
160    then
161      LIBLOUIS_CONFIG+=" -CinputTextEncoding=UTF8"
162    else
163      LIBLOUIS_CONFIG+=" -CinputTextEncoding=ascii8"
164    fi
165
166    #
167    # Page numbers options
168    #
169    BRAILLEPAGENUMBER=$(getOption BraillePageNumber)
170    case "$BRAILLEPAGENUMBER" in
171      None) LIBLOUIS_CONFIG+=" -CbraillePages=no";;
172      TopMargin) LIBLOUIS_CONFIG+=" -CbraillePages=yes -CbraillePageNumberAt=top -CpageNumberTopSeparateLine=yes";;
173      BottomMargin) LIBLOUIS_CONFIG+=" -CbraillePages=yes -CbraillePageNumberAt=bottom -CpageNumberBottomSeparateLine=yes";;
174      TopInline) LIBLOUIS_CONFIG+=" -CbraillePages=yes -CbraillePageNumberAt=top -CpageNumberTopSeparateLine=no";;
175      BottomInline) LIBLOUIS_CONFIG+=" -CbraillePages=yes -CbraillePageNumberAt=bottom -CpageNumberBottomSeparateLine=no";;
176      *)
177	printf "ERROR: Unknown braille page number option '%s'\n" "$BRAILLEPAGENUMBER" >&2
178	exit 1
179	;;
180    esac
181
182    PRINTPAGENUMBER=$(getOption PrintPageNumber)
183    case "$PRINTPAGENUMBER" in
184      None) LIBLOUIS_CONFIG+=" -CprintPages=no";;
185      TopMargin) LIBLOUIS_CONFIG+=" -CprintPages=yes -CprintPageNumberAt=top -CpageNumberTopSeparateLine=yes";;
186      BottomMargin) LIBLOUIS_CONFIG+=" -CprintPages=yes -CprintPageNumberAt=bottom -CpageNumberBottomSeparateLine=yes";;
187      TopInline) LIBLOUIS_CONFIG+=" -CprintPages=yes -CprintPageNumberAt=top -CpageNumberTopSeparateLine=no";;
188      BottomInline) LIBLOUIS_CONFIG+=" -CprintPages=yes -CprintPageNumberAt=bottom -CpageNumberBottomSeparateLine=no";;
189      *)
190	printf "ERROR: Unknown print page number option '%s'\n" "$PRINTPAGENUMBER" >&2
191	exit 1
192	;;
193    esac
194
195    # Page numbering in top or bottom margin actually reduce the given margin
196    if [ "$BRAILLEPAGENUMBER" = TopMargin -o "$PRINTPAGENUMBER" = TopMargin ]
197    then
198      TOPMARGIN=$((TOPMARGIN - 1))
199      TEXTHEIGHT=$((TEXTHEIGHT + 1))
200    fi
201    if [ "$BRAILLEPAGENUMBER" = BottomMargin -o "$PRINTPAGENUMBER" = BottomMargin ]
202    then
203      BOTTOMMARGIN=$((BOTTOMMARGIN - 1))
204      TEXTHEIGHT=$((TEXTHEIGHT + 1))
205    fi
206
207    PAGESEPARATOR=$(getOption PageSeparator)
208    case "$PAGESEPARATOR" in
209      True|true) LIBLOUIS_CONFIG+=" -CpageSeparator=yes";;
210      False|false) LIBLOUIS_CONFIG+=" -CpageSeparator=no";;
211      *)
212	printf "ERROR: Unknown page separator option '%s'\n" "$PAGESEPARATOR" >&2
213	exit 1
214	;;
215    esac
216
217    PAGESEPARATORNUMBER=$(getOption PageSeparatorNumber)
218    case "$PAGESEPARATORNUMBER" in
219      True|true) LIBLOUIS_CONFIG+=" -CpageSeparatorNumber=yes";;
220      False|false) LIBLOUIS_CONFIG+=" -CpageSeparatorNumber=no";;
221      *)
222	printf "ERROR: Unknown page separator number option '%s'\n" "$PAGESEPARATORNUMBER" >&2
223	exit 1
224	;;
225    esac
226
227    CONTINUEPAGES=$(getOption ContinuePages)
228    case "$CONTINUEPAGES" in
229      True|true) LIBLOUIS_CONFIG+=" -CcontinuePages=yes";;
230      False|false) LIBLOUIS_CONFIG+=" -CcontinuePages=no";;
231      *)
232	printf "ERROR: Unknown page separator number option '%s'\n" "$CONTINUEPAGES" >&2
233	exit 1
234	;;
235    esac
236
237    LIBLOUIS_CONFIG+=" -CcellsPerLine=$TEXTWIDTH -ClinesPerPage=$TEXTHEIGHT "
238
239    RENDER_CALL="$LIBLOUIS_TOOL -Chyphenate=yes -CliteraryTextTable=en-us-brf.dis,$LIBLOUIS_TABLES,braille-patterns.cti $LIBLOUIS_CONFIG"
240  elif type lou_translate > /dev/null
241  then
242    # Only liblouis, but better than nothing
243    setupTextRendering
244    printf "WARN: The liblouisutdml package is required for translating braille better\n" >&2
245    TRANSLATE="lou_translate en-us-brf.dis,$LIBLOUIS_TABLES,braille-patterns.cti"
246  else
247    printf "ERROR: The liblouisutdml package is required for translating braille\n" >&2
248    exit 1
249  fi
250else
251  # No translation, only text rendering
252  printf "WARN: No braille table translation was selected\n" >&2
253  setupTextRendering
254fi
255
256# Now proceeed
257cd $TMPDIR
258echo "INFO: Reformating text" >&2
259
260(
261set -o pipefail
262set -e
263if [ -z "$CONVERT" ]
264then
265  printf "DEBUG: Calling $RENDER_CALL on '%s'\n" "$FILE" >&2
266  if [ -z "$FILE" ]
267  then
268    $RENDER_CALL 2> /dev/null | addmargins
269  else
270   < "$FILE" $RENDER_CALL 2> /dev/null | addmargins
271  fi
272elif [ -z "$TRANSLATE" ]
273then
274  printf "DEBUG: Calling $CONVERT | $RENDER_CALL on '%s'\n" "$FILE" >&2
275  if [ -z "$FILE" ]
276  then
277    $CONVERT | $RENDER_CALL 2> /dev/null | addmargins
278  else
279  < "$FILE" $CONVERT | $RENDER_CALL 2> /dev/null | addmargins
280  fi
281else
282  printf "DEBUG: Calling $CONVERT | $RENDER_CALL | $TRANSLATE on '%s'\n" "$FILE" >&2
283  if [ -z "$FILE" ]
284  then
285    $CONVERT | $RENDER_CALL 2> /dev/null | $TRANSLATE | addmargins
286  else
287  < "$FILE" $CONVERT | $RENDER_CALL 2> /dev/null | $TRANSLATE | addmargins
288  fi
289fi
290) || {
291  printf "ERROR: text conversion pipeline $CONVERT | $RENDER_CALL | $TRANSLATE | addmargins failed\n" >&2
292  exit 1
293}
294
295echo "INFO: Ready" >&2
296