% Author        : C. Pierquet (tks priiduonu for the tip with stroke color !)
% licence       : Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txtf
% licence icons : CC0 license https://khushmeen.com/icons

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{doodle-icons}[2026/10/31 v0.1c doodle-icons]

%====history
% 0.1c : option [nolist]
% 0.1b : bugfix with prop
% 0.1a : initial version

%====packages
\RequirePackage{graphicx}

%====suppress warnings
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname pdfsuppresswarningpagegroup\endcsname\relax
\else
  \pdfsuppresswarningpagegroup=1\relax
\fi

\ExplSyntaxOn

%====variables
\tl_new:N   \l__doodleicon_page_tl
\tl_new:N   \l__doodleicon_height_tl
\tl_new:N   \l__doodleicon_strut_tl
\tl_new:N   \l__doodleicon_dstrut_tl
\dim_new:N  \l__doodleicon_height_dim
\dim_new:N  \l__doodleicon_depth_dim
\box_new:N  \l__doodleicon_box
\seq_new:N  \l__doodleicon_seq
\bool_new:N \l__doodleicon_nolist_bool

%====keys
\keys_define:nn { doodleicon }
  {
    height .tl_set:N  = \l__doodleicon_height_tl ,
    height .initial:n = dauto ,
    strut  .tl_set:N  = \l__doodleicon_strut_tl ,
    strut  .initial:n = X ,
    dstrut .tl_set:N  = \l__doodleicon_dstrut_tl ,
    dstrut .initial:n = qX ,
  }

%====package option : [nolist]
\DeclareOption { nolist } { \bool_set_true:N \l__doodleicon_nolist_bool }
\ProcessOptions\relax

%====messages
\msg_new:nnn { doodle-icons } { unknown-icon }
  { Unknown~icon~'#1'. }

%====icon registration : page number in doodleicons-all.pdf
\NewDocumentCommand \GenerateDoodleIcons { m m }
  { \cs_gset_nopar:cpn { __doodleicon_page_ #2 } {#1} }

%====alias : \AliasDoodleIcon{new name}{existing name} (to be used after the icons are registered)
\NewDocumentCommand \AliasDoodleIcon { m m }
  {
    \cs_if_exist:cTF { __doodleicon_page_ #2 }
      { \cs_gset_eq:cc { __doodleicon_page_ #1 } { __doodleicon_page_ #2 } }
      { \msg_error:nnn { doodle-icons } { unknown-icon } {#2} }
  }

%====includegraphics call (#1 = height, #2 = extra keys)
\cs_new_protected:Npn \__doodleicon_include:nn #1#2
  {
    \includegraphics
      [ page = \l__doodleicon_page_tl , height = #1 , #2 ]
      { doodleicons-all.pdf }
  }

%====main macro
\cs_new_protected:Npn \__doodleicon_typeset:n #1
  {
    \str_if_eq:VnTF \l__doodleicon_height_tl { auto }
      {
        % height of the strut
        \hbox_set:Nn \l__doodleicon_box { \l__doodleicon_strut_tl }
        \dim_set:Nn \l__doodleicon_height_dim { \box_ht:N \l__doodleicon_box }
        \__doodleicon_include:nn { \l__doodleicon_height_dim } {#1}
      }
      {
        \str_if_eq:VnTF \l__doodleicon_height_tl { dauto }
          {
            % total height and depth of the dstrut
            \hbox_set:Nn \l__doodleicon_box { \l__doodleicon_dstrut_tl }
            \dim_set:Nn \l__doodleicon_depth_dim { \box_dp:N \l__doodleicon_box }
            \dim_set:Nn \l__doodleicon_height_dim
              { \box_ht:N \l__doodleicon_box + \box_dp:N \l__doodleicon_box }
            \raisebox { - \l__doodleicon_depth_dim }
              { \__doodleicon_include:nn { \l__doodleicon_height_dim } {#1} }
          }
          {
            \seq_set_split:NnV \l__doodleicon_seq { / } \l__doodleicon_height_tl
            \int_compare:nNnTF { \seq_count:N \l__doodleicon_seq } > { 1 }
              {
                % height/depth given manually
                \dim_set:Nn \l__doodleicon_height_dim { \seq_item:Nn \l__doodleicon_seq { 1 } }
                \dim_set:Nn \l__doodleicon_depth_dim  { \seq_item:Nn \l__doodleicon_seq { 2 } }
                \raisebox { \l__doodleicon_depth_dim }
                  { \__doodleicon_include:nn { \l__doodleicon_height_dim } {#1} }
              }
              {
                \dim_set:Nn \l__doodleicon_height_dim { \l__doodleicon_height_tl }
                \__doodleicon_include:nn { \l__doodleicon_height_dim } {#1}
              }
          }
      }
  }

\NewDocumentCommand \doodleicon { O{} D<>{} m }
  {
    \group_begin:
      \keys_set:nn { doodleicon } {#1}
      \cs_if_exist:cTF { __doodleicon_page_ #3 }
        {
          \tl_set_eq:Nc \l__doodleicon_page_tl { __doodleicon_page_ #3 }
          \__doodleicon_typeset:n {#2}
        }
        { \msg_error:nnn { doodle-icons } { unknown-icon } {#3} }
    \group_end:
  }

%====loading of the list of the icons
\cs_new_protected:Npn \__doodleicon_load_list:
  {
    \bool_if:NF \l__doodleicon_nolist_bool
      {
        \ExplSyntaxOff
        \input { doodle-icons-list.def }
        \ExplSyntaxOn
      }
  }
\__doodleicon_load_list:
 
\ExplSyntaxOff

\endinput