% ======================================================================
% plength-tikz.tex
% Copyright (c) Markus Kohm, 2005-2022
%
% This file is part of the LaTeX2e KOMA-Script bundle.
%
% This work may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, version 1.3c of the license.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX 
% version 2005/12/01 or later and of this work.
%
% This work has the LPPL maintenance status "author-maintained".
%
% The Current Maintainer and author of this work is Markus Kohm.
%
% This work consists of all files listed in MANIFEST.md.
% ----------------------------------------------------------------------
%
% Generation of plength figures at scrlttr2 chapter of the KOMA-Script
% guide
%
% Maintained by Markus Kohm
% Original metapost source by Stephan Hennig
% Original TikZ source by Marei Peischl
%
% ======================================================================

\KOMAProvidesFile{plength-tikz.tex}%
                 [$Date: 2022-06-05 12:40:11 +0200 (So, 05. Jun 2022) $
                  KOMA-Script guide (figure in scrlttr2.tex)]

\ExplSyntaxOn
\prop_if_exist:NF \l_this_plength_description_prop {
  \prop_new:N \l_this_plength_description_prop
}
\prop_set_from_keyval:Nn \l_this_plength_description_prop {
  firsthead=\letterheadname,
  firstfoot=\letterfootname,
  backaddress=\backaddressname,
  specialmail=\specialmailname,
  toaddr=\toaddrname,
  refline=\reflinename,
  title=\titlename,
  subject=\subjectname,
  opening=\openingname,
  body=\letterbodyname,
  closing=\closingname,
  signature=\signaturename,
  location=\begin{tabular}{@{}c@{}}\locationname\end{tabular},
}

\prop_if_exist:NF \l_this_plength_var_prop {
  \prop_new:N \l_this_plength_var_prop
}

