\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{joufonts}[2026/03/07 JOU shared font setup]

% 严格字体检查选项
% 用法: \usepackage[strictfonts]{joufonts}
% 效果: 缺少商业字体时阻止编译
\newif\ifjou@strictfontcheck
\jou@strictfontcheckfalse
\DeclareOption{strictfonts}{\jou@strictfontchecktrue}
\DeclareOption*{\PackageWarning{joufonts}{Unknown option '\CurrentOption'}}
\ProcessOptions\relax

\RequirePackage{iftex}
\ifXeTeX\else
    \PackageError{joufonts}{This template requires XeLaTeX}{Please compile with XeLaTeX.}
\fi

\RequirePackage{fontspec}
\RequirePackage{xeCJK}

\defaultfontfeatures{Ligatures=TeX}

\newcommand{\JOUOpenSourceFontDir}{fonts/opensource}
\newcommand{\JOUProprietaryFontDir}{fonts/proprietary}
\newcommand{\JOUWPSFontDir}{}
\newcommand{\JOUWindowsFontDir}{}

\IfFileExists{fonts/opensource/Tinos-Regular.ttf}{%
    \renewcommand{\JOUOpenSourceFontDir}{fonts/opensource}%
    \renewcommand{\JOUProprietaryFontDir}{fonts/proprietary}%
}{%
    \IfFileExists{../../fonts/opensource/Tinos-Regular.ttf}{%
        \renewcommand{\JOUOpenSourceFontDir}{../../fonts/opensource}%
        \renewcommand{\JOUProprietaryFontDir}{../../fonts/proprietary}%
    }{}
}

\IfFileExists{/Applications/wpsoffice.app/Contents/Resources/office6/fonts/HYKaiTiJ.ttf}{%
    \renewcommand{\JOUWPSFontDir}{/Applications/wpsoffice.app/Contents/Resources/office6/fonts}%
}{%
    \IfFileExists{/Applications/WPS Office.app/Contents/Resources/office6/fonts/HYKaiTiJ.ttf}{%
        \renewcommand{\JOUWPSFontDir}{/Applications/WPS Office.app/Contents/Resources/office6/fonts}%
    }{%
        \IfFileExists{/opt/kingsoft/wps-office/office6/fonts/HYKaiTiJ.ttf}{%
            \renewcommand{\JOUWPSFontDir}{/opt/kingsoft/wps-office/office6/fonts}%
        }{%
            \IfFileExists{/usr/share/fonts/wps-office/HYKaiTiJ.ttf}{%
                \renewcommand{\JOUWPSFontDir}{/usr/share/fonts/wps-office}%
            }{}%
        }%
    }%
}

\IfFileExists{C:/Windows/Fonts/times.ttf}{%
    \renewcommand{\JOUWindowsFontDir}{C:/Windows/Fonts}%
}{%
    \IfFileExists{C:/WINDOWS/Fonts/times.ttf}{%
        \renewcommand{\JOUWindowsFontDir}{C:/WINDOWS/Fonts}%
    }{}%
}

\IfFileExists{C:/Program Files/WPS Office/office6/fonts/HYKaiTiJ.ttf}{%
    \renewcommand{\JOUWPSFontDir}{C:/Program Files/WPS Office/office6/fonts}%
}{%
    \IfFileExists{C:/Program Files (x86)/WPS Office/office6/fonts/HYKaiTiJ.ttf}{%
        \renewcommand{\JOUWPSFontDir}{C:/Program Files (x86)/WPS Office/office6/fonts}%
    }{%
        \IfFileExists{C:/Program Files/Kingsoft/WPS Office/office6/fonts/HYKaiTiJ.ttf}{%
            \renewcommand{\JOUWPSFontDir}{C:/Program Files/Kingsoft/WPS Office/office6/fonts}%
        }{%
            \IfFileExists{C:/Program Files (x86)/Kingsoft/WPS Office/office6/fonts/HYKaiTiJ.ttf}{%
                \renewcommand{\JOUWPSFontDir}{C:/Program Files (x86)/Kingsoft/WPS Office/office6/fonts}%
            }{}%
        }%
    }%
}

