summaryrefslogtreecommitdiff
path: root/macros/optex/base/margins.opm
blob: 0f544fc5dd6b80e36e5f4b317f01a03b75f6d7ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
%% This is part of the OpTeX project, see http://petr.olsak.net/optex

\_codedecl \margins {Macros for margins setting <2023-05-01>} % 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
                            \_setpagerightoffset}%
      \_else \_opwarning{use \_string\_margins/1 or \_string\_margins/2}%
   \_fi\_fi\_fi
   \_setpagerightoffset
}
\_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 ;

   \_doc -----------------------------
   When left-to-right direction of typesetting is selected (default) then
   \"main vertical line" of the page has `\hoffset` distance from the left
   paper border and all lines at the page start here and run to the right side
   (exceptions can be done by `\moveleft` or `\moveright`, of course). When we
   have set right-to-left direction (using `\textdir TRT`, for example), then
   the \"main vertical line" cannot be at the same position because lines run to the left,
   i.e.\ they would be off paper. This is reason why the setting `\pagedir TRT` shifts the
   \"main vertical line" to an alternative position: it has `\pagerightoffset+1in` distance from the
   {\em right} paper border and thus right-to-left lines are visible on the
   paper. We have to set `\pagerightoffset` properly for such cases. This is
   done in the macro \`\_setpagerightoffset`.
   It must be called whenever `\hoffset` is changed.
   \_cod -----------------------------

\_def\_setpagerightoffset{%
   \_pagerightoffset=\_dimexpr\_pdfpagewidth-\_xhsize-\_hoffset-1in\_relax
}
\_setpagerightoffset % setting default value from default values

   \_doc -----------------------------
   Page numbers and numbers of (sub)sections have to be printed in
   left-to-right mode even though the document mode is right-to-left.
   We print these numbers via \`\_numprint``{<number>}` in \OpTeX/ macros.
   The `\_numprint` is `\_useit` by default (i.e.\ do nothing special)
   because we have left-to-right mode as default. But a user can define
   \begtt
   \_def\_numprint#1{{\textdir TLT #1}}
   \endtt
   if the document is set to right-to-left mode.
   \_cod -----------------------------

\_let\_numprint=\_useit

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

2023-05-01 \_numprint for page numbers, section numbers introduced
2023-04-29 \_setpagerightoffset for TRT typesetting introduced again
2021-03-15 \_setxhsize added
2020-03-14 released