%%% kstextks.sty
%%%
%%% version 0.3
%%% written by nanim
%%%
\ProvidesPackage{kstextks}[2025/04/08 v0.3.1 KS X 1001 Special Characters]

\RequirePackage{xparse,expl3,l3keys2e}

\ExplSyntaxOn

\bool_new:N \ks_symfont_bool
\bool_new:N \ks_onlyopt_bool
\clist_new:N \ks_only_clist

\keys_define:nn { kstextks }
{
  hcr		.bool_set:N = \ks_hcr_bool,
  symfont 	.code:n = { \bool_set_true:N \ks_symfont_bool \tl_set:Nn \ks_sym_font { #1 } },
  units		.bool_set:N = \ks_units_bool,
  puncts	.bool_set:N = \ks_puncts_bool,
  symbols	.bool_set:N = \ks_symbols_bool,
  all		.code:n = { \bool_set_true:N \ks_puncts_bool \bool_set_true:N \ks_symbols_bool \bool_set_true:N \ls_units_bool },
  none		.code:n = { \bool_set_false:N \ks_puncts_bool \bool_set_false:N \ks_symbols_bool \bool_set_false:N \ls_units_bool },
  useonly	.code:n = { \bool_set_true:N \ks_onlyopt_bool \clist_set:Nn \ks_only_clist {#1} }
}

\keys_set:nn { kstextks }
{
  hcr = false,
  units = false,
  puncts = true,
  symbols = true,
}

\ProcessKeysOptions { kstextks }

\bool_if:NT \ks_hcr_bool
{
  \bool_set_true:N \ks_symfont_bool
  \tl_set:Nn \ks_sym_font { HANBatang-LVT.ttf }
}

\RequirePackage{fontspec}
\RequirePackage{amsmath}

\clist_new:N \provided_commands_clist

\cs_new:Npn \provide_command_ks_text:nn #1 #2
{
  \expandafter \providecommand \cs:w textks #1 \cs_end:
  {
    \group_begin:
    \bool_if:NT \ks_symfont_bool
    {
       \exp_args:Nf \fontspec{ \ks_sym_font }
       \cs_if_exist:NT \hangulfontspec
       {
	\exp_args:Nf \adhochanjafont{ \ks_sym_font }
	\exp_args:Nf \adhochangulfont{ \ks_sym_font }
       }
    }
    #2
    \group_end:
  }
  
  \clist_put_right:Nn \provided_commands_clist { #1 }
}

%\ior_open:Nn \g_tmpa_ior { kstextks_data_a.txt }
%\ior_map_inline:Nn \g_tmpa_ior
%{
%	\bool_if:NTF \ks_onlyopt_bool
%	{
%    	\tl_set:Nn \l_tmpa_tl { #1 }
%    	\regex_replace_once:nnN { ^\{(.*?)\} \{ (.*)$ } { \1 } \l_tmpa_tl
%    	\tl_set_eq:NN \teststringabc \l_tmpa_tl		%% debug
%		
%		\clist_if_in:NVT \ks_only_clist \l_tmpa_tl
%		{
%			\provide_command_ks_text:nn #1
%		}
%	}
%	{
%		\provide_command_ks_text:nn #1
%	}
%}
%\ior_close:N \g_tmpa_ior

\bool_if:NT \ks_units_bool
{
    \ior_open:Nn \g_tmpa_ior { kstextks_data_units.txt }
    \ior_map_inline:Nn \g_tmpa_ior
    {
    	\bool_if:NTF \ks_onlyopt_bool
    	{
        	\tl_set:Nn \l_tmpa_tl { #1 }
        	\regex_replace_once:nnN { ^\{(.*?)\} \{ (.*)$ } { \1 } \l_tmpa_tl
%%%%%        	\tl_set_eq:NN \teststringabc \l_tmpa_tl		%% debug
    		
    		\clist_if_in:NVT \ks_only_clist \l_tmpa_tl
    		{
    			\provide_command_ks_text:nn #1
    		}
    	}
    	{
    		\provide_command_ks_text:nn #1
    	}
    }
    \ior_close:N \g_tmpa_ior
}

\bool_if:NT \ks_symbols_bool
{
    \ior_open:Nn \g_tmpa_ior { kstextks_data_symbols.txt }
    \ior_map_inline:Nn \g_tmpa_ior
    {
    	\bool_if:NTF \ks_onlyopt_bool
    	{
        	\tl_set:Nn \l_tmpa_tl { #1 }
        	\regex_replace_once:nnN { ^\{(.*?)\} \{ (.*)$ } { \1 } \l_tmpa_tl
%%%%%        	\tl_set_eq:NN \teststringabc \l_tmpa_tl		%% debug
    		
    		\clist_if_in:NVT \ks_only_clist \l_tmpa_tl
    		{
    			\provide_command_ks_text:nn #1
    		}
    	}
    	{
    		\provide_command_ks_text:nn #1
    	}
    }
    \ior_close:N \g_tmpa_ior
}

\bool_if:NT \ks_puncts_bool
{
    \ior_open:Nn \g_tmpa_ior { kstextks_data_puncts.txt }
    \ior_map_inline:Nn \g_tmpa_ior
    {
    	\bool_if:NTF \ks_onlyopt_bool
    	{
        	\tl_set:Nn \l_tmpa_tl { #1 }
        	\regex_replace_once:nnN { ^\{(.*?)\} \{ (.*)$ } { \1 } \l_tmpa_tl
%%%%%        	\tl_set_eq:NN \teststringabc \l_tmpa_tl		%% debug
    		
    		\clist_if_in:NVT \ks_only_clist \l_tmpa_tl
    		{
    			\provide_command_ks_text:nn #1
    		}
    	}
    	{
    		\provide_command_ks_text:nn #1
    	}
    }
    \ior_close:N \g_tmpa_ior
}


\cs_new:Npn \kstext_dispcs:n #1
{
  \texttt { \textbackslash textks#1 }
}

\cs_new:Npn \kstext_dispfrm:n #1
{
  \fbox { \use:c { textks #1 } }
}

\NewDocumentCommand \DisplayAllSymbols {  }
{
   \clist_map_inline:Nn \provided_commands_clist
   {
      \makebox [1.8em] [l] { \kstext_dispfrm:n { ##1 } }
      \space 
      \kstext_dispcs:n { ##1 } 
      \par 
   }
}

\ExplSyntaxOff 

\providecommand*\kssim{%
   \ifmmode\;\text{\fontspec{NanumMyeongjo}∽}\;\else{\fontspec{NanumMyeongjo}∽}\fi
}

\endinput
