% This file is part of the CTAN package named plain-grid.
% 
%   plaingridB.tex: text, math, insertions,
%                   and support for boxes in horizontal mode
%   Version 1.0, 05.05.2026
%
%   Copyright (C) 2026  Udo Wermuth (author)
%
%   This program is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published by
%   the Free Software Foundation, either version 3 of the License, or
%   (at your option) any later version.
%
%   This program is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with this program.  If not, see <http://www.gnu.org/licenses/>.
%

\input plaingridI.tex
\newif\ifGRIDvtop % false/true for vbox/vtop
\newif\ifGRIDmax % tallest/deepest vbox/vtop
\def\gridvbox{\GRIDvtopfalse \GRIDbuildbox}
\def\gridvtop{\GRIDdim=-\GRIDgrid % init for
 \GRIDvtoptrue \GRIDbuildbox}% \vtop's depth
\def\GRIDbuildbox#1{% #1: 1 signals the max
 \def\GRIDtmp{1}\def\GRIDnext{#1}%
 \ifx\GRIDtmp\GRIDnext \GRIDmaxtrue
 \else \GRIDmaxfalse \fi
 \setbox\GRIDbox=\vbox\GRIDv}% start the box
\def\gridboxann#1{% #1: lines in next vbox
 \ifvmode \GRIDprevgrid \GRIDlinesavailable
  \ifnum\GRIDfreelines=0 \grideject
  \else \ifnum\GRIDfreelines<#1\GRIDlog0{%
    GRID: vbox moved; check}\grideject\fi\fi
 \else \unhcopy\GRIDsbox % in hmode; a strut
  \vadjust{\kern-\dp\GRIDsbox
   \GRIDprevgrid \GRIDlinesavailable
   \advance\GRIDfreelines by -1 %  this line
   \ifnum\GRIDfreelines=0 \penalty-10000
   \else\ifnum\GRIDfreelines<#1%    no space
    \GRIDlog0{GRID: vbox moved; check}%
    \kern\GRIDfreelines\GRIDgrid
    \penalty-10000 \fi\fi}\fi\ignorespaces}
\def\GRIDv{\bgroup \GRIDhb}%  \vbox of \hbox
\def\GRIDhb#{% put text in \hbox
 \global\advance\GRIDdim by\GRIDgrid % count
 \hbox\bgroup \unhcopy\GRIDsbox    % a strut
 \aftergroup\GRIDnxt \let\GRIDtmp= }
\def\GRIDnxt{\futurelet\GRIDtmp\GRIDeval}
\def\GRIDeval{% another \hbox? end of \vbox?
 \ifcat\GRIDtmp\bgroup \let\GRIDnext=\GRIDhb
 \else \let\GRIDnext=\GRIDboxend % end \vbox
 \fi \GRIDnext}
\def\GRIDboxend{\egroup % \vbox ends, output
 \ifGRIDvtop \GRIDboxheight\GRIDbox(:\vfil)%
  \setbox\GRIDbox=% transform \vbox -> \vtop
   \vtop to \GRIDgrid{%  keep interline glue
    \unvbox\GRIDbox\vss}%
 \else \GRIDboxheight\GRIDbox(\vfil:)% \vbox
 \fi \ifGRIDmax\ifGRIDvtop \GRIDvtop \fi\fi
 \GRIDmaxfalse \GRIDvtopfalse \box\GRIDbox}
\def\GRIDvtop{\dimen0=\GRIDdim
 \advance\dimen0 by \GRIDgrid
 \ifvmode \par \penalty0 \GRIDlinesavailable
  \ifdim\GRIDfreelines\GRIDgrid>\dimen0
  \else \GRIDlog0{GRID: vtop moved; check}%
   \grideject \noindent  % vtop in this line
   \vadjust{\kern\GRIDdim}\fi
 \else \advance\dimen0 by \pagetotal % hmode
  \ifdim\dimen0<\pagegoal
   \vadjust{\kern\GRIDdim}%   middle of page
  \else \GRIDlog0{GRID: vtop moved; check}%
   \vadjust{\par \penalty0 \kern\GRIDdim}%
   \pagetotal=\pagegoal\fi\fi}
