%% ***********************************************************
%%   Copyright 2024 by M.Y. XIA <xiamyphys@gmail.com>        *
%%                                                           *
%%   This work may be distributed and/or modified under      *
%%   the conditions of the LaTeX Project Public License      *
%%                                                           *
%%       http://www.latex-project.org/lppl.txt               *
%%                                                           *
%%   either version 1.3c of this license or any later        *
%%   version.                                                *
%%                                                           *
%%   This work has the LPPL maintenance status `maintained'. *
%%                                                           *
%%   The Current Maintainers of this work is M.Y. XIA        *
%%                                                           *
%%   This work consists of the files notebeamer.sty,         *
%%                               and README.md.              *
%%   available at https://github.com/xiamyphys/notebeamer    *
%% ***********************************************************
\RequirePackage{xparse}% For TeX Live 2019 - 2020 Compatibility
\ProvidesExplPackage{notebeamer}{2024/10/15}{4.0A}
  {Package provides macros for inputting slides on note papers quickly.}

\RequirePackage{l3graphics,tikz,tikzpagenodes}

\cs_new:Nn \int_div_roundup:nn { \fp_eval:n { ceil(#1/#2,0) } }

\seq_new:N \l__notebeamer_tmpa_seq 
\seq_new:N \l__notebeamer_tmpb_seq
\clist_new:N \l__notebeamer_tmpa_clist
\cs_set_nopar:Npn \notebeamer_range_to_list:nN #1#2
  {
    \clist_clear:N #2
    \seq_set_split:Nnn \l__notebeamer_tmpa_seq { , } {#1}
    \seq_map_inline:Nn \l__notebeamer_tmpa_seq 
      {
        \tl_if_in:nnTF {##1}{ - }
          {
            \seq_set_split:Nnn \l__notebeamer_tmpb_seq { - } { ##1 }
            \int_step_inline:nnn 
              { \seq_item:Nn \l__notebeamer_tmpb_seq { 1 } } 
              { \seq_item:Nn \l__notebeamer_tmpb_seq { 2 } } 
              { \clist_put_right:Nn #2 {####1} }
          }
          { \clist_put_right:Nn #2 {##1} }
      }
  }

\keys_define:nn { notebeamer / includebeamer }
  {
    pages .tl_set:N      = \l__includebeamer_pages_tl,
    pages .initial:n     = 1,
    nup .int_set:N       = \l__includebeamer_nup_int,
    nup .initial:n       = 3,
    lines .int_set:N     = \l__includebeamer_lines_int,
    color .tl_set:N      = \l__includebeamer_color_tl,
    color .initial:n     = black,
    ratio .fp_set:N      = \l__includebeamer_ratio_fp,
    ratio .initial:n     = .48,
    sep .dim_set:N       = \l__includebeamer_sep_dim,
    sep .initial:n       = 2ex,
    lefthead .tl_set:N   = \l__includebeamer_lefthead_tl,
    righthead .tl_set:N  = \l__includebeamer_righthead_tl,
  }

\int_new:N \l__notebeamer_pages_total_int
\int_new:N \l__notebeamer_pages_residue_int
\cs_new_protected_nopar:Npn \__notebeamer_empty_note_aux:
  {
    \tikz [ remember~picture, overlay ]
    {
      \draw [ very~thick, \l__includebeamer_color_tl!80 ]
            (current~page~text~area.north~west) --
            (current~page~text~area.north~east)
       node [ at~start, above~right, font = \sffamily\Large\bfseries ]
        { \l__includebeamer_lefthead_tl }
       node [ above~left, font = \sffamily\Large\bfseries ]
        { \l__includebeamer_righthead_tl };
      \draw [ very~thick, \l__includebeamer_color_tl!80 ]
            (current~page~text~area.south~west) --
            (current~page~text~area.south~east);
      \int_step_inline:nn { \l__includebeamer_lines_int - 1 }
        {
          \draw [ thick, \l__includebeamer_color_tl!60 ]
          ([shift =
            {(
              \fp_use:N \l__includebeamer_ratio_fp \textwidth,
              -\textheight/\l__includebeamer_lines_int * ##1
            )}
           ]current~page~text~area.north~west) --++
          (\fp_eval:n { 1 - \l__includebeamer_ratio_fp } \textwidth,0);
        }
    }
    \pagestyle{empty}
  }

\cs_new_protected_nopar:Npn \__notebeamer_includebeamer_aux:n #1
  {
    \graphics_get_pagecount:nN {#1} \l__includebeamer_filepages_int
    \tl_if_eq:NnTF \l__includebeamer_pages_tl { - }
      {
        \notebeamer_range_to_list:nN
          { 1 - \l__includebeamer_filepages_int } \l__notebeamer_tmpa_clist
      }
      {
        \exp_args:NV
        \notebeamer_range_to_list:nN
          \l__includebeamer_pages_tl \l__notebeamer_tmpa_clist
      }
    \int_set:Nn \l__notebeamer_pages_total_int
      {
        \int_div_roundup:nn { \clist_count:N \l__notebeamer_tmpa_clist }
          { \l__includebeamer_nup_int } - 1
      }
    \int_set:Nn \l__notebeamer_pages_residue_int
      {
        \int_eval:n
          {
            \clist_count:N \l__notebeamer_tmpa_clist -
            \l__includebeamer_nup_int * \l__notebeamer_pages_total_int
          }
      }
    \int_step_inline:nn { \int_use:N \l__notebeamer_pages_total_int }
      {
        \clearpage
        \__notebeamer_empty_note_aux:
        \int_step_inline:nn { \l__includebeamer_nup_int }
          {
            \tikz [ remember~picture, overlay ]
              {
                \node
                  [
                    yshift = - ( ####1 - 1 ) *
                              \textheight/\l__includebeamer_nup_int
                  ] at 
                  ([shift =
                    {(
                      \fp_use:N \l__includebeamer_ratio_fp \textwidth/2,
                      -\textheight/\l__includebeamer_nup_int/2
                    )}
                   ]current~page~text~area.north~west)
                  {
                    \includegraphics
                      [
                        height =
                          \dim_eval:n 
                            {
                              \textheight/\l__includebeamer_nup_int - 
                              \l__includebeamer_sep_dim
                            },
                        page =
                          \clist_item:Nn \l__notebeamer_tmpa_clist
                            { ####1 + \l__includebeamer_nup_int * ( ##1 - 1 ) }
                      ] {#1}
                  };
              }
          }
        \clearpage
      }
    \__notebeamer_empty_note_aux:
    \int_step_inline:nn { \int_use:N \l__notebeamer_pages_residue_int }
      {
        \tikz [ remember~picture, overlay ]
          {
            \node
              [
                yshift = - ( ##1 - 1 ) * \textheight/\l__includebeamer_nup_int
              ] at
              ([shift = 
              {(
                \fp_use:N \l__includebeamer_ratio_fp \textwidth/2,
                -\textheight/\l__includebeamer_nup_int/2
              )}
               ]current~page~text~area.north~west)
              {
                \includegraphics
                  [
                    height =
                      \dim_eval:n
                        {
                          \textheight/\l__includebeamer_nup_int - 
                          \l__includebeamer_sep_dim
                        },
                    page = 
                      \clist_item:Nn \l__notebeamer_tmpa_clist
                        {
                          \l__includebeamer_nup_int * 
                          \l__notebeamer_pages_total_int + ##1
                      }
                  ] {#1}
              };
          }
      }
    \clearpage
  }
\NewDocumentCommand{\includebeamer}{ O{} m O{} }{
  \group_begin:
  \keys_set:nn { notebeamer / includebeamer } { #1 #3 }
  \__notebeamer_includebeamer_aux:n {#2}
  \group_end:
}

% End of file notebeamer.sty
