home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2191 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  6.7 KB

  1. From: les@chinet.chi.il.us (Leslie Mikesell)
  2. Newsgroups: comp.lang.perl,alt.sources
  3. Subject: nametag.pl
  4. Message-ID: <1990Dec04.185336.4946@chinet.chi.il.us>
  5. Date: 4 Dec 90 18:53:36 GMT
  6.  
  7.  
  8. I haven't been able to find a generic mail-merge routine that forces
  9. the merged text to fit into certain spaces, so I wrote this...
  10.  
  11. This program generates postscript code for nametags with the text
  12. centered and scaled to fit into the appropriate spots (for our
  13. nametag stock).  I normally run it as part of a spooler printer
  14. interface program, but it can be run as a stand-alone operation.
  15.  
  16. Bugs:
  17. It should probably read a parameter file to get the positioning info,
  18. but with an interpreted language it is just as easy to edit the
  19. script - at least for the person who wrote it.
  20. The whole operation could doubtlessly be done in postscript
  21. by someone with a stack-oriented brain.  In retrospect it was a bad
  22. idea to try to hit a pre-printed bar exactly (although the color
  23. does give a nice effect).  I've had to adjust the $YSLIDE variable
  24. as much as 12 points to compensate for different printers.
  25. ---
  26. Les Mikesell
  27.   les@chinet.chi.il.us   or  les@fb.com
  28.  
  29. # ---- cut here --------
  30. # nametag.pl
  31. # Perl program to generate postscript program....
  32. # execute with:
  33. # perl nametag.pl file...
  34. #    or pass the input on stdin - output is to stdout
  35. # This is set up for pre-printed nametag stock 2 across, 3 down
  36. # with a logo at the top and a red bar near the bottom on each, 
  37. # but it should work to change the constants for other layouts.
  38. #
  39. # print nametags from files in format:
  40. # line1|line2|line3|line4|line5
  41. # ...  (each line contains the data for one nametag, up to 5 lines
  42. #       using "|" as the delimiter for lines on the tag)
  43. #  
  44. # if line2 is empty, line3 moves up
  45. # line4 prints in 14 pt italic just above red bar
  46. # line5 lands on red bar (red bar refers to a position near the bottom)
  47. #
  48. #  blank lines are ignored
  49. #  a form-feed in the file ejects the current page (for easier
  50. #    grouping for distribution)
  51. #  otherwise pages are filled (6/page) until the end of file is reached
  52. #
  53. #  Procedure tline:
  54. #   Fonts are selected by first attempting ch1 (choice 1 below) in its
  55. #   specified size.  If it is too wide, an attempt is made using ch2.
  56. #   If it still doesn't fit, ch2 is rescaled to make it fit.
  57. #   Iline does the same with fonts ch3 and ch4 for italics.
  58. # starting point size is imbedded in the perl code:
  59. # 24pt for lines 1,2,3, 14 pt for line 4, 20 pt for line 5
  60.  
  61. # ---- positioning info -----
  62. # column layout
  63. $COLS=2 ;
  64. $ROWS=3 ;
  65. # -- these numbers are postscript units --
  66. # -- adjust this number for vertical allignment --
  67. $YSLIDE= 1; # may need to tweak for printer
  68.  
  69. # -- starting points -- 
  70. $X1=25;    # indent to 1st printing posn
  71. $Y1=687 + $YSLIDE ;   # top line vertical posn
  72. $Y4=601 + $YSLIDE ;   # above red bar
  73. $Y5=572 + $YSLIDE ;   # red bar
  74. $DX=286;   # add for 2nd col
  75. $DY=264;   # sub for 2nd, 3rd rows
  76. $LINE=30;  # sub for 2nd,3rd lines
  77. $WIDTH = $DX -10; # max space to fill
  78. # ----
  79. # This is the postscript header with some definitions
  80. # -- ch1 def sets first font choice, ch2 is used if 1st attempt is too wide
  81. # -- likewise ch3 & ch4 for italics
  82. $HD='%!
  83. % 1st, 2nd tries for tline
  84. /ch1 { /Helvetica-Bold findfont exch scalefont setfont} def
  85. /ch2 { /Helvetica-Narrow-Bold findfont exch scalefont setfont} def
  86. % 1st, 2nd tries for iline
  87. /ch3 { /Helvetica-BoldOblique findfont exch scalefont setfont} def
  88. /ch4 { /Helvetica-BoldOblique findfont exch scalefont setfont} def
  89. %  x y width string max-pts
  90. % pick a font that lets string fit in width (ch1, ch2 pair)
  91. % center within the space
  92. /tline
  93.  {
  94.   /pts exch def
  95.   /str exch def
  96.   /wd exch def
  97.   /y1 exch def
  98.   /x1 exch def
  99. % try 1st choice
  100.    pts ch1
  101.    str stringwidth pop wd gt { 
  102.        % did not fit - try 2nd choice font
  103.        pts ch2
  104.        /sw str stringwidth pop def
  105.        sw wd gt  {
  106.        % still did not fit - rescale
  107.            currentfont wd sw div scalefont setfont  
  108.        }if
  109.     }if
  110.     % center within space and print
  111.     wd str stringwidth pop sub 2 div x1 add y1 moveto str show
  112.     }def
  113.  
  114. %  x y width string max-pts
  115. % same w/italic font (ch3, ch4 choices)
  116. % center within the space
  117. /iline
  118.  {
  119.   /pts exch def
  120.   /str exch def
  121.   /wd exch def
  122.   /y1 exch def
  123.   /x1 exch def
  124. % try 1st choice
  125.    pts ch3
  126.    str stringwidth pop wd gt { 
  127.        % did not fit - try 2nd choice font
  128.        pts ch4
  129.        /sw str stringwidth pop def
  130.        sw wd gt  {
  131.        % still did not fit - rescale
  132.            currentfont wd sw div scalefont setfont  
  133.        }if
  134.     }if
  135.     % center within space and print
  136.     wd str stringwidth pop sub 2 div x1 add y1 moveto str show
  137.     }def
  138.  
  139. ';
  140.  
  141. $CCOL=-1;
  142. $CROW=0;
  143.  
  144. printf "%s",$HD;  #postscript program
  145. $NEWPAGE=1;
  146. $NPAGE=0;
  147. while (<>) {      # read input line 
  148.     if (/\f/) {   # go to different page
  149.         $NEWPAGE = 1;  
  150.         s/\f//;   # likely not to be a newline after formfeed
  151.     }
  152.     s/\r//;       # ignore carriage returns
  153.     chop;         # remove linefeed
  154.     if (/^ *$/) { next;}  # skip blank lines
  155.     # "(" in text must be "\(" in postscript output
  156.     s/\\/\\\\/g;    # I hate it when this happens...
  157.     s/\(/\\(/g; 
  158.     s/\)/\\)/g;
  159. #
  160. #    print $CCOL;   #..for debugging only
  161. #    print "\n";
  162. #    print $CROW;
  163. #    print "\n";
  164.     if ($NEWPAGE > 0) {   # starting a page
  165.       $CCOL = $COLS -1;
  166.       $CROW = $ROWS -1;
  167.     }
  168.     if (++$CCOL == $COLS) {
  169.         $CCOL = 0;
  170.         if (++$CROW == $ROWS) {
  171.         $CROW = 0;
  172.         if ($NPAGE++ > 0 ) {
  173.             printf "%s\n","showpage";      # eject previous page
  174.         }
  175.             printf "%% page %d\n",$NPAGE;  # sometimes handy to know
  176.         $NEWPAGE = 0;
  177.         }
  178.     }
  179.     # calc x y coordinates for this one
  180.     $LX = $X1 + $CCOL * $DX;
  181.     $LY1 = $Y1 - $CROW * $DY;
  182.     $LY2 = $LY1 - $LINE;
  183.     $LY3 = $LY2 - $LINE;
  184.     $LY4 = $Y4 - $CROW * $DY;
  185.     $LY5 = $Y5 - $CROW * $DY;
  186.     # break item into lines 
  187.      @LINE=split(/\|/);
  188.     if ($LINE[1] eq "" ) {
  189.         $LY3 = $LY2; # close up if line 2 is blank
  190.     }
  191.     # print the non-blank lines - the number is the initial point size
  192.     # tline uses helvetica, iline is italic
  193.     if ($LINE[0] ne "" ) {
  194.         printf "%d %d %d (%s) 24 tline \n",$LX,$LY1,$WIDTH,$LINE[0] ;
  195.     }
  196.     if ($LINE[1] ne "" ) {
  197.         printf "%d %d %d (%s) 24 tline \n",$LX,$LY2,$WIDTH,$LINE[1] ;
  198.     }
  199.     if ($LINE[2] ne "" ) {
  200.         printf "%d %d %d (%s) 24 tline \n",$LX,$LY3,$WIDTH,$LINE[2] ;
  201.     }
  202.     if ($LINE[3] ne "" ) {
  203.         printf "%d %d %d (%s) 14 iline \n",$LX,$LY4,$WIDTH,$LINE[3] ;
  204.     }
  205.     if ($LINE[4] ne "" ) {
  206.         printf "%d %d %d (%s) 20 tline \n",$LX,$LY5,$WIDTH,$LINE[4] ;
  207.     }
  208. }
  209.  
  210. printf "%s\n%% end of page %d\n","showpage",$NPAGE;
  211. # you may or may not need a control-D here, depending on whether
  212. # or not something else handles postscript EOF
  213. # uncomment the next line if you need it
  214. # print "\004";
  215.