\IfFileExists{styles/joufontspaths.local.tex}{%
    \input{styles/joufontspaths.local.tex}%
}{%
    \IfFileExists{../../styles/joufontspaths.local.tex}{%
        \input{../../styles/joufontspaths.local.tex}%
    }{}%
}

\newcommand{\JOUFontMode}{oss}

\newcommand{\jou@setupopenlatin}{%
    \setmainfont[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=Tinos-Regular.ttf,
        BoldFont=Tinos-Bold.ttf,
        ItalicFont=Tinos-Italic.ttf,
        BoldItalicFont=Tinos-BoldItalic.ttf
    ]{Tinos-Regular.ttf}%
    \setsansfont[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=NotoSansCJKsc-Regular.otf,
        BoldFont=NotoSansCJKsc-Bold.otf
    ]{NotoSansCJKsc-Regular.otf}%
    \setmonofont[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=CourierPrime-Regular.ttf,
        BoldFont=CourierPrime-Bold.ttf
    ]{CourierPrime-Regular.ttf}%
}

\newcommand{\jou@setupproprietarylatin}{%
    \renewcommand{\JOUFontMode}{licensed}%
    \setmainfont[
        Path=\JOUProprietaryFontDir/,
        UprightFont=TimesNewRoman-Regular.ttf,
        BoldFont=TimesNewRoman-Bold.ttf
    ]{TimesNewRoman-Regular.ttf}%
    \IfFileExists{\JOUProprietaryFontDir/Arial-Regular.ttf}{%
        \setsansfont[
            Path=\JOUProprietaryFontDir/,
            UprightFont=Arial-Regular.ttf,
            BoldFont=Arial-Bold.ttf
        ]{Arial-Regular.ttf}%
    }{%
        \setsansfont[
            Path=\JOUOpenSourceFontDir/,
            UprightFont=NotoSansCJKsc-Regular.otf,
            BoldFont=NotoSansCJKsc-Bold.otf
        ]{NotoSansCJKsc-Regular.otf}%
    }%
    \IfFileExists{\JOUProprietaryFontDir/CourierNew-Regular.ttf}{%
        \setmonofont[
            Path=\JOUProprietaryFontDir/,
            UprightFont=CourierNew-Regular.ttf,
            BoldFont=CourierNew-Bold.ttf
        ]{CourierNew-Regular.ttf}%
    }{%
        \setmonofont[
            Path=\JOUOpenSourceFontDir/,
            UprightFont=CourierPrime-Regular.ttf,
            BoldFont=CourierPrime-Bold.ttf
        ]{CourierPrime-Regular.ttf}%
    }%
}

\newcommand{\jou@setupwindowslatin}{%
    \renewcommand{\JOUFontMode}{system-licensed}%
    \setmainfont[
        Path=\JOUWindowsFontDir/,
        UprightFont=times.ttf,
        BoldFont=timesbd.ttf,
        ItalicFont=timesi.ttf,
        BoldItalicFont=timesbi.ttf
    ]{times.ttf}%
    \IfFileExists{\JOUWindowsFontDir/arial.ttf}{%
        \setsansfont[
            Path=\JOUWindowsFontDir/,
            UprightFont=arial.ttf,
            BoldFont=arialbd.ttf,
            ItalicFont=ariali.ttf,
            BoldItalicFont=arialbi.ttf
        ]{arial.ttf}%
    }{%
        \setsansfont[
            Path=\JOUOpenSourceFontDir/,
            UprightFont=NotoSansCJKsc-Regular.otf,
            BoldFont=NotoSansCJKsc-Bold.otf
        ]{NotoSansCJKsc-Regular.otf}%
    }%
    \IfFileExists{\JOUWindowsFontDir/cour.ttf}{%
        \setmonofont[
            Path=\JOUWindowsFontDir/,
            UprightFont=cour.ttf,
            BoldFont=courbd.ttf,
            ItalicFont=couri.ttf,
            BoldItalicFont=courbi.ttf
        ]{cour.ttf}%
    }{%
        \setmonofont[
            Path=\JOUOpenSourceFontDir/,
            UprightFont=CourierPrime-Regular.ttf,
            BoldFont=CourierPrime-Bold.ttf
        ]{CourierPrime-Regular.ttf}%
    }%
}

