%D \module
%D   [       file=spec-tst,
%D        version=2002.12.08,
%D          title=\CONTEXT\ \PDF\ Macros,
%D       subtitle=Special Test Macro
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\ifcase \pdfoutput \endinput \fi

\unprotect

%D The conversions comes down to (for the sake fo speed the
%D implementation combines steps):
%D
%D \startitemize
%D \item we expand the \UTF\ sequences into \type {\unicodechar}'s
%D \item spaces become character 255's (so that they are not
%D      gobbled in argument fetching
%D \item normal \ASCII\ chars are unchanged
%D \item \par's and alike are converted to \type {\unicodechar}'s
%D \stopitemize
%D
%D This happens by expansion; next we convert the resulting
%D sequence by interpreting the stream.

\long\def\sanitizePDFuniencoding#1\to#2%
  {\enablePDFunicrlf
   \let\unicodechar\relax % prevent further expansion
   \retainlccodes\lccode32=255 % slooow
   \lowercasestring\PDFunicodetrigger#1\to#2%
   \edef#2{\expandafter\doPDFuni#2\empty\empty}} % slooow

%D Handling of empty lines:

\bgroup
\catcode`\^^M=\@@active
\gdef\enablePDFunicrlf%
  {\def\\{\unicodechar{13}}%
   \def\par{\\\\}%
   \catcode`\^^M=\@@active%
   \let^^M=\\}
\egroup

%D Conversion to 16 bit \UNICODE:

\def\PDFunicodechar#1%
  {\@EA\lchexnumbers\@EA{\number\utfdiv{#1}}%
   \@EA\lchexnumbers\@EA{\number\utfmod{#1}}}

\def\PDFunicodetrigger
  {\unicodechar{65279}}

%D The postprocessor:

\def\doPDFuni#1%
  {\ifx#1\relax
     \@EA\dodoPDFuni
   \else\ifx#1\empty
     % quit
   \else
     \@EAEAEA\nodoPDFuni
   \fi\fi#1}

\def\nodoPDFuni#1%
  {\PDFunicodechar{\ifnum`#1=255 32\else`#1\fi}\doPDFuni}

\def\dodoPDFuni#1#2%
  {\PDFunicodechar{#2}\doPDFuni}

\def\sanitizePDFencoding
  {\doifelse\currentregime{utf}%
     {\PDFunicodetrue\sanitizePDFuniencoding}\sanitizePDFdocencoding}

% pdftex specific

\def\doPDFinsertbookmark#1#2#3#4#5% level sublevels text page open=1
  {\bgroup
   \sanitizePDFencoding#3\to\bookmarktext % uses scratchcounter
   \stripstring\bookmarktext
   \doPDFbookmark level #1 n #2 text {\bookmarktext} page {#4} open #5
   \egroup}

\def\doPDFbookmark level #1 n #2 text #3 page #4 open #5%
  {\doPDFgetpagereference{#4}\PDFobjectreference
   \pdfoutline
     user {<</S /GoTo /D [\PDFobjectreference\space\PDFpageviewwrd]>>}%
     \ifcase#2 \else count \ifcase#5-\fi#2 \fi
     {\ifPDFunicode<#3>\else#3\fi}}

\def\doPDFsetupidentity#1#2#3#4#5#6%
  {\bgroup
   \enablePDFdocencoding
   \sanitizePDFencoding#1\to\idtitle  \stripstring\idtitle
   \sanitizePDFencoding#2\to\idsubject\stripstring\idsubject
   \sanitizePDFencoding#3\to\idauthor \stripstring\idauthor
   \sanitizePDFencoding#4\to\idcreator\stripstring\idcreator
   \sanitizePDFencoding#6\to\idkeyword\stripstring\idkeyword
   \expanded{\doPDFaddtoinfo
     {/Title   \ifPDFunicode<\idtitle  >\else(\idtitle  )\fi
      /Subject \ifPDFunicode<\idsubject>\else(\idsubject)\fi
      /Author  \ifPDFunicode<\idauthor >\else(\idauthor )\fi
      /Creator \ifPDFunicode<\idcreator>\else(\idcreator)\fi
      /ModDate (#4)
      /ID (\jobname.#5) % needed for pdf/x
      /Keywords \ifPDFunicode<\idkeyword>\else(\idkeyword)\fi}}%
   \egroup}

\protect

\doifnotmode{demo}{\endinput}

% \input spec-tst.tex

\mainlanguage[vn]
\enableregime[utf]
\usetypescript[all][computer-modern][t5]

\setupinteraction
  [state=start,
   title={Thử tiếng Việt},
   author={Tác Văn Giả},
   keyword={Thử tiếng Việt}]

\placebookmarks[chapter,section,subsection]

\starttext

\placelist[chapter,section,subsection][alternative=c]

\chapter{Thử tiếng Việt}

\section   {Mục thứ nhất}
\subsection{Mục nhỏ thứ nhất} Thử tiếng Việt
\subsection{Mục nhỏ thứ hai}    Thử tiếng Việt

\section   {Mục thứ hai}
\subsection{Mục nhỏ thứ nhất} Thử tiếng Việt
\subsection{Mục nhỏ thứ hai}    Thử tiếng Việt

\section   {Mục thứ ba}
\subsection{Mục nhỏ thứ nhất} Thử tiếng Việt
\subsection{Mục nhỏ thứ hai}    Thử tiếng Việt

\stoptext

