summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/intopdf/intopdf.dtx
blob: 903e295b80160591a9cc12447791ceac89dd408f (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
% \iffalse meta-comment
%
%% Copyright (C) 2018 by Marcel Krueger
%%
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.3c of this license or (at your option) any later
%% version. The latest version of this license is in:
%%
%% http://www.latex-project.org/lppl.txt
%%
%% and version 1.3 or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.
%
%<*batch>
%<*gobble>
\ifx\jobname\relax\let\documentclass\undefined\fi
\ifx\documentclass\undefined
\csname fi\endcsname
%</gobble>
\input l3docstrip.tex
\keepsilent
\preamble
\endpreamble
\generate{\file{intopdf.sty}{\from{intopdf.dtx}{package}}}
\endbatchfile
%</batch>
%<*gobble>
\fi
\expandafter\ifx\csname @currname\endcsname\empty
\csname fi\endcsname
%</gobble>
%<*driver>
\documentclass[full]{l3doc}
\usepackage{intopdf,metalogo}
\begin{document}
\DocInput{intopdf.dtx}
\PrintIndex
\PrintChanges
\end{document}
%</driver>
%<*gobble>
\fi
%</gobble>
% \fi
%
% \GetFileInfo{intopdf.dtx}
% \title{The \pkg{intopdf} package\thanks{This document
%        corresponds to \pkg{intopdf}~0.2.1, dated~2019/05/28.}}
% \author{Marcel Kr\"uger \\ \href{mailto:tex@2krueger.de}{tex@2krueger.de}}
%
% \maketitle
%
% \begin{documentation}
% This package defines a command \cmd\attachandlink{} which allows to attach an arbitrary file to your PDF document and linking to it from the main text.
% It is inspired by the \TeX Stack Exchange question \href{https://tex.stackexchange.com/questions/418606/embed-non-pdf-files-e-g-bibtex-into-pdf-with-hyperlink-in-the-pdf/418827#418827}{Embed non-PDF files (e.g., BibTex) into PDF with hyperlink in the PDF}. The engines pdf\LaTeX\ and \LuaLaTeX\ are supported. 
%
% \section{Usage}
% \begin{function}{\attachandlink}
%   \begin{syntax}
%     |\attachandlink| [\meta{filespec}]\Arg{filename}[\meta{mime-type}]\Arg{Description}\Arg{link text}
%   \end{syntax}
%   The text \meta{link text} is inserted, linking to the file \meta{filename} which is attached to the document.
%
%   \meta{Description} should be a description of the file and will be shown by the PDF viewer in the atttached files section and sometimes in a mouseover tooltip.\\
%   \meta{mime-type} should be the MIME-Type of \meta{filename}. Currently \meta{mime-type} is mandatory but I hope to make it optional in a later release.
%   Additionally \meta{filespec} can be used to specify the filename which will be shown in the PDF viewer.
%   If this is not given, it will default to \meta{filename}.
% \end{function}
% \section{Example}
% The source of the document is attached
% \attachandlink{intopdf.dtx}[application/x-tex]
%     {The source of this document}{here}.
% \begin{verbatim}
%   The source of the document is attached
%   \attachandlink{intopdf.dtx}[application/x-tex]
%       {The source of this document}{here}.
% \end{verbatim}
% \end{documentation}
% \begin{implementation}
% \section{The implementation}
% \changes{v0.0.1}{2018/03/13}{Initial version}
%    \begin{macrocode}
%<@@=intopdf>
%    \end{macrocode}
% \iffalse
%<*package>
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3,xparse}
\ProvidesExplPackage
  {intopdf}
  {2019/05/28}
  {0.2.1}
  {Embed non-PDF files into PDF with hyperlink}

\RequirePackage{hyperref}
%</package>
% \fi
% \changes{v0.1.0}{2018/03/15}{Added \LuaLaTeX\ support}
% Only \pdfTeX{} and \LuaLaTeX{} are supported.
% For other engines we show an error.
%    \begin{macrocode}
\msg_new:nnnn{intopdf}{engine-unsupported}
  {Your~TeX~engine~is~not~supported~by~intopdf.}
  {To~use~intopdf,~you~have~to~use~pdfLaTeX~or~LuaLaTeX.}
%    \end{macrocode}
% Then we define some wrappers for the primitives.
%    \begin{macrocode}
\cs_generate_variant:Nn\str_case:nnF{VnF}
\str_case:VnF\c_sys_engine_str{
  {pdftex}{
    \cs_new:Nn\__intopdf_escape_name:n{\pdfescapename{#1}}
    \cs_new:Nn\__intopdf_escape_string:n{(\pdfescapestring{#1})}
    \cs_new:Nn\__intopdf_pdfobj:n{
      \tex_immediate:D\pdftex_pdfobj:D{<<#1>>}
    }
    \cs_new:Nn\__intopdf_pdfstream_file:nn{
      \tex_immediate:D\pdftex_pdfobj:D stream attr {#1} file {#2}
    }
    \cs_new:Nn\__intopdf_lastobj_ref:{
      \c_space_tl
      \int_use:N\pdftex_pdflastobj:D
      \c_space_tl
      0
      \c_space_tl
      R
    }
    \cs_new:Npn\__intopdf_annot:nnw#1#2{
      \leavevmode
      \pdftex_pdfstartlink:D attr{#1} user {#2}
    }
    \cs_new:Nn\__intopdf_annot_end:{
      \pdftex_pdfendlink:D
    }
  }
  {luatex}{
    \RequirePackage{pdftexcmds}
    \cs_new:Nn\__intopdf_escape_name:n{\pdf@escapename{#1}}
    \cs_new:Nn\__intopdf_escape_string:n{(\pdf@escapestring{#1})}
    \cs_new:Nn\__intopdf_pdfobj:n{
      \tex_immediate:D\luatex_pdfextension:D obj{<<#1>>}
    }
    \cs_new:Nn\__intopdf_pdfstream_file:nn{
      \tex_immediate:D\luatex_pdfextension:D obj stream attr {#1} file {#2}
    }
    \cs_new:Nn\__intopdf_lastobj_ref:{
      \c_space_tl
      \luatex_pdffeedback:D lastobj
      \c_space_tl
      0
      \c_space_tl
      R
    }
    \cs_new:Npn\__intopdf_annot:nnw#1#2{
      \leavevmode
      \luatex_pdfextension:D startlink attr{#1} user {#2}
    }
    \cs_new:Nn\__intopdf_annot_end:{
      \luatex_pdfextension:D endlink
      \scan_stop:
    }
  }
}{
  \msg_critical:nn{intopdf}{engine-unsupported}
}
\cs_new:Nn\__intopdf_annot:nnn{
  \__intopdf_annot:nnw{#1}{#2}
  #3
  \__intopdf_annot_end:
}
%    \end{macrocode}
% \begin{macro}{\attachandlink,\intopdf_attach_link:nnnnn,\intopdf_attach_link:nnnn}
% \changes{v0.2.0}{2019/05/21}{Added filespec support. (The first optional argument) Added \cs{intopdf_attach_link:nnnnn}.}
% The main functionality.
%    \begin{macrocode}
\cs_new_protected:Nn\intopdf_attach_link:nnnnn{
  \__intopdf_pdfstream_file:nn{
    /Subtype /\__intopdf_escape_name:n{#3}
  }{#2}
  \__intopdf_pdfobj:n{
    /Type /Filespec
    /F \__intopdf_escape_string:n{#1}
    /EF << /F \__intopdf_lastobj_ref: >>
    /Desc \__intopdf_escape_string:n{#4}
  }%
  \__intopdf_annot:nnn{
    \Hy@setpdfborder
    \tl_if_empty:NF\@pdfhighlight{
      /H\@pdfhighlight
    }
    \cs_if_free:NF\@urlbordercolor{
      /C[\@urlbordercolor]
    }
  }{
    /Subtype /Link
  }{
    \__intopdf_annot:nnn{}{
      /Subtype /FileAttachment
      /FS \__intopdf_lastobj_ref:
      /F\c_space_tl 416
      /CA\c_space_tl 0
      /Contents \__intopdf_escape_string:n{#4}
    }{
      #5
    }
  }
}
\cs_new_protected:Npn \intopdf_attach_link:nnnn #1{
  \intopdf_attach_link:nnnnn{#1}{#1}
}
\NewDocumentCommand\attachandlink{O{#2}mr[]mm}{
  \intopdf_attach_link:nnnnn{#1}{#2}{#3}{#4}{#5}
}
%    \end{macrocode}
% \end{macro}
% \end{implementation}