123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- var PDFRenderer =
- {
- PDFRenderer_loadedPages: [],
- PDFRenderer_loadedDocuments: [],
- PDFRenderer_loadedCanvas: [],
- PDFRenderer_workers: [],
-
- PDFJS_InitLibrary: function()
- {
- var script = document.createElement("script")
- script.type = "text/javascript";
-
- if(script.readyState)
- {
- //IE
- script.onreadystatechange = function()
- {
- if (script.readyState === "loaded" || script.readyState === "complete")
- {
- script.onreadystatechange = null;
-
- pdfjsLib.GlobalWorkerOptions.workerSrc = 'pdf.worker.js';
- pdfjsLib.cMapUrl = "cmaps/";
- pdfjsLib.cMapPacked = true;
-
- SendMessage("WebGL_JSRuntime", 'OnLibraryInitialized', '');
- }
- };
- }
- else
- {
- //Others
- script.onload = function()
- {
- pdfjsLib.GlobalWorkerOptions.workerSrc = 'pdf.worker.js';
- pdfjsLib.cMapUrl = "cmaps/";
- pdfjsLib.cMapPacked = true;
-
- SendMessage("WebGL_JSRuntime", 'OnLibraryInitialized', '');
- };
- }
-
- script.src = 'pdf.js';
- document.getElementsByTagName("head")[0].appendChild(script);
- _PDFRenderer_Initialized = true;
- },
-
- PDFJS_LoadDocumentFromURL__deps: ['PDFRenderer_loadedDocuments'],
- PDFJS_LoadDocumentFromURL: function(promiseHandle, url)
- {
- var promiseHandleString = Pointer_stringify(promiseHandle);
-
- var loadingTask = pdfjsLib.getDocument(Pointer_stringify(url));
-
- loadingTask.onProgress = function(progress) {
- SendMessage("WebGL_JSRuntime", "OnPromiseProgress", "promiseHandle: " + promiseHandleString + " progress: " + (progress.loaded / progress.total));
- }
-
- loadingTask.promise.then(function(pdf)
- {
- var assignedIndex = -1;
- var length = _PDFRenderer_loadedDocuments.length;
- for (var i = 0; i < length; i++)
- {
- if (typeof _PDFRenderer_loadedDocuments[i] == 'undefined')
- {
- _PDFRenderer_loadedDocuments[i] = pdf;
- assignedIndex = i;
- break;
- }
- }
- if (assignedIndex == -1)
- {
- assignedIndex = _PDFRenderer_loadedDocuments.push(pdf) - 1;
- }
-
- SendMessage("WebGL_JSRuntime", "OnPromiseThen", "promiseHandle: " + promiseHandleString + " objectHandle: " + (assignedIndex + 1).toString());
- },function(reason)
- {
- SendMessage("WebGL_JSRuntime", "OnPromiseCatch", "promiseHandle: " + promiseHandleString + " objectHandle: " + "0");
- });
-
- },
-
- PDFJS_LoadDocumentFromBytes__deps: ['PDFRenderer_loadedDocuments'],
- PDFJS_LoadDocumentFromBytes: function(promiseHandle, base64)
- {
- var promiseHandleString = Pointer_stringify(promiseHandle);
-
- var raw = atob(Pointer_stringify(base64));
- var uint8Array = new Uint8Array(new ArrayBuffer(raw.length));
- for (var i = 0; i < raw.length; i++)
- {
- uint8Array[i] = raw.charCodeAt(i);
- }
-
- pdfjsLib.getDocument(uint8Array).promise.then(function(pdf)
- {
- var assignedIndex = -1;
- var length = _PDFRenderer_loadedDocuments.length;
- for (var i = 0; i < length; i++)
- {
- if (typeof _PDFRenderer_loadedDocuments[i] == 'undefined')
- {
- _PDFRenderer_loadedDocuments[i] = pdf;
- assignedIndex = i;
- break;
- }
- }
- if (assignedIndex == -1)
- {
- assignedIndex = _PDFRenderer_loadedDocuments.push(pdf) - 1;
- }
-
- SendMessage("WebGL_JSRuntime", "OnPromiseThen", "promiseHandle: " + promiseHandleString + " objectHandle: " + (assignedIndex + 1).toString());
- },function(reason)
- {
- SendMessage("WebGL_JSRuntime", "OnPromiseCatch", "promiseHandle: " + promiseHandleString + " objectHandle: " + "0");
- });
- },
-
- PDFJS_CloseDocument__deps: ['PDFRenderer_loadedDocuments'],
- PDFJS_CloseDocument: function(documentHandle)
- {
- var pdfDocument = _PDFRenderer_loadedDocuments[documentHandle - 1];
- _PDFRenderer_loadedDocuments[documentHandle - 1] = undefined;
- delete pdfDocument;
- },
-
- PDFJS_GetPageCount__deps: ['PDFRenderer_loadedDocuments'],
- PDFJS_GetPageCount: function(documentHandle)
- {
- var pdfDocument = _PDFRenderer_loadedDocuments[documentHandle - 1];
- return pdfDocument.numPages;
- },
-
- PDFJS_LoadPage__deps: ['PDFRenderer_loadedDocuments', 'PDFRenderer_loadedPages'],
- PDFJS_LoadPage: function(promiseHandle, documentHandle, pageIndex)
- {
- var promiseHandleString = Pointer_stringify(promiseHandle);
- var pdfDocument = _PDFRenderer_loadedDocuments[documentHandle - 1];
-
- pdfDocument.getPage(pageIndex).then(function(page)
- {
- var assignedIndex = -1;
- var length = _PDFRenderer_loadedPages.length;
- for (var i = 0; i < length; i++)
- {
- if (typeof _PDFRenderer_loadedPages[i] == 'undefined')
- {
- _PDFRenderer_loadedPages[i] = page;
- assignedIndex = i;
- break;
- }
- }
- if (assignedIndex == -1)
- {
- assignedIndex = _PDFRenderer_loadedPages.push(page) - 1;
- }
-
- SendMessage("WebGL_JSRuntime", "OnPromiseThen", "promiseHandle: " + promiseHandleString + " objectHandle: " + (assignedIndex + 1).toString());
- },function(reason)
- {
- SendMessage("WebGL_JSRuntime", "OnPromiseCatch", "promiseHandle: " + promiseHandleString + " objectHandle: " + "0");
- });
- },
-
- PDFJS_ClosePage__deps: ['PDFRenderer_loadedPages'],
- PDFJS_ClosePage: function(pageHandle)
- {
- var page = _PDFRenderer_loadedPages[pageHandle - 1];
- _PDFRenderer_loadedPages[pageHandle - 1] = undefined;
- delete page;
- },
-
- PDFJS_TryTerminateRenderWorker__deps: ['PDFRenderer_workers'],
- PDFJS_TryTerminateRenderWorker: function(promiseHandle)
- {
- var worker = _PDFRenderer_workers[promiseHandle];
- if (worker != null && worker != undefined)
- {
- worker.cancel();
-
- delete _PDFRenderer_workers[promiseHandle];
-
- SendMessage("WebGL_JSRuntime", "OnPromiseCancel", "promiseHandle: " + promiseHandleString + " objectHandle: " + "0");
- }
- },
-
- PDFJS_RenderPageIntoCanvas__deps: ['PDFRenderer_loadedPages', 'PDFRenderer_loadedCanvas', 'PDFRenderer_workers'],
- PDFJS_RenderPageIntoCanvas: function(promiseHandle, pageHandle, scale, width, height)
- {
- var promiseHandleString = Pointer_stringify(promiseHandle);
- var page = _PDFRenderer_loadedPages[pageHandle - 1];
- var viewport = page.getViewport(scale);
- var canvas = document.createElement('canvas');
- var context = canvas.getContext('2d');
-
- context.canvas.width = width;
- context.canvas.height = height;
- var renderContext =
- {
- canvasContext: context,
- viewport: viewport
- };
-
- _PDFRenderer_workers[promiseHandle] = page.render(renderContext).promise.then(function()
- {
- var assignedIndex = -1;
- var length = _PDFRenderer_loadedCanvas.length;
- for (var i = 0; i < length; i++)
- {
- if (typeof _PDFRenderer_loadedCanvas[i] == 'undefined')
- {
- _PDFRenderer_loadedCanvas[i] = canvas;
- assignedIndex = i;
- break;
- }
- }
- if (assignedIndex == -1)
- {
- assignedIndex = _PDFRenderer_loadedCanvas.push(canvas) - 1;
- }
-
- delete _PDFRenderer_workers[promiseHandle];
-
- SendMessage("WebGL_JSRuntime", "OnPromiseThen", "promiseHandle: " + promiseHandleString + " objectHandle: " + (assignedIndex + 1).toString());
-
-
- },function(reason)
- {
- delete _PDFRenderer_workers[promiseHandle];
-
- SendMessage("WebGL_JSRuntime", "OnPromiseCatch", "promiseHandle: " + promiseHandleString + " objectHandle: " + "0");
- }).internalRenderTask;
- },
-
- PDFJS_RenderCanvasIntoTexture__deps: ['PDFRenderer_loadedCanvas'],
- PDFJS_RenderCanvasIntoTexture: function (canvasHandle, textureHandle)
- {
- var canvas = _PDFRenderer_loadedCanvas[canvasHandle - 1];
-
- GLctx.pixelStorei(GLctx.UNPACK_FLIP_Y_WEBGL, true);
- GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[textureHandle]);
- if (typeof WebGL2RenderingContext !== 'undefined' && GLctx instanceof WebGL2RenderingContext)
- {
- GLctx.texSubImage2D(GLctx.TEXTURE_2D, 0, 0, 0, canvas.width, canvas.height, GLctx.RGBA, GLctx.UNSIGNED_BYTE, canvas);
- }
- else
- {
- GLctx.texImage2D(GLctx.TEXTURE_2D, 0, GLctx.RGBA, GLctx.RGBA, GLctx.UNSIGNED_BYTE, canvas);
- }
- GLctx.bindTexture(GLctx.TEXTURE_2D, null);
- GLctx.pixelStorei(GLctx.UNPACK_FLIP_Y_WEBGL, false);
- },
-
- PDFJS_DestroyCanvas__deps: ['PDFRenderer_loadedCanvas'],
- PDFJS_DestroyCanvas: function(canvasHandle)
- {
- var canvas = _PDFRenderer_loadedCanvas[canvasHandle - 1];
- delete canvas;
- _PDFRenderer_loadedCanvas[canvasHandle - 1] = undefined;
- },
-
- PDFJS_GetPageWidth__deps: ['PDFRenderer_loadedPages'],
- PDFJS_GetPageWidth: function(pageHandle, scale)
- {
- return _PDFRenderer_loadedPages[pageHandle - 1].getViewport(scale).width;
- },
-
- PDFJS_GetPageHeight__deps: ['PDFRenderer_loadedPages'],
- PDFJS_GetPageHeight: function(pageHandle, scale)
- {
- return _PDFRenderer_loadedPages[pageHandle - 1].getViewport(scale).height;
- }
- };
- mergeInto(LibraryManager.library, PDFRenderer);
|