% pdfuni.tex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Petr Olsak, 2013

% OPmac converts the Czech/Slovak texts for PDFoutlines to ASCII (removes accents)
% because this is more robust and PDFencoding is incompatible with
% Czech/Slovak alphabet. If you dislike this behavior, you can write:
%
% \input opmac
% \input pdfuni
%
% Now, the \outlines<num> macro generates Czech/Slovak texts with accents.
% The Unicode Encoding is used internally here.
%
% For more information about the implemetation and the posibilities of
% setting more converted characters (not only Czech/Slovak) see the end of
% this file (after \endinput command)


\def\tmp#1#2\end{\if$#2$\else 
   \errmessage {This file is UTF-8 encoded. Use TeX+encTeX or 16bit TeX engine.}\fi}%
\tmp č\end

\ifx\tmpnum\undefined \csname newcount\endcsname\tmpnum \fi
{\lccode`\?=`\\  \lowercase{\gdef\Bslash{?}}}

\edef\pdfunichars{\Bslash()
   ÁáÄäČčĎď ÉéĚěÍíĹĺ ĽľŇňÓóÖö ÔôŔŕŘřŠš ŤťÚúŮůÜü ÝýŽž
}
\def\pdfunicodes {\or00134\or00050\or00051\or                       % \ ()
   00301\or00341\or00304\or00344\or01014\or01015\or01016\or01017\or % ÁáÄäČčĎď
   00311\or00351\or01032\or01033\or00315\or00355\or01071\or01072\or % ÉéĚěÍíĹĺ
   01075\or01076\or01107\or01110\or00323\or00363\or00326\or00366\or % ĽľŇňÓóÖö
   00324\or00364\or01124\or01125\or01130\or01131\or01140\or01141\or % ÔôŔŕŘřŠš
   01144\or01145\or00332\or00372\or01156\or01157\or00334\or00374\or % ŤťÚúŮůÜü
   00335\or00375\or01175\or01176%                                   % ÝýŽž
}
\def\pdfunipre {\def\TeX{TeX}\def\LaTeX{LaTeX}\def~{ }\def\ { }%
   \let\ss\relax \let\l\relax \let\L\relax 
   \let\ae\relax \let\oe\relax \let\AE\relax \let\OE\relax 
   \let\o\relax \let\O\relax \let\i\relax \let\j\relax \let\aa\relax \let\AA\relax 
   \let\S\relax \let\P\relax \let\copyright\relax 
   \let\dots\relax \let\dag\relax \let\ddag\relax 
   \let\clqq\relax \let\crqq\relax \let\flqq\relax \let\frqq\relax 
   \let\promile\relax \let\euro\relax
   \def\\{\Bslash}\let\{\relax \let\}\relax
   \def\${\string^^24}\def\&{\string&}\def\_{\string_}\def\~{\string~}% 
}
\def\pdfunipost {\odef\ss000337 \odef\l001102 \odef\L001101 
   \odef\AE000306 \odef\ae000346 \odef\OE001122 \odef\oe001123 
   \odef\o000370 \odef\O000330 \odef\aa000345 \odef\AA000305
   \odef\i001061 \odef\j002067  \odef\S000247 \odef\P000266
   \odef\copyright000251 \odef\dots040046 \odef\dag040040 \odef\ddag040041 
   \odef\clqq040033 \odef\crqq040034 \odef\flqq000253 \odef\frqq000273 
   \odef\promile040060 \odef\euro040254 \odef\{000173 \odef\}000175 \odef\#000043
}
\def\pdfinitcode{\Bslash376\Bslash377} 
\def\pdfspacecode{\Bslash000\Bslash040}
\def\pdfcircumflexcode{\Bslash000\Bslash136}
\def\pdflowlinecode{\Bslash000\Bslash137}