\newcommand{\jou@useopensourcehei}{%
    \setCJKsansfont[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=NotoSansCJKsc-Regular.otf,
        BoldFont=NotoSansCJKsc-Bold.otf
    ]{NotoSansCJKsc-Regular.otf}%
    \setCJKfamilyfont{zhhei}[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=NotoSansCJKsc-Regular.otf,
        BoldFont=NotoSansCJKsc-Bold.otf
    ]{NotoSansCJKsc-Regular.otf}%
    \newCJKfontfamily\JOUCoverHei[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=NotoSansCJKsc-Bold.otf
    ]{NotoSansCJKsc-Bold.otf}%
}

\newcommand{\jou@useopensourcekai}{%
    \setCJKfamilyfont{zhkai}[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=LXGWWenKaiGB-Regular.ttf,
        BoldFont=LXGWWenKaiGB-Medium.ttf
    ]{LXGWWenKaiGB-Regular.ttf}%
    \newCJKfontfamily\JOUCoverKai[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=LXGWWenKaiGB-Regular.ttf,
        BoldFont=LXGWWenKaiGB-Medium.ttf
    ]{LXGWWenKaiGB-Regular.ttf}%
}

\newcommand{\jou@useopensourcefang}{%
    \setCJKfamilyfont{zhfs}[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=FandolFang-Regular.otf
    ]{FandolFang-Regular.otf}%
}

\newcommand{\jou@useopensourcesong}{%
    \setCJKmainfont[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=NotoSerifCJKsc-Regular.otf,
        BoldFont=NotoSerifCJKsc-Bold.otf
    ]{NotoSerifCJKsc-Regular.otf}%
    \setCJKfamilyfont{zhsong}[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=NotoSerifCJKsc-Regular.otf,
        BoldFont=NotoSerifCJKsc-Bold.otf
    ]{NotoSerifCJKsc-Regular.otf}%
}

\newcommand{\jou@useopensourcexiaobiaosong}{%
    \newCJKfontfamily\JOUXiaobiaoSong[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=NotoSerifCJKsc-Black.otf
    ]{NotoSerifCJKsc-Black.otf}%
}

\newcommand{\jou@useopensourcexingkai}{%
    \newCJKfontfamily\JOUXingkai[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=LXGWWenKaiGB-Medium.ttf
    ]{LXGWWenKaiGB-Medium.ttf}%
}

\newcommand{\jou@useopensourcelishu}{%
    \newCJKfontfamily\JOULishu[
        Path=\JOUOpenSourceFontDir/,
        UprightFont=NotoSerifCJKsc-Black.otf
    ]{NotoSerifCJKsc-Black.otf}%
}

\newcommand{\jou@setupopensourcecjk}{%
    \jou@useopensourcesong
    \jou@useopensourcehei
    \jou@useopensourcekai
    \jou@useopensourcefang
    \jou@useopensourcexiaobiaosong
    \jou@useopensourcexingkai
    \jou@useopensourcelishu
}

