pdfrenderer.jslib 8.5 KB

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