\prop_set_from_keyval:Nn \l_this_plength_var_prop {
  ticksize=1,
  textwidth= 147,
  textheight= 209.4,
  evensidemargin= 6.1,
  oddsidemargin = 6.1,
  paperwidth = 210,
  paperheight = 297,
  baselineskip = .9\baselineskip,% 3.86607,
  headheight     =  6,
  headsep        =7.2,
  footskip       =16.73,
  foldmarkhpos = 3.5,
  tfoldmarkvpos = 105,
  bfoldmarkvpos = 210,
  tfoldmarklength = 2,
  pfoldmarklength = 4,
  bfoldmarklength = 2,
  toaddrvpos = 45,
  refvpos = 98.5,
  refaftervskip = \UseVar{baselineskip},
  toaddrhpos = 20,
  toaddrwidth = 85,
  toaddrheight = 40,
  toaddrindent = 6,
  specialmailwidth = 50,
  specialmailrightindent = 4,
  specialmailheight = \UseVar{baselineskip},
  locwidth = 37.5,
  backaddrheight = 5,
  firstheadvpos = 8,
  firstheadwidth = \UseVar{paperwidth} - 2 * \UseVar{toaddrhpos},
  firstfootwidth = \UseVar{firstheadwidth},
  firstfootvpos =  16.58 + \UseVar{headheight} + \UseVar{headsep} + \UseVar{textheight} + \UseVar{footskip},
  refwidth = 0,
  sigindent = 0,
  toaddrindent =0,
  sigbeforevskip = 2*\UseVar{baselineskip},
  firstheadhpos = 0.5* \UseVar{paperwidth}-.5*\UseVar{firstheadwidth},
  firstheadheight = 5*\UseVar{baselineskip},
  firstfoothpos = 0.5*(\UseVar{paperwidth}-\UseVar{firstfootwidth}),
  firstfootheight = 3*\UseVar{baselineskip},
  fromrulewidth = 0.5 * \UseVar{firstheadwidth},
  lochpos = \UseVar{paperwidth}-\UseVar{toaddrhpos}-\UseVar{locwidth},
  refhpos = 25.40+\UseVar{oddsidemargin},
  text = \UseVar{refhpos},
  textcenter = \UseVar{refhpos}+0.5*\UseVar{textwidth},
  refheight = 2*\UseVar{baselineskip},
  refwidth = \UseVar{textwidth},
  titlevpos = \UseVar{refvpos}+\UseVar{refheight}+\UseVar{refaftervskip},
  titlewidth = 90,
  titleheight = 1.2*\UseVar{baselineskip},
  subjectvpos = \UseVar{titlevpos}+\UseVar{titleheight}+1*\UseVar{baselineskip},
  subjectwidth = 80,
  subjectheight = \UseVar{baselineskip},
  openingvpos = \UseVar{subjectvpos}+\UseVar{subjectheight}+2*\UseVar{baselineskip},
  openingwidth = 60,
  openingheight = \UseVar{baselineskip},
  bodyvpos = \UseVar{openingvpos}+\UseVar{openingheight}+\UseVar{baselineskip},
  bodywidth = \UseVar{textwidth},
  bodyheight = 6*\UseVar{baselineskip},
  typeareabottom = \UseVar{firstfootvpos}-\UseVar{footskip},
  sigvpos = \UseVar{bodyvpos}+\UseVar{bodyheight}+\UseVar{baselineskip},
  sigwidth = 50,
  sigheight = \UseVar{baselineskip},
  locvpos = \UseVar{toaddrvpos},
  locheight = \UseVar{toaddrheight},
  sigindent = 10,
  toaddrindent = 7,
}
\def\UseVar#1{
  \fp_eval:n {\prop_item:Nn \l_this_plength_var_prop {#1}}
}

\def\UseDesc#1{
  \desc
  \prop_item:Nn \l_this_plength_description_prop {#1}
}

\ExplSyntaxOff

\def\desc{\itshape}

\providecommand*{\Multi}[1]{%
  {\def\and{, }%
    \begin{tabular}{@{}l@{}}
      #1
    \end{tabular}
  }%
}

\pgfarrowsdeclare{measure}{measure}
{
  \arrowsize=\pgflinewidth
  \pgfarrowsleftextend{0\arrowsize}
  \pgfarrowsrightextend{5\arrowsize}
}{
  \arrowsize=\pgflinewidth
  \pgfsetdash{}{0pt}
  \pgfsetlinewidth{.5\arrowsize}
  \pgfpathmoveto{\pgfpoint{4.75\arrowsize}{7\arrowsize}}
  \pgfpathlineto{\pgfpoint{4.75\arrowsize}{-7\arrowsize}}
  \pgfusepathqstroke
  \pgfsetlinewidth{0.01pt}
  \pgfpathmoveto{\pgfpoint{4.5\arrowsize}{0pt}}
  \pgfpathlineto{\pgfpoint{-.5\arrowsize}{2\arrowsize}}
  \pgfpathlineto{\pgfpoint{-.5\arrowsize}{-2\arrowsize}}
  \pgfpathclose
  \pgfusepathqfillstroke
}

\pgfdeclaredecoration{vmeasure}{measure begin}{%
  \state{measure begin}[
  width={\pgfmetadecoratedpathlength - \pgfdecorationsegmentlength},
  next state=measure end,
  ]
  {
    \pgfpathmoveto{\pgfqpoint{\dimexpr\pgfmetadecoratedpathlength - \pgfdecorationsegmentlength}{0pt}}
  }%
  \state{measure end}[width=0pt, next state=final]{
    \pgfpathlineto{\pgfpointorigin}
  }%
  \state{final}
  {
    \pgfpathlineto{\pgfpointdecoratedpathlast}
  }%
}%

\tikzset{
  measure/.style={arrows=measure-measure,every node/.append style={font=\sffamily\strut}},
  top hmeasure/.style={measure,
    yshift=7\pgflinewidth,
    every node/.append style={yshift=10\pgflinewidth},
  },
  bottom hmeasure/.style={measure,every node/.append style={below},yshift=-7\pgflinewidth,},
  left double vmeasure/.style={
    measure,
    xshift=-7\pgflinewidth,
    every node/.append style={rotate=90,above,},
  },
  right double vmeasure/.style={
    measure,
    xshift=7\pgflinewidth,
    every node/.append style={rotate=90,below},
  },
  right vmeasure/.style={
    measure,
    xshift=7\pgflinewidth,
    arrows=-measure,
    decoration={vmeasure,post length=14\pgflinewidth,segment length=20\pgflinewidth},decorate,
    every node/.append style={above=7\pgflinewidth},
  },
  left vmeasure/.style={
    measure,
    xshift=-7\pgflinewidth,
    arrows=-measure,
    decoration={vmeasure,post length=14\pgflinewidth,segment length=20\pgflinewidth},decorate,
    every node/.append style={above=14\pgflinewidth},
  },
  hmeasure/.style={yshift=7\pgflinewidth},
  measure right/.style={xshift=7\pgflinewidth},
  measure left/.style={xshift=-7\pgflinewidth}
}

\begin{tikzpicture}[fill=black!20]
  \draw (0,0)rectangle (\UseVar{paperwidth},\UseVar{paperheight});
  
  \filldraw(\UseVar{firstheadhpos},\UseVar{firstheadvpos})rectangle node{\UseDesc{firsthead}}+(\UseVar{firstheadwidth},\UseVar{firstheadheight});

  \filldraw(\UseVar{toaddrhpos},\UseVar{toaddrvpos}) rectangle
  node {\UseDesc{backaddress}}
  +(\UseVar{toaddrwidth},\UseVar{backaddrheight});
  
  \filldraw(\UseVar{toaddrhpos}+.5*\UseVar{toaddrwidth}-\UseVar{specialmailrightindent},\UseVar{toaddrvpos}+\UseVar{backaddrheight}) rectangle
  node {\UseDesc{specialmail}}
  +(.5*\UseVar{toaddrwidth},\UseVar{specialmailheight});

  \filldraw(\UseVar{toaddrhpos}+\UseVar{toaddrindent},\UseVar{toaddrvpos}+\UseVar{backaddrheight}+\UseVar{specialmailheight})
  rectangle node {\UseDesc{toaddr}}
  +(\UseVar{toaddrwidth}-2*\UseVar{toaddrindent},\UseVar{toaddrheight}-\UseVar{backaddrheight}-\UseVar{specialmailheight});
  
  \draw(\UseVar{toaddrhpos},\UseVar{toaddrvpos})rectangle+(\UseVar{toaddrwidth},\UseVar{toaddrheight});
  
  \filldraw (\UseVar{refhpos},\UseVar{refvpos})rectangle node{\UseDesc{refline}}
  +(\UseVar{refwidth},\UseVar{refheight});
  
  \filldraw (\UseVar{textcenter}-.5*\UseVar{titlewidth},\UseVar{titlevpos})rectangle node{\UseDesc{title}}
  +(\UseVar{titlewidth},\UseVar{titleheight});
  
  \filldraw (\UseVar{text},\UseVar{subjectvpos})rectangle node{\UseDesc{subject}}
  +(\UseVar{subjectwidth},\UseVar{subjectheight});
  
  \filldraw (\UseVar{text},\UseVar{openingvpos})rectangle node{\UseDesc{opening}}
  +(\UseVar{openingwidth},\UseVar{openingheight});
  
  \filldraw (\UseVar{text},\UseVar{bodyvpos})rectangle node{\UseDesc{body}}
  +(\UseVar{bodywidth},\UseVar{bodyheight});
  
  \filldraw (\UseVar{text}+\UseVar{sigindent},\UseVar{sigvpos})rectangle node{\UseDesc{closing}}
  +(\UseVar{sigwidth},\UseVar{sigheight});
  
  \filldraw (\UseVar{text}+\UseVar{sigindent}+.1*\UseVar{sigwidth},\UseVar{sigvpos}+\UseVar{sigheight}+\UseVar{sigbeforevskip})rectangle node{\UseDesc{signature}}
  +(.8*\UseVar{sigwidth},\UseVar{sigheight});
  
  \filldraw (\UseVar{lochpos},\UseVar{locvpos}) rectangle node{\UseDesc{location}}+(\UseVar{locwidth},\UseVar{locheight});
  
  \filldraw (\UseVar{firstfoothpos},\UseVar{firstfootvpos}) rectangle node{\UseDesc{firstfoot}} +(\UseVar{firstfootwidth},\UseVar{firstfootheight});
  
  \draw[thick] (\UseVar{foldmarkhpos},\UseVar{tfoldmarkvpos}) --+(\UseVar{tfoldmarklength},0);
  \draw[thick] (\UseVar{foldmarkhpos},.5*\UseVar{paperheight}) --+(\UseVar{pfoldmarklength},0);
  \draw[thick] (\UseVar{foldmarkhpos},\UseVar{bfoldmarkvpos}) --+(\UseVar{bfoldmarklength},0);
  %%%%%%%%%%%%%%%%%%%%%%%% 
  \draw (\UseVar{text}+\UseVar{sigindent},\UseVar{sigvpos})rectangle +(\UseVar{sigwidth},2*\UseVar{sigheight}+\UseVar{sigbeforevskip});
  
  \draw (\UseVar{text},\UseVar{titlevpos})rectangle (\UseVar{text}+\UseVar{textwidth},\UseVar{firstfootvpos}-\UseVar{footskip})
  -- +(.5*\UseVar{firstfootwidth}-.5*\UseVar{textwidth},0);
  %%%%%%%%%%%%%%%%%%%%%%%% 
  
  \draw[right vmeasure] (\UseVar{foldmarkhpos}+\UseVar{tfoldmarklength},0)--++(0,\UseVar{tfoldmarkvpos})+(0,\UseVar{ticksize})node[anchor=150]{\DescRef{scrlttr2.plength.tfoldmarkvpos}};
  \draw[right vmeasure] (\UseVar{foldmarkhpos}+\UseVar{bfoldmarklength},0)--++(0,\UseVar{bfoldmarkvpos})+(0,-\UseVar{ticksize})node[anchor=-150]{\DescRef{scrlttr2.plength.bfoldmarkvpos}};
  
  \draw[top hmeasure] (0,\UseVar{firstheadvpos}) node[right]{\DescRef{scrlttr2.plength.firstheadhpos}} -- +(\UseVar{firstheadhpos},0);
  \draw[top hmeasure] (\UseVar{firstheadhpos},\UseVar{firstheadvpos})-- node{\DescRef{scrlttr2.plength.firstheadwidth}} +(\UseVar{firstheadwidth},0);
  \draw[right vmeasure] (\UseVar{paperwidth}-\UseVar{firstheadhpos},0)--node[near end,anchor=base east]{\DescRef{scrlttr2.plength.firstheadvpos}}+(0,\UseVar{firstheadvpos});
  
  \draw[right vmeasure] (\UseVar{toaddrhpos}+\UseVar{toaddrwidth},0)--+(0,\UseVar{toaddrvpos})node{\DescRef{scrlttr2.plength.toaddrvpos}};
  \draw[right double vmeasure] (\UseVar{toaddrhpos}+\UseVar{toaddrwidth},\UseVar{toaddrvpos}) -- node[rotate=-90,right]{\DescRef{scrlttr2.plength.backaddrheight}} +(0,\UseVar{backaddrheight});
  \draw[top hmeasure] (0,\UseVar{toaddrvpos}) node[right]{\DescRef{scrlttr2.plength.toaddrhpos}} --+(\UseVar{toaddrhpos},0);
  \draw[top hmeasure] (\UseVar{toaddrhpos},\UseVar{toaddrvpos})-- node{\DescRef{scrlttr2.plength.toaddrwidth}} +(\UseVar{toaddrwidth},0);
  \draw[measure] (\UseVar{toaddrhpos}+\UseVar{toaddrwidth},\UseVar{toaddrvpos}+1.5*\UseVar{backaddrheight})node[right] {\DescRef{scrlttr2.plength.specialmailindent}} --+(-\UseVar{specialmailrightindent},0);
  \draw[top hmeasure] (\UseVar{toaddrhpos},\UseVar{toaddrvpos}+\UseVar{toaddrheight})-- +(\UseVar{toaddrindent},0) node[right]{\DescRef{scrlttr2.plength.toaddrindent}};
  \draw[top hmeasure] (\UseVar{toaddrhpos}+\UseVar{toaddrwidth},\UseVar{toaddrvpos}+\UseVar{toaddrheight}) --+ (-\UseVar{toaddrindent},0) node[left]{\DescRef{scrlttr2.plength.toaddrindent}};
  \draw[left double vmeasure] (\UseVar{toaddrhpos},\UseVar{toaddrvpos})--
  node{\DescRef{scrlttr2.plength.toaddrheight}}
  +(0,\UseVar{toaddrheight});
  
  \draw[right double vmeasure] (\UseVar{lochpos}+\UseVar{locwidth},\UseVar{locvpos})--node{\DescRef{scrlttr2.plength.locheight}}+(0,\UseVar{locheight});
  \draw[left vmeasure] (\UseVar{lochpos},0)--+(0,\UseVar{locvpos})node{\DescRef{scrlttr2.plength.locvpos}};
  \draw[top hmeasure] (\UseVar{lochpos}+\UseVar{locwidth},\UseVar{locvpos})-- node{\DescRef{scrlttr2.plength.lochpos}} (\UseVar{paperwidth},\UseVar{locvpos});
  \draw[top hmeasure] (\UseVar{lochpos},\UseVar{locvpos})-- node{\DescRef{scrlttr2.plength.locwidth}} +(\UseVar{locwidth},0);
  
  \draw[right vmeasure] (\UseVar{refhpos}+\UseVar{refwidth},0)--+(0,\UseVar{refvpos})node[above=\baselineskip]{\DescRef{scrlttr2.plength.refvpos}};
  \draw[top hmeasure](0,\UseVar{refvpos})--node{\DescRef{scrlttr2.plength.refhpos}}+(\UseVar{refhpos},0);
  \draw[top hmeasure](\UseVar{refhpos},\UseVar{refvpos})--node{\DescRef{scrlttr2.plength.refwidth}}+(\UseVar{refwidth},0);
  
  \draw[right double vmeasure] (\UseVar{refhpos}+\UseVar{refwidth},\UseVar{refvpos}+\UseVar{refheight})--
  node[rotate=-90,right]{\DescRef{scrlttr2.plength.refaftervskip}}
  +(0,\UseVar{refaftervskip});
  
  \draw[right double vmeasure] (\UseVar{text}+\UseVar{openingwidth},\UseVar{titlevpos}+\UseVar{titleheight})--
  node[rotate=-90,right]{\Length{baselineskip}+\DescRef{scrlttr2.plength.subjectbeforevskip}}
  (\UseVar{text}+\UseVar{openingwidth},\UseVar{subjectvpos});
  
  \draw[right double vmeasure] (\UseVar{text}+\UseVar{openingwidth},\UseVar{subjectvpos}+\UseVar{subjectheight})--
  node[rotate=-90,right]{\DescRef{scrlttr2.plength.subjectaftervskip}}
  (\UseVar{text}+\UseVar{openingwidth},\UseVar{openingvpos});
  
  \draw[right double vmeasure] (\UseVar{text}+\UseVar{openingwidth},\UseVar{openingvpos}+\UseVar{openingheight})--
  node[rotate=-90,right]{\Length{baselineskip}}
  (\UseVar{text}+\UseVar{openingwidth},\UseVar{bodyvpos});
  
  \draw[right double vmeasure] (\UseVar{text}+\UseVar{openingwidth},\UseVar{bodyvpos}+\UseVar{bodyheight})--
  node[rotate=-90,right]{\Length{baselineskip}}
  (\UseVar{text}+\UseVar{openingwidth},\UseVar{sigvpos});
  
  \draw[bottom hmeasure](\UseVar{text},\UseVar{firstfootvpos}-\UseVar{footskip})--node{\Length{textwidth}} +(\UseVar{textwidth},0);
  
  \draw[left double vmeasure] (\UseVar{text}+1.5*\UseVar{sigindent},\UseVar{sigvpos}+\UseVar{sigheight})--node[rotate=-90,right]{\DescRef{scrlttr2.plength.sigbeforevskip}}
  +(0,\UseVar{sigbeforevskip});
  
  \draw[left vmeasure] (\UseVar{firstfoothpos},0)--+(0,\UseVar{firstfootvpos})node{\DescRef{scrlttr2.plength.firstfootvpos}};
  \draw[top hmeasure] (\UseVar{firstfoothpos},\UseVar{firstfootvpos}) -- node{\DescRef{scrlttr2.plength.firstfootwidth}} +(\UseVar{firstfootwidth},0);
  \draw[bottom hmeasure] (0,\UseVar{firstfootvpos}+\UseVar{firstfootheight}) node[below right]{\DescRef{scrlttr2.plength.firstfoothpos}} -- +(\UseVar{firstfoothpos},0);
  \draw[right double vmeasure] (\UseVar{firstfoothpos}+\UseVar{firstfootwidth},\UseVar{firstfootvpos}) -- node{$\geq$ \Length{footskip}} +(0,-\UseVar{footskip});
\end{tikzpicture}

\endinput

%%% Local Variables: 
%%% mode: latex
%%% coding: utf-8
%%% End: 
