| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \pdfunidef {PDFunicode strings for outlines <2026-04-21>} % preloaded in format |
| 4 |
|
| 5 |
\_doc ----------------------------- |
| 6 |
\`\pdfunidef``\macro{<text>}` defines `\macro` as <text> converted to |
| 7 |
Big Endian UTF-16 and enclosed to \code{<>}. Example of usage: |
| 8 |
`\pdfunidef\infoauthor{Petr Olšák} \pdfinfo{/Author \infoauthor}`.\nl |
| 9 |
\^`\pdfunidef` does more things than only converting to hexadecimal PDF string. |
| 10 |
The <text> can be scanned in verbatim mode (it is true because \^`\_Xtoc` |
| 11 |
reads the <text> in verbatim mode). First `\edef` do |
| 12 |
`\unexpanded\ea{\scantextokens{..}}` and second `\edef` expands the parameter |
| 13 |
according to current values on selected macros from `\_regoul`. Then |
| 14 |
\`\_removeoutmath` converts `..$x^2$..` to `..x^2..`, i.e removes dollars. |
| 15 |
Then \`\_removeoutbraces` converts `..{x}..` to `..x..`. |
| 16 |
Finally, the <text> is detokenized, and \^`\pdfstring` is applied.\nl |
| 17 |
Characters for quotes (and separators for quotes) are activated by first |
| 18 |
`\_scatextokens` and they are defined as the same non-active characters. |
| 19 |
But `\_regoul` can change this definition. |
| 20 |
\_cod ----------------------------- |
| 21 |
|
| 22 |
\_def\_pdfunidef#1#2{% |
| 23 |
\_begingroup |
| 24 |
\_catcodetable\_optexcatcodes \_adef"{"}\_adef'{'}% |
| 25 |
\_the\_regoul \_relax % \_regmacro alternatives of logos etc. |
| 26 |
\_ifx\_savedttchar\_undefined \_def#1{\_unexpanded\_ea{\_scantextokens{#2}}}% |
| 27 |
\_else \_lccode`\;=\_savedttchar \_lowercase{\_prepinverb#1;}{#2}\fi |
| 28 |
\_edef#1{#1}% |
| 29 |
\_escapechar=-1 |
| 30 |
\_edef#1{#1\_empty}% |
| 31 |
\_escapechar=`\\ |
| 32 |
\_ea\_edef \_ea#1\_ea{\_ea\_removeoutmath #1$\_fin$}% $x$ -> x |
| 33 |
\_ea\_edef \_ea#1\_ea{\_ea\_removeoutbraces #1{\_fin}}% {x} -> x |
| 34 |
\_edef#1{\_detokenize\_ea{#1}}% |
| 35 |
\_ea |
| 36 |
\_endgroup |
| 37 |
\_ea\_edef\_ea#1\_ea{\_pdfstring\_ea{#1}} |
| 38 |
} |
| 39 |
\_def\_removeoutbraces #1#{#1\_removeoutbracesA} |
| 40 |
\_def\_removeoutbracesA #1{\_ifx\_fin#1\_else #1\_ea\_removeoutbraces\_fi} |
| 41 |
\_def\_removeoutmath #1$#2${#1\_ifx\_fin#2\_else #2\_ea\_removeoutmath\_fi} |
| 42 |
|
| 43 |
\_doc ----------------------------- |
| 44 |
The \`\_prepinverb``<macro><separator>{<text>}`, |
| 45 |
e.g.\ `\_prepinverb\tmpb|{aaa |bbb| cccc |dd| ee}` |
| 46 |
does `\def\tmpb{<us>{aaa }bbb<us>{ cccc }dd<us>{ ee}}` where |
| 47 |
<us> is `\unexpanded\scantextokens`. It means that in-line verbatim |
| 48 |
are not argument of `\scantextoken`. First `\edef\tmpb` tokenizes again |
| 49 |
the <text> but not the parts which were in the the in-line verbatim. |
| 50 |
\_cod ----------------------------- |
| 51 |
|
| 52 |
\_def\_prepinverb#1#2#3{\_def#1{}% |
| 53 |
\_def\_dotmpb ##1#2##2{\_addto#1{\_unexpanded\_ea{\_scantextokens{##1}}}% |
| 54 |
\_ifx\_fin##2\_else\_ea\_dotmpbA\_ea##2\_fi}% |
| 55 |
\_def\_dotmpbA ##1#2{\_addto#1{##1}\_dotmpb}% |
| 56 |
\_dotmpb#3#2\_fin |
| 57 |
} |
| 58 |
|
| 59 |
\_doc ----------------------------- |
| 60 |
The \^`\regmacro` is used in order to set the values of macros |
| 61 |
`\em`, `\rm`, `\bf`, `\it`, `\bi`, `\tt`, `\/` and `~` to values usable in |
| 62 |
PDF outlines. |
| 63 |
\_cod ----------------------------- |
| 64 |
|
| 65 |
\_regmacro {}{}{\_let\em=\_empty \_let\rm=\_empty \_let\bf=\_empty |
| 66 |
\_let\it=\_empty \_let\bi=\_empty \_let\tt=\_empty \_let\/=\_empty |
| 67 |
\_let~=\_space |
| 68 |
} |
| 69 |
\public \pdfunidef ; |
| 70 |
|
| 71 |
\_endcode % -------------------------------- |
| 72 |
|
| 73 |
There are only two encodings for PDF strings (used in PDFoutlines, PDFinfo, |
| 74 |
etc.). The first one is PDFDocEncoding which is single-byte encoding, but it |
| 75 |
misses most international characters. |
| 76 |
|
| 77 |
The second encoding is Big Endian UTF-16 which is implemented in this file. It |
| 78 |
encodes a single character in either two or four bytes. |
| 79 |
This encoding is \TeX/-discomfortable because it looks like |
| 80 |
|
| 81 |
\begtt |
| 82 |
<FEFF 0043 0076 0069 010D 0065 006E 00ED 0020 006A 0065 0020 007A 00E1 0074 |
| 83 |
011B 017E 0020 0061 0020 0078 2208 D835DD44> |
| 84 |
\endtt |
| 85 |
|
| 86 |
This example shows a hexadecimal PDF string (enclosed in \code{<>} as opposed |
| 87 |
to the literal PDF string enclosed in `()`). In these strings each byte is |
| 88 |
represented by two hexadecimal characters (`0-9`, `A-F`). You can tell the |
| 89 |
encoding is UTF-16BE, because it starts with \"Byte order mark" `FEFF`. Each |
| 90 |
unicode character is then encoded in one or two byte pairs. The example string |
| 91 |
corresponds to the text \"Cvičení je zátěž a ${\rm x} ∈ 𝕄$". Notice the 4 bytes |
| 92 |
for the last character, $𝕄$. (Even the whitespace would be OK in a PDF file, |
| 93 |
because it should be ignored by PDF viewers, but \LuaTeX\ doesn't allow it.) |
| 94 |
This conversion is done by the expandable \"pseudoprimitive " \^`\pdfstring`. |
| 95 |
|
| 96 |
\_endinput |
| 97 |
|
| 98 |
2026-04-21 \scantextokens{\unexpanded -> \unexpanded\ea{\scantextokens, bug fixed |
| 99 |
2024-12-18 \_pdfhexstring removed, used \_pdfstring. |
| 100 |
2021-02-08 \_octalprint -> \_hexprint |
| 101 |
2020-03-12 Released |