%
% luatexja-fontspec.sty
%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{luatexja-fontspec}[2011/11/16 v0.3]

\def\UTFencname{EU2}\def\XeTeXpicfile{}
\RequirePackage{xunicode}
%% Patch for xunicode.sty
{\catcode`\"=12
\gdef\check@hexcom@digits@#1#2#3#4#5@!@#6#7#8{%
 \ifx @#4\relax
  \UTFacc@warning@{insufficient hex digits #7 for #6-\string#8}%
 \else
  \ifcat \active\noexpand#8%
   \ifx\add@flag\@ne %
    \expandafter\def\csname\UTFencname\string#8\endcsname{\ltjalchar"#1#2#3#4\relax}%
    \ifx\unDeFiNed@#8%
     \ifx\cf@encoding\UTFencname
      \DeclareTextCommand{#8}{OT1}{\undefined}%
     \else
      \DeclareTextCommand{#8}{\cf@encoding}{\undefined}%
     \fi
    \else {% macro #8 exists already ...
      \let\protect\noexpand
      \edef\UTF@testi{#8}\def\UTF@testii{#8}%
      \ifx\UTF@testi\UTF@testii\aftergroup\UTF@ignore
      \else\aftergroup\UTF@doit\fi
     }%
     \iffalse
      % ... but when it isn't robust, make it so
      \expandafter\let\csname?-\string#8\endcsname#8\relax
      \edef\next@UTF@{{\cf@encoding}%
        {\expandafter\noexpand\csname?-\string#8\endcsname}}%
      \expandafter\DeclareTextCommand\expandafter
         {\expandafter#8\expandafter}\next@UTF@
     \fi
    \fi %
   \else % \add@flag \z@
    \expandafter\global\expandafter
      \let\csname\UTFencname\string#8\endcsname\relax
   \fi % end of \add@flag switch
  \else % not active catcode --- shouldn't happen
  % \typeout{*** did you really mean #8 ? ***}%
   \ifx\add@flag\@ne %
    \edef\tmp@name{\expandafter\string\csname\UTFencname\endcsname
      \expandafter\string\csname#8\endcsname}%
    \expandafter\def\csname\tmp@name\endcsname{\ltjalchar"#1#2#3#4\relax}%
    \ifx\cf@encoding\UTFencname
     \expandafter\DeclareTextCommand\expandafter
       {\csname#8\endcsname}{OT1}{\undefined}%
    \else
     \expandafter\DeclareTextCommand\expandafter
       {\csname#8\endcsname}{\cf@encoding}{\undefined}%
    \fi
   \else % \add@flag \z@
    \expandafter\global\expandafter\let\csname#8\endcsname\relax
   \fi % end of \add@flag switch
  \fi % end of \ifcat
 \fi}
\gdef\check@hex@digits@#1#2#3#4#5@!@#6#7#8#9{%
 \ifx @#4\relax
  \UTFacc@warning@{insufficient hex digits #7 for #6-\string#8#9}%
 \else
  \def\UTFchar{\ltjalchar"#1#2#3#4\relax}%
  \expandafter\expandafter\expandafter\declare@utf@composite
  \expandafter\expandafter\expandafter
   {\expandafter\csname#6\endcsname}{\UTFchar}{#8}{#9}\relax
 \fi}
%\gdef\add@UTF@accent#1#2#3{#2\ltjalchar"#1\relax}
\gdef\add@UTF@accent#1#2#3{\ifx\relax#2\relax\ltjalchar"#3\else
 \ifx\ #2\relax\ltjalchar"#3\else
 \expandafter\ifx\UTF@space#2\relax\ltjalchar"#3\else
 \ifx~#2\ltjalchar"#3\else#2\ltjalchar"#1\fi\fi\fi\fi\relax}
\gdef\add@UTF@accents#1#2#3{#2\ltjalchar"#1\ltjalchar"#3\relax}
}
%% End of patch for xunicode.sty "

\RequirePackage{fontspec}
\RequirePackage{luatexja}

\ExplSyntaxOn

\cs_new:Npn \ltj_fontspec_warning:n   { \msg_warning:nn   {ltj-fontspec} }

\msg_new:nnn {ltj-fontspec} {addjfontfeatures-ignored}
{
  \string\addjfontfeature (s)~ ignored;\\
  it~ cannot~ be~ used~ with~ a~ font~ that~ wasn't~ selected~ by~ luatexja-fontspec.
}

\cs_generate_variant:Nn \prg_new_conditional:Nnn {Nnx}

\prg_new_conditional:Nnx \ltj_fontspec_if_liiikeys: {TF,F} {
  \@ifpackagelater {fontspec} {2011/09/13}
  { \exp_not:N \prg_return_true: }  % fontspec >= 2.2 (l3keys)
  { \exp_not:N \prg_return_false: } % fontspec <  2.2 (xkeyval)
}

\cs_new:Nn \ltj_fontspec_define_option:nn {
  \ltj_fontspec_if_liiikeys:TF {
    \keys_define:nn {fontspec} { #1 .code:n = {#2}}
  } {
    \define@key[zf]{options}{#1}{#2}
  }
}

\cs_new:Nn \ltj_fontspec_define_preparse_external:nn {
  \ltj_fontspec_if_liiikeys:TF {
    \keys_define:nn {fontspec-preparse-external} { #1 .code:n = {#2}}
  } {
    \define@key[zf]{preparse-external}{#1}{#2}
  }
}

\cs_new:Nn \ltj_fontspec_select:nn {
  \group_begin:
  \ltj_fontspec_define_option:nn {JFM} {
    \fontspec_update_fontid:n  {+jfm=##1}
    \fontspec_update_featstr:n {jfm=##1}      
  }
  \ltj_fontspec_define_option:nn {JFM-var} {
    \fontspec_update_fontid:n  {+jfmvar=##1}
    \fontspec_update_featstr:n {jfmvar=##1}      
  }
  \ltj_fontspec_define_preparse_external:nn {NoEmbed} {
    \cs_set:Nn \fontspec_namewrap:n {psft:####1}
  }
  %% Omit the warning message
  %% "OpenType feature 'Kerning=Off' (-kern) not available ..."
  \ltj_fontspec_define_option:nn {Kerning/Off} {
    \fontspec_update_fontid:n  {-kern}
    \fontspec_update_featstr:n {-kern}      
  }  
  \ltj_fontspec_if_liiikeys:TF {
    \tl_set:Nn \g_fontspec_encoding_tl {JY3}
  } {
    \tl_set:Nn \zf@enc {JY3}
  }
  \tl_set:Nx \g_fontspec_default_fontopts_tl 
             {JFM=ujis,Scale=0.962216,Kerning=Off,\g_ltj_fontspec_default_fontopts_tl}
  \cs_set_eq:NN \DeclareFontFamily \DeclareKanjiFamily
  \fontspec_select:nn{#1}{#2}
  \group_end:
  \ltj_fontspec_if_liiikeys:F {
    \tl_set_eq:NN \l_fontspec_family_tl \zf@family
  }
}

\cs_new:Nn \ltj_fontspec_set_family:Nnn {
  \ltj_fontspec_select:nn{#2}{#3}
  \tl_set_eq:NN #1 \l_fontspec_family_tl
}

\DeclareDocumentCommand \jfontspec { O{} m } {
  \ltj_fontspec_set_family:Nnn \k@family {#1}{#2}
  \selectfont
  \ignorespaces
}

\DeclareDocumentCommand \setmainjfont { O{} m } {
  \ltj_fontspec_set_family:Nnn \mcdefault {#1}{#2}
  \normalfont
}

\DeclareDocumentCommand \setsansjfont { O{} m } {
  \ltj_fontspec_set_family:Nnn \gtdefault {#1}{#2}
  \normalfont
}

\DeclareDocumentCommand \newjfontfamily { m O{} m } {
  \ltj_fontspec_select:nn{#2}{#3}
  \use:x {
    \exp_not:N \DeclareRobustCommand \exp_not:N #1 {
      \exp_not:N \kanjifamily {\l_fontspec_family_tl} \exp_not:N \selectfont
    }
  }
}

\DeclareDocumentCommand \newjfontface { m O{} m } {
  \newjfontfamily #1 [ BoldFont={},ItalicFont={},SmallCapsFont={},#2 ] {#3}
}

\DeclareDocumentCommand \defaultjfontfeatures {m} {
  \tl_set:Nn \g_ltj_fontspec_default_fontopts_tl {#1,}
}

\tl_clear:N \g_ltj_fontspec_default_fontopts_tl

\DeclareDocumentCommand \addjfontfeatures {m} {
  \ifcsname zf@family@fontdef\k@family\endcsname
    \group_begin:
      \tl_clear:N \g_ltj_fontspec_default_fontopts_tl
      \use:x {
        \exp_not:N\ltj_fontspec_select:nn
          {\csname zf@family@options\k@family\endcsname,#1}
          {\csname zf@family@fontname\k@family\endcsname}
      }
    \group_end:
    \fontfamily\l_fontspec_family_tl\selectfont
  \else
    \ltj_fontspec_warning:n {addjfontfeatures-ignored}
  \fi
  \ignorespaces
}

\cs_set_eq:NN \addjfontfeature \addjfontfeatures

% We reload xunicode package here.
\ExplSyntaxOff\ReloadXunicode{EU2}
\endinput

%% the following patch is needed to xunicode.sty:
%% 
--- /opt/texlive/2011/texmf-dist/tex/xelatex/xunicode/xunicode.sty	2011-09-12 08:31:47.000000000 +0900
+++ xunicode.sty	2011-11-16 22:06:17.061413113 +0900
@@ -1475,7 +1475,11 @@
 
 \newtoks\tipasavetokens
 \newtoks\tipachecktokens
+
+\fi
 \newif\iftipaonetoken
+\expandafter\ifx\csname ReloadXunicode\endcsname\relax
+
 \def\tipalasttoken{!@! do nothing with this !@!}
 \def\tipacatchonechar#1{\begingroup
  \def\textipa##1{##1}% prevent recursion