\newcommand{\jou@setupproprietarycjk}{%
    \renewcommand{\JOUFontMode}{licensed}%
    \IfFileExists{\JOUProprietaryFontDir/SimSun.ttf}{%
        \setCJKmainfont[
            Path=\JOUProprietaryFontDir/,
            UprightFont=SimSun.ttf
        ]{SimSun.ttf}%
        \setCJKfamilyfont{zhsong}[
            Path=\JOUProprietaryFontDir/,
            UprightFont=SimSun.ttf
        ]{SimSun.ttf}%
    }{%
        \jou@useopensourcesong
    }%
    \IfFileExists{\JOUProprietaryFontDir/SimHei.ttf}{%
        \setCJKsansfont[
            Path=\JOUProprietaryFontDir/,
            UprightFont=SimHei.ttf
        ]{SimHei.ttf}%
        \setCJKfamilyfont{zhhei}[
            Path=\JOUProprietaryFontDir/,
            UprightFont=SimHei.ttf
        ]{SimHei.ttf}%
        \newCJKfontfamily\JOUCoverHei[
            Path=\JOUProprietaryFontDir/,
            UprightFont=SimHei.ttf
        ]{SimHei.ttf}%
    }{%
        \jou@useopensourcehei
    }%
    \IfFileExists{\JOUProprietaryFontDir/KaiTi_GB2312.ttf}{%
        \setCJKfamilyfont{zhkai}[
            Path=\JOUProprietaryFontDir/,
            UprightFont=KaiTi_GB2312.ttf
        ]{KaiTi_GB2312.ttf}%
        \newCJKfontfamily\JOUCoverKai[
            Path=\JOUProprietaryFontDir/,
            UprightFont=KaiTi_GB2312.ttf,
            BoldFont=KaiTi_GB2312.ttf,
            AutoFakeBold=2.2
        ]{KaiTi_GB2312.ttf}%
    }{%
        \jou@useopensourcekai
    }%
    \IfFileExists{\JOUProprietaryFontDir/FangSong_GB2312.ttf}{%
        \setCJKfamilyfont{zhfs}[
            Path=\JOUProprietaryFontDir/,
            UprightFont=FangSong_GB2312.ttf
        ]{FangSong_GB2312.ttf}%
    }{%
        \jou@useopensourcefang
    }%
    \IfFileExists{\JOUProprietaryFontDir/FangZhengXiaoBiaoSongJianTi.ttf}{%
        \newCJKfontfamily\JOUXiaobiaoSong[
            Path=\JOUProprietaryFontDir/,
            UprightFont=FangZhengXiaoBiaoSongJianTi.ttf
        ]{FangZhengXiaoBiaoSongJianTi.ttf}%
    }{%
        \jou@useopensourcexiaobiaosong
    }%
    \IfFileExists{\JOUProprietaryFontDir/STXingkai.ttf}{%
        \newCJKfontfamily\JOUXingkai[
            Path=\JOUProprietaryFontDir/,
            UprightFont=STXingkai.ttf
        ]{STXingkai.ttf}%
    }{%
        \jou@useopensourcexingkai
    }%
    \jou@useopensourcelishu
}

\newcommand{\jou@setupwpsbundledcjk}{%
    \renewcommand{\JOUFontMode}{wps-compat}%
    \setCJKmainfont[
        Path=\JOUWPSFontDir/,
        UprightFont=FZSSK.ttf
    ]{FZSSK.ttf}%
    \setCJKfamilyfont{zhsong}[
        Path=\JOUWPSFontDir/,
        UprightFont=FZSSK.ttf
    ]{FZSSK.ttf}%
    \setCJKsansfont[
        Path=\JOUWPSFontDir/,
        UprightFont=HYZhongJianHeiJ.ttf
    ]{HYZhongJianHeiJ.ttf}%
    \setCJKfamilyfont{zhhei}[
        Path=\JOUWPSFontDir/,
        UprightFont=HYZhongJianHeiJ.ttf
    ]{HYZhongJianHeiJ.ttf}%
    \newCJKfontfamily\JOUCoverHei[
        Path=\JOUWPSFontDir/,
        UprightFont=HYZhongJianHeiJ.ttf
    ]{HYZhongJianHeiJ.ttf}%
    \setCJKfamilyfont{zhkai}[
        Path=\JOUWPSFontDir/,
        UprightFont=HYKaiTiJ.ttf
    ]{HYKaiTiJ.ttf}%
    \newCJKfontfamily\JOUCoverKai[
        Path=\JOUWPSFontDir/,
        UprightFont=HYKaiTiJ.ttf,
        BoldFont=HYKaiTiJ.ttf,
        AutoFakeBold=1.5
    ]{HYKaiTiJ.ttf}%
    \setCJKfamilyfont{zhfs}[
        Path=\JOUWPSFontDir/,
        UprightFont=FZFSK.ttf
    ]{FZFSK.ttf}%
    \newCJKfontfamily\JOUXiaobiaoSong[
        Path=\JOUWPSFontDir/,
        UprightFont=FZSSK.ttf
    ]{FZSSK.ttf}%
    \jou@useopensourcexingkai
    \jou@useopensourcelishu
}

