% !TeX root = ./examples/pig.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-pigs}[2020/12/04 version v0.4 Drawing pigs in TikZ]

\RequirePackage{tikz}
\RequirePackage{tikzlings-addons}
\definecolor{pigdark}{RGB}{241,164,142}

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

\newif\ifpig@threeD
\newif\ifpig@back

\tikzset{
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % Pass unknown keys on to tikz
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  /pig/.search also={/tikz,/pgf,/thing},
  /pig/.cd,
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % body
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  body/.code         = \def\pig@body{#1},
  body               = pigdark,
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % options
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3D/.code           = \pig@threeDtrue, 
  back/.code         = \pig@backtrue,  
}

\def\pig@draw{%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% minimal bounding box size 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\path (-0.625,0.1) rectangle (0.905, 2.27);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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 and 3D, from
% https://chat.stackexchange.com/transcript/message/45991801#45991801
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifpig@threeD
  \def\pig@part@draw[##1]{\shade[ball color=##1]}
\else
  \def\pig@part@draw[##1]{\fill[##1]}
\fi
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Body parts (center: x=0)
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\csname pighookbackground\endcsname
\csname tikzlinghookbackground\endcsname
%
% Tail %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifpig@back\else
  \draw[\pig@body,line width=\scalingfactor*0.8pt] (0.8431, 0.583) .. controls (0.8609, 0.4063) and (0.7006, 0.361) .. (0.6384, 0.4293) .. controls (0.5762, 0.4976) and (0.5613, 0.6052) .. (0.6551, 0.629) .. controls (0.7537, 0.6541) and (0.7759, 0.5628) .. (0.7455, 0.4948) .. controls (0.6412, 0.2697) and (0.3543, 0.356) .. (0.2638, 0.4238) .. controls (0.1967, 0.4779) and (0.1788, 0.6076) .. (0.3192, 0.633) .. controls (0.6000, 0.6836) and (0.5130, 0.3048) .. (0.2098, 0.3322) .. controls (0.0809, 0.3438) and (0.0809, 0.5236) .. (0.0809, 0.5236);
\fi
%
% Feet if back %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifpig@back
  \pig@part@draw[\pig@body] (0.3216,0.15) arc (-15:195:0.12) -- (0.16,0.15) to [bend left] (0.25,0.15) -- cycle;
  \pig@part@draw[\pig@body] (-0.3216,0.15) arc (195:-15:0.12) -- (-0.16,0.15) to[bend right] (-0.25,0.15) --cycle;
\fi%
% Body %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pig@part@draw[\pig@body!55!white] ( 0.6,  0.8 ) .. controls ( 0.6 , 0.4) and ( 0.35, 0.16) .. ( 0.0,  0.16) .. controls (-0.35, 0.16) and (-0.6 , 0.4) .. (-0.6,  0.8 ) .. controls (-0.6 , 1.3) and (-0.35, 1.5) .. ( 0.0,  1.5) .. controls ( 0.35, 1.5) and ( 0.6 , 1.3) .. ( 0.6,  0.75) -- cycle;
%
% adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\csname pighookbelly\endcsname
\csname tikzlinghookbelly\endcsname
%
% Arms %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifpig@back\else
  \pig@part@draw[\pig@body] (-0.2402, 0.9823) .. controls (-0.2994, 1.0582) and (-0.3721, 1.0651) .. (-0.4024, 0.9978) .. controls (-0.4327, 0.9305) and (-0.4092, 0.8145) .. (-0.35, 0.7386) .. controls (-0.3311, 0.7145) and (-0.3101, 0.6964) .. (-0.289, 0.6862) .. controls (-0.2755, 0.7023) and (-0.2498, 0.7563) .. (-0.2498, 0.7563) .. controls (-0.2498, 0.7563) and (-0.1790, 0.731) .. (-0.1611, 0.746) .. controls (-0.1611, 0.746) and (-0.1809, 0.9064) .. (-0.2402, 0.9823) -- cycle;
  \pig@part@draw[\pig@body] (0.2402, 0.9823) .. controls (0.2994, 1.0582) and (0.3721, 1.0651) .. (0.4024, 0.9978) .. controls (0.4327, 0.9305) and (0.4092, 0.8145) .. (0.35, 0.7386) .. controls (0.3311, 0.7145) and (0.3101, 0.6964) .. (0.289, 0.6862) .. controls (0.2755, 0.7023) and (0.2498, 0.7563) .. (0.2498, 0.7563) .. controls (0.2498, 0.7563) and (0.1790, 0.731) .. (0.1611, 0.746) .. controls (0.1611, 0.746) and (0.1809, 0.9064) .. (0.2402, 0.9823) -- cycle;
\fi
%
% Head %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pig@part@draw[\pig@body!55!white] (0,1.64) ellipse[x radius=.5, y radius=.5];
%
% Show details only from the front %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifpig@back\else
%
% Eyes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pig@part@draw[gray] (-0.09, 1.82) circle[radius=0.02];
\pig@part@draw[gray] ( 0.09, 1.82) circle[radius=0.02];
%
% Nose %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pig@part@draw[\pig@body]  (0, 1.62) ellipse[x radius=0.18, y radius=0.14];
\pig@part@draw[gray]  (-0.06, 1.62) circle[radius=0.035];
\pig@part@draw[gray]  ( 0.06, 1.62) circle[radius=0.035];
%
% Mouth %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifpig@threeD
  \draw[gray!70!black,line width=\scalingfactor*0.4pt] (0.105, 1.46) arc [start angle=-50, end angle=-130, radius=0.16] ;
\else
  \draw[gray,line width=\scalingfactor*0.4pt] (0.105, 1.46) arc [start angle=-50, end angle=-130, radius=0.16] ;
\fi
%
% Feet %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pig@part@draw[\pig@body] (0.3216,0.15) arc (-15:195:0.12) -- (0.16,0.15) to [bend left] (0.25,0.15) -- cycle;
\pig@part@draw[\pig@body] (-0.3216,0.15) arc (195:-15:0.12) -- (-0.16,0.15) to[bend right] (-0.25,0.15) --cycle;
%
\fi
%
% Ears %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pig@part@draw[\pig@body] (0.22, 2.0392) .. controls   (0.22, 2.1584) and   (0.49460, 2.255) ..   (0.49460, 2.255) .. controls   (0.49460, 2.255) and (0.5300, 1.86) .. (0.3721, 1.86) .. controls (0.2891, 1.86) and   (0.22, 1.9201) .. (0.22, 2.0392) -- cycle;
\pig@part@draw[\pig@body!55!white] (0.22, 2.0392) .. controls (0.26, 2.1584) and (0.47, 2.22) .. (0.47, 2.20) .. controls (0.47, 2.2) and (0.49, 1.9) .. (0.3721, 1.9) .. controls (0.2891, 1.88) and (0.22, 1.88) .. (0.22, 2.0392) -- cycle;  
\pig@part@draw[\pig@body] (-0.22, 2.0392) .. controls   (-0.22, 2.1584) and   (-0.49460, 2.255) ..   (-0.49460, 2.255) .. controls   (-0.49460, 2.255) and (-0.5300, 1.86) .. (-0.3721, 1.86) .. controls (-0.2891, 1.86) and   (-0.22, 1.9201) .. (-0.22, 2.0392) -- cycle;
\pig@part@draw[\pig@body!55!white] (-0.22, 2.0392) .. controls (-0.26, 2.1584) and (-0.47, 2.22) .. (-0.47, 2.20) .. controls (-0.47, 2.2) and (-0.49, 1.9) .. (-0.3721, 1.9) .. controls (-0.2891, 1.88) and (-0.22, 1.88) .. (-0.22, 2.0392) -- cycle;  
%  
% Tail  if back %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifpig@back
  \draw[\pig@body,line width=\scalingfactor*0.8pt] (0.8431, 0.583) .. controls (0.8609, 0.4063) and (0.7006, 0.361) .. (0.6384, 0.4293) .. controls (0.5762, 0.4976) and (0.5613, 0.6052) .. (0.6551, 0.629) .. controls (0.7537, 0.6541) and (0.7759, 0.5628) .. (0.7455, 0.4948) .. controls (0.6412, 0.2697) and (0.3543, 0.356) .. (0.2638, 0.4238) .. controls (0.1967, 0.4779) and (0.1788, 0.6076) .. (0.3192, 0.633) .. controls (0.6000, 0.6836) and (0.5130, 0.3048) .. (0.2098, 0.3322) .. controls (0.0809, 0.3438) and (0.0809, 0.5236) .. (0.0809, 0.5236);
\fi
% adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\csname pighookbody\endcsname
\csname tikzlinghookbody\endcsname
%
}
