%% This is part of OpTeX project, see http://petr.olsak.net/optex \_codedecl \ulink {Hyperlinks <2020-03-15>} % preloaded in format \_newcount \_tocrefnum \_doc ---------------------------- `\dest[:]` creates a destination of internal links. The destination is declared by `:`. If `\hyperlinks` are not declared, then `\dest` does nothing else it is set to `\_destactive`. The `\_destactive` is implemented by `\_pdfdest` primitive. It creates a box in which the destination is shifted by `\_destheight`. The reason is that the destination is exactly at top border of the PDF viewer but we want to se the line where destination is. The destination box is positioned by different way dependent on current vertical or horizontal mode. \_cod ---------------------------- \_def\_destheight{1.4em} \_def\_destactive[#1:#2]{\_if$#2$\_else\_ifvmode \_tmpdim=\_prevdepth \_prevdepth=-1000pt \_destbox[#1:#2]\_prevdepth=\_tmpdim \_else \_destbox[#1:#2]% \_fi\_fi } \_def\_destbox[#1]{\_vbox to0pt{\_kern-\_destheight \_pdfdest name{#1} xyz\_vss}} \_def\_dest[#1]{} \_public \dest ; \_doc ---------------------------- `\_link[:]{}{}` creates an internal link to `\dest` with the same `:`. You can have more links with the same `:` but only one `\dest` in the document. If `\hyperlinks` are not declared, then `\link` only prints else it is set to `\_linkactive`. The `\_linkactive` is implemented by `\_pdfstartlink...\_pdfendlink` primitives. `\ilink[:]{}` is equivalent to `\_link` but is used from `\hyperlinks` decaration. \_cod ---------------------------- \_protected\_def\_linkactive[#1:#2]#3#4{\_leavevmode\_pdfstartlink height.9em depth.3em \_pdfborder{#1} goto name{#1:#2}\_relax {#3#4}\_pdfendlink } \_protected\_def\_link[#1]#2#3{\_leavevmode{#3}} \_protected\_def\_ilink[#1]#2{\_leavevmode{#2}} \_public \ilink ; \_doc ---------------------------- `\ulink[]{}` creates external link. It prints only by default but the `\hyperlinks` declaration defines it as `\_urlactive[url:]{}`. The external link is created by `\_pdfstartlink...\pdfendlink` primitives. The is detokenized with `\escapechar=-1` before it is used, so `\%`, `\#` etc. can be used in the . \_cod ---------------------------- \_protected\_def\_urlactive[#1:#2]#3#4{\_leavevmode{\_escapechar=-1 \_pdfstartlink height.9em depth.3em \_pdfborder{#1}% user{/Subtype/Link/A <>}\relax {#3#4}\pdfendlink}% } \_def\_ulink[#1]#2{\_leavevmode{#2}} \_def\_urlcolor{} \_public \ulink ; \_doc ---------------------------- The `\_pdfstartlink` primitive uses `\_pdfborder{}` in its parameter (see `\_linkactive` or `\_urlactive` macros). The `\_pdfbordef{}` expands to `attr{/C[? ? ?] /Border[0 0 .6]}` if the `\border` is defined. User can define it in order to create colored frames around active links. For example `\def\tocborder{1 0 0}` causes red frames in TOC (not printed, only visible in PDF viewers). \_cod ---------------------------- \_def\_pdfborder#1{\_if^#1^\_else \_isdefined{#1border}\_iftrue \_if^\_csname #1border\_endcsname^\_else attr{/C[\_csname #1border\_endcsname] /Border[0 0 .6]}\_fi \_else attr{/Border[0 0 0]}\_fi\_fi } \_doc ---------------------------- `\hyperlinks{}{}` activates `\dest`, `\link`, `\ilink`, `\ulink` in order they create links. These macros are redefined here to their \"active" version. \_cod ---------------------------- \_def\_hyperlinks#1#2{% \_let\_dest=\_destactive \_let\_link=\_linkactive \_def\_ilink[##1]##2{\_link[##1]{\_localcolor#1}{##2}}% \_def\_ulink[##1]##2{\_urlactive[url:##1]{\_localcolor#2}{##2}}% \_public \dest \ilink \ulink ;% } \_public \hyperlinks ; \_doc ---------------------------- `\url{}` does approximately the same as `\ulink[]{}`, but more work is done before the `\ulink` is processed. The link-version of is saved to `\_tmpa` and the printed version in `\_tmpb`. The printed version is modified in order to set a breakpoints in special places of the . For example `//` is replaced by `\_urlskip/\_urlskip/\_urlbskip` where `\urlskip` adds a small nobreakable glue between these two slashes and before them and `\_urlbskip` adds a breakable glue after them. \_cod \_fin ---------------------- \_def\_url#1{{% \_def\_tmpa{#1}\_replstring\_tmpa {\|}{}% \_def\_tmpb{#1}\_replstring\_tmpb {\|}{\\|}% {\_escapechar=-1 \_ea}\_ea\_edef\_ea\_tmpb\_ea{\detokenize\_ea{\_tmpb}}% \_let\_tmpa=\_tmpb \_replstring\_tmpb {//} {{\_urlskip\_urlslashslash\_urlbskip}}% \_replstring\_tmpb {/} {{\_urlskip/\_urlbskip}}% \_replstring\_tmpb {.} {{\_urlskip.\_urlbskip}}% \_replstring\_tmpb {?} {{\_urlskip?\_urlbskip}}% \_replstring\_tmpb {=} {{\urlskip=\urlbskip}}% \_ea\_replstring\_ea\_tmpb \_ea{\_string &} {{\urlbskip\char`\& \urlskip}}% \_ea\_replstring\_ea\_tmpb \_ea{\_bslash|} {{\_penalty0}}% \_ea\_ulink \_ea[\_tmpa] {\_urlfont\_tmpb\_null}% }} \_def\_urlfont{\_tt} \_def\_urlskip{\_null\_nobreak\_hskip0pt plus0.05em\_relax} \_def\_urlbskip{\_penalty100 \_hskip0pt plus0.05em\_relax} \_def\_urlslashslash{/\_urlskip/} \_public \url ; \_endcode % ---------------------------------------- There are four types of the internal links and one type of external link: \begitems * `ref: