% Author     : C. Pierquet
% licence    : Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txt

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{CreationBoites}[2026/02/12 0.20a Macros pour creer des boites tcbox simples, avec personnalisation(s)]

%====HISTORIQUE
% v 0.20a  Ajout d'un mode de sauvegarde des couleurs à l'intérieur des boîtes
% v 0.1.9  Bugfix avec AltBoite et compteur...
% v 0.1.8  Style alternatif (simplifiée) + option ulem
% v 0.1.7  Suppression dépendance à xstring (avec macros internes)
% v 0.1.6  Option pour compatibilité avec fontawesome5/6/7
% v 0.1.5  Modification des espacements
% v 0.1.4  Ajout d'une option 'style peros rajouté' à la macro principale
% v 0.1.3  Amélioration du code pour twemoji
% v 0.1.2  Option pour compatibilité avec fontawesome5/6
% v 0.1.1  Correction d'un bug avec [breakable] pour autoriser la coupure des boîtes + Clé pour décaler le logo CoinBasDroit + Décoration Icone
% v 0.1.0  Version initiale

%====OPTION
\newif\if@breakable \@breakablefalse
\DeclareOption{breakable}{\@breakabletrue}
\newif\if@ulem \@ulemfalse
\newif\if@normalulem \@normalulemfalse
\DeclareOption{ulem}{\@ulemtrue}
\DeclareOption{normalulem}{\@normalulemtrue}
\newif\if@twemojis \@twemojistrue
\newif\if@fafive \@fafivetrue
\newif\if@fasix \@fasixfalse
\newif\if@faseven \@fasevenfalse
\DeclareOption{nonfa}{\@fafivefalse\@fasixfalse\@fasevenfalse}
\DeclareOption{fa6}{\@fafivefalse\@fasixtrue\@fasevenfalse}
\DeclareOption{fa7}{\@fafivefalse\@fasixfalse\@faseventrue}
\DeclareOption*{}%si option inconnue ;-)
\ProcessOptions\relax

%====BASE
\RequirePackage{tcolorbox}
\tcbuselibrary{fitting,skins}
\usetikzlibrary{calc}
\if@fafive
  \RequirePackage{fontawesome5}
\fi
\if@fasix
  \RequirePackage{fontawesome6}
\fi
\if@faseven
  \RequirePackage{fontawesome7}
\fi
\if@twemojis
  \RequirePackage{twemojis}
\fi
\if@ulem
  \RequirePackage{ulem}
\fi
\if@normalulem
  \RequirePackage[normalem]{ulem}
\fi
\RequirePackage{simplekv}

%====INT MACRO L3
\ExplSyntaxOn

