%%
%% This is file `bitpattern.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% bitpattern.dtx  (with options: `package')
%% 
%% This file is part of the bitpattern, a LaTeX package to typeset bit
%% pattern diagrams.
%% https://bitbucket.org/bourguet/bitpattern
%% 
%% Copyright 2005--2015 Jean-Marc Bourguet
%% 
%% This program is provided under the terms of the LaTeX Project Public
%% License distributed from CTAN archives in directory
%% macros/latex/base/lppl.txt.
%% 
%% \CheckSum{412}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{bitpattern}[2015/12/11 bit pattern diagrams]

\RequirePackage{keyval}
\RequirePackage{calc}
\RequirePackage{multido}
\newif\ifbp@NumberBitsAbove
\newif\ifbp@NumberBitsBelow
\newif\ifbp@HasBitNumbers
\newif\ifbp@NumberFieldTwice
\newif\ifbp@NumberAllBits
\newif\ifbp@NumberBitsBigEndian
\newcount\bp@StartBit
\newlength{\bp@BitWidth}
\newlength{\bp@TickHeight}
\newcommand\bpLittleEndian
{\bp@NumberBitsBigEndianfalse}

\newcommand\bpBigEndian
{\bp@NumberBitsBigEndiantrue}

\newcommand\bpNumberBitsAbove
{\bp@NumberBitsAbovetrue
  \bp@NumberBitsBelowfalse}

\newcommand\bpNumberBitsBelow
{\bp@NumberBitsAbovefalse
  \bp@NumberBitsBelowtrue}

\newcommand\bpNoBitNumbers
{\bp@HasBitNumbersfalse}

\newcommand\bpNumberFieldsOnce
{\bp@HasBitNumberstrue
  \bp@NumberFieldTwicefalse
  \bp@NumberAllBitsfalse}

\newcommand\bpNumberFieldsTwice
{\bp@HasBitNumberstrue
  \bp@NumberFieldTwicetrue
  \bp@NumberAllBitsfalse}

\newcommand\bpNumberAllBits
{\bp@HasBitNumberstrue
  \bp@NumberFieldTwicetrue
  \bp@NumberAllBitstrue}
