% Calculate LaTeX paper and margin settings for arbitrary magnification
% (C) Silas S. Brown 2026, Version 1.69

% Please see the documentation file for more information.

%% Licensed under the Apache License, Version 2.0 (the ``License'');
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at

%%     http://www.apache.org/licenses/LICENSE-2.0

%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an ``AS IS'' BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{latex-papersize}
\RequirePackage{expl3,l3keys2e,iftex}
\ExplSyntaxOn

\keys_define:nn { latex-papersize }
  {
    basesize    .fp_set:N = \basesize_fp,
    basesize    .initial:n = 10,
    desiredsize .fp_set:N = \desiredsize_fp,
    desiredsize .initial:n = 26,
    paperwidth  .dim_set:N = \paperwidth_dim,
    paperwidth  .initial:n = 210mm,
    paperheight .dim_set:N = \paperheight_dim,
    paperheight .initial:n = 297mm,
    marginleft  .dim_set:N = \marginleft_dim,
    marginleft  .initial:n = 10mm,
    margintop   .dim_set:N = \margintop_dim,
    margintop   .initial:n = 10mm,
    extrabottom .dim_set:N = \extrabottom_dim,
    extrabottom .initial:n = 0pt}
\ProcessKeysOptions { latex-papersize }
\fp_set:Nn \magnification_fp { \desiredsize_fp / \basesize_fp }
\ifdefined\pdfoutput\ifnum\pdfoutput=0\PackageError{latex-papersize}{DVI~output~mode~is~not~supported\MessageBreak~by~latex-papersize.sty.\MessageBreak~This~package~requires~PDF~output\MessageBreak~(pdflatex,~lualatex,~or~xelatex).~\MessageBreak~For~DVI/dvips~workflows,~please~use~the~older~latex-papersize~Python~script}\fi\fi

\fp_set:Nn \realPaperwidth_fp { \dim_to_decimal_in_unit:nn { \paperwidth_dim } { 1mm } }
\fp_set:Nn \realPaperheight_fp { \dim_to_decimal_in_unit:nn { \paperheight_dim } { 1mm } }
\fp_set:Nn \squashedMarginleft_fp { \dim_to_decimal_in_unit:nn { \marginleft_dim } { 1mm } / \magnification_fp }
\fp_set:Nn \squashedMargintop_fp { \dim_to_decimal_in_unit:nn { \margintop_dim } { 1mm } / \magnification_fp }
\fp_set:Nn \realExtrabottom_fp { \dim_to_decimal_in_unit:nn { \extrabottom_dim } { 1mm } }
\fp_set:Nn \squashedTextwidth_fp  { \realPaperwidth_fp / \magnification_fp - 2 * \squashedMarginleft_fp }
\fp_set:Nn \squashedTextheight_fp { \realPaperheight_fp / \magnification_fp - 2 * \squashedMargintop_fp - \realExtrabottom_fp } % use realExtrabottom_fp not squashed because it corresponds to the height of the textual page-number which will be magnified

\cs_new:Npn \mfs_mm_to_dim:n #1 { \dim_eval:n { \fp_use:N #1 mm } }
\dim_set:Nn \textwidth { \mfs_mm_to_dim:n { \squashedTextwidth_fp } }
\dim_set:Nn \textheight { \mfs_mm_to_dim:n { \squashedTextheight_fp } }
\dim_set:Nn \topmargin { \mfs_mm_to_dim:n { \squashedMargintop_fp } }
\dim_set:Nn \marginparwidth { 0mm }
\dim_set:Nn \oddsidemargin { \mfs_mm_to_dim:n { \squashedMarginleft_fp } }
\dim_set:Nn \evensidemargin { \mfs_mm_to_dim:n { \squashedMarginleft_fp } }
\dim_set:Nn \columnsep { \mfs_mm_to_dim:n { \squashedMarginleft_fp } }

\fp_compare:nNnTF{\realExtrabottom_fp} > {0}{}{\pagestyle{empty}}

\int_gset_eq:NN \mag { \fp_eval:n { round(1000 * \magnification_fp, 0) } }

\fp_set:Nn \pdfvorigin_fp { -12.95 * \magnification_fp }
\fp_set:Nn \xvorigin_fp { \pdfvorigin_fp - 25.4 }
\ifPDFTeX
\pdfvorigin    = \fp_use:N \pdfvorigin_fp true mm
\pdfhorigin    = 0 true mm
\pdfpagewidth  = \fp_use:N \realPaperwidth_fp true mm
\pdfpageheight = \fp_use:N \realPaperheight_fp true mm
\fi\ifLuaTeX
\pdfvariable vorigin    = \fp_use:N \pdfvorigin_fp true mm
\pdfvariable horigin    = 0 true mm
\pagewidth  = \fp_use:N \realPaperwidth_fp true mm
\pageheight = \fp_use:N \realPaperheight_fp true mm
\fi\ifXeTeX
\voffset    = \fp_use:N \xvorigin_fp true mm
\hoffset    = -1 true in
\pdfpagewidth  = \fp_use:N \realPaperwidth_fp true mm
\pdfpageheight = \fp_use:N \realPaperheight_fp true mm
\fi
\paperwidth    = \fp_use:N \realPaperwidth_fp true mm
\paperheight   = \fp_use:N \realPaperheight_fp true mm

\ExplSyntaxOff

\endinput
