| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \outlines {PDF outlines <2021-02-09>} % preloaded in format |
| 4 |
|
| 5 |
\_def\_outlines#1{\_pdfcatalog{/PageMode/UseOutlines}\_openref |
| 6 |
\_ifx\_toclist\_empty |
| 7 |
\_opwarning{\_noexpand\outlines -- data unavailable. TeX me again}% |
| 8 |
\_incr\_unresolvedrefs |
| 9 |
\_else |
| 10 |
\_ifx\_dest\_destactive \_else |
| 11 |
\_opwarning{\_noexpand\outlines doesn't work when \_noexpand\hyperlinks isn't declared}\_fi |
| 12 |
{\_let\_tocline=\_outlinesA |
| 13 |
\_count0=0 \_count1=0 \_count2=0 \_count3=0 \_toclist % calculate numbers o childs |
| 14 |
\_def\_outlinelevel{#1}\_let\_tocline=\_outlinesB |
| 15 |
\_tocrefnum=0 \_count0=0 \_count1=0 \_count2=0 \_count3=0 |
| 16 |
\_toclist}% create outlines |
| 17 |
\_fi |
| 18 |
} |
| 19 |
\_def\_outlinesA#1#2#3#4#5#6#7{% |
| 20 |
\_isequal{\relax}{#4}\_iffalse |
| 21 |
\_advance\_count#1 by1 |
| 22 |
\_ifcase#1\_or |
| 23 |
\_addoneol{_ol:\_the\_count0}\_or |
| 24 |
\_addoneol{_ol:\_the\_count0:\_the\_count1}\_or |
| 25 |
\_addoneol{_ol:\_the\_count0:\_the\_count1:\_the\_count2}\_or |
| 26 |
\_addoneol{_ol:\_the\_count0:\_the\_count1:\_the\_count2:\_the\_count3}\_fi |
| 27 |
\_fi |
| 28 |
} |
| 29 |
\_def\_addoneol#1{% |
| 30 |
\_ifcsname #1\_endcsname |
| 31 |
\_tmpnum=\_csname#1\_endcsname\_relax |
| 32 |
\_advance\_tmpnum by1 \_sxdef{#1}{\_the\_tmpnum}% |
| 33 |
\_else \_sxdef{#1}{1}% |
| 34 |
\_fi |
| 35 |
} |
| 36 |
\_def\_outlinesB#1#2#3#4#5#6#7{% |
| 37 |
\_advance\_tocrefnum by1 |
| 38 |
\_isequal{\relax}{#4}\_iffalse |
| 39 |
\_advance\_count#1 by1 |
| 40 |
\_ifcase#1% |
| 41 |
\_tmpnum=\_trycs{_ol:\_the\_count0}{0}\_or |
| 42 |
\_tmpnum=\_trycs{_ol:\_the\_count0:\_the\_count1}{0}\_relax\_or |
| 43 |
\_tmpnum=\_trycs{_ol:\_the\_count0:\_the\_count1:\_the\_count2}{0}\_relax\_or |
| 44 |
\_tmpnum=\_trycs{_ol:\_the\_count0:\_the\_count1:\_the\_count2:\_the\_count3}{0}\_relax\_or |
| 45 |
\_tmpnum = 0\_relax\_fi |
| 46 |
\_isempty{#4}\_iftrue \_pdfunidef\_tmp{#5}\_else \_pdfunidef\_tmp{#4}\_fi |
| 47 |
\_outlinesC{toc:\_the\_tocrefnum}{\_ifnum#1<\_outlinelevel\_space\_else-\_fi}{\_tmpnum}{\_tmp}% |
| 48 |
\_fi |
| 49 |
} |
| 50 |
\_def\_outlinesC#1#2#3#4{\_pdfoutline goto name{#1} count #2#3{#4}\_relax} |
| 51 |
|
| 52 |
\_newcount\_oulnum |
| 53 |
\_def\_insertoutline#1{\_incr\_oulnum |
| 54 |
\_pdfdest name{oul:\_the\_oulnum} xyz\_relax |
| 55 |
\_pdfunidef\_tmp{#1}% |
| 56 |
\_pdfoutline goto name{oul:\_the\_oulnum} count0 {\_tmp}\_relax |
| 57 |
} |
| 58 |
\_public \outlines \insertoutline ; |
| 59 |
|
| 60 |
\_endcode % ---------------------------- |
| 61 |
|
| 62 |
The problem is that PDF format needs to know the number of direct |
| 63 |
descendants of each outline if we need to create the tree of structured |
| 64 |
outlines. But we know only the level of each outline. The required data |
| 65 |
should be calculated from TOC data. We use two steps over TOC data saved in |
| 66 |
the `\_toclist` where each record is represented by one `\_tocline`. |
| 67 |
|
| 68 |
The first step, the \`\outlines` macro sets `\_tocline` to \`\_outlinesA` and |
| 69 |
calculates the number of direct descendants of each record. The second step, |
| 70 |
the `\outlines` macro sets `\_tocline` to \`\_outlinesB` and it uses prepared |
| 71 |
data and creates outlines. |
| 72 |
|
| 73 |
Each outline is mapped to the control sequence of the type |
| 74 |
`\_ol:<num>` or `\_ol:<num>:<num>` or `\_ol:<num>:<num>:<num>` or etc. |
| 75 |
The first one is reserved for level 0, the second one for level 1 (chapters), the third |
| 76 |
one for level 2 (sections) etc. The number of direct descendants will be stored |
| 77 |
in these macros after the first step is finished. Each new outline of a given level |
| 78 |
increases the `<num>` at the given level. When the first step is processed then |
| 79 |
(above that) the `\_ol:..` sequence of the parent increases its value too. The |
| 80 |
`_ol:...` sequences are implemented by `\_ol:\_count0:\_count1:\count2` etc. |
| 81 |
For example, when section (level 2) is processed in the first step then we do: |
| 82 |
|
| 83 |
\begtt |
| 84 |
\advance \count2 by 1 |
| 85 |
% increases the mapping pointer of the type |
| 86 |
% \_ol:\_count0:\_count1:\_count2 of this section |
| 87 |
\advance \_ol:\_count0:\_count1 by 1 |
| 88 |
% increases the number of descendants connected |
| 89 |
% to the parent of this section. |
| 90 |
\endtt |
| 91 |
|
| 92 |
When the second step is processed, then we only read the stored data about the |
| 93 |
number of descendants. And we use it in `count` parameter of `\_pdfoutline` |
| 94 |
primitive. |
| 95 |
|
| 96 |
For linking, we use the same links as in TOC, i.e. the `toc:\_the\_tocrefnum` |
| 97 |
labels are used. |
| 98 |
|
| 99 |
\`\insertoutline` `{<text>}` inserts one outline with zero direct descendants. |
| 100 |
It creates a link destination of the type `oul:<num>` into the document |
| 101 |
(where \^`\insertoutline` is used) and the link itself is created too in the |
| 102 |
outline. |
| 103 |
|
| 104 |
\endinput |
| 105 |
|
| 106 |
2021-02-15 \_advance -> \_incr |
| 107 |
2021-02-09 \thisoutline implemented |
| 108 |
2020-03-12 released |