%%
%% This is file `documentmetadata-support.ltx',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% documentmetadata-support.dtx  (with options: `code')
%% 
%% This is a generated file.
%% 
%% Copyright 2021-2025 LaTeX Project
%% 
%% This file was generated from file(s) of the  `LaTeX-lab Bundle'.
%% ------------------------------------------------------------------------------------
%% 
%% It may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%    http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2008 or later.
%% 
%% This file may only be distributed together with a copy of the LaTeX
%% `LaTeX-lab Bundle'. You may however distribute the `LaTeX-lab Bundle'
%% without such generated files.
%% 
%% The newest sources can be found below
%% 
%%    https://github.com/latex3/latex2e/required/latex-lab
%% 
%% where one can also log issues in case there are any.
%% 
%% 
%% File: documentmetadata-support.dtx
\def\documentmetadatasupportversion{1.0t}
\def\documentmetadatasupportdate{2025-09-25}


\RequirePackage{pdfmanagement-testphase}
\ExplSyntaxOn\makeatletter

\clist_if_exist:NF \g__pdfmanagement_firstaidoff_clist
  { \clist_new:N \g__pdfmanagement_firstaidoff_clist }
\tl_if_exist:NF \g__pdfmanagement_testphase_tl
 { \tl_new:N \g__pdfmanagement_testphase_tl }
\cs_new_protected:Npn \@kernel@before@DocumentMetadata
  {
    \ifx\Umathcode\@undefined
    \fontencoding{T1}
    \renewcommand\encodingdefault{T1}
    \fi
  }

