%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part of the ptx2pdf macro package for formatting USFM text
% copyright (c) 2007-2020 by SIL International
% written by Jonathan Kew
%
% Permission is hereby granted, free of charge, to any person obtaining  
% a copy of this software and associated documentation files (the  
% "Software"), to deal in the Software without restriction, including  
% without limitation the rights to use, copy, modify, merge, publish,  
% distribute, sublicense, and/or sell copies of the Software, and to  
% permit persons to whom the Software is furnished to do so, subject to  
% the following conditions:
%
% The above copyright notice and this permission notice shall be  
% included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,  
% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF  
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  
% NONINFRINGEMENT. IN NO EVENT SHALL SIL INTERNATIONAL BE LIABLE FOR  
% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  
% CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION  
% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
% Except as contained in this notice, the name of SIL International  
% shall not be used in advertising or otherwise to promote the sale,  
% use or other dealings in this Software without prior written  
% authorization from SIL International.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Character style macros

\newwrite\f@ntl@g
\newif\ifLogFonts
\newif\ifb@ckgroundelsewhere \b@ckgroundelsewherefalse
\newtoks\end@box@run
\newtoks\start@box@run
\newdimen\minfontsize \minfontsize=4pt

\newif\ifbox@run % Advanced-use hook, activates end@box@run toklist
\newif\ifb@xedstyle % Some styles need putting in a box and reformatting, e.g. underline
\newif\ifCharstyleExtend\CharstyleExtendtrue  % Does \ft^130 get processed as 130%, or a mal-formed \ft ^130?


%+cchar_parsecolor
\def\hex#1{\count3=#1\relax%
  \edef\r@s{}\count1=0
  \loop%
    \count2=\count3 \divide\count2 by 16 \multiply\count2 by 16
    \advance\count3 by -\count2
    \ifnum\count3=0\edef\r@s{0\r@s}\else\ifnum\count3<10 \edef\r@s{\number\count3 \r@s}%
    \else\advance\count3 by -10 \edef\r@s{\ifcase\count3 A\or B\or C\or D\or E\or F\fi\r@s}\fi\fi%
    \divide\count2 by 16 \count3=\count2 \advance\count1 by 1
    \ifnum\count1<6\repeat%
  \count3=0 \count1=0 \count2=0
}

% Decimal BGR -> RGB hex
\def\colorhex#1{\count1=#1\divide\count1 by 256\multiply\count1 by 256
    \count2=#1\advance\count2 by -\count1 \multiply\count2 by 65536 % r << 16
    \count3=#1\divide\count3 by 65536 \multiply\count3 by 65536 % b << 16
    \advance\count1 by -\count3 \divide\count3 by 65536 % count1=g << 8, count3=b
    \advance\count3 by \count2 \advance\count3 by \count1%
    \hex{\count3}%
}

