% \iffalse meta-comment
% (The MIT License)
%
% Copyright (c) 2021-2022 Yegor Bugayenko
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the 'Software'), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all
% copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
% SOFTWARE.
% \fi

% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}

% \GetFileInfo{kdpcover.dtx}
% \DoNotIndex{\endgroup,\begingroup,\let,\else,\fi,\newcommand,\newenvironment}

% \iffalse
%<*driver>
\ProvidesFile{kdpcover.dtx}
%</driver>
%<class>\NeedsTeXFormat{LaTeX2e}
%<class>\ProvidesClass{kdpcover}
%<*class>
[2022.11.30 0.5.1 Cover for Kindle Direct Publishing books]
%</class>
%<*driver>
\documentclass{ltxdoc}
\usepackage[tt=false, type1=true]{libertine}
\usepackage{microtype}
\AddToHook{env/verbatim/begin}{\microtypesetup{protrusion=false}}
\usepackage{href-ul}
\usepackage[dtx,margin=0,small]{docshots}
\usepackage{graphicx}
\PageIndex
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
	\DocInput{kdpcover.dtx}
	\PrintChanges
	\PrintIndex
\end{document}
%</driver>
% \fi

% \title{\includegraphics[width=0.75in]{yb-book-logo.pdf} \\ |kdpcover|: \LaTeX{} Package \\ for Amazon Book Covers\thanks{The sources are in GitHub at \href{https://github.com/yegor256/kdpcover}{yegor256/kdpcover}}}
% \author{Yegor Bugayenko \\ \texttt{yegor256@gmail.com}}
% \date{\filedate, \fileversion}
%
% \maketitle
%
% \section{Introduction}
%

% \changes{v0.5.0}{2022/11/29}{The \texttt{docshots} package is used to render the documentation.}
% This class will render you a book cover, suitable for Kindle Direct Publishing (KDP).
% I use it for \href{https://amzn.to/2WMTXWF}{all my books}.
% This is how you make your \LaTeX{} document look like:
% \docshotPrerequisite{cactus.pdf}
% \begin{docshot}
% \documentclass[pages=200,6x9]{kdpcover}
% \begin{document}
% \putSpine{kdpcover, a LaTeX class for KDP books}
% \putVolume{1}
% \putVersion{0.5.1}
% \putPrice{\$0.00}
% \putCopyright{\the\year}{Yegor Bugayenko}
% \putTitle{kdpcover}
% \putAuthor{Yegor Bugayenko}
% \putTLDR{This book will tell you
%   an interesting story!}
% \putPicture{cactus.pdf}
% \end{document}
% \end{docshot}

% The problem this class solves is the necessity to change the size of the
% cover PDF according to the number of pages in the book --- the bigger the book,
% the larger the spine of the book must be. The provided class makes
% the necessary calculations on-the-fly, using the |qpdf| tool. Obviously,
% you need to have it installed. Also, you must run pdflatex with
% the |--shell-escape| option, in order to allow \LaTeX{} to run |qpdf|.

% \section{Class Options}

% \begin{macro}{pages}
% With |pages| you can set the total count of the pages in the book:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\documentclass[pages=120]{kdpcover}
\begin{document}
\end{document}
\end{verbatim}
%\iffalse
%</verb>
%\fi
% \end{macro}

% \begin{macro}{pdf}
% It's better not to use |pages| option, but let the class calculate
% the size of your book from the PDF file (using |qpdf| command line
% tool, which has to be installed):
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\documentclass[pdf=book.pdf]{kdpcover}
\begin{document}
\end{document}
\end{verbatim}
%\iffalse
%</verb>
%\fi
% \end{macro}

% \begin{macro}{6x9}
% With this option you can set the size of the page to 6x9 inches.
% You can also set your own size like this:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\documentclass[6x9]{kdpcover}
\setlength\kdpxsize{8.1in}
\setlength\kdpysize{12.7in}
\begin{document}
\end{document}
\end{verbatim}
%\iffalse
%</verb>
%\fi
% \end{macro}

% \begin{macro}{barless}
% To get rid of the black bar at the center of the page, just add
% |barless| option:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\documentclass[barless]{kdpcover}
\begin{document}
\end{document}
\end{verbatim}
%\iffalse
%</verb>
%\fi
% \end{macro}

% \section{Commands}

% \begin{macro}{\putSpine}
% The |\putSpine| command puts a vertical black spine line with two white elements
% inside: the text at the top and the logo at the bottom. Make sure
% the text is short enough so that it doesn't touch the logo. It's
% a good practice to mention the volume number at the end of the text,
% if you have more than one volume, e.g. "my book, vol. 1". I suggest
% using small caps in the text only.
% \end{macro}

% \begin{macro}{\putVolume}
% This one prints the volume picture. It is intentionally made large,
% in order to make it obvious.
% \end{macro}

% \begin{macro}{\putVersion}
% This one prints the version and it will also add the date of the latest
% Git commit in the current directory. If it's not a Git repository,
% the hash won't be printed.
% \end{macro}

% \begin{macro}{\putPrice}
% This one prints the price, which is a good practice to mention, I believe.
% \end{macro}

% \begin{macro}{\putCopyright}
% This one prints the copyright short message with the year and the
% name of the author.
% \end{macro}

% \begin{macro}{\putTitle}
% This one prints the title of the book
% \end{macro}

% \begin{macro}{\putAuthor}
% This one prints the author.
% \end{macro}

% \begin{macro}{\putTLDR}
% This one prints the TL;DR paragraph, which summarizies the message
% of the entire book.
% \end{macro}

% \begin{macro}{\putPicture}
% This one prints the front picture. You can use |cactus.pdf|.
% \end{macro}

% \begin{macro}{\putBack}
% This is prints the text on the back of the book, which usually I suggest
% to omit and keep the back empty and white.
% \end{macro}

% \StopEventually{}

% \section{Implementation}

% \changes{v0.2.0}{2021/07/14}{Initial version}

% First, we load our parent class:
% \changes{v0.5.0}{2022/11/29}{The base class is now \texttt{article}.}
%    \begin{macrocode}
\LoadClass{article}
%    \end{macrocode}

% Then, we define a few internal commands:
%    \begin{macrocode}
\makeatletter
\newcommand*\kdp@pages{100}
\newlength\kdp@xsize
\setlength\kdp@xsize{6in}
\newlength\kdp@ysize
\setlength\kdp@ysize{9in}
\makeatother
%    \end{macrocode}

% Then, we include |iexec| for being able to execute |qpdf|:
%    \begin{macrocode}
\RequirePackage{iexec}
%    \end{macrocode}

% Then, we parse class options:
%    \begin{macrocode}
\RequirePackage{xkeyval}
\makeatletter
\DeclareOptionX{6x9}
  {\setlength\kdp@xsize{6in}\setlength\kdp@ysize{9in}}
\DeclareOptionX{7x10}
  {\setlength\kdp@xsize{7in}\setlength\kdp@ysize{10in}}
\DeclareOptionX{8x10}
  {\setlength\kdp@xsize{8in}\setlength\kdp@ysize{10in}}
\DeclareOptionX{pages}
  {\renewcommand*\kdp@pages{#1}}
\newif\ifkdp@barless
\DeclareOptionX{barless}{\kdp@barlesstrue}
\DeclareOptionX{pdf}{%
  \iexec[trace,quiet,stdout=kdpcover-pages-count.txt]
    {qpdf --show-npages #1 | tr -d '[[:space:]]' | \{ cat; echo \%; \}}%
  \newread\kdp@pagescount%
  \openin\kdp@pagescount=kdpcover-pages-count.txt
  \read\kdp@pagescount to \kdp@pages%
}
\ProcessOptionsX\relax\makeatother
%    \end{macrocode}

% Then, we include a few useful packages:
%    \begin{macrocode}
\RequirePackage{anyfontsize}
\RequirePackage{tikz}
\RequirePackage[letterspace=-50]{microtype}
\RequirePackage{xcolor}
\RequirePackage{graphicx}
\RequirePackage{calc}
%    \end{macrocode}

% Then, we set spacing using |setspace|:
%    \begin{macrocode}
\RequirePackage{setspace}
  \setstretch{1.2}
%    \end{macrocode}

% Then, we set calculate size.
% The height of the page is 9 inches plus 0.125 "bleed" on top and
% on the bottom, see \href{https://kdp.amazon.com/en_US/help/topic/G201953020}{this}.
% The width is 6 inches plus 0.125 "bleed" on each side. The width of the
% "spine" depends on the amount of pages in the book.
%    \begin{macrocode}
\makeatletter
  \newlength\kdp@height
  \setlength\kdp@height{0.125in + \kdp@ysize + 0.125in}
  \newlength\kdp@width
  \setlength\kdp@width{
    0.125in + \kdp@xsize
    + 0.0025in * \kdp@pages
    + \kdp@xsize + 0.125in}
\makeatother
%    \end{macrocode}

% Then, we set the size of the page, using |geometry|:
%    \begin{macrocode}
\RequirePackage{geometry}
\makeatletter
\geometry{paperwidth=\kdp@width,paperheight=\kdp@height,
  left=0pt,right=0pt,top=0pt,bottom=0pt}
\makeatother
%    \end{macrocode}

% Then, we set coordinates using |textpos|:
%    \begin{macrocode}
\RequirePackage[absolute]{textpos}
\TPGrid{16}{16}
%    \end{macrocode}

% \begin{macro}{\putSpine}
% Then, we define |\putSpine|:
%    \begin{macrocode}
\makeatletter\newcommand\putSpine[2][kdpcover-signature]{%
  \ifkdp@barless\else
    \begin{textblock}{2.4}[0.5,0](8,0)%
      \begin{tikzpicture}%
        \node [rectangle, inner sep=0em, fill=black,
        minimum width=2.4\TPHorizModule,
        minimum height=16\TPVertModule] at (0,0) {};
      \end{tikzpicture}%
    \end{textblock}%
  \fi
  \begin{textblock}{1}[0.5,0](8,2)%
    \begin{tikzpicture}%
      \node [color=white, inner sep=0cm, outer sep=0cm,
        rotate=270, minimum height=\TPHorizModule] at (0,0) {
        \Large #2
      };%
    \end{tikzpicture}%
  \end{textblock}%
  \begin{textblock}{2.4}[0.5,1](8,14)%
    \centerline{\includegraphics[width=0.32in]{#1}}%
  \end{textblock}%
}\makeatother
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\putPicture}
% Then, we define |\putPicture|:
%    \begin{macrocode}
\newcommand\putPicture[1]{%
  \begin{textblock}{4}(10,2)%
    \includegraphics[width=\textwidth]{#1}
  \end{textblock}%
}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\putVolume}
% Then, we define |\putVolume|:
%    \begin{macrocode}
\newcommand\putVolume[1]{%
  \begin{textblock}{2}[1,1](15,14)%
    \raggedleft
    \includegraphics[height=0.4in]{kdpcover-vol-#1}
  \end{textblock}%
}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\putPrice}
% Then, we define |\putPrice|:
%    \begin{macrocode}
\newcommand\putPrice[1]{%
  \begin{textblock}{4}[0,1](1,2)%
    \small #1
  \end{textblock}%
}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\putBack}
% Then, we define |\putBack|:
%    \begin{macrocode}
\newcommand\putBack[1]{%
  \begin{textblock}{5}[0,0](1,3)%
    \small #1
  \end{textblock}%
}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\putTitle}
% Then, we define |\putTitle|:
%    \begin{macrocode}
\newcommand\putTitle[1]{%
  \begin{textblock}{5}(10,9)%
    \fontsize{32}{32}\selectfont #1
  \end{textblock}%
}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\putAuthor}
% Then, we define |\putAuthor|:
%    \begin{macrocode}
\newcommand\putAuthor[1]{%
  \begin{textblock}{4}(10,10)%
    \large by #1
  \end{textblock}%
}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\putTLDR}
% Then, we define |\putTLDR|:
%    \begin{macrocode}
\newcommand\putTLDR[1]{%
  \begin{textblock}{5}(10,11)%
    TL;DR #1
  \end{textblock}%
}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\putVersion}
% Then, we define |\putVersion|:
%    \begin{macrocode}
\newcommand\putVersion[1]{%
  \begin{textblock}{4}[0,1](10,14)%
    #1
    \IfFileExists{.git}
      {\quad\iexec{git log -n 1 --pretty='format:\%ad' --date='format:\%e-\%b-\%Y'}}
      {}
  \end{textblock}%
}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\putCopyright}
% Then, we define |\putCopyright|:
%    \begin{macrocode}
\newcommand\putCopyright[2]{%
  \begin{textblock}{4}[0,1](1,14)%
    \small (c)
    #1 #2
  \end{textblock}%
}
%    \end{macrocode}
% \end{macro}

% Finally, a few layout instructions:
%    \begin{macrocode}
\AtBeginDocument{%
  \ttfamily%
  \raggedright%
  \setlength\parindent{0pt}%
  \setlength\parskip{0pt}%
  \interfootnotelinepenalty=10000%
}
%    \end{macrocode}

% \Finale

%\clearpage
%
%\PrintChanges
%\clearpage
%\PrintIndex
