% Clément PAGÈS <contact@clementpages.fr>
%
% Version 0.2.1
%
% Draws basic playcards with Tikz. Inspired by https://tex.stackexchange.com/questions/47924/creating-playing-cards-using-tikz
%
% Provides cards for a4paper format
%
% Licence Gnu Lesser General Public Licence https://www.gnu.org/licenses/lgpl-3.0.html



\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{playcards}[2023/06/11, Provides commands to draw playcards]

\RequirePackage{simplekv}
\RequirePackage{graphicx}
\RequirePackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{shadows}
\usepgflibrary {shadings}

% Check if attor is given a optional parameter
\newif\if@loadanttorfont\@loadanttorfontfalse
\DeclareOption{anttor}{\@loadanttorfonttrue}
\ProcessOptions\relax


\RequirePackage[outline]{contour}

\pgfmathsetmacro{\cardwidth}{59}
\pgfmathsetmacro{\cardheight}{89}
\pgfmathsetmacro{\cardmargin}{4}
\pgfmathsetmacro{\cardwidthbody}{\cardwidth-\cardmargin}
\pgfmathsetmacro{\cardheightbody}{\cardheight-\cardmargin}
\pgfmathsetmacro{\stripwidth}{10}
\pgfmathsetmacro{\strippadding}{2}


\def\shapeCard{(0,0) rectangle (\cardwidth,\cardheight)}
\def\shapeCardBody{(0,0) rectangle (\cardwidthbody,\cardheightbody)}
\def\shapeMargin{(\cardmargin,\cardmargin) rectangle (\cardwidthbody,\cardheightbody)}



\newcommand{\carddebug}{
    \draw [step=1,help lines] (0,0) grid (\cardwidth,\cardheight);
}

\newcommand{\cardborder}{
    \draw[lightgray,cardcorners] \shapeCard;
    \draw[black,cardcorners,line width=1pt] \shapeMargin;
}

\newcommand{\cardpadding}{
    \draw[white,cardcorners] {(0,0) rectangle (\cardwidth+2,\cardheight+2)};
}


\setKVdefault[RectoKeys]{borders=true, trame=true, corners=true, backgroundImg=true, textColor=black, backgroundColor=red, contentsFontSize=120, circleRay=20}
\setKVdefault[VersoKeys]{backgroundImg=true, trame=true, contentsFontSize=50}



