%
% ***************** THE SPBMARK 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}[2018/12/31]
\RequirePackage{xparse,l3keys2e}
\ProvidesExplPackage{spbmark}{2021/07/30}{1.1a}
  {Customize superscripts and subscripts}

\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 } }
      },
    newsupb .bool_set:N = \l__spb_new_supersub_bool,
    newsupb .default:n  = true,
    newsupb .initial:n  = true
  }

\ProcessKeysOptions{spbmark/option}

\tl_new:N \l__spb_super_script_formatii_tl
\tl_new:N \l__spb_sub_script_formatii_tl
\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,
    supersub-hshift .dim_set:N  = \l__spb_supersub_script_hshift_dim,
    supersub-hshift .initial:n  = 0pt,
    super-format .tl_set:N      = \l__spb_super_script_formati_tl,
    sub-format .tl_set:N        = \l__spb_sub_script_formati_tl,
    supersub-format .code:n     = \spb_both_format_assign:n {#1},
    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 }
\tl_set:Nn \l__spb_box_max_wd_tl
  { \dim_max:nn { \l__spb_boxa_wd_tl } { \l__spb_boxb_wd_tl } }
\providecommand{\hbox_overlap_center:n}[1]
  { \hbox_to_zero:n { \hss #1\hss } }
\cs_new:Npn \spb_boxa_move_up:n #1
  { \box_move_up:nn {#1} { \box_use:N \l_tmpa_box } }
\cs_new:Npn \spb_boxa_move_down:n #1
  { \box_move_down:nn {#1} { \box_use:N \l_tmpa_box } }
\cs_new:Npn \spb_boxb_move_down:n #1
  { \box_move_down:nn {#1} { \box_use:N \l_tmpb_box } }
\cs_new:Npn \spb_boxa_vshift:nn #1
  {
    \str_case:nn {#1}
      {
        { super } { \spb_boxa_move_up:n }
        { sub } { \spb_boxa_move_down:n }
      }
  }
\cs_new:Npn \spb_hbox_overlap_vshift:n #1#2
  {
    \use:c { hbox_overlap_#2:n } { \spb_boxa_move_up:n {#1} }
    \use:c { hbox_overlap_#2:n } { \spb_boxb_move_down:n {#1} }
  }
\cs_new:Npn \spb_math_print_store:nnn #1#2#3
  {
    \hbox_set:Nn \l_tmpa_box
      { \ensuremath{#1{\spb_math:nn {#2} {#3}}} }
  }
\cs_new:Npn \spb_text_print_store:nnn #1#2#3
  {
    \hbox_set:Nn \l_tmpa_box
      { #1{\use:c { spb@text#2script@save }{#3}} }
  }
\cs_new:Npn \spb_math_print_store:nnnn #1#2#3#4
  {
    \hbox_set:Nn \l_tmpa_box { \ensuremath{#1{\spb@sp@save{#3}}} }
    \hbox_set:Nn \l_tmpb_box
      { \ensuremath{\tl_if_novalue:nF {#2} {#2}{\spb@sb@save{#4}}} }
  }
\cs_new:Npn \spb_text_print_store:nnnn #1#2#3#4
  {
    \hbox_set:Nn \l_tmpa_box { #1{\spb@textsuperscript@save{#3}} }
    \hbox_set:Nn \l_tmpb_box
      { \tl_if_novalue:nF {#2} {#2}{\spb@textsubscript@save{#4}} }
  }
\cs_set_eq:NN \spb@textsuperscript@save \textsuperscript
\cs_set_eq:NN \spb@textsubscript@save \textsubscript
\cs_set_eq:NN \spb@sp@save \sp
\cs_set_eq:NN \spb@sb@save \sb
\cs_new:Npn \spb_math:nn #1
  {
    \str_case:nn {#1}
      {
        { super } { \spb@sp@save }
        { sub } { \spb@sb@save }
      }
  }
\cs_new_protected:Npn \spb_box_ht_dp_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} }
  }
\NewDocumentCommand{\spb_both_format_assign:n}
  {>{\SplitArgument{1}{,}}m}
  {\spb_supersub_script_both_format:nn #1}
\cs_new_protected:Npn \spb_supersub_script_both_format:nn #1#2
  {
    \tl_set:Nn \l__spb_super_script_formatii_tl {#1}
    \tl_if_novalue:nTF {#2}
      { \tl_clear:N \l__spb_sub_script_formatii_tl }
      { \tl_set:Nn \l__spb_sub_script_formatii_tl {#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(){\tl_use:c { l__spb_#1_script_formati_tl }}
        mO{\dim_use:c { l__spb_#1_script_hshift_dim }}
      }
      {
        \IfBooleanTF{##1}
          {\spb_math_print_store:nnn {##4} {#1} {##5}}
          {
            \mode_if_math:TF
              { \spb_math_print_store:nnn {##4} {#1} {##5} }
              { \spb_text_print_store:nnn {##4} {#1} {##5} }
          }
        \hbox_set:Nn \l_tmpa_box
          {
            \dim_compare:nNnTF {##6} < { 0pt }
              {
                \hbox_overlap_left:n
                  {
                    \hbox_to_wd:nn { \l__spb_boxa_wd_tl - ##6 }
                      { \spb_boxa_vshift:nn {#1} {##2}\hss }
                  }
              }
              {
                \hbox_to_wd:nn { \l__spb_boxa_wd_tl + ##6 }
                  { \hss\spb_boxa_vshift:nn {#1} {##2} }
              }
          }
        \spb_box_ht_dp_assign:nn ##3
        \box_use:N \l_tmpa_box
      }
  }
\NewDocumentCommand{\supersub}
  {
    sO{\l__spb_supersub_script_sep_dim}
    D(){\l__spb_supersub_script_align_tl}
    mmO{\l__spb_supersub_script_hshift_dim}
    >{\SplitArgument{1}{,}}D()
    {
      \l__spb_super_script_formatii_tl,
      \l__spb_sub_script_formatii_tl
    }
  }
  {
    \IfBooleanTF{#1}
      {\spb_math_print_store:nnnn #7 {#4} {#5}}
      {
        \mode_if_math:TF
          { \spb_math_print_store:nnnn #7 {#4} {#5} }
          { \spb_text_print_store:nnnn #7 {#4} {#5} }
      }
    \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
      }
      {
        \dim_compare:nNnTF {#6} < { 0pt }
          {
            \hbox_overlap_left:n
              {
                \spb_hbox_overlap_vshift:n {#2} { right } 
                \skip_horizontal:n { -#6 }
              }
          }
          {
            \hbox:n
              {
                \skip_horizontal:n {#6}
                \spb_hbox_overlap_vshift:n {#2} { right } 
                \skip_horizontal:n { \l__spb_box_max_wd_tl }
              }
          }
      }
      {
        \dim_compare:nNnTF {#6} < { 0pt }
          {
            \hbox_overlap_left:n
              {
                \spb_hbox_overlap_vshift:n {#2} { center } 
                \skip_horizontal:n { \l__spb_box_max_wd_tl/2 - #6 }
              }
          }
          {
            \hbox:n
              {
                \skip_horizontal:n { \l__spb_box_max_wd_tl/2 + #6 }
                \spb_hbox_overlap_vshift:n {#2} { center } 
                \skip_horizontal:n { \l__spb_box_max_wd_tl/2 }
              }
          }
      }
      {
        \dim_compare:nNnTF {#6} < { 0pt }
          {
            \hbox_overlap_left:n
              {
                \spb_hbox_overlap_vshift:n {#2} { left } 
                \skip_horizontal:n { -#6 }
              }
          }
          {
            \hbox:n
              {
                \skip_horizontal:n { \l__spb_box_max_wd_tl + #6 }
                \spb_hbox_overlap_vshift:n {#2} { left } 
              }
          }
      }
  }
\msg_new:nnn { spbmark } { unsupported-align }
  {
    Alignment~only~supports~l,~c,~and~r. \\
    The~l~parameter~will~be~used~by~default.
  }

\bool_if:NT \l__spb_new_supersub_bool
  {
    \clist_map_inline:nn
      {
        { sp,sb },
        { textsuperscript,textsubscript }
      }
      {
        \cs_set_eq:cN { \clist_item:nn {#1} { 1 } } \super
        \cs_set_eq:cN { \clist_item:nn {#1} { 2 } } \sub
      }
  }
\cs_set_eq:NN \spb \supersub
\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'.