%%%
% Nonogramme
%%%
\def\filedateNonogramme{2025/12/13}%
\def\fileversionNonogramme{0.1a}%
\message{-- \filedateNonogramme\space v\fileversionNonogramme}%
%
\setKVdefault[Nono]{Largeur=10pt,Taille=5,Unite=1cm,Solution=false,Enonce=true}

\newtoks\toklistenonoa%
\newtoks\toklistenonob%

\def\UpdatetoksNonoA#1\nil{\addtotok\toklistenonoa{"#1",}}%

\makeatletter
\NewDocumentCommand\Nonogramme{o m}{%
  \useKVdefault[Nono]%
  \setKV[Nono]{#1}%
  \setsepchar{,}%
  \readlist*\PfC@ListeNonoa{#2}%
  \toklistenonoa{}%
  \toklistenonob{}%
  \xintFor* ##2 in{\xintSeq{1}{\useKV[Nono]{Taille}}}\do{%
    \xdef\PfC@NonoFoo{}%
    \xintFor* ##1 in{\xintSeq{1}{\PfC@ListeNonoalen}}\do{%
      \StrChar{\PfC@ListeNonoa[##1]}{##2}[\PfC@NonoRetiens]
      \xdef\PfC@NonoFoo{\PfC@NonoFoo\PfC@NonoRetiens}
    }%
    \expandafter\UpdatetoksNonoA\PfC@NonoFoo\nil%
  }%
  \expandafter\toklistenonob\expandafter{\the\toklistenonoa}%
  \toklistenonoa{}%
  \foreachitem\compteur\in\PfC@ListeNonoa{\expandafter\UpdatetoksNonoA\compteur\nil}%
  \PfC@BuildNono{\the\toklistenonoa}{\the\toklistenonob}%
 }%

\NewDocumentCommand\PfC@BuildNono{m m}{%
  \mplibforcehmode
  \begin{mplibcode}
    Taille:=\useKV[Nono]{Taille};
    u:=\useKV[Nono]{Unite};
    Unite=\useKV[Nono]{Unite};
    %
    if u<7mm:
      nu:=7mm;
    else:
      nu:=u;
    fi;
    %
    boolean Solution,Enonce;
    Solution:=\useKV[Nono]{Solution};
    Enonce:=\useKV[Nono]{Enonce};
    
    color NonoFill;
    NonoFill=white;
    
    numeric nbretenir[][];
    numeric nbretenircol[][];
    
    vardef NombreLigne(text t)=
      retiens:=0;
      nblignes:=0;
      for p_=t:
        nblignes:=nblignes+1;
      endfor;
    enddef;

    path Square;
    Square=unitsquare scaled u;
    
    vardef Nonoa(text t)=
      cptretiensnblignes:=1;
      for p_=t:
        for k=0 upto Taille-1:
          if substring(k,k+1) of p_="o":
          elseif substring(k,k+1) of p_="X":
            if Solution:
              fill ((Square scaled 0.95) shifted(u*((k,-cptretiensnblignes)+(0.025,0.025))))
            fi;
          fi;
          trace (Square shifted(u*(k,-cptretiensnblignes)));
        endfor;
        cptretiensnblignes:=cptretiensnblignes+1;
      endfor;
    enddef;

    string Sequence[][];
    string SequenceCol[][];
    
    vardef AffichageL(text t)=
      pair Alg[];
      cptnblignes:=0;
      for p_=t:
        pas:=0;
        cptnblignes:=cptnblignes+1;
        nbcoups:=0;
        for k=0 upto Taille-1:
          if substring(k,k+1) of p_="o":
            pas:=pas+1;
            nbcoups:=0;
          elseif substring(k,k+1) of p_="X":
            nbcoups:=nbcoups+1;
            Sequence[cptnblignes][pas]:=decimal(nbcoups);
          fi;
        endfor;
        nbpas:=0;
        picture Etiquette;
        for k=pas downto 0:
          Alg[k]:=(0,u*(-cptnblignes+0.5));
          if unknown Sequence[cptnblignes][k]:
          else:
            nbpas:=nbpas+1;
            label(TEX(Sequence[cptnblignes][k]),Alg[k]-nbpas*(0.5*nu,0));
          fi;
        endfor;
      endfor;
    enddef;

    vardef AffichageC(text t)=
      cptnbcolonnes:=0;
      for p_=t:
        pas:=0;
        cptnbcolonnes:=cptnbcolonnes+1;
        nbcoups:=0;
        for k=0 upto nblignes-1:
          if substring(k,k+1) of p_="o":
            pas:=pas+1;
            nbcoups:=0;
          elseif substring(k,k+1) of p_="X":
            nbcoups:=nbcoups+1;
            SequenceCol[cptnbcolonnes][pas]:=decimal(nbcoups);
          fi;
        endfor;
        nbpas:=0;
        for k=pas downto 0:
          if unknown SequenceCol[cptnbcolonnes][k]:
          else:
            nbpas:=nbpas+1;
            label.top(TEX(SequenceCol[cptnbcolonnes][k]),(0,0) shifted (u*(cptnbcolonnes-0.5),(nbpas-1)*0.5*nu+0.25*nu));
          fi;
        endfor;
      endfor;
    enddef;
    
    NombreLigne(#1);
    Nonoa(#1);
    if Enonce:
      AffichageL(#1);
      AffichageC(#2);
    fi;
  \end{mplibcode}%
}%
\makeatother