% Copyright 2010--2018 TeX Users Group.
% Copyright 1994--1997 Y&Y, Inc.

% Released under the LaTeX Project Public License, any version.

% ============================================================================
% Code to allow plain TeX and LaTeX to fully exploit `texnansi.vec' encoding
%		(TeX 'n ANSI)	VERSION 2.8 (2018 May 25)
% ============================================================================

% \input texnansi in TeX source when using fonts with `TeX n ANSI' encoding.

% Note that a lot of code that already exists in plain.tex is commented out
% --- it is included here merely for reference.

% `plain' TeX - and `lplain' TeX - have accents hard-wired to certain codes.
% When a font is encoded differently, compound characters and accents move.
% This file deals with this for TeX 'n ANSI encoding.

% NOTE:  Default is to use the 58 ready-made accented/composite characters
% that occur in text fonts directly, using the macros defined below.
% To instead build them by overprinting, use \buildcomposites.
% To instead link to them via pseudo ligatures, use \usecomposites.

% NOTE: texnansi.vec is designed to avoid char codes 0, 9, 10, 13, 32, 127
% since some brain-dead software has trouble with some of these...

% NOTE: definitions have embedded numbers that depend on the chosen encoding
% These will need to be changed if you use an encoding other than `TEXNANSI'

% --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
% First, protect against style files that make quotedbl `active'

\chardef\dblcode=\catcode`\"	% save catcode of quotedbl
\catcode`\"=12			% make quotedbl `other'

\chardef\atcode=\catcode`\@	% save catcode of atsign
\catcode`\@=11			% make atsign `letter'
% --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

% NOTE: plain TeX (and LaTeX) has the accent character positions hardwired to:

% 16 for `dotlessi',	   17 for `dotlessj',	18 for `grave',	
% 19 for `acute',	   20 for `caron',	21 for `breve',	
% 22 for `macron',	   23 for `ring',	24 for `cedilla',
% 25 for `germandbls',	   26 for `ae',		27 for `oe',
% 28 for `oslash',	   29 for `AE',		30 for 'OE',	
% 31 for `Oslash',	   94 for `circumflex',	95 for `dotaccent',
% 125 for `hungarumlaut',  126 for `tilde',	127 for `dieresis',
% The *real* troublesome ones are `dotaccent' and `hungarumlaut'
% --- and CM fonts do not have `ogonek' --- which we add here.
% (see page 356 of the TeX book for additional information)

% --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

% \ae, \oe, \o, \AE, \OE, \O, \ss are in same place as in TeX text

% Following constructed from pieces in CM, but exist in Type 1 text fonts

\def\aa{^^e5} \def\AA{^^c5}		% aring (229), Aring (197)
% \def\cc{^^e7} \def\CC{^^c7}		% ccedilla (231), Ccedilla (199)

% NOTE: \cc may conflict with `carbon copy' in letter.sty ...

\def\l{^^90} \def\L{^^80}		% lslash (144), Lslash (128)

% TEXNANSI also has the Icelandic characters:

\def\th{^^fe} \def\TH{^^de}		% thorn (254), Thorn (222)
\def\dh{^^f0} \def\DH{^^d0}		% eth (240), Eth (208)

\def\pounds{^^a3}	\def\copyright{^^a9}	% 163 and 169
\def\registered{^^ae}	\def\trademark{^^99}	% 174 and 153
% \def\dag{^^86}	\def\ddag{^^87}	% dagger (134), daggerdbl (135)
\def\S{^^a7}		\def\P{^^b6}	% section (167), paragraph (182)

% Make it more convenient to use { } _ | \ glyphs direct from text font ?

