 % !TeX root = ./examples/snowman.tex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% This package is part of the TikZlings package
%% A package to bring cute little animals and other beings into tikz
%% Maintained by samcarter
%%
%% Project repository and bug tracker:
%% https://github.com/samcarter/tikzlings
%%
%% Released under the LaTeX Project Public License v1.3c or later
%% See https://www.latex-project.org/lppl.txt
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ProvidesPackage{tikzlings-snowmen}[2025/01/05 version v2.2 Draw snowmen in TikZ]

\RequirePackage{tikz}
\RequirePackage{tikzlings-addons}

\newcommand*{\snowman}[1][]{%
  \begin{scope}%
    \tikzset{/snowman/.cd,#1}%
    \snowman@draw%
  \end{scope}%
  \thing[#1]%
  % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \csname snowmanhookforeground\endcsname
  \csname tikzlinghookforeground\endcsname
  %
}

\newif\ifsnowman@threeD
\newif\ifsnowman@back
\newif\ifsnowman@globalwarming
\newif\ifsnowman@contour
\newif\ifsnowman@openmouth

\tikzset{
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % Pass unknown keys on to tikz
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  /snowman/.search also={/tikz,/pgf,/thing},
  /snowman/.cd,
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % body
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  body/.store in     = \snowman@body,
  body               = white!95!black,
  eye/.store in      = \snowman@eye,
  eye                = black,
  nose/.store in     = \snowman@nose,
  nose               = orange,
  mouth/.store in    = \snowman@mouth,
  mouth              = black,
  buttons/.store in  = \snowman@buttons,
  buttons            = black,
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % options
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  openmouth/.code    = \snowman@openmouthtrue,
  open mouth/.forward to = /snowman/openmouth,
  3D/.code           = \snowman@threeDtrue,
  back/.code         = \snowman@backtrue,
  globalwarming/.code= \snowman@globalwarmingtrue
                       \def\snowman@globalwarming{#1},
  globalwarming/.default = blue!10!white,
  global warming/.forward to = /snowman/globalwarming,
  contour/.code      = \snowman@contourtrue
                       \def\snowman@contour{#1}
                       \def\snowman@mouth{#1},
  contour/.default   = black,
  outline/.forward to = /snowman/contour,
}

\def\snowman@draw{%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % minimal bounding box size
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \path (-0.75, 0.0) rectangle (0.75, 2.13);
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % store the current scale factor
  % from https://github.com/samcarter/tikzlings/issues/3#issuecomment-461373494
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \pgfgettransformentries{\tmpscaleA}{\tmpscaleB}{\tmpscaleC}{\tmpscaleD}{\tmp}{\tmp}%
  \pgfmathsetmacro{\scalingfactor}{sqrt(abs(\tmpscaleA*\tmpscaleD-\tmpscaleB*\tmpscaleC))*sqrt(abs((\pgf@xx/1cm)*(\pgf@yy/1cm)-(\pgf@xy/1cm)*(\pgf@yx/1cm)))}%
  \pgfmathsetmacro{\xscalefactor}{\tmpscaleA}
  \pgfmathsetmacro{\yscalefactor}{\tmpscaleD}
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % switch between 2D, 3D and contours, from
  % https://chat.stackexchange.com/transcript/message/45991801#45991801
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \ifsnowman@threeD
    \def\snowman@part@draw[##1]{\shade[ball color=##1]}
  \else% 3D
    \ifsnowman@contour
      \def\snowman@part@draw[##1]{\draw[##1,\snowman@contour,fill=white,line width=\scalingfactor*0.4pt]}
    \else% contour
      \def\snowman@part@draw[##1]{\fill[##1]}
    \fi% contour
  \fi% 3D
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % Body parts
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \csname snowmanhookbackground\endcsname
  \csname tikzlinghookbackground\endcsname
  %
  % Global warming %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \ifsnowman@globalwarming
    %
    % Puddle %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \ifsnowman@contour
      \draw[\snowman@contour,fill=white,line width=\scalingfactor*0.4pt] (0,1.15) ellipse[x radius=1.6,y radius=1.15];
    \else% contour
      \fill[\snowman@globalwarming] (0,1.15) ellipse[x radius=1.6,y radius=1.15];
    \fi% contour
    %
    % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \csname snowmanhookbelly\endcsname
    \csname tikzlinghookbelly\endcsname
    %
    % Mouth %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \fill[\snowman@mouth] (0,1.23) circle[radius=0.025];
    \fill[\snowman@mouth] (0.06,1.3) circle[radius=0.025];
    \fill[\snowman@mouth] (-0.06,1.27) circle[radius=0.025];
    \fill[\snowman@mouth] (0.09,1.37) circle[radius=0.025];
    \fill[\snowman@mouth] (-0.18,1.28) circle[radius=0.025];
    \fill[\snowman@mouth] (0.2,1.28) circle[radius=0.025];
    \fill[\snowman@mouth] (-0.25,1.2) circle[radius=0.025];
    %
    % Eyes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \snowman@part@draw[\snowman@eye] (-0.5,1.75) circle[radius=0.06];
    \snowman@part@draw[\snowman@eye] (-0.1,1.55) circle[radius=0.06];
    %
    % Nose %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \snowman@part@draw[\snowman@nose] (-0.6,1.4) -- (-0.8,1.42) -- (-0.6,1.54) (-0.6,1.4) arc[radius=0.07, start angle=-90, end angle=90];
    %
    % Buttons %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \snowman@part@draw[\snowman@buttons] (0.1,0.8) circle[radius=0.06];
    \snowman@part@draw[\snowman@buttons] (-0.15,0.55) circle[radius=0.06];
    \snowman@part@draw[\snowman@buttons] (0,0.25) circle[radius=0.06];
  %
  % normal snowman %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \else% global warming
    %
    % Arms %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \snowman@part@draw[\snowman@body] (0.5,1.1) ellipse[x radius=0.3, y radius=0.12, rotate=-50];
    \snowman@part@draw[\snowman@body] (-0.5,1.1) ellipse[x radius=0.3, y radius=0.12, rotate=50];
    %
    % Body + Head %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \snowman@part@draw[\snowman@body] (0,0.5) ellipse[x radius=0.6, y radius=0.5];
    \snowman@part@draw[\snowman@body] (0,1.1) ellipse[x radius=0.5, y radius=0.4];
    \snowman@part@draw[\snowman@body] (0,1.7) circle[radius=0.4];
    %
    % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \csname snowmanhookbelly\endcsname
    \csname tikzlinghookbelly\endcsname
    %
    % Show details only from the front %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \unless\ifsnowman@back
      %
      % Mouth% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      \fill[\snowman@mouth] (0,1.5) circle[radius=0.016];
      \fill[\snowman@mouth] (0.04,1.507) circle[radius=0.016];
      \fill[\snowman@mouth] (-0.04,1.507) circle[radius=0.016];
      \fill[\snowman@mouth] (0.08,1.522) circle[radius=0.016];
      \fill[\snowman@mouth] (-0.08,1.522) circle[radius=0.016];
      \fill[\snowman@mouth] (0.12,1.543) circle[radius=0.016];
      \fill[\snowman@mouth] (-0.12,1.543) circle[radius=0.016];
      \ifsnowman@openmouth
        \fill[\snowman@mouth] (0,1.57) circle[radius=0.016];
        \fill[\snowman@mouth] (-0.04,1.565) circle[radius=0.016];
        \fill[\snowman@mouth] (-0.08,1.56) circle[radius=0.016];
        \fill[\snowman@mouth] (0.04,1.565) circle[radius=0.016];
        \fill[\snowman@mouth] (0.08,1.56) circle[radius=0.016];
      \fi
      %
      % Eyes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      \snowman@part@draw[\snowman@eye] (-0.155,1.8) circle[radius=0.04];
      \snowman@part@draw[\snowman@eye] (0.155,1.8) circle[radius=0.04];
      %
      % Nose %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      \snowman@part@draw[\snowman@nose] (0,1.72) arc [radius=0.05, start angle=90, end angle=270] (0,1.62) -- (0.15,1.64) -- (0,1.72);
      %
      % Buttons %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      \snowman@part@draw[\snowman@buttons] (0,1.15) circle[radius=0.04];
      \snowman@part@draw[\snowman@buttons] (0,0.95) circle[radius=0.04];
      \snowman@part@draw[\snowman@buttons] (0,0.75) circle[radius=0.04];
      %
    \fi% back
  %
  \fi% global warming
  %
  % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \csname snowmanhookbody\endcsname
  \csname tikzlinghookbody\endcsname
  %
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Documentation
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\clearpage
%\section[Snowman]{Yuki, the snowman}
%
%\emph{Yuki is the transcription of the Japanese word for snow}
%
%\subsection{Package name}
%
%\begin{tcolorbox}[title={Package usage}]
%\begin{samcode}
%\usepackage{tikzlings-snowmen}
%\end{samcode}
%\end{tcolorbox}
%
%\subsection{Basic Usage}
%
%\begin{tcblisting}{title={Basic snowman}}
%\snowman
%\end{tcblisting}
%
%\subsection{Options}
%
%The basic snowman can be modified by changing its colour:
%\begin{tcblisting}{title={Body colour}}
%\snowman[body=blue]
%\end{tcblisting}
%
%In addition to the colour of the body, the colour of various body parts can be adjusted:
%\begin{tcblisting}{title={Eye colour}}
%\snowman[eye=red]
%\end{tcblisting}
%\begin{tcblisting}{title={Nose colour}}
%\snowman[nose=red]
%\end{tcblisting}
%\begin{tcblisting}{title={Mouth colour}}
%\snowman[mouth=red]
%\end{tcblisting}
%\begin{tcblisting}{title={Button colour}}
%\snowman[buttons=red]
%\end{tcblisting}
%
% The snowman can open its mouth:
%\begin{tcblisting}{title={Open mouth}}
%\snowman[openmouth]
%\end{tcblisting}
%
%The biggest enemy of the snowman are raising temperatures:
%\begin{tcblisting}{title={Global warming}}
%\snowman[globalwarming,tophat]
%\end{tcblisting}
%
%To view the snowman from behind:
%\begin{tcblisting}{title={Back view}}
%\snowman[back]
%\end{tcblisting}
%
%The key \saminline|3D| will make the snowman 3-dimensional:
%\begin{tcblisting}{title={3D view}}
%\snowman[3D]
%\end{tcblisting}
%
%And finally the \saminline|contour| key will only draw the outlines:
%\begin{tcblisting}{title={Contours}}
%\snowman[contour=black]
%\end{tcblisting}
