%
% pxgentombow.sty
% written by Hironobu Yamashita (@aminophen)
%
% This package is part of the platex-tools bundle.
% https://github.com/aminophen/platex-tools
%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{pxgentombow}
    [2017/12/17 v0.9 Generate crop mark 'tombow']
\def\pxgtmb@pkgname{pxgentombow}

%% error status (shared with gentombow.sty)
\ifx\pxgtmb@errlevel\@undefined
  \chardef\pxgtmb@errlevel=\z@
\fi

%% supported engines
\ifx\pfmtversion\@undefined
  \@ifpackageloaded{luatexja}{}{\chardef\pxgtmb@errlevel=\@ne}
\fi
\ifnum\pxgtmb@errlevel>\z@
  \ifx\pxgtmb@emu@pkgname\@undefined % gentombow.sty not loaded
    \PackageError{\pxgtmb@pkgname}{%
      Only pLaTeX, upLaTeX and LuaTeX-ja are supported!\MessageBreak
      Please use `gentombow' package instead}\@ehc
    \expandafter\expandafter\expandafter\endinput\expandafter
  \fi
\else
  \@namedef{ver@gentombow.sty}{}% fake
\fi

%% import from jsclasses
\hour\time \divide\hour by 60\relax
\@tempcnta\hour \multiply\@tempcnta 60\relax
\minute\time \advance\minute-\@tempcnta

\ifnum\mag=\@m\else
  % if BXjscls is detected and \mag != 1000,
  % the layout will be definitely broken
  \ifx\bxjs@param@mag\@undefined\else
    \PackageError{\pxgtmb@pkgname}{%
      It seems you are using Japanese `BXjscls'\MessageBreak
      (bxjsarticle, bxjsbook, bxjsreport, etc.) or\MessageBreak
      some derived class. Try adding `nomag' or\MessageBreak
      `nomag*' to the class option list}\@ehc
  \fi
\fi

%% this package will use tombo feature in pLaTeX kernel
%  if tombow-related option is not included in class option list,
%  show info and enable it now
\iftombow\else
  % if jsclasses is detected and \mag != 1000, it's too late
  % -- When a size option other than `10pt' is specified,
  %    jsclasses uses \mag and calculates \oddsidemargin and \topmargin
  %    differently, depending on tombow status.
  %    In order to force `jsclasses' to calculate correctly,
  %    `tombow' or `tombo' is required as a class option.
  %    ... or, you may add `nomag' or `nomag*' instead.
  \ifnum\mag=\@m\else \ifx\inv@mag\@undefined\else
    \PackageError{\pxgtmb@pkgname}{%
      It seems you are using Japanese `jsclasses'\MessageBreak
      (jsarticle, jsbook, jsreport, etc.) or some\MessageBreak
      derived class. Please add `tombow' or `tombo'\MessageBreak
      to the class option list}\@ehc
  \fi \fi
  % BXjscls is already checked above, no check here
  \PackageInfo\pxgtmb@pkgname{tombow feature enabled by \pxgtmb@pkgname}
\fi
\tombowtrue %\tombowdatetrue %% enabled by tombowdate option
\setlength{\@tombowwidth}{.1\p@}%

%% import from jsclasses
\@bannertoken{%
  \jobname\space(\number\year-\two@digits\month-\two@digits\day
  \space\two@digits\hour:\two@digits\minute)}

%% prepare dimension
\ifx\stockheight\@undefined \newdimen\stockheight \fi
\ifx\stockwidth\@undefined  \newdimen\stockwidth  \fi

%% prepare flag
\newif\ifpxgtmb@switch    \pxgtmb@switchfalse
\newif\ifpxgtmb@landscape \pxgtmb@landscapefalse

%% package options part 1
\DeclareOption{tombowdate}{\tombowdatetrue}
\DeclareOption{notombowdate}{\tombowdatefalse}

%% register a list of candidate papersize
%  * \pxgtmb@addpapersize[<tombowname>]{<papername>}{<shorter edge>}{<longer edge>}
%      used for declaration of papersize.
%      when no option is specified (that is, \ifpxgtmb@switch = \iffalse),
%      also used for automatic stocksize determination.
%      * if <tombowname> = \@empty, the next <papername> is assumed.
%      * if <tombowname> = n, stocksize is set to papersize + 2in.
\def\pxgtmb@addpapersize{\@ifnextchar[{\pxgtmb@addp@persize}{\pxgtmb@addp@persize[\@empty]}}
\def\pxgtmb@addp@persize[#1]#2#3#4{%
  % get current papersize and search through known standard in ascending order
  \ifx\pxgtmb@guessedtombow\@empty
  \ifx\pxgtmb@guessedpaper\@empty
    % shorter edge -> \@tempdima, longer edge -> \@tempdimb
    \ifdim\paperwidth>\paperheight\relax
      \pxgtmb@landscapetrue
      \@tempdima\paperheight \@tempdimb\paperwidth
    \else
      \pxgtmb@landscapefalse
      \@tempdima\paperwidth  \@tempdimb\paperheight
    \fi
    % \@ovri and \@ovro are used temporarily (safe enough)
    \@ovri=#3\relax
    \@ovro=#4\relax
    % if \mag != 1000 and \inv@mag is defined, assume jsclasses-style \mag employment
    \ifnum\mag=\@m\else \ifx\inv@mag\@undefined\else
      \@ovri=\inv@mag\@ovri\relax
      \@ovro=\inv@mag\@ovro\relax
    \fi \fi
    % compare
    \ifdim\@tempdima=\@ovri\relax \ifdim\@tempdimb=\@ovro\relax
      \def\pxgtmb@guessedpaper{#2}%
      \ifx#1\@empty\else
        \def\pxgtmb@guessedtombow{#1}%
        \if n\pxgtmb@guessedtombow\else
          \ExecuteOptions{tombow-#1}% package defaults to tombowdatetrue
          \pxgtmb@switchfalse
        \fi
      \fi
    \fi \fi
  \else
    \def\pxgtmb@guessedtombow{#2}% save for console message
    \pxgtmb@setstock{#3}{#4}%      set stockwidth/height
  \fi\fi
  \DeclareOption{tombow-#2}{%
    \pxgtmb@switchtrue
    \tombowdatetrue
    \pxgtmb@setstock{#3}{#4}%
  }%
  \DeclareOption{tombo-#2}{%
    \pxgtmb@switchtrue
    \tombowdatefalse
    \pxgtmb@setstock{#3}{#4}%
  }%
  \DeclareOption{mentuke-#2}{%
    \pxgtmb@switchtrue
    \tombowdatefalse
    \setlength{\@tombowwidth}{\z@}%
    \pxgtmb@setstock{#3}{#4}%
  }%
}
\def\pxgtmb@setstock#1#2{%
  \ifpxgtmb@landscape
    \setlength\stockwidth{#2}%
    \setlength\stockheight{#1}%
  \else
    \setlength\stockwidth{#1}%
    \setlength\stockheight{#2}%
  \fi
  % if \mag != 1000 and \inv@mag is defined, assume jsclasses-style \mag employment
  \ifnum\mag=\@m\else \ifx\inv@mag\@undefined\else
    \stockwidth=\inv@mag\stockwidth\relax
    \stockheight=\inv@mag\stockheight\relax
  \fi \fi
}%
\@onlypreamble\pxgtmb@addpapersize
\@onlypreamble\pxgtmb@addp@persize
\@onlypreamble\pxgtmb@setstock

%% initialize before search
\def\pxgtmb@guessedpaper{}
\def\pxgtmb@guessedtombow{}
\@onlypreamble\pxgtmb@guessedpaper
\@onlypreamble\pxgtmb@guessedtombow

%% package options part 2
%  ISO A series <=> JIS B series in the ascending order
\pxgtmb@addpapersize{a10}{26mm}{37mm}
\pxgtmb@addpapersize{b10}{32mm}{45mm}
\pxgtmb@addpapersize{a9}{37mm}{52mm}
\pxgtmb@addpapersize{b9}{45mm}{64mm}
\pxgtmb@addpapersize{a8}{52mm}{74mm}
\pxgtmb@addpapersize{b8}{64mm}{91mm}
\pxgtmb@addpapersize{a7}{74mm}{105mm}
\pxgtmb@addpapersize{b7}{91mm}{128mm}
\pxgtmb@addpapersize{a6}{105mm}{148mm}
\pxgtmb@addpapersize{b6}{128mm}{182mm}
\pxgtmb@addpapersize{a5}{148mm}{210mm}
\pxgtmb@addpapersize{b5}{182mm}{257mm}
\pxgtmb@addpapersize{a4}{210mm}{297mm}
\pxgtmb@addpapersize{b4}{257mm}{364mm}
\pxgtmb@addpapersize{a3}{297mm}{420mm}
\pxgtmb@addpapersize{b3}{364mm}{515mm}
\pxgtmb@addpapersize{a2}{420mm}{594mm}
\pxgtmb@addpapersize{b2}{515mm}{728mm}
\pxgtmb@addpapersize{a1}{594mm}{841mm}
\pxgtmb@addpapersize{b1}{728mm}{1030mm}
\pxgtmb@addpapersize[n]{a0}{841mm}{1189mm}
\pxgtmb@addpapersize[n]{b0}{1030mm}{1456mm}

%% package options part 3
%  ISO C series
\pxgtmb@addpapersize[a9]{c10}{28mm}{40mm}
\pxgtmb@addpapersize[a8]{c9}{40mm}{57mm}
\pxgtmb@addpapersize[a7]{c8}{57mm}{81mm}
\pxgtmb@addpapersize[a6]{c7}{81mm}{114mm}
\pxgtmb@addpapersize[a5]{c6}{114mm}{162mm}
\pxgtmb@addpapersize[a4]{c5}{162mm}{229mm}
\pxgtmb@addpapersize[a3]{c4}{229mm}{354mm}
\pxgtmb@addpapersize[a2]{c3}{324mm}{458mm}
\pxgtmb@addpapersize[a1]{c2}{458mm}{648mm}
\pxgtmb@addpapersize[a0]{c1}{648mm}{917mm}
\pxgtmb@addpapersize[n]{c0}{917mm}{1297mm}
%  misc
\pxgtmb@addpapersize[b4]{a4j}{210mm}{297mm}
\pxgtmb@addpapersize[b5]{a5j}{148mm}{210mm}
\pxgtmb@addpapersize[a3]{b4j}{257mm}{364mm}
\pxgtmb@addpapersize[a4]{b5j}{182mm}{257mm}
\pxgtmb@addpapersize[b4]{a4var}{210mm}{283mm}
\pxgtmb@addpapersize[a4]{b5var}{182mm}{230mm}
\pxgtmb@addpapersize[a3]{letter}{8.5in}{11in}
\pxgtmb@addpapersize[a3]{legal}{8.5in}{14in}
\pxgtmb@addpapersize[a4]{executive}{7.25in}{10.5in}

%% default options
\ExecuteOptions{tombowdate}% package defaults to tombowdatetrue
\ProcessOptions

%% display search result
%  if any of explicit size option is specified, \ifpxgtmb@switch = \iftrue.
%  otherwise, automatic size detection should be successful.
\ifpxgtmb@switch\else
  % check status
  \@tempcnta=\z@\relax
  \ifx\pxgtmb@guessedpaper\@empty
    \advance\@tempcnta\@ne\relax
  \fi
  \ifx\pxgtmb@guessedtombow\@empty
    \advance\@tempcnta\tw@\relax
  \else\if n\pxgtmb@guessedtombow
    \advance\@tempcnta\tw@\relax
  \fi\fi
  % message
  \ifodd\@tempcnta
    %\PackageWarningNoLine\pxgtmb@pkgname{%
    %  No size option specified, and automatic papersize\MessageBreak
    %  detection also failed}
  \else
    \typeout{***** Package \pxgtmb@pkgname\space detected \pxgtmb@guessedpaper paper. *****}
  \fi
  \ifnum\@tempcnta>\@ne\relax
    \PackageWarningNoLine\pxgtmb@pkgname{%
      Output size cannot be determined. Please add size\MessageBreak
      option (e.g. `tombow-a4') to specify output size.\MessageBreak
      Falling back to +1in ..}
    \stockwidth\paperwidth   \advance\stockwidth 2in
    \stockheight\paperheight \advance\stockheight 2in
  \else
    \typeout{***** Now the output size is automatically set to \pxgtmb@guessedtombow. *****}
  \fi
\fi

%% warnings
\ifdim\stockwidth<\paperwidth
  \PackageWarningNoLine\pxgtmb@pkgname{%
    \string\stockwidth\space is smaller than \string\paperwidth!\MessageBreak
    Is this really what you want?}
\fi
\ifdim\stockheight<\paperheight
  \PackageWarningNoLine\pxgtmb@pkgname{%
    \string\stockheight\space is smaller than \string\paperheight!\MessageBreak
    Is this really what you want?}
\fi

%% make tombow box according to (no)tombowdate & \@tombowwidth status
\maketombowbox

%% shift amount
\hoffset .5\stockwidth
\advance\hoffset -.5\paperwidth
\advance\hoffset-1truein\relax
\voffset .5\stockheight
\advance\voffset -.5\paperheight
\advance\voffset-1truein\relax

\endinput