\newcommand{\jou@setupwindowscjk}{%
    \renewcommand{\JOUFontMode}{system-licensed}%
    \setCJKmainfont[
        Path=\JOUWindowsFontDir/,
        UprightFont=simsun.ttc
    ]{simsun.ttc}%
    \setCJKfamilyfont{zhsong}[
        Path=\JOUWindowsFontDir/,
        UprightFont=simsun.ttc
    ]{simsun.ttc}%
    \IfFileExists{\JOUWindowsFontDir/simhei.ttf}{%
        \setCJKsansfont[
            Path=\JOUWindowsFontDir/,
            UprightFont=simhei.ttf
        ]{simhei.ttf}%
        \setCJKfamilyfont{zhhei}[
            Path=\JOUWindowsFontDir/,
            UprightFont=simhei.ttf
        ]{simhei.ttf}%
        \newCJKfontfamily\JOUCoverHei[
            Path=\JOUWindowsFontDir/,
            UprightFont=simhei.ttf
        ]{simhei.ttf}%
    }{%
        \jou@useopensourcehei
    }%
    \IfFileExists{\JOUWindowsFontDir/simkai.ttf}{%
        \setCJKfamilyfont{zhkai}[
            Path=\JOUWindowsFontDir/,
            UprightFont=simkai.ttf
        ]{simkai.ttf}%
        \newCJKfontfamily\JOUCoverKai[
            Path=\JOUWindowsFontDir/,
            UprightFont=simkai.ttf,
            BoldFont=simkai.ttf,
            AutoFakeBold=2.2
        ]{simkai.ttf}%
    }{%
        \jou@useopensourcekai
    }%
    \IfFileExists{\JOUWindowsFontDir/simfang.ttf}{%
        \setCJKfamilyfont{zhfs}[
            Path=\JOUWindowsFontDir/,
            UprightFont=simfang.ttf
        ]{simfang.ttf}%
    }{%
        \jou@useopensourcefang
    }%
    \IfFileExists{\JOUWindowsFontDir/FZXBSJW.TTF}{%
        \newCJKfontfamily\JOUXiaobiaoSong[
            Path=\JOUWindowsFontDir/,
            UprightFont=FZXBSJW.TTF
        ]{FZXBSJW.TTF}%
    }{%
        \jou@useopensourcexiaobiaosong
    }%
    \IfFileExists{\JOUWindowsFontDir/STXINGKA.TTF}{%
        \newCJKfontfamily\JOUXingkai[
            Path=\JOUWindowsFontDir/,
            UprightFont=STXINGKA.TTF
        ]{STXINGKA.TTF}%
    }{%
        \jou@useopensourcexingkai
    }%
    \jou@useopensourcelishu
}

\newcommand{\jou@applyprivatecjkoverrides}{%
    \IfFileExists{\JOUProprietaryFontDir/SimSun.ttf}{%
        \typeout{JOU: Applying local SimSun override from \JOUProprietaryFontDir}%
        \setCJKmainfont[
            Path=\JOUProprietaryFontDir/,
            UprightFont=SimSun.ttf
        ]{SimSun.ttf}%
        \setCJKfamilyfont{zhsong}[
            Path=\JOUProprietaryFontDir/,
            UprightFont=SimSun.ttf
        ]{SimSun.ttf}%
    }{}%
    \IfFileExists{\JOUProprietaryFontDir/SimHei.ttf}{%
        \typeout{JOU: Applying local SimHei override from \JOUProprietaryFontDir}%
        \setCJKsansfont[
            Path=\JOUProprietaryFontDir/,
            UprightFont=SimHei.ttf
        ]{SimHei.ttf}%
        \setCJKfamilyfont{zhhei}[
            Path=\JOUProprietaryFontDir/,
            UprightFont=SimHei.ttf
        ]{SimHei.ttf}%
        \setCJKfamilyfont{joucoverhei}[
            Path=\JOUProprietaryFontDir/,
            UprightFont=SimHei.ttf
        ]{SimHei.ttf}%
        \renewcommand{\JOUCoverHei}{\CJKfamily{joucoverhei}}%
    }{}%
    \IfFileExists{\JOUProprietaryFontDir/KaiTi_GB2312.ttf}{%
        \typeout{JOU: Applying local KaiTi_GB2312 override from \JOUProprietaryFontDir}%
        \setCJKfamilyfont{joucoverkai}[
            Path=\JOUProprietaryFontDir/,
            UprightFont=KaiTi_GB2312.ttf,
            BoldFont=KaiTi_GB2312.ttf,
            AutoFakeBold=2.2
        ]{KaiTi_GB2312.ttf}%
        \setCJKfamilyfont{zhkai}[
            Path=\JOUProprietaryFontDir/,
            UprightFont=KaiTi_GB2312.ttf
        ]{KaiTi_GB2312.ttf}%
        \renewcommand{\JOUCoverKai}{\CJKfamily{joucoverkai}}%
    }{}%
    \IfFileExists{\JOUProprietaryFontDir/FangSong_GB2312.ttf}{%
        \typeout{JOU: Applying local FangSong_GB2312 override from \JOUProprietaryFontDir}%
        \setCJKfamilyfont{zhfs}[
            Path=\JOUProprietaryFontDir/,
            UprightFont=FangSong_GB2312.ttf
        ]{FangSong_GB2312.ttf}%
    }{}%
    \IfFileExists{\JOUProprietaryFontDir/FangZhengXiaoBiaoSongJianTi.ttf}{%
        \typeout{JOU: Applying local FangZhengXiaoBiaoSongJianTi override from \JOUProprietaryFontDir}%
        \setCJKfamilyfont{jouxiaobiaosong}[
            Path=\JOUProprietaryFontDir/,
            UprightFont=FangZhengXiaoBiaoSongJianTi.ttf
        ]{FangZhengXiaoBiaoSongJianTi.ttf}%
        \renewcommand{\JOUXiaobiaoSong}{\CJKfamily{jouxiaobiaosong}}%
    }{}%
}

