\ExplSyntaxOn

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Environment for read-aloud text.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Standard for every read-aloud text
\DeclareTColorBox {__rpg_read_aloud} {o}
  {
    before~upper    =
      {
        \bool_if:NT \l__rpg_layout_bool
          {
            \bool_if:NF \l__rpg_justified_bool
              {\RaggedRight}
          }
      },
    code            = {\linespread {1.1} },
    enhanced~jigsaw,
    frame~hidden,
    boxrule         = 0pt,
    breakable,
    enhanced,
    before~skip     = 13pt plus 4pt minus 4pt,
    boxsep          = 8pt,
    top             = 0pt,
    left            = 0pt,
    right           = 0pt,
    bottom          = 0pt,
    sharp~corners,
    parbox          = false,
    borderline~west = {1pt} {-0.5pt} {titlered},
    borderline~east = {1pt} {-0.5pt} {titlered},
    fontupper       = \RpgFontReadAloud,
    fontlower       = \RpgFontReadAloud,
    overlay         =
      {
        \foreach\n ~ in ~ { north~east, north~west, south~east, south~west }
        {\draw [ titlered, fill = titlered ] ( frame.\n ) circle (1.5pt); };
      },
    after~skip      = 13pt plus 4pt minus 4pt,
    #1,
  }

% Extra key for controlling the color of the title and the background
\keys_define:nn { rpg / read_aloud }
  {
    color .tl_set:N         = \l__rpg_read_aloud_color_tl,
    color .initial:n        = readaloudcolor,
    color .value_required:n = true,
  }

% This function, through its variant, forces the expansion of the tcb keys
% passed to the environment by the user before it invokes the tcolorbox
\cs_new_protected:Nn \__rpg_start_read_aloud:nn
  {
    \begin {__rpg_read_aloud} [ #1, #2 ]
  }

\cs_generate_variant:Nn \__rpg_start_read_aloud:nn { nV }

% The RpgReadAloud environment
% #1 - keys. We handle the custom color key before passing other keys on
\NewDocumentEnvironment {RpgReadAloud} {o}
  {
    \group_begin:
    \keys_set_known:nnN { rpg / read_aloud } {#1} \l_tmpa_tl

    \str_if_eq:VnT \l_tmpa_tl { -NoValue- }
      { \tl_set_eq:NN \l_tmpa_tl \c_empty_tl }

    \__rpg_start_read_aloud:nV
      {
          colback = \l__rpg_read_aloud_color_tl,
      }
      {\l_tmpa_tl}
  }
  {
    \end {__rpg_read_aloud}
    \group_end:
  }
