Skip to content

Commit

Permalink
merge PR#317 from isimisi:dev
Browse files Browse the repository at this point in the history
  • Loading branch information
modesty committed Oct 21, 2023
2 parents e23cb1a + cb66f23 commit c53b3ee
Show file tree
Hide file tree
Showing 9 changed files with 4,480 additions and 1,573 deletions.
142 changes: 78 additions & 64 deletions base/display/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,80 +18,94 @@

'use strict';

var Metadata = PDFJS.Metadata = (function MetadataClosure() {
function fixMetadata(meta) {
return meta.replace(/>\\376\\377([^<]+)/g, function(all, codes) {
var bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g,
function(code, d1, d2, d3) {
return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
var Metadata = (PDFJS.Metadata = (function MetadataClosure() {
function fixMetadata(meta) {
return meta.replace(/>\\376\\377([^<]+)/g, function (all, codes) {
var bytes = codes.replace(
/\\([0-3])([0-7])([0-7])/g,
function (code, d1, d2, d3) {
return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
}
);
var chars = '';
for (var i = 0; i < bytes.length; i += 2) {
var code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
chars +=
code >= 32 &&
code < 127 &&
code != 60 &&
code != 62 &&
code != 38 &&
false
? String.fromCharCode(code)
: '&#x' + (0x10000 + code).toString(16).substring(1) + ';';
}
return '>' + chars;
});
var chars = '';
for (var i = 0; i < bytes.length; i += 2) {
var code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
chars += code >= 32 && code < 127 && code != 60 && code != 62 &&
code != 38 && false ? String.fromCharCode(code) :
'&#x' + (0x10000 + code).toString(16).substring(1) + ';';
}
return '>' + chars;
});
}
}

function Metadata(meta) {
if (typeof meta === 'string') {
// Ghostscript produces invalid metadata
meta = fixMetadata(meta);
function Metadata(meta) {
if (typeof meta === 'string') {
// Ghostscript produces invalid metadata
meta = fixMetadata(meta);

var parser = new DOMParser();
meta = parser.parseFromString(meta, 'application/xml');
} else if (!(meta instanceof Document)) {
error('Metadata: Invalid metadata object');
}
var parser = new DOMParser();
meta = parser.parseFromString(meta, 'application/xml');
} else if (!(meta instanceof Document)) {
error('Metadata: Invalid metadata object');
}

this.metaDocument = meta;
this.metadata = {};
this.parse();
}
this.metaDocument = meta;
this.metadata = {};
this.parse();
}

Metadata.prototype = {
parse: function Metadata_parse() {
var doc = this.metaDocument;
var rdf = doc.documentElement;
Metadata.prototype = {
parse: function Metadata_parse() {
var doc = this.metaDocument;
var rdf = doc.documentElement;

if (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf') { // Wrapped in <xmpmeta>
rdf = rdf.firstChild;
while (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf')
rdf = rdf.nextSibling;
}
if (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
// Wrapped in <xmpmeta>
rdf = rdf.firstChild;
while (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf')
rdf = rdf.nextSibling;
}

var nodeName = (rdf) ? rdf.nodeName.toLowerCase() : null;
if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes())
return;
var nodeName = rdf ? rdf.nodeName.toLowerCase() : null;
if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) return;

var children = rdf.childNodes, desc, entry, name, i, ii, length, iLength;
var children = rdf.childNodes,
desc,
entry,
name,
i,
ii,
length,
iLength;

for (i = 0, length = children.length; i < length; i++) {
desc = children[i];
if (desc.nodeName.toLowerCase() !== 'rdf:description')
continue;
for (i = 0, length = children.length; i < length; i++) {
desc = children[i];
if (desc.nodeName.toLowerCase() !== 'rdf:description') continue;

for (ii = 0, iLength = desc.childNodes.length; ii < iLength; ii++) {
if (desc.childNodes[ii].nodeName.toLowerCase() !== '#text') {
entry = desc.childNodes[ii];
name = entry.nodeName.toLowerCase();
this.metadata[name] = entry.textContent.trim();
}
}
}
},
for (ii = 0, iLength = desc.childNodes.length; ii < iLength; ii++) {
if (desc.childNodes[ii].nodeName.toLowerCase() !== '#text') {
entry = desc.childNodes[ii];
name = entry.nodeName.toLowerCase();
this.metadata[name] = entry.textContent.trim();
}
}
}
},

get: function Metadata_get(name) {
return this.metadata[name] || null;
},
get: function Metadata_get(name) {
return this.metadata[name] || null;
},

has: function Metadata_has(name) {
return typeof this.metadata[name] !== 'undefined';
}
};
has: function Metadata_has(name) {
return typeof this.metadata[name] !== 'undefined';
},
};

return Metadata;
})();
return Metadata;
})());
Loading

0 comments on commit c53b3ee

Please sign in to comment.