OSDN Git Service

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