% Copyright 2026 Open-Guji (https://github.com/open-guji)
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
%     http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.

% luatex-cn-font-autodetect.sty
% 中文字体自动探测模块 - Automatic Chinese Font Detection for LuaLaTeX
%
% ============================================================================
% 功能说明 (Description)
% ============================================================================
% 本模块根据操作系统自动选择合适的中文字体：
%   - Windows: SimSun (宋体), SimHei (黑体), KaiTi (楷体), FangSong (仿宋)
%   - macOS:   Songti SC, PingFang SC, Kaiti SC, STFangsong
%   - Linux:   FandolSong, FandolHei, FandolKai, FandolFang (开源方正字体)
%              或 Noto Serif/Sans CJK SC (Google Noto 字体)
%
% ============================================================================
% 使用方法 (Usage)
% ============================================================================
%
% 方式一：配合 ltc-guji 文档类使用（推荐）
% ------------------------------------------
% ltc-guji 会自动加载本模块，无需手动设置字体：
%
%   \documentclass{ltc-guji}
%   \begin{document}
%   \begin{正文}
%   这里的文字会自动使用系统中文字体排版。
%   \end{正文}
%   \end{document}
%
% 如需手动指定字体，使用 font 参数覆盖自动探测：
%
%   \begin{正文}[font=Source Han Serif SC]
%   使用思源宋体排版。
%   \end{正文}
%
% 方式二：独立使用本模块
% ------------------------------------------
%   \documentclass{article}
%   \usepackage{fontspec}
%   \usepackage{luatex-cn-font-autodetect}
%
%   \begin{document}
%   \ApplyAutoFont  % 应用自动探测的字体
%   中文内容...
%   \end{document}
%
% ============================================================================
% 提供的命令 (Commands)
% ============================================================================
% 注：以下命令（ApplyAutoFont、GetAutoFontName、GetAutoFontFeatures、
%     DeclareFontFallback）已被弃用，不再推荐使用。
%     请在 contentSetup 中使用 font 参数或通过 fontspec 直接配置字体。
%
% ============================================================================
% 手动设置字体 (Manual Font Configuration)
% ============================================================================
% 如果自动探测的字体不符合需求，可以手动使用 fontspec：
%
%   \setmainfont{Your Font Name}[
%     RawFeature={+vert,+vrt2},  % 竖排字形
%     CharacterWidth=Full        % 全角字符
%   ]
%
% 使用字体递补 (Font Fallbacks):
%
%   \DeclareFontFallback{mychain}{Font2, Font3}
%   \setmainfont{PrimaryFont}[RawFeature={fallback=mychain}]
%
% ============================================================================

\RequirePackage{expl3}
\RequirePackage{xparse}
\RequirePackage{luatexbase}
\ProvidesExplPackage {fonts/luatex-cn-font-autodetect} {2026/02/26} {0.3.1} {Automatic Chinese font detection for LuaLaTeX}

% Ensure we're using LuaTeX
\sys_if_engine_luatex:TF
  { }
  {
    \PackageError { luatex-cn-font-autodetect } { This~package~requires~LuaLaTeX }
      { Please~compile~your~document~with~lualatex. }
  }

% Load the Lua module
\lua_now:e {
  fontdetect~=~require("fonts.luatex-cn-font-autodetect")
}

\tl_new:N \l__luatexcn_autodetect_font_name_tl



% Internal helper to resolve first existing font from a comma-separated list
\cs_new:Npn \__luatexcn_resolve_font:n #1
  {
    \clist_map_inline:nn { #1 }
      {
        \fontspec_font_if_exist:nTF { ##1 }
          {
            \tl_set:Nn \l__luatexcn_autodetect_font_name_tl { ##1 }
            \clist_map_break:
          }
          { }
      }
  }

% ============================================================================
% Core Implementation for Auto Font Detection
% ============================================================================
% Internal command to apply auto-detected font
\NewDocumentCommand{\ApplyAutoFont}{}{
  \lua_now:e {
    texio.write_nl("term~and~log",~"[Font~Auto-Detect]~ApplyAutoFont~command~called");~

    if~not~fontdetect~then~
      texio.write_nl("term~and~log",~"[Font~Auto-Detect]~ERROR:~fontdetect~module~not~loaded");~
    else~
        local~font_setup~=~fontdetect.get_font_setup();~
        if~font_setup~then~
          texio.write_nl("term~and~log",~"[Font~Auto-Detect]~Candidates:~"~..~font_setup.main);~
          token.set_macro("l_tmpa_tl",~font_setup.main);~
          token.set_macro("l__luatexcn_content_font_features_tl",~font_setup.features);~
        else~
          token.set_macro("l_tmpa_tl",~"");~
        end;~
    end;
  }
  \tl_if_empty:NF \l_tmpa_tl
    {
      \tl_clear:N \l__luatexcn_autodetect_font_name_tl
      \exp_args:NV \__luatexcn_resolve_font:n \l_tmpa_tl
      \tl_if_empty:NTF \l__luatexcn_autodetect_font_name_tl
        {
          \iow_log:n { [Font~Auto-Detect]~No~fonts~found~in~candidate~list }
          \iow_term:n { [Font~Auto-Detect]~No~fonts~found~in~candidate~list }
        }
        {
          \iow_log:x { [Font~Auto-Detect]~Applying~resolved~font:~ \l__luatexcn_autodetect_font_name_tl }
          \iow_term:x { [Font~Auto-Detect]~Applying~resolved~font:~ \l__luatexcn_autodetect_font_name_tl }
          \exp_args:NV \setmainfont \l__luatexcn_autodetect_font_name_tl [\l__luatexcn_content_font_features_tl]
        }
    }
}

% ============================================================================
% DEPRECATED COMMANDS (no longer user-facing)
% ============================================================================
% The following commands have been marked as internal and are no longer
% recommended for user code:
%   - \GetAutoFontName (use contentSetup font parameter instead)
%   - \GetAutoFontFeatures (not needed)
%   - \DeclareFontFallback (use fontspec directly for font fallbacks)
%
% \ApplyAutoFont is still used internally by document classes.

% Command to set main font with a fallback chain in one step
% Usage: \SetMainFontChain{Font1, Font2, Font3}
\DeclareDocumentCommand{\setfontfamily}{m}
  {
    \tl_set:Nx \l_tmpa_tl { \clist_item:nn { #1 } { 1 } }
    \exp_args:NV \setmainfont \l_tmpa_tl
  }

\DeclareDocumentCommand{\setFontFamily}{m}{\setfontfamily{#1}}

% ============================================================================
% CJK Aliases (中文别名)
% ============================================================================
% Simplified Chinese / 简体
\DeclareDocumentCommand{\设置字体族}{m}{\setfontfamily{#1}}
\DeclareDocumentCommand{\设置字体}{m}{\setmainfont{#1}}
% Traditional Chinese / 繁体
\DeclareDocumentCommand{\設置字體族}{m}{\setfontfamily{#1}}
\DeclareDocumentCommand{\設置字體}{m}{\setmainfont{#1}}

\ExplSyntaxOff%
%
\endinput%
