summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/csplain/opmac/pdfuni.tex
blob: a2c35b4c5a02ab547c0cc279f6f613b3afa46bd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
% 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)

\ifx\XeTeXversion\undefined\else  \def\cnvhook#1{}\endinput \fi

\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