From 62c9dd8318fd5d4a24c7ace937455052923bbf42 Mon Sep 17 00:00:00 2001 From: Samuel Thibault Date: Sun, 17 Dec 2017 20:20:01 +0100 Subject: [PATCH] braille: Add support for page-ranges option --- drv/generic-brf.drv | 2 +- drv/generic-ubrl.drv | 2 +- drv/indexv3.drv | 2 +- drv/indexv4.drv | 2 +- filter/braille/filters/brftopagedbrf.in | 144 ++++++++++++++++++++++ filter/braille/filters/cups-braille.sh.in | 2 + mime/braille.convs | 3 + mime/braille.types | 3 + 8 files changed, 156 insertions(+), 4 deletions(-) create mode 100755 filter/braille/filters/brftopagedbrf.in diff --git a/drv/generic-brf.drv b/drv/generic-brf.drv index 6f35d9124..4b9431a4c 100644 --- a/drv/generic-brf.drv +++ b/drv/generic-brf.drv @@ -35,7 +35,7 @@ Version 1.0 Throughput 1 -Filter application/vnd.cups-brf 0 brftoembosser +Filter application/vnd.cups-paged-brf 0 brftoembosser Filter image/vnd.cups-brf 0 brftoembosser MediaSize Legal diff --git a/drv/generic-ubrl.drv b/drv/generic-ubrl.drv index 4e273aa4d..d18a63005 100644 --- a/drv/generic-ubrl.drv +++ b/drv/generic-ubrl.drv @@ -35,7 +35,7 @@ Version 1.0 Throughput 1 -Filter text/vnd.cups-ubrl 0 - +Filter text/vnd.cups-paged-ubrl 0 - Filter image/vnd.cups-ubrl 0 - MediaSize Legal diff --git a/drv/indexv3.drv b/drv/indexv3.drv index 3ce675a0a..8e2feec69 100644 --- a/drv/indexv3.drv +++ b/drv/indexv3.drv @@ -34,7 +34,7 @@ Manufacturer "Index" Version 1.0 -Filter application/vnd.cups-brf 0 textbrftoindexv3 +Filter application/vnd.cups-paged-brf 0 textbrftoindexv3 Filter image/vnd.cups-ubrl 0 imageubrltoindexv3 // diff --git a/drv/indexv4.drv b/drv/indexv4.drv index e38655de1..db08efd7f 100644 --- a/drv/indexv4.drv +++ b/drv/indexv4.drv @@ -34,7 +34,7 @@ Manufacturer "Index" Version 1.0 -Filter application/vnd.cups-brf 0 textbrftoindexv4 +Filter application/vnd.cups-paged-brf 0 textbrftoindexv4 Filter image/vnd.cups-ubrl 0 imageubrltoindexv4 // diff --git a/filter/braille/filters/brftopagedbrf.in b/filter/braille/filters/brftopagedbrf.in new file mode 100755 index 000000000..138c900e5 --- /dev/null +++ b/filter/braille/filters/brftopagedbrf.in @@ -0,0 +1,144 @@ +#!/bin/bash + +# +# Copyright (c) 2015-2017 Samuel Thibault +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# + +# Make sure we have enough options +if [ $# != 5 -a $# != 6 ]; then + echo "ERROR: $0 jobid user name nb options [filename]" >&2 + exit 1 +fi + +NB=$4 +OPTIONS=$5 +FILE=$6 + +. @CUPS_DATADIR@/braille/cups-braille.sh + +shopt -s extglob + +# Construct list of pages from PAGERANGES + +PAGES=" " # Explicit list of pages +AFTER= # All pages including and after this +BEFORE= # All pages before and including this + +while [ -n "${PAGERANGES}" ] +do + PAGERANGE=${PAGERANGES/,*} + PAGERANGE=${PAGERANGE%%*( )} + PAGERANGE=${PAGERANGE##*( )} + if [ -n "${PAGERANGES/*,*}" ] + then + # last range + PAGERANGES="" + else + # Remove leading range + PAGERANGES="${PAGERANGES#*,}" + fi + + if [ -n "${PAGERANGE/*-*}" ] + then + # single-page + PAGES="$PAGES$PAGERANGE " + + elif [ -z "${PAGERANGE%%*-}" ] + then + # To the end + FIRST=${PAGERANGE%%-*} + if [ -z "$AFTER" ] || [ "$FIRST" -lt "$AFTER" ] + then + AFTER="$FIRST" + fi + + elif [ -z "${PAGERANGE##-*}" ] + then + # From the beginning + LAST=${PAGERANGE##*-} + if [ -z "$BEFORE" ] || [ "$LAST" -gt "$BEFORE" ] + then + BEFORE="$LAST" + fi + + else + # page range + FIRST=${PAGERANGE/-*} + LAST=${PAGERANGE/*-} + PAGES="$PAGES$(seq "$FIRST" "$LAST" | tr $'\012' ' ')" + + fi +done + +# Determine whether to print this page +doprint() { + PAGE="$1" + if [ -n "$BEFORE" ] && [ "$PAGE" -le "$BEFORE" ] + then + echo 1 + return + elif [ -n "$AFTER" ] && [ "$PAGE" -ge "$AFTER" ] + then + echo 1 + return + fi + case "$PAGES" in + *\ $PAGE\ *) + echo 1 + return + ;; + esac + echo 0 +} + +if [ -z "$FILE" ] +then + cat +else + cat "$FILE" +fi | ( + P=1 + DOPRINT=$(doprint $P) + while IFS=$'\n' read -r LINE + do + while [ -z "${LINE/*$'\014'*}" ] + do + # Possibly print before FF + HEAD=${LINE%%$'\014'*} + [ $DOPRINT == 0 ] || printf %s "$HEAD"$'\014' + + # Next page + P=$(($P + 1)) + DOPRINT=$(doprint $P) + + # Drop head of line + LINE=${LINE#*$'\014'} + done + + # Remainder of line + [ $DOPRINT == 0 ] || printf "%s\n" "$LINE" + done +) + +echo "INFO: Ready" >&2 +exit 0 diff --git a/filter/braille/filters/cups-braille.sh.in b/filter/braille/filters/cups-braille.sh.in index 704c9a4be..433b6fe14 100644 --- a/filter/braille/filters/cups-braille.sh.in +++ b/filter/braille/filters/cups-braille.sh.in @@ -85,7 +85,9 @@ getOptionNumber () { printf "%s" "$VALUE" } +# Printing options: number of copies and page ranges [ -z "$NB" ] && NB=1 +PAGERANGES=$(getOption page-ranges) # # Page size diff --git a/mime/braille.convs b/mime/braille.convs index 660fe33ee..662cf0f4b 100644 --- a/mime/braille.convs +++ b/mime/braille.convs @@ -30,6 +30,9 @@ application/xhtml application/vnd.cups-brf 10 texttobrf application/xml application/vnd.cups-brf 10 texttobrf application/sgml application/vnd.cups-brf 10 texttobrf +application/vnd.cups-brf application/vnd.cups-paged-brf 0 brftopagedbrf +application/vnd.cups-ubrl application/vnd.cups-paged-ubrl 0 brftopagedbrf + application/vnd.recordare.musicxml+xml application/vnd.cups-brf 30 musicxmltobrf application/vnd.oasis.opendocument.chart application/vnd.cups-brf 30 texttobrf diff --git a/mime/braille.types b/mime/braille.types index a5051d295..32ee84bd8 100644 --- a/mime/braille.types +++ b/mime/braille.types @@ -28,6 +28,9 @@ image/vnd.cups-brf image/vnd.cups-ubrl image/vnd.cups-pdf +application/vnd.cups-paged-brf +application/vnd.cups-paged-ubrl + application/sgml sgml application/vnd.recordare.musicxml+xml xml contains(0,1000,"