]> mj.ucw.cz Git - ucwmac.git/blob - ucw-link.tex
Makefile: Compile tests twice to get references right
[ucwmac.git] / ucw-link.tex
1 % The UCW Macro Collection: Destinations and links
2 % Written by Martin Mares <mj@ucw.cz> in 2018 and placed into public domain
3 % -------------------------------------------------------------------------
4
5 \ucwdefmodule{link}
6
7 % Should clickable links be produced?
8 \newif\ifclickable
9 \clickabletrue
10
11 % Common style of all clickable links
12 \pdflinkmargin=1pt
13 \def\commonlinkargs{height \the\dimexpr\ht\strutbox-0.5pt\relax depth \the\dimexpr\dp\strutbox-0.5pt\relax attr {/C [0 0 0.5] /Border [0 0 2]}}
14
15 % Define a PDF destination for the current position at the page
16 \def\destpos#1{\ifclickable\pdfdest name {#1} xyz\relax\fi}
17
18 % Define a PDF destination for the current page
19 \def\destpage#1{\ifclickable\pdfdest name {#1} fit\relax\fi}
20
21 % Typeset a clickable link to the given destination
22 % \link{dest}{text}
23 \def\link#1#2{%
24         \ifclickable
25                 \pdfstartlink\commonlinkargs goto name {#1}\relax
26                 #2%
27                 \pdfendlink\relax
28         \else
29                 #2%
30         \fi
31 }
32
33 % Typeset a clickable link to the given page number
34 % (This does not use named destinations. We use it in tables of contents and indices,
35 % where absolute page numbers are known from other sources.)
36 % \linkpage{page}{text}
37 \def\linkpage#1#2{\ifclickable\pdfstartlink\commonlinkargs goto page #1 {/Fit}\relax #2\pdfendlink\else #2\fi}
38
39 % Typeset a clickable URL
40 % \url{http://example.com/}
41 \def\url{\begingroup\allowurlchars\urlaux}
42 \def\urlaux#1{\linkurlaux{#1}{\displayurl #1^^X}}
43
44 % Typeset a clickable link to the given URL
45 % \linkurl{http://example.com/}{text}
46 \def\linkurl{\begingroup\allowurlchars\linkurlaux}
47 \def\linkurlaux#1#2{\endgroup
48         \leavevmode
49         \ifclickable
50                 \pdfstartlink\commonlinkargs user {/Subtype/Link /A << /Type/Action /S/URI /URI(#1) >>}\relax
51         \fi
52         #2%
53         \ifclickable
54                 \pdfendlink
55         \fi
56 }
57
58 % Catcode each special character valid in URL to 'other'
59 \def\allowurlchars{\catcode`\#=12\catcode`\_=12\catcode`\%=12\catcode`\&=12\catcode`\$=12\catcode`\~=12\relax}
60
61 % Style switches and the beginning/end of an URL
62 \let\urlprefix\it
63 \let\urlsuffix\/
64
65 % Kern to place between "//" in an URL
66 \newdimen\urlinterslashkern
67 \urlinterslashkern=-0.1em
68
69 % Internal macro for typesetting of URLs
70 \def\displayurl#1:#2#3^^X{{\urlprefix #1:#2\ifx#2/\kern\urlinterslashkern\fi#3\urlsuffix}}