\def\ParseColor#1#2\end{\edef\t@mp{#1}\trace{F}{Color prefix is "#1"}%
    \if x\t@mp \edef\r@s{#2}\else\colorhex{#1#2}\fi}                                %(1)
%-cchar_parsecolor

%
% Each USFM character style marker is defined to call \ch@rstyle with the marker name as parameter
%

%+cchar_charstyle

\def\ch@rstyle{\ifinn@te\let\n@xt\ch@rstyleinnote\trace{sa}{In a note}\else
  \ifimplicitcl@se\let\n@xt\@ch@rstyle \trace{sa}{Implicit closing}\else
    \ifdoingt@ble\let\n@xt\ch@rstyleintbl\trace{sa}{In a table}\else
  \let\n@xt\ch@rstylepls\trace{sa}{Plus marker}\fi\fi\fi\n@xt}

\def\ch@rstyleinnote#1{%USFM3.1 handles note structure markers differently to character styles.
 \ifcsname STR@CT-#1\endcsname
    \trace{sa}{#1 Note structure marker}\let\n@xt\@ch@rstyle
 \else\trace{sa}{#1 Not Note structure marker}\let\n@xt\ch@rstylepls\fi
 \n@xt{#1}%
}
\def\ch@rstyleintbl#1{%USFM3.1 handles table structure markers differently to character styles.
 \ifcsname STR@CT-#1\endcsname
    \trace{sa}{Table structure marker}\let\n@xt\@ch@rstyle
 \else\let\n@xt\ch@rstylepls\fi
 \n@xt{#1}%
}

\def\structm@rker#1{\x@\let\csname STR@CT-#1\endcsname\empty}
\structm@rker{ft}
\structm@rker{fq}
\structm@rker{fr}
\structm@rker{fqa}
\structm@rker{fk}
\structm@rker{fl}
\structm@rker{fw}
\structm@rker{fp} % Is this cannonically followed by an ft?
%\structm@rker{fv} % Not structural??
\structm@rker{fdc}
\structm@rker{xo}
%\structm@rker{xop} % not structural
\structm@rker{xk}
\structm@rker{xq}
\structm@rker{xt}
\structm@rker{xta}

\def\@ch@rstyle#1{%
 \trace{sa}{CH@RSTYLE:#1 nesting:(\the\p@ranesting/\the\n@tenesting) \the\currentgrouplevel}%
 \xdef\newch@rstyle{\detokenize{#1}}% record the name of the style
 \catcode32=12 % make <space> an "other" character, so it won't be skipped by \futurelet
 \catcode13=12 % ditto for <return>
 \futurelet\n@xt\doch@rstyle % look at following character and call \doch@rstyle
}

%Version of above for plus versions
\def\ch@rstylepls#1{\leavevmode%Stacking character styles (at least in headers) seem to break paragraph styling if they are invoked before leaving vertical mode. It's something to do with grouping. On the assumption that there will *be* text coming, it ought to be safe to leave vertical mode on meeting a stacking character style
 \trace{sa}{CH@RSTYLEPLS:#1 nesting:(\the\p@ranesting/\the\n@tenesting) \the\currentgrouplevel}%
 \edef\newch@rstyle{\detokenize{#1}}% record the name of the style
 \catcode32=12 % make <space> an "other" character, so it won't be skipped by \futurelet
 \catcode13=12 % ditto for <return>
 %\tracingassigns=1
 \futurelet\n@xt\doch@rstylepls % look at following character and call \doch@rstylepls
}
%-cchar_charstyle

%+cchar_docharstyle_intro
\def\c@rrfontsize{12}
\catcode`\~=12 \lccode`\~=32 % we'll use \lowercase{~} when we need a category-12 space
\catcode`\_=12 \lccode`\_=13 % and \lowercase{_} for category-12 <return>
\lccode`\|=`\\
\let\thisch@rstyle\empty
\let\ch@rfontextend\empty
\lowercase{
%-cchar_docharstyle_intro
%+cchar_docharstyle
 \let\@@st@risk*
 \let\@@spc~
 \let\@@nl_
 \let\@@min-
 \def\startch@rstyle@misc{\startch@rstyle \relax}%
 \def\doch@rstyle{% here, \n@xt has been \let to the next character after the marker    %(1)
  \catcode32=10 % reset <space> to act like a space again
  \catcode13=10 % and <return> is also a space (we don't want blank line -> \par)
  %\tracingassigns=1
  %\trace{ct}{n@xt: '\meaning\n@xt'= \meaning\@st@risk?}%
  \ifx\n@xt\@@st@risk \let\n@xt@\endch@rstylen % check for "*", if so then we need to end the style
  \else
    \temptrue
    \ifx\n@xt\@@spc\global\let\n@xt@\startch@rstyle@spc\trace{ss}{space delimiter for \newch@rstyle, after \thisch@rstyle}\else
      \ifx\n@xt\@@nl\global\let\n@xt@\startch@rstyle@nl\trace{ss}{nl delimiter for \newch@rstyle}\else
        \ifx\n@xt\@@min\global\let\n@xt@\startmst@nestyle@minus\trace{ss}{minus delimiter for \newch@rstyle}\let\thismil@stone\newch@rstyle\tempfalse\else
	  \global\let\n@xt@\startch@rstyle@misc\trace{ss}{misc delimiter for \newch@rstyle (\meaning\n@xt = \meaning \cflexLtr )?}%
          \ifx \n@xt\cflexLtr\relax
            \ifCharstyleExtend
              \trace{ss}{extend syntax for \newch@rstyle}%
              \global\let\n@xt@\startch@rstyle@extend
            \else
              \trace{ss}{\newch@rstyle ^ found, but CharstyleExtend false. Check usage.}%
            \fi
          \fi
    \fi\fi\fi
    \iftemp
      \trace{ss}{group level before popping \the\currentgrouplevel}%
      \end@llpoppedstyles{\ss@Char}%a char style closes all other char styles, milestones do not.e
      \trace{ss}{group level after popping \the\currentgrouplevel}%
    \fi
  \fi % else we need to start it
  \let\ch@rfontextend\empty
  \xdef\stylet@pe{\ss@Char}%                                                                   %(2)
  %\tracingassigns=0
  \n@xt@} % chain to the start or end macro
 %doch@rstylepls is like doch@rstyle, but wraps the style in a group and generates a comma-separated stack of styles.
 \def\retrych@rstylepls~{\futurelet\n@xt\doch@rstylepls} % look at following character and call \doch@rstyle
 \def\doch@rstylepls{% here, \n@xt has been \let to the next character after the marker %(1)
  \catcode32=10 % reset <space> to act like a space again
  \catcode13=10 % and <return> is also a space (we don't want blank line -> \par)
  \trace{sa}{charstyleplus}%
  %\tracingassigns=1
  \ifx\n@xt\@@st@risk\let\n@xt@\endch@rstylepls % check for "*", if so then we need to end the style
  \else\ifx\n@xt\@@spc\trace{ss}{space delimiter for +\newch@rstyle, after \thisch@rstyle}%
      \ifr@gurgitated %has this bit of input been detokenized and retokenized? that adds spaces.
        \ifx\thisch@rstyle\newch@rstyle\let\n@xt@\retrych@rstylepls\else\let\n@xt@\startch@rstyle@spc\fi
      \else\let\n@xt@\startch@rstyle@spc
      \fi
    \else\ifx\n@xt\@@nl\trace{ss}{nl delimiter for +\newch@rstyle, after \thisch@rstyle}\let\n@xt@\startch@rstyle@nl
      \else\ifx\n@xt\@@min\trace{ss}{minus delimiter for +\newch@rstyle, after \thisch@rstyle}\let\n@xt@\startmst@nestyle@minus\let\thismil@stone\newch@rstyle
        \else
          \let\n@xt@\startch@rstyle
          \ifx\n@xt\cflexLtr\trace{ss}{extend syntax for +\newch@rstyle}%
            \ifCharstyleExtend\let\n@xt@\startch@rstyle@extend
            \else
              \message{\newch@rstyle ^ found, but CharstyleExtend false. Check usage.}%
            \fi
          \fi
    \fi\fi\fi
  \fi % else we need to start it
  %\tracingassigns=0
  \xdef\stylet@pe{\ss@ChrP}%                                                                   %(2)
  \n@xt@} % chain to the start or end macro
 % when \startch@rstyle is called, the following <space> or <return> has become category-12
 % so we have to explicitly consume it here as part of the macro parameter list
 \def\startch@rstyle@spc~{\startch@rstyle}%                                             %(3)
 \def\startch@rstyle@nl_{\startch@rstyle}%
}
{
 \catcode`\^=12 
 \gdef\startch@rstyle@extend^#1 {\edef\ch@rfontextend{\p@rcent#1xx\E}\startch@rstyle}%
}
%-cchar_docharstyle

%+cchar_endstyle
\def\endch@rstylen*{%Consume the star..
  \trace{ss}{end of \newch@rstyle}\trace{s}{endch@rstylen \newch@rstyle}%
  \def\d@##1+##2\E{\tempfalse\if ##1c\else\if ##1C\else\temptrue\fi\fi}\mctopnoms
  \iftemp\MSG{Unexpected closing marker for no plus \newch@rstyle * on stack \mcstack. Ignoring. implicitcl@se\ifimplicitcl@se true\else false\fi}\else\endch@rstyle*\fi
}
\def\endch@rstylepls*{%Consume the star..
  \trace{ss}{end of \newch@rstyle}\trace{s}{endch@rstylepls \newch@rstyle}%
  \def\d@##1+##2+##3\E{\edef\tmp{\detokenize{##2}}\edef\tmq{\newch@rstyle}\tempfalse\ifx\tmp\tmq\else\temptrue\MSG{\tmp !=\tmq}\fi}\mctopnoms
  \iftemp\MSG{Unexpected closing marker for plus +\newch@rstyle * on stack \mcstack. Ignoring. (implicitcl@se\ifimplicitcl@se true\else false\fi)}\else\endch@rstyle*\fi
}
%-cchar_endstyle
%+cchar_startcharstyle
\def\startch@rstyle{\trace{s}{startch@rstyle \newch@rstyle (\stylet@pe)}%
  \mcpush{\ifx\stylet@pe\empty \ss@Char\else\stylet@pe\fi}{\newch@rstyle}%
  \let\styst@k=\empty
  \s@tstyst@k%
  \t@stpublishability{\newch@rstyle}\ifn@npublishable                   %(1)
   \setbox0=\hbox\bgroup\skipch@rstyletrue
   \let\thisch@rstyle=\newch@rstyle
  \else
   \ifvmode\temptrue\else\tempfalse\fi
   \leavevmode % in case the paragraph hasn't started yet
   \iftemp\the\@veryparstarthooks\fi               %(2)
   \op@ninghooks{before}{\newch@rstyle}{\styst@k}\relax% execute any <before> hook
   \bgroup % start a group to encapsulate the style's formatting changes
    \x@\let\x@\@ttriblist\csname @ttriblist-\newch@rstyle\endcsname
    \trace{A}{Attributes-\newch@rstyle: \@ttriblist}%
    \skipch@rstylefalse
    \ifx\ch@rfontextend\empty\else
     \m@kenumber{\ch@rfontextend}%
      \ifx\f@ntextend\empty\let\f@ntextend\@@result
      \else
        \bgroup\dimen0=\f@ntextend pt\xdef\tmpv@l{\strip@pt{\dimexpr \@@result \dimen0\relax}}%
        \egroup
        \let\f@ntextend\tmpv@l
      \fi
      \trace{sa}{ch@rfontextend \ch@rfontextend, f@ntextend=\f@ntextend}% 
    \fi
    \trace{sa}{using newch@rstyle (\newch@rstyle) at nesting (\the\p@ranesting/\the\n@tenesting) \the\currentgrouplevel}%
    \let\thisch@rstyle=\newch@rstyle % remember the current style
    \s@tfont{\thisch@rstyle}{\styst@k}% set up font attributes
    \ifColorFonts
      \ifInterWordSpaceShapingBug
        \ifnum\XeTeXinterwordspaceshaping=0 \else 
          \edef\newch@rfont{\fontname\font}%
          \ifx\oldch@rfont\newch@rfont % So-far this (SEGV) bug has only been seen with . \em* \em ..., but *actually* compare font.
            \ifcsname \styst@k h@scolour\endcsname
              \trace{s}{Applying work-around for interwordspaceshaping bug(\newch@rstyle)}%
              \kern0pt 
            \else
              \message{\styst@k\space has no colour}%
            \fi % Work-around bug with colour and intercharwordspaceshaping
          \fi
        \fi
      \fi
    \fi
    \ifnum\n@tenesting>0 \global\advance\n@tenesting by 1 % record nesting level in para or note
    \else \global\advance\p@ranesting by 1 \fi
    \t@gstart{Span}{\newch@rstyle}%
    \b@xedstylefalse % do we box the text?
    \ifAlwaysCheckAttributes\b@xedstyletrue
      \trace{sb}{Boxed style required: possibly linking}%
    \fi % Linking attributes are possibly universally applicable.
    \ifcsname complex-\newch@rstyle\endcsname \b@xedstyletrue\fi % For rb and similar 
    \x@\ifx\csname b@xed-\newch@rstyle\endcsname\tr@e \b@xedstyletrue
      \trace{sb}{Boxed style required for \newch@rstyle}%
    \fi
    \ifb@xedstyle\else
      \ifx\@ttriblist\relax\let\@ttriblist\empty\fi
      \ifx\@ttriblist\empty\else
        \ifversion{4}{%Check *which* attributes are expected (and if any of them need boxing)
          \attrspecialfalse
          \def\d@##1\E{\CheckSpecial{##1}}\x@\cstackdown\@ttriblist,\E
          \ifattrspecial\b@xedstyletrue
            \trace{sb}{Special attributes in use for \newch@rstyle}%
          \fi
        }{% Old: anything with an attribute
          \trace{sb}{Attributes in use for \newch@rstyle}%
          \b@xedstyletrue}%
      \fi 
      \getmcp@ram{raise}%
      \ifx\p@ram\relax\else \b@xedstyletrue\fi
      \ch@ckb@xedstyle{\styst@kfirst}{\styst@k}%
    \fi
    \ifb@xedstyle
      \trace{sb}{Starting box for boxed style \newch@rstyle}%
      \x@\let\csname b@xed-\newch@rstyle\endcsname\tr@e % NOT global, just in case this is from the style stack.
      \setbox0=\hbox\bgroup\fi\xtr@ 
    %WIP \ifversion{3}{\setbox0=\vbox\bgroup\hsize=\maxdimen\everypar={}\let\par\endgraf}{}
    %fi                     %(3)
    \op@ninghooks{start}{\thisch@rstyle}{\styst@k}%% execute any <start> hook
  \fi
  \csname init@ttribs\endcsname %If ptx-attribute is in use, use it... 
  \trace{s}{Actual start of \styst@k (b@xedstyle\ifb@xedstyle true\else false\fi)}%
}
%-cchar_startcharstyle
\def\ch@ckb@xedstyle#1#2{%Check parameters that need a style to be boxed and reprocessed
    \trace{sb}{ch@ckb@xedstyle #1}%
    \def\@@w##1{\trace{sb}{b@xing #1(##1)}}%
    \ifb@ckgroundelsewhere
      \b@ckgroundelsewherefalse
    \else
      \getp@ram{background}{#1}{#2}%
      \def\xtr@{}% Any extra code to run at the start of the group
      \ifx\p@ram\relax\else\ifx\p@ram\h@phen\else\b@xedstyletrue\@@w{background}\fi\fi
    \fi
    \getp@ram{underline}{#1}{#2}%
    \ifx\p@ram\tru@\b@xedstyletrue\@@w{underline}\def\xtr@{\ifnum\XeTeXuseglyphmetrics=0 \XeTeXuseglyphmetrics=3\fi}\fi
    \ifx\p@ram\tw@\b@xedstyletrue\@@w{double-underline}\def\xtr@{\ifnum\XeTeXuseglyphmetrics=0 \XeTeXuseglyphmetrics=3\fi}\fi
    \getp@ram{outline}{#1}{#2}%
    \ifx\p@ram\relax\else\b@xedstyletrue\@@w{outline}\fi
    \getp@ram{shadow}{#1}{#2}%
    \ifx\p@ram\relax\else\b@xedstyletrue\@@w{shadow}\fi
    \getmcp@ram{b@xattr}%
    \ifx\p@ram\relax\else\b@xedstyletrue\@@w{b@xattr}\fi
}
\newbox\ulb@x
\newif\ifUnderlineSpaces\UnderlineSpacestrue

\def\Dulsp@ce{\leaders\vtop{\hbox to \DoubleUnderlineSpaceQuantum{\hss}\kern\dimen1\hrule height\UnderlineThickness\kern\DoubleUnderlineSep\hrule height\UnderlineThickness}\hskip \fontdimen2\font plus \fontdimen3\font minus\fontdimen4\font}
\def\ulsp@ce{\leaders\hrule depth\dimen0 height-\dimen1\hskip \fontdimen2\font plus \fontdimen3\font minus\fontdimen4\font}
\def\Culsp@ce{\cleaders\vtop{\lineskip=0pt\lineskiplimit=100pt\hbox to \wd9{\hss}\kern\dimen1\copy 9}\hskip \fontdimen2\font plus \fontdimen3\font minus\fontdimen4\font}
\def\@do@Sunderline{\let\@@@do\@do@underline\unskip\unpenalty\setbox0\lastbox\dimen2=\dp0\ifdim\wd0=0pt \let\@@@do\relax\else\global\setbox\ulb@x=\hbox{\vtop{\hbox{\unhbox0}\kern-\dimen2\kern\dimen1\hrule height\UnderlineThickness}\ifvoid\ulb@x\else\@ulsp@ce\unhbox\ulb@x\fi}\fi\@@@do}%
\def\@do@Cunderline{\let\@@@do\@do@underline\unskip\unpenalty\setbox0\lastbox\setbox0\hbox{\unhbox0}\dimen2=\dp0\dimen5=\wd0\ifdim\wd0=0pt \let\@@@do\relax\else\global\setbox\ulb@x\hbox{\vtop{\lineskip=0pt\lineskiplimit=100pt\box0\kern-\dimen2\kern\dimen1\hbox to \dimen5{\Cle@dertype\copy9\hskip 0pt plus 1fil}}\ifvoid\ulb@x\else\@ulsp@ce\unhbox\ulb@x\fi}\fi\@@@do}%
\def\@do@Dunderline{\let\@@@do\@do@underline\unskip\unpenalty\setbox0\lastbox\dimen2=\dp0\ifdim\wd0=0pt \let\@@@do\relax\else\global\setbox\ulb@x=\hbox{\vtop{\hbox{\unhbox0}\kern-\dimen2\kern\dimen1\hrule height\UnderlineThickness\kern\DoubleUnderlineSep\hrule height\UnderlineThickness}\ifvoid\ulb@x\else\@ulsp@ce\unhbox\ulb@x\fi}\fi\@@@do}%
\let\@do@underline\@do@Sunderline
\def\@s@tstringunderline#1"#2\E{%
    \setbox9\hbox{\XeTeXuseglyphmetrics=1 \s@tfont{underline}{underline+\styst@k}#1}%
}
\def\@s@tcodeunderline#1]#2\E{%
    \setbox9\hbox{\XeTeXuseglyphmetrics=1 \s@tfont{underline}{underline+\styst@k}\setbox0\hbox{#1}%
      \getp@ram{spacebeside}{underline}{underline+\styst@k}%
      \ifx\p@ram\relax
        \box0
      \else
        \dimen1=\p@ram\wd0
        \kern \dimen1\box0\kern\dimen1
      \fi
      \global\let\Cle@dertype\cleaders
    }%
}
\def\s@tcharunderline#1#2\E{%
  \if "#1\relax\@s@tstringunderline #2"\E
  \else
    \if [#1\relax\@s@tcodeunderline #2]\E
    \else
      \trace{s}{Setting underline glyph to #1}%
      \edef\@gid{\the\XeTeXcharglyph`#1}%
      \setbox9\hbox{\XeTeXuseglyphmetrics=1 \s@tfont{underline}{underline+\styst@k}%
        \getp@ram{spacebeside}{underline}{underline+\styst@k}%
        \ifx\p@ram\relax
          \dimen1=0pt
        \else     
          \dimen1=\fontcharwd\font `#1 
          \dimen1=\p@ram\dimen1
          \global\let\Cle@dertype\cleaders
        \fi
        \dimen2=\dimen1
        \advance\dimen1 -\XeTeXglyphbounds1 \@gid
        \advance\dimen2 -\XeTeXglyphbounds3 \@gid
        \trace{sa}{Glyph \@gid \space has bounds \XeTeXglyphbounds1 \@gid , \XeTeXglyphbounds3 \@gid}%
        \kern \dimen1 #1\kern \dimen2}%
    \fi
  \fi
}
\def\d@underline#1{\showboxbreadth=99
  \ifx\@do@underline\@do@Cunderline
    %\tracingassigns=1
    \global\let\Cle@dertype\xleaders
    \x@\s@tcharunderline\ulcp@ram\E
    \getp@ram{raise}{underline}{underline+\styst@k}\ifx\p@ram\relax\else\ifx\p@ram\empty\else
      \setbox9\hbox{\vbox{\kern-\p@ram\box9\kern\p@ram}}%
    \fi\fi
    %\tracingassigns=0
  \fi
  \ifUnderlineSpaces
    \ifx\@do@underline\@do@Dunderline
      \let\@ulsp@ce\Dulsp@ce
    \else
      \ifx\@do@underline\@do@Cunderline
        \let\@ulsp@ce\Culsp@ce
      \else
        \let\@ulsp@ce\ulsp@ce
      \fi
    \fi
  \else
    \let\@ulsp@ce\space
  \fi
  \setbox\ulb@x\box\voidb@x
  {\dimen0=\UnderlineLower\relax \ifdim\dimen0<0pt \dimen0=-\dimen0\else \advance\dimen0\dp#1\fi\dimen1=\dimen0\advance\dimen0\UnderlineThickness\everypar={}\setbox#1=\vbox{\hfuzz=\maxdimen \hsize=1em \hyphenpenalty=10000 \leftskip=0pt \rightskip=0pt \unhbox#1 \endgraf\@do@underline}%
  %\showbox#1
  %\showbox\ulb@x
  %Last resort... something couldn't be unwrapped:
  \ifdim \wd#1=0pt \else\setbox#1\hbox{\vtop{\box#1\kern\UnderlineLower\hrule height\UnderlineThickness}}%
    \unhbox#1\@ulsp@ce\fi
  }%
  \setbox#1\box\ulb@x
}

\def\s@tb@kgroundlower{%% Calculate where we don't chop off descenders from line above.
  \bgroup\XeTeXuseglyphmetrics=0\relax
  \setbox1=\hbox{~}%
  \xdef\b@ckgroundlower{\the\dp1}%
  \xdef\b@ckgroundht{\the\dimexpr \baselineskip-\dp1\relax}%
  \ifhe@dings
    \ifdim\b@ckgroundht<\ht1
      \xdef\b@ckgroundht{\the\ht1}%
    \fi
  \fi
  \trace{sa}{Set background lower to \b@ckgroundlower, ht:\b@ckgroundht, bls:\the\baselineskip}%
  \egroup
}
\def\BackgroundOverrun{0.25pt}
\def\bgsp@ce{\bgc@l\leaders\hrule depth\dimen0 height \dimen1\hskip 1ex plus 0.2ex minus 0.2ex\endbgc@l}
\def\@@do@background#1{\hbox to 0pt{\kern-\BackgroundOverrun\bgc@l\vrule height \dimen1 depth\dimen0 width \dimen2 \endbgc@l\hss}\box#1}%
\def\@do@background{\let\@@@do\@do@background\unskip\unpenalty\setbox0\lastbox\setbox0\hbox{\unhbox 0}\dimen2=\wd0\advance\dimen2 by \dimexpr \BackgroundOverrun*2\relax\ifdim\wd0=0pt \let\@@@do\relax\else\global\setbox\ulb@x=\hbox{\@@do@background{0}\ifvoid\ulb@x\else\@bgsp@ce\unhbox\ulb@x\fi}\fi\@@@do}%
\def\d@background#1#2{%\showboxbreadth=99
  \let\@bgsp@ce\bgsp@ce
  \setbox\ulb@x\box\voidb@x
  \setbgc@l{#1}%
  \s@tb@kgroundlower
  {\dimen1=\b@ckgroundht \dimen0=\b@ckgroundlower\everypar={}\setbox#2=\vbox{\hfuzz=\maxdimen \hsize=1em \hyphenpenalty=10000 \leftskip=0pt \rightskip=0pt \unhbox#2 \endgraf\@do@background}%
  %\showbox#1
  %\showbox\ulb@x
  \ifdim \wd#2=0pt \else\setbox#2\hbox{\@@do@background{#2}}%
    \unhbox#2\@bgsp@ce\fi
  %\unhbox\ulb@x
  }%
  \setbox#2\box\ulb@x
}

\def\shdwmul#1 #2\E{%
  \edef\tmp{#1}\ifx\tmp\relaxval
    \let\@@shdw@x\relax\let\@@shdw@y\relax
  \else
    \edef\@@shdw@x{\the\dimexpr #1\dimexpr \c@rrfontsize\FontSizeUnit\relax\relax}%
  \fi
  \def\tmp{#2}\ifx\tmp\empty\let\shdw@y\shdw@x\else
    \edef\@@shdw@y{\the\dimexpr #2\dimexpr \c@rrfontsize\FontSizeUnit\relax\relax}%
  \fi
  \trace{Fe}{Shadow dims \@@shdw@x x \@@shdw@y}%
}

\def\@@do@shadow#1{\trace{Fe}{Chunk is \the\wd#1 wide, so far \the\wd\ulb@x}\hbox{%
  \special{pdf:code q}%
  \ifx\@@shadcol\empty\else\special{pdf:code \@@shadcol\space rg}\fi%
  \ifx\@@shdw@x\relax\else\kern\@@shdw@x \relax\raise\@@shdw@y\copy#1 \kern-\dimexpr \wd#1+\@@shdw@x\relax\fi
  \ifx\@@shadcol\empty\else\special{pdf:code Q q}\fi
  \ifdim \@@outlinw pt>0pt \special{pdf:code \@@outlinw\space w 2 Tr \@@outcol\space RG}\fi
  \ifx\@@fgcol\empty\else \special{pdf:code \@@fgcol\space rg}\fi%
  \copy#1
  \ifdim \@@outlinw pt >0pt \special{pdf:code Q q \ifx \@@fgcol\empty 0 0 0\else \@@fgcol\fi\space rg}\kern-\wd#1\box#1\fi
  \special{pdf:code Q}}%
}
\def\@do@shadow{\let\@@@do\@do@shadow\unskip\unpenalty\setbox0\lastbox\setbox0\hbox{\unhbox0}\ifdim\wd0=0pt \let\@@@do\relax\else\global\setbox\ulb@x=\hbox{\@@do@shadow{0}\ifvoid\ulb@x\else { }\unhbox\ulb@x\fi}\fi\@@@do}%
\def\d@shadow#1#2#3#4#5#6{%\showboxbreadth=99
  \trace{Fe}{Shadow/Outline called: #1 / #2/ #3 / #4 / #5 / #6}%
  {\setbox\ulb@x\box\voidb@x
  \x@\shdwmul#2 \E
  \edef\@@outlinw{\strip@pt{\dimexpr #4\FontSizeUnit \relax}}%
  \edef\@@outcol{#5}\edef\@@shadcol{#3}\edef\@@fgcol{#6}% 
  \edef\sv@interactionmode{\the\interactionmode}%
  \nonstopmode
  \tracingassigns=1
  \everypar={}\everyhbox={}\setbox#1=\vbox{\hfuzz=\maxdimen \hsize=1em \hyphenpenalty=10000 \leftskip=0pt \rightskip=0pt \unhbox#1 \endgraf
  \@do@shadow}%
  \ifdim \wd#1=0pt \else %\showbox#1
    \setbox#1\hbox{\@@do@shadow{#1}}%
    \unhbox#1\fi
  \tracingassigns=0
  \interactionmode=\sv@interactionmode
  }%
  %\showbox\ulb@x
  \setbox#1\box\ulb@x
}
\newif\ifAlwaysCheckAttributes \AlwaysCheckAttributesfalse
%+cchar_endcharstyle
%\lccode`\|=`\\ % for printing backslash in error message
\def\set@endb@xedstyle#1#2{%
  \b@xedstylefalse
  \getp@ram{raise}{#1}{#2}%
  \global\let\char@raise\p@ram
  \ifx\p@ram\relax\else
     %{\setbox0\hbox{\count255=\numexpr \p@ram}\ifdim\wd0>0pt \else \xdef\char@raise{\p@ram \FontSizeUnit}\fi}%
     %\dimen9=\char@raise \ifdim\dimen9=0pt\relax \else \b@xedstyletrue\fi
  \fi
     \ifb@ckgroundelsewhere\global\let\char@background\relax
      \b@ckgroundelsewherefalse
     \else
       \getp@ram{background}{#1}{#2}%
       \global\let\char@background\p@ram
       \ifx\p@ram\relax\else\ifx\p@ram\h@phen\else\b@xedstyletrue\fi\fi
     \fi
     \getp@ram{underline}{#1}{#2}%
     \ifx\p@ram\tru@\b@xedstyletrue\global\let\ulp@ram\p@ram
       \global\let\ulcp@ram\relax
       \getp@ram{underchar}{#1}{#2}\ifx\p@ram\relax\else\ifx\p@ram\empty\else
         \global\let\ulcp@ram\p@ram
       \fi\fi
     \else
       \ifx\p@ram\tw@\b@xedstyletrue\global\let\ulp@ram\p@ram
       \else
        \global\let\ulp@ram\relax
       \fi
     \fi
     \global\let\outlncolp@ram\empty\global\let\shdwcolp@ram\empty
     \getp@ram{p@rsedcol}{#1}{#2}\global\let\p@rsedcol\p@ram
     \getp@ram{shadow}{#1}{#2}\global\let\shdwp@ram\p@ram
     \ifx\p@ram\relax\else\ifx\p@ram\h@phen\global\let\shdwp@ram\relax\else
       \getp@ram{shadowcol}{#1}{#2}\ifx\p@ram\relax\xdef\shdwcolp@ram{0 0 0}\else\global\let\shdwcolp@ram\p@ram\fi
       \b@xedstyletrue
     \fi\fi
     \getp@ram{outline}{#1}{#2}\global\let\outlnp@ram\p@ram
     \ifx\p@ram\relax\else\ifx\p@ram\h@phen\global\let\outlnp@ram\relax\else\b@xedstyletrue
       \getp@ram{outlinecol}{#1}{#2}\ifx\p@ram\relax\xdef\outlncolp@ram{0 0 0}\else\global\let\outlncolp@ram\p@ram\fi
     \fi\fi
}

\def\apply@endb@xedstyle#1{%
  \tempfalse% d@shadow applies both outline and shadow, as they are closely linked. Test for either.
  \ifx\shdwp@ram\relax\def\shdwp@ram{\relax}\else\temptrue\fi
  \ifx\outlnp@ram\relax\def\outlnp@ram{#1}\else\temptrue\fi
  \iftemp
    \ifx\p@rsedcol\z@ros\let\p@rsedcol\relax\fi
    \ifx\p@rsedcol\relax\else
       \x@\h@xsixtotripple\x@ x\p@rsedcol\end \let\p@rsedcol\rgb@out% Convert hexadecimal to R G B spec
    \fi
    \d@shadow{#1}{\shdwp@ram}{\shdwcolp@ram}{\outlnp@ram}{\outlncolp@ram}{\ifx\p@rsedcol\relax\else\p@rsedcol\fi}%
  \fi
  \ifx\ulp@ram\tru@ 
    \ifx\ulcp@ram\relax
      \let\@do@underline\@do@Sunderline\d@underline{#1}%
      \message{Single underline for \newch@rstyle}%
    \else
      \message{Character underline for \newch@rstyle}%
      \let\@do@underline\@do@Cunderline\d@underline{#1}%
    \fi
  \else
    \ifx\ulp@ram\tw@ \let\@do@underline\@do@Dunderline\d@underline{#1}%
      \message{Double underline for \newch@rstyle}%
  \fi\fi
  \ifx\char@background\relax\else\d@background{\char@background}{#1}\fi
}

\def\LimitIgnoreRaise{1.0} % How much of a baselineskip raise gets ignored in footnotes.

%\lowercase{
 \def\endch@rstyle*{\TRACE{endch@rstyle}% consume the * that marked the SFM as ending a style
   \def\d@##1+##2+##3\E{\xdef\oldch@rstyle{##2}}\mctopnoms
   \xdef\@ldch@rf@nt{\fontname\font}%
   \trace{s}{endch@rstyle \oldch@rstyle\space from \newch@rstyle}%
   \tempfalse
   \ifx\@ttriblist\relax\let\@ttriblist\empty\fi
   \ifx\@ttributes\empty\else
     \trace{A}{@ttributes not empty. \@ttributes (\csname @ttriblist-\oldch@rstyle\endcsname)}%
     \parse@ttribs{\@ttributes}%
   \fi
   \global\let\h@@kstyst@k\styst@k
   \ifx\thisch@rstyle\undefined
     \trace{s}{unmatched character style end-marker |\newch@rstyle*}%
   \else
    \ifskipch@rstyle\egroup\trace{s}{Closing skipped style}\else%
     \cl@singhooks{end}{\oldch@rstyle}{\h@@kstyst@k}% discover and execute any <end> hook
     \set@endb@xedstyle{\styst@kfirst}{\styst@k}%
     \x@\let\x@\@ttriblist\csname @ttriblist-\oldch@rstyle\endcsname
     \ifx\@ttriblist\relax\let\@ttriblist\empty\fi
     \x@\ifx\csname b@xed-\oldch@rstyle\endcsname\tr@e \b@xedstyletrue\fi
     \ifx\@ttriblist\empty\trace{s}{No attributes expected}\else
        \trace{A}{Attributes: \@ttriblist}\fi
     \ifAlwaysCheckAttributes\b@xedstyletrue\fi % Linking attributes are possibly universally applicable.
     \global\tmpt@ks{}%
     \ifbox@run
       \x@\global\x@\tmpt@ks\x@{\the\end@box@run}\b@xedstyletrue
     \else
       \global\tmpt@ks{}%
     \fi
     \global\let\@newch@rstyle\newch@rstyle %Preserve newch@rstyle past egroup
     \ifb@xedstyle\egroup%<----------EGROUP HERE---------->
       \let\newch@rstyle\@newch@rstyle
       \the\tmpt@ks
       \apply@endb@xedstyle{0}%
       \ifx\@ttributes\empty\else% If there are actually attributes, call an appropriate hook	
	     \let\d@\apply@attr@specials
	     \x@\cstackdown \attribsus@d,\E
         \trace{A}{Special atributes done. trying complex-\oldch@rstyle}%
         \ifcsname complex-\oldch@rstyle\c@rrdstat\endcsname
           \csname complex-\oldch@rstyle\c@rrdstat\endcsname
         \else 
           \ifcsname complex-\oldch@rstyle\endcsname
             \csname complex-\oldch@rstyle\endcsname
           \fi
         \fi
       \fi
       \ifx\attribsus@d\empty\else
         \unset@ttribs% Clear any old attributes.
       \fi
       \ifx\char@raise\relax
         \unhbox0
       \else\dimen9=\char@raise \relax
         \ifinn@te
           \ifversion{2}{% Make a raised/lowered the box affect linespacing as if it were on the baseline
             \trace{f}{Before raise-based corrections (|\the\dimen9| <\the\dimexpr \LimitIgnoreRaise\baselineskip), box is \the\ht0+\the\dp0}%
             \ifdim\dimen9<0pt
              \ifdim\dimen9>-\LimitIgnoreRaise\baselineskip % Put a limit on this
                 \ht0=\the\dimexpr \ht0 + \dimen9\relax
                 \dp0=\the\dimexpr \dp0 + \dimen9\relax
              \fi
             \else
               \ifdim\dimen9<\LimitIgnoreRaise\baselineskip
                 \ht0=\the\dimexpr \ht0 - \dimen9\relax
                 \dp0=\the\dimexpr \dp0 - \dimen9\relax
               \fi
             \fi
             \trace{f}{After any raise-based corrections, box is \the\ht0+\the\dp0}%
             }{}%
         \fi
         \ifdim\dimen9<0pt \lower-\dimen9 \else\raise\dimen9 \fi\box0
       \fi
     \fi
     \cl@singhooks{after}{\oldch@rstyle}{\h@@kstyst@k}% discover any <after> hooks
     \ifnum\n@tenesting>0 \global\advance\n@tenesting by -1 % decrement nesting level
       \global\let\@newch@rstyle\newch@rstyle %Preserve newch@rstyle past egroup
     \else \global\advance\p@ranesting by -1
     \fi
     \egroup % end the style's group, so formatting reverts             %(2)
     \let\oldch@rfont\@ldch@rf@nt
     \let\newch@rstyle\@newch@rstyle
     \the\afterh@@ks%\global\afterh@@ks{} % execute all the <after>-hook, if there was one
    \fi
    \t@gend{Span}%
    \trace{sa}{ending charstyle \newch@rstyle\space(\mcstack)}%
    \mcpop
    \trace{sa}{end char style, stack now: \mcstack (\styst@kfirst)}%
    \ifp@pping\else\setwh@tvrstyle\fi
   \fi
 }
%}
\def\setwh@tvrstyle{%
  \trace{s}{setwh@tvrstyle: \mcstack}%
   \def\thiswh@tvrstyle{p}%Default value just in case
   \ifx\mcstack\mcstack@mpty\else
     %\def\d@##1+##2+##3\E{\d@code{##1}{##2}\let\thiswh@tvrstyle\tmp}\mctop
   \fi
   \let\styst@k=\empty
   \s@tstyst@k%
   \s@tfont{\styst@kfirst}{\styst@k}% set up font attributes
}
%-cchar_endcharstyle
%+cchar_endallcharstyles
\newif\ifskipch@rstyle
\newcount\n@tenesting \newcount\p@ranesting
\def\SuperscriptRaise{0.85ex} % note that this is in terms of the scaled-down superscript font size
\def\UnderlineLower{0.1em}
\def\UnderlineThickness{0.05em}
\def\DoubleUnderlineSep{0.07em}
\def\DoubleUnderlineSpaceQuantum{0.05ex} % double-underlineing spaces use line-fragments. How long are they?

%
% end all character styles in effect within the current note or paragraph
% now replaced by end@llpoppedstyles
%\def\end@llcharstyles{\let\d@=\end@llcharstyle\mcdown}
%\def\end@llcharstyle#1+#2\E{\trace{s}{endcharstyles: #1+#2}\edef\tmp{#1}%
    %\ifx\tmp\empty\else\if#1c\endch@rstyle*\else\if#1C\endch@rstyle*\else\let\d@=\cstackrelax\fi\fi\fi}
%-cchar_endallcharstyles

%\def\end@llcharstyles{%
% \ifnum\n@tenesting>0 \doendch@rstyles\n@tenesting
%  \else \doendch@rstyles\p@ranesting \fi}
%\def\doendch@rstyles#1{\@LOOP \ifnum#1>1 \endch@rstyle*\@REPEAT}
% loop macros copied from plain.tex, renamed to avoid clashes in case of nesting
%\def\@LOOP #1\@REPEAT{\gdef\@BODY{#1}\@ITERATE}
%\def\@ITERATE{\@BODY \global\let\@NEXT\@ITERATE
% \else \global\let\@NEXT\relax \fi \@NEXT}
%\let\@REPEAT\fi

%+cchar_getfontname
\newif\ifColorFonts \ColorFontstrue
%
% Set up the font attributes for a given marker (used by all style types, not only char styles)
%
\let\SpaceStretchFactor=\empty %default, do-nothing value
\let\SpaceShrinkFactor=\empty %default, do-nothing value
\def\t@@stid#1id:#2|#3\relax{\edef\tmp{#2}\ifx\tmp\tmpa\t@mptrue\fi}%
\def\t@stid#1#2{% See if #2 contains id:#1
  \edef\tmpa{#1}\t@mpfalse
  \x@\t@@stid #2id:|\relax
}
\def\g@tfontname#1#2{%
 \trace{F}{Font request for (\c@tegory)(\mspr@fix)#1[#2](\c@rrdstat)[\styst@k]}%
 \x@\stystacksplit#2+\E% Cut split the stylestack between head and body 
 \ifx\tmpstyst@kfirst\empty\trace{F}{stylestack invalid}\edef\tmpstyst@kfirst{#1}\fi%
 \ifdiglot\def\f@ntstyle{\tmpstyst@kfirst\c@rrdstat}\let\sid@\c@rrdstat\else\def\f@ntstyle{\tmpstyst@kfirst}\def\sid@{-}\fi%
 \ifx\mspr@fix\empty\else\edef\f@ntstyle{\mspr@fix\f@ntstyle}\fi % Milestones now on stack
 \ifx\c@tegory\empty\else\s@tc@tpr@fix\edef\f@ntstyle{\c@tprefix\f@ntstyle}\fi % Categories also have different styles
 \let\reqf@ntstyle\f@ntstyle
 \edef\cmpf@ntstyle{#1}%
 \let\d@@=\d@
 \getp@ram{fontsize}{#1}{#2}%
 %\def\d@##1+##2+##3\E{\edef\tmp{##2}\ifx\cmpf@ntstyle\tmp \getmcfonts@ze\else
   %\csname d@code-##1\endcsname
   %\getp@ram{fontsize}{#1}{#1\ifx\tmp\empty\else +\tmp\fi}\ifx\p@ram\relax\xdef\c@rrfontsize{12}%
     %\else\global\let\c@rrfontsize=\p@ram\fi\fi}%
 %\mctop
 \trace{F}{id: `\id@@@`. after fontsize: p@ssp@rams \p@ssParams}%
 \let\d@=\d@@
 \let\idpfx\empty
 \ifx\id@@@\empty\else
   \x@\t@stid\x@{\id@@@}{\p@ssParams}%
   \ift@mp\edef\idpfx{id:\id@@@|}\fi
 \fi
 \ifx\p@ram\relax\xdef\c@rrfontsize{12}\else\global\let\c@rrfontsize=\p@ram\fi
 \x@\stystacksplit#2+\E% redo the split because it's not preserved.
 \edef\f@ntstyle{\idpfx\f@ntstyle-\c@rrfontsize\ifx\tmpstyst@kbody\empty\else+\tmpstyst@kbody\fi\ifx\f@ntextend\empty\else @\f@ntextend\fi}%
 \trace{F}{Font style name is \f@ntstyle (\tmpstyst@kfirst | \tmpstyst@kbody}%
}

\def\nomsg@tfontname#1{%Fontname with no milestones/categories or stylestack, etc. Define it if there isn't one, but don't switch to it.
  \getp@ram{fontsize}{#1}{#1}%
  \ifx\p@ram\relax\xdef\c@rrfontsize{12}\else\global\let\c@rrfontsize=\p@ram\fi
  \edef\f@ntstyle{#1\ifdiglot\c@rrdstat\fi-\c@rrfontsize}%
  \x@\ifx\csname font<\f@ntstyle>\endcsname
    \else\definef@nt{#1}{#1}%
  \fi%
}
%-cchar_getfontname

\let\styst@k\empty
\let\f@ntlist\empty % List of all fonts defined.

\newif\ifh@vefont \h@vefontfalse
%+cchar_setfont
\xdef\z@ros{000000}%What does ParseColor come out with for black?
\def\s@tfont#1#2{%
 \trace{sa}{s@tfont #1 / #2}%
 \edef\r@qmkr{#1}\ifx\empty\r@qmkr\trace{F}{Font for empty marker requested. Nonsense! Substituting p}\def\r@qmkr{p}\fi
 \edef\r@qstyst@k{#2}\ifx\empty\r@qstyst@k\trace{F}{Empty style stack requested. Substituting \r@qmkr}\edef\r@qstyst@k{\r@qmkr}%
 \else
   \x@\stystacksplit#2+\E
   \ifx\r@qmkr\tmpstyst@kfirst\else
     \edef\r@qstyst@k{\r@qmkr+\r@qstyst@k}%
     \trace{F}{Fixed broken stystak \r@qstyst@k}%
   \fi
 \fi 
 \g@tfontname{\r@qmkr}{\r@qstyst@k}%
 \trace{F}{f@ntstyle=\f@ntstyle}%
 \ifdiglot\else
   \let\c@rrdstat\empty %Whether diglot or not, it might have been diglot before.
 \fi 
 \setLRspecific %Does nothing if there's no change
 \x@\ifx\csname font<\f@ntstyle>\endcsname \relax 
   \definef@nt{#1}{#2}%
 \fi
 % switch to the appropriate font
 \trace{F}{Font set to font<\f@ntstyle>}%
 \csname font<\f@ntstyle>\endcsname                                     %(+)
 \getFp@ram{fonticspace}{#1}{#2}\ifx\p@ram\relax
   \ifdiglot \fi
 \else\XeTeXinterchartoks=\p@ram\fi
 \getFp@ram{fontname}{#1}{#2}\ifx\p@ram\relax
   \getFp@ram{fontgrspace}{#1}{#2}\ifx\p@ram\relax\else\XeTeXinterwordspaceshaping=\p@ram\fi
 \fi
}

\def\definef@nt#1#2{%Assumes \f@ntstyle is the pre-determined name, and that the font actually needs defining.
  \trace{F}{definef@nt font for #1 (#2) as \f@ntstyle \space at \c@rrfontsize}%
  \trace{S}{Normal font for \sid@\space is \regular}%
  \let\regul@r=\regular
  \let\b@ld=\bold
  \let\it@lic=\italic
  \let\b@lditalic=\bolditalic
  \let\typef@ce=\regul@r
  \getp@ram{fontname}{#1}{#2}% see if \FontName was specified in the stylesheet
  \ifx\p@ram\relax % if not, check the \Bold and \Italic properties
	  \getp@ram{bold}{#1}{#2}%
	  \ifx\p@ram\tru@
		\let\typef@ce=\b@ld
		\getp@ram{italic}{#1}{#2}%
		\ifx\p@ram\tru@ \let\typef@ce=\b@lditalic \fi
	  \else
		\getp@ram{italic}{#1}{#2}%
		\ifx\p@ram\tru@ \let\typef@ce=\it@lic \fi
	  \fi
  \else
    \edef\fontn@me{\p@ram}% use font name from the stylesheet
    \getp@ram{bold}{#1}{#2}%
    \trace{F}{Bold Suffix is \BoldSuffix, bold is \p@ram}%
    \ifx\p@ram\tru@
      \getp@ram{italic}{#1}{#2}%
      \ifx\p@ram\tru@
        \edef\typef@ce{"\fontn@me\BoldItalicSuffix"}%
      \else
        \edef\typef@ce{"\fontn@me\BoldSuffix"}%
      \fi
    \else
      \getp@ram{italic}{#1}{#2}%
      \ifx\p@ram\tru@
        \edef\typef@ce{"\fontn@me\ItalicSuffix"}%
      \else
        \edef\typef@ce{"\fontn@me"}%
      \fi
    \fi
  \fi
  \getp@ram{fontfeatures}{#1}{#2}%
  \ifx\p@ram\relax\else\edef\typef@ce{\typef@ce\p@ram}\fi
  \getp@ram{smallcaps}{#1}{#2}%                                           %(1)
  \ifx\p@ram\tru@
    \edef\typef@ce{\typef@ce\SmallCapsSuffix}%
  \fi
  \getp@ram{color}{#1}{#2}%                                                %(+)
  \ifColorFonts\ifx\p@ram\relax\else
    \let\colp@ram\p@ram
    \x@\ParseColor\colp@ram\end
    \x@\global\x@\let\csname #2h@scolour\endcsname\tr@e
    \temptrue % Apply here?
    \getp@ram{shadow}{#1}{#2}\ifx\p@ram\relax\else\tempfalse\fi
    \getp@ram{outline}{#1}{#2}\ifx\p@ram\relax\else\tempfalse\fi
    \iftemp
      \ifx\r@s\z@ros\else\edef\typef@ce{\typef@ce :color=\r@s}\fi%
    \else
      \setp@ram{p@rsedcol}{#2}{\r@s}% remember for later
    \fi
  \fi\fi%
  \dimen0=\c@rrfontsize\FontSizeUnit                                    %(+)
  \getp@ram{fontfactor}{#1}{#2}% scale down by \SuperscriptFactor if superscripted style 
  \ifx\p@ram\relax\else \trace{F}{Scaling by \p@ram}\dimen0=\p@ram\dimen0\fi
  %\edef\f@ntstyle{\f@ntstyle-\c@rrfontsize}%
  % create the font identifier for this style
  \trace{F}{font<\f@ntstyle>=\typef@ce\space at \the\dimen0}%
  \ifdim\dimen0 < \minfontsize
    \message{Warning: <\f@ntstyle> = \typef@ce\space at \the\dimen0 is smaller than specified minimum font size (first met on page \the\pageno)}%
  \fi
  \x@\global\x@\font
    \csname font<\f@ntstyle>\endcsname=\typef@ce\space at \the \dimen0  %(+)
  \ifLogFonts
    \edef\f@ntl@gmsg{font<\f@ntstyle>=\typef@ce\space at \the \dimen0}%
    \ifb@xedstyle % a write node breaks word-by-word underlining and other boxedstyle things.
      \x@\immediate\x@\write\x@\f@ntl@g\x@{\f@ntl@gmsg\space (at or after page \the\pageno)}%
    \else
      \x@\write\x@\f@ntl@g\x@{\f@ntl@gmsg\space (page \the\pageno)}%
    \fi
  \fi
  \xdef\f@ntlist{\f@ntlist,\f@ntstyle}%
  \x@\ifcsname \typef@ce _spacefactor\endcsname
    \dimen0=\x@\csname \typef@ce _spacefactor\endcsname \x@\x@\fontdimen2\csname font<\f@ntstyle>\endcsname
    \trace{F}{\typef@ce _spacefactor = \the\dimen0}%
    \x@\x@\fontdimen2\csname font<\f@ntstyle>\endcsname=\dimen0
  \fi
  \ifx\SpaceStretchFactor\empty\else
    \dimen0=\SpaceStretchFactor\x@\x@\fontdimen2\csname font<\f@ntstyle>\endcsname
    \x@\x@\fontdimen3\csname font<\f@ntstyle>\endcsname=\dimen0
    \trace{F}{Font \f@ntstyle, space stretch \the\dimen0}%
  \fi
  \ifx\SpaceShrinkFactor\empty\else
    \dimen0=\SpaceShrinkFactor\x@\x@\fontdimen2\csname font<\f@ntstyle>\endcsname
    \x@\x@\fontdimen4\csname font<\f@ntstyle>\endcsname=\dimen0
    \trace{F}{Font \f@ntstyle, space shrink \the\dimen0}%
  \fi
}



%-cchar_setfont

%+cchar_extrafont
\def\extraregular{"Times New Roman"}
\def\s@textrafont#1{%
  \stystacksplit\styst@k+\E
  \edef\@xfname{#1+\tmpstyst@kbody}%
  \ifcsname extrafont<\@xfname>\endcsname\else
    \let\typef@ce=\extraregular
    \stystacksplit\styst@k+\E
    \getp@ram{fontsize}{#1}{\@xfname}%
    \dimen0=\p@ram\FontSizeUnit
    \getp@ram{fontfactor}{#1}{\@xfname}% scale down by \SuperscriptFactor if superscripted style
    \ifx\p@ram\relax\else \dimen0=\p@ram\dimen0 \fi
    \ifdim\dimen0 < \minfontsize \message{Warning: <\f@ntstyle> = \typef@ce\space at \the\dimen0 is smaller than specified minimum font size (first met on page \the\pageno)}\fi
    \x@\global\x@\font\csname extrafont<\@xfname>\endcsname=\typef@ce\space at \dimen0
    \trace{sa}{define extrafont<\@xfname> \typef@ce\space at \the\dimen0}%
  \fi
  \trace{sa}{extrafont<\@xfname> =\csname extrafont<\@xfname>\endcsname}%
  \csname extrafont<\@xfname>\endcsname
}
\def\SuperscriptFactor{0.75}
\def\SmallCapsSuffix{/ICU:+smcp}
\def\BoldSuffix{/B}
\def\ItalicSuffix{/I}
\def\BoldItalicSuffix{/BI}
%-cchar_extrafont

\endinput
