%% File `memhangul-x.sty`
%%
%% (C) Copyright 2006-2023 Kangsoo Kim <modviv2k15 at gmail com>
%%
%% 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.
%%
%%% file `memhangul-x.sty`
%%    part of oblivoir v2
%%
\NeedsTeXFormat{LaTeX2e}
\def\filedate{2023/03/08}
\def\fileversion{23.03}
\ProvidesPackage{memhangul-x}[\filedate Hangul Typography for (x)oblivoir]
%\typeout{Hangul Typography for (x)oblivoir}

%% Options :

\providecommand\DisabledOption[1]{%
  \typeout{[#1] option to this class has been disabled!}}

%%%% DisabledOptions
\newif\if@interwordHWP\@interwordHWPfalse
\DeclareOption{interwordHWP}{\DisabledOption{interwordHWP}}
\newif\if@interworddefault\@interworddefaultfalse
%\DeclareOption{interworddefault}{\DisabledOption{interworddefault}}
\DeclareOption{interworddefault}{\@interworddefaulttrue}
%\newif\if@finemath\@finemathfalse
\DeclareOption{finemath}{\DisabledOption{finemath}}
%\newif\if@nojosa\@nojosafalse
\DeclareOption{nojosa}{\DisabledOption{nojosa}}
%\newif\if@dvips{\@dvipsfalse}
\DeclareOption{dvips}{\DisabledOption{dvips}}
\DeclareOption{strictcharcheck}{\DisabledOption{strictcharcheck}}
\DeclareOption{adjustmath}{\DisabledOption{adjustmath}}

%%%% active options
\newif\if@hsspace\@hsspacetrue
\DeclareOption{nosetspace}{\@hsspacefalse}

\newcommand\nosetspace{%
  \@hsspacefalse
}

%\RequirePackage{ifluatex}
%\RequirePackage{ifxetex}

\newif\if@hsspacenoquote\@hsspacenoquotetrue
\DeclareOption{quotespacing}{\@hsspacenoquotefalse}
\newif\if@nonfrench\@nonfrenchfalse
\DeclareOption{nonfrench}{\@nonfrenchtrue}
\newif\if@hanja\@hanjafalse
\DeclareOption{hanja}{\@hanjatrue}
\newif\if@mathdisp\@mathdisptrue
\DeclareOption{mathdisp}{\@mathdispfalse}

\newif\if@arabicfrontmatter{\@arabicfrontmatterfalse}
\DeclareOption{arabicfront}{\@arabicfrontmattertrue}
\newif\if@defaulthangulpagestyle{\@defaulthangulpagestylefalse}
\DeclareOption{hangulpagestyle}{\@defaulthangulpagestyletrue}
\newif\if@pdfbookmark{\@pdfbookmarkfalse}
\DeclareOption{pdfbookmark}{\@pdfbookmarktrue}
\newif\if@polyglos\@polyglosfalse
\newif\if@babelhangul\@babelhangulfalse
\newif\if@babelvacant\@babelvacantfalse
\DeclareOption{polyglossia}{\@polyglostrue\@babelhangulfalse\@babelvacantfalse}
\DeclareOption{babelhangul}{\@polyglostrue\@babelhangultrue\@babelvacantfalse}
\DeclareOption{babelvacant}{\@polyglostrue\@babelhangultrue\@babelvacanttrue}

\ifxetex
%%% xetexko options
\DeclareOption{gremph}{\PassOptionsToPackage{\CurrentOption}{xetexko-var}}
\DeclareOption{itemph}{\PassOptionsToPackage{\CurrentOption}{xetexko-var}}
%%% new option
\DeclareOption{latinquote}{\PassOptionsToPackage{\CurrentOption}{xetexko-var}}
\DeclareOption{oldhangul}{\PassOptionsToPackage{\CurrentOption}{xetexko-var}}
\DeclareOption{manualfontspec}{\PassOptionsToPackage{\CurrentOption}{xetexko-var}}
\DeclareOption{nowinname}{\PassOptionsToPackage{\CurrentOption}{xetexko-var}}
\DeclareOption{unfonts}{\PassOptionsToPackage{\CurrentOption}{xetexko-var}}
\DeclareOption{nounfonts}{\PassOptionsToPackage{\CurrentOption}{xetexko-var}}
\DeclareOption{nanum}{\PassOptionsToPackage{\CurrentOption}{xetexko-var}}
\DeclareOption{hcr}{\PassOptionsToPackage{\CurrentOption}{xetexko-var}}
\DeclareOption{noto}{\PassOptionsToPackage{\CurrentOption}{xetexko-var}}
\else\ifluatex
%%% luatexko options
\DeclareOption{gremph}{\PassOptionsToPackage{\CurrentOption}{xob-font}}
\DeclareOption{itemph}{\PassOptionsToPackage{\CurrentOption}{xob-font}}
%%% new option
\DeclareOption{latinquote}{\PassOptionsToPackage{\CurrentOption}{xob-font}}
\DeclareOption{oldhangul}{\PassOptionsToPackage{\CurrentOption}{xob-font}}
\DeclareOption{manualfontspec}{\PassOptionsToPackage{\CurrentOption}{xob-font}}
\DeclareOption{nowinname}{\PassOptionsToPackage{\CurrentOption}{xob-font}}
\DeclareOption{unfonts}{\PassOptionsToPackage{\CurrentOption}{xob-font}}
\DeclareOption{nounfonts}{\PassOptionsToPackage{\CurrentOption}{xob-font}}
\DeclareOption{nanum}{\PassOptionsToPackage{\CurrentOption}{xob-font}}
\DeclareOption{hcr}{\PassOptionsToPackage{\CurrentOption}{xob-font}}
\DeclareOption{noto}{\PassOptionsToPackage{\CurrentOption}{xob-font}}
\else
%%% pdftex options
\ifPDFTeX
\DeclareOption{gremph}{\DisabledOption{gremph}}
\DeclareOption{itemph}{\DisabledOption{itemph}}
\DeclareOption{latinquote}{\DisabledOption{latinquote}}
\DeclareOption{oldhangul}{\DisabledOption{oldhangul}}
\DeclareOption{manualfontspec}{\DisabledOption{manualfontspec}}
\DeclareOption{nowinname}{\DisabledOption{nowinname}}
\DeclareOption{unfonts}{\DisabledOption{unfonts}}
\DeclareOption{nounfonts}{\DisabledOption{nounfonts}}
\DeclareOption{nanum}{\DisabledOption{nanum}}
\DeclareOption{hcr}{\DisabledOption{hcr}}
\DeclareOption{noto}{\DisableOption{noto}}
\fi
\fi\fi

%%% figtabcapt
\newif\if@figtabcapt@\@figtabcapt@false
\DeclareOption{figtabcapt}{\@figtabcapt@true}

\newif\ifKOTEXCJK\KOTEXCJKtrue
\DeclareOption{cjk}{\KOTEXCJKtrue}
\DeclareOption{dhucs}{\KOTEXCJKfalse}
\DeclareOption{uset1font}{\PassOptionsToPackage{usecjkt1font}{cjkutf8-ko}}
\DeclareOption{usedotemph}{\PassOptionsToPackage{\CurrentOption}{cjkutf8-ko}}

\ProcessOptions \relax

\newif\if@@babelkorfontdeclared\@@babelkorfontdeclaredfalse

\if@polyglos
	\if@babelhangul
	  \if@babelvacant \else
		 \ifluatex
  		\RequirePackage{babel}
  		\AtEndPreamble{
  			\babelprovide[import, onchar=ids fonts]{korean}
				\if@@babelkorfontdeclared\else
				\babelfont[korean]{rm}{UnBatang}
				\fi
  		}		
  		\ProvideDocumentCommand\setobmainfont{mo}{%
  			\IfValueTF{#2}{%
  				 \babelfont{rm}[#2]{#1}}%
  				{\babelfont{rm}{#1}}}
  		\ProvideDocumentCommand\setobsansfont{mo}{%
  			\IfValueTF{#2}{%
  				 \babelfont{sf}[#2]{#1}}%
  				{\babelfont{sf}{#1}}}
  		\ProvideDocumentCommand\setobmonofont{mo}{%
  			\IfValueTF{#2}{%
  				 \babelfont{tt}[#2]{#1}}%
  				{\babelfont{tt}{#1}}}
  		\ProvideDocumentCommand\setkomainfont{mo}{%
  			\IfValueTF{#2}{%
  				 \babelfont[korean]{rm}[#2]{#1}}%
  				{\babelfont[korean]{rm}{#1}}%
				\@@babelkorfontdeclaredtrue
				}
  		\ProvideDocumentCommand\setkosansfont{mo}{%
  			\IfValueTF{#2}{%
  				 \babelfont[korean]{sf}[#2]{#1}}%
  				{\babelfont[korean]{sf}{#1}}}
  		\ProvideDocumentCommand\setkomonofont{mo}{%
  			\IfValueTF{#2}{%
  				 \babelfont[korean]{tt}[#2]{#1}}%
  				{\babelfont[korean]{tt}{#1}}}
		 \else\ifxetex
		   \RequirePackage{fontspec}
		   \setmainfont{UnBatang}
		   \XeTeXlinebreaklocale="ko"
		   \fi
		 \fi
		\fi
\else
    \RequirePackage{polyglossia}
    \RequirePackage{xparse}
  %%% \sethangulfont{FONTNAME} or \sethangulfont[tt]{FONTNAME}[FEATURES]
    \NewDocumentCommand\sethangulfont{omo}{%
      \IfValueTF{#1}{%
        \IfValueTF{#3}{%
        	\expandafter\newfontfamily\csname hangulfont#1\endcsname
  		{#2}[#3]}{%
  		\expandafter\newfontfamily\csname hangulfont#1\endcsname
  		{#2}}%
  	}%
  	{%
        \IfValueTF{#3}{%
        	\newfontfamily\hangulfont{#2}[#3]%
      	}{%
  	    \newfontfamily\hangulfont{#2}%
      	}%
    	}%
    }
  \fi
\else
\ifluatex
  \@ifpackageloaded{luatexko}{}{\RequirePackage{luatexko}}
  \RequirePackage{xob-font}
  \RequirePackage{xob-dotemph}
\else\ifxetex
  \@ifpackageloaded{xetexko}{\RequirePackage{xob-font}}{\RequirePackage{xetexko-var}}
  \RequirePackage{xob-dotemph}
  \else
    \ifKOTEXCJK
     \RequirePackage{cjkutf8-ko}
%    \else
%     \RequirePackage{dhucs}
    \fi
\fi\fi\fi

%% define memh@hu
\newdimen\memh@hu\memh@hu=0.059375em

\newif\if@inthekoreancontext\@inthekoreancontexttrue

%% memoir [ms] option
\ifmsdoc
\newcommand*\MSNormalSize{
  \let\large\normalsize
  \let\Large\normalsize
  \let\LARGE\normalsize
  \let\huge\normalsize
  \let\Huge\normalsize
  \let\HUGE\normalsize
  \let\small\normalsize
  \let\scriptsize\normalsize
  \let\footnotesize\normalsize
  \let\tiny\normalsize
 }
\AtBeginDocument{
 \renewcommand{\msdoublespacing}{%
    \renewcommand{\baselinestretch}{1.75}\large\normalsize}
 \renewcommand{\mssinglespacing}{%
    \renewcommand{\baselinestretch}{1.1}\large\normalsize}
 \renewcommand{\familydefault}{cmtt}
 \renewcommand{\rmdefault}{cmtt}
 \renewcommand{\sfdefault}{cmtt}
 \renewcommand{\bfdefault}{m}
 \renewcommand{\itdefault}{n}
 \renewcommand{\sldefault}{n}
 \renewcommand{\scdefault}{n}
 \renewcommand{\baselinestretch}{1.75}
 \@twocolumnfalse
 \onecolumn
 \sloppy
 \@twosidefalse
 \raggedbottom
 \pagestyle{plain}
}
\fi

\if@pdfbookmark
  \@ifpackageloaded{hyperref}{% hyperref is already loaded
  	\typeout{hyperref is already loaded!}%
	\RequirePackage{memhfixc}}%
	{% 
     \RequirePackage{hyperref}
	 \RequirePackage{memhfixc}}
\fi

%%% interword
\if@interworddefault
 \RequirePackage{memucs-interword-x}
\fi

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 한글 관련 이름 설정 (not from hangul.sty)

\if@hanja
\def\today{\number\year 年~\number\month 月~\number\day 日}
\def\enclname{同封物}
\def\ccname{寫本}
\def\headtoname{受信人}
\def\seename{\을~參照}
\def\alsoname{\을~參照}
\def\contentsname{目~次}
\def\listfigurename{圖~目次}
\def\listtablename{表~目次}
\def\refname{參考~文獻}
\def\indexname{索~引}
\def\tablename{表}
\def\figurename{圖}
\def\abstractname{要~約}
\def\bibname{參考~圖書}
\def\appendixname{附錄}
\def\subappendixname{附錄}
\def\appendixtocname{附~錄}
\def\appendixpagename{附~錄} 
\def\partname{篇}
\def\chaptername{章}
\def\sectionname{節}
\def\colorlayer{色相레이어}
\def\glossaryname{語彙}
\def\figurerefname{圖}
\def\tablerefname{表}
\def\pagename{面}
\def\pagerefname{面}
\def\partrefname{篇}
\def\chapterrefname{章}
\def\sectionrefname{\S}
\else
\def\today{\number\year 년~\number\month 월~\number\day 일}
\def\enclname{동봉물}
\def\ccname{사본}
\def\headtoname{받는이}
\def\seename{\을~참조}
\def\alsoname{\을~함께~참조}
\def\contentsname{차~례}
\def\listfigurename{그림~차례}
\def\listtablename{표~차례}
\def\refname{참고~문헌}
\def\indexname{찾아보기}
\def\tablename{표}
\def\figurename{그림}
\def\abstractname{요~약}
\def\bibname{참고~문헌}
\def\appendixname{부록}
\def\subappendixname{부록}
\def\appendixtocname{부~록}
\def\appendixpagename{부~록} 
\def\partname{편}
\def\chaptername{장}
\def\sectionname{절}
\def\colorlayer{환등판~색상}
\def\glossaryname{용어집}
\def\figurerefname{그림}
\def\tablerefname{표}
\def\pagename{페이지}
\def\pagerefname{페이지}
\def\partrefname{편}
\def\chapterrefname{장}
\def\sectionrefname{\S}
\fi

%% \marginparswitchtrue/false
\providecommand{\marginparswitchtrue}{\@mparswitchtrue}
\providecommand{\marginparswitchfalse}{\@mparswitchfalse}
\providecommand{\reversemargintrue}{\@reversemargintrue}
\providecommand{\reversemarginfalse}{\@reversemarginfalse}

%% select textwidth with chars
\def\testh@ng@lch@r@cters{가갸거겨고교구규그기}
\newlength\t@nch@ars
\newcommand*\setxxxlength[2]{%
  \setbox0\hbox{\testh@ng@lch@r@cters}\setlength\t@nch@ars{\wd0}%
  \@ifundefined{#1length}{\expandafter\newlength\csname #1length\endcsname}{}%
  \expandafter\setlength\csname #1length\endcsname{#2\t@nch@ars}%
  \expandafter\divide\csname #1length\endcsname by 10%
}

%%% kscntformat
%%% HLaTeX의 \kscntformat 명령을 흉내냄
%%% \kscntformat{section}{제}{절} 형식으로 쓴다.
\newsavebox{\tmp@XXbox}
\newdimen\tmp@XXdim
\def\kscntformat#1#2#3{%
	\sbox{\tmp@XXbox}{#2\expandafter\number\csname c@#1\endcsname#3}%
	\setlength\tmp@XXdim{\wd\tmp@XXbox}\advance\tmp@XXdim by1em%
	\expandafter\def\csname the#1\endcsname{#2\expandafter\number\csname c@#1\endcsname#3}%
	\expandafter\renewcommand\csname cft#1numwidth\endcsname{\tmp@XXdim}%
	\expandafter\renewcommand\csname cft#1presnum\endcsname{}%
	\expandafter\renewcommand\csname cft#1aftersnum\endcsname{}%
}

\AtBeginDocument{%
\ifnokorean\else
\ifkosection
\kscntformat{section}{제\ob@onethirdspace }{\ob@onethirdspace 절}
\renewcommand\thesubsection{\arabic{section}.\arabic{subsection}}
\fi\fi
}

\RequirePackage{memhangul-common}

\ifx\memucsinterwordhook\undefined
\def\memucsinterwordhook{}
\fi 

\ifx\memucsinterwordchapterskiphook\undefined
\def\memucsinterwordchapterskiphook{}
\fi

\if@nonfrench\else
 \frenchspacing
\fi

%%% hsetspace for memucs
\if@hsspace
  \RequirePackage[hangul,adjustverbatim,adjustfootnotesep]{memucs-setspace}
  \if@hsspacenoquote\else\adjustquotespacing\fi
\else
  \RequirePackage{memucs-setspace}
  \linespread{1.33}
\fi
%\SetHangulspace{1.333}{1.1}

%%% fix first pdf page layout error : XeTeX
%%% based on v1.61803398
%%% these codes are to be deleted later. (2009/09/25)
\renewcommand*{\fixpdflayout}{%
  \pdfpageheight=\the\stockheight
  \pdfpagewidth=\the\stockwidth
  \ifxetex
    \AtBeginDvi{\special{pdf: pagesize width \the\stockwidth\ height \the\stockheight}}%
  \else
    \ifdim\pdfvorigin=0pt\pdfvorigin=1in\fi
    \ifdim\pdfhorigin=0pt\pdfhorigin=1in\fi
  \fi}

%%% 호환을 위하여 dhucs 명령 몇 개를 무력화함.
\ifx\SetAdhocFonts\undefined
 \def\SetAdhocFonts#1#2{}
\fi
\ifx\regremph\undefined
 \let\regremph\relax
 \let\ungremph\relax
\fi

\endinput

% CHANGE LOG
% 2015/10/05 : change \sethangulfont command for polyglossia
% 2014/04/29 : fix a bug reported by Progress
% 2013/10/22 : introduce memhangul-common.sty
% 2013/10/16 : \kscntformat and [kosection] bug fix.
% 2013/09/27 : \cntrdotss
% 2013/09/15 : \leavevmode at \explpunc, \expldash
% 2013/09/01 : [cjk], [dhucs] option introduced
% 2012/12/02 : drop shipout hacking codes 
% 2012/11/29 : \pdfstringdefDisableCommands
% 2012/11/22 : [figtabcapt] opton introduced
% 2012/11/01 : avoid error in \cnm, \ccnm etc.
% 2012/07/12 : introduce xob-font
% 2011/09/15 : changes according to xoblivoir
% 2010/09/16 : cntrdot, adjustmath
% 2009/05/11 : protect bnm commands
% 2009/04/02 : redefine memoir-shipout for pdflualatex and other...
% 2009/02/11. memoir v1.6180339e compatibility mend 1.
%             titleref, hangul page style.
%%%
% 오전 6:35 2008-09-18 : rename xkospace.sty to xkospace-var.sty
%                        to avoid confusion
% 오후 2:16 2008-09-18 : latinquote 옵션
% 오전 4:32 2008-09-30 : hangul item titles
%                        코드 정리.
% 오전 8:24 2008-10-07 : revive interword hook
% 오후 5:25 2008-10-07 : revive gremph/itemph
% 오전 1:21 2008-10-13 : xetexko v0.1 반영 
% 오후 2:37 2008-10-23 : bnmcmds, oblivoirdblquote
% 오후 17:59 2008-12-03 : nowinname 옵션 추가
% 오전 2:17 2009-09-26 : try to fix the first pdf page layout error on XeTeX (moved from fapapersize)
%
