%%
%% This is file `pdfmanagement-firstaid.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% pdfmanagement-firstaid.dtx  (with options: `package')
%% 
%% Copyright (C) 2019-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:
%% 
%%    https://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.
%% 
%% File: pdfmanagement-firstaid.dtx
\ProvidesExplPackage{pdfmanagement-firstaid}{2023-10-09}{0.96a}
  {LaTeX PDF management testphase bundle / firstaid-patches}

\clist_map_inline:nn
  {pgf,transparent,xmp,pdflscape,xcolor,color,
   beamer,output,colorspace,fontspec,luacolor}
  {
    \bool_new:c       { g__pdfmanagement_firstaid_#1_bool }
    \bool_gset_true:c { g__pdfmanagement_firstaid_#1_bool }
  }
\clist_map_inline:Nn \g__pdfmanagement_firstaidoff_clist
  {
    \bool_if_exist:cT { g__pdfmanagement_firstaid_#1_bool }
      {
        \bool_gset_false:c { g__pdfmanagement_firstaid_#1_bool }
      }
  }
\msg_new:nnn  { pdfmanagement } { firstaid }
              { loading~pdfmanagement~firstaid~code~for~#1 }
\msg_new:nnn  { pdfmanagement } { firstaid-changed }
              { package~#1~has~changed.~Check~if~patch~is~still~valid! }
\msg_new:nnn  { pdfmanagement } { firstaid-disabled }
              { The~loading~of~package~#1~is~disabled.\\
                It~is~not~compatible~with~the~PDF~management. }
\msg_new:nnn  { pdfmanagement } { firstaid-too-old }
              { Package~#1~is~too~old~and~not~compatible.\\
                Get~at~least~version~#2.}

\sys_if_engine_luatex:T
   {
     \bool_if:NT \g__pdfmanagement_firstaid_fontspec_bool
       {
         \@ifpackagelater{fontspec}{2023-03-10}
          {
            \__fontspec_keys_define_code:nnn {fontspec} {Color}
             {
               \tl_set:Nn \l__fontspec_hexcol_tl {#1}
             }
          }{}
      }
   }

\bool_if:NT \g__pdfmanagement_firstaid_color_bool
  {
    \declare@file@substitution{color.sty}{color-ltx.sty}
  }
\bool_if:NT \g__pdfmanagement_firstaid_xcolor_bool
  {
    \AddToHook
       {
         package/xcolor/after
       }
       {\RequirePackage{xcolor-patches-tmp-ltx}}
    \DeclareHookRule{package/xcolor/after}{pdfmanagement-firstaid}{before}{xcolor}
  }
\bool_lazy_all:nT
  {
    {\sys_if_engine_luatex_p:}
    {\g__pdfmanagement_firstaid_luacolor_bool}
    {\sys_if_output_pdf_p:}
  }
  {
    \AddToHook{package/luacolor/after}
     {
       \cs_set_protected:Npn \__color_backend_select:nn #1#2
        {
          \tl_set:Nn \l__color_backend_fill_tl {#1}
          \tl_set:Nn \l__color_backend_stroke_tl {#2}
          \LuaCol@setattribute\LuaCol@Attribute
            {
              \directlua
                {
                  oberdiek.luacolor.get("\luaescapestring{#1~#2}")
                }
            }
         }
       \cs_set_protected:Npn \__color_backend_fill:n #1
         {
          \tl_set:Nn \l__color_backend_fill_tl {#1}
          \LuaCol@setattribute\LuaCol@Attribute
            {
              \directlua
                {
                  oberdiek.luacolor.get("\luaescapestring{#1}")
                }
            }
         }
       \cs_set_protected:Npn \__color_backend_stroke:n #1
         {
           \tl_set:Nn \l__color_backend_stroke_tl {#1}
           \LuaCol@setattribute\LuaCol@Attribute
             {
               \directlua
                 {
                   oberdiek.luacolor.get("\luaescapestring{#1}")
                 }
             }
         }
       \cs_set_protected:Npn \__color_backend_reset:  {}
       \cs_set_eq:NN \__color_backend_fill_reset:   \__color_backend_reset:
       \cs_set_eq:NN \__color_backend_stroke_reset: \__color_backend_reset:
     }
  }

\bool_if:NT \g__pdfmanagement_firstaid_pgf_bool
  {
    \msg_info:nnn{pdfmanagement }{firstaid}{pgf}
    \AddToHook
      {
        file/pgfrcs.sty/after
      }
      {
        \cs_set_eq:NN
          \__pdfmanagement_pgfori_pgfutil@setuppdfresources
          \pgfutil@setuppdfresources
        \def\pgfutil@setuppdfresources
          {
            \pdfmanagement_if_active:TF
              {
                \__pdfmanagement_pgf_sys_setuppdfresources_plain:
              }
              {
                \__pdfmanagement_pgfori_pgfutil@setuppdfresources
              }
         }
     }
  }
\cs_new_protected:Npn \__pdfmanagement_pgf_sys_setuppdfresources_plain:
  {
   %objects are already created ...
    \def\pgf@sys@pdf@possible@resources
      {
        /ColorSpace~\pdf_object_ref:n {__pdf/Page/Resources/ColorSpace}
        /Pattern   ~\pdf_object_ref:n {__pdf/Page/Resources/Pattern}
        /ExtGState ~\pdf_object_ref:n {__pdf/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
          \__pdfmanagement_patch_pgfextgs:w ##1\q_stop
      }
    \def\pgf@sys@addpdfresource@patterns@plain##1
      {
        \__pdfmanagement_patch_pgfpatterns:w ##1\q_stop
      }
   \def\pgf@sys@addpdfresource@colorspaces@plain##1
      {
        \__pdfmanagement_patch_pgfcolorspaces:w ##1\q_stop
      }
  }

\cs_new:Npn \__pdfmanagement_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 \pdfmanagement_add:nnn {nex}
\cs_new:Npn \__pdfmanagement_patch_pgfextgs:w  #1/#2<<#3>>#4\q_stop
  {
    \tl_set:Nx\l_tmpa_tl{#2}
      \pdfmanagement_add:nex
        {Page/Resources/ExtGState}{\tl_trim_spaces:V\l_tmpa_tl}{<<#3 #4>>}
  }
\cs_new:Npn \__pdfmanagement_patch_pgfpatterns:w  #1/#2\space#3\q_stop
  {
     \pdfmanagement_add:nex
        {Page/Resources/Pattern}{\tl_trim_spaces:n{#2}}{#3}
  }
\cs_new:Npn \__pdfmanagement_patch_pgfcolorspaces:w  #1/#2[#3]#4\q_stop
  {
    \pdfmanagement_add:nex
      {Page/Resources/ColorSpace}{\tl_trim_spaces:n{#2}}{[#3]}
  }

\bool_if:NT \g__pdfmanagement_firstaid_transparent_bool
 {
  \AddToHook{package/transparent/after}
    {
     \@ifpackagelater{transparent}{2022-10-27}{}
       {
         \msg_warning:nnnn{pdfmanagement}{firstaid-too-old}
         {transparent}
         {1.5~from~2022-10-27}
       }
    }
 }
\bool_if:NT \g__pdfmanagement_firstaid_pdflscape_bool
 {
  \AddToHook{package/pdflscape/after}
    {
     \@ifpackagelater{pdflscape}{2022-10-27}{}
       {
         \msg_warning:nnnn{pdfmanagement}{firstaid-too-old}
         {pdflscape}
         {0.13~from~2022-10-27}
       }
    }
 }
\bool_if:NT \g__pdfmanagement_firstaid_xmp_bool
 {
   \disable@package@load{hyperxmp}{\msg_warning:nnn{pdfmanagement}{firstaid-disabled}{hyperxmp}}
   \disable@package@load{pdfx}    {\msg_warning:nnn{pdfmanagement}{firstaid-disabled}{pdfx}}
   \AddToHook{package/doclicense/after}
    {
      \AtBeginDocument
        {
          \hypersetup
            {
              pdfcopyright  = {\doclicenseLongTextForHyperref},
              pdflicenseurl = {\doclicenseURL},
            }
        }
    }
 }
\bool_if:NT \g__pdfmanagement_firstaid_colorspace_bool
 {
    \AddToHook
      {
       package/colorspace/after
      }
      {\RequirePackage{colorspace-patches-tmp-ltx}}
 }
%% 
%%
%% End of file `pdfmanagement-firstaid.sty'.
