%%% License %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This package is licensed under the terms of the MIT License.

% Copyright (c) 2017-2025 Munehiro Yamamoto <munepixyz@gmail.com>
% Copyright (c) 2025 Kosei Kawaguchi

% Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% 2025/11/04
% 再読送り仮名のボックスの取る幅を毎度リセットするように修正。

% 2025/11/08
% 再読返り点の出力位置および\KanHyphenの仕様を変更。

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{gckanbun}[2025/11/12, Version 2.1.0]

%% declare package errors
\def\gcknbn@error{\PackageError{gckanbun}}
\def\gcknbn@warning{\PackageWarning{gckanbun}}
\def\gcknbn@warningnoline{\PackageWarningNoLine{gckanbun}}
\def\gcknbn@info{\PackageInfo{gckanbun}}

\RequirePackage{keyval,etoolbox}
\DeclareOption*{\gcknbn@setkey}
\def\gcknbn@setkey{\expandafter\@gcknbn@setkey\expandafter{\CurrentOption}}
\def\@gcknbn@setkey{\setkeys{gcknbn}}
\def\gcknbn@prefix{gckanbun}%%given <prefix> for each commands
\define@key{gcknbn}{prefix}{\gdef\gcknbn@prefix{#1}}

\ExecuteOptions{prefix}
\ProcessOptions\relax

%% auto-detect engine
\RequirePackage{ifuptex}
\RequirePackage{ifluatex}
\ifluatex
  \@ifpackageloaded{luatexja}{}{%
    \gcknbn@error{Please load package 'luatexja' when loading this package.}}
\else\ifuptex
  \def\zw{zw}\def\zh{zh}
\else\ifptex
  \def\zw{zw}\def\zh{zh}
\else
  \gcknbn@error{Package 'gckanbun' currently supports (u)pLaTeX and LuaLaTeX.}
\fi\fi\fi

%% 縦書き判定
\newif\ifgcknbn@tdir
\gcknbn@tdirfalse

\@ifundefined{iftdir}{\let\iftdir\iffalse}{}%

\ifluatex
  \@ifundefined{ltjgetparameter}{}{%
    \ifnum\ltjgetparameter{direction}=3 %
      \gcknbn@tdirtrue
    \fi
  }%
\else
  \iftdir
    \gcknbn@tdirtrue
\fi\fi

\newcommand*\gcknbn@kanjiskip@fill{%
  \ifluatex
    \ltjsetparameter{kanjiskip=\fill}%
  \else
    \kanjiskip=\fill\relax
  \fi
}

\newlength{\gcknbn@adjust@yokotate}

\NewDocumentCommand{\gcknbn@adjust@yokotate@use}{}{%
  \ifgcknbn@tdir
    \setlength{\gcknbn@adjust@yokotate}{-1.75ex}%
  \else%
    \setlength{\gcknbn@adjust@yokotate}{-1.3ex}%
  \fi%
}

\newlength{\gcknbn@adjust@kaeri}

\NewDocumentCommand{\gcknbn@adjust@kaeri@use}{}{%
  \ifgcknbn@tdir
    \setlength{\gcknbn@adjust@kaeri}{.35\zw}%
  \else%
    \setlength{\gcknbn@adjust@kaeri}{.15\zw}%
  \fi%
}

%% ルビ
%%  * グループルビ
%%  * 漢文訓点に対するふりがな（モノルビ）
\let\gcknbn@rubybox@text\relax
\let\gcknbn@rubybox@text@s\relax
\newdimen\gcknbn@rubybox@width
\newdimen\gcknbn@furigana@width \gcknbn@furigana@width\z@
\newdimen\gcknbn@furigana@width@s \gcknbn@furigana@width@s\z@
\newdimen\gcknbn@dima

% pre,post,bothをとれる。
\newdimen\gcknbn@intr@pre
\newdimen\gcknbn@intr@post
\define@key{gcknbn@ruby}{intrusion}{\edef\gcknbn@ruby@intr@mark{#1}}

\DeclareDocumentCommand{\gcknbn@ruby}{ s O{} m m O{\vphantom{羅} }}{%
  \gcknbn@adjust@yokotate@use%
  \global\gcknbn@furigana@width\z@%
  \global\gcknbn@furigana@width@s\z@%
  \global\def\gcknbn@ruby@intr@mark{}%
  \leavevmode%
  \begingroup%
    \setkeys{gcknbn@ruby}{#2}%
    \gcknbn@dima\f@size\p@\relax \divide\gcknbn@dima by \tw@%
    \def\tiny{\@setfontsize\tiny{\gcknbn@dima}{\z@}}%
    \setbox\z@=\hbox{#3}\setbox\@ne=\hbox{\tiny#4}%
    \setbox\tw@=\hbox{\tiny#5}%
    \ifdim\wd\@ne>\wd\tw@%侵入量計算
      \ifdim\wd\@ne>\zw%
        \global\gcknbn@intr@pre=\dimexpr(\wd\@ne-\zw)/2\relax%再読なし前方
        \global\gcknbn@intr@post=\dimexpr(\wd\@ne-\zw)/2\relax%再読なし後方
      \else%
        \global\gcknbn@intr@pre=0em\relax%
        \global\gcknbn@intr@post=0em\relax%
      \fi%
    \else%
      \ifdim\wd\tw@>\zw%
        \global\gcknbn@intr@pre=\dimexpr(\wd\tw@-\zw)/2\relax%再読あり前方
        \global\gcknbn@intr@post=\dimexpr(\wd\tw@-\zw)/2\relax%再読あり後方
      \else%
        \global\gcknbn@intr@pre=0em\relax%
        \global\gcknbn@intr@post=0em\relax%
      \fi%
    \fi%
    \ifdefstring{\gcknbn@ruby@intr@mark}{pre}{%
      \kern-\gcknbn@intr@pre%
    }{%
      \ifdefstring{\gcknbn@ruby@intr@mark}{both}{%
        \kern-\gcknbn@intr@pre%
      }{%
        % それ以外は何もなし
    }}%
    \gdef\gcknbn@rubybox@text{#4}%
    \gdef\gcknbn@rubybox@text@s{#5}%
    \global\gcknbn@rubybox@width=\wd\z@\relax%
    \IfBooleanTF{#1}{\gcknbn@furigana@okurigana@SMASH}{\gcknbn@furigana@okurigana}%
    }

\def\gcknbn@furigana@okurigana{%
  \futurelet\@let@token\gcknbn@@furigana@okurigana}

\def\gcknbn@furigana@okurigana@SMASH{%
  \futurelet\@let@token\gcknbn@@furigana@okurigana@SMASH}

\def\gcknbn@@furigana@okurigana{%
    \ifx\@let@token\gcknbn@okurigana%
      \global\gcknbn@furigana@width=\wd\@ne\relax%
      \global\gcknbn@furigana@width@s=\wd\tw@\relax%
      \dimen\z@=\wd\z@%
      \raisebox{\gcknbn@adjust@yokotate}{\hbox{%
        \vbox{\hbox to \dimen\z@{\box\@ne\hss}%
          \nointerlineskip\hbox to \dimen\z@{\hfil\box\z@\hfil}%
          \nointerlineskip\hbox to \dimen\z@{\box\tw@\hss}%
          }}}%
    \else
      \ifdim\wd\z@>\wd\@ne% \dimen\z@=max{\wd\z@,\wd\@ne,\wd\tw@}
        \dimen\z@=\wd\z@%
      \else%
        \dimen\z@=\wd\@ne%
      \fi%
      \ifdim\dimen\z@<\wd\tw@%
        \dimen\z@=\wd\tw@%
      \fi%
      \penalty\@lowpenalty%
      \raisebox{\gcknbn@adjust@yokotate}{\hbox{%
        \vbox{\hbox to \dimen\z@{\tiny\hfil\gcknbn@rubybox@text\hfil}%
          \nointerlineskip\hbox to \dimen\z@{\hfil\box\z@\hfil}%
          \nointerlineskip\hbox to \dimen\z@{\hss\box\tw@\hss}%
          }}}%
    \fi%
    \ifdefstring{\gcknbn@ruby@intr@mark}{post}{%
      \kern-\gcknbn@intr@post%
    }{%
    \ifdefstring{\gcknbn@ruby@intr@mark}{both}{%
        \kern-\gcknbn@intr@post%
      }{%
        % それ以外は何もなし
    }}%
  \endgroup}

\def\gcknbn@@furigana@okurigana@SMASH{%
    \ifx\@let@token\gcknbn@okurigana%
      \global\gcknbn@furigana@width=\wd\@ne\relax%
      \global\gcknbn@furigana@width@s=\wd\tw@\relax%
      \dimen\z@=\wd\z@%
      \raisebox{\gcknbn@adjust@yokotate}{\smash{\hbox{%
      \vbox{\hbox to \dimen\z@{\box\@ne\hss}%
        \nointerlineskip\hbox to \dimen\z@{\hfil\box\z@\hfil}%
        \nointerlineskip\hbox to \dimen\z@{\box\tw@\hss}%
        }}}}%
    \else%
      \ifdim\wd\z@>\wd\@ne\dimen\z@=\wd\z@\else\dimen\z@=\wd\@ne\fi%
      \penalty\@lowpenalty%
      \raisebox{\gcknbn@adjust@yokotate}{\smash{\hbox{%
        \vbox{\hbox to \dimen\z@{\tiny\hfil\gcknbn@rubybox@text\hfil}%
          \nointerlineskip\hbox to \dimen\z@{\hfil\box\z@\hfil}%
          \nointerlineskip\hbox to \dimen\z@{\hss\box\tw@\hss}%
          }}}}%
    \fi
    \ifdefstring{\gcknbn@ruby@intr@mark}{post}{%
      \kern-\gcknbn@intr@post%
    }{%
      \ifdefstring{\gcknbn@ruby@intr@mark}{both}{%
        \kern-\gcknbn@intr@post%
      }{%
        % それ以外は何もなし
    }}%
  \endgroup}

%% 訓点
\newdimen\gcknbn@okurigana@width \gcknbn@okurigana@width\z@
\newdimen\gcknbn@okurigana@width@s \gcknbn@okurigana@width@s\z@
\newdimen\gcknbn@kaeriten@width \gcknbn@kaeriten@width\z@

% post,bothをとれるが挙動は一緒
\define@key{gcknbn@okurigana}{intrusion}{\edef\gcknbn@okuri@intr@mark{#1}}

%%訓点送り仮名
\DeclareDocumentCommand{\gcknbn@okurigana}{ s O{} m O{\vphantom{羅}} }{%
  \nobreak\leavevmode%
  \gcknbn@adjust@yokotate@use%
  \global\gcknbn@okurigana@width\z@%
  \global\gcknbn@okurigana@width@s\z@%
  \global\def\gcknbn@okuri@intr@mark{}%
  \begingroup%
    \setkeys{gcknbn@okurigana}{#2}%
    \gcknbn@dima\f@size\p@\relax \divide\gcknbn@dima by \tw@%
    \def\tiny{\@setfontsize\tiny{\gcknbn@dima}{\z@}}%
    \setbox\z@=\hbox{\tiny #3}%
    \setbox\@ne=\hbox{\tiny #4}%
    \ifdim\gcknbn@furigana@width>0.9999\zw\relax%
      \global\gcknbn@okurigana@width=\dimexpr\gcknbn@furigana@width + \wd\z@ - 1\zw\relax%
    \else%
      \global\gcknbn@okurigana@width=\dimexpr\wd\z@ - .5\zw\relax%
    \fi%
    \ifdim\gcknbn@furigana@width@s>0.9999\zw\relax%
      \global\gcknbn@okurigana@width@s=\dimexpr\gcknbn@furigana@width@s + \wd\@ne - 1\zw\relax%
    \else%
      \global\gcknbn@okurigana@width@s=\dimexpr\wd\@ne - .5\zw\relax%
    \fi%
    \IfBooleanTF{#1}{%
    \smash{\hbox{%
      \raisebox{\gcknbn@adjust@yokotate}{\vbox{\hbox to \gcknbn@okurigana@width{%
        \ifdim\gcknbn@furigana@width>0.9999\zw\relax%
          \hspace*{\dimexpr\gcknbn@furigana@width - 1\zw\relax}%
        \else%
          \hspace*{-.5\zw}%
        \fi%
        \box\z@}%
        \nointerlineskip%
        \hbox to \gcknbn@okurigana@width{\hfil\vphantom{\char\euc"A1A1%"
          }\hfil}%
        \nointerlineskip%
        \hbox to \gcknbn@okurigana@width@s{%
        \ifdim\gcknbn@furigana@width@s>0.9999\zw\relax%
          \hspace*{\dimexpr\gcknbn@furigana@width@s - 1\zw\relax}%
        \else%
          \hspace*{-.5\zw}%
        \fi%
        \box\@ne}%
        }}}}}%
    {%
      \hbox{%
      \raisebox{\gcknbn@adjust@yokotate}{\vbox{\hbox to \gcknbn@okurigana@width{%
        \ifdim\gcknbn@furigana@width>0.9999\zw\relax
          \hspace*{\dimexpr\gcknbn@furigana@width - 1\zw\relax}%
        \else
          \hspace*{-.5\zw}%
        \fi
        \box\z@}%
        \nointerlineskip%
        \hbox to \gcknbn@okurigana@width{\hfil\vphantom{\char\euc"A1A1%"
          }\hfil}%
        \nointerlineskip%
        \hbox to \gcknbn@okurigana@width@s{%
        \ifdim\gcknbn@furigana@width@s>0.9999\zw\relax
          \hspace*{\dimexpr\gcknbn@furigana@width@s - 1\zw\relax}%
        \else%
          \hspace*{-.5\zw}%
        \fi%
        \box\@ne}%
        }}}%
    }%
    \global\gcknbn@furigana@width=\z@\relax%
    \gcknbn@okurigana@intr%
    \gcknbn@okurigana@kaeriten%
    }

\def\gcknbn@okurigana@kaeriten{%
  \futurelet\@let@token\gcknbn@@okurigana@kaeriten}
\def\gcknbn@@okurigana@kaeriten{%
    \ifx\@let@token、
      \gcknbn@okurigana@kutoten@skip
      \global\gcknbn@okurigana@width=\z@
    \else\ifx\@let@token。
      \gcknbn@okurigana@kutoten@skip
      \global\gcknbn@okurigana@width=\z@
    \else\ifx\@let@token\gcknbn@kaeriten
      \gcknbn@okurigana@kaeriten@skip
    \else%
      \ifdefstring{\gcknbn@okuri@intr@mark}{post}{%
        \kern-\gcknbn@intr@post%
      }{%
      \ifdefstring{\gcknbn@okuri@intr@mark}{both}{%
          \kern-\gcknbn@intr@post%
        }{%
          % それ以外は何もなし
      }}%
    \fi\fi\fi
  \endgroup}
\def\gcknbn@okurigana@kutoten@skip{%
  \ifgcknbn@tdir\hspace*{-\gcknbn@okurigana@width}\else\fi%
}
\def\gcknbn@okurigana@kaeriten@skip{%
  \ifdim\gcknbn@okurigana@width>\gcknbn@okurigana@width@s%
    \hspace*{-\gcknbn@okurigana@width}%
  \else%
    \hspace*{-\gcknbn@okurigana@width@s}%
  \fi
}
\def\gcknbn@okurigana@intr{%
  \ifdim\gcknbn@okurigana@width>\gcknbn@okurigana@width@s%
    \global\gcknbn@intr@post=\gcknbn@okurigana@width%
  \else%
    \global\gcknbn@intr@post=\gcknbn@okurigana@width@s%
  \fi
}

%%訓点返り点
\newcommand*\gcknbn@kaeriten{%
  \@ifnextchar-{\gcknbn@tateten@kaeriten@hh}{%
  \@ifnextchar‐{\gcknbn@tateten@kaeriten@zh}{%
    \gcknbn@@kaeriten}}}
% \gcknbn@@kaeriten}

% post,bothをとれるが挙動は一緒
\define@key{gcknbn@kaeriten}{intrusion}{\edef\gcknbn@kaeri@intr@mark{#1}}

\NewDocumentCommand\gcknbn@@kaeriten{ O{} m }{%
  \nobreak\leavevmode
  \gcknbn@adjust@kaeri@use%
  \global\def\gcknbn@kaeri@intr@mark{}%
  \setkeys{gcknbn@kaeriten}{#1}%
  \begingroup
    \gcknbn@dima\f@size\p@\relax \divide\gcknbn@dima by \tw@
    \def\tiny{\@setfontsize\tiny{\gcknbn@dima}{\z@}}%
    \setbox\z@=\hbox{\tiny #2}%
    \global\gcknbn@kaeriten@width=\wd\z@\relax
    \ifdim\wd\z@>\gcknbn@intr@post%
      \global\gcknbn@intr@post=\wd\z@%
    \fi%
    \smash{\lower\gcknbn@adjust@kaeri\hbox{\box\z@\hss}}%
  \endgroup%
  \gcknbn@kaeriten@kutoten%
  }

\def\gcknbn@kaeriten@kutoten{\futurelet\@let@token\gcknbn@@kaeriten@kutoten}
\def\gcknbn@@kaeriten@kutoten{%
  \ifx\@let@token、
    \gcknbn@kaeriten@kutoten@skip
    \global\gcknbn@kaeriten@width=\z@
  \else\ifx\@let@token。
    \gcknbn@kaeriten@kutoten@skip
    \global\gcknbn@kaeriten@width=\z@
  \else%
    \gcknbn@kaeriten@okurigana@skip
    \ifdefstring{\gcknbn@kaeri@intr@mark}{post}{%
      \kern-\gcknbn@intr@post%
    }{%
    \ifdefstring{\gcknbn@kaeri@intr@mark}{both}{%
        \kern-\gcknbn@intr@post%
      }{%
        % それ以外は何もなし
    }}%
  \fi\fi
}
\def\gcknbn@kaeriten@kutoten@skip{%
  \ifgcknbn@tdir%
    \hspace*{-\gcknbn@kaeriten@width}%
  \fi
}
\def\gcknbn@okurigana@kutoten@skip{%
  \hspace*{-\gcknbn@okurigana@width}}
\def\gcknbn@kaeriten@okurigana@skip{%
  \ifdim\gcknbn@okurigana@width>\gcknbn@kaeriten@width
    \hspace*{\dimexpr\gcknbn@okurigana@width - \gcknbn@kaeriten@width}%
  \fi
  \ifdim\gcknbn@okurigana@width<\gcknbn@okurigana@width@s%
    \hspace*{\gcknbn@okurigana@width@s - \gcknbn@kaeriten@width}%
  \else
  \fi
}

\def\gcknbn@tateten@kaeriten@hh-{\gcknbn@@tateten@kaeriten}
\def\gcknbn@tateten@kaeriten@zh‐{\gcknbn@@tateten@kaeriten}
\newcommand*\gcknbn@@tateten@kaeriten[1]{%
  \nobreak\leavevmode
  \begingroup
    \gcknbn@dima\f@size\p@\relax \divide\gcknbn@dima by \tw@
    \def\tiny{\@setfontsize\tiny{\gcknbn@dima}{\z@}}%
    \setbox\z@=\hbox{\tiny\char"3190}%"
    \global\gcknbn@kaeriten@width=\wd\z@\relax
    \smash{\lower.35\zw\hbox{%
    \vbox{\hbox to \gcknbn@kaeriten@width{\hfil\box\z@\hfil}%
    \vspace*{-.1\zw}%
    \nointerlineskip\hbox to \gcknbn@kaeriten@width{\tiny\hfil#1\hfil}}}}%
  \endgroup}


%% Finaly, provide \<prefix>ruby, \<prefix>okurigana, \<prefix>kaeriten
\expandafter\let\csname\gcknbn@prefix ruby\endcsname\gcknbn@ruby
\expandafter\let\csname\gcknbn@prefix okurigana\endcsname\gcknbn@okurigana
\expandafter\let\csname\gcknbn@prefix kaeriten\endcsname\gcknbn@kaeriten

%% 短縮マクロ
\expandafter\let\csname 振り\endcsname\gcknbn@ruby
\expandafter\let\csname 送り\endcsname\gcknbn@okurigana
\expandafter\let\csname 返り\endcsname\gcknbn@kaeriten

\NewDocumentCommand{\IchiRe}{}{%
  \ifgcknbn@tdir
    \hspace{-.5ex}一\hspace{-1.9ex}レ%
  \else%
    \raisebox{.5ex}{一}\llap{レ}%
  \fi%
  }
\NewDocumentCommand{\JyouRe}{}{%
  \ifgcknbn@tdir
    上\hspace{-.7ex}レ%
  \else%
    \raisebox{1.7ex}{上}\llap{レ}%
  \fi%
  }
\NewDocumentCommand{\KouRe}{}{%
  \ifgcknbn@tdir
    甲\hspace{-.4ex}レ%
  \else%
    \raisebox{1.9ex}{甲}\llap{レ}%
  \fi%
  }
\NewDocumentCommand{\TenRe}{}{%
  \ifgcknbn@tdir
    天\hspace{-.4ex}レ%
  \else%
    \raisebox{1.9ex}{天}\llap{レ}%
  \fi%
  }
\NewDocumentCommand{\KanHyphen}{}{\symbol{"2015}}

\endinput
