pdf.js/src/display/metadata.js

95 lines
2.9 KiB
JavaScript
Raw Normal View History

2012-09-01 07:48:21 +09:00
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2012-03-29 03:09:03 +09:00
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);
2012-04-24 06:43:20 +09:00
});
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) ? String.fromCharCode(code) :
'&#x' + (0x10000 + code).toString(16).substring(1) + ';';
2012-03-25 03:59:51 +09:00
}
return '>' + chars;
});
}
2012-03-25 03:59:51 +09:00
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)) {
2017-06-29 05:51:31 +09:00
throw new Error('Metadata: Invalid metadata object');
2012-03-25 03:59:51 +09:00
}
this.metaDocument = meta;
this.metadata = Object.create(null);
this.parse();
}
2012-03-29 02:15:59 +09:00
Metadata.prototype = {
parse: function Metadata_parse() {
var doc = this.metaDocument;
var rdf = doc.documentElement;
2012-03-28 05:37:02 +09:00
if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') { // Wrapped in <xmpmeta>
rdf = rdf.firstChild;
while (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
rdf = rdf.nextSibling;
}
}
2012-03-25 03:59:51 +09:00
var nodeName = (rdf) ? rdf.nodeName.toLowerCase() : null;
if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) {
return;
}
2012-03-25 03:59:51 +09:00
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 (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();
2012-03-25 03:59:51 +09:00
}
}
}
},
2012-03-25 03:59:51 +09:00
get: function Metadata_get(name) {
return this.metadata[name] || null;
},
2012-03-25 03:59:51 +09:00
has: function Metadata_has(name) {
return typeof this.metadata[name] !== 'undefined';
Fix inconsistent spacing and trailing commas in objects in remaining `src/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on http://eslint.org/docs/rules/comma-dangle http://eslint.org/docs/rules/object-curly-spacing Given that we currently have quite inconsistent object formatting, fixing this in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead. Please note: This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch. ```diff diff --git a/src/display/canvas.js b/src/display/canvas.js index 5739f6f2..4216b2d2 100644 --- a/src/display/canvas.js +++ b/src/display/canvas.js @@ -2071,7 +2071,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { var map = []; for (var i = 0, ii = positions.length; i < ii; i += 2) { map.push({ transform: [scaleX, 0, 0, scaleY, positions[i], - positions[i + 1]], x: 0, y: 0, w: width, h: height, }); + positions[i + 1]], x: 0, y: 0, w: width, h: height, }); } this.paintInlineImageXObjectGroup(imgData, map); }, diff --git a/src/display/svg.js b/src/display/svg.js index 9eb05dfa..2aa21482 100644 --- a/src/display/svg.js +++ b/src/display/svg.js @@ -458,7 +458,11 @@ SVGGraphics = (function SVGGraphicsClosure() { for (var x = 0; x < fnArrayLen; x++) { var fnId = fnArray[x]; - opList.push({ 'fnId': fnId, 'fn': REVOPS[fnId], 'args': argsArray[x], }); + opList.push({ + 'fnId': fnId, + 'fn': REVOPS[fnId], + 'args': argsArray[x], + }); } return opListToTree(opList); }, ```
2017-06-02 18:26:37 +09:00
},
};
2012-03-25 03:59:51 +09:00
export {
Metadata,
};