\ExplSyntaxOn

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Font definitions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{lettrine}
\RequirePackage{Royal}
\RequirePackage[auto]{contour}

\bool_if:NT \l__rpg_layout_bool
  {
    \RequirePackage{bookman}
    \RequirePackage[type1]{gillius2}
    \RequirePackage[notext,nomath,nott]{kpfonts}
    \RequirePackage[T1]{fontenc}
    \renewcommand{\sfdefault}{jkpss}
  }

\cs_new_protected:Npn \__rpg_sf_initial_family:
  {
    \bool_if:NTF \l__rpg_layout_bool
      { \gilliustwo }
      { \sffamily }
  }

\keys_define:nn { rpg / fonts }
  {
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Sectioning commands
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Part
    part-family .tl_set:N         = \l__rpg_part_family_tl,
    part-family .initial:n        = \normalfont,
    part-family .value_required:n = true,
    part-style .tl_set:N         = \l__rpg_part_style_tl,
    part-style .initial:n        = \color{titlered} \Huge \scshape,
    part-style .value_required:n = true,
    % Chapter
    chapter-family .tl_set:N         = \l__rpg_chapter_family_tl,
    chapter-family .initial:n        = \normalfont,
    chapter-family .value_required:n = true,
    chapter-style .tl_set:N         = \l__rpg_chapter_style_tl,
    chapter-style .initial:n        = \linespread{.9} \color{titlered} \Huge \scshape,
    chapter-style .value_required:n = true,
    % Section
    section-family .tl_set:N         = \l__rpg_section_family_tl,
    section-family .initial:n        = \normalfont,
    section-family .value_required:n = true,
    section-style .tl_set:N         = \l__rpg_section_style_tl,
    section-style .initial:n        = \linespread{.9} \color{titlered} \huge \scshape \RaggedRight,
    section-style .value_required:n = true,
    % Subsection
    subsection-family .tl_set:N         = \l__rpg_subsection_family_tl,
    subsection-family .initial:n        = \normalfont,
    subsection-family .value_required:n = true,
    subsection-style .tl_set:N         = \l__rpg_subsection_style_tl,
    subsection-style .initial:n        = \linespread{.9} \color{titlered} \Large \scshape \RaggedRight,
    subsection-style .value_required:n = true,
    % subsubsection
    subsubsection-family .tl_set:N         = \l__rpg_subsubsection_family_tl,
    subsubsection-family .initial:n        = \normalfont,
    subsubsection-family .value_required:n = true,
    subsubsection-style .tl_set:N         = \l__rpg_subsubsection_style_tl,
    subsubsection-style .initial:n        = \linespread{.9} \color{titlered} \large \scshape \RaggedRight,
    subsubsection-style .value_required:n = true,
    % paragraph
    paragraph-family .tl_set:N         = \l__rpg_paragraph_family_tl,
    paragraph-family .value_required:n = true,
    paragraph-style .tl_set:N         = \l__rpg_paragraph_style_tl,
    paragraph-style .initial:n        = \bfseries \slshape,
    paragraph-style .value_required:n = true,
    % subparagraph
    subparagraph-family .tl_set:N         = \l__rpg_subparagraph_family_tl,
    subparagraph-family .value_required:n = true,
    subparagraph-style .tl_set:N         = \l__rpg_subparagraph_style_tl,
    subparagraph-style .initial:n        = \bfseries \slshape,
    subparagraph-style .value_required:n = true,
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Tables
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Table title
    table-title-family .tl_set:N         = \l__rpg_table_title_family_tl,
    table-title-family .initial:n        = \sffamily,
    table-title-family .value_required:n = true,
    table-title-style .tl_set:N         = \l__rpg_table_title_style_tl,
    table-title-style .initial:n        = \bfseries \scshape \large,
    table-title-style .value_required:n = true,
    % Table header
    table-header-family .tl_set:N         = \l__rpg_table_header_family_tl,
    table-header-family .initial:n        = \sffamily,
    table-header-family .value_required:n = true,
    table-header-style .tl_set:N         = \l__rpg_table_header_style_tl,
    table-header-style .initial:n        = \bfseries,
    table-header-style .value_required:n = true,
    % Table body
    table-body-family .tl_set:N         = \l__rpg_table_body_family_tl,
    table-body-family .initial:n        = \__rpg_sf_initial_family:,
    table-body-family .value_required:n = true,
    table-body-style .tl_set:N         = \l__rpg_table_body_style_tl,
    table-body-style .initial:n        = \small,
    table-body-style .value_required:n = true,
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Comment boxes
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Comment title
    comment-title-family .tl_set:N         = \l__rpg_comment_title_family_tl,
    comment-title-family .initial:n        = \sffamily,
    comment-title-family .value_required:n = true,
    comment-title-style .tl_set:N         = \l__rpg_comment_title_style_tl,
    comment-title-style .initial:n        = \bfseries \scshape,
    comment-title-style .value_required:n = true,
    % Comment body
    comment-body-family .tl_set:N         = \l__rpg_comment_body_family_tl,
    comment-body-family .initial:n        = \__rpg_sf_initial_family:,
    comment-body-family .value_required:n = true,
    comment-body-style .tl_set:N         = \l__rpg_comment_body_style_tl,
    comment-body-style .initial:n        = \small,
    comment-body-style .value_required:n = true,
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Sidebars
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Sidebar title
    sidebar-title-family .tl_set:N         = \l__rpg_sidebar_title_family_tl,
    sidebar-title-family .initial:n        = \sffamily,
    sidebar-title-family .value_required:n = true,
    sidebar-title-style .tl_set:N         = \l__rpg_sidebar_title_style_tl,
    sidebar-title-style .initial:n        = \bfseries \scshape,
    sidebar-title-style .value_required:n = true,
    % Sidebar body
    sidebar-body-family .tl_set:N         = \l__rpg_sidebar_body_family_tl,
    sidebar-body-family .initial:n        = \__rpg_sf_initial_family:,
    sidebar-body-family .value_required:n = true,
    sidebar-body-style .tl_set:N         = \l__rpg_sidebar_body_style_tl,
    sidebar-body-style .initial:n        = \small,
    sidebar-body-style .value_required:n = true,
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Read-aloud boxes
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    readaloud-family .tl_set:N         = \l__rpg_readaloud_family_tl,
    readaloud-family .initial:n        = \__rpg_sf_initial_family:,
    readaloud-family .value_required:n = true,
    readaloud-style .tl_set:N         = \l__rpg_readaloud_style_tl,
    readaloud-style .initial:n        = \small,
    readaloud-style .value_required:n = true,
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Table of Contents
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Part
    toc-part-family .tl_set:N         = \l__rpg_toc_part_family_tl,
    toc-part-family .initial:n        = \normalfont,
    toc-part-family .value_required:n = true,
    toc-part-style .tl_set:N         = \l__rpg_toc_part_style_tl,
    toc-part-style .initial:n        = \Large \scshape \color{titlered},
    toc-part-style .value_required:n = true,
    % Chapter
    toc-chapter-family .tl_set:N         = \l__rpg_toc_chapter_family_tl,
    toc-chapter-family .initial:n        = \normalfont,
    toc-chapter-family .value_required:n = true,
    toc-chapter-style .tl_set:N         = \l__rpg_toc_chapter_style_tl,
    toc-chapter-style .initial:n        = \large \scshape \color{titlered},
    toc-chapter-style .value_required:n = true,
    % Section
    toc-section-family .tl_set:N         = \l__rpg_toc_section_family_tl,
    toc-section-family .initial:n        = \normalfont,
    toc-section-family .value_required:n = true,
    toc-section-style .tl_set:N         = \l__rpg_toc_section_style_tl,
    toc-section-style .initial:n        = \normalsize,
    toc-section-style .value_required:n = true,
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Stat blocks
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Stat block title
    stat-block-title-family .tl_set:N         = \l__rpg_stat_block_title_family_tl,
    stat-block-title-family .initial:n        = \normalfont,
    stat-block-title-family .value_required:n = true,
    stat-block-title-style .tl_set:N         = \l__rpg_stat_block_title_style_tl,
    stat-block-title-style .initial:n        = \bfseries \scshape \LARGE,
    stat-block-title-style .value_required:n = true,
    % Stat block body
    stat-block-body-family .tl_set:N         = \l__rpg_stat_block_body_family_tl,
    stat-block-body-family .initial:n        = \__rpg_sf_initial_family:,
    stat-block-body-family .value_required:n = true,
    stat-block-body-style .tl_set:N         = \l__rpg_stat_block_body_style_tl,
    stat-block-body-style .initial:n        = \small,
    stat-block-body-style .value_required:n = true,
    % Stat block section
    stat-block-section-family .tl_set:N         = \l__rpg_stat_block_section_family_tl,
    stat-block-section-family .initial:n        = \sffamily,
    stat-block-section-family .value_required:n = true,
    stat-block-section-style .tl_set:N         = \l__rpg_stat_block_section_style_tl,
    stat-block-section-style .initial:n        = \color{titlered} \scshape \large,
    stat-block-section-style .value_required:n = true,
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Miscellaneous
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Footer
    footer-family .tl_set:N         = \l__rpg_footer_family_tl,
    footer-family .initial:n        = \normalfont,
    footer-family .value_required:n = true,
    footer-style .tl_set:N         = \l__rpg_footer_style_tl,
    footer-style .initial:n        = \scriptsize \textcolor{pagegold},
    footer-style .value_required:n = true,
    % Page number
    page-number-family .tl_set:N         = \l__rpg_page_number_family_tl,
    page-number-family .initial:n        = \normalfont,
    page-number-family .value_required:n = true,
    page-number-style .tl_set:N         = \l__rpg_page_number_style_tl,
    page-number-style .initial:n        = \scriptsize \textcolor{pagegold},
    page-number-style .value_required:n = true,
    % Drop caps
    drop-cap-family .tl_set:N         = \l__rpg_drop_cap_family_tl,
    drop-cap-family .initial:n        = \Royal,
    drop-cap-family .value_required:n = true,
    drop-cap-style .tl_set:N         = \l__rpg_drop_cap_style_tl,
    drop-cap-style .value_required:n = true,
  }

