% Language definition file (part of polyglossia v2.10 -- 2026/04/18)
%
\ProvidesFile{gloss-tibetan.ldf}[polyglossia: module for Tibetan]
%% Copyright 2013 Elie Roux
%% Under the CC0 license <http://creativecommons.org/publicdomain/zero/1.0/>
%%
%% A good font to make tests is \newfontfamily\tibetanfont{Tibetan Machine Uni}
%%

\PolyglossiaSetup{tibetan}{
  bcp47=bo,
  bcp47-language=bo,
  bcp47-script=Tibt,
  script=Tibetan,
  scripttag=tibt,
  langtag=TIB,
  hyphennames={nohyphenation},
  localnumeral=tibetannumerals,
  Localnumeral=tibetannumerals
  %TODO localalph={xxx@alph,xxx@Alph}
}

% BCP-47 compliant aliases
\setlanguagealias*{tibetan}{bo}

\newif\if@tibetan@numerals
\DeclareKeys[polyglossia/gloss/tibetan]
  {
    numerals.choice:,
    numerals/tibetan.code = \@tibetan@numeralstrue
    	   \SetLanguageKeys{tibetan}{bcp47-extension-u=nu-tibt},
    numerals/arabic.code = \@tibetan@numeralsfalse
           \SetLanguageKeys{tibetan}{bcp47-extension-u=nu-latn},
    numerals.default:n = tibetan
  }

\ifluatex
  \newattribute\xpg@tibteol %
  \directlua{require('polyglossia-tibt')}%
\fi

\def\tibetan@eol{%
  \ifluatex%
    \xpg@tibteol=1\relax%
    \directlua{polyglossia.activate_tibt_eol()}%
  \else%
    \XeTeXlinebreaklocale "bo"%
    \XeTeXlinebreakskip=0pt plus 0.1em% doesn't do much, but doesn't harm I guess...
  \fi%
}

\def\notibetan@eol{%
  \ifluatex%
    \xpg@tibteol=0\relax%
    %\directlua{polyglossia.activate_tibt_eol()}%
  \else%
    \XeTeXlinebreaklocale "en"% en? really?
    \XeTeXlinebreakskip=0pt plus 0pt%
  \fi%
}

% Register default options
\InitializeGlossOptions{tibetan}{numerals=tibetan}

% some are known, but very few
% a few come from "Standardizing Tibetan Terms of Information Technology"
% from the China Tibetology Research Center
\def\captionstibetan{%
   %\def\refname{}%
   \def\abstractname{གནད་བསྡུས།}%
   \def\bibname{དཔེ་ཆའི་ཐོ་གཞུང་།}% or dpe deb kyi re'u mig?
   \def\prefacename{དཔེ་དེབ་ཀྱི་གླེང་བརྗོད།}% or gleng brjod 'god pa or ngo sprod?
   \def\chaptername{ལེའུ་}%
   \def\appendixname{ཞར་བྱུང་།}%
   \def\contentsname{དཀར་ཆག།}%
   %\def\listfigurename{}%
   %\def\listtablename{}%
   \def\indexname{གསུལ་བྱང་།}%
   \def\figurename{པར་རིས་}% or dpe ris?
   \def\tablename{རེའུ་མིག་}%
   %\def\thepart{}%
   \def\partname{ཆ་ཤས་}%
   \def\pagename{ཤོག་}%
   %\def\seename{}%
   %\def\alsoname{}%
   %\def\enclname{}%
   \def\ccname{འདྲ་བཤུས་ལེན་མཁན་}%
   %\def\headtoname{}%
   \def\proofname{བདེན་དཔང་།}% not sure about this one...
   \def\glossaryname{མིང་ཚིག་རེའུ་མིག།}%
}

