summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/pdfmanagement-testphase/pdfmanagement-firstaid.dtx
blob: 54fb0ffa102923562c39910e3f949d805bd276eb (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
% \iffalse meta-comment
%
%% File: pdfmanagement-firstaid.dtx
%
% Copyright (C) 2018-2021 The LaTeX Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    http://www.latex-project.org/lppl.txt
%
% This file is part of the "LaTeX PDF management testphase bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/latex3/pdfresources
%
% for those people who are interested.
%
%<*driver>
\RequirePackage{pdfmanagement-testphase}
\DeclareDocumentMetadata{pdfstandard=A-2b}
\makeatletter
\declare@file@substitution{doc.sty}{doc-v3beta.sty}
\makeatother
\documentclass[full]{l3doc}
\usepackage{array,booktabs,hyperxmp}
\hypersetup{pdfauthor=The LaTeX Project,pdftitle=pdfmanagement-firstaid
  (LaTeX PDF management testphase bundle)}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% \title{^^A
%   The \pkg{pdfmanagement-firstaid} package  -- temporary patches and package replacements  ^^A
%   \\ \LaTeX{} PDF management testphase bundle
% }
%
% \author{^^A
%  The \LaTeX{} Project\thanks
%    {^^A
%      E-mail:
%        \href{mailto:latex-team@latex-project.org}
%          {latex-team@latex-project.org}^^A
%    }^^A
% }
%
% \date{Version 0.95c, released 2021-03-17}
%
% \maketitle
% \begin{documentation}
%
% \section{\pkg{pdfmanagement-firstaid} documentation}
% This code is temporary! It tries to patch commands of other packages or even
% replace package which are incompatible with the pdfmanagement,
% to remove clashes and test if everything works as expected.
% This code should disappear when packages adapt to the central interfaces.
%
% The package contains an number of sections for various packages. Every
% section can be disabled in (the first) \cs{DeclareDocumentMetadata} with
% |firstaidoff={name1,name2,...}|.
%    \begin{macrocode}
%<*package>
\ProvidesExplPackage{pdfmanagement-firstaid}{2021-03-17}{0.95c}
  {LaTeX PDF management testphase bundle / firstaid-patches}

%<@@=pdfmanagement>
\clist_map_inline:nn {pgf,transparent,hyperxmp,pdflscape,xcolor,color,beamer}
  {
    \bool_new:c       { g_@@_firstaid_#1_bool }
    \bool_gset_true:c { g_@@_firstaid_#1_bool }
  }
\clist_map_inline:Nn \g_@@_firstaidoff_clist
  {
    \bool_if_exist:cT { g_@@_firstaid_#1_bool }
      {
        \bool_gset_false:c { g_@@_firstaid_#1_bool }
      }
  }
\msg_new:nnn  { pdfmanagement } { firstaid }
              { loading~pdfmanagement~firstaid~code~for~#1 }
%    \end{macrocode}
% \subsection{\pkg{beamer}}
% \pkg{beamer} makes use of the now unsupported syntax |linkbordercolor={.5 .5 .5}|.
% To avoid an error we define this color as a name.
%    \begin{macrocode}
\bool_if:NT \g_@@_firstaid_beamer_bool
  {
    \color_set:nnn{.5~.5~.5}{rgb}{0.5,0.5,0.5}
  }
%    \end{macrocode}
% \subsection{\pkg{color}}
%
%  \pkg{color} is not incompatible, but the new \pkg{hyperref} driver makes use of
%  \pkg{l3color} to set the colors. It is therefore necessary to patch some
%  internal \pkg{color} commands, so
%  that colors defined with its \cs{definecolor} command are known to \pkg{l3color} and
%  so \pkg{hyperref}. This only supports the color models from l3color (which covers
%  all standard model of the \pkg{color} package). The |named| model is mapped to
%  \cs{color_set:nn}.
%
%  This patch serves also as test to check if this change can be safely
%  added to \pkg{color} later.
%    \begin{macrocode}
\@ifundefined{color_set:nn}{
\RequirePackage{l3color}}{}
\bool_if:NT \g_@@_firstaid_color_bool
  {
    \declare@file@substitution{color.sty}{color-ltx.sty}
  }
%    \end{macrocode}
%
% \subsection{\pkg{xcolor}}
%
%  \pkg{xcolor} is not incompatible, but the new \pkg{hyperref} driver makes use of
%  \pkg{l3color} to set the colors. It is therefore necessary to patch \pkg{xcolor}, so
%  that colors defined with its \cs{definecolor} command are known to \pkg{l3color} and
%  so \pkg{hyperref}. This only supports the color model from l3color. Colors defined
%  with the models |cmy| and |tHsb| are silently ignored.
%
%  The |named| model is mapped to \cs{color_set:nn}.
%
%    \begin{macrocode}
\@ifundefined{color_set:nn}{
\RequirePackage{l3color}}{}
\bool_if:NT \g_@@_firstaid_xcolor_bool
  {
    \AddToHook{package/after/xcolor}
       {\RequirePackage{xcolor-patches-tmp-ltx}\XC@@@@names}
  }
%    \end{macrocode}
% \subsection{\pkg{pgf}}
%
% In \pkg{pgf}, resource management is set up in the file |pgfutil-common.tex|.
% This then provides three functions for adding to the resources, all of which
% are objects:
% \begin{itemize}
%   \item \cs{pgfutil@addpdfresource@extgs}: Extended graphics state
%   \item \cs{pgfutil@addpdfresource@colorspaces}: Color spaces
%   \item \cs{pgfutil@addpdfresource@patterns}: Patterns
% \end{itemize}
%
% These resource dictionaries are used by adding entries in a cumulative sense;
% the macro layer deals with ensuring that each entry is only given once. Note
% that the objects themselves must be given only once for each page.
%
% To support these functions, there are a series of set-up macros which install
% these resources. That has to take place for every page: the exact route
% therefore depends on the driver.
%
% For the pdfmanagement project we need to avoid that pgf interferes in ExtGState,
% ColorSpace and Pattern (Shadings are added to the xform resources and so probably
% unproblematic for now).
% The actual patch is in a file hook guarded by the boolean,
% the rest of the code is always defined.
%    \begin{macrocode}

\bool_if:NT \g_@@_firstaid_pgf_bool
  {
    \msg_info:nnn{pdfmanagement }{firstaid}{pgf}
    \AddToHook{file/after/pgfrcs.sty}
      {
        \cs_set_eq:NN
          \@@_pgfori_pgfutil@setuppdfresources
          \pgfutil@setuppdfresources
        \def\pgfutil@setuppdfresources
          {
            \pdfmanagement_if_active:TF
              {
                \@@_pgf_sys_setuppdfresources_plain:
              }
              {
                \@@_pgfori_pgfutil@setuppdfresources
              }
         }
     }
  }
%\def\pgfutil@addpdfresource@extgs#1{\pgf@sys@addpdfresource@extgs@plain{#1}}
%\def\pgfutil@addpdfresource@colorspaces#1{\pgf@sys@addpdfresource@colorspaces@plain{#1}}
%\def\pgfutil@addpdfresource@patterns#1{\pgf@sys@addpdfresource@patterns@plain{#1}}
%\def\pgfutil@setuppdfresources{\pgf@sys@setuppdfresources@plain}
% \pgf@sys@pdf@possible@resources %used in xform
%Trying to patch pgf ..
\cs_new_protected:Npn \@@_pgf_sys_setuppdfresources_plain:
  {
   %objects are already created ...
    \def\pgf@sys@pdf@possible@resources
      {
        /ColorSpace~\pdf_object_ref:n {Page/Resources/ColorSpace}
        /Pattern   ~\pdf_object_ref:n {Page/Resources/Pattern}
        /ExtGState ~\pdf_object_ref:n {Page/Resources/ExtGState}
      }
    \let\pgf@sys@pdf@check@resources=\relax%
    %not sure if needed, but perhaps the lists are used somewhere else ...
    \let\pgf@sys@pgf@resource@list@extgs=\pgfutil@empty%
    \let\pgf@sys@pgf@resource@list@patterns=\pgfutil@empty%
    \let\pgf@sys@pgf@resource@list@colorspaces=\pgfutil@empty%
    % the commands to add page resources
    \def\pgf@sys@addpdfresource@extgs@plain##1
      {
        %\exp_after:wN %for transparent which passes a command
          \@@_patch_pgfextgs:w ##1\q_stop
      }
    \def\pgf@sys@addpdfresource@patterns@plain##1
      {
        \@@_patch_pgfpatterns:w ##1\q_stop
      }
   \def\pgf@sys@addpdfresource@colorspaces@plain##1
      {
        \@@_patch_pgfcolorspaces:w ##1\q_stop
      }
  }

%\AtEndPreamble{\pgfutil@setuppdfresources}
% helper commands as pgf doesn't pass resources as two arguments
% code to add to the resources existing stuff in the format "/name value":
\cs_new:Npn \@@_split_dict_entry_aux:NNw #1 #2 /#3~#4\q_stop
  {
    \tl_set:Nn #1 {#3}
    \tl_set:Nn #2 {#4}
  }

\cs_generate_variant:Nn \tl_trim_spaces:n{V}
\cs_generate_variant:Nn \__pdf_backend_PageResources_gput:nnn {nex}
\cs_new:Npn \@@_patch_pgfextgs:w  #1/#2<<#3>>#4\q_stop
  {
    \tl_set:Nx\l_tmpa_tl{#2}
      \__pdf_backend_PageResources_gput:nex
        {ExtGState}{\tl_trim_spaces:V\l_tmpa_tl}{<<#3 #4>>}
  }
\cs_new:Npn \@@_patch_pgfpatterns:w  #1/#2\space#3\q_stop
  {
    \exp_args:Nnxx
      \__pdf_backend_PageResources_gput:nnn
        {Pattern}{\tl_trim_spaces:n{#2}}{#3}
  }
\cs_new:Npn \@@_patch_pgfcolorspaces:w  #1/#2[#3]#4\q_stop
  {
    \exp_args:Nne
    \__pdf_backend_PageResources_gput:nnn
      {ColorSpace}{\tl_trim_spaces:n{#2}}{[#3]}
  }

%    \end{macrocode}
% \subsection{\pkg{transparent}}
% We simply replace by the new version.
%    \begin{macrocode}
\bool_if:NT \g_@@_firstaid_transparent_bool
 {
   \declare@file@substitution{transparent.sty}{transparent-ltx.sty}
 }
%    \end{macrocode}
% \subsection{\pkg{pdflscape}}
% We simply replace by the new version.
%    \begin{macrocode}
\bool_if:NT \g_@@_firstaid_pdflscape_bool
 {
   \declare@file@substitution{pdflscape.sty}{pdflscape-ltx.sty}
 }
%    \end{macrocode}
% \subsection{hyperxmp}
% We add some code at the end of hyperxmp.sty.
%    \begin{macrocode}
\bool_if:NT \g_@@_firstaid_hyperxmp_bool
 {
    \AddToHook
      {file/after/hyperxmp.sty}
      {\RequirePackage{hyperxmp-patches-tmp-ltx}}
 }
%</package>
%    \end{macrocode}
% \subsection{\pkg{colorspace}}
% This is rather difficult as no real places to inject patches
% at first a try to avoid that its ExtGState is missing:
% it can not be avoided to recreate the objects (and so to get duplicates)
% as colorspace uses temporary macros whose contents is lost.
%    \begin{macrocode}
%<*package>
%<@@=pdf>
% this must be earlier, to avoid problems with luatex which has two pageresources
% lua/tex
\hook_gput_code:nnn {begindocument} {pdf}
  {
    \tl_if_exist:NT \spc@op
      {
        \def\spc@Pageresources#1{}
      }

  }

\hook_gput_code:nnn {begindocument/end} {pdf}
  {
    \tl_if_exist:NT \spc@op
      {
        \@@_backend_object_new:nn  {__spc_extgstate_op_false}{dict}
        \@@_backend_object_write:nn
          {__spc_extgstate_op_false}
          {/Type /ExtGState~/op~false~/OP~false}
        \pdfmanagement_add:nnn
          {Page/Resources/ExtGState}
          {SPCko}
          {\@@_backend_object_ref:n {__spc_extgstate_op_false}}
        \@@_backend_object_new:nn  {__spc_extgstate_op_true0}{dict}
        \@@_backend_object_write:nn
          {__spc_extgstate_op_true0}
          {/Type /ExtGState~/op~true~/OP~true~/OPM~0}%
        \pdfmanagement_add:nnn
          {Page/Resources/ExtGState}
          {SPCmz}
          {\@@_backend_object_ref:n {__spc_extgstate_op_true0}}
        \@@_backend_object_new:nn  {__spc_extgstate_op_true1}{dict}
        \@@_backend_object_write:nn
          {__spc_extgstate_op_true1}
          {/Type /ExtGState~/op~true~/OP~true~/OPM~1}%
         \pdfmanagement_add:nnn
          {Page/Resources/ExtGState}
          {SPCop}
          {\@@_backend_object_ref:n {__spc_extgstate_op_true1}}
      }
  }
%</package>
%    \end{macrocode}%
% \end{documentation}
% \PrintIndex