%%
%% This is file `swfigure.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% swfigure.dtx  (with options: `package')
%% 
%%   Copyright (C)  2020 Claudio Beccari  all rights reserved.
%%   License information appended
%% 
\NeedsTeXFormat{LaTeX2e}[2020/01/01]
\ProvidesPackage{swfigure}%
        [2020-10-11 v.0.9.07 Managing large and spread wide figures]
\RequirePackage{etoolbox}
\RequirePackage{xparse}
\RequirePackage{xfp}

\ExplSyntaxOn
\AtBeginDocument{%
\ProvideExpandableDocumentCommand\fptest{m m m}{%
   \fp_compare:nTF{#1}{#2}{#3}}
}
\ExplSyntaxOff

\RequirePackage{trace}

\RequirePackage{graphicx}
\RequirePackage{afterpage}
\RequirePackage{wrapfig}

\newdimen\internalmargin
\internalmargin=\dimexpr\oddsidemargin+1in+1bp\relax
\newdimen\spreadwidth
\spreadwidth=\dimexpr 2\textwidth+2\internalmargin\relax
\newdimen\DFwidth
\newdimen\DFheight
\newdimen\DFhalfwidth
\newdimen\DFheight
\newdimen\FigSpace
\newsavebox\DFtotalimage
\newsavebox\DFimageI
\newsavebox\DFimageII
\newsavebox\RFbox
\newdimen\VS@textwidth
\newcount\VS@lines

\NewDocumentCommand\DFcaption{O{#2} m o}{\refstepcounter{figure}%
  \vtop to 0pt{\hsize=\textheight\parindent=0pt\leavevmode
  Figure \thefigure\quad #2\vss}%
  \addcontentsline{lof}{figure}{\protect\numberline{\thefigure}#1}
  \IfValueT{#3}{\label{#3}}\relax%
}

\DeclareRobustCommand\cleartoevenpage{%
  \clearpage\ifodd\value{page}\null\clearpage\fi}
\NewDocumentEnvironment{DFimage}{O{SW} m O{#4} m o D(){0.8} D<>{0}}{%
\ifstrequal{#1}{SW}%
  {%
    \SWfigure{#2}[#3]{#4}[#5]%
  }%
  {\ifstrequal{#1}{NF}%
    {%
      \NFfigure{#2}[#3]{#4}[#5](#6)%
    }%
    {\ifstrequal{#1}{RF}% OK
      {%
        \RFfigure{#2}[#3]{#4}[#5]%
      }%
      {\ifstrequal{#1}{VS}%
        {%
          \VSfigure{#2}[#3]{#4}[#5](#6)<#7>%
        }%
        {\ifstrequal{#1}{HS}
          {%
          \HSfigure{#2}[#3]{#4}[#5](#6)%
         }%
          {\PackageWarning{swfigure}%
          {%
            **************************************\MessageBreak
            Option #1\space is not valid           \MessageBreak
            Nothing done                           \MessageBreak
            Image #2\space was not processed       \MessageBreak
            **************************************\MessageBreak}%
          }%
        }%
      }%
    }%
  }%
}%

\NewDocumentCommand\SWfigure{m O{#3} m o}{%
\unless\ifvmode
  \csuse{@bsphack}%
  \vadjust{\vspace{0pt plus1fill}\csuse{@esphack}\newpage}%
\fi
\cleartoevenpage
\setbox\DFtotalimage=\hbox{\includegraphics{#1}}%
\DFwidth=\wd\DFtotalimage \DFhalfwidth=0.5\DFwidth
\DFheight=\ht\DFtotalimage
\FigSpace=\dimexpr\textwidth+\internalmargin\relax
\setbox\DFimageI\hbox{\bgroup
\edef\x{\egroup\noexpand\includegraphics*[%
trim = 0 0 \the\DFhalfwidth\space 0]}\x{#1}}%
\setbox\DFimageII\hbox{\bgroup
\edef\x{\egroup\noexpand\includegraphics*[%
trim = \the\DFhalfwidth\space 0 0 0]}\x{#1}}%
\fptest{\DFheight/\DFhalfwidth > \textheight/\FigSpace}%
  {\edef\DFscalefactor{\fpeval{\textheight/\DFheight}}}%
  {\edef\DFscalefactor{\fpeval{\FigSpace/\DFhalfwidth}}}%
\setbox\DFimageI=\hbox{\scalebox{\DFscalefactor}{\usebox{\DFimageI}}}%
\setbox\DFimageII=\hbox{\scalebox{\DFscalefactor}{\usebox{\DFimageII}}}%
\begin{figure}[p]%
\vbox to\textheight{\vss\hsize=\textwidth%\
\makebox[\hsize][l]{\makebox[\FigSpace][r]{\box\DFimageI}}\vss}%
\end{figure}\clearpage
\begin{figure}[p]%
\vbox to\textheight{\vss\hsize=\textwidth
\makebox[\hsize][r]{\makebox[\FigSpace][l]{\box\DFimageII}%
\makebox(0,0)[lb]{\hspace*{1em}\raisebox{0.5\textheight}{%
\rotatebox[origin=tc]{90}{\DFcaption[#2]{#3}[#4]%
}}}}\vss}%
\end{figure}\clearpage}

\NewDocumentCommand\NFfigure{m O{#3} m o d()}{%
  \begin{figure}[p]
    \includegraphics[width=\linewidth,height=#5\textheight,
                     keepaspectratio]{#1}%
    \caption[#2]{#3}%
    \IfValueT{#4}{\label{#4}}\relax
  \end{figure}
}

\NewDocumentCommand\RFfigure{m O{#3} m o}{%
      \dimen8=\textwidth\dimen10=\textheight
      \figure[p]\setbox\RFbox=\hbox{%
      \rotatebox[origin=cc]{90}{\parbox[b][\dimen8][c]{\dimen10}%
        {\centering\includegraphics[width=\dimen10, height=\dimen8,
                        keepaspectratio]{#1}%
          \caption[#2]{#3}%
          \IfValueT{#4}{\label{#4}}\relax%
        }}}%
        \edef\RFx{\fpeval{\ht\RFbox/\textheight}}%
        \edef\RFy{\fpeval{\wd\RFbox/\textwidth}}%
        \fptest{\RFx > \RFy}%
          {\scalebox{\RFx}{\box\RFbox}}%
          {\scalebox{\RFy}{\box\RFbox}}%
        \endfigure}

\NewDocumentCommand\VSfigure{m O{#3} m o d() D<>{0}}{%
   \setbox\DFtotalimage=\hbox{\includegraphics{#1}}
   \DFwidth=\wd\DFtotalimage \DFheight=\ht\DFtotalimage
   \edef\VS@aspectratio{\fpeval{\DFheight/\DFwidth}}
   \fptest{ \VS@aspectratio < 2 }%
     {\begin{center}\ttfamily
      ===========================================\\
      Figure #2 is not tall enough.\\
      Consider using a display mode different\\
      from VS; may be NF or RF are better suited.\\
      ===========================================\\
      Nothing done!\\
      ===========================================
      \end{center}
     }%
     {\edef\VS@factor{\fpeval{#5\textheight/\DFheight}}%
   \setbox\DFtotalimage=
     \hbox{\scalebox{\VS@factor}{\box\DFtotalimage}}%
   \edef\VS@width{\fpeval{\VS@factor*\DFwidth}\p@}%
   \setbox\DFtotalimage=\vbox{\hsize=\VS@width%
   \def\@captype{figure}\box\DFtotalimage
   \caption[#2]{#3}%
   \IfValueT{#4}{\label{#4}}\relax}
   \VS@lines=
   \fpeval{round(\ht\DFtotalimage/\baselineskip,0)+#6}%
   \begin{wrapfigure}[\VS@lines]{O}[0pt]{\VS@width}%
   \box\DFtotalimage
   \end{wrapfigure}}%
}

\NewDocumentCommand\HSfigure{m o m o d()}{%
  \unless\ifvmode
    \csuse{@bsphack}%
    \vadjust{\vspace{0pt plus1fill}\csuse{@esphack}\newpage}%
  \fi
  \cleartoevenpage
  \setbox\DFtotalimage=\hbox{\includegraphics{#1}}
  \DFwidth=\wd\DFtotalimage \DFhalfwidth=0.5\DFwidth
  \FigSpace=0.5\spreadwidth% W
  \setbox\DFimageI\hbox{\bgroup
  \edef\x{\egroup\noexpand\includegraphics*[%
          trim = 0 0 \the\DFhalfwidth\space 0]}\x{#1}}%
  \setbox\DFimageII\hbox{\bgroup
  \edef\x{\egroup\noexpand\includegraphics*[%
         trim = \the\DFhalfwidth\space 0 0 0]}\x{#1}}%
  \edef\DFscalefactor{\fpeval{\FigSpace/\DFhalfwidth}}%
  \setbox\DFimageI=\hbox{%
    \scalebox{\DFscalefactor}{\usebox{\DFimageI}}}%
  \setbox\DFimageII=\hbox{%
    \scalebox{\DFscalefactor}{\usebox{\DFimageII}}}%
  \setbox\DFimageII=
    \hbox{\dimen10=\linewidth\dimen8\internalmargin
           \vbox{\hsize\DFhalfwidth\parindent\z@
             \box\DFimageII\par
             \leavevmode\hspace*{\dimen8}%
             \vtop{\hsize\dimen10\parindent\z@
               \textwidth=\hsize
               \DFcaption[#2]{#3}[#4]%
              }%
            }%
          }%
  \setbox\DFimageI=\vbox to\ht\DFimageII{\box\DFimageI\vss}%
  \begin{figure}[t]
  \makebox[\textwidth][l]{%
    \makebox[\DFhalfwidth][l]{\box\DFimageI}}%
  \end{figure}
  \afterpage{%
    \begin{figure}[t]
    \makebox[\textwidth][r]{%
       \makebox[\DFhalfwidth][r]{\box\DFimageII}}%
    \end{figure}%
  }
}
%% 
%% Distributable under the LaTeX Project Public License,
%% version 1.3c or higher (your choice). The latest version of
%% this license is at: http://www.latex-project.org/lppl.txt
%% 
%% This work is "maintained"
%% 
%% This work consists of files swfigure.dtx and READEME.txt, and the derived
%% files swfigure.sty and swfigure.pdf
%% 
%%
%% End of file `swfigure.sty'.
