% =============================================================================
% worldflags.sty - Flags of the World with TikZ
% (c) Wilhelm Haager 2021-04-19
% =============================================================================
\RequirePackage{ifthen}
\RequirePackage{tikz}
\RequirePackage{xcolor}
\RequirePackage{etex}
\RequirePackage{xkeyval}
\usetikzlibrary{calc,shadows,shapes,shapes.symbols,positioning,math}
\definecolor{gridcolor}{rgb}{0.8,0.8,0.8}

% ----------------------------------------------------------------------------
% Default values
% ----------------------------------------------------------------------------
\newdimen\fl@gwidth\fl@gwidth=15mm
\newdimen\fl@glength\fl@glength=0mm
\newdimen\fl@gframe\fl@gframe=0.2mm
\def\fl@gstretch{0}
\def\fr@mecolor{black!60}
\newif\if@rid\@ridfalse
\newif\if@mbl@m\@mbl@mtrue
\newif\ifn@@mbl@m\n@@mbl@mtrue
\def\view@ngle{0}
\def\hang@ngle{0}

% ----------------------------------------------------------------------------
% 2 horizontal stripes
% ----------------------------------------------------------------------------
\def\hstripesII#1#2{% 3 horizontal stripes
  \fill [#1] (0,1/2) rectangle (\flaglength,1);
  \fill [#2] (0,0) rectangle (\flaglength,1/2);}

% ----------------------------------------------------------------------------
% 3 horizontal stripes
% ----------------------------------------------------------------------------
\def\hstripesIII#1#2#3{% 3 horizontal stripes
  \fill [#1] (0,2/3) rectangle (\flaglength,1);
  \fill [#2] (0,1/3) rectangle (\flaglength,2/3);
  \fill [#3] (0,0) rectangle (\flaglength,1/3);}

% ----------------------------------------------------------------------------
% 4 horizontal stripes
% ----------------------------------------------------------------------------
\def\hstripesIV#1#2#3#4{%
  \fill [#1] (0,3/4) rectangle (\flaglength,1);
  \fill [#2] (0,1/2) rectangle (\flaglength,3/4);
  \fill [#3] (0,1/4) rectangle (\flaglength,1/2);
  \fill [#4] (0,0) rectangle (\flaglength,1/4);}

% ----------------------------------------------------------------------------
% 5 horizontal stripes
% ----------------------------------------------------------------------------
\def\hstripesV#1#2#3#4#5{%
  \fill [#1] (0,4/5) rectangle (\flaglength,1);
  \fill [#2] (0,3/5) rectangle (\flaglength,4/5);
  \fill [#3] (0,2/5) rectangle (\flaglength,3/5);
  \fill [#4] (0,1/5) rectangle (\flaglength,2/5);
  \fill [#5] (0,0) rectangle (\flaglength,1/5);}

% ----------------------------------------------------------------------------
% 6 horizontal stripes
% ----------------------------------------------------------------------------
\def\hstripesVI#1#2#3#4#5#6{%
  \fill [#1] (0,5/6) rectangle (\flaglength,1);
  \fill [#2] (0,4/6) rectangle (\flaglength,5/6);
  \fill [#3] (0,3/6) rectangle (\flaglength,4/6);
  \fill [#4] (0,2/6) rectangle (\flaglength,3/6);
  \fill [#5] (0,1/6) rectangle (\flaglength,2/6);
  \fill [#6] (0,0) rectangle (\flaglength,1/6);}

% ----------------------------------------------------------------------------
% 2 vertical stripes
% ----------------------------------------------------------------------------
\def\vstripesII#1#2{% 2 vertical stripes
  \fill [#1] (0,0) rectangle (0.5\flaglength,1);
  \fill [#2] (0.5\flaglength,0) rectangle (\flaglength,1);}

% ----------------------------------------------------------------------------
% 3 vertical stripes
% ----------------------------------------------------------------------------
\def\vstripesIII#1#2#3{% 3 vertical stripes
  \fill [#1] (0,0) rectangle (\flaglength/3,1);
  \fill [#2] (\flaglength/3,0) rectangle (2\flaglength/3,1);
  \fill [#3] (2\flaglength/3,0) rectangle (\flaglength,1);}

% ----------------------------------------------------------------------------
% Vertical bar (width relative to flagWIDTH)
% ----------------------------------------------------------------------------
% #1...color, #2...position, #3...width
\def\vbar#1#2#3{%
  \fill [#1] (#2,0) ++(-#3/2,0)
    rectangle ++(#3,1);}

% ----------------------------------------------------------------------------
% Horizontal bar (width relative to flagwidth)
% ----------------------------------------------------------------------------
% #1...color, #2...position, #3...width
\def\hbar#1#2#3{%
  \fill [#1] (0,#2) ++(0,-#3/2)
    rectangle ++(\flaglength,#3);}

% ----------------------------------------------------------------------------
% Cevron; #1...color, #2...width
% ----------------------------------------------------------------------------
\def\chevron#1#2{%
  \fill [#1] (0,0)--(0,1)--(#2,0.5)--cycle;}

% ----------------------------------------------------------------------------
% n-pointed-star
% ----------------------------------------------------------------------------
% #1...color, #2...points, #3...coordinate, #4...outer radius,
% #5...inner radius, #6...angle
\def\starn#1#2#3#4#5#6{%
  \begin{scope}[shift={#3},rotate=#6]
  \fill [#1] (90:#4) foreach \n in {1,...,#2}
  {--(90-180/#2+\n*360/#2:#5)--(90+\n*360/#2:#4)}--cycle;
  \end{scope}}

% ----------------------------------------------------------------------------
% 5-pointed-star
% ----------------------------------------------------------------------------
% #1...color, #2...coordinate, #3...outer radius, #4...angle
\def\starV#1#2#3#4{\starn{#1}{5}{#2}{#3}{0.381966*#3}{#4}}

% ----------------------------------------------------------------------------
% 6-pointed-star
% ----------------------------------------------------------------------------
% #1...color, #2...coordinate, #3...outer radius, #4...angle
\def\starVI#1#2#3#4{\starn{#1}{6}{#2}{#3}{0.57735*#3}{#4}}

% ----------------------------------------------------------------------------
% Moon
% ----------------------------------------------------------------------------
% #1...color, #2...outer coordinates, #3...outer radius,
% #4 inner coordinates #5...inner radius
\def\moon#1#2#3#4#5{%
  \begin{scope}
    \clip #2 circle (#3);
    \fill [even odd rule,#1] #2 circle (1.01*#3) #4 circle (#5);
  \end{scope}}

% ----------------------------------------------------------------------------
% Natural aspect-ratio of a flag (for internal use)
% ----------------------------------------------------------------------------
\def\AspectRatio#1#2{%
  \ifdim\flaglength=0pt\flaglength\flagwidth\divide\flaglength by #1\multiply\flaglength by #2\fi
  \ifdim\flagwidth=0pt\flagwidth\flaglength\divide\flagwidth by #2\multiply\flagwidth by #1\fi
  \newdimen\sh\sh=-\flagwidth\divide\sh by #1\multiply\sh by #2\advance\sh by\flaglength\sh=\flagstretch\sh
  \pgfmathparse{(\flaglength/\flagwidth*#1/#2-1)*\flagstretch+1}
  \edef\StretchFactor{\pgfmathresult}
  }

% ----------------------------------------------------------------------------
% FrameCode and GridCode
% ----------------------------------------------------------------------------
\def\FrameCode{
   \ifx\framecolor\undefined\global\def\framecolor{\fr@mecolor}\fi
   \ifx\flagframe\undefined\newdimen\flagframe\global\flagframe\fl@gframe\fi
   \ifdim\flagframe>0pt\draw
   [\framecolor,line width=\flagframe] (0,0) rectangle (\flaglength,\flagwidth);\fi}

\def\GridCode{\ifgrid
   \begin{scope}[x=1mm,y=1mm]
   \pgfmathparse{\flaglength*0.351}
   \def\n{\pgfmathresult}
   \foreach \x in {0,1,...,\n} \draw [line width=0.1pt,gridcolor!20] (\x,0)--++(0,\flagwidth);
   \foreach \x in {0,5,...,\n} \draw [line width=0.25pt,gridcolor!60] (\x,0)--++(0,\flagwidth);
   \foreach \x in {0,10,...,\n} \draw [line width=0.5pt,gridcolor] (\x,0)--++(0,\flagwidth);
   \pgfmathparse{\flagwidth*0.351}
   \def\n{\pgfmathresult}
   \foreach \x in {0,1,...,\n} \draw [line width=0.1pt,gridcolor!20] (0,\x)--++(\flaglength,0);
   \foreach \x in {0,5,...,\n} \draw [line width=0.25pt,gridcolor!60] (0,\x)--++(\flaglength,0);
   \foreach \x in {0,10,...,\n} \draw [line width=0.5pt,gridcolor] (0,\x)--++(\flaglength,0);
   \end{scope}
   \fi}

% ----------------------------------------------------------------------------
% Begin and end of flag description (just initiates and ends zwo scopes)
% ----------------------------------------------------------------------------
\def\BeginFlagDescription#1#2{%
    \AspectRatio{#1}{#2}
    \begin{scope}[x=\flagwidth,y=\flagwidth,cm={cos(\viewangle)*cos(\hangangle),
        -sin(\hangangle),0,1,(0,0)}]
    \begin{scope}[shift={(-0.5\flaglength,-0.5\flagwidth)}]
    \coordinate (-0) at (0.5\flaglength,0.5\flagwidth);
    \coordinate (-nw) at (0,\flagwidth);
    \coordinate (-w) at (0,0.5\flagwidth);
    \coordinate (-sw) at (0,0);
    \coordinate (-n) at (0.5\flaglength,\flagwidth);
    \coordinate (-s) at (0.5\flaglength,0);
    \coordinate (-ne) at (\flaglength,\flagwidth);
    \coordinate (-e) at (\flaglength,0.5\flagwidth);
    \coordinate (-se) at (\flaglength,0);}
\def\EndFlagDescription{\GridCode\end{scope}\end{scope}}

% ----------------------------------------------------------------------------
% Union Jack (neuer Versuch, 12.4.2021)
% ----------------------------------------------------------------------------
\def\UnionJack#1#2#3#4{%
  \begin{scope}[cm={1,0,0,1,(#1,#2)}]
  \newdimen\b\b=#3\advance\b by -#1
  \newdimen\h\h=#4\advance\h by -#2
  \newdimen\lwb\lwb=\h\divide \lwb by 3
  \newdimen\lws\lws=\h\divide \lws by 5
  \pgfmathparse{atan2(\h,\b)}
  \edef\winkel{\pgfmathresult}
  \clip (0,0) rectangle (\b,\h);
  \fill [blue] (0,0) rectangle (\b,\h);
  \draw [white,line width=\lws] (0,\h)--(\b,0) (0,0)--(\b,\h);
  \begin{scope}[rotate around={\winkel:(\b/2,\h/2)}]
  \draw [red,line width=\lws/3,yshift=\lws/6] (0.5\b,0.5\h)--++(0:0.701\b);
  \draw [red,line width=\lws/3,yshift=-\lws/6] (0.5\b,0.5\h)--++(180:0.701\b);
  \end{scope}
  \begin{scope}[rotate around={-\winkel:(\b/2,\h/2)}]
  \draw [red,line width=\lws/3,yshift=\lws/6] (0.5\b,0.5\h)--++(0:0.701\b);
  \draw [red,line width=\lws/3,yshift=-\lws/6] (0.5\b,0.5\h)--++(180:0.701\b);
  \end{scope}
  \draw [white,line width=\lwb] (-0.5\b,\h/2)--(1.5\b,\h/2) (\b/2,-\h)--(\b/2,2\h);
  \draw [red,line width=\lws] (-0.5\b,\h/2)--(1.5\b,\h/2) (\b/2,-\h)--(\b/2,2\h);
  \end{scope}}

% ----------------------------------------------------------------------------
% Key Management
% ----------------------------------------------------------------------------
\define@key{worldflag}{length}{\flaglength#1}
\define@key{worldflag}{width}{\flagwidth#1}
\define@key{worldflag}{framewidth}{\flagframe#1}
\define@key{worldflag}{framecolor}{\def\framecolor{#1}}
\define@key{worldflag}{emblem}[true]{\emblemtrue\noemblemfalse}
\define@key{worldflag}{noemblem}[true]{\noemblemtrue\emblemfalse}
\define@key{worldflag}{emblemdefault}[true]{\noemblemtrue\emblemtrue}
\define@key{worldflag}{grid}[true]{\gridtrue}
\define@key{worldflag}{stretch}[1]{\def\flagstretch{#1}}
\define@key{worldflag}{turn}[1]{\def\viewangle{#1}}
\define@key{worldflag}{hang}[1]{\def\hangangle{#1}}

\define@key{flagsdefault}{length}{\fl@glength#1}
\define@key{flagsdefault}{width}{\fl@gwidth#1}
\define@key{flagsdefault}{framewidth}{\fl@gframe#1}
\define@key{flagsdefault}{framecolor}{\def\fr@mecolor{#1}}
\define@key{flagsdefault}{emblem}[true]{\@mbl@mtrue\n@@mbl@mfalse}
\define@key{flagsdefault}{noemblem}[true]{\n@@mbl@mtrue\@mbl@mfalse}
\define@key{flagsdefault}{emblemdefault}[true]{\n@@mbl@mtrue\@mbl@mtrue}
\define@key{flagsdefault}{grid}[true]{\@ridtrue}
\define@key{flagsdefault}{stretch}[1]{\def\fl@gstretch{#1}}
\define@key{flagsdefault}{turn}[1]{\def\view@ngle{#1}}
\define@key{flagsdefault}{hang}[1]{\def\hang@ngle{#1}}

% ----------------------------------------------------------------------------
% flagsdefault - setting default values
% ----------------------------------------------------------------------------
\def\flagsdefault{\@ifnextchar[\@flagsdefault{\@flagsdefault[]}}
\def\@flagsdefault[#1]{\setkeys{flagsdefault}{#1}}

% ----------------------------------------------------------------------------
% worldflag (as tikzpicture)
% ----------------------------------------------------------------------------
\def\worldflag{\@ifnextchar[\@worldflag{\@worldflag[]}}%
  \def\@worldflag[#1]#2{%
  \def\framecolor{\fr@mecolor}%
  \newdimen\flagwidth\flagwidth=\fl@gwidth
  \newdimen\flaglength\flaglength=\fl@glength
  \newdimen\flagframe\flagframe=\fl@gframe
  \def\flagstretch{\fl@gstretch}%
  \newif\ifgrid\if@rid\gridtrue\else\gridfalse\fi
  \newif\ifemblem\if@mbl@m\emblemtrue\else\emblemfalse\fi
  \newif\ifnoemblem\ifn@@mbl@m\noemblemtrue\else\noemblemfalse\fi
  \def\viewangle{0}%
  \def\hangangle{0}%
  \setkeys{worldflag}{#1}%
  \IfFileExists{worldflag_#2.tex}{%
     \begin{tikzpicture}
     \input{worldflag_#2.tex}
     \end{tikzpicture}}{\relax}
  \global\let\flagwidth\undefined
  \global\let\flaglength\undefined
  \global\let\flagframe\undefined
  \global\let\flagstretch\undefined
  \global\let\framecolor\undefined
  \global\let\ifemblem\undefined
  \global\let\ifnoemblem\undefined
  \global\let\ifgrid\undefined
  \global\let\viewangle\undefined
  \global\let\hangangle\undefined
}

% ----------------------------------------------------------------------------
% worldflag (as pic)
% ----------------------------------------------------------------------------
\tikzset{length/.style={execute at begin scope={\newdimen\flaglength\flaglength=#1}}}
\tikzset{width/.style={execute at begin scope={\newdimen\flagwidth\flagwidth=#1}}}
\tikzset{framewidth/.style={execute at begin scope={\newdimen\flagframe\flagframe=#1}}}
\tikzset{stretch/.style={execute at begin scope={\def\flagstretch{#1}}}}
\tikzset{framecolor/.style={execute at begin scope={\def\framecolor{#1}}}}
\tikzset{country/.style={execute at begin scope={\def\country{#1}}}}
\tikzset{emblem/.style={execute at begin scope={\newif\ifemblem\emblemtrue\newif\ifnoemblem\noemblemfalse}}}
\tikzset{grid/.style={execute at begin scope={\newif\ifgrid\gridtrue}}}
\tikzset{noemblem/.style={execute at begin scope={\newif\ifnoemblem\noemblemtrue}}}
\tikzset{emblemdefault/.style={execute at begin scope={\newif\ifnoemblem\noemblemtrue\newif\ifemblem\emblemtrue}}}
\tikzset{turn/.style={execute at begin scope={\def\viewangle{#1}}}}
\tikzset{hang/.style={execute at begin scope={\def\hangangle{#1}}}}
\tikzset{
  worldflag/.pic={
    %\begin{scope}
    \ifx\flagwidth\undefined\newdimen\flagwidth\global\flagwidth=\fl@gwidth\fi
    \ifx\flaglength\undefined\newdimen\flaglength\global\flaglength=\fl@glength\fi
    \ifx\flagframe\undefined\newdimen\flagframe\global\flagframe=\fl@gframe\fi
    \ifx\flagstretch\undefined\def\flagstretch{\fl@gstretch}\fi
    \ifx\framecolor\undefined\def\framecolor{\fr@mecolor}\fi
    \def\makeifgrid{\newif\ifgrid\if@rid\gridtrue\else\gridfalse\fi}
    \ifx\ifgrid\undefined\makeifgrid\fi
    \def\makeifemblem{\newif\ifemblem\if@mbl@m\emblemtrue\else\emblemfalse\fi}
    \ifx\ifemblem\undefined\makeifemblem\fi
    \def\makeifnoemblem{\newif\ifnoemblem\ifn@@mbl@m\noemblemtrue\else\noemblemfalse\fi}
    \ifx\ifnoemblem\undefined\makeifnoemblem\fi
    \ifx\viewangle\undefined\def\viewangle{\view@ngle}\fi
    \ifx\hangangle\undefined\def\hangangle{\hang@ngle}\fi
    \IfFileExists{worldflag_\country.tex}{\input{worldflag_\country.tex}}{\relax}
    %\end{scope}
    \global\let\flagwidth\undefined
    \global\let\flaglength\undefined
    \global\let\flagframe\undefined
    \global\let\flagstretch\undefined
    \global\let\framecolor\undefined
    \global\let\ifemblem\undefined
    \global\let\ifnoemblem\undefined
    \global\let\ifgrid\undefined
    \global\let\viewangle\undefined
    \global\let\hangangle\undefined
    }
}
