\documentclass[a4paper]{scrartcl}

% Load font packages depending on engine:
\ifdefined\luatexversion 
  \usepackage{fontspec}
\fi
\ifdefined\XeTeXrevision 
  \usepackage{fontspec}
\fi
\ifdefined \UTFencname % set by "fontspec.sty"
  % Latin Modern misses most Greek characters.
  \setmainfont{DejaVu Serif}
  \setsansfont{DejaVu Sans}
\else
  \usepackage[LGR,T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{lmodern}
  % \usepackage{kerkis} % missing glyphs (Qoppa, in sans serif also koppa, Koppa)  
  % \usepackage{dejavu} missing glyphs (qoppa, Qoppa, Sampi)
  % \usepackage{epigrafica} # many missing glyphs
  % \usepackage{gentium} % missing glyphs (Qoppa, Sampi)
  % \usepackage{gfsdidot} % sampi in place of qoppa, missing sampi Sampi
  % \usepackage{gfsartemisia} % sampi in place of qoppa, missing sampi Sampi
  % \usepackage{libertinegc}
  % \usepackage{noto}
  % \usepackage{OldStandard}
  % \usepackage{roboto}
\fi


% Greek script support with textalpha (greek-fontec) or babel-greek:

% \usepackage{textalpha}
\usepackage[greek,english]{babel}

% Save the unpatched version
\let\origgreeknumeral\greeknumeral
\let\origGreeknumeral\Greeknumeral

% uncomment, if you prefer Qoppa for Koppa
% \let\textKoppa\textQoppa
% \let\textkoppa\textqoppa


% Hyperref 
% default PDFstring encoding is PD1 without Greek support:
% \usepackage[unicode]{hyperref}
% With option unicode, there is partial Greek support (no polytonic)
\usepackage[unicode]{hyperref}


\DeclareTextCommand{\greekscript}{PU}{}
\DeclareTextCommand{\ensuregreek}{PU}[1]{#1}
% Greek LICR names from "greek-fontenc"
\DeclareTextCommand{\textdexiakeraia}{PU}{\83\164}% U+0374
\DeclareTextCommand{\textaristerikeraia}{PU}{\83\165}% U+0375
\DeclareTextCommand{\textQoppa}{PU}{\83\330}% U+03D8
\DeclareTextCommand{\textqoppa}{PU}{\83\331}% U+03D9
\DeclareTextCommand{\textStigma}{PU}{\83\332}% U+03DA
\DeclareTextCommand{\textstigma}{PU}{\83\333}% U+03DB
\DeclareTextCommand{\textDigamma}{PU}{\83\334}% U+03DC
\DeclareTextCommand{\textdigamma}{PU}{\83\335}% U+03DD
\DeclareTextCommand{\textKoppa}{PU}{\83\336}% U+03DE
\DeclareTextCommand{\textkoppa}{PU}{\83\337}% U+03DF
\DeclareTextCommand{\textSampi}{PU}{\83\340}% U+03E0
\DeclareTextCommand{\textsampi}{PU}{\83\341}% U+03E1
% TODO: support polytonic Greek (Greek Extended Unicode block)



\begin{document}

% \textuppercase{Grüße}

% \selectlanguage{greek}
% \selectlanguage{english}


\section{Test Hyperref-safe implementation of Greek numerals}

Every number is set in 4 versions:

1: lowercase Babel, 
2: lowercase Hyperref-patched,

3: uppercase Babel,
4: uppercase Hyperref-patched,


\DeclareRobustCommand{\numeraltest}[1]{
  \origgreeknumeral{#1}
  \ensuregreek{\greeknumeral{#1}}\ 
  \origGreeknumeral{#1}
  \ensuregreek{\Greeknumeral{#1}}
}


\noindent single numerals

1-5
\numeraltest{1}
\numeraltest{2}
\numeraltest{3}
\numeraltest{4}
\numeraltest{5}

6-9
\numeraltest{6}
\numeraltest{7}
\numeraltest{8}
\numeraltest{9}

10-50
\numeraltest{10}
\numeraltest{20}
\numeraltest{30}
\numeraltest{40}
\numeraltest{50}

60-90
\numeraltest{60}
\numeraltest{70}
\numeraltest{80}
\numeraltest{90}

100-500
\numeraltest{100}
\numeraltest{200}
\numeraltest{300}
\numeraltest{400}
\numeraltest{500}

600-900
\numeraltest{600}
\numeraltest{700}
\numeraltest{800}
\numeraltest{900}

1000-5000
\numeraltest{1000}
\numeraltest{2000}
\numeraltest{3000}
\numeraltest{4000}
\numeraltest{5000}

6000-9000
\numeraltest{6000}
\numeraltest{7000}
\numeraltest{8000}
\numeraltest{9000}

10 000 - 900 000
\numeraltest{10000}
\numeraltest{20000}
\numeraltest{800000}
\numeraltest{900000}

\noindent compound numbers

123-323
\numeraltest{123}
\numeraltest{223}
\numeraltest{323}

445-645
\numeraltest{445}
\numeraltest{545}
\numeraltest{645}

767-967
\numeraltest{767}
\numeraltest{867}
\numeraltest{967}

967021 \numeraltest{967021}

\noindent outside of bounds

Negative and zero
\greeknumeral{-1}
% \Greeknumeral{-1}
\greeknumeral{0}
%\Greeknumeral{0}

Too large:
\greeknumeral{1000000}
% \Greeknumeral{1000000}

\paragraph{Discussion}

The new implementation gives identical results.

\section{Greek numerals in PDF bookmarks}

\section{
  \numeraltest{1}
  \numeraltest{90}
  \numeraltest{200}
}

\section{
  \numeraltest{1000}
  \numeraltest{90000}
  \numeraltest{200000}
}
\section{
  \numeraltest{24}
  \numeraltest{96}
}

\section{
  \numeraltest{96}
  \numeraltest{263}
}

\section{
  \numeraltest{4695}
}

\section{Original numeral: 
  \greekscript{\origgreeknumeral{1293}}
  % \greekscript{\origGreeknumeral{1293}}
}

Babels original lower case version works fine in text. However, use in a
section heading results in spurious numeral marks (dexia keraia) in the
PDF bookmark. and the warning
\begin{verbatim}
  Package hyperref Warning: Token not allowed in a PDF string (Unicode):
  (hyperref)                removing `\@ifnextchar' on input line 353.
\end{verbatim}

The upper case version fails because MakeUpercase is not defined for
PDFstrings.

\end{document},
