OSDN Git Service

ltjclasses.dtx: Make ltjt*.cls compatible with \geometry, etc.
[luatex-ja/luatexja.git] / src / luatexja-core.sty
index a2dc0ed..4a5d3f0 100644 (file)
 \newluatexattribute\jfam          % index for current jfam
 
 \newcount\ltj@dir@count           % counter for indicating direction
+\newcount\ltj@adjdir@count        % counter for indicating direction (for vadjust)
 \newluatexattribute\ltj@dir
 
 \ltj@dir=0
 %%%%%%%% \inhibitglue
 \protected\def\ltj@reset@globaldefs{%
   \directlua{luatexja.global_temp = tex.globaldefs; tex.globaldefs = 0}}
-\protected\def\ltj@restore@globaldefs{\directlua{tex.globaldefs = luatexja.global_temp}}
+\protected\def\ltj@restore@globaldefs{\directlua{tex.globaldefs = luatexja.global_temp}\relax}
 \protected\def\inhibitglue{\ifhmode\ltj@@ihb\fi}
 \def\ltj@@ihb{\relax\directlua{luatexja.jfmglue.create_inhibitglue_node()}}
 
     luatexja.stack.hmode = -tex.nest[tex.nest.ptr].mode
   }%
 }
+\setbox0\vbox{%
+  \directlua{% This value depends on the version of LuaTeX.
+    luatexja.stack.vmode = -tex.nest[tex.nest.ptr].mode
+  }%
+}
 
 % prebreakpenalty = {<char_code>, <penalty>}
 \define@key[ltj]{japaram}{kcatcode}{%
   \fi}
 
 % [x]]kanjiskip = <dimen>
