Debian Codegraph @

filter/braille/filters/cups-braille.sh.in

raw
1 #
2 # Copyright (c) 2015-2018 Samuel Thibault <[email protected]>
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
10 #
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 # Get an attribute from the ppd file
26 getAttribute () {
27 ATTRIBUTE=$1
28 VALUE=`grep "^\*$ATTRIBUTE:" "$PPD" | cut -d" " -f2-`
29 VALUE=${VALUE##\"}
30 VALUE=${VALUE%%\"}
31 printf "DEBUG: Attribute $ATTRIBUTE is '%s'\n" "$VALUE" >&2
32 printf "%s" "$VALUE"
33 }
34
35 # Get an option for the document: either default ppd attribute or user-provided value
36 getOption () {
37 OPTION=$1
38 VALUE=$(getAttribute Default$OPTION)
39 printf "DEBUG: Default $OPTION is '%s'\n" "$VALUE" >&2
40
41 if [ -n "$OPTIONS" ]
42 then
43 # Case of the very first option
44 if [ -z "${OPTIONS/$OPTION=*}" ]
45 then
46 VALUE=${OPTIONS#$OPTION=}
47 VALUE=${VALUE%% *}
48 printf "DEBUG: Selected $OPTION is '%s'\n" "$VALUE" >&2
49 fi
50 # Case of other options
51 if [ -z "${OPTIONS/* $OPTION=*}" ]
52 then
53 VALUE=${OPTIONS##* $OPTION=}
54 VALUE=${VALUE%% *}
55 printf "DEBUG: Selected $OPTION is '%s'\n" "$VALUE" >&2
56 fi
57
58 # Boolean options
59 if [ -z "${OPTIONS/* $OPTION *}" ]
60 then
61 VALUE=True
62 printf "DEBUG: Selected $OPTION is '%s'\n" "$VALUE" >&2
63 fi
64 if [ -z "${OPTIONS/* no$OPTION *}" ]
65 then
66 VALUE=False
67 printf "DEBUG: Selected $OPTION is '%s'\n" "$VALUE" >&2
68 fi
69 fi
70
71 printf "%s" "$VALUE"
72 }
73
74 # Get an option for the document and check that it is a number
75 getOptionNumber () {
76 OPTION=$1
77 VALUE=$(getOption $OPTION)
78 VALUE=${VALUE#Custom.}
79 case "$VALUE" in
80 [0-9]*) ;;
81 *) printf "ERROR: Option $OPTION must be a number, got '%s'\n" "$VALUE" >&2
82 exit 1
83 ;;
84 esac
85 printf "%s" "$VALUE"
86 }
87
88 # Printing options: number of copies and page ranges
89 [ -z "$NB" ] && NB=1
90 PAGERANGES=$(getOption page-ranges)
91
92 #
93 # Page size
94 # Units in 100th of mm
95 #
96
97 # TODO: better handle imageable area
98 PAGESIZE=$(getOption PageSize)
99 case "$PAGESIZE" in
100 Legal)
101 PAGEWIDTH=21590
102 PAGEHEIGHT=35560
103 ;;
104 Letter)
105 PAGEWIDTH=21590
106 PAGEHEIGHT=27940
107 ;;
108 A3)
109 PAGEWIDTH=29700
110 PAGEHEIGHT=42000
111 ;;
112 A4)
113 PAGEWIDTH=21000
114 PAGEHEIGHT=29700
115 ;;
116 A4TF)
117 PAGEWIDTH=21000
118 PAGEHEIGHT=30480
119 ;;
120 A5)
121 PAGEWIDTH=14850
122 PAGEHEIGHT=21000
123 ;;
124 110x115)
125 PAGEWIDTH=27940
126 PAGEHEIGHT=29210
127 ;;
128 110x120)
129 PAGEWIDTH=27940
130 PAGEHEIGHT=30480
131 ;;
132 110x170)
133 PAGEWIDTH=27940
134 PAGEHEIGHT=43180
135 ;;
136 115x110)
137 PAGEWIDTH=29210
138 PAGEHEIGHT=27940
139 ;;
140 120x120)
141 PAGEWIDTH=30480
142 PAGEHEIGHT=30480
143 ;;
144 *)
145 printf "ERROR: Unknown page size '%s'\n" "$PAGESIZE" >&2
146 exit 1
147 ;;
148 esac
149
150 # Margins as announced by embosser
151 HWMARGINS=$(getAttribute HWMargins)
152 echo "DEBUG: HW margins are $HWMARGINS" >&2
153 HWMARGIN_LEFT="${HWMARGINS%% *}"
154 HWMARGINS="${HWMARGINS#* }"
155 HWMARGIN_BOTTOM="${HWMARGINS%% *}"
156 HWMARGINS="${HWMARGINS#* }"
157 HWMARGIN_RIGHT="${HWMARGINS%% *}"
158 HWMARGINS="${HWMARGINS#* }"
159 HWMARGIN_TOP="${HWMARGINS%% *}"
160
161 # Convert from points (1/72 of inch) to 1/100th of mm
162 points2mm() {
163 # First get an integer so bash can compute
164 # I.e. convert to 10^15th of point
165 # Note: bash's integer computation will work until about 1000mm, that's plenty :)
166 POINTS="$1"
167 INT_POINTS="${POINTS%.*}"
168 if [ "$INT_POINTS" = "$POINTS" ]
169 then
170 FRAC_POINTS=000000000000000
171 else
172 FRAC_POINTS="${POINTS#*.}000000000000000"
173 fi
174 FRAC_POINTS="${FRAC_POINTS:0:15}"
175 FRAC_POINTS="$INT_POINTS$FRAC_POINTS"
176 # Then we can compute conversion
177 # We round up to be safe
178 FRAC_INCH=$((($FRAC_POINTS + 71) / 72))
179 FRAC_CM=$((($FRAC_INCH * 254 + 99) / 100))
180 HUNDRENDTH_MM=$((($FRAC_CM + 999999999999) / 1000000000000))
181 echo $HUNDRENDTH_MM
182 }
183
184 MARGIN_LEFT=$(points2mm "$HWMARGIN_LEFT")
185 MARGIN_RIGHT=$(points2mm "$HWMARGIN_RIGHT")
186 MARGIN_TOP=$(points2mm "$HWMARGIN_TOP")
187 MARGIN_BOTTOM=$(points2mm "$HWMARGIN_BOTTOM")
188
189 # Margins requested by user
190 PAGE_LEFT=$(getOptionNumber page-left)
191 [ "$?" = 0 ] || exit 1
192 PAGE_LEFT=$(points2mm $PAGE_LEFT)
193 PAGE_RIGHT=$(getOptionNumber page-right)
194 [ "$?" = 0 ] || exit 1
195 PAGE_RIGHT=$(points2mm $PAGE_RIGHT)
196 PAGE_TOP=$(getOptionNumber page-top)
197 [ "$?" = 0 ] || exit 1
198 PAGE_TOP=$(points2mm $PAGE_TOP)
199 PAGE_BOTTOM=$(getOptionNumber page-bottom)
200 [ "$?" = 0 ] || exit 1
201 PAGE_BOTTOM=$(points2mm $PAGE_BOTTOM)
202
203 [ -n "$PAGE_LEFT" -a "$MARGIN_LEFT" -le "$PAGE_LEFT" ] || PAGE_LEFT=$MARGIN_LEFT
204 [ -n "$PAGE_RIGHT" -a "$MARGIN_RIGHT" -le "$PAGE_RIGHT" ] || PAGE_RIGHT=$MARGIN_RIGHT
205 [ -n "$PAGE_TOP" -a "$MARGIN_TOP" -le "$PAGE_TOP" ] || PAGE_TOP=$MARGIN_TOP
206 [ -n "$PAGE_BOTTOM" -a "$MARGIN_BOTTOM" -le "$PAGE_BOTTOM" ] || PAGE_BOTTOM=$MARGIN_BOTTOM
207
208 echo "DEBUG: hard margins are left $MARGIN_LEFT right $MARGIN_RIGHT top $MARGIN_TOP bottom $MARGIN_BOTTOM" >&2
209 echo "DEBUG: graphical margins are left $PAGE_LEFT right $PAGE_RIGHT top $PAGE_TOP bottom $PAGE_BOTTOM" >&2
210
211 # This is the hardware-printable area
212 PRINTABLEWIDTH=$(($PAGEWIDTH - $MARGIN_LEFT - $MARGIN_RIGHT))
213 PRINTABLEHEIGHT=$(($PAGEHEIGHT - $MARGIN_TOP - $MARGIN_BOTTOM))
214
215 echo "DEBUG: printable area is ${PRINTABLEWIDTH}x${PRINTABLEHEIGHT}" >&2
216
217 #
218 # Text spacing
219 #
220
221 TEXTDOTDISTANCE=$(getOptionNumber TextDotDistance)
222 [ "$?" = 0 ] || exit 1
223 case "$TEXTDOTDISTANCE" in
224 220) TEXTCELLDISTANCE=310 ;;
225 250) TEXTCELLDISTANCE=350 ;;
226 320) TEXTCELLDISTANCE=525 ;;
227 *)
228 printf "ERROR: Unknown text dot distance '%s'\n" "$TEXTDOTDISTANCE" >&2
229 exit 1
230 ;;
231 esac
232
233 TEXTDOTS=$(getOptionNumber TextDots)
234 [ "$?" = 0 ] || exit 1
235 LINESPACING=$(getOptionNumber LineSpacing)
236 [ "$?" = 0 ] || exit 1
237
238 # Cell dimension, including spacing
239 TEXTCELLWIDTH=$(( $TEXTDOTDISTANCE + $TEXTCELLDISTANCE ))
240 TEXTCELLHEIGHT=$(( $TEXTDOTDISTANCE * ($TEXTDOTS / 2 - 1) + $LINESPACING ))
241
242 # Compute number of printable cells according to page width and height
243 PRINTABLETEXTWIDTH=$(( ($PRINTABLEWIDTH + $TEXTCELLDISTANCE) / $TEXTCELLWIDTH ))
244 PRINTABLETEXTHEIGHT=$(( ($PRINTABLEHEIGHT + $LINESPACING) / $TEXTCELLHEIGHT ))
245
246 if [ "$(getOption TopMargin)" = "" ]
247 then
248 # No margin
249 TEXTWIDTH=$PRINTABLETEXTWIDTH
250 TEXTHEIGHT=$PRINTABLETEXTHEIGHT
251 else
252 # Margins in cells
253 TOPMARGIN=$(getOptionNumber TopMargin)
254 [ "$?" = 0 ] || exit 1
255 BOTTOMMARGIN=$(getOptionNumber BottomMargin)
256 [ "$?" = 0 ] || exit 1
257 LEFTMARGIN=$(getOptionNumber LeftMargin)
258 [ "$?" = 0 ] || exit 1
259 RIGHTMARGIN=$(getOptionNumber RightMargin)
260 [ "$?" = 0 ] || exit 1
261 TEXTWIDTH=$(( $PRINTABLETEXTWIDTH - $LEFTMARGIN - $RIGHTMARGIN ))
262 TEXTHEIGHT=$(( $PRINTABLETEXTHEIGHT - $TOPMARGIN - $BOTTOMMARGIN ))
263 fi
264
265 # Filter that adds top and left margins on the fly, to be used while producing
266 # BRF output.
267 addmargins() {
268 NEWPAGE=""
269 if [ -n "$TOPMARGIN" ]; then
270 for I in $(seq 1 $TOPMARGIN) ; do
271 NEWPAGE="$NEWPAGE"$'\r'$'\n'
272 done
273 fi
274 NEWPAGESED=${NEWPAGE//$'\n'/\\$'\n'}
275 LEFTSPACES=""
276 if [ -n "$LEFTMARGIN" ]; then
277 for I in $(seq 1 $LEFTMARGIN) ; do
278 LEFTSPACES="$LEFTSPACES "
279 done
280 fi
281
282 echo -n "$NEWPAGE"
283 sed -e '$s/ $//' \
284 -e "s/^\( \?\)\([^ ]\)/\1$LEFTSPACES\2/" \
285 -e "s/ / $NEWPAGESED/"
286 echo -n " "
287 }
288
289 #
290 # Graphic spacing
291 #
292
293 # Compute number of printable cells according to page size
294 GRAPHICDOTDISTANCE=$(getOptionNumber GraphicDotDistance)
295 [ "$?" = 0 ] || exit 1
296
297 # This is the total area we will send to the embosser
298 TOTALGRAPHICWIDTH=$(( ( ($PRINTABLEWIDTH - 160) / $GRAPHICDOTDISTANCE ) / 2 * 2 ))
299 TOTALGRAPHICHEIGHT=$(( ( ($PRINTABLEHEIGHT - 160) / $GRAPHICDOTDISTANCE ) / 4 * 4 ))
300
301 echo "DEBUG: total graphical: ${TOTALGRAPHICWIDTH}x${TOTALGRAPHICHEIGHT}" >&2
302
303 # This is how many dots we have to introduce to respect at least the software left+top margin
304 GRAPHICHOFFSET=$(( ($PAGE_LEFT - $MARGIN_LEFT + $GRAPHICDOTDISTANCE - 1) / $GRAPHICDOTDISTANCE ))
305 GRAPHICVOFFSET=$(( ($PAGE_TOP - $MARGIN_TOP + $GRAPHICDOTDISTANCE - 1) / $GRAPHICDOTDISTANCE ))
306
307 echo "DEBUG: graphical offset: ${GRAPHICHOFFSET}x${GRAPHICVOFFSET}" >&2
308
309 # This is the resulting actual margin
310 GRAPHICLEFTMARGIN=$(( $MARGIN_LEFT + $GRAPHICHOFFSET * $GRAPHICDOTDISTANCE ))
311 GRAPHICTOPMARGIN=$(( $MARGIN_TOP + $GRAPHICVOFFSET * $GRAPHICDOTDISTANCE ))
312
313 echo "DEBUG: rounded graphical top-left corner margin: ${GRAPHICLEFTMARGIN}x${GRAPHICTOPMARGIN}" >&2
314
315 # Then compute how many dots we can afford until reaching the software right+bottom margin
316 GRAPHICWIDTH=$(( ( ( $PAGEWIDTH - $GRAPHICLEFTMARGIN - $PAGE_RIGHT ) - 160) / $GRAPHICDOTDISTANCE ))
317 GRAPHICHEIGHT=$(( ( ( $PAGEHEIGHT - $GRAPHICTOPMARGIN - $PAGE_BOTTOM ) - 160) / $GRAPHICDOTDISTANCE ))
318
319 echo "DEBUG: resulting graphical area: ${GRAPHICWIDTH}x${GRAPHICHEIGHT}" >&2
320
321 #
322 # Text translation
323 #
324
325 TABLESDIR=@TABLESDIR@
326 echo "DEBUG: Liblouis table directory is $TABLESDIR" >&2
327
328 getOptionLibLouis () {
329 OPTION=$1
330 VALUE=$(getOption $OPTION)
331
332 # Check validity of input
333 case "$VALUE" in
334 [-_.0-9A-Za-z]*) ;;
335 *) printf "ERROR: Option $OPTION must be a valid liblouis table name, got '%s'\n" "$VALUE" >&2
336 exit 1
337 ;;
338 esac
339
340 LOCALE=${LANG%@*}
341 LOCALE=${LOCALE%.*}
342 LANGUAGE=${LOCALE%_*}
343 COUNTRY=${LOCALE#*_}
344 LOUIS_LOCALE=$LANGUAGE-$COUNTRY
345
346 getLibLouisTableScore () {
347 GRADE="$1"
348 printf "DEBUG: looking for locale '%s' and grade '%s' \n" "$LOCALE" "$GRADE" >&2
349 # Try to select a good table from its metadata
350 selected=
351 selectedscore=0
352 for table in "$TABLESDIR/"*.tbl "$TABLESDIR/"*.ctb "$TABLESDIR/"*.utb; do
353 score=0
354 name=${table#$TABLESDIR/}
355
356 if grep -q "^#+locale:$LOUIS_LOCALE$" $table; then
357 printf "DEBUG: %s has correct locale %s\n" "$name" "$LOUIS_LOCALE" >&2
358 score=$((score + 15))
359 elif grep -q "^#+locale:$LANGUAGE$" $table; then
360 printf "DEBUG: %s has correct language %s\n" "$name" "$LANGUAGE" >&2
361 score=$((score + 10))
362 else
363 # Requested language is a must
364 continue
365 fi
366
367 if [ -n "$GRADE" ]; then
368 if grep -q "^#+grade:$GRADE$" $table || \
369 ( [ "$GRADE" = 0 ] && ( grep -q "^#+contraction:no" $table || grep -q "^#+type:computer" $table ) ) \
370 then
371 printf "DEBUG: %s has correct grade %s\n" "$name" "$GRADE" >&2
372 score=$((score + 10))
373 else
374 # Requested grade is a must
375 continue
376 fi
377 fi
378
379 # Dot numbers are not always specified in liblouis :/
380 if grep -q "^#+dots:$TEXTDOTS$" $table || \
381 ( [ "$TEXTDOTS" = 6 ] && grep -q "^#+grade:[1-3]" $table ) \
382 then
383 printf "DEBUG: %s has correct dots %s\n" "$name" "$TEXTDOTS" >&2
384 score=$((score + 2))
385 fi
386
387 if [ $score -gt $selectedscore ]; then
388 printf "DEBUG: %s has better score $score\n" "$name" >&2
389 selected=$name
390 selectedscore=$score
391 fi
392 done
393
394 echo $selected
395 }
396
397 # Check presence of table
398 case "$VALUE" in
399 None)
400 printf None
401 ;;
402 Locale)
403 selected=$(getLibLouisTableScore '')
404 # Try tagged tables before untagged ones
405 if [ -n "$selected" ]; then
406 printf "%s" "$selected"
407 elif [ -f "$TABLESDIR/$LOCALE.tbl" ]; then
408 printf "%s" "$LOCALE.tbl"
409 elif [ -f "$TABLESDIR/$LANGUAGE.tbl" ]; then
410 printf "%s" "$LANGUAGE.tbl"
411 else
412 printf "ERROR: Could not find $OPTION table with locale %s\n" "$LOCALE" >&2
413 printf None
414 exit 1
415 fi
416 ;;
417 Locale-g[0-3])
418 GRADE=${VALUE#Locale-g}
419 selected=$(getLibLouisTableScore $GRADE)
420 if [ -n "$selected" ]; then
421 printf "%s" "$selected"
422 exit 0
423 else
424 for i in "$TABLESDIR/$LOCALE.tbl" "$TABLESDIR/$LOCALE"*.tbl "$TABLESDIR/$LANGUAGE.tbl" "$TABLESDIR/$LANGUAGE"*.tbl
425 do
426 if grep -q "^#+grade:$GRADE$" "$i"
427 then
428 printf "%s" "${i//*\/}"
429 exit 0
430 fi
431 done
432 fi
433 printf "ERROR: Could not find $OPTION table with locale %s and grade %s\n" "$LOCALE" "$GRADE" >&2
434 printf None
435 exit 1
436 ;;
437 HyphLocale)
438 if [ -f "$TABLESDIR/hyph_$LOCALE.dic" ]; then
439 printf "%s" "hyph_$LOCALE.dic"
440 elif [ -f "$TABLESDIR/hyph_$LANGUAGE.dic" ]; then
441 printf "%s" "hyph_$LANGUAGE.dic"
442 else
443 printf "WARN: Could not find $OPTION hyphenation table with locale %s\n" "$LOCALE" >&2
444 printf None
445 fi
446 ;;
447 *)
448 [ -f "$TABLESDIR/$VALUE".utb ] && VALUE="$VALUE".utb
449 [ -f "$TABLESDIR/$VALUE".ctb ] && VALUE="$VALUE".ctb
450 if [ ! -f "$TABLESDIR/$VALUE" ]
451 then
452 printf "ERROR: Could not find $OPTION table '%s'\n" "$VALUE" >&2
453 exit 1
454 fi
455
456 printf "%s" "$VALUE"
457 ;;
458 esac
459 }
460
461 LIBLOUIS1=$(getOptionLibLouis LibLouis)
462 [ "$?" = 0 ] || exit 1
463 LIBLOUIS2=$(getOptionLibLouis LibLouis2)
464 [ "$?" = 0 ] || exit 1
465 LIBLOUIS3=$(getOptionLibLouis LibLouis3)
466 [ "$?" = 0 ] || exit 1
467 LIBLOUIS4=$(getOptionLibLouis LibLouis4)
468 [ "$?" = 0 ] || exit 1
469
470 echo "DEBUG: Table1 $LIBLOUIS1" >&2
471 echo "DEBUG: Table2 $LIBLOUIS2" >&2
472 echo "DEBUG: Table3 $LIBLOUIS3" >&2
473 echo "DEBUG: Table4 $LIBLOUIS4" >&2
474
475 [ "$LIBLOUIS1" != None ] && LIBLOUIS_TABLES="$LIBLOUIS1"
476 [ "$LIBLOUIS2" != None ] && LIBLOUIS_TABLES="${LIBLOUIS_TABLES:+$LIBLOUIS_TABLES,}$LIBLOUIS2"
477 [ "$LIBLOUIS3" != None ] && LIBLOUIS_TABLES="${LIBLOUIS_TABLES:+$LIBLOUIS_TABLES,}$LIBLOUIS3"
478 [ "$LIBLOUIS4" != None ] && LIBLOUIS_TABLES="${LIBLOUIS_TABLES:+$LIBLOUIS_TABLES,}$LIBLOUIS4"
479
480 #
481 # Checking for presence of tools
482 #
483 checkTool() {
484 TOOL=$1
485 PACKAGE=$2
486 USE=$3
487 if ! type $TOOL > /dev/null
488 then
489 printf "ERROR: The $PACKAGE package is required for $USE\n" >&2
490 exit 1
491 fi
492 }