% \iffalse meta-comment
%
% Copyright (C) 2011 by Raphaël Pinson <raphink@gmail.com>
% ---------------------------------------------------------------------------
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Raphaël Pinson.
%
% This work consists of the files babyloniannum.dtx and babyloniannum.ins
% and the derived filebase babyloniannum.sty.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{babyloniannum.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{babyloniannum}
%<*package>
    [2011/08/19 0.5 Typeset Babylonian numerals with XeTeX and LuaTeX]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{babyloniannum}[2011/08/19]
\usepackage{metalogo}
\usepackage{pgffor}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{babyloniannum.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{138}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{0.5}{2011/08/19}{Fix documentation and kerning}
% \changes{0.4}{2011/08/18}{To infinity and beyond}
% \changes{0.3}{2011/08/17}{Add counter function}
% \changes{0.2}{2011/08/17}{Improved documentation}
% \changes{0.1}{2011/08/17}{Initial release}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \providecommand*{\url}{\texttt}
% \GetFileInfo{babyloniannum.dtx}
% \title{The \textsf{babyloniannum} package}
% \author{Raphaël Pinson \\ \url{raphink@gmail.com}}
% \date{\fileversion~from \filedate}
%
% \renewcommand{\thesection}{\babylonian{section}}
% \renewcommand{\thesubsection}{\babylonian{section}.\babylonian{subsection}}
%
% \maketitle
%
% \section{Introduction}
%
% This package was created as an answer to a
% question\footnote{\url{http://tex.stackexchange.com/questions/25939/typesetting-babylonian-numerals/25947\#25947}}
% about typesetting Babylonian numerals asked on \url{http://tex.stackexchange.com}.
%
% This package allows to typeset Babylonian numerals using \XeTeX{} or \LuaTeX{}.
% It makes use of the \babylonianfont{} Paleo-Babylonian TrueType font
% which can be downloaded at \url{http://www.hethport.uni-wuerzburg.de/cuneifont/}.
%
% \section{Usage}
%
% 
% \DescribeMacro{\babylonianfont}
%
% Set the font used. Currently, only the \babylonianfont{} font has been tested.
% Let me know if you get the package to work with other fonts.
%
% Example usage: \verb!\babyloninanfont{Santakku}!
%
% \DescribeMacro{\babyloniannum}
%
% This is the main macro of this package. It takes a number between 1 and 59
% as argument and typesets it with Babylonian numerals.
%
% Example usage:
%
% \verb!\babyloninannum{424000}! is \babyloniannum{424000} (1,57,46,40 in base 60)
%
% \verb!\babyloninannum{21609}! is \babyloniannum{21609} (6,0,9 in base 60)
%
% \DescribeMacro{\babylonian}
%
% Like \verb!\arabic! or \verb!\roman!, this macro takes a counter
% name as argument and returns its Babylonian representation.
%
% For example, this documentation is typeset with:
% \begin{verbatim}
%   \renewcommand{\thesection}{\babylonian{section}}
% \end{verbatim}
% 
% \DescribeMacro{\unicodedisp}
%
% This macro lets you print characters using their unicode reference.
% It is used by \verb!\babyloniannum! to display Babylonian numbers.
%
% Example usage: \verb!\unicodedisp{1230B}!
%
% \pagebreak
% \section{Table of characters}
%
% Below is a sample list of Babylonian characters this package can typeset:
%
% \begin{minipage}{0.3\textwidth}
% \noindent\foreach \n in {1,...,40}{%
% \n~\babyloniannum{\n}\\
% }
% \end{minipage} \hfill
% \begin{minipage}{0.3\textwidth}
% \noindent\foreach \n in {41,...,80}{%
% \n~\babyloniannum{\n}\\
% }
% \end{minipage} \hfill
% \begin{minipage}{0.3\textwidth}
% \noindent\foreach \n in {81,...,120}{%
% \n~\babyloniannum{\n}\\
% }
% \end{minipage}
%
% \section{Known issues}
%
% \subsection{Glyph for 20}
%
% The glyph for number 20 was not found in the \babylonianfont{} font.
% Therefore, it has been replaced by the combination of two 10 glyphs,
% with a kerning adjustment.
%
% \subsection{Glyph for 0}
%
% The Babylonian system has no glyph for 0, which is represented by a
% large space. In this package, 0 is implemented as a 0.5em kerning space.
%
% \subsection{Multiples of 60}
%
% The Babylonian numeral system is a sexagesimal system
% (a positional base 60 system), which does not feature a glyph for 0.
% Therefore, a number such as \babyloniannum{23} can stand for
% $23$, $23 \times 60$ or $23 \times 60 \times 60$ or even $23/60$.
% Only the context allows to decide which number is represented.
%
% \StopEventually{}
%
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
%
%    \begin{macrocode}
\ProvidesPackage{babyloniannum}
\RequirePackage{fontspec}
\RequirePackage{xunicode}
\RequirePackage{numname}
%    \end{macrocode}
%
% \begin{macro}{\babylonianfont}
%    \begin{macrocode}
\newcommand{\babylonianfont}{Santakku}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\unicodedisp}
%    \begin{macrocode}
\newcommand{\unicodedisp}[1]{\char"#1}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\babylonian}
%    \begin{macrocode}
\newcommand{\babylonian}[1]{%
  \protect\babyloniannum{\arabic{#1}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\babylonianglyph}
%    \begin{macrocode}
\newcommand{\babylonianglyph}[1]{%
\ifnum #1 > \z@ % glyph is not zero
  \chardef\m@mten=10 % cuts by units of 10
  \numdigits{#1} % parse number
  \ifcase\c@xsm@mctr %
    \relax %
    \or
    \unicodedisp{1230B} %10
    \or
    \unicodedisp{1230B}\kern-0.15em{}\unicodedisp{1230B} %20 -- unknown?
    \or
    \unicodedisp{1230D} %30
    \or
    \unicodedisp{1240F} %40
    \or
    \unicodedisp{12410} %50
  \fi
  \ifnum \c@ism@mctr > \z@ %
    \ifnum \c@xsm@mctr > \z@ %
  \kern-0.5em{} % make glyphs closer
    \fi %
  \fi %
  \ifcase\c@ism@mctr %
    \or
    \unicodedisp{12079} %1
    \or
    \unicodedisp{1222B} %2
    \or
    \unicodedisp{12408} %3
    \or
    \unicodedisp{120FB} %4
    \or
    \unicodedisp{1240A} %5
    \or
    \unicodedisp{1240B} %6
    \or
    \unicodedisp{1240C} %7
    \or
    \unicodedisp{1240D} %8
    \or
    \unicodedisp{1240E} %9
  \fi
  \addtocounter{baby@glyphs}{1}%
\else
  \ifnum\c@baby@glyphs > \z@ %
\kern0.5em{}% empty space for zero
  \fi
\fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\babylonian@setcounters}
%    \begin{macrocode}
\newcounter{baby@ism@mctr}  % "units"
\newcounter{baby@xsm@mctr}  % "tens"
\newcounter{baby@csm@mctr}  % "hundreds"
\newcounter{baby@ksm@mctr}  % "thousands"
\newcounter{baby@xksm@mctr} % "ten thousands"
\newcounter{baby@cksm@mctr} % "hundred thousands"
\newcounter{baby@msm@mctr}  % "millions"
\newcounter{baby@xmsm@mctr} % "ten millions"
\newcounter{baby@cmsm@mctr} % "hundred millions"
\newcounter{baby@bsm@mctr}  % "billions"
\newcommand{\babylonian@setcounters}{%
  \setcounter{baby@ism@mctr}{\c@ism@mctr}%
  \setcounter{baby@xsm@mctr}{\c@xsm@mctr}%
  \setcounter{baby@csm@mctr}{\c@csm@mctr}%
  \setcounter{baby@ksm@mctr}{\c@ksm@mctr}%
  \setcounter{baby@xksm@mctr}{\c@xksm@mctr}%
  \setcounter{baby@cksm@mctr}{\c@cksm@mctr}%
  \setcounter{baby@msm@mctr}{\c@msm@mctr}%
  \setcounter{baby@xmsm@mctr}{\c@xmsm@mctr}%
  \setcounter{baby@cmsm@mctr}{\c@cmsm@mctr}%
  \setcounter{baby@bsm@mctr}{\c@bsm@mctr}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\babyloniannum}
%    \begin{macrocode}
\newcounter{baby@glyphs}%
\newcommand{\babyloniannum}[1]{%
  \chardef\m@mten=60 % Cut by units of 60
  \numdigits{#1} % Parse number
  \babylonian@setcounters%
  {\fontspec{\babylonianfont}%
  \mbox{%
  \setcounter{baby@glyphs}{0}%
  \babylonianglyph{\c@baby@bsm@mctr}%
  \babylonianglyph{\c@baby@cmsm@mctr}%
  \babylonianglyph{\c@baby@xmsm@mctr}%
  \babylonianglyph{\c@baby@msm@mctr}%
  \babylonianglyph{\c@baby@cksm@mctr}%
  \babylonianglyph{\c@baby@xksm@mctr}%
  \babylonianglyph{\c@baby@ksm@mctr}%
  \babylonianglyph{\c@baby@csm@mctr}%
  \babylonianglyph{\c@baby@xsm@mctr}%
  \babylonianglyph{\c@baby@ism@mctr}%
  }}
}
%    \end{macrocode}
% \end{macro}
%
%
% \iffalse
%</package>
% \fi
%
% \Finale
\endinput