\NewDocumentCommand { \RpgSetFonts } { o }
  {
    \keys_set:nn { rpg / fonts } { #1 }
  }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Font access functions combine the selected family and style
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sectioning commands
\NewDocumentCommand{\RpgFontPart}{}
  { \l__rpg_part_family_tl \l__rpg_part_style_tl}

\NewDocumentCommand{\RpgFontChapter}{}
  { \l__rpg_chapter_family_tl \l__rpg_chapter_style_tl }

\NewDocumentCommand{\RpgFontSection}{}
  { \l__rpg_section_family_tl \l__rpg_section_style_tl }

\NewDocumentCommand{\RpgFontSubsection}{}
  { \l__rpg_subsection_family_tl \l__rpg_subsection_style_tl }

\NewDocumentCommand{\RpgFontSubsubsection}{}
  { \l__rpg_subsubsection_family_tl \l__rpg_subsubsection_style_tl }

\NewDocumentCommand{\RpgFontParagraph}{}
  { \l__rpg_paragraph_family_tl \l__rpg_paragraph_style_tl }

\NewDocumentCommand{\RpgFontSubaragraph}{}
  { \l__rpg_subparagraph_family_tl \l__rpg_subparagraph_style_tl }

% Tables
\NewDocumentCommand{\RpgFontTableTitle}{}
  { \l__rpg_table_title_family_tl \l__rpg_table_title_style_tl }

\NewDocumentCommand{\RpgFontTableHeader}{}
  { \l__rpg_table_header_family_tl \l__rpg_table_header_style_tl }

\NewDocumentCommand{\RpgFontTableBody}{}
  { \l__rpg_table_body_family_tl \l__rpg_table_body_style_tl }

% Comment boxes
\NewDocumentCommand{\RpgFontCommentTitle}{}
  { \l__rpg_comment_title_family_tl \l__rpg_comment_title_style_tl }

\NewDocumentCommand{\RpgFontCommentBody}{}
  { \l__rpg_comment_body_family_tl \l__rpg_comment_body_style_tl }

% Sidebars
\NewDocumentCommand{\RpgFontSidebarTitle}{}
  { \l__rpg_sidebar_title_family_tl \l__rpg_sidebar_title_style_tl }

\NewDocumentCommand{\RpgFontSidebarBody}{}
  { \l__rpg_sidebar_body_family_tl \l__rpg_sidebar_body_style_tl }

% Read-aloud boxes
\NewDocumentCommand{\RpgFontReadAloud}{}
  { \l__rpg_readaloud_family_tl \l__rpg_readaloud_style_tl }

% Table of Contents
\NewDocumentCommand{\RpgFontTocPart}{}
  { \l__rpg_toc_part_family_tl \l__rpg_toc_part_style_tl}

\NewDocumentCommand{\RpgFontTocChapter}{}
  { \l__rpg_toc_chapter_family_tl \l__rpg_toc_chapter_style_tl}

\NewDocumentCommand{\RpgFontTocSection}{}
  { \l__rpg_toc_section_family_tl \l__rpg_toc_section_style_tl}

% Stat blocks
\NewDocumentCommand{\RpgFontStatBlockTitle}{}
  { \l__rpg_stat_block_title_family_tl \l__rpg_stat_block_title_style_tl }

\NewDocumentCommand{\RpgFontStatBlockBody}{}
  { \l__rpg_stat_block_body_family_tl \l__rpg_stat_block_body_style_tl }

\NewDocumentCommand{\RpgFontStatBlockSection}{}
  { \l__rpg_stat_block_section_family_tl \l__rpg_stat_block_section_style_tl }

% Miscellaneous
\NewDocumentCommand{\RpgFontFooter}{}
  { \l__rpg_footer_family_tl \l__rpg_footer_style_tl }

\NewDocumentCommand{\RpgFontPageNumber}{}
  { \l__rpg_page_number_family_tl \l__rpg_page_number_style_tl }

\NewDocumentCommand{\RpgFontDropCap}{}
  { \l__rpg_drop_cap_family_tl \l__rpg_drop_cap_style_tl }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Drop Caps
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\renewcommand{\LettrineFontHook}
  { \l__rpg_drop_cap_family_tl \l__rpg_drop_cap_style_tl }

% Usage: RpgDropCapLine[<lettrine options>]{<first letter>}{<small caps line>}
%    It takes trial and error to get the 2nd argument to align with
%    the linebreak. Lettrine package will not play nicely with \FirstLine. See
%    the lettrine package for options.
\NewDocumentCommand{\RpgDropCapLine}{ O{} m m }
  {
    \lettrine[
        lines   = 4,
        depth   = 0,
        findent = \l__rpg_space_dim,
        nindent = 0pt,
        #1
      ]
      {#2}
      {#3}
  }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Contour that can break across lines. Can accept \newline to force a break.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\cs_new_protected:Npn \__rpg_contour_preserve_space:nn #1#2
  {
    \group_begin:
    \seq_set_split:Nnn \l_tmpa_seq { ~ } { #2 }
    \seq_set_map:NNn \l_tmpb_seq \l_tmpa_seq { \exp_not:n {\contour{#1}{##1}} }
    \seq_use:Nn \l_tmpb_seq { ~ }
    \group_end:
  }

\NewDocumentCommand{\RpgContour}{ O{contourgray} m }
  {
    \group_begin:
    \seq_set_split:Nnn \l_tmpa_seq { \newline } { #2 }
    \seq_set_map:NNn \l_tmpb_seq \l_tmpa_seq { \exp_not:n {\__rpg_contour_preserve_space:nn{#1}{##1}} }
    \seq_use:Nn \l_tmpb_seq { \newline }
    \group_end:
  }
