%% $Id: hvqrurl.sty 848 2024-01-28 11:57:35Z herbert $
%%
%% This file is distributed under the terms of the LaTeX Project Public
%% License from CTAN archives in directory  macros/latex/base/lppl.txt.
%% Either version 1.3 or, at your option, any later version.
%%
% Copyright 2019,21,23,24 Herbert Voss hvoss@tug.org
%%
\ProvidesPackage{hvqrurl}[%
  2024/01/28 v.0.05 (Herbert Voss) Supports qr images into the margin (hv)] 
%
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{url}
\RequirePackage{xcolor}
\RequirePackage{marginnote}
\RequirePackage{qrcode}
\RequirePackage{xkeyval}

\newlength\qr@url@qrheight
\newlength\qr@url@qradjust

\define@key{hvqr}{qrheight}[1cm]{\setlength\qr@url@qrheight{#1}}
\define@key{hvqr}{qradjust}[-1.5\normalbaselineskip]{\setlength\qr@url@qradjust{#1}}
\define@key{hvqr}{qrcolor}[black]{\colorlet{qr@url@qrcolor}{#1}}
\define@key{hvqr}{qrlevel}[M]{\def\qr@url@qrlevel{#1}}
\define@key{hvqr}{qrlink}[link]{\def\qr@url@qrlink{#1}}
\define@key{hvqr}{linktext}[]{\def\qr@url@linktext{#1}}
\define@boolkey{hvqr}[hv@]{qrreverse}[true]{}

\setkeys{hvqr}{qrheight,qrcolor=black,qradjust,qrlevel,qrlink=link,
	qrreverse=false,linktext={}}% the default setting

\newcommand*\hvqrset[1]{\setkeys{hvqr}{#1}}

\def\hvqrurl{\@ifnextchar*{\@tempswafalse\hvqr@url}{\@tempswatrue\hvqr@@url}}

\def\hvqr@url*{\hvqr@@url}

\newcommand*\hvqr@@url[2][]{%
  \begingroup
  \providecommand*\qr@blank{0}%
  \ifx\relax#1\relax \else\hvqrset{#1}\fi
  \expandafter\qrset\expandafter{\qr@url@qrlink,height=\qr@url@qrheight,level=\qr@url@qrlevel}%
  \ifhv@qrreverse\reversemarginpar\fi
  \if@tempswa
    \ifx\relax\qr@url@linktext\relax % alternative link text??
      \edef\hv@foo{\detokenize\expandafter{#2}}%
    \else
      \edef\hv@foo{\detokenize\expandafter{\qr@url@linktext}}%
    \fi
    \expandafter\url\expandafter{\hv@foo}%
  \fi
  \edef\reserved@a{\noexpand\marginnote{\noexpand\color{qr@url@qrcolor}\noexpand\qrcode{%
     \detokenize\expandafter{#2}}}[\qr@url@qradjust]}%
  \reserved@a
  \endgroup
}

\endinput