% \chardef\{=`\{ \chardef\}=`\} \chardef\_=`\_
% \chardef\|=`\| \chardef\backslash=`\\

% --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

% Following set up for the 58 accented characters/composites in text fonts:

% aacute, acircumflex, adieresis, agrave, aring, atilde, ccedilla,
% eacute, ecircumflex, edieresis, egrave, iacute, icircumflex,
% idieresis, igrave, ntilde, oacute, ocircumflex, odieresis, ograve, otilde,
% scaron, uacute, ucircumflex, udieresis, ugrave, yacute, ydieresis, zcaron,
% Aacute, Acircumflex, Adieresis, Agrave, Aring, Atilde, Ccedilla,
% Eacute, Ecircumflex, Edieresis, Egrave, Iacute, Icircumflex,
% Idieresis, Igrave, Ntilde, Oacute, Ocircumflex, Odieresis, Ograve, Otilde,
% Scaron, Uacute, Ucircumflex, Udieresis, Ugrave, Yacute, Ydieresis, Zcaron,

\def\`#1{% grave
\if#1a^^e0\else%	\char224 
\if#1e^^e8\else%	\char232 
\if#1i^^ec\else%	\char236 
\if#1o^^f2\else%	\char242 
\if#1u^^f9\else%	\char249 
\if#1A^^c0\else%	\char192 
\if#1E^^c8\else%	\char200 
\if#1I^^cc\else%	\char204 
\if#1O^^d2\else%	\char210 
\if#1U^^d9\else%	\char217 
% \if#1{\i}^^ec\else%	\char236 
\if#1\i^^ec\else%	\char236 
{\accent18 #1}%
\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi}

\def\'#1{% acute
\if#1a^^e1\else%	\char225 
\if#1e^^e9\else%	\char233
\if#1i^^ed\else%	\char237
\if#1o^^f3\else%	\char243 
\if#1u^^fa\else%	\char250 
\if#1y^^fd\else%	\char253 
\if#1A^^c1\else%	\char193 
\if#1E^^c9\else%	\char201 
\if#1I^^cd\else%	\char205 
\if#1O^^d3\else%	\char211 
\if#1U^^da\else%	\char218 
\if#1Y^^dd\else%	\char221 
% \if#1{\i}^^ed\else%	\char237 
\if#1\i^^ed\else%	\char237 
{\accent19 #1}%
\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi}

\def\^#1{% circumflex
\if#1a^^e2\else%	\char226 
\if#1e^^ea\else%	\char234 
\if#1i^^ee\else%	\char238 
\if#1o^^f4\else%	\char244 
\if#1u^^fb\else%	\char251 
\if#1A^^c2\else%	\char194 
\if#1E^^ca\else%	\char202 
\if#1I^^ce\else%	\char206 
\if#1O^^d4\else%	\char212 
\if#1U^^db\else%	\char219 
% \if#1{\i}^^ee\else%	\char238 
\if#1\i^^ee\else%	\char238 
{\accent94 #1}%
\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi}

\def\~#1{% tilde
\if#1a^^e3\else%	\char227 
\if#1n^^f1\else%	\char241 
\if#1o^^f5\else%	\char245 
\if#1A^^c3\else%	\char195 
\if#1N^^d1\else%	\char209 
\if#1O^^d5\else%	\char213 
{\accent126 #1}%
\fi\fi\fi\fi\fi\fi}

\def\"#1{% dieresis
\if#1a^^e4\else%	\char228 
\if#1e^^eb\else%	\char235 
\if#1i^^ef\else%	\char239 
\if#1o^^f6\else%	\char246 
\if#1u^^fc\else%	\char252 
\if#1y^^ff\else%	\char255 
\if#1A^^c4\else%	\char196 
\if#1E^^cb\else%	\char203 
\if#1I^^cf\else%	\char207 
\if#1O^^d6\else%	\char214 
\if#1U^^dc\else%	\char220 
\if#1Y^^9f\else%	\char159 
% \if#1{\i}^^ef\else%	\char239 
\if#1\i^^ef\else%	\char239 
% {\accent127 #1}%
{\accent168 #1}%
\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi}

\def\v #1{% caron
\if#1s^^9a\else%	\char154 
\if#1z^^9d\else%	\char157 
\if#1S^^8a\else%	\char138 
\if#1Z^^8d\else%	\char141 
{\accent20 #1}%
\fi\fi\fi\fi}

\def\r #1{% ring
\if#1a^^e5\else%	\char229 
\if#1A^^c5\else%	\char197 
{\accent23 #1}%
\fi\fi}

\def\c #1{% cedilla
\if#1c^^e7\else%	\char231 
\if#1C^^c7\else%	\char199 
{\setbox0\hbox{#1}\ifdim\ht0=1ex\accent24 #1%
  \else{\ooalign{\hidewidth\char24\hidewidth\crcr\unhbox0}}\fi}%
\fi\fi}

\def\H#1{{\accent6 #1}}%	 hungarumlaut	% not in 125
\def\u#1{{\accent21 #1}}%	 breve

% dotaccent and macron have to be treated differently in AMS TeX

\ifx\amstexloaded@\relax%
\def\D#1{{\accent5 #1}}		% dotaccent	% not in 95
\def\B#1{{\accent22 #1}}	% macron
\let\graveaccent\`
\let\acuteaccent\'
\let\tildeaccent\~
\let\hataccent\^
\let\underscore\_
\else
\def\.#1{{\accent5 #1}}		% dotaccent	% not in 95
\def\=#1{{\accent22 #1}}\fi	% macron

% underline (using macron at 22)
\def\b#1{\oalign{#1\crcr\hidewidth
    \vbox to.2ex{\hbox{\char22}\vss}\hidewidth}}%

% ogonek (using ogonek at 7)
\def\k#1{\setbox0\hbox{#1}\ifdim\ht0=1ex\accent7 #1%
  \else{\ooalign{\hidewidth\char7\hidewidth\crcr\unhbox0}}\fi}%

% --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

% For backward compatability, provide for use of font with TeX's \accent:
% (It is however generally better to use ready-made accented characters, since
% \accent creates explicit kerning, which breaks the hyphenation machinery)
% NOTE: Use \buildcomposites only if you want to force accented/composite
% characters to be constructed by `overprinting' base and accent.

