pdf_find_controller.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* Copyright 2012 Mozilla Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. 'use strict';
  17. /* globals PDFFindBar, PDFJS, FindStates, FirefoxCom, Promise */
  18. /**
  19. * Provides a "search" or "find" functionality for the PDF.
  20. * This object actually performs the search for a given string.
  21. */
  22. var PDFFindController = {
  23. startedTextExtraction: false,
  24. extractTextPromises: [],
  25. pendingFindMatches: {},
  26. // If active, find results will be highlighted.
  27. active: false,
  28. // Stores the text for each page.
  29. pageContents: [],
  30. pageMatches: [],
  31. // Currently selected match.
  32. selected: {
  33. pageIdx: -1,
  34. matchIdx: -1
  35. },
  36. // Where find algorithm currently is in the document.
  37. offset: {
  38. pageIdx: null,
  39. matchIdx: null
  40. },
  41. resumePageIdx: null,
  42. state: null,
  43. dirtyMatch: false,
  44. findTimeout: null,
  45. pdfPageSource: null,
  46. integratedFind: false,
  47. initialize: function(options) {
  48. if(typeof PDFFindBar === 'undefined' || PDFFindBar === null) {
  49. throw 'PDFFindController cannot be initialized ' +
  50. 'without a PDFFindController instance';
  51. }
  52. this.pdfPageSource = options.pdfPageSource;
  53. this.integratedFind = options.integratedFind;
  54. var events = [
  55. 'find',
  56. 'findagain',
  57. 'findhighlightallchange',
  58. 'findcasesensitivitychange'
  59. ];
  60. this.firstPagePromise = new Promise(function (resolve) {
  61. this.resolveFirstPage = resolve;
  62. }.bind(this));
  63. this.handleEvent = this.handleEvent.bind(this);
  64. for (var i = 0; i < events.length; i++) {
  65. window.addEventListener(events[i], this.handleEvent);
  66. }
  67. },
  68. reset: function pdfFindControllerReset() {
  69. this.startedTextExtraction = false;
  70. this.extractTextPromises = [];
  71. this.active = false;
  72. },
  73. calcFindMatch: function(pageIndex) {
  74. var pageContent = this.pageContents[pageIndex];
  75. var query = this.state.query;
  76. var caseSensitive = this.state.caseSensitive;
  77. var queryLen = query.length;
  78. if (queryLen === 0) {
  79. // Do nothing the matches should be wiped out already.
  80. return;
  81. }
  82. if (!caseSensitive) {
  83. pageContent = pageContent.toLowerCase();
  84. query = query.toLowerCase();
  85. }
  86. var matches = [];
  87. var matchIdx = -queryLen;
  88. while (true) {
  89. matchIdx = pageContent.indexOf(query, matchIdx + queryLen);
  90. if (matchIdx === -1) {
  91. break;
  92. }
  93. matches.push(matchIdx);
  94. }
  95. this.pageMatches[pageIndex] = matches;
  96. this.updatePage(pageIndex);
  97. if (this.resumePageIdx === pageIndex) {
  98. this.resumePageIdx = null;
  99. this.nextPageMatch();
  100. }
  101. },
  102. extractText: function() {
  103. if (this.startedTextExtraction) {
  104. return;
  105. }
  106. this.startedTextExtraction = true;
  107. this.pageContents = [];
  108. var extractTextPromisesResolves = [];
  109. for (var i = 0, ii = this.pdfPageSource.pdfDocument.numPages; i < ii; i++) {
  110. this.extractTextPromises.push(new Promise(function (resolve) {
  111. extractTextPromisesResolves.push(resolve);
  112. }));
  113. }
  114. var self = this;
  115. function extractPageText(pageIndex) {
  116. self.pdfPageSource.pages[pageIndex].getTextContent().then(
  117. function textContentResolved(bidiTexts) {
  118. var str = '';
  119. for (var i = 0; i < bidiTexts.length; i++) {
  120. str += bidiTexts[i].str;
  121. }
  122. // Store the pageContent as a string.
  123. self.pageContents.push(str);
  124. extractTextPromisesResolves[pageIndex](pageIndex);
  125. if ((pageIndex + 1) < self.pdfPageSource.pages.length)
  126. extractPageText(pageIndex + 1);
  127. }
  128. );
  129. }
  130. extractPageText(0);
  131. },
  132. handleEvent: function(e) {
  133. if (this.state === null || e.type !== 'findagain') {
  134. this.dirtyMatch = true;
  135. }
  136. this.state = e.detail;
  137. this.updateUIState(FindStates.FIND_PENDING);
  138. this.firstPagePromise.then(function() {
  139. this.extractText();
  140. clearTimeout(this.findTimeout);
  141. if (e.type === 'find') {
  142. // Only trigger the find action after 250ms of silence.
  143. this.findTimeout = setTimeout(this.nextMatch.bind(this), 250);
  144. } else {
  145. this.nextMatch();
  146. }
  147. }.bind(this));
  148. },
  149. updatePage: function(idx) {
  150. var page = this.pdfPageSource.pages[idx];
  151. if (this.selected.pageIdx === idx) {
  152. // If the page is selected, scroll the page into view, which triggers
  153. // rendering the page, which adds the textLayer. Once the textLayer is
  154. // build, it will scroll onto the selected match.
  155. page.scrollIntoView();
  156. }
  157. if (page.textLayer) {
  158. page.textLayer.updateMatches();
  159. }
  160. },
  161. nextMatch: function() {
  162. var previous = this.state.findPrevious;
  163. var currentPageIndex = this.pdfPageSource.page - 1;
  164. var numPages = this.pdfPageSource.pages.length;
  165. this.active = true;
  166. if (this.dirtyMatch) {
  167. // Need to recalculate the matches, reset everything.
  168. this.dirtyMatch = false;
  169. this.selected.pageIdx = this.selected.matchIdx = -1;
  170. this.offset.pageIdx = currentPageIndex;
  171. this.offset.matchIdx = null;
  172. this.hadMatch = false;
  173. this.resumePageIdx = null;
  174. this.pageMatches = [];
  175. var self = this;
  176. for (var i = 0; i < numPages; i++) {
  177. // Wipe out any previous highlighted matches.
  178. this.updatePage(i);
  179. // As soon as the text is extracted start finding the matches.
  180. if (!(i in this.pendingFindMatches)) {
  181. this.pendingFindMatches[i] = true;
  182. this.extractTextPromises[i].then(function(pageIdx) {
  183. delete self.pendingFindMatches[pageIdx];
  184. self.calcFindMatch(pageIdx);
  185. });
  186. }
  187. }
  188. }
  189. // If there's no query there's no point in searching.
  190. if (this.state.query === '') {
  191. this.updateUIState(FindStates.FIND_FOUND);
  192. return;
  193. }
  194. // If we're waiting on a page, we return since we can't do anything else.
  195. if (this.resumePageIdx) {
  196. return;
  197. }
  198. var offset = this.offset;
  199. // If there's already a matchIdx that means we are iterating through a
  200. // page's matches.
  201. if (offset.matchIdx !== null) {
  202. var numPageMatches = this.pageMatches[offset.pageIdx].length;
  203. if ((!previous && offset.matchIdx + 1 < numPageMatches) ||
  204. (previous && offset.matchIdx > 0)) {
  205. // The simple case, we just have advance the matchIdx to select the next
  206. // match on the page.
  207. this.hadMatch = true;
  208. offset.matchIdx = previous ? offset.matchIdx - 1 : offset.matchIdx + 1;
  209. this.updateMatch(true);
  210. return;
  211. }
  212. // We went beyond the current page's matches, so we advance to the next
  213. // page.
  214. this.advanceOffsetPage(previous);
  215. }
  216. // Start searching through the page.
  217. this.nextPageMatch();
  218. },
  219. matchesReady: function(matches) {
  220. var offset = this.offset;
  221. var numMatches = matches.length;
  222. var previous = this.state.findPrevious;
  223. if (numMatches) {
  224. // There were matches for the page, so initialize the matchIdx.
  225. this.hadMatch = true;
  226. offset.matchIdx = previous ? numMatches - 1 : 0;
  227. this.updateMatch(true);
  228. // matches were found
  229. return true;
  230. } else {
  231. // No matches attempt to search the next page.
  232. this.advanceOffsetPage(previous);
  233. if (offset.wrapped) {
  234. offset.matchIdx = null;
  235. if (!this.hadMatch) {
  236. // No point in wrapping there were no matches.
  237. this.updateMatch(false);
  238. // while matches were not found, searching for a page
  239. // with matches should nevertheless halt.
  240. return true;
  241. }
  242. }
  243. // matches were not found (and searching is not done)
  244. return false;
  245. }
  246. },
  247. nextPageMatch: function() {
  248. if (this.resumePageIdx !== null) {
  249. console.error('There can only be one pending page.');
  250. }
  251. do {
  252. var pageIdx = this.offset.pageIdx;
  253. var matches = this.pageMatches[pageIdx];
  254. if (!matches) {
  255. // The matches don't exist yet for processing by "matchesReady",
  256. // so set a resume point for when they do exist.
  257. this.resumePageIdx = pageIdx;
  258. break;
  259. }
  260. } while (!this.matchesReady(matches));
  261. },
  262. advanceOffsetPage: function(previous) {
  263. var offset = this.offset;
  264. var numPages = this.extractTextPromises.length;
  265. offset.pageIdx = previous ? offset.pageIdx - 1 : offset.pageIdx + 1;
  266. offset.matchIdx = null;
  267. if (offset.pageIdx >= numPages || offset.pageIdx < 0) {
  268. offset.pageIdx = previous ? numPages - 1 : 0;
  269. offset.wrapped = true;
  270. return;
  271. }
  272. },
  273. updateMatch: function(found) {
  274. var state = FindStates.FIND_NOTFOUND;
  275. var wrapped = this.offset.wrapped;
  276. this.offset.wrapped = false;
  277. if (found) {
  278. var previousPage = this.selected.pageIdx;
  279. this.selected.pageIdx = this.offset.pageIdx;
  280. this.selected.matchIdx = this.offset.matchIdx;
  281. state = wrapped ? FindStates.FIND_WRAPPED : FindStates.FIND_FOUND;
  282. // Update the currently selected page to wipe out any selected matches.
  283. if (previousPage !== -1 && previousPage !== this.selected.pageIdx) {
  284. this.updatePage(previousPage);
  285. }
  286. }
  287. this.updateUIState(state, this.state.findPrevious);
  288. if (this.selected.pageIdx !== -1) {
  289. this.updatePage(this.selected.pageIdx, true);
  290. }
  291. },
  292. updateUIState: function(state, previous) {
  293. if (this.integratedFind) {
  294. FirefoxCom.request('updateFindControlState',
  295. {result: state, findPrevious: previous});
  296. return;
  297. }
  298. PDFFindBar.updateUIState(state, previous);
  299. }
  300. };