% Author:  Marc Wäckerlin
% License: LGPL
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{businesscard-qrcode}[2018/08/15 version 1.2 ready for ctan]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% option evaluation
\RequirePackage{kvoptions}
\SetupKeyvalOptions{
	family=BCQ,
	prefix=BCQ@
}
\DeclareStringOption[89mm]{paperwidth}
\DeclareStringOption[59mm]{paperheight}
\DeclareStringOption[85mm]{contentwidth}
\DeclareStringOption[55mm]{contentheight}
\DeclareStringOption[8pt]{fontsize}
\DeclareStringOption[2mm]{padding}
\DeclareStringOption[2]{cutdist}
\DeclareStringOption[1]{cutlen}
\DeclareStringOption[0.50]{textwidth}
\DeclareStringOption[0.40]{qrwidth}
\DeclareStringOption[Q]{qreclevel} % QR code error correction level (L,M,Q,H) default Q
\DeclareStringOption[0.25]{qrlogoscale} % logo size in QR code as fraction of QR width (1.0 = 100%), default 0.25
\DeclareStringOption[0.02]{qrlogoborder} % white padding around logo in QR as fraction of QR width (1.0 = 100%), default 0.02
\DeclareStringOption{logoheight} % logo height next to name (with unit, e.g., 4em), default: 4em if company set, 2em otherwise
\DeclareStringOption[1.0]{bgscale} % background image scale (1.0 = 100% card height), default 1.0
\DeclareStringOption[1.0]{bgopacity} % background image opacity (1.0 = 100% visible, 0.0 = invisible), default 1.0
\DeclareStringOption[1.0]{qrbgopacity} % QR code background opacity (1.0 = white solid, 0.0 = transparent), default 1.0
\DeclareStringOption[de]{lang}
\DeclareBoolOption[false]{ioscrlf} % use CRLF line endings in vCard (iOS compatibility); default off to avoid pdfTeX issues
\DeclareBoolOption[false]{logoinqr} % if true: logo goes in QR center; if false (default): logo next to name
\DeclareBoolOption[true]{address}
\DeclareComplementaryOption{noaddress}{address}
\DeclareBoolOption[true]{hint}
\DeclareComplementaryOption{nohint}{hint}
\DeclareBoolOption[true]{icon}
\DeclareComplementaryOption{noicon}{icon}
\DeclareBoolOption[true]{rightalign}
\DeclareComplementaryOption{leftalign}{rightalign}
\DeclareBoolOption[true]{iconleft}
\DeclareComplementaryOption{iconright}{iconleft}
\DeclareBoolOption[true]{fill}
\DeclareComplementaryOption{nofill}{fill}
\DeclareBoolOption[true]{qrfirst}
\DeclareComplementaryOption{textfirst}{qrfirst}
\DeclareBoolOption[true]{https}
\DeclareComplementaryOption{www}{https}
\DeclareStringOption[auto]{countryformat} % auto: <4 chars inline with mdash, >=4 separate line; inline: always inline; below: always separate line
\DeclareDefaultOption{\@unknownoptionerror}
\ProcessKeyvalOptions*


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% definitions from the options
\def\content{paperwidth=\BCQ@contentwidth,paperheight=\BCQ@contentheight}
\def\papersize{width=\BCQ@paperwidth,height=\BCQ@paperheight}
\def\padding{\BCQ@padding} % padding around the content
\def\border{\BCQ@cutdist} % distance between start of cutmark and content in mm
\def\cutlen{\BCQ@cutlen} % length of ct marks in mm
\def\textpercents{\BCQ@textwidth} % size of text part 0..1
\def\imagepercents{\BCQ@qrwidth} % size of qrcode image part 0..1
\def\lang{\BCQ@lang}
\def\protdisplay{\ifBCQ@https https://\else www.\fi}
\def\protprefix{\ifBCQ@https https://\fi}
\ifBCQ@address\def\printaddress{}\fi


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% global dependencies and settings
\LoadClass[\BCQ@fontsize]{extarticle}
\RequirePackage{marvosym}
\RequirePackage{fontawesome}
\RequirePackage[final]{qrcode}
\RequirePackage{etoolbox}
\RequirePackage{xstring} % for string length checking
\RequirePackage{DejaVuSans}
\RequirePackage[T1]{fontenc}
\RequirePackage{wrapfig}
\RequirePackage{graphicx} % needed for optional logo
\RequirePackage{tikz} % needed for logo overlay in QR code
\RequirePackage{eso-pic} % needed for background images without layout impact
% Fix XeLaTeX opacity issue: ensure TikZ/PGF is loaded before eso-pic uses opacity
\DeclareHookRule{shipout/background}{pgfrcs}{>}{eso-pic}
\RequirePackage[\content,top=\padding,left=\padding,right=\padding,bottom=\padding]{geometry}
%\RequirePackage{pbox}
\RequirePackage{varwidth}
\RequirePackage{calc}
\pagestyle{empty}
\setlength{\parindent}{0pt}
\renewcommand*\familydefault{\sfdefault}
\setlength{\fboxsep}{0pt}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% data registration
\newcommand\registerData[1]{
	\expandafter\newcommand\csname #1\endcsname[1]{
		\expandafter\def\csname X#1\endcsname{##1}
	}
}
\registerData{type}
\registerData{givennames}
\registerData{familynames}
\registerData{honoricprefix}
\registerData{honoricsuffix}
\registerData{company}
\registerData{pobox}
\registerData{extaddr}
\registerData{street}
\registerData{city}
\registerData{region}
\registerData{zip}
\registerData{country}
\registerData{phone}
\registerData{email}
\registerData{jabber}
\registerData{matrixorg}
\registerData{cloud}
\registerData{homepage}
\registerData{wordpress}
\registerData{drupal}
\registerData{joomla}
\registerData{wikipedia}
\registerData{link}
\registerData{world}
\registerData{git}
\registerData{gitea}
\registerData{github}
\registerData{facebook}
\registerData{twitter}
\registerData{youtube}
\registerData{google}
\registerData{pgpurl}
\registerData{pgpfingerprint}
\registerData{logo} % path to logo file for display next to name
\registerData{background} % path to background image file or color name


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% auxiliary commands
\newcommand\enforceright{\leftskip0pt plus 1fill\rightskip0pt}
\newcommand\exec[1]{\csname #1\endcsname}
\newcommand\insa[3][]{\ifcsdef{#2}{

	\ifBCQ@iconleft
		\ifBCQ@icon\parbox{1em}{\centering\exec{#3}}\ \fi\ifBCQ@hint{\tiny#1}\fi\ifBCQ@fill\hfill\fi\exec{#2}
	\else
		\ifBCQ@hint{\tiny#1\ }\fi\exec{#2}\ifBCQ@fill\hfill\fi\ifBCQ@icon\ \parbox{1em}{\centering\exec{#3}}\fi
	\fi
}{}}
\newcommand\ifexists[2]{\ifcsdef{#1}{#2}{}}
\newcommand\ifboth[3]{\ifcsdef{#1}{\ifcsdef{#2}{#3}{}}{}}
\newcommand\ifany[3]{\ifcsdef{#1}{#3}{\ifcsdef{#2}{#3}{}}}
\newcommand\cond[1]{\ifcsdef{#1}{\exec{#1}}{}}
\newcommand\heightscale{\dimexpr\textheight-\ifcsempty{name}{0em}{2em}-\ifcsdef{Xpgpfingerprint}{2em}{0em}-\ifcsdef{Xcompany}{2em}{0em}\relax}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% name - assemble full name from the parts, such as Xgivennames and Xfamilynames
\newcommand\name{\ifexists{Xhonoricprefix}{\Xhonoricprefix\ }\ifexists{Xgivennames}{\Xgivennames\ }\ifexists{Xfamilynames}{\Xfamilynames}\ifexists{Xhonoricsuffix}{\ \Xhonoricsuffix}}

% internal box for measuring name height when logo is present
\newsavebox{\BCQ@namebox}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% vcard - the content of the vcard
% newline selection: default LF (robust for qrcode). Optional CRLF if ioscrlf option set.
\ifBCQ@ioscrlf
	\def\BCQ@nl{\char13\char10}% CRLF
\else
	\def\BCQ@nl{^^J}% LF only
\fi
% vcard assembly (version 4.0 retained). CRLF improves iOS import robustness.
\newcommand\vcard{BEGIN:VCARD\BCQ@nl
VERSION:4.0\BCQ@nl
N:\cond{Xfamilynames};\cond{Xgivennames};\cond{Xcompany};\cond{Xhonoricprefix};\cond{Xhonoricsuffix}\BCQ@nl
FN:\name\ifexists{Xcompany}{\ifcsempty{name}{} { }\Xcompany}\BCQ@nl
\ifexists{printaddress}{ADR\ifexists{Xtype}{;TYPE=\Xtype}:\cond{Xpobox};\cond{Xextaddr};\cond{Xstreet};\cond{Xcity};\cond{Xregion};\cond{Xzip};\cond{Xcountry}\BCQ@nl}
\ifexists{Xphone}{TEL;VALUE=uri;TYPE=\ifexists{Xtype}{\Xtype,}voice,text:tel:\Xphone\BCQ@nl}
\ifexists{Xemail}{EMAIL\ifexists{Xtype}{;TYPE=\Xtype}:\Xemail\BCQ@nl}
\ifexists{Xjabber}{IMPP;TYPE=XMPP:\Xjabber\BCQ@nl}
\ifexists{Xmatrixorg}{IMPP;TYPE=MATRIX:\Xmatrixorg\BCQ@nl}
\ifexists{Xcloud}{URL:https://nextcloud.com/federation/\#\Xcloud\BCQ@nl}
\ifexists{Xhomepage}{URL:https://\Xhomepage\BCQ@nl}
\ifexists{Xwordpress}{URL:https://\Xwordpress\BCQ@nl}
\ifexists{Xdrupal}{URL:https://\Xdrupal\BCQ@nl}
\ifexists{Xjoomla}{URL:https://\Xjoomla\BCQ@nl}
\ifexists{Xwikipedia}{URL:https://\lang.wikipedia.org/wiki/\Xwikipedia\BCQ@nl}
\ifexists{Xlink}{URL:https://\Xlink\BCQ@nl}
\ifexists{Xworld}{URL:https://\Xworld\BCQ@nl}
\ifexists{Xgit}{URL:https://\Xgit\BCQ@nl}
\ifexists{Xgitea}{URL:https://\Xgitea\BCQ@nl}
\ifexists{Xgithub}{URL:https://github.com/\Xgithub\BCQ@nl}
\ifexists{Xfacebook}{URL:https://facebook.com/\Xfacebook\BCQ@nl}
\ifexists{Xtwitter}{URL:https://twitter.com/\Xtwitter\BCQ@nl}
\ifexists{Xyoutube}{URL:https://youtube.com/user/\Xyoutube\BCQ@nl}
\ifexists{Xgoogle}{URL:https://plus.google.com/+\Xgoogle\BCQ@nl}
\ifexists{Xpgpurl}{KEY;MEDIATYPE=application/pgp-keys:\Xpgpurl\BCQ@nl}
\ifexists{Xpgpfingerprint}{KEY:data:application/x-pgp-fingerprint,\Xpgpfingerprint\BCQ@nl}
END:VCARD\BCQ@nl}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% formatcountry - format country based on length and options
\newcommand\formatcountry{%
	\ifcsdef{Xcountry}{%
		\def\BCQ@shouldinline{false}%
		% Check countryformat option
		\ifdefstring{\BCQ@countryformat}{inline}{%
			\def\BCQ@shouldinline{true}%
		}{%
			\ifdefstring{\BCQ@countryformat}{below}{%
				\def\BCQ@shouldinline{false}%
			}{%
				% auto mode: check length
				\StrLen{\Xcountry}[\BCQ@countrylen]%
				\ifnum\BCQ@countrylen<4\relax%
					% Short country code AND city/zip exists → inline
					\ifboolexpr{test {\ifcsdef{Xcity}} or test {\ifcsdef{Xzip}}}{%
						\def\BCQ@shouldinline{true}%
					}{}%
				\fi%
			}%
		}%
		% Output country
		\ifdefstring{\BCQ@shouldinline}{true}{%
			\Xcountry\ \textemdash\ %
		}{%
			\Xcountry
			
		}%
	}{}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% address - the address as shown in the textbox
\newcommand\address{
	
	\cond{Xpobox}

	\cond{Xextaddr}
	
	\cond{Xstreet}
	
	\formatcountry\cond{Xzip} \cond{Xcity}
	
	\cond{Xregion}
	
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% inserttext - assemble the textbox
\newcommand\inserttext{
	%\frame
	{
	\begin{minipage}[c][\heightscale][c]{\textpercents\textwidth}
		\ifBCQ@rightalign\begin{flushright}\fi	
		\ifexists{printaddress}{
			\ifBCQ@iconleft
				\ifBCQ@icon\parbox[c]{1em}{\faMapMarker}\ \ifBCQ@fill\hfill\fi\fi
				%\frame{
					%\pbox[t]{\dimexpr\textwidth-2em\relax}{
				\begin{varwidth}{\dimexpr\textwidth-2em\relax}
					\ifBCQ@rightalign\enforceright\fi\address
				\end{varwidth}
					%}
				%}
			\else
				%\pbox[t]{\dimexpr\textwidth-2em\relax}{
				\begin{varwidth}{\dimexpr\textwidth-2em\relax}
					\ifBCQ@rightalign\enforceright\fi\address
				\end{varwidth}
				%}
				\ifBCQ@icon\ifBCQ@fill\hfill\fi\ \parbox[c]{1em}{\faMapMarker}\fi
			\fi
			\vspace{1em}
		}
	
		\insa[tel:]{Xphone}{faMobile}\insa[mailto:]{Xemail}{Email}\insa[xmpp]{Xjabber}{faCommentsO}\insa[matrix.org]{Xmatrixorg}{faCommentsO}\insa[nextcloud-id]{Xcloud}{faCloud}\insa[\protdisplay]{Xhomepage}{faHome}\insa[\protdisplay]{Xwordpress}{faWordpress}\insa[\protdisplay]{Xdrupal}{faDrupal}\insa[\protdisplay]{Xjoomla}{faJoomla}\insa[{\protprefix}\lang.wikipedia.org/wiki/]{Xwikipedia}{faWikipediaW}\insa[\protdisplay]{Xlink}{faLink}\insa[\protdisplay]{Xworld}{faGlobe}\insa[\protdisplay]{Xgit}{faGit}\insa[\protdisplay]{Xgitea}{faGithubAlt}\insa[{\protprefix}github.com/]{Xgithub}{faGithub}\insa[{\protprefix}facebook.com/]{Xfacebook}{faFacebook}\insa[{\protprefix}twitter.com/]{Xtwitter}{faTwitter}\insa[{\protprefix}youtube.com/user/]{Xyoutube}{faYoutube}\insa[{\protprefix}plus.google.com/+]{Xgoogle}{faGooglePlus}

		\ifBCQ@rightalign\end{flushright}\fi
	\end{minipage}
}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% insertqrcode - insert the qr-code with optional white background
\newcommand\insertqrcode{
	%\frame
	{
	\begin{minipage}[c][\heightscale][c]{\imagepercents\textwidth}
		\begin{tikzpicture}
			% White background behind QR code (for readability over background images)
			\node[fill=white, opacity=\fpeval{\BCQ@qrbgopacity}, inner sep=0pt, outer sep=2mm, minimum size=\textwidth] (bg) at (0,0) {};
			% QR code on top
			\node[inner sep=0pt, outer sep=0pt] (qr) at (0,0) {%
			\ifBCQ@logoinqr
				% QR code with logo overlay in center
				\ifcsdef{Xlogo}{%
					\begin{tikzpicture}
						\node[inner sep=0pt] (qr2) {\qrcode[level=\BCQ@qreclevel,version=0,height=\textwidth]{\vcard}};
						% Logo with transparent background preserved (no fill), padding still applied
						% qrlogoscale and qrlogoborder are now fractions (1.0 = 100%)
						\node[inner sep=\BCQ@qrlogoborder\textwidth] at (qr2.center) {%
							\includegraphics[width=\BCQ@qrlogoscale\textwidth]{\Xlogo}%
						};
					\end{tikzpicture}%
				}{%
					% No logo defined, just show QR code
					\qrcode[level=\BCQ@qreclevel,version=0,height=\textwidth]{\vcard}%
				}%
				\else
					% Standard QR code without overlay
					\qrcode[level=\BCQ@qreclevel,version=0,height=\textwidth]{\vcard}%
				\fi
			};
		\end{tikzpicture}
	\end{minipage}
}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% insertname - insert the name on the top
\newcommand\insertname{
	% typeset name into box for measurement
	\sbox{\BCQ@namebox}{\bfseries\cond{name}\ifexists{Xcompany}{\ifcsempty{name}{} { }\Xcompany}}%
	% Set default logoheight if not specified: 4em with company, 2em without
	\ifx\BCQ@logoheight\@empty
		\ifcsdef{Xcompany}{\def\BCQ@logoheight{4em}}{\def\BCQ@logoheight{2em}}%
	\fi
	\ifcsdef{Xlogo}{% logo defined
		\ifBCQ@logoinqr
			% Logo in QR code: show name only
			{\bfseries\cond{name}}%
			\ifexists{Xcompany}{\\\Xcompany}%
		\else
			% Logo next to name: two-column layout (logo | name)
			\begin{minipage}{\textwidth}
				\begin{minipage}[c]{0.30\textwidth}% logo column
					\includegraphics[height=\BCQ@logoheight]{\Xlogo}%
				\end{minipage}
				\hfill
				\begin{minipage}[c]{0.67\textwidth}% name column
					\ifBCQ@rightalign\raggedleft\fi% apply alignment
					{\bfseries\cond{name}}%
					\ifexists{Xcompany}{\\\Xcompany}%
				\end{minipage}
			\end{minipage}
		\fi
	}{% no logo: just name
		{\bfseries\cond{name}}%
		\ifexists{Xcompany}{\\\Xcompany}%
	}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% drawcardcontent - assemble all blocks (name, text, qr, pgp)
\newcommand\drawcardcontent{
	\ifBCQ@rightalign\begin{flushright}\fi
		\insertname 
		\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}} % horizontal line
		\vfill
		\ifBCQ@qrfirst
			\insertqrcode
			\hfill
			\inserttext
		\else
			\inserttext
			\hfill
			\insertqrcode
		\fi
		\ifexists{Xpgpfingerprint}{
			\vfill
			\ifBCQ@iconleft
				\faLock\ifBCQ@hint{\tiny\ pgp}\fi\hfill{\small\Xpgpfingerprint}
			\else
				{\small\Xpgpfingerprint}\hfill\ifBCQ@hint{\tiny{pgp\ }}\fi\faLock
			\fi
		}
	\ifBCQ@rightalign\end{flushright}\fi
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% drawcard - main card with optional background
\newcommand\drawcard{
	% Handle background
	\ifcsdef{Xbackground}{%
		\IfFileExists{\Xbackground}{%
			% It's an image file: add to background using eso-pic (no layout impact)
			\AddToShipoutPictureBG*{%
				\AtPageCenter{%
					\begin{tikzpicture}[overlay]
						\node[inner sep=0pt, opacity=\fpeval{\BCQ@bgopacity}] at (0,0) {%
							\includegraphics[height=\fpeval{\BCQ@bgscale}\paperheight]{\Xbackground}%
						};
					\end{tikzpicture}%
				}%
			}%
		}{%
			% Not a file: treat as color
			\pagecolor{\Xbackground}%
		}%
	}{}%
	% Draw card content
	\drawcardcontent
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% cut / crop marks
\RequirePackage[\papersize,noinfo,center,pdftex]{crop}
\newcommand\tl{
	\begin{picture}(0,0)
	\thinlines\unitlength1mm
	\put(-\border,0){\line(1,0){\cutlen}}
	\put(0,\border){\line(0,-1){\cutlen}}
	\end{picture}
}
\newcommand\tr{
	\begin{picture}(0,0)
	\thinlines\unitlength1mm
	\put(\border,0){\line(-1,0){\cutlen}}
	\put(0,\border){\line(0,-1){\cutlen}}
	\end{picture}
}
\newcommand\bl{
	\begin{picture}(0,0)
	\thinlines\unitlength1mm
	\put(-\border,0){\line(1,0){\cutlen}}
	\put(0,-\border){\line(0,1){\cutlen}}
	\end{picture}
}
\newcommand\br{
	\begin{picture}(0,0)
	\thinlines\unitlength1mm
	\put(\border,0){\line(-1,0){\cutlen}}
	\put(0,-\border){\line(0,1){\cutlen}}
	\end{picture}
}
\cropdef[]\tl\tr\bl\br{cut}
\crop[cut]
