pdfrenderer.jslib 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. var PDFRenderer =
  2. {
  3. PDFRenderer_loadedPages: [],
  4. PDFRenderer_loadedDocuments: [],
  5. PDFRenderer_loadedCanvas: [],
  6. PDFRenderer_workers: [],
  7. PDFJS_InitLibrary: function()
  8. {
  9. var script = document.createElement("script")
  10. script.type = "text/javascript";
  11. if(script.readyState)
  12. {
  13. //IE
  14. script.onreadystatechange = function()
  15. {
  16. if (script.readyState === "loaded" || script.readyState === "complete")
  17. {
  18. script.onreadystatechange = null;
  19. PDFJS.workerSrc = 'pdf.worker.js';
  20. SendMessage("WebGL_JSRuntime", 'OnLibraryInitialized', '');
  21. }
  22. };
  23. }
  24. else
  25. {
  26. //Others
  27. script.onload = function()
  28. {
  29. PDFJS.workerSrc = 'pdf.worker.js';
  30. SendMessage("WebGL_JSRuntime", 'OnLibraryInitialized', '');
  31. };
  32. }
  33. script.src = '../pdf.js';
  34. document.getElementsByTagName("head")[0].appendChild(script);
  35. _PDFRenderer_Initialized = true;
  36. },
  37. PDFJS_LoadDocumentFromURL__deps: ['PDFRenderer_loadedDocuments'],
  38. PDFJS_LoadDocumentFromURL: function(promiseHandle, url)
  39. {
  40. var promiseHandleString = Pointer_stringify(promiseHandle);
  41. PDFJS.getDocument(Pointer_stringify(url)).then(function(pdf)
  42. {
  43. var assignedIndex = -1;
  44. var length = _PDFRenderer_loadedDocuments.length;
  45. for (var i = 0; i < length; i++)
  46. {
  47. if (typeof _PDFRenderer_loadedDocuments[i] == 'undefined')
  48. {
  49. _PDFRenderer_loadedDocuments[i] = pdf;
  50. assignedIndex = i;
  51. break;
  52. }
  53. }
  54. if (assignedIndex == -1)
  55. {
  56. assignedIndex = _PDFRenderer_loadedDocuments.push(pdf) - 1;
  57. }
  58. SendMessage("WebGL_JSRuntime", "OnPromiseThen", "promiseHandle: " + promiseHandleString + " objectHandle: " + (assignedIndex + 1).toString());
  59. },function(reason)
  60. {
  61. SendMessage("WebGL_JSRuntime", "OnPromiseCatch", "promiseHandle: " + promiseHandleString + " objectHandle: " + "0");
  62. });
  63. },
  64. PDFJS_LoadDocumentFromBytes__deps: ['PDFRenderer_loadedDocuments'],
  65. PDFJS_LoadDocumentFromBytes: function(promiseHandle, base64)
  66. {
  67. var promiseHandleString = Pointer_stringify(promiseHandle);
  68. var raw = atob(Pointer_stringify(base64));
  69. var uint8Array = new Uint8Array(new ArrayBuffer(raw.length));
  70. for (var i = 0; i < raw.length; i++)
  71. {
  72. uint8Array[i] = raw.charCodeAt(i);
  73. }
  74. PDFJS.getDocument(uint8Array).then(function(pdf)
  75. {
  76. var assignedIndex = -1;
  77. var length = _PDFRenderer_loadedDocuments.length;
  78. for (var i = 0; i < length; i++)
  79. {
  80. if (typeof _PDFRenderer_loadedDocuments[i] == 'undefined')
  81. {
  82. _PDFRenderer_loadedDocuments[i] = pdf;
  83. assignedIndex = i;
  84. break;
  85. }
  86. }
  87. if (assignedIndex == -1)
  88. {
  89. assignedIndex = _PDFRenderer_loadedDocuments.push(pdf) - 1;
  90. }
  91. SendMessage("WebGL_JSRuntime", "OnPromiseThen", "promiseHandle: " + promiseHandleString + " objectHandle: " + (assignedIndex + 1).toString());
  92. },function(reason)
  93. {
  94. SendMessage("WebGL_JSRuntime", "OnPromiseCatch", "promiseHandle: " + promiseHandleString + " objectHandle: " + "0");
  95. });
  96. },
  97. PDFJS_CloseDocument__deps: ['PDFRenderer_loadedDocuments'],
  98. PDFJS_CloseDocument: function(documentHandle)
  99. {
  100. var pdfDocument = _PDFRenderer_loadedDocuments[documentHandle - 1];
  101. _PDFRenderer_loadedDocuments[documentHandle - 1] = undefined;
  102. delete pdfDocument;
  103. },
  104. PDFJS_GetPageCount__deps: ['PDFRenderer_loadedDocuments'],
  105. PDFJS_GetPageCount: function(documentHandle)
  106. {
  107. var pdfDocument = _PDFRenderer_loadedDocuments[documentHandle - 1];
  108. return pdfDocument.numPages;
  109. },
  110. PDFJS_LoadPage__deps: ['PDFRenderer_loadedDocuments', 'PDFRenderer_loadedPages'],
  111. PDFJS_LoadPage: function(promiseHandle, documentHandle, pageIndex)
  112. {
  113. var promiseHandleString = Pointer_stringify(promiseHandle);
  114. var pdfDocument = _PDFRenderer_loadedDocuments[documentHandle - 1];
  115. pdfDocument.getPage(pageIndex).then(function(page)
  116. {
  117. var assignedIndex = -1;
  118. var length = _PDFRenderer_loadedPages.length;
  119. for (var i = 0; i < length; i++)
  120. {
  121. if (typeof _PDFRenderer_loadedPages[i] == 'undefined')
  122. {
  123. _PDFRenderer_loadedPages[i] = page;
  124. assignedIndex = i;
  125. break;
  126. }
  127. }
  128. if (assignedIndex == -1)
  129. {
  130. assignedIndex = _PDFRenderer_loadedPages.push(page) - 1;
  131. }
  132. SendMessage("WebGL_JSRuntime", "OnPromiseThen", "promiseHandle: " + promiseHandleString + " objectHandle: " + (assignedIndex + 1).toString());
  133. },function(reason)
  134. {
  135. SendMessage("WebGL_JSRuntime", "OnPromiseCatch", "promiseHandle: " + promiseHandleString + " objectHandle: " + "0");
  136. });
  137. },
  138. PDFJS_ClosePage__deps: ['PDFRenderer_loadedPages'],
  139. PDFJS_ClosePage: function(pageHandle)
  140. {
  141. var page = _PDFRenderer_loadedPages[pageHandle - 1];
  142. _PDFRenderer_loadedPages[pageHandle - 1] = undefined;
  143. delete page;
  144. },
  145. PDFJS_TryTerminateRenderWorker__deps: ['PDFRenderer_workers'],
  146. PDFJS_TryTerminateRenderWorker: function(promiseHandle)
  147. {
  148. var worker = _PDFRenderer_workers[promiseHandle];
  149. if (worker != null && worker != undefined)
  150. {
  151. worker.cancel();
  152. delete _PDFRenderer_workers[promiseHandle];
  153. SendMessage("WebGL_JSRuntime", "OnPromiseCancel", "promiseHandle: " + promiseHandleString + " objectHandle: " + "0");
  154. }
  155. },
  156. PDFJS_RenderPageIntoCanvas__deps: ['PDFRenderer_loadedPages', 'PDFRenderer_loadedCanvas', 'PDFRenderer_workers'],
  157. PDFJS_RenderPageIntoCanvas: function(promiseHandle, pageHandle, scale)
  158. {
  159. var promiseHandleString = Pointer_stringify(promiseHandle);
  160. var page = _PDFRenderer_loadedPages[pageHandle - 1];
  161. var viewport = page.getViewport(scale);
  162. var canvas = document.createElement('canvas');
  163. var context = canvas.getContext('2d');
  164. context.canvas.width = viewport.width;
  165. context.canvas.height = viewport.height;
  166. var renderContext =
  167. {
  168. canvasContext: context,
  169. viewport: viewport
  170. };
  171. _PDFRenderer_workers[promiseHandle] = page.render(renderContext).then(function()
  172. {
  173. var assignedIndex = -1;
  174. var length = _PDFRenderer_loadedCanvas.length;
  175. for (var i = 0; i < length; i++)
  176. {
  177. if (typeof _PDFRenderer_loadedCanvas[i] == 'undefined')
  178. {
  179. _PDFRenderer_loadedCanvas[i] = canvas;
  180. assignedIndex = i;
  181. break;
  182. }
  183. }
  184. if (assignedIndex == -1)
  185. {
  186. assignedIndex = _PDFRenderer_loadedCanvas.push(canvas) - 1;
  187. }
  188. delete _PDFRenderer_workers[promiseHandle];
  189. SendMessage("WebGL_JSRuntime", "OnPromiseThen", "promiseHandle: " + promiseHandleString + " objectHandle: " + (assignedIndex + 1).toString());
  190. },function(reason)
  191. {
  192. delete _PDFRenderer_workers[promiseHandle];
  193. SendMessage("WebGL_JSRuntime", "OnPromiseCatch", "promiseHandle: " + promiseHandleString + " objectHandle: " + "0");
  194. }).internalRenderTask;
  195. },
  196. PDFJS_RenderCanvasIntoTexture__deps: ['PDFRenderer_loadedCanvas'],
  197. PDFJS_RenderCanvasIntoTexture: function(canvasHandle, textureHandle)
  198. {
  199. var canvas = _PDFRenderer_loadedCanvas[canvasHandle - 1];
  200. GLctx.pixelStorei(GLctx.UNPACK_FLIP_Y_WEBGL, true);
  201. GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[textureHandle]);
  202. GLctx.texImage2D(GLctx.TEXTURE_2D, 0, GLctx.RGBA, GLctx.RGBA, GLctx.UNSIGNED_BYTE, canvas);
  203. GLctx.bindTexture(GLctx.TEXTURE_2D, null);
  204. GLctx.pixelStorei(GLctx.UNPACK_FLIP_Y_WEBGL, false);
  205. },
  206. PDFJS_DestroyCanvas__deps: ['PDFRenderer_loadedCanvas'],
  207. PDFJS_DestroyCanvas: function(canvasHandle)
  208. {
  209. var canvas = _PDFRenderer_loadedCanvas[canvasHandle - 1];
  210. delete canvas;
  211. _PDFRenderer_loadedCanvas[canvasHandle - 1] = undefined;
  212. },
  213. PDFJS_GetPageWidth__deps: ['PDFRenderer_loadedPages'],
  214. PDFJS_GetPageWidth: function(pageHandle, scale)
  215. {
  216. return _PDFRenderer_loadedPages[pageHandle - 1].getViewport(scale).width;
  217. },
  218. PDFJS_GetPageHeight__deps: ['PDFRenderer_loadedPages'],
  219. PDFJS_GetPageHeight: function(pageHandle, scale)
  220. {
  221. return _PDFRenderer_loadedPages[pageHandle - 1].getViewport(scale).height;
  222. }
  223. };
  224. mergeInto(LibraryManager.library, PDFRenderer);