%% This is part of the OpTeX project, see http://petr.olsak.net/optex

\_codedecl \margins {Macros for margins setting <2021-03-15>} % preloaded in format

    \_doc ----------------------------
    \`\margins``/<pg> <fmt> (<left>,<right>,<top>,<bot>)<unit>`
    takes its parameters, does calculation and sets `\hoffset`, `\voffset`,
    `\hsize` and `\vsize` registers. Note that \OpTeX/ sets the page origin at
    the top left corner of the paper, no at the obscure position 1\,in, 1\,in.
    It is much more comfortable for macro writers.
    \_cod ----------------------------

\_newdimen\_pgwidth  \_newdimen\_pgheight  \_pgwidth=0pt
\_newdimen\_shiftoffset

\_def\_margins/#1 #2 (#3,#4,#5,#6)#7 {\_def\_tmp{#7}%
   \_ifx\_tmp\_empty
      \_opwarning{\_string\_margins: missing unit, mm inserted}\_def\_tmp{mm}\_fi
   \_setpagedimens #2 % setting \_pgwidth, \_pgheight
   \_ifdim\_pgwidth=0pt \_else
      \_hoffset=0pt \_voffset=0pt
      \_if$#3$\_if$#4$\_hoffset =\_dimexpr (\_pgwidth -\_hsize)/2 \_relax
              \_else  \_hoffset =\_dimexpr \_pgwidth -\_hsize - #4\_tmp \_relax % only right margin
              \_fi
      \_else  \_if$#4$\_hoffset = #3\_tmp \_relax  % only left margin
              \_else  \_hsize =\_dimexpr \_pgwidth - #3\_tmp - #4\_tmp \_relax % left+right margin
                      \_hoffset = #3\_tmp \_relax
                      \_xhsize =\_hsize \_setxhsize % \_xhsize used by \output routine
      \_fi\_fi
      \_if$#5$\_if$#6$\_voffset =\_dimexpr (\_pgheight -\_vsize)/2 \_relax
              \_else  \_voffset =\_dimexpr \_pgheight -\_vsize - #6\_tmp \_relax % only bottom margin
              \_fi
      \_else  \_if$#6$\_voffset = #5\_tmp \_relax  % only top margin
              \_else  \_vsize=\_dimexpr \_pgheight - #5\_tmp - #6\_tmp \_relax % top+bottom margin
                      \_voffset = #5\_tmp \_relax
      \_fi\_fi
      \_if 1#1\_shiftoffset=0pt \_def\_prepoffsets{}\_else \_if 2#1% double-page layout
         \_shiftoffset = \_dimexpr \_pgwidth -\_hsize -2\_hoffset \_relax
         \_def\_prepoffsets{\_ifodd\_pageno \_else \_advance\_hoffset \_shiftoffset \_fi}%
      \_else \_opwarning{use \_string\_margins/1 or \_string\_margins/2}%
   \_fi\_fi\_fi
}
\_def\_setpagedimens{\_isnextchar({\_setpagedimensB}{\_setpagedimensA}}
\_def\_setpagedimensA#1 {\_ifcsname _pgs:#1\_endcsname
   \_ea\_ea\_ea\_setpagedimensB \_csname _pgs:#1\_ea\_endcsname\_space
   \_else \_opwarning{page specification "#1" is undefined}\_fi}
\_def\_setpagedimensB (#1,#2)#3 {\_setpagedimensC\_pgwidth=#1:#3
                                 \_setpagedimensC\_pgheight=#2:#3
        \_pdfpagewidth=\_pgwidth \_pdfpageheight=\_pgheight
}
\_def\_setpagedimensC #1=#2:#3 {#1=#2\_ifx^#3^\_tmp\_else#3\_fi\_relax\_truedimen#1}

\_public \margins ;

    \_doc ----------------------------
    The common page dimensions are defined here.
    \_cod ----------------------------

\_sdef{_pgs:a3}{(297,420)mm}  \_sdef{_pgs:a4}{(210,297)mm}  \_sdef{_pgs:a5}{(148,210)mm}
\_sdef{_pgs:a3l}{(420,297)mm} \_sdef{_pgs:a4l}{(297,210)mm} \_sdef{_pgs:a5l}{(210,148)mm}
\_sdef{_pgs:b5}{(176,250)mm}  \_sdef{_pgs:letter}{(8.5,11)in}

    \_doc ----------------------------
    \`\magscale` `[<factor>]` does `\mag=<factor>` and recalculates page
    dimensions to their true values.
    \_cod ----------------------------

\_def\_trueunit{}
\_def\_magscale[#1]{\_mag=#1\_def\_trueunit{true}%
   \_ifdim\_pgwidth=0pt \_else \_truedimen\_pgwidth \_truedimen\_pgheight \_fi
   \_truedimen\_pdfpagewidth \_truedimen\_pdfpageheight
}
\_def\_truedimen#1{\_ifx\_trueunit\_empty \_else#1=\_ea\_ignorept\_the#1truept \_fi}

\_public \magscale ;

\_endcode % -----------------------------------------

2021-03-15 \_setxhsize added
2020-03-14 released
