% metaphor.sty    1.0  2026/03/25
% Andrew Lounsbury

%************************************************************************
%% metaphor.sty
%% Copyright 2026 A. W. Lounsbury
%
% 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.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is A. W. Lounsbury.
%
% This work consists of the file metaphor.sty. 
%************************************************************************
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{metaphor}[2026/03/25 1.0 conceptual metaphor typesetting]
\RequirePackage{
    imakeidx, kvoptions, tabularx
}
\SetupKeyvalOptions{
  family=metaphor,
  prefix=metaphor@
}
\DeclareBoolOption{borders}
\DeclareBoolOption{index}
\DeclareBoolOption{small}
\DeclareBoolOption{footnotesize}
\DeclareBoolOption{xcols}
\DeclareStringOption[1.0]{rowheight} % default 1.0
\DeclareStringOption[0.4pt]{thickness} % default 0.4pt
\DeclareStringOption[Index of Metaphors and Metonyms]{indextitle} % default 0.4pt
\ProcessKeyvalOptions*
\ifmetaphor@index
    \makeindex[name=metaphors,title=\metaphor@indextitle]
\fi
\newlength{\metaphor@maxleft}
\newlength{\metaphor@maxright}
\newlength{\metaphor@temp}
\newcommand{\gsetlength}[2]{\global#1=#2\relax}
\newcommand{\metaphor@map@measure}[2]{%
  \settowidth{\metaphor@temp}{%
    \ifmetaphor@small\small\else\ifmetaphor@footnotesize\footnotesize\fi\fi #1%
  }%
  \ifdim\metaphor@temp>\metaphor@maxleft 
    \gsetlength{\metaphor@maxleft}{\metaphor@temp}% 
  \fi
  \settowidth{\metaphor@temp}{%
    \ifmetaphor@small\small\else\ifmetaphor@footnotesize\footnotesize\fi\fi #2%
  }%
  \ifdim\metaphor@temp>\metaphor@maxright 
    \gsetlength{\metaphor@maxright}{\metaphor@temp}% 
  \fi
}
\newcommand{\metaphor@map@typeset}[2]{%
  \ifmetaphor@small{\small #1}\else\ifmetaphor@footnotesize{\footnotesize #1}\else #1\fi\fi 
  & $\longrightarrow$ & 
  \ifmetaphor@small{\small #2}\else\ifmetaphor@footnotesize{\footnotesize #2}\else #2\fi\fi \\
  \ifmetaphor@borders \hline \fi
}
\NewDocumentEnvironment{metaphormapping}{O{1.0} m m +b}
{%
  \par\bigskip
  \centering
  \global\setlength{\metaphor@maxleft}{0pt}
  \global\setlength{\metaphor@maxright}{0pt}
  
  \ifmetaphor@xcols \else
  \setbox0=\vbox{%
    \let\map\metaphor@map@measure
    #4
  }%
  \fi
  
  \renewcommand{\arraystretch}{\metaphor@rowheight}
  \let\map\metaphor@map@typeset
  
  \ifmetaphor@borders
    \setlength{\arrayrulewidth}{\metaphor@thickness}
    \ifmetaphor@xcols
        \begin{tabularx}{#1\textwidth}{|X c X|}
            \hline
            \scshape #2 & & \scshape #3 \\\hline
            #4
        \end{tabularx}
    \else
        \begin{tabular}{|p{\metaphor@maxleft} c p{\metaphor@maxright}|}
          \hline  
          \scshape #2 & & \scshape #3 \\\hline
            #4
        \end{tabular}
    \fi
  \else
    \ifmetaphor@xcols
        \begin{tabularx}{#1\textwidth}{X c X}
            \scshape #2 & & \scshape #3 \\
            #4
        \end{tabularx}
    \else
        \begin{tabular}{p{\metaphor@maxleft} c p{\metaphor@maxright}}
            \scshape #2 & & \scshape #3 \\
            #4
        \end{tabular}
    \fi
  \fi
  \par\bigskip
}{}
\NewDocumentCommand{\metaphor}{m}{%
    \ifmetaphor@index%
        \index[metaphors]{\textsc{#1}}%
        \textsc{#1}%
    \else
        \textsc{#1}%
    \fi
}
\NewDocumentCommand{\metonym}{m}{%
    \ifmetaphor@index%
        \index[metaphors]{\textsc{#1}}%
        \textsc{#1}%
    \else
        \textsc{#1}%
    \fi
}
