summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/hyperxmp/hyperxmp.dtx
blob: 2cc3ff17731d3baf058557bcb85b5d0dcaa3b8d0 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
% \iffalse meta-comment
%
% Copyright (C) 2011 by Scott Pakin <scott+hyxmp@pakin.org>
% -------------------------------------------------------
%
% This file 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/05/04 or later.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{hyperxmp.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{hyperxmp}
%<*package>
    [2011/04/30 v1.3 Store hyperref metadata in XMP format]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{graphicx}
\usepackage{tocbibind}
\usepackage{hyperxmp}
\usepackage[bookmarksopen,bookmarksopenlevel=2,pdflang=en]{hyperref}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{hyperxmp.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{872}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v1.0}{2006/05/14}{Initial version}
%
% \GetFileInfo{hyperxmp.dtx}
%
% \DoNotIndex{\#,\&,\<,\>,\^,\\,\_,\|,\ }
% \DoNotIndex{\@cons,\@empty,\@ifpackageloaded,\@ifundefined}
% \DoNotIndex{\@tempcnta,\@tempcntb}
% \DoNotIndex{\advance,\afterassignment,\aftergroup}
% \DoNotIndex{\begingroup,\bgroup}
% \DoNotIndex{\catcode}
% \DoNotIndex{\def,\divide}
% \DoNotIndex{\edef,\egroup,\else,\endgroup,\expandafter}
% \DoNotIndex{\fi,\futurelet}
% \DoNotIndex{\gdef,\global}
% \DoNotIndex{\if,\ifcase,\ifnum,\ifx,\immediate}
% \DoNotIndex{\let,\loop}
% \DoNotIndex{\MessageBreak,\multiply}
% \DoNotIndex{\newcommand,\noexpand}
% \DoNotIndex{\or}
% \DoNotIndex{\relax,\repeat}
% \DoNotIndex{\space,\string}
% \DoNotIndex{\the,\toks}
% \DoNotIndex{\xdef}
%
% ^^A  Define a few logical styles.
% \DeclareRobustCommand{\term}[1]{#1\SortIndex{#1}{#1}}
% \DeclareRobustCommand{\pkgname}[1]{\mbox{\textsf{#1}}\SortIndex{#1}{\textsf{#1}}}
% \DeclareRobustCommand{\xmpterm}[1]{\mbox{\textsf{#1}}\SortIndex{#1}{\textsf{#1}}}
% \DeclareRobustCommand{\pdfterm}[1]{\mbox{\textsf{#1}}\SortIndex{#1}{\textsf{#1}}}
% \DeclareRobustCommand{\cmdname}[1]{\mbox{\texttt{#1}}\SortIndex{#1}{\texttt{#1}}}
% \DeclareRobustCommand{\acrostyle}[1]{\textsc{\MakeLowercase{#1}}}
% \DeclareRobustCommand{\acro}[1]{^^A
%   \mbox{\acrostyle{#1}}^^A
%   \SortIndex{#1}{\acrostyle{#1}}^^A
% }
%
% ^^A  Define some other shortcut macros.
% \DeclareRobustCommand{\XeTeXlogo}{^^A
%   X\lower0.5ex\hbox{\kern-.15em\reflectbox{E}}\kern-0.1667em\TeX
% }
% \DeclareRobustCommand{\XeTeX}{^^A
%   \texorpdfstring{\XeTeXlogo\SortIndex{XeTeX}{\XeTeXlogo}}{XeTeX}^^A
% }
% \DeclareRobustCommand{\XeLaTeXlogo}{^^A
%   X\lower0.5ex\hbox{\kern-.15em\reflectbox{E}}\kern-0.1667em\LaTeX
% }
% \DeclareRobustCommand{\XeLaTeX}{^^A
%   \texorpdfstring{\XeLaTeXlogo\SortIndex{XeLaTeX}{\XeLaTeXlogo}}{XeLaTeX}^^A
% }
%
% ^^A  Specify this document's metadata.
% \title{The \pkgname{hyperxmp} package\thanks{This document
%   corresponds to \pkgname{hyperxmp}~\fileversion, dated \filedate.}}
% \author{Scott Pakin \\ \texttt{scott+hyxmp@pakin.org}}
% \hypersetup{%
%   pdfauthor={Scott Pakin},
%   pdftitle={The hyperxmp package},
%   pdfsubject={LaTeX2e support for XMP metadata},
%   pdfkeywords={LaTeX, embedded metadata, XMP, PDF, copyright, license, comments},
%   pdfcopyright={Copyright (C) 2011, Scott Pakin},
%   pdflicenseurl={http://www.latex-project.org/lppl/},
%   pdfcaptionwriter={Scott Pakin}
% }
%
% \maketitle
% \sloppy
%
% \begin{abstract}
%   \pkgname{hyperxmp} makes it easy for an author to include \acro{XMP}
%   metadata in a \acro{PDF} document produced by \LaTeX\@.  \pkgname{hyperxmp}
%   integrates seamlessly with \pkgname{hyperref} and requires virtually
%   no modifications to a document that already specifies document
%   metadata through \pkgname{hyperref}'s mechanisms.
% \end{abstract}
%
%
% \section{Introduction}
%
% Adobe Systems, Inc.\ has been promoting
% \acro{XMP}~\cite{Adobe2010:XMP}---eXtensible Metadata Platform---as a
% standard way to include metadata within a document.  The idea behind
% \acro{XMP} is that it is an \acro{XML}-based description of various
% document attributes and is embedded as uncompressed, unencoded text
% within the document it describes.  By storing the metadata this way it
% is independent of the document's file format.  That is, regardless of
% whether a document is in \acro{PDF}, \acrostyle{JPEG},
% \acrostyle{HTML}, or any other format, it is trivial for a program (or
% human) to locate, extract, and---using any standard \acro{XML}
% parser---process the embedded \acro{XMP} metadata.
%
% As of this writing there are few tools that actually do process \acro{XMP}\@.
% However, it is easy to imagine future support existing in file
% browsers for displaying not only a document's filename but also its
% title, list of authors, description, and other metadata.
%
% \paragraph{This is too abstract!  Give me an example.}
% Consider a \LaTeX\ document with three authors: Jack Napier, Edward
% Nigma, and Harvey Dent.  The generated \acro{PDF} file will contain, among
% other information, the following stanza of \acro{XMP} code embedded within
% it:
%
% \begin{verbatim}
%    <dc:creator>
%      <rdf:Seq>
%        <rdf:li>Jack Napier</rdf:li>
%        <rdf:li>Edward Nigma</rdf:li>
%        <rdf:li>Harvey Dent</rdf:li>
%      </rdf:Seq>
%    </dc:creator>
% \end{verbatim}
%
% In the preceding code, the |dc| namespace refers to the
% \href{http://purl.org/DC/}{Dublin Core schema}, a collection of
% metadata properties.  The \xmpterm{dc:creator} property surrounds the
% list of authors.  The \textsf{rdf} namespace is the
% \href{http://www.w3.org/RDF/}{Resource Description Framework}, which
% defines \xmpterm{rdf:Seq} as an ordered list of values.  Each author
% is represented by an individual list item (\xmpterm{rdf:li}), making
% it easy for an \acro{XML} parser to separate the authors' names.
%
% Remember that \acro{XMP} code is stored as \emph{metadata}.  It does not
% appear when viewing or printing the \acro{PDF} file.  Rather, it is intended
% to make it easy for applications to identify and categorize the
% document.
%
% \paragraph{What metadata does \textsf{hyperxmp} process?}
% \pkgname{hyperxmp} knows how to embed all of the following types of
% metadata within a document:
%
% \begin{itemize}
%   \item authors (\xmpterm{dc:creator})
%   \item copyright (\xmpterm{dc:rights})
%   \item date (\xmpterm{dc:date})
%   \item document identifier (\xmpterm{xapMM:DocumentID})
%   \item document instance identifier (\xmpterm{xapMM:InstanceID})
%   \item format (\xmpterm{dc:format})
%   \item keywords (\xmpterm{pdf:Keyword} and \xmpterm{dc:subject})
%   \item license \acro{URL} (\xmpterm{xapRights:WebStatement})
%   \item metadata writer (\xmpterm{photoshop:CaptionWriter})
%   \item \acro{PDF}-generating tool (\xmpterm{pdf:Producer})
%   \item primary author's position/title (\xmpterm{photoshop:AuthorsPosition})
%   \item summary (\xmpterm{dc:description})
%   \item title (\xmpterm{dc:title})
% \end{itemize}
%
% \noindent
% More types of metadata may be added in a future release.
%
% \paragraph{How does \textsf{hyperxmp} compare to the \textsf{xmpincl}
% package?}
% The short answer is that \pkgname{xmpincl} is more flexible but
% \pkgname{hyperxmp} is easier to use.  With \pkgname{xmpincl}, the
% author manually constructs a file of arbitrary \acro{XMP} data and the
% package merely embeds it within the generated \acro{PDF} file.  With
% \pkgname{hyperxmp}, the author specifies values for various predefined
% metadata types and the package formats those values as \acro{XMP} and embeds
% the result within the generated \acro{PDF} file.
%
% \pkgname{xmpincl} can embed \acro{XMP} only when running under pdf\LaTeX\ and
% only when in \acro{PDF}-generating mode.  \pkgname{hyperxmp} additionally
% works with a few other \acro{PDF}-producing \LaTeX\ backends.
%
% \pkgname{hyperxmp} and \pkgname{xmpincl} can complement each other.
% An author may want to use \pkgname{hyperxmp} to produce a basic set of
% \acro{XMP} code, then extract the \acro{XMP} code from the \acro{PDF} file with a text
% editor, augment the \acro{XMP} code with any metadata not supported by
% \pkgname{hyperxmp}, and use \pkgname{xmpincl} to include the modified
% \acro{XMP} code in the \acro{PDF} file.
%
% \section{Usage}
%
% \pkgname{hyperxmp} provides no commands of its own.  Rather, it
% processes some of the package options honored by \pkgname{hyperref}.
% To use \pkgname{hyperxmp}, merely put a |\usepackage{hyperxmp}|
% somewhere in your document's preamble.  \pkgname{hyperxmp} will
% construct its \acro{XMP} data using the following \pkgname{hyperref} options:
%
% \begin{itemize}
%   \item |pdfauthor|
%   \item |pdfkeywords|
%   \item |pdflang|
%   \item |pdfproducer|
%   \item |pdfsubject|
%   \item |pdftitle|
% \end{itemize}
%
% \noindent
% \pkgname{hyperxmp} instructs \pkgname{hyperref} also to accept the
% following options, which have meaning only to \pkgname{hyperxmp}:
%
% \begin{itemize}
%   \item |pdfauthortitle|
%   \item |pdfcaptionwriter|
%   \item |pdfcopyright|
%   \item |pdflicenseurl|
%   \item |pdfmetalang|
% \end{itemize}
%
% \noindent
% |pdfauthortitle| indicates the primary author's position or title.
% |pdfcaptionwriter| specifies the name of the person who added
% the metadata to the document.  |\pdfcopyright| defines the copyright
% text.  |pdflicenseurl| identifies a \acro{URL} that points to the
% document's license agreement.  |pdfmetalang| indicates the natural
% language in which the metadata is written, typically as an \acro{IETF}
% language tag~\cite{IANA2011:lang-tags}, for example, ``|en|'' for
% English, ``|en-US|'' for specifically United States English, ``|de|''
% for German, and so forth.  If |pdfmetalang| is not specified,
% \pkgname{hyperxmp} assumes the metadata language is the same as the
% document language (\pkgname{hyperref}'s |pdflang| option).  If neither
% |pdfmetalang| nor |pdflang| is specified, \pkgname{hyperxmp} uses
% ``|x-default|'' as the metadata language.
%
% It's usually more convenient to provide values for those options using
% \pkgname{hyperref}'s |\hypersetup| command than on the |\usepackage|
% command line.  See
% \href{ftp://tug.ctan.org/pub/tex-archive/macros/latex/contrib/hyperref/doc/manual.pdf}{the
% \pkgname{hyperref} manual} for more information.  The following is a
% sample \LaTeX\ document that provides values for most of the metadata
% options that \pkgname{hyperxmp} recognizes:
%
% \begin{verbatim}
%     \documentclass{article}
%     \usepackage{hyperxmp}
%     \usepackage{hyperref}
%     \title{%
%       On a heuristic viewpoint concerning the production and
%       transformation of light}
%     \author{Albert Einstein}
%     \hypersetup{%
%       pdftitle={%
%         On a heuristic viewpoint concerning the production and
%         transformation of light},
%       pdfauthor={Albert Einstein},
%       pdfcopyright={Copyright (C) 1905, Albert Einstein},
%       pdfsubject={photoelectric effect},
%       pdfkeywords={energy quanta, Hertz effect, quantum physics},
%       pdflang={en}
%     }
%     \begin{document}
%     \maketitle
%     A profound formal difference exists between the theoretical
%     concepts that physicists have formed about gases and other
%     ponderable bodies, and Maxwell's theory of electromagnetic
%     processes in so-called empty space\dots
%     \end{document}
% \end{verbatim}
%
% Compile the document to \acro{PDF} using any of the following approaches:
%
% \begin{itemize}
%   \item pdf\LaTeX
%   \item Lua\LaTeX
%   \item \LaTeX~$+$ Dvipdfm
%   \item \LaTeX~$+$ Dvips~$+$ Ghostscript
%   \item \LaTeX~$+$ Dvips~$+$ Adobe Acrobat Distiller
%   \item \XeLaTeX
% \end{itemize}
%
% Besides the approaches listed above, other approaches may work as well
% but have not been tested.  Note that in many \TeX\ distributions
% \cmdname{ps2pdf} is a convenience script that calls Ghostscript with
% the appropriate options for converting PostScript to \acro{PDF} and
% \cmdname{dvipdf} is a convenience script that calls \cmdname{dvips}
% and \cmdname{ps2pdf}; both \cmdname{ps2pdf} and \cmdname{dvipdf}
% should be compatible with \pkgname{hyperxmp}.
%
% \bigskip
%
% The resulting \acro{PDF} file will contain an \acro{XMP} packet that
% looks something like this:
%
% \begin{verbatim}
%     <?xpacket begin="???" id="W5M0MpCehiHzreSzNTczkc9d"?>
%     <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="3.1-702">
%        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
%           <rdf:Description rdf:about=""
%                xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
%                <pdf:Keywords>energy quanta, Hertz effect,
%                quantum physics</pdf:Keywords>
%                <pdf:Producer>pdfeTeX-1.10b</pdf:Producer>
%           </rdf:Description>
%           <rdf:Description rdf:about=""
%                 xmlns:dc="http://purl.org/dc/elements/1.1/">
%              <dc:format>application/pdf</dc:format>
%              <dc:title>
%                 <rdf:Alt>
%                    <rdf:li xml:lang="en">On a heuristic viewpoint
%                    concerning the production and transformation of
%                    light</rdf:li>
%                 </rdf:Alt>
%              </dc:title>
%              <dc:description>
%                 <rdf:Alt>
%                    <rdf:li xml:lang="en">photoelectric effect</rdf:li>
%                 </rdf:Alt>
%              </dc:description>
%              <dc:rights>
%                 <rdf:Alt>
%                    <rdf:li xml:lang="en">Copyright (C) 1905,
%                    Albert Einstein</rdf:li>
%                 </rdf:Alt>
%              </dc:rights>
%              <dc:creator>
%                 <rdf:Seq>
%                    <rdf:li>Albert Einstein</rdf:li>
%                 </rdf:Seq>
%              </dc:creator>
%              <dc:subject>
%                 <rdf:Bag>
%                    <rdf:li>energy quanta</rdf:li>
%                    <rdf:li>Hertz effect</rdf:li>
%                    <rdf:li>quantum physics</rdf:li>
%                 </rdf:Bag>
%              </dc:subject>
%              <dc:date>
%                 <rdf:Seq>
%                    <rdf:li>2006-04-19</rdf:li>
%                 </rdf:Seq>
%              </dc:date>
%           </rdf:Description>
%           <rdf:Description rdf:about=""
%                 xmlns:xapMM="http://ns.adobe.com/xap/1.0/mm/">
%              <xapMM:DocumentID>uuid:c4188820-aef2-0a82-626ce4182b62</xapMM:DocumentID>
%              <xapMM:InstanceID>uuid:9b62b67f-d754-626c-4c959595fd75</xapMM:InstanceID>
%           </rdf:Description>
%        </rdf:RDF>
%     </x:xmpmeta>
%     <?xpacket end="w"?>
% \end{verbatim}
%
% \paragraph{Note~1: Acrobat \pdfterm{Author} bug}
% A bug in Adobe Acrobat---at least in versions~10.0.1 and
% earlier---causes that \acro{PDF} reader to confuse the \acro{XMP} and
% non-\acro{XMP} author lists when displaying the document's metadata.
% Specifically, the first author is displayed as the concatenated list
% of authors from the non-\acro{XMP} data (\pdfterm{Author}) while the
% remaining authors are displayed from the \acro{XMP} data
% (\xmpterm{dc:creator}).  For example, suppose that a document's authors
% are Jack Napier, Edward Nigma, and Harvey Dent.  When displaying the
% document properties, Adobe Acrobat replaces ``Jack Napier'' with a
% single author named ``Jack Napier, Edward Nigma, Harvey Dent'' and
% leaves ``Edward Nigma'' and ``Harvey Dent'' as the second and third
% authors, respectively.
%
% A workaround, independent of \TeX, is to modify the \acro{PDF} file to
% remove all but the first author from the non-\acro{XMP} author list
% while retaining all of the authors in the \acro{XMP} author list.
% Doing so will cause Adobe Acrobat to properly display all of the
% authors but at the cost of other \acro{PDF} readers likely displaying
% only the first author.  The following Perl command (which should be
% entered as a single line) automates this modification:
%
% \begin{verbatim}
%   perl -i -ne 's,(/Author\s*\([^\,\)]+)(.*?)\),"$1)" . " " x length($2),ge;
%     print' myfile.pdf
% \end{verbatim}
%
% \noindent
% (Systems with different quoting conventions from Linux/Unix may need
% to adapt the preceding commands as appropriate.)
%
%
% \paragraph{Note~2: \XeLaTeX\ object compression}
% \XeLaTeX\ (or, more precisely, the \cmdname{xdvipdfmx} back end),
% compresses \emph{all} \acro{PDF} objects, including the ones
% containing \acro{XMP} metadata.  While Adobe Acrobat can still detect
% and utilize the \acro{XMP} metadata, non-\acro{PDF}-aware applications
% are unlikely to see the metadata.  Either use a different program
% (e.g.,~Lua\LaTeX) or use \XeLaTeX\ to produce a \acro{DVI} or
% \acro{XDV} file and run \cmdname{xdvipdfmx} manually on that file
% using the |-z0| option to turn off all compression (which will of
% course make the \acro{PDF} file substantially larger).
%
%
% \paragraph{Note~3: Literal commas}
% \pkgname{hyperxmp} splits the |pdfauthor| and |pdfkeywords| lists at
% commas.  Therefore, when specifying |pdfauthor| and |pdfkeywords|, you
% should separate items with commas.  Also, omit ``|and|'' and other
% text that does not belong to any list item.  The following example
% should serve as clarification:
%
% \begin{description}
%   \item[Wrong:] |pdfauthor={Jack Napier, Edward Nigma, and Harvey Dent}|
%   \item[Wrong:] |pdfauthor={Jack Napier; Edward Nigma; Harvey Dent}|
%   \item[Right:] |pdfauthor={Jack Napier, Edward Nigma, Harvey Dent}|
% \end{description}
%
% If you desperately need to include a comma within an author or keyword
% list you can define your own comma macro as follows:
%
% \begin{verbatim}
%     \bgroup
%     \catcode`,=11
%     \gdef\mycomma{,}
%     \egroup
% \end{verbatim}
%
% \noindent
% Thereafter, you can use |\mycomma| as a literal comma:
%
% \begin{verbatim}
%     pdfauthor={Napier\mycomma\ Jack,
%                Nigma\mycomma\ Edward,
%                Dent\mycomma\ Harvey}
% \end{verbatim}
%
%
% \StopEventually{^^A
% \begin{thebibliography}{1}
% \bibitem{Adobe2010:pdfmark}
% Adobe Systems, Inc., San Jose, California.
% \newblock {\em {A}dobe {A}crobat~{X} {SDK} Help, pdfmark Reference}.
% \newblock Available from
%   \url{http://www.adobe.com/devnet/acrobat/documentation.html}.
%
% \bibitem{Adobe2008:PDF}
% Adobe Systems, Inc., San Jose, California.
% \newblock {\em Document Management---Portable Document Format---Part 1:
%   PDF~1.7}, July 2008.
% \newblock ISO \mbox{32000-1} standard document. Available from
%   \url{http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/PDF32000_2008.pdf}.
%
% \bibitem{Adobe2010:XMP}
% Adobe Systems, Inc., San Jose, California.
% \newblock {\em {XMP} Specification Part~1: Data model, Serialization, and Core
%   Properties}, July 2010.
% \newblock Available from
%   \url{http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/xmp/pdfs/XMPSpecificationPart1.pdf}.
%
% \bibitem{Downes1994:ATB15}
% Michael Downes.
% \newblock Around the bend~\#15, answers, 4th (last) installment.
% \newblock \href{news:comp.text.tex}{\texttt{comp.text.tex}} newsgroup posting,
%   January~3, 1994.
% \newblock Archived by Google at
%   \url{http://groups.google.com/group/comp.text.tex/msg/7da7643b9e8f3b48}.
%
% \bibitem{IANA2011:lang-tags}
% {Internet Assigned Numbers Authority}.
% \newblock Language subtag registry, January~11, 2011.
% \newblock Available from
%   \url{http://www.iana.org/assignments/language-subtag-registry}.
% \end{thebibliography}
% }
%
%
% \section{Implementation}
%
% This section presents the commented \LaTeX\ source code for
% \pkgname{hyperxmp}.  Read this section only if you want to learn how
% \pkgname{hyperxmp} is implemented.
%
%
% \subsection{Initial preparation}
% \label{sec:initial-prep}
%
% \changes{v1.2}{2011/04/17}{Made the package compatible with \pkgname{ngerman}.
%   Thanks to Tobias Mueller for the bug report.}
%
% \begin{macro}{\hyxmp@dq@code}
% The \pkgname{ngerman} package redefines
% ``\,{\fontencoding{T1}\selectfont\textquotedbl}\,'' as an active
% character, which causes problems for \pkgname{hyperxmp} when it tries
% to use that character.  We therefore save the double-quote character's
% current category code in |\hyxmp@dq@code| and mark the character as
% category code~12 (``other'').  The original category code is restored
% at the end of the package code (Section~\ref{sec:clean-up}).
%    \begin{macrocode}
\edef\hyxmp@dq@code{\the\catcode`\"}
\catcode`\"=12
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Integration with \textsf{hyperref}}
%
% An important design decision underlying \pkgname{hyperxmp} is that the
% package should integrate seamlessly with \pkgname{hyperref}.  To that
% end, \pkgname{hyperxmp} takes its \acro{XMP} metadata from the
% \pkgname{hyperref} |pdftitle|, |pdfauthor|, |pdfsubject|, and
% |pdfkeywords| options plus two new options, |pdfcopyright| and
% |pdflicenseurl|, introduced by \pkgname{hyperxmp}.  For consistency
% with \pkgname{hyperref}'s document-metadata naming conventions (which
% are in turn based on \LaTeX's document-metadata naming conventions),
% we do not prefix metadata-related macro names with our
% package-specific |\hyxmp@| prefix.  That is, we use names like
% |\@pdfcopyright| instead of |\hyxmp@pdfcopyright|.
%
% \bigskip
%
% We load three helper packages: \pkgname{keyval} for package-option
% processing and \pkgname{pdfescape} and \pkgname{stringenc} for
% re-encoding Unicode strings.
%
%    \begin{macrocode}
\RequirePackage{keyval}
\RequirePackage{pdfescape}
\RequirePackage{stringenc}
%    \end{macrocode}
%
% \begin{macro}{\@pdfcopyright}
% Prepare to store the document's copyright statement.
%    \begin{macrocode}
\def\@pdfcopyright{}
\define@key{Hyp}{pdfcopyright}{\pdfstringdef\@pdfcopyright{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@pdflicenseurl}
% Prepare to store the \acro{URL} containing the document's license
% agreement.
%    \begin{macrocode}
\def\@pdflicenseurl{}
\define@key{Hyp}{pdflicenseurl}{\pdfstringdef\@pdflicenseurl{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@pdfauthortitle}
% Prepare to store the author's position/title (e.g.,~Staff Writer).
%    \begin{macrocode}
\def\@pdfauthortitle{}
\define@key{Hyp}{pdfauthortitle}{\pdfstringdef\@pdfauthortitle{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@pdfcaptionwriter}
% Prepare to store the name of the person who inserted the
% \pkgname{hyperxmp} metadata.
%    \begin{macrocode}
\def\@pdfcaptionwriter{}
\define@key{Hyp}{pdfcaptionwriter}{\pdfstringdef\@pdfcaptionwriter{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@pdfmetalang}
% Prepare to store the natural language of the document's metadata,
% typically as an \acro{ISO}~\mbox{639-1} two-letter abbreviation.
%    \begin{macrocode}
\def\@pdfmetalang{}
\define@key{Hyp}{pdfmetalang}{\pdfstringdef\@pdfmetalang{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@find@metadata}
% \begin{macro}{\hyxmp@concated@metadata}
% Issue a warning message if the author failed to include any metadata
% at all.  Note that we don't consider |\@pdflang| or |\@pdfmetalang| as
% metadata, as they're meaningful only when used in conjunction with
% other information.
%    \begin{macrocode}
\newcommand*{\hyxmp@find@metadata}{%
  \edef\hyxmp@concated@metadata{%
    \@pdfauthor
    \@pdfauthortitle
    \@pdfcaptionwriter
    \@pdfcopyright
    \@pdfkeywords
    \@pdflicenseurl
    \@pdfsubject
    \@pdftitle
  }%
  \ifx\hyxmp@concated@metadata\@empty
    \PackageWarningNoLine{hyperxmp}{%
\jobname.tex did not specify any metadata to\MessageBreak
include in the XMP packet.\space\space Please see the hyperxmp\MessageBreak
documentation for instructions on how to provide\MessageBreak
metadata values to hyperxmp}%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% Rather than load \pkgname{hyperref} ourself we let the author do it
% then verify he actually did.  This approach gives the author the
% flexibility to load \pkgname{hyperxmp} and \pkgname{hyperref} in
% either order and to call |\hypersetup| anywhere in the document's
% preamble, not just before \pkgname{hyperxmp} is loaded.
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifpackageloaded{hyperref}%
    {%
%    \end{macrocode}
% If the user explicitly specified the language to use for the
% document's metadata, we use that.  If not, we use the document
% language, specified to \pkgname{hyperref} with the |pdflang| option.
% If the author did not specify a language, we use |x-default| as the
% metadata language.
% \changes{v1.3}{2011/04/25}{Introduced the \texttt{pdfmetalang} package
%   option, which enables an author to specify the language in which he
%   wrote the document's metadata}
%    \begin{macrocode}
      \ifx\@pdfmetalang\@empty
        \ifx\@pdflang\@empty
          \def\@pdfmetalang{x-default}%
        \else
          \edef\@pdfmetalang{\@pdflang}%
        \fi
      \fi
      \ifHy@unicode
        \hyxmp@reencode\@pdfmetalang
      \fi
%    \end{macrocode}
% We wait until the end of the document to construct the \acro{XMP}
% packet and write it to the \acro{PDF} document catalog.  This gives
% the author ample opportunity to provide metadata to \pkgname{hyperref}
% and thereby \pkgname{hyperxmp}.
%    \begin{macrocode}
      \AtEndDocument{%
        \hyxmp@find@metadata
        \hyxmp@embed@packet
      }%
    }%
    {\PackageWarningNoLine{hyperxmp}{%
\jobname.tex failed to include a\MessageBreak
\string\usepackage\string{hyperref\string}
in the preamble.\MessageBreak
Consequently, all hyperxmp functionality will be\MessageBreak
disabled}%
    }%
}
%    \end{macrocode}
%
%
% \subsection{Manipulating author-supplied data}
%
% The author provides metadata information to \pkgname{hyperxmp} via
% package options to \pkgname{hyperref} or via \pkgname{hyperref}'s
% |\hypersetup| command.  The functions in this section convert
% author-supplied lists (e.g.,~|pdfkeywords={foo, bar, baz}|) into
% \LaTeX\ lists (e.g.,~|\@elt {foo}| |\@elt {bar}| |\@elt {baz}|) that
% can be more easily manipulated (Section~\ref{sec:list-manip}); define
% macros for the \acro{XML} entites |&lt;|, |&gt;|, and |&amp;|
% (Section~\ref{sec:xml-entities}); trim spaces off the ends of strings
% (Section~\ref{sec:trim-spaces}); and, in Section~\ref{sec:text-xml},
% convert text to \acro{XML} (e.g.,~from |<scott+hyxmp@pakin.org>| to
% |&lt;scott+hyxmp@pakin.org&gt;|).
%
% \subsubsection{List manipulation}
% \label{sec:list-manip}
%
% We define a macro for converting a list of comma-separated elements
% (e.g.,~the list of \acro{PDF} keywords) to a list of \LaTeX\
% |\@elt|-separated elements.
%
% \begin{macro}{\hyxmp@commas@to@list}
% Given a macro name~(|#1|) and a comma-separated list~(|#2|), define
% the macro name as the elements of the list, each preceded by |\@elt|.
% (Executing the macro therefore applies |\@elt| to each element in
% turn.)
%    \begin{macrocode}
\newcommand*{\hyxmp@commas@to@list}[2]{%
  \gdef#1{}%
  \expandafter\hyxmp@commas@to@list@i\expandafter#1#2,,%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@commas@to@list@i}
% \begin{macro}{\next}
% Recursively construct macro~|#1| from comma-separated list~|#2|.  Stop
% if |#2| is empty.
%    \begin{macrocode}
\def\hyxmp@commas@to@list@i#1#2,{%
  \gdef\hyxmp@sublist{#2}%
  \ifx\hyxmp@sublist\@empty
    \let\next=\relax
  \else
    \hyxmp@trimspaces\hyxmp@sublist
    \@cons{#1}{{\hyxmp@sublist}}%
    \def\next{\hyxmp@commas@to@list@i{#1}}%
  \fi
  \next
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Character-code and XML entity definitions}
% \label{sec:xml-entities}
%
% The \pkgname{hyperref} package invokes |\pdfstringdef| on its metadata
% parameters, setting every character to \TeX\ category code~11
% (``other'').  To match against these, we have to define a few category
% code~11 characters of our own.  Furthermore, because \acro{XMP} is an
% \acro{XML} format, we have to replace the characters ``|&|'', ``|<|'',
% and ``|>|'' with equivalent \acro{XML} entities.
%
% \begin{macro}{\hyxmp@xml@amp}
% \begin{macro}{\hyxmp@other@amp}
% \begin{macro}{\hyxmp@amp}
% Define category code~11 (``other'') versions of the character~``|&|''
% and map |\hyxmp@other@amp| to its \acro{XML} entity, |&amp;|.
%    \begin{macrocode}
\bgroup
\catcode`\&=11
\gdef\hyxmp@xml@amp{&amp;}
\global\let\hyxmp@other@amp=&
\gdef\hyxmp@amp{&}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@xml@lt}
% \begin{macro}{\hyxmp@other@lt}
% Define a category code~11 (``other'') version of the character~``|<|''
% and map |\hyxmp@other@lt| to its \acro{XML} entity, |&lt;|.
%    \begin{macrocode}
\catcode`\<=11
\gdef\hyxmp@xml@lt{&lt;}
\global\let\hyxmp@other@lt=<
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@xml@gt}
% \begin{macro}{\hyxmp@other@gt}
% Define a category code~11 (``other'') version of the character~``|>|''
% and map |\hyxmp@other@gt| to its \acro{XML} entity, |&gt;|.
%    \begin{macrocode}
\catcode`\>=11
\gdef\hyxmp@xml@gt{&gt;}
\global\let\hyxmp@other@gt=>
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@other@space}
% \begin{macro}{\next}
% Define a category code~11 (``other'') version of the space character.
%    \begin{macrocode}
\def\next#1{#1}
\next{\global\let\hyxmp@other@space= } %
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@other@bs}
% Define a category code~11 (``other'') version of the character~``|\|''.
%    \begin{macrocode}
\catcode`\|=0
\catcode`\\=11
|global|let|hyxmp@other@bs=\
|egroup
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Trimming leading and trailing spaces}
% \label{sec:trim-spaces}
%
% To make it easier for \acro{XMP} processors to manipulate our output we
% define a |\hyxmp@trimspaces| macro to strip leading and trailing
% spaces from various data fields.
%
% \begin{macro}{\hyxmp@trimspaces}
% Redefine a macro as its previous value but without leading or trailing
% spaces.  This code---as well as that for its helper macros,
% |\hyxmp@trimb| and |\hyxmp@trimc|---was taken almost verbatim from a
% solution to an \emph{Around the Bend} puzzle~\cite{Downes1994:ATB15}.
% Inline comments are also taken from the solution text.
%    \begin{macrocode}
\catcode`\Q=3
%    \end{macrocode}
% |\hyxmp@trimspaces\x| redefines |\x| to have the same replacement text
% sans leading and trailing space tokens.
%    \begin{macrocode}
\newcommand{\hyxmp@trimspaces}[1]{%
%    \end{macrocode}
% Use grouping to emulate a multi-token |afterassignment| queue.
%    \begin{macrocode}
  \begingroup
%    \end{macrocode}
% Put ``|\toks 0 {|'' into the |afterassignment| queue.
%    \begin{macrocode}
  \aftergroup\toks\aftergroup0\aftergroup{%
%    \end{macrocode}
% Apply |\hyxmp@trimb| to the replacement text of |#1|, adding a leading
% |\noexpand| to prevent brace stripping and to serve another purpose
% later.
%    \begin{macrocode}
  \expandafter\hyxmp@trimb\expandafter\noexpand#1Q Q}%
%    \end{macrocode}
% Transfer the trimmed text back into |#1|.
%    \begin{macrocode}
  \edef#1{\the\toks0}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@trimb}
% |\hyxmp@trimb| removes a trailing space if present, then calls
% |\hyxmp@trimc| to clean up any leftover bizarre |Q|s, and trim a
% leading space. In order for |\hyxmp@trimc| to work properly we need to
% put back a |Q| first.
%    \begin{macrocode}
\def\hyxmp@trimb#1 Q{\hyxmp@trimc#1Q}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@trimc}
% Execute |\vfuzz| assignment to remove leading space; the |\noexpand|
% will now prevent unwanted expansion of a macro or other expandable
% token at the beginning of the trimmed text.  The |\endgroup| will feed
% in the |\aftergroup| tokens after the |\vfuzz| assignment is
% completed.
%    \begin{macrocode}
\def\hyxmp@trimc#1Q#2{\afterassignment\endgroup \vfuzz\the\vfuzz#1}
\catcode`\Q=11
%    \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Converting text to XML}
% \label{sec:text-xml}
%
% The ``|<|'', ``|>|'', and ``|&|'' characters are significant to \acro{XML}.
% We therefore need to escape them in any author-supplied text.
%
% \begin{macro}{\hyxmp@reencode}
% \changes{v1.3}{2011/04/25}{Introduced this macro to re-encode Unicode
%   strings as 8-bit strings before manipulating them into \acro{XMP}
%   schema.  This change addresses a bug reported by Martin M\"unch}
% \begin{macro}{\hyxmp@reencoded}
% Given a control word that expands to a Unicode (|utf16be|) string,
% re-encode it in a more basic (|pdfdoc|) 8-bit encoding so we don't
% wind up escaping each octet of what should be a single 16-bit
% character.  The bulk of the work is left to |\EdefUnescapeString| from
% the \pkgname{pdfescape} package and |\StringEncodingConvert| and to
% |\StringEncodingSuccessFailure| from the \pkgname{stringenc} package.
%    \begin{macrocode}
\newcommand*{\hyxmp@reencode}[1]{%
  \EdefUnescapeString\hyxmp@reencoded{#1}%
  \StringEncodingConvert\hyxmp@reencoded\hyxmp@reencoded{utf16be}{pdfdoc}%
  \StringEncodingSuccessFailure{%
    \global\let\hyxmp@reencoded=\hyxmp@reencoded
  }{%
    \gdef\hyxmp@reencoded{#1}%
  }%
  \edef#1{\hyxmp@reencoded}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@xmlify}
% \begin{macro}{\hyxmp@xmlified}
% \begin{macro}{\hyxmp@text}
% Given a piece of text defined using |\pdfstringdef| (i.e.,~with many
% special characters redefined to have category code~11), set
% |\hyxmp@xmlified| to the same text but with all occurrences of~``|<|''
% replaced with~|&lt;|, all occurrences of~``|>|'' replaced with~|&gt;|,
% and all occurrences of~``|&|'' replaced with~|&amp;|.
%
% If |\pdfmark| is defined then there's a chance the user will run
% \cmdname{dvips} on the resulting \acro{DVI} file and \cmdname{dvips}
% may convert some of the spaces to newlines, which is problematic for
% the proper display of an \acro{XMP} packet.  We therefore
% conditionally invoke
% |\hyxmp@obscure@spaces| to replace all spaces with |&#32;|.
%    \begin{macrocode}
\newcommand*{\hyxmp@xmlify}[1]{%
  \gdef\hyxmp@xmlified{}%
  \edef\hyxmp@text{#1}%
  \ifHy@unicode
    \hyxmp@reencode\hyxmp@text
  \fi
  \expandafter\hyxmp@xmlify@i\hyxmp@text\@empty
  \@ifundefined{pdfmark}{}{%
    \expandafter\hyxmp@obscure@spaces\expandafter{\hyxmp@xmlified}%
  }%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@xmlify@i}
% \begin{macro}{\hyxmp@one@token}
% Bind the next token in the input stream to |\hyxmp@one@token| and
% invoke |\hyxmp@xmlify@ii|.  |\hyxmp@xmlify@i| (and therefore
% |\hyxmp@xmlify@ii|) is invoked on each character in the text supplied
% to |\hyxmp@xmlify|.
%    \begin{macrocode}
\def\hyxmp@xmlify@i{\futurelet\hyxmp@one@token\hyxmp@xmlify@ii}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@xmlify@ii}
% \begin{macro}{\next}
% Given a token in |\hyxmp@one@token|, define |\next| to consume the
% token, append the corresponding text to |\hyxmp@xmlified|, and
% recursively invoke |\hyxmp@xmlify@i| to consume subsequent tokens.
%    \begin{macrocode}
\def\hyxmp@xmlify@ii{%
  \if\hyxmp@one@token\hyxmp@other@lt
%    \end{macrocode}
% Replace ``|<|'' with |&lt;|.
%    \begin{macrocode}
    \def\next##1{%
      \xdef\hyxmp@xmlified{\hyxmp@xmlified\hyxmp@xml@lt}%
      \hyxmp@xmlify@i
    }%
  \else
    \if\hyxmp@one@token\hyxmp@other@gt
%    \end{macrocode}
% Replace ``|>|'' with |&gt;|.
%    \begin{macrocode}
      \def\next##1{%
        \xdef\hyxmp@xmlified{\hyxmp@xmlified\hyxmp@xml@gt}%
        \hyxmp@xmlify@i
      }%
    \else
      \if\hyxmp@one@token\hyxmp@other@amp
%    \end{macrocode}
% Replace ``|&|'' with |&amp;|.
%    \begin{macrocode}
        \def\next##1{%
          \xdef\hyxmp@xmlified{\hyxmp@xmlified\hyxmp@xml@amp}%
          \hyxmp@xmlify@i
        }%
      \else
        \ifx\hyxmp@one@token\hyxmp@other@space
%    \end{macrocode}
% Store spaces.  We need a special case for this to avoid inadvertently
% discarding spaces.
%    \begin{macrocode}
          \def\next##1{%
            \g@addto@macro\hyxmp@xmlified{ }%
            \hyxmp@xmlify@i##1%
          }%
        \else
          \if\hyxmp@one@token\hyxmp@other@bs
%    \end{macrocode}
% Replace |\|\meta{ooo} with |&#|\meta{ddd}|;|.  For example, |\100|,
% the octal code for ``|@|'', is represented in \acro{XML} as~|&#64;|.
%    \begin{macrocode}
            \def\next##1{\futurelet\hyxmp@one@token\hyxmp@xmlify@iii}
          \else
            \ifx\hyxmp@one@token\@empty
%    \end{macrocode}
% End the recursion upon encountering |\@empty|.
%    \begin{macrocode}
              \def\next##1{}%
            \else
%    \end{macrocode}
% In most cases we merely append the next character in the input to
% |\hyxmp@xmlified| without any special processing.
%    \begin{macrocode}
              \def\next##1{%
                \g@addto@macro\hyxmp@xmlified{##1}%
                \hyxmp@xmlify@i
              }%
            \fi
          \fi
        \fi
      \fi
    \fi
  \fi
%    \end{macrocode}
% Recursively process the next character in the input stream.
%    \begin{macrocode}
  \next
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@xmlify@iii}
% \begin{macro}{\next}
% \pkgname{hyperref}'s |\pdfstringdef| macro converts certain special
% characters to a backslash followed by a three-digit octal number.
% However, it also replaces ``|(|'' and ``|)|'' with ``|\(|'' and
% ``|\)|''.  The |\hyxmp@xmlify@iii| macro is called after encountering
% (and removing) a backslash.  If the next character in the input stream
% (|\hyxmp@one@token|) is a parenthesis, |\hyxmp@xmlify@iii| leaves it
% alone.  Otherwise, |\hyxmp@xmlify@iii| assumes it's an octal number
% and replaces it with its \acro{XML} equivalent.
%    \begin{macrocode}
\def\hyxmp@xmlify@iii{%
  \def\next##1##2##3{%
    \@tempcnta='##1##2##3
    \xdef\hyxmp@xmlified{\hyxmp@xmlified
      \hyxmp@amp\hyxmp@hash\the\@tempcnta;%
    }%
    \hyxmp@xmlify@i
  }%
  \if\hyxmp@one@token(
    \let\next=\hyxmp@xmlify@i
  \else
    \if\hyxmp@one@token)
      \let\next=\hyxmp@xmlify@i
    \fi
  \fi
  \next
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@obscure@spaces}
% The \cmdname{dvips} backend rather obnoxiously word-wraps text.  Doing
% so can cause \acro{XMP} metadata to be displayed incorrectly.  For
% example, Adobe Acrobat displays the document's \xmpterm{dc:rights}
% (copyright notice) within a single-line field.  By introducing an
% extra line break in the middle of the copyright notice,
% \cmdname{dvips} implicitly causes it to be truncated when displayed.
%
% To thwart \cmdname{dvips}'s word-wrapping, we define
% |\hyxmp@obscure@spaces| to replace each space in a given piece of text
% with an \acro{XML} |&#32;| (space) entity.
%    \begin{macrocode}
\newcommand*{\hyxmp@obscure@spaces}[1]{%
  \gdef\hyxmp@xmlified{}%
  \expandafter\hyxmp@obscure@spaces@i#1 {} %
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@obscure@spaces@i}
% \begin{macro}{\hyxmp@one@token}
% \begin{macro}{\next}
% Do all of the work for |\hyxmp@obscure@spaces|.
%    \begin{macrocode}
\def\hyxmp@obscure@spaces@i #1 #2 {%
  \def\hyxmp@one@token{#2}%
  \ifx\hyxmp@one@token\@empty
    \xdef\hyxmp@xmlified{\hyxmp@xmlified#1}%
    \let\next=\relax
  \else
    \xdef\hyxmp@xmlified{\hyxmp@xmlified#1\hyxmp@amp\hyxmp@hash32;}%
    \def\next{\expandafter\hyxmp@obscure@spaces@i\expandafter#2 }%
  \fi
  \next
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \subsection{UUID generation}
% \label{sec:uuid-gen}
%
% We use a linear congruential generator to produce pseudorandom
% \acro{UUID}s.  True, this method has its flaws but it's simple to
% implement in \TeX\ and is good enough for producing the \acro{XMP}
% \xmpterm{xapMM:DocumentID} and \xmpterm{xapMM:InstanceID} fields.
%
% \begin{macro}{\hyxmp@modulo@a}
% Replace the contents of |\@tempcnta| with the contents modulo~|#1|.
% Note that |\@tempcntb| is overwritten in the process.
%    \begin{macrocode}
\def\hyxmp@modulo@a#1{%
  \@tempcntb=\@tempcnta
  \divide\@tempcntb by #1
  \multiply\@tempcntb by #1
  \advance\@tempcnta by -\@tempcntb
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@big@prime}
% \begin{macro}{\hyxmp@big@prime@ii}
% Define a couple of large prime numbers that can still be stored in a
% \TeX\ counter.
%    \begin{macrocode}
\def\hyxmp@big@prime{536870923}
\def\hyxmp@big@prime@ii{536870027}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@seed@rng}
% \begin{macro}{\hyxmp@one@token}
% Seed \pkgname{hyperxmp}'s random-number generator from a given piece
% of text.
%    \begin{macrocode}
\def\hyxmp@seed@rng#1{%
  \@tempcnta=\hyxmp@big@prime
  \futurelet\hyxmp@one@token\hyxmp@seed@rng@i#1\@empty
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@seed@rng@i}
% \begin{macro}{\hyxmp@one@token}
% \begin{macro}{\next}
% Do all of the work for |\hyxmp@seed@rng|.  For each character code $c$
% of the input text, assign $\mathtt{\string\@tempcnta} \leftarrow 3
% \cdot \mathtt{\string\@tempcnta} + c
% \pmod{\mathtt{\string\hyxmp@big@prime}}$.
%    \begin{macrocode}
\def\hyxmp@seed@rng@i{%
  \ifx\hyxmp@one@token\@empty
    \let\next=\relax
  \else
    \def\next##1{%
      \multiply\@tempcnta by 3
      \advance\@tempcnta by `##1
      \hyxmp@modulo@a{\hyxmp@big@prime}%
      \futurelet\hyxmp@one@token\hyxmp@seed@rng@i
    }%
  \fi
  \next
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@set@rand@num}
% \begin{macro}{\hyxmp@rand@num}
% Advance |\hyxmp@rand@num| to the next pseudorandom number in the
% sequence.  Specifically, we assign $\mathtt{\string\hyxmp@rand@num}
% \leftarrow 3 \cdot \mathtt{\string\hyxmp@rand@num} +
% \mathtt{\string\hyxmp@big@prime@ii}
% \pmod{\mathtt{\string\hyxmp@big@prime}}$.  Note that both |\@tempcnta|
% and |\@tempcntb| are overwritten in the process.
%    \begin{macrocode}
\def\hyxmp@set@rand@num{%
  \@tempcnta=\hyxmp@rand@num
  \multiply\@tempcnta by 3
  \advance\@tempcnta by \hyxmp@big@prime@ii
  \hyxmp@modulo@a{\hyxmp@big@prime}%
  \xdef\hyxmp@rand@num{\the\@tempcnta}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@append@hex}
% Append a randomly selected hexadecimal digit to macro~|#1|.  Note that
% both |\@tempcnta| and |\@tempcntb| are overwritten in the process.
%    \begin{macrocode}
\def\hyxmp@append@hex#1{%
  \hyxmp@set@rand@num
  \@tempcnta=\hyxmp@rand@num
  \hyxmp@modulo@a{16}%
  \ifnum\@tempcnta<10
    \xdef#1{#1\the\@tempcnta}%
  \else
%    \end{macrocode}
% There \emph{must} be a better way to handle the numbers~10--15 than
% with |\ifcase|.
%    \begin{macrocode}
    \advance\@tempcnta by -10
    \ifcase\@tempcnta
      \xdef#1{#1a}%
      \or\xdef#1{#1b}%
      \or\xdef#1{#1c}%
      \or\xdef#1{#1d}%
      \or\xdef#1{#1e}%
      \or\xdef#1{#1f}%
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@append@hex@iv}
% Invoke |\hyxmp@append@hex| four times.
%    \begin{macrocode}
\def\hyxmp@append@hex@iv#1{%
  \hyxmp@append@hex#1%
  \hyxmp@append@hex#1%
  \hyxmp@append@hex#1%
  \hyxmp@append@hex#1%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@create@uuid}
% Define macro~|#1| as a \acro{UUID} of the form
% ``|uuid:|\textit{xxxxxxxx}|-|\textit{xxxx}|-|\textit{xxxx}|-|\textit{xxxxxxxxxxxx}''
% in which each ``\textit{x}'' is a lowercase hexadecimal digit.  We
% assume that the random-number generator is already seeded.  Note that
% |\hyxmp@create@uuid| overwrites both |\@tempcnta| and |\@tempcntb|.
%    \begin{macrocode}
\def\hyxmp@create@uuid#1{%
  \def#1{uuid:}%
  \hyxmp@append@hex@iv#1%
  \hyxmp@append@hex@iv#1%
  \g@addto@macro#1{-}%
  \hyxmp@append@hex@iv#1%
  \g@addto@macro#1{-}%
  \hyxmp@append@hex@iv#1%
  \g@addto@macro#1{-}%
  \hyxmp@append@hex@iv#1%
  \hyxmp@append@hex@iv#1%
  \hyxmp@append@hex@iv#1%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@def@DocumentID}
% \begin{macro}{\hyxmp@DocumentID}
% Seed the random-number generator with a function of the current
% filename, \acro{PDF} document title, and \acro{PDF} author, then invoke
% |\hyxmp@create@uuid| to define |\hyxmp@DocumentID| as a random \acro{UUID}.
%    \begin{macrocode}
\newcommand*{\hyxmp@def@DocumentID}{%
  \edef\hyxmp@seed@string{\jobname:\@pdftitle:\@pdfauthor}%
  \expandafter\hyxmp@seed@rng\expandafter{\hyxmp@seed@string}%
  \edef\hyxmp@rand@num{\the\@tempcnta}%
  \hyxmp@create@uuid\hyxmp@DocumentID
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@def@InstanceID}
% \begin{macro}{\hyxmp@InstanceID}
% Seed the random-number generator with a function of the current
% filename, \acro{PDF} document title, \acro{PDF} author, and the
% current day, month, year, and minutes since midnight, then invoke
% |\hyxmp@create@uuid| to define |\hyxmp@InstanceID| as a random
% \acro{UUID}.
%    \begin{macrocode}
\newcommand*{\hyxmp@def@InstanceID}{%
  \edef\hyxmp@seed@string{%
    \jobname:\@pdftitle:\@pdfauthor:%
    \the\year/\the\month/\the\day:%
    \the\time
  }%
  \expandafter\hyxmp@seed@rng\expandafter{\hyxmp@seed@string}%
  \edef\hyxmp@rand@num{\the\@tempcnta}%
  \hyxmp@create@uuid\hyxmp@InstanceID
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \subsection{Constructing the XMP packet}
%
% An \acro{XMP} packet ``shall consist of the following, in order: a
% header \acro{PI}, the serialized \acro{XMP} data model (the \acro{XMP}
% packet) with optional white-space padding, and a trailer
% \acro{PI}''~\cite{Adobe2010:XMP}.  (``\acro{PI}'' is an abbreviation
% for ``processing instructions'').  The serialized \acro{XMP} includes
% blocks of \acro{XML} for various \acro{XMP} schemata: Adobe \acro{PDF}
% (Section~\ref{sec:adobe-pdf}), Dublin Core
% (Section~\ref{sec:dublin-core}), \acro{XMP} Rights Management
% (Section~\ref{sec:xmp-rights}), and \acro{XMP} Media Management
% (Section~\ref{sec:xmp-media}).  The |\hyxmp@construct@packet| macro
% constructs the \acro{XMP} packet into |\hyxmp@xml|.  It first writes
% the appropriate \acro{XML} header, then calls the various
% schema-writing macros, then injects |\hyxmp@padding| as padding, and
% finally writes the appropriate \acro{XML} trailer.
%
% \subsubsection{XMP utility functions}
%
% \begin{macro}{\hyxmp@add@to@xml}
% Given a piece of text, replace all underscores with category-code~11
% (``other'') spaces and append the result to the |\hyxmp@xml| macro.
%    \begin{macrocode}
\newcommand*{\hyxmp@add@to@xml}[1]{%
  \bgroup
    \@tempcnta=0
    \loop
      \lccode\@tempcnta=\@tempcnta
      \advance\@tempcnta by 1
      \ifnum\@tempcnta<256
    \repeat
    \lccode`\_=`\ \relax
    \lowercase{\xdef\hyxmp@xml{\hyxmp@xml#1}}%
  \egroup
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@hash}
% Define a category-code~11 (``other'') version of the ``|#|'' character.
%    \begin{macrocode}
\bgroup
\catcode`\#=11
\gdef\hyxmp@hash{#}
\egroup
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@padding}
% \begin{macro}{\hyxmp@xml}
% The \acro{XMP} specification recommends leaving approximately
% 2000~bytes of whitespace at the end of each \acro{XMP} packet to
% facilitate editing the packet in place~\cite{Adobe2010:XMP}.
% |\hyxmp@padding| is defined to contain 32~lines of 50~spaces and a
% newline apiece for a total of 1632 characters of whitespace.
%    \begin{macrocode}
\bgroup
  \xdef\hyxmp@xml{}%
  \hyxmp@add@to@xml{%
__________________________________________________^^J%
  }
  \xdef\hyxmp@padding{\hyxmp@xml}%
\egroup
\xdef\hyxmp@padding{\hyxmp@padding\hyxmp@padding}
\xdef\hyxmp@padding{\hyxmp@padding\hyxmp@padding}
\xdef\hyxmp@padding{\hyxmp@padding\hyxmp@padding}
\xdef\hyxmp@padding{\hyxmp@padding\hyxmp@padding}
\xdef\hyxmp@padding{\hyxmp@padding\hyxmp@padding}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\hyxmp@today}
% Define today's date in \textit{YYYY}-\textit{MM}-\textit{DD} format.
%    \begin{macrocode}
\xdef\hyxmp@today{\the\year}%
\ifnum\month<10
  \xdef\hyxmp@today{\hyxmp@today-0\the\month}%
\else
  \xdef\hyxmp@today{\hyxmp@today-\the\month}%
\fi
\ifnum\day<10
  \xdef\hyxmp@today{\hyxmp@today-0\the\day}%
\else
  \xdef\hyxmp@today{\hyxmp@today-\the\day}%
\fi
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{The Adobe PDF schema}
% \label{sec:adobe-pdf}
%
% \begin{macro}{\hyxmp@pdf@schema}
% Add properties defined by the Adobe \acro{PDF} schema to the |\hyxmp@xml|
% macro.
%    \begin{macrocode}
\newcommand*{\hyxmp@pdf@schema}{%
%    \end{macrocode}
% \begin{macro}{\hyxmp@have@any}
% Include an Adobe \acro{PDF} schema block if at least one of
% |\@pdfkeywords| and |\@pdfproducer| is defined.
%    \begin{macrocode}
  \let\hyxmp@have@any=!%
  \ifx\@pdfkeywords\@empty
    \ifx\@pdfproducer\@empty
      \let\hyxmp@have@any=\@empty
    \fi
  \fi
  \ifx\hyxmp@have@any\@empty
  \else
%    \end{macrocode}
% Add a block of \acro{XML} to |\hyxmp@xml| that lists the document's
% keywords (the \xmpterm{pdf:Keywords} property) and the tools used to
% produce the \acro{PDF} file (the \xmpterm{pdf:Producer} property).
%    \begin{macrocode}
    \hyxmp@add@to@xml{%
______<rdf:Description rdf:about=""^^J%
___________xmlns:pdf="http://ns.adobe.com/pdf/1.3/">^^J%
    }%
    \ifx\@pdfkeywords\@empty
    \else
      \hyxmp@xmlify{\@pdfkeywords}%
      \hyxmp@add@to@xml{%
___________<pdf:Keywords>\hyxmp@xmlified</pdf:Keywords>^^J%
      }%
    \fi
    \ifx\@pdfproducer\@empty
    \else
      \hyxmp@xmlify{\@pdfproducer}%
      \hyxmp@add@to@xml{%
___________<pdf:Producer>\hyxmp@xmlified</pdf:Producer>^^J%
      }%
    \fi
    \hyxmp@add@to@xml{%
______</rdf:Description>^^J%
    }%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{The Dublin Core schema}
% \label{sec:dublin-core}
%
% \begin{macro}{\hyxmp@rdf@dc}
% Given a Dublin Core property~(|#1|) and a macro containing some
% |\pdfstringdef|-defined text~(|#2|), append the appropriate block of
% \acro{XML} to the |\hyxmp@xml| macro but only if |#2| is non-empty.
%    \begin{macrocode}
\newcommand*{\hyxmp@rdf@dc}[2]{%
  \ifx#2\@empty
  \else
    \hyxmp@xmlify{#2}%
    \hyxmp@add@to@xml{%
_________<dc:#1>^^J%
____________<rdf:Alt>^^J%
_______________<rdf:li xml:lang="\@pdfmetalang">\hyxmp@xmlified</rdf:li>^^J%
____________</rdf:Alt>^^J%
_________</dc:#1>^^J%
    }%
  \fi%
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@list@to@xml}
% Given a Dublin Core property~(|#1|), an RDF array~(|#2|), and a macro
% containing a comma-separated list~(|#3|), append the appropriate block
% of \acro{XML} to the |\hyxmp@xml| macro but only if |#3| is non-empty.
%    \begin{macrocode}
\newcommand*{\hyxmp@list@to@xml}[3]{%
  \ifx#3\@empty
  \else
    \hyxmp@add@to@xml{%
_________<dc:#1>^^J%
____________<rdf:#2>^^J%
    }%
    \bgroup
%    \end{macrocode}
% \begin{macro}{\hyxmp@text}
% \begin{macro}{\@elt}
% We store the comma-separated list in |\hyxmp@text| so we can re-encode
% it from Unicode if necessary.  We then redefine |\@elt| to
% \acro{XML}-ify each element of the list and append it to
% |\hyxmp@xmlified|.
%    \begin{macrocode}
      \edef\hyxmp@text{#3}%
      \ifHy@unicode
        \hyxmp@reencode\hyxmp@text
      \fi
      \hyxmp@commas@to@list\hyxmp@list{\hyxmp@text}%
      \def\@elt##1{%
        \hyxmp@xmlify{##1}%
        \hyxmp@add@to@xml{%
_______________<rdf:li>\hyxmp@xmlified</rdf:li>^^J%
        }%
      }%
      \hyxmp@list
    \egroup
    \hyxmp@add@to@xml{%
____________</rdf:#2>^^J%
_________</dc:#1>^^J%
    }%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\hyxmp@dc@schema}
% Add properties defined by the Dublin Core schema to the |\hyxmp@xml|
% macro.  Specifically, we add entries for the \xmpterm{dc:title} property
% if the author specified a |pdftitle|, the \xmpterm{dc:description}
% property if the author specified a |pdfsubject|, the \xmpterm{dc:rights}
% property if the author specified a |pdfcopyright|, the
% \xmpterm{dc:creator} property if the author specified a |pdfauthor|, and
% the \xmpterm{dc:subject} property if the author specified |pdfkeywords|.
% We also specify the \xmpterm{dc:date} property using the date the
% document was run through \LaTeX.
%    \begin{macrocode}
\newcommand*{\hyxmp@dc@schema}{%
  \hyxmp@add@to@xml{%
______<rdf:Description rdf:about=""^^J%
____________xmlns:dc="http://purl.org/dc/elements/1.1/">^^J%
_________<dc:format>application/pdf</dc:format>^^J%
  }%
  \hyxmp@rdf@dc{title}{\@pdftitle}%
  \hyxmp@rdf@dc{description}{\@pdfsubject}%
  \hyxmp@rdf@dc{rights}{\@pdfcopyright}%
  \hyxmp@list@to@xml{creator}{Seq}{\@pdfauthor}%
  \hyxmp@list@to@xml{subject}{Bag}{\@pdfkeywords}%
  \hyxmp@list@to@xml{date}{Seq}{\hyxmp@today}%
  \hyxmp@add@to@xml{%
______</rdf:Description>^^J%
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{The XMP Rights Management schema}
% \label{sec:xmp-rights}
%
% \begin{macro}{\hyxmp@xapRights@schema}
% Add properties defined by the XMP Rights Management schema to the
% |\hyxmp@xml| macro.  Currently, these are only the
% \xmpterm{xapRights:Marked} property and the
% \xmpterm{xapRights:WebStatement} property and only if the author
% defined a |pdflicenseurl|.
%    \begin{macrocode}
\newcommand*{\hyxmp@xapRights@schema}{%
  \ifx\@pdflicenseurl\@empty
  \else
    \hyxmp@xmlify{\@pdflicenseurl}%
    \hyxmp@add@to@xml{%
______<rdf:Description rdf:about=""^^J%
___________xmlns:xapRights="http://ns.adobe.com/xap/1.0/rights/">^^J%
_________<xapRights:Marked>True</xapRights:Marked>^^J%
_________<xapRights:WebStatement>\hyxmp@xmlified</xapRights:WebStatement>^^J%
______</rdf:Description>^^J%
    }%
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{The XMP Media Management schema}
% \label{sec:xmp-media}
%
% \begin{macro}{\hyxmp@mm@schema}
% Add properties defined by the XMP Media Management schema to the
% |\hyxmp@xml| macro.  According to the \acro{XMP} specification, the
% \xmpterm{xapMM:DocumentID} property is supposed to uniquely identify a
% document, and the \xmpterm{xapMM:InstanceID} property is supposed to
% change with each save operation~\cite{Adobe2010:XMP}.  As seen in
% Section~\ref{sec:uuid-gen}, we do what we can to honor this intention
% from within a \TeX-based workflow.
%    \begin{macrocode}
\gdef\hyxmp@mm@schema{%
  \hyxmp@def@DocumentID
  \hyxmp@def@InstanceID
  \hyxmp@add@to@xml{%
______<rdf:Description rdf:about=""^^J%
____________xmlns:xapMM="http://ns.adobe.com/xap/1.0/mm/">^^J%
_________<xapMM:DocumentID>\hyxmp@DocumentID</xapMM:DocumentID>^^J%
_________<xapMM:InstanceID>\hyxmp@InstanceID</xapMM:InstanceID>^^J%
______</rdf:Description>^^J%
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{The Photoshop schema}
% \label{sec:photoshop}
%
% \begin{macro}{\hyxmp@photoshop@schema}
% \begin{macro}{\hyxmp@photoshop@data}
% Add properties defined by the Photoshop schema to the |\hyxmp@xml|
% macro.  We support only the \xmpterm{photoshop:AuthorsPosition} and
% \xmpterm{photoshop:CaptionWriter} properties, as that's all that Adobe
% Acrobat currently displays.
% \changes{v1.2}{2010/06/07}{Added support for the Photoshop schema}
%    \begin{macrocode}
\gdef\hyxmp@photoshop@schema{%
  \edef\hyxmp@photoshop@data{\@pdfauthortitle\@pdfcaptionwriter}%
  \ifx\hyxmp@photoshop@data\@empty
  \else
    \hyxmp@add@to@xml{%
______<rdf:Description rdf:about=""^^J%
____________xmlns:photoshop="http://ns.adobe.com/photoshop/1.0/">^^J%
    }%
  \fi
%    \end{macrocode}
%    \begin{macrocode}
  \ifx\@pdfauthortitle\@empty
  \else
    \hyxmp@xmlify{\@pdfauthortitle}%
    \hyxmp@add@to@xml{%
_________<photoshop:AuthorsPosition>\hyxmp@xmlified</photoshop:AuthorsPosition>^^J%
    }%
  \fi
%    \end{macrocode}
%    \begin{macrocode}
  \ifx\@pdfcaptionwriter\@empty
  \else
    \hyxmp@xmlify{\@pdfcaptionwriter}%
    \hyxmp@add@to@xml{%
_________<photoshop:CaptionWriter>\hyxmp@xmlified</photoshop:CaptionWriter>^^J%
    }%
  \fi
%    \end{macrocode}
%    \begin{macrocode}
  \ifx\hyxmp@photoshop@data\@empty
  \else
    \hyxmp@add@to@xml{%
______</rdf:Description>^^J%
    }%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Constructing the XMP packet}
%
% \begin{macro}{\hyxmp@construct@packet}
% \begin{macro}{\hyxmp@xml}
% Successively add \acro{XML} data to |\hyxmp@xml| until we have
% something we can insert into the document's \acro{PDF} catalog.  The
% \acro{XMP} specification states that the argument to the
% \xmpterm{begin} attribute is supposed to be ``the Unicode character
% U+FEFF used as a byte-order marker''~\cite{Adobe2010:XMP}, so that's
% what we use, although inserted as the 8-bit character sequence
% \meta{EF}\meta{BB}\meta{BF}.  We explicitly mark those characters as
% character code~12 (``letter'') because the \pkgname{inputenc} package
% re-encodes them as character code~13 (``active''), which causes
% \LaTeX\ to abort with an ``\texttt{Undefined control sequence}'' error
% upon invoking |\hyxmp@construct@packet|.
% \changes{v1.1}{2006/05/21}{Explicitly set the category codes of
%   characters \string\meta{EF}, \string\meta{BB}, and
%   \string\meta{BF} to ``letter''.  Thanks to Daniel Sch\"omer for
%   the bug report}
%    \begin{macrocode}
\bgroup
\catcode`\^^ef=12
\catcode`\^^bb=12
\catcode`\^^bf=12
\gdef\hyxmp@construct@packet{%
  \gdef\hyxmp@xml{}%
  \hyxmp@add@to@xml{%
<?xpacket begin="^^ef^^bb^^bf" id="W5M0MpCehiHzreSzNTczkc9d"?>^^J%
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="3.1-702">^^J%
___<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns\hyxmp@hash">^^J%
  }%
  \hyxmp@pdf@schema
  \hyxmp@xapRights@schema
  \hyxmp@dc@schema
  \hyxmp@photoshop@schema
  \hyxmp@mm@schema
  \hyxmp@add@to@xml{%
___</rdf:RDF>^^J%
</x:xmpmeta>^^J%
\hyxmp@padding
<?xpacket end="w"?>^^J%
  }%
}
\egroup
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \subsection{Embedding the XMP packet}
%
% The \acro{PDF} specification says that ``a metadata stream may be
% attached to a document through the \pdfterm{Metadata} entry in the
% document catalogue''~\cite{Adobe2008:PDF} so that's what we do here.
%
% \begin{macro}{\hyxmp@embed@packet}
% \begin{macro}{\hyxmp@driver}
% Determine which \pkgname{hyperref} driver is in use and invoke the
% appropriate embedding function.
%    \begin{macrocode}
\newcommand*{\hyxmp@embed@packet}{%
  \hyxmp@construct@packet
  \def\hyxmp@driver{hpdftex}%
  \ifx\hyxmp@driver\Hy@driver
    \hyxmp@embed@packet@pdftex
  \else
    \def\hyxmp@driver{hdvipdfm}%
    \ifx\hyxmp@driver\Hy@driver
      \hyxmp@embed@packet@dvipdfm
    \else
      \def\hyxmp@driver{hxetex}%
      \ifx\hyxmp@driver\Hy@driver
        \hyxmp@embed@packet@xetex
      \else
        \@ifundefined{pdfmark}{%
          \PackageWarningNoLine{hyperxmp}{%
            Unrecognized hyperref driver `\Hy@driver'.\MessageBreak
            \jobname.tex's XMP metadata will *not* be\MessageBreak
            embedded in the resulting file}%
        }{%
          \hyxmp@embed@packet@pdfmark
        }%
      \fi
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Embedding using pdf\TeX}
%
% \begin{macro}{\hyxmp@embed@packet@pdftex}
% Embed the \acro{XMP} packet using pdf\TeX\ primitives.
%    \begin{macrocode}
\newcommand*{\hyxmp@embed@packet@pdftex}{%
  \bgroup
    \pdfcompresslevel=0
    \immediate\pdfobj stream attr {%
      /Type /Metadata
      /Subtype /XML
    }{\hyxmp@xml}%
    \pdfcatalog {/Metadata \the\pdflastobj\space 0 R}%
  \egroup
}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Embedding using any \texttt{pdfmark}-based backend}
%
% \begin{macro}{\hyxmp@embed@packet@pdfmark}
% Embed the \acro{XMP} packet using \pkgname{hyperref}'s |\pdfmark|
% command.  I believe |\pdfmark| is used by the |dvipdf|, |dvipsone|,
% |dvips|, |dviwindo|, |nativepdf|, |pdfmark|, |ps2pdf| |textures|, and
% |vtexpdfmark| options to \pkgname{hyperref} but I've tested only a few
% of those.
%    \begin{macrocode}
\newcommand*{\hyxmp@embed@packet@pdfmark}{%
  \pdfmark{%
    pdfmark=/OBJ,
    Raw={/_objdef \string{hyxmp@Metadata\string} /type /stream}%
  }%
  \pdfmark{%
    pdfmark=/PUT,
    Raw={\string{hyxmp@Metadata\string}%
      <<
        /Type /Metadata
        /Subtype /XML
      >>
    }%
  }%
  \pdfmark{%
    pdfmark=/PUT,
    Raw={\string{hyxmp@Metadata\string} (\hyxmp@xml)}%
  }%
  \pdfmark{%
    pdfmark=/CLOSE,
    Raw={\string{hyxmp@Metadata\string}}%
  }%
%    \end{macrocode}
% Adobe's |pdfmark| reference indicates that a metadata stream should be
% added to the document catalog by specifying the \pdfterm{Metadata}
% |pdfmark|~\cite{Adobe2010:pdfmark}.  However, earlier versions of
% Adobe Acrobat Distiller (pre-6.0) and Ghostscript ignored
% \pdfterm{Metadata} but honored \pdfterm{PUT} so that's what versions
% of \pkgname{hyperxmp} prior to~1.3 used.  As all current
% PostScript-to-\acro{PDF} generators seem to honor the
% \pdfterm{Metadata} |pdfmark|, \pkgname{hyperxmp} now uses that
% mechanism to point the document catalog to our metadata stream.
%    \begin{macrocode}
  \pdfmark{%
    pdfmark=/Metadata,
    Raw={\string{Catalog\string}%
      <<
        /Metadata \string{hyxmp@Metadata\string}%
      >>
    }%
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Embedding using \texttt{dvipdfm}}
%
% \begin{macro}{\hyxmp@embed@packet@dvipdfm}
% Embed the \acro{XMP} packet using \cmdname{dvipdfm}-specific
% |\special| commands.  Note that \cmdname{dvipdfm} rather irritatingly
% requires us to count the number of characters in the |\hyxmp@xml|
% stream ourselves.
%    \begin{macrocode}
\newcommand*{\hyxmp@embed@packet@dvipdfm}{%
  \hyxmp@string@len{\hyxmp@xml}%
  \special{pdf: object @hyxmp@Metadata
    <<
      /Type /Metadata
      /Subtype /XML
      /Length \the\@tempcnta
    >>
    stream^^J\hyxmp@xml endstream%
  }%
  \special{pdf: docview
    <<
      /Metadata @hyxmp@Metadata
    >>
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@string@len}
% Set |\@tempcnta| to the number of characters in a given string~(|#1|).
% The approach is first to tally the number of space characters then to
% tally the number of non-space characters.  While this is rather
% sloppy I haven't found a better way to achieve the same effect,
% especially given that all of the characters in |#1| have already been
% assigned their category codes.
%    \begin{macrocode}
\newcommand*{\hyxmp@string@len}[1]{%
  \@tempcnta=0
  \expandafter\hyxmp@count@spaces#1 {} %
  \expandafter\hyxmp@count@non@spaces#1{}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@count@spaces}
% Count the number of spaces in a given string.  We rely on the built-in
% pattern matching of \TeX's |\def| primitive to pry one word at a time
% off the head of the input string.
%    \begin{macrocode}
\def\hyxmp@count@spaces#1 {%
  \def\hyxmp@one@token{#1}%
  \ifx\hyxmp@one@token\@empty
    \advance\@tempcnta by -1
  \else
    \advance\@tempcnta by 1
    \expandafter\hyxmp@count@spaces
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hyxmp@count@non@spaces}
% Count the number of non-spaces in a given string.  Ideally, we'd count
% both spaces and non-spaces but |\TeX| won't bind |#1| to a space
% character (category code~10).  Hence, in each iteration, |#1| is bound
% to the next non-space character only.
%    \begin{macrocode}
\newcommand*{\hyxmp@count@non@spaces}[1]{%
  \def\hyxmp@one@token{#1}%
  \ifx\hyxmp@one@token\@empty
  \else
    \advance\@tempcnta by 1
    \expandafter\hyxmp@count@non@spaces
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Embedding using \XeTeX}
%
% \begin{macro}{\hyxmp@embed@packet@xetex}
% Embed the \acro{XMP} packet using \cmdname{xdvipdfmx}-specific
% |\special| commands.  I don't know how to tell \cmdname{xdvipdfmx}
% always to leave the \pdfterm{Metadata} stream uncompressed, so the
% \acro{XMP} metadata is likely to be missed by non-\acro{PDF}-aware
% \acro{XMP} viewers.
% \changes{v1.2}{2010/06/04}{Added support for the \XeTeX\ backend
%   (\texttt{xdvipdfmx})}
%    \begin{macrocode}
\newcommand*{\hyxmp@embed@packet@xetex}{%
  \special{pdf:stream @hyxmp@Metadata (\hyxmp@xml)
    <<
      /Type /Metadata
      /Subtype /XML
    >>
  }%
  \special{pdf:put @catalog
    <<
      /Metadata @hyxmp@Metadata
    >>
  }%
}
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Final clean-up}
% \label{sec:clean-up}
%
% Having saved the category code of
% ``\,{\fontencoding{T1}\selectfont\textquotedbl}\,'' at the start of
% the package code (Section~\ref{sec:initial-prep}), we now restore that
% character's original category code.
%
%    \begin{macrocode}
\catcode`\"=\hyxmp@dq@code
%    \end{macrocode}
%
% \Finale
\endinput