\def\pdfunidef#1#2{\bgroup \def\tmpa{\noexpand#1}%
   \pdfunipre \edef\tmp{#2}\edef\out{\pdfinitcode}%
   \def\odef##1##2##3##4##5##6##7 
      {\def##1{&\edef\out{\out\Bslash##2##3##4\Bslash##5##6##7}}}\pdfunipost
   \def\insertbslashes ##1##2##3##4##5{\Bslash0##1##2\Bslash##3##4##5}%
   \def\gobbletwowords ##1 ##2 {}%
   \tmpnum=0 
   \loop 
      \sfcode\tmpnum=0 \advance\tmpnum by1
      \ifnum\tmpnum<128 \repeat
   \tmpnum=0
   \expandafter \pdfunidefA \pdfunichars {}%
}
\def\pdfunidefA #1{\if\relax#1\relax%
      \def\next{\let\next= }\afterassignment\pdfunidefB \expandafter\next\tmp\end
   \else \advance\tmpnum by1 \sfcode`#1=\tmpnum \expandafter \pdfunidefA 
   \fi
}
\def\pdfunidefB {%
   \ifcat x\noexpand\next \pdfunidefC
   \else \ifcat .\noexpand\next \pdfunidefC
   \else \ifcat\noexpand\next\space \edef\out{\out\pdfspacecode}%
   \else \ifcat^\noexpand\next \edef\out{\out\pdfcircumflexcode}%
   \else \ifcat_\noexpand\next \edef\out{\out\pdflowlinecode}%
   \else \expandafter\ifx\expandafter&\next
   \fi\fi\fi\fi\fi\fi
   \ifx\next\end \edef\next{\def\tmpa{\out}}\expandafter\egroup\next \else
      \def\next{\let\next= }\afterassignment\pdfunidefB \expandafter\next
   \fi
}
\def\pdfunidefC {\edef\tmp{\expandafter\gobbletwowords\meaning\next}\edef\out{\out\pdfunidefD}}
\def\pdfunidefD{%
   \ifnum\sfcode\expandafter`\tmp=0 \Bslash 000\tmp
   \else
      \expandafter \insertbslashes \ifcase \sfcode\expandafter`\tmp \pdfunicodes 
         \else 000077\fi
   \fi
}
\def\cnvhook #1#2{#2\pdfunidef\tmp\tmp} % the default convertor in OPmac is
                                        % redefined here
\endinput

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

There are only two encodings for PDF strings (used in PDFoutlines, PDFinfo
etc.). First one is PDFDocEncoding which is one-byte encoding, but most
Czech or Slovak characters are missing here. This is the reason, why OPmac
converts these strings to ASCII.

The second encoding is PDFunicode encoding wich is implemented in this file for
Czech and Slovak languages. This encoding is TeX-dicomfortable, because it
looks like

\376\377\000C\000v\000i\001\015\000e\000n\000\355\000\040\000j\000e\000\040
\000z\000\341\000t\001\033\001\176 

This example is real encoding of the string "Cvičení je zátěž". You can see
that this is UTF-16 encoding (two bytes per character) with two starting
bytes FEFF. Moreover, each byte is encoded by three octal digits preceded by
backslash. The only exception is the visible ASCII character encoding: such
a character is encoded by its real byte preceded by \000.

The pdfuni.tex macro implements the command

\pdfunidef\macro{string}

which defines \macro and stores the PDFunicode encoded string into it
(backslashes are with catcode 12 here).

The main conversion bijective function are declared by two macros
\pdfunichars and \pdfunicodes. The number of non-space tokens in
\pdfunichars must be the same as the number of codes in \pdfunicodes.
Each character corresponds to its code in the same order. The codes have
first trailing zero and backlashes missing, ie. the 00301 means the real code
\000\301 or the 01140 means \001\140. The codes are separated by \or
primitive. The characters with internal code < 128 and not listed in 
\pdfunichars are treated as ASCII character, i.e. they are converted 
to \000<char>. Other undefined characters are converted to \000\077,
which is question mark.

User can enlarge the encoding table by \addto command (defined in opmac).
The following example adds the Ë and ë characters:

\addto\pdfunichars{Ëë}  \addto\pdfinicodes{\or00313\or00353}

The converted string is expanded (in the group). The \pdfunipre is preceeded
before such expansion. The re-definition of logos (like \TeX, \LaTeX) is
placed here. The macros which we need to convert to cpecial code (like \ae)
is \let as \relax here. The code of such macro is defined by
\odef\ae<6-digit-code><space> in the \pdfunipost list (\odef means
``octal-def'' here). User can add next macros to \pdfunipre and \pdfunipost
lists by \addto command. Example:

\addto\pdfunipre{\let\endash\relax} \addto\pdfunipost{\odef\endash040023 }

This example makes possibility to convert \endash to \040\023.

Notice for encTeX users: New UTF-8 input codes can be mapped by encTeX to
the control sequences. You can define the PDFunicode output of such
sequences in order to these charaters work properly on PDFoutlines. The
following example solves the character ñ which is not the part of
Czech/Slovak alphabet.

\mubyte \ntilde ^^c3^^b1\endmubyte
\def\ntilde{\~n}
\addto\pdfunipre{\let\ntilde\relax} \addto\pdfunipost{\odef\ntilde000361 }

What you need to know: the ntilde character is U+00F1 with its UTF-8 code
0xC3 0xB1 and with its UTF-16 code \000\361 typed in octal encoding.

For more information see the article ``PDFuni -- akcenty v PDF záložkách'' 
written in Czech (file pdfuni-article.tex,pdf).

--------------

Versions:

May 2013:  Released


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end of the file pdfuni.tex
