% \iffalse meta-comment
%
% Copyright (C) 2018 by Leah Grace Velleman
%
% This file 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.
%
% \fi

% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[2018/01/01]
%<package>\ProvidesPackage{stoneipa}
%<package> [2018/07/07 v0.8 Stone Sans Phonetic font support] 
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{stoneipa}
\setromanfont[Scale=MatchLowercase]{Cambria}
\setmonofont[Scale=MatchLowercase]{Consolas}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
\DocInput{stoneipa.dtx}
\end{document}
%</driver>
%\fi


% \title{The \textsf{StoneIPA} package}
% \author{Leah Grace Velleman \\ leahvelleman@gmail.com}
% \maketitle
%
% \section{About}
% Stone Sans Phonetic is an excellent IPA font designed by Sumner Stone in the
% 1980s. It harmonizes well with its companions Stone Serif and Stone Sans, and
% also with a number of other popular text fonts.
%
%  It remains commercially available, but in an idiosyncratic
% pre-Unicode encoding. 
%
% This package --- designed for use with XeLaTeX --- sets
% up Stone Sans as the sans-serif document font, and sets up the Unicode
% characters in the IPA range so that they will produce the correct glyphs from
% Stone Sans Phonetic. It does not include the fonts themselves, which must be
% purchased separately.
%
%\begin{figure}
% \sffamily
%\begin{center}
%\begin{tabular}{lllllllllllllllllllll}
%    p & b &   &   &   &   & t & d &   &   & ʈ & ɖ & c & ɟ & k & g & q & ɢ &   & ʔ \\
%    ƥ & ɓ &   &   &   &   & ƭ & ɗ &   &   &   &   & ƈ & ʄ & ƙ & ɠ & ʠ & ʛ \\
%      & m &   & ɱ &   &   &   & n &   &   &   & ɳ &   & ɲ &   & ŋ &   & ɴ \\
%      & ʙ &   &   &   &   &   & r &   &   &   &   &   &   &   &   &   & ʀ \\
%      &   &   &   &   &   &   & ɾ &   &   &   & ɽ \\
%    ɸ & β & f & v & θ & ð & s & z & ʃ & ʒ & ʂ & ʐ & ç & ʝ & x & ɣ & χ & ʁ & ħ ʕ & h ɦ \\
%      &   &   &   &   &   & ɬ & ɮ \\
%      &   &   & ʋ &   &   &   & ɹ &   &   &   & ɻ &   & j &   & ɰ \\
%      &   &   &   &   &   &   & l &   &   &   & ɭ &   & ʎ &   & ʟ\\
%\end{tabular}
%\caption{The IPA consonants in Stone Sans Phonetic}
%\end{center}
%\end{figure}
%\begin{figure}
%\sffamily
%\begin{center}
%\begin{tabular}{llllll}
%            i & y & ɨ & ʉ & ɯ & u \\
%            ɪ & ʏ &   &   &   & ʊ \\
%            e & ø & ɘ & ɵ & ɤ & o \\
%              &   & ə \\
%            ɛ & œ & ɜ & ɞ & ʌ & ɔ \\
%            æ &   & ɐ \\
%            a & ɶ &   &   & ɑ & ɒ\\
%\end{tabular} 
%\caption{The IPA vowels in Stone Sans Phonetic and Stone Sans Phonetic Alternates}
%\end{center}
%\end{figure}
%\begin{figure}
%\sffamily
%\begin{center}
%\begin{tabular}{lllll}
% ƥ & ƭ & ƙ & ʠ & ˀ \\
% š & č & ž \\
% λ & ƛ
%\end{tabular}
%\caption{Non-IPA characters from Stone Sans Phonetic and Stone Sans Phonetic Alternates}
%\end{center}
%\end{figure}
%
%\section{Requirements}
% \textsf{StoneIPA} requires XeLaTeX, \textsf{fontspec}, \textsf{newunicodechar}, and \textsf{pdftexcmds}. It also requires the fonts Stone Sans, Stone Sans Phonetic
% and Stone Sans Phonetic Alternates.
%
%\section{Package options}
% \textsf{StoneIPA} uses your system installed versions of Stone Sans, Stone Sans
% Phonetic, and Stone Sans Phonetic Alternates. By default, it expects these
% fonts to be installed under the names |StoneSans|, |StoneSans-PhoneticIPA|, and
% |StoneSans-PhoneticAlternate|. If yours are installed under different names, 
% specify those names using the |sansfontname|, |phoneticfontname|, and
% |alternatefontname| options.
%
%\begin{verbatim}
%\usepackage[phoneticfontname=StoneSansIPA, 
%            alternatefontname=StoneSansAlt]{stoneipa}
%\end{verbatim}
%
% If some characters appear too small, use the |noalternatescaling| option. As
% of this writing, the version of Stone Sans Phonetic Alternates that is for sale
% on Adobe's website has characters about 10\% too large, and the \textsf{StoneIPA}
% package attempts to compensate for this. The |noalternatescaling| option
% turns off this compensation.
%\begin{verbatim}
%\usepackage[noalternatescaling]{stoneipa}
%\end{verbatim}

