%% file oblivoir-misc.sty
%% part of oblivoir class
%%
%% (c) 2022-2025 Kangsoo Kim 
%%
%% 2022-03-01, 03-06
%% 2022-04-07, bugfix
%% 2023-02-14, tikzpagenodes-conflict fixed
%% 2023-03-08, hverse environment
%% 2025-02-07, hverse corrected
%% 2025-04-04, stdclasses support
%%
\ProvidesPackage{oblivoir-misc}[2025/04/08 version0.6.1]

\RequirePackage{etoolbox}

\ExplSyntaxOn

\bool_new:N \opt_miscstdclasses_bool
\@ifclassloaded{oblivoir}{
	\bool_set_false:N \opt_miscstdclasses_bool
}{
	\bool_set_true:N  \opt_miscstdclasses_bool
}

\ExplSyntaxOff

%%% \texthl
%%% from oblivoir-patch2.sty, 2021
%%% \texthl command
%%% highlighs text in XeTeX
%%%	 * now, in mathmode it doesn't work.
%%%. * now, lualatex is not supported.
%%% 
%%% \obhlcolor, \obhlheight, \obhlraisedim can be redefined by user.
\ExplSyntaxOn
\msg_new:nnn { texthl } { luatexkocompat } { Do~not~use~ulem~with~luatexko. }
\msg_new:nnn { texthl } { ulemcolor      } { ulem~and~(x)color~required. }
\bool_new:N \g_ulemloaded_bool
\bool_new:N \g_xcolorloaded_bool
\dim_new:N \obhlraisedim
\dim_new:N \obhlheight
\AtEndPreamble{
	\@ifpackageloaded{color}
		{ \bool_set_true:N \g_xcolorloaded_bool }
		{ 
		  \@ifpackageloaded{xcolor}
			{ \bool_set_true:N \g_xcolorloaded_bool }	
			{ \bool_set_false:N \g_xcolorloaded_bool }
		}
	\@ifpackageloaded{ulem}
		{ \bool_set_true:N \g_ulemloaded_bool }
		{ \bool_set_false:N \g_ulemloaded_bool 
		  \let\UL@protected\protected
		}
  \sys_if_engine_luatex:TF {
    \cs_if_exist:NF \obhlcolor { \def\obhlcolor{yellow!80} }
    \dim_set:Nn \obhlraisedim { -.1\baselineskip }
    \dim_set:Nn \obhlheight { 1.01em }
    \ProvideDocumentCommand\texthl{}{
      \bool_if:NTF \g_ulemloaded_bool
      {
      	\msg_error:nn { texthl } { luatexkocompat }
	  }
	  {
	    \hbox_set:Nn \l_tmpa_box { \color{\obhlcolor}\rule{1pt}{\obhlheight} }
    	\markoverwith{ 
	  	  \box_move_up:nn { \obhlraisedim } { \box_use:N \l_tmpa_box }
	    }
      }
    }
  }{
%  \sys_if_engine_xetex:TF {
        \bool_if:nTF 
        {
        	\g_ulemloaded_bool
        	&&
        	\g_xcolorloaded_bool
        }
        {
        	\cs_if_exist:NF \obhlcolor
        	{
        		\def\obhlcolor{yellow!80}
        	}
        	\dim_set:Nn \obhlraisedim { -.1\baselineskip }
        	\dim_set:Nn \obhlheight { 1.01em }
            \UL@protected\def \texthl 
            {
                \bgroup
                \hbox_set:Nn \l_tmpa_box {\color{\obhlcolor}\rule{1pt}{\obhlheight}}
                \box_set_ht:Nn \l_tmpa_box { \c_zero_dim }
                \box_set_dp:Nn \l_tmpa_box { \c_zero_dim }
                \markoverwith{%
                	\kern-.1pt
                	\box_move_up:nn { \obhlraisedim } { \box_use:N \l_tmpa_box }
                }\ULon
            }
        }
        {
        	\def\texthl {
        		\msg_error:nn { texthl } { ulemcolor }
        	}
        }
%	}
%	{
%		\msg_error:nn { texthl } { pdftex~not~supported }
%	}
  }
}

\bool_if:NT \opt_miscstdclasses_bool { \endinput }