\def\buildcomposites{%
% following four lines are for TeX n ANSI encoding only
\def\.##1{{\accent5 ##1}}%	 dotaccent	% not in 95
\def\H##1{{\accent6 ##1}}%	 hungarumlaut	% not in 125
\def\k##1{{\accent7 ##1}}%	 ogonek		% not in CM fonts
\def\"##1{{\accent168 ##1}}%	 dieresis	% alternate to 127
% following are plain TeX defaults (needed only to undo \usecomposites)
\def\`##1{{\accent18 ##1}}%	 grave
\def\'##1{{\accent19 ##1}}%	 acute
\def\^##1{{\accent94 ##1}}%	 circumflex
\def\~##1{{\accent126 ##1}}%	 tilde
\def\=##1{{\accent22 ##1}}%	 macron
\def\v##1{{\accent20 ##1}}%	 caron
\def\u##1{{\accent21 ##1}}%	 breve
% \def\"##1{{\accent127 ##1}}%	 dieresis	% use 168 instead
% underline and cedilla accents (macron at 22, cedilla at 24)
\def\b##1{\oalign{##1\crcr\hidewidth
    \vbox to.2ex{\hbox{\char22}\vss}\hidewidth}}%
\def\c##1{\setbox0\hbox{##1}\ifdim\ht0=1ex\accent24 ##1%
  \else{\ooalign{\hidewidth\char24\hidewidth\crcr\unhbox0}}\fi}%
}

% --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

% USING READY-MADE ACCENTED CHARACTERS:
% We can also use pre-built accented character using pseudo ligatures ---
% provided the TFM files for the text fonts have them wired in.
% The following assumes that the TFM files have the required pseudo-ligatures.
% Use -vadj on the command line with AFMtoTFM to set up these pseudo-ligatures.
% This only works for the 58 ready-made accented/composite characters.

\def\usecomposites{%
\chardef\`=18	% grave
\chardef\'=19	% acute
\chardef\^=94	% circumflex
\chardef\~=126	% tilde
\chardef\"=127	% dieresis	% must match TFM, hence do *not* use 168 here
\chardef\c=24	% cedilla	% only for ccedilla, Ccedilla
\chardef\v=20	% caron		% only for scaron, zcaron, Scaron, Zcaron
\chardef\r=23	% ring		% non-standard, for Aring and aring
% and some extra goodies
\chardef\.=5	% dotaccent	% no standard accented chars use this
\chardef\H=6	% hungarumlaut	% no standard accented chars use this
\chardef\k=7	% ogonek	% no standard accented chars use this
\chardef\u=21	% breve		% no standard accented chars use this
\chardef\==22	% macron	% no standard accented chars use this
% \chardef\i=16	% dotlessi
}

% NOTE: To use the above scheme, use \'a, NOT \'{a}, and \c c, NOT \c{c}.

% --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

% \buildcomposites	% build composites by overprinting base and accent
% \usecomposites	% use 58 ready-made composites via pseudo ligatures

% Default is to use 58 ready-made composites, and build others by overprinting.

% --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

% Changes required in math macros when roman font is reencoded to `texnansi'.
% (An alternative is to draw the accents from the math fonts)

% Most accents are in same place as in TeX text encoding (except dotaccent)

\def\usetextaccents{
% \def\grave{\mathaccent"7012 }	%  18 grave
% \def\acute{\mathaccent"7013 }	%  19 acute
% \def\hat{\mathaccent"705E }	%  94 circumflex
% \def\tilde{\mathaccent"707E }	% 126 tilde
% \def\ddot{\mathaccent"707F }	% 127 dieresis
\def\ddot{\mathaccent"70A8 }	% 168 dieresis
% \def\bar{\mathaccent"7016 }	%  22 macron
% \def\check{\mathaccent"7014 }	%  20 caron
% \def\breve{\mathaccent"7015 }	%  21 breve
\def\dot{\mathaccent"7005 }	%   5 dotaccent
}

% Do not redefine accents in math mode when using MathTime,
% since MathTime uses accents in the math fonts - not those in the text font

% In LaTeX 2e check whether mathtime package has been loaded,
% otherwise check whether \MTMI is defined

\ifx\@ifpackageloaded\undefined\ifx\MTMI\undefined\usetextaccents\fi
\else\@ifpackageloaded{mathtime}{}{\usetextaccents}\fi

% **************************************************************************

% If you want to use < for `guilsinglleft' and > for `guilsinglright'
% then uncomment the following lines:

% \catcode`\<=\active \chardef<=139
% \catcode`\>=\active \chardef>=155

% If you use < for `exclamdown', > for `questiondown', and | for `emdash'
% then uncomment the following lines:

% \catcode`\<=\active \chardef<=161
% \catcode`\>=\active \chardef>=191
% \catcode`\|=\active \chardef|=151

% \chardef\lq=96 \chardef\rq=39

% Note that \lq and \rq also provide access to ` and '

\catcode`\@=\atcode		% restore original catcode of atsign

\catcode`\"=\dblcode		% restore original catcode of quotedbl

% If you use " for quotedblright then uncomment the following:

% \catcode`\"=\active \chardef"=148

% For proper hyphenation of words with accented characters we need to let
% TeX know how to translate words with accented characters to lower case.
% Hence define \lccode so that one can use them in \hyphenation{...}
% Just for fun, we also define \uccode, and set the \catcode to letter...

% Mostly in ISO Latin 1, accented characters (and a few special characters)
% occur in upper case form in the range 192 - 222, which is offset
% by 32 from the corresponding lower case forms in the range 224 - 254.

\bgroup
\newcount\n\newcount\m\n=224\m=192
\loop\ifnum\n<255
\global\catcode\n=11\global\catcode\m=11
\global\lccode\n=\n\global\lccode\m=\n
\global\uccode\m=\m\global\uccode\n=\m
\advance\n by1\advance\m by1\repeat
\egroup

% Then there are just a few odd ones in TEXNANSI that don't fit this pattern

\catcode140=11\catcode156=11	% OE, oe
\lccode140=156\lccode156=156
\uccode140=140\uccode156=140
\catcode159=11\catcode255=11	% Ydieresis, ydieresis
\lccode159=255\lccode255=255
\uccode159=159\uccode255=159
\catcode138=11\catcode154=11	% Scaron, scaron
\lccode138=154\lccode154=154
\uccode138=138\uccode154=138
\catcode141=11\catcode157=11	% Zcaron, zcaron
\lccode141=157\lccode157=157
\uccode157=141\uccode141=141
\catcode128=11\catcode144=11	% Lslash, lslash
\lccode128=144\lccode144=144
\uccode144=128\uccode128=128

\endinput