% ============================================================================
% 字体加载策略：优先标准学术字体，确保毕业论文可编辑、可交付、可跨平台
% ============================================================================
% 优先级：
% 1. fonts/proprietary/ 本地商业字体文件
% 2. 系统安装的标准学术字体（Windows/macOS/Linux 自动检测）
% 3. WPS 兼容字体（仅在标准字体不可用时使用）
% 4. fonts/opensource/ 仓库开源字体（最后兜底）
% ============================================================================

\IfFileExists{\JOUOpenSourceFontDir/Tinos-Regular.ttf}{}{%
    \PackageError{joufonts}{Missing vendored fonts under fonts/opensource}{Run python3 scripts/download_fonts.py first.}
}

% 拉丁字体加载逻辑
\IfFileExists{\JOUProprietaryFontDir/TimesNewRoman-Regular.ttf}{%
    \jou@setupproprietarylatin
}{%
    \IfFileExists{\JOUWindowsFontDir/times.ttf}{%
        \jou@setupwindowslatin
    }{%
        \IfFontExistsTF{Times New Roman}{%
            \renewcommand{\JOUFontMode}{system-licensed}%
            \setmainfont{Times New Roman}%
            \IfFontExistsTF{Arial}{%
                \setsansfont{Arial}%
            }{%
                \setsansfont[
                    Path=\JOUOpenSourceFontDir/,
                    UprightFont=NotoSansCJKsc-Regular.otf,
                    BoldFont=NotoSansCJKsc-Bold.otf
                ]{NotoSansCJKsc-Regular.otf}%
            }%
            \IfFontExistsTF{Courier New}{%
                \setmonofont{Courier New}%
            }{%
                \setmonofont[
                    Path=\JOUOpenSourceFontDir/,
                    UprightFont=CourierPrime-Regular.ttf,
                    BoldFont=CourierPrime-Bold.ttf
                ]{CourierPrime-Regular.ttf}%
            }%
        }{%
            \jou@setupopenlatin
        }%
    }%
}

