%%
%% This is file `spbmark.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% spbmark.dtx  (with options: `package')
%% Copyright (C) 2021 by Qu Yi <quee123@foxmail.com>
%% 
%% This work may be distributed and/or modified under the
%% conditions of the CC-BY 4.0 License.
%% The latest version of this license is in
%%   https://creativecommons.org/licenses/by/4.0/legalcode
\NeedsTeXFormat{LaTeX2e}[2020/10/01]
\RequirePackage{l3keys2e}
\ProvidesExplPackage{spbmark}{2021/06/28}{1.00a}{beta}

\cs_new_protected:Npn \spb_define:n { \keys_define:nn { spbmark } }
\cs_new_protected:Npn \spbset { \keys_set:nn { spbmark } }
\keys_define:nn { spbmark/option }
  {
    ctex .code:n =
      {
        \cs_set:Npn \spb_define:n { \keys_define:nn { ctex/style } }
        \cs_set:Npn \spbset { \keys_set:nn { ctex/style } }
      }
  }

\ProcessKeysOptions{spbmark/option}

\spb_define:n
  {
    super-vshift .dim_set:N   = \l__spb_super_script_vshift_dim,
    super-vshift .initial:n   = 0pt,
    super-hshift .dim_set:N   = \l__spb_super_script_hshift_dim,
    super-hshift .initial:n   = 0pt,
    sub-vshift .dim_set:N     = \l__spb_sub_script_vshift_dim,
    sub-vshift .initial:n     = 0pt,
    sub-hshift .dim_set:N     = \l__spb_sub_script_hshift_dim,
    sub-hshift .initial:n     = 0pt,
    supersubsep .dim_set:N    = \l__spb_supersub_script_sep_dim,
    supersubsep .initial:n    = 0.3ex,
    supersubalign .tl_set:N   = \l__spb_supersub_script_align_tl,
    supersubalign .initial:n  = l
  }

\tl_set:Nn \l__spb_boxa_wd_tl { \box_wd:N \l_tmpa_box }
\tl_set:Nn \l__spb_boxb_wd_tl { \box_wd:N \l_tmpb_box }
\cs_new:Npn \spb_tmp_boxa_use: { \box_use:N \l_tmpa_box }
\cs_new:Npn \spb_tmp_boxb_use: { \box_use:N \l_tmpb_box }
\cs_new:Npn \spb_tmp_hboxa_set:n #1 { \hbox_set:Nn \l_tmpa_box {#1} }
\cs_new:Npn \spb_tmp_hboxb_set:n #1 { \hbox_set:Nn \l_tmpb_box {#1} }
\cs_new:Npn \spb_box_max_dim_horizontal:
  {
    \skip_horizontal:n
      { \dim_max:nn { \l__spb_boxa_wd_tl } { \l__spb_boxb_wd_tl } }
  }