\NewDocumentCommand\intcbteststreq{ m m }%l3-ifeq
  {
    \tl_set:Ne \l_tmpa_tl { #1 }
    \tl_set:Ne \l_tmpb_tl { #2 }
    \tl_if_eq:NNTF \l_tmpa_tl \l_tmpb_tl { \use_i:nn } { \use_ii:nn }
  }

\NewDocumentCommand\intcbbeforechar{ m m o }%l3-strbefore
  {
    \seq_set_split:Nee \l_tmpa_seq { #2 } { #1 }
    \IfNoValueTF{#3}
      {
        \seq_item:Nn \l_tmpa_seq { 1 }
      }
      {
      \tl_set:Ne #3 { \seq_item:Nn \l_tmpa_seq { 1 } }
      }
  }
  
\NewDocumentCommand\intcbafterchar{ m m o }%l3-strbehind
  {
    \seq_set_split:Nee \l_tmpa_seq { #2 } { #1 }
    \IfNoValueTF{#3}
      {
        \seq_item:Nn \l_tmpa_seq { 2 }
      }
      {
      \tl_set:Ne #3 { \seq_item:Nn \l_tmpa_seq { 2 } }
      }
  }
  
\DeclareExpandableDocumentCommand\CreaBoitesIfNoValueOrEmptyTF{mmm}{\IfNoValueTF{#1}{#2}{\tl_if_empty:nTF {#1} {#2} {#3}}}

\ExplSyntaxOff

%====CRÉATION DES COMPTEURS ET DES PARAMÈTRES DE BASE
\NewDocumentCommand\CreerCompteur{ m }{%
  \newcounter{#1}%création
  \setcounter{#1}{0}%initialisation
}
\newcommand\policetitreboite{\bfseries\sffamily}
\newcommand\policesoustitreboite{\small\bfseries\sffamily}
\newcommand\couleursoustitreboite{red}
\newcommand\tailleiconeboite{\small}
\newcommand\margeboite{4mm}
\newcommand\eptraitboite{1.25pt}
\newcommand\margeinttitreboite{3pt}

%====STYLE GÉNÉRAL DES TCBOX
\if@breakable
  \tcbuselibrary{breakable}
  \tcbset{baseboite/.style={
    enhanced,sharp corners=uphill,boxrule=\eptraitboite,%
    breakable,%
    before skip=0.5em,after skip=0.5em,%
    colback=white,top=4mm,left={\margeboite-\margeinttitreboite-\eptraitboite},right={\margeboite-\margeinttitreboite-\eptraitboite}
    }%
  }
\else
  \tcbset{baseboite/.style={
    enhanced,sharp corners=uphill,boxrule=\eptraitboite,%
    before skip=0.5em,after skip=0.5em,%
    colback=white,top=4mm,left={\margeboite-\margeinttitreboite-\eptraitboite},right={\margeboite-\margeinttitreboite-\eptraitboite}
    }%
  }
\fi

%====MACROS TITRE/SOUS-TITRE/LOGO

\NewDocumentCommand\TitreBoite{ m m m m }{%
  %1=couleur
  %2=icone
  %3=noms
  %4=compteur
  \node[inner sep=\margeinttitreboite,rounded corners=3pt,draw=#1,line width=\eptraitboite,rectangle,fill=#1!5!white,anchor=west,xshift=\margeboite,text=black,font=\policetitreboite] at (frame.north west)
  {%
    \,%
    \CreaBoitesIfNoValueOrEmptyTF{#2}{}{{\tailleiconeboite#2}\space}%
    \vphantom{Ppé}%
    \ifboolKV[Boites]{Pluriel}{\intcbafterchar{#3}{/}}{\intcbbeforechar{#3}{/}}%
    \intcbteststreq{\BoxModifLabel}{}%
      {}%
      {\BoxModifLabel}%
    \ifboolKV[Boites]{Compteur}{\space#4}{}%
    \intcbteststreq{\BoxCpltTitle}{}%
      {}%
      {\BoxCpltTitle}%
    \,%
  } ;
}

\NewDocumentCommand\SousTitreBoite{ }{%
  \node[fill=white,anchor=east,xshift=-\margeboite,text=\couleursoustitreboite!75!black,font=\policesoustitreboite] at (frame.north east) {\vphantom{Ppé}\BoxSubTitle} ;
}
\NewDocumentCommand\LogoCoinDroit{ }{%OK
  \begin{tcbclipinterior}\node[opacity=\BoxOpaciteLogo,rotate=-\BoxRotationLogo] at ($(interior.south east)+(\BoxOffsetLogo)$){\includegraphics[height=\BoxHauteurLogo]{\BoxLogo}};\end{tcbclipinterior}%
}
\NewDocumentCommand\EmojiCoinDroit{ }{%OK
  \begin{tcbclipinterior}\node[opacity=\BoxOpaciteLogo,rotate=-\BoxRotationLogo] at ($(interior.south east)+(\BoxOffsetLogo)$){\twemoji[height=\BoxHauteurLogo]{\BoxLogoEmoji}};\end{tcbclipinterior}%
}
\NewDocumentCommand\IconeCoinDroit{ }{%Ok
  \begin{tcbclipinterior}\node[opacity=\BoxOpaciteIcone,rotate=-\BoxRotationIcone,scale=\BoxEchelleIcone] at ($(interior.south east)+(\BoxOffsetLogo)$){\BoxIcone};\end{tcbclipinterior}%
}

%====MACRO POUR QQUES PARAMÈTRES BASIQUES
\defKV[ParamBoites]{%
  PoliceTitre=\renewcommand\policetitreboite{#1},%
  PoliceSousTitre=\renewcommand\policesoustitreboite{#1},%
  Marge=\renewcommand\margeboite{#1},%
  TailleIcone=\renewcommand\tailleiconeboite{#1},%
  CouleurSousTitre=\renewcommand\couleursoustitreboite{#1},%
  EpaisseurBordure=\renewcommand\eptraitboite{#1},%
  MargeTitre=\renewcommand\margeinttitreboite{#1}
}
\setKVdefault[ParamBoites]{%
  PoliceTitre=\bfseries\sffamily,%
  PoliceSousTitre=\small\bfseries\sffamily,%
  Marge=4mm,%
  TailleIcone=\small,%
  CouleurSousTitre=red,%
  EpaisseurBordure=1.25pt,%
  MargeTitre=3pt
}
\NewDocumentCommand\ParamBoites{ O{} }{%
  \useKVdefault[ParamBoites]%
  \setKV[ParamBoites]{#1}%
}

%====MACRO PRINCIPALE
\defKV[Boites]{%
  ComplementTitre=\def\BoxCpltTitle{#1},%
  SousTitre=\def\BoxSubTitle{#1},%
  Logo=\def\BoxLogo{#1},%
  Emoji=\def\BoxLogoEmoji{#1},%
  ModifLabel=\def\BoxModifLabel{#1},%
  HauteurLogo=\def\BoxHauteurLogo{#1},%
  RotationLogo=\def\BoxRotationLogo{#1},%
  OpaciteLogo=\def\BoxOpaciteLogo{#1},%
  OffsetDeco=\def\BoxOffsetLogo{#1},%
  Icone=\def\BoxIcone{#1},%
  EchelleIcone=\def\BoxEchelleIcone{#1},%
  RotationIcone=\def\BoxRotationIcone{#1},%
  OpaciteIcone=\def\BoxOpaciteIcone{#1}
}
\setKVdefault[Boites]{%
  Pluriel=false,%
  ComplementTitre={},%
  SousTitre={},
  Compteur=true,%
  Logo={},%
  Emoji={},%
  ModifLabel={},%
  HauteurLogo=24pt,%
  RotationLogo=45,%
  OpaciteLogo=0.33,%
  OffsetDeco={-10pt,10pt},%
  Icone={},%
  EchelleIcone=1,%
  RotationIcone=30,%
  OpaciteIcone=0.5
}

\NewDocumentCommand\CreationBoite{ O{black} m m m m !O{} }{%
  %1 = couleur
  %2 = nom
  %2 = compteur
  %4 = icone
  %5 = label singulier/pluriel
  %6 = options persos tcbox
  \CreerCompteur{#3}%
  \colorlet{cbcoulbord#2}{#1}%
  \colorlet{cbcoulfond#2}{#1!5!white}%
  \NewDocumentEnvironment{#2}{ O{} D<>{} }%
    %1 = clés
    %2 = options spécifiques tcbox
    {%
      \colorlet{cbcoulbord}{cbcoulbord#2}%
      \colorlet{cbcoulfond}{cbcoulfond#2}%
      \useKVdefault[Boites]%
      \setKV[Boites]{##1}%
      \ifboolKV[Boites]{Compteur}{\stepcounter{#3}}{}%on augmente le compteur si demandé
      \tcbset{reset}%je pense que c'est mieux
      \tcolorbox[baseboite,colframe=#1,##2,overlay={%
        \intcbteststreq{\BoxLogo}{}{}{\LogoCoinDroit}%logo 
        \intcbteststreq{\BoxIcone}{}{}{\IconeCoinDroit}%icone
        \intcbteststreq{\BoxLogoEmoji}{}{}{\EmojiCoinDroit}%emoji
        \tikzset{overlay=false}%
        \TitreBoite{#1}{#4}{#5}{\arabic{#3}}%titre
        \intcbteststreq{\BoxSubTitle}{}{}{\SousTitreBoite}%soustitre
      },#6]%
    }%
  {\endtcolorbox}%
}

%====MACRO STYLE SIMPLIFIÉ
\def\opaciteboitealt{2.5}
\if@breakable
  \tcbuselibrary{breakable}
  \tcbset{basecpcours/.style={
      enhanced,empty,size=small,boxsep=2.25pt,top=0pt,bottom=2.25pt,left=2.25pt,right=2.25pt,breakable,
      overlay={%
        \fill[#1!\opaciteboitealt] (frame.north west) rectangle (frame.south east) ;
        \draw[line width=1.5pt,#1] (frame.north west) -- (frame.south west) ;
        %\draw[line width=1.25pt,#1] ([xshift=2.5mm]frame.north west) -- (frame.north west) |- ([xshift=7.5mm]frame.south west) ;
      }
    }
  }
\else
  \tcbset{basecpcours/.style={
      enhanced,empty,size=small,boxsep=2.25pt,top=0pt,bottom=2.25pt,left=2.25pt,right=2.25pt,
      overlay={%
        \fill[#1!\opaciteboitealt] (frame.north west) rectangle (frame.south east) ;
        \draw[line width=1.5pt,#1] (frame.north west) -- (frame.south west) ;
        %\draw[line width=1.25pt,#1] ([xshift=2.5mm]frame.north west) -- (frame.north west) |- ([xshift=7.5mm]frame.south west) ;
      }
    }
  }
\fi

\defKV[cpaltboites]{%
  ComplementTitre=\def\cpaltboitescplttitre{#1},%
  SousTitre=\def\cpaltboitessoustitre{#1}
}

\setKVdefault[cpaltboites]{%
  ComplementTitre={},%
  SousTitre={},%
  Compteur=true,%
  Pluriel=false,%
  SouligneLabel=false
}

\NewDocumentCommand\CreationBoiteAlt{ O{} m m m m m !O{} }{%
  %1 = couleur
  %2 = nom
  %3 = compteur
  %4 = label singulier
  %5 = label pluriel
  %6 = before label
  %7 = options persos tcbox
  %counter
  \newcounter{#3}%création
  \setcounter{#3}{0}%initialisation
  %testcouleurl3
  \colorlet{cbcoulbord#2}{#1}%
  \colorlet{cbcoulfond#2}{#1!\opaciteboitealt}%
  %next
  \NewDocumentEnvironment{#2}{ O{} D<>{} }%
  {%
    \restoreKV[cpaltboites]%
    \setKV[cpaltboites]{##1}%
    \ifboolKV[cpaltboites]{Pluriel}%
      {\def\tmplabelboite{#5}}%
      {\def\tmplabelboite{#4}}%
    \tcbset{reset}%
    \tcolorbox[basecpcours={#1},#7,##2]%
    \colorlet{cbcoulbord}{cbcoulbord#2}%
    \colorlet{cbcoulfond}{cbcoulfond#2}%
    \ifboolKV[cpaltboites]{SouligneLabel}
      {%
        \renewcommand\ULthickness{1pt}%
        \settodepth\ULdepth{\hbox{pqyj}}%
      }%
      {}%
    \ifboolKV[cpaltboites]{Compteur}%
    {%
      \ifboolKV[cpaltboites]{SouligneLabel}%
        {%
          \textcolor{#1!33!black}%
            {%
              \policetitreboite%
              {#6}%
              {\uline{\stepcounter{#3}{\vphantom{(qPÉ)}\tmplabelboite\cpaltboitescplttitre~\arabic{#3}}}}\textit{\cpaltboitessoustitre}%
            }%
          \par
        }%
        {%
          \textcolor{#1!33!black}%
            {%
              \policetitreboite%
              \stepcounter{#3}%
              \vphantom{(qPÉ)}{#6}\tmplabelboite\cpaltboitescplttitre~\arabic{#3}\textit{\cpaltboitessoustitre}%
            }%
          \par
        }%
    }%
    {%
      \ifboolKV[cpaltboites]{SouligneLabel}%
        {%
          \textcolor{#1!33!black}%
            {%
              \policetitreboite%
              {#6}%
              {\uline{{\vphantom{(qPÉ)}\tmplabelboite\cpaltboitescplttitre}}\textit{\cpaltboitessoustitre}}%
            }%
          \par
        }%
        {%
          \textcolor{#1!33!black}%
            {%
              \policetitreboite%
              \vphantom{(qPÉ)}{#6}\tmplabelboite\cpaltboitescplttitre\textit{\cpaltboitessoustitre}%
            }
            %
          \par
        }%
    }%
    \vspace*{0.5mm}%
  }%
  {%
    \endtcolorbox%
  }%
}

\NewDocumentCommand\CreationBoiteAltResetSection{ O{} m m m m m !O{} }{%
  %1 = couleur
  %2 = nom
  %3 = compteur
  %4 = label singulier
  %5 = label pluriel
  %6 = before label
  %7 = options persos tcbox
  %counter
  \newcounter{#3}%création
  \setcounter{#3}{0}%initialisation
  \counterwithin{#3}{section}%
  %testcouleurl3
  \colorlet{cbcoulbord#2}{#1}%
  \colorlet{cbcoulfond#2}{#1!\opaciteboitealt}%
  %next
  \NewDocumentEnvironment{#2}{ O{} D<>{} }%
  {%
    \restoreKV[cpaltboites]%
    \setKV[cpaltboites]{##1}%
    \ifboolKV[cpaltboites]{Pluriel}%
      {\def\tmplabelboite{#5}}%
      {\def\tmplabelboite{#4}}%
    \tcbset{reset}%
    \tcolorbox[basecpcours={#1},#7,##2]%
    \colorlet{cbcoulbord}{cbcoulbord#2}%
    \colorlet{cbcoulfond}{cbcoulfond#2}%
    \ifboolKV[cpaltboites]{SouligneLabel}
      {%
        \renewcommand\ULthickness{1pt}%
        \settodepth\ULdepth{\hbox{pqyj}}%
      }%
      {}%
    \ifboolKV[cpaltboites]{Compteur}%
    {%
      \ifboolKV[cpaltboites]{SouligneLabel}%
        {%
          \textcolor{#1!33!black}%
            {%
              \policetitreboite%
              {#6}%
              {\uline{\stepcounter{#3}{\vphantom{(qPÉ)}\tmplabelboite\cpaltboitescplttitre~\ifnum\value{section}=0\else\arabic{section}.\fi\arabic{#3}}}}\textit{\cpaltboitessoustitre}%
            }%
          \par
        }%
        {%
          \textcolor{#1!33!black}%
            {%
              \policetitreboite%
              \stepcounter{#3}%
              \vphantom{(qPÉ)}{#6}\tmplabelboite\cpaltboitescplttitre~\ifnum\value{section}=0\else\arabic{section}.\fi\arabic{#3}\textit{\cpaltboitessoustitre}%
            }%
          \par
        }%
    }%
    {%
      \ifboolKV[cpaltboites]{SouligneLabel}%
        {%
          \textcolor{#1!33!black}%
            {%
              \policetitreboite%
              {#6}%
              {\uline{{\vphantom{(qPÉ)}\tmplabelboite\cpaltboitescplttitre}}\textit{\cpaltboitessoustitre}}%
            }%
          \par
        }%
        {%
          \textcolor{#1!33!black}%
            {%
              \policetitreboite%
              \vphantom{(qPÉ)}{#6}\tmplabelboite\cpaltboitescplttitre\textit{\cpaltboitessoustitre}%
            }
            %
          \par
        }%
    }%
    \vspace*{0.5mm}%
  }%
  {%
    \endtcolorbox%
  }%
}

\endinput