\NeedsTeXFormat{LaTeX2e}

% Le package se présente
\def\fileversion{1.0}%
\def\filedate{2015/04/01}%
\ProvidesPackage{miseenpage}%
	[\filedate\space\fileversion\space miseenpage package]% %
%
%\RequirePackage{cellspace}
\RequirePackage{titlesec}
\RequirePackage{titletoc}
%

\input{/home/frederic/texmf/tex/latex/miseenpage/colors.tex}
\input{/home/frederic/texmf/tex/latex/miseenpage/fonts.tex}

% Sommaire
\newlength\controltoctitle
\setlength{\controltoctitle}{0.25cm}

%%%%%%%%%%%% Définition des styles de page

\colorlet{titrechapcouleur}{Apricot!50!Black}
\colorlet{titrechapcouleurfin}{white}
\colorlet{pagenumber@bg@color}{titrechapcouleur}
\colorlet{shadow@color}{Apricot!20!Black}

\definecolor{chapterexocolor}{RGB}{255,230,179}
\colorlet{chaptertextexocolor}{gray!50!black}
%\colorlet{chapterexocolor}{titrechapcouleur!50!yellow}
\colorlet{chapter@title@color}{gray!50!black}
\colorlet{section@title@color}{gray!50!black}
\colorlet{subsection@title@color}{Apricot!70!Black}
\colorlet{subsubsection@title@color}{Apricot!90!Black}

\newcommand{\chaptertoccolor}{white}
\newcommand{\sectiontoccolor}{section@title@color}
\newcommand{\subsectiontoccolor}{subsection@title@color}
\newcommand{\subsubsectiontoccolor}{subsubsection@title@color}




\fancypagestyle{chapterstart}{% 1ère page des chapitres
\renewcommand{\headrulewidth}{0pt}
	\fancyhf{}%
  	\fancyfoot[RO,RE]{%
  	\vskip0.1pt
  	\begin{tikzpicture}
  	\node[rounded corners=3pt,fill=shadow@color,inner ysep=5pt, inner xsep=2ex,text=shadow@color] (shadow) {\pagenumber@font\thepage};
  	\node[rounded corners=3pt,fill=pagenumber@bg@color,inner ysep=5pt, inner xsep=2ex,text=white] at ($(shadow)+(-2pt,2pt)$) {\pagenumber@font\thepage};
  	\end{tikzpicture}
  	}
}

\fancypagestyle{toc}{% Sommaire
\renewcommand{\headrulewidth}{0pt}
	\fancyhf{}%
  	\fancyfoot{}
}

\fancypagestyle{special}{%
  \fancyhead{} % clear all header fields
  \fancyhead[L]{}
\renewcommand{\headrulewidth}{5pt}

}

\fancypagestyle{main}{% 
	\fancyhf{}%

	% Si besoin recompil pour les videos ces 3 lignes sont à commenter
    \renewcommand{\headrulewidth}{1pt}
	\fancyhead[LO]{\leftmark \rightmark }
	\fancyhead[RE]{\textcolor{chapter@title@color}{Chapitre \thechapter }}

  	\fancyfoot[LO]{% pages impaires
  	\ClearShipoutPicture
	% pour video mettre \vskip0.1pt
  	\vskip -5.1pt  	
  	\begin{tikzpicture}
  	\node[rounded corners=3pt,fill=shadow@color,inner ysep=5pt, inner xsep=2ex,text=shadow@color] (shadow) {\pagenumber@font\thepage};
  	\node[rounded corners=3pt,fill=pagenumber@bg@color,inner ysep=5pt, inner xsep=2ex,text=white] at ($(shadow)+(-2pt,2pt)$) {\pagenumber@font\thepage};
  	\end{tikzpicture}
  	}
  	\fancyfoot[RE]{% pages paires	
  		\ClearShipoutPicture
	% pour video mettre \vskip0.1pt
		 \vskip -5.1pt
  		\begin{tikzpicture}
  		\node[rounded corners=3pt,fill=shadow@color,inner ysep=5pt, inner xsep=2ex,text=shadow@color] (shadow) {\pagenumber@font\thepage};
  		\node[rounded corners=3pt,fill=pagenumber@bg@color,inner ysep=5pt, inner xsep=2ex,text=white] at ($(shadow)+(2pt,2pt)$) {\pagenumber@font\thepage};
  		\end{tikzpicture}
  	}
}

