%%
%% This is file `novel-LayoutSettings.sty', part of class `novel'.
%% Copyright 2017 Robert Allgeyer.
%%
%% 
%% This file 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 2005/12/01 or later.
%% 
%%
\ProvidesFile{novel-LayoutSettings.sty}%
[2017/07/31 v1.34 LaTeX file (layout settings)]
%%

% Novel Tracking codes in this file begin with 3.
\NDB{3.0 in novel-LayoutSettings.sty.}

%% Note that nearly all layout settings are preconfigured based on Trim Size.
%% In general, these user commands may be used to over-ride the presets.

\newlength\Old@TrimWidth % finished width of printed book
\newlength\Old@TrimHeight % finished height of printed book
\newlength\New@TrimWidth % Initialized to \Old@TrimWidth.
\newlength\New@TrimHeight % Initialized to \Old@TrimHeight.
\newlength\Old@TopMargin
\newlength\Old@OutsideMargin
\newlength\Old@BottomMargin
\newlength\Old@InsideMargin
\newlength\New@TopMargin
\newlength\New@OutsideMargin
\newlength\New@BottomMargin
\newlength\New@InsideMargin
\newlength\Old@MediaWidth % Defaults to trim width, but can be set larger.
\newlength\Old@MediaHeight % Defaults to trim height, but can be set larger.
\newlength\New@MediaWidth % Initialized to \Old@MediaWidth.
\newlength\New@MediaHeight % Initialized to \Old@MediaHeight.
\newlength\Old@FontSize
%
\gdef\my@HeadFootStyle{1} % default: header but not footer
\gdef\my@VersoEmblem{} % no default emblem
\gdef\my@RectoEmblem{} % no default emblem
\gdef\novel@checkfeatures#1{} % re-defined below
%
\newif \if@TrimSizeSet
\newif \if@MarginsSet
\newif \if@MediaSizeSet
\newif \if@LinesPerPageSet
\newif \if@FontSizeSet
\newif \if@ReworkTrimSet
\newif \if@ReworkMarginsSet
%
\newlength\normalxheight
\newlength\normalXheight
%%
\NDB{3.1 in novel-LayoutSettings.sty.}


%% SIZE-RELATED COMMANDS AVAILABLE IN PREAMBLE.
%% --------------------------------------------------------------------------
%% Use if necessary for customization.
%% If you like a class option that pre-sets these values,
%% then you do not have to use any of these commands.
%
% Note: This class does not ask you to set the width/height of the textblock.
% Instead, these values are automatically calculated from other dimensions.
%

