%!
% config.a4grid
% Configuration and header file for dvips. Load with: dvips -P a4grid
%
% Draw a line grid in millimetres, starting from the top-left of the page.
% The resulting grid can be used for measuring page layout.
%
% This code is based on code from the book
%
%	@Book{Merz:PostscriptAcrobatBibel,
%	  author =       "Thomas Merz",
%	  title =        "Die PostScript- und Acrobat-Bibel. Was Sie schon
%	  		 immer {\"u}ber PostScript und Acrobat/{PDF} wissen
%			 wollten",
%	  publisher =    "Thomas Merz Verlag",
%	  year =         "1996",
%	  address =      "M{\"u}nchen",
%	  edition =      "1",
%	  ISBN =         "3-9804943-0-6",
%	}
%	@Book{Merz:PostscriptAcrobatBible,
%	  author =       "Thomas Merz",
%	  title =        "PostScript & acrobat/{PDF}: applications,
%                	 troubleshooting, and cross-platform publishing",
%	  publisher =    "Springer",
%	  year =         "1997",
%	  address =      "Berlin",
%	  ISBN =         "3-540-60854-0",
%	}
%
% The original code can be found in file ps/eps/gitter.ps on the CD which
% accompanies the German edition.
% The original code is copyright (C) Thomas Merz 1994-96.
% Incorporation of the original code into this file is with permission of
% Thomas Merz.
% For further information about the book see http://www.pdflib.com/
%
% This file is built so it can be used both as the header file for
% PostScript and as the configuration file for dvips.
% (Note that dvips ignores any lines beginning with a space or a "%".)
%
% Copyright (C) 1995-2006 by Volker Kuhlmann.
% Released under the terms of the GNU General Public License (GPL) Version 2.
% See http://www.gnu.org/ for details.
%
% Volker Kuhlmann <VolkerKuhlmann@gmx.de>
%   25, 26 Oct 1997; 25 Jan 1998; 9 Mar 2000; 11 May 2006
%
 /dr@wgrid {
 
 save
 25 dict begin
 
 % usable units:
 /mm {25.4 div 72 mul} bind def
 
 % Paper height
 /grid@paperheight 297 mm def	% A4
 
 % Start Y scale from the top, down.
 0 grid@paperheight translate
 
 /gridunit  1 mm def		% the grid unit, in PostScript points
 /step      5 def		% grid line increment, in grid units
 /medstep  10 def		% solid grid lines every this many grid units
 /numstep  20 def		% numbered grid lines every this many grid units
 
 /FontSize 10 def		% font size of numbers
 
 /numstepunit numstep gridunit mul def
 
 %initgraphics % don't use - can stuff things up and is not necessary
 newpath 0 setlinecap 0 setlinejoin 10 setmiterlimit
 
 clippath pathbbox newpath
 cvi /ury exch def
 cvi /urx exch def
 % This seems to be designed to ensure llx,lly are a little too big.
 % Unfortunately, a lly of -0.19 becomes -10, doing bad things to the grid
 % position (i.e. it's totally wrong), with grid units other than 1.
 % -> Use the nearest integer, as for urx, ury.
 %10 div floor 10 mul cvi /lly exch def
 %10 div floor 10 mul cvi /llx exch def
 cvi /lly exch def
 cvi /llx exch def
 urx llx sub /lenx exch def
 ury lly sub /leny exch def
 
 /snaptopixel {
     transform
 	.25 sub round .25 add exch
 	.25 sub round .25 add exch
     itransform
 } bind def
 
 0.01 setlinewidth
 [1 2] 0 setdash		% dashed line
 0 setgray
 /set-line-style {
     dup gridunit div round cvi medstep mod 0 eq { [ ] 0 setdash } if
     dup gridunit div round cvi numstep mod 0 eq { 0.4 setlinewidth } if
     dup gridunit div round cvi 0 eq { 0.6 setlinewidth } if
 } bind def
 
 llx  step gridunit mul  urx { %  draw vertical lines
     gsave
     set-line-style
     lly snaptopixel moveto 0 leny snaptopixel rlineto stroke
     grestore
 } for
 
 ury  step gridunit mul neg  lly { % draw horizontal lines
     gsave
     set-line-style
     llx exch snaptopixel moveto lenx 0 snaptopixel rlineto stroke
     grestore
 } for
 
 % circle at (0,0)
 [ ] 0 setdash 1 setlinewidth newpath 15 0 moveto 0 0 15 0 360 arc stroke
 
 /kreis { % clip light circle from existing lines
     gsave 1 setgray
     %/d (500) stringwidth pop def
     dup stringwidth pop /d exch def % adjust circle size to string width
     currentpoint FontSize 3 div add exch d 2 div add exch
     d 0.6 mul 0 360 arc
     fill grestore
 } def
 
 % axes labelling font
 /Helvetica-Bold findfont FontSize scalefont setfont
 0.5 setgray
 
 % print axis labelling, X
 llx numstepunit div floor numstepunit mul cvi
 numstepunit
 urx
 { % for
     dup 0 eq
     {
       pop
     }{
       %dup 20 grid@paperheight sub moveto	% labelling at bottom
       dup -27 moveto				% labelling at top
       gridunit div round cvi
       (      ) cvs dup stringwidth pop 2 div neg 0 rmoveto
       kreis show
     } ifelse
 } bind for
 
 % print axis labelling, Y
 lly numstepunit div floor numstepunit mul cvi
 numstepunit
 ury 
 { % for
     dup 0 eq
     {
       pop
     }{
       40 1 index moveto
       gridunit div round cvi neg
       (      ) cvs dup stringwidth pop 
       neg 3 sub FontSize 3 div neg rmoveto
       kreis show
     } ifelse
 } bind for
 
 end restore
 
 } bind def  % dr@wgrid
 
 % %% Hook into dvips-generated PS
 % at start of page:
 %/bop-hook where
 %	{
 %	  pop
 %	  /@ldbophookgrid /bop-hook load def
 %	  /bop-hook { @ldbophookgrid dr@wgrid } def
 %	}{
 %	  /bop-hook { dr@wgrid } def
 %	} ifelse
 % at end of page:
 /eop-hook where
 	{
 	  pop
 	  /@ldeophookgrid /eop-hook load def
 	  /eop-hook { @ldeophookgrid dr@wgrid } def
 	}{
 	  /eop-hook { dr@wgrid } def
 	} ifelse
%
 { % start a procedure so PostScript `ignores' the dvips configuration part.
h config.a4grid
 } pop % PostScript cleanup
%
% EOF config.a4grid