% CJK 字体加载逻辑
\IfFileExists{\JOUProprietaryFontDir/SimSun.ttf}{%
    \jou@setupproprietarycjk
}{%
    \IfFileExists{\JOUWindowsFontDir/simsun.ttc}{%
        \jou@setupwindowscjk
    }{%
        \newif\ifjou@systemcjkfound
        \jou@systemcjkfoundfalse

        % 优先检测标准 Windows/跨平台学术字体
        \ifjou@systemcjkfound\else
            \IfFontExistsTF{SimSun}{%
                \jou@systemcjkfoundtrue
                \renewcommand{\JOUFontMode}{system-licensed}%
                \setCJKmainfont{SimSun}%
                \setCJKfamilyfont{zhsong}{SimSun}%
                \IfFontExistsTF{SimHei}{%
                    \setCJKsansfont{SimHei}%
                    \setCJKfamilyfont{zhhei}{SimHei}%
                    \newCJKfontfamily\JOUCoverHei{SimHei}%
                }{%
                    \jou@useopensourcehei
                }%
                \IfFontExistsTF{KaiTi_GB2312}{%
                    \setCJKfamilyfont{zhkai}{KaiTi_GB2312}%
                    \newCJKfontfamily\JOUCoverKai{KaiTi_GB2312}[AutoFakeBold=2.2]%
                }{%
                    \IfFontExistsTF{KaiTi}{%
                        \setCJKfamilyfont{zhkai}{KaiTi}%
                        \newCJKfontfamily\JOUCoverKai{KaiTi}[AutoFakeBold=2.2]%
                    }{%
                        \jou@useopensourcekai
                    }%
                }%
                \IfFontExistsTF{FangSong_GB2312}{%
                    \setCJKfamilyfont{zhfs}{FangSong_GB2312}%
                }{%
                    \IfFontExistsTF{FangSong}{%
                        \setCJKfamilyfont{zhfs}{FangSong}%
                    }{%
                        \jou@useopensourcefang
                    }%
                }%
                \IfFontExistsTF{FZXiaoBiaoSong-B05}{%
                    \newCJKfontfamily\JOUXiaobiaoSong{FZXiaoBiaoSong-B05}%
                }{%
                    \jou@useopensourcexiaobiaosong
                }%
                \IfFontExistsTF{STXingkai}{%
                    \newCJKfontfamily\JOUXingkai{STXingkai}%
                }{%
                    \jou@useopensourcexingkai
                }%
                \jou@useopensourcelishu
            }{}%
        \fi

        % 其次检测标准 macOS 学术字体
        \ifjou@systemcjkfound\else
            \IfFontExistsTF{STSong}{%
                \jou@systemcjkfoundtrue
                \renewcommand{\JOUFontMode}{system-licensed}%
                \setCJKmainfont{STSong}%
                \setCJKfamilyfont{zhsong}{STSong}%
                \IfFontExistsTF{STHeiti}{%
                    \setCJKsansfont{STHeiti}%
                    \setCJKfamilyfont{zhhei}{STHeiti}%
                    \newCJKfontfamily\JOUCoverHei{STHeiti}%
                }{%
                    \jou@useopensourcehei
                }%
                \IfFontExistsTF{STKaiti}{%
                    \setCJKfamilyfont{zhkai}{STKaiti}%
                    \newCJKfontfamily\JOUCoverKai{STKaiti}[AutoFakeBold=2.2]%
                }{%
                    \jou@useopensourcekai
                }%
                \IfFontExistsTF{STFangsong}{%
                    \setCJKfamilyfont{zhfs}{STFangsong}%
                }{%
                    \jou@useopensourcefang
                }%
                \IfFontExistsTF{FZXiaoBiaoSong-B05}{%
                    \newCJKfontfamily\JOUXiaobiaoSong{FZXiaoBiaoSong-B05}%
                }{%
                    \jou@useopensourcexiaobiaosong
                }%
                \IfFontExistsTF{STXingkai}{%
                    \newCJKfontfamily\JOUXingkai{STXingkai}%
                }{%
                    \jou@useopensourcexingkai
                }%
                \jou@useopensourcelishu
            }{}%
        \fi

        % 再使用系统已安装的 WPS 兼容字体
        \ifjou@systemcjkfound\else
            \IfFontExistsTF{HYKaiTiKW}{%
                \jou@systemcjkfoundtrue
                \renewcommand{\JOUFontMode}{wps-compat}%
                \IfFontExistsTF{HYShuSongErKW}{%
                    \setCJKmainfont{HYShuSongErKW}%
                    \setCJKfamilyfont{zhsong}{HYShuSongErKW}%
                }{%
                    \jou@useopensourcesong
                }%
                \IfFontExistsTF{HYZhongHeiKW}{%
                    \setCJKsansfont{HYZhongHeiKW}%
                    \setCJKfamilyfont{zhhei}{HYZhongHeiKW}%
                    \newCJKfontfamily\JOUCoverHei{HYZhongHeiKW}%
                }{%
                    \IfFontExistsTF{HYZhongJianHeiJ}{%
                        \setCJKsansfont{HYZhongJianHeiJ}%
                        \setCJKfamilyfont{zhhei}{HYZhongJianHeiJ}%
                        \newCJKfontfamily\JOUCoverHei{HYZhongJianHeiJ}%
                    }{%
                        \jou@useopensourcehei
                    }%
                }%
                \setCJKfamilyfont{zhkai}{HYKaiTiKW}%
                \newCJKfontfamily\JOUCoverKai{HYKaiTiKW}[AutoFakeBold=2.2]%
                \IfFontExistsTF{FZFSK--GBK1-0}{%
                    \setCJKfamilyfont{zhfs}{FZFSK--GBK1-0}%
                }{%
                    \jou@useopensourcefang
                }%
                \IfFontExistsTF{FZXBSJW--GB1-0}{%
                    \newCJKfontfamily\JOUXiaobiaoSong{FZXBSJW--GB1-0}%
                }{%
                    \jou@useopensourcexiaobiaosong
                }%
                \IfFontExistsTF{STXingkai}{%
                    \newCJKfontfamily\JOUXingkai{STXingkai}%
                }{%
                    \jou@useopensourcexingkai
                }%
                \jou@useopensourcelishu
            }{}%
        \fi

        % 最后使用 WPS 应用包自带字体文件
        \ifjou@systemcjkfound\else
            \IfFileExists{\JOUWPSFontDir/HYKaiTiJ.ttf}{%
                \jou@systemcjkfoundtrue
                \typeout{JOU: Using WPS bundled compatibility fonts from \JOUWPSFontDir}%
                \jou@setupwpsbundledcjk
            }{}%
        \fi

        \ifjou@systemcjkfound\else
            \jou@setupopensourcecjk
        \fi
    }%
}
\jou@applyprivatecjkoverrides

