% Copyright (c) 2026 Simon Rubinstein-Salzedo.
% Distributed under the LaTeX Project Public License, version 1.3c or later.
% Maintenance status: maintained. Current Maintainer: Simon Rubinstein-Salzedo.
% License: https://www.latex-project.org/lppl.txt
% See README.md for the list of files and attribution.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% combgames-symbols.sty
% AUTHOR: Simon Rubinstein-Salzedo
%         based on combgames.sty by Raymond Chen, 
%         David Wolfe, and Aaron Siegel
% PLEASE SUGGEST IMPROVEMENTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{combgames-symbols}[2026/10/28 v1.0 Combinatorial game theory symbols and notation]

\RequirePackage{iftex}
\RequirePackage{expl3}
\RequirePackage{xparse}
\RequirePackage{amsmath,amssymb}


% ---------- pdfLaTeX path: use the combgames-symbols.tfm + combgames-symbols.map you installed ----------
\ifPDFTeX
% Symbol font family (assumes combgames-symbols.tfm + combgames-symbols.map are installed & enabled)
\pdfmapfile{=combgames-symbols.map}
\DeclareFontFamily{U}{cgtsym}{}
\DeclareFontShape{U}{cgtsym}{m}{n}{<-> combgames-symbols}{}
\DeclareSymbolFont{cgtsym}{U}{cgtsym}{m}{n}
% Use the same font in all math versions (display/text/script/scriptscript)
\SetSymbolFont{cgtsym}{normal}{U}{cgtsym}{m}{n}
\SetSymbolFont{cgtsym}{bold}{U}{cgtsym}{m}{n}

% Slot numbers = order in combgames-symbols.enc (0-based). Keep these in sync with your .enc.
\DeclareMathSymbol{\cgtripleup}   {\mathord}{cgtsym}{0}
\DeclareMathSymbol{\cgtripledown} {\mathord}{cgtsym}{1}
\DeclareMathSymbol{\cgquadup}   {\mathord}{cgtsym}{2}
\DeclareMathSymbol{\cgquaddown} {\mathord}{cgtsym}{3}
\DeclareMathSymbol{\cggfuz}   {\mathrel}{cgtsym}{4}
\DeclareMathSymbol{\cglfuz}   {\mathrel}{cgtsym}{5}
\DeclareMathSymbol{\cgfuzzy}   {\mathrel}{cgtsym}{6}
\DeclareMathSymbol{\cgtiny}   {\mathord}{cgtsym}{7}
\DeclareMathSymbol{\cgminy}   {\mathord}{cgtsym}{8}
\DeclareMathSymbol{\cgupsum}   {\mathbin}{cgtsym}{9}
\DeclareMathSymbol{\cgdownsum}   {\mathbin}{cgtsym}{10}
\DeclareMathSymbol{\cgko}   {\mathord}{cgtsym}{11}
\DeclareMathSymbol{\cgKo}   {\mathord}{cgtsym}{12}
\DeclareMathSymbol{\cgkobar}   {\mathord}{cgtsym}{13}
\DeclareMathSymbol{\cgKobar}   {\mathord}{cgtsym}{14}
\DeclareMathSymbol{\cgfarstar}   {\mathord}{cgtsym}{15}
\DeclareMathSymbol{\cgloony}      {\mathord}{cgtsym}{16}
\DeclareMathSymbol{\cgsunny}      {\mathord}{cgtsym}{17}

% ---------- Xe/Lua path: use the OTF directly via fontspec ----------
\else
\RequirePackage{fontspec}
% Load the bundled font by filename; no system font installation is needed.
\newfontfamily\cgtsymfont{combgames-symbols.otf}[Scale=1]

