home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / bin / foo2oak-wrapper < prev    next >
Encoding:
Text File  |  2006-06-19  |  13.1 KB  |  579 lines

  1. #!/bin/sh
  2.  
  3. #* Copyright (C) 2003  Rick Richardson
  4. #*
  5. #* This program is free software; you can redistribute it and/or modify
  6. #* it under the terms of the GNU General Public License as published by
  7. #* the Free Software Foundation; either version 2 of the License, or
  8. #* (at your option) any later version.
  9. #*
  10. #* This program is distributed in the hope that it will be useful,
  11. #* but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #* GNU General Public License for more details.
  14. #*
  15. #* You should have received a copy of the GNU General Public License
  16. #* along with this program; if not, write to the Free Software
  17. #* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. #*
  19. #* Authors: Rick Richardson <rickr@mn.rr.com>
  20.  
  21. VERSION='$Id: foo2oak-wrapper.in,v 1.19 2005/12/20 11:08:53 rick Exp $'
  22.  
  23. PROGNAME="$0"
  24. BASENAME=`basename $PROGNAME`
  25. PREFIX=/usr
  26. SHARE=$PREFIX/share/foo2oak
  27.  
  28. #
  29. #    Log the command line, for debugging and problem reports
  30. #
  31. if [ -x /usr/bin/logger ]; then
  32.     logger -t "$BASENAME" -p lpr.info -- "foo2oak-wrapper $@" </dev/null
  33. fi
  34.  
  35. usage() {
  36.     cat <<EOF
  37. Usage:
  38.     $BASENAME [options] [ps-file]
  39.  
  40.     Foomatic printer wrapper for the foo2oak printer driver.
  41.     This script reads a Postscript ps-file or standard input
  42.     and converts it to Oak Technoligies OAKT printer format.
  43.  
  44. Normal Options:
  45. -b bits           Bits per plane (1 or 2) [$BPP]
  46. -c                Print in color (else monochrome)
  47. -m media          Media code to send to printer [$MEDIA]
  48.                     0=auto 1=plain 2=preprinted 3=letterhead 4=graytrans
  49.                     5=prepunched 6=labels 7=bond 8=recycled 9=color
  50.                     10=cardstock 11=heavy 12=envelope 13=light 14=tough
  51. -p paper          Paper code [$PAPER]
  52.                     1=letter, 5=legal, 7=executive, 9=A4, 11=A5, 13=B5jis
  53. -n copies         Number of copies [$COPIES]
  54. -r <xres>x<yres>  Set device resolution in pixels/inch [$RES]
  55. -s source         Source code to send to printer [$SOURCE]
  56.                     1=tray1, 4=manual, 7=auto
  57.             Code numbers may vary with printer model.
  58. -2 / -4           2-up, 4-up
  59.  
  60. Printer Tweaking Options:
  61. -u <xoff>x<yoff>  Set offset of upper left printable in pixels [varies]
  62. -l <xoff>x<yoff>  Set offset of lower right printable in pixels [varies]
  63. -L mask           Send logical clipping values from -u/-l in ZjStream [3]
  64.                   0=no, 1=Y, 2=X, 3=XY
  65.  
  66. Color Tweaking Options:
  67. -g gsopts         Additional options to pass to Ghostscript, such as
  68.                   -dDITHERPPI=nnn, etc.  May appear more than once. []
  69. -G profile.icm    Convert profile.icm to a Postscript CRD using icc2ps and
  70.                   adjust colors using the setcolorrendering PS operator.
  71.                   $SHARE/icm/ will be searched for profile.icm.
  72. -I intent         Select profile intent from ICM file [$INTENT]
  73.                   0=Perceptual, 1=Colorimetric, 2=Saturation, 3=Absolute
  74. -G gamma-file.ps  Prepend gamma-file to the Postscript input to perform
  75.                   color correction using the setcolortransfer PS operator.
  76.  
  77. Debugging Options:
  78. -S plane          Output just a single color plane from a color print [all]
  79.                   1=Cyan, 2=Magenta, 3=Yellow, 4=Black
  80. -D lvl            Set Debug level [$DEBUG]
  81. -V                $VERSION
  82. EOF
  83.  
  84.     exit 1
  85. }
  86.  
  87. #
  88. #       Report an error and exit
  89. #
  90. error() {
  91.     echo "$BASENAME: $1" >&2
  92.     exit 1
  93. }
  94.  
  95. dbgcmd() {
  96.     if [ $DEBUG -ge 1 ]; then
  97.         echo "$@" >&2
  98.     fi
  99.     "$@"
  100. }
  101.  
  102. #
  103. #    N-up-ify the job.  Requires psnup from psutils package
  104. #
  105. nup2() {
  106.     tr '\r' '\n' | psnup -d2 -2 -m.3in -p$paper -q
  107. }
  108.  
  109. nup4() {
  110.     tr '\r' '\n' | psnup -d2 -4 -m.5in -p$paper -q
  111. }
  112.  
  113. #
  114. #       Process the options
  115. #
  116. BPP=1
  117. GSBIN=gs
  118. CMDLINE="$*"
  119. DEBUG=0
  120. DUPLEX=1
  121. COLOR=
  122. QUALITY=1
  123. MEDIA=1
  124. COPIES=1
  125. test -r /etc/papersize && PAPER=$(cat /etc/papersize)
  126. test "$PAPER" || PAPER=1
  127. RES=600x600
  128. SOURCE=7
  129. NUP=
  130. CLIP_UL=
  131. CLIP_LR=
  132. CLIP_LOG=
  133. BC=
  134. AIB=
  135. COLOR2MONO=
  136. GAMMAFILE=
  137. INTENT=0
  138. GSOPTS=
  139. GSDEV=-sDEVICE=pbmraw
  140. while getopts "24b:cd:g:l:u:L:m:n:p:q:r:s:ABS:D:G:I:Vh?" opt
  141. do
  142.     case $opt in
  143.     b)    BPP=$OPTARG;;
  144.     c)    COLOR=-c;;
  145.     d)    DUPLEX="$OPTARG";;
  146.     g)    GSOPTS="$GSOPTS $OPTARG";;
  147.     m)    MEDIA="$OPTARG";;
  148.     n)    COPIES="$OPTARG";;
  149.     p)    PAPER="$OPTARG";;
  150.     q)    QUALITY="$OPTARG";;
  151.     r)    RES="$OPTARG";;
  152.     s)    SOURCE="$OPTARG";;
  153.     l)    CLIP_LR="-l $OPTARG";;
  154.     u)    CLIP_UL="-u $OPTARG";;
  155.     L)    CLIP_LOG="-L $OPTARG";;
  156.     A)    AIB=-A;;
  157.     B)    BC=-B;;
  158.     S)    COLOR2MONO="-S$OPTARG";;
  159.     D)    DEBUG="$OPTARG";;
  160.     G)    GAMMAFILE="$OPTARG";;
  161.     I)    INTENT="$OPTARG";;
  162.     2)    NUP="2";;
  163.     4)    NUP="4";;
  164.     V)    echo "$VERSION"; foo2oak -V; exit 0;;
  165.     h|\?)
  166.         if [ "$CMDLINE" != "-?" -a "$CMDLINE" != -h ]; then
  167.             echo "Illegal command:"
  168.             echo "    $0 $CMDLINE"
  169.             echo
  170.         fi
  171.         usage;;
  172.     esac
  173. done
  174. shift `expr $OPTIND - 1`
  175.  
  176. #
  177. #    Select the ghostscript device to use
  178. #
  179. case "$BPP" in
  180. 1)    if [ "" = "$COLOR" ]; then
  181.         GSDEV=-sDEVICE=pbmraw
  182.     else
  183.         GSDEV=-sDEVICE=bitcmyk
  184.     fi
  185.     ;;
  186. 2)    if [ "" = "$COLOR" ];
  187.     then
  188.         GSDEV=-sDEVICE=pgmraw
  189.     else
  190.         GSDEV="-sDEVICE=cups -dcupsColorSpace=6 -dcupsBitsPerColor=2"
  191.     fi
  192.     ;;
  193. *)    error "Illegal number of bits per plane ($BPP)";;
  194. esac
  195.  
  196. #
  197. case "$QUALITY" in
  198. 0)
  199.     GSOPTS="-dCOLORSCREEN $GSOPTS"
  200.     ;;
  201. 1)
  202.     GSOPTS="-dCOLORSCREEN $GSOPTS"
  203.     ;;
  204. 2)
  205.     GSOPTS="-dMaxBitMap=500000000 $GSOPTS"
  206.     ;;
  207. esac
  208.  
  209. #
  210. #    Validate media code
  211. #
  212. case "$MEDIA" in
  213. 0|auto)        MEDIA=0;;
  214. 1|plain)    MEDIA=1;;
  215. 2|preprinted)    MEDIA=2;;
  216. 3|letterhead)    MEDIA=3;;
  217. 4|gratrans*)    MEDIA=4;;
  218. 5|prepunched)    MEDIA=5;;
  219. 6|labels)    MEDIA=6;;
  220. 7|bond)        MEDIA=7;;
  221. 8|recylcled)    MEDIA=8;;
  222. 9|color)    MEDIA=9;;
  223. 10|cardstock)    MEDIA=10;;
  224. 11|heavy)    MEDIA=11;;
  225. 12|envelope)    MEDIA=12;;
  226. 13|light)    MEDIA=13;;
  227. 14|tough)    MEDIA=14;;
  228. [0-9]*)        ;;
  229. *)        error "Unknown media code $MEDIA";;
  230. esac
  231.  
  232. #
  233. #    Validate source (InputSlot) code
  234. #
  235. case "$SOURCE" in
  236. 1|tray1)    SOURCE=1;;
  237. 4|manual)    SOURCE=4;;
  238. 7|auto)        SOURCE=7;;
  239. [0-9]*)        ;;
  240. *)        error "Unknown source code $SOURCE";;
  241. esac
  242.  
  243. #
  244. #    Validate Duplex code
  245. #
  246. case "$DUPLEX" in
  247. 1|off|none)    DUPLEX=1;;
  248. 2|long*)    DUPLEX=2;;
  249. 3|short*)    DUPLEX=3;;
  250. [0-9]*)        ;;
  251. *)        error "Unknown duplex code $DUPLEX";;
  252. esac
  253.  
  254. #
  255. #    Validate Resolution
  256. #
  257. case "$RES" in
  258. 600x600)    ;;
  259. 1200x600)    ;;
  260. 2400x600)    ;;
  261. *)        error "Illegal resolution $RES";;
  262. esac
  263.  
  264. #
  265. #    Figure out the paper dimensions in pixels/inch, and set the
  266. #    default clipping region.
  267. #
  268. set_clipping() {
  269.     ulx=$1; uly=$2
  270.     lrx=$3; lry=$4
  271.  
  272.     # Set clipping region if it isn't already set
  273.     if [ "$CLIP_UL" = "" ]; then
  274.     case "$RES" in
  275.     600x600)    ulx=`expr $ulx / 2`;;
  276.     2400x600)    ulx=`expr $ulx \* 2`;;
  277.     esac
  278.     CLIP_UL="-u ${ulx}x${uly}"
  279.     fi
  280.     if [ "$CLIP_LR" = "" ]; then
  281.     case "$RES" in
  282.     600x600)    lrx=`expr $lrx / 2`;;
  283.     2400x600)    lrx=`expr $lrx \* 2`;;
  284.     esac
  285.     CLIP_LR="-l ${lrx}x${lry}"
  286.     fi
  287. }
  288.  
  289. case "$PAPER" in
  290. [0-9]*x*[0-9])
  291.         XDIM=`echo "$PAPER" | sed 's/x.*//' `
  292.         YDIM=`echo "$PAPER" | sed 's/.*x//' `
  293.         XDIM=`awk -vval=$XDIM 'BEGIN{ print int(val * 1200.0) }' `
  294.         YDIM=`awk -vval=$YDIM 'BEGIN{ print int(val * 600.0) }' `
  295.         set_clipping 140 100    140 100
  296.         paper=letter
  297.         PAPER=256
  298.         ;;
  299. 1|letter)    PAPER=1;    paper=letter;    XDIM="10200"; YDIM="6600"
  300.         set_clipping 140 100    140 100
  301.         ;;
  302. 5|legal)    PAPER=5;    paper=legal;     XDIM="10200"; YDIM="8400"
  303.         set_clipping 140 100    140 100
  304.         ;;
  305. 7|executive)    PAPER=7;    paper=executive; XDIM="8700";  YDIM="6300"
  306.         set_clipping 140 100    140 100
  307.         ;;
  308. 9|a4|A4)    PAPER=9;    paper=a4;        XDIM="9920";  YDIM="7014"
  309.         set_clipping 160 100    160 100
  310.         ;;
  311. 11|a5|A5)    PAPER=11;    paper=a5;        XDIM="6992";  YDIM="4960"
  312.         set_clipping 140 100    140 100
  313.         ;;
  314. 13|b5jis|B5JIS)    PAPER=13;    paper=b5;        XDIM="8598";  YDIM="6070"
  315.         set_clipping 140 100    140 100
  316.         ;;
  317. *)        error "Unimplemented paper code $PAPER";;
  318. esac
  319. PAPERSIZE="-sPAPERSIZE=$paper";
  320.  
  321. case "$RES" in
  322. 600x600)    XDIM=`expr $XDIM / 2`;;
  323. 1200x600)    ;;
  324. 2400x600)    XDIM=`expr $XDIM \* 2`;;
  325. esac
  326. DIM="${XDIM}x${YDIM}"
  327.  
  328. #
  329. # If there is an argument left, take it as the file to print.
  330. # Else, the input comes from stdin.
  331. #
  332. if [ $# -ge 1 ]; then
  333.     if [ "$LPJOB" = "" ]; then
  334.     LPJOB="$1"
  335.     fi
  336.     exec < $1
  337. fi
  338.  
  339. #
  340. # Filter thru psnup if N-up printing has been requested
  341. #
  342. case $NUP in
  343. 2)    PREFILTER="nup2";;
  344. 4)    PREFILTER="nup4";;
  345. *)    PREFILTER=cat;;
  346. esac
  347.  
  348. #
  349. #    Overload -G.  If the file name ends with ".icm" or ".ICM"
  350. #    then convert the ICC color profile to a Postscript CRD,
  351. #    then prepend it to the users job.  Select the intent
  352. #    using the -I option.
  353. #
  354.  
  355. create_crd() {
  356.     #
  357.     # Create a Postscript CRD
  358.     #
  359.     ICC2PS=$PREFIX/bin/foo2zjs-icc2ps
  360.     if [ -x $ICC2PS ]; then
  361.     $ICC2PS -o $GAMMAFILE -t$INTENT > $ICCTMP.crd.ps 2>$ICCTMP.log \
  362.     || error "Problem converting .ICM file to Postscript"
  363.     cat > $ICCTMP.usecie.ps <<-EOF
  364.         %!PS-Adobe-3.0
  365.         <</UseCIEColor true>>setpagedevice
  366.     EOF
  367.     cat > $ICCTMP.selcrd.ps <<-EOF
  368.         /Current /ColorRendering findresource setcolorrendering
  369.     EOF
  370.     GAMMAFILE="$ICCTMP.usecie.ps $ICCTMP.crd.ps $ICCTMP.selcrd.ps"
  371.     else
  372.     GAMMFILE=
  373.     fi
  374. }
  375.  
  376. if [ $DEBUG -gt 0 ]; then
  377.     ICCTMP=/tmp/icc
  378. else
  379.     ICCTMP=/tmp/icc$$
  380. fi
  381. case "$GAMMAFILE" in
  382. *.icm|*.ICM|*.icc|*.ICC)
  383.     #
  384.     # Its really an .ICM file, not a gamma file.
  385.     #
  386.     # The file can be a full path name, or the name of a file in $SHARE/icm/
  387.     #
  388.     if [ "$COLOR" = "" ]; then
  389.     GAMMAFILE=
  390.     elif [ -r "$GAMMAFILE" ]; then
  391.     create_crd
  392.     elif [ -r "$SHARE/icm/$GAMMAFILE" ]; then
  393.     GAMMAFILE="$SHARE/icm/$GAMMAFILE"
  394.     create_crd
  395.     else
  396.     GAMMAFILE=
  397.     fi
  398.     ;;
  399. esac
  400.  
  401. #
  402. #    Use a Well Tempered Screen in quality mode 2.
  403. #       from Karl Putland <karl@putland.linux-site.net>
  404. #
  405. #    NOTE from Rick: Karl abandoned this approach.
  406. #
  407. create_wts() {
  408.     #
  409.     # Screen frequencies
  410.     #
  411.     C_FREQ="120"
  412.     M_FREQ="123.33"
  413.     Y_FREQ="126.85"
  414.     K_FREQ="143.22"
  415.  
  416.     case "$RES" in
  417.     600x600)    MUL=0.50;;
  418.     1200x600)    MUL=0.75;;
  419.     2400x600)    MUL=1.00;;
  420.     *)        MUL=0.50;;
  421.     esac
  422.  
  423.     cat > $ICCTMP.wts.ps <<-EOF
  424.     %!PS-Adobe-3.0
  425.     << /UseWTS true >> setuserparams
  426.     <<
  427.     /HalftoneType 5
  428.     /Cyan <<
  429.         /AccurateScreens true
  430.         /HalftoneType 1
  431.         /SpotFunction {
  432.         % 180 mul cos exch 180 mul cos add 2 div
  433.         abs exch abs 2 dup add 0.75 le
  434.             { 2 exp exch 2 exp add 1 exch sub }
  435.             { 2 dup add 1.23 le 
  436.             { exch 0.76 mul add 1 exch sub }
  437.             { 1 sub 2 exp exch 1 sub 2 exp add 1 sub }
  438.             ifelse
  439.             }
  440.             ifelse 
  441.         }
  442.         /TransferFunction {1 exp}
  443.         /Frequency $C_FREQ $MUL mul
  444.         /Angle 98
  445.     >>
  446.     /Magenta <<
  447.         /AccurateScreens true
  448.         /HalftoneType 1
  449.         /SpotFunction { 
  450.         % 180 mul cos exch 180 mul cos add 2 div
  451.         abs exch abs 2 dup add 0.75 le
  452.             { 2 exp exch 2 exp add 1 exch sub }
  453.             { 2 dup add 1.23 le 
  454.             { exch 0.76 mul add 1 exch sub }
  455.             { 1 sub 2 exp exch 1 sub 2 exp add 1 sub }
  456.             ifelse
  457.             }
  458.             ifelse 
  459.         } 
  460.         /TransferFunction {0.45 exp}
  461.         /Frequency $M_FREQ $MUL mul
  462.         /Angle 51.5
  463.     >>
  464.     /Yellow <<
  465.         /AccurateScreens true
  466.         /HalftoneType 1
  467.         /SpotFunction { 
  468.         % 180 mul cos exch 180 mul cos add 2 div
  469.         abs exch abs 2 dup add 0.75 le
  470.             { 2 exp exch 2 exp add 1 exch sub }
  471.             { 2 dup add 1.23 le 
  472.             { exch 0.76 mul add 1 exch sub }
  473.             { 1 sub 2 exp exch 1 sub 2 exp add 1 sub }
  474.             ifelse
  475.             }
  476.             ifelse 
  477.         } 
  478.         /TransferFunction {0.45 exp}
  479.         /Frequency $Y_FREQ $MUL mul
  480.         /Angle 27
  481.     >>
  482.     /Black <<
  483.         /AccurateScreens true
  484.         /HalftoneType 1
  485.         /SpotFunction { 
  486.         % 180 mul cos exch 180 mul cos add 2 div
  487.         abs exch abs 2 dup add 0.75 le
  488.             { 2 exp exch 2 exp add 1 exch sub }
  489.             { 2 dup add 1.23 le 
  490.             { exch 0.76 mul add 1 exch sub }
  491.             { 1 sub 2 exp exch 1 sub 2 exp add 1 sub }
  492.             ifelse
  493.             }
  494.             ifelse 
  495.         } 
  496.         /TransferFunction {0.60 exp}
  497.         /Frequency $K_FREQ $MUL mul
  498.         /Angle 75
  499.     >>
  500.     /Default <<
  501.         /AccurateScreens true
  502.         /HalftoneType 1
  503.         /SpotFunction { 
  504.         % 180 mul cos exch 180 mul cos add 2 div
  505.         abs exch abs 2 dup add 0.75 le
  506.         { 2 exp exch 2 exp add 1 exch sub }
  507.         { 2 dup add 1.23 le 
  508.             { exch 0.76 mul add 1 exch sub }
  509.             { 1 sub 2 exp exch 1 sub 2 exp add 1 sub }
  510.             ifelse
  511.         }
  512.         ifelse 
  513.         } 
  514.         /TransferFunction {0.45 exp}
  515.         /Frequency $Y_FREQ $MUL mul
  516.         /Angle 27
  517.     >>
  518.     >> sethalftone
  519.     EOF
  520.  
  521.     GAMMAFILE="$GAMMAFILE $ICCTMP.wts.ps"
  522. }
  523.  
  524. if [ "$COLOR" != "" -a "$QUALITY" = 2 ]; then
  525.     create_wts
  526. fi
  527.  
  528. #
  529. #    Figure out USERNAME
  530. #
  531. if [ "$LPUSER" != "" ]; then
  532.     USER="$LPUSER@$LPHOST"
  533. else
  534.     USER=""
  535. fi
  536.  
  537. #
  538. #    Main Program, just cobble together the pipeline and run it
  539. #
  540. #    The malarky with file descriptors 1 and 3 is to avoid a bug in
  541. #    (some versions?) of Ghostscript where Postscript's stdout gets
  542. #    intermingled with the printer drivers output, resulting in
  543. #    corrupted image data.
  544. #
  545. GS="$GSBIN -q -dBATCH -dSAFER -dQUIET -dNOPAUSE"
  546.  
  547. $PREFILTER \
  548. | ($GS $PAPERSIZE -g$DIM -r$RES $GSDEV $GSOPTS \
  549.     -sOutputFile="|cat 1>&3" $GAMMAFILE - >/dev/null 2>/dev/null) 3>&1 \
  550. | dbgcmd foo2oak -r$RES -g$DIM -p$PAPER -m$MEDIA -n$COPIES -d$DUPLEX -s$SOURCE \
  551.         $COLOR -b$BPP $CLIP_UL $CLIP_LR $CLIP_LOG \
  552.         -J "$LPJOB" -U "$USER" \
  553.         $BC $AIB $COLOR2MONO -D$DEBUG
  554.  
  555. #
  556. #    Log the command line, for debugging and problem reports
  557. #
  558. if [ $DEBUG = 0 -a -x /usr/bin/logger ]; then
  559.     logger -t "$BASENAME" -p lpr.info -- \
  560.     "gs $PAPERSIZE -g$DIM -r$RES $GSDEV $GSOPT"
  561.     logger -t "$BASENAME" -p lpr.info -- \
  562.     "foo2oak -r$RES -g$DIM -p$PAPER -m$MEDIA \
  563. -n$COPIES -d$DUPLEX -s$SOURCE $COLOR -b$BPP $CLIP_UL $CLIP_LR $CLIP_LOG \
  564. $BC $AIB $COLOR2MONO"
  565. fi
  566.  
  567. #
  568. #    Remove cruft
  569. #
  570. if [ $DEBUG -eq 0 ]; then
  571.     for i in crd.ps log usecie.ps selcrd.ps wts.ps
  572.     do
  573.     file="$ICCTMP.$i"
  574.     [ -f $file ] && rm -f $file
  575.     done
  576. fi
  577.  
  578. exit 0
  579.