pdf_viewer.js 106 KB

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