% Helper: pull a PUA codepoint from \cgtsymfont, wrapped in \text so it sizes per math style
\newcommand\cgtsymslot[1]{\text{\cgtsymfont\symbol{#1}}}

% Map your glyph codepoints (from your FontForge script)
% E100 … E104 here; change if you used different values.
\newcommand{\cgtripleup}{\mathord{\cgtsymslot{"E100}}}
\newcommand{\cgtripledown}{\mathord{\cgtsymslot{"E101}}}
\newcommand{\cgquadup}{\mathord{\cgtsymslot{"E102}}}
\newcommand{\cgquaddown}{\mathord{\cgtsymslot{"E103}}}
\newcommand{\cggfuz}{\mathrel{\cgtsymslot{"E104}}}
\newcommand{\cglfuz}{\mathrel{\cgtsymslot{"E105}}}
\newcommand{\cgfuzzy}{\mathrel{\cgtsymslot{"E106}}}
\newcommand{\cgtiny}{\mathord{\cgtsymslot{"E107}}}
\newcommand{\cgminy}{\mathord{\cgtsymslot{"E108}}}
\newcommand{\cgupsum}{\mathbin{\cgtsymslot{"E109}}}
\newcommand{\cgdownsum}{\mathbin{\cgtsymslot{"E110}}}
\newcommand{\cgko}{\mathord{\cgtsymslot{"E111}}}
\newcommand{\cgKo}{\mathord{\cgtsymslot{"E112}}}
\newcommand{\cgkobar}{\mathord{\cgtsymslot{"E113}}}
\newcommand{\cgKobar}{\mathord{\cgtsymslot{"E114}}}
\newcommand{\cgfarstar}{\mathord{\cgtsymslot{"E115}}}
\newcommand{\cgloony}{\mathord{\cgtsymslot{"E116}}}
\newcommand{\cgsunny}{\mathord{\cgtsymslot{"E117}}}
\fi

\newcommand{\cgup}{\mathord{\uparrow}}
\newcommand{\cgdown}{\mathord{\downarrow}}
\newcommand{\cgdoubleup}{\mathord{\Uparrow}}
\newcommand{\cgdoubledown}{\mathord{\Downarrow}}
\newcommand{\cgneg}{\mathord{-}}
\newcommand{\cgstar}{\mathord{\ast}}
\newcommand{\cgnmultiply}{\mathbin{\cdot}}
% \newcommand{\cgbow}{\mathrel{\bowtie}}
\newcommand{\cgbow}{\mathbin{\Join}}

\newcommand\cgups[1]{%
	\ifcase#1\relax%
	0\or\cgup\or\cgdoubleup\or\cgtripleup\or\cgquadup%
	\else%
	\ifnum#1<\z@\cg@updownerror\else{#1}\cdot\cgup\fi%
	\fi%
}
\newcommand\cgdowns[1]{%
	\ifcase#1\relax%
	0\or\cgdown\or\cgdoubledown\or\cgtripledown\or\cgquaddown%
	\else%
	\ifnum#1<\z@\cg@updownerror\else{#1}\cdot\cgdown\fi%
	\fi%
}
\newcommand\cg@updownerror{\errmessage{Argument to cgups and cgdowns must be nonnegative.}}

\newmuskip\cgslashmuskip
\cgslashmuskip=-2mu
\newlength\cgslashextension
\setlength\cgslashextension{1.5pt}

\def\cg@slashskip{\mskip\cgslashmuskip}

\newif\ifcg@star
\newif\ifcg@building\cg@buildingfalse
\newif\ifcg@drawbars\cg@drawbarsfalse
\newbox\cg@gamebox\setbox\cg@gamebox=\null
\newdimen\cg@extension
\newcounter{cg@readbarctr}

\newcommand\combgame{%
	\begingroup%
	\catcode`\|=\active%
	\cg@definebars%
	\@ifstar{\cg@startrue\cg@combgame}{\cg@starfalse\cg@combgame}%
}

\def\cg@combgame#1{%
	\mathpalette\cg@buildgame{#1}%
	\endgroup%
}

\def\cg@buildgame#1#2{%
	\begingroup%
	\cg@buildingtrue%
	\ifx#1\displaystyle%
	\ifcg@star%
	\cg@extension=0pt%
	\else%
	\cg@extension=\the\cgslashextension%
	\fi%
	\fi%
	\cg@drawbarsfalse%
	\setbox\z@\hbox{$#1#2$}%
	\ht\cg@gamebox\ht\z@%
	\dp\cg@gamebox\dp\z@%
	\cg@drawbarstrue%
	#1{#2}%
	\cg@buildingfalse%
	\endgroup%
}

\begingroup
\catcode`\|=\active
\gdef\cg@definebars{%
	\def|{\setcounter{cg@readbarctr}{1}\cg@readbars}%
	\def\{{\left\lbrace}
	\def\}{\right\rbrace}
	\def\<{\left\langle}
	\def\>{\right\rangle}
}
\gdef\cg@readbars{%
	\@ifnextchar|{\stepcounter{cg@readbarctr}\cg@readnextbar}{\cgslashes{\thecg@readbarctr}}%
}
\gdef\cg@readnextbar|{\cg@readbars}
\endgroup

\newcommand\cgslash{\cgslashes{1}}
\newcommand\cgsslash{\cgslashes{2}}

\newcommand\cgslashes[2][]{%
	\ifcg@building
	\ifcg@drawbars
	\mathrel
	{\hbox{$%
			%% This part is deadly because \left and \right create implicit
			%% grouping.
			\toks\z@{}%
			\cg@loopmbars{#2}{\global\toks\z@\expandafter{\the\toks\z@\cg@mbarshelper}}%
			\def\cg@mbarshelper{\left\vert\mskip\cgslashmuskip}\the\toks\z@
			\mskip-\cgslashmuskip
			\@tempcnta#2\relax \advance\@tempcnta\m@ne
			\raise\@tempcnta\cg@extension\copy\cg@gamebox
			\lower\@tempcnta\cg@extension\copy\cg@gamebox
			\gdef\cg@mbarshelper{\right.}\the\toks\z@\n@space$}}%
	\else\mathrel\vert\fi
	\else
	\cg@badbar
	\fi
}

\def\cg@loopmbars#1#2{
	\@tempcnta#1\relax
	\loop
	#2%
	\advance\@tempcnta\m@ne
	\ifnum\@tempcnta>\z@
	\repeat
}

\def\cg@badbar{\errmessage{cgslash commands may only be used inside a combgame command.}}

% Center the tiny symbol on the current math axis in every math style.
\let\cgtOriginalTiny\cgtiny
\renewcommand{\cgtiny}{%
  \mathord{\mathpalette\cgtCenterTiny{}}%
}
\newcommand{\cgtCenterTiny}[2]{%
  \vcenter{\hbox{$\m@th#1\cgtOriginalTiny$}}%
}
