%% File `fapapersize.sty`
%%    this file is a part of oblivoir-packages.
%%
%% Copyright (c) 2006-2022 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.
%%
%% ver 0.031 (2006/12/04)
%%   trivial bug fix.
%% ver 0.03 (2006/11/24)
%%   stocksize를 별도 지정할 수 있게 함
%%   option [stock], command \usefastocksize{<width>}{<height>}
%% ver 0.02 (2007)
%%   사륙배판, 국판, 신국판 정의.
%%   marginnote 영역 설정은 제외되어 있음.
%% ver 0.1 (2020)
%%   option 1in added
%% ver 0.2 (2022)
%%   option a4 added
%%   \definefageometry, \selectfageometry added.
%% ver 0.3 (2022)
%%   bug fix about trimmargin and paperwidth in the changetext.
%%
\ProvidesPackage{fapapersize}[2022/03/08 v0.3]
%%%
%%% 옵션
%%% dbl4x6 : 사륙배판
%%% mum : 국판
%%% newmum : 신국판
%%% 1in : margin 1in
%%% a4 : a4paper + 1in
%%% stock : stocksize
\DeclareOption{dbl4x6}{%
  \AtEndOfPackage{\usefapapersize{190mm,260mm,30mm,*,30mm,*}}%
}
\DeclareOption{mum}{%
  \AtEndOfPackage{\usefapapersize{148mm,210mm,25mm,*,25mm,*}}%
}
\DeclareOption{newmum}{%
  \AtEndOfPackage{\usefapapersize{154mm,225mm,25mm,*,25mm,*}}%
}
\DeclareOption{1in}{%
  \AtEndOfPackage{\usefapapersize{*,*,1in,*,1in,*}}%
}
\DeclareOption{a4}{%
  \AtEndOfPackage{\usefapapersize{210mm,290mm,1in,*,1in,*}}%
}
\newif\ifStockSize@\StockSize@false
\DeclareOption{stock}{%
  \StockSize@true
}
\newif\if@showtrim\@showtrimfalse
%\DeclareOption{trimmark}{\@showtrimtrue}
\DeclareOption{showtrims}{\@showtrimtrue}
\ProcessOptions

\newlength\fa@stockheight\newlength\fa@stockwidth
\newlength\fa@paperheight\newlength\fa@paperwidth
\newlength\fa@leftmargin\newlength\fa@rightmargin
\newlength\fa@uppermargin\newlength\fa@lowermargin
\newlength\fa@tmplenwd\newlength\fa@tmplenht

\newdimen\tmp@dim

\def\set@paper@size{%
 \ifnum\fa@paperwidth>\fa@tmplenwd
  \if@showtrim
   \typeout{the page size is too small. Trim mark will not be shown.}
   \@showtrimfalse\trimNone
  \fi
 \fi
 \if@showtrim
   \setstocksize{\fa@tmplenht}{\fa@tmplenwd}
   \settrimmedsize{\fa@paperheight}{\fa@paperwidth}{*}
   \setlrmarginsandblock{\fa@leftmargin}{\fa@rightmargin}{*}
   \setulmarginsandblock{\fa@uppermargin}{\fa@lowermargin}{*}
   \setlength{\trimtop}{\stockheight}
   \addtolength{\trimtop}{-\paperheight}
   \setlength{\trimedge}{\stockwidth}
   \addtolength{\trimedge}{-\paperwidth}
   \settrims{.5\trimtop}{.5\trimedge}
   \trimLmarks 
 \else
   \setstocksize{\fa@paperheight}{\fa@paperwidth}
   \settrimmedsize{\stockheight}{\stockwidth}{*}
   \setlrmarginsandblock{\fa@leftmargin}{\fa@rightmargin}{*}
   \setulmarginsandblock{\fa@uppermargin}{\fa@lowermargin}{*}
   \trimNone
 \fi
 \tmp@dim=\headheight\advance\tmp@dim by\headsep\advance\tmp@dim by1mm
 \ifdim\fa@uppermargin<\tmp@dim
   \setheaderspaces{*}{10pt}{*}
   \tmp@dim=\fa@uppermargin\advance\tmp@dim by-\headsep\advance\tmp@dim by-1mm
   \setheadfoot{\tmp@dim}{\tmp@dim}
 \fi
 \checkandfixthelayout
}