\cs_new:Npn \spb_boxwd_subtract:nnnn #1#2#3#4
  {
    \skip_horizontal:n
      { #1\tl_use:c { l__spb_#3_wd_tl } - #2\tl_use:c { l__spb_#4_wd_tl } }
  }
\cs_new:Npn \spb_boxwd_script_align:nn #1#2
  {
    \spb_boxwd_subtract:nnnn { 0.5 } { 0.5 } {#1} {#2}
    \use:c { spb_tmp_#2_use: }
    \spb_boxwd_subtract:nnnn { -0.5 } { 0.5 } {#1} {#2}
    \use:c { spb_tmp_#1_use: }
  }
\cs_set_eq:NN \spb@textsuperscript@save \textsuperscript
\cs_set_eq:NN \spb@textsubscript@save \textsubscript
\cs_new:Npn \spb_math:nn #1
  {
    \str_case:nn {#1}
      {
        { super } { \sp }
        { sub } { \sb }
      }
  }
\cs_new_protected:Npn \spb_box_dp_ht_assign:nn #1#2
  {
    \tl_if_empty:nF {#1} { \box_set_ht:Nn \l_tmpb_box {#1} }
    \tl_if_novalue:nF {#2} { \box_set_dp:Nn \l_tmpb_box {#2} }
  }
\clist_map_inline:nn
  { super,sub }
  {
    \exp_args:Nc \NewDocumentCommand {#1}
      {
        sO{\dim_use:c { l__spb_#1_script_vshift_dim }}
        >{\SplitArgument{1}{,}}O{}D(){}
        mO{\dim_use:c { l__spb_#1_script_hshift_dim }}
      }
      {
        \IfBooleanTF{##1}
          {\spb_tmp_hboxa_set:n { $##4{\spb_math:nn {#1} {##5}}$ }}
          {
            \mode_if_math:TF
              { \spb_tmp_hboxa_set:n { $##4{\spb_math:nn {#1} {##5}}$ } }
              {
                \spb_tmp_hboxa_set:n
                  { ##4{\use:c { spb@text#1script@save }{##5}} }
              }
          }
        \hbox_set_to_wd:Nnn \l_tmpb_box { \l__spb_boxa_wd_tl + ##6 }
          { \hss\box_move_up:nn {##2} { \spb_tmp_boxa_use: } }
        \spb_box_dp_ht_assign:nn ##3
        \spb_tmp_boxb_use:
      }
  }
\NewDocumentCommand{\supersub}
  {
    sO{\l__spb_supersub_script_sep_dim}
    O{\l__spb_supersub_script_align_tl}D(){}mmO{0pt}D(){}
  }
  {
    \IfBooleanTF{#1}
      {
        \spb_tmp_hboxa_set:n { $#4{\sp{#5}}$ }
        \spb_tmp_hboxb_set:n  { $#8{\sb{#6}}$ }
      }{
        \mode_if_math:TF
          {
            \spb_tmp_hboxa_set:n { $#4{\sp{#5}}$ }
            \spb_tmp_hboxb_set:n  { $#8{\sb{#6}}$ }
          }
          {
            \spb_tmp_hboxa_set:n { #4{\spb@textsuperscript@save{#5}} }
            \spb_tmp_hboxb_set:n  { #8{\spb@textsubscript@save{#6}} }
          }
      }
    \hbox_set_to_wd:Nnn \l_tmpa_box { \l__spb_boxa_wd_tl + #7 }
      { \hss\box_move_up:nn {#2} { \spb_tmp_boxa_use: } }
    \hbox_set_to_wd:Nnn \l_tmpb_box { \l__spb_boxb_wd_tl + #7 }
      { \hss\box_move_down:nn {#2} { \spb_tmp_boxb_use: } }
    \str_case:onF {#3}
      {
        { l } { \use_i:nnn }
        { c } { \use_ii:nnn }
        { r } { \use_iii:nnn }
      }
      {
        \msg_warning:nn { spbmark } { unsupported-align }
        \use_i:nnn
      }
      {
        \hbox_overlap_right:n { \spb_tmp_boxa_use: }
        \hbox_overlap_right:n { \spb_tmp_boxb_use: }
        \spb_box_max_dim_horizontal:
      }
      {
        \dim_compare:nTF { \l__spb_boxa_wd_tl > \l__spb_boxb_wd_tl }
          { \spb_boxwd_script_align:nn { boxa } { boxb } }
          { \spb_boxwd_script_align:nn { boxb } { boxa } }
      }
      {
        \spb_box_max_dim_horizontal:
        \hbox_overlap_left:n { \spb_tmp_boxa_use: }
        \hbox_overlap_left:n { \spb_tmp_boxb_use: }
      }
  }
\msg_new:nnn { spbmark } { unsupported-align }
  {
    Alignment~only~supports~l,~c,~and~r. \\
    The~l~parameter~will~be~used~by~default.
  }
\cs_set_eq:NN \textsuperscript \super
\cs_set_eq:NN \textsubscript \sub
\cs_set:Npn \@makefnmark
  { \hbox:n { \fnmarkfont\super{\@thefnmark}[0pt] } }
\cs_if_exist:NTF \ftntm@font
  { \cs_new:Npn \fnmarkfont { \ftntm@font } }
  { \cs_new:Npn \fnmarkfont { \normalfont } }
\endinput
%%
%% End of file `spbmark.sty'.
