% !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 http://www.latex-project.org/lppl.txt
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ProvidesPackage{tikzlings-snowmen}[2021/04/16 version v0.6 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

\tikzset{
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % Pass unknown keys on to tikz
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  /snowman/.search also={/tikz,/pgf,/thing},
  /snowman/.cd,
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % body
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  body/.code         = \def\snowman@body{#1},
  body               = white!95!black,
  eye/.code          = \def\snowman@eye{#1},
  eye                = black,
  nose/.code         = \def\snowman@nose{#1},
  nose               = orange,
  mouth/.code        = \def\snowman@mouth{#1},
  mouth              = black,
  buttons/.code      = \def\snowman@buttons{#1},
  buttons            = black, 
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % options
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  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
  \ifsnowman@contour
    \def\snowman@part@draw[##1]{\draw[##1,\snowman@contour,fill=white,line width=\scalingfactor*0.4pt]}
  \else
    \def\snowman@part@draw[##1]{\fill[##1]}
  \fi
\fi
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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
    \fill[\snowman@globalwarming] (0,1.15) ellipse[x radius=1.6,y radius=1.15];
  \fi
  %
  % 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
  %
  % 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];
  %
  % 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\fi
% adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\csname snowmanhookbody\endcsname
\csname tikzlinghookbody\endcsname
%
}
