%
% lltjp-fontspec.sty 2018-08-24
%

\NeedsTeXFormat{LaTeX2e}

\RequirePackage{fontspec}
\ExplSyntaxOn

% <= v2.6g uses \l__fontspec_nfss_enc_tl instead of \g__fontspec_nfss_enc_tl
\ifdefined \g__fontspec_nfss_enc_tl \else
  \def \g__fontspec_nfss_enc_tl { \l__fontspec_nfss_enc_tl }
\fi

%%%%%%%% fontspec v2.5c overwrites \rmfamily etc. in \setmainfont
\@ifpackagelater{fontspec}{2018/07/29}{ %% 2.6h or later
\cs_set:Nn \__fontspec_main_setmainfont:nn
  {
    \fontspec_set_family:Nnn \l__fontspec_rmfamily_family_tl {#1} {#2}
    \tl_set_eq:NN \rmdefault \l__fontspec_rmfamily_family_tl
    \use:x
      {
        \exp_not:n { \DeclareRobustCommand \rmfamily }
          {
            \exp_not:N \fontencoding { \g__fontspec_nfss_enc_tl }
            \exp_not:N \fontfamily { \l__fontspec_rmfamily_family_tl }
    \exp_not:N\if@ltj@match@family\exp_not:N\kanjifamily\exp_not:N\mcdefault\exp_not:N\fi%%%
            \exp_not:N \selectfont
          }
      }
    \str_if_eq:eeT {\familydefault} {\rmdefault}
      { \tl_set_eq:NN \encodingdefault \g__fontspec_nfss_enc_tl }
    \__fontspec_setmainfont_hook:nn {#1} {#2}
    \normalfont
  }
\cs_set:Nn \__fontspec_main_setsansfont:nn
  {
    \fontspec_set_family:Nnn \l__fontspec_sffamily_family_tl {#1} {#2}
    \tl_set_eq:NN \sfdefault \l__fontspec_sffamily_family_tl
    \use:x
      {
        \exp_not:n { \DeclareRobustCommand \sffamily }
          {
            \exp_not:N \fontencoding { \g__fontspec_nfss_enc_tl }
            \exp_not:N \fontfamily { \l__fontspec_sffamily_family_tl }
    \exp_not:N\if@ltj@match@family\exp_not:N\kanjifamily\exp_not:N\gtdefault\exp_not:N\fi%%%
            \exp_not:N \selectfont
          }
      }
    \str_if_eq:eeT {\familydefault} {\sfdefault}
      { \tl_set_eq:NN \encodingdefault \g__fontspec_nfss_enc_tl }
    \__fontspec_setsansfont_hook:nn {#1} {#2}
    \normalfont
  }
\cs_set:Nn \__fontspec_main_setmonofont:nn
  {
    \fontspec_set_family:Nnn \l__fontspec_ttfamily_family_tl {#1} {#2}
    \tl_set_eq:NN \ttdefault \l__fontspec_ttfamily_family_tl
    \use:x
      {
        \exp_not:n { \DeclareRobustCommand \ttfamily }
          {
            \exp_not:N \fontencoding { \g__fontspec_nfss_enc_tl }
            \exp_not:N \fontfamily { \l__fontspec_ttfamily_family_tl }
    \exp_not:N\if@ltj@match@family\exp_not:N\kanjifamily\exp_not:N\jttdefault\exp_not:N\fi%%%
            \exp_not:N \selectfont
          }
      }
    \str_if_eq:eeT {\familydefault} {\ttdefault}
      { \tl_set_eq:NN \encodingdefault \g__fontspec_nfss_enc_tl }
    \__fontspec_setmonofont_hook:nn {#1} {#2}
    \normalfont
  }
}{ %% <= 2.6g
\DeclareDocumentCommand \setmainfont { O{} m O{} }
 {
  \fontspec_set_family:Nnn \g__fontspec_rmfamily_family {#1,#3} {#2}
  \tl_set_eq:NN \rmdefault \g__fontspec_rmfamily_family
  \use:x { \exp_not:n { \DeclareRobustCommand \rmfamily }
   {
    \exp_not:N \fontencoding { \g__fontspec_nfss_enc_tl }
    \exp_not:N \fontfamily { \g__fontspec_rmfamily_family }
    \exp_not:N\if@ltj@match@family\exp_not:N\kanjifamily\exp_not:N\mcdefault\exp_not:N\fi%%%
    \exp_not:N \selectfont
   }
  }
  \str_if_eq:eeT {\familydefault} {\rmdefault}
    { \tl_set_eq:NN \encodingdefault \g__fontspec_nfss_enc_tl }
  \normalfont
  \ignorespaces
 }
\DeclareDocumentCommand \setsansfont { O{} m O{} }
 {
  \fontspec_set_family:Nnn \g__fontspec_sffamily_family {#1,#3} {#2}
  \tl_set_eq:NN \sfdefault \g__fontspec_sffamily_family
  \use:x { \exp_not:n { \DeclareRobustCommand \sffamily }
   {
    \exp_not:N \fontencoding { \g__fontspec_nfss_enc_tl }
    \exp_not:N \fontfamily { \g__fontspec_sffamily_family }
    \exp_not:N\if@ltj@match@family\exp_not:N\kanjifamily\exp_not:N\gtdefault\exp_not:N\fi%%%
    \exp_not:N \selectfont
   }
  }
  \str_if_eq:eeT {\familydefault} {\sfdefault}
    { \tl_set_eq:NN \encodingdefault \g__fontspec_nfss_enc_tl }
  \normalfont
  \ignorespaces
 }
\DeclareDocumentCommand \setmonofont { O{} m O{} }
 {
  \fontspec_set_family:Nnn \g__fontspec_ttfamily_family {#1,#3} {#2}
  \tl_set_eq:NN \ttdefault \g__fontspec_ttfamily_family
  \use:x { \exp_not:n { \DeclareRobustCommand \ttfamily }
   {
    \exp_not:N \fontencoding { \g__fontspec_nfss_enc_tl }
    \exp_not:N \fontfamily { \g__fontspec_ttfamily_family }
    \exp_not:N\if@ltj@match@family\exp_not:N\kanjifamily\exp_not:N\jttdefault\exp_not:N\fi%%%
    \exp_not:N \selectfont
   }
  }
  \str_if_eq:eeT {\familydefault} {\ttdefault}
    { \tl_set_eq:NN \encodingdefault \g__fontspec_nfss_enc_tl }
  \normalfont
  \ignorespaces
 }
}

\cs_set:Nn \fontspec_visible_space:
 {
 \__fontspec_primitive_font_glyph_if_exist:NnTF \font {"2423}
   { \ltjalchar"2423\scan_stop: }
   { \fontspec_visible_space_fallback: }
 }

\AtBeginDocument{
  \cs_if_exist:NF \verbvisiblespace {
    % >= LaTeX2e <2018-12-01> does not need the following patch
    \def\verb
    {
      \relax\ifmmode\hbox\else\leavevmode\fi % same as lltjcore
      \bgroup
        \verb@eol@error \let\do\@makeother \dospecials
        \verbatim@font\@noligs
        \@ifstar\__fontspecsverb\@verb
    }
  }
}
\cs_set:Npn \emshape { \gtfamily \itshape }
\cs_set:Npn \eminnershape { \mcfamily \upshape }

\ExplSyntaxOff
\expandafter\patchcmd\csname TU\string\textasteriskcentered\endcsname{\char}{\ltjalchar}{}{}

\endinput