% \StopEventually{}
% \section{Implementation}
% \subsection{Option handling}
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}[1994/06/01]

\RequirePackage{fontspec}
\RequirePackage{newunicodechar}
\RequirePackage{pdftexcmds}
\RequirePackage{kvoptions}
\SetupKeyvalOptions{family=sipa, prefix=sipa@}
\DeclareStringOption[StoneSans]{fontname}
\DeclareStringOption[StoneSansSCITCTTMedium]{scfontname}
\DeclareStringOption[StoneSans-PhoneticIPA]{phoneticfontname}
\DeclareStringOption[StoneSans-PhoneticAlternate]{alternatefontname}
\DeclareBoolOption{noalternatescaling}
\ProcessKeyvalOptions*
%    \end{macrocode}
% \subsection{Font declarations}
%    \begin{macrocode}
\setsansfont{\sipa@fontname}[Scale=MatchLowercase, 
							 SmallCapsFont=\sipa@scfontname]

\newfontfamily{\sipafont}{\sipa@phoneticfontname}[Scale=MatchLowercase]

\ifsipa@noalternatescaling
    \newfontfamily{\sipaalternatefont}{\sipa@alternatefontname}[Scale=1.0]
\else
    \newfontfamily{\sipaalternatefont}{\sipa@alternatefontname}[Scale=0.9]
\fi
%    \end{macrocode}
% \subsection{Character macros}
% \DescribeMacro{\sipachoosephoneticchar} 
% \DescribeMacro{\sipachoosealternatechar}
% The Stone Sans Phonetic font is weirdly encoded. For instance, a symbol that 
% looks like \textsf{ʷ} is stored at the code point normally used for |+|. This means that
% sometimes, the Unicode in our |.tex| file will need to be translated. For
% instance, if we encounter a \textsf{ʷ} character when the current font family is Roman,
% we can print it as usual; but if we encounter the same character when the
% font family is sans serif, we should translate it to a |+| and print it in
% Stone Sans Phonetic. The |\sipachoosephoneticchar| and |\sipachoosealternatechar|% macros do this conditional translation:
% given two characters, when the current family is sans serif, they print the
% first in Stone Sans Phonetic; and when the current family is anything else,
% they print the second.
%    \begin{macrocode}
\def\sipachoosephoneticchar#1#2{%
    \ifnum\pdf@strcmp{\f@family}{\sfdefault}=\z@
        {\sipafont #1}%
    \else
        #2%
    \fi
}

\def\sipachoosealternatechar#1#2{%
    \ifnum\pdf@strcmp{\f@family}{\sfdefault}=\z@
        {\sipaalternatefont #1}%
    \else
        #2%
    \fi
}
%    \end{macrocode}
% \subsection{Character setup}
% \DescribeMacro{\sipasetup}\DescribeMacro{\sipaalternatesetup}
% Now we define two convenience functions that we will use to set up Unicode
% characters. The macro |\sipasetup#1#2| declares that the Stone Sans Phonetic glyph
% stored at |#1| should be used when the TeX source contains the Unicode character
% |#2|. The macro |\sipaalternatesetup| does the same, but for Stone Sans Phonetic
% Alternates.
%    \begin{macrocode}

