% Copyright (c) 2026 Wang Yang (王阳) and contributors
% SPDX-License-Identifier: CC-BY-SA-4.0
% HUST names and institutional marks are excluded from this license grant.
% See LICENSE and NOTICE.md for details.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{beamerthemehust}[2026/07/20 v0.7.1 Unofficial HUST Beamer theme]

\RequirePackage{etoolbox}
\RequirePackage{graphicx}
\RequirePackage{tikz}
\RequirePackage{xcolor}

\mode<presentation>

% Shared geometry tokens. Branded pages use the physical safe grid; ordinary
% frames use a wider title field and an inset reading field.
\newlength{\hust@margin}
\newlength{\hust@contentwidth}
\newlength{\hust@coverfieldwidth}
\newlength{\hust@frametitlemargin}
\newlength{\hust@frametitlewidth}
\newlength{\hust@bodymarginleft}
\newlength{\hust@bodymarginright}
\setlength{\hust@margin}{10mm}
\setlength{\hust@contentwidth}{%
  \dimexpr\paperwidth-2\hust@margin\relax}
\setlength{\hust@coverfieldwidth}{60mm}
\setlength{\hust@frametitlemargin}{12mm}
\setlength{\hust@frametitlewidth}{136mm}
\setlength{\hust@bodymarginleft}{17mm}
\setlength{\hust@bodymarginright}{28mm}

% Public metadata hooks.
\newcommand{\hust@department}{}
\newcommand{\hust@departmenten}{}
\newcommand{\hust@departmentshort}{}
\newcommand{\hust@event}{}
\newcommand{\hust@eventen}{}
\newcommand{\hust@sectionnote}{}
\newcommand{\hust@coverimage}{}
\newcommand{\hust@coverimageoptions}{}
\newcommand{\hust@assetpath}{assets}
\newcommand{\hust@customlogo}{}
\newcommand{\hust@emblemfile}{}
\newcommand{\hust@emblemwhitefile}{}
\newcommand{\hust@namefile}{}
\newcommand{\hust@namewhitefile}{}
\newif\ifhust@hascustomlogo
\hust@hascustomlogofalse