%
% \SetTrimSize{}{} width and height of printed book.
\newcommand\SetTrimSize[2]{%
  \NDB{3.2.0 in novel-LayoutSettings.sty.}
  \global\@TrimSizeSettrue
  \ifthenelse{\dimtest{#1}{<}{2.99in}}{
    \ClassError{novel}{Unacceptably small Trim Width}%
     {Trim Width must be at least 3in.}%
  }{}
  \ifthenelse{\dimtest{#2}{<}{2.99in}}{
    \ClassError{novel}{Unacceptably small Trim Height}%
     {Trim Height must be at least 3in.}%
  }{}
  \if@ReworkTrimSet
    \ClassError{novel}{^^JCannot set Trim after reworking Trim or Margins}%
     {\string\SetTrimSize\space cannot follow \string\ReworkTrimSize.}%
  \fi
  \if@ReworkMarginsSet
    \ClassError{novel}{^^JCannot set Trim after reworking Trim or Margins}%
     {\string\SetTrimSize\space cannot follow \string\ReworkTrimSize^^J%
      and cannot follow \string\ReworkMargins.}%
  \fi
  \NDB{3.2.1 in novel-LayoutSettings.sty.}
  \setlength\Old@TrimWidth{#1}%
  \setlength\Old@TrimHeight{#2}%
  \setlength\New@TrimWidth{#1}%
  \setlength\New@TrimHeight{#2}%
  \if@MediaSizeSet\else
    \setlength\Old@MediaWidth{#1}%
    \setlength\Old@MediaHeight{#2}%
    \setlength\New@MediaWidth{#1}%
    \setlength\New@MediaHeight{#2}%
  \fi
  \NDB{3.2.3 in novel-LayoutSettings.sty.}
}
%
% Retroactively change Trim Size after layout. See documentation:
\newcommand\ReworkTrimSize[2]{%
  \NDB{3.4.0 in novel-LayoutSettings.sty.}
  \if@coverart % New in version 1.2
  \ClassError{novel}{Rework command cannot be used with coverart option}%
  {Instead of reworking document size, you must re-do the artwork.}
  \else
    \global\@ReworkTrimSettrue
    \setlength\New@TrimWidth{#1}%
    \setlength\New@TrimHeight{#2}%
    \setlength\New@MediaWidth{#1}%
    \setlength\New@MediaHeight{#2}%
    \typeout{^^J Class `novel' Info: %
     Used \string\ReworkTrimSize{}{} command.^^J%
     Carefully review your PDF to ensure that line breaks, paragraphs,^^J%
     and page breaks are where you expect them to be.^^J}%
  \fi
  \NDB{3.4.1 in novel-LayoutSettings.sty.}
}
% Margins at top and bottom are relative to:
%   top of header, or top of textblock if no header;
%   bottom of footer, or bottom of textblock if no footer.
% Inside margin generally greater than outside, due to gutter.
% Standard margins command ("TOBI, or not TOBI, that is the question."):
\newcommand\SetMargins[4]{ % top, outside, bottom, inside
  \NDB{3.5.0 in novel-LayoutSettings.sty.}
  \global\@MarginsSettrue
  \if@ReworkMarginsSet
    \ClassError{novel}{^^JCannot set Margins after reworking them}%
     {\string\SetMargins\space cannot follow \string\ReworkMargins.}%
  \fi
  \setlength\Old@TopMargin{#1}
  \setlength\New@TopMargin{#1}
  \setlength\Old@OutsideMargin{#2}
  \setlength\New@OutsideMargin{#2}
  \setlength\Old@BottomMargin{#3}
  \setlength\New@BottomMargin{#3}
  \setlength\Old@InsideMargin{#4}
  \setlength\New@InsideMargin{#4}
  \NDB{3.5.1 in novel-LayoutSettings.sty.}
}
% Retroactively change margins after layout. See documentation:
\newcommand\ReworkMargins[4]{
  \NDB{3.6.0 in novel-LayoutSettings.sty.}
  \if@coverart % New in version 1.2
  \ClassError{novel}{Rework command cannot be used with coverart option}%
  {Instead of reworking document size, you must re-do the artwork.}
  \else
  \global\@ReworkMarginsSettrue
  \setlength\New@TopMargin{#1}
  \setlength\New@OutsideMargin{#2}
  \setlength\New@BottomMargin{#3}
  \setlength\New@InsideMargin{#4}
  \typeout{^^J Class novel Info: %
   Used \string\ReworkMargins{}{} command.^^J%
   Carefully review your PDF to ensure that line breaks, paragraphs,^^J%
   and page breaks are where you expect them to be.^^J}%
  \fi
  \NDB{3.6.1 in novel-LayoutSettings.sty.}
}
% \SetMediaSize[]{}{} such as {8.5in}{11in}.
%   This command is only used when the Trim Size must be placed in a larger
%   Media Size. Otherwise, Media Size is automatically set to Trim Size.
%   This command cannot be used until after Trim Size is set, either by
%   class option or by \SetTrimSize{}{}.
% Trim will be horizontally and vertically centered in media.
% No `rework' is needed for Media Size, because interior layout is relative
%   to Trim Size, not Media Size.
%
\newif \if@offcenterTrim \@offcenterTrimfalse
\newcommand\SetMediaSize[3][center]{ % [alignment] width, height
  \NDB{3.7.0 in novel-LayoutSettings.sty.}
  \ifthenelse{\equal{#1}{center} \OR \equal{#1}{edge}}{
    \global\@MediaSizeSettrue
    \setlength\Old@MediaWidth{#2}
    \setlength\New@MediaWidth{#2}
    \setlength\Old@MediaHeight{#3}
    \setlength\New@MediaHeight{#3}
  }{
    \ClassError{novel}{Bad optional argument for \string\SetMediaSize}%
    {\string\SetMediaSize\space optional argument must be `center' or `edge'.}%
  }
  \if@coverart\else % new in version 1.2
    \ifthenelse{\equal{#1}{edge}}{\global\@offcenterTrimtrue}{}
  \fi
  \NDB{3.7.1 in novel-LayoutSettings.sty.}
}
% \SetLinesPerPage{} limited due to fontsize and Trim Size constraints.
% There is no `rework' command, because as long as the number of lines fit,
%   the line spacing will be automatically changed to fit a reworked size.
%   The only possible problem is if lines were originally so tight that
%   a reworked size cannot fit. In that case, the user's best plan is remove
%   header or footer (if possible), or reduce vertical margins (if possible).
\newcommand\SetLinesPerPage[1]{
  \NDB{3.8.0 in novel-LayoutSettings.sty.}
  \global\@LinesPerPageSettrue
  \FPiflt{#1}{1}%
    \ClassError{novel}{\string\SetLinesPerPage\ must be at least 1}%
      {You requested less than one line per page. Naughty!^^J}%
  \else\fi %
  \FPround{\temp@IntegerLines}{#1}{0}
  \def\my@LinesPerPage{\temp@IntegerLines}
  \NDB{3.8.1 in novel-LayoutSettings.sty.}
}
% \SetFontSize{} may be in any units, even decimals: 12.2pt, 11.5bp, etc.
% When packages load, the font size is temporarily set to 11pt at 15pt skip,
% so that moldy-oldie packages don't get confused about metrics.
% Then the size is changed to its actual value based on this command, or on
% the defaults for Trim Size, or on the effects of \Rework.
\newcommand\SetFontSize[1]{
  \NDB{3.9.0 in novel-LayoutSettings.sty.}
  \global\@FontSizeSettrue
  \global\setlength\Old@FontSize{#1}
  \NDB{3.9.1 in novel-LayoutSettings.sty.}
}
%
%% End size-related commands available in Preamble.



%% RESERVE SPACE FOR HEADER/FOOTER, AND SET DEFAULT CHAPTER DISPLAY PAGE
%% --------------------------------------------------------------------------
%% At this point, `novel' needs to know whether or not header/footer used,
%% so that space can be reserved at top/bottom. Actual styling comes later.
%
% If the user wishes to tweak layout, these Preamble commands store info,
%   that will be used in novel-CalculateLayout.sty.
% The jumps are scale factors, which increase/decrease headsep and footskip
%   relative to normal baselineskip. Do not use \setlength\headsep or
%   \setlength\footskip directly, because direct settings will lead to
%   improper calculations.
% These "jumps" are measured baseline-to-baseline. So, \SetHeadJump{1} means
%   that the baseline of the header is 1 baselineskip above the baseline of
%   the first line of textblock. In other words, on-grid. Value 2 would be
%   equivalent to double-spaced vertically. A value of 1.5 is default, based
%   on observations of actual novels.
% If \SetHeadJump is less than 1, then a warning appears in the log.
\gdef\SetHeadJump#1{\gdef\@headjump{#1}}
\SetHeadJump{1.5} % default
\gdef\SetFootJump#1{\gdef\@footjump{#1}}
\SetFootJump{1.5} % default
\NDB{3.10 in novel-LayoutSettings.sty.}
% New in version 1.2, coverart.
\if@coverart % cover art has no header or footer
  \gdef\SetHeadFootStyle#1{\gdef\my@HeadFootStyle{0}}
  \gdef\SetHeadStyle#1{\gdef\my@HeadFootStyle{0}} % deprecated
  \SetHeadFootStyle{0}
\else
  % See docs part 7 for available style choices:
  \gdef\SetHeadFootStyle#1{\gdef\my@HeadFootStyle{#1}}
  \gdef\SetHeadStyle#1{\gdef\my@HeadFootStyle{#1}} % deprecated
\fi
\NDB{3.11 in novel-LayoutSettings.sty.}
% See docs part 7 for what these emblems do, if used:
\newcommand\SetEmblems[2]{ % verso, recto
  \gdef\my@VersoEmblem{{\headfont #1}}
  \gdef\my@RectoEmblem{{\headfont #2}}
}
\NDB{3.12 in novel-LayoutSettings.sty.}
\let\SetEmblem\SetEmblems% for convenience
% If you want something fancier than \thepage:
\gdef\SetPageNumberStyle#1{\gdef\pagenumberstyle{#1}}

% See novel.cls for the accompanying AtBeginDocument routine.

\NDB{3.13 in novel-LayoutSettings.sty.}
%
\newif \if@gotHeader
\newif \if@gotFooter
\newif \if@styledHeader
\newif \if@styledFooter
\newif \if@validHF
%
\gdef\@reserveHFspace{ % used for layout calculations
  \NDB{3.14.1 in novel-LayoutSettings.sty.}
  \ifthenelse{\equal{\my@HeadFootStyle}{0}}{
    \@gotHeaderfalse\@gotFooterfalse\@validHFtrue
  }{}
  \ifthenelse{\equal{\my@HeadFootStyle}{1}}{ % default
    \@gotHeadertrue\@gotFooterfalse\@validHFtrue
  }{}
  \ifthenelse{\equal{\my@HeadFootStyle}{2}}{
    \@gotHeaderfalse\@gotFootertrue\@validHFtrue
  }{}
  \ifthenelse{\equal{\my@HeadFootStyle}{3}}{
    \@gotHeaderfalse\@gotFootertrue\@validHFtrue
  }{}
  \ifthenelse{\equal{\my@HeadFootStyle}{4}}{
    \@gotHeadertrue\@gotFooterfalse\@validHFtrue
  }{}
  \ifthenelse{\equal{\my@HeadFootStyle}{5}}{
    \@gotHeadertrue\@gotFootertrue\@validHFtrue
  }{}
  \ifthenelse{\equal{\my@HeadFootStyle}{6}}{
    \@gotHeadertrue\@gotFootertrue\@validHFtrue
  }{}
  % Ensure that user choice was on the allowed list:
  \if@validHF\else
    \ClassError{novel}{Invalid choice for \string\SetHeadFootStyle}%
      {\string\SetHeadFootStyle\space needs choice of 0,1,2,3,4,5,6,^^J%
      even if you wish to customize using fancyhdr syntax.^^J}%
  \fi
  \NDB{3.14.2 in novel-LayoutSettings.sty.}
} % end \@reserveHFspace
% Each new chapter generally has its own header/footer style. This allows a
% default setting when chapters begin with ChapterStart environment:
\gdef\SetChapterStart#1{\gdef\@setchapterstart{#1}}
\SetChapterStart{footer} % default
\NDB{3.15 in novel-LayoutSettings.sty.}
%
% See novel.cls for the accompanying AtBeginDocument routine.
%
%% End reserve space for header/footer and set default ChapterStart


%% New in version 1.2: cover image.
\newcommand\SetCoverImage[1]{
  \NDB{3.17.0 in novel-LayoutSettings.sty.}
  \def\coverimageok{false}
  \IfSubStr{#1}{jpg}{\def\coverimageok{true}}{}
  \IfSubStr{#1}{jpeg}{\def\coverimageok{true}}{}
  \IfSubStr{#1}{JPG}{\def\coverimageok{true}}{}
  \IfSubStr{#1}{JPEG}{\def\coverimageok{true}}{}
  \ifthenelse{\equal{\coverimageok}{true}}{%
    \gdef\novelcoverimage{#1}
  }{%
    \ClassError{novel}{Cover image has wrong file extension}%
    {Cover image must be jpg, JPG, jpeg, or JPEG.}
  }%
  \NDB{3.17.1 in novel-LayoutSettings.sty.}
}
%%


%%
\newtoggle{@usesymbolmark} % true when footnote markers are symbolic
\newtoggle{@realmarker} % true when OT features sups, sinf, subs requested
%
% \SetMarkers does two things.
% Option real: OT features sups, sinf, subs requested. Fake if not available.
% Option fake: Always fake OT features sups, sinf, subs.
% Mandatory: asterisk, dagger, number. Pertains to footnote markers only.
% No matter real or fake, the asterisk symbol is always real, and the
%   dagger, dbldagger, and section (when used as footnote markers)
%   are always partially faked.
\newcommand\SetMarkerStyle[2][real]{
  \NDB{3.18.0 in novel-LayoutSettings.sty.}
  \def\@goodfnm{no}
  \ifthenelse{\equal{#2}{asterisk}}{%
    \def\@goodfnm{yes}\global\toggletrue{@usesymbolmark}%
    \gdef\@myfnsymbols{*}}{}
  \ifthenelse{\equal{#2}{dagger}}{%
    \def\@goodfnm{yes}\global\toggletrue{@usesymbolmark}%
    \gdef\@myfnsymbols{†}}{}
  \ifthenelse{\equal{#2}{number}}{%
    \def\@goodfnm{yes}\global\togglefalse{@usesymbolmark}}{}
  \def\@goodfnk{no}
  \ifthenelse{\equal{#1}{real}\OR\equal{#1}{}}{%
    \def\@goodfnk{yes}\global\toggletrue{@realmarker}}{}
  \ifthenelse{\equal{#1}{fake}}{%
    \def\@goodfnk{yes}\global\togglefalse{@realmarker}}{}
  \ifthenelse{\equal{\@goodfnm}{no}\OR\equal{\@goodfnk}{no}}{%
    \ClassError{novel}{Bad choice for \string\SetMarkers}%
    {Optional: real, fake. Required: asterisk, dagger, number.}%
  }{}
  \NDB{3.18.1 in novel-LayoutSettings.sty.}
}
%
\SetMarkerStyle[real]{asterisk} % default
%%


%% Sets the gap between cropmarks and trim area:
\newlength\@mygap
\def\SetCropmarkGap#1{
  \ifthenelse{\lengthtest{#1 < 0pt}}{
    \setlength\@mygap{0pt}
  }{
  \setlength\@mygap{#1}
  }
}
\SetCropmarkGap{0.125in} % default
%%

\NDB{3.19 in novel-LayoutSettings.sty.}


%% Fake \microtypesetup until package is loaded later:
\newif \if@setmicrotype
\gdef\microtypesetup#1{
  \gdef\@mymicrotypesetup{#1} % used in novel-FontDefaults.sty
  \global\@setmicrotypetrue
}
%%

\NDB{3.20 in novel-LayoutSettings.sty.}


%% MISCELLANOUS SETTINGS. Mostly remove "glue". Best to leave as-is.
%% --------------------------------------------------------------------------
%%
%
\@twocolumnfalse
\@twosidetrue
\@mparswitchtrue
%
\abovedisplayskip \z@
\abovedisplayshortskip \z@
\belowdisplayshortskip \z@
\belowdisplayskip \abovedisplayskip
\global\@topnum \z@
%
\setlength\smallskipamount{0pt}
\setlength\medskipamount{0pt}
\setlength\bigskipamount{0pt}
\setlength\topsep{0pt}
\setlength\partopsep{0pt}
\setlength\parsep{0pt}
\setlength\marginparwidth{0pt}
\setlength\marginparsep{0pt}
\setlength\marginparpush{0pt}
\setlength\textfloatsep{0pt}
\setlength\dbltextfloatsep{0pt}
%
\setlength\lineskip{0pt}
\setlength\normallineskip{0pt}
\renewcommand\baselinestretch{1} % multiplier
\setlength\parskip{0pt}
\@lowpenalty   51
\@medpenalty  151
\@highpenalty 301
%
\setcounter{topnumber}{2}
\renewcommand\topfraction{.7}
\setcounter{bottomnumber}{1}
\renewcommand\bottomfraction{.3}
\setcounter{totalnumber}{3}
\renewcommand\textfraction{.2}
\renewcommand\floatpagefraction{.5}
\setcounter{dbltopnumber}{2}
\renewcommand\dbltopfraction{.7}
\renewcommand\dblfloatpagefraction{.5}
%
\DeclareOldFontCommand{\rm}{\normalfont\rmfamily}{\normalfont\rmfamily}
\DeclareOldFontCommand{\sf}{\normalfont\sffamily}{\normalfont\sffamily}
\DeclareOldFontCommand{\tt}{\normalfont\ttfamily}{\normalfont\ttfamily}
\DeclareOldFontCommand{\bf}{\normalfont\bfseries}{\normalfont\bfseries}
\DeclareOldFontCommand{\it}{\normalfont\itshape}{\normalfont\itshape}
\DeclareOldFontCommand{\sl}{\normalfont\slshape}{\normalfont\slshape}
\DeclareOldFontCommand{\sc}{\normalfont\scshape}{\normalfont\scshape}
%
\setlength\columnsep{12pt} % usually not needed, picked something at random
\setlength\columnseprule{0pt}
\pagestyle{empty} % will be over-ridden by fancyhead in separate file
\pagenumbering{arabic} % may be over-ridden in document
\flushbottom % may be over-ridden in document, but not advised
%
%% End miscellaneous settings.

\NDB{3.21 in novel-LayoutSettings.sty.}

% The following might become unnecessary if fontspec adds the capability to
% disable keys. For now: Color and Opacity cannot be used as font features,
%   for a black/white printed book. Alas, fontspec over-rules xcolor.
% So, this code over-rules fontspec:
\gdef\novel@checkfeatures#1{%
  \NDB{3.22.0 in novel-LayoutSettings.sty.}
  \def\@featuresareok{yes}
  \IfSubStr{#1}{Color}{\def\@featuresareok{no}}{}%
  \IfSubStr{#1}{Colour}{\def\@featuresareok{no}}{}%
  \IfSubStr{#1}{color}{\def\@featuresareok{no}}{}%
  \IfSubStr{#1}{colour}{\def\@featuresareok{no}}{}%
  \IfSubStr{#1}{Opacity}{\def\@featuresareok{no}}{}%
  \IfSubStr{#1}{opacity}{\def\@featuresareok{no}}{}%
  \ifthenelse{\equal{\@featuresareok}{yes}}{}{%
    \ClassError{novel}{Color and Opacity forbidden as font features}%
    {You defined a font, or added a font feature, with Color or Opacity.^^J%
     That is disallowed in `novel' class.}%
  }
  \NDB{3.22.1 in novel-LayoutSettings.sty.}
}
%%



%% DEFINITION OF PARENT AND OTHER FONTS
%% --------------------------------------------------------------------------
%% Prior to v. 1.1, parentfont was known as masterfont. Backwards compatible.
%
\providecommand\rmdefault{lmr} % should be lmr anyway, at this point.
\let\@noveloldrmdefault\rmdefault % stored for later comparison.
% The `no-math' option prevents fontspec from tracking certain font changes.
% This is probably necessary, since novel does some of its own font tracking:
\RequirePackage[no-math]{fontspec} % For LuaLaTeX. NOT package `fontenc'.
\defaultfontfeatures{} % reset
% User-created fonts (not standard, not novel-specific defaults:
\defaultfontfeatures{%
  SmallCapsFeatures={Renderer=Basic},Kerning=On,%
  Ligatures=TeX,%
}%
% You shouldn't be doing math! You can use \setmathfont, if you wish.
% If you must load a math font package, it should be done using
%  \RequirePackage before (yes, before) calling the document class.
\RequirePackage{unicode-math}
%%
%% This could have been easier, but fontspec allows mandatory and optional
%   arguments to appear in either order. We wish to store the fontnames
%   and arguments, but need Expl syntax to tell which is which.
\ExplSyntaxOn
%% Store settings for the parent font:
% \AtEndPreamble, the parent font becomes becomes
%   the default ancestor of all novel-defined fonts, except the deco font.
\newif \if@parentfontset
\gdef\@parentfontfeatures{}
\gdef\@parentfontname{}
\NDB{3.23 in novel-LayoutSettings.sty.}
\DeclareDocumentCommand \SetParentFont { O{} m O{} } {% from version 1.1.
  \novel@checkfeatures{#1,#3}
  \gdef\@parentfontfeatures{#1,#3}
  \gdef\@parentfontname{#2}
  \global\@parentfontsettrue
}%
\DeclareDocumentCommand \SetMasterFont { O{} m O{} } {% DEPRECATED
  \novel@checkfeatures{#1,#3}
  \gdef\@parentfontfeatures{#1,#3}
  \gdef\@parentfontname{#2}
  \global\@parentfontsettrue
}%
\NDB{3.24 in novel-LayoutSettings.sty.}
%% Store settings for the chapterfont:
% The chapter font is used by the \ChapterTitle{} command.
\newif \if@chapterfontset
\gdef\@chapterfontfeatures{}
\gdef\@chapterfontname{}
\DeclareDocumentCommand \SetChapterFont { O{} m O{} } {%
  \novel@checkfeatures{#1,#3}
  \gdef\@chapterfontfeatures{#1,#3}
  \gdef\@chapterfontname{#2}
  \global\@chapterfontsettrue
}%
%% Store settings for the subchfont:
% The subch font is used by the \ChapterSubtitle{} command.
\newif \if@subchfontset
\gdef\@subchfontfeatures{}
\gdef\@subchfontname{}
\DeclareDocumentCommand \SetSubchFont { O{} m O{} } {%
  \novel@checkfeatures{#1,#3}
  \gdef\@subchfontfeatures{#1,#3}
  \gdef\@subchfontname{#2}
  \global\@subchfontsettrue
}%
%% Store settings for the headfont:
% The headfont is used in headers and footers.
\newif \if@headfontset
\gdef\@headfontfeatures{}
\gdef\@headfontname{}
\DeclareDocumentCommand \SetHeadFont { O{} m O{} } {%
  \novel@checkfeatures{#1,#3}
  \gdef\@headfontfeatures{#1,#3}
  \gdef\@headfontname{#2}
  \global\@headfontsettrue
}%
%% Store settings for the deco font:
% The deco font is used for decorations and acronyms.
% It must be NovelDeco.otf or a font based on NovelDeco.otf. No other!
\newif \if@decofontset
\newif \if@gotdeco
\gdef\@decofontfeatures{}
\gdef\@decofontname{}
\DeclareDocumentCommand \SetDecoFont { O{} m O{} } {%
  \novel@checkfeatures{#1,#3}
  \gdef\@decofontfeatures{#1,#3}
  \gdef\@decofontname{#2}
  \global\@decofontsettrue
  \global\@gotdecotrue
  \newfontface\decofont[#1,#3]{#2}
}%
\NDB{3.25 in novel-LayoutSettings.sty.}
%
\msg_set:nnnn {fontspec} {font-not-found} { % rewrites the standard message
  Font~ "#1"~ cannot~ be~ found.
  }{
  If~the~missing~font~is~Libertinus~or~Libertine~or~Biolinum~or~NovelDeco,\\
  see~the~`novel'~class~documents~for~help.\\
  If~the~missing~font~is~anything~else,~then~the~missing~font~is\\
  not~caused~by~`novel'~class.\\
}%
\NDB{3.26 in novel-LayoutSettings.sty.}
%% For syntax consistency:
\cs_new_eq:NN \NewFontFamily \newfontfamily
\cs_new_eq:NN \NewFontFace \newfontface
\cs_new_eq:NN \SetSansFont \setsansfont
\cs_new_eq:NN \SetMonoFont \setmonofont
\cs_new_eq:NN \SetMathFont \setmathfont
\cs_new_eq:NN \AddFontFeatures \addfontfeatures
\cs_new_eq:NN \AddFontFeature \addfontfeatures
%%
\NDB{3.27 in novel-LayoutSettings.sty.}
%% \CreateFontFeature{substitutions}{feature code}
% Problem: Font has one or more alternate glyphs that you would like to use,
%   but they are not listed in a feature set for the font.
% Solution: Create your own feature set, with \CreateFontFeature. The created
%   feature may then be used like a real feature, in any font definition.
% The substitutions are a comma-separated list, char=char.alt and so forth.
% For example, the following substitutions are syntactically OK:
%   emdash=emdah.alt, one=one.rev, ntilde=ntilde.03
% If a substitution is not available in a particular font, it is ignored.
% You have to look at the font in a font editor, to know what is available.
% It is possible to make silly substitutions, such as a=b, five=three without
%   throwing an error, unless your code causes a cycle.
% The most useful application of this command is to reach alternate emdash,
%   and alternate oldstyle one. Not every font has these.
% The feature code is a 4-character pseudo- Open Type code, otherwise unused.
% For example, the feature code might be myfs (my fake substitution)
%   but it cannot be a pre-defined code such as lnum or smcp. It may be
%   a style set such as ss03 if you are sure that the font in question
%   do not already have ss03 (but this is taking a chance).
% Once defined, you may use it like this (for example):
%   \SetParentFont[RawFeature=+myfs]{fontname}
%   \NewFontFamily\myfake[RawFeature=+myfs]{fontname}
%   In the document body: {\addfontfeature{myfs} text}
\DeclareDocumentCommand \CreateFontFeature { m m } {%
  \IfEndWith{#1}{,}{\StrGobbleRight{#1}{1}[\@fakezero]}{\def\@fakezero{#1}}
  \def\@fakeone{"\@fakezero"} %%%%%
  \StrSubstitute{\@fakeone}{=}{"]="}[\@fakeone]
  \StrSubstitute{\@fakeone}{,}{",\space["}[\@fakeone]
  \directlua{
    fonts.handlers.otf.addfeature {
      name = "#2",
      type = "substitution",
      data = {\@fakeone},
    }
  }
} %% end \CreateFontFeature. Above thanks to fontspec 2016.
%%
\ExplSyntaxOff
%%
%% end definition of parent and other fonts.

\NDB{3.29 in novel-LayoutSettings.sty.}

%% FAKE POLYGLOSSIA COMMANDS
%% --------------------------------------------------------------------------
% The polyglossia package is not loaded until \AtEndPreamble.
% So, its commands are not directly available in the Preamble.
% These fake commands store the data. Later, the commands will be relaxed,
% then the real polyglossia commands can work, and the data will be passed
% to the real commands at that time.
% Default main language will be set to english by novel-FontDefaults.sty.
\newcommand\SetMainLanguage[2][]{
  \gdef\@setmainlangopt{#1}
  \gdef\@setmainlang{#2}
}
\let\setmainlanguage\SetMainLanguage\relax
\newcommand\SetDefaultLanguage[2][]{
  \gdef\@setmainlangopt{#1}
  \gdef\@setmainlang{#2}
}
\let\setdefaultlanguage\SetDefaultLanguage\relax
\SetMainLanguage[]{} % if not user-specified, defaults to english
\newcommand\SetOtherLanguage[2][]{
  \gdef\@setotherlangopt{#1}
  \gdef\@setotherlang{#2}
}
\let\setotherlanguage\SetOtherLanguage\relax
\SetOtherLanguage[]{} % default empty
\newcommand\SetOtherLanguages[2][]{
  \gdef\@setotherlangsopt{#1}
  \gdef\@setotherlangs{#2}
}
\let\setotherlanguages\SetOtherLanguages\relax
\SetOtherLanguages[]{} % default empty
%
%% end fake polyglossia commands.

\NDB{3.30 in novel-LayoutSettings.sty.}

%% FRONTMATTER, MAINMATTER, BACKMATTER
%% --------------------------------------------------------------------------
%
\newif \ifWithinFrontmatter \WithinFrontmatterfalse
\newif \ifWithinMainmatter \WithinMainmatterfalse
\newif \ifWithinBackmatter \WithinBackmatterfalse
\newcounter{frontmatterpages}
\newcounter{runningpages}
%
% See novel.cls for the AtBeginShipout routine.
%
\NDB{3.32 in novel-LayoutSettings.sty.}
\def\frontmatter{ % starts page count and frontmatter count at 1.
  \ifWithinFrontmatter
    \ClassError{novel}{\string\frontmatter\ used more than once}%
    {You issued \string\frontmatter\ more than once.^^J}%
  \fi
  \ifWithinMainmatter
   \ClassError{novel}{\string\frontmatter\ cannot follow \mainmatter}%
   {In order: \string\frontmatter\ \string\mainmatter\ \string\backmatter^^J%
    but you do not need all of them.^^J}%
  \fi
  \ifWithinBackmatter
   \ClassError{novel}{\string\frontmatter\ cannot follow \backmatter}%
   {In order: \string\frontmatter\ \string\mainmatter\ \string\backmatter^^J%
    but you do not need all of them.^^J}%
  \fi
  \WithinFrontmattertrue\WithinMainmatterfalse\WithinBackmatterfalse
  \clearpage
  \pagenumbering{roman}
  \setcounter{page}{1}
  \setcounter{frontmatterpages}{1}
}
\NDB{3.33 in novel-LayoutSettings.sty.}
%
\def\mainmatter{ % restarts page to 1, arabic numbers; freeze frontpagecount
  \ifWithinMainmatter
    \ClassError{novel}{\string\mainmatter\ used more than once}%
    {You issued \string\mainmatter\ more than once.}%
  \fi
  \ifWithinBackmatter
   \ClassError{novel}{\string\mainmatter\ cannot follow \backmatter}%
   {In order: \string\frontmatter\ \string\mainmatter\ \string\backmatter^^J%
    but you do not need all of them.^^J}%
  \fi
  \WithinFrontmatterfalse\WithinMainmattertrue\WithinBackmatterfalse
  \setcounter{frontmatterpages}{\value{page}}
  \addtocounter{frontmatterpages}{-1}
  \pagenumbering{arabic}
  \setcounter{page}{1}
}
\NDB{3.34 in novel-LayoutSettings.sty.}
%
\def\backmatter{\@ifstar\@backmatterst\@backmatterns}
%
\def\@backmatterst{% continues frontmatter count w/ lowercase roman numerals
  \ifWithinBackmatter
    \ClassError{novel}{\string\backmatter\space used more than once}%
    {You issued \string\backmatter\space more than once.^^J}%
  \fi
  % If you really must go directly from front to back without main,
  %   simply issue \mainmatter\backmatter together. Not advised.
  \ifWithinFrontmatter
  \ClassError{novel}{Missing \string\mainmatter}%
  {You went directly from \string\frontmatter\space to \string\backmatter^^J%
   Must have \string\mainmatter\space in between.^^J}%
  \fi
  \WithinFrontmatterfalse\WithinMainmatterfalse\WithinBackmattertrue
  \addtocounter{frontmatterpages}{1}
  \pagenumbering{roman}
  \setcounter{page}{\value{frontmatterpages}}
}
%
\def\@backmatterns{ % continues mainmatter arabic numerals
  \ifWithinBackmatter
    \ClassError{novel}{\string\backmatter\space used more than once}%
    {You issued \string\backmatter\space more than once.^^J}%
  \fi
  % If you really must go directly from front to back without main,
  %   simply issue \mainmatter\backmatter together. Not advised.
  \ifWithinFrontmatter
  \ClassError{novel}{Missing \string\mainmatter}%
  {You went directly from \string\frontmatter\space to \string\backmatter^^J%
   Must have \string\mainmatter\space in between.^^J}%
  \fi
  \WithinFrontmatterfalse\WithinMainmatterfalse\WithinBackmattertrue
}
\NDB{3.35 in novel-LayoutSettings.sty.}
%
\def\Backmatter{\@ifstar\@Backmatterst\@Backmatterns}
%
\def\@Backmatterst{% continues frontmatter count w/ uppercase Roman numerals
  \ifWithinBackmatter
    \ClassError{novel}{\string\backmatter\space used more than once}%
    {You issued \string\backmatter\space more than once.^^J}%
  \fi
  % If you really must go directly from front to back without main,
  %   simply issue \mainmatter\backmatter together. Not advised.
  \ifWithinFrontmatter
  \ClassError{novel}{Missing \string\mainmatter}%
  {You went directly from \string\frontmatter\space to \string\backmatter^^J%
   Must have \string\mainmatter\space in between.^^J}%
  \fi
  \WithinFrontmatterfalse\WithinMainmatterfalse\WithinBackmattertrue
  \addtocounter{frontmatterpages}{1}
  \pagenumbering{Roman}
  \setcounter{page}{\value{frontmatterpages}}
}
%
\def\@Backmatterns{ % restarts page count with uppercase Roman numerals
  \ifWithinBackmatter
    \ClassError{novel}{\string\backmatter\ used more than once}%
    {You issued \string\backmatter\ more than once.^^J}%
  \fi
  % If you really must go directly from front to back without main,
  %   simply issue \mainmatter\backmatter together. Not advised.
  \ifWithinFrontmatter
  \ClassError{novel}{Missing \string\mainmatter}%
  {You went directly from \string\frontmatter\space to \string\backmatter^^J%
   Must have \string\mainmatter\space in between.^^J}%
  \fi
  \WithinFrontmatterfalse\WithinMainmatterfalse\WithinBackmattertrue
  \pagenumbering{Roman}
  \setcounter{page}{1}
}
%
%% end frontmatter, mainmatter, backmatter.
\NDB{3.36 in novel-LayoutSettings.sty.}


%% DISABLE OR NEUTRALIZE SOME STANDARD LATEX COMMANDS
%% --------------------------------------------------------------------------
% The `novel' class disallows these commands, or neutralizes them by simply
% repeating their arguments. In some cases, they request functionality that's
% not implemented in `novel'. In other cases, functionality is part of core,
% but would interfere with the `novel' emphasis on constant line skip.
% You may redefine the commands using your own Preamble code (discouraged).
%
\gdef\null{\hbox{\strut}} % May be important!
%
% Standard LaTeX font sizes are ineffective:
\let\small\relax
\let\footnotesize\relax
\let\scriptsize\relax
\let\tiny\relax
%
\let\large\relax
\let\Large\relax
\let\LARGE\relax
\let\huge\relax
\let\Huge\relax
\let\HUGE\relax
% All these do is echo the argument, if any:
\long\gdef\part#1{#1}
\long\gdef\chapter#1{#1}
\long\gdef\section#1{#1}
\long\gdef\subsection#1{#1}
\long\gdef\subsubsection#1{#1}
\long\gdef\paragraph#1{#1}
\long\gdef\subparagraph#1{#1}
% In some situations, the use will be interrupted:
\gdef\@nodocommand#1{
  \ClassError{novel}{No command `\string\#1' in this class}%
    {The `\string\#1' command is not implemented in `novel' class.^^J}%
}
% Here is the interruption list:
\long\gdef\maketitle\@nodocommand{maketitle}
\long\gdef\makeindex\@nodocommand{makeindex}
\long\gdef\tableofcontents\@nodocommand{tableofcontents}
\long\gdef\listoftables\@nodocommand{listoftables}
\long\gdef\listoffigures\@nodocommand{listoffigures}
%
%% end disable or neutralize.
\NDB{3.37 in novel-LayoutSettings.sty.}


%%
%% End of file `novel-LayoutSettings.sty'.

