You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.3 KiB
JavaScript
123 lines
3.3 KiB
JavaScript
// Generated by CoffeeScript 1.12.5
|
|
|
|
/*
|
|
PDFObject - converts JavaScript types into their corrisponding PDF types.
|
|
By Devon Govett
|
|
*/
|
|
|
|
(function() {
|
|
var PDFObject, PDFReference;
|
|
|
|
PDFObject = (function() {
|
|
var escapable, escapableRe, pad, swapBytes;
|
|
|
|
function PDFObject() {}
|
|
|
|
pad = function(str, length) {
|
|
return (Array(length + 1).join('0') + str).slice(-length);
|
|
};
|
|
|
|
PDFObject.pad = pad;
|
|
|
|
escapableRe = /[\n\r\t\b\f\(\)\\]/g;
|
|
|
|
PDFObject.escapableRe = escapableRe;
|
|
|
|
escapable = {
|
|
'\n': '\\n',
|
|
'\r': '\\r',
|
|
'\t': '\\t',
|
|
'\b': '\\b',
|
|
'\f': '\\f',
|
|
'\\': '\\\\',
|
|
'(': '\\(',
|
|
')': '\\)'
|
|
};
|
|
|
|
PDFObject.escapable = escapable;
|
|
|
|
swapBytes = function(buff) {
|
|
var a, i, j, l, ref;
|
|
l = buff.length;
|
|
if (l & 0x01) {
|
|
throw new Error("Buffer length must be even");
|
|
} else {
|
|
for (i = j = 0, ref = l - 1; j < ref; i = j += 2) {
|
|
a = buff[i];
|
|
buff[i] = buff[i + 1];
|
|
buff[i + 1] = a;
|
|
}
|
|
}
|
|
return buff;
|
|
};
|
|
|
|
PDFObject.swapBytes = swapBytes;
|
|
|
|
PDFObject.convert = function(object) {
|
|
var e, i, isUnicode, items, j, key, out, ref, string, val;
|
|
if (typeof object === 'string') {
|
|
return '/' + object;
|
|
} else if (object instanceof String) {
|
|
string = object.replace(escapableRe, function(c) {
|
|
return escapable[c];
|
|
});
|
|
isUnicode = false;
|
|
for (i = j = 0, ref = string.length; j < ref; i = j += 1) {
|
|
if (string.charCodeAt(i) > 0x7f) {
|
|
isUnicode = true;
|
|
break;
|
|
}
|
|
}
|
|
if (isUnicode) {
|
|
string = swapBytes(new Buffer('\ufeff' + string, 'utf16le')).toString('binary');
|
|
}
|
|
return '(' + string + ')';
|
|
} else if (Buffer.isBuffer(object)) {
|
|
return '<' + object.toString('hex') + '>';
|
|
} else if (object instanceof PDFReference) {
|
|
return object.toString();
|
|
} else if (object instanceof Date) {
|
|
return '(D:' + pad(object.getUTCFullYear(), 4) + pad(object.getUTCMonth() + 1, 2) + pad(object.getUTCDate(), 2) + pad(object.getUTCHours(), 2) + pad(object.getUTCMinutes(), 2) + pad(object.getUTCSeconds(), 2) + 'Z)';
|
|
} else if (Array.isArray(object)) {
|
|
items = ((function() {
|
|
var k, len, results;
|
|
results = [];
|
|
for (k = 0, len = object.length; k < len; k++) {
|
|
e = object[k];
|
|
results.push(PDFObject.convert(e));
|
|
}
|
|
return results;
|
|
})()).join(' ');
|
|
return '[' + items + ']';
|
|
} else if ({}.toString.call(object) === '[object Object]') {
|
|
out = ['<<'];
|
|
for (key in object) {
|
|
val = object[key];
|
|
out.push('/' + key + ' ' + PDFObject.convert(val));
|
|
}
|
|
out.push('>>');
|
|
return out.join('\n');
|
|
} else if (typeof object === 'number') {
|
|
return PDFObject.number(object);
|
|
} else {
|
|
return '' + object;
|
|
}
|
|
};
|
|
|
|
PDFObject.number = function(n) {
|
|
if (n > -1e21 && n < 1e21) {
|
|
return Math.round(n * 1e6) / 1e6;
|
|
}
|
|
throw new Error("unsupported number: " + n);
|
|
};
|
|
|
|
return PDFObject;
|
|
|
|
})();
|
|
|
|
module.exports = PDFObject;
|
|
|
|
PDFReference = require('./reference');
|
|
|
|
}).call(this);
|