% \iffalse meta-comment
%
%% Copyright (C) 2020 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}
\clearpage
\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.4.1, dated~2022/07/21.}}
% \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}.
%
% \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}
  {2022/07/21}
  {v0.4.1}
  {Embed non-PDF files into PDF with hyperlink}

\RequirePackage{hyperref}
%</package>
% \fi
% \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}.}
% \changes{v0.3.0}{2020/03/13}{Switched to \texttt{l3pdf} instead of using primitives directly. Currently some internal functions from \texttt{l3backend} are needed too for handling links.}
% \changes{v0.4.0}{2021/02/26}{Update to new \texttt{l3pdf} names and support more engines.}
% \changes{v0.4.1}{2022/07/21}{Properly separate legacy and Unicode filenames}
% The main functionality.
%    \begin{macrocode}
\str_new:N \l__intopdf_mime_str
\str_new:N \l__intopdf_filename_str
\str_new:N \l__intopdf_description_str

\cs_new_protected:Nn\intopdf_attach_link:nnnnn{
  \leavevmode
  \str_set_convert:Nnnn \l__intopdf_mime_str { #3 } { default } { utf8/name }
  \pdf_object_unnamed_write:nx {fstream} { {
    /Subtype /\l__intopdf_mime_str
  } { #2 } }
  \str_set_convert:Nnnn \l__intopdf_filename_str { #1 } { default } { utf8/string }
  \str_set_convert:Nnnn \l__intopdf_filename_unicode_str { #1 } { default } { utf16/string }
  \str_set_convert:Nnnn \l__intopdf_description_str { #4 } { default } { utf16/string }
  \pdf_object_unnamed_write:nx {dict} {
    /Type /Filespec
    /F (\l__intopdf_filename_str)
    /UF (\l__intopdf_filename_unicode_str)
    /EF << /F~\pdf_object_ref_last: >>
    /Desc (\l__intopdf_description_str)
  }
  \__pdf_backend_link_begin_user:nnw {
    \Hy@setpdfborder
    \tl_if_empty:NF\@pdfhighlight{
      /H\@pdfhighlight
    }
    \cs_if_free:NF\@urlbordercolor{
      /C[\@urlbordercolor]
    }
  }{
    /Subtype /Link
  }
    \__pdf_backend_link_begin_user:nnw { } {
      /Subtype /FileAttachment
      /FS~\pdf_object_ref_last:
      /F~416
      /CA~0
      /Contents (\l__intopdf_description_str)
    }
      #5
    \__pdf_backend_link_end:
  \__pdf_backend_link_end:
}
\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}