%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  braille.sty
%  
%  William Park <opengeometry@yahoo.ca>
%  September 1998, April 1999
%      - original author
%
%  Dominique Unruh <dominique@unruh.de>
%  March 2001, October 2001
%      - major additions
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Copyright 1998-2010 William Park
%
%  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 William Park.
%
%  This work consists of all files listed below:
%      README          -- this file
%      braille.html    -- short description of braille.sty, grade[12].py
%      braille.sty     -- LaTeX2e package for typesetting Braille
%      braillegif1.gif -- transparent image
%      braillegif2.gif -- transparent image
%      grade1.py       -- Python script to convert text to Grade 1 Braille
%      grade2.py       -- Python script to convert text to Grade 2 Braille
%      summary.pdf     -- PDF of summary.tex
%      summary.tex     -- list of TeX macros and Braille symbols
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%  Major additions made by Dominique Unruh <dominique@unruh.de>
%  March 2001, October 2001
%   - braille yields error, if invalid points are used in \braillbox
%   - Option compact causes loading of \braille and tables to be ommitted (saves
%     memory)
%   - Option 8dots enables 8-dot braille
%   - Option mirror mirrors.
%   - Options useemptybox, puttinydots, 8dots, mirror can be switched on and off
%     inside the document via \ifbraille...true and \ifbraille...false
%   - Documentation in the header of braille.sty updated to reflect above
%     changes
%   - \braille{} command
%   - compatible with e-LaTeX's r2l mode.
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Author:
%  -------
%  William Park <opengeometry@yahoo.ca>
%  September 1998, April 1999
%
%  Description:
%  ------------
%
%  This package defines macros, commands, and fonts to typeset 6-dot or 8-dot
%  braille symbols for the blinds.  Both Grade 1 and 2 Braille are supported.
%  All 189 Grade 2 contractions are defined; however, some puctuation symbols
%  are missing, simply because I couldn't find them.
%  
%  Normally,
%      \usepackage{braille}
%  is sufficient.  The command \braille{} takes braille "text" which is made up
%  of single character or string enclosed in {}.  It converts "text" arguments
%  into braille tags and calls predefined TeX macros to print out the braille
%  symbols.  For example, 
%      \usepackage{braille}
%      \braille{I {like} {com}put{er}}
%  will print braille symbols for
%      'I', 'like', 'com', 'p', 'u', 't', 'er'
%  along with the 2 spaces.
%
%  In the argument, whitespaces are processed by TeX in the usual manner.
%  Multiple whitespaces (' ', '\t', '\n') collapse to single space.  If extra
%  spacing is needed, then it must come from outside of \braille{}.
%  
%  Examples:
%      \braille{September {Number}1998}
%      \braille{I run L{in}ux Slackw{ar}e {Number}3.3}
%      \braille{{Capital}{this} is LaTeX package {for} {braille} {ou}tput.}
%      \braille{I {also} wrote Py{th}on script {for} {con}v{er}t{ing} text
%      {to}{braille}.}
%
%  Therefore, you can type braille paragraph just as you would type normal text
%  paragraph.  To print single character, type that character.  To print
%  multi-character contraction, type that string enclosed in {}.
%
%  Note that the \braille command assumes English text. Other languages have
%  other abbreviations.
%
%
%  Options:
%  --------
%
%  The package is used as
%      \usepackage[puttinydots,useemptybox,compact,8dots]{braille}
%  where
%      puttinydots -- puts {\tiny.} at all dot positions for visual check
%      useemptybox -- uses empty braille box instead of \hskip
%      compact     -- load only command \braillebox, not \braille (saves memory)
%      8dots       -- display 8-dot braille (not used by \braille)
%      mirror      -- mirrors each braille box (for "printing" braille by hand
%                     with a needle, use e-LaTeX to get right-to-left writing).
%
%  You may activate or deactive inside the document
%      puttinydots by \brailleputtinydotstrue resp. \brailleputtinydotsfalse
%      useemptybox by \brailleuseemptyboxtrue resp. \brailleuseemptyboxfalse
%      8dots       by \brailleeightdotstrue   resp. \brailleeightdotsfalse
%      mirror      by \braillemirrortrue      resp. \braillemirrorfalse
%
%  Four macros are available to the user:
%      \braillebox{} -- LaTeX picture box used as braille font
%      \brailleunit  -- \unitlength for \braillebox{}
%      \brailledot{} -- actual dot printed in \braillebox{}
%      \braille{}    -- main user command (unless option compact is activated)
%  
%  Additional vertical space between paragraphs may be necessary, since
%  \parskip=0pt is default.  \braillebox{} is simple LaTeX picture box
%      \begin{picture}(2,3) ... \end{picture}
%  containing the actual patterns of dot which are placed with
%      \put(0.5,0.5){\brailledot} ...
%  The size and shape of braille box and dot can be changed by modifying
%  \brailleunit and \brailledot{}, respectively.
%
%
%  Other Files:
%  ------------
%
%  List of TeX macros and braille symbols defined here is summarized in
%      summary.tex -- "Summary of Grade 1 and 2 Braille"
%      summary.pdf
%  In order to type the braille tags manually, user would have to know Braille.
%  To make it easier for people who don't know Braille, I wrote Python scripts
%  to convert ordinary ASCII text into grade 1 and 2 braille tags:
%      grade1.py -- converts text into grade 1 braille
%      grade2.py -- converts text into grade 2 braille
%  which behave like any Unix filters.  Since my knowledge of Braille is
%  limited, there will be errors and omissions.  So, check with a braille
%  expert.
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{braille}
%  With options
%      puttinydots = put {\tiny.} at all dot positions
%      useemptybox = use empty braille box for space, not \hskip


