summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/pdfmanagement-testphase/hyperxmp-patches-tmp-ltx.sty
blob: 5c77d8ce6b070f7ee8335da55a669284b524365c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
%% This is file `hyperxmp-patches-tmp-ltx.sty"
% 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 "pdfmanagement bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/latex3/pdfresources
%
% for those people who are interested.
\NeedsTeXFormat{LaTeX2e}[2020/10/01]
\ProvidesExplPackage{hyperxmp-patches-tmp-ltx}{2022-08-24}{0.95r}
  {Store hyperref metadata in XMP format / temporay patches to test pdfresource management ... UF}

\cs_if_exist:NT \pdfmanagement_add:nnn
  {
    \pdfmanagement_if_active:T
     {
       \renewcommand\hyxmp@embed@packet{\hyxmp@embed@packet@generic}
     }
  }

%
\newcommand*{\hyxmp@embed@packet@generic}
 {%
   \typeout{}
   \typeout{!!~new-hyperxmp--generic~packet~command~used!!}{}
   \typeout{}
   \hyxmp@construct@packet
   %or some other command to write a object:
   \exp_args:Nnx
    \pdf_object_unnamed_write:nn{stream}{{/Type~/Metadata~/Subtype~/XML}{\hyxmp@xml}}%
    % reference in the Catalog:
    \pdfmanagement_add:nnx {Catalog} {Metadata}{\pdf_object_ref_last:}
 }

% A standard should be retrieved from the document settings.
\cs_new:Npn \__hypxmp_get_Astandard:
 {
   \exp_args:Ne \tl_if_eq:nnT{\pdfmeta_standard_item:n{type}}{A}
    {
      \Hy@pdfatrue
      \tl_set:Nx \@pdfapart{\pdfmeta_standard_item:n{level}}
      \tl_set:Nx \@pdfaconformance{\pdfmeta_standard_item:n{conformance}}
    }
 }
% A-4 will need also the year, so we have to extend the schema
\renewcommand*{\hyxmp@pdfa@id@schema}{%
  \ifHy@pdfa
    \exp_args:Nnx\hyxmp@add@simple{pdfaid:part}{\pdfmeta_standard_item:n{level}}
    \exp_args:Nnx\hyxmp@add@simple{pdfaid:conformance}{\pdfmeta_standard_item:n{conformance}}
    \int_compare:nNnT{\pdfmeta_standard_item:n{level}}>{3}
     { \exp_args:Nnx\hyxmp@add@simple{pdfaid:rev}{\pdfmeta_standard_item:n{year}} }
  \fi
}

\cs_new_protected:Npn \__hyxmp_get_metadata:
 {
   \__hypxmp_get_Astandard:
   \clist_map_inline:nn
    {
      % hyperxmp keys:
      ,pdfcopyright
      ,pdftype
      ,pdflicenseurl
      ,pdfauthortitle
      ,pdfcaptionwriter
      ,pdfmetalang
      %,pdfapart %document
      %,pdfaconformance %document
      ,pdfuapart %probably document too but later ...
      ,pdfxstandard
      ,pdfsource
      ,pdfdocumentid
      ,pdfinstanceid
      ,pdfversionid
      ,pdfrendition
      ,pdfpublication
      ,pdfpubtype
      ,pdfbytes
      ,pdfnumpages
      ,pdfissn
      ,pdfeissn
      ,pdfisbn
      ,pdfbookedition
      ,pdfpublisher
      ,pdfvolumenum
      ,pdfissuenum
      ,pdfpagerange
      ,pdfdoi
      ,pdfurl
      ,pdfidentifier
      ,pdfsubtitle
      ,pdfpubstatus
      ,pdfcontactaddress
      ,pdfcontactcity
      ,pdfcontactregion
      ,pdfcontactpostcode
      ,pdfcontactcountry
      ,pdfcontactphone
      ,pdfcontactemail
      ,pdfcontacturl
      ,pdfdate
    %hyperref, needs probably special handling
   %   ,pdftitle   %see below
   %   ,pdfsubject %see below
      ,pdfkeywords
      ,pdfproducer
    }
    {
     \tl_if_exist:cF{@##1}{\tl_new:c{@##1}}
     \tl_set:cx {@##1}{\GetDocumentProperties{hyperref/##1}}
    }

    % pdfauthor, this is only a work around for simple author
    % TODO needs improvement
    \exp_args:NNe
    \pdfstringdef\@pdfauthor{\GetDocumentProperties{hyperref/pdfauthor}}
    \cs_set_eq:NN\hyxmp@pdfauthor\@pdfauthor
    \exp_args:NNe
    \pdfstringdef\@pdfsubject{\GetDocumentProperties{hyperref/pdfsubject}}
    \cs_set_eq:NN\hyxmp@pdfsubject\@pdfsubject
    \exp_args:NNe
    \pdfstringdef\@pdftitle{\GetDocumentProperties{hyperref/pdftitle}}
    \cs_set_eq:NN\hyxmp@pdftitle\@pdftitle
    \exp_args:NNe
    \pdfstringdef\@pdfcopyright{\GetDocumentProperties{hyperref/pdfcopyright}}
    \cs_set_eq:NN\hyxmp@pdfcopyright\@pdfcopyright
    % pdflang,
    \tl_if_exist:cF{@pdflang}{\tl_new:c{@pdflang}}
    \tl_set:cx {@pdflang}{\GetDocumentProperties{document/lang}}
  }

% we need to provide a few commands so that hyperxmp stops to overwrite them
% in \AtEndPreamble
% we need also add it to the document properties to avoid stray commas
% with empty author.
\AddToHook{begindocument/before}[pdfmanagement/firstaid/hyperxmp]{%
 \def\@pdfauthor{Author}\def\@pdftitle {Title}%
 \tl_if_blank:eT{\GetDocumentProperties{hyperref/pdfauthor}}
  {\AddToDocumentProperties[hyperref]{pdfauthor}{Author}}
 \tl_if_blank:eT{\GetDocumentProperties{hyperref/pdftitle}}
  {\AddToDocumentProperties[hyperref]{pdfauthor}{Title}}
 }

% hyperxmp recalculates pdfproduce at begin document and we should copy that definition
\AddToHook{begindocument/end}
 {\edef\next{\noexpand\AddToDocumentProperties[hyperref]{pdfproducer}{\@pdfproducer}}\next}

\DeclareHookRule{begindocument/before}{pdfmanagement/firstaid/hyperxmp}{before}{hyperxmp}

% hyperxmp puts the code into enddocument, but for xetex this is too late.
% so we move that to shipout/lastpage and add our command before.
% the luatex code must stay in enddocument as hyperxmp tries to detect the pdf size
% there.
\sys_if_output_dvi:TF
  {
    \RemoveFromHook{enddocument}[hyperxmp]
    \AddToHook{shipout/lastpage}[pdfmanagement-firstaid]
      {
        \__hyxmp_get_metadata:
        \hyxmp@auto@assign@data
        \hyxmp@check@standards
        \hyxmp@warn@if@no@metadata
        \hyxmp@embed@packet
       }
   }
   {
     \AddToHook{enddocument}[pdfmanagement-firstaid]{\__hyxmp_get_metadata:}
     \DeclareHookRule{enddocument}{pdfmanagement-firstaid}{before}{hyperxmp}
   }


\endinput