%
% lltjcore.sty: derived from plcore.dtx in pLaTeX.
%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{lltjcore}[2025-04-01
  Patch to LaTeX2e Kernel for LuaTeX-ja (for LaTeX2e >=2025-06-01)]
\RequirePackage{etoolbox}

%%% expl3
\RequirePackage{expl3}
\ExplSyntaxOn
\cs_set_eq:NN \platex_direction_yoko: \yoko
\cs_set_eq:NN \platex_direction_tate: \tate
\cs_set_eq:NN \platex_direction_dtou: \dtou
\cs_set_eq:NN \platex_direction_utod: \utod % LuaTeX-ja specific
\prg_new_conditional:Npnn \platex_if_box_yoko:N #1 { p, T, F, TF }
  { \if_int_compare:w \ltjgetparameter{boxdir}{#1} = 4 \scan_stop:
      \prg_return_true: \else: \prg_return_false: \fi: }
\prg_new_conditional:Npnn \platex_if_box_utod:N #1 { p, T, F, TF }
  { \if_int_compare:w \ltjgetparameter{boxdir}{#1} = 11 \scan_stop:
      \prg_return_true: \else: \prg_return_false: \fi: }
\prg_new_eq_conditional:NNn \platex_if_box_tate_math:N \platex_if_box_utod:N { p, T, F, TF }
\prg_new_conditional:Npnn \platex_if_box_tate_nomath:N #1 { p, T, F, TF }
  { \if_int_compare:w \ltjgetparameter{boxdir}{#1} = 3 \scan_stop:
      \prg_return_true: \else: \prg_return_false: \fi: }
\prg_new_conditional:Npnn \platex_if_box_tate:N #1 { p, T, F, TF }
  { \if_int_compare:w \ltjgetparameter{boxdir}{#1} = 3 \scan_stop:
      \prg_return_true:
    \else: \if_int_compare:w \ltjgetparameter{boxdir}{#1} = 11 \scan_stop: % utod
      \prg_return_true:
    \else
      \prg_return_false:
    \fi: \fi:}
\prg_new_conditional:Npnn \platex_if_box_dtou:N #1 { p, T, F, TF }
  { \if_int_compare:w \ltjgetparameter{boxdir}{#1} = 1 \scan_stop:
      \prg_return_true: \else: \prg_return_false: \fi: }
\prg_new_conditional:Npnn \platex_if_direction_yoko: { p, T, F, TF }
  { \if_int_compare:w \ltjgetparameter{direction} = 4 \scan_stop:
      \prg_return_true: \else: \prg_return_false: \fi: }
\prg_new_conditional:Npnn \platex_if_direction_utod: { p, T, F, TF }
  { \if_int_compare:w \ltjgetparameter{direction} = 11 \scan_stop:
      \prg_return_true: \else: \prg_return_false: \fi: }
\prg_new_eq_conditional:NNn
  \platex_if_direction_tate_math: \platex_if_direction_utod: { p, T, F, TF }
\prg_new_conditional:Npnn \platex_if_direction_tate_nomath: { p, T, F, TF }
  { \if_int_compare:w \ltjgetparameter{direction} = 3 \scan_stop:
      \prg_return_true: \else: \prg_return_false: \fi: }
\prg_new_conditional:Npnn \platex_if_direction_tate: { p, T, F, TF }
  { \if_int_compare:w \ltjgetparameter{direction} = 3 \scan_stop:
      \prg_return_true:
    \else: \if_int_compare:w \ltjgetparameter{direction} = 11 \scan_stop: % utod
      \prg_return_true:
    \else
      \prg_return_false:
    \fi: \fi:}
\prg_new_conditional:Npnn \platex_if_direction_dtou: { p, T, F, TF }
  { \if_int_compare:w \ltjgetparameter{direction} = 1 \scan_stop:
      \prg_return_true: \else: \prg_return_false: \fi: }
\ExplSyntaxOff

\def\cleardoublepage{\clearpage\if@twoside
  \ifodd\c@page
    \ifnum\ltjgetparameter{direction}=3
      \hbox{}\thispagestyle{empty}\newpage
      \if@twocolumn\hbox{}\newpage\fi
    \fi
  \else
    \ifnum\ltjgetparameter{direction}=4
      \hbox{}\thispagestyle{empty}\newpage
      \if@twocolumn\hbox{}\newpage\fi
    \fi
  \fi\fi}

\patchcmd\@vtryfc{\global\setbox\@outputbox\vbox{}}
  {\global\setbox\@outputbox\vbox{%
     \unless\ifnum\ltjgetparameter{direction}=4
       \primitive\everypar{}\hskip\z@\fi
  }}{}{}

%%%%%% tombow
\newif\iftombow \tombowfalse
\newif\iftombowdate \tombowdatetrue
\newdimen\@tombowwidth
\setlength{\@tombowwidth}{.1\p@}
\newbox\@TL\newbox\@Tl
\newbox\@TC
\newbox\@TR\newbox\@Tr
\newbox\@BL\newbox\@Bl
\newbox\@BC
\newbox\@BR\newbox\@Br
\newbox\@CL
\newbox\@CR
\font\@bannerfont=file:lmmono9-regular.otf % cmtt9 in pLaTeX
\newtoks\@bannertoken
\@bannertoken{}
\def\maketombowbox{%
  \setbox\@TL\hbox to\z@{\yoko\hss
      \vrule width\dimexpr 10mm+\@tombowbleed\relax height\@tombowwidth depth\z@
      \vrule height10mm width\@tombowwidth depth\z@
      \iftombowdate
        \raise4pt\hbox to\z@{\hskip5mm\@bannerfont\the\@bannertoken\hss}%
      \fi}%
  \setbox\@Tl\hbox to\z@{\yoko\hss
      \vrule width10mm height\@tombowwidth depth\z@
      \vrule height\dimexpr 10mm+\@tombowbleed\relax  width\@tombowwidth depth\z@}%
  \setbox\@TC\hbox{\yoko
      \vrule width10mm height\@tombowwidth depth\z@
      \vrule height10mm width\@tombowwidth depth\z@
      \vrule width10mm height\@tombowwidth depth\z@}%
  \setbox\@TR\hbox to\z@{\yoko
      \vrule height10mm width\@tombowwidth depth\z@
      \vrule width\dimexpr 10mm+\@tombowbleed\relax height\@tombowwidth depth\z@\hss}%
  \setbox\@Tr\hbox to\z@{\yoko
      \vrule height\dimexpr 10mm+\@tombowbleed\relax width\@tombowwidth depth\z@
      \vrule width10mm height\@tombowwidth depth\z@\hss}%
  \setbox\@BL\hbox to\z@{\yoko\hss
      \vrule width\dimexpr 10mm+\@tombowbleed\relax depth\@tombowwidth height\z@
      \vrule depth10mm width\@tombowwidth height\z@}%
  \setbox\@Bl\hbox to\z@{\yoko\hss
      \vrule width10mm depth\@tombowwidth height\z@
      \vrule depth\dimexpr 10mm+\@tombowbleed\relax width\@tombowwidth height\z@}%
  \setbox\@BC\hbox{\yoko
      \vrule width10mm depth\@tombowwidth height\z@
      \vrule depth10mm width\@tombowwidth height\z@
      \vrule width10mm depth\@tombowwidth height\z@}%
  \setbox\@BR\hbox to\z@{\yoko
      \vrule depth10mm width\@tombowwidth height\z@
      \vrule width\dimexpr 10mm+\@tombowbleed\relax depth\@tombowwidth height\z@\hss}%
  \setbox\@Br\hbox to\z@{\yoko
      \vrule depth\dimexpr 10mm+\@tombowbleed\relax width\@tombowwidth height\z@
      \vrule width10mm depth\@tombowwidth height\z@\hss}%
  \setbox\@CL\hbox to\z@{\yoko\hss
      \vrule width10mm height.5\@tombowwidth depth.5\@tombowwidth
      \vrule height10mm depth10mm width\@tombowwidth}%
  \setbox\@CR\hbox to\z@{\yoko
      \vrule height10mm depth10mm width\@tombowwidth
      \vrule height.5\@tombowwidth depth.5\@tombowwidth width10mm\hss}%
}
\def\@outputtombow{%
  \iftombow
  \vbox to\z@{\kern\dimexpr-10mm-\@tombowbleed\relax
    \boxmaxdepth\maxdimen
      \moveleft\@tombowbleed\vbox to\@@paperheight{%
	  \color@begingroup
	  \@tombowcolor
      \hbox to\@@paperwidth{\hskip\@tombowbleed\relax
         \copy\@TL\hfill\copy\@TC\hfill\copy\@TR\hskip\@tombowbleed}%
      \kern-10mm%
      \hbox to\@@paperwidth{\copy\@Tl\hfill\copy\@Tr}%
      \vfill
      \hbox to\@@paperwidth{\copy\@CL\hfill\copy\@CR}%
      \vfill
      \hbox to\@@paperwidth{\copy\@Bl\hfill\copy\@Br}%
      \kern-10mm%
      \hbox to\@@paperwidth{\hskip\@tombowbleed\relax
         \copy\@BL\hfill\copy\@BC\hfill\copy\@BR\hskip\@tombowbleed}%
      \color@endgroup
    }\vss
  }%
  \fi
}
\def\@tombowcolor{\normalcolor}
\def\@tombowbleed{3mm}
\def\@tombowreset@@paper{%
   \@@topmargin \topmargin % only for compatibility
   \iftombow
     \@@paperwidth\dimexpr\paperwidth+2\dimexpr\@tombowbleed\relax
     \@@paperheight\dimexpr\paperheight+10mm+2\dimexpr\@tombowbleed\relax
     \advance\@@topmargin 1in\relax \advance\@themargin 1in\relax
   \fi
}
\newdimen\@@paperheight
\newdimen\@@paperwidth
\newdimen\@@topmargin % not used by LuaTeX-ja; only to avoid existing codes

%%%%%% output routine
\AddToHook{build/page/reset}[lltjcore]{%
  \ifnum\ltjgetparameter{direction}=4\else
    \dimen\z@\textwidth \textwidth\textheight \textheight\dimen\z@
    \let\@themargin\evensidemargin
    \let\evensidemargin\oddsidemargin
    \let\oddsidemargin\@themargin
  \fi
  \directlua{luatexja.lineskip.setting_backup()}%
}
\AddToHook{build/page/after}[lltjcore]{%
  \directlua{luatexja.lineskip.setting_restore()}%
}

\patchcmd\@outputpage{% supply \yoko
    \set@typeset@protect
    \aftergroup \endgroup
}{%
    \yoko \set@typeset@protect
    \aftergroup \endgroup
}{}{}

\NewSocketPlug {build/column/outputbox}{floats-footnotes-platex} {%
  \@outputbox@reinsertbskip
  \@outputbox@attachfloats
  \@if@footnotes@TF
    {\@outputbox@append{\@textbottom}\let\@textbottom\relax}%
    {}%
  \@outputbox@appendfootnotes
}

\newif\iffnfixbottom % only for old codes

% \@make@normalcolbox
\def\pltx@adjust@wd@outputbox{%
  \unless\ifnum\ltjgetparameter{direction}=4
    \vbox{\primitive\everypar{}\hskip\z@}%
   \fi}
\patchcmd{\@make@normalcolbox}{\vskip -\@outputbox@depth\@textbottom}%
  {\pltx@adjust@wd@outputbox\vskip -\@outputbox@depth\@textbottom}{}{}%

% トンボ対応用．将来は shipout hook で書き直したい
\patchcmd\@outputpage{\vskip\topmargin}{\vskip\topmargin\iftombow\vskip1in\fi}{}{}
\patchcmd\@outputpage{\reset@font}{\@tombowreset@@paper\reset@font}{}{}
\patchcmd\@outputpage{\@begindvi}{\@begindvi\@outputtombow}{}{}

%%%%%% direction of floats
\pretocmd\@floatboxreset{\pltx@save@float@dir}{}{}
\patchcmd\@endfloatbox{\outer@nobreak}{\outer@nobreak\pltx@check@float@dir}{}{}
\def\pltx@save@float@dir{%
  \edef\pltx@float@dir@first{\ltjgetparameter{direction}}}
\def\pltx@check@float@dir{%
  \ifx\pltx@float@dir@first\@undefined\else
    \ifnum\ltjgetparameter{direction}=\pltx@float@dir@first\else
      \pltx@err@float@dir
    \fi
  \fi}
\def\pltx@err@float@dir{%
  \@latex@error{Direction change inside float!?\MessageBreak
    Use \noexpand\layoutcaption provided in \string\usepackage{lltjext}}\@ehc}

%%%%%% Remove extra xkanjiskip
\begingroup
\@ifpackageloaded{hyperref}
  {\ifdefined\real@setref\def\ltj@setrefprefix{real}\else\def\ltj@setrefprefix{}\fi}
  {\def\ltj@setrefprefix{}}
\expandafter\gdef\csname \ltj@setrefprefix @setref\endcsname#1#2#3{%
  \ifx#1\relax
    \protect\G@refundefinedtrue
    \nfss@text{\reset@font\bfseries ??}%
    \@latex@warning{Reference `#3' on page \thepage \space
              undefined}%
  \else
    \expandafter#2#1\@empty\@empty\@empty\protect\@setref@{}% change \null to \protect\@setref@{}
  \fi}
\gdef\@setref@{\ifhmode\spacefactor\@m\fi}
\endgroup
\if@compatibility\else
\def\verb{\relax\ifmmode\hbox\else\leavevmode\vadjust{}\fi
  \bgroup
    \verb@eol@error \let\do\@makeother \dospecials
    \verbatim@font\@noligs
    \language\l@nohyphenation
    \@ifstar\@sverb\@verb}
\fi
\patchcmd{\do@noligs}{\kern\z@}{\vadjust{}}{}{} % reported by p_typo
\gdef\@stopfield{%
  \inhibitglue\@tempskipa\lastskip\unskip
  \hskip\@tempskipa\color@endgroup\egroup}

%%%%%% footnote
\renewcommand\@makefnmark{%
  \unless\ifnum\ltjgetparameter{direction}=3
    \hbox{}\hbox{\@textsuperscript{\normalfont\@thefnmark}}\hbox{}%
  \else\hbox{\yoko \@textsuperscript{\normalfont\@thefnmark}}\fi}
\pretocmd{\footnote}{\inhibitglue}{}{}
\pretocmd{\footnotemark}{\inhibitglue}{}{}
\def\@ltj@process@makefnmark#1{%
  \unless\ifnum\ltjgetparameter{direction}=3 #1%
  \else\hbox to\z@{\hskip-.25\zw\raise.9\zh#1\hss}\fi%
}%
\let\ltj@orig@kernel@process@makefnmark=\@kernel@process@makefnmark
\def\@kernel@process@makefnmark#1{\ltj@orig@kernel@process@makefnmark{\@ltj@process@makefnmark#1}}
\long\def\@iiiparbox#1#2[#3]#4#5{%
  \leavevmode
  \@pboxswfalse
  \setlength\@tempdima{#4}%
  \@begin@tempboxa\vbox{\hsize\@tempdima\@parboxrestore#5\@@par}%
    \ifx\relax#2\else
      \setlength\@tempdimb{#2}%
      \edef\@parboxto{to\the\@tempdimb}%
    \fi
    \if#1b\vbox
    \else\if #1t\vtop
    \else\ifmmode\vcenter
    \else\@pboxswtrue\null$\vcenter% !!!
    \fi\fi\fi
    \@parboxto{\let\hss\vss\let\unhbox\unvbox
       \csname bm@#3\endcsname}%
    \if@pboxsw \m@th$\null\fi% !!!
  \@end@tempboxa}
\DeclareRobustCommand\underline[1]{%
  \relax
  \ifmmode\@@underline{#1}%
  \else \leavevmode\null$\@@underline{\hbox{#1}}\m@th$\null\relax\fi}

\patchcmd\@finalstrut{\nobreak}{\nobreak\ltjfakeboxbdd}{}{}

\def\@gnewline #1{%
  \ifvmode
    \@nolnerr
  \else
    \unskip \reserved@e {\reserved@f#1}\nobreak \hfil \break \null
    % \null は \break の効果が行頭禁則文字で打ち消されないために必要
  \ignorespaces
\fi}

%%%%%% tabular (platex/#43) \@tabclassz
\def\ltj@sharp{\relax\pltx@next@inhibitglue\ltjfakeboxbdd\ignorespaces\@sharp\unskip\ltjfakeboxbdd}
\def\@tabclassz{%
  \ifcase\@lastchclass
    \@acolampacol
  \or
    \@ampacol
  \or
  \or
  \or
    \@addamp
  \or
    \@acolampacol
  \or
    \@firstampfalse\@acol
  \fi
  \edef\@preamble{%
    \@preamble{%
      \ifcase\@chnum
        \hfil\hskip1sp\ltj@sharp\hfil
      \or
        \hskip1sp\ltj@sharp\hfil
      \or
        \hfil\hskip1sp\ltj@sharp
      \fi}}}
\patchcmd\@classv{%
   \ignorespaces\@sharp
}{%
   \everypar\expandafter{\the\everypar\ltjfakeboxbdd}\ignorespaces\@sharp\unskip
}{}{}
\patchcmd{\@tabular}{\leavevmode}{\leavevmode\null}{}{}
\apptocmd{\endtabular}{\null}{}{}
\expandafter \let \csname endtabular*\endcsname = \endtabular

%%%%%% tabbing (platex/#53)
\patchcmd\@startline{\strut}{\strut\inhibitglue}{}{}
\def\ltj@unhbox@curfield{\ltjfakeboxbdd\unhbox\@curfield\ltjfakeboxbdd}
%\patchcmd\@contfield{\unhbox\@curfield}{\ltj@unhbox@curfield}{}{}
\patchcmd\@addfield{\unhbox\@curfield}{\ltj@unhbox@curfield}{}{}

\def\mathsm@sh#1#2{%
  \setbox\z@\hbox{\ltj@yablshift0$\m@th#1{#2}$}%
  {}\finsm@sh}

\endinput
