OSDN Git Service

aa4873f941c5bb39643f68ad652c6ebf4b535ecb
[luatex-ja/luatexja.git] / src / patches / lltjp-geometry.sty
1 %
2 % lltjp-geometry.sty
3 %
4
5 \NeedsTeXFormat{LaTeX2e}
6 \ProvidesPackage{lltjp-geometry}[2014/11/23 Patch to geometry for LuaTeX-ja with vertical writing mode]
7 \RequirePackage{ifluatex}
8
9 \newif\if@ltj@@geometry@tate
10 \@ltj@@geometry@tatefalse
11 \ifluatex\else % pTeX
12   \@ltj@@geometry@tatetrue
13 \def \AtBeginDvi #1{%
14   \global \setbox \@begindvibox
15     \vbox{\yoko\unvbox \@begindvibox #1}%
16 }
17 \fi
18
19 \RequirePackage{filehook}
20 \newif\ifGm@ltj@layoutswitch
21
22 \AtBeginOfPackageFile{geometry}{%
23   \edef\Gm@ltj@topskip{\the\topskip}
24   \edef\Gm@ltj@headsep{\the\headsep}
25   \edef\Gm@ltj@footskip{\the\footskip}
26 }
27 \AtEndOfPackageFile{geometry}{%
28 \ifluatex
29   \ifnum\ltjgetparameter{direction}=3 \@ltj@@geometry@tatetrue\fi
30 \fi
31 \if@ltj@@geometry@tate
32
33 % TODO: 
34 %  * pTeX, LuaTeX ともに landscape, truedimen が正しく動作しない
35 %  * pTeX  resetpaper が正しく動作しない
36
37 % `h', `v' は「水平」「垂直」を意味するが:
38 % ユーザー側では「紙の物理的な水平方向」「物理的な垂直方向」を,
39 % しかしパッケージ内部処理では「字送り方向」「行送り方向」を表す.
40 % そのため,key-value interface 内で h, v の交換を行う必要がある.
41
42 % twoside 指定時は,lmargin は小口側,rmargin はノド側の余白を指す.
43 % また,bindingoffset 偶数ページを右に,奇数ページを左にずらす.
44 \define@key{Gm}{hdivide}{\Gm@parse@divide{#1}{lmargin}{height}{rmargin}}%
45
46 \define@key{Gm}{vscale}{\Gm@hbodytrue\edef\Gm@hscale{#1}}%
47 \define@key{Gm}{hscale}{\Gm@vbodytrue\edef\Gm@vscale{#1}}%
48 \define@key{Gm}{vdivide}{\Gm@parse@divide{#1}{tmargin}{width}{bmargin}}%
49 \define@key{Gm}{divide}{\Gm@parse@divide{#1}{tmargin}{width}{bmargin}%
50   \Gm@parse@divide{#1}{lmargin}{height}{rmargin}}%
51 \define@key{Gm}{vmargin}{\Gm@branch{#1}{tmargin}{bmargin}}%
52 \define@key{Gm}{hmargin}{\Gm@branch{#1}{lmargin}{rmargin}}%
53 \define@key{Gm}{margin}{\Gm@branch{#1}{tmargin}{lmargin}%
54   \Gm@branch{#1}{bmargin}{rmargin}}%
55 \define@key{Gm}{headsep}{\Gm@defbylen{ltj@headsep}{#1}}%
56 \define@key{Gm}{footskip}{\Gm@defbylen{ltj@footskip}{#1}}%
57 \let\KV@Gm@foot\KV@Gm@footskip
58 \define@key{Gm}{vmarginratio}{\edef\Gm@hmarginratio{#1}}%
59 \define@key{Gm}{hmarginratio}{\edef\Gm@vmarginratio{#1}}%
60 \define@key{Gm}{nohead}[true]{\Gm@doifelse{nohead}{#1}%
61   {\Gm@setlength\headheight\z@\Gm@defbylen{ltj@headsep}\z@}{}}%
62 \define@key{Gm}{nofoot}[true]{\Gm@doifelse{nofoot}{#1}%
63   {\Gm@defbylen{ltj@footskip}\z@}{}}%
64 \define@key{Gm}{noheadfoot}[true]{\Gm@doifelse{noheadfoot}{#1}%
65   {\Gm@setlength\headheight\z@\Gm@defbylen{ltj@headsep}\z@
66   \Gm@defbylen{ltj@footskip}\z@}{}}%
67
68 % \@mparswitch は傍注の出力位置を左右ページで変えるスイッチだが,
69 % geometry.sty はこれを左右ページのレイアウトを変えるか否かの判定にも使っている.
70 % 縦組クラスの場合は \@mparswitch は常に偽でないとおかしなことになるので
71 % \@mparswitch -> \Gm@ltj@layoutswitch と名称変更する.
72
73 \let\Gm@ltj@layoutswitch\@mparswitch
74 \@mparswitchfalse
75 \define@key{Gm}{twoside}[true]{\Gm@doifelse{twoside}{#1}%
76   {\@twosidetrue\Gm@ltj@layoutswitchtrue}{\@twosidefalse\Gm@ltj@layoutswitchfalse}}%
77 \define@key{Gm}{asymmetric}[true]{\Gm@doifelse{asymmetric}{#1}%
78   {\@twosidetrue\Gm@ltj@layoutswitchfalse}{}}%
79
80 % includemp 指定時は,\headsep (if reversemp || twocolumn) や,
81 % \footskip (if !reversemp || twocolumn) を傍注幅だけ増加させる.
82
83
84 \def\Gm@Dhratio{2:3}% = top:bottom default
85 \def\Gm@Dvratio{1:1}% = left:right default for oneside
86 \def\Gm@Dvratiotwo{3:2}% = inner:outer default for twoside.
87
88 % save length
89 \def\Gm@save{%
90   \Gm@savelength{paperwidth}%
91   \Gm@savelength{paperheight}%
92   \Gm@savelength{textwidth}%
93   \Gm@savelength{textheight}%
94   \Gm@savelength{evensidemargin}%
95   \Gm@savelength{oddsidemargin}%
96   \Gm@savelength{topmargin}%
97   \Gm@savelength{headheight}%
98   %\Gm@savelength{headsep}%
99   \Gm@savelength{topskip}%
100   %\Gm@savelength{footskip}%
101   \Gm@savelength{baselineskip}%
102   \Gm@savelength{marginparwidth}%
103   \Gm@savelength{marginparsep}%
104   \Gm@savelength{columnsep}%
105   \Gm@savelength{hoffset}%
106   \Gm@savelength{voffset}
107   \Gm@savelength{Gm@layoutwidth}%
108   \Gm@savelength{Gm@layoutheight}%
109   \Gm@savelength{Gm@layouthoffset}%
110   \Gm@savelength{Gm@layoutvoffset}%
111   \Gm@saveboolean{@twocolumn}%
112   \Gm@saveboolean{@twoside}%
113   \Gm@saveboolean{Gm@ltj@layoutswitch}%
114   \Gm@saveboolean{@reversemargin}}%
115
116 % frame
117 \ifluatex
118   \def\Gm@pageframe@cmd{\vb@xt@\z@}
119 \else
120   \def\Gm@pageframe@cmd{\vb@xt@\z@\bgroup\yoko\aftergroup\egroup}
121 \fi
122 \renewcommand*{\Gm@pageframes}{%
123   \Gm@pageframe@cmd{%
124    \ifGm@showcrop
125     \vb@xt@\z@{\vskip-1\Gm@truedimen in\vskip\Gm@layoutvoffset%
126      \hb@xt@\z@{\hskip-1\Gm@truedimen in\hskip\Gm@layouthoffset%
127       \vb@xt@\Gm@layoutheight{%
128        \let\protect\relax
129        \hb@xt@\Gm@layoutwidth{\Gm@cropmark(-1,1,-3,3)\hfil\Gm@cropmark(1,1,3,3)}%
130        \vfil
131        \hb@xt@\Gm@layoutwidth{\Gm@cropmark(-1,-1,-3,-3)\hfil\Gm@cropmark(1,-1,3,-3)}}%
132      \hss}%
133     \vss}%
134    \fi%
135    \ifGm@showframe
136     \if@twoside
137      \ifodd\count\z@
138        \let\@themargin\oddsidemargin
139      \else
140        \let\@themargin\evensidemargin
141      \fi
142     \fi
143     \moveright\@themargin%
144     \vb@xt@\z@{%
145      \vskip\topmargin\vb@xt@\z@{\vss\Gm@hrule}%
146      \vskip\headheight\vb@xt@\z@{\vss\Gm@hruled}%
147      \vskip\headsep\vb@xt@\z@{\vss\Gm@hrule}%
148      \if@reversemargin\vb@xt@\z@{%
149        \vss\vb@xt@\z@{\vss\Gm@hrule}%
150        \vskip\marginparwidth\vb@xt@\z@{\vss\Gm@hrule}%
151        \vskip\marginparsep
152      }\fi
153      \hb@xt@\textwidth{\llap{\Gm@vrule}\hfil\Gm@vrule}%
154      \vb@xt@\z@{\vss\Gm@hruled}%
155      \unless\if@reversemargin\vb@xt@\z@{%
156        \vskip\marginparsep\vb@xt@\z@{\vss\Gm@hrule}%
157        \vskip\marginparwidth\vb@xt@\z@{\vss\Gm@hruled}%
158      }\fi%
159      \vskip\footskip\vb@xt@\z@{\vss\Gm@hruled}%
160      \vss}%
161     \fi%
162   }}
163 \def\Gm@detall#1#2#3#4{%
164   \@tempcnta\z@
165   \if#1h
166     \let\Gm@mratio\Gm@hmarginratio
167     \edef\Gm@Dmratio{\Gm@Dhratio}%
168   \else
169     \let\Gm@mratio\Gm@vmarginratio
170     \edef\Gm@Dmratio{\if@twoside\Gm@Dvratiotwo\else\Gm@Dvratio\fi}%
171   \fi
172   \if#1h
173     \ifx\Gm@tmargin\@undefined\else\advance\@tempcnta4\relax\fi
174     \ifGm@hbody\advance\@tempcnta2\relax\fi
175     \ifx\Gm@bmargin\@undefined\else\advance\@tempcnta1\relax\fi
176     \Gm@cnth\@tempcnta
177   \else
178     \ifx\Gm@lmargin\@undefined\else\advance\@tempcnta4\relax\fi
179     \ifGm@vbody\advance\@tempcnta2\relax\fi
180     \ifx\Gm@rmargin\@undefined\else\advance\@tempcnta1\relax\fi
181     \Gm@cntv\@tempcnta
182   \fi
183   \ifcase\@tempcnta
184     \if#1h
185       \Gm@defbylen{width}{\Gm@Dhscale\Gm@layoutwidth}%
186     \else
187       \Gm@defbylen{height}{\Gm@Dvscale\Gm@layoutheight}%
188     \fi
189     \Gm@detiiandiii{#2}{#3}{#4}%
190   \or
191     \ifx\Gm@mratio\@undefined
192       \if#1h
193         \Gm@defbylen{width}{\Gm@Dhscale\Gm@layoutwidth}%
194       \else
195         \Gm@defbylen{height}{\Gm@Dvscale\Gm@layoutheight}%
196       \fi
197       \setlength\@tempdimc{\@nameuse{Gm@#4}}%
198       \Gm@detiiandiii{#2}{#3}{#4}%
199       \expandafter\let\csname Gm@#2\endcsname\@undefined
200       \Gm@defbylen{#4}{\@tempdimc}%
201     \else
202       \Gm@setbyratio[f]{#1}{#4}{#3}%
203     \fi
204     \Gm@detiv{#2}{#3}{#4}{#2}%
205   \or\Gm@detiiandiii{#2}{#3}{#4}%
206   \or\Gm@detiv{#2}{#2}{#4}{#3}%
207   \or
208     \ifx\Gm@mratio\@undefined
209       \if#1h
210         \Gm@defbylen{width}{\Gm@Dhscale\Gm@layoutwidth}%
211       \else
212         \Gm@defbylen{height}{\Gm@Dvscale\Gm@layoutheight}%
213       \fi
214       \setlength\@tempdimc{\@nameuse{Gm@#3}}%
215       \Gm@detiiandiii{#2}{#4}{#3}%
216       \expandafter\let\csname Gm@#2\endcsname\@undefined
217       \Gm@defbylen{#3}{\@tempdimc}%
218     \else
219       \Gm@setbyratio[b]{#1}{#3}{#4}%
220     \fi
221     \Gm@detiv{#2}{#3}{#4}{#2}%
222   \or\Gm@detiv{#2}{#3}{#4}{#2}%
223   \or\Gm@detiv{#2}{#2}{#3}{#4}%
224   \or\Gm@warning{Over-specification in `#1'-direction.%
225                   ^^J\@spaces `#2' (\@nameuse{Gm@#2}) is ignored}%
226     \Gm@detiv{#2}{#3}{#4}{#2}%
227   \else\fi}%
228 \def\Gm@clean{%
229   \ifnum\Gm@cnth<4\let\Gm@tmargin\@undefined\fi
230   \ifodd\Gm@cnth\else\let\Gm@bmargin\@undefined\fi
231   \ifnum\Gm@cntv<4\let\Gm@lmargin\@undefined\fi
232   \ifodd\Gm@cntv\else\let\Gm@rmargin\@undefined\fi
233   \ifGm@hbody\else
234     \let\Gm@hscale\@undefined
235     \let\Gm@width\@undefined
236     \let\Gm@textwidth\@undefined
237   \fi
238   \ifGm@vbody\else
239     \let\Gm@vscale\@undefined
240     \let\Gm@height\@undefined
241     \let\Gm@textheight\@undefined
242   \fi
243   }%
244 \def\Gm@adjustpaper{%
245   \ifdim\paperwidth>\p@\else
246     \PackageError{geometry}{%
247     \string\paperwidth\space(\the\paperwidth) too short}{%
248     Set a paper type (e.g., `a4paper').}%
249   \fi
250   \ifdim\paperheight>\p@\else
251     \PackageError{geometry}{%
252     \string\paperheight\space(\the\paperheight) too short}{%
253     Set a paper type (e.g., `a4paper').}%
254   \fi
255   \ifGm@swap@papersize
256     \setlength\@tempdima{\paperwidth}%
257     \setlength\paperwidth{\paperheight}%
258     \setlength\paperheight{\@tempdima}%
259   \fi
260   \ifGm@layout\else
261     \setlength\Gm@layoutwidth{\paperheight}%
262     \setlength\Gm@layoutheight{\paperwidth}%
263   \fi}%
264 \def\Gm@adjustbody{
265   \ifGm@hbody
266     \ifx\Gm@width\@undefined
267       \ifx\Gm@hscale\@undefined
268         \Gm@defbylen{width}{\Gm@Dhscale\Gm@layoutwidth}%
269       \else
270         \Gm@defbylen{width}{\Gm@hscale\Gm@layoutwidth}%
271       \fi
272     \fi
273     \ifx\Gm@textwidth\@undefined\else
274       \setlength\@tempdima{\Gm@textwidth}%
275       \ifGm@includemp
276         \advance\@tempdima\Gm@wd@mp
277       \fi
278       \ifGm@includehead
279         \addtolength\@tempdima{\headheight}%
280         \addtolength\@tempdima{\headsep}%
281       \fi
282       \ifGm@includefoot
283         \addtolength\@tempdima{\footskip}%
284       \fi
285       \edef\Gm@width{\the\@tempdima}%
286     \fi
287   \fi
288   \ifGm@vbody
289     \ifx\Gm@height\@undefined
290       \ifx\Gm@vscale\@undefined
291         \Gm@defbylen{height}{\Gm@Dvscale\Gm@layoutheight}%
292       \else
293         \Gm@defbylen{height}{\Gm@vscale\Gm@layoutheight}%
294       \fi
295     \fi
296     \setlength\topskip\Gm@ltj@topskip
297     \ifx\Gm@lines\@undefined\else
298       \setlength\maxdepth{1\Cht}%
299       \setbox\z@=\hbox{\tate% next \ifdim must be executed in tate dir.
300         \ifdim\topskip<\ht\tstrutbox
301           \@tempdima\topskip
302           \global\topskip\ht\tstrutbox
303           \PackageWarningNoLine{lltjp-geometry}%
304            {do not worry about  `\noexpand\topskip was changed ...'\MessageBreak
305               warning above}
306           \PackageWarningNoLine{lltjp-geometry}%
307             {\noexpand\topskip was changed from \the\@tempdima\space
308               to \the\topskip}
309         \fi}%
310       \setlength\@tempdima{\baselineskip}%
311       \multiply\@tempdima\Gm@lines
312       \addtolength\@tempdima{\topskip}%
313       \addtolength\@tempdima{-\baselineskip}%
314       \edef\Gm@textheight{\the\@tempdima}%
315     \fi
316     \ifx\Gm@textheight\@undefined\else
317       \edef\Gm@height{\Gm@textheight}%
318     \fi
319   \fi}%
320
321 \def\Gm@adjustmp{%
322   \ifGm@includemp
323     \@tempdimb\marginparwidth
324     \advance\@tempdimb\marginparsep
325     \Gm@wd@mp\@tempdimb
326     \if@twocolumn
327       \Gm@wd@mp2\@tempdimb
328     \fi
329   \fi}%
330 \def\Gm@@process{%
331   \Gm@expandlengths
332   \Gm@adjustpaper
333   \addtolength\Gm@layoutheight{-\Gm@bindingoffset}%
334   \Gm@adjustmp
335   \Gm@adjustbody
336   \Gm@detall{h}{width}{tmargin}{bmargin}%
337   \Gm@detall{v}{height}{lmargin}{rmargin}%
338   \setlength\textwidth{\Gm@width}%
339   \setlength\textheight{\Gm@height}%
340   \setlength\headsep{\Gm@ltj@headsep}%
341   \setlength\footskip{\Gm@ltj@footskip}%
342   \setlength\topmargin{\Gm@tmargin}%
343   \setlength\oddsidemargin{\Gm@lmargin}%
344   \addtolength\oddsidemargin{-1\Gm@truedimen in}%
345   \ifGm@ltj@layoutswitch
346     \setlength\evensidemargin{\Gm@rmargin}%
347     \addtolength\evensidemargin{-1\Gm@truedimen in}%
348   \else
349     \evensidemargin\oddsidemargin
350   \fi
351   \advance\evensidemargin\Gm@bindingoffset
352   \addtolength\topmargin{-1\Gm@truedimen in}%
353   \ifGm@includehead
354     \addtolength\textwidth{-\headheight}%
355     \addtolength\textwidth{-\headsep}%
356   \else
357     \addtolength\topmargin{-\headheight}%
358     \addtolength\topmargin{-\headsep}%
359   \fi
360   \ifGm@includefoot
361     \addtolength\textwidth{-\footskip}%
362   \fi
363   \ifGm@includemp
364     \advance\textwidth-\Gm@wd@mp
365     \if@twocolumn
366       \advance\headsep.5\Gm@wd@mp
367       \advance\footskip.5\Gm@wd@mp
368     \else\if@reversemargin
369       \advance\headsep\Gm@wd@mp
370     \else
371       \advance\footskip\Gm@wd@mp
372     \fi\fi
373   \fi
374   \ifGm@heightrounded
375     \setlength\@tempdima{\textheight}%
376     \addtolength\@tempdima{-\topskip}%
377     \@tempcnta\@tempdima
378     \@tempcntb\baselineskip
379     \divide\@tempcnta\@tempcntb
380     \setlength\@tempdimb{\baselineskip}%
381     \multiply\@tempdimb\@tempcnta
382     \advance\@tempdima-\@tempdimb
383     \multiply\@tempdima\tw@
384     \ifdim\@tempdima>\baselineskip
385       \addtolength\@tempdimb{\baselineskip}%
386     \fi
387     \addtolength\@tempdimb{\topskip}%
388     \textheight\@tempdimb
389   \fi
390   \advance\oddsidemargin\Gm@layoutvoffset%
391   \advance\evensidemargin\Gm@layoutvoffset%
392   \advance\topmargin\Gm@layouthoffset%
393   \addtolength\Gm@layoutheight{\Gm@bindingoffset}%
394 }% end of \Gm@@process
395 % log
396 \def\Gm@logcontent#1{%
397   *geometry* verbose mode - [ #1 ] result:^^J%
398   \ifGm@pass * pass: disregarded the geometry package!^^J%
399   \else
400   * driver: \if\Gm@driver<none>\else\Gm@driver\fi^^J%
401   * paper: \ifx\Gm@paper\@undefined<default>\else\Gm@paper\fi^^J%
402   * layout: \ifGm@layout<custom>\else<same size as paper>\fi^^J%
403   \ifGm@layout
404   * layout(width,height): (\the\Gm@layoutwidth,\the\Gm@layoutheight)^^J%
405   \fi
406   * layoutoffset:(h,v)=(\the\Gm@layouthoffset,\the\Gm@layoutvoffset)^^J%
407   \@ifundefined{Gm@lines}{}{* lines: \Gm@lines^^J}%
408   \@ifundefined{Gm@hmarginratio}{}{* hratio: \Gm@hmarginratio^^J}%
409   \@ifundefined{Gm@vmarginratio}{}{* vratio: \Gm@vmarginratio^^J}%
410   \ifdim\Gm@bindingoffset=\z@\else
411   * bindingoffset: \the\Gm@bindingoffset^^J\fi
412   * modes: %
413    \Gm@showbool{landscape}%
414    \Gm@showbool{includehead}%
415    \Gm@showbool{includefoot}%
416    \Gm@showbool{includemp}%
417    \if@twoside twoside\space\fi%
418    \ifGm@ltj@layoutswitch\else\if@twoside asymmetric\space\fi\fi%
419    \Gm@showbool{heightrounded}%
420    \ifx\Gm@truedimen\@empty\else truedimen\space\fi%
421    \Gm@showbool{showframe}%
422    \Gm@showbool{showcrop}%
423   ^^J%
424   * h-part:(L,W,R)=(\Gm@lmargin, \Gm@height, \Gm@rmargin)^^J%
425   * v-part:(T,H,B)=(\Gm@tmargin, \Gm@width, \Gm@bmargin)^^J%
426   \fi
427   \Gm@showdim{\paperwidth}%
428   \Gm@showdim{\paperheight}%
429   \Gm@showdim{\textwidth}%
430   \Gm@showdim{\textheight}%
431   \Gm@showdim{\oddsidemargin}%
432   \Gm@showdim{\evensidemargin}%
433   \Gm@showdim{\topmargin}%
434   \Gm@showdim{\headheight}%
435   \Gm@showdim{\headsep}%
436   \Gm@showdim{\topskip}%
437   \Gm@showdim{\footskip}%
438   \Gm@showdim{\marginparwidth}%
439   \Gm@showdim{\marginparsep}%
440   \Gm@showdim{\columnsep}%
441   * \string\skip\string\footins=\the\skip\footins^^J%
442   \Gm@showdim{\hoffset}%
443   \Gm@showdim{\voffset}%
444   \Gm@showdim{\mag}%
445   * \string\@twocolumn\if@twocolumn true\else false\fi^^J%
446   * \string\@twoside\if@twoside true\else false\fi^^J%
447   * \string\@mparswitch\if@mparswitch true\else false\fi^^J%
448   * \string\@reversemargin\if@reversemargin true\else false\fi^^J%
449   * (1in=72.27pt=25.4mm, 1cm=28.453pt)^^J}%
450
451
452 \Gm@initall
453 \Gm@processconfig
454 \ProcessOptionsKV[c]{Gm}%
455 \Gm@setdefaultpaper
456 \ProcessOptionsKV[p]{Gm}%
457 \Gm@process
458 \fi}
459 \endinput