% bitmap.sty	by hisato-h@ascii.co.jp
%
% \bitmapinput[<t|c|b>]{<dpi>}{<file name>}
%	input file: "<file name>.xbm" (bitmap format)
%
% \begin{bitmap}[<t|c|b>]{<dpi>}
%   <X Window bitmap data>
% \end{bitmap}
%
\newcount\bm@nchar \newcount\bm@nline
\newcount\bm@bytes \newcount\bm@crl
\newdimen\bm@pixel \newdimen\bm@width
%
\def\bitmapinput{\@ifnextchar[{\bm@input}{\bm@input[c]}}
%
\begingroup
\catcode`#=13 \catcode`+=6
\global\def\bm@input[+1]+2+3{{%
  \catcode`\#=13 \def#{\bitmap[+1]{+2}}%
  \input +3.xbm \endbitmap}}
\endgroup
%
\def\bitmap{\leavevmode \hbox\bgroup $\@ifnextchar[{\bm@head}{\bm@head[c]}}
%
\def\endbitmap{$\egroup}
%
\def\bm@head[#1]#2#3width #4 #5height #6 #7= {%
  \bm@crl=0 \bm@pixel=1in \divide\bm@pixel by#2
  \bm@bytes=#4 \advance\bm@bytes by 7 \divide\bm@bytes by 8 \bm@nline=#6
  \if t#1\vtop \else\if b#1\vbox \else\vcenter \fi\fi
  \bgroup \aftergroup\del@colon
    \baselineskip\bm@pixel
    \bm@nchar=\bm@bytes
    \bm@width=\bm@pixel \multiply\bm@width by#4
    \hbox to\bm@width\bgroup
      \afterassignment\bm@byte\let\next=}
%
\def\del@colon;{}
%
\def\bm@byte#10x#2#3{\csname nybl@#3\endcsname \csname nybl@#2\endcsname
  \advance\bm@nchar-1
  \ifnum\bm@nchar>0\let\next=\bm@byte
  \else \run@length{0}\hss\egroup \advance\bm@nline-1
    \ifnum\bm@nline>0 \hbox to \bm@width\bgroup \let\next=\bm@byte
    \else \let\next=\relax\fi
  \fi\next}
%
\def\run@length#1{\ifnum#1=0
    \ifnum\bm@crl>0 \vrule width\bm@crl\bm@pixel height\bm@pixel \bm@crl=#1
    \else \kern-\bm@crl\bm@pixel \bm@crl=#1 \fi
  \else\ifnum#1>0\ifnum\bm@crl>0 \advance\bm@crl#1
      \else \kern-\bm@crl\bm@pixel \bm@crl=#1 \fi
    \else\ifnum\bm@crl>0 \vrule width\bm@crl\bm@pixel height\bm@pixel \bm@crl=#1
      \else \advance\bm@crl#1 \fi\fi\fi}
%
\expandafter\def\csname nybl@0\endcsname{\run@length{-4}}
\expandafter\def\csname nybl@1\endcsname{\run@length{1}\run@length{-3}}
\expandafter\def\csname nybl@2\endcsname
  {\run@length{-1}\run@length{1}\run@length{-2}}
\expandafter\def\csname nybl@3\endcsname{\run@length{2}\run@length{-2}}
\expandafter\def\csname nybl@4\endcsname
  {\run@length{-2}\run@length{1}\run@length{-1}}
\expandafter\def\csname nybl@5\endcsname
  {\run@length{1}\run@length{-1}\run@length{1}\run@length{-1}}
\expandafter\def\csname nybl@6\endcsname
  {\run@length{-1}\run@length{2}\run@length{-1}}
\expandafter\def\csname nybl@7\endcsname{\run@length{3}\run@length{-1}}
\expandafter\def\csname nybl@8\endcsname{\run@length{-3}\run@length{1}}
\expandafter\def\csname nybl@9\endcsname
  {\run@length{1}\run@length{-2}\run@length{1}}
\def\nybl@a{\run@length{-1}\run@length{1}\run@length{-1}\run@length{1}}
\def\nybl@b{\run@length{2}\run@length{-1}\run@length{1}}
\def\nybl@c{\run@length{-2}\run@length{2}}
\def\nybl@d{\run@length{1}\run@length{-1}\run@length{2}}
\def\nybl@e{\run@length{-1}\run@length{3}}
\def\nybl@f{\run@length{4}}
%
\endinput