\ExplSyntaxOff
%%% tikz pagenode
%%% see http://www.ktug.org/xe/index.php?mid=KTUG_QnA_board&document_srl=253542

\AtEndPreamble{%
    \@ifpackageloaded{pgf}{%
        \newcommand\setpagenode{%
            \expandafter\def\csname pgf@sh@ns@current page\endcsname{rectangle}%
            \strictpagecheck%
            \checkoddpage%
            \ifoddpage%
                \expandafter\def\csname pgf@sh@np@current page\endcsname{%
                  \def\southwest{\pgfpoint{\stockwidth-\paperwidth-\trimedge}%
                                          {\stockheight-\trimtop-\paperheight}}%
                  \def\northeast{\pgfpoint{\stockwidth-\trimedge}{\stockheight-\trimtop}}%
                }%
            \else
                \expandafter\def\csname pgf@sh@np@current page\endcsname{%
                  \def\southwest{\pgfpoint{\trimedge}{\stockheight-\trimtop-\paperheight}}%
                  \def\northeast{\pgfpoint{\trimedge+\paperwidth}{\stockheight-\trimtop}}%
                }%
            \fi
            \expandafter\def\csname pgf@sh@nt@current page\endcsname{{1}{0}{0}{1}{0pt}{0pt}}%
            \expandafter\def\csname pgf@sh@pi@current page\endcsname{pgfpageorigin}}
        
        \pgfkeys{/tikz/overlay/.add code={}{\setpagenode}}
      }{}
}

%%% showtrims and tikzpagenode
%%% http://www.ktug.org/xe/index.php?mid=KTUG_open_board&document_srl=229299
\AtEndPreamble{
	\ifshowtrims
	\@ifpackageloaded{tikzpagenodes}{%
		\def\current@textarea@top{(\paperheight-\textheight-\lowermargin-\trimtop)}}{}
	\fi
}

%%%%
%%%% 2023-03-08, hverse
%%%% 2024-06, revised
%%%% write external file and read in.
%%%%

\ExplSyntaxOn

\tl_set:Nx \temp_filename_tl { \jobname.hverse }
\ior_new:N \l_txp_file

\skip_set:Nn \stanzaskip { .63\onelineskip }

\ProvideDocumentEnvironment {hverse} {}
    {
    	\exp_args:Nx \verbatimoutput {\temp_filename_tl}
    }
    {
    	\endverbatimoutput
    	
    	\begin{verse}
    	
    	\exp_args:NNx \ior_open:Nn \l_txp_file {\temp_filename_tl}
    	
    	\ior_str_map_inline:Nn \l_txp_file
    	{		
    		\tl_if_empty:nTF { ##1 } 
    		{
    			\par\vskip\stanzaskip
    		}
    		{
    			\regex_match:nnTF { \\ } { ##1 }
    				{ \bool_set_true:N \l_tmpa_bool }
    				{ \bool_set_false:N \l_tmpa_bool }
    			\bool_if:nTF { \l_tmpa_bool || \sys_if_engine_pdftex_p: }
    			{
    	    		\tl_set:Nn \l_tmpa_tl { ##1 }
        			\regex_replace_all:nnN { \s } { \  } \l_tmpa_tl
    				\tl_set_rescan:NnV \l_tmpb_tl { \cctab_select:N \c_document_cctab } \l_tmpa_tl
    				\tl_use:N \l_tmpb_tl \\
    			}
    			{
    				##1 \\
    			}
    		}
    	}
    	\ior_close:N \l_txp_file
    	
    	\end{verse}
    }

%
%\NewDocumentEnvironment {hverse} {o}
%{
%	\exp_args:Nx \verbatimoutput {\temp_filename_tl}
%}
%{
%	\endverbatimoutput
%	
%	\begin{verse}
%	
%	\exp_args:NNx \ior_open:Nn \l_txp_file {\temp_filename_tl}
%	
%	\ior_str_map_inline:Nn \l_txp_file
%	{
%		\tl_if_empty:nTF { ##1 } 
%		{
%			\par\vskip\stanzaskip
%		}
%		{
%			##1 \\
%		}
%	}
%	\ior_close:N \l_txp_file
%	
%	\end{verse}
%}


\ExplSyntaxOff


\endinput