\cs_set_protected:Npn \DocumentMetadata #1
  {
    \cs_if_eq:NNTF \documentclass \@twoclasseserror
      { \msg_error:nn { meta } { after-class } }
      {
        \@kernel@before@DocumentMetadata
        \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
          \pdf_version_gset:n { 2.0 }
          \keys_set_groups:nnn { document / metadata} {pdf}{ #1 }
          \RequirePackage{latex-lab-testphase-latest}
          \AddToDocumentProperties [document]{tagging}{inactive}
          \AddToDocumentProperties [document]{tagging/para}{inactive}
        \keys_set_exclude_groups:nnn  { document / metadata } { init } { #1 }
        \g@addto@macro\@kernel@after@begindocument
         {
           \pdfdict_get:nnN {g__pdf_Core/Catalog}{Lang}\l__pdfmanagement_tmpa_tl
           \quark_if_no_value:NT\l__pdfmanagement_tmpa_tl
            {
              \tl_if_empty:eTF { \BCPdata{main.language} }
               { \tl_set:Ne \l__pdfmanagement_tmpb_tl { \BCPdata{language} } }
               { \tl_set:Ne \l__pdfmanagement_tmpb_tl { \BCPdata{main.language} } }
              \msg_warning:nne { meta } { lang-missing }{ \l__pdfmanagement_tmpb_tl }
              \exp_last_unbraced:Ne
               \AddToDocumentProperties{[document]{lang}{\l__pdfmanagement_tmpb_tl}}
              \pdfmanagement_add:nne {Catalog} {Lang}{(\l__pdfmanagement_tmpb_tl)}
            }
         }
        \hook_use_once:n {pdfmanagement/add}
        \cs_set_protected:Npn \DocumentMetadata ##1
          {
            \keys_set_exclude_groups:nnn  { document / metadata } { init } { ##1 }
          }
        \g__pdfmanagement_testphase_tl
        \RequirePackage{pdfmanagement-firstaid}
      }
  }

\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}
      }
    ,pdfversion .groups:n = { pdf }
    ,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 =
      {
        \clist_map_inline:nn{#1}
         {
           \keys_set:ne {document / metadata} {_pdfstandard=\str_uppercase:n{##1}}
         }
      }
    ,_pdfstandard .choices:nn =
      {A-1B,A-2A,A-2B,A-2U,A-3A,A-3B,A-3U,A-4}
      {
        \prop_gset_eq:Nc \g__pdfmeta_standard_prop { g__pdfmeta_standard_pdf/#1 _prop }
        \AddToDocumentProperties [document]{pdfstandard}{#1}
      }
    ,_pdfstandard / A-4F .code:n =
      {
        \prop_gset_eq:Nc \g__pdfmeta_standard_prop { g__pdfmeta_standard_pdf/A-4F_prop }
        \AddToDocumentProperties [document]{pdfstandard}{A-4F}
      }
    ,_pdfstandard / A-4E .code:n =
      {
        \prop_gset_eq:Nc \g__pdfmeta_standard_prop { g__pdfmeta_standard_pdf/A-4E_prop }
        \AddToDocumentProperties [document]{pdfstandard}{A-4E}
      }
    ,_pdfstandard / unknown .code:n =
      {
        \msg_error:nnn{pdf}{unknown-standard}{#1}
      }
    ,testphase .multichoice:
    ,testphase / latest .code:n =
      {
       \keys_set:nn
        {document / metadata}
        {tagging=on}
      }
    ,testphase / tagpdf .code:n =
      {
        \keys_set:nn
        {document / metadata}
        {tagging=on}
      }
    ,testphase / unknown .code:n =
      {
        \tl_gput_right:Nn\g__pdfmanagement_testphase_tl
           {
             \tl_if_empty:eT{\GetDocumentProperties{document/testphase/#1}}
              {
                \AddToDocumentProperties [document]{testphase/#1}{loaded}
                \file_if_exist_input:nF {#1-latex-lab-testphase.ltx}
                 {
                  \msg_warning:nnn{meta}{latex-lab-pkg-missing}{#1}
                  \AddToDocumentProperties [document]{testphase/#1}{missing}
                 }
              }
           }
      }
    ,tagging .choice:,
    ,tagging / on .code:n =
      {
        \tagpdfsetup{activate,para/tagging,activate/spaces}
        \AddToDocumentProperties [document]{tagging}{active}
        \AddToDocumentProperties [document]{tagging/para}{active}
        \AddToDocumentProperties [document]{tagging/interwordspace}{active}
      }
    ,tagging / off .code:n =
      {
        \AddToHook{class/before}
         {
           \tagpdfsetup{activate/mc=false,activate/tree=false,
                        activate/struct=false,activate/socket=false,
                        activate/spaces=true,
                        activate/spaces=false,
                        para/tagging=false,math/setup=}
           \AddToDocumentProperties [document]{tagging}{inactive}
           \AddToDocumentProperties [document]{tagging/para}{inactive}
           \AddToDocumentProperties [document]{tagging/interwordspace}{inactive}
           \tag_suspend:n {global}
           \cs_set_protected:Npn\tagpdfsetup##1{}
           %\cs_set_eq:NN\tag_suspend:n\use_none:n
           %\cs_set_eq:NN\tag_resume:n \use_none:n
         }
      }
    ,tagging / draft .code:n =
      {
        \keys_set:nn { document / metadata }{ tagging=on }
        \AddToHook{shipout/lastpage}[tagging-setup]{\tagpdfsetup{activate/tree=false}}
        \DeclareHookRule{shipout/lastpage}{tagging-setup}{before}{tagpdf}
      }
    ,tagging-setup .code:n =
      {
        \keys_set:nn { document / metadata }{ tagging=on }
        \tl_gclear:N\g__pdfmanagement_testphase_tl
        \keys_set_groups:nnn{__tag/setup}{load}{#1}
        \AddToHook{class/before}
         {\keys_set_exclude_groups:nnn {__tag/setup}{load}{#1}}
      }
    ,debug .code:n =
      {
        \keys_set:nn { document / metadata / debug } {#1}
      }
    ,debug / para .code:n =
      {
        \AddToHook
          {
            package/tagpdf/after
          }
          {
             \tagpdfsetup{debug/show=para}
          }
      }
    ,debug / log .code:n =
      {
        \AddToHook
          {
           package/tagpdf/after
          }
          {
             \tagpdfsetup{debug/log=#1}
          }
      }
    ,debug / tagpdf .code:n =
      {
        \AddToHook
          {
           package/tagpdf/after
          }
          {
             \RequirePackage{tagpdf-debug}
          }
      }
    ,debug / uncompress .code:n =
      {
        \pdf_uncompress:
      }
    ,debug / pdfmanagement .bool_gset:N = \g__pdfmanagement_active_bool
    ,debug / firstaidoff .clist_gset:N = \g__pdfmanagement_firstaidoff_clist
    ,debug / BBox .code:n =
      {
        \AddToHook{package/latex-lab-testphase-graphic/after}
         { \bool_set_true:N \l__tag_graphic_debug_bool}
      }
  }
\keys_define:nn{__tag/setup}
 {
   ,modules .code:n =
     {
      \tl_gclear:N\g__pdfmanagement_testphase_tl
      \keys_set:nn { document / metadata }{testphase={#1}}
     }
   ,modules .groups:n = { load }
   ,extra-modules .code:n =
     {
       \keys_set:nn { document / metadata }{testphase={#1}}
     }
   ,extra-modules .groups:n = { load }
 }
\prop_gput:Nnn \g_msg_module_type_prop { meta } { LaTeX }
\prop_gput:Nnn \g_msg_module_name_prop { meta } { DocumentMetadata }

\msg_new:nnn { meta } { after-class }
             {
                \token_to_str:N \DocumentMetadata \c_space_tl
                should~be~used~only~before~\token_to_str:N\documentclass
             }
\msg_new:nnn { meta } { latex-lab-pkg-missing }
             {
               LaTeX-lab~package~'#1'~not~found.
              }
\msg_new:nnn { meta } { lang-missing }
             {
               The~language~has~not~been~set~in~\token_to_str:N
               \DocumentMetadata.\\Setting~it~to~'#1'~as~fallback.
             }
\msg_new:nnn { meta } { testphase-latest-loading }
             {
               Loading~testphase~modules\\
               #1
             }
\ExplSyntaxOff\makeatother
\endinput
%%
%% End of file `documentmetadata-support.ltx'.
