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

  1. #!/bin/sh
  2.  
  3. #* Copyright (C) 2005  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: foo2hp2600-wrapper.in,v 1.25 2006/01/13 04:51:08 rick Exp $'
  22.  
  23. #
  24. # Printer Notes:
  25. #
  26. # hp2600 -
  27. #
  28.  
  29. PROGNAME="$0"
  30. BASENAME=`basename $PROGNAME`
  31. PREFIX=/usr
  32. SHARE=$PREFIX/share/foo2hp
  33.  
  34. #
  35. #    Log the command line, for debugging and problem reports
  36. #
  37. if [ -x /usr/bin/logger ]; then
  38.     logger -t "$BASENAME" -p lpr.info -- "foo2hp2600-wrapper $@" </dev/null
  39. fi
  40.  
  41. usage() {
  42.     cat <<EOF
  43. Usage:
  44.     $BASENAME [options] [ps-file]
  45.  
  46.     Foomatic printer wrapper for the foo2hp2600 printer driver.
  47.     This script reads a Postscript ps-file or standard input
  48.     and converts it to Zenographics ZjStream printer format.
  49.  
  50. Normal Options:
  51. -b bits           Bits per plane (1 or 2) [$BPP]
  52. -c                Print in color (else monochrome)
  53. -d duplex         Duplex code to send to printer [$DUPLEX]
  54.                     1=off, 2=longedge, 3=shortedge
  55. -m media          Media code to send to printer [$MEDIA]
  56.                     1=standard, 2=transparency, 3=glossy, 257=envelope,
  57.                     259=letterhead, 261=thickstock, 262=postcard, 263=labels
  58. -p paper          Paper code [$PAPER]
  59.                     1=letter, 5=legal, 7=executive, 9=A4, 11=A5, 13=B5
  60. -n copies         Number of copies [$COPIES]
  61. -r <xres>x<yres>  Set device resolution in pixels/inch [$RES]
  62. -s source         Source code to send to printer [$SOURCE]
  63.                     1=upper, 2=lower, 4=manual, 7=auto
  64.             Code numbers may vary with printer model.
  65. -t                Draft mode.  Every other pixel is white.
  66. -2 / -4           2-up, 4-up
  67.  
  68. Printer Tweaking Options:
  69. -u <xoff>x<yoff>  Set offset of upper left printable in pixels [varies]
  70. -l <xoff>x<yoff>  Set offset of lower right printable in pixels [varies]
  71. -L mask           Send logical clipping values from -u/-l in ZjStream [3]
  72.                   0=no, 1=Y, 2=X, 3=XY
  73. -P                Do not output START_PLANE codes.  May be needed by some
  74.                   monochrome-only printers.
  75. -X padlen         Add extra zero padding to the end of BID segments [16]
  76.  
  77. Color Tweaking Options:
  78. -g gsopts         Additional options to pass to Ghostscript, such as
  79.                   -dDITHERPPI=nnn, etc.  May appear more than once. []
  80. -G profile.icm    Convert profile.icm to a Postscript CRD using icc2ps and
  81.                   adjust colors using the setcolorrendering PS operator.
  82.                   $SHARE/icm/ will be searched for profile.icm.
  83. -I intent         Select profile intent from ICM file [$INTENT]
  84.                   0=Perceptual, 1=Colorimetric, 2=Saturation, 3=Absolute
  85. -G gamma-file.ps  Prepend gamma-file to the Postscript input to perform
  86.                   color correction using the setcolortransfer PS operator.
  87.  
  88. Debugging Options:
  89. -S plane          Output just a single color plane from a color print [all]
  90.                   1=Cyan, 2=Magenta, 3=Yellow, 4=Black
  91. -D lvl            Set Debug level [$DEBUG]
  92. -V                $VERSION
  93. EOF
  94.  
  95.     exit 1
  96. }
  97.  
  98. #
  99. #       Report an error and exit
  100. #
  101. error() {
  102.     echo "$BASENAME: $1" >&2
  103.     exit 1
  104. }
  105.  
  106. dbgcmd() {
  107.     if [ $DEBUG -ge 1 ]; then
  108.         echo "$@" >&2
  109.     fi
  110.     "$@"
  111. }
  112.  
  113. #
  114. # Portable version of 'which'
  115. #
  116. pathfind() {
  117.     if [ "$1" = -p ]; then
  118.     optp=1
  119.     shift
  120.     else
  121.     optp=0
  122.     fi
  123.     OLDIFS="$IFS"
  124.     IFS=:
  125.     for p in $PATH; do
  126.     if [ -x "$p/$*" ]; then
  127.         if [ $optp = 1 ]; then
  128.         echo "$p/$*"
  129.         fi
  130.         IFS="$OLDIFS"
  131.         return 0
  132.     fi
  133.     done
  134.     IFS="$OLDIFS"
  135.     return 1
  136. }
  137.  
  138. #
  139. # Returns true if $1 is 32-bit binary
  140. #
  141. is32() {
  142.     if pathfind file; then
  143.     path=`pathfind -p "$*"`
  144.     is32=`file -L "$path" | grep 32-bit` 
  145.     if [ "$is32" = "" ]; then
  146.         return 1
  147.     else
  148.         return 0
  149.     fi
  150.     else
  151.     return 1
  152.     fi
  153. }
  154.  
  155. #
  156. #    N-up-ify the job.  Requires psnup from psutils package
  157. #
  158. nup2() {
  159.     tr '\r' '\n' | psnup -d2 -2 -m.3in -p$paper -q
  160. }
  161.  
  162. nup4() {
  163.     tr '\r' '\n' | psnup -d2 -4 -m.5in -p$paper -q
  164. }
  165.  
  166. #
  167. #       Process the options
  168. #
  169.  
  170. # Try to use a local copy of GhostScript 8.14, if available.  Otherwise,
  171. # fallback to whatever the Linux distro has installed (usually 7.05)
  172. #
  173. # N.B. := operator used here, when :- would be better, because "ash"
  174. # doesn't have :-
  175. if gs8 -v >/dev/null 2>&1; then
  176.         GSBIN=${GSBIN:-gs8}
  177. else
  178.         GSBIN=${GSBIN:-gs}
  179. fi
  180.  
  181. CMDLINE="$*"
  182. DEBUG=0
  183. DUPLEX=1
  184. BPP=1
  185. COLOR=
  186. COLORMODE=default
  187. QUALITY=1
  188.  
  189. MEDIA=1
  190. COPIES=1
  191. test -r /etc/papersize && PAPER=$(cat /etc/papersize)
  192. test "$PAPER" || PAPER=1
  193. RES=600x600
  194. SOURCE=7
  195. NUP=
  196. CLIP_UL=
  197. CLIP_LR=
  198. CLIP_LOG=
  199. BC=
  200. AIB=
  201. NOPLANES=
  202. COLOR2MONO=
  203. GAMMAFILE=default
  204. INTENT=0
  205. GSOPTS=
  206. EXTRAPAD=
  207. SAVETONER=
  208. GSDEV=-sDEVICE=pbmraw
  209. DefaultRGB=sRGB
  210. # What mode to use if the user wants us to pick the "best" mode
  211. case `$GSBIN --version` in
  212. 7*)
  213.     DEFAULTCOLORMODE=10
  214.     ;;
  215. *)
  216.     DEFAULTCOLORMODE=10
  217.     ;;
  218. esac
  219.  
  220. while getopts "24b:cC:d:g:l:u:L:m:n:p:q:r:s:tABS:D:G:I:PX:Vh?" opt
  221. do
  222.     case $opt in
  223.     b)    BPP="$OPTARG";;
  224.     c)    COLOR=-c;;
  225.     d)    DUPLEX="$OPTARG";;
  226.     g)    GSOPTS="$GSOPTS $OPTARG";;
  227.     m)    MEDIA="$OPTARG";;
  228.     n)    COPIES="$OPTARG";;
  229.     p)    PAPER="$OPTARG";;
  230.     q)    QUALITY="$OPTARG";;
  231.     r)    RES="$OPTARG";;
  232.     s)    SOURCE="$OPTARG";;
  233.     t)    SAVETONER="-t";;
  234.     l)    CLIP_LR="-l $OPTARG";;
  235.     u)    CLIP_UL="-u $OPTARG";;
  236.     L)    CLIP_LOG="-L $OPTARG";;
  237.     A)    AIB=-A;;
  238.     B)    BC=-B;;
  239.     C)    COLORMODE="$OPTARG";;
  240.     S)    COLOR2MONO="-S$OPTARG";;
  241.     D)    DEBUG="$OPTARG";;
  242.     G)    GAMMAFILE="$OPTARG";;
  243.     I)    INTENT="$OPTARG";;
  244.     P)    NOPLANES=-P;;
  245.     X)    EXTRAPAD="-X $OPTARG";;
  246.     2)    NUP="2";;
  247.     4)    NUP="4";;
  248.     V)    echo "$VERSION"; foo2hp -V; exit 0;;
  249.     h|\?)
  250.         if [ "$CMDLINE" != "-?" -a "$CMDLINE" != -h ]; then
  251.             echo "Illegal command:"
  252.             echo "    $0 $CMDLINE"
  253.             echo
  254.         fi
  255.         usage;;
  256.     esac
  257. done
  258. shift `expr $OPTIND - 1`
  259.  
  260. #
  261. #    Select the ghostscript device to use
  262. #
  263. case "$BPP" in
  264. 1)    if [ "" = "$COLOR" ]; then
  265.         GSDEV=-sDEVICE=pbmraw
  266.     else
  267.         GSDEV=-sDEVICE=bitcmyk
  268.     fi
  269.     case "$GAMMAFILE" in
  270.     default)
  271.         if is32 $GSBIN; then
  272.         GAMMAFILE=km2430_2.icm
  273.         else
  274.         GAMMAFILE=hpclj2600n.icm
  275.         fi
  276.         ;;
  277.     none) GAMMAFILE=;;
  278.     esac
  279.     ;;
  280. 2)    if [ "" = "$COLOR" ];
  281.     then
  282.         # GSDEV=-sDEVICE=pgmraw
  283.         # error "2-bpp monochrome is not yet supported"
  284.         GSDEV="-sDEVICE=cups -dcupsColorSpace=3 -dcupsBitsPerColor=2"
  285.         GSDEV="$GSDEV -dcupsColorOrder=2"
  286.     else
  287.         GSDEV="-sDEVICE=cups -dcupsColorSpace=6 -dcupsBitsPerColor=2"
  288.         GSDEV="$GSDEV -dcupsColorOrder=2"
  289.     fi
  290.  
  291.     case "$GAMMAFILE" in
  292.     default) 
  293.         if is32 $GSBIN; then
  294.         GAMMAFILE=km2430_2.icm
  295.         else
  296.         GAMMAFILE=
  297.         fi
  298.         ;;
  299.     none) GAMMAFILE=;;
  300.     esac
  301.     ;;
  302. *)    error "Illegal number of bits per plane ($BPP)";;
  303. esac
  304.  
  305. #
  306. case "$QUALITY" in
  307. 0)
  308.     GSOPTS="-dCOLORSCREEN $GSOPTS"
  309.     ;;
  310. 1)
  311.     GSOPTS="-dCOLORSCREEN $GSOPTS"
  312.     ;;
  313. 2)
  314.     GSOPTS="-dMaxBitMap=500000000 $GSOPTS"
  315.     ;;
  316. esac
  317.  
  318. #
  319. #    Validate media code
  320. #
  321. case "$MEDIA" in
  322. 1|plain)    MEDIA=1;;
  323. 514|preprinted)    MEDIA=514;;
  324. 513|letterhead)    MEDIA=513;;
  325. 2|transparency)    MEDIA=2;;
  326. 515|prepunched)    MEDIA=515;;
  327. 265|labels)    MEDIA=265;;
  328. 260|bond)    MEDIA=260;;
  329. 516|recycled)    MEDIA=516;;
  330. 512|color)    MEDIA=512;;
  331. 276|tough)    MEDIA=276;;
  332. 267|envelope)    MEDIA=267;;
  333. 258|light)    MEDIA=258;;
  334. 262|heavy)    MEDIA=262;;
  335. 261|cardstock)    MEDIA=261;;
  336. 268|lightglossy)    MEDIA=268;;
  337. 269|glossy)    MEDIA=269;;
  338. 270|heavyglossy)    MEDIA=270;;
  339. 277|cover)    MEDIA=277;;
  340. 278|photo)    MEDIA=278;;
  341. [0-9]*)        ;;
  342. *)        error "Unknown media code $MEDIA";;
  343. esac
  344.  
  345. #
  346. #    Validate source (InputSlot) code
  347. #
  348. case "$SOURCE" in
  349. 1|tray2)    SOURCE=1;;
  350. 4|tray1)    SOURCE=4;;
  351. 7|auto)        SOURCE=7;;
  352. [0-9]*)        ;;
  353. *)        error "Unknown source code $SOURCE";;
  354. esac
  355.  
  356. #
  357. #    Validate Duplex code
  358. #
  359. case "$DUPLEX" in
  360. 1|off|none)    DUPLEX=1;;
  361. 2|long*)    DUPLEX=2;;
  362. 3|short*)    DUPLEX=3;;
  363. [0-9]*)        ;;
  364. *)        error "Unknown duplex code $DUPLEX";;
  365. esac
  366.  
  367. #
  368. #    Validate Resolution
  369. #
  370. case "$RES" in
  371. 600x600)    ;;
  372. 1200x600)    ;;
  373. 2400x600)    ;;
  374. *)        error "Illegal resolution $RES";;
  375. esac
  376.  
  377. #
  378. #    Figure out the paper dimensions in pixels/inch, and set the
  379. #    default clipping region.  Unfortunately, this is a trouble
  380. #    area for ZjStream printers.  Various versions of ZjS print
  381. #    engines react differently when asked to print into their
  382. #    unprintable regions.
  383. #
  384. set_clipping() {
  385.     ulx=$1; uly=$2
  386.     lrx=$3; lry=$4
  387.  
  388.     # Set clipping region if it isn't already set
  389.     if [ "$CLIP_UL" = "" ]; then
  390.     case "$RES" in
  391.     600x600)    ulx=`expr $ulx / 2`;;
  392.     2400x600)    ulx=`expr $ulx \* 2`;;
  393.     esac
  394.     CLIP_UL="-u ${ulx}x${uly}"
  395.     fi
  396.     if [ "$CLIP_LR" = "" ]; then
  397.     case "$RES" in
  398.     600x600)    lrx=`expr $lrx / 2`;;
  399.     2400x600)    lrx=`expr $lrx \* 2`;;
  400.     esac
  401.     CLIP_LR="-l ${lrx}x${lry}"
  402.     fi
  403. }
  404.  
  405. case "$PAPER" in
  406. 1|letter)    PAPER=1;    paper=letter;    XDIM="10200"; YDIM="6600"
  407.         set_clipping 172 80    172 80
  408.         # set_clipping 86 40    258 120
  409.         ;;
  410. 5|legal)    PAPER=5;    paper=legal;     XDIM="10200"; YDIM="8400"
  411.         set_clipping 172 80    172 80
  412.         # set_clipping 86 40    258 120
  413.         ;;
  414. 7|executive)    PAPER=7;    paper=executive; XDIM="8700";  YDIM="6300"
  415.         set_clipping 174 78    174 78
  416.         # set_clipping 86 40    262 116
  417.         ;;
  418. 9|a4|A4)    PAPER=9;    paper=a4;        XDIM="9920";  YDIM="7016"
  419.         set_clipping 176 84    176 84
  420.         # set_clipping 88 42    264 126
  421.         ;;
  422. 11|a5|A5)    PAPER=11;    paper=a5;        XDIM="6992";  YDIM="4960"
  423.         set_clipping 176 80    176 80
  424.         # set_clipping 88 40    264 120
  425.         ;;
  426. 13|b5|B5|b5jis)    PAPER=13;    paper=b5;        XDIM="8598";  YDIM="6070"
  427.         set_clipping 172 83    171 83
  428.         # set_clipping 86 41    257 125
  429.         ;;
  430. *)        error "Unimplemented paper code $PAPER";;
  431. esac
  432. PAPERSIZE="-sPAPERSIZE=$paper";
  433.  
  434. case "$RES" in
  435. 600x600)    XDIM=`expr $XDIM / 2`;;
  436. 1200x600)    ;;
  437. 2400x600)    XDIM=`expr $XDIM \* 2`;;
  438. esac
  439. DIM="${XDIM}x${YDIM}"
  440.  
  441. #
  442. # If there is an argument left, take it as the file to print.
  443. # Else, the input comes from stdin.
  444. #
  445. if [ $# -ge 1 ]; then
  446.     if [ "$LPJOB" = "" ]; then
  447.     : # LPJOB="$1"
  448.     fi
  449.     exec < $1
  450. fi
  451.  
  452. #
  453. # Filter thru psnup if N-up printing has been requested
  454. #
  455. case $NUP in
  456. 2)    PREFILTER="nup2";;
  457. 4)    PREFILTER="nup4";;
  458. *)    PREFILTER=cat;;
  459. esac
  460.  
  461. #
  462. #    Overload -G.  If the file name ends with ".icm" or ".ICM"
  463. #    then convert the ICC color profile to a Postscript CRD,
  464. #    then prepend it to the users job.  Select the intent
  465. #    using the -I option.
  466. #
  467.  
  468. create_crd() {
  469.     #
  470.     # Create a Postscript CRD
  471.     #
  472.     ICC2PS=$PREFIX/bin/foo2zjs-icc2ps
  473.     if [ -x $ICC2PS ]; then
  474.     $ICC2PS -o $GAMMAFILE -t$INTENT > $ICCTMP.crd.ps 2>$ICCTMP.log \
  475.     || error "Problem converting .ICM file to Postscript"
  476.     cat > $ICCTMP.usecie.ps <<-EOF
  477.         %!PS-Adobe-3.0
  478.         <</UseCIEColor true>>setpagedevice
  479.     EOF
  480.     cat > $ICCTMP.selcrd.ps <<-EOF
  481.         /Current /ColorRendering findresource setcolorrendering
  482.     EOF
  483.     GAMMAFILE="$ICCTMP.usecie.ps $ICCTMP.crd.ps $ICCTMP.selcrd.ps"
  484.     else
  485.     GAMMFILE=
  486.     fi
  487. }
  488.  
  489. if [ $DEBUG -gt 0 ]; then
  490.     ICCTMP=/tmp/icc
  491. else
  492.     ICCTMP=/tmp/icc$$
  493. fi
  494.  
  495. if [ "" = "$COLOR" ]; then
  496.     COLORMODE=
  497.     GAMMAFILE=
  498. else
  499.     case "$COLORMODE" in
  500.     default)    COLORMODE=$DEFAULTCOLORMODE;;
  501.     esac
  502. fi
  503.  
  504. CRDBASE="$PREFIX/share/foo2zjs/crd"
  505. PSFILES="$PREFIX/share/foo2hp/psfiles"
  506. case "$RES" in
  507.     600x600)    SCREEN=screen1200.ps;;
  508.     1200x600)    SCREEN=screen1200.ps;;
  509.     2400x600)    SCREEN=screen2400.ps;;
  510. esac
  511.  
  512. case "$COLORMODE" in
  513. 0|"")
  514.     # Monochrome
  515.     ;;
  516. 10|icm)
  517.     # Use old ICM method
  518.     AIB=-A
  519.     BC=-B
  520.     case "$GAMMAFILE" in
  521.     *.icm|*.ICM|*.icc|*.ICC)
  522.     #
  523.     # Its really an .ICM file, not a gamma file.
  524.     #
  525.     # The file can be a full path name, or the name of a file in $SHARE/icm/
  526.     #
  527.     if [ -r "$GAMMAFILE" ]; then
  528.         create_crd
  529.     elif [ -r "$SHARE/icm/$GAMMAFILE" ]; then
  530.         GAMMAFILE="$SHARE/icm/$GAMMAFILE"
  531.         create_crd
  532.     else
  533.         GAMMAFILE=
  534.     fi
  535.     ;;
  536.     esac
  537.     ;;
  538. *.crd)
  539.     GAMMAFILE="$CRDBASE/prolog.ps"
  540.     if [ -f $COLORMODE ]; then
  541.     GAMMAFILE="$GAMMAFILE $COLORMODE"
  542.     elif [ -f $CRDBASE/$COLORMODE ]; then
  543.     GAMMAFILE="$GAMMAFILE $CRDBASE/$COLORMODE"
  544.     else
  545.     error "Can't find CRD '$COLORMODE' in . or in $CRDBASE"
  546.     fi
  547.     GAMMAFILE="$GAMMAFILE $CRDBASE/$SCREEN"
  548.     ;;
  549. *)
  550.     error "Unknown color method '$COLORMODE'"
  551.     ;;
  552. esac
  553.  
  554. if [ "" != "$COLOR" ]; then
  555.     if [ "" = "$AIB" -a "" = "$BC" ]; then
  556.     # Faster, but can't handle AllIsBlack or BlackClears
  557.     : #GSDEV=-sDEVICE=pksmraw
  558.     else
  559.     # Can't handle different size pages
  560.     : #GSDEV=-sDEVICE=bitcmyk
  561.     fi
  562. fi
  563.  
  564. #
  565. #    Figure out USERNAME
  566. #
  567. if [ "$LPUSER" != "" ]; then
  568.     USER="$LPUSER@$LPHOST"
  569. else
  570.     USER=""
  571. fi
  572.  
  573. #
  574. #    Main Program, just cobble together the pipeline and run it
  575. #
  576. #    The malarky with file descriptors 1 and 3 is to avoid a bug in
  577. #    (some versions?) of Ghostscript where Postscript's stdout gets
  578. #    intermingled with the printer drivers output, resulting in
  579. #    corrupted image data.
  580. #
  581. GS="$GSBIN -q -dBATCH -dSAFER -dQUIET -dNOPAUSE"
  582.  
  583. $PREFILTER \
  584. | ($GS $PAPERSIZE -g$DIM -r$RES $GSDEV $GSOPTS \
  585.     -sOutputFile="|cat 1>&3" $GAMMAFILE - >/dev/null 2>&1) 3>&1 \
  586. | foo2hp -r$RES -g$DIM -p$PAPER -m$MEDIA -n$COPIES -d$DUPLEX -s$SOURCE \
  587.         $COLOR -b$BPP $CLIP_UL $CLIP_LR $CLIP_LOG $SAVETONER \
  588.         -J "$LPJOB" -U "$USER" \
  589.         $BC $AIB $COLOR2MONO $NOPLANES $EXTRAPAD -D$DEBUG
  590.  
  591. #
  592. #    Log the command line, for debugging and problem reports
  593. #
  594. if [ -x /usr/bin/logger ]; then
  595.     logger -t "$BASENAME" -p lpr.info -- \
  596.     "$GSBIN $PAPERSIZE -g$DIM -r$RES $GSDEV $GSOPT $GAMAFILE"
  597.     logger -t "$BASENAME" -p lpr.info -- \
  598.     "foo2hp -r$RES -g$DIM -p$PAPER -m$MEDIA \
  599. -n$COPIES -d$DUPLEX -s$SOURCE $COLOR -b$BPP $CLIP_UL $CLIP_LR $CLIP_LOG \
  600. $SAVETONER $BC $AIB $COLOR2MONO $NOPLANES $EXTRAPAD"
  601. fi
  602.  
  603. #
  604. #    Remove cruft
  605. #
  606. if [ $DEBUG -eq 0 ]; then
  607.     for i in crd.ps log usecie.ps selcrd.ps
  608.     do
  609.     file="$ICCTMP.$i"
  610.     [ -f $file ] && rm -f $file
  611.     done
  612. fi
  613.  
  614. exit 0
  615.