%%
%% jlreq-deluxe.sty
%%
%% Copyright (c) 2019-2026 Yukimasa Morimi (h20y6m)
%%   GitHub:   https://github.com/h20y6m
%%
%% This package is distributed under the MIT License.
%%

% --------------------------------------------------

\NeedsTeXFormat{pLaTeX2e}
\RequirePackage{expl3}[2019-05-03]
\ProvidesExplPackage {jlreq-deluxe} {2026-03-14} {0.6.0}
  {Multi-weight Japanese font for jlreq class}

% --------------------------------------------------
% load packeges

\RequirePackage{l3keys2e}

% --------------------------------------------------
% Variables

\tl_new:N \l__jldx_prefix_tl
\tl_new:N \l__jldx_scale_tl
\bool_new:N \l__jldx_deluxe_bool
\bool_new:N \l__jldx_jismmiv_bool
\bool_new:N \l__jldx_platex_bool
\bool_new:N \l__jldx_uplatex_bool
\bool_new:N \l__jldx_hanging_punctuation_bool
\bool_new:N \l__jldx_zenkakunibu_nibu_bool

% --------------------------------------------------
% Messages

\msg_new:nnn { jlreq-deluxe } { option-not-available }
  { The~`#1'~option~is~not~available. }

\msg_new:nnn { jlreq-deluxe } { multi-engine-options }
  { Multiple~engine~options~were~specified. }

% --------------------------------------------------
% Helpers

\prg_new_conditional:Npnn \__jldx_if_class_loaded:n #1 { p , T , F , TF }
  {
    \@ifclassloaded {#1}
      { \prg_return_true: }
      { \prg_return_false: }
  }

% --------------------------------------------------
%% Options

% options for japanese-otf package
\keys_define:nn { jlreq-deluxe }
  {
    deluxe    .bool_set:N         = \l__jldx_deluxe_bool,
    jis2004   .bool_set:N         = \l__jldx_jismmiv_bool,
    nojis2004 .bool_set_inverse:N = \l__jldx_jismmiv_bool,
    platex    .bool_set:N         = \l__jldx_platex_bool,
    uplatex   .bool_set:N         = \l__jldx_uplatex_bool,
    scale     .tl_set:N           = \l__jldx_scale_tl,
    scale     .value_required:n   = true,
  }
% forbidden options for japanese-otf package
\keys_define:nn { jlreq-deluxe }
  {
    burasage  .code:n =
      {
        \msg_error:nnn { jlreq-deluxe } { option-not-available }
          { burasage }
      },
  }

% options for this package
\keys_define:nn { jlreq-deluxe }
  {
    hanging_punctuation .bool_set:N = \l__jldx_hanging_punctuation_bool,
    zenkakunibu_nibu    .bool_set:N = \l__jldx_zenkakunibu_nibu_bool,
  }

% unknown option
\keys_define:nn { jlreq-deluxe }
  {
    unknown .code:n =
      {
        \PassOptionsToPackage { \CurrentOption } { pxjodel }
      }
  }

% set default options
\keys_set:nn { jlreq-deluxe }
  {
    deluxe  = true,
    jis2004 = true,
  }

% process options
\ProcessKeysOptions { jlreq-deluxe }

% both platex and uplatex
\bool_lazy_and:nnT { \l__jldx_platex_bool } { \l__jldx_uplatex_bool }
  {
    \msg_error:nn { jlreq-deluxe } { multi-engine-options }
  }

% --------------------------------------------------
%% jlreq parameters

\__jldx_if_class_loaded:nT { jlreq }
  {
    % uplatex
    \bool_lazy_and:nnTF
      { \token_if_primitive_p:N \ucs }
      { \int_compare_p:n { \ucs"FA4A = "FA4A } }
      {
        \bool_set_false:N \l__jldx_platex_bool
        \bool_set_true:N  \l__jldx_uplatex_bool
      }
      {
        \bool_set_true:N  \l__jldx_platex_bool
        \bool_set_false:N \l__jldx_uplatex_bool
      }
    % scale
    \jlreq@divide { \jlreq@jafontsize } { \jlreq@fontsize }
    \tl_set:Nx \l__jldx_scale_tl
      {
        \strip@pt \jlreq@resultdimen
      }
    % hanging_punctuation
    \bool_set:Nn \l__jldx_zenkakunibu_nibu_bool
      {
        \str_if_eq_p:no { zenkakunibu_nibu } { \jlreq@open@bracket@pos }
      }
    % zenkakunibu_nibu
    \bool_set:Nn \l__jldx_hanging_punctuation_bool
      {
        \legacy_if_p:n { jlreq@burasage }
      }
  }

% --------------------------------------------------
%% prefix

\tl_set:Nn \l__jldx_prefix_tl { jlreq }

% zenkakunibu_nibu
\bool_if:NT \l__jldx_zenkakunibu_nibu_bool
  {
    \tl_put_left:Nn \l__jldx_prefix_tl { z }
  }
% hanging_punctuation
\bool_if:NT \l__jldx_hanging_punctuation_bool
  {
    \tl_put_left:Nn \l__jldx_prefix_tl { b }
  }

% --------------------------------------------------
% pxjodel

% prefix
\PassOptionsToPackage { prefix = \l__jldx_prefix_tl } { pxjodel }

% deluxe
\bool_if:NT \l__jldx_deluxe_bool
  {
    \PassOptionsToPackage { deluxe } { pxjodel }
  }

% jis2004
\bool_if:NTF \l__jldx_jismmiv_bool
  {
    \PassOptionsToPackage { jis2004 } { pxjodel }
    \PassOptionsToPackage { jis2004 } { pxchfon }
  }
  {
    \PassOptionsToPackage { nojis2004 } { pxchfon }
  }

% platex
\bool_if:NT \l__jldx_platex_bool
  {
    \PassOptionsToPackage { platex } { pxjodel }
  }
% uplatex
\bool_if:NT \l__jldx_uplatex_bool
  {
    \PassOptionsToPackage { uplatex } { pxjodel }
  }

% scale
\tl_if_empty:NF \l__jldx_scale_tl
  {
    \PassOptionsToPackage { scale = \l__jldx_scale_tl } { pxjodel }
  }

% load pxjodel package
\RequirePackage{pxjodel}