% Shared family selectors for templates that need deterministic font routing.
\newcommand{\JOUSong}{\CJKfamily{zhsong}}
\newcommand{\JOUHei}{\CJKfamily{zhhei}}
\newcommand{\JOUKai}{\CJKfamily{zhkai}}
\newcommand{\JOUFang}{\CJKfamily{zhfs}}
\providecommand{\JOUCoverKai}{\JOUKai}
\providecommand{\JOUCoverHei}{\JOUHei}

% 编译时字体模式提示（友好且简洁）
\AtBeginDocument{%
    \typeout{}%
    \typeout{===============================================}%
    \typeout{Font Mode: \JOUFontMode}%
    \ifdefstring{\JOUFontMode}{licensed}{%
        \typeout{Status: Using local academic standard fonts (Best)}%
    }{%
    \ifdefstring{\JOUFontMode}{wps-compat}{%
        \typeout{Status: Using WPS compatibility fonts (Fallback)}%
    }{%
    \ifdefstring{\JOUFontMode}{system-licensed}{%
        \typeout{Status: Using system academic standard fonts (Excellent)}%
    }{%
    \ifdefstring{\JOUFontMode}{oss}{%
        \typeout{Status: Using open source academic fallback fonts (Preview)}%
    }{}}}}%
    \typeout{===============================================}%
    %
    \ifjou@strictfontcheck
        \ifdefstring{\JOUFontMode}{oss}{%
            \typeout{}%
            \typeout{Strict mode: Commercial fonts required for final submission.}%
            \typeout{Run: python3 scripts/check_fonts.py}%
            \typeout{}%
            \PackageError{joufonts}{%
                Strict font checking enabled: commercial fonts required!\MessageBreak
                Run: python3 scripts/check_fonts.py%
            }{%
                Install commercial fonts or disable strict mode.%
            }%
        }{}%
    \else
        \ifdefstring{\JOUFontMode}{oss}{%
            \typeout{}%
            \typeout{TIP: For best academic output, prefer KaiTi_GB2312,}%
            \typeout{     SimSun, SimHei, and Times New Roman.}%
            \typeout{     Check: python3 scripts/check_fonts.py}%
        }{}%
    \fi
    \typeout{}%
}
