%% memucs-interword-x.sty
%%
%% Copyright (c) 2007-2013 Kangsoo Kim <karnes at ktug org>
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%  http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2006/05/20 or later.
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{memucs-interword-x}[2009/04/02]
\RequirePackage{ifluatex}
\RequirePackage{ifxetex}

\def\memucsinterwordskip{}
\def\memucsinterwordchapterskip{}
\def\memucshangulskips{}

\newif\if@nonfrench\@nonfrenchfalse
\DeclareOption{nonfrench}{%
    \@nonfrenchtrue}
\DeclareOption{HWP}{%
	\def\memucsinterwordskip{\HWPinterwordskip}}
\DeclareOption{default}{%
	\@nonfrenchtrue\def\memucsinterwordskip{\DEFAULTskips}%\interHchar{0pt}
	}
\DeclareOption{wider}{}
%	\def\memucshangulskips{\widerhangul}}
\DeclareOption{narrower}{}
%	\def\memucshangulskips{\narrowerhangul}}
%\DeclareOption*{%
%	\def\memucsinterwordskip{\DEFAULTskips}%
%}

%\newlength\dimen@skip
%\setlength\dimen@skip{.1pt}

%% finemath active.
%\ifluatex\else\ifxetex\else
%  \if@dhucsfinemath\else\@dhucsfinemathtrue\fi
%\fi\fi

%%% inter hchar
\newcommand*\interHchar[1]{%
  \ifluatex
    \@luatex@setinterhangulskip{#1}%
  \else\ifxetex
    \@xetex@setinterhangulskip{#1}%
  \else
    \@setinterhangulskip{#1}%
  \fi\fi
}

\newcommand*\DefaultInterHchar{%
  \interHchar{-.04}%
}

\newcommand*\@luatex@setinterhangulskip[1]{%
\directlua{
  fmconfig.hangul_hangul = #1 % ÇÑ±Û »çÀÌ ÀÚ°£
}}

\newcommand*\@xetex@setinterhangulskip[1]{%
  \def\xetexkointerhchar{#1em}%
}

\newcommand*\@setinterhangulskip[1]{%
  \setInterHangulSkip{#1em}%
}

\newcommand*\interhword[4][\@empty]{%
   \ifx#1\@empty\setinterwordskip{#2}{#3}{#4}\else
	   \setinterwordskip[#1]{#2}{#3}{#4}\fi
}

%%%\def\narrowerhangul{%
%%%	\def\memucshangulskips{%
%%%		\interHchar{-.5pt}% 
%%%	}%
%%%}
%%%\def\widerhangul{%
%%%	\def\memucshangulskips{%
%%%		\interHchar{1pt}% 
%%%	}%
%%%}

\newdimen\BASE@FONTsize
\BASE@FONTsize=\f@size\p@

\def\HWPinterwordskip{%
  \def\memucshangulskips{%
    \DefaultInterHchar
  }%
  \def\memucsinterwordskip{%
    \spaceskip = .475\BASE@FONTsize plus .25\BASE@FONTsize minus .1331\BASE@FONTsize
    \if@nonfrench
      \xspaceskip = .6\BASE@FONTsize plus .2\BASE@FONTsize minus .1\BASE@FONTsize
    \fi
  }%
  \def\memucsinterwordchapterskip{%
    \spaceskip = .5\BASE@FONTsize plus .1\BASE@FONTsize minus .05\BASE@FONTsize
    \if@nonfrench
      \xspaceskip = .65\BASE@FONTsize plus .2\BASE@FONTsize minus .1\BASE@FONTsize
    \fi
  }%
}

\def\setinterwordskip{\@ifnextchar[\setinterwordskip@\setinterwordskip@@}
\def\setinterwordskip@[#1]#2#3#4{%
  \def\memucsinterwordskip{%
    \spaceskip = #2\BASE@FONTsize plus #3\BASE@FONTsize minus #4\BASE@FONTsize
  \if@nonfrench
    \xspaceskip = #1\BASE@FONTsize plus #3\BASE@FONTsize minus #4\BASE@FONTsize
  \fi
 }%
}

\def\setinterwordskip@@#1#2#3{%
  \def\memucsinterwordskip{%
   \spaceskip = #1\BASE@FONTsize plus #2\BASE@FONTsize minus #3\BASE@FONTsize
  \if@nonfrench
   \xspaceskip = .6\BASE@FONTsize plus #2\BASE@FONTsize minus #3\BASE@FONTsize
  \fi
 }%
}

\def\DEFAULTskips{%
  \DefaultInterHchar
  \def\memucsinterwordskip{%
   \spaceskip = .413\BASE@FONTsize plus .1\BASE@FONTsize minus .1\BASE@FONTsize
   \if@nonfrench
   \xspaceskip = .55\BASE@FONTsize plus .11\BASE@FONTsize minus .1\BASE@FONTsize
   \fi
  }%
  \def\memucsinterwordchapterskip{%
   \spaceskip = .46\BASE@FONTsize plus .11\BASE@FONTsize minus .1\BASE@FONTsize
   \if@nonfrench
     \xspaceskip = .575\BASE@FONTsize plus .1\BASE@FONTsize minus .1\BASE@FONTsize
   \fi
  }%
}

\ProcessOptions\relax

\def\verbatim@font{\m@mverbfont\spaceskip\z@
                   \hyphenchar\font\m@ne
                   \let\do\do@noligs
                   \verbatim@nolig@list} 

\def\narrowerinterhword{%
  \spaceskip=.331\BASE@FONTsize plus .111\BASE@FONTsize minus .1\BASE@FONTsize
  \if@nonfrench
    \xspaceskip=.466\BASE@FONTsize plus .05\BASE@FONTsize minus .05\BASE@FONTsize
  \fi
} 
 
\def\memucsinterwordhook{\memucsinterwordskip\memucshangulskips}%
\def\memucsinterwordchapterskiphook{\memucsinterwordchapterskip\memucshangulskips}%
\def\memucsfninterwordhook{\narrowerinterhword}%

% engtext env
\@ifundefined{engtext}%
 {%
   \newenvironment{engtext}%
     {\begingroup\spaceskip=\fontdimen2\font plus\fontdimen3\font minus\fontdimen4\font}%
     {\endgroup}%
 }{}

% engtxt
\newskip\ORIG@spaceskip
\def\engtxt{\ORIG@spaceskip=\spaceskip
  \spaceskip=\fontdimen2\font plus\fontdimen3\font minus\fontdimen4\font}
\def\endengtxt{\spaceskip=\ORIG@spaceskip}

\endinput

%% CHANGE LOG
% 2009/04/02. small alterations.
% 2005/01/07. code clearance 
% 2005/02/08. reset interwordchapterskip values
% 2005/05/17. engtext env. added.
% 2005/07/30. replace em by \BASE@FONTsize
% 2005/08/05. nonfrench option introduced.
% 2005/11/17. \interhword bug fixed.
% 2007/06/03. dhucs 4.0 hfontspec applied
