mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-09 09:45:42 +02:00
Makes PDF data reading Streams API friendly.
This commit is contained in:
parent
8cdb69634f
commit
0d591719d9
8 changed files with 1153 additions and 178 deletions
|
@ -567,6 +567,55 @@ function stringToBytes(str) {
|
|||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets length of the array (Array, Uint8Array, or string) in bytes.
|
||||
* @param {Array|Uint8Array|string} arr
|
||||
* @returns {number}
|
||||
*/
|
||||
function arrayByteLength(arr) {
|
||||
if (arr.length !== undefined) {
|
||||
return arr.length;
|
||||
}
|
||||
assert(arr.byteLength !== undefined);
|
||||
return arr.byteLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines array items (arrays) into single Uint8Array object.
|
||||
* @param {Array} arr - the array of the arrays (Array, Uint8Array, or string).
|
||||
* @returns {Uint8Array}
|
||||
*/
|
||||
function arraysToBytes(arr) {
|
||||
// Shortcut: if first and only item is Uint8Array, return it.
|
||||
if (arr.length === 1 && (arr[0] instanceof Uint8Array)) {
|
||||
return arr[0];
|
||||
}
|
||||
var resultLength = 0;
|
||||
var i, ii = arr.length;
|
||||
var item, itemLength ;
|
||||
for (i = 0; i < ii; i++) {
|
||||
item = arr[i];
|
||||
itemLength = arrayByteLength(item);
|
||||
resultLength += itemLength;
|
||||
}
|
||||
var pos = 0;
|
||||
var data = new Uint8Array(resultLength);
|
||||
for (i = 0; i < ii; i++) {
|
||||
item = arr[i];
|
||||
if (!(item instanceof Uint8Array)) {
|
||||
if (typeof item === 'string') {
|
||||
item = stringToBytes(item);
|
||||
} else {
|
||||
item = new Uint8Array(item);
|
||||
}
|
||||
}
|
||||
itemLength = item.byteLength;
|
||||
data.set(item, pos);
|
||||
pos += itemLength;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
function string32(value) {
|
||||
return String.fromCharCode((value >> 24) & 0xff, (value >> 16) & 0xff,
|
||||
(value >> 8) & 0xff, value & 0xff);
|
||||
|
@ -2361,6 +2410,8 @@ exports.UnexpectedResponseException = UnexpectedResponseException;
|
|||
exports.UnknownErrorException = UnknownErrorException;
|
||||
exports.Util = Util;
|
||||
exports.XRefParseException = XRefParseException;
|
||||
exports.arrayByteLength = arrayByteLength;
|
||||
exports.arraysToBytes = arraysToBytes;
|
||||
exports.assert = assert;
|
||||
exports.bytesToString = bytesToString;
|
||||
exports.combineUrl = combineUrl;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue