% luatexja-preset.sty
\ProvidesPackage{luatexja-preset}[2020-06-14 Japanese font presets]

\RequirePackage{expl3,l3keys2e}
\RequirePackage{luatexja}
\ExplSyntaxOn

%% messages
\msg_new:nnn { luatexja-preset }{ ltj-fontspec }{
  The~ luatexja-fontspec~ package~ is~ loaded~ already.~
  Hence~ we~ ignore~ `nfssonly'~option.
}
\msg_new:nnn { luatexja-preset }{ no-font }{
  Font~ #1~ (#2)~ is~ not~found.~ Hence~ I~ substitute~ #3~ (#4)~ for~ it.
}
\msg_new:nnn { luatexja-preset }{ scale }{
  Japanese~fonts~will~be~scaled~by~#1.
}

\msg_new:nnn { luatexja-preset }{ exist-preset-nooverwrite }{
  The~ preset~ `#1'~ is~ already~ defined.~
  To~ redefine~ this~ preset,~ use~ \str_use:N \c_backslash_str ltjnewpreset*~ instead.
}
\msg_new:nnn { luatexja-preset }{ exist-preset-overwrite }{
  The~ preset~ `#1'~ is~ redefined.
}

\msg_new:nnn { luatexja-preset }{ illegal-preset-name }{
  The~ preset~ `#1'~ cannot~ be~ defined,~
  since~ the~ luatexja-preset~ package~ an~ option~ with~ the~ same~ name.
}

\seq_new:N \g__ltjpreset_presets_seq

