OSDN Git Service

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