%%
%% This is file `pdfmanagement-testphase.ltx',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% pdfmanagement-testphase.dtx  (with options: `header')
%% l3pdfdict.dtx  (with options: `package')
%% l3pdfmanagement.dtx  (with options: `package')
%% ltdocinit.dtx  (with options: `package')
%% l3pdfannot.dtx  (with options: `package')
%% l3pdfxform.dtx  (with options: `package')
%% l3pdfmeta.dtx  (with options: `package')
%% l3pdftools.dtx  (with options: `package')
%% l3pdffile.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-testphase.dtx
\ProvidesExplFile{pdfmanagement-testphase.ltx}{2022-01-28}{0.95k}
  {PDF~management~code~(testphase)}
%% File: l3pdfdict.dtx
\cs_new:Npn \__pdfdict_get_type:n #1
  {
    \str_case_e:nn { \str_head:n{#1} }
     {
       {g}{global}
       {l}{local}
     }
  }
\msg_new:nnn  { pdfdict } { show-dict }
  { %#1: name of the dictionary
    %#2: expanded content
    %#3: type
    The~#3~dictionary~'#1'~
    \tl_if_empty:nTF {#2}
      { is~empty \\>~ . }
      { contains~the~pairs~(without~outer~braces): #2 . }
  }
\msg_new:nnn  { pdfdict } { unknown-dict }
  {
    The~dictionary~'#1'~is~unknown.
  }
\msg_new:nnn  { pdfdict } { dict-already-defined  }
  {
    The~#2~dictionary~'#1'~is~already~defined.
  }
\msg_new:nnn  { pdfdict } { empty-value }
              { The~value~#1~for~#2~is~blank~and~will~be~ignored }

\msg_new:nnn  { pdfdict } { invalid-name }
              { Name~'#1'~is~not~valid\\
                Names~of~dictionaries~should~start~with~'g_'~or~'l_' }


\seq_new:N \g__pdfdict_lnames_seq
\seq_new:N \g__pdfdict_gnames_seq
\cs_new:Npn \__pdfdict_name:n #1  % #1 dictionary name
  {
    \str_head:n{#1}__pdfdict_/#1_prop
  }
\cs_set_eq:NN \__kernel_pdfdict_name:n \__pdfdict_name:n

\cs_new_protected:Npn \__pdfdict_new:n #1
  {
    \__pdfdict_if_exist:nTF { #1 }
      {
        \msg_error:nnxx
          { pdfdict }
          { dict-already-defined }
          { \tl_to_str:n {#1} }
          { \__pdfdict_get_type:n{#1} }
       }
       {
         \str_case_e:nnF { \str_head:n{#1} }
           {
             {g}
             {
                 \prop_new:c  { \__pdfdict_name:n { #1 } }
                 \seq_gput_right:cn {g__pdfdict_gnames_seq} { #1 }
             }
             {l}
             {
                 \prop_new:c  { \__pdfdict_name:n { #1 } }
                 \seq_gput_right:cn {g__pdfdict_lnames_seq} { #1 }
             }
           }
           {
             \msg_error:nnx{pdfdict}{invalid-name}{\tl_to_str:n{#1}}
           }
       }
  }

\cs_set_eq:NN \pdfdict_new:n \__pdfdict_new:n
\cs_new_protected:Npn \__pdfdict_set_eq:nn #1 #2
  {
    \__pdfdict_if_exist:nTF  { #2 }
      {
        \__pdfdict_if_exist:nF { #1 }
          {
            \__pdfdict_new:n { #1 }
          }
        \prop_set_eq:cc { \__pdfdict_name:n {#1} }{ \__pdfdict_name:n {#2} }
      }
      {
         \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
      }
  }

\cs_set_eq:NN \pdfdict_set_eq:nn \__pdfdict_set_eq:nn

\cs_new_protected:Npn \__pdfdict_gset_eq:nn #1 #2
  {
    \__pdfdict_if_exist:nTF { #2 }
      {
        \__pdfdict_if_exist:nF { #1 }
          {
            \__pdfdict_new:n { #1 }
          }
        \prop_gset_eq:cc { \__pdfdict_name:n {#1} }{ \__pdfdict_name:n {#2} }
      }
      {
         \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
      }
  }

\cs_set_eq:NN \pdfdict_gset_eq:nn \__pdfdict_gset_eq:nn
\prg_new_conditional:Npnn \__pdfdict_if_exist:n #1 { p , T , F , TF }
  {
    \prop_if_exist:cTF
      { \__pdfdict_name:n { #1 }  }
      { \prg_return_true:  }
      { \prg_return_false: }
  }
\prg_set_eq_conditional:NNn
  \pdfdict_if_exist:n \__pdfdict_if_exist:n { p , T , F , TF }
\prg_new_conditional:Npnn \__pdfdict_if_empty:n #1 { p , T , F , TF }
  {
    \prop_if_empty:cTF
      { \__pdfdict_name:n { #1 } }
      { \prg_return_true:  }
      { \prg_return_false: }
  }

\prg_set_eq_conditional:NNn
  \pdfdict_if_empty:n \__pdfdict_if_empty:n { p , T , F , TF }
\cs_new_protected:Npn \__pdfdict_put:nnn #1 #2 #3  %#1 (local) dict, #2 name, #3 value
  {
    \tl_if_blank:nTF { #3 }
      {
        \msg_warning:nnnn { pdfdict }{ empty-value }{ #2 } { #1 }
      }
      {
        \__pdfdict_if_exist:nTF  { #1 }
          {
            \exp_args:Nnx \prop_put:cnn
              { \__pdfdict_name:n { #1 } }{ \str_convert_pdfname:n { #2 } } { #3 }
          }
          {
            \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
          }
      }
  }

\cs_set_eq:NN \pdfdict_put:nnn \__pdfdict_put:nnn
\cs_generate_variant:Nn \pdfdict_put:nnn {nnx,nno}

\cs_new_protected:Npn \__pdfdict_gput:nnn #1 #2 #3  %#1 global dict, #2 name, #3 value
  {
    \tl_if_empty:nTF { #3 }
      {
        \msg_warning:nnnn { pdfdict }{ empty-value }{ #2 } { #1 }
      }
      {
        \__pdfdict_if_exist:nTF { #1 }
          {
            \exp_args:Nnx \prop_gput:cnn
              { \__pdfdict_name:n { #1 } }{ \str_convert_pdfname:n { #2 } } { #3 }
          }
          {
            \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
          }
      }
  }

\cs_set_eq:NN \pdfdict_gput:nnn \__pdfdict_gput:nnn
\cs_generate_variant:Nn \pdfdict_gput:nnn {nnx,nno}
\cs_new_protected:Npn \__pdfdict_get:nnN  #1 #2 #3 %dict,key,macro
  {
    \__pdfdict_if_exist:nTF { #1 }
      {
        \exp_args:Nnx \prop_get:cnN
          { \__pdfdict_name:n { #1 } }
          { \str_convert_pdfname:n { #2 } } #3
      }
      {
        \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
      }
  }

\cs_set_eq:NN \pdfdict_get:nnN \__pdfdict_get:nnN
\cs_new_protected:Npn \__pdfdict_remove:nn #1 #2 %dict,name
  {
    \__pdfdict_if_exist:nTF { #1 }
      {
        \exp_args:Nnx \prop_remove:cn
          { \__pdfdict_name:n  { #1 } }{ \str_convert_pdfname:n { #2 } }
      }
      {
        \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
      }
  }
\cs_set_eq:NN \pdfdict_remove:nn \__pdfdict_remove:nn

\cs_new_protected:Npn \__pdfdict_gremove:nn #1 #2 %dict,name
  {
    \__pdfdict_if_exist:nTF  { #1 }
       {
         \exp_args:Nnx \prop_gremove:cn
           { \__pdfdict_name:n  { #1 } }{ \str_convert_pdfname:n { #2 } }
       }
       {
         \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
       }
  }

\cs_set_eq:NN \pdfdict_gremove:nn \__pdfdict_gremove:nn
\cs_new_protected:Npn \__pdfdict_show:Nn #1#2 %#1 message command, #2 dict
  {
    \prop_if_exist:cTF { \__pdfdict_name:n { #2 } }
       {
         #1
            { pdfdict }
            { show-dict }
            { \tl_to_str:n {#2}  }
            { \prop_map_function:cN {\__pdfdict_name:n { #2 }} \msg_show_item:nn }
            { \__pdfdict_get_type:n{#2} }
            { }
       }
       {
         #1 { pdfdict } { unknown-dict } { #2 } {}{}{}
       }
  }
\cs_new_protected:Npn \pdfdict_show:n #1
  {
    \__pdfdict_show:Nn \msg_show:nnxxxx {#1}
  }
\cs_new:Npn \__pdfdict_item:nn #1 #2 %#1 name, #2 value
  {
    \tl_if_blank:nF {#2} { /#1~#2~ }
  }
\cs_generate_variant:Nn \__pdfdict_item:nn {ne}
\cs_set_eq:NN \pdfdict_item:nn \__pdfdict_item:nn
\cs_generate_variant:Nn \pdfdict_item:nn {ne}
\cs_new:Npn \__pdfdict_use:n #1  %#1 dict
  {
    \prop_map_function:cN { \__pdfdict_name:n { #1 } } \__pdfdict_item:ne
  }

\cs_set_eq:NN \pdfdict_use:n \__pdfdict_use:n
%% File: l3pdfmanagement.dtx


\msg_new:nnn  { pdfmanagement } { unknown-dict }
              { The~PDF~management~resource~'#1'~is~unknown. }

\msg_new:nnn  { pdfmanagement } { empty-value }
              { The~value~for~#1~is~empty~and~will~be~ignored }

\msg_new:nnn  { pdfmanagement } { no-removal }
              { It~is~not~possible~to~remove~values~from~'#1'.}

\msg_new:nnn  { pdfmanagement } { no-show }
              { It~is~not~possible~to~show~the~content~of~'#1'.}

\msg_new:nnn  { pdfmanagement } { name-exist }
              { The~name~'#1'~has~already~been~used~for~name~tree~'#2'.}

\msg_new:nnn  { pdfmanagement } { show-dict }
  {
    The~PDF~resource~'#1'~
    \tl_if_empty:nTF {#2}
      { is~empty \\>~ . }
      { contains~the~pairs~(without~outer~braces): #2 . }
  }
\msg_new:nnn  { pdfmanagement } { dict-already-defined  }
  {
    The~path~'#1'~is~already~defined.
  }
\msg_new:nnn  { pdfmanagement } { inactive  }
  {
    The~PDF~resources~management~is~not~active\\
    command~'#1'~ignored.
  }
\tl_new:N \l__pdfmanagement_tmpa_tl
\tl_new:N \l__pdfmanagement_tmpb_tl
\seq_new:N \l__pdfmanagement_tmpa_seq

\bool_new:N \g__pdfmanagement_active_bool
\prg_new_conditional:Npnn \__pdfmanagement_if_active:  { p , T , F , TF }
  {
    \bool_if:NTF \g__pdfmanagement_active_bool
      { \prg_return_true:  }
      { \prg_return_false: }
  }
\prg_set_eq_conditional:NNn
  \pdfmanagement_if_active: \__pdfmanagement_if_active: { p , T , F , TF }

\cs_set_eq:NN \IfPDFManagementActiveTF\pdfmanagement_if_active:TF
\hook_new:n {pdfmanagement/add}
\cs_new_protected:Npn \pdfmanagement_add:nnn #1 #2 #3
  {
    \__pdfmanagement_if_active:TF
      {
        \pdfdict_if_exist:nTF { g__pdf_Core/#1 }
          {
            \hook_gput_code:nnn
              {pdfmanagement/add}
              {pdfmanagement}
              {
                \__pdfmanagement_handler_gput:nnn { #1 }{ #2 }{ #3 }
              }
          }
          {
            \msg_error:nnn{pdfmanagement}{unknown-dict}{#1}
          }
      }
      {
        \msg_warning:nnx {pdfmanagement}{inactive}
          {\tl_to_str:n {\pdfmanagement_add:nnn}}
      }
  }

\cs_generate_variant:Nn \pdfmanagement_add:nnn {nnx,nxx,xxx}
\cs_set_eq:NN \PDFManagementAdd \pdfmanagement_add:xxx
\tl_new:N \g__kernel_pdfmanagement_thispage_shipout_code_tl
\tl_new:N \g__kernel_pdfmanagement_lastpage_shipout_code_tl
\tl_new:N \g__kernel_pdfmanagement_end_run_code_tl
\tl_gset:Nn \g__kernel_pdfmanagement_thispage_shipout_code_tl
  {
     \bool_if:NT \g__pdfmanagement_active_bool
       {
         \exp_args:NV \__pdf_backend_ThisPage_gpush:n      { \g_shipout_readonly_int }
         \exp_args:NV \__pdf_backend_PageResources_gpush:n { \g_shipout_readonly_int }
       }
  }

\tl_gset:Nn \g__kernel_pdfmanagement_end_run_code_tl
  {
    \bool_if:NT \g__pdfmanagement_active_bool
       {
         \__pdf_backend_PageResources_obj_gpush:          %ExtGState etc
         \__pdfmanagement_Pages_gpush:            %pagesattr
         \__pdfmanagement_Info_gpush:             %pdfinfo
         \__pdfmanagement_Catalog_gpush:
       }
  }



\cs_new_protected:Npn \__pdfmanagement_handler_gput:nnn #1 #2 #3  %#1 dict, #2 name, #3 value
  {
    \tl_if_empty:nTF { #3 }
      {
        \msg_none:nnn { pdfmanagement }{ empty-value }{ /#1/#2 }
      }
      {
        \pdfdict_if_exist:nTF { g__pdf_Core/#1 }
          {
            \cs_if_exist:cTF
              { __pdfmanagement_handler/#1/?_gput:nn } %general, name independant handler
              { \use:c {__pdfmanagement_handler/#1/?_gput:nn} {#2} {#3} }
              {
                \cs_if_exist:cTF
                  { __pdfmanagement_handler/#1/#2_gput:n }
                  { \use:c {__pdfmanagement_handler/#1/#2_gput:n} {#3} } %special handler
                  {
                    \exp_args:Nnx
                    \prop_gput:cnn
                      { \__kernel_pdfdict_name:n { g__pdf_Core/#1 } }
                      { \str_convert_pdfname:n { #2 } }
                      { #3 }
                  }
              }
          }
          {
            \msg_error:nnn { pdfmanagement } { unknown-dict } { #1 }
          }
      }
  }

\cs_generate_variant:Nn \__pdfmanagement_handler_gput:nnn {nxx}

\cs_new_protected:Npn \__pdfmanagement_get:nnN  #1 #2 #3 %path,key,macro
  {
    \exp_args:Nnx
    \prop_get:cnN
      { \__kernel_pdfdict_name:n { g__pdf_Core/#1 } }
      { \str_convert_pdfname:n {#2} } #3
  }

\cs_new_protected:Npn \__pdfmanagement_handler_gremove:nn #1 #2 %path,key
  {
    \pdfdict_if_exist:nTF { g__pdf_Core/#1 }
          {
            \cs_if_exist:cTF
              { __pdfmanagement_handler/#1/?_gremove:n } %general, name independant handler
              { \use:c {__pdfmanagement_handler/#1/?_gremove:n} {#2} }
              {
                \cs_if_exist:cTF
                  { __pdfmanagement_handler/#1/#2_gremove: }
                  { \use:c {__pdfmanagement_handler/#1/#2_gremove:} } %special handler
                  {
                    \exp_args:Nnx
                    \prop_gremove:cn
                      { \__kernel_pdfdict_name:n { g__pdf_Core/#1 } }
                      { \str_convert_pdfname:n {#2} }
                  }
              }
          }
          {
            \msg_error:nnn { pdfmanagement } { unknown-dict } { #1 }
          }
  }

\cs_new_protected:Npn \__pdfmanagement_gremove:nn #1 #2 %path,key
  {
    \pdfdict_if_exist:nTF { g__pdf_Core/#1 }
          {
            \exp_args:Nnx
            \prop_gremove:cn
              { \__kernel_pdfdict_name:n { g__pdf_Core/#1 } }
              { \str_convert_pdfname:n{#2} }
          }
          {
            \msg_error:nnn { pdfmanagement } { unknown-dict } { #1 }
          }
  }

\cs_new_protected:Npn \__pdfmanagement_show:Nn #1#2
  {
    \cs_if_exist:cTF
      { __pdfmanagement_handler/#2/?_show: } %general, name independant handler
      { \use:c {__pdfmanagement_handler/#2/?_show:} }
      {
        \prop_if_exist:cTF { \__kernel_pdfdict_name:n { g__pdf_Core/#2 } }
          {
             #1
               { pdfmanagement } { show-dict }
               { \tl_to_str:n {#2} }
               {
                 \prop_map_function:cN
                  {\__kernel_pdfdict_name:n { g__pdf_Core/#2 }}
                  \msg_show_item:nn
               }
               { } { }
          }
          {
            #1 { pdfmanagement } { unknown-dict } {#2}{}{}{}
          }
       }
  }

\cs_new_protected:Npn \__pdfmanagement_show:n #1  %path
  {
    \prop_show:c { \__kernel_pdfdict_name:n { g__pdf_Core/#1 } }
  }
\cs_new_protected:Npn \pdfmanagement_show:n #1
  {
    \__pdfmanagement_show:Nn \msg_show:nnxxxx {#1}
  }
\cs_new_protected:Npn \pdfmanagement_remove:nn #1 #2
  {
    \pdfdict_if_exist:nTF { g__pdf_Core/#1 }
      {
        \__pdfmanagement_handler_gremove:nn { #1 }{ #2 }
      }
      {
        \msg_error:nnn{pdfmanagement}{unknown-dict}{#1}
      }
  }
\cs_new_protected:Npn \pdfmanagement_get:nnN #1 #2 #3
  {
    \pdfdict_if_exist:nTF { g__pdf_Core/#1 }
      {
        \__pdfmanagement_get:nnN { #1 }{ #2 } #3
      }
      {
        \msg_error:nnn{pdfmanagement}{unknown-dict}{#1}
      }
  }
\pdfdict_new:n { g__pdf_Core/Info}
\cs_new_protected:Npn \__pdfmanagement_Info_gpush:
  {
    \prop_map_function:cN
      { \__kernel_pdfdict_name:n { g__pdf_Core/Info} }
      \__pdf_backend_info_gput:nn
    \prop_gclear:c { \__kernel_pdfdict_name:n { g__pdf_Core/Info} }
  }
\pdfdict_new:n { g__pdf_Core/Pages}
\cs_new_protected:Npn \__pdfmanagement_Pages_gpush:
  {
    \pdfdict_if_empty:nF { g__pdf_Core/Pages}
      {
        \exp_args:Nx \__pdf_backend_Pages_primitive:n
          {
            \pdfdict_use:n { g__pdf_Core/Pages}
          }
      }
  }

\pdfdict_new:n { g__pdf_Core/Page }
\pdfdict_new:n { g__pdf_Core/ThisPage }

\cs_new_protected:cpn { __pdfmanagement_handler/Page/?_gput:nn } #1 #2
  {
    \__pdf_backend_Page_gput:nn { #1 }{ #2 }
  }
\cs_new_protected:cpn { __pdfmanagement_handler/Page/?_gremove:n } #1
  {
    \__pdf_backend_Page_gremove:n { #1 }
  }

\cs_new_protected:cpn { __pdfmanagement_handler/ThisPage/?_gput:nn } #1 #2
  {
    \prop_gput:cnn  { \__kernel_pdfdict_name:n { g__pdf_Core/ThisPage } }{ #1 } { #2 }
    \bool_if:NT \g__pdfmanagement_active_bool
      {
        \__pdf_backend_ThisPage_gput:nn { #1 }{ #2 }
      }
  }

\cs_new_protected:cpn { __pdfmanagement_handler/ThisPage/?_gremove:n } #1
  {
    \msg_warning:nnn { pdfmanagement } { no-removal }{ThisPage}
  }

\cs_new_protected:cpn { __pdfmanagement_handler/ThisPage/?_show: }
  {
    \msg_warning:nnn { pdfmanagement } { no-show }{ThisPage}
  }

\clist_const:Nn \c__pdfmanagement_PageResources_clist
  {
    ExtGState,
    ColorSpace,
    Pattern,
    Shading,
  }

\clist_map_inline:Nn \c__pdfmanagement_PageResources_clist
  {
    \pdfdict_new:n { g__pdf_Core/Page/Resources/#1}
  }
\cs_new_protected:cpn { __pdfmanagement_handler/Page/Resources/ExtGState/?_gput:nn } #1 #2
  {
    \__pdf_backend_PageResources_gput:nnn {ExtGState} { #1 }{ #2 }
  }

\cs_new_protected:cpn { __pdfmanagement_handler/Page/Resources/ColorSpace/?_gput:nn } #1 #2
  {
    \__pdf_backend_PageResources_gput:nnn {ColorSpace} { #1 }{ #2 }
  }

\cs_new_protected:cpn { __pdfmanagement_handler/Page/Resources/Shading/?_gput:nn } #1 #2
  {
    \__pdf_backend_PageResources_gput:nnn {Shading} { #1 }{ #2 }
  }

\cs_new_protected:cpn { __pdfmanagement_handler/Page/Resources/Pattern/?_gput:nn } #1 #2
  {
    \__pdf_backend_PageResources_gput:nnn {Pattern} { #1 }{ #2 }
  }
\pdfdict_new:n { g__pdf_Core/Catalog}

\clist_const:Nn \c__pdfmanagement_Catalog_toplevel_clist
  {
    Collection,
    DPartRoot,
    Lang,
    Legal,
    Metadata,
    NeedsRendering,
    OCProperties/D,
    OpenAction,
    PageLabels,
    PageLayout,
    PageMode,
    Perms,
    PieceInfo,
    SpiderInfo,
    StructTreeRoot,
    Threads,
    URI,
    Version
  }

\clist_const:Nn \c__pdfmanagement_Catalog_sub_clist
  {
    AA,
    AcroForm,
    AcroForm/DR,
    AcroForm/DR/Font,
    MarkInfo,
    ViewerPreferences,
    OCProperties
  }

\clist_map_inline:Nn \c__pdfmanagement_Catalog_sub_clist
  {
    \pdfdict_new:n { g__pdf_Core/Catalog/#1}
  }

\clist_const:Nn \c__pdfmanagement_Catalog_seq_clist
  {
    AF,
    OCProperties/OCGs,
    OCProperties/Configs,
    OutputIntents,
    Requirements,
    AcroForm/Fields,
    AcroForm/CO
  }

\clist_const:Nn \c__pdfmanagement_Catalog_nametree_clist
  {
    AP,
    JavaScript,
  }
\clist_map_inline:Nn \c__pdfmanagement_Catalog_seq_clist
 {
   \seq_new:c { g__pdfmanagement_/Catalog/#1_seq } % new name later
   \cs_new_protected:cpn { __pdfmanagement_handler/Catalog/#1_gput:n } ##1
     {
       \seq_gput_right:cn { g__pdfmanagement_/Catalog/#1_seq } {  ##1  }
     }
 }

\cs_new_protected:cpn { __pdfmanagement_handler/Catalog/OCProperties/D_gput:n } #1
  {
    \seq_gput_left:cn
      { g__pdfmanagement_/Catalog/OCProperties/Configs_seq }
      {  #1  }
  }
\cs_new_protected:Npn \__pdfmanagement_nametree_add_aux:nnn #1 #2 #3
  %#1 name tree, #2 sanitized name #3 value
  {
     \prop_get:coNTF
       { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/Names/#1 }}
       { #2 }
       \l__pdfmanagement_tmpb_tl
       {
         \msg_error:nnnn{pdfmanagement}{name-exist}{#2}{#1}
       }
       {
         \prop_gput:con
           { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/Names/#1 }}
           { #2 }
           { #3 }
       }
  }
\clist_map_inline:Nn \c__pdfmanagement_Catalog_nametree_clist
 {
   \pdfdict_new:n { g__pdf_Core/Catalog/Names/#1}
   \cs_new_protected:cpn { __pdfmanagement_handler/Catalog/Names/#1/?_gput:nn } ##1 ##2
     {
       \pdf_string_from_unicode:nnN {utf8/string}{##1}\l__pdfmanagement_tmpa_tl
       \exp_args:Nno
         \__pdfmanagement_nametree_add_aux:nnn {#1}{\l__pdfmanagement_tmpa_tl}{##2}
     }
 }
\pdfdict_new:n { g__pdf_Core/Catalog/Names/EmbeddedFiles}
\cs_new_protected:cpn { __pdfmanagement_handler/Catalog/Names/EmbeddedFiles/?_gput:nn } #1 #2
  {
    \pdf_string_from_unicode:nnN {utf8/string}{#1}\l__pdfmanagement_tmpa_tl
    \exp_args:Nno
    \__pdfmanagement_nametree_add_aux:nnn
      {EmbeddedFiles}{\l__pdfmanagement_tmpa_tl}{#2}
    \exp_args:No
    \__pdf_backend_NamesEmbeddedFiles_add:nn {\l__pdfmanagement_tmpa_tl}{#2}
  }
\cs_new_protected:Npn \__pdfmanagement_Catalog_gpush:
  {
    \use:c { __pdfmanagement_/Catalog/AA_gpush: }
    \use:c { __pdfmanagement_/Catalog/AcroForm_gpush: }
    \use:c { __pdfmanagement_/Catalog/AF_gpush: }
    \use:c { __pdfmanagement_/Catalog/MarkInfo_gpush: }
    \pdfmeta_standard_verify:nT {Catalog_no_OCProperties}
      {
        \use:c { __pdfmanagement_/Catalog/OCProperties_gpush: }
      }
    \use:c { __pdfmanagement_/Catalog/OutputIntents_gpush: }
    \use:c { __pdfmanagement_/Catalog/Requirements_gpush: }
    \use:c { __pdfmanagement_/Catalog/ViewerPreferences_gpush: }
    % output the single values:
    \prop_map_function:cN
      { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog} }
      \__pdf_backend_catalog_gput:nn
    % output names tree:
    \use:c{ __pdfmanagement_/Catalog/Names_gpush:n } {EmbeddedFiles}
    \clist_map_inline:Nn \c__pdfmanagement_Catalog_nametree_clist
     {
      \use:c{ __pdfmanagement_/Catalog/Names_gpush:n } {##1}
     }
  }
\cs_new_protected:cpn { __pdfmanagement_/Catalog/AA_gpush: }
  {
    \prop_if_empty:cF
     { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/AA } }
     {
       \__pdf_backend_object_new:nn  { __pdfmanagement/Catalog/AA } { dict }
       \__pdf_backend_object_write:nx
            { __pdfmanagement/Catalog/AA }
            { \pdfdict_use:n { g__pdf_Core/Catalog/AA } }
       \exp_args:Nnx
         \__pdf_backend_catalog_gput:nn
           {AA}
           {
             \__pdf_backend_object_ref:n { __pdfmanagement/Catalog/AA }
           }
     }
  }
\cs_new_protected:cpn { __pdfmanagement_/Catalog/AcroForm_gpush: }
  {
    \seq_if_empty:cF { g__pdfmanagement_/Catalog/AcroForm/Fields_seq }
      {
        \__pdf_backend_object_new:nn  { __pdfmanagement/Catalog/AcroForm/Fields } { array }
        \__pdf_backend_object_write:nx
            { __pdfmanagement/Catalog/AcroForm/Fields }
            { \seq_use:cn { g__pdfmanagement_/Catalog/AcroForm/Fields_seq } {~} }
        \exp_args:Nnnx
          \prop_gput:cnn %we have to use \prop here to avoid the handler ...
            { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/AcroForm } }
            { Fields }
            { \__pdf_backend_object_ref:n { __pdfmanagement/Catalog/AcroForm/Fields } }
      }
    \seq_if_empty:cF { g__pdfmanagement_/Catalog/AcroForm/CO_seq }
      {
        \__pdf_backend_object_new:nn  { __pdfmanagement/Catalog/AcroForm/CO } { array }
        \exp_args:Nnx
          \__pdf_backend_object_write:nn
            { __pdfmanagement/Catalog/AcroForm/CO }
            { \seq_use:cn { g__pdfmanagement_/Catalog/AcroForm/CO_seq } {~} }
        \exp_args:Nnnx
          \prop_gput:cnn %we have to use \prop here to avoid the handler ...
            { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/AcroForm } }
            { CO }
            { \__pdf_backend_object_ref:n { __pdfmanagement/Catalog/AcroForm/CO } }
      }
     \prop_if_empty:cF { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/AcroForm/DR/Font}}
       {
         \__pdf_backend_object_new:nn { __pdfmanagement/Catalog/AcroForm/DR/Font } {dict}
         \exp_args:Nnx
           \__pdf_backend_object_write:nn
             { __pdfmanagement/Catalog/AcroForm/DR/Font }
             { \pdfdict_use:n { g__pdf_Core/Catalog/AcroForm/DR/Font } }
         \exp_args:Nnnx
           \prop_gput:cnn %we have to use \prop here to avoid the handler ...
             { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/AcroForm/DR } }
             { Font }
             { \__pdf_backend_object_ref:n { __pdfmanagement/Catalog/AcroForm/DR/Font } }
       }
     \prop_if_empty:cF { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/AcroForm/DR}}
       {
         \__pdf_backend_object_new:nn { __pdfmanagement/Catalog/AcroForm/DR } {dict}
         \exp_args:Nnx
           \__pdf_backend_object_write:nn
             { __pdfmanagement/Catalog/AcroForm/DR }
             { \pdfdict_use:n { g__pdf_Core/Catalog/AcroForm/DR } }
         \exp_args:Nnnx
           \prop_gput:cnn %we have to use \prop here to avoid the handler ...
             { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/AcroForm } }
             { DR }
             { \__pdf_backend_object_ref:n { __pdfmanagement/Catalog/AcroForm/DR } }
       }
     \prop_if_empty:cF { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/AcroForm} }
       {
         \__pdf_backend_object_new:nn { __pdfmanagement/Catalog/AcroForm } {dict}
         \exp_args:Nnx
           \__pdf_backend_object_write:nn
             { __pdfmanagement/Catalog/AcroForm }
             { \pdfdict_use:n { g__pdf_Core/Catalog/AcroForm } }
         \exp_args:Nnnx
           \__pdfmanagement_handler_gput:nnn
             { Catalog }
             { AcroForm }
             { \__pdf_backend_object_ref:n { __pdfmanagement/Catalog/AcroForm } }
       }
  }

\cs_new_protected:cpn { __pdfmanagement_/Catalog/AF_gpush: }
  {
    \seq_if_empty:cF
     { g__pdfmanagement_/Catalog/AF_seq }
     {
       \__pdf_backend_object_new:nn  { __pdfmanagement/Catalog/AF } { array }
       \exp_args:Nnx
         \__pdf_backend_object_write:nn
            { __pdfmanagement/Catalog/AF }
            { \seq_use:cn { g__pdfmanagement_/Catalog/AF_seq } {~} }
       \exp_args:Nnx
         \__pdf_backend_catalog_gput:nn
           {AF}
           {
             \__pdf_backend_object_ref:n {__pdfmanagement/Catalog/AF}
           }
     }
  }
\cs_new_protected:cpn { __pdfmanagement_/Catalog/MarkInfo_gpush: }
  {
    \prop_if_empty:cF
     { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/MarkInfo } }
     {
       \__pdf_backend_object_new:nn  { __pdfmanagement/Catalog/MarkInfo } { dict }
       \exp_args:Nnx
         \__pdf_backend_object_write:nn
            { __pdfmanagement/Catalog/MarkInfo }
            { \pdfdict_use:n { g__pdf_Core/Catalog/MarkInfo } }
       \exp_args:Nnx
         \__pdf_backend_catalog_gput:nn
           {MarkInfo}
           {
             \__pdf_backend_object_ref:n {__pdfmanagement/Catalog/MarkInfo}
           }
     }
  }
\cs_new_protected:cpn { __pdfmanagement_/Catalog/OCProperties_gpush: }
  {
   \int_compare:nNnT
      {
        ( \seq_count:c { g__pdfmanagement_/Catalog/OCProperties/OCGs_seq }  )*
        ( \seq_count:c { g__pdfmanagement_/Catalog/OCProperties/Configs_seq } )
      }
      >
      { 0 }
      {
        \__pdf_backend_object_new:nn  { __pdfmanagement/Catalog/OCProperties } { dict }
        \seq_gpop_left:cN { g__pdfmanagement_/Catalog/OCProperties/Configs_seq} \l__pdfmanagement_tmpa_tl
        \exp_args:Nnx
          \__pdf_backend_object_write:nn {__pdfmanagement/Catalog/OCProperties}
            {
              /OCGs~[ \seq_use:cn { g__pdfmanagement_/Catalog/OCProperties/OCGs_seq } {~} ]
              /D~\l__pdfmanagement_tmpa_tl~
              \seq_if_empty:cF { g__pdfmanagement_/Catalog/OCProperties/Configs_seq }
                {
                  /Configs~
                  [ \seq_use:cn { g__pdfmanagement_/Catalog/OCProperties/Configs_seq} {~} ]
                }
            }
        \exp_args:Nnx
          \__pdf_backend_catalog_gput:nn
            { OCProperties }
            { \__pdf_backend_object_ref:n {__pdfmanagement/Catalog/OCProperties} }
      }
  }
\cs_new_protected:cpn { __pdfmanagement_/Catalog/OutputIntents_gpush: }
  {
    \seq_if_empty:cF
     { g__pdfmanagement_/Catalog/OutputIntents_seq }
     {
       \__pdf_backend_object_new:nn  { __pdfmanagement/Catalog/OutputIntents } { array }
       \exp_args:Nnx
         \__pdf_backend_object_write:nn
            { __pdfmanagement/Catalog/OutputIntents }
            { \seq_use:cn { g__pdfmanagement_/Catalog/OutputIntents_seq } {~} }
       \exp_args:Nnx
         \__pdf_backend_catalog_gput:nn
           {OutputIntents}
           {
             \__pdf_backend_object_ref:n {__pdfmanagement/Catalog/OutputIntents}
           }
     }
  }
\cs_new_protected:cpn { __pdfmanagement_/Catalog/Requirements_gpush: }
  {
    \seq_if_empty:cF
     { g__pdfmanagement_/Catalog/Requirements_seq }
     {
       \__pdf_backend_object_new:nn  { __pdfmanagement/Catalog/Requirements } { array }
       \exp_args:Nnx
         \__pdf_backend_object_write:nn
            { __pdfmanagement/Catalog/Requirements }
            { \seq_use:cn { g__pdfmanagement_/Catalog/Requirements_seq } {~} }
       \exp_args:Nnx
         \__pdf_backend_catalog_gput:nn
           {Requirements}
           {
             \__pdf_backend_object_ref:n { __pdfmanagement/Catalog/Requirements }
           }
     }
  }
\cs_new_protected:cpn { __pdfmanagement_/Catalog/ViewerPreferences_gpush: }
  {
    \prop_if_empty:cF
     { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/ViewerPreferences } }
     {
       \__pdf_backend_object_new:nn  { __pdfmanagement/Catalog/ViewerPreferences } { dict }
       \exp_args:Nnx
         \__pdf_backend_object_write:nn
            { __pdfmanagement/Catalog/ViewerPreferences }
            { \pdfdict_use:n { g__pdf_Core/Catalog/ViewerPreferences } }
       \exp_args:Nnx
         \__pdf_backend_catalog_gput:nn
           {ViewerPreferences}
           {
             \__pdf_backend_object_ref:n {__pdfmanagement/Catalog/ViewerPreferences}
           }
     }
  }
\int_new:N \g__pdfmanagement_EmbeddedFiles_int
\cs_new:Npn \__pdfmanagement_EmbeddedFiles_name:
 {
   (
    l3ef
    \int_compare:nNnT {\g__pdfmanagement_EmbeddedFiles_int} < {10}
     {0}
    \int_compare:nNnT {\g__pdfmanagement_EmbeddedFiles_int} < {100}
     {0}
    \int_compare:nNnT {\g__pdfmanagement_EmbeddedFiles_int} < {1000}
     {0}
    \int_use:N \g__pdfmanagement_EmbeddedFiles_int
   )
 }
\pdfdict_new:n { g__pdf_Core/Catalog/Names }

\cs_new_protected:cpn { __pdfmanagement_handler/Catalog/Names/EmbeddedFiles_gput:n } #1
  {
    \int_gincr:N \g__pdfmanagement_EmbeddedFiles_int
    \exp_args:Nnx
    \prop_gput:cnn
      { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/Names/EmbeddedFiles }}
      { \__pdfmanagement_EmbeddedFiles_name: }
      { #1 }
     \exp_args:Nx
     \__pdf_backend_NamesEmbeddedFiles_add:nn {\__pdfmanagement_EmbeddedFiles_name:} { #1 }
  }
\cs_new_protected:cpn  { __pdfmanagement_/Catalog/Names_gpush:n } #1 %#1 name of name tree
 {
   \pdfdict_if_empty:nF { g__pdf_Core/Catalog/Names/#1 }
     {
       \seq_clear:N \l__pdfmanagement_tmpa_seq
       \prop_map_inline:cn
         {\__kernel_pdfdict_name:n { g__pdf_Core/Catalog/Names/#1 }}
         { \seq_put_right:Nn \l__pdfmanagement_tmpa_seq {##1~##2}}
       \seq_sort:Nn  \l__pdfmanagement_tmpa_seq
         {
           \str_compare:nNnTF {##1} > {##2}
             { \sort_return_swapped: }
             { \sort_return_same: }
         }
       \exp_args:Nnx \__pdf_backend_Names_gpush:nn
         {#1}
         {
           \seq_use:Nn \l__pdfmanagement_tmpa_seq {~}
         }
     }
 }
\cs_new_protected:cpn {__pdfmanagement_handler/Catalog/?_show:}
  {
    \iow_term:x
      {
        \iow_newline:
        The~Catalog~contains~in~the~top~level~the~single~value~entries
        \prop_map_function:cN
          {\__kernel_pdfdict_name:n { g__pdf_Core/Catalog }}
          \msg_show_item:nn
      }
    \clist_map_inline:Nn \c__pdfmanagement_Catalog_seq_clist
     {
       \seq_if_empty:cF {  g__pdfmanagement_/Catalog/##1_seq }
         {
           \iow_term:x
             {
               The~'##1'~array~contains~the~entries
               \seq_map_function:cN { g__pdfmanagement_/Catalog/##1_seq } \msg_show_item:n
             }
         }
      }
    \clist_map_inline:Nn \c__pdfmanagement_Catalog_sub_clist
      {
        \prop_if_empty:cF { \__kernel_pdfdict_name:n { g__pdf_Core/Catalog/##1 } }
          {
            \iow_term:x
              {
                The~Catalog~subdirectory~'##1'~contains~the~single~value~entries
                \prop_map_function:cN
                  {\__kernel_pdfdict_name:n { g__pdf_Core/Catalog/##1 }}
                  \msg_show_item:nn
              }
          }
      }
    \tl_show:x {\tl_to_str:n{\pdfmanagement_show:n{Catalog}}}
  }
\pdfdict_new:n { g__pdf_Core/Xform/Resources/Properties}
%% File: ltdocinit.dtx
\clist_new:N \g__pdfmanagement_firstaidoff_clist
\tl_new:N \g__pdfmanagement_testphase_tl
\cs_if_free:NT \DocumentMetadata
  {
    \keys_define:nn { document / metadata }
      {
        backend .choices:nn =
          { dvipdfmx , dvips , dvisvgm , luatex , pdftex , pdfmode , xdvipdfmx , xetex }
          {
            \sys_load_backend:n {#1}
          },
        backend .groups:n = { init } ,
      }

    \keys_define:nn { document / metadata }
      {
        ,pdfversion .code:n =
          {
            \pdf_version_gset:n { #1 }
            \AddToDocumentProperties[document]{pdfversion}{#1}
          }
        ,uncompress .code:n =
          {
            \pdf_uncompress:
          }
        ,uncompress .value_forbidden:n = true
        ,lang .code:n =
          {
            \pdfmanagement_add:nnn {Catalog} {Lang}{(#1)}
            \AddToDocumentProperties[document]{lang}{#1}
          }
        %,xmpmeta .bool_gset:N = \g_pdfmeta_xmp_bool %see pdfmeta unused and undefined for now!
        % this uses internal command from pdfmeta, it should probably move there ...
        ,pdfstandard .code:n =
          {
            \exp_args:Nnx
            \keys_set:nn {document / metadata} {_pdfstandard=\str_uppercase:n{#1}}
          }
        ,_pdfstandard .choices:nn =
          {A-1B,A-2A,A-2B,A-2U,A-3A,A-3B,A-3U}
          {
            \prop_if_exist:cT { g__pdfmeta_standard_pdf/#1_prop }
              {
                \prop_gset_eq:Nc \g__pdfmeta_standard_prop { g__pdfmeta_standard_pdf/#1 _prop }
              }
            \AddToDocumentProperties [document]{pdfstandard}{#1}
          }
        ,_pdfstandard / unknown .code:n =
          {
            \msg_warning:nnn{pdf}{unknown-standard}{#1}
          }
        ,testphase .multichoice:
        ,testphase / tagpdf .code:n =
          {
            \tl_gput_right:Nn\g__pdfmanagement_testphase_tl
              {
                \file_if_exist_input:nF {tagpdf-latex-lab-testphase.ltx}
                  {
                    \RequirePackage{tagpdf}
                    \AddToDocumentProperties [document]{testphase/tagpdf}{loaded}
                    \tagpdfsetup{activate,paratagging,interwordspace}
                    \AddToDocumentProperties [document]{tagging}{active}
                    \AddToDocumentProperties [document]{tagging/para}{active}
                    \AddToDocumentProperties [document]{tagging/interwordspace}{active}
                  }
              }
          }
        ,testphase / unknown .code:n =
          {
            \tl_gput_right:Nn\g__pdfmanagement_testphase_tl
               {
                 \file_if_exist_input:nF {#1-latex-lab-testphase.ltx}
                  {
                     \msg_warning:nnn{document}{latex-lab-pkg-missing}{#1}
                  }
               }
          }
        ,activate .multichoice:
        ,activate / tagging .code:n =
          {
            \PackageWarning{pdfmanagement-testphase}
             {The~activate~key~is~deprecated.\MessageBreak
              Tagging~is~activated~with~'testphase=tagpdf'~directly}{}
          }
        ,debug .code:n =
          {
            \keys_set:nn { document / metadata / debug } {#1}
          }
        ,debug / para .code:n =
          {
            \AddToHook
              {
                package/tagpdf/after
              }
              {
                 \tagpdfsetup{paratagging-show}
              }
          }
        ,debug / log .code:n =
          {
            \AddToHook
              {
               package/tagpdf/after
              }
              {
                 \tagpdfsetup{log=#1}
              }
          }
        ,debug / uncompress .code:n =
          {
            \pdf_uncompress:
          }
        ,debug / pdfmanagement .bool_gset:N = \g__pdfmanagement_active_bool
        ,debug / firstaidoff .clist_gset:N = \g__pdfmanagement_firstaidoff_clist
      }
   }
\cs_if_exist:NT \DocumentMetadata
   {
     \cs_new_protected:Npn \DeclareDocumentMetadata
        {
          \cs_if_free:NT \DocumentMetadata
            {
              \file_if_exist_input:nF {documentmetadata-support.ltx}%
               {
                 \@latex@error
                   {No~support~files~for~\noexpand\DeclareDocumentMetadata~found}
                   {
                     Is~the~'LaTeX-lab'~bundle~installed?
                     \MessageBreak
                     Without~it,~the~declaration~is~ignored.
                   }
                \let\DeclareDocumentMetadata\@gobble
               }
            }
          \RenewDocumentCommand \DeclareDocumentMetadata {}
           { \DocumentMetadata }
          \DocumentMetadata
        }
    }
\cs_if_free:NT \DocumentMetadata
  {
    \prop_gput:Nnn \g_msg_module_type_prop { document } { LaTeX }
    \prop_gput:Nnn \g_msg_module_name_prop { document } { DocumentMetadata }
    \msg_new:nnn
      { document } { setup-after-documentclass }
      {
        \token_to_str:N \DocumentMetadata \c_space_tl
        should~be~used~only~before~\token_to_str:N\documentclass
      }
    \msg_new:nnn
      { document } { latex-lab-pkg-missing }
      {
        LaTeX-lab~package~'#1'~not~found.
      }
    \NewDocumentCommand\DocumentMetadata { m }
      {
        \cs_if_eq:NNTF \documentclass \@twoclasseserror
          { \msg_error:nn { document }{ setup-after-documentclass } }
          {
            \bool_gset_true:N \g__pdfmanagement_active_bool
            \keys_set_groups:nnn { document / metadata} {init}{ #1 }
        %if no backend has been loaded force it now:
            \str_if_exist:NF \c_sys_backend_str
              {
                \sys_load_backend:n {}
              }
        \ExplSyntaxOn\makeatletter
              \file_input:n {l3backend-testphase-\c_sys_backend_str.def}
        \ExplSyntaxOff\makeatother
            \keys_set_filter:nnn  { document / metadata } { init } { lang=en-US, #1 }
            \bool_if:NT \g__pdfmanagement_active_bool
              {
                \PassOptionsToPackage{customdriver=hgeneric-testphase}{hyperref}
              }
           \hook_use_once:n {pdfmanagement/add}
           \RenewDocumentCommand\DocumentMetadata { m }
              {
                \keys_set_filter:nnn  { document / metadata } { init } { ##1 }
                \bool_if:NTF \g__pdfmanagement_active_bool
                  {
                    \str_remove_all:cn
                      {opt@hyperref.sty}{customdriver=hgeneric-testphase}
                    \PassOptionsToPackage
                      {customdriver=hgeneric-testphase}{hyperref}
                 }
                 {
                   \str_remove_all:cn
                     {opt@hyperref.sty}{customdriver=hgeneric-testphase}
                 }
              }
            \g__pdfmanagement_testphase_tl
            \RequirePackage{pdfmanagement-firstaid}
          }
      }
    \NewDocumentCommand\DeclareDocumentMetadata{}{\DocumentMetadata}
  }
\prop_new:N \g__pdfmanagement_documentproperties_prop %
\NewDocumentCommand\AddToDocumentProperties{O{\@currname}mm}
  {
    \exp_args:NNx
      \prop_gput:Nnn \g__pdfmanagement_documentproperties_prop
        {
          \tl_if_blank:eTF {#1}{top-level/}{#1/} #2
        }
        { #3}
  }
\NewExpandableDocumentCommand\GetDocumentProperties{m}
  {
    \prop_item:Nn \g__pdfmanagement_documentproperties_prop {#1}
  }
\msg_new:nnn  { pdfmanagement } { show-properties }
  {
    The~following~document~properties~have~been~stored:
    #1
  }
\NewDocumentCommand\ShowDocumentProperties {}
  {
    \msg_show:nnx {pdfmanagement}{show-properties}
      {
        \prop_map_function:NN \g__pdfmanagement_documentproperties_prop \msg_show_item:nn
      }
  }
%% File: l3pdfannot.dtx
\RequirePackage{l3bitset}
\bitset_new:Nn \l_pdfannot_F_bitset
  {
    Invisible      = 1,
    Hidden         = 2,
    Print          = 3,
    NoZoom         = 4,
    NoRotate       = 5,
    NoView         = 6,
    ReadOnly       = 7,
    Locked         = 8,
    ToggleNoView   = 9,
    LockedContents = 10
  }
\bool_new:N \g__pdfannot_use_lastlink_bool
\cs_new_protected:Npn \pdfannot_box:nnnn #1 #2 #3 #4
  {
    \__pdf_backend_annotation:nnnn {#1}{#2}{#3}{#4}
    \bool_gset_false:N\g__pdfannot_use_lastlink_bool
  }
\cs_generate_variant:Nn \pdfannot_box:nnnn {nnnx}
\cs_new:Npn \pdfannot_box_ref_last:
  {
    \__pdf_backend_annotation_last:
  }

 \pdfdict_new:n   { l__pdfannot/widget }
 \pdfdict_new:n   { l__pdfannot/widget/AA }
 \pdfdict_new:n   { l__pdfannot/widget/AP }
 \pdfdict_new:n   { l__pdfannot/widget/MK }
 \pdfdict_new:n   { l__pdfannot/widget/BS }
 \pdfdict_put:nnn { l__pdfannot/widget }{ Subtype }{ /Widget }
 \hook_new_pair:nn
   {pdfannot/widget/before}
   {pdfannot/widget/after}
\cs_new_protected:Npn \pdfannot_widget_box:nnn #1 #2 #3
  {
    \hook_use:n { pdfannot/widget/before }
    \group_begin:
    \pdfmeta_standard_verify:nT
      {annot_widget_no_AA}
      {
        \pdfdict_if_empty:nF { l__pdfannot/widget/AA }
          {
            \pdf_object_unnamed_write:nx {dict}{\pdfdict_use:n{l__pdfannot/widget/AA}}
            \pdfdict_put:nnx { l__pdfannot/widget }
              {AA}
              {\pdf_object_ref_last:}
          }
      }
    \pdfdict_if_empty:nF { l__pdfannot/widget/AP }
      {
        \pdf_object_unnamed_write:nx {dict}{\pdfdict_use:n{l__pdfannot/widget/AP}}
        \pdfdict_put:nnx { l__pdfannot/widget }
             {AP}
             {\pdf_object_ref_last:}
      }
    \pdfdict_if_empty:nF { l__pdfannot/widget/MK }
      {
        \pdf_object_unnamed_write:nx {dict}{\pdfdict_use:n{l__pdfannot/widget/MK}}
        \pdfdict_put:nnx { l__pdfannot/widget }
             {MK}
             {\pdf_object_ref_last:}
      }
    \pdfdict_if_empty:nF { l__pdfannot/widget/BS }
      {
        \pdf_object_unnamed_write:nx {dict}{\pdfdict_use:n{l__pdfannot/widget/BS}}
        \pdfdict_put:nnx { l__pdfannot/widget }
             {BS}
             {\pdf_object_ref_last:}
      }
    \pdfannot_box:nnnx {#1}{#2}{#3}
      {
        \pdfdict_use:n { l__pdfannot/widget}
      }
    \hook_use:n { pdfannot/widget/end }
    \group_end:
    \bool_gset_false:N\g__pdfannot_use_lastlink_bool
  }
\seq_const_from_clist:Nn \c_pdfannot_link_types_seq { URI , GoToR , Launch , GoTo, Named }
\seq_map_inline:Nn \c_pdfannot_link_types_seq
  {
    \pdfdict_new:n { l__pdfannot/link/#1 }
    \pdfdict_put:nnn { l__pdfannot/link/#1 }{ Subtype }{ /Link }
    \hook_new_pair:nn
      {pdfannot/link/#1/before}
      {pdfannot/link/#1/after}
    \hook_new_pair:nn
      {pdfannot/link/#1/begin}
      {pdfannot/link/#1/end}
   }
\cs_new_protected:Nn \pdfannot_link_off: { \__pdf_backend_link_off: }
\cs_new_protected:Nn \pdfannot_link_on:  { \__pdf_backend_link_on: }
\cs_new_protected:Nn \pdfannot_link:nnn %#1 type (URI, GoTo etc),
                                        %#2 action spec, #3 link text
  {
    \hook_use:n { pdfannot/link/#1/before}
    \mode_leave_vertical:
    \exp_args:Nxx %xetex needs expansion
    \__pdf_backend_link_begin_user:nnw
      {
         \pdfdict_if_exist:nT { l__pdfannot/link/#1 }
          {
            \pdfdict_use:n { l__pdfannot/link/#1}
          }
      }
      {
        #2 %exp_not?
      }
    \bool_gset_true:N \g__pdfannot_use_lastlink_bool
    \hook_use:n { pdfannot/link/#1/begin}
    #3
    \hook_use:n { pdfannot/link/#1/end}
    \__pdf_backend_link_end:
    \bool_gset_true:N \g__pdfannot_use_lastlink_bool
    \hook_use:n { pdfannot/link/#1/after}
  }
\cs_generate_variant:Nn \pdfannot_link:nnn {nxn}
\cs_new_protected:Npn \pdfannot_link_begin:nnw #1 #2 %#1 type, #2 action spec
  {
    \hook_use:n { pdfannot/link/#1/before}
    \mode_leave_vertical:
    \exp_args:Nxx %xetex needs expansion
      \__pdf_backend_link_begin_user:nnw
        {
           \pdfdict_if_exist:nT { l__pdfannot/link/#1 }
            {
              \pdfdict_use:n { l__pdfannot/link/#1}
            }
        }
        { #2 }
      \bool_gset_true:N \g__pdfannot_use_lastlink_bool
      \hook_use:n { pdfannot/link/#1/begin}
  }

\cs_new_protected:Nn \pdfannot_link_end:n %#1 type, e.g. url
  {
    \hook_use:n { pdfannot/link/#1/end}
    \__pdf_backend_link_end:
    \bool_gset_true:N \g__pdfannot_use_lastlink_bool
    \hook_use:n { pdfannot/link/#1/after}
  }
\cs_generate_variant:Nn \pdfannot_link_begin:nnw {nxw}
\cs_new_protected:Npn \pdfannot_link_goto_begin:nw #1 %#1 destination
  {
    \pdfdict_remove:nn { l__pdfannot/link/GoTo} {Subtype}
    \hook_use:n { pdfannot/link/GoTo/before} %the backend add it too
    \mode_leave_vertical:
    \exp_args:Nxx %xetex needs expansion
    \__pdf_backend_link_begin_goto:nnw
      {
        \pdfdict_use:n { l__pdfannot/link/GoTo}
      }
      { #1 }
     \bool_gset_true:N \g__pdfannot_use_lastlink_bool
    \pdfdict_put:nnn { l__pdfannot/link/GoTo} {Subtype}{GoTo}
     \hook_use:n { pdfannot/link/GoTo/begin}
  }

\cs_new_protected:Nn \pdfannot_link_goto_end:
  {
    \hook_use:n { pdfannot/link/GoTo/end}
    \__pdf_backend_link_end:
    \bool_gset_true:N \g__pdfannot_use_lastlink_bool
     \hook_use:n { pdfannot/link/GoTo/after}
  }
\cs_new:Nn \pdfannot_link_ref_last: { \__pdf_backend_link_last: }
\cs_new:Npn \pdfannot_ref_last:
  {
    \bool_if:NTF \g__pdfannot_use_lastlink_bool
      {
        \__pdf_backend_link_last:
      }
      {
        \__pdf_backend_annotation_last:
      }
  }
\cs_new_protected:Npn \pdfannot_link_margin:n #1
  {
    \__pdf_backend_link_margin:n { #1 }
  }
\cs_new_protected:Npn \pdfannot_dict_put:nnn #1 #2 #3
  {
    \pdfdict_put:nnn { l__pdfannot/#1 } { #2 }{ #3 }
  }
\cs_generate_variant:Nn \pdfannot_dict_put:nnn {nnx}
\cs_new_protected:Npn \pdfannot_dict_remove:nn #1 #2
  {
    \pdfdict_remove:nn { l__pdfannot/#1 } { #2 }
  }
\cs_new_protected:Npn \pdfannot_dict_show:n #1
  {
    \pdfdict_show:n { l__pdfannot/#1 }
  }

\cs_new:Npn \pdfannot_dict_use:n #1
  {
    \pdfdict_use:n { l__pdfannot/#1 }
  }
%% File: l3pdfxform.dtx
\cs_new_protected:Npn  \pdfxform_new:nnn #1 #2 #3
  {
    \__pdf_backend_xform_new:nnnn { #1 } { #2 } {  } { #3 }
  }
\cs_new_protected:Npn \pdfxform_use:n #1
  {
    \__pdf_backend_xform_use:n { #1 }
  }
\cs_new:Npn \pdfxform_ref:n #1
  {
    \__pdf_backend_xform_ref:n { #1 }
  }

\cs_generate_variant:Nn \pdfxform_ref:n {o}
\cs_new:Npn \pdfxform_wd:n #1
  {
    \tl_use:c { c__pdf_backend_xform_wd_ \tl_to_str:n { #1 } _tl }
  }

\cs_new:Npn \pdfxform_ht:n #1
  {
    \tl_use:c { c__pdf_backend_xform_ht_ \tl_to_str:n { #1 } _tl }
  }

\cs_new:Npn \pdfxform_dp:n #1
  {
    \tl_use:c { c__pdf_backend_xform_dp_ \tl_to_str:n { #1 } _tl }
  }
%% File: l3pdfmeta.dtx
\msg_new:nnn  {pdf }{unknown-standard}{The~standard~'#1'~is~unknown~and~has~been~ignored}
\tl_new:N\l__pdfmeta_tmpa_tl
\tl_new:N\l__pdfmeta_tmpb_tl
\str_new:N \l__pdfmeta_tmpa_str
\prop_new:N \g__pdfmeta_standard_prop
\cs_new:Npn \pdfmeta_standard_item:n #1
 {
   \prop_item:Nn \g__pdfmeta_standard_prop {#1}
 }
\cs_new_protected:Npn \pdfmeta_standard_get:nN #1 #2
 {
   \prop_get:NnN \g__pdfmeta_standard_prop {#1} #2
 }
\prg_new_conditional:Npnn \pdfmeta_standard_verify:n #1 {T,F,TF}
  {
     \prop_if_in:NnTF \g__pdfmeta_standard_prop {#1}
       {
         \prg_return_false:
       }
       {
         \prg_return_true:
       }
  }
\prg_new_protected_conditional:Npnn \pdfmeta_standard_verify:nn #1 #2  {T,F,TF}
  {
    \prop_if_in:NnTF \g__pdfmeta_standard_prop {#1}
      {
        \cs_if_exist:cTF {__pdfmeta_standard_verify_handler_#1:nn}
          {
            \exp_args:Nnnx
            \use:c
              {__pdfmeta_standard_verify_handler_#1:nn}
              { #2 }
              { \prop_item:Nn \g__pdfmeta_standard_prop {#1} }
          }
          {
            \prg_return_false:
          }
      }
      {
        \prg_return_true:
      }
   }
\cs_new_protected:Npn \__pdfmeta_standard_verify_handler_min_pdf_version:nn #1 #2
 {
   \pdf_version_compare:NnTF <
     { #2 }
     {\prg_return_false:}
     {\prg_return_true:}
 }

\cs_new_protected:Npn \__pdfmeta_standard_verify_handler_named_actions:nn #1 #2
 {
   \tl_if_in:nnTF { #2 }{ #1 }
     {\prg_return_true:}
     {\prg_return_false:}
 }
\cs_new_protected:Npn \__pdfmeta_standard_verify_handler_annot_action_A:nn #1 #2
 {
   \tl_if_in:nnTF { #2 }{ #1 }
     {\prg_return_true:}
     {\prg_return_false:}
 }
\cs_new_protected:Npn \__pdfmeta_standard_verify_handler_outputintent_subtype:nn #1 #2
 {
   \tl_if_eq:nnTF { #2 }{ #1 }
     {\prg_return_true:}
     {\prg_return_false:}
 }
\cs_new_protected:Npn \__pdfmeta_verify_pdfa_annot_flags:
  {
    \bitset_set_true:Nn  \l_pdfannot_F_bitset {Print}
    \bitset_set_false:Nn \l_pdfannot_F_bitset {Hidden}
    \bitset_set_false:Nn \l_pdfannot_F_bitset {Invisible}
    \bitset_set_false:Nn \l_pdfannot_F_bitset {NoView}
    \pdfannot_dict_put:nnn {link/URI}{F}{ \bitset_to_arabic:N \l_pdfannot_F_bitset }
    \pdfannot_dict_put:nnn {link/GoTo}{F}{ \bitset_to_arabic:N \l_pdfannot_F_bitset }
    \pdfannot_dict_put:nnn {link/GoToR}{F}{ \bitset_to_arabic:N \l_pdfannot_F_bitset }
    \pdfannot_dict_put:nnn {link/Launch}{F}{ \bitset_to_arabic:N \l_pdfannot_F_bitset }
    \pdfannot_dict_put:nnn {link/Named}{F}{ \bitset_to_arabic:N \l_pdfannot_F_bitset }
  }
\hook_gput_code:nnn {begindocument} {pdf}
  {
    \pdfmeta_standard_verify:nF { annot_flags }
     { \__pdfmeta_verify_pdfa_annot_flags: }
  }
\prop_new:c { g__pdfmeta_standard_pdf/A-1B_prop }
\prop_gset_from_keyval:cn { g__pdfmeta_standard_pdf/A-1B_prop }
  {
    ,name             = pdf/A-1B
    ,type             = A
    ,level            = 1
    ,conformance      = B
    ,year             = 2005
    ,min_pdf_version  = 1.4        %minimum
    ,no_encryption    =
    ,no_external_content =  % no F, FFilter, or FDecodeParms in stream dicts
    ,no_embed_content = % no EF key in filespec, no /Type/EmbeddedFiles
    ,max_string_size  = 65535
    ,max_array_size   = 8191
    ,max_dict_size    = 4095
    ,max_obj_num      = 8388607
    ,max_nest_qQ      = 28
    ,named_actions    = {NextPage, PrevPage, FirstPage, LastPage}
    ,annot_flags      =
    %booleans. Only the existence of the key matter.
    %If the entry is added it means a requirements is there
    %(in most cases "don't use ...")
    %
    %===============
    % Rule 6.1.13-1 CosDocument, isOptionalContentPresent == false
      ,Catalog_no_OCProperties =
    %===============
    % Rule 6.6.1-1: PDAction, S == "GoTo" || S == "GoToR" || S == "Thread"
    %               || S == "URI" || S == "Named" || S == "SubmitForm"
    % means: no /S/Launch, /S/Sound, /S/Movie, /S/ResetForm, /S/ImportData,
    %        /S/JavaScript, /S/Hide
      ,annot_action_A        = {GoTo,GoToR,Thread,URI,Named,SubmitForm}
    %===============
    % Rule 6.6.2-1: PDAnnot, Subtype != "Widget" || AA_size == 0
    % means: no AA dictionary
      ,annot_widget_no_AA      =
    %===============
    % Rule 6.9-2: PDAnnot, Subtype != "Widget" || (A_size == 0 && AA_size == 0)
    % (looks like a tightening of the previous rule)
      ,annot_widget_no_A_AA    =
    %===============
    % Rule 6.9-1 PDAcroForm, NeedAppearances == null || NeedAppearances == false
    ,form_no_NeedAppearances =
    %===============
    %Rule 6.9-3 PDFormField, AA_size == 0
    ,form_no_AA              =
    %===============
    % to be continued https://docs.verapdf.org/validation/pdfa-part1/
    % - Outputintent/colorprofiles requirements
    % an outputintent should be loaded and is unique.
    ,outputintent_A         = {GTS_PDFA1}
    % - no Alternates key in image dictionaries
    % - no OPI, Ref, Subtype2 with PS key in xobjects
    % - Interpolate  = false in images
    % - no TR, TR2 in ExtGstate
  }

\prop_new:c { g__pdfmeta_standard_pdf/A-2B_prop }
\prop_gset_eq:cc
  { g__pdfmeta_standard_pdf/A-2B_prop }
  { g__pdfmeta_standard_pdf/A-1B_prop }
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-2B_prop }{name}{pdf/A-2B}
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-2B_prop }{year}{2011}
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-2B_prop }{level}{2}
\prop_gremove:cn
  { g__pdfmeta_standard_pdf/A-2B_prop }
  { embed_content}

\prop_new:c { g__pdfmeta_standard_pdf/A-2U_prop }
\prop_gset_eq:cc
  { g__pdfmeta_standard_pdf/A-2U_prop }
  { g__pdfmeta_standard_pdf/A-2B_prop }
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-2U_prop }{name}{pdf/A-2U}
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-2U_prop }{conformance}{U}
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-2U_prop }{unicode}{}

\prop_new:c { g__pdfmeta_standard_pdf/A-2A_prop }
\prop_gset_eq:cc
  { g__pdfmeta_standard_pdf/A-2A_prop }
  { g__pdfmeta_standard_pdf/A-2B_prop }
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-2A_prop }{name}{pdf/A-2A}
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-2A_prop }{conformance}{A}
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-2A_prop }{tagged}{}

\prop_new:c { g__pdfmeta_standard_pdf/A-3B_prop }
\prop_gset_eq:cc
  { g__pdfmeta_standard_pdf/A-3B_prop }
  { g__pdfmeta_standard_pdf/A-2B_prop }
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-3B_prop }{name}{pdf/A-3B}
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-3B_prop }{year}{2012}
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-3B_prop }{level}{3}
\prop_gremove:cn
  { g__pdfmeta_standard_pdf/A-3B_prop }
  { embed_content}
\prop_new:c { g__pdfmeta_standard_pdf/A-3U_prop }
\prop_gset_eq:cc
  { g__pdfmeta_standard_pdf/A-3U_prop }
  { g__pdfmeta_standard_pdf/A-3B_prop }
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-3U_prop }{name}{pdf/A-3U}
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-3U_prop }{conformance}{U}
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-3U_prop }{unicode}{}

\prop_new:c { g__pdfmeta_standard_pdf/A-3A_prop }
\prop_gset_eq:cc
  { g__pdfmeta_standard_pdf/A-3A_prop }
  { g__pdfmeta_standard_pdf/A-3B_prop }
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-3A_prop }{name}{pdf/A-3A}
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-3A_prop }{conformance}{A}
\prop_gput:cnn
  { g__pdfmeta_standard_pdf/A-3A_prop }{tagged}{}
\prop_new:N \g__pdfmeta_outputintents_prop
\keys_define:nn { document / metadata }
  {
    colorprofiles .code:n =
     {
       \keys_set:nn { document / metadata / colorprofiles }{#1}
     }
  }
\keys_define:nn { document / metadata / colorprofiles }
 {
   ,A .code:n =
      {
        \tl_if_blank:nF {#1}
          {
            \prop_gput:Nnn \g__pdfmeta_outputintents_prop
             { GTS_PDFA1  } {#1}
          }
      }
   ,a .code:n =
      {
        \tl_if_blank:nF {#1}
          {
            \prop_gput:Nnn \g__pdfmeta_outputintents_prop
              { GTS_PDFA1  } {#1}
          }
      }
   ,X .code:n =
      {
        \tl_if_blank:nF {#1}
          {
             \prop_gput:Nnn \g__pdfmeta_outputintents_prop
              { GTS_PDFX  } {#1}
          }
      }
   ,x .code:n =
      {
        \tl_if_blank:nF {#1}
          {
            \prop_gput:Nnn \g__pdfmeta_outputintents_prop
              { GTS_PDFX  } {#1}
          }
      }
   ,unknown .code:n =
     {
       \tl_if_blank:nF {#1}
          {
           \exp_args:NNo
            \prop_gput:Nnn \g__pdfmeta_outputintents_prop
              { \l_keys_key_str  } {#1}
          }
     }
 }
\pdfdict_new:n   {l_pdfmeta/outputintent}
\pdfdict_put:nnn {l_pdfmeta/outputintent}
  {Type}{/OutputIntent}
\prop_const_from_keyval:cn { c__pdfmeta_colorprofile_sRGB.icc}
  {
    ,OutputConditionIdentifier=IEC~sRGB
    ,Info=IEC~61966-2.1~Default~RGB~colour~space~-~sRGB
    ,RegistryName=http://www.iec.ch
    ,N = 3
  }
\prop_const_from_keyval:cn { c__pdfmeta_colorprofile_FOGRA39L_coated.icc}
  {
    ,OutputConditionIdentifier=FOGRA39L~Coated
    ,Info={Offset~printing,~according~to~ISO~12647-2:2004/Amd~1,~OFCOM,~ %
           paper~type~1~or~2~=~coated~art,~115~g/m2,~tone~value~increase~
           curves~A~(CMY)~and~B~(K)}
    ,RegistryName=http://www.fogra.org
    ,N = 4
  }
\cs_new_protected:Npn \__pdfmeta_embed_colorprofile:n #1%#1 file name
  {
    \pdf_object_if_exist:nF { __color_icc_ #1 }
      {
        \pdf_object_new:nn  { __color_icc_ #1 }{fstream}
        \pdf_object_write:nx { __color_icc_ #1 }
         {
           {/N\c_space_tl
             \prop_item:cn{c__pdfmeta_colorprofile_#1}{N}
           }
           {#1}
         }
      }
  }

\cs_new_protected:Npn \__pdfmeta_write_outputintent:nn #1 #2 %#1 file name, #2 subtype
  {
    \group_begin:
     \pdfdict_put:nnx {l_pdfmeta/outputintent}{S}{/\str_convert_pdfname:n{#2}}
     \pdfdict_put:nnx {l_pdfmeta/outputintent}
       {DestOutputProfile}
       {\pdf_object_ref:n{ __color_icc_ #1 }}
     \clist_map_inline:nn { OutputConditionIdentifier, Info, RegistryName }
       {
         \prop_get:cnNT
          { c__pdfmeta_colorprofile_#1}
          { ##1 }
          \l__pdfmeta_tmpa_tl
          {
            \pdf_string_from_unicode:nVN {utf8/string}\l__pdfmeta_tmpa_tl\l__pdfmeta_tmpa_str
            \pdfdict_put:nnx
              {l_pdfmeta/outputintent}{##1}{\l__pdfmeta_tmpa_str}
          }
       }
     \pdf_object_unnamed_write:nx {dict}{\pdfdict_use:n {l_pdfmeta/outputintent} }
     \pdfmanagement_add:nnx {Catalog}{OutputIntents}{\pdf_object_ref_last:}
    \group_end:
  }

\AddToHook{begindocument/end}
  {
    \pdfmeta_standard_verify:nTF {outputintent_A}
      {
         \prop_map_inline:Nn \g__pdfmeta_outputintents_prop
           {
             \__pdfmeta_embed_colorprofile:n
               {#2}
             \__pdfmeta_write_outputintent:nn
               {#2}
               {#1}
           }
      }
      {
         \exp_args:NNx
         \prop_if_in:NnF
           \g__pdfmeta_outputintents_prop
           { \pdfmeta_standard_item:n { outputintent_A } }
           {
             \exp_args:NNx
             \prop_gput:Nnn
               \g__pdfmeta_outputintents_prop
               { \pdfmeta_standard_item:n { outputintent_A } }
               { sRGB.icc }
           }
         \exp_args:NNx
         \prop_get:NnN
           \g__pdfmeta_outputintents_prop
           { \pdfmeta_standard_item:n { outputintent_A } }
           \l__pdfmeta_tmpb_tl
         \exp_args:NV \__pdfmeta_embed_colorprofile:n \l__pdfmeta_tmpb_tl
         \prop_map_inline:Nn \g__pdfmeta_outputintents_prop
           {
             \exp_args:NV
             \__pdfmeta_write_outputintent:nn
               \l__pdfmeta_tmpb_tl
               { #1 }
           }
       }
   }
%% File: l3pdftools.dtx
\cs_generate_variant:Nn \str_convert_pdfname:n { e }

\cs_new:Npn \pdf_name_from_unicode_e:n #1
  {
    \__kernel_pdf_name_from_unicode_e:n { #1 }
  }

\cs_generate_variant:Nn \pdf_name_from_unicode_e:n {V}
\bool_lazy_any:nTF
  {
    \sys_if_engine_luatex_p:
    \sys_if_engine_xetex_p:
  }
  {
    \prop_gput:Nnn \g__str_alias_prop { default } {  }
  }
  {
    \prop_gput:Nnn \g__str_alias_prop { default } { utf8 }
  }
\cs_new:Npn \pdf_string_from_unicode:nnN #1 #2 #3
  {
    \cs_if_exist_use:cF { __pdf_string_from_unicode_#1:nN }
      {
        \__kernel_msg_error:nnn { pdf } { unknown-convert } {#1}
        \use_none:nn
      }
    { #2 } #3
  }

\cs_generate_variant:Nn \pdf_string_from_unicode:nnN {nVN}
%% TODO Names need a review when it is clear which converters
%% are actually needed
%% string conversions and printing
%% we assume here that the text purify step has been done. The input is
%% a list of (utf8) chars.
%% str convert, not expandable.


\cs_new_protected:cpn { __pdf_string_from_unicode_utf8/string-raw:nN }  #1 #2
  {
     \str_set_convert:Nnnn #2
        { #1 }
        { default }
        {utf8/string}
  }

\cs_new_protected:cpn { __pdf_string_from_unicode_utf8/string:nN } #1 #2
  {
    \use:c { __pdf_string_from_unicode_utf8/string-raw:nN } { #1 } #2
    \str_put_left:Nn #2  {(}
    \str_put_right:Nn #2 {)}
  }
\cs_new_protected:cpx { __pdf_string_from_unicode_utf8/URI-raw:nN }  #1 #2
  {
     \exp_not:N \str_set_convert:Nnnn #2
       { #1 }
       { default }
       {utf8/url}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 3A} {:}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 2F} {/}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 23} {\c_hash_str}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 5B} {[}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 5D} {]}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 40} {\c_atsign_str}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 21} {!}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 24} {\c_dollar_str}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 26} {\c_ampersand_str}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 27} {'}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 2A} {*}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 2B} {+}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 2C} {,}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 3B} {;}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 3D} {=}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 3F} {?}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 30} {0}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 31} {1}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 32} {2}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 33} {3}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 34} {4}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 35} {5}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 36} {6}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 37} {7}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 38} {8}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 39} {9}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 28} {\c_backslash_str(}
     \exp_not:N \str_replace_all:Nnn #2 {\c_percent_str 29} {\c_backslash_str)}
  }

\cs_new_protected:cpn { __pdf_string_from_unicode_utf8/URI:nN } #1 #2
  {
     \use:c { __pdf_string_from_unicode_utf8/URI-raw:nN } {#1} #2
     \str_put_left:Nn #2  {(}
     \str_put_right:Nn #2 {)}
  }
\cs_new_protected:cpn { __pdf_string_from_unicode_utf16/string-raw:nN }  #1 #2
  {
    \str_set_convert:Nnnn #2
      { #1 }
      { default }
      {utf16/string}
  }

\cs_new_protected:cpn { __pdf_string_from_unicode_utf16/string:nN } #1 #2
  {
    \use:c { __pdf_string_from_unicode_utf16/string-raw:nN } {#1} #2
    \str_put_left:Nn #2  {(}
    \str_put_right:Nn #2 {)}
  }

\cs_new_protected:cpn { __pdf_string_from_unicode_utf16/hex-raw:nN } #1 #2
  {
     \str_set_convert:Nnnn #2
       { #1 }
       { default }
       {utf16/hex}
  }

\cs_new_protected:cpn { __pdf_string_from_unicode_utf16/hex:nN } #1 #2
  {
    \use:c { __pdf_string_from_unicode_utf16/hex-raw:nN } {#1} #2
    \str_put_left:Nn #2  {<}
    \str_put_right:Nn #2 {>}
  }

\cs_new_protected:Npn  \pdf_bdc:nn #1 #2 { \__pdf_backend_bdc:nn { #1 }{ #2 } }
\cs_new_protected:Npn  \pdf_bdcobject:nn #1 #2 { \__pdf_backend_bdcobject:nn { #1 }{ #2 } }
\cs_new_protected:Npn  \pdf_bdcobject:n #1     { \__pdf_backend_bdcobject:n  { #1 } }
\cs_new_protected:Npn  \pdf_bmc:n #1     { \__pdf_backend_bmc:n  { #1 } }
\cs_new_protected:Npn  \pdf_emc:         { \__pdf_backend_emc: }
%% File: l3pdffile.dtx

\cs_new_protected:Npn \__pdffile_filename_convert_to_print:nN #1 #2
  {\pdf_string_from_unicode:nnN {utf16/hex}{#1}{#2}}
\msg_new:nnn { pdffile } { file-not-found }
  {
    File~'\tl_to_str:n{#1}'~not~found
  }

\msg_new:nnn { pdffile } { mimetype-missing }
  {
    Mime~type~not~set~for~file~'\tl_to_str:n{#1}'
  }

\msg_new:nnn { pdffile } { target-name-missing }
  {
    a~target~name~for~the~/Filespec~dictionary~is~missing.
  }

\msg_new:nnn { pdffile } { object-exists }
  {
    object~name~'#1'~is~already~used.
  }

\msg_new:nnn  { pdffile } { show-files }
  {
    The~following~files~have~been~embedded\\
     #1
  }
\tl_new:N  \l__pdffile_tmpa_tl
\tl_new:N  \l__pdffile_tmpb_tl
\str_new:N \l__pdffile_tmpa_str
\str_new:N \l__pdffile_tmpb_str
\str_new:N \l__pdffile_ext_str
\tl_new:N  \l__pdffile_automimetype_tl
\tl_new:N  \l__pdffile_embed_ref_tl
\prop_new:N \g_pdffile_mimetypes_prop
\prop_gset_from_keyval:Nn \g_pdffile_mimetypes_prop
  {
    ,.css = text/css
    ,.csv = text/csv
    ,.html= text/html
    ,.dtx = text/plain %or application/x-tex, not in iana.org list
    ,.eps = application/postscript
    ,.jpg = image/jpeg
    ,.mp4 = video/mp4
    ,.pdf = application/pdf
    ,.png = image/png
    ,.tex = text/plain %or application/x-tex, not in iana.org list
    ,.txt = text/plain
    ,.sty = text/plain
    ,.xml = text/xml
  }
\str_new:N  \l_pdffile_source_name_str
\pdfdict_new:n { l_pdffile }
\pdfdict_put:nnn { l_pdffile }{Type}{/EmbeddedFile}
\pdfdict_new:n { l_pdffile/Params }
\pdfdict_put:nnn { l_pdffile/Params }
  {ModDate}  { (\file_timestamp:n { \l_pdffile_source_name_str }) }
\pdfdict_put:nnn { l_pdffile/Params }
  {Size}     { \file_size:n { \l_pdffile_source_name_str } }
\pdfdict_put:nnn { l_pdffile/Params }
  {CheckSum} { (\file_mdfive_hash:n { \l_pdffile_source_name_str }) }
\pdfdict_new:n { l_pdffile/streamParams }
\pdfdict_put:nnn { l_pdffile/streamParams }
  {ModDate}  {
               (
                 D:\int_use:N\c_sys_year_int
                  \int_compare:nNnT{\c_sys_month_int}<{10}{0}
                  \int_use:N\c_sys_month_int
                  \int_compare:nNnT{\c_sys_day_int}<{10}{0}
                  \int_use:N\c_sys_day_int
               )
             }
\pdfdict_new:n { l_pdffile/Filespec }
\pdfdict_put:nnn { l_pdffile/Filespec }
  {Type} { /Filespec }
\pdfdict_put:nnn { l_pdffile/Filespec }
  {AFRelationship} { /Unspecified }

\prop_new:N \g_pdffile_embed_prop
\cs_new_protected:Npn \pdffile_embed_show:
  {
    \msg_show:nnx
     {pdffile}{show-files}
     {
       \prop_map_function:NN {\g_pdffile_embed_prop} \msg_show_item:nn
     }
  }
\cs_new_protected:Npn \__pdffile_mimetype_set:nN #1 #2
  {
    \file_parse_full_name:nNNN
          {#1}
          \l__pdffile_tmpa_str %unused
          \l__pdffile_tmpb_str %unused
          \l__pdffile_ext_str
        %check if Subtype has been set
        \pdfdict_get:nnN { l_pdffile}{Subtype}\l__pdffile_tmpa_tl
        %if not look up in the prop:
        \quark_if_no_value:NT \l__pdffile_tmpa_tl
          {
            \prop_get:NVNTF
              \g_pdffile_mimetypes_prop
              \l__pdffile_ext_str
              \l__pdffile_tmpb_tl
              {
                \tl_set:Nx #2 {/Subtype~\pdf_name_from_unicode_e:V \l__pdffile_tmpb_tl}
              }
              {
                \msg_warning:nnx { pdffile }{ mimetype-missing} {#1}
                \tl_clear:N #2
              }
          }
    }

\cs_generate_variant:Nn \__pdffile_mimetype_set:nN {VN}

\cs_new_protected:Npn \__pdffile_fstream_write:nN #1 #2
  {
    \pdf_object_unnamed_write:nx { fstream }
      {
        {
          #2
          \pdfdict_use:n { l_pdffile}
          \pdfdict_if_empty:nF { l_pdffile/Params}
            {
              /Params
                <<
                  \pdfdict_use:n { l_pdffile/Params}
                >>
            }
        }
        { #1 }
      }
     \tl_clear:N \l__pdffile_automimetype_tl
   }

\cs_generate_variant:Nn \__pdffile_fstream_write:nN {VN}

\cs_new_protected:Npn \__pdffile_stream_write:nN #1 #2
  {
    \pdf_object_unnamed_write:nx { stream }
      {
        {
          #2
          \pdfdict_use:n { l_pdffile}
          \pdfdict_if_empty:nF { l_pdffile/streamParams}
            {
              /Params
                <<
                  \pdfdict_use:n { l_pdffile/streamParams}
                >>
            }
        }
        { \exp_not:n { #1 } }
      }
     \tl_clear:N \l__pdffile_automimetype_tl
   }

\cs_generate_variant:Nn \__pdffile_stream_write:nN {VN}

\cs_new_protected:Npn \__pdffile_filespec_write:nnn #1 #2 #3
  {
    \tl_if_blank:nT { #2 }
      {
        \msg_error:nn {pdffile}{target-name-missing}
      }
      {
        \group_begin:
          \__pdffile_filename_convert_to_print:nN  { #2 } \l__pdffile_tmpa_str
          \pdfdict_put:nnx {l_pdffile/Filespec}{F} { \l__pdffile_tmpa_str }
          \pdfdict_put:nnx {l_pdffile/Filespec}{UF}{ \l__pdffile_tmpa_str }
          \pdf_object_write:nx { #1 }
            {
              \pdfdict_use:n { l_pdffile/Filespec}
              \tl_if_empty:nF { #3 }
                {
                  /EF <</F~#3 /UF~#3>>
                }
            }
        \group_end:
      }
  }

\cs_set_eq:NN \pdffile_filespec:nnn \__pdffile_filespec_write:nnn
\cs_generate_variant:Nn \pdffile_filespec:nnn {nnx}
\cs_new_protected:Npn \pdffile_embed_file:nnn #1 #2 #3
  { %               if #1 empty => only filespec
    %               if #2 empty => = #1
    \pdf_object_if_exist:nTF { #3 }
      {
        \msg_error:nnn { pdffile }{ object-exists } { #3 }
      }
      {
        \tl_if_blank:nTF { #1 }
          {
            \tl_set:Nn \l__pdffile_embed_ref_tl {}
          }
          {
            \file_get_full_name:nNTF {#1} \l_pdffile_source_name_str
              {
                \__pdffile_mimetype_set:VN
                  \l_pdffile_source_name_str
                  \l__pdffile_automimetype_tl
                \__pdffile_fstream_write:VN
                  \l_pdffile_source_name_str
                  \l__pdffile_automimetype_tl
                \tl_set:Nx \l__pdffile_embed_ref_tl { \pdf_object_ref_last: }
              }
              {
                \msg_error:nnn { pdffile }{ file-not-found }{ #1 }
              }

           }
        \prop_gput:Nnx
           \g_pdffile_embed_prop
           { #3 }
           {
             { \tl_if_blank:nTF { #1 } {filespec}{file} }
             {\l_pdffile_source_name_str}
             {
               \tl_if_blank:nTF { #2 }
                 { \l_pdffile_source_name_str }
                 { \tl_to_str:n{#2}}
             }
           }
        \tl_if_blank:nTF { #2 }
          {
            \pdf_object_new:nn   { #3 } {dict}
            \exp_args:Nnnx
              \__pdffile_filespec_write:nnn
                %#1 dict, #2 target file name, #3 object ref
                { #3 }
                { #1 }
                {\l__pdffile_embed_ref_tl}
          }
          {
            \pdf_object_new:nn   { #3 } {dict}
            \exp_args:Nnnx
              \__pdffile_filespec_write:nnn
                %#1 dict, #2 target file name, #3 object ref
                { #3 }
                { #2 }
                {\l__pdffile_embed_ref_tl}
          }
      }
  }

\cs_new_protected:Npn \pdffile_embed_stream:nnn #1 #2 #3
  {
    %               if #2 empty => error
    \pdf_object_if_exist:nTF { #3 }
      {
         \msg_error:nnn { pdffile }{ object-exists } { #3 }
      }
      {
         \prop_gput:Nnx
            \g_pdffile_embed_prop
            { #3 }
            {{stream}{}{\tl_if_blank:nTF {#2}{stream.txt}{\exp_not:n{#2}}}}
         \tl_if_blank:nTF {#2}
          { \__pdffile_mimetype_set:nN {stream.txt}\l__pdffile_automimetype_tl}
          { \__pdffile_mimetype_set:nN { #2 } \l__pdffile_automimetype_tl }
         \__pdffile_stream_write:nN
           { #1 }
           \l__pdffile_automimetype_tl
         \tl_set:Nx \l__pdffile_embed_ref_tl { \pdf_object_ref_last: }
         \pdf_object_new:nn   { #3 } {dict}
         \exp_args:Nnxx
           \__pdffile_filespec_write:nnn
             %#1 dict, #2 target file name, #3 object ref
             { #3 }
             { \tl_if_blank:nTF {#2}{stream.txt}{\exp_not:n{#2}} }
             {\l__pdffile_embed_ref_tl}
     }
  }

%% 
%%
%% End of file `pdfmanagement-testphase.ltx'.
