%% File `fapapersize.sty`
%%
%% Copyright (c) 2006-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.
%%
%% 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
%%   사륙배판, 국판, 신국판 정의.
%%   marginnote 영역 설정은 제외되어 있음.
\ProvidesPackage{fapapersize}
%%%
%%% 옵션
%%% dbl4x6 : 사륙배판
%%% mum : 국판
%%% newmum : 신국판
\DeclareOption{dbl4x6}{%
  \AtEndOfPackage{\usefapapersize{190mm,260mm,30mm,*,30mm,*}}%
}
\DeclareOption{mum}{%
  \AtEndOfPackage{\usefapapersize{148mm,210mm,25mm,*,25mm,*}}%
}
\DeclareOption{newmum}{%
  \AtEndOfPackage{\usefapapersize{154mm,225mm,25mm,*,25mm,*}}%
}
\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
}

\endinput
