mirror of
https://github.com/pierre42100/comunic
synced 2025-10-19 20:34:48 +00:00
First commit
This commit is contained in:
107
3rdparty/pdf.js/examples/node/domparsermock.js
vendored
Executable file
107
3rdparty/pdf.js/examples/node/domparsermock.js
vendored
Executable file
@@ -0,0 +1,107 @@
|
||||
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
||||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
// Dummy XML Parser
|
||||
|
||||
function DOMNodeMock(nodeName, nodeValue) {
|
||||
this.nodeName = nodeName;
|
||||
this.nodeValue = nodeValue;
|
||||
Object.defineProperty(this, 'parentNode', {value: null, writable: true});
|
||||
}
|
||||
DOMNodeMock.prototype = {
|
||||
get firstChild() {
|
||||
return this.childNodes[0];
|
||||
},
|
||||
get nextSibling() {
|
||||
var index = this.parentNode.childNodes.indexOf(this);
|
||||
return this.parentNode.childNodes[index + 1];
|
||||
},
|
||||
get textContent() {
|
||||
if (!this.childNodes) {
|
||||
return this.nodeValue || '';
|
||||
}
|
||||
return this.childNodes.map(function (child) {
|
||||
return child.textContent;
|
||||
}).join('');
|
||||
},
|
||||
hasChildNodes: function () {
|
||||
return this.childNodes && this.childNodes.length > 0;
|
||||
}
|
||||
};
|
||||
|
||||
function decodeXML(text) {
|
||||
if (text.indexOf('&') < 0) {
|
||||
return text;
|
||||
}
|
||||
return text.replace(/&(#(x[0-9a-f]+|\d+)|\w+);/gi, function (all, entityName, number) {
|
||||
if (number) {
|
||||
return String.fromCharCode(number[0] === 'x' ? parseInt(number.substring(1), 16) : +number);
|
||||
}
|
||||
switch (entityName) {
|
||||
case 'amp':
|
||||
return '&';
|
||||
case 'lt':
|
||||
return '<';
|
||||
case 'gt':
|
||||
return '>';
|
||||
case 'quot':
|
||||
return '\"';
|
||||
case 'apos':
|
||||
return '\'';
|
||||
}
|
||||
return '&' + entityName + ';';
|
||||
});
|
||||
}
|
||||
|
||||
function DOMParserMock() {};
|
||||
DOMParserMock.prototype = {
|
||||
parseFromString: function (content) {
|
||||
content = content.replace(/<\?[\s\S]*?\?>|<!--[\s\S]*?-->/g, '').trim();
|
||||
var nodes = [];
|
||||
content = content.replace(/>([\s\S]+?)</g, function (all, text) {
|
||||
var i = nodes.length;
|
||||
var node = new DOMNodeMock('#text', decodeXML(text));
|
||||
nodes.push(node);
|
||||
if (node.textContent.trim().length === 0) {
|
||||
return '><'; // ignoring whitespaces
|
||||
}
|
||||
return '>' + i + ',<';
|
||||
});
|
||||
content = content.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, function (all, text) {
|
||||
var i = nodes.length;
|
||||
var node = new DOMNodeMock('#text', text);
|
||||
nodes.push(node);
|
||||
return i + ',';
|
||||
});
|
||||
var lastLength;
|
||||
do {
|
||||
lastLength = nodes.length;
|
||||
content = content.replace(/<([\w\:]+)((?:[\s\w:=]|'[^']*'|"[^"]*")*)(?:\/>|>([\d,]*)<\/[^>]+>)/g,
|
||||
function (all, name, attrs, content) {
|
||||
var i = nodes.length;
|
||||
var node = new DOMNodeMock(name);
|
||||
var children = [];
|
||||
if (content) {
|
||||
content = content.split(',');
|
||||
content.pop();
|
||||
content.forEach(function (child) {
|
||||
var childNode = nodes[+child];
|
||||
childNode.parentNode = node;
|
||||
children.push(childNode);
|
||||
})
|
||||
}
|
||||
node.childNodes = children;
|
||||
nodes.push(node);
|
||||
return i + ',';
|
||||
|
||||
});
|
||||
} while(lastLength < nodes.length);
|
||||
return {
|
||||
documentElement: nodes.pop()
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
exports.DOMParserMock = DOMParserMock;
|
140
3rdparty/pdf.js/examples/node/domstubs.js
vendored
Executable file
140
3rdparty/pdf.js/examples/node/domstubs.js
vendored
Executable file
@@ -0,0 +1,140 @@
|
||||
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
||||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
var sheet = {
|
||||
cssRules: [],
|
||||
insertRule: function(rule) {
|
||||
this.cssRules.push(rule);
|
||||
},
|
||||
};
|
||||
|
||||
var style = {
|
||||
sheet: sheet,
|
||||
};
|
||||
|
||||
function xmlEncode(s){
|
||||
var i = 0, ch;
|
||||
s = String(s);
|
||||
while (i < s.length && (ch = s[i]) !== '&' && ch !== '<' &&
|
||||
ch !== '\"' && ch !== '\n' && ch !== '\r' && ch !== '\t') {
|
||||
i++;
|
||||
}
|
||||
if (i >= s.length) {
|
||||
return s;
|
||||
}
|
||||
var buf = s.substring(0, i);
|
||||
while (i < s.length) {
|
||||
ch = s[i++];
|
||||
switch (ch) {
|
||||
case '&':
|
||||
buf += '&';
|
||||
break;
|
||||
case '<':
|
||||
buf += '<';
|
||||
break;
|
||||
case '\"':
|
||||
buf += '"';
|
||||
break;
|
||||
case '\n':
|
||||
buf += '
';
|
||||
break;
|
||||
case '\r':
|
||||
buf += '
';
|
||||
break;
|
||||
case '\t':
|
||||
buf += '	';
|
||||
break;
|
||||
default:
|
||||
buf += ch;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
global.btoa = function btoa(chars) {
|
||||
var digits =
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
||||
var buffer = '';
|
||||
var i, n;
|
||||
for (i = 0, n = chars.length; i < n; i += 3) {
|
||||
var b1 = chars.charCodeAt(i) & 0xFF;
|
||||
var b2 = chars.charCodeAt(i + 1) & 0xFF;
|
||||
var b3 = chars.charCodeAt(i + 2) & 0xFF;
|
||||
var d1 = b1 >> 2, d2 = ((b1 & 3) << 4) | (b2 >> 4);
|
||||
var d3 = i + 1 < n ? ((b2 & 0xF) << 2) | (b3 >> 6) : 64;
|
||||
var d4 = i + 2 < n ? (b3 & 0x3F) : 64;
|
||||
buffer += (digits.charAt(d1) + digits.charAt(d2) +
|
||||
digits.charAt(d3) + digits.charAt(d4));
|
||||
}
|
||||
return buffer;
|
||||
};
|
||||
|
||||
function DOMElement(name) {
|
||||
this.nodeName = name;
|
||||
this.childNodes = [];
|
||||
this.attributes = {};
|
||||
this.textContent = '';
|
||||
}
|
||||
|
||||
DOMElement.prototype = {
|
||||
|
||||
setAttributeNS: function DOMElement_setAttributeNS(NS, name, value) {
|
||||
value = value || '';
|
||||
value = xmlEncode(value);
|
||||
this.attributes[name] = value;
|
||||
},
|
||||
|
||||
appendChild: function DOMElement_appendChild(element) {
|
||||
var childNodes = this.childNodes;
|
||||
if (childNodes.indexOf(element) === -1) {
|
||||
childNodes.push(element);
|
||||
}
|
||||
},
|
||||
|
||||
toString: function DOMElement_toString() {
|
||||
var attrList = [];
|
||||
for (i in this.attributes) {
|
||||
attrList.push(i + '="' + xmlEncode(this.attributes[i]) + '"');
|
||||
}
|
||||
|
||||
if (this.nodeName === 'svg:tspan' || this.nodeName === 'svg:style') {
|
||||
var encText = xmlEncode(this.textContent);
|
||||
return '<' + this.nodeName + ' ' + attrList.join(' ') + '>' +
|
||||
encText + '</' + this.nodeName + '>';
|
||||
} else if (this.nodeName === 'svg:svg') {
|
||||
var ns = 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
|
||||
'xmlns:svg="http://www.w3.org/2000/svg"'
|
||||
return '<' + this.nodeName + ' ' + ns + ' ' + attrList.join(' ') + '>' +
|
||||
this.childNodes.join('') + '</' + this.nodeName + '>';
|
||||
} else {
|
||||
return '<' + this.nodeName + ' ' + attrList.join(' ') + '>' +
|
||||
this.childNodes.join('') + '</' + this.nodeName + '>';
|
||||
}
|
||||
},
|
||||
|
||||
cloneNode: function DOMElement_cloneNode() {
|
||||
var newNode = new DOMElement(this.nodeName);
|
||||
newNode.childNodes = this.childNodes;
|
||||
newNode.attributes = this.attributes;
|
||||
newNode.textContent = this.textContent;
|
||||
return newNode;
|
||||
},
|
||||
}
|
||||
|
||||
global.document = {
|
||||
childNodes : [],
|
||||
|
||||
getElementById: function (id) {
|
||||
if (id === 'PDFJS_FONT_STYLE_TAG') {
|
||||
return style;
|
||||
}
|
||||
},
|
||||
|
||||
createElementNS: function (NS, element) {
|
||||
var elObject = new DOMElement(element);
|
||||
return elObject;
|
||||
},
|
||||
};
|
76
3rdparty/pdf.js/examples/node/getinfo.js
vendored
Executable file
76
3rdparty/pdf.js/examples/node/getinfo.js
vendored
Executable file
@@ -0,0 +1,76 @@
|
||||
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
||||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
//
|
||||
// Basic node example that prints document metadata and text content.
|
||||
// Requires single file built version of PDF.js -- please run
|
||||
// `node make singlefile` before running the example.
|
||||
//
|
||||
|
||||
var fs = require('fs');
|
||||
|
||||
// HACK few hacks to let PDF.js be loaded not as a module in global space.
|
||||
global.window = global;
|
||||
global.navigator = { userAgent: "node" };
|
||||
global.PDFJS = {};
|
||||
global.DOMParser = require('./domparsermock.js').DOMParserMock;
|
||||
|
||||
require('../../build/singlefile/build/pdf.combined.js');
|
||||
|
||||
// Loading file from file system into typed array
|
||||
var pdfPath = process.argv[2] || '../../web/compressed.tracemonkey-pldi-09.pdf';
|
||||
var data = new Uint8Array(fs.readFileSync(pdfPath));
|
||||
|
||||
// Will be using promises to load document, pages and misc data instead of
|
||||
// callback.
|
||||
PDFJS.getDocument(data).then(function (doc) {
|
||||
var numPages = doc.numPages;
|
||||
console.log('# Document Loaded');
|
||||
console.log('Number of Pages: ' + numPages);
|
||||
console.log();
|
||||
|
||||
var lastPromise; // will be used to chain promises
|
||||
lastPromise = doc.getMetadata().then(function (data) {
|
||||
console.log('# Metadata Is Loaded');
|
||||
console.log('## Info');
|
||||
console.log(JSON.stringify(data.info, null, 2));
|
||||
console.log();
|
||||
if (data.metadata) {
|
||||
console.log('## Metadata');
|
||||
console.log(JSON.stringify(data.metadata.metadata, null, 2));
|
||||
console.log();
|
||||
}
|
||||
});
|
||||
|
||||
var loadPage = function (pageNum) {
|
||||
return doc.getPage(pageNum).then(function (page) {
|
||||
console.log('# Page ' + pageNum);
|
||||
var viewport = page.getViewport(1.0 /* scale */);
|
||||
console.log('Size: ' + viewport.width + 'x' + viewport.height);
|
||||
console.log();
|
||||
return page.getTextContent().then(function (content) {
|
||||
// Content contains lots of information about the text layout and
|
||||
// styles, but we need only strings at the moment
|
||||
var strings = content.items.map(function (item) {
|
||||
return item.str;
|
||||
});
|
||||
console.log('## Text Content');
|
||||
console.log(strings.join(' '));
|
||||
}).then(function () {
|
||||
console.log();
|
||||
});
|
||||
})
|
||||
};
|
||||
// Loading of the first page will wait on metadata and subsequent loadings
|
||||
// will wait on the previous pages.
|
||||
for (var i = 1; i <= numPages; i++) {
|
||||
lastPromise = lastPromise.then(loadPage.bind(null, i));
|
||||
}
|
||||
return lastPromise;
|
||||
}).then(function () {
|
||||
console.log('# End of Document');
|
||||
}, function (err) {
|
||||
console.error('Error: ' + err);
|
||||
});
|
86
3rdparty/pdf.js/examples/node/pdf2svg.js
vendored
Executable file
86
3rdparty/pdf.js/examples/node/pdf2svg.js
vendored
Executable file
@@ -0,0 +1,86 @@
|
||||
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
||||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
//
|
||||
// Node tool to dump SVG output into a file.
|
||||
//
|
||||
|
||||
var fs = require('fs');
|
||||
|
||||
// HACK few hacks to let PDF.js be loaded not as a module in global space.
|
||||
global.window = global;
|
||||
global.navigator = { userAgent: 'node' };
|
||||
global.PDFJS = {};
|
||||
|
||||
require('./domstubs.js');
|
||||
|
||||
PDFJS.workerSrc = true;
|
||||
require('../../build/singlefile/build/pdf.combined.js');
|
||||
|
||||
// Loading file from file system into typed array
|
||||
var pdfPath = process.argv[2] || '../../web/compressed.tracemonkey-pldi-09.pdf';
|
||||
var data = new Uint8Array(fs.readFileSync(pdfPath));
|
||||
|
||||
// Dumps svg outputs to a folder called svgdump
|
||||
function writeToFile(svgdump, pageNum) {
|
||||
var name = getFileNameFromPath(pdfPath);
|
||||
fs.mkdir('./svgdump/', function(err) {
|
||||
if (!err || err.code === 'EEXIST') {
|
||||
fs.writeFile('./svgdump/' + name + "-" + pageNum + '.svg', svgdump,
|
||||
function(err) {
|
||||
if (err) {
|
||||
console.log('Error: ' + err);
|
||||
} else {
|
||||
console.log('Page: ' + pageNum);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Get filename from the path
|
||||
|
||||
function getFileNameFromPath(path) {
|
||||
var index = path.lastIndexOf('/');
|
||||
var extIndex = path.lastIndexOf('.');
|
||||
return path.substring(index , extIndex);
|
||||
}
|
||||
|
||||
// Will be using promises to load document, pages and misc data instead of
|
||||
// callback.
|
||||
PDFJS.getDocument(data).then(function (doc) {
|
||||
var numPages = doc.numPages;
|
||||
console.log('# Document Loaded');
|
||||
console.log('Number of Pages: ' + numPages);
|
||||
console.log();
|
||||
|
||||
var lastPromise = Promise.resolve(); // will be used to chain promises
|
||||
var loadPage = function (pageNum) {
|
||||
return doc.getPage(pageNum).then(function (page) {
|
||||
console.log('# Page ' + pageNum);
|
||||
var viewport = page.getViewport(1.0 /* scale */);
|
||||
console.log('Size: ' + viewport.width + 'x' + viewport.height);
|
||||
console.log();
|
||||
|
||||
return page.getOperatorList().then(function (opList) {
|
||||
var svgGfx = new PDFJS.SVGGraphics(page.commonObjs, page.objs);
|
||||
svgGfx.embedFonts = true;
|
||||
return svgGfx.getSVG(opList, viewport).then(function (svg) {
|
||||
var svgDump = svg.toString();
|
||||
writeToFile(svgDump, pageNum);
|
||||
});
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
for (var i = 1; i <= numPages; i++) {
|
||||
lastPromise = lastPromise.then(loadPage.bind(null, i));
|
||||
}
|
||||
return lastPromise;
|
||||
}).then(function () {
|
||||
console.log('# End of Document');
|
||||
}, function (err) {
|
||||
console.error('Error: ' + err);
|
||||
});
|
Reference in New Issue
Block a user