\newcommand{\hustsetdepartment}[1]{\renewcommand{\hust@department}{#1}}
\newcommand{\hustsetdepartmenten}[1]{%
  \renewcommand{\hust@departmenten}{#1}}
% Retained for source compatibility; light-page footers are pagination-only.
\newcommand{\hustsetdepartmentshort}[1]{%
  \renewcommand{\hust@departmentshort}{#1}}
\newcommand{\hustsetevent}[1]{\renewcommand{\hust@event}{#1}}
\newcommand{\hustseteventen}[1]{\renewcommand{\hust@eventen}{#1}}
\newcommand{\hustsetsectionnote}[1]{\gdef\hust@sectionnote{#1}}
\newcommand{\hustsetcoverimage}[2][]{%
  \renewcommand{\hust@coverimageoptions}{#1}%
  \renewcommand{\hust@coverimage}{#2}}
\newcommand{\hustclearcoverimage}{%
  \renewcommand{\hust@coverimageoptions}{}%
  \renewcommand{\hust@coverimage}{}}
\newcommand{\hustsetassetpath}[1]{\renewcommand{\hust@assetpath}{#1}}
\newcommand{\hustsetlogo}[1]{%
  \renewcommand{\hust@customlogo}{#1}%
  \hust@hascustomlogotrue
}

% Resolve the bundled vector assets late so \hustsetassetpath may be used in
% the document preamble. The ../assets fallback supports compilation from the
% example directory.
\newcommand{\hust@resolveassets}{%
  \IfFileExists{\hust@assetpath/hust-emblem-color.pdf}{%
    \gdef\hust@emblemfile{\hust@assetpath/hust-emblem-color.pdf}%
  }{%
    \IfFileExists{../\hust@assetpath/hust-emblem-color.pdf}{%
      \gdef\hust@emblemfile{../\hust@assetpath/hust-emblem-color.pdf}%
    }{}%
  }%
  \IfFileExists{\hust@assetpath/hust-name-blue.pdf}{%
    \gdef\hust@namefile{\hust@assetpath/hust-name-blue.pdf}%
  }{%
    \IfFileExists{../\hust@assetpath/hust-name-blue.pdf}{%
      \gdef\hust@namefile{../\hust@assetpath/hust-name-blue.pdf}%
    }{}%
  }%
  \IfFileExists{\hust@assetpath/hust-emblem-white.pdf}{%
    \gdef\hust@emblemwhitefile{\hust@assetpath/hust-emblem-white.pdf}%
  }{%
    \IfFileExists{../\hust@assetpath/hust-emblem-white.pdf}{%
      \gdef\hust@emblemwhitefile{%
        ../\hust@assetpath/hust-emblem-white.pdf}%
    }{}%
  }%
  \IfFileExists{\hust@assetpath/hust-name-white.pdf}{%
    \gdef\hust@namewhitefile{\hust@assetpath/hust-name-white.pdf}%
  }{%
    \IfFileExists{../\hust@assetpath/hust-name-white.pdf}{%
      \gdef\hust@namewhitefile{../\hust@assetpath/hust-name-white.pdf}%
    }{}%
  }%
  \ifx\hust@emblemfile\@empty
    \PackageWarningNoLine{beamerthemehust}{%
      Official emblem asset not found. Set \string\hustsetassetpath\space
      to the directory containing hust-emblem-color.pdf%
    }%
  \fi
  \ifx\hust@namefile\@empty
    \PackageWarningNoLine{beamerthemehust}{%
      Official wordmark asset not found. Set \string\hustsetassetpath\space
      to the directory containing hust-name-blue.pdf%
    }%
  \fi
  \ifx\hust@emblemwhitefile\@empty
    \PackageWarningNoLine{beamerthemehust}{%
      Inverse emblem asset not found. Set \string\hustsetassetpath\space
      to the directory containing hust-emblem-white.pdf%
    }%
  \fi
  \ifx\hust@namewhitefile\@empty
    \PackageWarningNoLine{beamerthemehust}{%
      Inverse wordmark asset not found. Set \string\hustsetassetpath\space
      to the directory containing hust-name-white.pdf%
    }%
  \fi
}
\AtBeginDocument{\hust@resolveassets}

\newcommand{\hustbrandmark}[1][10mm]{%
  \ifhust@hascustomlogo
    \resizebox{!}{#1}{\hust@customlogo}%
  \else
    \ifx\hust@emblemfile\@empty
      \relax
    \else
      \includegraphics[height=#1]{\hust@emblemfile}%
    \fi
  \fi
}

\newcommand{\hustwordmark}[1][46mm]{%
  \ifx\hust@namefile\@empty
    \relax
  \else
    \includegraphics[width=#1]{\hust@namefile}%
  \fi
}

\newcommand{\hustbrandmarkinverse}[1][10mm]{%
  \ifhust@hascustomlogo
    \resizebox{!}{#1}{\hust@customlogo}%
  \else
    \ifx\hust@emblemwhitefile\@empty
      \hustbrandmark[#1]%
    \else
      \includegraphics[height=#1]{\hust@emblemwhitefile}%
    \fi
  \fi
}

\newcommand{\hustwordmarkinverse}[1][30mm]{%
  \ifx\hust@namewhitefile\@empty
    \relax
  \else
    \includegraphics[width=#1]{\hust@namewhitefile}%
  \fi
}

% Shared physical-page notation, including title and section dividers.
\newcommand{\hust@twodigits}[1]{\ifnum#1<10 0\fi#1}
\newcommand{\hust@displaypagenumber}{%
  \hust@twodigits{\insertpagenumber}%
  \hspace{1mm}{\color{HUSTFrameRule}/}\hspace{1mm}%
  {\color{HUSTMuted}\hust@twodigits{\insertdocumentendpage}}%
}
\newcommand{\hust@displaypagenumberinverse}{%
  {\color{white}\hust@twodigits{\insertpagenumber}}%
  \hspace{1mm}{\color{HUSTBluePale}/}\hspace{1mm}%
  {\color{HUSTBluePale}\hust@twodigits{\insertdocumentendpage}}%
}

% Editorial utilities used by the example and available to users.
\newcommand{\hustkicker}[1]{%
  {\usebeamerfont{hust kicker}\usebeamercolor[fg]{hust kicker}#1}%
}
\newcommand<>{\hustitem}[2]{%
  \item#3%
  {\usebeamerfont{hust item title}%
    \usebeamercolor[fg]{hust item title}#1\par}%
  \ifstrempty{#2}{}{%
    \vspace{0.25mm}%
    {\usebeamerfont{hust item note}%
      \usebeamercolor[fg]{hust item note}#2\par}%
  }%
}
\newcommand{\hustmetric}[2]{%
  \begin{beamercolorbox}[wd=\linewidth,sep=2mm]{hust metric card}%
    {\usebeamerfont{hust metric}\usebeamercolor[fg]{hust metric}#1\par}%
    \vspace{0.5mm}%
    {\usebeamerfont{hust metric label}%
      \usebeamercolor[fg]{hust metric label}#2\par}%
  \end{beamercolorbox}%
}
\newcommand{\hustoutlineitem}[3]{%
  \begin{beamercolorbox}[wd=\linewidth,sep=0pt]{hust outline item}%
    \makebox[14mm][l]{%
      {\usebeamerfont{hust outline number}%
        \usebeamercolor[fg]{hust outline number}#1}}%
    \parbox[t]{\dimexpr\linewidth-14mm\relax}{%
      {\usebeamerfont{hust outline title}#2\par}%
      \vspace{0.35mm}%
      {\usebeamerfont{hust outline note}%
        \usebeamercolor[fg]{hust outline note}#3\par}%
    }%
    \par\vspace{1.4mm}%
  \end{beamercolorbox}%
}
\newcommand{\hustclosing}[3]{%
  \makebox[\linewidth][l]{%
    \hspace*{\dimexpr\hust@margin-\hust@bodymarginleft\relax}%
    \begin{minipage}[c]{\hust@contentwidth}
      \raggedright
      {\usebeamerfont{hust closing title}%
        \usebeamercolor[fg]{hust closing title}#1\par}%
      \vspace{3mm}%
      {\usebeamerfont{hust closing subtitle}%
        \usebeamercolor[fg]{hust closing subtitle}#2\par}%
      \vspace{7mm}%
      {\usebeamerfont{hust closing detail}%
        \usebeamercolor[fg]{hust closing detail}#3\par}%
    \end{minipage}%
  }%
}

% Moloch remains the structural base; HUST replaces its visual templates.
\RequirePackage{beamerthememoloch}
\molochset{
  progressbar=none,
  sectionpage=simple,
  subsectionpage=none
}
\beamer@centeredfalse
\AtBeginDocument{\beamer@centeredfalse}

% Keep the content baseline fixed. Beamer's stock `t` alignment contains
% stretchable top glue, so sparse and dense frames otherwise start at visibly
% different heights.
\define@key{beamerframe}{t}[true]{%
  \beamer@frametopskip=2mm\relax
  \beamer@framebottomskip=0pt plus 1fill\relax
  \beamer@frametopskipautobreak=\beamer@frametopskip\relax
  \beamer@framebottomskipautobreak=\beamer@framebottomskip\relax
}

\usecolortheme{hust}
\usefonttheme{hust}
\useinnertheme{hust}
\useoutertheme{hust}

\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{caption}[numbered]

\mode<all>
\endinput
