%
% Copyright (C) 2018, 2019 by Ruixi Zhang <ruixizhang42@gmail.com>
%
% 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
%    https://www.latex-project.org/lppl.txt
% and version 1.3c 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 Ruixi Zhang.
%
% This work consists of the files zhlineskip.sty,
%                                 zhlineskip.tex,
%                                 zhlineskip-test.tex,
%                                 Latinmetrics.pdf,
%                                 CJKmetrics.pdf,
%                                 README.md
%           and the derived file  zhlineskip.pdf.
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{zhlineskip}[%
  2019/05/15 v1.0e Line spacing for CJK documents]

\RequirePackage{kvoptions}
\RequirePackage{xintexpr}
\RequirePackage{etoolbox}

\SetupKeyvalOptions{
  family=ZhLS,
  prefix=ZhLS@
}
\DeclareStringOption[1.5]{bodytextleadingratio}[1.5]
\DeclareStringOption[1.48]{footnoteleadingratio}[1.48]
\DeclareBoolOption[true]{restoremathleading}
\DeclareBoolOption[false]{UseMSWordMultipleLineSpacing}
\DeclareStringOption[1.15]{MSWordLineSpacingMultiple}[1.15]
\DeclareStringOption[1.296875]{MSWordSinglespaceRatio}[1.296875]
\DeclareDefaultOption{%
  \PackageError{\@currname}{%
    Unknown option `\CurrentOption'%
  }{%
    Available package options are:\MessageBreak
    \space\space`bodytextleadingratio',\MessageBreak
    \space\space`footnoteleadingratio',\MessageBreak
    \space\space`restoremathleading',\MessageBreak
    \space\space`UseMSWordMultipleLineSpacing',\MessageBreak
    \space\space`MSWordLineSpacingMultiple',\MessageBreak
    \space\space`MSWordSinglespaceRatio'%
  }%
}
\ProcessKeyvalOptions*

\begingroup
  \normalsize
  \@tempdima\f@size\p@
  \@tempdimb\f@baselineskip
  \edef\@tempnuma{\the\numexpr\@tempdima\relax}%
  \ifZhLS@UseMSWordMultipleLineSpacing
    \PackageWarningNoLine{\@currname}{%
      Use Microsoft Word multiple line spacing\MessageBreak
      Singlespace to fontsize ratio = `\ZhLS@MSWordSinglespaceRatio'\MessageBreak
      Multiple = `\ZhLS@MSWordLineSpacingMultiple'%
    }%
    \xdef\ZhLS@targetbodyleading{%
      \@tempnuma*\ZhLS@MSWordSinglespaceRatio*\ZhLS@MSWordLineSpacingMultiple
    }%
  \else
    \xdef\ZhLS@targetbodyleading{%
      \@tempnuma*\ZhLS@bodytextleadingratio
    }%
  \fi
  \xdef\ZhLS@defaultbodyleading{\the\numexpr\@tempdimb\relax}%
\endgroup
\begingroup
  \footnotesize
  \@tempdima\f@size\p@
  \@tempdimb\f@baselineskip
  \edef\@tempnuma{\the\numexpr\@tempdima\relax}%
  \ifZhLS@UseMSWordMultipleLineSpacing
    \xdef\ZhLS@targetfootleading{%
      \@tempnuma*\ZhLS@MSWordSinglespaceRatio*\ZhLS@MSWordLineSpacingMultiple
    }%
  \else
    \xdef\ZhLS@targetfootleading{%
      \@tempnuma*\ZhLS@footnoteleadingratio
    }%
  \fi
  \xdef\ZhLS@defaultfootleading{\the\numexpr\@tempdimb\relax}%
\endgroup
\edef\ZhLS@bodylinespread{%
  \xintthefloatexpr\xintexpr
    \ZhLS@targetbodyleading/\ZhLS@defaultbodyleading\relax\relax
}
\edef\ZhLS@footlinespread{%
  \xintthefloatexpr\xintexpr
    \ZhLS@targetfootleading/\ZhLS@defaultfootleading\relax\relax
}

\AtBeginDocument{%
  \linespread{\ZhLS@bodylinespread}\selectfont
}

\patchcmd\@footnotetext
  {\reset@font}
  {\def\baselinestretch{\ZhLS@footlinespread}\reset@font}
  {}{}
\patchcmd\@mpfootnotetext
  {\reset@font}
  {\def\baselinestretch{\ZhLS@footlinespread}\reset@font}
  {}{}
\def\ZhLS@resetfootnotesep#1{%
  \begingroup
    \footnotesize
    \linespread{#1}\selectfont
    \global\footnotesep=\ht\strutbox
  \endgroup
}
\ZhLS@resetfootnotesep{\ZhLS@footlinespread}

\newcommand*\SetTextEnvironmentSinglespace[1]{%
  \def\ZhLS@textlinespread{#1}%
}
\SetTextEnvironmentSinglespace{1}
\newcommand*\RestoreTextEnvironmentLeading[1]{%
  \forcsvlist\ZhLS@restoretextenvironmentleading{#1}%
}
\def\ZhLS@restoretextenvironmentleading#1{%
  \AtBeginEnvironment{#1}{%
    \linespread{\ZhLS@textlinespread}\selectfont\ignorespaces
  }%
}

\ifZhLS@restoremathleading
  \RequirePackage{mathtools}
  \newcommand*\SetMathEnvironmentSinglespace[1]{%
    \def\ZhLS@mathlinespread{#1}%
  }
  \SetMathEnvironmentSinglespace{1}
  \newcommand*\RestoreMathEnvironmentLeading[1]{%
    \forcsvlist\ZhLS@restoremathenvironmentleading{#1}%
  }
  \def\ZhLS@restoremathenvironmentleading#1{%
    \AtBeginEnvironment{#1}{%
      \linespread{\ZhLS@mathlinespread}\selectfont\ignorespaces
    }%
  }
  \RestoreMathEnvironmentLeading{array}
  \RestoreMathEnvironmentLeading{matrix}
  \RestoreMathEnvironmentLeading{pmatrix}
  \RestoreMathEnvironmentLeading{bmatrix}
  \RestoreMathEnvironmentLeading{Bmatrix}
  \RestoreMathEnvironmentLeading{vmatrix}
  \RestoreMathEnvironmentLeading{Vmatrix}
  \RestoreMathEnvironmentLeading{cases}
  \RestoreMathEnvironmentLeading{aligned}
  \RestoreMathEnvironmentLeading{alignedat}
  \RestoreMathEnvironmentLeading{gathered}
  \patchcmd\start@gather
    {\collect@body}
    {\linespread{\ZhLS@mathlinespread}\selectfont\collect@body}
    {}{}
  \patchcmd\start@align
    {\collect@body}
    {\linespread{\ZhLS@mathlinespread}\selectfont\collect@body}
    {}{}
  \patchcmd\start@multline
    {\collect@body}
    {\linespread{\ZhLS@mathlinespread}\selectfont\collect@body}
    {}{}
  \patchcmd\gather@split
    {\spread@equation}
    {\linespread{\ZhLS@mathlinespread}\selectfont\spread@equation}
    {}{}
  \RestoreMathEnvironmentLeading{matrix*}
  \RestoreMathEnvironmentLeading{pmatrix*}
  \RestoreMathEnvironmentLeading{bmatrix*}
  \RestoreMathEnvironmentLeading{Bmatrix*}
  \RestoreMathEnvironmentLeading{vmatrix*}
  \RestoreMathEnvironmentLeading{Vmatrix*}
  \RestoreMathEnvironmentLeading{cases*}
  \RestoreMathEnvironmentLeading{dcases}
  \RestoreMathEnvironmentLeading{dcases*}
  \RestoreMathEnvironmentLeading{rcases}
  \RestoreMathEnvironmentLeading{rcases*}
  \RestoreMathEnvironmentLeading{drcases}
  \RestoreMathEnvironmentLeading{drcases*}
  \RestoreMathEnvironmentLeading{multlined}
  \RestoreMathEnvironmentLeading{lgathered}
  \RestoreMathEnvironmentLeading{rgathered}
\else
  \PackageWarningNoLine{\@currname}{%
    mathtools is NOT loaded by zhlineskip\MessageBreak
    Some of its features may not be available\MessageBreak
    Leading in multi-line math will be stretched%
  }%
  \newcommand*\SetMathEnvironmentSinglespace[1]{%
    \PackageError{zhlineskip}{%
      Cannot use \string\SetMathEnvironmentSinglespace\space here%
    }{%
      Try loading zhlineskip with option\MessageBreak
      \space\space`restoremathleading=true'%
    }%
  }
  \newcommand*\RestoreMathEnvironmentLeading[1]{%
    \PackageError{zhlineskip}{%
      Cannot use \string\RestoreMathEnvironmentLeading\space here%
    }{%
      Try loading zhlineskip with option\MessageBreak
      \space\space`restoremathleading=true'%
    }%
  }
\fi

\endinput
