% \iffalse meta-comment -*- mode: doctex -*-
%
% ullrcorners: draw corners on the upper left and lower right sides of
% a word
%
% Copyright (C) 2025 Vincent Goulet
%
% This file 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
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Vincent Goulet
% <vincent.goulet@act.ulaval.ca>.
%
% This work consists of ullrcorners.dtx and the derived files.
% \fi
%
% \iffalse
%<*dtx>
\ProvidesFile{ullrcorners.dtx}
%</dtx>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{ullrcorners}
%<*package>
[2025/05/16 v1.0 ullrcorners]
%</package>
%<*driver>
\documentclass[11pt,x11names]{ltxdoc}
  \usepackage{natbib}
  \usepackage{ullrcorners}
  \usepackage{enumitem}
  \usepackage{pict2e}
  \usepackage{metalogo}
  \def\changes#1#2#3{\glossary{#1>#3}} % simplified
  \DisableCrossrefs
  \CodelineNumbered
  \RecordChanges

  %% Configuration of the glossary
  \def\glossaryname{Version history}
  \GlossaryPrologue{\section*{\glossaryname}%
    \addcontentsline{toc}{section}{\glossaryname}}

  %% Fonts
  \iftutex
    \RequirePackage{microtype}
    \RequirePackage{fontspec}
    \RequirePackage{unicode-math}
    \setmainfont{STIXTwoText}
    [
      Extension = .otf,
      UprightFont = *-Regular,
      BoldFont = *-SemiBold,
      ItalicFont = *-Italic,
      BoldItalicFont = *-SemiBoldItalic,
      Ligatures = TeX,
    ]
    \setmathfont{STIXTwoMath-Regular}
    [
      Extension = .otf,
      bold-style = TeX
    ]
  \else
    \RequirePackage{microtype}
    \RequirePackage[utf8]{inputenc}
    \RequirePackage[T1]{fontenc}
    \RequirePackage{stix2}
  \fi
  \RequirePackage[medium,lining,scale=0.90]{FiraMono}

  %% Colors
  \usepackage{xcolor}
  \definecolor{link}{rgb}{0,0.4,0.6}   % internal links
  \definecolor{url}{rgb}{0.6,0,0}      % external links
  \definecolor{citation}{rgb}{0,0.5,0} % citations
  \colorlet{codebg}{LightYellow1}      % code background
  \colorlet{ullrcornerscolor}{Snow4}   % corners

  %% New \DescribeMacro type environment for colors
  \NewDocElement[envlike,notoplevel]{Colors}{colors}

  %% Hyperlinks
  \usepackage{hypdoc}
  \hypersetup{%
    pdftitle={Guide to the package ullrcorners},
    pdfauthor={Vincent Goulet},
    colorlinks = true,
    linktocpage = true,
    urlcolor = {url},
    linkcolor = {link},
    citecolor = {citation},
    pdfpagemode = {UseOutlines},
    pdfstartview = {Fit}}

  %% Source code
  \usepackage{listings}
  \lstset{%
    basicstyle=\ttfamily,
    showstringspaces=false,
    backgroundcolor=\color{codebg},
    frame=leftline,
    framerule=2pt,
    framesep=5pt,
    xleftmargin=7.4pt
  }

  %% Package name
  \newcommand*\pkgname{ullrcorners}
  \newcommand*\ullrpkg{\pkg{\pkgname}}

  %% Redefine \year and define \isodate using information from
  %% \filedate instead of using compilation time
  \def\parsedate#1/#2/#3\relax{\def\year{#1}\def\isodate{#1-#2-#3}}
  \def\parseversion v#1.#2\relax{\def\version{#1.#2}}

  %% Utility macro
  \let\pkg\textbf

\begin{document}
  \DocInput{ullrcorners.dtx}