\newcommand\bpStartAtBit[1]{\bp@StartBit=#1}
\newcommand\bpSetBitWidth[1]{\setlength{\bp@BitWidth}{#1}}
\newcommand\bpSetTickHeight[1]{\setlength{\bp@TickHeight}{#1}}
\define@key{bitpattern}{littleEndian}[true]{\bpLittleEndian}
\define@key{bitpattern}{bigEndian}[true]{\bpBigEndian}
\define@key{bitpattern}{numberBitsAbove}[true]{\bpNumberBitsAbove}
\define@key{bitpattern}{numberBitsBelow}[true]{\bpNumberBitsBelow}
\define@key{bitpattern}{noBitNumbers}[true]{\bpNoBitNumbers}
\define@key{bitpattern}{numberFieldsOnce}[true]{\bpNumberFieldsOnce}
\define@key{bitpattern}{numberFieldsTwice}[true]{\bpNumberFieldsTwice}
\define@key{bitpattern}{numberAllBits}[true]{\bpNumberAllBits}

\define@key{bitpattern}{startBit}{\bpStartAtBit{#1}}
\define@key{bitpattern}{bitWidth}{\bpSetBitWidth{#1}}
\define@key{bitpattern}{tickSize}{\bpSetTickHeight{#1}}
\DeclareOption{littleEndian}{\bpLittleEndian}
\DeclareOption{bigEndian}{\bpBigEndian}
\DeclareOption{numberBitsAbove}{\bpNumberBitsAbove}
\DeclareOption{numberBitsBelow}{\bpNumberBitsBelow}
\DeclareOption{noBitNumbers}{\bpNoBitNumbers}
\DeclareOption{numberFieldsOnce}{\bpNumberFieldsOnce}
\DeclareOption{numberFieldsTwice}{\bpNumberFieldsTwice}
\DeclareOption{numberAllBits}{\bpNumberAllBits}
\bpLittleEndian
\bpNumberBitsBelow
\bpNumberFieldsOnce
\AtEndOfPackage{\ifbp@NumberBitsBigEndian
  \bpStartAtBit{0}\else\bpStartAtBit{7}\fi}
\bpSetBitWidth{1em}
\bpSetTickHeight{2pt}
\providecommand\bpFormatField[1]{\strut\emph{#1}}
\providecommand\bpFormatBitNumber[1]{{\tiny\sffamily\strut #1}}
\newcount\bp@CurBit
\newlength{\bp@FieldWidth}
\newlength{\bp@RuleWidth}\setlength{\bp@RuleWidth}{0.4pt}
\newsavebox{\bp@TickBox}
\newcommand\bp@FormatTopBit[1]{\ifbp@HasBitNumbers
  \ifbp@NumberBitsAbove\bpFormatBitNumber{#1}\fi\fi}
\newcommand\bp@FormatBottomBit[1]{\ifbp@HasBitNumbers
  \ifbp@NumberBitsBelow\bpFormatBitNumber{#1}\fi\fi}
\newcommand\bp@Tick{\rule{\bp@RuleWidth}{\bp@TickHeight}}
\newcommand\bp@vbox[3]
{\parbox[#1]{#2}{\lineskip=0pt\lineskiplimit=0pt\baselineskip=0pt\relax#3}}
\newcommand\bp@BrokenRuleFill{\dimen0=\bp@RuleWidth\multiply\dimen0 3
\leaders \hrule height \bp@RuleWidth\hfill
\xleaders\hbox to \dimen0{\rule{\bp@RuleWidth}{\bp@RuleWidth}\hfill}\hfill
\leaders \hrule height \bp@RuleWidth\hfill}
\newcommand\bp@BrokenTicks
{\usebox{\bp@TickBox}\usebox{\bp@TickBox}\hfill\usebox{\bp@TickBox}}
\newcommand\bp@FillWithTicks
{\leaders\hbox to \bp@BitWidth{\usebox{\bp@TickBox}}\hfill}
\newcommand\bp@RuleFill[2]
{\ifnum #1 = #2
  \leaders \hrule height \bp@RuleWidth\hfill
\else
  \bp@BrokenRuleFill
\fi}
\newcommand\bp@TicksFill[2]
{\ifnum #1 = #2
  \bp@FillWithTicks
\else
  \bp@BrokenTicks
\fi}
\newcommand\bp@MakeNumbers[2]{\begingroup\ifbp@NumberAllBits
\mbox{\multido{}{#1}{%
    \makebox[\bp@BitWidth]{\bpFormatBitNumber{\the\bp@CurBit}}%
    \advance\bp@CurBit\ifbp@NumberBitsBigEndian 1\else -1\fi}}%
\else\ifbp@NumberFieldTwice
\mbox{%
  \makebox[\bp@FieldWidth]%
     {\makebox[\bp@BitWidth][#2]{\bpFormatBitNumber{\the\bp@CurBit}}%
       \ifnum #1 > 1
         \advance\bp@CurBit\ifbp@NumberBitsBigEndian #1\else -#1\fi
         \advance\bp@CurBit\ifbp@NumberBitsBigEndian -1\else 1\fi
         \hfill\makebox[\bp@BitWidth][#2]%
                    {\bpFormatBitNumber{\the\bp@CurBit}}\fi}}%
\else
\ifx#2r
  \ifnum #1 > 1
    \advance\bp@CurBit\ifbp@NumberBitsBigEndian #1\else -#1\fi
    \advance\bp@CurBit\ifbp@NumberBitsBigEndian -1\else 1\fi
\fi\fi
\makebox[\bp@FieldWidth][#2]{\bpFormatBitNumber{\the\bp@CurBit}}%
\fi\fi\endgroup}
\newcommand\bp@MakeTopNumbers[1]%
{\ifbp@HasBitNumbers
    \ifbp@NumberBitsAbove
      \bp@MakeNumbers{#1}{l}
    \else
      \makebox[\bp@FieldWidth][l]{\hfill}\fi\fi}
\newcommand\bp@MakeBottomNumbers[1]%
{\ifbp@HasBitNumbers
    \ifbp@NumberBitsBelow
      \bp@MakeNumbers{#1}{r}
    \else
      \makebox[\bp@FieldWidth][r]{\hfill}\fi\fi}
\newcommand{\bitpattern}[1][]{%
  \begingroup
    \setkeys{bitpattern}{#1}%
    \savebox{\bp@TickBox}[\bp@BitWidth]{\bp@Tick\hfill}%
    \bp@CurBit=\bp@StartBit%
    \bp@FieldIfPresent}
\newcommand{\bp@FieldIfPresent}{\@ifnextchar/{\bp@Done}{\bp@Field}}
\newcommand{\bp@Field}[1]%
{\@ifnextchar[{\bp@SizedField[#1]}{\bp@DoField[#1][1][1]}}
\def\bp@SizedField[#1][#2]%
{\@ifnextchar[{\bp@DoField[#1][#2]}{\bp@DoField[#1][#2][#2]}}
\def\bp@DoField[#1][#2][#3]%
{\setlength{\bp@FieldWidth}{\bp@BitWidth * #3}%
  \ifnum #2 > #3 \ifbp@NumberAllBits\bpNumberFieldsTwice\fi\fi
  \bp@vbox{t}{\bp@FieldWidth}{%
    \bp@vbox{b}{\bp@FieldWidth}{%
      \bp@MakeTopNumbers{#2}
      \makebox[\bp@FieldWidth][s]{\bp@RuleFill{#2}{#3}}
      \makebox[\bp@FieldWidth][s]{\bp@TicksFill{#2}{#3}}
      \makebox[\bp@FieldWidth][s]%
        {\vrule width \bp@RuleWidth\hfil\bpFormatField{#1}\hfil}}
    \makebox[\bp@FieldWidth][s]{\bp@TicksFill{#2}{#3}}
    \makebox[\bp@FieldWidth][s]{\bp@RuleFill{#2}{#3}}
    \bp@MakeBottomNumbers{#2}}%
  \advance\bp@CurBit\ifbp@NumberBitsBigEndian #2\else -#2\fi%
  \bp@FieldIfPresent}
\def\bp@Done/{\bp@vbox{t}{\bp@RuleWidth}{%
    \bp@vbox{b}{\bp@RuleWidth}{%
      \makebox[\bp@RuleWidth]{\bp@FormatTopBit{}\hfill}
      \makebox[\bp@RuleWidth]{\bp@RuleFill{0}{0}}
      \makebox[\bp@RuleWidth]{\bp@Tick\hfill}
      \makebox[\bp@RuleWidth]%
        {\vrule width \bp@RuleWidth\hfil\bpFormatField{}\hfil}}
    \makebox[\bp@RuleWidth]{\bp@Tick\hfill}
    \makebox[\bp@RuleWidth]{\bp@RuleFill{0}{0}}
    \makebox[\bp@RuleWidth]{\hfill\bp@FormatBottomBit{}}}\endgroup}
\ProcessOptions
\endinput
%%
%% End of file `bitpattern.sty'.
