| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: pdfmanagement-firstaid.dtx |
| 4 |
% |
| 5 |
% Copyright (C) 2018-2026 The LaTeX Project |
| 6 |
% |
| 7 |
% It may be distributed and/or modified under the conditions of the |
| 8 |
% LaTeX Project Public License (LPPL), either version 1.3c of this |
| 9 |
% license or (at your option) any later version. The latest version |
| 10 |
% of this license is in the file |
| 11 |
% |
| 12 |
% http://www.latex-project.org/lppl.txt |
| 13 |
% |
| 14 |
% This file is part of the "LaTeX PDF management bundle" (The Work in LPPL) |
| 15 |
% and all files in that bundle must be distributed together. |
| 16 |
% |
| 17 |
% ----------------------------------------------------------------------- |
| 18 |
% |
| 19 |
% The development version of the bundle can be found at |
| 20 |
% |
| 21 |
% https://github.com/latex3/pdfresources |
| 22 |
% |
| 23 |
% for those people who are interested. |
| 24 |
% |
| 25 |
%<*driver> |
| 26 |
\DocumentMetadata{tagging=on,pdfstandard=ua-2} |
| 27 |
\documentclass[full]{l3doc} |
| 28 |
\usepackage{latex-lab-testphase-l3doc} |
| 29 |
\usepackage{array,booktabs} |
| 30 |
\hypersetup{pdfauthor=The LaTeX Project,pdftitle=pdfmanagement-firstaid |
| 31 |
(LaTeX PDF management bundle)} |
| 32 |
\begin{document} |
| 33 |
\DocInput{\jobname.dtx} |
| 34 |
\end{document} |
| 35 |
%</driver> |
| 36 |
% \fi |
| 37 |
% \title{^^A |
| 38 |
% The \pkg{pdfmanagement-firstaid} package -- temporary patches and package replacements ^^A |
| 39 |
% \\ \LaTeX{} PDF management bundle |
| 40 |
% } |
| 41 |
% |
| 42 |
% \author{^^A |
| 43 |
% The \LaTeX{} Project\thanks |
| 44 |
% {^^A |
| 45 |
% E-mail: |
| 46 |
% \href{mailto:[email protected]} |
| 47 |
% {[email protected]}^^A |
| 48 |
% }^^A |
| 49 |
% } |
| 50 |
% |
| 51 |
% \date{Version 0.97c, released 2026-05-26} |
| 52 |
% |
| 53 |
% \maketitle |
| 54 |
% \begin{documentation} |
| 55 |
% |
| 56 |
% \section{\pkg{pdfmanagement-firstaid} documentation} |
| 57 |
% This code is temporary! It tries to patch commands of other packages or even |
| 58 |
% replace package which are incompatible with the pdfmanagement, |
| 59 |
% to remove clashes and test if everything works as expected. |
| 60 |
% This code should disappear when packages adapt to the central interfaces. |
| 61 |
% |
| 62 |
% The package contains an number of sections for various packages. Every |
| 63 |
% section can be disabled in (the first) \cs{DocumentMetadata} with |
| 64 |
% |debug={firstaidoff={name1,name2,...},...}|. |
| 65 |
% \begin{macrocode} |
| 66 |
%<*package> |
| 67 |
\ProvidesExplPackage{pdfmanagement-firstaid}{2026-05-26}{0.97c} |
| 68 |
{LaTeX PDF management bundle / firstaid-patches} |
| 69 |
|
| 70 |
%<@@=pdfmanagement> |
| 71 |
\clist_map_inline:nn |
| 72 |
{pgf,xmp,xcolor,color,output,colorspace,luacolor} |
| 73 |
{ |
| 74 |
\bool_new:c { g_@@_firstaid_#1_bool } |
| 75 |
\bool_gset_true:c { g_@@_firstaid_#1_bool } |
| 76 |
} |
| 77 |
\clist_map_inline:Nn \g_@@_firstaidoff_clist |
| 78 |
{ |
| 79 |
\bool_if_exist:cT { g_@@_firstaid_#1_bool } |
| 80 |
{ |
| 81 |
\bool_gset_false:c { g_@@_firstaid_#1_bool } |
| 82 |
} |
| 83 |
} |
| 84 |
\msg_new:nnn { pdfmanagement } { firstaid } |
| 85 |
{ loading~pdfmanagement~firstaid~code~for~#1 } |
| 86 |
\msg_new:nnn { pdfmanagement } { firstaid-changed } |
| 87 |
{ package~#1~has~changed.~Check~if~patch~is~still~valid! } |
| 88 |
\msg_new:nnn { pdfmanagement } { firstaid-disabled } |
| 89 |
{ The~loading~of~package~#1~is~disabled.\\ |
| 90 |
It~is~not~compatible~with~the~PDF~management. } |
| 91 |
\msg_new:nnn { pdfmanagement } { firstaid-too-old } |
| 92 |
{ Package~#1~is~too~old~and~not~compatible.\\ |
| 93 |
Get~at~least~version~#2.} |
| 94 |
|
| 95 |
% \end{macrocode} |
| 96 |
% |
| 97 |
% \subsection{\pkg{color}} |
| 98 |
% |
| 99 |
% \pkg{color} is not incompatible, but the new \pkg{hyperref} driver makes use of |
| 100 |
% \pkg{l3color} to set the colors. It is therefore necessary to patch some |
| 101 |
% internal \pkg{color} commands, so |
| 102 |
% that colors defined with its \cs{definecolor} command are known to \pkg{l3color} and |
| 103 |
% so \pkg{hyperref}. This only supports the color models from l3color (which covers |
| 104 |
% all standard model of the \pkg{color} package). The |named| model is mapped to |
| 105 |
% \cs{color_set:nn}. |
| 106 |
% |
| 107 |
% This patch serves also as test to check if this change can be safely |
| 108 |
% added to \pkg{color} later. |
| 109 |
% \begin{macrocode} |
| 110 |
\bool_if:NT \g_@@_firstaid_color_bool |
| 111 |
{ |
| 112 |
\declare@file@substitution{color.sty}{color-ltx.sty} |
| 113 |
} |
| 114 |
% \end{macrocode} |
| 115 |
% |
| 116 |
% \subsection{\pkg{xcolor}} |
| 117 |
% |
| 118 |
% \pkg{xcolor} is not incompatible, but the new \pkg{hyperref} driver makes use of |
| 119 |
% \pkg{l3color} to set the colors. It is therefore necessary to patch \pkg{xcolor}, so |
| 120 |
% that colors defined with its \cs{definecolor} command are known to \pkg{l3color} and |
| 121 |
% so \pkg{hyperref}. This only supports the color model from l3color. Colors defined |
| 122 |
% with the models |cmy| and |tHsb| are silently ignored. |
| 123 |
% |
| 124 |
% The |named| model is mapped to \cs{color_set:nn}. |
| 125 |
% |
| 126 |
% \begin{macrocode} |
| 127 |
\bool_if:NT \g_@@_firstaid_xcolor_bool |
| 128 |
{ |
| 129 |
\AddToHook |
| 130 |
{ |
| 131 |
package/xcolor/after |
| 132 |
} |
| 133 |
{\RequirePackage{xcolor-patches-tmp-ltx}} |
| 134 |
% \end{macrocode} |
| 135 |
% The patch must before color definitions are loaded, which will happen in |
| 136 |
% hooks in a newer xcolor: |
| 137 |
% \begin{macrocode} |
| 138 |
\DeclareHookRule{package/xcolor/after}{pdfmanagement-firstaid}{before}{xcolor} |
| 139 |
} |
| 140 |
% \end{macrocode} |
| 141 |
% |
| 142 |
% \subsection{luacolor} |
| 143 |
% |
| 144 |
% The luacolor package doesn't take colors from l3color into account. |
| 145 |
% We add a fix, but only for pdf mode. luacolor can disable the |
| 146 |
% code by clearing the hook if needed. |
| 147 |
% |
| 148 |
% \begin{macrocode} |
| 149 |
\bool_lazy_all:nT |
| 150 |
{ |
| 151 |
{\sys_if_engine_luatex_p:} |
| 152 |
{\g_@@_firstaid_luacolor_bool} |
| 153 |
{\sys_if_output_pdf_p:} |
| 154 |
} |
| 155 |
{ |
| 156 |
\AddToHook{package/luacolor/after} |
| 157 |
{ |
| 158 |
\cs_set_protected:Npn \__color_backend_select:nn #1#2 |
| 159 |
{ |
| 160 |
\tl_set:Nn \l__color_backend_fill_tl {#1} |
| 161 |
\tl_set:Nn \l__color_backend_stroke_tl {#2} |
| 162 |
\LuaCol@setattribute\LuaCol@Attribute |
| 163 |
{ |
| 164 |
\directlua |
| 165 |
{ |
| 166 |
oberdiek.luacolor.get("\luaescapestring{#1~#2}") |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
\cs_set_protected:Npn \__color_backend_fill:n #1 |
| 171 |
{ |
| 172 |
\tl_set:Nn \l__color_backend_fill_tl {#1} |
| 173 |
\LuaCol@setattribute\LuaCol@Attribute |
| 174 |
{ |
| 175 |
\directlua |
| 176 |
{ |
| 177 |
oberdiek.luacolor.get("\luaescapestring{#1}") |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
\cs_set_protected:Npn \__color_backend_stroke:n #1 |
| 182 |
{ |
| 183 |
\tl_set:Nn \l__color_backend_stroke_tl {#1} |
| 184 |
\LuaCol@setattribute\LuaCol@Attribute |
| 185 |
{ |
| 186 |
\directlua |
| 187 |
{ |
| 188 |
oberdiek.luacolor.get("\luaescapestring{#1}") |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
\cs_set_protected:Npn \__color_backend_reset: {} |
| 193 |
\cs_set_eq:NN \__color_backend_fill_reset: \__color_backend_reset: |
| 194 |
\cs_set_eq:NN \__color_backend_stroke_reset: \__color_backend_reset: |
| 195 |
} |
| 196 |
} |
| 197 |
% \end{macrocode} |
| 198 |
% \subsection{\pkg{pgf}} |
| 199 |
% |
| 200 |
% In \pkg{pgf}, resource management is set up in the file |pgfutil-common.tex|. |
| 201 |
% This then provides three functions for adding to the resources, all of which |
| 202 |
% are objects: |
| 203 |
% \begin{itemize} |
| 204 |
% \item \cs{pgfutil@addpdfresource@extgs}: Extended graphics state |
| 205 |
% \item \cs{pgfutil@addpdfresource@colorspaces}: Color spaces |
| 206 |
% \item \cs{pgfutil@addpdfresource@patterns}: Patterns |
| 207 |
% \end{itemize} |
| 208 |
% |
| 209 |
% These resource dictionaries are used by adding entries in a cumulative sense; |
| 210 |
% the macro layer deals with ensuring that each entry is only given once. Note |
| 211 |
% that the objects themselves must be given only once for each page. |
| 212 |
% |
| 213 |
% To support these functions, there are a series of set-up macros which install |
| 214 |
% these resources. That has to take place for every page: the exact route |
| 215 |
% therefore depends on the driver. |
| 216 |
% |
| 217 |
% For the pdfmanagement project we need to avoid that pgf interferes in ExtGState, |
| 218 |
% ColorSpace and Pattern (Shadings are added to the xform resources and so probably |
| 219 |
% unproblematic for now). |
| 220 |
% The actual patch is in a file hook guarded by the boolean, |
| 221 |
% the rest of the code is always defined. |
| 222 |
% \begin{macrocode} |
| 223 |
|
| 224 |
\bool_if:NT \g_@@_firstaid_pgf_bool |
| 225 |
{ |
| 226 |
\msg_info:nnn{pdfmanagement }{firstaid}{pgf} |
| 227 |
\AddToHook |
| 228 |
{ |
| 229 |
file/pgfrcs.sty/after |
| 230 |
} |
| 231 |
{ |
| 232 |
\cs_set_eq:NN |
| 233 |
\@@_pgfori_pgfutil@setuppdfresources |
| 234 |
\pgfutil@setuppdfresources |
| 235 |
\def\pgfutil@setuppdfresources |
| 236 |
{ |
| 237 |
\@@_pgf_sys_setuppdfresources_plain: |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
%\def\pgfutil@addpdfresource@extgs#1{\pgf@sys@addpdfresource@extgs@plain{#1}} |
| 242 |
%\def\pgfutil@addpdfresource@colorspaces#1{\pgf@sys@addpdfresource@colorspaces@plain{#1}} |
| 243 |
%\def\pgfutil@addpdfresource@patterns#1{\pgf@sys@addpdfresource@patterns@plain{#1}} |
| 244 |
%\def\pgfutil@setuppdfresources{\pgf@sys@setuppdfresources@plain} |
| 245 |
% \pgf@sys@pdf@possible@resources %used in xform |
| 246 |
%Trying to patch pgf .. |
| 247 |
\cs_new_protected:Npn \@@_pgf_sys_setuppdfresources_plain: |
| 248 |
{ |
| 249 |
%objects are already created ... |
| 250 |
\def\pgf@sys@pdf@possible@resources |
| 251 |
{ |
| 252 |
/ColorSpace~\pdf_object_ref:n {__pdf/Page/Resources/ColorSpace} |
| 253 |
/Pattern ~\pdf_object_ref:n {__pdf/Page/Resources/Pattern} |
| 254 |
/ExtGState ~\pdf_object_ref:n {__pdf/Page/Resources/ExtGState} |
| 255 |
} |
| 256 |
\let\pgf@sys@pdf@check@resources=\relax% |
| 257 |
%not sure if needed, but perhaps the lists are used somewhere else ... |
| 258 |
\let\pgf@sys@pgf@resource@list@extgs=\pgfutil@empty% |
| 259 |
\let\pgf@sys@pgf@resource@list@patterns=\pgfutil@empty% |
| 260 |
\let\pgf@sys@pgf@resource@list@colorspaces=\pgfutil@empty% |
| 261 |
% the commands to add page resources |
| 262 |
\def\pgf@sys@addpdfresource@extgs@plain##1 |
| 263 |
{ |
| 264 |
%\exp_after:wN %for transparent which passes a command |
| 265 |
\@@_patch_pgfextgs:w ##1\q_stop |
| 266 |
} |
| 267 |
\def\pgf@sys@addpdfresource@patterns@plain##1 |
| 268 |
{ |
| 269 |
\@@_patch_pgfpatterns:w ##1\q_stop |
| 270 |
} |
| 271 |
\def\pgf@sys@addpdfresource@colorspaces@plain##1 |
| 272 |
{ |
| 273 |
\@@_patch_pgfcolorspaces:w ##1\q_stop |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
%\AtEndPreamble{\pgfutil@setuppdfresources} |
| 278 |
% helper commands as pgf doesn't pass resources as two arguments |
| 279 |
% code to add to the resources existing stuff in the format "/name value": |
| 280 |
\cs_new:Npn \@@_split_dict_entry_aux:NNw #1 #2 /#3~#4\q_stop |
| 281 |
{ |
| 282 |
\tl_set:Nn #1 {#3} |
| 283 |
\tl_set:Nn #2 {#4} |
| 284 |
} |
| 285 |
|
| 286 |
\cs_generate_variant:Nn \tl_trim_spaces:n{V} |
| 287 |
\cs_generate_variant:Nn \pdfmanagement_add:nnn {nee} |
| 288 |
\cs_new:Npn \@@_patch_pgfextgs:w #1/#2<<#3>>#4\q_stop |
| 289 |
{ |
| 290 |
\tl_set:Ne\l_tmpa_tl{#2} |
| 291 |
\pdfmanagement_add:nee |
| 292 |
{Page/Resources/ExtGState}{\tl_trim_spaces:V\l_tmpa_tl}{<<#3 #4>>} |
| 293 |
} |
| 294 |
\cs_new:Npn \@@_patch_pgfpatterns:w #1/#2\space#3\q_stop |
| 295 |
{ |
| 296 |
\pdfmanagement_add:nee |
| 297 |
{Page/Resources/Pattern}{\tl_trim_spaces:n{#2}}{#3} |
| 298 |
} |
| 299 |
\cs_new:Npn \@@_patch_pgfcolorspaces:w #1/#2[#3]#4\q_stop |
| 300 |
{ |
| 301 |
\pdfmanagement_add:nee |
| 302 |
{Page/Resources/ColorSpace}{\tl_trim_spaces:n{#2}}{[#3]} |
| 303 |
} |
| 304 |
|
| 305 |
% \end{macrocode} |
| 306 |
% |
| 307 |
% \subsection{xmp} |
| 308 |
% This handles the new xmp code. |
| 309 |
% \begin{macrocode} |
| 310 |
\bool_if:NT \g_@@_firstaid_xmp_bool |
| 311 |
{ |
| 312 |
\disable@package@load{hyperxmp}{\msg_warning:nnn{pdfmanagement}{firstaid-disabled}{hyperxmp}} |
| 313 |
\disable@package@load{pdfx} {\msg_warning:nnn{pdfmanagement}{firstaid-disabled}{pdfx}} |
| 314 |
\AddToHook{package/doclicense/after} |
| 315 |
{ |
| 316 |
\AtBeginDocument |
| 317 |
{ |
| 318 |
\hypersetup |
| 319 |
{ |
| 320 |
pdfcopyright = {\doclicenseLongTextForHyperref}, |
| 321 |
pdflicenseurl = {\doclicenseURL}, |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
%</package> |
| 327 |
% \end{macrocode} |
| 328 |
% \subsection{\pkg{colorspace}} |
| 329 |
% This is rather difficult as no real places to inject patches |
| 330 |
% at first a try to avoid that its ExtGState is missing: |
| 331 |
% it can not be avoided to recreate the objects (and so to get duplicates) |
| 332 |
% as colorspace uses temporary macros whose contents is lost. |
| 333 |
% \begin{macrocode} |
| 334 |
%<*package> |
| 335 |
\bool_if:NT \g_@@_firstaid_colorspace_bool |
| 336 |
{ |
| 337 |
\AddToHook |
| 338 |
{ |
| 339 |
package/colorspace/after |
| 340 |
} |
| 341 |
{\RequirePackage{colorspace-patches-tmp-ltx}} |
| 342 |
} |
| 343 |
%</package> |
| 344 |
% \end{macrocode}% |
| 345 |
% |
| 346 |
% \end{documentation} |
| 347 |
% \PrintIndex |