OSDN Git Service

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