\keys_define:nn { luatexja-preset }{
  unknown .code:n =
    \keys_set_known:noN { ltjpreset-presets } { \tl_use:N \l_keys_tl } \l_tmpa_tl
    \tl_if_empty:NT \l_tmpa_tl {
      \PassOptionsToPackage { \l_tmpa_tl } { luatexja-fontspec }
    },
  scale .code:n  = \gdef\Cjascale{#1},
  match .code: n =
    \renewcommand\jttdefault{\gtdefault}\@ltj@match@familytrue
    \PassOptionsToPackage { match } { luatexja-fontspec },
  match .value_forbidden:n = true,
  __custom  .bool_set:N = \g__ltjpreset_custom_bool,  __custom  .groups:n = { preset },
  __office  .bool_set:N = \g__ltjpreset_office_bool,  __office  .groups:n = { preset },
  __noembed .bool_set:N = \g__ltjpreset_noembed_bool, __noembed .groups:n = { preset },
}
\cs_new:Nn \__ltjpreset_declare_option:nn {
  \keys_define:nn { luatexja-preset }{
    #1 .code:n = { #2 },
    #1 .value_forbidden:n = true
  }
}
\cs_new:Nn \ltjpreset_declare_preset:nn {
  \keys_define:nn { luatexja-preset }{
    #1 .meta:n = { #2 },
    #1 .value_forbidden:n = true, #1 .groups:n = { preset },
  }
  \seq_put_right:Nn \g__ltjpreset_presets_seq { #1 }
}
\cs_generate_variant:Nn \ltjpreset_declare_preset:nn {nx}
\cs_new:Nn \__ltjpreset_declare_bool_option:nn {
  \bool_new:c { g__ltjpreset_#1_bool }
  \bool_set_false:c { g__ltjpreset_#1_bool }
  \keys_define:nn { luatexja-preset }{
    #1 .bool_set:c =  { g__ltjpreset_#1_bool },
    #2 .bool_set_inverse:c =  { g__ltjpreset_#1_bool },
    #1 .default:n =  true, #2 .default:n =  true,
  }
}

%%%%%%%% bool options
\__ltjpreset_declare_bool_option:nn { nfssonly }{ fontspec }
\__ltjpreset_declare_bool_option:nn { bold }{ nobold }
\__ltjpreset_declare_bool_option:nn { expert }{ noexpert }
\__ltjpreset_declare_bool_option:nn { deluxe }{ nodeluxe }
\__ltjpreset_declare_bool_option:nn { nocheck }{ check }

% JFM
\tl_new:N \__ltjpreset_use_jfm_yoko_tl % 使用する JFM（横）
\tl_set:Nn \__ltjpreset_use_jfm_yoko_tl {\ltj@stdyokojfm}
\tl_new:N \__ltjpreset_use_jfm_tate_tl % 使用する JFM（縦）
\tl_set:Nn \__ltjpreset_use_jfm_tate_tl {\ltj@stdtatejfm}
\__ltjpreset_declare_option:nn{jis}{\tl_set:Nn  \__ltjpreset_use_jfm_yoko_tl {jis}}     % jis
\__ltjpreset_declare_option:nn{ujis}{\tl_set:Nn  \__ltjpreset_use_jfm_yoko_tl {ujis}}   % ujis

% 漢字字形
\tl_new:N \l__ltjpreset_kanji_shape_tl
\__ltjpreset_declare_option:nn{90jis}{  \tl_set:Nn \l__ltjpreset_kanji_shape_tl { , CJKShape=JIS1990 }} % 90jis
\__ltjpreset_declare_option:nn{jis90}{  \tl_set:Nn \l__ltjpreset_kanji_shape_tl { , CJKShape=JIS1990 }} % 90jis
\__ltjpreset_declare_option:nn{jis2004}{\tl_set:Nn \l__ltjpreset_kanji_shape_tl { , CJKShape=JIS2004 }} % jis2004
\__ltjpreset_declare_option:nn{2004jis}{\tl_set:Nn \l__ltjpreset_kanji_shape_tl { , CJKShape=JIS2004 }} % jis2004

% font preset
\prop_new:N \g__ltjpreset_font_prop
\keys_define:nn { luatexja-preset } {
  mc .code:n =
     \prop_put:Nnn \g__ltjpreset_font_prop { mc-l } {#1}
     \prop_put:Nnn \g__ltjpreset_font_prop { mc-m } {#1}
     \prop_put:Nnn \g__ltjpreset_font_prop { mc-b } {#1},
  mc .groups:n = { preset }, mc .value_required:n = true,
  mc-bx .code:n =
     \prop_put:Nnn \g__ltjpreset_font_prop { mc-b } {#1},
  gt .code:n =
     \prop_put:Nnn \g__ltjpreset_font_prop { gt-u } {#1}
     \prop_put:Nnn \g__ltjpreset_font_prop { gt-d } {#1}
     \prop_put:Nnn \g__ltjpreset_font_prop { gt-b } {#1}
     \prop_put:Nnn \g__ltjpreset_font_prop { gt-eb } {#1},
  gt-bx .code:n =
     \prop_put:Nnn \g__ltjpreset_font_prop { gt-b } {#1},
  gt-m .code:n = % gtfamily , medium series
     \prop_put:Nnn \g__ltjpreset_font_prop { gt-u } {#1}
     \prop_put:Nnn \g__ltjpreset_font_prop { gt-d } {#1},
  % gt-d, gt-u は隠し
  gt .groups:n = { preset },   gt .value_required:n = true,
  gt-m .groups:n = { preset }, gt-m .value_required:n = true,
}
\cs_set:Nn \ltjpreset_define_preset_prop:n {
  \keys_define:nn { luatexja-preset } {
    #1 .code:n = \prop_put:Non \g__ltjpreset_font_prop { #1 } { ##1 },
    #1 .groups:n = { preset }, #1 .value_required:n = true,
  }
}
\ltjpreset_define_preset_prop:n { mc-l }
\ltjpreset_define_preset_prop:n { mc-m }
\ltjpreset_define_preset_prop:n { mc-b }
\ltjpreset_define_preset_prop:n { gt-u }
\ltjpreset_define_preset_prop:n { gt-d }
\ltjpreset_define_preset_prop:n { gt-b }
\ltjpreset_define_preset_prop:n { gt-eb}
\ltjpreset_define_preset_prop:n { mg-m }

\cs_new:Nn \__ltjpreset_set_uni:nn {
  mc = #1, gt = #2, mc-b = #2, mg-m = #2,
  __custom = false, __office = false, __noembed = false,
}
\cs_new:Npn \__ltjpreset_set_office:nn #1 #2{
  mc   = #1,         mc-b = HGMinchoE ,
  gt-d = HGGothicM , gt-b = HGGothicE ,
  gt-u = #2 ,        gt-eb = HGSoeiKakugothicUB ,
  mg-m = HGMaruGothicMPRO,
  __custom = false, __office = true, __noembed = false,
}

%%%%%%%% プリセット達

% カスタム指定
 \keys_define:nn { luatexja-preset }{
    custom .meta:n = { #1, __custom = true, }, 
    custom .value_required:n = true,
    jfm_yoko .tl_set:N = \__ltjpreset_use_jfm_yoko_tl,
    jfm_tate .tl_set:N = \__ltjpreset_use_jfm_tate_tl,
}

% 小塚 (Adobe)
%% Pro
\ltjpreset_declare_preset:nx{kozuka-pro}{
    mc-m =  KozMinPro-Regular, mc-b = KozMinPro-Bold,
    gt-d =  KozGoPro-Regular,  gt-b = KozGoPro-Bold,
    gt-u =  KozGoPro-Medium,   gt-eb = KozGoPro-Heavy,
    mg-m =  KozGoPro-Heavy,    mc-l =  KozMinPro-Light,
    __custom = false, __office = false, __noembed = false,
}
%% Pr6
\ltjpreset_declare_preset:nx{kozuka-pr6}{
    mc-m =  KozMinProVI-Regular, mc-b = KozMinProVI-Bold,
    gt-d =  KozGoProVI-Regular,  gt-b = KozGoProVI-Bold,
    gt-u =  KozGoProVI-Medium,   gt-eb = KozGoProVI-Heavy,
    mg-m =  KozGoProVI-Heavy,    mc-l =  KozMinProVI-Light,
    __custom = false, __office = false, __noembed = false,
}
%% Pr6N
\ltjpreset_declare_preset:nx{kozuka-pr6n}{
    mc-m =  KozMinPr6N-Regular, mc-b = KozMinPr6N-Bold,
    gt-d =  KozGoPr6N-Regular,  gt-b = KozGoPr6N-Bold,
    gt-u =  KozGoPr6N-Medium,   gt-eb = KozGoPr6N-Heavy,
    mg-m =  KozGoPr6N-Heavy,    mc-l =  KozMinPr6N-Light,
    __custom = false, __office = false, __noembed = false,
}

% ヒラギノ (OSX)
\ltjpreset_declare_preset:nx{hiragino-pro}{
    mc-m = HiraMinPro-W3,  mc-b = HiraMinPro-W6,
    gt-d = HiraKakuPro-W3, gt-b = HiraKakuPro-W6,
    gt-u = HiraKakuPro-W6, gt-eb = HiraKakuStd-W8,
    mg-m = HiraMaruPro-W4, mc-l =  HiraMinPro-W2,
    __custom = false, __office = false, __noembed = false,
}
\ltjpreset_declare_preset:nx{hiragino-pron}{
    mc-m = HiraMinProN-W3,  mc-b = HiraMinProN-W6,
    gt-d = HiraKakuProN-W3, gt-b = HiraKakuProN-W6,
    gt-u = HiraKakuProN-W6, gt-eb = HiraKakuStdN-W8,
    mg-m = HiraMaruProN-W4, mc-l =  HiraMinProN-W2,
    __custom = false, __office = false, __noembed = false,
}

% モリサワ
\ltjpreset_declare_preset:nx{bizud}{
    mc =   BIZ-UDMinchoM.ttc,
    gt-d = BIZ-UDGothicR.ttc, gt-b = BIZ-UDGothicB.ttc,
    gt-u = BIZ-UDGothicR.ttc, gt-eb = BIZ-UDGothicB.ttc,
    mg-m = BIZ-UDGothicB.ttc,
    __custom = false, __office = false, __noembed = false,
}
\ltjpreset_declare_preset:nx{morisawa-pro}{
    mc =   A-OTF-RyuminPro-Light.otf,      mc-b = A-OTF-FutoMinA101Pro-Bold.otf,
    gt-d = A-OTF-GothicBBBPro-Medium.otf , gt-b = A-OTF-FutoGoB101Pro-Bold.otf,
    gt-u = A-OTF-GothicBBBPro-Medium.otf , gt-eb = A-OTF-MidashiGoPro-MB31.otf,
    mg-m = A-OTF-Jun101Pro-Light.otf,
    __custom = false, __office = false, __noembed = false,
}
\ltjpreset_declare_preset:nx{morisawa-pr6n}{
    mc =   A-OTF-RyuminPr6N-Light.otf,      mc-b = A-OTF-FutoMinA101Pr6N-Bold.otf,
    gt-d = A-OTF-GothicBBBPr6N-Medium.otf , gt-b = A-OTF-FutoGoB101Pr6N-Bold.otf,
    gt-u = A-OTF-GothicBBBPr6N-Medium.otf , gt-eb = A-OTF-MidashiGoPr6N-MB31.otf,
    mg-m = A-OTF-Jun101Pr6N-Light.otf,
    __custom = false, __office = false, __noembed = false,
}

% 游明朝/游ゴシック (Win8.1)
\ltjpreset_declare_preset:nx{yu-win}{
    mc-m = YuMincho-Regular, mc-b = YuMincho-Demibold,
    gt-d = YuGothic-Regular, gt-b = YuGothic-Bold,
    gt-u = YuGothic-Regular, gt-eb = YuGothic-Bold,
    mg-m = YuGothic-Bold,    mc-l =  YuMincho-Light,
    __custom = false, __office = false, __noembed = false,
}
% Win10
\ltjpreset_declare_preset:nx{yu-win10}{
    mc-m = YuMincho-Regular, mc-b = YuMincho-Demibold,
    gt-d = YuGothic-Regular, gt-b = YuGothic-Bold,
    gt-u = YuGothic-Medium,  gt-eb = YuGothic-Bold,
    mg-m = YuGothic-Bold,    mc-l =  YuMincho-Light,
    __custom = false, __office = false, __noembed = false,
}

% Yu fonts in OSX
% according to http://support.apple.com/kb/HT5944
\ltjpreset_declare_preset:nx{yu-osx}{
    mc =  YuMincho~Medium,   mc-b = YuMincho~Demibold,
    gt-d =  YuGothic~Medium, gt-u =  YuGothic~Medium,
    gt-b = YuGothic~Bold,    gt-eb = YuGothic~Bold,
    mg-m =  YuGothic~Bold,
    __custom = false, __office = false, __noembed = false,
}

% moga-mobo
\ltjpreset_declare_preset:nx{moga-mobo}{
    mc =   Moga90Mincho, mc-b = Moga90Mincho~Bold,
    gt-d = Moga90Gothic, gt-b = Moga90Gothic~Bold,
    gt-u = Moga90Gothic, gt-eb = Moga90Gothic~Bold,
    mg-m = Mobo90Gothic,
    __custom = false, __office = false, __noembed = false,
}
\ltjpreset_declare_preset:nx{moga-mobo-ex}{
    mc =   MogaEx90Mincho, mc-b = MogaEx90Mincho~Bold,
    gt-d = MogaEx90Gothic, gt-b = MogaEx90Gothic~Bold,
    gt-u = MogaEx90Gothic, gt-eb = MogaEx90Gothic~Bold,
    mg-m = MoboEx90Gothic,
    __custom = false, __office = false, __noembed = false,
}

% Ume fones
\ltjpreset_declare_preset:nx{ume}{
    mc = Ume~Mincho,
    gt-d = Ume~Gothic,    gt-b = Ume~Gothic~O5,
    gt-u = Ume~Gothic~O5, gt-eb = Ume~Gothic~O5,
    mg-m = Ume~Gothic~O4,
    __custom = false, __office = false, __noembed = false,
}

% Source Han {Serif,Sans}
\ltjpreset_declare_preset:nx{sourcehan}{
    mc-m =  Source~Han~Serif~Regular,
    mc-b =  Source~Han~Serif~Bold,
    gt-d =  Source~Han~Sans~Regular,
    gt-b =  Source~Han~Sans~Bold,
    gt-u =  Source~Han~Sans~Medium,
    gt-eb = Source~Han~Sans~Heavy,
    mg-m =  Source~Han~Sans~Medium,
    mc-l =  Source~Han~Serif~Light,
    __custom = false, __office = false, __noembed = false,
}
\ltjpreset_declare_preset:nx{sourcehan-jp}{
    mc-m =  Source~Han~Serif~JP~Regular,
    mc-b =  Source~Han~Serif~JP~Bold,
    gt-d =  Source~Han~Sans~JP~Regular,
    gt-b =  Source~Han~Sans~JP~Bold,
    gt-u =  Source~Han~Sans~JP~Medium,
    gt-eb = Source~Han~Sans~JP~Heavy,
    mg-m =  Source~Han~Sans~JP~Medium,
    mc-l =  Source~Han~Serif~JP~Light,
    __custom = false, __office = false, __noembed = false,
}
\ltjpreset_declare_preset:nx{noto-otc}{
    mc-m =  Noto~Serif~CJK~Regular,
    mc-b =  Noto~Serif~CJK~Bold,
    gt-d =  Noto~Sans~CJK~Regular,
    gt-b =  Noto~Sans~CJK~Bold,
    gt-u =  Noto~Sans~CJK~Medium,
    gt-eb = Noto~Sans~CJK~Black,
    mg-m =  Noto~Sans~CJK~Medium,
    mc-l =  Noto~Serif~CJK~Light,
    __custom = false, __office = false, __noembed = false,
}
\ltjpreset_declare_preset:nx{noto-otf}{
    mc-m =  Noto~Serif~CJK~JP~Regular,
    mc-b =  Noto~Serif~CJK~JP~Bold,
    gt-d =  Noto~Sans~CJK~JP~Regular,
    gt-b =  Noto~Sans~CJK~JP~Bold,
    gt-u =  Noto~Sans~CJK~JP~Medium,
    gt-eb = Noto~Sans~CJK~JP~Black,
    mg-m =  Noto~Sans~CJK~JP~Medium,
    mc-l =  Noto~Serif~CJK~JP~Light,
    __custom = false, __office = false, __noembed = false,
}

% HaranoAji {Mincho, Gothic}
\ltjpreset_declare_preset:nx{haranoaji}{
    mc-m =  HaranoAjiMincho-Regular.otf,
    mc-b =  HaranoAjiMincho-Bold.otf,
    gt-d =  HaranoAjiGothic-Regular.otf,
    gt-b =  HaranoAjiGothic-Bold.otf,
    gt-u =  HaranoAjiGothic-Medium.otf,
    gt-eb = HaranoAjiGothic-Heavy.otf,
    mg-m =  HaranoAjiGothic-Medium.otf,
    mc-l =  HaranoAjiMincho-Light.otf,
    __custom = false, __office = false, __noembed = false,
}


% ipa, ipaex, ms
\ltjpreset_declare_preset:nx{ipa}{
  \__ltjpreset_set_uni:nn { IPAMincho } { IPAGothic }
}
\ltjpreset_declare_preset:nx{ipaex}{
  \__ltjpreset_set_uni:nn { IPAExMincho } { IPAExGothic }
}
\ltjpreset_declare_preset:nx{ms}{
  \__ltjpreset_set_uni:nn { MS-Mincho } { MS-Gothic }
}

% {ipa,ipaex,ms}-dx % Office 付属フォントを利用
\ltjpreset_declare_preset:nx{ipaex-hg}{
  \__ltjpreset_set_office:nn { IPAExMincho } { IPAExGothic }
}
\ltjpreset_declare_preset:nx{ipa-hg}{
  \__ltjpreset_set_office:nn { IPAMincho } { IPAGothic }
}
\ltjpreset_declare_preset:nx{ms-hg}{
  \__ltjpreset_set_office:nn { MS-Mincho } { MS-Gothic }
}


% 非埋込
\ltjpreset_declare_preset:nx{noembed}{
  \__ltjpreset_set_uni:nn { Ryumin-Light } { GothicBBB-Medium },
  __custom = false, __office = false, __noembed = true,
}

\keys_set:nn {luatexja-preset} { haranoaji } %% old: ipaex
\ProcessKeysOptions { luatexja-preset }
\@ifpackageloaded{luatexja-fontspec}{
  \bool_if:NT \g__ltjpreset_nfssonly_bool
    { \msg_warning:nn { luatexja-preset }{ ltj-fontspec } }
  \bool_set_false:N \g__ltjpreset_nfssonly_bool
}{}

%%%%%%%% main routine

\cs_new:Nn \__ltjpreset_extract_fonts: {
  %% MogaMincho の場合は 90 をつける/はずす
  \bool_if:NF \g__ltjpreset_custom_bool {
    \tl_set:Nn \l_tmpa_tl {, CJKShape=JIS2004 }
    \tl_if_eq:NNT \l__ltjpreset_kanji_shape_tl \l_tmpa_tl {
      \tl_set:Nx \l_tmpb_tl { \prop_item:Nn \g__ltjpreset_font_prop { mc-m } }
      \tl_if_in:NnT  \l_tmpb_tl { Moga90Mincho } {
        \prop_clear:N \l_tmpa_prop
        \prop_map_inline:Nn \g__ltjpreset_font_prop {
           \tl_set:Nn \l_tmpb_tl { ##2 } \tl_remove_all:Nn \l_tmpb_tl {90}
          \prop_put:Nnx \l_tmpa_prop { ##1 } { \tl_use:N \l_tmpb_tl }
         } 
         \prop_set_eq:NN \g__ltjpreset_font_prop \l_tmpa_prop
       }
       \tl_if_in:NnT  \l_tmpb_tl { MogaEx90Mincho } {
         \prop_clear:N \l_tmpa_prop
         \prop_map_inline:Nn \g__ltjpreset_font_prop {
           \tl_set:Nn \l_tmpb_tl { ##2 } \tl_remove_all:Nn \l_tmpb_tl {90}
           \prop_put:Nnx \l_tmpa_prop { ##1 } { \tl_use:N \l_tmpb_tl }
         }
         \prop_set_eq:NN \g__ltjpreset_font_prop \l_tmpa_prop
       }
    }
  }
  %%%%  リストからの展開
  \cs_set:Nx \__ltjpreset_font_mc_l:  { \prop_item:Nn \g__ltjpreset_font_prop { mc-l } }
  \cs_set:Nx \__ltjpreset_font_mc_m:  { \prop_item:Nn \g__ltjpreset_font_prop { mc-m } }
  \cs_set:Nx \__ltjpreset_font_mc_b:  { \prop_item:Nn \g__ltjpreset_font_prop { mc-b } }
  \cs_set:Nx \__ltjpreset_font_gt_m:  { \prop_item:Nn \g__ltjpreset_font_prop { gt-d } }
  \cs_set:Nx \__ltjpreset_font_gt_u:  { \prop_item:Nn \g__ltjpreset_font_prop { gt-u } }
  \cs_set:Nx \__ltjpreset_font_gt_b:  { \prop_item:Nn \g__ltjpreset_font_prop { gt-b } }
  \cs_set:Nx \__ltjpreset_font_gt_eb: { \prop_item:Nn \g__ltjpreset_font_prop { gt-eb } }
  \cs_set:Nx \__ltjpreset_font_mg_m:  { \prop_item:Nn \g__ltjpreset_font_prop { mg-m } }
  %% HG 系フォント + JIS2004/90JIS のときはフォント名を置換
  \bool_if:nT { !\g__ltjpreset_custom_bool && \g__ltjpreset_office_bool } {
    \tl_set:Nn \l_tmpa_tl {, CJKShape=JIS2004 }
    \tl_if_eq:NNT \l__ltjpreset_kanji_shape_tl \l_tmpa_tl {
      \cs_set:Nx \__ltjpreset_font_mc_b:  { hgrme04.ttc }
      \cs_set:Nx \__ltjpreset_font_gt_b:  { hgrge04.ttc }
      \cs_set:Nx \__ltjpreset_font_gt_eb: { hgrsgu04.ttc }
      \cs_set:Nx \__ltjpreset_font_mg_m:  { hgrsmp04.ttf }
      \cs_set_eq:NN \__ltjpreset_font_gt_m: \__ltjpreset_font_gt_u:
      % HG ゴシックM のときはそうはいかないので 1 ウェイト時のゴシック体を使う
    }
    \tl_set:Nn \l_tmpa_tl {, CJKShape=JIS1990 }
    \tl_if_eq:NNT \l__ltjpreset_kanji_shape_tl \l_tmpa_tl {
      \cs_set:Nx \__ltjpreset_font_mc_b:  { hgrme.ttc }
      \cs_set:Nx \__ltjpreset_font_gt_b:  { hgrge.ttc }
      \cs_set:Nx \__ltjpreset_font_gt_eb: { hgrsgu.ttc }
      \cs_set:Nx \__ltjpreset_font_mg_m:  { hgrsmp.ttf }
    }
  }
  % mc/l
  \tl_set:Nx \l_tmpa_tl { \__ltjpreset_font_mc_l: }
  \tl_if_empty:NT \l_tmpa_tl {\tl_set:Nn \l_tmpa_tl {~}}
  \cs_set:Nx \__ltjpreset_font_mc_l: {
    \tl_if_blank:nTF \l_tmpa_tl { \__ltjpreset_font_mc_m: } {\tl_use:N \l_tmpa_tl }
  }
  %% bold オプションの処理
  \bool_if:NTF \g__ltjpreset_deluxe_bool {
    \bool_if:NT \g__ltjpreset_bold_bool {
      \cs_set_eq:NN \__ltjpreset_font_mc_b: \__ltjpreset_font_gt_b:
    }
  }{
    \bool_if:NTF \g__ltjpreset_bold_bool
      { \cs_set_eq:NN \__ltjpreset_font_gt_m: \__ltjpreset_font_gt_b: }
      { \cs_set_eq:NN \__ltjpreset_font_gt_m: \__ltjpreset_font_gt_u: }
    \cs_set_eq:NN \__ltjpreset_font_gt_b: \__ltjpreset_font_gt_m:
    \cs_set_eq:NN \__ltjpreset_font_mc_b: \__ltjpreset_font_gt_m:
  }
}

%%%%%%%% フォント名を \l__ltjpreset_font_name: に格納
\clist_new:N \l__ltjpreset_extensions_clist
\clist_set:Nn \l__ltjpreset_extensions_clist {.otf,.ttf,.OTF,.TTF,.ttc,.TTC}
\prg_new_conditional:Nnn \__ltjpreset_if_detect_external:n {TF} {
  \clist_map_inline:Nn \l__ltjpreset_extensions_clist {
    \bool_set_false:N \l_tmpa_bool
    \tl_if_in:nnT {#1 <= end_of_string} {##1 <= end_of_string}
      { \bool_set_true:N \l_tmpa_bool \clist_map_break: }
  }
  \bool_if:NTF \l_tmpa_bool \prg_return_true: \prg_return_false:
}
\cs_new:Nn \__ltjpreset_wrap_fontname:n {
  \__ltjpreset_if_detect_external:nTF { #1 }
    { \cs_set:Nn \l__ltjpreset_font_name: { [#1] } }
    { \cs_set:Nn \l__ltjpreset_font_name: { "#1" } }
}
\cs_generate_variant:Nn \__ltjpreset_wrap_fontname:n { e }

%%%%%%%% 存在判定
\cs_new:Nn \__ltjpreset_substitute:nnnn {
  \group_begin:
    \suppressfontnotfounderror=1
    \cs_if_exist:cTF { __ltjpreset_font_#1_#2: } {
      \bool_if:NF \g__ltjpreset_nocheck_bool {
        \__ltjpreset_wrap_fontname:e { \cs:w __ltjpreset_font_#1_#2: \cs_end: }
        \font \g__ltjpreset_font_test: = \l__ltjpreset_font_name:\relax
        \ifx \g__ltjpreset_font_test: \nullfont
          \msg_warning:nnxxxx { luatexja-preset }{ no-font }
            { \cs:w __ltjpreset_font_#1_#2: \cs_end: }{ #1/#2 }
            { \cs:w __ltjpreset_font_#3_#4: \cs_end: }{ #3/#4 }
          \cs_gset_eq:cc { __ltjpreset_font_#1_#2: } { __ltjpreset_font_#3_#4: }
        \fi
      }
    } { \cs_gset_eq:cc { __ltjpreset_font_#1_#2: } { __ltjpreset_font_#3_#4: } }
  \group_end:
}

%%%%%%%% deluxe (\mgfamily はあとで定義）
\bool_if:NTF \g__ltjpreset_deluxe_bool {
  \cs_new:Nn \__ltjpreset_check_deluxe_existence: {
    \__ltjpreset_substitute:nnnn { gt } { eb } { gt } { b }
    \__ltjpreset_substitute:nnnn { mg } { m  } { gt } { b }
    \__ltjpreset_substitute:nnnn { mc } { l  } { mc } { m }
  }
  \def\ltdefault{l}
  \DeclareRobustCommand\ltseries { \not@math@alphabet\ltseries\relax\fontseries\ltdefault\selectfont }
  \def\ebdefault{eb}
  \DeclareRobustCommand\ebseries { \not@math@alphabet\ebseries\relax\fontseries\ebdefault\selectfont }
  \DeclareRobustCommand\gtebfamily { \gtfamily\fontseries{\ebdefault}\selectfont }
  \DeclareTextFontCommand{\textlt}{\ltseries}
  \DeclareTextFontCommand{\texteb}{\ebseries}
} {
  \cs_set_eq:NN \__ltjpreset_check_deluxe_existence: \prg_do_nothing:
}

\int_new:N \g__ltjpreset_applycount_int

\bool_if:NTF \g__ltjpreset_nfssonly_bool {
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% nfss only
  \cs_if_exist:NF \Cjascale {
    \group_begin:
    \fontsize{16}{16}\selectfont
    \cs_gset:Npx \Cjascale { \fp_to_decimal:n {\zw / 16} }
    \group_end:
  }

  \msg_warning:nnx { luatexja-preset }{ scale }{ \Cjascale }

  % {1: encoding}{2: family}{3: series}{4: shape}{5: fontname}{6: feature}{7: scale}
  \cs_new:Nn \__ltjpreset_declare_font_shape:nnnnnnn {
    \DeclareFontShape{#1}{#2}{#3}{#4}{ <-> s*[#7]
      \bool_if:NT \g__ltjpreset_noembed_bool {psft:} #5
      ;-kern \tl_use:N \l__ltjpreset_kanji_shape_tl; #6 } {}
  }
  \cs_new:Nn \__ltjpreset_define_family:nnn {
	\__ltjpreset_wrap_fontname:e { #3 }
    \__ltjpreset_declare_font_shape:nnnnnnn { JY3 }
      { ltjp #1 \int_to_arabic:n { \g__ltjpreset_applycount_int }} { #2 } { n }
      { \l__ltjpreset_font_name: : jfm=\tl_use:N \__ltjpreset_use_jfm_yoko_tl }
      { script=hani \bool_if:NT \g__ltjpreset_expert_bool { ;+hkna } }
	  { \Cjascale }
    \__ltjpreset_declare_font_shape:nnnnnnn { JT3 }
      { ltjp #1 \int_to_arabic:n { \g__ltjpreset_applycount_int }} { #2 } { n }
      { \l__ltjpreset_font_name:  : jfm=\tl_use:N \__ltjpreset_use_jfm_tate_tl }
      { script=hani \bool_if:NT \g__ltjpreset_expert_bool { ;+vkna } }
	  { \Cjascale }
    \bool_if:NT \g__ltjpreset_expert_bool {
      \__ltjpreset_declare_font_shape:nnnnnnn { JY3 }
        { ltjp #1 \int_to_arabic:n { \g__ltjpreset_applycount_int }} { #2 } { rb }
        { \l__ltjpreset_font_name:  : jfm=\tl_use:N \__ltjpreset_use_jfm_yoko_tl } { script=kana; +ruby }
	    { \Cjascale }
      \__ltjpreset_declare_font_shape:nnnnnnn { JT3 }
        { ltjp #1 \int_to_arabic:n { \g__ltjpreset_applycount_int }} { #2 } { rb }
        { \l__ltjpreset_font_name:  : jfm=\tl_use:N \__ltjpreset_use_jfm_tate_tl } { script=kana; +ruby }
	    { \Cjascale }
    }
    \cs_if_exist:NT \ltjpreset_define_family_hook_nfssonly:nnn
      { \ltjpreset_define_family_hook_nfssonly:nnn { #1 } { #2 } { #3 } }
  }
  \bool_if:NT \g__ltjpreset_deluxe_bool {
    \let\mgfamily\undefined
    \DeclareRobustCommand\mgfamily {
      \not@math@alphabet\mgfamily\relax
      \kanjifamily{ltjpmg \int_to_arabic:n { \g__ltjpreset_applycount_int }}\selectfont
    }
    \DeclareTextFontCommand{\textmg}{\mgfamily}
  }
  \bool_if:NT \g__ltjpreset_expert_bool {
    \DeclareRobustCommand\rubyfamily { \kanjishape{rb}\selectfont }
  }
  %
  \cs_new:Nn \__ltjpreset_do_fontset: { \group_begin:
    % ======
    \xdef\mcdefault{ltjpmc \int_to_arabic:n { \g__ltjpreset_applycount_int }}
    \xdef\gtdefault{ltjpgt \int_to_arabic:n { \g__ltjpreset_applycount_int }}
    \DeclareKanjiFamily{JY3}{\mcdefault}{}
    \DeclareKanjiFamily{JT3}{\mcdefault}{}
    \DeclareKanjiFamily{JY3}{\gtdefault}{}
    \DeclareKanjiFamily{JT3}{\gtdefault}{}
    \unless\ifltj@disablejfam
      \SetSymbolFont{mincho}{normal}{JY3}{\mcdefault}{m}{n}
      \SetSymbolFont{mincho}{bold}{JY3}{\mcdefault}{b}{n}
      \DeclareMathAlphabet{\mathgt}{JY3}{\gtdefault}{m}{n}
    \fi
    \bool_if:NT \g__ltjpreset_deluxe_bool {
      \DeclareKanjiFamily{JY3}{ltjpmg \int_to_arabic:n { \g__ltjpreset_applycount_int }}{}
      \DeclareKanjiFamily{JT3}{ltjpmg \int_to_arabic:n { \g__ltjpreset_applycount_int }}{}
    }
    % ======
    \tl_set:Nn \l_tmpa_tl {, CJKShape=JIS1990 }
    \tl_if_eq:NNT \l__ltjpreset_kanji_shape_tl \l_tmpa_tl {
      \tl_set:Nn \l__ltjpreset_kanji_shape_tl { ;+jp90 }
    }
    \tl_set:Nn \l_tmpa_tl {, CJKShape=JIS2004 }
    \tl_if_eq:NNT \l__ltjpreset_kanji_shape_tl \l_tmpa_tl {
      \tl_set:Nx \l__ltjpreset_kanji_shape_tl { ;+jp04 }
    }
    \__ltjpreset_define_family:nnn {mc} {m}  {\__ltjpreset_font_mc_m:}
    \__ltjpreset_define_family:nnn {mc} {bx} {\__ltjpreset_font_mc_b:}
    \__ltjpreset_define_family:nnn {mc} {b}  {\__ltjpreset_font_mc_b:}
    \__ltjpreset_define_family:nnn {gt} {m}  {\__ltjpreset_font_gt_m:}
    \__ltjpreset_define_family:nnn {gt} {bx} {\__ltjpreset_font_gt_b:}
    \__ltjpreset_define_family:nnn {gt} {b}  {\__ltjpreset_font_gt_b:}
    \normalfont
    \bool_if:NT \g__ltjpreset_deluxe_bool {
      \__ltjpreset_define_family:nnn {mc}  {l} {\__ltjpreset_font_mc_l:}
      \__ltjpreset_define_family:nnn {gt}  {eb} {\__ltjpreset_font_gt_eb:}
      \__ltjpreset_define_family:nnn {mg} {m}  {\__ltjpreset_font_mg_m:}
    }
    \group_end:
  }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% fontspec
} {
  \RequirePackage{luatexja-fontspec}
  % scale
  \msg_warning:nnx { luatexja-preset }{ scale }{ \Cjascale }
  \tl_new:N \l__ltjpreset_add_features_tl

  \bool_if:NT \g__ltjpreset_expert_bool {
    \cs_set:Npn \rubyfamily { \addjfontfeatures {
      Style=Ruby , YokoFeatures={RawFeature=-hkna} , TateFeatures={RawFeature=-vkna}
    } }
  }
  \addjfontfeature{ Kerning=Off }
  % 実際の fontspec の呼びだし
  \cs_new:Nn \__ltjpreset_do_fontset: { \group_begin:
      \tl_gset:Nx \l__ltjpreset_add_features_tl {
        \tl_use:N \l__ltjpreset_kanji_shape_tl,
        YokoFeatures = { JFM = \tl_use:N \__ltjpreset_use_jfm_yoko_tl },
        TateFeatures = { JFM = \tl_use:N \__ltjpreset_use_jfm_tate_tl },
        Script=CJK, \bool_if:NT \g__ltjpreset_noembed_bool { , NoEmbed }
      }
    \group_end:
    \bool_if:NT \g__ltjpreset_expert_bool {
      \tl_put_left:Nn \l__ltjpreset_add_features_tl {
        , YokoFeatures = {Style = HorizontalKana}, TateFeatures = {Style = VerticalKana}
      }
    }
    \setmainjfont [
      \bool_if:NT \g__ltjpreset_deluxe_bool {
	    FontFace={l}{n}{ Font = \__ltjpreset_font_mc_l:,
          \cs_if_exist:NT \ltjpreset_define_family_hook_fontspec:nnn {
            \ltjpreset_define_family_hook_fontspec:nnn
			  { mc } { l } { \__ltjpreset_font_mc_l: }
	      }
		},
	  }
      BoldFont = \__ltjpreset_font_mc_b: \tl_use:N \l__ltjpreset_add_features_tl,
      \cs_if_exist:NT \ltjpreset_define_family_hook_fontspec:nnn {
		UprightFeatures = { \ltjpreset_define_family_hook_fontspec:nnn
		  { mc } { m } { \__ltjpreset_font_mc_m: } },
		BoldFeatures = { \ltjpreset_define_family_hook_fontspec:nnn
		  { mc } { b } { \__ltjpreset_font_mc_b: } },
	  }
    ] { \__ltjpreset_font_mc_m: }
    \setsansjfont [
      \bool_if:NT \g__ltjpreset_deluxe_bool {
		FontFace={eb}{n}{ Font = \__ltjpreset_font_gt_eb:,
          \cs_if_exist:NT \ltjpreset_define_family_hook_fontspec:nnn {
            \ltjpreset_define_family_hook_fontspec:nnn
			  { gt } { eb } { \__ltjpreset_font_gt_eb: }
	      }
	    },
	  }
      BoldFont = \__ltjpreset_font_gt_b: \tl_use:N \l__ltjpreset_add_features_tl,
      \cs_if_exist:NT \ltjpreset_define_family_hook_fontspec:nnn {
		UprightFeatures = { \ltjpreset_define_family_hook_fontspec:nnn
		  { gt } { m } { \__ltjpreset_font_gt_m: } },
		BoldFeatures = { \ltjpreset_define_family_hook_fontspec:nnn
		  { gt } { b } { \__ltjpreset_font_gt_b: } },
	  }
    ] { \__ltjpreset_font_gt_m: }
    \bool_if:NT \g__ltjpreset_deluxe_bool {
      %% mg
      \bool_if_exist:NTF \setfontfamily {
        \setjfontfamily \mgfamily [
          BoldFont = \__ltjpreset_font_mg_m: \tl_use:N \l__ltjpreset_add_features_tl,
          \cs_if_exist:NT \ltjpreset_define_family_hook_fontspec:nnn {
		    \ltjpreset_define_family_hook_fontspec:nnn
		      { mg } { m } { \__ltjpreset_font_mg_m: },
          }
        ] { \__ltjpreset_font_mg_m: }
      } {
        \newjfontfamily \mgfamily [
          BoldFont = \__ltjpreset_font_mg_m: \tl_use:N \l__ltjpreset_add_features_tl,
          \cs_if_exist:NT \ltjpreset_define_family_hook_fontspec:nnn {
		    \ltjpreset_define_family_hook_fontspec:nnn
		      { mg } { m } { \__ltjpreset_font_mg_m: },
          }
        ] { \__ltjpreset_font_mg_m: }
      }
      \DeclareTextFontCommand{\textmg}{\mgfamily}
    }
  }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
}

\def\ltjnewpreset{
    \@ifstar{\__ltjpreset_new_preset:nnn{ true }}{\__ltjpreset_new_preset:nnn{ false }}}
\cs_set:Nn \__ltjpreset_new_preset:nnn {
  \keys_if_exist:nnTF { luatexja-preset } { #2 } {
    \seq_if_in:NnTF \g__ltjpreset_presets_seq { #2 } {
      \bool_if:cTF { c_ #1 _bool } {
        \msg_warning:nnn { luatexja-preset }{ exist-preset-overwrite } { #2 }
        \keys_define:nn { luatexja-preset } {
        #2 .meta:n = { #3, __custom = true, __noembed = false, __office = false, },
        #2 .value_forbidden:n = true, #2 .groups:n = { preset },
        }
      } {
        \msg_error:nnn { luatexja-preset }{ exist-preset-nooverwrite } { #2 }
      }
    } {
      \msg_error:nnn { luatexja-preset }{ illegal-preset-name } { #2 }
    }
  } {
    \keys_define:nn { luatexja-preset } {
      #2 .meta:n = { #3, __custom = true, __noembed = false, __office = false, },
      #2 .value_forbidden:n = true, #2 .groups:n = { preset },
    }
    \seq_put_right:Nn \g__ltjpreset_presets_seq { #2 }
  }
}


\cs_set:Npn \ltjapplypreset #1 {
  \int_gincr:N \g__ltjpreset_applycount_int
  \keys_set_groups:nnn { luatexja-preset } { preset } { #1 }
  % \prop_map_inline:Nn \g__ltjpreset_font_prop {\typeout{ [##1] = "##2" } }
  \__ltjpreset_extract_fonts:
  \__ltjpreset_check_deluxe_existence:
  \__ltjpreset_do_fontset:
  \normalfont
}

\ltjapplypreset {}

\@onlypreamble\ltjnewpreset
\@onlypreamble\ltjapplypreset

\ExplSyntaxOff

\endinput