\end{document}
%</driver>
% \fi
% ^^A NOTE: The first three "versions" are in the project
% ^^A https://gitlab.com/vigou3/programmer-avec-r
% \changes{0.1}{2019-11-28}{First private version.}
% \changes{0.2}{2019-12-09}{Fix usage of the main macro at the
% beginning of a paragraph.}
% \changes{0.3}{2024-12-13}{Fix underfull horizontal boxes caused by
% the upper left corner.}
% \changes{1.0}{2025-05-16}{First public release.}
%
% \GetFileInfo{ullrcorners.dtx}
% \expandafter\parsedate\filedate\relax
% \expandafter\parseversion\fileversion\relax
%
% \title{{%
%   \setlength\ullrcornerslinewidth{1.02pt}
%   \setlength\ullrcornershlength{0.6ex}
%   \setlength\ullrcornersvlength{1.5\ullrcornershlength}
%   \setlength\ullrcornersvgap{\ullrcornerslinewidth}
%   \setlength\ullrcornershgap{\ullrcornersvgap}
%   \ullrcorners{ullrcorners}}}
% \author{Vincent Goulet \\
%   \href{mailto:vincent.goulet@act.ulaval.ca}{\url{vincent.goulet@act.ulaval.ca}}}
% \date{Version~\version, \isodate}
% \maketitle
%
% \section{Package features}
% \label{sec:features}
%
% {\ullrpkg} provides a command to surround a word or a sequence or
% words by small corners on the upper left and lower right sides, like
% \ullrcorners{this}. The typographical convention is inspired by
% \citet{Friedl:regex:2006}, where it is used to delimit regular
% expressions.
%
% \begin{DescribeMacro}{\ullrcorners}
%   The command |\ullrcorners| surrounds the word or sequence of words
%   in argument by corners. The example above is typeset using:
% \iffalse
%<*example>
% \fi
\begin{lstlisting}
\ullrcorners{this}
\end{lstlisting}
% \iffalse
%</example>
% \fi
% \end{DescribeMacro}
%
% \begin{DescribeColors}{ullrcornerscolor}
%   If, and only if, a color |ullrcornerscolor| is defined in the
%   preamble, the package will use this color instead of the default
%   text color to draw the corners. Therefore, the example above is
%   really obtained by loading \pkg{xcolor} with the option
%   |x11names|, and by defining the color in the preamble:
%   \iffalse
%<*example>
% \fi
\begin{lstlisting}
\colorlet{ullrcornerscolor}{Snow4}
\end{lstlisting}
% \iffalse
%</example>
% \fi
% \end{DescribeColors}
%
% That is pretty much all there is in terms of package features.
%
% \section{Customization}
% \label{sec:customization}
%
% You may customize the appearance of the corners by redefining the
% following lengths using |\setlength| (see \autoref{fig:parameters}
% for a visual summary):
% \begin{description}[font=\normalfont\ttfamily,noitemsep]
% \item[\ttfamily\string\ullrcornerslinewidth]
%   (default $0.8$~pt) \\
%   rule thickness of the corners;
% \item[\ttfamily\string\ullrcornershlength]
%   (default $0.6$~ex) \\
%   length of the horizontal rules;
% \item[\ttfamily\string\ullrcornersvlength]
%   (default $1.5$ times |\ullrcornershlength|) \\
%   length of the vertical rules;
% \item[\ttfamily\string\ullrcornersvgap]
%   (default |\ullrcornerslinewidth|) \\
%   space between the horizontal rules and the text;
% \item[\ttfamily\string\ullrcornershgap]
%   (default |\ullrcornersvgap|) \\
%   space between the vertical rules and the text.
% \end{description}
%
% The package does not provide support for asymetrical upper left and
% lower right corners.
%
% \begin{figure}
%   \centering
%   \setlength{\unitlength}{20mm}
%   ^^A picture pretty much to scale using 1mm = 2.84526pt and
%   ^^A 1ex = 4.71945pt = 1,65871mm in 11pt font size
%   ^^A (https://tex.stackexchange.com/a/8337)
%   \begin{picture}(5.25,2.5)(-2.5,0.5)
%     \small
%     ^^A text outline
%     \put(0,2){\line(1,0){2.5}}
%     \put(0,2){\line(0,-1){1.5}}
%
%     ^^A top and left rules
%     \put(-0.56234,2.28117){\rule{0.99523\unitlength}{0.28117\unitlength}}
%     \put(-0.56234,2.28117){\rule[-1.21168\unitlength]{0.28117\unitlength}{1.49285\unitlength}}
%
%     ^^A length of top rule
%     \multiput(-0.56234,2.61234)(0.99523,0){2}{\line(0,1){0.2}}
%     \Vector(-0.06473,2.71234)(-0.56234,2.71234)
%     \Vector(-0.06473,2.71234)(0.43289,2.71234)
%     \put(-0.06473,3.01234){\makebox(0,0){\texttt{\string\ullrcornershlength}}}
%     \Vector(-0.06473,2.91234)(-0.06473,2.71234)
%
%     ^^A width of top rule
%     \multiput(0.48289,2.28117)(0,0.28117){2}{\line(1,0){0.2}}
%     \put(0.58289,2.421755){\vector(0,-1){0.140585}}
%     \put(0.58289,2.421755){\vector(0,1){0.140585}}
%     \put(0.68289,2.421755){\makebox(0,0)[l]{\texttt{\string\ullrcornerslinewidth}}}
%
%     ^^A vertical gap
%     \put(0.58289,2.140585){\vector(0,-1){0.140585}}
%     \put(0.58289,2.140585){\vector(0,1){0.140585}}
%     \put(0.68289,2.140585){\makebox(0,0)[l]{\texttt{\string\ullrcornersvgap}}}
%
%     ^^A length of left rule
%     \multiput(-0.81234,2.28117)(0,-1.21168){2}{\line(1,0){0.2}}
%     \put(-0.71234,1.67533){\vector(0,-1){0.60584}}
%     \put(-0.71234,1.67533){\vector(0,1){0.60584}}
%     \put(-0.81234,1.67533){\makebox(0,0)[r]{\texttt{\string\ullrcornersvlength}}}
%
%     ^^A horizontal gap
%     \put(-0.28117,1,01949){\line(0,-1){0.2}}
%     \put(-0.140585,0.91949){\vector(1,0){ 0.140585}}
%     \put(-0.140585,0.91949){\vector(-1,0){0.140585}}
%     \put(-0.5321755,0.71949){\makebox(0,0)[r]{\texttt{\string\ullrcornershgap}}}
%     \Vector(-0.140585,0.81949)(-0.140585,0.91949)
%     \qbezier(-0.140585,0.81949)(-0.140585,0.71949)(-0.240585,0.71949)
%     \Line(-0.4321755,0.71949)(-0.240585,0.71949)
%   \end{picture}
%   \caption{Parameters of the corners (shown here for the upper left
%     corner)}
%   \label{fig:parameters}
% \end{figure}
%
% \MaybeStop{%
%   \begin{thebibliography}{1}
%   \bibitem[{Friedl(2006)}]{Friedl:regex:2006}
%   Friedl, J. E.~F. (2006).
%   \newblock \emph{Mastering Regular Expressions}.
%   \newblock 3rd ed. O'Reilly {M}edia.
%   \newblock ISBN 0-59652812-4.
%   \end{thebibliography}
%   \PrintChanges
% }
%
% \appendix
%
% ^^A >>>>> Start of the code of the package
%% \section{Implementation}
% \label{sec:implementation}
%
% First, we define the lengths needed by the package and set their
% default values.
%
%\iffalse
%<*package>
%\fi
%    \begin{macrocode}
\newlength\ullrcornerslinewidth
\newlength\ullrcornershlength
\newlength\ullrcornersvlength
\newlength\ullrcornersvgap
\newlength\ullrcornershgap
\setlength\ullrcornerslinewidth{0.8pt}
\setlength\ullrcornershlength{0.6ex}
\setlength\ullrcornersvlength{1.5\ullrcornershlength}
\setlength\ullrcornersvgap{\ullrcornerslinewidth}
\setlength\ullrcornershgap{\ullrcornersvgap}
%    \end{macrocode}
%
% Second, we will need a command to set the color of the corners. By
% default, this command does nothing. However, if a color
% |ullrcornerscolor| is defined at the beginning of the document, then
% the command is redefined as a call to |\color| (this assumes that a
% color package is loaded).
% \changes{1.0}{2025-05-16}{Implementation: the main macro now has
% only one argument and uses the color \texttt{ullrcornerscolor} if it
% is defined in the preamble.}
%    \begin{macrocode}
\newcommand*\ullrc@color{\relax}
\AtBeginDocument{%
  \@ifundefined{\string\color@ullrcornerscolor}{}{%
    \renewcommand\ullrc@color{\color{ullrcornerscolor}}}}
%    \end{macrocode}
%
% Finally, we define the robust user command |\ullrcorners|. It draws
% the corners using {\TeX} rules and boxes.
% \changes{1.0}{2025-05-16}{Implementation: fix boxes such that the
% horizontal space between the text and the corners really is
% \cs{ullrcornershgap} without hacks.}
% \changes{1.0}{2025-05-16}{Implementation: declare the main macro as
% a robust command.}
%    \begin{macrocode}
\DeclareRobustCommand{\ullrcorners}[1]{%
  \setbox\z@\hbox{\kern\ullrcornershgap #1\kern\ullrcornershgap}%
  \dimen\z@=\dimexpr\ullrcornerslinewidth+\ullrcornersvgap+\dp\z@
  \setbox\tw@\hbox to\dimexpr\wd\z@+2\ullrcornerslinewidth{%
    \hfil%
    \vbox{%
      \ullrc@color%
      \hbox to\ullrcornershlength{%
        \hfil\vrule \@height\ullrcornersvlength \@width\ullrcornerslinewidth%
      }%
      \hrule \@height\ullrcornerslinewidth
    }%
  }%
  \dimen\tw@=\dimexpr\ullrcornersvgap-\ullrcornersvlength
  \leavevmode%
  \vbox{%
    \offinterlineskip
    \vbox{%
      \ullrc@color%
      \hrule \@height\ullrcornerslinewidth
      \hbox to\ullrcornershlength{%
        \vrule \@height\ullrcornersvlength \@width\ullrcornerslinewidth\hfil
      }%
    }
    \kern\dimen\tw@
    \hbox{\kern\ullrcornerslinewidth\box\z@}%
    \kern\dimen\tw@
    \hbox{\raise -\dimen\z@\box\tw@}%
  }%
}
%    \end{macrocode}
% \iffalse
%</package>
%\fi
%
% \Finale
\endinput