%%%
% Papiers
%%%
\setKVdefault[Papiers]{Cinq=true,Seyes=false,Echelle=8,Isometrique=false,IsometriquePointe=false,Millimetre=false,Triangle=false,Largeur=5,Hauteur=4,Couleur=black,Grille=-1,GrillePointe=-1,PageEntiere=false,ZoneTexte=false,Baseline=false,CodeAfter=false}%
\defKV[Papiers]{Traces=\setKV[Papiers]{CodeAfter}}

%\def\MPBaseLineSkip#1#2#3{%à retravailler : ne fonctionne pas :(
%  %
%  \ifluatex
%  \mplibforcehmode
%  \begin{mplibcode}
%    path horizon,verticon;
%    horizon=(0,0)--(#1*cm,0);
%    %verticon=(0,0)--(0,#2*cm);
%    drawoptions(withcolor #3);
%    %for k=0 step 0.5 until #1:
%    %draw verticon shifted((k*cm,0));
%    %endfor;
%    for k=(#2-(\mpdim{1.6ex}/1cm)) step (-\mpdim{\baselineskip}/1cm) until 0:
%    draw horizon shifted((0,k*cm));
%    endfor;
%    drawoptions(withcolor blue);
%    for k=#2 step (-\mpdim{\baselineskip}/1cm) until 0:
%    draw horizon shifted((0,k*cm));
%    endfor;
%  \end{mplibcode}
%  \fi
%}

\def\MPGrille#1#2#3#4{%
  \ifluatex%
  \mplibforcehmode%
  \begin{mplibcode}%
    path horizon,verticon;
    horizon=(0,0)--(#1*cm,0);
    verticon=(0,0)--(0,#2*cm);
    drawoptions(withcolor #3);
    for k=0 step (#4*100) until (#1*100):
    draw verticon shifted(((k/100)*cm,0));
    endfor;
    for k=0 step (#4*100) until (#2*100):
    draw horizon shifted((0,(k/100)*cm));
    endfor;
  \end{mplibcode}%
  \else%
  \begin{mpost}%
    path horizon,verticon;
    horizon=(0,0)--(#1*cm,0);
    verticon=(0,0)--(0,#2*cm);
    drawoptions(withcolor #3);
    for k=0 step 0.5 until #1:
    draw verticon shifted((k*cm,0));
    endfor;
    for k=0 step 0.5 until #2:
    draw horizon shifted((0,k*cm));
    endfor;
  \end{mpost}%
  \fi%
}%

\def\MPGrillePointe#1#2#3#4{%
  \ifluatex%
  \mplibforcehmode%
  \begin{mplibcode}%
    drawoptions(withcolor #3);
    for k=0 step (#4*100) until (#1*100):
    for l=0 step (#4*100) until (#2*100):
    fill (cercles((0,0),1pt) shifted(((k/100)*cm,(l/100)*cm)));
    endfor;
    endfor;
  \end{mplibcode}%
  \else%
  \begin{mpost}%
    drawoptions(withcolor #3);
    for k=0 step (#4*100) until (#1*100):
    for l=0 step (#4*100) until (#2*100):
    fill (cercles((0,0),0.5pt) shifted(((k/100)*cm,(l/100)*cm)));
    endfor;
    endfor;
  \end{mpost}%
  \fi%
}%

\def\MPCinq#1#2#3{%
  \ifluatex%
  \mplibforcehmode%
  \begin{mplibcode}%
    path horizon,verticon;
    horizon=(0,0)--(#1*cm,0);
    verticon=(0,0)--(0,#2*cm);
    drawoptions(withcolor #3);
    for k=0 step 0.5 until #1:
    draw verticon shifted((k*cm,0));
    endfor;
    for k=0 step 0.5 until #2:
    draw horizon shifted((0,k*cm));
    endfor;
    if \useKV[Papiers]{CodeAfter}:
    \useKV[Papiers]{Traces};
    fi;
  \end{mplibcode}%
  \else%
  \begin{mpost}
    path horizon,verticon;
    horizon=(0,0)--(#1*cm,0);
    verticon=(0,0)--(0,#2*cm);
    drawoptions(withcolor #3);
    for k=0 step 0.5 until #1:
    draw verticon shifted((k*cm,0));
    endfor;
    for k=0 step 0.5 until #2:
    draw horizon shifted((0,k*cm));
    endfor;
  \end{mpost}%
  \fi%
}%

\def\MPSeyes#1#2#3{%
  \ifluatex%
  \mplibforcehmode%
  \begin{mplibcode}%
    u:=\useKV[Papiers]{Echelle};
    path horizon,verticon;
    horizon=(0,0)--(#1*cm,0);
    verticon=(0,0)--(0,#2*cm);
    drawoptions(withcolor #3);
    for k=0 step u until (#1*10):
    draw verticon shifted(((k/10)*cm,0));
    endfor;
    for k=0 step (u/4) until (#2*10):
    draw horizon shifted((0,(k/10)*cm)) withpen pencircle scaled 0.5;
    endfor;
    for k=0 step u until (#2*10):
    draw horizon shifted((0,(k/10)*cm)) withpen pencircle scaled 1.25;
    endfor;
  \end{mplibcode}%
  \else%
  \begin{mpost}[mpsettings={u:=\useKV[Papiers]{Echelle};}]%
    path horizon,verticon;
    horizon=(0,0)--(#1*cm,0);
    verticon=(0,0)--(0,#2*cm);
    drawoptions(withcolor #3);
    for k=0 step u until (#1*10):
    draw verticon shifted(((k/10)*cm,0));
    endfor;
    for k=0 step (u/4) until (#2*10):
    draw horizon shifted((0,(k/10)*cm)) withpen pencircle scaled 0.5;
    endfor;
    for k=0 step u until (#2*10):
    draw horizon shifted((0,(k/10)*cm)) withpen pencircle scaled 1.25;
    endfor;
  \end{mpost}
  \fi%
}%

\def\MPMillimetre#1#2#3{%
  \ifluatex%
  \mplibforcehmode%
  \begin{mplibcode}%
    path horizon,verticon;
    horizon=(0,0)--(#1*cm,0);
    verticon=(0,0)--(0,#2*cm);
    drawoptions(withcolor #3);
    for k=0 step 1 until (#1*10):
    draw verticon shifted(((k/10)*cm,0)) withpen pencircle scaled 0.2;
    endfor;
    for k=0 step 5 until (#1*10):
    draw verticon shifted(((k/10)*cm,0)) withpen pencircle scaled 0.5;
    endfor;
    for k=0 step 1 until (#1):
    draw verticon shifted((k*cm,0)) withpen pencircle scaled 1.25;
    endfor;
    for k=0 step 1 until (#2*10):
    draw horizon shifted((0,(k/10)*cm)) withpen pencircle scaled 0.2;
    endfor;
    for k=0 step 5 until (#2*10):
    draw horizon shifted((0,(k/10)*cm)) withpen pencircle scaled 0.5;
    endfor;
    for k=0 step 1 until (#2):
    draw horizon shifted((0,k*cm)) withpen pencircle scaled 1.25;
    endfor;
  \end{mplibcode}%
  \else%
  \begin{mpost}%
    path horizon,verticon;
    horizon=(0,0)--(#1*cm,0);
    verticon=(0,0)--(0,#2*cm);
    drawoptions(withcolor #3);
    for k=0 step 1 until (#1*10):
    draw verticon shifted(((k/10)*cm,0)) withpen pencircle scaled 0.2;
    endfor;
    for k=0 step 5 until (#1*10):
    draw verticon shifted(((k/10)*cm,0)) withpen pencircle scaled 0.5;
    endfor;
    for k=0 step 1 until (#1):
    draw verticon shifted((k*cm,0)) withpen pencircle scaled 1.25;
    endfor;
    for k=0 step 1 until (#2*10):
    draw horizon shifted((0,(k/10)*cm)) withpen pencircle scaled 0.2;
    endfor;
    for k=0 step 5 until (#2*10):
    draw horizon shifted((0,(k/10)*cm)) withpen pencircle scaled 0.5;
    endfor;
    for k=0 step 1 until (#2):
    draw horizon shifted((0,k*cm)) withpen pencircle scaled 1.25;
    endfor;
  \end{mpost}%
  \fi%
}%

\def\MPIsometrique#1#2#3{%
  \ifluatex%
  \mplibforcehmode%
  \begin{mplibcode}%
    path diagon,antidiagon;
    diagon=(0,0)--#2*(sqrt(3)*cm,1*cm);
    antidiagon=(0,0)--#2*(-sqrt(3)*cm,1*cm);
    drawoptions(withcolor #3);
    for k=0 step 1 until (#1):
    draw diagon shifted((k*cm,0));
    draw antidiagon shifted((k*cm,0));
    endfor;
    for k=0 step (sqrt(3)/3) until (#2):
    draw diagon shifted((0,k*cm));
    draw antidiagon shifted((#1*cm,k*cm));
    endfor;
    clip currentpicture to polygone((0,0),(#1*cm,0),(#1*cm,#2*cm),(0,#2*cm));
  \end{mplibcode}%
  \else%
  \begin{mpost}%
    path diagon,antidiagon;
    diagon=(0,0)--#2*(sqrt(3)*cm,1*cm);
    antidiagon=(0,0)--#2*(-sqrt(3)*cm,1*cm);
    drawoptions(withcolor #3);
    for k=0 step 1 until (#1):
    draw diagon shifted((k*cm,0));
    draw antidiagon shifted((k*cm,0));
    endfor;
    for k=0 step (sqrt(3)/3) until (#2):
    draw diagon shifted((0,k*cm));
    draw antidiagon shifted((#1*cm,k*cm));
    endfor;
    clip currentpicture to polygone((0,0),(#1*cm,0),(#1*cm,#2*cm),(0,#2*cm));
  \end{mpost}%
  \fi%
}%

\def\MPIsometriquePointe#1#2#3{%
  \ifluatex%
  \mplibforcehmode%
  \begin{mplibcode}%
    pair diagon,antidiagon;
    diagon=(1pt,1pt);
    antidiagon=(1pt+0.5cm,1pt+0.5*(sqrt(3)*1cm/3));
    drawoptions(withcolor #3);
    for k=0 step (sqrt(3)/3) until (#2):
    for l=0 step 1 until (#1):
    fill (cercles(diagon,1pt) shifted((l*cm,k*cm)));
    fill (cercles(antidiagon,1pt) shifted((l*cm,k*cm)));
    endfor;
    endfor;
    clip currentpicture to polygone((0,0),(#1*cm+2pt,0),(#1*cm+2pt,#2*cm+1pt),(0,#2*cm+1pt));
  \end{mplibcode}%
  \else%
  \begin{mpost}%
    pair diagon,antidiagon;
    diagon=(1pt,1pt);
    antidiagon=(1pt+0.5cm,1pt+0.5*(sqrt(3)*1cm/3));
    drawoptions(withcolor #3);
    for k=0 step (sqrt(3)/3) until (#2):
    for l=0 step 1 until (#1):
    fill (cercles(diagon,1pt) shifted((l*cm,k*cm)));
    fill (cercles(antidiagon,1pt) shifted((l*cm,k*cm)));
    endfor;
    endfor;
    clip currentpicture to polygone((0,0),(#1*cm+2pt,0),(#1*cm+2pt,#2*cm+1pt),(0,#2*cm+1pt));
  \end{mpost}%
  \fi%
}%

\def\MPTriangulaire#1#2#3{%
  \ifluatex%
  \mplibforcehmode%
  \begin{mplibcode}%
    path horizon,diagon,antidiagon;
    horizon=(0,0)--(#1*cm,0);
    diagon=(0,0)--#2*(sqrt(3)*cm/3,1*cm);
    antidiagon=(0,0)--#2*(-sqrt(3)*cm/3,1*cm);
    drawoptions(withcolor #3);
    for k=0 step 1 until #1:
    draw diagon shifted((k*cm,0));
    endfor;
    for k=0 step (sqrt(3)) until (#2):
    draw diagon shifted((0,k*cm));
    endfor;
    for k=0 step 1 until (#1):
    draw antidiagon shifted((k*cm,0));
    endfor;
    for k=0 step (sqrt(3)) until (#2):
    draw antidiagon shifted((#1*cm,k*cm));
    endfor;
    for k=0 step (sqrt(3)/2) until (#2):
    draw horizon shifted((0,k*cm));
    endfor;
    clip currentpicture to polygone((0,0),(#1*cm,0),(#1*cm,#2*cm),(0,#2*cm));
  \end{mplibcode}%
  \else%
  \begin{mpost}%
    path horizon,diagon,antidiagon;
    horizon=(0,0)--(#1*cm,0);
    diagon=(0,0)--#2*(sqrt(3)*cm/3,1*cm);
    antidiagon=(0,0)--#2*(-sqrt(3)*cm/3,1*cm);
    drawoptions(withcolor #3);
    for k=0 step 1 until #1:
    draw diagon shifted((k*cm,0));
    endfor;
    for k=0 step (sqrt(3)) until (#2):
    draw diagon shifted((0,k*cm));
    endfor;
    for k=0 step 1 until (#1):
    draw antidiagon shifted((k*cm,0));
    endfor;
    for k=0 step (sqrt(3)) until (#2):
    draw antidiagon shifted((#1*cm,k*cm));
    endfor;
    for k=0 step (sqrt(3)/2) until (#2):
    draw horizon shifted((0,k*cm));
    endfor;
    clip currentpicture to polygone((0,0),(#1*cm,0),(#1*cm,#2*cm),(0,#2*cm));
  \end{mpost}%
  \fi%
}%

\RequirePackage{ifoddpage}

\newlength{\PapierLeftCurrent}

\newcommand\Papiers[1][]{%
  \useKVdefault[Papiers]%
  \setKV[Papiers]{#1}%
  \xdef\PapierLargeur{\useKV[Papiers]{Largeur}}%
  \xdef\PapierHauteur{\useKV[Papiers]{Hauteur}}%
  \xdef\PapierCouleur{\useKV[Papiers]{Couleur}}%
  \xdef\PapierGrille{\useKV[Papiers]{Grille}}%
  \xdef\PapierGrillePointe{\useKV[Papiers]{GrillePointe}}%
  \ifboolKV[Papiers]{ZoneTexte}{%
    \checkoddpage\ifoddpage%
    \setlength{\PapierLeftCurrent}{\oddsidemargin}%
    \else%
    \setlength{\PapierLeftCurrent}{\evensidemargin}%
    \fi%
    \xdef\PapierLeft{\the\dimexpr1in+\PapierLeftCurrent}%
    \xdef\PapierBottom{\fpeval{\paperheight-\textheight-\voffset-\headheight-\topmargin-\headsep-1in}}%
    \xdef\PapierHauteur{\fpeval{\textheight/1cm}}%
    \xdef\PapierLargeur{\fpeval{\textwidth/1cm}}%
    \begin{tikzpicture}[remember picture,overlay]%
      \node[anchor=south west,inner sep=0pt,transform canvas={xshift=\PapierLeft,yshift=\PapierBottom}] at (current page.south west) {%
        \xintifboolexpr{\useKV[Papiers]{GrillePointe}>0}{%
          \MPGrillePointe{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}{\PapierGrillePointe}%
        }{%
          \xintifboolexpr{\useKV[Papiers]{Grille}>0}{%
            \MPGrille{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}{\PapierGrille}%
          }{\ifboolKV[Papiers]{Baseline}{%
              \MPBaseLineSkip{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
            }{%
              \ifboolKV[Papiers]{IsometriquePointe}{%
                \MPIsometriquePointe{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
              }{%
                \ifboolKV[Papiers]{Triangle}{%
                  \MPTriangulaire{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                }{\ifboolKV[Papiers]{Millimetre}{%
                    \MPMillimetre{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                  }{\ifboolKV[Papiers]{Isometrique}{%
                      \MPIsometrique{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                    }{\ifboolKV[Papiers]{Seyes}{%
                        \MPSeyes{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                      }{\MPCinq{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                      }%
                    }%
                  }%
                }%
              }%
            }%
          }%
        }%
      };%
    \end{tikzpicture}%
  }{%
    \ifboolKV[Papiers]{PageEntiere}{%
      \checkoddpage\ifoddpage%
      \setlength{\PapierLeftCurrent}{\oddsidemargin}%
      \else%
      \setlength{\PapierLeftCurrent}{\evensidemargin}%
      \fi%
      \xdef\PapierLeft{\the\dimexpr1in+\PapierLeftCurrent}%
      \xdef\PapierBottom{\fpeval{\paperheight-\textheight-\voffset-\headheight-\topmargin-\headsep-1in}}%
      \xdef\PapierHauteur{\fpeval{\paperheight/1cm}}%
      \xdef\PapierLargeur{\fpeval{\paperwidth/1cm}}%
      \begin{tikzpicture}[remember picture,overlay]%
        \node[anchor=south west,inner sep=0pt] at (current page.south west) {%
          \xintifboolexpr{\useKV[Papiers]{GrillePointe}>0}{%
            \MPGrillePointe{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}{\PapierGrillePointe}%
          }{%
            \xintifboolexpr{\useKV[Papiers]{Grille}>0}{%
              \MPGrille{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}{\PapierGrille}%
            }{\ifboolKV[Papiers]{IsometriquePointe}{%
                \MPIsometriquePointe{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
              }{\ifboolKV[Papiers]{Triangle}{%
                  \MPTriangulaire{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                }{\ifboolKV[Papiers]{Millimetre}{%
                    \MPMillimetre{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                  }{\ifboolKV[Papiers]{Isometrique}{%
                      \MPIsometrique{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                    }{\ifboolKV[Papiers]{Seyes}{%
                        \MPSeyes{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                      }{\MPCinq{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                      }%
                    }%
                  }%
                }%
              }%
            }%
          }%
        };%
      \end{tikzpicture}%
    }{%
      \xintifboolexpr{\useKV[Papiers]{GrillePointe}>0}{%
        \MPGrillePointe{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}{\PapierGrillePointe}%
      }{%
        \xintifboolexpr{\useKV[Papiers]{Grille}>0}{%
          \MPGrille{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}{\PapierGrille}%
        }{\ifboolKV[Papiers]{Baseline}{%
            \MPBaseLineSkip{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
          }{%
            \ifboolKV[Papiers]{IsometriquePointe}{%
              \MPIsometriquePointe{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
            }{%
              \ifboolKV[Papiers]{Triangle}{%
                \MPTriangulaire{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
              }{\ifboolKV[Papiers]{Millimetre}{%
                  \MPMillimetre{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                }{\ifboolKV[Papiers]{Isometrique}{%
                    \MPIsometrique{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                  }{\ifboolKV[Papiers]{Seyes}{%
                      \MPSeyes{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                    }{\MPCinq{\PapierLargeur}{\PapierHauteur}{\PapierCouleur}%
                    }%
                  }%
                }%
              }%
            }%
          }%
        }%
      }%
    }%
  }%
}%