-\define@key[ltj]{japaram}{kanjiskip}{%              % SKIP
+\define@key[ltj]{japaram}{kanjiskip}{\ltjsetkanjiskip{#1}}
+\define@key[ltj]{japaram}{xkanjiskip}{\ltjsetxkanjiskip{#1}}
+\protected\def\ltjsetkanjiskip#1{%                  % SKIP
   \directlua{%
     local d = \ltj@safe@glue{#1}
     luatexja.stack.set_stack_skip(luatexja.stack_table_index.KSK, d); node.free(d)
-}}
-\define@key[ltj]{japaram}{xkanjiskip}{%             % SKIP
+}\ignorespaces}
+\protected\def\ltjsetxkanjiskip#1{%                  % SKIP
   \directlua{%
     local d = \ltj@safe@glue{#1}
     luatexja.stack.set_stack_skip(luatexja.stack_table_index.XSK, d); node.free(d)
-}}
+}\ignorespaces}
+
 
 % jcharwidowpenalty = <count>
 \define@key[ltj]{japaram}{jcharwidowpenalty}{%      %COUNT
     \else\directlua{luatexja.isglobal=''}\fi
 }
 \protected\def\ltjsetparameter#1{%
-  \ltj@setpar@global\setkeys[ltj]{japaram}{#1}\ignorespaces}
+  \directlua{luatexja.base.start_time_measure('set_par')}%
+  \ltj@setpar@global\setkeys[ltj]{japaram}{#1}%
+  \directlua{luatexja.base.stop_time_measure('set_par')}%
+  \ignorespaces}
 \protected\def\ltjglobalsetparameter#1{%
   \relax\ifnum\globaldefs<0\directlua{luatexja.isglobal=''}%
     \else\directlua{luatexja.isglobal='global'}\fi%
-  \setkeys[ltj]{japaram}{#1}\ignorespaces}
+  \directlua{luatexja.base.start_time_measure('set_par')}%
+  \setkeys[ltj]{japaram}{#1}%
+  \directlua{luatexja.base.stop_time_measure('set_par')}%
+  \ignorespaces}
 
 %%%%%%%% \ltjgetparameter
 \def\ltj@@decl@array@param#1{%
 \ltj@@decl@array@param{jacharrange}
 \ltj@@decl@array@param{chartorange}
 \def\ltjgetparameter#1{%
+  \directlua{luatexja.base.start_time_measure('get_par')}%
   \ifcsname ltj@@array@param/#1\endcsname
     \expandafter\ltx@firstoftwo
   \else
   {\ltj@@getparam@one{#1}}%
 }
 \def\ltj@@getparam@one#1{%
-  \directlua{luatexja.ext_get_parameter_unary('#1')}}%
+  \directlua{luatexja.ext_get_parameter_unary('#1')}%
+  \directlua{luatexja.base.stop_time_measure('get_par')}%
+}%
 \def\ltj@@getparam@two#1#2{%
   \directlua{luatexja.ext_get_parameter_binary('#1', \ltj@safe@num@or{nil}{#2})}%
+  \directlua{luatexja.base.stop_time_measure('get_par')}%
 }
 
 %%%%%%%% \ltjalchar<number>, \ltjjachar<number>
 %%%%%%%% \yoko, \tate, \ifydir, \iftdir
 \newif\ifydir
 \newif\iftdir
+\newif\ifddir
 \protected\def\tate{%
   \directlua{luatexja.direction.set_list_direction(3, 'tate')}%
-  \ltj@dir@count=3 \tdirtrue\ydirfalse
-}
+  \ltj@reset@globaldefs\ltj@dir@count=3\relax
+  \ifvmode\ltj@adjdir@count=3\fi\tdirtrue\ydirfalse\ddirfalse\ltj@restore@globaldefs}
 \protected\def\yoko{%
   \directlua{luatexja.direction.set_list_direction(4, 'yoko')}%
-  \ltj@dir@count=4 \ydirtrue\tdirfalse
-}
+  \ltj@reset@globaldefs\ltj@dir@count=4\relax
+  \ifvmode\ltj@adjdir@count=4\fi\ydirtrue\tdirfalse\ddirfalse\ltj@restore@globaldefs}
+\protected\def\dtou{%
+  \directlua{luatexja.direction.set_list_direction(1, 'dtou')}%
+  \ltj@reset@globaldefs\ltj@dir@count=1\relax
+  \ifvmode\ltj@adjdir@count=1\fi\ydirfalse\tdirfalse\ddirtrue\ltj@restore@globaldefs}
 \yoko
 
+\newcount\ltj@adjdir@count@backup
+%%%% insert
+\let\orig@insert=\insert
+\protected\def\insert{\begingroup\afterassignment\ltj@@insert@A\ltj@tempcnta}
+\protected\def\ltj@@insert@A{\orig@insert\ltj@tempcnta\bgroup\aftergroup\endgroup\let\next}
+%%%% vadjust
+\let\ltj@@orig@vadjust=\vadjust
+\protected\def\vadjust{%
+  \ltj@@orig@vadjust\bgroup
+  \ifcase\ltj@adjdir@count\or\dtou\or\or\tate\or\yoko\fi
+  \aftergroup\ltj@@vadjust@check@dir\let\next}
+\protected\def\ltj@@vadjust@check@dir{%
+  \directlua{luatexja.direction.check_adjust_direction()}%
+}
+%%%% unhbox and unvbox
+\let\ltj@@orig@unhbox=\unhbox
+\let\ltj@@orig@unvbox=\unvbox
+\protected\def\unhbox{\begingroup\afterassignment\ltj@@unhbox\ltj@tempcnta}
+\protected\def\unvbox{\begingroup\afterassignment\ltj@@unvbox\ltj@tempcnta}
+\protected\def\ltj@@unhbox{%
+  \directlua{luatexja.direction.check_dir(\the\ltj@tempcnta)}%
+  \ltj@@orig@unhbox\ltj@tempcnta\endgroup}
+\protected\def\ltj@@unvbox{%
+  \directlua{luatexja.direction.check_dir(\the\ltj@tempcnta)}%
+  \ltj@@orig@unvbox\ltj@tempcnta\endgroup}
+
+%%%%%%%% \ltjgetwd<box_num> etc.
+%\def\ltjgetwd{\expandafter\expandafter\expandafter\ltj@@getwd\ltj@grab@num}
+%\def\ltjgetht{\expandafter\expandafter\expandafter\ltj@@getht\ltj@grab@num}
+%\def\ltjgetdp{\expandafter\expandafter\expandafter\ltj@@getdp\ltj@grab@num}
+\def\ltj@@getwd#1{%
+  \directlua{luatexja.direction.get_box_dim('width', \the\numexpr#1\relax)}%
+  \ltj@tempdima%
+}
+\def\ltj@@getht#1{%
+  \directlua{luatexja.direction.get_box_dim('height', \the\numexpr#1\relax)}%
+  \ltj@tempdima%
+}
+\def\ltj@@getdp#1{%
+  \directlua{luatexja.direction.get_box_dim('depth', \the\numexpr#1\relax)}%
+  \ltj@tempdima%
+}
+\let\ltjgetwd=\ltj@@getwd\let\ltjgetht=\ltj@@getht\let\ltjgetdp=\ltj@@getdp
+
+%%%%%%%% \ltjsetwd<box_num>=<dimen> etc.
+\protected\def\ltjsetwd{\begingroup
+  \ltj@reset@globaldefs\afterassignment\ltj@@setwd@a\ltj@tempcnta}
+\def\ltj@@setwd@a{\afterassignment\ltj@@setwd@b\ltj@tempdima}
+\def\ltj@@setwd@b{%
+  \directlua{luatexja.direction.set_box_dim('width')}%
+  \ltj@restore@globaldefs\endgroup}
+\protected\def\ltjsetht{\begingroup
+  \ltj@reset@globaldefs\afterassignment\ltj@@setht@a\ltj@tempcnta}
+\def\ltj@@setht@a{\afterassignment\ltj@@setht@b\ltj@tempdima}
+\def\ltj@@setht@b{%
+  \directlua{luatexja.direction.set_box_dim('height')}%
+  \ltj@restore@globaldefs\endgroup}
+\protected\def\ltjsetdp{\begingroup
+  \ltj@reset@globaldefs\afterassignment\ltj@@setdp@a\ltj@tempcnta}
+\def\ltj@@setdp@a{\afterassignment\ltj@@setdp@b\ltj@tempdima}
+\def\ltj@@setdp@b{%
+  \directlua{luatexja.direction.set_box_dim('depth')}%
+  \ltj@restore@globaldefs\endgroup}
+
+%%%%%%%% \ifybox, \iftbox, \ifdbox
+\def\ltj@@box@cond#1#2\fi{\fi\csname if#1\endcsname}
+\def\ifybox#1{\directlua{luatexja.direction.dir_conditional(\the\numexpr #1\relax, 4)}%
+  \ltj@@box@cond{true}\else\ltj@@box@cond{false}\fi}
+\def\iftbox#1{\directlua{luatexja.direction.dir_conditional(\the\numexpr #1\relax, 3)}%
+  \ltj@@box@cond{true}\else\ltj@@box@cond{false}\fi}
+\def\ifdbox#1{\directlua{luatexja.direction.dir_conditional(\the\numexpr #1\relax, 1)}%
+  \ltj@@box@cond{true}\else\ltj@@box@cond{false}\fi}
+
 %%------------------ Load module for the format
 
 \ifltj@in@latex                 %<*LaTeX>