pdf_viewer.js 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128
  1. /* Copyright 2014 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. (function (root, factory) {
  16. 'use strict';
  17. if (typeof define === 'function' && define.amd) {
  18. define('pdfjs-dist/web/pdf_viewer', [
  19. 'exports',
  20. 'pdfjs-dist/build/pdf'
  21. ], factory);
  22. } else if (typeof exports !== 'undefined') {
  23. factory(exports, require('../build/pdf.js'));
  24. } else {
  25. factory(root.pdfjsDistWebPDFViewer = {}, root.pdfjsDistBuildPdf);
  26. }
  27. }(this, function (exports, pdfjsLib) {
  28. 'use strict';
  29. var pdfViewerLibs = { pdfjsWebPDFJS: pdfjsLib };
  30. (function () {
  31. (function (root, factory) {
  32. factory(root.pdfjsWebPDFRenderingQueue = {});
  33. }(this, function (exports) {
  34. var CLEANUP_TIMEOUT = 30000;
  35. var RenderingStates = {
  36. INITIAL: 0,
  37. RUNNING: 1,
  38. PAUSED: 2,
  39. FINISHED: 3
  40. };
  41. var PDFRenderingQueue = function PDFRenderingQueueClosure() {
  42. function PDFRenderingQueue() {
  43. this.pdfViewer = null;
  44. this.pdfThumbnailViewer = null;
  45. this.onIdle = null;
  46. this.highestPriorityPage = null;
  47. this.idleTimeout = null;
  48. this.printing = false;
  49. this.isThumbnailViewEnabled = false;
  50. }
  51. PDFRenderingQueue.prototype = {
  52. setViewer: function PDFRenderingQueue_setViewer(pdfViewer) {
  53. this.pdfViewer = pdfViewer;
  54. },
  55. setThumbnailViewer: function PDFRenderingQueue_setThumbnailViewer(pdfThumbnailViewer) {
  56. this.pdfThumbnailViewer = pdfThumbnailViewer;
  57. },
  58. isHighestPriority: function PDFRenderingQueue_isHighestPriority(view) {
  59. return this.highestPriorityPage === view.renderingId;
  60. },
  61. renderHighestPriority: function PDFRenderingQueue_renderHighestPriority(currentlyVisiblePages) {
  62. if (this.idleTimeout) {
  63. clearTimeout(this.idleTimeout);
  64. this.idleTimeout = null;
  65. }
  66. if (this.pdfViewer.forceRendering(currentlyVisiblePages)) {
  67. return;
  68. }
  69. if (this.pdfThumbnailViewer && this.isThumbnailViewEnabled) {
  70. if (this.pdfThumbnailViewer.forceRendering()) {
  71. return;
  72. }
  73. }
  74. if (this.printing) {
  75. return;
  76. }
  77. if (this.onIdle) {
  78. this.idleTimeout = setTimeout(this.onIdle.bind(this), CLEANUP_TIMEOUT);
  79. }
  80. },
  81. getHighestPriority: function PDFRenderingQueue_getHighestPriority(visible, views, scrolledDown) {
  82. var visibleViews = visible.views;
  83. var numVisible = visibleViews.length;
  84. if (numVisible === 0) {
  85. return false;
  86. }
  87. for (var i = 0; i < numVisible; ++i) {
  88. var view = visibleViews[i].view;
  89. if (!this.isViewFinished(view)) {
  90. return view;
  91. }
  92. }
  93. if (scrolledDown) {
  94. var nextPageIndex = visible.last.id;
  95. if (views[nextPageIndex] && !this.isViewFinished(views[nextPageIndex])) {
  96. return views[nextPageIndex];
  97. }
  98. } else {
  99. var previousPageIndex = visible.first.id - 2;
  100. if (views[previousPageIndex] && !this.isViewFinished(views[previousPageIndex])) {
  101. return views[previousPageIndex];
  102. }
  103. }
  104. return null;
  105. },
  106. isViewFinished: function PDFRenderingQueue_isViewFinished(view) {
  107. return view.renderingState === RenderingStates.FINISHED;
  108. },
  109. renderView: function PDFRenderingQueue_renderView(view) {
  110. var state = view.renderingState;
  111. switch (state) {
  112. case RenderingStates.FINISHED:
  113. return false;
  114. case RenderingStates.PAUSED:
  115. this.highestPriorityPage = view.renderingId;
  116. view.resume();
  117. break;
  118. case RenderingStates.RUNNING:
  119. this.highestPriorityPage = view.renderingId;
  120. break;
  121. case RenderingStates.INITIAL:
  122. this.highestPriorityPage = view.renderingId;
  123. var continueRendering = function () {
  124. this.renderHighestPriority();
  125. }.bind(this);
  126. view.draw().then(continueRendering, continueRendering);
  127. break;
  128. }
  129. return true;
  130. }
  131. };
  132. return PDFRenderingQueue;
  133. }();
  134. exports.RenderingStates = RenderingStates;
  135. exports.PDFRenderingQueue = PDFRenderingQueue;
  136. }));
  137. (function (root, factory) {
  138. factory(root.pdfjsWebDownloadManager = {}, root.pdfjsWebPDFJS);
  139. }(this, function (exports, pdfjsLib) {
  140. function download(blobUrl, filename) {
  141. var a = document.createElement('a');
  142. if (a.click) {
  143. a.href = blobUrl;
  144. a.target = '_parent';
  145. if ('download' in a) {
  146. a.download = filename;
  147. }
  148. (document.body || document.documentElement).appendChild(a);
  149. a.click();
  150. a.parentNode.removeChild(a);
  151. } else {
  152. if (window.top === window && blobUrl.split('#')[0] === window.location.href.split('#')[0]) {
  153. var padCharacter = blobUrl.indexOf('?') === -1 ? '?' : '&';
  154. blobUrl = blobUrl.replace(/#|$/, padCharacter + '$&');
  155. }
  156. window.open(blobUrl, '_parent');
  157. }
  158. }
  159. function DownloadManager() {
  160. }
  161. DownloadManager.prototype = {
  162. downloadUrl: function DownloadManager_downloadUrl(url, filename) {
  163. if (!pdfjsLib.createValidAbsoluteUrl(url, 'http://example.com')) {
  164. return;
  165. }
  166. download(url + '#pdfjs.action=download', filename);
  167. },
  168. downloadData: function DownloadManager_downloadData(data, filename, contentType) {
  169. if (navigator.msSaveBlob) {
  170. return navigator.msSaveBlob(new Blob([data], { type: contentType }), filename);
  171. }
  172. var blobUrl = pdfjsLib.createObjectURL(data, contentType, pdfjsLib.PDFJS.disableCreateObjectURL);
  173. download(blobUrl, filename);
  174. },
  175. download: function DownloadManager_download(blob, url, filename) {
  176. if (!URL) {
  177. this.downloadUrl(url, filename);
  178. return;
  179. }
  180. if (navigator.msSaveBlob) {
  181. if (!navigator.msSaveBlob(blob, filename)) {
  182. this.downloadUrl(url, filename);
  183. }
  184. return;
  185. }
  186. var blobUrl = URL.createObjectURL(blob);
  187. download(blobUrl, filename);
  188. }
  189. };
  190. exports.DownloadManager = DownloadManager;
  191. }));
  192. (function (root, factory) {
  193. factory(root.pdfjsWebUIUtils = {}, root.pdfjsWebPDFJS);
  194. }(this, function (exports, pdfjsLib) {
  195. var CSS_UNITS = 96.0 / 72.0;
  196. var DEFAULT_SCALE_VALUE = 'auto';
  197. var DEFAULT_SCALE = 1.0;
  198. var MIN_SCALE = 0.25;
  199. var MAX_SCALE = 10.0;
  200. var UNKNOWN_SCALE = 0;
  201. var MAX_AUTO_SCALE = 1.25;
  202. var SCROLLBAR_PADDING = 40;
  203. var VERTICAL_PADDING = 5;
  204. var mozL10n = document.mozL10n || document.webL10n;
  205. var PDFJS = pdfjsLib.PDFJS;
  206. PDFJS.disableFullscreen = PDFJS.disableFullscreen === undefined ? false : PDFJS.disableFullscreen;
  207. PDFJS.useOnlyCssZoom = PDFJS.useOnlyCssZoom === undefined ? false : PDFJS.useOnlyCssZoom;
  208. PDFJS.maxCanvasPixels = PDFJS.maxCanvasPixels === undefined ? 16777216 : PDFJS.maxCanvasPixels;
  209. PDFJS.disableHistory = PDFJS.disableHistory === undefined ? false : PDFJS.disableHistory;
  210. PDFJS.disableTextLayer = PDFJS.disableTextLayer === undefined ? false : PDFJS.disableTextLayer;
  211. PDFJS.ignoreCurrentPositionOnZoom = PDFJS.ignoreCurrentPositionOnZoom === undefined ? false : PDFJS.ignoreCurrentPositionOnZoom;
  212. PDFJS.locale = PDFJS.locale === undefined ? navigator.language : PDFJS.locale;
  213. function getOutputScale(ctx) {
  214. var devicePixelRatio = window.devicePixelRatio || 1;
  215. var backingStoreRatio = ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.msBackingStorePixelRatio || ctx.oBackingStorePixelRatio || ctx.backingStorePixelRatio || 1;
  216. var pixelRatio = devicePixelRatio / backingStoreRatio;
  217. return {
  218. sx: pixelRatio,
  219. sy: pixelRatio,
  220. scaled: pixelRatio !== 1
  221. };
  222. }
  223. function scrollIntoView(element, spot, skipOverflowHiddenElements) {
  224. var parent = element.offsetParent;
  225. if (!parent) {
  226. console.error('offsetParent is not set -- cannot scroll');
  227. return;
  228. }
  229. var checkOverflow = skipOverflowHiddenElements || false;
  230. var offsetY = element.offsetTop + element.clientTop;
  231. var offsetX = element.offsetLeft + element.clientLeft;
  232. while (parent.clientHeight === parent.scrollHeight || checkOverflow && getComputedStyle(parent).overflow === 'hidden') {
  233. if (parent.dataset._scaleY) {
  234. offsetY /= parent.dataset._scaleY;
  235. offsetX /= parent.dataset._scaleX;
  236. }
  237. offsetY += parent.offsetTop;
  238. offsetX += parent.offsetLeft;
  239. parent = parent.offsetParent;
  240. if (!parent) {
  241. return;
  242. }
  243. }
  244. if (spot) {
  245. if (spot.top !== undefined) {
  246. offsetY += spot.top;
  247. }
  248. if (spot.left !== undefined) {
  249. offsetX += spot.left;
  250. parent.scrollLeft = offsetX;
  251. }
  252. }
  253. parent.scrollTop = offsetY;
  254. }
  255. function watchScroll(viewAreaElement, callback) {
  256. var debounceScroll = function debounceScroll(evt) {
  257. if (rAF) {
  258. return;
  259. }
  260. rAF = window.requestAnimationFrame(function viewAreaElementScrolled() {
  261. rAF = null;
  262. var currentY = viewAreaElement.scrollTop;
  263. var lastY = state.lastY;
  264. if (currentY !== lastY) {
  265. state.down = currentY > lastY;
  266. }
  267. state.lastY = currentY;
  268. callback(state);
  269. });
  270. };
  271. var state = {
  272. down: true,
  273. lastY: viewAreaElement.scrollTop,
  274. _eventHandler: debounceScroll
  275. };
  276. var rAF = null;
  277. viewAreaElement.addEventListener('scroll', debounceScroll, true);
  278. return state;
  279. }
  280. function parseQueryString(query) {
  281. var parts = query.split('&');
  282. var params = {};
  283. for (var i = 0, ii = parts.length; i < ii; ++i) {
  284. var param = parts[i].split('=');
  285. var key = param[0].toLowerCase();
  286. var value = param.length > 1 ? param[1] : null;
  287. params[decodeURIComponent(key)] = decodeURIComponent(value);
  288. }
  289. return params;
  290. }
  291. function binarySearchFirstItem(items, condition) {
  292. var minIndex = 0;
  293. var maxIndex = items.length - 1;
  294. if (items.length === 0 || !condition(items[maxIndex])) {
  295. return items.length;
  296. }
  297. if (condition(items[minIndex])) {
  298. return minIndex;
  299. }
  300. while (minIndex < maxIndex) {
  301. var currentIndex = minIndex + maxIndex >> 1;
  302. var currentItem = items[currentIndex];
  303. if (condition(currentItem)) {
  304. maxIndex = currentIndex;
  305. } else {
  306. minIndex = currentIndex + 1;
  307. }
  308. }
  309. return minIndex;
  310. }
  311. function approximateFraction(x) {
  312. if (Math.floor(x) === x) {
  313. return [
  314. x,
  315. 1
  316. ];
  317. }
  318. var xinv = 1 / x;
  319. var limit = 8;
  320. if (xinv > limit) {
  321. return [
  322. 1,
  323. limit
  324. ];
  325. } else if (Math.floor(xinv) === xinv) {
  326. return [
  327. 1,
  328. xinv
  329. ];
  330. }
  331. var x_ = x > 1 ? xinv : x;
  332. var a = 0, b = 1, c = 1, d = 1;
  333. while (true) {
  334. var p = a + c, q = b + d;
  335. if (q > limit) {
  336. break;
  337. }
  338. if (x_ <= p / q) {
  339. c = p;
  340. d = q;
  341. } else {
  342. a = p;
  343. b = q;
  344. }
  345. }
  346. if (x_ - a / b < c / d - x_) {
  347. return x_ === x ? [
  348. a,
  349. b
  350. ] : [
  351. b,
  352. a
  353. ];
  354. } else {
  355. return x_ === x ? [
  356. c,
  357. d
  358. ] : [
  359. d,
  360. c
  361. ];
  362. }
  363. }
  364. function roundToDivide(x, div) {
  365. var r = x % div;
  366. return r === 0 ? x : Math.round(x - r + div);
  367. }
  368. function getVisibleElements(scrollEl, views, sortByVisibility) {
  369. var top = scrollEl.scrollTop, bottom = top + scrollEl.clientHeight;
  370. var left = scrollEl.scrollLeft, right = left + scrollEl.clientWidth;
  371. function isElementBottomBelowViewTop(view) {
  372. var element = view.div;
  373. var elementBottom = element.offsetTop + element.clientTop + element.clientHeight;
  374. return elementBottom > top;
  375. }
  376. var visible = [], view, element;
  377. var currentHeight, viewHeight, hiddenHeight, percentHeight;
  378. var currentWidth, viewWidth;
  379. var firstVisibleElementInd = views.length === 0 ? 0 : binarySearchFirstItem(views, isElementBottomBelowViewTop);
  380. for (var i = firstVisibleElementInd, ii = views.length; i < ii; i++) {
  381. view = views[i];
  382. element = view.div;
  383. currentHeight = element.offsetTop + element.clientTop;
  384. viewHeight = element.clientHeight;
  385. if (currentHeight > bottom) {
  386. break;
  387. }
  388. currentWidth = element.offsetLeft + element.clientLeft;
  389. viewWidth = element.clientWidth;
  390. if (currentWidth + viewWidth < left || currentWidth > right) {
  391. continue;
  392. }
  393. hiddenHeight = Math.max(0, top - currentHeight) + Math.max(0, currentHeight + viewHeight - bottom);
  394. percentHeight = (viewHeight - hiddenHeight) * 100 / viewHeight | 0;
  395. visible.push({
  396. id: view.id,
  397. x: currentWidth,
  398. y: currentHeight,
  399. view: view,
  400. percent: percentHeight
  401. });
  402. }
  403. var first = visible[0];
  404. var last = visible[visible.length - 1];
  405. if (sortByVisibility) {
  406. visible.sort(function (a, b) {
  407. var pc = a.percent - b.percent;
  408. if (Math.abs(pc) > 0.001) {
  409. return -pc;
  410. }
  411. return a.id - b.id;
  412. });
  413. }
  414. return {
  415. first: first,
  416. last: last,
  417. views: visible
  418. };
  419. }
  420. function noContextMenuHandler(e) {
  421. e.preventDefault();
  422. }
  423. function getPDFFileNameFromURL(url) {
  424. var reURI = /^(?:([^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
  425. var reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
  426. var splitURI = reURI.exec(url);
  427. var suggestedFilename = reFilename.exec(splitURI[1]) || reFilename.exec(splitURI[2]) || reFilename.exec(splitURI[3]);
  428. if (suggestedFilename) {
  429. suggestedFilename = suggestedFilename[0];
  430. if (suggestedFilename.indexOf('%') !== -1) {
  431. try {
  432. suggestedFilename = reFilename.exec(decodeURIComponent(suggestedFilename))[0];
  433. } catch (e) {
  434. }
  435. }
  436. }
  437. return suggestedFilename || 'document.pdf';
  438. }
  439. function normalizeWheelEventDelta(evt) {
  440. var delta = Math.sqrt(evt.deltaX * evt.deltaX + evt.deltaY * evt.deltaY);
  441. var angle = Math.atan2(evt.deltaY, evt.deltaX);
  442. if (-0.25 * Math.PI < angle && angle < 0.75 * Math.PI) {
  443. delta = -delta;
  444. }
  445. var MOUSE_DOM_DELTA_PIXEL_MODE = 0;
  446. var MOUSE_DOM_DELTA_LINE_MODE = 1;
  447. var MOUSE_PIXELS_PER_LINE = 30;
  448. var MOUSE_LINES_PER_PAGE = 30;
  449. if (evt.deltaMode === MOUSE_DOM_DELTA_PIXEL_MODE) {
  450. delta /= MOUSE_PIXELS_PER_LINE * MOUSE_LINES_PER_PAGE;
  451. } else if (evt.deltaMode === MOUSE_DOM_DELTA_LINE_MODE) {
  452. delta /= MOUSE_LINES_PER_PAGE;
  453. }
  454. return delta;
  455. }
  456. var animationStarted = new Promise(function (resolve) {
  457. window.requestAnimationFrame(resolve);
  458. });
  459. var localized = new Promise(function (resolve, reject) {
  460. if (!mozL10n) {
  461. reject(new Error('mozL10n service is not available.'));
  462. return;
  463. }
  464. if (mozL10n.getReadyState() !== 'loading') {
  465. resolve();
  466. return;
  467. }
  468. window.addEventListener('localized', function localized(evt) {
  469. resolve();
  470. });
  471. });
  472. var EventBus = function EventBusClosure() {
  473. function EventBus() {
  474. this._listeners = Object.create(null);
  475. }
  476. EventBus.prototype = {
  477. on: function EventBus_on(eventName, listener) {
  478. var eventListeners = this._listeners[eventName];
  479. if (!eventListeners) {
  480. eventListeners = [];
  481. this._listeners[eventName] = eventListeners;
  482. }
  483. eventListeners.push(listener);
  484. },
  485. off: function EventBus_on(eventName, listener) {
  486. var eventListeners = this._listeners[eventName];
  487. var i;
  488. if (!eventListeners || (i = eventListeners.indexOf(listener)) < 0) {
  489. return;
  490. }
  491. eventListeners.splice(i, 1);
  492. },
  493. dispatch: function EventBus_dispath(eventName) {
  494. var eventListeners = this._listeners[eventName];
  495. if (!eventListeners || eventListeners.length === 0) {
  496. return;
  497. }
  498. var args = Array.prototype.slice.call(arguments, 1);
  499. eventListeners.slice(0).forEach(function (listener) {
  500. listener.apply(null, args);
  501. });
  502. }
  503. };
  504. return EventBus;
  505. }();
  506. var ProgressBar = function ProgressBarClosure() {
  507. function clamp(v, min, max) {
  508. return Math.min(Math.max(v, min), max);
  509. }
  510. function ProgressBar(id, opts) {
  511. this.visible = true;
  512. this.div = document.querySelector(id + ' .progress');
  513. this.bar = this.div.parentNode;
  514. this.height = opts.height || 100;
  515. this.width = opts.width || 100;
  516. this.units = opts.units || '%';
  517. this.div.style.height = this.height + this.units;
  518. this.percent = 0;
  519. }
  520. ProgressBar.prototype = {
  521. updateBar: function ProgressBar_updateBar() {
  522. if (this._indeterminate) {
  523. this.div.classList.add('indeterminate');
  524. this.div.style.width = this.width + this.units;
  525. return;
  526. }
  527. this.div.classList.remove('indeterminate');
  528. var progressSize = this.width * this._percent / 100;
  529. this.div.style.width = progressSize + this.units;
  530. },
  531. get percent() {
  532. return this._percent;
  533. },
  534. set percent(val) {
  535. this._indeterminate = isNaN(val);
  536. this._percent = clamp(val, 0, 100);
  537. this.updateBar();
  538. },
  539. setWidth: function ProgressBar_setWidth(viewer) {
  540. if (viewer) {
  541. var container = viewer.parentNode;
  542. var scrollbarWidth = container.offsetWidth - viewer.offsetWidth;
  543. if (scrollbarWidth > 0) {
  544. this.bar.setAttribute('style', 'width: calc(100% - ' + scrollbarWidth + 'px);');
  545. }
  546. }
  547. },
  548. hide: function ProgressBar_hide() {
  549. if (!this.visible) {
  550. return;
  551. }
  552. this.visible = false;
  553. this.bar.classList.add('hidden');
  554. document.body.classList.remove('loadingInProgress');
  555. },
  556. show: function ProgressBar_show() {
  557. if (this.visible) {
  558. return;
  559. }
  560. this.visible = true;
  561. document.body.classList.add('loadingInProgress');
  562. this.bar.classList.remove('hidden');
  563. }
  564. };
  565. return ProgressBar;
  566. }();
  567. exports.CSS_UNITS = CSS_UNITS;
  568. exports.DEFAULT_SCALE_VALUE = DEFAULT_SCALE_VALUE;
  569. exports.DEFAULT_SCALE = DEFAULT_SCALE;
  570. exports.MIN_SCALE = MIN_SCALE;
  571. exports.MAX_SCALE = MAX_SCALE;
  572. exports.UNKNOWN_SCALE = UNKNOWN_SCALE;
  573. exports.MAX_AUTO_SCALE = MAX_AUTO_SCALE;
  574. exports.SCROLLBAR_PADDING = SCROLLBAR_PADDING;
  575. exports.VERTICAL_PADDING = VERTICAL_PADDING;
  576. exports.mozL10n = mozL10n;
  577. exports.EventBus = EventBus;
  578. exports.ProgressBar = ProgressBar;
  579. exports.getPDFFileNameFromURL = getPDFFileNameFromURL;
  580. exports.noContextMenuHandler = noContextMenuHandler;
  581. exports.parseQueryString = parseQueryString;
  582. exports.getVisibleElements = getVisibleElements;
  583. exports.roundToDivide = roundToDivide;
  584. exports.approximateFraction = approximateFraction;
  585. exports.getOutputScale = getOutputScale;
  586. exports.scrollIntoView = scrollIntoView;
  587. exports.watchScroll = watchScroll;
  588. exports.binarySearchFirstItem = binarySearchFirstItem;
  589. exports.normalizeWheelEventDelta = normalizeWheelEventDelta;
  590. exports.animationStarted = animationStarted;
  591. exports.localized = localized;
  592. }));
  593. (function (root, factory) {
  594. factory(root.pdfjsWebDOMEvents = {}, root.pdfjsWebUIUtils);
  595. }(this, function (exports, uiUtils) {
  596. var EventBus = uiUtils.EventBus;
  597. function attachDOMEventsToEventBus(eventBus) {
  598. eventBus.on('documentload', function () {
  599. var event = document.createEvent('CustomEvent');
  600. event.initCustomEvent('documentload', true, true, {});
  601. window.dispatchEvent(event);
  602. });
  603. eventBus.on('pagerendered', function (e) {
  604. var event = document.createEvent('CustomEvent');
  605. event.initCustomEvent('pagerendered', true, true, {
  606. pageNumber: e.pageNumber,
  607. cssTransform: e.cssTransform
  608. });
  609. e.source.div.dispatchEvent(event);
  610. });
  611. eventBus.on('textlayerrendered', function (e) {
  612. var event = document.createEvent('CustomEvent');
  613. event.initCustomEvent('textlayerrendered', true, true, { pageNumber: e.pageNumber });
  614. e.source.textLayerDiv.dispatchEvent(event);
  615. });
  616. eventBus.on('pagechange', function (e) {
  617. var event = document.createEvent('UIEvents');
  618. event.initUIEvent('pagechange', true, true, window, 0);
  619. event.pageNumber = e.pageNumber;
  620. e.source.container.dispatchEvent(event);
  621. });
  622. eventBus.on('pagesinit', function (e) {
  623. var event = document.createEvent('CustomEvent');
  624. event.initCustomEvent('pagesinit', true, true, null);
  625. e.source.container.dispatchEvent(event);
  626. });
  627. eventBus.on('pagesloaded', function (e) {
  628. var event = document.createEvent('CustomEvent');
  629. event.initCustomEvent('pagesloaded', true, true, { pagesCount: e.pagesCount });
  630. e.source.container.dispatchEvent(event);
  631. });
  632. eventBus.on('scalechange', function (e) {
  633. var event = document.createEvent('UIEvents');
  634. event.initUIEvent('scalechange', true, true, window, 0);
  635. event.scale = e.scale;
  636. event.presetValue = e.presetValue;
  637. e.source.container.dispatchEvent(event);
  638. });
  639. eventBus.on('updateviewarea', function (e) {
  640. var event = document.createEvent('UIEvents');
  641. event.initUIEvent('updateviewarea', true, true, window, 0);
  642. event.location = e.location;
  643. e.source.container.dispatchEvent(event);
  644. });
  645. eventBus.on('find', function (e) {
  646. if (e.source === window) {
  647. return;
  648. }
  649. var event = document.createEvent('CustomEvent');
  650. event.initCustomEvent('find' + e.type, true, true, {
  651. query: e.query,
  652. phraseSearch: e.phraseSearch,
  653. caseSensitive: e.caseSensitive,
  654. highlightAll: e.highlightAll,
  655. findPrevious: e.findPrevious
  656. });
  657. window.dispatchEvent(event);
  658. });
  659. eventBus.on('attachmentsloaded', function (e) {
  660. var event = document.createEvent('CustomEvent');
  661. event.initCustomEvent('attachmentsloaded', true, true, { attachmentsCount: e.attachmentsCount });
  662. e.source.container.dispatchEvent(event);
  663. });
  664. eventBus.on('sidebarviewchanged', function (e) {
  665. var event = document.createEvent('CustomEvent');
  666. event.initCustomEvent('sidebarviewchanged', true, true, { view: e.view });
  667. e.source.outerContainer.dispatchEvent(event);
  668. });
  669. eventBus.on('pagemode', function (e) {
  670. var event = document.createEvent('CustomEvent');
  671. event.initCustomEvent('pagemode', true, true, { mode: e.mode });
  672. e.source.pdfViewer.container.dispatchEvent(event);
  673. });
  674. eventBus.on('namedaction', function (e) {
  675. var event = document.createEvent('CustomEvent');
  676. event.initCustomEvent('namedaction', true, true, { action: e.action });
  677. e.source.pdfViewer.container.dispatchEvent(event);
  678. });
  679. eventBus.on('presentationmodechanged', function (e) {
  680. var event = document.createEvent('CustomEvent');
  681. event.initCustomEvent('presentationmodechanged', true, true, {
  682. active: e.active,
  683. switchInProgress: e.switchInProgress
  684. });
  685. window.dispatchEvent(event);
  686. });
  687. eventBus.on('outlineloaded', function (e) {
  688. var event = document.createEvent('CustomEvent');
  689. event.initCustomEvent('outlineloaded', true, true, { outlineCount: e.outlineCount });
  690. e.source.container.dispatchEvent(event);
  691. });
  692. }
  693. var globalEventBus = null;
  694. function getGlobalEventBus() {
  695. if (globalEventBus) {
  696. return globalEventBus;
  697. }
  698. globalEventBus = new EventBus();
  699. attachDOMEventsToEventBus(globalEventBus);
  700. return globalEventBus;
  701. }
  702. exports.attachDOMEventsToEventBus = attachDOMEventsToEventBus;
  703. exports.getGlobalEventBus = getGlobalEventBus;
  704. }));
  705. (function (root, factory) {
  706. factory(root.pdfjsWebPDFFindController = {}, root.pdfjsWebUIUtils);
  707. }(this, function (exports, uiUtils) {
  708. var scrollIntoView = uiUtils.scrollIntoView;
  709. var FindStates = {
  710. FIND_FOUND: 0,
  711. FIND_NOTFOUND: 1,
  712. FIND_WRAPPED: 2,
  713. FIND_PENDING: 3
  714. };
  715. var FIND_SCROLL_OFFSET_TOP = -50;
  716. var FIND_SCROLL_OFFSET_LEFT = -400;
  717. var CHARACTERS_TO_NORMALIZE = {
  718. '\u2018': '\'',
  719. '\u2019': '\'',
  720. '\u201A': '\'',
  721. '\u201B': '\'',
  722. '\u201C': '"',
  723. '\u201D': '"',
  724. '\u201E': '"',
  725. '\u201F': '"',
  726. '\u00BC': '1/4',
  727. '\u00BD': '1/2',
  728. '\u00BE': '3/4'
  729. };
  730. var PDFFindController = function PDFFindControllerClosure() {
  731. function PDFFindController(options) {
  732. this.pdfViewer = options.pdfViewer || null;
  733. this.onUpdateResultsCount = null;
  734. this.onUpdateState = null;
  735. this.reset();
  736. var replace = Object.keys(CHARACTERS_TO_NORMALIZE).join('');
  737. this.normalizationRegex = new RegExp('[' + replace + ']', 'g');
  738. }
  739. PDFFindController.prototype = {
  740. reset: function PDFFindController_reset() {
  741. this.startedTextExtraction = false;
  742. this.extractTextPromises = [];
  743. this.pendingFindMatches = Object.create(null);
  744. this.active = false;
  745. this.pageContents = [];
  746. this.pageMatches = [];
  747. this.pageMatchesLength = null;
  748. this.matchCount = 0;
  749. this.selected = {
  750. pageIdx: -1,
  751. matchIdx: -1
  752. };
  753. this.offset = {
  754. pageIdx: null,
  755. matchIdx: null
  756. };
  757. this.pagesToSearch = null;
  758. this.resumePageIdx = null;
  759. this.state = null;
  760. this.dirtyMatch = false;
  761. this.findTimeout = null;
  762. this.firstPagePromise = new Promise(function (resolve) {
  763. this.resolveFirstPage = resolve;
  764. }.bind(this));
  765. },
  766. normalize: function PDFFindController_normalize(text) {
  767. return text.replace(this.normalizationRegex, function (ch) {
  768. return CHARACTERS_TO_NORMALIZE[ch];
  769. });
  770. },
  771. _prepareMatches: function PDFFindController_prepareMatches(matchesWithLength, matches, matchesLength) {
  772. function isSubTerm(matchesWithLength, currentIndex) {
  773. var currentElem, prevElem, nextElem;
  774. currentElem = matchesWithLength[currentIndex];
  775. nextElem = matchesWithLength[currentIndex + 1];
  776. if (currentIndex < matchesWithLength.length - 1 && currentElem.match === nextElem.match) {
  777. currentElem.skipped = true;
  778. return true;
  779. }
  780. for (var i = currentIndex - 1; i >= 0; i--) {
  781. prevElem = matchesWithLength[i];
  782. if (prevElem.skipped) {
  783. continue;
  784. }
  785. if (prevElem.match + prevElem.matchLength < currentElem.match) {
  786. break;
  787. }
  788. if (prevElem.match + prevElem.matchLength >= currentElem.match + currentElem.matchLength) {
  789. currentElem.skipped = true;
  790. return true;
  791. }
  792. }
  793. return false;
  794. }
  795. var i, len;
  796. matchesWithLength.sort(function (a, b) {
  797. return a.match === b.match ? a.matchLength - b.matchLength : a.match - b.match;
  798. });
  799. for (i = 0, len = matchesWithLength.length; i < len; i++) {
  800. if (isSubTerm(matchesWithLength, i)) {
  801. continue;
  802. }
  803. matches.push(matchesWithLength[i].match);
  804. matchesLength.push(matchesWithLength[i].matchLength);
  805. }
  806. },
  807. calcFindPhraseMatch: function PDFFindController_calcFindPhraseMatch(query, pageIndex, pageContent) {
  808. var matches = [];
  809. var queryLen = query.length;
  810. var matchIdx = -queryLen;
  811. while (true) {
  812. matchIdx = pageContent.indexOf(query, matchIdx + queryLen);
  813. if (matchIdx === -1) {
  814. break;
  815. }
  816. matches.push(matchIdx);
  817. }
  818. this.pageMatches[pageIndex] = matches;
  819. },
  820. calcFindWordMatch: function PDFFindController_calcFindWordMatch(query, pageIndex, pageContent) {
  821. var matchesWithLength = [];
  822. var queryArray = query.match(/\S+/g);
  823. var subquery, subqueryLen, matchIdx;
  824. for (var i = 0, len = queryArray.length; i < len; i++) {
  825. subquery = queryArray[i];
  826. subqueryLen = subquery.length;
  827. matchIdx = -subqueryLen;
  828. while (true) {
  829. matchIdx = pageContent.indexOf(subquery, matchIdx + subqueryLen);
  830. if (matchIdx === -1) {
  831. break;
  832. }
  833. matchesWithLength.push({
  834. match: matchIdx,
  835. matchLength: subqueryLen,
  836. skipped: false
  837. });
  838. }
  839. }
  840. if (!this.pageMatchesLength) {
  841. this.pageMatchesLength = [];
  842. }
  843. this.pageMatchesLength[pageIndex] = [];
  844. this.pageMatches[pageIndex] = [];
  845. this._prepareMatches(matchesWithLength, this.pageMatches[pageIndex], this.pageMatchesLength[pageIndex]);
  846. },
  847. calcFindMatch: function PDFFindController_calcFindMatch(pageIndex) {
  848. var pageContent = this.normalize(this.pageContents[pageIndex]);
  849. var query = this.normalize(this.state.query);
  850. var caseSensitive = this.state.caseSensitive;
  851. var phraseSearch = this.state.phraseSearch;
  852. var queryLen = query.length;
  853. if (queryLen === 0) {
  854. return;
  855. }
  856. if (!caseSensitive) {
  857. pageContent = pageContent.toLowerCase();
  858. query = query.toLowerCase();
  859. }
  860. if (phraseSearch) {
  861. this.calcFindPhraseMatch(query, pageIndex, pageContent);
  862. } else {
  863. this.calcFindWordMatch(query, pageIndex, pageContent);
  864. }
  865. this.updatePage(pageIndex);
  866. if (this.resumePageIdx === pageIndex) {
  867. this.resumePageIdx = null;
  868. this.nextPageMatch();
  869. }
  870. if (this.pageMatches[pageIndex].length > 0) {
  871. this.matchCount += this.pageMatches[pageIndex].length;
  872. this.updateUIResultsCount();
  873. }
  874. },
  875. extractText: function PDFFindController_extractText() {
  876. if (this.startedTextExtraction) {
  877. return;
  878. }
  879. this.startedTextExtraction = true;
  880. this.pageContents = [];
  881. var extractTextPromisesResolves = [];
  882. var numPages = this.pdfViewer.pagesCount;
  883. for (var i = 0; i < numPages; i++) {
  884. this.extractTextPromises.push(new Promise(function (resolve) {
  885. extractTextPromisesResolves.push(resolve);
  886. }));
  887. }
  888. var self = this;
  889. function extractPageText(pageIndex) {
  890. self.pdfViewer.getPageTextContent(pageIndex).then(function textContentResolved(textContent) {
  891. var textItems = textContent.items;
  892. var str = [];
  893. for (var i = 0, len = textItems.length; i < len; i++) {
  894. str.push(textItems[i].str);
  895. }
  896. self.pageContents.push(str.join(''));
  897. extractTextPromisesResolves[pageIndex](pageIndex);
  898. if (pageIndex + 1 < self.pdfViewer.pagesCount) {
  899. extractPageText(pageIndex + 1);
  900. }
  901. });
  902. }
  903. extractPageText(0);
  904. },
  905. executeCommand: function PDFFindController_executeCommand(cmd, state) {
  906. if (this.state === null || cmd !== 'findagain') {
  907. this.dirtyMatch = true;
  908. }
  909. this.state = state;
  910. this.updateUIState(FindStates.FIND_PENDING);
  911. this.firstPagePromise.then(function () {
  912. this.extractText();
  913. clearTimeout(this.findTimeout);
  914. if (cmd === 'find') {
  915. this.findTimeout = setTimeout(this.nextMatch.bind(this), 250);
  916. } else {
  917. this.nextMatch();
  918. }
  919. }.bind(this));
  920. },
  921. updatePage: function PDFFindController_updatePage(index) {
  922. if (this.selected.pageIdx === index) {
  923. this.pdfViewer.currentPageNumber = index + 1;
  924. }
  925. var page = this.pdfViewer.getPageView(index);
  926. if (page.textLayer) {
  927. page.textLayer.updateMatches();
  928. }
  929. },
  930. nextMatch: function PDFFindController_nextMatch() {
  931. var previous = this.state.findPrevious;
  932. var currentPageIndex = this.pdfViewer.currentPageNumber - 1;
  933. var numPages = this.pdfViewer.pagesCount;
  934. this.active = true;
  935. if (this.dirtyMatch) {
  936. this.dirtyMatch = false;
  937. this.selected.pageIdx = this.selected.matchIdx = -1;
  938. this.offset.pageIdx = currentPageIndex;
  939. this.offset.matchIdx = null;
  940. this.hadMatch = false;
  941. this.resumePageIdx = null;
  942. this.pageMatches = [];
  943. this.matchCount = 0;
  944. this.pageMatchesLength = null;
  945. var self = this;
  946. for (var i = 0; i < numPages; i++) {
  947. this.updatePage(i);
  948. if (!(i in this.pendingFindMatches)) {
  949. this.pendingFindMatches[i] = true;
  950. this.extractTextPromises[i].then(function (pageIdx) {
  951. delete self.pendingFindMatches[pageIdx];
  952. self.calcFindMatch(pageIdx);
  953. });
  954. }
  955. }
  956. }
  957. if (this.state.query === '') {
  958. this.updateUIState(FindStates.FIND_FOUND);
  959. return;
  960. }
  961. if (this.resumePageIdx) {
  962. return;
  963. }
  964. var offset = this.offset;
  965. this.pagesToSearch = numPages;
  966. if (offset.matchIdx !== null) {
  967. var numPageMatches = this.pageMatches[offset.pageIdx].length;
  968. if (!previous && offset.matchIdx + 1 < numPageMatches || previous && offset.matchIdx > 0) {
  969. this.hadMatch = true;
  970. offset.matchIdx = previous ? offset.matchIdx - 1 : offset.matchIdx + 1;
  971. this.updateMatch(true);
  972. return;
  973. }
  974. this.advanceOffsetPage(previous);
  975. }
  976. this.nextPageMatch();
  977. },
  978. matchesReady: function PDFFindController_matchesReady(matches) {
  979. var offset = this.offset;
  980. var numMatches = matches.length;
  981. var previous = this.state.findPrevious;
  982. if (numMatches) {
  983. this.hadMatch = true;
  984. offset.matchIdx = previous ? numMatches - 1 : 0;
  985. this.updateMatch(true);
  986. return true;
  987. } else {
  988. this.advanceOffsetPage(previous);
  989. if (offset.wrapped) {
  990. offset.matchIdx = null;
  991. if (this.pagesToSearch < 0) {
  992. this.updateMatch(false);
  993. return true;
  994. }
  995. }
  996. return false;
  997. }
  998. },
  999. updateMatchPosition: function PDFFindController_updateMatchPosition(pageIndex, index, elements, beginIdx) {
  1000. if (this.selected.matchIdx === index && this.selected.pageIdx === pageIndex) {
  1001. var spot = {
  1002. top: FIND_SCROLL_OFFSET_TOP,
  1003. left: FIND_SCROLL_OFFSET_LEFT
  1004. };
  1005. scrollIntoView(elements[beginIdx], spot, true);
  1006. }
  1007. },
  1008. nextPageMatch: function PDFFindController_nextPageMatch() {
  1009. if (this.resumePageIdx !== null) {
  1010. console.error('There can only be one pending page.');
  1011. }
  1012. do {
  1013. var pageIdx = this.offset.pageIdx;
  1014. var matches = this.pageMatches[pageIdx];
  1015. if (!matches) {
  1016. this.resumePageIdx = pageIdx;
  1017. break;
  1018. }
  1019. } while (!this.matchesReady(matches));
  1020. },
  1021. advanceOffsetPage: function PDFFindController_advanceOffsetPage(previous) {
  1022. var offset = this.offset;
  1023. var numPages = this.extractTextPromises.length;
  1024. offset.pageIdx = previous ? offset.pageIdx - 1 : offset.pageIdx + 1;
  1025. offset.matchIdx = null;
  1026. this.pagesToSearch--;
  1027. if (offset.pageIdx >= numPages || offset.pageIdx < 0) {
  1028. offset.pageIdx = previous ? numPages - 1 : 0;
  1029. offset.wrapped = true;
  1030. }
  1031. },
  1032. updateMatch: function PDFFindController_updateMatch(found) {
  1033. var state = FindStates.FIND_NOTFOUND;
  1034. var wrapped = this.offset.wrapped;
  1035. this.offset.wrapped = false;
  1036. if (found) {
  1037. var previousPage = this.selected.pageIdx;
  1038. this.selected.pageIdx = this.offset.pageIdx;
  1039. this.selected.matchIdx = this.offset.matchIdx;
  1040. state = wrapped ? FindStates.FIND_WRAPPED : FindStates.FIND_FOUND;
  1041. if (previousPage !== -1 && previousPage !== this.selected.pageIdx) {
  1042. this.updatePage(previousPage);
  1043. }
  1044. }
  1045. this.updateUIState(state, this.state.findPrevious);
  1046. if (this.selected.pageIdx !== -1) {
  1047. this.updatePage(this.selected.pageIdx);
  1048. }
  1049. },
  1050. updateUIResultsCount: function PDFFindController_updateUIResultsCount() {
  1051. if (this.onUpdateResultsCount) {
  1052. this.onUpdateResultsCount(this.matchCount);
  1053. }
  1054. },
  1055. updateUIState: function PDFFindController_updateUIState(state, previous) {
  1056. if (this.onUpdateState) {
  1057. this.onUpdateState(state, previous, this.matchCount);
  1058. }
  1059. }
  1060. };
  1061. return PDFFindController;
  1062. }();
  1063. exports.FindStates = FindStates;
  1064. exports.PDFFindController = PDFFindController;
  1065. }));
  1066. (function (root, factory) {
  1067. factory(root.pdfjsWebPDFHistory = {}, root.pdfjsWebDOMEvents);
  1068. }(this, function (exports, domEvents) {
  1069. function PDFHistory(options) {
  1070. this.linkService = options.linkService;
  1071. this.eventBus = options.eventBus || domEvents.getGlobalEventBus();
  1072. this.initialized = false;
  1073. this.initialDestination = null;
  1074. this.initialBookmark = null;
  1075. }
  1076. PDFHistory.prototype = {
  1077. initialize: function pdfHistoryInitialize(fingerprint) {
  1078. this.initialized = true;
  1079. this.reInitialized = false;
  1080. this.allowHashChange = true;
  1081. this.historyUnlocked = true;
  1082. this.isViewerInPresentationMode = false;
  1083. this.previousHash = window.location.hash.substring(1);
  1084. this.currentBookmark = '';
  1085. this.currentPage = 0;
  1086. this.updatePreviousBookmark = false;
  1087. this.previousBookmark = '';
  1088. this.previousPage = 0;
  1089. this.nextHashParam = '';
  1090. this.fingerprint = fingerprint;
  1091. this.currentUid = this.uid = 0;
  1092. this.current = {};
  1093. var state = window.history.state;
  1094. if (this._isStateObjectDefined(state)) {
  1095. if (state.target.dest) {
  1096. this.initialDestination = state.target.dest;
  1097. } else {
  1098. this.initialBookmark = state.target.hash;
  1099. }
  1100. this.currentUid = state.uid;
  1101. this.uid = state.uid + 1;
  1102. this.current = state.target;
  1103. } else {
  1104. if (state && state.fingerprint && this.fingerprint !== state.fingerprint) {
  1105. this.reInitialized = true;
  1106. }
  1107. this._pushOrReplaceState({ fingerprint: this.fingerprint }, true);
  1108. }
  1109. var self = this;
  1110. window.addEventListener('popstate', function pdfHistoryPopstate(evt) {
  1111. if (!self.historyUnlocked) {
  1112. return;
  1113. }
  1114. if (evt.state) {
  1115. self._goTo(evt.state);
  1116. return;
  1117. }
  1118. if (self.uid === 0) {
  1119. var previousParams = self.previousHash && self.currentBookmark && self.previousHash !== self.currentBookmark ? {
  1120. hash: self.currentBookmark,
  1121. page: self.currentPage
  1122. } : { page: 1 };
  1123. replacePreviousHistoryState(previousParams, function () {
  1124. updateHistoryWithCurrentHash();
  1125. });
  1126. } else {
  1127. updateHistoryWithCurrentHash();
  1128. }
  1129. }, false);
  1130. function updateHistoryWithCurrentHash() {
  1131. self.previousHash = window.location.hash.slice(1);
  1132. self._pushToHistory({ hash: self.previousHash }, false, true);
  1133. self._updatePreviousBookmark();
  1134. }
  1135. function replacePreviousHistoryState(params, callback) {
  1136. self.historyUnlocked = false;
  1137. self.allowHashChange = false;
  1138. window.addEventListener('popstate', rewriteHistoryAfterBack);
  1139. history.back();
  1140. function rewriteHistoryAfterBack() {
  1141. window.removeEventListener('popstate', rewriteHistoryAfterBack);
  1142. window.addEventListener('popstate', rewriteHistoryAfterForward);
  1143. self._pushToHistory(params, false, true);
  1144. history.forward();
  1145. }
  1146. function rewriteHistoryAfterForward() {
  1147. window.removeEventListener('popstate', rewriteHistoryAfterForward);
  1148. self.allowHashChange = true;
  1149. self.historyUnlocked = true;
  1150. callback();
  1151. }
  1152. }
  1153. function pdfHistoryBeforeUnload() {
  1154. var previousParams = self._getPreviousParams(null, true);
  1155. if (previousParams) {
  1156. var replacePrevious = !self.current.dest && self.current.hash !== self.previousHash;
  1157. self._pushToHistory(previousParams, false, replacePrevious);
  1158. self._updatePreviousBookmark();
  1159. }
  1160. window.removeEventListener('beforeunload', pdfHistoryBeforeUnload, false);
  1161. }
  1162. window.addEventListener('beforeunload', pdfHistoryBeforeUnload, false);
  1163. window.addEventListener('pageshow', function pdfHistoryPageShow(evt) {
  1164. window.addEventListener('beforeunload', pdfHistoryBeforeUnload, false);
  1165. }, false);
  1166. self.eventBus.on('presentationmodechanged', function (e) {
  1167. self.isViewerInPresentationMode = e.active;
  1168. });
  1169. },
  1170. clearHistoryState: function pdfHistory_clearHistoryState() {
  1171. this._pushOrReplaceState(null, true);
  1172. },
  1173. _isStateObjectDefined: function pdfHistory_isStateObjectDefined(state) {
  1174. return state && state.uid >= 0 && state.fingerprint && this.fingerprint === state.fingerprint && state.target && state.target.hash ? true : false;
  1175. },
  1176. _pushOrReplaceState: function pdfHistory_pushOrReplaceState(stateObj, replace) {
  1177. if (replace) {
  1178. window.history.replaceState(stateObj, '', document.URL);
  1179. } else {
  1180. window.history.pushState(stateObj, '', document.URL);
  1181. }
  1182. },
  1183. get isHashChangeUnlocked() {
  1184. if (!this.initialized) {
  1185. return true;
  1186. }
  1187. return this.allowHashChange;
  1188. },
  1189. _updatePreviousBookmark: function pdfHistory_updatePreviousBookmark() {
  1190. if (this.updatePreviousBookmark && this.currentBookmark && this.currentPage) {
  1191. this.previousBookmark = this.currentBookmark;
  1192. this.previousPage = this.currentPage;
  1193. this.updatePreviousBookmark = false;
  1194. }
  1195. },
  1196. updateCurrentBookmark: function pdfHistoryUpdateCurrentBookmark(bookmark, pageNum) {
  1197. if (this.initialized) {
  1198. this.currentBookmark = bookmark.substring(1);
  1199. this.currentPage = pageNum | 0;
  1200. this._updatePreviousBookmark();
  1201. }
  1202. },
  1203. updateNextHashParam: function pdfHistoryUpdateNextHashParam(param) {
  1204. if (this.initialized) {
  1205. this.nextHashParam = param;
  1206. }
  1207. },
  1208. push: function pdfHistoryPush(params, isInitialBookmark) {
  1209. if (!(this.initialized && this.historyUnlocked)) {
  1210. return;
  1211. }
  1212. if (params.dest && !params.hash) {
  1213. params.hash = this.current.hash && this.current.dest && this.current.dest === params.dest ? this.current.hash : this.linkService.getDestinationHash(params.dest).split('#')[1];
  1214. }
  1215. if (params.page) {
  1216. params.page |= 0;
  1217. }
  1218. if (isInitialBookmark) {
  1219. var target = window.history.state.target;
  1220. if (!target) {
  1221. this._pushToHistory(params, false);
  1222. this.previousHash = window.location.hash.substring(1);
  1223. }
  1224. this.updatePreviousBookmark = this.nextHashParam ? false : true;
  1225. if (target) {
  1226. this._updatePreviousBookmark();
  1227. }
  1228. return;
  1229. }
  1230. if (this.nextHashParam) {
  1231. if (this.nextHashParam === params.hash) {
  1232. this.nextHashParam = null;
  1233. this.updatePreviousBookmark = true;
  1234. return;
  1235. } else {
  1236. this.nextHashParam = null;
  1237. }
  1238. }
  1239. if (params.hash) {
  1240. if (this.current.hash) {
  1241. if (this.current.hash !== params.hash) {
  1242. this._pushToHistory(params, true);
  1243. } else {
  1244. if (!this.current.page && params.page) {
  1245. this._pushToHistory(params, false, true);
  1246. }
  1247. this.updatePreviousBookmark = true;
  1248. }
  1249. } else {
  1250. this._pushToHistory(params, true);
  1251. }
  1252. } else if (this.current.page && params.page && this.current.page !== params.page) {
  1253. this._pushToHistory(params, true);
  1254. }
  1255. },
  1256. _getPreviousParams: function pdfHistory_getPreviousParams(onlyCheckPage, beforeUnload) {
  1257. if (!(this.currentBookmark && this.currentPage)) {
  1258. return null;
  1259. } else if (this.updatePreviousBookmark) {
  1260. this.updatePreviousBookmark = false;
  1261. }
  1262. if (this.uid > 0 && !(this.previousBookmark && this.previousPage)) {
  1263. return null;
  1264. }
  1265. if (!this.current.dest && !onlyCheckPage || beforeUnload) {
  1266. if (this.previousBookmark === this.currentBookmark) {
  1267. return null;
  1268. }
  1269. } else if (this.current.page || onlyCheckPage) {
  1270. if (this.previousPage === this.currentPage) {
  1271. return null;
  1272. }
  1273. } else {
  1274. return null;
  1275. }
  1276. var params = {
  1277. hash: this.currentBookmark,
  1278. page: this.currentPage
  1279. };
  1280. if (this.isViewerInPresentationMode) {
  1281. params.hash = null;
  1282. }
  1283. return params;
  1284. },
  1285. _stateObj: function pdfHistory_stateObj(params) {
  1286. return {
  1287. fingerprint: this.fingerprint,
  1288. uid: this.uid,
  1289. target: params
  1290. };
  1291. },
  1292. _pushToHistory: function pdfHistory_pushToHistory(params, addPrevious, overwrite) {
  1293. if (!this.initialized) {
  1294. return;
  1295. }
  1296. if (!params.hash && params.page) {
  1297. params.hash = 'page=' + params.page;
  1298. }
  1299. if (addPrevious && !overwrite) {
  1300. var previousParams = this._getPreviousParams();
  1301. if (previousParams) {
  1302. var replacePrevious = !this.current.dest && this.current.hash !== this.previousHash;
  1303. this._pushToHistory(previousParams, false, replacePrevious);
  1304. }
  1305. }
  1306. this._pushOrReplaceState(this._stateObj(params), overwrite || this.uid === 0);
  1307. this.currentUid = this.uid++;
  1308. this.current = params;
  1309. this.updatePreviousBookmark = true;
  1310. },
  1311. _goTo: function pdfHistory_goTo(state) {
  1312. if (!(this.initialized && this.historyUnlocked && this._isStateObjectDefined(state))) {
  1313. return;
  1314. }
  1315. if (!this.reInitialized && state.uid < this.currentUid) {
  1316. var previousParams = this._getPreviousParams(true);
  1317. if (previousParams) {
  1318. this._pushToHistory(this.current, false);
  1319. this._pushToHistory(previousParams, false);
  1320. this.currentUid = state.uid;
  1321. window.history.back();
  1322. return;
  1323. }
  1324. }
  1325. this.historyUnlocked = false;
  1326. if (state.target.dest) {
  1327. this.linkService.navigateTo(state.target.dest);
  1328. } else {
  1329. this.linkService.setHash(state.target.hash);
  1330. }
  1331. this.currentUid = state.uid;
  1332. if (state.uid > this.uid) {
  1333. this.uid = state.uid;
  1334. }
  1335. this.current = state.target;
  1336. this.updatePreviousBookmark = true;
  1337. var currentHash = window.location.hash.substring(1);
  1338. if (this.previousHash !== currentHash) {
  1339. this.allowHashChange = false;
  1340. }
  1341. this.previousHash = currentHash;
  1342. this.historyUnlocked = true;
  1343. },
  1344. back: function pdfHistoryBack() {
  1345. this.go(-1);
  1346. },
  1347. forward: function pdfHistoryForward() {
  1348. this.go(1);
  1349. },
  1350. go: function pdfHistoryGo(direction) {
  1351. if (this.initialized && this.historyUnlocked) {
  1352. var state = window.history.state;
  1353. if (direction === -1 && state && state.uid > 0) {
  1354. window.history.back();
  1355. } else if (direction === 1 && state && state.uid < this.uid - 1) {
  1356. window.history.forward();
  1357. }
  1358. }
  1359. }
  1360. };
  1361. exports.PDFHistory = PDFHistory;
  1362. }));
  1363. (function (root, factory) {
  1364. factory(root.pdfjsWebPDFLinkService = {}, root.pdfjsWebUIUtils, root.pdfjsWebDOMEvents);
  1365. }(this, function (exports, uiUtils, domEvents) {
  1366. var parseQueryString = uiUtils.parseQueryString;
  1367. var PageNumberRegExp = /^\d+$/;
  1368. function isPageNumber(str) {
  1369. return PageNumberRegExp.test(str);
  1370. }
  1371. var PDFLinkService = function PDFLinkServiceClosure() {
  1372. function PDFLinkService(options) {
  1373. options = options || {};
  1374. this.eventBus = options.eventBus || domEvents.getGlobalEventBus();
  1375. this.baseUrl = null;
  1376. this.pdfDocument = null;
  1377. this.pdfViewer = null;
  1378. this.pdfHistory = null;
  1379. this._pagesRefCache = null;
  1380. }
  1381. PDFLinkService.prototype = {
  1382. setDocument: function PDFLinkService_setDocument(pdfDocument, baseUrl) {
  1383. this.baseUrl = baseUrl;
  1384. this.pdfDocument = pdfDocument;
  1385. this._pagesRefCache = Object.create(null);
  1386. },
  1387. setViewer: function PDFLinkService_setViewer(pdfViewer) {
  1388. this.pdfViewer = pdfViewer;
  1389. },
  1390. setHistory: function PDFLinkService_setHistory(pdfHistory) {
  1391. this.pdfHistory = pdfHistory;
  1392. },
  1393. get pagesCount() {
  1394. return this.pdfDocument ? this.pdfDocument.numPages : 0;
  1395. },
  1396. get page() {
  1397. return this.pdfViewer.currentPageNumber;
  1398. },
  1399. set page(value) {
  1400. this.pdfViewer.currentPageNumber = value;
  1401. },
  1402. navigateTo: function PDFLinkService_navigateTo(dest) {
  1403. var destString = '';
  1404. var self = this;
  1405. var goToDestination = function (destRef) {
  1406. var pageNumber;
  1407. if (destRef instanceof Object) {
  1408. pageNumber = self._cachedPageNumber(destRef);
  1409. } else if ((destRef | 0) === destRef) {
  1410. pageNumber = destRef + 1;
  1411. } else {
  1412. console.error('PDFLinkService_navigateTo: "' + destRef + '" is not a valid destination reference.');
  1413. return;
  1414. }
  1415. if (pageNumber) {
  1416. if (pageNumber < 1 || pageNumber > self.pagesCount) {
  1417. console.error('PDFLinkService_navigateTo: "' + pageNumber + '" is a non-existent page number.');
  1418. return;
  1419. }
  1420. self.pdfViewer.scrollPageIntoView({
  1421. pageNumber: pageNumber,
  1422. destArray: dest
  1423. });
  1424. if (self.pdfHistory) {
  1425. self.pdfHistory.push({
  1426. dest: dest,
  1427. hash: destString,
  1428. page: pageNumber
  1429. });
  1430. }
  1431. } else {
  1432. self.pdfDocument.getPageIndex(destRef).then(function (pageIndex) {
  1433. self.cachePageRef(pageIndex + 1, destRef);
  1434. goToDestination(destRef);
  1435. }).catch(function () {
  1436. console.error('PDFLinkService_navigateTo: "' + destRef + '" is not a valid page reference.');
  1437. return;
  1438. });
  1439. }
  1440. };
  1441. var destinationPromise;
  1442. if (typeof dest === 'string') {
  1443. destString = dest;
  1444. destinationPromise = this.pdfDocument.getDestination(dest);
  1445. } else {
  1446. destinationPromise = Promise.resolve(dest);
  1447. }
  1448. destinationPromise.then(function (destination) {
  1449. dest = destination;
  1450. if (!(destination instanceof Array)) {
  1451. console.error('PDFLinkService_navigateTo: "' + destination + '" is not a valid destination array.');
  1452. return;
  1453. }
  1454. goToDestination(destination[0]);
  1455. });
  1456. },
  1457. getDestinationHash: function PDFLinkService_getDestinationHash(dest) {
  1458. if (typeof dest === 'string') {
  1459. return this.getAnchorUrl('#' + (isPageNumber(dest) ? 'nameddest=' : '') + escape(dest));
  1460. }
  1461. if (dest instanceof Array) {
  1462. var str = JSON.stringify(dest);
  1463. return this.getAnchorUrl('#' + escape(str));
  1464. }
  1465. return this.getAnchorUrl('');
  1466. },
  1467. getAnchorUrl: function PDFLinkService_getAnchorUrl(anchor) {
  1468. return (this.baseUrl || '') + anchor;
  1469. },
  1470. setHash: function PDFLinkService_setHash(hash) {
  1471. var pageNumber, dest;
  1472. if (hash.indexOf('=') >= 0) {
  1473. var params = parseQueryString(hash);
  1474. if ('search' in params) {
  1475. this.eventBus.dispatch('findfromurlhash', {
  1476. source: this,
  1477. query: params['search'].replace(/"/g, ''),
  1478. phraseSearch: params['phrase'] === 'true'
  1479. });
  1480. }
  1481. if ('nameddest' in params) {
  1482. if (this.pdfHistory) {
  1483. this.pdfHistory.updateNextHashParam(params.nameddest);
  1484. }
  1485. this.navigateTo(params.nameddest);
  1486. return;
  1487. }
  1488. if ('page' in params) {
  1489. pageNumber = params.page | 0 || 1;
  1490. }
  1491. if ('zoom' in params) {
  1492. var zoomArgs = params.zoom.split(',');
  1493. var zoomArg = zoomArgs[0];
  1494. var zoomArgNumber = parseFloat(zoomArg);
  1495. if (zoomArg.indexOf('Fit') === -1) {
  1496. dest = [
  1497. null,
  1498. { name: 'XYZ' },
  1499. zoomArgs.length > 1 ? zoomArgs[1] | 0 : null,
  1500. zoomArgs.length > 2 ? zoomArgs[2] | 0 : null,
  1501. zoomArgNumber ? zoomArgNumber / 100 : zoomArg
  1502. ];
  1503. } else {
  1504. if (zoomArg === 'Fit' || zoomArg === 'FitB') {
  1505. dest = [
  1506. null,
  1507. { name: zoomArg }
  1508. ];
  1509. } else if (zoomArg === 'FitH' || zoomArg === 'FitBH' || (zoomArg === 'FitV' || zoomArg === 'FitBV')) {
  1510. dest = [
  1511. null,
  1512. { name: zoomArg },
  1513. zoomArgs.length > 1 ? zoomArgs[1] | 0 : null
  1514. ];
  1515. } else if (zoomArg === 'FitR') {
  1516. if (zoomArgs.length !== 5) {
  1517. console.error('PDFLinkService_setHash: ' + 'Not enough parameters for \'FitR\'.');
  1518. } else {
  1519. dest = [
  1520. null,
  1521. { name: zoomArg },
  1522. zoomArgs[1] | 0,
  1523. zoomArgs[2] | 0,
  1524. zoomArgs[3] | 0,
  1525. zoomArgs[4] | 0
  1526. ];
  1527. }
  1528. } else {
  1529. console.error('PDFLinkService_setHash: \'' + zoomArg + '\' is not a valid zoom value.');
  1530. }
  1531. }
  1532. }
  1533. if (dest) {
  1534. this.pdfViewer.scrollPageIntoView({
  1535. pageNumber: pageNumber || this.page,
  1536. destArray: dest,
  1537. allowNegativeOffset: true
  1538. });
  1539. } else if (pageNumber) {
  1540. this.page = pageNumber;
  1541. }
  1542. if ('pagemode' in params) {
  1543. this.eventBus.dispatch('pagemode', {
  1544. source: this,
  1545. mode: params.pagemode
  1546. });
  1547. }
  1548. } else {
  1549. if (isPageNumber(hash) && hash <= this.pagesCount) {
  1550. console.warn('PDFLinkService_setHash: specifying a page number ' + 'directly after the hash symbol (#) is deprecated, ' + 'please use the "#page=' + hash + '" form instead.');
  1551. this.page = hash | 0;
  1552. }
  1553. dest = unescape(hash);
  1554. try {
  1555. dest = JSON.parse(dest);
  1556. if (!(dest instanceof Array)) {
  1557. dest = dest.toString();
  1558. }
  1559. } catch (ex) {
  1560. }
  1561. if (typeof dest === 'string' || isValidExplicitDestination(dest)) {
  1562. if (this.pdfHistory) {
  1563. this.pdfHistory.updateNextHashParam(dest);
  1564. }
  1565. this.navigateTo(dest);
  1566. return;
  1567. }
  1568. console.error('PDFLinkService_setHash: \'' + unescape(hash) + '\' is not a valid destination.');
  1569. }
  1570. },
  1571. executeNamedAction: function PDFLinkService_executeNamedAction(action) {
  1572. switch (action) {
  1573. case 'GoBack':
  1574. if (this.pdfHistory) {
  1575. this.pdfHistory.back();
  1576. }
  1577. break;
  1578. case 'GoForward':
  1579. if (this.pdfHistory) {
  1580. this.pdfHistory.forward();
  1581. }
  1582. break;
  1583. case 'NextPage':
  1584. if (this.page < this.pagesCount) {
  1585. this.page++;
  1586. }
  1587. break;
  1588. case 'PrevPage':
  1589. if (this.page > 1) {
  1590. this.page--;
  1591. }
  1592. break;
  1593. case 'LastPage':
  1594. this.page = this.pagesCount;
  1595. break;
  1596. case 'FirstPage':
  1597. this.page = 1;
  1598. break;
  1599. default:
  1600. break;
  1601. }
  1602. this.eventBus.dispatch('namedaction', {
  1603. source: this,
  1604. action: action
  1605. });
  1606. },
  1607. cachePageRef: function PDFLinkService_cachePageRef(pageNum, pageRef) {
  1608. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  1609. this._pagesRefCache[refStr] = pageNum;
  1610. },
  1611. _cachedPageNumber: function PDFLinkService_cachedPageNumber(pageRef) {
  1612. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  1613. return this._pagesRefCache && this._pagesRefCache[refStr] || null;
  1614. }
  1615. };
  1616. function isValidExplicitDestination(dest) {
  1617. if (!(dest instanceof Array)) {
  1618. return false;
  1619. }
  1620. var destLength = dest.length, allowNull = true;
  1621. if (destLength < 2) {
  1622. return false;
  1623. }
  1624. var page = dest[0];
  1625. if (!(typeof page === 'object' && typeof page.num === 'number' && (page.num | 0) === page.num && typeof page.gen === 'number' && (page.gen | 0) === page.gen) && !(typeof page === 'number' && (page | 0) === page && page >= 0)) {
  1626. return false;
  1627. }
  1628. var zoom = dest[1];
  1629. if (!(typeof zoom === 'object' && typeof zoom.name === 'string')) {
  1630. return false;
  1631. }
  1632. switch (zoom.name) {
  1633. case 'XYZ':
  1634. if (destLength !== 5) {
  1635. return false;
  1636. }
  1637. break;
  1638. case 'Fit':
  1639. case 'FitB':
  1640. return destLength === 2;
  1641. case 'FitH':
  1642. case 'FitBH':
  1643. case 'FitV':
  1644. case 'FitBV':
  1645. if (destLength !== 3) {
  1646. return false;
  1647. }
  1648. break;
  1649. case 'FitR':
  1650. if (destLength !== 6) {
  1651. return false;
  1652. }
  1653. allowNull = false;
  1654. break;
  1655. default:
  1656. return false;
  1657. }
  1658. for (var i = 2; i < destLength; i++) {
  1659. var param = dest[i];
  1660. if (!(typeof param === 'number' || allowNull && param === null)) {
  1661. return false;
  1662. }
  1663. }
  1664. return true;
  1665. }
  1666. return PDFLinkService;
  1667. }();
  1668. var SimpleLinkService = function SimpleLinkServiceClosure() {
  1669. function SimpleLinkService() {
  1670. }
  1671. SimpleLinkService.prototype = {
  1672. get page() {
  1673. return 0;
  1674. },
  1675. set page(value) {
  1676. },
  1677. navigateTo: function (dest) {
  1678. },
  1679. getDestinationHash: function (dest) {
  1680. return '#';
  1681. },
  1682. getAnchorUrl: function (hash) {
  1683. return '#';
  1684. },
  1685. setHash: function (hash) {
  1686. },
  1687. executeNamedAction: function (action) {
  1688. },
  1689. cachePageRef: function (pageNum, pageRef) {
  1690. }
  1691. };
  1692. return SimpleLinkService;
  1693. }();
  1694. exports.PDFLinkService = PDFLinkService;
  1695. exports.SimpleLinkService = SimpleLinkService;
  1696. }));
  1697. (function (root, factory) {
  1698. factory(root.pdfjsWebPDFPageView = {}, root.pdfjsWebUIUtils, root.pdfjsWebPDFRenderingQueue, root.pdfjsWebDOMEvents, root.pdfjsWebPDFJS);
  1699. }(this, function (exports, uiUtils, pdfRenderingQueue, domEvents, pdfjsLib) {
  1700. var CSS_UNITS = uiUtils.CSS_UNITS;
  1701. var DEFAULT_SCALE = uiUtils.DEFAULT_SCALE;
  1702. var getOutputScale = uiUtils.getOutputScale;
  1703. var approximateFraction = uiUtils.approximateFraction;
  1704. var roundToDivide = uiUtils.roundToDivide;
  1705. var RenderingStates = pdfRenderingQueue.RenderingStates;
  1706. var TEXT_LAYER_RENDER_DELAY = 200;
  1707. var PDFPageView = function PDFPageViewClosure() {
  1708. function PDFPageView(options) {
  1709. var container = options.container;
  1710. var id = options.id;
  1711. var scale = options.scale;
  1712. var defaultViewport = options.defaultViewport;
  1713. var renderingQueue = options.renderingQueue;
  1714. var textLayerFactory = options.textLayerFactory;
  1715. var annotationLayerFactory = options.annotationLayerFactory;
  1716. var enhanceTextSelection = options.enhanceTextSelection || false;
  1717. var renderInteractiveForms = options.renderInteractiveForms || false;
  1718. this.id = id;
  1719. this.renderingId = 'page' + id;
  1720. this.pageLabel = null;
  1721. this.rotation = 0;
  1722. this.scale = scale || DEFAULT_SCALE;
  1723. this.viewport = defaultViewport;
  1724. this.pdfPageRotate = defaultViewport.rotation;
  1725. this.hasRestrictedScaling = false;
  1726. this.enhanceTextSelection = enhanceTextSelection;
  1727. this.renderInteractiveForms = renderInteractiveForms;
  1728. this.eventBus = options.eventBus || domEvents.getGlobalEventBus();
  1729. this.renderingQueue = renderingQueue;
  1730. this.textLayerFactory = textLayerFactory;
  1731. this.annotationLayerFactory = annotationLayerFactory;
  1732. this.renderTask = null;
  1733. this.renderingState = RenderingStates.INITIAL;
  1734. this.resume = null;
  1735. this.onBeforeDraw = null;
  1736. this.onAfterDraw = null;
  1737. this.textLayer = null;
  1738. this.zoomLayer = null;
  1739. this.annotationLayer = null;
  1740. var div = document.createElement('div');
  1741. div.id = 'pageContainer' + this.id;
  1742. div.className = 'page';
  1743. div.style.width = Math.floor(this.viewport.width) + 'px';
  1744. div.style.height = Math.floor(this.viewport.height) + 'px';
  1745. div.setAttribute('data-page-number', this.id);
  1746. this.div = div;
  1747. container.appendChild(div);
  1748. }
  1749. PDFPageView.prototype = {
  1750. setPdfPage: function PDFPageView_setPdfPage(pdfPage) {
  1751. this.pdfPage = pdfPage;
  1752. this.pdfPageRotate = pdfPage.rotate;
  1753. var totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  1754. this.viewport = pdfPage.getViewport(this.scale * CSS_UNITS, totalRotation);
  1755. this.stats = pdfPage.stats;
  1756. this.reset();
  1757. },
  1758. destroy: function PDFPageView_destroy() {
  1759. this.zoomLayer = null;
  1760. this.reset();
  1761. if (this.pdfPage) {
  1762. this.pdfPage.cleanup();
  1763. }
  1764. },
  1765. reset: function PDFPageView_reset(keepZoomLayer, keepAnnotations) {
  1766. this.cancelRendering();
  1767. var div = this.div;
  1768. div.style.width = Math.floor(this.viewport.width) + 'px';
  1769. div.style.height = Math.floor(this.viewport.height) + 'px';
  1770. var childNodes = div.childNodes;
  1771. var currentZoomLayerNode = keepZoomLayer && this.zoomLayer || null;
  1772. var currentAnnotationNode = keepAnnotations && this.annotationLayer && this.annotationLayer.div || null;
  1773. for (var i = childNodes.length - 1; i >= 0; i--) {
  1774. var node = childNodes[i];
  1775. if (currentZoomLayerNode === node || currentAnnotationNode === node) {
  1776. continue;
  1777. }
  1778. div.removeChild(node);
  1779. }
  1780. div.removeAttribute('data-loaded');
  1781. if (currentAnnotationNode) {
  1782. this.annotationLayer.hide();
  1783. } else {
  1784. this.annotationLayer = null;
  1785. }
  1786. if (this.canvas && !currentZoomLayerNode) {
  1787. this.canvas.width = 0;
  1788. this.canvas.height = 0;
  1789. delete this.canvas;
  1790. }
  1791. this.loadingIconDiv = document.createElement('div');
  1792. this.loadingIconDiv.className = 'loadingIcon';
  1793. div.appendChild(this.loadingIconDiv);
  1794. },
  1795. update: function PDFPageView_update(scale, rotation) {
  1796. this.scale = scale || this.scale;
  1797. if (typeof rotation !== 'undefined') {
  1798. this.rotation = rotation;
  1799. }
  1800. var totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  1801. this.viewport = this.viewport.clone({
  1802. scale: this.scale * CSS_UNITS,
  1803. rotation: totalRotation
  1804. });
  1805. var isScalingRestricted = false;
  1806. if (this.canvas && pdfjsLib.PDFJS.maxCanvasPixels > 0) {
  1807. var outputScale = this.outputScale;
  1808. if ((Math.floor(this.viewport.width) * outputScale.sx | 0) * (Math.floor(this.viewport.height) * outputScale.sy | 0) > pdfjsLib.PDFJS.maxCanvasPixels) {
  1809. isScalingRestricted = true;
  1810. }
  1811. }
  1812. if (this.canvas) {
  1813. if (pdfjsLib.PDFJS.useOnlyCssZoom || this.hasRestrictedScaling && isScalingRestricted) {
  1814. this.cssTransform(this.canvas, true);
  1815. this.eventBus.dispatch('pagerendered', {
  1816. source: this,
  1817. pageNumber: this.id,
  1818. cssTransform: true
  1819. });
  1820. return;
  1821. }
  1822. if (!this.zoomLayer) {
  1823. this.zoomLayer = this.canvas.parentNode;
  1824. this.zoomLayer.style.position = 'absolute';
  1825. }
  1826. }
  1827. if (this.zoomLayer) {
  1828. this.cssTransform(this.zoomLayer.firstChild);
  1829. }
  1830. this.reset(true, true);
  1831. },
  1832. cancelRendering: function PDFPageView_cancelRendering() {
  1833. if (this.renderTask) {
  1834. this.renderTask.cancel();
  1835. this.renderTask = null;
  1836. }
  1837. this.renderingState = RenderingStates.INITIAL;
  1838. this.resume = null;
  1839. if (this.textLayer) {
  1840. this.textLayer.cancel();
  1841. this.textLayer = null;
  1842. }
  1843. },
  1844. updatePosition: function PDFPageView_updatePosition() {
  1845. if (this.textLayer) {
  1846. this.textLayer.render(TEXT_LAYER_RENDER_DELAY);
  1847. }
  1848. },
  1849. cssTransform: function PDFPageView_transform(canvas, redrawAnnotations) {
  1850. var CustomStyle = pdfjsLib.CustomStyle;
  1851. var width = this.viewport.width;
  1852. var height = this.viewport.height;
  1853. var div = this.div;
  1854. canvas.style.width = canvas.parentNode.style.width = div.style.width = Math.floor(width) + 'px';
  1855. canvas.style.height = canvas.parentNode.style.height = div.style.height = Math.floor(height) + 'px';
  1856. var relativeRotation = this.viewport.rotation - canvas._viewport.rotation;
  1857. var absRotation = Math.abs(relativeRotation);
  1858. var scaleX = 1, scaleY = 1;
  1859. if (absRotation === 90 || absRotation === 270) {
  1860. scaleX = height / width;
  1861. scaleY = width / height;
  1862. }
  1863. var cssTransform = 'rotate(' + relativeRotation + 'deg) ' + 'scale(' + scaleX + ',' + scaleY + ')';
  1864. CustomStyle.setProp('transform', canvas, cssTransform);
  1865. if (this.textLayer) {
  1866. var textLayerViewport = this.textLayer.viewport;
  1867. var textRelativeRotation = this.viewport.rotation - textLayerViewport.rotation;
  1868. var textAbsRotation = Math.abs(textRelativeRotation);
  1869. var scale = width / textLayerViewport.width;
  1870. if (textAbsRotation === 90 || textAbsRotation === 270) {
  1871. scale = width / textLayerViewport.height;
  1872. }
  1873. var textLayerDiv = this.textLayer.textLayerDiv;
  1874. var transX, transY;
  1875. switch (textAbsRotation) {
  1876. case 0:
  1877. transX = transY = 0;
  1878. break;
  1879. case 90:
  1880. transX = 0;
  1881. transY = '-' + textLayerDiv.style.height;
  1882. break;
  1883. case 180:
  1884. transX = '-' + textLayerDiv.style.width;
  1885. transY = '-' + textLayerDiv.style.height;
  1886. break;
  1887. case 270:
  1888. transX = '-' + textLayerDiv.style.width;
  1889. transY = 0;
  1890. break;
  1891. default:
  1892. console.error('Bad rotation value.');
  1893. break;
  1894. }
  1895. CustomStyle.setProp('transform', textLayerDiv, 'rotate(' + textAbsRotation + 'deg) ' + 'scale(' + scale + ', ' + scale + ') ' + 'translate(' + transX + ', ' + transY + ')');
  1896. CustomStyle.setProp('transformOrigin', textLayerDiv, '0% 0%');
  1897. }
  1898. if (redrawAnnotations && this.annotationLayer) {
  1899. this.annotationLayer.render(this.viewport, 'display');
  1900. }
  1901. },
  1902. get width() {
  1903. return this.viewport.width;
  1904. },
  1905. get height() {
  1906. return this.viewport.height;
  1907. },
  1908. getPagePoint: function PDFPageView_getPagePoint(x, y) {
  1909. return this.viewport.convertToPdfPoint(x, y);
  1910. },
  1911. draw: function PDFPageView_draw() {
  1912. if (this.renderingState !== RenderingStates.INITIAL) {
  1913. console.error('Must be in new state before drawing');
  1914. this.reset();
  1915. }
  1916. this.renderingState = RenderingStates.RUNNING;
  1917. var pdfPage = this.pdfPage;
  1918. var viewport = this.viewport;
  1919. var div = this.div;
  1920. var canvasWrapper = document.createElement('div');
  1921. canvasWrapper.style.width = div.style.width;
  1922. canvasWrapper.style.height = div.style.height;
  1923. canvasWrapper.classList.add('canvasWrapper');
  1924. var canvas = document.createElement('canvas');
  1925. canvas.id = 'page' + this.id;
  1926. canvas.setAttribute('hidden', 'hidden');
  1927. var isCanvasHidden = true;
  1928. canvasWrapper.appendChild(canvas);
  1929. if (this.annotationLayer && this.annotationLayer.div) {
  1930. div.insertBefore(canvasWrapper, this.annotationLayer.div);
  1931. } else {
  1932. div.appendChild(canvasWrapper);
  1933. }
  1934. this.canvas = canvas;
  1935. canvas.mozOpaque = true;
  1936. var ctx = canvas.getContext('2d', { alpha: false });
  1937. var outputScale = getOutputScale(ctx);
  1938. this.outputScale = outputScale;
  1939. if (pdfjsLib.PDFJS.useOnlyCssZoom) {
  1940. var actualSizeViewport = viewport.clone({ scale: CSS_UNITS });
  1941. outputScale.sx *= actualSizeViewport.width / viewport.width;
  1942. outputScale.sy *= actualSizeViewport.height / viewport.height;
  1943. outputScale.scaled = true;
  1944. }
  1945. if (pdfjsLib.PDFJS.maxCanvasPixels > 0) {
  1946. var pixelsInViewport = viewport.width * viewport.height;
  1947. var maxScale = Math.sqrt(pdfjsLib.PDFJS.maxCanvasPixels / pixelsInViewport);
  1948. if (outputScale.sx > maxScale || outputScale.sy > maxScale) {
  1949. outputScale.sx = maxScale;
  1950. outputScale.sy = maxScale;
  1951. outputScale.scaled = true;
  1952. this.hasRestrictedScaling = true;
  1953. } else {
  1954. this.hasRestrictedScaling = false;
  1955. }
  1956. }
  1957. var sfx = approximateFraction(outputScale.sx);
  1958. var sfy = approximateFraction(outputScale.sy);
  1959. canvas.width = roundToDivide(viewport.width * outputScale.sx, sfx[0]);
  1960. canvas.height = roundToDivide(viewport.height * outputScale.sy, sfy[0]);
  1961. canvas.style.width = roundToDivide(viewport.width, sfx[1]) + 'px';
  1962. canvas.style.height = roundToDivide(viewport.height, sfy[1]) + 'px';
  1963. canvas._viewport = viewport;
  1964. var textLayerDiv = null;
  1965. var textLayer = null;
  1966. if (this.textLayerFactory) {
  1967. textLayerDiv = document.createElement('div');
  1968. textLayerDiv.className = 'textLayer';
  1969. textLayerDiv.style.width = canvasWrapper.style.width;
  1970. textLayerDiv.style.height = canvasWrapper.style.height;
  1971. if (this.annotationLayer && this.annotationLayer.div) {
  1972. div.insertBefore(textLayerDiv, this.annotationLayer.div);
  1973. } else {
  1974. div.appendChild(textLayerDiv);
  1975. }
  1976. textLayer = this.textLayerFactory.createTextLayerBuilder(textLayerDiv, this.id - 1, this.viewport, this.enhanceTextSelection);
  1977. }
  1978. this.textLayer = textLayer;
  1979. var resolveRenderPromise, rejectRenderPromise;
  1980. var promise = new Promise(function (resolve, reject) {
  1981. resolveRenderPromise = resolve;
  1982. rejectRenderPromise = reject;
  1983. });
  1984. var self = this;
  1985. function pageViewDrawCallback(error) {
  1986. if (renderTask === self.renderTask) {
  1987. self.renderTask = null;
  1988. }
  1989. if (error === 'cancelled') {
  1990. rejectRenderPromise(error);
  1991. return;
  1992. }
  1993. self.renderingState = RenderingStates.FINISHED;
  1994. if (isCanvasHidden) {
  1995. self.canvas.removeAttribute('hidden');
  1996. isCanvasHidden = false;
  1997. }
  1998. if (self.loadingIconDiv) {
  1999. div.removeChild(self.loadingIconDiv);
  2000. delete self.loadingIconDiv;
  2001. }
  2002. if (self.zoomLayer) {
  2003. var zoomLayerCanvas = self.zoomLayer.firstChild;
  2004. zoomLayerCanvas.width = 0;
  2005. zoomLayerCanvas.height = 0;
  2006. if (div.contains(self.zoomLayer)) {
  2007. div.removeChild(self.zoomLayer);
  2008. }
  2009. self.zoomLayer = null;
  2010. }
  2011. self.error = error;
  2012. self.stats = pdfPage.stats;
  2013. if (self.onAfterDraw) {
  2014. self.onAfterDraw();
  2015. }
  2016. self.eventBus.dispatch('pagerendered', {
  2017. source: self,
  2018. pageNumber: self.id,
  2019. cssTransform: false
  2020. });
  2021. if (!error) {
  2022. resolveRenderPromise(undefined);
  2023. } else {
  2024. rejectRenderPromise(error);
  2025. }
  2026. }
  2027. var renderContinueCallback = null;
  2028. if (this.renderingQueue) {
  2029. renderContinueCallback = function renderContinueCallback(cont) {
  2030. if (!self.renderingQueue.isHighestPriority(self)) {
  2031. self.renderingState = RenderingStates.PAUSED;
  2032. self.resume = function resumeCallback() {
  2033. self.renderingState = RenderingStates.RUNNING;
  2034. cont();
  2035. };
  2036. return;
  2037. }
  2038. if (isCanvasHidden) {
  2039. self.canvas.removeAttribute('hidden');
  2040. isCanvasHidden = false;
  2041. }
  2042. cont();
  2043. };
  2044. }
  2045. var transform = !outputScale.scaled ? null : [
  2046. outputScale.sx,
  2047. 0,
  2048. 0,
  2049. outputScale.sy,
  2050. 0,
  2051. 0
  2052. ];
  2053. var renderContext = {
  2054. canvasContext: ctx,
  2055. transform: transform,
  2056. viewport: this.viewport,
  2057. renderInteractiveForms: this.renderInteractiveForms
  2058. };
  2059. var renderTask = this.renderTask = this.pdfPage.render(renderContext);
  2060. renderTask.onContinue = renderContinueCallback;
  2061. this.renderTask.promise.then(function pdfPageRenderCallback() {
  2062. pageViewDrawCallback(null);
  2063. if (textLayer) {
  2064. self.pdfPage.getTextContent({ normalizeWhitespace: true }).then(function textContentResolved(textContent) {
  2065. textLayer.setTextContent(textContent);
  2066. textLayer.render(TEXT_LAYER_RENDER_DELAY);
  2067. });
  2068. }
  2069. }, function pdfPageRenderError(error) {
  2070. pageViewDrawCallback(error);
  2071. });
  2072. if (this.annotationLayerFactory) {
  2073. if (!this.annotationLayer) {
  2074. this.annotationLayer = this.annotationLayerFactory.createAnnotationLayerBuilder(div, this.pdfPage, this.renderInteractiveForms);
  2075. }
  2076. this.annotationLayer.render(this.viewport, 'display');
  2077. }
  2078. div.setAttribute('data-loaded', true);
  2079. if (self.onBeforeDraw) {
  2080. self.onBeforeDraw();
  2081. }
  2082. return promise;
  2083. },
  2084. setPageLabel: function PDFView_setPageLabel(label) {
  2085. this.pageLabel = typeof label === 'string' ? label : null;
  2086. if (this.pageLabel !== null) {
  2087. this.div.setAttribute('data-page-label', this.pageLabel);
  2088. } else {
  2089. this.div.removeAttribute('data-page-label');
  2090. }
  2091. }
  2092. };
  2093. return PDFPageView;
  2094. }();
  2095. exports.PDFPageView = PDFPageView;
  2096. }));
  2097. (function (root, factory) {
  2098. factory(root.pdfjsWebTextLayerBuilder = {}, root.pdfjsWebDOMEvents, root.pdfjsWebPDFJS);
  2099. }(this, function (exports, domEvents, pdfjsLib) {
  2100. var EXPAND_DIVS_TIMEOUT = 300;
  2101. var TextLayerBuilder = function TextLayerBuilderClosure() {
  2102. function TextLayerBuilder(options) {
  2103. this.textLayerDiv = options.textLayerDiv;
  2104. this.eventBus = options.eventBus || domEvents.getGlobalEventBus();
  2105. this.textContent = null;
  2106. this.renderingDone = false;
  2107. this.pageIdx = options.pageIndex;
  2108. this.pageNumber = this.pageIdx + 1;
  2109. this.matches = [];
  2110. this.viewport = options.viewport;
  2111. this.textDivs = [];
  2112. this.findController = options.findController || null;
  2113. this.textLayerRenderTask = null;
  2114. this.enhanceTextSelection = options.enhanceTextSelection;
  2115. this._bindMouse();
  2116. }
  2117. TextLayerBuilder.prototype = {
  2118. _finishRendering: function TextLayerBuilder_finishRendering() {
  2119. this.renderingDone = true;
  2120. if (!this.enhanceTextSelection) {
  2121. var endOfContent = document.createElement('div');
  2122. endOfContent.className = 'endOfContent';
  2123. this.textLayerDiv.appendChild(endOfContent);
  2124. }
  2125. this.eventBus.dispatch('textlayerrendered', {
  2126. source: this,
  2127. pageNumber: this.pageNumber,
  2128. numTextDivs: this.textDivs.length
  2129. });
  2130. },
  2131. render: function TextLayerBuilder_render(timeout) {
  2132. if (!this.textContent || this.renderingDone) {
  2133. return;
  2134. }
  2135. this.cancel();
  2136. this.textDivs = [];
  2137. var textLayerFrag = document.createDocumentFragment();
  2138. this.textLayerRenderTask = pdfjsLib.renderTextLayer({
  2139. textContent: this.textContent,
  2140. container: textLayerFrag,
  2141. viewport: this.viewport,
  2142. textDivs: this.textDivs,
  2143. timeout: timeout,
  2144. enhanceTextSelection: this.enhanceTextSelection
  2145. });
  2146. this.textLayerRenderTask.promise.then(function () {
  2147. this.textLayerDiv.appendChild(textLayerFrag);
  2148. this._finishRendering();
  2149. this.updateMatches();
  2150. }.bind(this), function (reason) {
  2151. });
  2152. },
  2153. cancel: function TextLayerBuilder_cancel() {
  2154. if (this.textLayerRenderTask) {
  2155. this.textLayerRenderTask.cancel();
  2156. this.textLayerRenderTask = null;
  2157. }
  2158. },
  2159. setTextContent: function TextLayerBuilder_setTextContent(textContent) {
  2160. this.cancel();
  2161. this.textContent = textContent;
  2162. },
  2163. convertMatches: function TextLayerBuilder_convertMatches(matches, matchesLength) {
  2164. var i = 0;
  2165. var iIndex = 0;
  2166. var bidiTexts = this.textContent.items;
  2167. var end = bidiTexts.length - 1;
  2168. var queryLen = this.findController === null ? 0 : this.findController.state.query.length;
  2169. var ret = [];
  2170. if (!matches) {
  2171. return ret;
  2172. }
  2173. for (var m = 0, len = matches.length; m < len; m++) {
  2174. var matchIdx = matches[m];
  2175. while (i !== end && matchIdx >= iIndex + bidiTexts[i].str.length) {
  2176. iIndex += bidiTexts[i].str.length;
  2177. i++;
  2178. }
  2179. if (i === bidiTexts.length) {
  2180. console.error('Could not find a matching mapping');
  2181. }
  2182. var match = {
  2183. begin: {
  2184. divIdx: i,
  2185. offset: matchIdx - iIndex
  2186. }
  2187. };
  2188. if (matchesLength) {
  2189. matchIdx += matchesLength[m];
  2190. } else {
  2191. matchIdx += queryLen;
  2192. }
  2193. while (i !== end && matchIdx > iIndex + bidiTexts[i].str.length) {
  2194. iIndex += bidiTexts[i].str.length;
  2195. i++;
  2196. }
  2197. match.end = {
  2198. divIdx: i,
  2199. offset: matchIdx - iIndex
  2200. };
  2201. ret.push(match);
  2202. }
  2203. return ret;
  2204. },
  2205. renderMatches: function TextLayerBuilder_renderMatches(matches) {
  2206. if (matches.length === 0) {
  2207. return;
  2208. }
  2209. var bidiTexts = this.textContent.items;
  2210. var textDivs = this.textDivs;
  2211. var prevEnd = null;
  2212. var pageIdx = this.pageIdx;
  2213. var isSelectedPage = this.findController === null ? false : pageIdx === this.findController.selected.pageIdx;
  2214. var selectedMatchIdx = this.findController === null ? -1 : this.findController.selected.matchIdx;
  2215. var highlightAll = this.findController === null ? false : this.findController.state.highlightAll;
  2216. var infinity = {
  2217. divIdx: -1,
  2218. offset: undefined
  2219. };
  2220. function beginText(begin, className) {
  2221. var divIdx = begin.divIdx;
  2222. textDivs[divIdx].textContent = '';
  2223. appendTextToDiv(divIdx, 0, begin.offset, className);
  2224. }
  2225. function appendTextToDiv(divIdx, fromOffset, toOffset, className) {
  2226. var div = textDivs[divIdx];
  2227. var content = bidiTexts[divIdx].str.substring(fromOffset, toOffset);
  2228. var node = document.createTextNode(content);
  2229. if (className) {
  2230. var span = document.createElement('span');
  2231. span.className = className;
  2232. span.appendChild(node);
  2233. div.appendChild(span);
  2234. return;
  2235. }
  2236. div.appendChild(node);
  2237. }
  2238. var i0 = selectedMatchIdx, i1 = i0 + 1;
  2239. if (highlightAll) {
  2240. i0 = 0;
  2241. i1 = matches.length;
  2242. } else if (!isSelectedPage) {
  2243. return;
  2244. }
  2245. for (var i = i0; i < i1; i++) {
  2246. var match = matches[i];
  2247. var begin = match.begin;
  2248. var end = match.end;
  2249. var isSelected = isSelectedPage && i === selectedMatchIdx;
  2250. var highlightSuffix = isSelected ? ' selected' : '';
  2251. if (this.findController) {
  2252. this.findController.updateMatchPosition(pageIdx, i, textDivs, begin.divIdx);
  2253. }
  2254. if (!prevEnd || begin.divIdx !== prevEnd.divIdx) {
  2255. if (prevEnd !== null) {
  2256. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  2257. }
  2258. beginText(begin);
  2259. } else {
  2260. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, begin.offset);
  2261. }
  2262. if (begin.divIdx === end.divIdx) {
  2263. appendTextToDiv(begin.divIdx, begin.offset, end.offset, 'highlight' + highlightSuffix);
  2264. } else {
  2265. appendTextToDiv(begin.divIdx, begin.offset, infinity.offset, 'highlight begin' + highlightSuffix);
  2266. for (var n0 = begin.divIdx + 1, n1 = end.divIdx; n0 < n1; n0++) {
  2267. textDivs[n0].className = 'highlight middle' + highlightSuffix;
  2268. }
  2269. beginText(end, 'highlight end' + highlightSuffix);
  2270. }
  2271. prevEnd = end;
  2272. }
  2273. if (prevEnd) {
  2274. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  2275. }
  2276. },
  2277. updateMatches: function TextLayerBuilder_updateMatches() {
  2278. if (!this.renderingDone) {
  2279. return;
  2280. }
  2281. var matches = this.matches;
  2282. var textDivs = this.textDivs;
  2283. var bidiTexts = this.textContent.items;
  2284. var clearedUntilDivIdx = -1;
  2285. for (var i = 0, len = matches.length; i < len; i++) {
  2286. var match = matches[i];
  2287. var begin = Math.max(clearedUntilDivIdx, match.begin.divIdx);
  2288. for (var n = begin, end = match.end.divIdx; n <= end; n++) {
  2289. var div = textDivs[n];
  2290. div.textContent = bidiTexts[n].str;
  2291. div.className = '';
  2292. }
  2293. clearedUntilDivIdx = match.end.divIdx + 1;
  2294. }
  2295. if (this.findController === null || !this.findController.active) {
  2296. return;
  2297. }
  2298. var pageMatches, pageMatchesLength;
  2299. if (this.findController !== null) {
  2300. pageMatches = this.findController.pageMatches[this.pageIdx] || null;
  2301. pageMatchesLength = this.findController.pageMatchesLength ? this.findController.pageMatchesLength[this.pageIdx] || null : null;
  2302. }
  2303. this.matches = this.convertMatches(pageMatches, pageMatchesLength);
  2304. this.renderMatches(this.matches);
  2305. },
  2306. _bindMouse: function TextLayerBuilder_bindMouse() {
  2307. var div = this.textLayerDiv;
  2308. var self = this;
  2309. var expandDivsTimer = null;
  2310. div.addEventListener('mousedown', function (e) {
  2311. if (self.enhanceTextSelection && self.textLayerRenderTask) {
  2312. self.textLayerRenderTask.expandTextDivs(true);
  2313. if (expandDivsTimer) {
  2314. clearTimeout(expandDivsTimer);
  2315. expandDivsTimer = null;
  2316. }
  2317. return;
  2318. }
  2319. var end = div.querySelector('.endOfContent');
  2320. if (!end) {
  2321. return;
  2322. }
  2323. var adjustTop = e.target !== div;
  2324. adjustTop = adjustTop && window.getComputedStyle(end).getPropertyValue('-moz-user-select') !== 'none';
  2325. if (adjustTop) {
  2326. var divBounds = div.getBoundingClientRect();
  2327. var r = Math.max(0, (e.pageY - divBounds.top) / divBounds.height);
  2328. end.style.top = (r * 100).toFixed(2) + '%';
  2329. }
  2330. end.classList.add('active');
  2331. });
  2332. div.addEventListener('mouseup', function (e) {
  2333. if (self.enhanceTextSelection && self.textLayerRenderTask) {
  2334. expandDivsTimer = setTimeout(function () {
  2335. if (self.textLayerRenderTask) {
  2336. self.textLayerRenderTask.expandTextDivs(false);
  2337. }
  2338. expandDivsTimer = null;
  2339. }, EXPAND_DIVS_TIMEOUT);
  2340. return;
  2341. }
  2342. var end = div.querySelector('.endOfContent');
  2343. if (!end) {
  2344. return;
  2345. }
  2346. end.style.top = '';
  2347. end.classList.remove('active');
  2348. });
  2349. }
  2350. };
  2351. return TextLayerBuilder;
  2352. }();
  2353. function DefaultTextLayerFactory() {
  2354. }
  2355. DefaultTextLayerFactory.prototype = {
  2356. createTextLayerBuilder: function (textLayerDiv, pageIndex, viewport, enhanceTextSelection) {
  2357. return new TextLayerBuilder({
  2358. textLayerDiv: textLayerDiv,
  2359. pageIndex: pageIndex,
  2360. viewport: viewport,
  2361. enhanceTextSelection: enhanceTextSelection
  2362. });
  2363. }
  2364. };
  2365. exports.TextLayerBuilder = TextLayerBuilder;
  2366. exports.DefaultTextLayerFactory = DefaultTextLayerFactory;
  2367. }));
  2368. (function (root, factory) {
  2369. factory(root.pdfjsWebAnnotationLayerBuilder = {}, root.pdfjsWebUIUtils, root.pdfjsWebPDFLinkService, root.pdfjsWebPDFJS);
  2370. }(this, function (exports, uiUtils, pdfLinkService, pdfjsLib) {
  2371. var mozL10n = uiUtils.mozL10n;
  2372. var SimpleLinkService = pdfLinkService.SimpleLinkService;
  2373. var AnnotationLayerBuilder = function AnnotationLayerBuilderClosure() {
  2374. function AnnotationLayerBuilder(options) {
  2375. this.pageDiv = options.pageDiv;
  2376. this.pdfPage = options.pdfPage;
  2377. this.renderInteractiveForms = options.renderInteractiveForms;
  2378. this.linkService = options.linkService;
  2379. this.downloadManager = options.downloadManager;
  2380. this.div = null;
  2381. }
  2382. AnnotationLayerBuilder.prototype = {
  2383. render: function AnnotationLayerBuilder_render(viewport, intent) {
  2384. var self = this;
  2385. var parameters = { intent: intent === undefined ? 'display' : intent };
  2386. this.pdfPage.getAnnotations(parameters).then(function (annotations) {
  2387. viewport = viewport.clone({ dontFlip: true });
  2388. parameters = {
  2389. viewport: viewport,
  2390. div: self.div,
  2391. annotations: annotations,
  2392. page: self.pdfPage,
  2393. renderInteractiveForms: self.renderInteractiveForms,
  2394. linkService: self.linkService,
  2395. downloadManager: self.downloadManager
  2396. };
  2397. if (self.div) {
  2398. pdfjsLib.AnnotationLayer.update(parameters);
  2399. } else {
  2400. if (annotations.length === 0) {
  2401. return;
  2402. }
  2403. self.div = document.createElement('div');
  2404. self.div.className = 'annotationLayer';
  2405. self.pageDiv.appendChild(self.div);
  2406. parameters.div = self.div;
  2407. pdfjsLib.AnnotationLayer.render(parameters);
  2408. if (typeof mozL10n !== 'undefined') {
  2409. mozL10n.translate(self.div);
  2410. }
  2411. }
  2412. });
  2413. },
  2414. hide: function AnnotationLayerBuilder_hide() {
  2415. if (!this.div) {
  2416. return;
  2417. }
  2418. this.div.setAttribute('hidden', 'true');
  2419. }
  2420. };
  2421. return AnnotationLayerBuilder;
  2422. }();
  2423. function DefaultAnnotationLayerFactory() {
  2424. }
  2425. DefaultAnnotationLayerFactory.prototype = {
  2426. createAnnotationLayerBuilder: function (pageDiv, pdfPage, renderInteractiveForms) {
  2427. return new AnnotationLayerBuilder({
  2428. pageDiv: pageDiv,
  2429. pdfPage: pdfPage,
  2430. renderInteractiveForms: renderInteractiveForms,
  2431. linkService: new SimpleLinkService()
  2432. });
  2433. }
  2434. };
  2435. exports.AnnotationLayerBuilder = AnnotationLayerBuilder;
  2436. exports.DefaultAnnotationLayerFactory = DefaultAnnotationLayerFactory;
  2437. }));
  2438. (function (root, factory) {
  2439. factory(root.pdfjsWebPDFViewer = {}, root.pdfjsWebUIUtils, root.pdfjsWebPDFPageView, root.pdfjsWebPDFRenderingQueue, root.pdfjsWebTextLayerBuilder, root.pdfjsWebAnnotationLayerBuilder, root.pdfjsWebPDFLinkService, root.pdfjsWebDOMEvents, root.pdfjsWebPDFJS);
  2440. }(this, function (exports, uiUtils, pdfPageView, pdfRenderingQueue, textLayerBuilder, annotationLayerBuilder, pdfLinkService, domEvents, pdfjsLib) {
  2441. var UNKNOWN_SCALE = uiUtils.UNKNOWN_SCALE;
  2442. var SCROLLBAR_PADDING = uiUtils.SCROLLBAR_PADDING;
  2443. var VERTICAL_PADDING = uiUtils.VERTICAL_PADDING;
  2444. var MAX_AUTO_SCALE = uiUtils.MAX_AUTO_SCALE;
  2445. var CSS_UNITS = uiUtils.CSS_UNITS;
  2446. var DEFAULT_SCALE = uiUtils.DEFAULT_SCALE;
  2447. var DEFAULT_SCALE_VALUE = uiUtils.DEFAULT_SCALE_VALUE;
  2448. var scrollIntoView = uiUtils.scrollIntoView;
  2449. var watchScroll = uiUtils.watchScroll;
  2450. var getVisibleElements = uiUtils.getVisibleElements;
  2451. var PDFPageView = pdfPageView.PDFPageView;
  2452. var RenderingStates = pdfRenderingQueue.RenderingStates;
  2453. var PDFRenderingQueue = pdfRenderingQueue.PDFRenderingQueue;
  2454. var TextLayerBuilder = textLayerBuilder.TextLayerBuilder;
  2455. var AnnotationLayerBuilder = annotationLayerBuilder.AnnotationLayerBuilder;
  2456. var SimpleLinkService = pdfLinkService.SimpleLinkService;
  2457. var PresentationModeState = {
  2458. UNKNOWN: 0,
  2459. NORMAL: 1,
  2460. CHANGING: 2,
  2461. FULLSCREEN: 3
  2462. };
  2463. var DEFAULT_CACHE_SIZE = 10;
  2464. var PDFViewer = function pdfViewer() {
  2465. function PDFPageViewBuffer(size) {
  2466. var data = [];
  2467. this.push = function cachePush(view) {
  2468. var i = data.indexOf(view);
  2469. if (i >= 0) {
  2470. data.splice(i, 1);
  2471. }
  2472. data.push(view);
  2473. if (data.length > size) {
  2474. data.shift().destroy();
  2475. }
  2476. };
  2477. this.resize = function (newSize) {
  2478. size = newSize;
  2479. while (data.length > size) {
  2480. data.shift().destroy();
  2481. }
  2482. };
  2483. }
  2484. function isSameScale(oldScale, newScale) {
  2485. if (newScale === oldScale) {
  2486. return true;
  2487. }
  2488. if (Math.abs(newScale - oldScale) < 1e-15) {
  2489. return true;
  2490. }
  2491. return false;
  2492. }
  2493. function PDFViewer(options) {
  2494. this.container = options.container;
  2495. this.viewer = options.viewer || options.container.firstElementChild;
  2496. this.eventBus = options.eventBus || domEvents.getGlobalEventBus();
  2497. this.linkService = options.linkService || new SimpleLinkService();
  2498. this.downloadManager = options.downloadManager || null;
  2499. this.removePageBorders = options.removePageBorders || false;
  2500. this.enhanceTextSelection = options.enhanceTextSelection || false;
  2501. this.renderInteractiveForms = options.renderInteractiveForms || false;
  2502. this.defaultRenderingQueue = !options.renderingQueue;
  2503. if (this.defaultRenderingQueue) {
  2504. this.renderingQueue = new PDFRenderingQueue();
  2505. this.renderingQueue.setViewer(this);
  2506. } else {
  2507. this.renderingQueue = options.renderingQueue;
  2508. }
  2509. this.scroll = watchScroll(this.container, this._scrollUpdate.bind(this));
  2510. this.presentationModeState = PresentationModeState.UNKNOWN;
  2511. this._resetView();
  2512. if (this.removePageBorders) {
  2513. this.viewer.classList.add('removePageBorders');
  2514. }
  2515. }
  2516. PDFViewer.prototype = {
  2517. get pagesCount() {
  2518. return this._pages.length;
  2519. },
  2520. getPageView: function (index) {
  2521. return this._pages[index];
  2522. },
  2523. get pageViewsReady() {
  2524. return this._pageViewsReady;
  2525. },
  2526. get currentPageNumber() {
  2527. return this._currentPageNumber;
  2528. },
  2529. set currentPageNumber(val) {
  2530. if ((val | 0) !== val) {
  2531. throw new Error('Invalid page number.');
  2532. }
  2533. if (!this.pdfDocument) {
  2534. this._currentPageNumber = val;
  2535. return;
  2536. }
  2537. this._setCurrentPageNumber(val, true);
  2538. },
  2539. _setCurrentPageNumber: function PDFViewer_setCurrentPageNumber(val, resetCurrentPageView) {
  2540. if (this._currentPageNumber === val) {
  2541. if (resetCurrentPageView) {
  2542. this._resetCurrentPageView();
  2543. }
  2544. return;
  2545. }
  2546. if (!(0 < val && val <= this.pagesCount)) {
  2547. console.error('PDFViewer_setCurrentPageNumber: "' + val + '" is out of bounds.');
  2548. return;
  2549. }
  2550. var arg = {
  2551. source: this,
  2552. pageNumber: val,
  2553. pageLabel: this._pageLabels && this._pageLabels[val - 1]
  2554. };
  2555. this._currentPageNumber = val;
  2556. this.eventBus.dispatch('pagechanging', arg);
  2557. this.eventBus.dispatch('pagechange', arg);
  2558. if (resetCurrentPageView) {
  2559. this._resetCurrentPageView();
  2560. }
  2561. },
  2562. get currentPageLabel() {
  2563. return this._pageLabels && this._pageLabels[this._currentPageNumber - 1];
  2564. },
  2565. set currentPageLabel(val) {
  2566. var pageNumber = val | 0;
  2567. if (this._pageLabels) {
  2568. var i = this._pageLabels.indexOf(val);
  2569. if (i >= 0) {
  2570. pageNumber = i + 1;
  2571. }
  2572. }
  2573. this.currentPageNumber = pageNumber;
  2574. },
  2575. get currentScale() {
  2576. return this._currentScale !== UNKNOWN_SCALE ? this._currentScale : DEFAULT_SCALE;
  2577. },
  2578. set currentScale(val) {
  2579. if (isNaN(val)) {
  2580. throw new Error('Invalid numeric scale');
  2581. }
  2582. if (!this.pdfDocument) {
  2583. this._currentScale = val;
  2584. this._currentScaleValue = val !== UNKNOWN_SCALE ? val.toString() : null;
  2585. return;
  2586. }
  2587. this._setScale(val, false);
  2588. },
  2589. get currentScaleValue() {
  2590. return this._currentScaleValue;
  2591. },
  2592. set currentScaleValue(val) {
  2593. if (!this.pdfDocument) {
  2594. this._currentScale = isNaN(val) ? UNKNOWN_SCALE : val;
  2595. this._currentScaleValue = val.toString();
  2596. return;
  2597. }
  2598. this._setScale(val, false);
  2599. },
  2600. get pagesRotation() {
  2601. return this._pagesRotation;
  2602. },
  2603. set pagesRotation(rotation) {
  2604. if (!(typeof rotation === 'number' && rotation % 90 === 0)) {
  2605. throw new Error('Invalid pages rotation angle.');
  2606. }
  2607. this._pagesRotation = rotation;
  2608. if (!this.pdfDocument) {
  2609. return;
  2610. }
  2611. for (var i = 0, l = this._pages.length; i < l; i++) {
  2612. var pageView = this._pages[i];
  2613. pageView.update(pageView.scale, rotation);
  2614. }
  2615. this._setScale(this._currentScaleValue, true);
  2616. if (this.defaultRenderingQueue) {
  2617. this.update();
  2618. }
  2619. },
  2620. setDocument: function (pdfDocument) {
  2621. if (this.pdfDocument) {
  2622. this._cancelRendering();
  2623. this._resetView();
  2624. }
  2625. this.pdfDocument = pdfDocument;
  2626. if (!pdfDocument) {
  2627. return;
  2628. }
  2629. var pagesCount = pdfDocument.numPages;
  2630. var self = this;
  2631. var resolvePagesPromise;
  2632. var pagesPromise = new Promise(function (resolve) {
  2633. resolvePagesPromise = resolve;
  2634. });
  2635. this.pagesPromise = pagesPromise;
  2636. pagesPromise.then(function () {
  2637. self._pageViewsReady = true;
  2638. self.eventBus.dispatch('pagesloaded', {
  2639. source: self,
  2640. pagesCount: pagesCount
  2641. });
  2642. });
  2643. var isOnePageRenderedResolved = false;
  2644. var resolveOnePageRendered = null;
  2645. var onePageRendered = new Promise(function (resolve) {
  2646. resolveOnePageRendered = resolve;
  2647. });
  2648. this.onePageRendered = onePageRendered;
  2649. var bindOnAfterAndBeforeDraw = function (pageView) {
  2650. pageView.onBeforeDraw = function pdfViewLoadOnBeforeDraw() {
  2651. self._buffer.push(this);
  2652. };
  2653. pageView.onAfterDraw = function pdfViewLoadOnAfterDraw() {
  2654. if (!isOnePageRenderedResolved) {
  2655. isOnePageRenderedResolved = true;
  2656. resolveOnePageRendered();
  2657. }
  2658. };
  2659. };
  2660. var firstPagePromise = pdfDocument.getPage(1);
  2661. this.firstPagePromise = firstPagePromise;
  2662. return firstPagePromise.then(function (pdfPage) {
  2663. var scale = this.currentScale;
  2664. var viewport = pdfPage.getViewport(scale * CSS_UNITS);
  2665. for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  2666. var textLayerFactory = null;
  2667. if (!pdfjsLib.PDFJS.disableTextLayer) {
  2668. textLayerFactory = this;
  2669. }
  2670. var pageView = new PDFPageView({
  2671. container: this.viewer,
  2672. eventBus: this.eventBus,
  2673. id: pageNum,
  2674. scale: scale,
  2675. defaultViewport: viewport.clone(),
  2676. renderingQueue: this.renderingQueue,
  2677. textLayerFactory: textLayerFactory,
  2678. annotationLayerFactory: this,
  2679. enhanceTextSelection: this.enhanceTextSelection,
  2680. renderInteractiveForms: this.renderInteractiveForms
  2681. });
  2682. bindOnAfterAndBeforeDraw(pageView);
  2683. this._pages.push(pageView);
  2684. }
  2685. var linkService = this.linkService;
  2686. onePageRendered.then(function () {
  2687. if (!pdfjsLib.PDFJS.disableAutoFetch) {
  2688. var getPagesLeft = pagesCount;
  2689. for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  2690. pdfDocument.getPage(pageNum).then(function (pageNum, pdfPage) {
  2691. var pageView = self._pages[pageNum - 1];
  2692. if (!pageView.pdfPage) {
  2693. pageView.setPdfPage(pdfPage);
  2694. }
  2695. linkService.cachePageRef(pageNum, pdfPage.ref);
  2696. getPagesLeft--;
  2697. if (!getPagesLeft) {
  2698. resolvePagesPromise();
  2699. }
  2700. }.bind(null, pageNum));
  2701. }
  2702. } else {
  2703. resolvePagesPromise();
  2704. }
  2705. });
  2706. self.eventBus.dispatch('pagesinit', { source: self });
  2707. if (this.defaultRenderingQueue) {
  2708. this.update();
  2709. }
  2710. if (this.findController) {
  2711. this.findController.resolveFirstPage();
  2712. }
  2713. }.bind(this));
  2714. },
  2715. setPageLabels: function PDFViewer_setPageLabels(labels) {
  2716. if (!this.pdfDocument) {
  2717. return;
  2718. }
  2719. if (!labels) {
  2720. this._pageLabels = null;
  2721. } else if (!(labels instanceof Array && this.pdfDocument.numPages === labels.length)) {
  2722. this._pageLabels = null;
  2723. console.error('PDFViewer_setPageLabels: Invalid page labels.');
  2724. } else {
  2725. this._pageLabels = labels;
  2726. }
  2727. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  2728. var pageView = this._pages[i];
  2729. var label = this._pageLabels && this._pageLabels[i];
  2730. pageView.setPageLabel(label);
  2731. }
  2732. },
  2733. _resetView: function () {
  2734. this._pages = [];
  2735. this._currentPageNumber = 1;
  2736. this._currentScale = UNKNOWN_SCALE;
  2737. this._currentScaleValue = null;
  2738. this._pageLabels = null;
  2739. this._buffer = new PDFPageViewBuffer(DEFAULT_CACHE_SIZE);
  2740. this._location = null;
  2741. this._pagesRotation = 0;
  2742. this._pagesRequests = [];
  2743. this._pageViewsReady = false;
  2744. this.viewer.textContent = '';
  2745. },
  2746. _scrollUpdate: function PDFViewer_scrollUpdate() {
  2747. if (this.pagesCount === 0) {
  2748. return;
  2749. }
  2750. this.update();
  2751. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  2752. this._pages[i].updatePosition();
  2753. }
  2754. },
  2755. _setScaleDispatchEvent: function pdfViewer_setScaleDispatchEvent(newScale, newValue, preset) {
  2756. var arg = {
  2757. source: this,
  2758. scale: newScale,
  2759. presetValue: preset ? newValue : undefined
  2760. };
  2761. this.eventBus.dispatch('scalechanging', arg);
  2762. this.eventBus.dispatch('scalechange', arg);
  2763. },
  2764. _setScaleUpdatePages: function pdfViewer_setScaleUpdatePages(newScale, newValue, noScroll, preset) {
  2765. this._currentScaleValue = newValue.toString();
  2766. if (isSameScale(this._currentScale, newScale)) {
  2767. if (preset) {
  2768. this._setScaleDispatchEvent(newScale, newValue, true);
  2769. }
  2770. return;
  2771. }
  2772. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  2773. this._pages[i].update(newScale);
  2774. }
  2775. this._currentScale = newScale;
  2776. if (!noScroll) {
  2777. var page = this._currentPageNumber, dest;
  2778. if (this._location && !pdfjsLib.PDFJS.ignoreCurrentPositionOnZoom && !(this.isInPresentationMode || this.isChangingPresentationMode)) {
  2779. page = this._location.pageNumber;
  2780. dest = [
  2781. null,
  2782. { name: 'XYZ' },
  2783. this._location.left,
  2784. this._location.top,
  2785. null
  2786. ];
  2787. }
  2788. this.scrollPageIntoView({
  2789. pageNumber: page,
  2790. destArray: dest,
  2791. allowNegativeOffset: true
  2792. });
  2793. }
  2794. this._setScaleDispatchEvent(newScale, newValue, preset);
  2795. if (this.defaultRenderingQueue) {
  2796. this.update();
  2797. }
  2798. },
  2799. _setScale: function PDFViewer_setScale(value, noScroll) {
  2800. var scale = parseFloat(value);
  2801. if (scale > 0) {
  2802. this._setScaleUpdatePages(scale, value, noScroll, false);
  2803. } else {
  2804. var currentPage = this._pages[this._currentPageNumber - 1];
  2805. if (!currentPage) {
  2806. return;
  2807. }
  2808. var hPadding = this.isInPresentationMode || this.removePageBorders ? 0 : SCROLLBAR_PADDING;
  2809. var vPadding = this.isInPresentationMode || this.removePageBorders ? 0 : VERTICAL_PADDING;
  2810. var pageWidthScale = (this.container.clientWidth - hPadding) / currentPage.width * currentPage.scale;
  2811. var pageHeightScale = (this.container.clientHeight - vPadding) / currentPage.height * currentPage.scale;
  2812. switch (value) {
  2813. case 'page-actual':
  2814. scale = 1;
  2815. break;
  2816. case 'page-width':
  2817. scale = pageWidthScale;
  2818. break;
  2819. case 'page-height':
  2820. scale = pageHeightScale;
  2821. break;
  2822. case 'page-fit':
  2823. scale = Math.min(pageWidthScale, pageHeightScale);
  2824. break;
  2825. case 'auto':
  2826. var isLandscape = currentPage.width > currentPage.height;
  2827. var horizontalScale = isLandscape ? Math.min(pageHeightScale, pageWidthScale) : pageWidthScale;
  2828. scale = Math.min(MAX_AUTO_SCALE, horizontalScale);
  2829. break;
  2830. default:
  2831. console.error('PDFViewer_setScale: "' + value + '" is an unknown zoom value.');
  2832. return;
  2833. }
  2834. this._setScaleUpdatePages(scale, value, noScroll, true);
  2835. }
  2836. },
  2837. _resetCurrentPageView: function () {
  2838. if (this.isInPresentationMode) {
  2839. this._setScale(this._currentScaleValue, true);
  2840. }
  2841. var pageView = this._pages[this._currentPageNumber - 1];
  2842. scrollIntoView(pageView.div);
  2843. },
  2844. scrollPageIntoView: function PDFViewer_scrollPageIntoView(params) {
  2845. if (!this.pdfDocument) {
  2846. return;
  2847. }
  2848. if (arguments.length > 1 || typeof params === 'number') {
  2849. console.warn('Call of scrollPageIntoView() with obsolete signature.');
  2850. var paramObj = {};
  2851. if (typeof params === 'number') {
  2852. paramObj.pageNumber = params;
  2853. }
  2854. if (arguments[1] instanceof Array) {
  2855. paramObj.destArray = arguments[1];
  2856. }
  2857. params = paramObj;
  2858. }
  2859. var pageNumber = params.pageNumber || 0;
  2860. var dest = params.destArray || null;
  2861. var allowNegativeOffset = params.allowNegativeOffset || false;
  2862. if (this.isInPresentationMode || !dest) {
  2863. this._setCurrentPageNumber(pageNumber, true);
  2864. return;
  2865. }
  2866. var pageView = this._pages[pageNumber - 1];
  2867. if (!pageView) {
  2868. console.error('PDFViewer_scrollPageIntoView: ' + 'Invalid "pageNumber" parameter.');
  2869. return;
  2870. }
  2871. var x = 0, y = 0;
  2872. var width = 0, height = 0, widthScale, heightScale;
  2873. var changeOrientation = pageView.rotation % 180 === 0 ? false : true;
  2874. var pageWidth = (changeOrientation ? pageView.height : pageView.width) / pageView.scale / CSS_UNITS;
  2875. var pageHeight = (changeOrientation ? pageView.width : pageView.height) / pageView.scale / CSS_UNITS;
  2876. var scale = 0;
  2877. switch (dest[1].name) {
  2878. case 'XYZ':
  2879. x = dest[2];
  2880. y = dest[3];
  2881. scale = dest[4];
  2882. x = x !== null ? x : 0;
  2883. y = y !== null ? y : pageHeight;
  2884. break;
  2885. case 'Fit':
  2886. case 'FitB':
  2887. scale = 'page-fit';
  2888. break;
  2889. case 'FitH':
  2890. case 'FitBH':
  2891. y = dest[2];
  2892. scale = 'page-width';
  2893. if (y === null && this._location) {
  2894. x = this._location.left;
  2895. y = this._location.top;
  2896. }
  2897. break;
  2898. case 'FitV':
  2899. case 'FitBV':
  2900. x = dest[2];
  2901. width = pageWidth;
  2902. height = pageHeight;
  2903. scale = 'page-height';
  2904. break;
  2905. case 'FitR':
  2906. x = dest[2];
  2907. y = dest[3];
  2908. width = dest[4] - x;
  2909. height = dest[5] - y;
  2910. var hPadding = this.removePageBorders ? 0 : SCROLLBAR_PADDING;
  2911. var vPadding = this.removePageBorders ? 0 : VERTICAL_PADDING;
  2912. widthScale = (this.container.clientWidth - hPadding) / width / CSS_UNITS;
  2913. heightScale = (this.container.clientHeight - vPadding) / height / CSS_UNITS;
  2914. scale = Math.min(Math.abs(widthScale), Math.abs(heightScale));
  2915. break;
  2916. default:
  2917. console.error('PDFViewer_scrollPageIntoView: \'' + dest[1].name + '\' is not a valid destination type.');
  2918. return;
  2919. }
  2920. if (scale && scale !== this._currentScale) {
  2921. this.currentScaleValue = scale;
  2922. } else if (this._currentScale === UNKNOWN_SCALE) {
  2923. this.currentScaleValue = DEFAULT_SCALE_VALUE;
  2924. }
  2925. if (scale === 'page-fit' && !dest[4]) {
  2926. scrollIntoView(pageView.div);
  2927. return;
  2928. }
  2929. var boundingRect = [
  2930. pageView.viewport.convertToViewportPoint(x, y),
  2931. pageView.viewport.convertToViewportPoint(x + width, y + height)
  2932. ];
  2933. var left = Math.min(boundingRect[0][0], boundingRect[1][0]);
  2934. var top = Math.min(boundingRect[0][1], boundingRect[1][1]);
  2935. if (!allowNegativeOffset) {
  2936. left = Math.max(left, 0);
  2937. top = Math.max(top, 0);
  2938. }
  2939. scrollIntoView(pageView.div, {
  2940. left: left,
  2941. top: top
  2942. });
  2943. },
  2944. _updateLocation: function (firstPage) {
  2945. var currentScale = this._currentScale;
  2946. var currentScaleValue = this._currentScaleValue;
  2947. var normalizedScaleValue = parseFloat(currentScaleValue) === currentScale ? Math.round(currentScale * 10000) / 100 : currentScaleValue;
  2948. var pageNumber = firstPage.id;
  2949. var pdfOpenParams = '#page=' + pageNumber;
  2950. pdfOpenParams += '&zoom=' + normalizedScaleValue;
  2951. var currentPageView = this._pages[pageNumber - 1];
  2952. var container = this.container;
  2953. var topLeft = currentPageView.getPagePoint(container.scrollLeft - firstPage.x, container.scrollTop - firstPage.y);
  2954. var intLeft = Math.round(topLeft[0]);
  2955. var intTop = Math.round(topLeft[1]);
  2956. pdfOpenParams += ',' + intLeft + ',' + intTop;
  2957. this._location = {
  2958. pageNumber: pageNumber,
  2959. scale: normalizedScaleValue,
  2960. top: intTop,
  2961. left: intLeft,
  2962. pdfOpenParams: pdfOpenParams
  2963. };
  2964. },
  2965. update: function PDFViewer_update() {
  2966. var visible = this._getVisiblePages();
  2967. var visiblePages = visible.views;
  2968. if (visiblePages.length === 0) {
  2969. return;
  2970. }
  2971. var suggestedCacheSize = Math.max(DEFAULT_CACHE_SIZE, 2 * visiblePages.length + 1);
  2972. this._buffer.resize(suggestedCacheSize);
  2973. this.renderingQueue.renderHighestPriority(visible);
  2974. var currentId = this._currentPageNumber;
  2975. var firstPage = visible.first;
  2976. for (var i = 0, ii = visiblePages.length, stillFullyVisible = false; i < ii; ++i) {
  2977. var page = visiblePages[i];
  2978. if (page.percent < 100) {
  2979. break;
  2980. }
  2981. if (page.id === currentId) {
  2982. stillFullyVisible = true;
  2983. break;
  2984. }
  2985. }
  2986. if (!stillFullyVisible) {
  2987. currentId = visiblePages[0].id;
  2988. }
  2989. if (!this.isInPresentationMode) {
  2990. this._setCurrentPageNumber(currentId);
  2991. }
  2992. this._updateLocation(firstPage);
  2993. this.eventBus.dispatch('updateviewarea', {
  2994. source: this,
  2995. location: this._location
  2996. });
  2997. },
  2998. containsElement: function (element) {
  2999. return this.container.contains(element);
  3000. },
  3001. focus: function () {
  3002. this.container.focus();
  3003. },
  3004. get isInPresentationMode() {
  3005. return this.presentationModeState === PresentationModeState.FULLSCREEN;
  3006. },
  3007. get isChangingPresentationMode() {
  3008. return this.presentationModeState === PresentationModeState.CHANGING;
  3009. },
  3010. get isHorizontalScrollbarEnabled() {
  3011. return this.isInPresentationMode ? false : this.container.scrollWidth > this.container.clientWidth;
  3012. },
  3013. _getVisiblePages: function () {
  3014. if (!this.isInPresentationMode) {
  3015. return getVisibleElements(this.container, this._pages, true);
  3016. } else {
  3017. var visible = [];
  3018. var currentPage = this._pages[this._currentPageNumber - 1];
  3019. visible.push({
  3020. id: currentPage.id,
  3021. view: currentPage
  3022. });
  3023. return {
  3024. first: currentPage,
  3025. last: currentPage,
  3026. views: visible
  3027. };
  3028. }
  3029. },
  3030. cleanup: function () {
  3031. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  3032. if (this._pages[i] && this._pages[i].renderingState !== RenderingStates.FINISHED) {
  3033. this._pages[i].reset();
  3034. }
  3035. }
  3036. },
  3037. _cancelRendering: function PDFViewer_cancelRendering() {
  3038. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  3039. if (this._pages[i]) {
  3040. this._pages[i].cancelRendering();
  3041. }
  3042. }
  3043. },
  3044. _ensurePdfPageLoaded: function (pageView) {
  3045. if (pageView.pdfPage) {
  3046. return Promise.resolve(pageView.pdfPage);
  3047. }
  3048. var pageNumber = pageView.id;
  3049. if (this._pagesRequests[pageNumber]) {
  3050. return this._pagesRequests[pageNumber];
  3051. }
  3052. var promise = this.pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  3053. pageView.setPdfPage(pdfPage);
  3054. this._pagesRequests[pageNumber] = null;
  3055. return pdfPage;
  3056. }.bind(this));
  3057. this._pagesRequests[pageNumber] = promise;
  3058. return promise;
  3059. },
  3060. forceRendering: function (currentlyVisiblePages) {
  3061. var visiblePages = currentlyVisiblePages || this._getVisiblePages();
  3062. var pageView = this.renderingQueue.getHighestPriority(visiblePages, this._pages, this.scroll.down);
  3063. if (pageView) {
  3064. this._ensurePdfPageLoaded(pageView).then(function () {
  3065. this.renderingQueue.renderView(pageView);
  3066. }.bind(this));
  3067. return true;
  3068. }
  3069. return false;
  3070. },
  3071. getPageTextContent: function (pageIndex) {
  3072. return this.pdfDocument.getPage(pageIndex + 1).then(function (page) {
  3073. return page.getTextContent({ normalizeWhitespace: true });
  3074. });
  3075. },
  3076. createTextLayerBuilder: function (textLayerDiv, pageIndex, viewport, enhanceTextSelection) {
  3077. return new TextLayerBuilder({
  3078. textLayerDiv: textLayerDiv,
  3079. eventBus: this.eventBus,
  3080. pageIndex: pageIndex,
  3081. viewport: viewport,
  3082. findController: this.isInPresentationMode ? null : this.findController,
  3083. enhanceTextSelection: this.isInPresentationMode ? false : enhanceTextSelection
  3084. });
  3085. },
  3086. createAnnotationLayerBuilder: function (pageDiv, pdfPage, renderInteractiveForms) {
  3087. return new AnnotationLayerBuilder({
  3088. pageDiv: pageDiv,
  3089. pdfPage: pdfPage,
  3090. renderInteractiveForms: renderInteractiveForms,
  3091. linkService: this.linkService,
  3092. downloadManager: this.downloadManager
  3093. });
  3094. },
  3095. setFindController: function (findController) {
  3096. this.findController = findController;
  3097. },
  3098. getPagesOverview: function () {
  3099. return this._pages.map(function (pageView) {
  3100. var viewport = pageView.pdfPage.getViewport(1);
  3101. return {
  3102. width: viewport.width,
  3103. height: viewport.height
  3104. };
  3105. });
  3106. }
  3107. };
  3108. return PDFViewer;
  3109. }();
  3110. exports.PresentationModeState = PresentationModeState;
  3111. exports.PDFViewer = PDFViewer;
  3112. }));
  3113. }.call(pdfViewerLibs));
  3114. var PDFJS = pdfjsLib.PDFJS;
  3115. PDFJS.PDFViewer = pdfViewerLibs.pdfjsWebPDFViewer.PDFViewer;
  3116. PDFJS.PDFPageView = pdfViewerLibs.pdfjsWebPDFPageView.PDFPageView;
  3117. PDFJS.PDFLinkService = pdfViewerLibs.pdfjsWebPDFLinkService.PDFLinkService;
  3118. PDFJS.TextLayerBuilder = pdfViewerLibs.pdfjsWebTextLayerBuilder.TextLayerBuilder;
  3119. PDFJS.DefaultTextLayerFactory = pdfViewerLibs.pdfjsWebTextLayerBuilder.DefaultTextLayerFactory;
  3120. PDFJS.AnnotationLayerBuilder = pdfViewerLibs.pdfjsWebAnnotationLayerBuilder.AnnotationLayerBuilder;
  3121. PDFJS.DefaultAnnotationLayerFactory = pdfViewerLibs.pdfjsWebAnnotationLayerBuilder.DefaultAnnotationLayerFactory;
  3122. PDFJS.PDFHistory = pdfViewerLibs.pdfjsWebPDFHistory.PDFHistory;
  3123. PDFJS.PDFFindController = pdfViewerLibs.pdfjsWebPDFFindController.PDFFindController;
  3124. PDFJS.EventBus = pdfViewerLibs.pdfjsWebUIUtils.EventBus;
  3125. PDFJS.DownloadManager = pdfViewerLibs.pdfjsWebDownloadManager.DownloadManager;
  3126. PDFJS.ProgressBar = pdfViewerLibs.pdfjsWebUIUtils.ProgressBar;
  3127. exports.PDFJS = PDFJS;
  3128. }));