diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..95de9fb8e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +pdf.pdf +intelisa.pdf +openweb_tm-PRINT.pdf diff --git a/README b/README index ee537f0a5..423943c5b 100644 --- a/README +++ b/README @@ -7,6 +7,14 @@ You can read more about pdf.js here: http://andreasgal.com/2011/06/15/pdf-js/ http://blog.mozilla.com/cjones/2011/06/15/overview-of-pdf-js-guts/ -Or follow us on twitter: @pdfjs +follow us on twitter: @pdfjs http://twitter.com/#!/pdfjs + +join our mailing list: + +dev-pdf-js@lists.mozilla.org + +and talk to us on IRC: + +#pdfjs on irc.mozilla.org diff --git a/crypto.js b/crypto.js new file mode 100644 index 000000000..e888d0212 --- /dev/null +++ b/crypto.js @@ -0,0 +1,260 @@ +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- / +/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ + +"use strict"; + +var ARCFourCipher = (function() { + function constructor(key) { + this.a = 0; + this.b = 0; + var s = new Uint8Array(256); + var i, j = 0, tmp, keyLength = key.length; + for (i = 0; i < 256; ++i) + s[i] = i; + for (i = 0; i < 256; ++i) { + tmp = s[i]; + j = (j + tmp + key[i % keyLength]) & 0xFF; + s[i] = s[j]; + s[j] = tmp; + } + this.s = s; + } + + constructor.prototype = { + encryptBlock: function(data) { + var i, n = data.length, tmp, tmp2; + var a = this.a, b = this.b, s = this.s; + var output = new Uint8Array(n); + for (i = 0; i < n; ++i) { + var tmp; + a = (a + 1) & 0xFF; + tmp = s[a]; + b = (b + tmp) & 0xFF; + tmp2 = s[b] + s[a] = tmp2; + s[b] = tmp; + output[i] = data[i] ^ s[(tmp + tmp2) & 0xFF]; + } + this.a = a; + this.b = b; + return output; + } + }; + + return constructor; +})(); + +var md5 = (function() { + var r = new Uint8Array([ + 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, + 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, + 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, + 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]); + var k = new Int32Array([ + -680876936, -389564586, 606105819, -1044525330, -176418897, 1200080426, + -1473231341, -45705983, 1770035416, -1958414417, -42063, -1990404162, + 1804603682, -40341101, -1502002290, 1236535329, -165796510, -1069501632, + 643717713, -373897302, -701558691, 38016083, -660478335, -405537848, 568446438, + -1019803690, -187363961, 1163531501, -1444681467, -51403784, 1735328473, + -1926607734, -378558, -2022574463, 1839030562, -35309556, -1530992060, + 1272893353, -155497632, -1094730640, 681279174, -358537222, -722521979, + 76029189, -640364487, -421815835, 530742520, -995338651, -198630844, 1126891415, + -1416354905, -57434055, 1700485571, -1894986606, -1051523, -2054922799, + 1873313359, -30611744, -1560198380, 1309151649, -145523070, -1120210379, + 718787259, -343485551]); + + function hash(data, offset, length) { + var h0 = 1732584193, h1 = -271733879, h2 = -1732584194, h3 = 271733878; + // pre-processing + var paddedLength = (length + 72) & ~63; // data + 9 extra bytes + var padded = new Uint8Array(paddedLength); + var i, j, n; + for (i = 0; i < length; ++i) + padded[i] = data[offset++]; + padded[i++] = 0x80; + n = paddedLength - 8; + for (; i < n; ++i) + padded[i] = 0; + padded[i++] = (length << 3) & 0xFF; + padded[i++] = (length >> 5) & 0xFF; + padded[i++] = (length >> 13) & 0xFF; + padded[i++] = (length >> 21) & 0xFF; + padded[i++] = (length >>> 29) & 0xFF; + padded[i++] = 0; + padded[i++] = 0; + padded[i++] = 0; + // chunking + // TODO ArrayBuffer ? + var w = new Int32Array(16); + for (i = 0; i < paddedLength;) { + for (j = 0; j < 16; ++j, i += 4) + w[j] = padded[i] | (padded[i + 1] << 8) | (padded[i + 2] << 16) | (padded[i + 3] << 24); + var a = h0, b = h1, c = h2, d = h3, f, g; + for (j = 0; j < 64; ++j) { + if (j < 16) { + f = (b & c) | ((~b) & d); + g = j; + } else if (j < 32) { + f = (d & b) | ((~d) & c); + g = (5 * j + 1) & 15; + } else if (j < 48) { + f = b ^ c ^ d; + g = (3 * j + 5) & 15; + } else { + f = c ^ (b | (~d)); + g = (7 * j) & 15; + } + var tmp = d, rotateArg = (a + f + k[j] + w[g]) | 0, rotate = r[j]; + d = c; + c = b; + b = (b + ((rotateArg << rotate) | (rotateArg >>> (32 - rotate)))) | 0; + a = tmp; + } + h0 = (h0 + a) | 0; + h1 = (h1 + b) | 0; + h2 = (h2 + c) | 0; + h3 = (h3 + d) | 0; + } + return new Uint8Array([ + h0 & 0xFF, (h0 >> 8) & 0xFF, (h0 >> 16) & 0xFF, (h0 >>> 24) & 0xFF, + h1 & 0xFF, (h1 >> 8) & 0xFF, (h1 >> 16) & 0xFF, (h1 >>> 24) & 0xFF, + h2 & 0xFF, (h2 >> 8) & 0xFF, (h2 >> 16) & 0xFF, (h2 >>> 24) & 0xFF, + h3 & 0xFF, (h3 >> 8) & 0xFF, (h3 >> 16) & 0xFF, (h3 >>> 24) & 0xFF + ]); + } + return hash; +})(); + +var CipherTransform = (function() { + function constructor(stringCipherConstructor, streamCipherConstructor) { + this.stringCipherConstructor = stringCipherConstructor; + this.streamCipherConstructor = streamCipherConstructor; + } + constructor.prototype = { + createStream: function (stream) { + var cipher = new this.streamCipherConstructor(); + return new DecryptStream(stream, function(data) { + return cipher.encryptBlock(data); + }); + }, + decryptString: function(s) { + var cipher = new this.stringCipherConstructor(); + var data = string2bytes(s); + data = cipher.encryptBlock(data); + return bytes2string(data); + } + }; + return constructor; +})(); + +var CipherTransformFactory = (function() { + function prepareKeyData(fileId, password, ownerPassword, userPassword, flags, revision, keyLength) { + var defaultPasswordBytes = new Uint8Array([ + 0x28, 0xBF, 0x4E, 0x5E, 0x4E, 0x75, 0x8A, 0x41, 0x64, 0x00, 0x4E, 0x56, 0xFF, 0xFA, 0x01, 0x08, + 0x2E, 0x2E, 0x00, 0xB6, 0xD0, 0x68, 0x3E, 0x80, 0x2F, 0x0C, 0xA9, 0xFE, 0x64, 0x53, 0x69, 0x7A]); + var hashData = new Uint8Array(88), i = 0, j, n; + if (password) { + n = Math.min(32, password.length); + for (; i < n; ++i) + hashData[i] = password[i]; + } + j = 0; + while (i < 32) { + hashData[i++] = defaultPasswordBytes[j++]; + } + // as now the padded password in the hashData[0..i] + for (j = 0, n = ownerPassword.length; j < n; ++j) + hashData[i++] = ownerPassword[j]; + hashData[i++] = flags & 0xFF; + hashData[i++] = (flags >> 8) & 0xFF; + hashData[i++] = (flags >> 16) & 0xFF; + hashData[i++] = (flags >>> 24) & 0xFF; + for (j = 0, n = fileId.length; j < n; ++j) + hashData[i++] = fileId[j]; + // TODO rev 4, if metadata is not encrypted pass 0xFFFFFF also + var hash = md5(hashData, 0, i); + var keyLengthInBytes = keyLength >> 3; + if (revision >= 3) { + for (j = 0; j < 50; ++j) { + hash = md5(hash, 0, keyLengthInBytes); + } + } + var encryptionKey = hash.subarray(0, keyLengthInBytes); + var cipher, checkData; + + if (revision >= 3) { + // padded password in hashData, we can use this array for user password check + i = 32; + for(j = 0, n = fileId.length; j < n; ++j) + hashData[i++] = fileId[j]; + cipher = new ARCFourCipher(encryptionKey); + var checkData = cipher.encryptBlock(md5(hashData, 0, i)); + n = encryptionKey.length; + var derrivedKey = new Uint8Array(n), k; + for (j = 1; j <= 19; ++j) { + for (k = 0; k < n; ++k) + derrivedKey[k] = encryptionKey[k] ^ j; + cipher = new ARCFourCipher(derrivedKey); + checkData = cipher.encryptBlock(checkData); + } + } else { + cipher = new ARCFourCipher(encryptionKey); + checkData = cipher.encryptBlock(hashData.subarray(0, 32)); + } + for (j = 0, n = checkData.length; j < n; ++j) { + if (userPassword[j] != checkData[j]) + error("incorrect password"); + } + return encryptionKey; + } + + function constructor(dict, fileId, password) { + var filter = dict.get("Filter"); + if (!IsName(filter) || filter.name != "Standard") + error("unknown encryption method"); + this.dict = dict; + var algorithm = dict.get("V"); + if (!IsInt(algorithm) || + (algorithm != 1 && algorithm != 2)) + error("unsupported encryption algorithm"); + // TODO support algorithm 4 + var keyLength = dict.get("Length") || 40; + if (!IsInt(keyLength) || + keyLength < 40 || (keyLength % 8) != 0) + error("invalid key length"); + // prepare keys + var ownerPassword = stringToBytes(dict.get("O")); + var userPassword = stringToBytes(dict.get("U")); + var flags = dict.get("P"); + var revision = dict.get("R"); + var fileIdBytes = stringToBytes(fileId); + var passwordBytes; + if (password) + passwordBytes = stringToBytes(password); + + this.encryptionKey = prepareKeyData(fileIdBytes, passwordBytes, + ownerPassword, userPassword, flags, revision, keyLength); + } + + constructor.prototype = { + createCipherTransform: function(num, gen) { + var encryptionKey = this.encryptionKey; + var key = new Uint8Array(encryptionKey.length + 5), i, n; + for (i = 0, n = encryptionKey.length; i < n; ++i) + key[i] = encryptionKey[i]; + key[i++] = num & 0xFF; + key[i++] = (num >> 8) & 0xFF; + key[i++] = (num >> 16) & 0xFF; + key[i++] = gen & 0xFF; + key[i++] = (gen >> 8) & 0xFF; + var hash = md5(key, 0, i); + key = hash.subarray(0, Math.min(key.length, 16)); + var cipherConstructor = function() { + return new ARCFourCipher(key); + }; + return new CipherTransform(cipherConstructor, cipherConstructor); + } + }; + + return constructor; +})(); diff --git a/fonts.js b/fonts.js index 4ac5935b5..19d16cbed 100644 --- a/fonts.js +++ b/fonts.js @@ -34,7 +34,7 @@ var kDisableFonts = false; * http://cgit.freedesktop.org/poppler/poppler/tree/poppler/GfxFont.cc#n65 */ -var kScalePrecision = 100; +var kScalePrecision = 40; var Fonts = { _active: null, @@ -68,8 +68,8 @@ var Fonts = { var unicode = encoding[charcode]; // Check if the glyph has already been converted - if (unicode instanceof Name) - unicode = encoding[unicode] = GlyphsUnicode[unicode.name]; + if (!IsNum(unicode)) + unicode = encoding[unicode] = GlyphsUnicode[unicode.name]; // Handle surrogate pairs if (unicode > 0xFFFF) { @@ -95,7 +95,7 @@ var Fonts = { } }; -var FontsLoader = { +var FontLoader = { bind: function(fonts) { var worker = (typeof window == "undefined"); var ready = true; @@ -105,10 +105,10 @@ var FontsLoader = { if (Fonts[font.name]) { ready = ready && !Fonts[font.name].loading; continue; - } else { - ready = false; } + ready = false; + var obj = new Font(font.name, font.file, font.properties); var str = ""; @@ -180,6 +180,7 @@ var Font = (function () { warn("Font " + properties.type + " is not supported"); break; } + this.data = data; Fonts[name] = { data: data, @@ -793,9 +794,18 @@ var Font = (function () { }); }, - bindDOM: function font_bindDom(data) { + bindDOM: function font_bindDom(data, callback) { var fontName = this.name; + // Just adding the font-face to the DOM doesn't make it load. It + // seems it's loaded once Gecko notices it's used. Therefore, + // add a div on the page using the loaded font. + var div = document.createElement("div"); + var style = 'font-family:"' + name + + '";position: absolute;top:-99999;left:-99999;z-index:-99999'; + div.setAttribute("style", style); + document.body.appendChild(div); + /** Hack begin */ // Actually there is not event when a font has finished downloading so // the following code are a dirty hack to 'guess' when a font is ready @@ -815,15 +825,19 @@ var Font = (function () { // For some reasons the font has not loaded, so mark it loaded for the // page to proceed but cry - if ((Date.now() - this.start) >= kMaxWaitForFontFace) { - window.clearInterval(interval); - Fonts[fontName].loading = false; - warn("Is " + fontName + " loaded?"); - this.start = 0; - } else if (textWidth != ctx.measureText(testString).width) { - window.clearInterval(interval); - Fonts[fontName].loading = false; - this.start = 0; + if (textWidth == ctx.measureText(testString).width) { + if ((Date.now() - this.start) < kMaxWaitForFontFace) { + return; + } else { + warn("Is " + fontName + " loaded?"); + } + } + + window.clearInterval(interval); + Fonts[fontName].loading = false; + this.start = 0; + if (callback) { + callback(); } }, 30, this); diff --git a/multi_page_viewer.css b/multi_page_viewer.css index b3eaab792..2eaca4870 100644 --- a/multi_page_viewer.css +++ b/multi_page_viewer.css @@ -74,6 +74,20 @@ span { width: 100%; } +.thumbnailPageNumber { + color: #fff; + font-size: 0.55em; + text-align: right; + margin: -6px 2px 6px 0px; + width: 102px; +} + +.thumbnail { + width: 104px; + height: 134px; + margin: 0px auto 10px; +} + .page { width: 816px; height: 1056px; @@ -163,27 +177,46 @@ span { } #sidebar { - background-color: rgba(0, 0, 0, 0.8); position: fixed; - width: 150px; + width: 200px; top: 62px; bottom: 18px; + left: -170px; + transition: left 0.25s ease-in-out 1s; + -moz-transition: left 0.25s ease-in-out 1s; + -webkit-transition: left 0.25s ease-in-out 1s; +} + +#sidebar:hover { + left: 0px; + transition: left 0.25s ease-in-out 0s; + -moz-transition: left 0.25s ease-in-out 0s; + -webkit-transition: left 0.25s ease-in-out 0s; +} + +#sidebarBox { + background-color: rgba(0, 0, 0, 0.7); + width: 150px; + height: 100%; border-top-right-radius: 8px; border-bottom-right-radius: 8px; -moz-border-radius-topright: 8px; -moz-border-radius-bottomright: 8px; -webkit-border-top-right-radius: 8px; -webkit-border-bottom-right-radius: 8px; + box-shadow: 0px 2px 8px #000; + -moz-box-shadow: 0px 2px 8px #000; + -webkit-box-shadow: 0px 2px 8px #000; } #sidebarScrollView { position: absolute; overflow: hidden; overflow-y: auto; - top: 40px; - right: 10px; + top: 10px; bottom: 10px; left: 10px; + width: 130px; } #sidebarContentView { diff --git a/multi_page_viewer.html b/multi_page_viewer.html index 47234686d..e90606a23 100644 --- a/multi_page_viewer.html +++ b/multi_page_viewer.html @@ -6,6 +6,7 @@ + @@ -39,13 +40,16 @@ Open File - +
--> + +