% \iffalse meta-comment
%
% Copyright (c) 2026 David Purton <dcpurton@marshwiggle.net>
%
% This work 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.
%
%<*driver>
\RequirePackage{pdfmanagement}
\documentclass[a4paper]{l3doc}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage{mlmodern}
\usepackage[font=small, skip=6pt]{caption}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{sblfonts}

\AddToHook{env/macrocode/before}{%
  \addvspace{\medskipamount}}

\AddToHook{env/macro/before}{%
  \addvspace{\medskipamount}}

\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{The \pkg{sblfonts} Package}
% \author{David Purton\thanks{Email: \url{dcpurton@marshwiggle.net}}}
% \date{2026-04-04 v1.0}
%
% \maketitle
%
% \begin{abstract}
%   \pkg{sblfonts} provides a LaTeX package for setting up font and language
%   support for documents conforming to the requirements of the Society of
%   Biblical Literature. Greek and Hebrew languages are supported using the
%   SBL fonts. If these fonts are not available then fonts included with
%   TeXLive are used.
% \end{abstract}
%
% \tableofcontents
%
% \section{Introduction}
%
% This package is intended to be used with the SBL fonts and a Unicode \TeX\
% engine (|lualatex| is recommended). The SBL fonts are free for personal use
% and can be downloaded from \url{https://www.sbl-site.org/resources/fonts/}.
% It is, however, possible to use the package with any Unicode font and there
% is limited support for 8bit fonts under |pdflatex| (in particular pointed
% Hebrew is not easily possible).
%
% \subsection{Usage}
%
% To use default \LaTeX\ fonts, load the package with no options. The \LaTeX\
% source is shown in Figure \ref{exampleusage} and the output is shown in
% Figure \ref{exampleoutput}.
%
% In reality, you will want to install the SBL fonts, use |lualatex|, and load
% the package with |\usepackage[sblfonts]{sblfonts}|. Note that it is not
% necessary to mark up inline Greek and Hebrew with |lualatex|.
%
% \begin{figure}
%   \centering
%   \framebox[\linewidth]{%
%     \parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{%
%       \small\ttfamily
%       \textbackslash\textcolor{blue!70!black}{documentclass}\{article\}\\
%       \textbackslash\textcolor{blue!70!black}{usepackage}\{sblfonts\}\\
%       \textbackslash\textcolor{blue!70!black}{begin}\{document\}\\
%       \textbackslash\textcolor{blue!70!black}{begin}\{quotation\}\\
%       \phantom{..}\textbackslash\textcolor{blue!70!black}{selectlanguage}\{polytonicgreek\}\\
%       {\fontencoding{LGR}\fontfamily{cmtt}\selectfont
%        \phantom{..}Ἐν ἀρχῇ ἦν ὁ λόγος, καὶ ὁ λόγος ἦν πρὸς τὸν θεόν, καὶ θεὸς ἦν ὁ λόγος.\\
%        \phantom{..}οὗτος ἦν ἐν ἀρχῇ πρὸς τὸν θεόν.}\\
%       \textbackslash\textcolor{blue!70!black}{end}\{quotation\}\\
%       \textbackslash\textcolor{blue!70!black}{begin}\{quotation\}\\
%       \phantom{..}\textbackslash\textcolor{blue!70!black}{selectlanguage}\{hebrew\}\\
%       {\fontencoding{NHE8}\fontfamily{miriam}\fontseries{b}\selectfont
%       \phantom{..}\foreignlanguage{hebrew}{ב\kern-4.6pt ּ\kern-1ptראש\kern-5.3pt ׁית
%       ב\kern-4.6pt ּ\kern-1ptרא אלהים את הש\kern-5.3pt ׁ\kern-6.3pt ּ\kern1ptמים ואת
%       הארץ׃ והארץ היתה תהו\kern-4pt ּ ובהו\kern-4pt ּ וחש\kern-5.3pt ׁך}\\
%       \phantom{..}\foreignlanguage{hebrew}{על־פ\kern-5.8pt ּני תהום
%       ורו\kern-4pt ּח אלהים מרחפת על־פ\kern-5.8pt ּני המ\kern-5.5pt ּים׃}}\\
%       \textbackslash\textcolor{blue!70!black}{end}\{quotation\}\\
%       Inline Greek
%       (\textbackslash\textcolor{blue!70!black}{foreignlanguage}\{polytonicgreek\}\{{\fontencoding{LGR}\fontfamily{cmtt}\selectfont
%       Ἐν ἀρχῇ}\}) and Hebrew
%       (\textbackslash\textcolor{blue!70!black}{foreignlanguage}\{hebrew\}\{{\fontencoding{NHE8}\fontfamily{miriam}\fontseries{b}\selectfont\foreignlanguage{hebrew}{ב\kern-4.6pt ּ\kern-1ptראש\kern-5.3pt ׁית}}\})
%       are also possible.\\
%       \textbackslash\textcolor{blue!70!black}{end}\{document\}%
%     }%
%   }
%   \caption{Example \LaTeX\ source showing default Greek and Hebrew fonts.}
%   \label{exampleusage}
% \end{figure}
%
% \begin{figure}
%   \centering
%   \framebox[\linewidth]{%
%     \parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{%
%       \begin{quotation}
%         \selectlanguage{polytonicgreek}
%         Ἐν ἀρχῇ ἦν ὁ λόγος, καὶ ὁ λόγος ἦν πρὸς τὸν θεόν, καὶ θεὸς ἦν ὁ
%         λόγος.  οὗτος ἦν ἐν ἀρχῇ πρὸς τὸν θεόν.
%       \end{quotation}
%       \begin{quotation}
%         \selectlanguage{hebrew}
%         בּראשׁית בּרא אלהים את השּׁמים ואת הארץ׃ והארץ היתה תהוּ ובהוּ וחשׁך על־פּני
%         תהום ורוּח אלהים מרחפת על־פּני המּים׃
%       \end{quotation}
%       Inline Greek (\foreignlanguage{polytonicgreek}{Ἐν ἀρχῇ}) and Hebrew
%       (\foreignlanguage{hebrew}{בּראשׁית}) are also possible.
%     }%
%   }
%   \caption{Example output showing Greek and Hebrew in default \LaTeX\
%     fonts.}
%   \label{exampleoutput}
% \end{figure}
%
% \subsection{Bug Reports and Feature Requests}
%
% Bug reports and feature requests can be made at the \pkg{sbltex}
% GitHub repository. See \url{https://github.com/dcpurton/sbltex}.
%
% \section{Package Options}
%
% \begin{function}{mainfont}
%   \begin{syntax}
%     mainfont = \meta{package} \textbar\ \meta{fontspec name}\hfill Default: \emph{unset}
%   \end{syntax}
%   Set the main font. Under |pdftex| this is the name of a font package to
%   load. Under Unicode engines it should be a \pkg{fontspec} name which is
%   passed to \cs{babelfont}. The optional arguments passed to the package or
%   \cs{babelfont} can be specified in the |mainfontoptions| option.
% \end{function}
%
% \begin{function}{mainfontoptions}
%   \begin{syntax}
%     mainfontoptions = \meta{package options} \textbar\ \meta{fontspec options}\hfill Default: \emph{unset}
%   \end{syntax}
%   The optional arguments passed to the font package under |pdflatex| or
%   \cs{babelfont} under Unicode engines.
% \end{function}
%
% \begin{function}{grfont}
%   \begin{syntax}
%     grfont = \meta{NFSS font} \textbar\ \meta{fontspec name}\hfill Default: \emph{unset}
%   \end{syntax}
%   Set the Greek font. For |pdflatex| this should be an NFSS font name which
%   supports the LGR encoding. For Unicode engines this should be a
%   \pkg{fontspec} font name. If unset, then under |pdflatex| the default
%   font is |grmn1000| and under Unicode engines the default font is CMU Serif
%   or |mainfont| if it supports Greek.
% \end{function}
%
% \begin{function}{hefont}
%   \begin{syntax}
%     hefont = \meta{NFSS font} \textbar\ \meta{fontspec name}\hfill Default: \emph{unset}
%   \end{syntax}
%   Set the Hebrew font. For |pdflatex| this should be an NFSS font name which
%   supports the NHE8 encoding. For Unicode engines this should be a
%   \pkg{fontspec} font name. If unset, then under |pdflatex| the default
%   font is |david| and under Unicode engines the default font is DavidCLM
%   or |mainfont| if it supports Hebrew.
%
%   In reality, |pdflatex| is really only supported for interest. You should
%   use |lualtex| for any right-to-left languages.
% \end{function}
%
% \subsection{Unicode only options}
%
% The following options control whether the SBL Greek and Hebrew fonts should
% be used.
%
% \begin{function}{sblfonts}
%   \begin{syntax}
%     sblfonts = true \textbar\ false \textbar\ fallback\hfill Default: fallback
%   \end{syntax}
%   This is a shortcut option for setting both |sblgreek| and |sblhebrew| at
%   the same time.
% \end{function}
%
% \begin{function}{sblgreek}
%   \begin{syntax}
%     sblgreek = true \textbar\ false \textbar\ fallback\hfill Default: fallback
%   \end{syntax}
%   This option controls if and when the SBL Greek font should be used. The order
%   of fonts tried for each option is:
%   \begin{description}
%     \item[\texttt{true}] SBL BibLit, SBL Greek, Alegreya.
%     \item[\texttt{false}] |mainfont| (if specified) otherwise CMU Serif.
%       This is useful if the test to detect Greek support in |mainfont| is
%       not accurate.
%     \item[\texttt{fallback}] |mainfont| (if specified and if Greek is
%     supported) otherwise CMU Serif.
%   \end{description}
% \end{function}
%
% \begin{function}{sblhebrew}
%   \begin{syntax}
%     sblhebrew = true \textbar\ false \textbar\ fallback\hfill Default: fallback
%   \end{syntax}
%   This option controls if and when the SBL Hebrew font should be used. The order
%   of fonts tried for each option is:
%   \begin{description}
%     \item[\texttt{true}] SBL BibLit, SBL Hebrew, Frank Ruehl CLM.
%     \item[\texttt{false}] |mainfont| (if specified) otherwise DavidCLM.
%       This is useful if the test to detect Hebrew support in |mainfont| is
%       not accurate.
%     \item[\texttt{fallback}] |mainfont| (if specified and if Hebrew is
%     supported) otherwise DavidCLM.
%   \end{description}
% \end{function}
%
% \section{Implementation}
%
% \setlength{\parindent}{0em}
%
%    \begin{macrocode}
%<*package>
%<@@=sblfonts>
%    \end{macrocode}
%
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage{sblfonts}{2026-04-04}{1.0}
  {Society of Biblical Literature Font and Language Set Up (DCP)}
%    \end{macrocode}
%
%    \begin{macrocode}
\keys_define:nn { sblfonts }
  {
    , mainfont        .tl_set:N    = \l_@@_mainfont_tl
    , mainfontoptions .clist_set:N = \l_@@_mainfont_options_clist
    , grfont          .tl_set:N    = \l_@@_grfont_tl
    , hefont          .tl_set:N    = \l_@@_hefont_tl
  }
%    \end{macrocode}
%
%    \begin{macrocode}
\str_new:N \l_@@_sblgreek_str
\str_new:N \l_@@_sblhebrew_str
%    \end{macrocode}
%
%    \begin{macrocode}
\sys_if_engine_opentype:T
  {
    \keys_define:nn { sblfonts }
      {
        , sblfonts  .choices:nn = { true, false, fallback }
                                  {
                                    \str_set:NV
                                      \l_@@_sblgreek_str
                                      \l_keys_choice_str
                                    \str_set:NV
                                      \l_@@_sblhebrew_str
                                      \l_keys_choice_str
                                  }
        , sblfonts  .default:n  = { true }
        , sblgreek  .choices:nn = { true, false, fallback }
                                  {
                                    \str_set:NV
                                      \l_@@_sblgreek_str
                                      \l_keys_choice_str
                                  }
        , sblgreek  .default:n  = { true }
        , sblgreek  .initial:n  = { fallback }
        , sblhebrew .choices:nn = { true, false, fallback }
                                  {
                                    \str_set:NV
                                      \l_@@_sblhebrew_str
                                      \l_keys_choice_str
                                  }
        , sblhebrew .default:n  = { true }
        , sblhebrew .initial:n  = { fallback }
      }
  }
%    \end{macrocode}
%
%    \begin{macrocode}
\ProcessKeyOptions
%    \end{macrocode}
%
% \subsection{Language Set Up}
%
% \cs{babelprovide} macros need to be run outside of ExplSyntax.
%
%    \begin{macrocode}
\ExplSyntaxOff
%    \end{macrocode}
%
%    \begin{macrocode}
\newcommand*{\@sblfonts@load@american}{%
  \babelprovide[import, main]{american}}
\newcommand*{\@sblfonts@load@polytonicgreek}{%
  \babelprovide[import, onchar=ids fonts]{polytonicgreek}}
\newcommand*{\@sblfonts@load@hebrew}{%
  \babelprovide[import, onchar=ids fonts]{hebrew}}
%    \end{macrocode}
%
%    \begin{macrocode}
\ExplSyntaxOn
%    \end{macrocode}
%
% \begin{macro}{\@@_load_languages:}
%
%   Load American, Polytonic Greek and Hebrew languages using \pkg{babel}.
%
%    \begin{macrocode}
\cs_new_protected:Nn \@@_load_languages:
  {
    \tl_if_eq:NnT \languagename { nil }
      { \@sblfonts@load@american }
    \@sblfonts@load@polytonicgreek
    \@sblfonts@load@hebrew
    \babeladjust{ select.write = shift }
  }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_set_eq:NN \@sblfonts@load@languages \@@_load_languages:
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\sys_if_engine_opentype:TF
  {
    \RequirePackage { fontspec }
    \sys_if_engine_luatex:T
      { \RequirePackage [ bidi=basic, provide*=* ] { babel } }
    \sys_if_engine_xetex:T
      { \RequirePackage [ bidi=default, provide*=* ] { babel } }
  }
  {
    \RequirePackage [ NHE8, LGR, T1 ] { fontenc }
    \RequirePackage [ bidi=default, provide*=* ] { babel }
  }
%    \end{macrocode}
%
% \cs{babelprovide} macros need to be run outside of ExplSyntax.
%
%    \begin{macrocode}
\ExplSyntaxOff
\@sblfonts@load@languages
\ExplSyntaxOn
%    \end{macrocode}
%
% \subsection{Font Set Up}
%
% \begin{macro}{\@@_load_default_greek_font:}
%
%   Load the default \LaTeX\ Greek font.
%
%    \begin{macrocode}
\cs_new_protected:Nn \@@_load_default_greek_font:
  {
    \babelfont [ polytonicgreek ] { rm }
      [
        Extension      = .otf ,
        ItalicFont     = cmunti ,
        BoldFont       = cmunbx ,
        BoldItalicFont = cmunbi
      ]
      { cmunrm }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_load_default_hebrew_font:}
%    \begin{macrocode}
%
%   Load the default \LaTeX\ Hebrew font.
%
\cs_new_protected:Nn \@@_load_default_hebrew_font:
  {
    \babelfont [ hebrew ] { rm }
      [
        Extension      = .otf ,
        UprightFont    = *-Medium ,
        ItalicFont     = *-MediumItalic ,
        BoldFont       = *-Bold ,
        BoldItalicFont = *-BoldItalic
      ]
      { DavidCLM }
  }
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\sys_if_engine_opentype:TF
  {
%    \end{macrocode}
%
% Use main font for Greek and Hebrew if it supports it unless SBL fonts have
% been explicitly asked for. \textbf{Note:} Some fonts support only monotonic
% Greek, but there generally isn't a way to tell this without checking which
% glyphs are actually present.
%
%    \begin{macrocode}
    \tl_if_empty:NTF \l_@@_mainfont_tl
      {
%    \end{macrocode}
%
% Initially set Greek and Hebrew to CMU Serif and David CLM
%
%    \begin{macrocode}
        \@@_load_default_greek_font:
        \@@_load_default_hebrew_font:
      }
      {
        \babelfont { rm }
          [ \l_@@_mainfont_options_clist ]
          { \l_@@_mainfont_tl }
        \fontspec_if_script:nF { grek }
          {
            \str_if_eq:VnT \l_@@_sblgreek_str { fallback }
              { \@@_load_default_greek_font: }
          }
        \fontspec_if_script:nF { hebr }
          {
            \str_if_eq:VnT \l_@@_sblhebrew_str { fallback }
              { \@@_load_default_hebrew_font: }
          }
      }
%    \end{macrocode}
%
% Try to load SBL fonts if asked for.
% \begin{description}
%   \item[For Greek:] SBL BibLit, SBL Greek, Alegreya
%   \item[For Hebrew:] SBL BibLit, SBL Hebrew, Frank Ruehl CLM
% \end{description}
% \textbf{Note:} The SBL font is strongly advised for biblical Hebrew.
% Alegreya and Frank Ruehl CLM are included in TeXLive so should be present.
%
%    \begin{macrocode}
    \str_if_eq:VnT \l_@@_sblgreek_str { true }
      {
        \fontspec_font_if_exist:nTF { SBL ~ BibLit }
          {
            \babelfont [ polytonicgreek ] { rm }
              [
                ItalicFont     = SBL ~ BibLit ,
                BoldFont       = SBL ~ BibLit ,
                BoldItalicFont = SBL ~ BibLit
              ]
              { SBL ~ BibLit }
          }
          {
            \fontspec_font_if_exist:nTF { SBL ~ Greek }
              {
                \babelfont [ polytonicgreek ] { rm }
                  [
                    ItalicFont     = SBL ~ Greek ,
                    BoldFont       = SBL ~ Greek ,
                    BoldItalicFont = SBL ~ Greek
                  ]
                  { SBL ~ Greek }
              }
              {
                \babelfont [ polytonicgreek ] { rm }
                  [
                    Extension   = .otf ,
                    UprightFont = *-Regular ,
                    ItalicFont  = *-Italic ,
                    BoldFont    = *-Bold ,
                    BoldItalic  = *-BoldItalic
                  ]
                  { Alegreya }
              }
          }
      }
%    \end{macrocode}
%
%    \begin{macrocode}
    \str_if_eq:VnT \l_@@_sblhebrew_str { true }
      {
        \fontspec_font_if_exist:nTF { SBL ~ BibLit }
          {
            \babelfont [ hebrew ] { rm }
              [
                ItalicFont     = SBL ~ BibLit ,
                BoldFont       = SBL ~ BibLit ,
                BoldItalicFont = SBL ~ BibLit
              ]
              { SBL ~ BibLit }
          }
          {
            \fontspec_font_if_exist:nTF { SBL ~ Hebrew }
              {
                \babelfont [ hebrew ] { rm }
                  [
                    ItalicFont     = SBL ~ Hebrew ,
                    BoldFont       = SBL ~ Hebrew ,
                    BoldItalicFont = SBL ~ Hebrew
                  ]
                  { SBL ~ Hebrew }
              }
              {
                \babelfont [ hebrew ] { rm }
                  [
                    Extension = .ttf ,
                    UprightFont = *-Medium ,
                    ItalicFont = *-MediumOblique ,
                    BoldFont = *-Bold ,
                    BoldItalic = *-BoldOblique
                  ]
                  { FrankRuehlCLM }
              }
          }
      }
%    \end{macrocode}
%
% Override Greek and Hebrew fonts if specified in |grfont| and |hefont|
% options.
%
%    \begin{macrocode}
    \tl_if_empty:NF \l_@@_grfont_tl
      {
        \babelfont [ polytonicgreek ] { rm } { \l_@@_grfont_tl }
      }
    \tl_if_empty:NF \l_@@_hefont_tl
      {
        \babelfont [ hebrew ] { rm } { \l_@@_hefont_tl }
      }
  }
  {
%    \end{macrocode}
%
% For |pdflatex|, load font using a package.
%
%    \begin{macrocode}
    \tl_if_empty:NF \l_@@_mainfont_tl
      {
        \RequirePackage
          [ \l_@@_mainfont_options_clist ]
          { \l_@@_mainfont_tl }
      }
%    \end{macrocode}
%
% Override Greek and Hebrew fonts if specified in |grfont| and |hefont|
% options.
%
%    \begin{macrocode}
    \tl_if_empty:NF \l_@@_grfont_tl
      {
        \DeclareFontFamilySubstitution
          { LGR }
          { \rmdefault }
          { \l_@@_grfont_tl }
      }
    \tl_if_empty:NF \l_@@_hefont_tl
      {
        \DeclareFontFamilySubstitution
          { NHE8 }
          { \rmdefault }
          { \l_@@_hefont_tl }
      }
  }
%    \end{macrocode}
%
%    \begin{macrocode}
\endinput
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