\def\@usefastocksize#1#2{%
 \setlength\fa@stockheight{#2}%
 \setlength\fa@stockwidth{#1}%
}

\newcommand*{\usefastocksize}[1]{%
 \set@fa@stocksizeparameters#1\@nil
}

\def\set@fa@stocksizeparameters#1,#2\@nil{%
 \setlength{\fa@stockwidth}{#1}%
 \setlength{\fa@stockheight}{#2}%
}

\def\@usefapapersize#1#2#3[#4]#5[#6]{%
 \setlength\fa@paperwidth{#1}
 \setlength\fa@paperheight{#2}
 \setlength\fa@leftmargin{#3}
 \setlength\fa@rightmargin{#4}
 \setlength\fa@uppermargin{#5}
 \setlength\fa@lowermargin{#6}
}

\newcommand*\usefapapersize[1]{%
 \get@stocksize
 \set@fa@papersizeparameters#1\@nil
 \set@paper@size
}

\def\get@stocksize{%
 \ifStockSize@
  \def\fa@tmplenwd{\fa@stockwidth}\def\fa@tmplenht{\fa@stockheight}
 \else
  \def\fa@tmplenwd{\stockwidth}\def\fa@tmplenht{\stockheight}
 \fi
}

\def\set@fa@papersizeparameters#1,#2,#3,#4,#5,#6\@nil{%
 \ifx*#1\def\@tmp@pgwidth{\fa@tmplenwd}\else\def\@tmp@pgwidth{#1}\fi
 \ifx*#2\def\@tmp@pgheight{\fa@tmplenht}\else\def\@tmp@pgheight{#2}\fi
 \ifx*#4
  \ifx*#6
     \@usefapapersize{\@tmp@pgwidth}{\@tmp@pgheight}{#3}[{#3}]{#5}[{#5}]
   \else
     \@usefapapersize{\@tmp@pgwidth}{\@tmp@pgheight}{#3}[{#3}]{#5}[{#6}]
  \fi\else
  \ifx*#6
     \@usefapapersize{\@tmp@pgwidth}{\@tmp@pgheight}{#3}[{#4}]{#5}[{#5}]
  \else
     \@usefapapersize{\@tmp@pgwidth}{\@tmp@pgheight}{#3}[{#4}]{#5}[{#6}]
  \fi
 \fi
}

%%%% 
%%% fapapersize.sty, 2021-06-21
%%% \definefageometry, \selectfageometry
%%%
\ExplSyntaxOn
	\NewDocumentCommand \definefageometry { m o m o }
	{
		\IfValueTF{#2}
		{
			\tl_gset:cn { g_famarginsetopt_#1_tl } { #2 }
		}
		{
			\tl_gset:cn { g_famarginsetopt_#1_tl } {}
		}
		\tl_gset:cn { g_famarginset_#1_tl } { #3 }
		\IfValueTF { #4 }
		{
			\tl_gset:cn { g_famarginsetoptpost_#1_tl } { #4 }
		}
		{
			\tl_gset:cn { g_famarginsetoptpost_#1_tl } {}
		}
	}
	
	\NewDocumentCommand \selectfageometry { s m }
	{
		\IfBooleanT { #1 } { \clearpage }
		\tl_use:c { g_famarginsetopt_#2_tl }
		\exp_args:Nx \usefapapersize{\fa@paperwidth,\fa@paperheight, \use:c{ g_famarginset_#2_tl } }
		\tl_if_empty:cF { g_famarginsetoptpost_#2_tl }
		{
			\tl_use:c { g_famarginsetoptpost_#2_tl }
		}
		\ch@ngetext
	}

\NewDocumentCommand \evenmarginsameasodd {}
{
	\setlength\evensidemargin{\oddsidemargin}
}

\ExplSyntaxOff

\endinput
