%% This is file `bfhletter.sty' version 2.2.6 (2026-01-12),
%% it is part of
%% BFH-CI -- Corporate Design for Bern University of Applied Sciences
%% ----------------------------------------------------------------------------
%%
%%  Copyright (C) 2021--2026 by
%%      Marei Peischl <marei@peitex.de>
%%      Andreas Habegger <andreas.habegger@bfh.ch>
%%
%% ============================================================================
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% 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 2008/05/04 or later.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainers of this work are
%%   Marei Peischl <bfh-ci@peitex.de>
%%   Andreas Habegger <andreas.habegger@bfh.ch>
%%
%% The development respository can be found at
%% https://gitlab.ti.bfh.ch/bfh-latex/bfh-ci/
%% Please use the issue tracker for feedback!
%%
%% ============================================================================
%%
\ProvidesPackage{bfhletter}[2026-01-12 v2.2.6 letter layouts for BFH-CI, CI of Bern University of Applied Sciences]

\PassOptionsToPackage{fromlogo=true,refline=false,addrfield=topaligned,pagenumber=botright}{scrletter}

\RequirePackage{l3keys2e}

\ExplSyntaxOn
\keys_define:nn {ptxcd/letter} {
	logofile .tl_gset:N = \g_ptxcd_letterlogofile_tl,
	logofile .initial:n = BFH-logo_\g__ptxcd_colormode_tl,
	colormode .code:n =\PassOptionsToPackage{colormode=#1}{bfhcolors},
}
\ExplSyntaxOff

\ProcessKeysOptions{ptxcd/letter}

\RequirePackage{scrletter}

\RequirePackage{graphicx}
\RequirePackage{bfhcolors}
\RequirePackage{translations}
\LoadDictionary{bfhtranslations}

\ExplSyntaxOn
\file_if_exist:nTF {\g_ptxcd_letterlogofile_tl.pdf}
{\setkomavar{fromlogo}{\includegraphics[height=19mm]{\g_ptxcd_letterlogofile_tl}}}
{
	\msg_new:nnnn {bfh-ci} {missing-bfhlogo}
	{I~could~not~find~the~bfhlogo~package.}
	{Please~have~a~look~at~the~bfh-ci~documentation~for~more~information~or~provide~an~alternative~using~the~logofile~option.}
	\msg_warning:nn {bfh-ci} {missing-bfhlogo}
}


\ExplSyntaxOff

\PassOptionsToPackage{pass}{geometry}
\RequirePackage{geometry}
\AtBeginLetter{
	\newgeometry{
		includehead=false,
		includefoot=false,
		top=44.7mm,
		bottom=21.8mm,
		left=25.3mm,
		right=36.7mm,
		footskip=\dimexpr12.7mm-8bp\relax
	}
	\makeatletter
	\input{bfhlettersize9.5pt.clo}
	\makeatother
}
\AtEndLetter{\restoregeometry}

\setkomavar{firsthead}{%
	\if@logo
		\rlap{\usekomavar{fromlogo}}%
	\fi
}

\ExplSyntaxOn

\setkomafont{placeanddate}{\small\sffamily}
\newkomafont{ptxcd_location}{\sffamily\small}
\setkomavar{specialmail}[]{\rule[-\dp\strutbox]{0pt}{5.9mm}}
\setkomavar*{fromurl}{}
\setkomavar*{fromemail}{}

%DOKU neue komavars
\newkomavar{frominstitution}
\newkomavar{frominstitution-en}
\newkomavar{fromfunction}

\setplength{PPdatamatrixvskip}{1cm}
\setkomavar{backaddress}{BFH\\\usekomavar{fromname}\\\usekomavar{fromaddress}}

\setkomavar{backaddressseparator}{\space|\space}

\@setplength{toaddrvpos}{\dimexpr51.8mm-7bp}
\@setplength{toaddrhpos}{25.3mm}
\@setplength{firstheadhpos}{25.3mm}
\@setplength{locvpos}{22.9mm}
\@setplength{refvpos}{\dimexpr94.6mm-\ht\strutbox}
\@setplength{locwidth}{53mm}
\@setplength{lochpos}{-139.3mm}

\cs_set:Nn \__ptxcd_locfield: {%
	\usekomafont{ptxcd_location}\usekomafont{fromaddress}
	\raggedright
	\setparsizes{\z@}{\z@}{\z@ plus 1fil}
	\par@updaterelative
	\begingroup
	\int_compare:nT {0 < \@pageat <3} {
		\ptxcd_letterpagemark:\\[\baselineskip]
	}
	\textbf{\GetTranslation{Bern~University~of~Applied~Sciences}}\\
	\Ifkomavarempty{frominstitution}{}{%
		{\parbox[t][2\baselineskip]{\linewidth}{\usekomavar[\raggedright]{frominstitution}}}
		\\[.5\baselineskip]}%
	\endgroup
	\Ifkomavarempty{fromname}{}
	{{\usekomafont{fromname}\usekomavar{fromname}}
		\Ifkomavarempty{fromfunction}{}{\\\usekomavar{fromfunction}}
		\\[\baselineskip]}%
	\Ifkomavarempty{fromaddress}{}{\usekomavar{fromaddress}\\[\baselineskip]}%
	\clist_map_inline:nn {fromphone, fromfax}{
		\Ifkomavarempty{##1}{}{
			\Ifkomavarempty*{##1}{}{\usekomavar*{##1}\space}\usekomavar{##1}\par
		}
	}
	\par\vspace{.5\baselineskip}
	\clist_map_inline:nn {fromemail, fromurl}{
		\Ifkomavarempty{##1}{}{
			\Ifkomavarempty*{##1}{}{\usekomavar*{##1}\space}\usekomavar{##1}\par
		}
	}
	\strut\seq_use:Nn  \g__ptxcd_reffields_seq {}
}

\seq_new:N  \g__ptxcd_reffields_seq

\tl_const:Nn \ptxcd_reffield_separator_tl {\\}

\setkomavar{location}{\parbox[t][\useplength{locheight}][t]{\useplength{locwidth}}{\__ptxcd_locfield:}}

\RequirePackage{bfhfonts}

\cs_if_exist_use:NT \fontspec_font_if_exist:nT {{LucidaSansOT.otf} {
			\setsansfont[
				Ligatures=TeX,
				ItalicFont=LucidaSansOT-Italic.otf,
				BoldFont=LucidaSansOT-Demi.otf,
				BoldItalicFont=LucidaSansOT-DemiItalic.otf,
			]{LucidaSansOT.otf}
		}}

\renewcommand*{\familydefault}{\sfdefault}

\ExplSyntaxOff

\if@refline
\else
	\renewcommand*{\@datefield}{%
		{%
				\setparsizes{\z@}{\z@}{\z@ plus 1fil}\par@updaterelative
				\@setplength{refwidth}{\useplength{locwidth}}%
				\@setplength{refhpos}{-\useplength{lochpos}}%
				\@tempswafalse
				\move@topt\vskip\useplength{refvpos}%
				\@tempswafalse
				\rlap{\noindent\move@topl\hskip\useplength{refhpos}%
					\vbox{\hsize\useplength{refwidth}%
						\noindent
						{\usekomafont{placeanddate}{\Ifkomavarempty{place}{}{%
									\usekomavar{place}\usekomavar{placeseparator}}%
								\usekomavar{date}}}%
					}%
				}%
				\vskip\useplength{refaftervskip}%
			}%
	}
\fi

%signature raggedright
\let\raggedsignature\raggedright

\endinput