\newcommand{\cardbackgroundverso}[2][]{
    \pgfmathsetmacro{\textsize}{\useKV[VersoKeys]{contentsFontSize}}
    \pgfmathsetmacro{\textlineskip}{1.2*\textsize}
	\clip[cardcorners] \shapeCard;
    \ifboolKV[VersoKeys]{backgroundImg} {%
		\begin{scope} % Draw background
			\draw[shading=color wheel,opacity=0.6] (0,0) rectangle (\cardwidth,\cardheight);
		\end{scope}
	}{%
		\draw[cardcorners, line width=1pt] \shapeCard;%
	}%
    \begin{scope}[shift={(0.5*\cardwidth,0.5*\cardheight)}] % White text
        \node[rotate=45, color=white] at (0,0) {\contourlength{1pt}\contour{black!}{\fontsize{\textsize}{\textlineskip pt}\selectfont #2}};
    \end{scope}
}


\newcommand{\cardcontent}[2][]{
    \pgfmathsetmacro{\textsize}{\useKV[RectoKeys]{contentsFontSize}}
    \pgfmathsetmacro{\textlineskip}{1.2*\textsize}
    \showKV[RectoKeys]{backgroundImg}
    \showKV[RectoKeys]{backgroundColor}
    \begin{scope}
		\ifboolKV[RectoKeys]{borders} {
			\clip[cardcorners] \shapeMargin;
		}{
			\clip[cardcorners] \shapeCard;
		}
		\ifboolKV[RectoKeys]{backgroundImg} {
			\shade[xshift=29.5mm, yshift=44.5mm, inner color={\useKV[RectoKeys]{backgroundColor}}, outer color=white] circle (\cardwidth) circle({\useKV[RectoKeys]{circleRay}});
		}
		{}
		\draw[color=white] (0,0) -- (60,70);
		\draw[color=white] (0,20) -- (60,15);
		\draw[color=white] (0,24) -- (60,30);
		\draw[color=white] (0,37) -- (60,36);
		\draw[color=white] (0,40) -- (60,60);
		\draw[color=white] (0,47) -- (60,5);
		\draw[color=white] (0,55) -- (60,45);
		\draw[color=white] (0,70) -- (40,0);
		\draw[color=white] (0,76) -- (60,65);
		\draw[color=white] (0,80) -- (25,0);
		\draw[color=white] (20,80) -- (15,0);
		\draw[color=white] (35,90) -- (0,80);
		\draw[color=white] (40,90) -- (60,-20);
		\draw[color=white] (0,5) -- (60,90);
		\draw[xshift=29.5mm, yshift=44.5mm, fill=white, color=white] circle(({\useKV[RectoKeys]{circleRay}});
	\end{scope}
	\node[align=center, text width=5 cm] at (0.5*\cardwidth,0.5*\cardheight) {{\color{\useKV[RectoKeys]{textColor}}\fontsize{\textsize}{\textlineskip}\selectfont #2}};
}

\newcommand{\cardcorner}[1]{
	\begin{scope}[shift={(13,76)}]
		\draw[rotate=30, fill=white, color=white] (0,0) ellipse (8 and 5) node[color=black, rotate=30]{{\color{\useKV[RectoKeys]{textColor}}{\fontsize{36pt}{43pt}\selectfont #1}}};
	\end{scope}
	\begin{scope}[shift={(\cardwidth-13,\cardheight-76)}]
		\draw[rotate=30, fill=white, color=white] (0,0) ellipse (8 and 5) node[rotate=210, color=black]{{\color{\useKV[RectoKeys]{textColor}}{\fontsize{36pt}{43pt}\selectfont #1}}};
	\end{scope}
}


\newcommand{\drawcardrecto}[2][]{
	\begin{tikzpicture}
		\cardborder
		\cardpadding
		\cardcontent{#2}
		\ifboolKV[RectoKeys]{corners}{
			\cardcorner{#2}
		}{
		}
	\end{tikzpicture}%
}


\newcommand{\drawcardverso}[2][]{
	\begin{tikzpicture}%
		\cardpadding
		\cardbackgroundverso{#2}
	\end{tikzpicture}
}


\newcommand{\drawcardsverso}[2][]{
	\tikzset{%
		x=1mm,y=1mm,%
		cardcorners/.style={%
			rounded corners=4%
		},%
	}
	\if@loadanttorfont % Change card font to anttor if parameter antor given
		\fontfamily{antt}
	\fi
    \useKVdefault[VersoKeys]%
    \setKV[VersoKeys]{#1}%
    \ifboolKV[VersoKeys]{trame}{%
		\foreach \k in {0,...,2}{%
			\foreach \n in {0,...,2}{%
				\noindent\drawcardverso{#2}%
			}\\%
		}%
	}{%
		\noindent\drawcardverso{#2}%
	}%
}

\newcommand{\drawcardsrecto}[2][] {
	\tikzset{%
		x=1mm,y=1mm,%
		cardcorners/.style={%
			rounded corners=4%
		},%
	}
	\if@loadanttorfont % Change card font to anttor if parameter antor given
		\fontfamily{antt}
	\fi
	\useKVdefault[RectoKeys]%
	\setKV[RectoKeys]{#1}%
	\ifboolKV[RectoKeys]{trame}{
		\foreach \k in {0,...,2}{%
			\foreach \n in {0,...,2}{%
				\noindent\drawcardrecto{#2}%
			}\\%
		}%
	}{
		\noindent\drawcardrecto{#2}%
	}%
}

.