%  Usage:  \braillebox{12345678}
%  Put dot (\brailledot) at positions 1, 2, 3, 4, 5, 6, 7, 8
%  Dot locations:
%      14
%      25
%      36
%      78
%
\newlength{\brailleunit}        % size of box containing one dot
\setlength{\brailleunit}{2mm}
\newcommand{\brailledot}{\circle*{0.6}}

\newif\ifbrailleputtinydots
\newif\ifbrailleeightdots
\newif\ifbraillecompact
\newif\ifbrailleuseemptybox
\newif\ifbraillemirror

\DeclareOption{puttinydots}{\brailleputtinydotstrue}
\DeclareOption{8dots}{\brailleeightdotstrue}
\DeclareOption{compact}{\braillecompacttrue}
\DeclareOption{useemptybox}{\brailleuseemptyboxtrue}
\DeclareOption{mirror}{\braillemirrortrue}

\ProcessOptions

\newcommand{\braillebox}[1]{{\unitlength=\brailleunit
    \ifx\TeXXeTstate\undefined\else\ifnum\TeXXeTstate=0\else\beginL\fi\fi
    \kern0.5\unitlength
    \ifbrailleeightdots
      \raisebox{-0.5\unitlength}[4\unitlength]{%
        \begin{picture}(2,4)(0,-1)
          \ifbrailleputtinydots\put@tinydots\fi
          \put@dot{#1}
        \end{picture}}%
    \else
      \raisebox{-0.5\unitlength}[3\unitlength]{%
        \begin{picture}(2,3)
          \ifbrailleputtinydots\put@tinydots\fi
          \put@dot{#1}
        \end{picture}}%
    \fi
    \kern0.5\unitlength
    \ifx\TeXXeTstate\undefined\else\ifnum\TeXXeTstate=0\else\endL\fi\fi}}

\newcommand{\put@tinydots}{%
  \put(0.5,2.5){\makebox[0pt]{\tiny.}}
  \put(0.5,1.5){\makebox[0pt]{\tiny.}}
  \put(0.5,0.5){\makebox[0pt]{\tiny.}}
  \put(1.5,2.5){\makebox[0pt]{\tiny.}}
  \put(1.5,1.5){\makebox[0pt]{\tiny.}}
  \put(1.5,0.5){\makebox[0pt]{\tiny.}}
  \ifbrailleeightdots
  \put(0.5,-.5){\makebox[0pt]{\tiny.}}%
  \put(1.5,-.5){\makebox[0pt]{\tiny.}}\fi}

\newcommand{\put@dot}[1]{\@tfor\tempyyy:=#1\do{\ifcase\tempyyy
    \PackageError{braille}{Bad dots #1}%
    {Can only display dots 1-8}\or
    \put(\ifbraillemirror1\else0\fi.5,2.5){\brailledot}\or
    \put(\ifbraillemirror1\else0\fi.5,1.5){\brailledot}\or
    \put(\ifbraillemirror1\else0\fi.5,0.5){\brailledot}\or
    \put(\ifbraillemirror0\else1\fi.5,2.5){\brailledot}\or
    \put(\ifbraillemirror0\else1\fi.5,1.5){\brailledot}\or
    \put(\ifbraillemirror0\else1\fi.5,0.5){\brailledot}\or
    \ifbrailleeightdots\put(\ifbraillemirror1\else0\fi.5,-.5){\brailledot}\else
    \PackageError{braille}{Bad dots in #1}{Use option 8dots to use
      eight dot braille}\fi\or
    \ifbrailleeightdots\put(\ifbraillemirror0\else1\fi.5,-.5){\brailledot}\else
    \PackageError{braille}{Bad dots in #1}{Use option 8dots to use
      eight dot braille}\fi\else
    \PackageError{braille}{Bad dots #1}%
    {Can only display dots 1-8}\fi}}

\ifbraillecompact

\newcommand\braille[1]{\PackageError{braille}{Command \protect\braille\space
    not loaded}{Do not use option compact, then you will get
    \protect\braille}}

\else

% \newcommand{\braille}[1]{\@tfor\tempxxx:=#1\do{\old@char{\tempxxx}}}
%
%  \braille{ab{the} c} runs sequence of predefined command associated
%  with each character or string in {}.  For example,
%       \BRAILLEa\BRAILLEb\BRAILLEthe\BRAILLEc
%  Skips all whitespaces.
%
%
% {\obeyspaces\gdef\braille{\begingroup\obeyspaces\@braille}}
% \def\@braille#1{\@tfor\tempxxx:=#1\do{\old@char{\tempxxx}}\endgroup}
%
%  Here, each space is dutifully printed with \hskip.  However, since
%  any previous spaces are removed with \unskip, multiple spaces
%  collapse to one space.  It still skips \t and \n, though.  So, for
%  multiple lines, put spaces at the beginning of each line.  Spaces
%  at the end of line will be ignored.
%
%
%  -- Fixed by Dominique:
%
\def\braille{\begingroup
    \catcode`\ =12
    \catcode`\^^M=12
    \let\brl@@spc\undefined
    \@braille}
\def\@braille#1{\@tfor\tempxxx:=#1\do{\old@char{\tempxxx}%
    \let\brl@@spc\brl@spc\let\brl@spc\undefined}\endgroup}


%  \new@char{new}{commands}     define \BRAILLE... command
%  \old@char{old}               run \BRAILLE... command
%
\newcommand{\new@char}[2]{%
    \@ifundefined{BRAILLE#1}%
        {\@namedef{BRAILLE#1}{#2}}%
        {\@latex@error{Braille symbol for {#1} already defined}\@ehc}%
}
\newcommand{\old@char}[1]{%
    \@ifundefined{BRAILLE#1}%
        {\@latex@error{Braille symbol for {#1} not defined}\@ehc}%
        {\@nameuse{BRAILLE#1}}%
}

                              
%  \def@char{new}{123456}               define one braille code
%  \ln@char{old}{new}                   ln old new
%  \ln@@char{olda}{oldb}{new}           ln olda+oldb new
%  \prefix@char{123456}{old}{new}       ln prefix+old new
%  \suffix@char{old}{123456}{new}       ln old+suffix new
%
\newcommand{\def@char}[2]{\new@char{#1}{\braillebox{#2}}}
\newcommand{\ln@char}[2]{\new@char{#2}{\old@char{#1}}}
\newcommand{\ln@@char}[3]{\new@char{#3}{\old@char{#1}\old@char{#2}}}
\newcommand{\prefix@char}[3]{\new@char{#3}{\braillebox{#1}\old@char{#2}}}
\newcommand{\suffix@char}[3]{\new@char{#3}{\old@char{#1}\braillebox{#2}}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Defines  \BRAILLE...
%  where ... can be one or more characters.

%  blank space  -- Fixed by Dominique:
\newcommand\brl@space{%
  \ifx\brl@@spc\undefined
  \ifbrailleuseemptybox
    \relax\braillebox{}\relax
  \else
    \unskip\hskip3\brailleunit\relax
  \fi\fi
  \let\brl@spc\@ne}  % 6mm
\new@char{ }{\brl@space}
\catcode`\^^M=12\relax%
\new@char{^^M}{\brl@space}%
\catcode`\^^M=5\relax%

%  Prefix
\def@char{Number}{3456}
\def@char{Letter}{56}
\def@char{Capital}{6}
\ln@@char{Capital}{Capital}{Upper}
\def@char{Italic}{46}

%  Letter and number
\def@char{a}{1}     \prefix@char{6}{a}{A} \ln@char{a}{1}
\def@char{b}{12}    \prefix@char{6}{b}{B} \ln@char{b}{2} \ln@char{b}{but}
\def@char{c}{14}    \prefix@char{6}{c}{C} \ln@char{c}{3} \ln@char{c}{can}
\def@char{d}{145}   \prefix@char{6}{d}{D} \ln@char{d}{4} \ln@char{d}{do} 
\def@char{e}{15}    \prefix@char{6}{e}{E} \ln@char{e}{5} \ln@char{e}{every}
\def@char{f}{124}   \prefix@char{6}{f}{F} \ln@char{f}{6} \ln@char{f}{from}
\def@char{g}{1245}  \prefix@char{6}{g}{G} \ln@char{g}{7} \ln@char{g}{go} 
\def@char{h}{125}   \prefix@char{6}{h}{H} \ln@char{h}{8} \ln@char{h}{have}
\def@char{i}{24}    \prefix@char{6}{i}{I} \ln@char{i}{9}
\def@char{j}{245}   \prefix@char{6}{j}{J} \ln@char{j}{0} \ln@char{j}{just}
\def@char{k}{13}    \prefix@char{6}{k}{K}                \ln@char{k}{knowledge}
\def@char{l}{123}   \prefix@char{6}{l}{L}                \ln@char{l}{like}    
\def@char{m}{134}   \prefix@char{6}{m}{M}                \ln@char{m}{more}    
\def@char{n}{1345}  \prefix@char{6}{n}{N}                \ln@char{n}{not}     
\def@char{o}{135}   \prefix@char{6}{o}{O}
\def@char{p}{1234}  \prefix@char{6}{p}{P}                \ln@char{p}{people}  
\def@char{q}{12345} \prefix@char{6}{q}{Q}                \ln@char{q}{quite}   
\def@char{r}{1235}  \prefix@char{6}{r}{R}                \ln@char{r}{rather}  
\def@char{s}{234}   \prefix@char{6}{s}{S}                \ln@char{s}{so}      
\def@char{t}{2345}  \prefix@char{6}{t}{T}                \ln@char{t}{that}    
\def@char{u}{136}   \prefix@char{6}{u}{U}                \ln@char{u}{us}      
\def@char{v}{1236}  \prefix@char{6}{v}{V}                \ln@char{v}{very}    
\def@char{w}{2456}  \prefix@char{6}{w}{W}                \ln@char{w}{will}    
\def@char{x}{1346}  \prefix@char{6}{x}{X}                \ln@char{x}{it}      
\def@char{y}{13456} \prefix@char{6}{y}{Y}                \ln@char{y}{you}     
\def@char{z}{1356}  \prefix@char{6}{z}{Z}                \ln@char{z}{as}      

%  Punctuation
\def@char{,}{2}     \ln@char{,}{ea}
\def@char{;}{23}    \ln@char{;}{bb}     \ln@char{;}{be}
\def@char{:}{25}    \ln@char{:}{cc}     \ln@char{:}{con}
\def@char{.}{256}   \ln@char{.}{$}      \ln@char{.}{dd}     \ln@char{.}{dis}
\def@char{!}{235}   \ln@char{!}{ff}     \ln@char{!}{to}
\def@char{(}{2356}  \ln@char{(}{)}      \ln@char{(}{gg}     \ln@char{(}{were}
\def@char{``}{236}  \ln@char{``}{?}     \ln@char{``}{his}
\def@char{''}{356}  \ln@char{''}{by}    \ln@char{''}{was}
\def@char{-}{36}    \ln@char{-}{com}
\def@char{'}{3}
\def@char{/}{34}
\prefix@char{6}{(}{[}
\suffix@char{)}{3}{]}
\prefix@char{6}{``}{.`}         % `...' is same as {.`}...{'.} which is
\suffix@char{''}{3}{'.}         % similar to actual braille code.
\ln@@char{in}{in}{*}
\ln@@char{:}{p}{percent}        % spell out \%

%  Multi letter
\def@char{and}{12346}
\def@char{for}{123456}
\def@char{of}{12356}
\def@char{the}{2346}
\def@char{with}{23456}

\def@char{ch}{16}       \ln@char{ch}{child}
\def@char{sh}{146}      \ln@char{sh}{shall}
\def@char{th}{1456}     \ln@char{th}{this}
\def@char{wh}{156}      \ln@char{wh}{which}
\def@char{ou}{1256}     \ln@char{ou}{out} 
\def@char{st}{34}       \ln@char{st}{still}     % same as /

\def@char{ar}{345}
\def@char{er}{12456}
\def@char{ed}{1246}
\def@char{gh}{126}
\def@char{ow}{246}

%  Miscellaneous
\def@char{ble}{3456}    % same as {Number}
\def@char{ing}{346}
\def@char{in}{35}
\ln@@char{in}{to}{into}
\def@char{en}{26}       \ln@char{en}{enough}

%  Final letter contraction
\prefix@char{46}{d}{ound}
\prefix@char{46}{e}{ance}
\prefix@char{46}{n}{sion}
\prefix@char{46}{s}{less}
\prefix@char{46}{t}{ount}

\prefix@char{56}{e}{ence}
\prefix@char{56}{g}{ong}
\prefix@char{56}{l}{ful}
\prefix@char{56}{n}{tion}
\prefix@char{56}{s}{ness}
\prefix@char{56}{t}{ment}
\prefix@char{56}{y}{ity}

\prefix@char{6}{n}{ation}
\prefix@char{6}{y}{ally}

%  Initial letter contraction
\prefix@char{45}{u}{upon}
\prefix@char{45}{w}{word}
\prefix@char{45}{th}{those}
\prefix@char{45}{wh}{whose}
\prefix@char{45}{the}{these}

\prefix@char{456}{c}{cannot}
\prefix@char{456}{h}{had}
\prefix@char{456}{m}{many}
\prefix@char{456}{s}{spirit}
\prefix@char{456}{w}{world}
\prefix@char{456}{the}{their}

\prefix@char{5}{d}{day}
\prefix@char{5}{e}{ever}
\prefix@char{5}{f}{father}
\prefix@char{5}{h}{here}
\prefix@char{5}{k}{know}
\prefix@char{5}{l}{lord}
\prefix@char{5}{m}{mother}
\prefix@char{5}{n}{name}
\prefix@char{5}{o}{one}
\prefix@char{5}{p}{part}
\prefix@char{5}{q}{question}
\prefix@char{5}{r}{right}
\prefix@char{5}{s}{some}
\prefix@char{5}{t}{time}
\prefix@char{5}{u}{under}
\prefix@char{5}{w}{work}
\prefix@char{5}{y}{young}

\prefix@char{5}{ch}{character}
\prefix@char{5}{th}{through}
\prefix@char{5}{wh}{where}
\prefix@char{5}{ou}{ought}
\prefix@char{5}{the}{there}

%  Abbreviation for whole word
\new@char{about}{\braille{ab}}
\new@char{above}{\braille{abv}}
\new@char{according}{\braille{ac}}
\new@char{across}{\braille{acr}}
\new@char{after}{\braille{af}}
\new@char{afternoon}{\braille{afn}}
\new@char{afterward}{\braille{afw}}
\new@char{again}{\braille{ag}}
\new@char{against}{\braille{ag{st}}}
\new@char{almost}{\braille{alm}}
\new@char{already}{\braille{alr}}
\new@char{also}{\braille{al}}
\new@char{although}{\braille{al{th}}}
\new@char{altogether}{\braille{alt}}
\new@char{always}{\braille{alw}}
\new@char{because}{\braille{{be}c}}
\new@char{before}{\braille{{be}f}}
\new@char{behind}{\braille{{be}h}}
\new@char{below}{\braille{{be}l}}
\new@char{beneath}{\braille{{be}n}}
\new@char{beside}{\braille{{be}s}}
\new@char{between}{\braille{{be}t}}
\new@char{beyond}{\braille{{be}y}}
\new@char{blind}{\braille{bl}}
\new@char{braille}{\braille{brl}}
\new@char{children}{\braille{{ch}n}}
\new@char{conceive}{\braille{{con}cv}}
\new@char{conceiving}{\braille{{con}cvg}}
\new@char{could}{\braille{cd}}
\new@char{deceive}{\braille{dcv}}
\new@char{deceiving}{\braille{dcvg}}
\new@char{declare}{\braille{dcl}}
\new@char{declaring}{\braille{dclg}}
\new@char{either}{\braille{ei}}
\new@char{first}{\braille{f{st}}}
\new@char{friend}{\braille{fr}}
\new@char{good}{\braille{gd}}
\new@char{great}{\braille{grt}}
\new@char{herself}{\braille{h{er}f}}
\new@char{him}{\braille{hm}}
\new@char{himself}{\braille{hmf}}
\new@char{immediate}{\braille{imm}}
\new@char{its}{\braille{xs}}
\new@char{itself}{\braille{xf}}
\new@char{letter}{\braille{lr}}
\new@char{little}{\braille{ll}}
\new@char{much}{\braille{m{ch}}}
\new@char{must}{\braille{m{st}}}
\new@char{myself}{\braille{myf}}
\new@char{necessary}{\braille{nec}}
\new@char{neither}{\braille{nei}}
\new@char{o'clock}{\braille{o'c}}
\new@char{oneself}{\braille{{one}f}}
\new@char{ourselves}{\braille{{ou}rvs}}
\new@char{paid}{\braille{pd}}
\new@char{perceive}{\braille{p{er}cv}}
\new@char{perceiving}{\braille{p{er}cvg}}
\new@char{perhaps}{\braille{p{er}h}}
\new@char{quick}{\braille{qk}}
\new@char{receive}{\braille{rcv}}
\new@char{receiving}{\braille{rcvg}}
\new@char{rejoice}{\braille{rjc}}
\new@char{rejoicing}{\braille{rjcg}}
\new@char{said}{\braille{sd}}
\new@char{should}{\braille{{sh}d}}
\new@char{such}{\braille{s{ch}}}
\new@char{themselves}{\braille{{the}mvs}}
\new@char{thyself}{\braille{{th}yf}}
\new@char{today}{\braille{td}}
\new@char{together}{\braille{tgr}}
\new@char{tomorrow}{\braille{tm}}
\new@char{tonight}{\braille{tn}}
\new@char{would}{\braille{wd}}
\new@char{your}{\braille{yr}}
\new@char{yourself}{\braille{yrf}}
\new@char{yourselves}{\braille{yrvs}}

\fi