%%%%%%%%%%%% En-tête et pied de page

% pour video mettre à 0pt
\renewcommand{\headrulewidth}{1pt}
\renewcommand{\footrulewidth}{0pt}
\pagestyle{main}

%%%%%%%%%%%% Définitions par défaut
\newcommand{\intro}[1]{\def\chapterintro{#1}}
\newcommand{\introauthor}[1]{\def\chapterintroauthor{#1}}
\introauthor{} % auteur de la citation en début de chapitre
\intro{} % Citation en début de chapitre ; à déclarer avant la commande \chapter{}


%%%%%%%%%%%% Page de présentation

%%%%%%%%%%%% Première page de chaque chapitre

\titleformat{\chapter}
{%
	\thispagestyle{chapterstart}
	\pagecolor{white}
	\bfseries\Huge\raggedleft
	\color{chapter@title@color}
	\chaptertitle@font
}
{%
  \AddToShipoutPicture
  {%
  	\put(\LenToUnit{0mm},\LenToUnit{0mm})
  	{%
	\begin{tikzpicture}
	    \clip (0,0) rectangle+(\paperwidth,-\paperheight);
	    % Rectangle du haut à droite
		\fill[bottom color=chapterdegrade@color,top color=white] (0,0) rectangle+(\paperwidth,-3);
		% Trait en biais dans le rectangle en haut à droite
	    \draw[chapterline@color] (0,3) -- (\paperwidth,-3);
	    % Petit cercle vide dans le rectangle en haut à droite
	    \draw[chapterline@color,very thick,opacity=0.5] (.8\paperwidth,-2) circle (5mm);
	    % Petit cercle rempli dans rectangle haut croit
	    \fill[chaptertopcircle@color] ($(.8\paperwidth,-2)+(8mm,8mm)$) circle (2mm);
	    % Citation éventuelle
	    \ifx\chapterintro\@empty
	    \else
	    	  \node[below left,text=chapterintro@color] (citation) at (21,-0.5) {\og \emph{\chapterintro} \fg{}};
	    	  \ifx\chapterintroauthor\@empty
	    	  \else
	    	     \node[below left,text=chapterintro@color] at (citation.south east) {(\chapterintroauthor)};
	    	  \fi
	    \fi
	    % cercle vide en bas à gauche
	    \fill[chapterbotcircle@color] (1,-27) circle (3cm);
	    \fill[white] (1,-27) circle (2.5cm);
	    % Petit cercle presque centré verticalement
	    \fill[chaptermidcircle@color] (1.2,-17) circle (.7cm);
	    \fill[white] (1.2,-17) circle (0.6cm);
	     % Traits verticaux à gauche
	    \draw[very thick,chapterrule@color] (1cm,0) -- (1cm,-\paperheight);
	    \draw[very thick,chapterrule@color] (1.2cm,0) -- (1.2cm,-\paperheight);
	    % Ellipse en haut à gauche
	    \fill[chapterellipse@bg@color] (0,0) ellipse[x radius=9cm,y radius=5cm];
	    % Ombre de l'ellipse
	    \foreach \rx/\ry/\c in { %
	    8.7/4.7/10,% 
	    8.66/4.66/30,%
	    8.62/4.62/50,%
	    8.58/4.58/70,%
	    8.54/4.54/90%
	    }
	    {
		\fill[fill=black!\c!chapterellipse@bg@color] (0,0) ellipse[x radius=\rx cm,y radius=\ry cm];
	    }
	    % ellipse
	    \fill[chapterellipse@color] (0,0) ellipse[x radius=8.5cm,y radius=4.5cm];    
	    % Dans l'ellipse en haut à gauche ...
	    \clip (0,0) ellipse[x radius=8.5cm,y radius=4.5cm];
	     % Traits verticaux à gauche
	    \draw[very thick,chapterrule@color] (1cm,0) -- (1cm,-\paperheight);
	    \draw[very thick,chapterrule@color] (1.2cm,0) -- (1.2cm,-\paperheight);
	    \fill[chapter@bg@color] (0,-0.8) rectangle+(15,-1.3);
	    \node[text=chaptertitlename@color,right] at (0.1,-1.5) {\huge\chaptertitlename@font\bfseries\chaptertitlename~\thechapter};
  \end{tikzpicture}
	   }
  }
}
{7ex}
{}


\newcommand\placeobjet[1]{
    \pgfmathsetmacro{\A}{random(900,2200)}
    \pgfmathsetmacro{\B}{random(2700,3000)}
    \pgfmathsetmacro{\R}{random(0,60)}
    \pgfmathsetmacro{\T}{random(1,1000)}
    \pgfmathsetmacro{\pileface}{random(0,1)}
\FPtrunc\A{\A}{0}
\FPtrunc\B{\B}{0}
\FPtrunc\R{\R}{0}
\FPtrunc\pileface{\pileface}{0}
\ifthenelse{\equal{\pileface}{0}}{\MULTIPLY{\R}{-1}{\RR}}{\COPY{\R}{\RR}}
	\DIVIDE{\A}{100}{\aa}
	\DIVIDE{\B}{100}{\bb}
	\DIVIDE{\T}{1000}{\temp}
	\ADD{\temp}{0.5}{\taille}
	\uput{0}[0]{\RR}(\aa,\bb){\scalebox{\taille}{\textcolor{white}{$\displaystyle #1$}}}
}

\titleformat{name=\chapter,numberless}[block]
%format
{%
	\thispagestyle{chapterstart}
	\pagecolor{white}
	\bfseries\Huge\raggedleft
	\color{chapter@title@color}
	%\color{red}
	\chaptertitle@font
}
%label
{%
  \AddToShipoutPicture
  {%
  	\put(\LenToUnit{0mm},\LenToUnit{0mm})
  	{%
	  \psset{xunit=1cm,yunit=1cm}
	  \begin{pspicture}(0,0)(10,10)
		\begin{psclip}{\psframe[linestyle=none,linecolor=white](0,27)(22,30)}
	% {espace}[angle]{rotation}(x,y){objet}
		%\psframe[fillstyle=solid, fillcolor=titrechapcouleur, dimen=middle, linestyle=none](0,27)(7,30)
	% {espace}[angle]{rotation}(x,y){objet}
		\uput{0}[0]{20}(9,25){\scalebox{0.4}{\rotatebox{0}{
		  \includegraphics{\cheminimage/fond-chap.jpg}
		}}}
%		\uput{0}[0]{20}(10,28){
%		  \begin{minipage}[t]{15cm}
%\[\langle d\mathit{S}{,}\varphi\rangle=\langle S
%{,}d\varphi \dfrac{1}{2i\pi}\int_{\GC}\dfrac{\partial \varphi}{\partial \overline{z}}\left( z
%\right)\dfrac{dz\wedge d\overline{z}}{z}\]
%car $\mathit{S}$ est défini par la forme différentielle localement intégrable
%$\dfrac{1}{2i\pi}\dfrac{dz}{z}$. Pour $\epsilon >0$, notons $\mathit{B}_{\epsilon}=\left\{ z\in \GC
%|\, |z|< \epsilon
%\right\}$, alors 
%\[\langle
%d\mathit{S}{,}\varphi\rangle=\Lim[\epsilon]{0}{\dfrac{1}{2i\pi}\int_{\GC\backslash\mathit{B}_{\epsilon}}\dfrac{\partial \varphi}{\partial \overline{z}}\left( z
%\right)\dfrac{dz\wedge
%d\overline{z}}{z}}=-\Lim[\epsilon]{0}{\int_{\GC\backslash\mathit{B}_{\epsilon}}}d\left(
%\dfrac{\varphi\left( z \right)}{z}dz
%\right).\]
%En appliquant le Théorème de Stokes, on obtient 
%\[\int_{\partial \mathit{B}_{\epsilon}}\dfrac{\varphi\left( z \right)}{z}dz = \int_{\partial
%\mathit{B}_{\epsilon}}\dfrac{\varphi\left( z \right)-\varphi\left( 0 \right)}{z}dz +\varphi\left(
%0 \right)\int_{\partial \mathit{B}_{\epsilon}}\dfrac{dz}{z}.\]
%Comme la fonction $\varphi$ est de classe $\mathcal{C}^{+\infty}$ donc en particulier de classe
%$\mathcal{C}^1$ la première intégrale du second membre tend vers $0$ quand $\epsilon$ tend vers
%$0$. D'autre part, par la formule de Cauchy on a, pour tout $\epsilon>0$,
%$\dfrac{1}{2i\pi}\int_{\partial \mathit{B}_{\epsilon}}\dfrac{dz}{z}=1$. Par conséquent 
%\[\langle d\mathit{S}{,}\varphi\rangle= \varphi\left( 0 \right)=\langle \left[ 0
%\right]{,}\varphi\rangle.\]
%		\end{minipage}
		%}
		%\psframe[fillstyle=solid,fillcolor=red](10,28)(15,30)
%	\psframe[framearc=0.0,fillstyle=gradient, gradbegin=titrechapcouleur,
%	  gradend=titrechapcouleur,
%		gradmidpoint=1, gradangle=90, dimen=middle,
%	  linestyle=none , opacity=1](0,27)(22,30)
	\psframe[framearc=0.0,fillstyle=solid
	  , fillcolor=titrechapcouleur
		, dimen=middle,
	  linestyle=none , opacity=0.9](0,27)(22,30)
	 %\psline[linewidth=3pt,linecolor=white](9,
	 \uput{0}[0]{0}(1,28.2){\scalebox{1.3}{\chaptertitlename@font \textcolor{white}{\textbf{Chapitre
		\thechapter}}}}
	  \end{psclip}
     \psellipse[fillstyle=solid, fillcolor=white, linewidth=0pt, 
		linecolor=white, linestyle=none](9,27)(2,1)
	 \psframe[fillstyle=solid, fillcolor=white, dimen=middle, linestyle=none](9,26.5)(30,28)
%	  \MesAxes[.6pt,Dx=1,Dy=1]{0}{0}{20}{30}
%	  \psaxes[subticks=4, ticksize=-4pt 4pt]{->}(0,0)(20,35)(-5,-5)
%	  \MaGrille[10,xunit=1,yunit=1]{0}{0}{20}{30}
		%\psplot[plotpoints=100]{9}{22}{x 3.4 add \pCos x -.0384  mul .846 add  mul 29 add }
%	    \multido{\ii=1+1}{30}
%  {%
%	\placeobjet{\overline{A}\wedge B}
%	\placeobjet{\implies}
%	\placeobjet{\iff}
%	\placeobjet{\exists x \in E}
%	\placeobjet{\forall x \in E}
%	\placeobjet{P_{n} \implies P_{n+1}}
%	\placeobjet{g\circ f}
%	\placeobjet{g\left( f\left( x \right) \right)}
%	\placeobjet{\sum\limits_{k=0}^{n}{\dbinom{n}{k}}a^{n-k}b^k }
%}
		%\psframe[fillstyle=solid,fillcolor=yellow](0,0)(10,10)
	  \end{pspicture}
	}
  }
}
%sep
{7ex}
%before
{}


\newcommand\fondpourlesexo{
  \AddToShipoutPicture*
  {%
  	\put(\LenToUnit{0mm},\LenToUnit{0mm})
  	{%
	  \psset{xunit=1cm,yunit=1cm}
	  \begin{pspicture}(0,0)(10,10)
		\begin{psclip}{\psframe[linestyle=none,linecolor=white](-2,27.8)(22,31)}
		\uput{0}[0]{20}(9,25){\scalebox{0.4}{\rotatebox{0}{
		  \includegraphics{\cheminimage/fond-chap.jpg}
		}}}
	\psframe[framearc=0.0,fillstyle=solid
	  , fillcolor=chapterexocolor
		, dimen=middle,
	  linestyle=none , opacity=0.9](-2,27.8)(22,31)
	 \uput{0}[0]{0}(1,28.65){\scalebox{1.3}{\Large\fontspec{URWGroteskT}
	 \textcolor{chaptertextexocolor}{Exercices du chapitre \thechapter}}}
	  \end{psclip}
     \psellipse[fillstyle=solid, fillcolor=white, linewidth=0pt, 
		linecolor=white, linestyle=none](12.46,27.8)(2,.8)
	 \psframe[fillstyle=solid, fillcolor=white, dimen=middle, linestyle=none](12.5,27.3)(30,28.6)
	  \end{pspicture}
	}
  }
}




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%% Tables des matières %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\addto\captionsfrench{\renewcommand{\contentsname}{Sommaire}}

%\setcounter{tocdepth}{2}

\renewcommand*\l@chapter[2]
{%
  \ifnum\c@tocdepth>\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      %\leavevmode \bfseries \chaptertitle@font \large
      \leavevmode \bfseries  \large
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      \def\@linkcolor{\chaptertoccolor}%
      \raisebox{-0.6em}{%
      \begin{tikzpicture}%
      \node[rounded corners=2pt,fill=chapter@title@color,text=white, xslant=0.0] {#1};
      \end{tikzpicture}%
      }
      \color{chapter@title@color}
      \nobreak\
       \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfil\nobreak\hb@xt@\@pnumwidth{\hss
        \def\@linkcolor{chapter@title@color}%
        \color{\chaptertoccolor}#2}\par
      \penalty\@highpenalty
    \endgroup
  \fi
  \medskip
}

% si cette section n'est pas commentée alors tocdepth ne fonctionne pas.

%\def\@dottedtocline#1#2#3#4#5
%{%
%    \vskip \z@ \@plus.2\p@
%    {%
%     \leftskip#2\relax\rightskip\@tocrmarg\parfillskip-\rightskip
%     \parindent #2\relax\@afterindenttrue
%     \interlinepenalty\@M
%     \leavevmode
%     \@tempdima #3\relax
%     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
%     {%
%     	\sectiontitle@font#4
%     	%#4
%     }
%     \nobreak\sectiontitle@font
%     \leaders\hbox{$\m@th
%        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
%        mu$}\hfill
%     \nobreak
%     \hb@xt@\@pnumwidth{\hfil #5}%
%     \par}%
%}
  
\renewcommand*\l@section{%
\color{\sectiontoccolor}
\def\@linkcolor{\sectiontoccolor}
\@dottedtocline{1}{0em}{2em}
}

\renewcommand*\l@subsection{%
\color{\subsectiontoccolor}
\def\@linkcolor{\subsectiontoccolor}
\@dottedtocline{1}{2em}{2em}
}

%\renewcommand*\l@subsubsection{%
%\color{\subsubsectiontoccolor}
%\def\@linkcolor{\subsubsectiontoccolor}
%\@dottedtocline{1}{4em}{2em}
%}

\def\contentsline#1#2#3#4{%
  \ifx\\#4\\%
    \csname l@#1\endcsname{#2}{#3}%
  \else
      \csname l@#1\endcsname{\hyper@linkstart{link}{#4}{#2}\hyper@linkend}{%
        \hyper@linkstart{link}{#4}{#3}\hyper@linkend
      }%
  \fi
}


%%%%%%%%%%%%% TOC

\renewcommand{\contentsname}{Sommaires}
\renewcommand\tableofcontents
{%
	\thispagestyle{toc}
	\AddToShipoutPicture
  	{  	
  		%\put(\LenToUnit{0mm},\LenToUnit{0mm})
%  		{%
%		  \psset{xunit=1cm,yunit=1cm}
%		  \begin{pspicture}(-100,100)(150,150)
%			\psframe[fillstyle=solid,fillcolor=yellow](0,0)(5,5)
%		  \end{pspicture}
%%			\begin{tikzpicture}
%%	    		\clip (0,0) rectangle+(\paperwidth,-\paperheight);
%%	    		\draw[decorate,decoration={text along path,
%%text={|\contentstitle@font\color{contentstitleshadow@color}|\contentsname}}] (1.6,-8.1) .. controls ($(\controltoctitle,-\controltoctitle)+(0.1,-0.1)$) .. (20.1,-2.1);
%%	    		\draw[decorate,decoration={text along path,
%%text={|\contentstitle@font\color{contentstitle@color}|\contentsname}}] (1.5,-8) .. controls (\controltoctitle,-\controltoctitle) .. (20,-2);
%%			\end{tikzpicture}
%		}
	}
	\if@twocolumn
          \@restonecoltrue\onecolumn
        \else
          \@restonecolfalse
        \fi
		\section*{\Huge \sffamily \hspace{0.3cm}\contentsname}
            %\@mkboth{%
             %  \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
        \@starttoc{toc}%
        \if@restonecol\twocolumn\fi
        }



