\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{emojinames}[2026/07/11 v1.1 Unified emoji macros]

\RequirePackage{etoolbox}
\RequirePackage{kvoptions}

% ---------- Options ----------
\SetupKeyvalOptions{
  family=se,
  prefix=se@
}

% style=color (default) or bw
\DeclareStringOption[color]{style}
\ProcessKeyvalOptions*

% ---------- Core macros ----------
\newcommand{\emojiDefine}[2]{%
  \expandafter\def\csname emoji@#1\endcsname{#2}%
}

% default \emoji (may be patched later once EmojiFont exists)
\newcommand{\emoji}[1]{%
  \ifcsdef{emoji@#1}%
    {\csname emoji@#1\endcsname}%
    {?}%
}

% ---------- Engine detection ----------
\newif\ifse@luatex
\newif\ifse@xetex
\newif\ifse@pdftex

\ifdefined\directlua
  \se@luatextrue
\fi
\ifdefined\XeTeXrevision
  \se@xetextrue
\fi
\ifx\pdfoutput\undefined\else
  \ifnum\pdfoutput>0\relax
    \se@pdftextrue
  \fi
\fi

% ---------- Font setup for LuaLaTeX ----------
\ifse@luatex
  \RequirePackage{fontspec}
  \newfontfamily\EmojiFont{Segoe UI Emoji} % default bw

  % If style=color: use TwemojiMozilla (COLR/CPAL vector format - works reliably)
  \ifdefstring{\se@style}{color}{%
    \IfFontExistsTF{Twemoji Mozilla}{%
      \renewfontfamily\EmojiFont{Twemoji Mozilla}[Renderer=HarfBuzz]%
    }{%
      \PackageWarning{emojinames}{TwemojiMozilla font not found, trying Segoe UI Emoji with color}%
      \renewfontfamily\EmojiFont{Segoe UI Emoji}[Renderer=HarfBuzz]%
    }%
  }{}

  % Patch \emoji to always use EmojiFont
  \renewcommand{\emoji}[1]{%
    \ifcsdef{emoji@#1}%
      {{\EmojiFont \csname emoji@#1\endcsname}}%
      {?}%
  }
\fi

% ---------- Font setup for XeLaTeX ----------
\ifse@xetex
  \RequirePackage{fontspec}
  \newfontfamily\EmojiFont{Segoe UI Emoji} % default bw

  % If style=color: use TwemojiMozilla (COLR/CPAL vector format)
  % Note: XeTeX color emoji support is limited compared to LuaTeX
  \ifdefstring{\se@style}{color}{%
    \IfFontExistsTF{Twemoji Mozilla}{%
      \renewfontfamily\EmojiFont{Twemoji Mozilla}[Renderer=HarfBuzz]%
    }{%
      \PackageWarning{emojinames}{TwemojiMozilla font not found, color emoji may not render}%
    }%
  }{}

  \renewcommand{\emoji}[1]{%
    \ifcsdef{emoji@#1}%
      {{\EmojiFont \csname emoji@#1\endcsname}}%
      {?}%
  }
\fi

% pdfLaTeX: leave \emoji as plain Unicode, user gets whatever their font/viewer supports
% (no fontspec here)

% ---------- Load generated map ----------
\input{emojinames-map.tex}