\def\datetibetan{%   
   \def\tibetan@month{%
     \ifcase\month\or
       ཟླ་དང་པོ\or ཟླ་གཉིས་པ\or ཟླ་གསུམ་པ%
      \or ཟླ་བཞི་པ\or ཟླ་ལྔ་པ\or ཟླ་དྲུག་པ\or ཟླ་བདུན་པ%
      \or ཟླ་བརྒྱད་པ\or ཟླ་དགུ་པ\or ཟླ་བཆུ་པ\or ཟླ་བཆུ་གཅིག་པ\or ཟླ་བཆུ་གཉིས་པ\fi}%      
   \def\tibetan@daynum{%
     \ifcase\day\or དང་པོ\or གཉིས་པ\or གསུམ་པ \or བཞི་པ\or ལྔ་པ\or དྲུག་པ\or བདུན་པ\or བརྒྱད་པ\or དགུ་པ\or བཆུ་པ%
 \or བཆུ་གཅིག་པ\or བཆུ་གཉིས་པ\or བཆུ་གསུམ་པ\or བཆུ་བཞི་པ\or བཆུ་ལྔ་པ\or བཆུ་དྲུག་པ\or བཆུ་བདུན་པ\or བཆུ་བརྒྱད་པ\or བཆུ་དགུ་པ\or ཉི་ཤུ་པ%
 
 \or ཉི་ཤུ་རྩ་གཅིག་པ\or ཉི་ཤུ་རྩ་གཉིས་པ\or ཉི་ཤུ་རྩ་གསུམ་པ\or ཉི་ཤུ་རྩ་བཞི་པ\or ཉི་ཤུ་རྩ་ལྔ་པ\or ཉི་ཤུ་རྩ་དྲུག་པ\or ཉི་ཤུ་རྩ་བདུན་པ\or ཉི་ཤུ་རྩ་བརྒྱད་པ\or ཉི་ཤུ་རྩ་དགུ་པ\or སུམ་ཆུ་པ%
 \or སུམ་ཆུ་སོ་གཅིག་པ\fi}%      
   % As we use gregorian calendar, it's better to stick with spyi lo
   %\newcount\tibetan@year%
   %\tibetan@year=\year%
   %\advance\tibetan@year by 127% this is bod rgyal lo, the most common, but there are others...
   % I'm not sure the / character is in most tibetan fonts
   %\def\today{\tibetannumber\day /\tibetannumber\day /\tibetannumber\year}%
   \def\today{\tibetannumber\day །\tibetannumber\month །\tibetannumber\year}%
   % this is more litterate, but longer
   %\def\today{སྤྱི་ལོ་\tibetannumber\year ་སྤྱི་\tibetan@month{}་ད་རེས་\tibetan@daynum{}།}%  
}

\def\tibetandigits#1{\expandafter\@tibetan@digits #1@}

\def\@tibetan@digits#1{%
  \ifx @#1% then terminate
  \else
    \ifx0#1༠\else\ifx1#1༡\else\ifx2#1༢\else\ifx3#1༣\else\ifx4#1༤\else\ifx5#1༥\else\ifx6#1༦\else\ifx7#1༧\else\ifx8#1༨\else\ifx9#1༩\else#1\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
    \expandafter\@tibetan@digits
  \fi
}

\def\tibetannumber#1{%
  \if@tibetan@numerals
    \tibetandigits{\number#1}%
    %%{\protect\addfontfeature{Mapping=tibetandigits}\protect\number#1}
  \else
    \number#1%
    %%{\protect\reset@font\number#1}
  \fi}

\def\@tibetanalph#1{%
    \ifcase#1\or ཀ\or ཁ\or ག\or ང\or ཅ\or ཆ\or
           ཇ\or ཉ\or ཏ\or ཐ\or ད\or ན\or པ\or ཕ\or
           བ\or མ\or ཙ\or ཚ\or ཛ\or ཝ\or ཞ\or ཟ\or
           འ\or ཡ\or ར\or ལ\or ཤ\or ས\or ཧ\or ཨ
    \else%
        \xpg@ill@value{#1}{@tibetanalph}%
    \fi%
}
 
\def\tibetanAlph#1{%
    \expandafter\@tibetanAlph\csname c@#1\endcsname%
}

\def\@tibetanAlph#1{%
    \ifcase#1\or ཀ\or ཁ\or ག\or ང\or ཅ\or ཆ\or ཇ\or
          ཉ\or ཏ\or ཐ\or ད\or ན\or པ\or ཕ\or བ\or མ\or ཙ\or
           ཚ\or ཛ\or ཝ\or ཞ\or ཟ\or འ\or ཡ\or ར\or ལ\or ཤ\or
            ས\or ཧ\or ཨ%
    \else%
       \xpg@ill@value{#1}{@tibetanalph}%
    \fi%
}

\newcommand{\tibetanumerals}[2]{\tibetannumber{#2}}

\def\tibetan@numbers{%
   \if@tibetan@numerals
     \let\@alph\@tibetanalph%
     \let\@Alph\@tibetanAlph%
   \fi
}

\def\notibetan@numbers{%
  \let\@alph\latin@alph%
  \let\@Alph\latin@Alph%
}

% Store original definition
\let\xpg@orig@arabic\@arabic

\def\tibetan@globalnumbers{%
   \let\@arabic\tibetannumber%
   \renewcommand{\thefootnote}{\localnumeral*{footnote}}%
}

\def\notibetan@globalnumbers{%
   \let\@arabic\xpg@orig@arabic%
}

\def\noextras@tibetan{%
   \notibetan@eol%
   \ifcsname xpg@orig@baselinestretch\endcsname%
       \renewcommand{\baselinestretch}{\xpg@orig@baselinestretch}%
   \fi%
}

\def\inlineextras@tibetan{%
   \xdef\xpg@orig@baselinestretch{%
       \ifcsname baselinestretch\endcsname
           \baselinestretch
        \else
           1%
        \fi%
   }%
   \renewcommand{\baselinestretch}{1.2}%
   \tibetan@eol%
}

\def\blockextras@tibetan{%
   \xdef\xpg@orig@baselinestretch{%
        \ifcsname baselinestretch\endcsname
            \baselinestretch
        \else
           1%
        \fi%
   }%
   \renewcommand{\baselinestretch}{1.2}%
   \tibetan@eol%
}

\endinput
