| 1 |
|
| 2 |
local M = { } |
| 3 |
local alloc = require('minim-alloc') |
| 4 |
alloc.remember('minim-xmp') |
| 5 |
local cb = require('minim-callbacks') |
| 6 |
|
| 7 |
local function sorted_pairs(t) |
| 8 |
local keys, i = { }, 0 |
| 9 |
for k, _ in pairs(t) do table.insert(keys, k) end |
| 10 |
table.sort(keys) |
| 11 |
return function() |
| 12 |
i = i + 1 |
| 13 |
local k = keys[i] |
| 14 |
if k then |
| 15 |
return k, t[k] |
| 16 |
end |
| 17 |
end |
| 18 |
end |
| 19 |
|
| 20 |
-- 1 Reading and writing metadata values |
| 21 |
|
| 22 |
M.getters = { |
| 23 |
Text = function(v) return v or '' end, |
| 24 |
['Lang Alt'] = function(t, a) return t[a or 'x-default'] or '' end, |
| 25 |
} |
| 26 |
|
| 27 |
M.setters = { |
| 28 |
Text = function(new, orig, opt, k) |
| 29 |
if opt then alloc.msg('Warning: no alternative for %s', k) end |
| 30 |
if orig then alloc.msg('Warning: overwriting metadata field %s', k) end |
| 31 |
return new |
| 32 |
end, |
| 33 |
['Lang Alt'] = function(value, orig, lang) |
| 34 |
orig = orig or { } |
| 35 |
orig[lang or 'x-default'] = value |
| 36 |
return orig |
| 37 |
end, |
| 38 |
Integer = function(new, orig, opt, k) |
| 39 |
if opt then alloc.msg('Warning: no alternative for %s', k) end |
| 40 |
if orig then alloc.msg('Warning: overwriting metadata field %s', k) end |
| 41 |
return tonumber(new) and new or alloc.err('Not a number: %s', new) |
| 42 |
end, |
| 43 |
Date = function(new, orig, opt, k) |
| 44 |
if opt then alloc.msg('Warning: no alternative for %s', k) end |
| 45 |
if orig then alloc.msg('Warning: overwriting metadata field %s', k) end |
| 46 |
return string.match(new, '^%d%d%d%d') |
| 47 |
and new or alloc.err('Date must start with year') |
| 48 |
end, |
| 49 |
Boolean = function(s) |
| 50 |
return s == 'True' |
| 51 |
or s == 'False' |
| 52 |
and s or alloc.err('Boolean value must be True or False') |
| 53 |
end, |
| 54 |
} |
| 55 |
|
| 56 |
-- 1 Namespace definitions |
| 57 |
|
| 58 |
local namespace_info = alloc.saved_table ('metadata namespace info') |
| 59 |
|
| 60 |
function M.add_key(prefix, name, t) |
| 61 |
namespace_info[prefix].keys[name] = { |
| 62 |
type = t.type or t[1], |
| 63 |
description = t.description or t[2], |
| 64 |
isInternal = t.isInternal or t[3] or false, |
| 65 |
inUse = false, |
| 66 |
} |
| 67 |
end |
| 68 |
|
| 69 |
function M.add_valuetype(prefix, name, t) |
| 70 |
local ns = namespace_info[prefix] |
| 71 |
ns.valuetypes = ns.valuetypes or { } |
| 72 |
ns.valuetypes[name] = { |
| 73 |
description = t.description or t[1], |
| 74 |
uri = t.uri or ns.uri, |
| 75 |
prefix = t.prefix or ns.prefix, |
| 76 |
inUse = false, |
| 77 |
} |
| 78 |
t.fields = t.fields or t[2] |
| 79 |
if t.fields then |
| 80 |
local fields = { } |
| 81 |
ns.valuetypes[name].fields = fields |
| 82 |
for n, f in pairs(t.fields) do |
| 83 |
fields[n] = { type = f.type or f[1], description = f.description or f[2] } |
| 84 |
end |
| 85 |
M.getters[name] = function(tt, k) |
| 86 |
if k then |
| 87 |
return (fields[k] or alloc.err('Unknown field ā%sā', k)) |
| 88 |
and tt and tt[k] or 'not found' |
| 89 |
else |
| 90 |
local res = { } |
| 91 |
for kk, v in sorted_pairs(tt) do |
| 92 |
table.insert(res, string.format('/{%s} {%s}', kk, v)) |
| 93 |
end |
| 94 |
return table.concat(res, ' ') |
| 95 |
end |
| 96 |
end |
| 97 |
M.setters[name] = function(value, orig, key, k) |
| 98 |
if not key then |
| 99 |
alloc.err('Missing key for metadata field %s', k) |
| 100 |
elseif not fields[key] then |
| 101 |
alloc.err('Unknown field ā%sā', key) |
| 102 |
else |
| 103 |
orig = orig or { } |
| 104 |
local setter = M.setters[fields[key].type] or M.setters.Text |
| 105 |
orig[key] = setter(value, orig[key], nil, k) |
| 106 |
end |
| 107 |
return orig |
| 108 |
end |
| 109 |
end |
| 110 |
end |
| 111 |
|
| 112 |
function M.add_namespace(description, prefix, uri, keys, valuetypes, predefined) |
| 113 |
namespace_info[prefix] = { |
| 114 |
uri = uri, |
| 115 |
description = description, |
| 116 |
keys = { }, |
| 117 |
predefined = predefined or false, |
| 118 |
inUse = false, |
| 119 |
} |
| 120 |
for name, t in pairs(keys) do |
| 121 |
M.add_key(prefix, name, t) |
| 122 |
end |
| 123 |
for name, t in pairs(valuetypes or { }) do |
| 124 |
M.add_valuetype(prefix, name, t) |
| 125 |
end |
| 126 |
end |
| 127 |
|
| 128 |
local function get_resource_type(ns, vtype) |
| 129 |
local vtypes = namespace_info[ns].valuetypes |
| 130 |
return vtypes and vtypes[vtype] and vtypes[vtype].fields |
| 131 |
and vtypes[vtype] or false |
| 132 |
end |
| 133 |
|
| 134 |
local function parse_type_name(vtype) |
| 135 |
local c = string.sub(vtype, 1, 4) |
| 136 |
local t = string.sub(vtype, 5) |
| 137 |
if c == 'Bag ' then |
| 138 |
return t, 'Bag' |
| 139 |
elseif c == 'Seq ' then |
| 140 |
return t, 'Seq' |
| 141 |
else |
| 142 |
return vtype, false |
| 143 |
end |
| 144 |
end |
| 145 |
|
| 146 |
-- 1 Built-in namespaces |
| 147 |
|
| 148 |
M.add_namespace('Adobe PDF', 'pdf', 'http://ns.adobe.com/pdf/1.3/', { |
| 149 |
Keywords = { 'Text', 'Keywords' }, |
| 150 |
PDFVersion = { 'Text', 'PDF version ', true}, |
| 151 |
Producer = { 'AgentName', 'Creator tool', true }, |
| 152 |
}, nil, true) |
| 153 |
|
| 154 |
M.add_namespace('PDF/A Identification', 'pdfaid', 'http://www.aiim.org/pdfa/ns/id/', { |
| 155 |
amd = { 'Text', 'Amendment identifier', true }, |
| 156 |
corr = { 'Text', 'Corrigendum identifier', true }, |
| 157 |
conformance = { 'Text', 'Conformance level', true }, |
| 158 |
part = { 'Integer', 'Version identifier', true }, |
| 159 |
}, nil, true) |
| 160 |
|
| 161 |
M.add_namespace('PDF/UA Identification', 'pdfuaid', 'http://www.aiim.org/pdfua/ns/id/', { |
| 162 |
amd = { 'Text', 'Amendment identifier', true }, |
| 163 |
corr = { 'Text', 'Corrigendum identifier', true }, |
| 164 |
part = { 'Integer', 'Version identifier', true }, |
| 165 |
}) |
| 166 |
|
| 167 |
M.add_namespace('Dublin Core', 'dc', 'http://purl.org/dc/elements/1.1/', { |
| 168 |
contributor = { 'Bag ProperName', 'Other contributors' }, |
| 169 |
coverage = { 'Text', 'Extent or scope' }, |
| 170 |
creator = { 'Seq ProperName', 'Authors' }, |
| 171 |
date = { 'Seq Date', 'Relevant dates' }, |
| 172 |
description = { 'Lang Alt', 'Description of contents' }, |
| 173 |
format = { 'MIMEType', 'File format (application/pdf)', true }, |
| 174 |
identifier = { 'Text', 'Unique identifier' }, |
| 175 |
language = { 'Bag Locale', 'Used languages', true }, |
| 176 |
publisher = { 'Bag ProperName', 'Publishers' }, |
| 177 |
relation = { 'Bag Text', 'Relationships to other documents', true }, |
| 178 |
rights = { 'Lang Alt', 'Copyright statement' }, |
| 179 |
source = { 'Text', 'Document source unique identifier' }, |
| 180 |
subject = { 'Bag Text', 'Keywords' }, |
| 181 |
title = { 'Lang Alt', 'Title' }, |
| 182 |
type = { 'Bag Text', 'Document type' }, |
| 183 |
}, nil, true) |
| 184 |
|
| 185 |
M.add_namespace('XMP Basic', 'xmp', 'http://ns.adobe.com/xap/1.0/', { |
| 186 |
BaseUrl = { 'URL', 'Base for relative URLs in the document', true }, |
| 187 |
CreateDate = { 'Date', 'Creation date' }, |
| 188 |
CreatorTool = { 'AgentName', 'Creator tool', true }, |
| 189 |
Identifier = { 'Bag Text', 'Multiple identifiers' }, |
| 190 |
MetadataDate = { 'Date', 'Metadata modification date', true }, |
| 191 |
ModifyDate = { 'Date', 'Document modification date', true }, |
| 192 |
Nickname = { 'Text', 'Informal name' }, |
| 193 |
}, nil, true) |
| 194 |
|
| 195 |
M.add_namespace('XMP Rights Management', 'xmpRights', 'http://ns.adobe.com/xap/1.0/rights/', { |
| 196 |
Certificate = { 'URL', 'Online rights management cerfificate' }, |
| 197 |
Marked = { 'Boolean', 'Indicates this is a rights-managed resource' }, |
| 198 |
Owner = { 'Bag ProperName', 'Right holders' }, |
| 199 |
UsageTerms = { 'Lang Alt', 'Instructions on usage' }, |
| 200 |
WebStatement = { 'URL', 'Location of rights statement' }, |
| 201 |
}, nil, true) |
| 202 |
|
| 203 |
M.add_namespace('XMP Media Managament', 'xmpMM', 'http://ns.adobe.com/xap/1.0/mm/', { |
| 204 |
DerivedFrom = { 'ResourceRef', 'Reference to original document', true }, |
| 205 |
DocumentID = { 'URI', 'UUID-based identifier across versions', true }, |
| 206 |
History = { 'Seq ResourceEvent', 'Human-readable history of changes', true }, |
| 207 |
ManagedFrom = { 'ResourceRef', 'Reference to unmanaged document', true }, |
| 208 |
Manager = { 'AgentName', 'Asset management system', true }, |
| 209 |
ManagerVariant = { 'Text', 'Particular variant', true }, |
| 210 |
ManageTo = { 'URI', 'Managed resource identifier', true }, |
| 211 |
ManageUI = { 'URI', 'Web browser interface', true }, |
| 212 |
RenditionClass = { 'RenditionClass', 'Rendition class name', true }, |
| 213 |
RenditionParams = { 'Text', 'Additional rendition parameters', true }, |
| 214 |
VersionID = { 'Text', 'Document version identifier', true }, |
| 215 |
Versions = { 'Seq Version', 'Version history', true }, |
| 216 |
}, { |
| 217 |
ResourceEvent = { 'Reference to a resource', { |
| 218 |
action = { 'Text', 'Action that occurred' }, |
| 219 |
instanceID = { 'URI', 'Instance ID of modifier resource' }, |
| 220 |
parameters = { 'Text', 'Additional description' }, |
| 221 |
softwareAgent = { 'AgentName', 'Software agent performing the action' }, |
| 222 |
when = { 'Date', 'Optional timestamp of the action' }, |
| 223 |
}, prefix = 'stEvt', uri = 'http://ns.adobe.com/xap/1.0/sType/ResourceEvent#' }, |
| 224 |
ResourceRef = { 'Reference to a resource', { |
| 225 |
instanceID = { 'URI', 'referenced xmpMM:instanceID' }, |
| 226 |
documentID = { 'URI', 'referenced xmpMM:documentID' }, |
| 227 |
versionID = { 'Text', 'referenced xmpMM:versionID' }, |
| 228 |
renditionClass = { 'RenditionClass', 'referenced xmpMM:renditionClass' }, |
| 229 |
renditionParams = { 'Text', 'referenced xmpMM:renditionParams' }, |
| 230 |
manager = { 'AgentName', 'referenced xmpMM:manager' }, |
| 231 |
managerVariant = { 'Text', 'referenced xmpMM:managerVariant' }, |
| 232 |
manageTo = { 'URI', 'referenced xmpMM:manageTo' }, |
| 233 |
manageUI = { 'URI', 'referenced xmpMM:manageUI' }, |
| 234 |
}, prefix = 'stRef', uri = 'http://ns.adobe.com/xap/1.0/sType/ResourceRef#' }, |
| 235 |
Version = { 'Document version', { |
| 236 |
comments = { 'Text', 'Description of changes' }, |
| 237 |
event = { 'Text', 'Description of changes' }, |
| 238 |
modifyDate = { 'Date', 'Check-in date' }, |
| 239 |
modifier = { 'ProperName', 'Person making the modifications' }, |
| 240 |
version = { 'Text', 'New version number' }, |
| 241 |
}, prefix = 'stVer', uri = 'http://ns.adobe.com/xap/1.0/sType/Version#' }, |
| 242 |
}, true) |
| 243 |
|
| 244 |
-- 1 Writing metadata packets |
| 245 |
|
| 246 |
local XMP = alloc.saved_table('metadata values') |
| 247 |
XMP[1] = XMP[1] or { dc = { format = 'application/pdf' } } -- document-level metadata |
| 248 |
|
| 249 |
function M.push_metadata() |
| 250 |
XMP[#XMP+1] = { } |
| 251 |
end |
| 252 |
|
| 253 |
local function get_xmp() |
| 254 |
return XMP[#XMP] |
| 255 |
end |
| 256 |
|
| 257 |
local function escape_xml_content(s) |
| 258 |
-- NB not to be used for attribute values |
| 259 |
s = string.gsub(s, '&', '&') |
| 260 |
return string.gsub(s, '<', '<') |
| 261 |
end |
| 262 |
|
| 263 |
-- 2 extension schemas |
| 264 |
|
| 265 |
local function get_metadata_extensions() |
| 266 |
local namespaces, anyInUse = { }, false |
| 267 |
for prefix, ns in pairs(namespace_info) do |
| 268 |
if not ns.predefined and ns.inUse then |
| 269 |
namespaces[prefix] = ns |
| 270 |
anyInUse = true |
| 271 |
end |
| 272 |
end |
| 273 |
if not anyInUse then return false end |
| 274 |
local rv = { [[ <rdf:Description rdf:about="" |
| 275 |
xmlns:pdfaExtension="http://www.aiim.org/pdfa/ns/extension/" |
| 276 |
xmlns:pdfaSchema="http://www.aiim.org/pdfa/ns/schema#" |
| 277 |
xmlns:pdfaProperty="http://www.aiim.org/pdfa/ns/property#" |
| 278 |
xmlns:pdfaType="http://www.aiim.org/pdfa/ns/type#" |
| 279 |
xmlns:pdfaField="http://www.aiim.org/pdfa/ns/field#" > |
| 280 |
<pdfaExtension:schemas> |
| 281 |
<rdf:Bag>]] } |
| 282 |
local add = function (...) table.insert(rv, string.format(...)) end |
| 283 |
for prefix, ns in sorted_pairs(namespaces) do |
| 284 |
add(' <rdf:li rdf:parseType="Resource">') |
| 285 |
add(' <pdfaSchema:schema>%s</pdfaSchema:schema>', ns.description) |
| 286 |
add(' <pdfaSchema:namespaceURI>%s</pdfaSchema:namespaceURI>', ns.uri) |
| 287 |
add(' <pdfaSchema:prefix>%s</pdfaSchema:prefix>', prefix) |
| 288 |
table.insert(rv, ' <pdfaSchema:property>\n <rdf:Seq>') |
| 289 |
for name, key in sorted_pairs(ns.keys) do if key.inUse then |
| 290 |
add(' <rdf:li rdf:parseType="Resource">') |
| 291 |
add(' <pdfaProperty:name>%s</pdfaProperty:name>', name) |
| 292 |
add(' <pdfaProperty:valueType>%s</pdfaProperty:valueType>', key.type) |
| 293 |
add(' <pdfaProperty:category>%s</pdfaProperty:category>', key.isInternal and 'internal' or 'external') |
| 294 |
add(' <pdfaProperty:description>%s</pdfaProperty:description>', key.description) |
| 295 |
add(' </rdf:li>') |
| 296 |
end end |
| 297 |
add(' </rdf:Seq>\n </pdfaSchema:property>') |
| 298 |
if ns.valuetypes then |
| 299 |
add(' <pdfaSchema:valueType>\n <rdf:Seq>') |
| 300 |
for name, vtype in sorted_pairs(ns.valuetypes) do |
| 301 |
if vtype.inUse then |
| 302 |
add(' <rdf:li rdf:parseType="Resource">') |
| 303 |
add(' <pdfaType:type>%s</pdfaType:type>', name) |
| 304 |
if vtype.fields then |
| 305 |
add(' <pdfaType:namespaceURI>%s</pdfaType:namespaceURI>', vtype.uri) |
| 306 |
add(' <pdfaType:prefix>%s</pdfaType:prefix>', vtype.prefix) |
| 307 |
end |
| 308 |
add(' <pdfaType:description>%s</pdfaType:description>', vtype.description) |
| 309 |
if vtype.fields then |
| 310 |
add(' <pdfaType:field>\n <rdf:Seq>') |
| 311 |
for fname, field in sorted_pairs(vtype.fields) do |
| 312 |
add(' <rdf:li rdf:parseType="Resource">') |
| 313 |
add(' <pdfaField:name>%s</pdfaField:name>', fname) |
| 314 |
add(' <pdfaField:valueType>%s</pdfaField:valueType>', field.type) |
| 315 |
add(' <pdfaField:description>%s</pdfaField:description>', field.description) |
| 316 |
add(' </rdf:li>') |
| 317 |
end |
| 318 |
add(' </rdf:Seq>\n </pdfaType:field>') |
| 319 |
end |
| 320 |
add(' </rdf:li>') |
| 321 |
end |
| 322 |
end |
| 323 |
add(' </rdf:Seq>\n </pdfaSchema:valueType>') |
| 324 |
end |
| 325 |
add(' </rdf:li>') |
| 326 |
end |
| 327 |
add(' </rdf:Bag>\n </pdfaExtension:schemas>\n </rdf:Description>') |
| 328 |
return table.concat(rv, '\n') |
| 329 |
end |
| 330 |
|
| 331 |
-- 2 main xmp packet |
| 332 |
|
| 333 |
local function write_fields(add, res, value, pre, name, indent) |
| 334 |
indent = indent or '' |
| 335 |
if res then |
| 336 |
add('%s <%s:%s rdf:parseType="Resource">', indent, pre, name) |
| 337 |
for k, _ in sorted_pairs(res.fields) do |
| 338 |
if value[k] then |
| 339 |
add('%s <%s:%s>%s</%s:%s>', indent, res.prefix, k, value[k], res.prefix, k) |
| 340 |
end |
| 341 |
end |
| 342 |
add('%s </%s:%s>', indent, pre, name) |
| 343 |
else |
| 344 |
add('%s <%s:%s>%s</%s:%s>', indent, pre, name, escape_xml_content(value), pre, name) |
| 345 |
end |
| 346 |
end |
| 347 |
|
| 348 |
local function make_xmp_packet(xmp) |
| 349 |
local rv = { } ; local add = function(...) table.insert(rv, string.format(...)) end |
| 350 |
-- wrapper and opening tags |
| 351 |
add('<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>') |
| 352 |
add('<xmp:xmpmeta xmlns:xmp="adobe:ns:meta/">') |
| 353 |
add('<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">') |
| 354 |
-- namespaces |
| 355 |
for id, keys in sorted_pairs(xmp) do |
| 356 |
local info = namespace_info[id] |
| 357 |
-- xml namespaces in enclosing description element |
| 358 |
add(' <rdf:Description rdf:about="" xmlns:%s="%s"', id, info.uri) |
| 359 |
local xmlns_uris = { } |
| 360 |
for _, vt in pairs(info.valuetypes or {}) do |
| 361 |
if vt.fields and vt.inUse |
| 362 |
and (vt.prefix ~= id or vt.uri ~= info.uri) then |
| 363 |
xmlns_uris[vt.prefix] = vt.uri |
| 364 |
end |
| 365 |
end |
| 366 |
for pre, uri in pairs(xmlns_uris) do |
| 367 |
add(' xmlns:%s="%s"', pre, uri) |
| 368 |
end |
| 369 |
rv[#rv] = rv[#rv] .. '>' |
| 370 |
-- metadata namespace contents |
| 371 |
for key, value in sorted_pairs(keys) do |
| 372 |
local vtype, lst = parse_type_name(info.keys[key].type) |
| 373 |
local res = get_resource_type(id, vtype) |
| 374 |
-- list values |
| 375 |
if lst then |
| 376 |
add(' <%s:%s>\n <rdf:%s>', id, key, lst) |
| 377 |
for _, v in ipairs(value) do |
| 378 |
write_fields(add, res, v, 'rdf', 'li', ' ') |
| 379 |
end |
| 380 |
add(' </rdf:%s>\n </%s:%s>', lst, id, key) |
| 381 |
-- language alternatives |
| 382 |
elseif vtype == 'Lang Alt' then |
| 383 |
add(' <%s:%s>\n <rdf:Alt>', id, key) |
| 384 |
for lang, v in sorted_pairs(value) do |
| 385 |
add(' <rdf:li xml:lang="%s">%s</rdf:li>', lang, escape_xml_content(v)) |
| 386 |
end |
| 387 |
add(' </rdf:Alt>\n </%s:%s>', id, key) |
| 388 |
-- simple values |
| 389 |
else |
| 390 |
if lst then value = value[1] end |
| 391 |
write_fields(add, res, value, id, key) |
| 392 |
end |
| 393 |
end |
| 394 |
add(' </rdf:Description>') |
| 395 |
end |
| 396 |
-- extension schemas |
| 397 |
if #XMP == 1 and xmp.pdfaid then |
| 398 |
local schemas = get_metadata_extensions() |
| 399 |
if schemas then add(schemas) end |
| 400 |
end |
| 401 |
-- closing tags and wrapper |
| 402 |
local editable = tex.count['metadatamodification'] > 0 |
| 403 |
add('</rdf:RDF>\n</xmp:xmpmeta>') |
| 404 |
if editable then add(string.rep(string.rep(' ', 80), 25, '\n')) end |
| 405 |
add('<?xpacket end="%s"?>', editable and 'w' or 'r') |
| 406 |
return table.concat(rv, '\n') |
| 407 |
end |
| 408 |
|
| 409 |
-- 2 |
| 410 |
|
| 411 |
function M.write_metadata() |
| 412 |
local xmp = make_xmp_packet(XMP[#XMP]) |
| 413 |
local nr = pdf.obj { |
| 414 |
type = 'stream', |
| 415 |
attr = '/Type/Metadata /Subtype/XML', |
| 416 |
immediate = true, |
| 417 |
compresslevel = 0, |
| 418 |
string = xmp, |
| 419 |
} |
| 420 |
XMP[#XMP] = nil |
| 421 |
return nr |
| 422 |
end |
| 423 |
|
| 424 |
-- 1 Getting and setting metadata values |
| 425 |
|
| 426 |
local function get_metadata_info(fullkey) |
| 427 |
local id, k = string.match(fullkey, '([^:]+):(.+)') |
| 428 |
if not id then return alloc.err('Malformed metadata key %s', fullkey) end |
| 429 |
local ns = namespace_info[id] |
| 430 |
if not ns then return alloc.err('Unknown namespace %s', id) end |
| 431 |
local key = ns.keys[k] |
| 432 |
if not key then return alloc.err('Unknown key %s in namespace %s', k, id) end |
| 433 |
local vtype, lst = parse_type_name(key.type) |
| 434 |
ns.inUse, key.inUse = true, true |
| 435 |
if ns.valuetypes and ns.valuetypes[vtype] then ns.valuetypes[vtype].inUse = true end |
| 436 |
return id, k, lst, vtype |
| 437 |
end |
| 438 |
|
| 439 |
local function get_separator() |
| 440 |
return utf8.char(tex.count['metadataseparator']) |
| 441 |
end |
| 442 |
|
| 443 |
function M.get_metadata(k, a) |
| 444 |
local xmp = get_xmp() |
| 445 |
local ns, key, lst, vtype = get_metadata_info(k) |
| 446 |
local getter = M.getters[vtype] or M.getters.Text |
| 447 |
if not ns or not xmp[ns] then |
| 448 |
return '' |
| 449 |
elseif lst then |
| 450 |
local rv = { } |
| 451 |
for _, v in ipairs(xmp[ns][key]) do |
| 452 |
table.insert(rv, getter(v, a)) |
| 453 |
end |
| 454 |
return table.concat(rv, get_separator()) |
| 455 |
else |
| 456 |
return getter(xmp[ns][key], a) |
| 457 |
end |
| 458 |
end |
| 459 |
|
| 460 |
local function split_metadata_input(s) |
| 461 |
local rv = s:explode(get_separator()) |
| 462 |
for i = 1, #rv do |
| 463 |
rv[i] = string.gsub(rv[i], '^ *', '') |
| 464 |
rv[i] = string.gsub(rv[i], ' *$', '') |
| 465 |
end |
| 466 |
return rv |
| 467 |
end |
| 468 |
|
| 469 |
function M.set_metadata(k, v, o) |
| 470 |
local ns, key, lst, vtype = get_metadata_info(k) |
| 471 |
if not ns then return end -- an error has already occurred |
| 472 |
local xmp = get_xmp() |
| 473 |
xmp[ns] = xmp[ns] or { } |
| 474 |
local setter = M.setters[vtype] or M.setters.Text |
| 475 |
if not v or v == '' then |
| 476 |
xmp[ns][key] = nil |
| 477 |
elseif lst then |
| 478 |
local l = xmp[ns][key] or { } |
| 479 |
xmp[ns][key] = l |
| 480 |
local nst = namespace_info[ns].valuetypes |
| 481 |
local f = nst and nst[vtype] and nst[vtype].fields |
| 482 |
if o and #l>0 and f and f[o] and not l[#l][o] then |
| 483 |
-- special case: add to property type |
| 484 |
l[#l] = setter(v, l[#l], o, l) |
| 485 |
else |
| 486 |
for _, val in ipairs(split_metadata_input(v)) do |
| 487 |
table.insert(l, setter(val, nil, o, k)) |
| 488 |
end |
| 489 |
end |
| 490 |
else |
| 491 |
xmp[ns][key] = setter(v, xmp[ns][key], o, k) |
| 492 |
end |
| 493 |
end |
| 494 |
|
| 495 |
-- 1 Callbacks and tex interface |
| 496 |
|
| 497 |
M.aliases = { |
| 498 |
author = { 'dc:creator' }, |
| 499 |
title = { 'dc:title' }, |
| 500 |
date = { 'dc:date', 'xmp:CreateDate', 'xmp:ModifyDate' }, |
| 501 |
language = { 'dc:language' }, |
| 502 |
keywords = { 'dc:subject', |
| 503 |
also = function(values) |
| 504 |
local kw = M.get_metadata('pdf:Keywords') |
| 505 |
if kw ~= '' then kw = kw..', ' end |
| 506 |
M.set_metadata('pdf:Keywords') -- prevent warning |
| 507 |
M.set_metadata('pdf:Keywords', kw..table.concat(split_metadata_input(values), ', ')) |
| 508 |
end }, |
| 509 |
publisher = { 'dc:publisher' }, |
| 510 |
abstract = { 'dc:description' }, |
| 511 |
copyright = { 'dc:rights' }, |
| 512 |
identifier = { 'dc:identifier' }, |
| 513 |
version = { 'xmpMM:VersionID' }, |
| 514 |
} |
| 515 |
|
| 516 |
cb.register('finish_pdffile', function() |
| 517 |
if tex.count['writedocumentmetadata'] == 0 then return end |
| 518 |
if #XMP > 1 then |
| 519 |
alloc.err('Not all metadata has been written out.') |
| 520 |
XMP = { [1] = XMP[1] } |
| 521 |
end |
| 522 |
if #XMP > 0 then |
| 523 |
local metadata_obj = M.write_metadata() |
| 524 |
local catalog = pdf.getcatalog() or '' |
| 525 |
pdf.setcatalog(catalog..string.format('/Metadata %s 0 R', metadata_obj)) |
| 526 |
end |
| 527 |
end) |
| 528 |
|
| 529 |
alloc.luadef('getmetadata', function() |
| 530 |
local option = token.scan_keyword('/') and token.scan_string() |
| 531 |
local keyname = token.scan_word() |
| 532 |
local alias = M.aliases[keyname] |
| 533 |
local key = alias and alias[1] or keyname |
| 534 |
tex.sprint(M.get_metadata(key, option)) |
| 535 |
end) |
| 536 |
|
| 537 |
local function set_aliased_metadata(keyname, value, option) |
| 538 |
local keys = M.aliases[keyname] or { keyname } |
| 539 |
for _, key in ipairs(keys) do |
| 540 |
M.set_metadata(key, value, option) |
| 541 |
end |
| 542 |
if keys.also then keys.also(value, option) end |
| 543 |
end |
| 544 |
|
| 545 |
alloc.luadef('setmetadata', function() |
| 546 |
local option = token.scan_keyword('/') and token.scan_string() |
| 547 |
local keyname = token.scan_word() |
| 548 |
local value = token.scan_string() |
| 549 |
set_aliased_metadata(keyname, value, option) |
| 550 |
end, 'protected') |
| 551 |
|
| 552 |
alloc.luadef('startmetadata', function() |
| 553 |
local key = token.scan_word() |
| 554 |
local opt = token.scan_keyword('/') and token.scan_string() |
| 555 |
repeat |
| 556 |
repeat |
| 557 |
set_aliased_metadata(key, token.scan_string(), opt) |
| 558 |
opt = token.scan_keyword('/') and token.scan_string() |
| 559 |
until not opt |
| 560 |
key = token.scan_word() |
| 561 |
opt = token.scan_keyword('/') and token.scan_string() |
| 562 |
until key == 'stopmetadata' |
| 563 |
end) |
| 564 |
|
| 565 |
alloc.luadef('newmetadata', function() |
| 566 |
M.push_metadata() |
| 567 |
end, 'protected') |
| 568 |
|
| 569 |
alloc.luadef('writemetadata', function() |
| 570 |
tex.sprint(M.write_metadata()) |
| 571 |
end) |
| 572 |
|
| 573 |
alloc.luadef('includemetadata', function() |
| 574 |
local file = kpse.find_file(token.scan_string()) |
| 575 |
if not file then |
| 576 |
alloc.err('Metadata file not found'); |
| 577 |
tex.sprint(0); |
| 578 |
else |
| 579 |
tex.sprint( pdf.obj { |
| 580 |
type = 'stream', |
| 581 |
attr = '/Type/Metadata /Subtype/XML', |
| 582 |
immediate = true, |
| 583 |
compresslevel = 0, |
| 584 |
file = file, |
| 585 |
}) |
| 586 |
end |
| 587 |
end) |
| 588 |
|
| 589 |
-- |
| 590 |
|
| 591 |
return M |
| 592 |
|