\def\sipasetup#1#2{\newunicodechar{#2}{{\sipachoosephoneticchar{#1}{#2}}}}
\def\sipaalternatesetup#1#2{\newunicodechar{#2}{{\sipachoosealternatechar{#1}{#2}}}}

\sipasetup{a\$}{ā}
\sipasetup{e\$}{ē}
\sipasetup{i\$}{ī}
\sipasetup{o\$}{ō}
\sipasetup{u\$}{ū}
\sipasetup{y\$}{ȳ}
\sipasetup{+}{ʷ}
\sipasetup{:}{ː}
\sipasetup{?}{ʔ}
\sipasetup{@}{ə}
\sipasetup{A}{ɑ}
\sipasetup{B}{ʙ}
\sipasetup{C}{ç}
\sipasetup{D}{ð}
\sipasetup{E}{ɛ}
\sipasetup{F}{ɸ}
\sipasetup{G}{ɢ}
\sipasetup{H}{ʜ}
\sipasetup{I}{ɪ}
\sipasetup{J}{ɟ}
%\sipasetup{K}{ɨ}
\sipasetup{L}{ʟ}
\sipasetup{M}{ɰ}
\sipasetup{N}{ɴ}
\sipasetup{O}{ɔ}
\sipasetup{Q}{ɾ}
\sipasetup{R}{ʀ}
\sipasetup{S}{ʃ}
\sipasetup{T}{θ}
\sipasetup{U}{ʊ}
\sipasetup{V}{ʋ}
\sipasetup{W}{ɺ}
\sipasetup{X}{χ}
\sipasetup{Y}{ʏ}
\sipasetup{Z}{ʒ}
\sipasetup{\ }{ʘ}
\sipasetup{\{}{æ}
\sipasetup{®}{ɲ}
\sipasetup{¿}{ʕ}
\sipasetup{Á}{ɒ}
\sipasetup{Â}{β}
\sipasetup{Ã}{ɕ}
\sipasetup{Ä}{ɖ}
\sipasetup{Æ}{ɜ}
\sipasetup{Ç}{ʛ}
\sipasetup{È}{ɦ}
\sipasetup{É}{ħ}
\sipasetup{Ë}{ɬ}
\sipasetup{Ì}{ɮ}
\sipasetup{Í}{ɱ}
\sipasetup{Î}{ŋ}
\sipasetup{Ï}{ɶ}
\sipasetup{Ð}{ɻ}
\sipasetup{Ñ}{ɽ}
\sipasetup{Ò}{ʁ}
\sipasetup{Ô}{ʈ}
\sipasetup{Õ}{ɵ}
\sipasetup{×}{ʄ}
\sipasetup{Ø}{ɧ}
\sipasetup{Ú}{ʑ}
\sipasetup{Þ}{ʢ}
\sipasetup{à}{ɫ}
\sipasetup{á}{ɐ}
\sipasetup{â}{ɓ}
\sipasetup{ã}{ƈ}
\sipasetup{ä}{ɗ}
\sipasetup{å}{ɣ}
\sipasetup{\ae}{ɤ}
\sipasetup{Ç}{ɠ}
\sipasetup{è}{ɥ}
\sipasetup{é}{ɨ}
\sipasetup{ê}{ʝ}
\sipasetup{ë}{ƙ}
\sipasetup{ì}{ɭ}
\sipasetup{í}{ɯ}
\sipasetup{î}{ɳ}
\sipasetup{ï}{ø}
\sipasetup{\dh}{ƥ}
\sipasetup{ñ}{ʠ}
\sipasetup{ò}{ɹ}
\sipasetup{ó}{ʂ}
\sipasetup{ô}{ƭ}
\sipasetup{õ}{ʉ}
\sipasetup{ö}{ʌ}
\sipasetup{÷}{ʍ}
\sipasetup{ù}{ʎ}
\sipasetup{ú}{ʐ}
\sipasetup{û}{œ}
\sipaalternatesetup{?}{ˀ}
\sipaalternatesetup{e}{ɘ}
\sipaalternatesetup{å}{ɞ}
\sipaalternatesetup{c}{č}
\sipaalternatesetup{s}{š}
\sipaalternatesetup{z}{ž}
\sipaalternatesetup{l}{λ}
\sipaalternatesetup{ì}{ƛ}
%    \end{macrocode}
% \Finale
