% \iffalse
%
%  EPSDICE.DTX  by Thomas Heim  (thomas.heim@unibas.ch)
%
%  LICENSE: LPPL
%
%  a small package to include dice faces in .eps format
%
%  provides the command \epsdice{#1}
%  taking one argument, an integer from 1 to 6
%
%  HISTORY:
%  ========
%
%  2001/02/11 -- first release, for dvips only
%
%  2001/02/13 -- now works under pdfLaTeX, too
%                (with Rolf Niepraschk's help)
%
%  2001/09/04 -- "program encapsulated" by introducing "@" in counter name
%             -- distributed as .dtx and .ins files, plus binary .pdf file
%             -- some modifications in documentation                      
%             -- NO CHANGES IN USER INTERFACE                        (tah)
%
% \fi
%<*sampledoc>
\documentclass[11pt]{article}
\usepackage{epsdice}
%
\title{Package `epsdice' --- a scalable dice font}
\author{2001/09/04 -- Version 1.2 \\[.5ex] Thomas Heim \\[.5ex]
  (thomas.heim@unibas.ch)}
\date{}
\begin{document}
\maketitle
\section{Introduction}
Dice fonts are already available in \textsc{metafont} format. 
(I should know, I wrote one myself: \texttt{dice3d.mf}.)
For some applications it seems preferable, however, to have the fonts
in scalable form. The package \texttt{epsdice} fills this gap.

\texttt{epsdice.sty} provides a single command,
\verb+\epsdice{#1}+. It takes one argument, an integer from 1 to 6. 
The value of a counter is allowed as an argument, too, so automatic 
representation of (generated or input) data is possible as well.
Depending on the value of the argument, \verb+\epsdice+ includes the 
appropriately clipped region from a file containing simple drawings 
of dice faces. The drawings scale with the current font because the 
height of the included graphics file is set in terms of `ex' units.
%
\section{Requirements and limitations}
\texttt{epsdice} relies on the \texttt{graphicx} package to include 
the drawings. 
It also uses the \texttt{ifthen} package to validate the arguments
(error checking).

The package has been tested with two widely used package drivers
for \texttt{graphicx}: \verb+[dvips]+ and \verb+[pdftex]+. By 
default, the command \verb+\epsdice+ includes 
\texttt{epsdice.eps} under \LaTeXe, and \texttt{epsdice.pdf} 
under pdf\LaTeX, respectively\footnote{Many thanks to Rolf 
Niepraschk for his help with the latter!}
(unless your \texttt{graphics.cfg} file specifies non-standard 
settings).

Clipping is supported only in pdf\TeX\ versions
$\ge 0.14$. You will also need an up-to-date version of 
\texttt{pdftex.def}: Clipping is not supported in versions 
\texttt{v0.03c} \texttt{[2000/09/04]} or earlier. 
If your pdf\TeX\ is up-to-date but
you still receive error messages about clipping not being
supported, get the latest \texttt{pdftex.def}
here: \\[.5ex] 
\verb+http://www.tug.org/applications/pdftex/pdftex.def+ 
\\[.5ex]
I tested the package successfully with pdf\TeX\ 0.14e 
and \texttt{pdftex.def} v0.03f \texttt{[2000/11/10]}. 
%
\section{Configuring this package}
To finish the installation of the package,
simply move the following files somewhere in your ``local texmf tree'':
\begin{verbatim}
  epsdice.sty,  epsdice.cfg,  dice.eps,  dice.pdf
\end{verbatim}
e.g.\ to a new subdirectory
\verb+/tex/latex/epsdice/+. Refresh the 
\TeX-system's file name database, and that's it, you're all set! 
The remainder of this section applies only if you want to change
the default settings.

The command \verb+\epsdice+ works by including an external 
file, so this file must be found by \LaTeXe. The package comes with 
a configuration file \texttt{epsdice.cfg} containing the file name 
in the variable \verb+\dicefile+. By default, this variable points 
to the file \verb+dice.{eps|pdf}+ (without file name extension).
If you don't like my drawings and would like to ``roll your own'', 
a look at section~\ref{pscode} may give some hints. 
It contains the same PostScript code as \texttt{dice.eps}, 
with some comments about what's going on. Conversion to \textsc{pdf}
format has been accomplished with \texttt{epstopdf} (using 
GhostScript), see pdf\TeX's web page. 

If you intend to keep the external file with the dice drawings in a
different place or if you want to use your own version you have to 
edit the configuration file \texttt{epsdice.cfg} accordingly, 
by adding the path to the file name, or by changing the file name in the
variable \verb+\dicefile+.
If a configuration file does not exist, the external file 
is assumed to be located in the present working directory.
%
\section{Examples}
This text shows dice faces in an 11pt environment: 
\epsdice{1} \epsdice{2} \epsdice{3} \epsdice{4} \epsdice{5} \epsdice{6}. 
The pictures fit into the surrounding text. 

{\Large 
Here is some large text. The dice pictures scale accordingly:% 
\footnote{\ldots as they do in footnotes, \epsdice{\value{footnote}}, 
like this.} \epsdice{3}. See?}  The package works with standard counters, 
too: Here's the result of \verb+\epsdice{\value{section}}+ 
\epsdice{\value{section}}.
%
\section{Drawing dice in PostScript}
\label{pscode}
Something as simple as a ``square with dots
on it'' is rather straightforward to draw in PostScript. The first line
\begin{flushleft}
\verb+%!PS-Adobe-2.0 EPSF-1.2+
\end{flushleft}
is just a standard header. Next comes the bounding box: Each face measures
$32\times 32$~pt, centered in a $43\times43$~pt box. For six faces in a row, 
this gives a total bounding box of $258\times43$~pt:
\begin{verbatim}
%%BoundingBox: 0 0 258 43
\end{verbatim}
The \verb+/frame+ macro defines a simple box with rounded corners. It takes
one argument, the face index $n$, and calculates a corresponding $x$-offset:
$x_{\mathrm{off}}=43 (n-1)$. The point $(x_{\mathrm{off}},0)$ then becomes
the origin of the coordinate system for this particular face. The frame
consists of straight lines separated by 5~pt from the outer margin, and of
four quarter circles with radius 5~pt centered at
$(10,10),\,(32,10),\,(10,32)$, and $(32,32)$.
\begin{verbatim}
/frame {
  /n exch def         % take n off the stack
  /offset n 1 sub 43 mul def  % offset = 43*(n-1)
  gsave               % save the graphics state
  newpath             % start a new path
  offset 0 translate  % move origin to (offset,0)
  32 5 moveto         % go to (32,5) in this system
  32 10 5 -90 0 arc   % SE quarter circle around (32,10)
  37 32 lineto        % right line
  32 32 5 0 90 arc    % NE quarter circle around (32,32)
  10 37 lineto        % top line
  10 32 5 90 180 arc  % NW quarter circle around (10,32)
  5 10 lineto         % left line
  10 10 5 180 270 arc % SW quarter circle around (10,10)
  closepath           % bottom line (closes the path)
  stroke              % paint the line
  grestore            % restore graphics state 
} def
\end{verbatim}
The dot positions are labelled within a face by $(x,y)$-coordinates 
running from $(1,1)$
for bottom left to $(3,3)$ for top right. The dot itself is
a filled circle with radius $3.5$~pt. Change the dots' positions
within the frame, or their radius, as you like. The \verb+/dot+ macro
takes three arguments off the stack: (i) the face index, to determine
the $x$-offset, (ii) the $x$-coordinate (1, 2, or 3) on the face, and
(iii) the $y$-coordinate (1, 2, or 3) on the face.
\begin{verbatim}
/dot {
  /y exch def         % take y-coordinate off the stack
  /x exch def         % take x-coordinate off the stack
  /n exch def         % take face index n off the stack
  /offset n 1 sub 43 mul def  % offset  = 43*(n-1)
  gsave               % save the graphics state
  newpath             % start a new path
  offset 0 translate  % move origin to (offset,0)
  x 8 mul 5 add       % the dot's x-position: 8*x+5
  y 8 mul 5 add       % the dot's y-position: 8*y+5
  3.5 0 360 arc       % a circle with radius 3.5 pt
  closepath           % close the circle
  fill                % fill it
  grestore            % restore graphics state 
} def
\end{verbatim}
Now choose a linewidth (2~pt)
\begin{verbatim}
2 setlinewidth
\end{verbatim}
and a color (black)
\begin{verbatim}
0 setgray 
\end{verbatim}
All we have to do now is draw the six faces 
\begin{verbatim}
1 1 6 { frame } for  % call /frame macro for n=1..6
\end{verbatim}
and fill in the appropriate dots.
The bottom left and top right dots appear on faces 2, 3, 4, 5, and 6.
Thus there is an outer loop from 2 to 6 in steps of 1. For each of these
face indices, we call the dot macro with $(x,y)$-argument equal to
$(1,1)$ and $(3,3)$. This can be achieved with an inner loop running
from 1 to 3 in steps of 2. Because the $x$- and $y$-coordinates coincide
for these particular points, we obtain them by duplicating the inner
loop index. The \verb+/dot+ macro gobbles not only the $x$- and
$y$-coordinates but the face index as well. In order to draw \emph{two}
dots on the same face, we thus have to duplicate the outer loop index, too.
\begin{verbatim}
2 1 6 {        % start loop 2 to 6 in steps of 1
  dup          % duplicate index (two dots on each face)
  1 2 3 {      % start loop 1 to 3 in steps of 2
    dup        % duplicate loop index -> 1 1 and 3 3
    dot        % call /dot macro, using three arguments
  } for        % inner loop (dot positions)
} for          % outer loop (face index)
\end{verbatim}
The center dot appears on faces 1, 3, and 5. We get it in a simple loop
from 1 to 5 in steps of 2, calling the \verb+/dot+ macro for each index
together with the coordinate set $(2,2)$ corresponding to the center.
\begin{verbatim}
1 2 5 { 2 2 dot } for  % (2,2) dot on 1, 3, 5
\end{verbatim}
The top left and the bottom right dots appear on faces 4, 5, and 6. Again, 
in order to draw \emph{two} dots on each face, we have to duplicate the
loop index, i.e., the face index:
\begin{verbatim}
4 1 6 {        % start loop 4 to 6 in steps of 1
  dup          % duplicate index (two dots on each face)
  1 3 dot      % call /dot macro for (1,3) dot on this face
  3 1 dot      % call /dot macro for (3,1) dot on this face 
} for          % loop over face index 
\end{verbatim}
Finally, on face 6 we have two dots in positions $(1,2)$ and $(3,2)$. We can
use a loop, but then we have to swap the loop index, i.e., the dot's 
$x$-coordinate, with the (constant) face index 6 inside the loop.
\begin{verbatim}
1 2 3 {        % start loop 1 to 3 in steps of 2
  6 exch       % swap loop index and constant face index 
  2            % push constant y-coordinate on stack
  dot          % call /dot macro for (x,2) dot on face 6
} for          % loop over x-coordinates 1 and 3
\end{verbatim}
That's it!
\begin{verbatim}
%%EOF
\end{verbatim}
\section{Errors}
Of course it doesn't make sense to ask for dice faces with arguments
other than 1, 2, 3, 4, 5, or 6. The package generates error
messages and simply prints the offending argument in the \TeX\ output.
%
%  This generates `benign' errors: \epsdice{99} and \epsdice{-3}.
%  The following is more serious, generating two different errors:
%  \epsdice{epsfoo}. Nevertheless, output continues with a fall-back
%  solution.
\end{document}
%
%</sampledoc>
%
%<*package>
%
%  I don't think the package code needs much of an explanation;
%  it's really very simple\ldots
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{epsdice}[2001/09/04 by Thomas Heim]
\RequirePackage{graphicx}
\RequirePackage{ifthen}
\InputIfFileExists{epsdice.cfg}{}{\def\dicefile{dice}}
%
\newcounter{eps@face}
\newcommand*\epsdice[1]{%
\setcounter{eps@face}{\number #1}%
\ifthenelse{\value{eps@face}>6}{\theeps@face\setcounter{eps@face}{0}}{}%
\ifthenelse{\value{eps@face}<0}{\theeps@face\setcounter{eps@face}{0}}{}%
\ifcase\value{eps@face}%
  \PackageError{EPSDICE}{%
Argument is not a number, or its value\MessageBreak
lies outside the allowed range for dice}{%
Make sure the argument is either 1, 2, 3, 4, 5, or 6.\MessageBreak
-- Just go on and I'll write whatever you typed;\MessageBreak
   the output may not make much sense, though}%
\or
  \includegraphics[height=1.75ex,viewport= 3 4 38 39,%
  clip=true]{\dicefile}%
\or 
  \includegraphics[height=1.75ex,viewport= 46 4 81 39,%
  clip=true]{\dicefile}%
\or 
  \includegraphics[height=1.75ex,viewport= 89 4 124 39,%
  clip=true]{\dicefile}%
\or 
  \includegraphics[height=1.75ex,viewport= 132 4 167 39,%
  clip=true]{\dicefile}%
\or 
  \includegraphics[height=1.75ex,viewport= 175 4 210 39,%
  clip=true]{\dicefile}%
\or 
  \includegraphics[height=1.75ex,viewport= 218 4 253 39,%
  clip=true]{\dicefile}%
\fi}
%
%</package>
%
%<config>\def\dicefile{dice}
%
%<*epsfile>
%
%  Here's the PostScript code for the drawings:
%
%<<PSFILE
%!PS-Adobe-2.0 EPSF-1.2
%%BoundingBox: 0 0 258 43
/frame { /n exch def /offset n 1 sub 43 mul def gsave newpath 
offset 0 translate 32 5 moveto 32 10 5 -90 0 arc 37 32 lineto
32 32 5 0 90 arc 10 37 lineto 10 32 5 90 180 arc 5 10 lineto 
10 10 5 180 270 arc closepath stroke grestore } def
/dot { /y exch def /x exch def /n exch def /offset n 1 sub 43 
mul def gsave newpath offset 0 translate x 8 mul 5 add y 8 mul 
5 add 3.5 0 360 arc closepath fill grestore } def 
2 setlinewidth 0 setgray 1 1 6 { frame } for 
2 1 6 { dup 1 2 3 { dup dot } for } for 1 2 5 { 2 2 dot } for 
4 1 6 { dup 1 3 dot 3 1 dot } for 1 2 3 { 6 exch 2 dot } for
%%EOF
%PSFILE
%
%</epsfile>
\endinput


