text_layer_builder.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* Copyright 2017 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. 'use strict';
  16. var domEvents = require('./dom_events.js');
  17. var pdfjsLib = require('./pdfjs.js');
  18. var EXPAND_DIVS_TIMEOUT = 300;
  19. var TextLayerBuilder = function TextLayerBuilderClosure() {
  20. function TextLayerBuilder(options) {
  21. this.textLayerDiv = options.textLayerDiv;
  22. this.eventBus = options.eventBus || domEvents.getGlobalEventBus();
  23. this.textContent = null;
  24. this.renderingDone = false;
  25. this.pageIdx = options.pageIndex;
  26. this.pageNumber = this.pageIdx + 1;
  27. this.matches = [];
  28. this.viewport = options.viewport;
  29. this.textDivs = [];
  30. this.findController = options.findController || null;
  31. this.textLayerRenderTask = null;
  32. this.enhanceTextSelection = options.enhanceTextSelection;
  33. this._bindMouse();
  34. }
  35. TextLayerBuilder.prototype = {
  36. _finishRendering: function TextLayerBuilder_finishRendering() {
  37. this.renderingDone = true;
  38. if (!this.enhanceTextSelection) {
  39. var endOfContent = document.createElement('div');
  40. endOfContent.className = 'endOfContent';
  41. this.textLayerDiv.appendChild(endOfContent);
  42. }
  43. this.eventBus.dispatch('textlayerrendered', {
  44. source: this,
  45. pageNumber: this.pageNumber,
  46. numTextDivs: this.textDivs.length
  47. });
  48. },
  49. render: function TextLayerBuilder_render(timeout) {
  50. if (!this.textContent || this.renderingDone) {
  51. return;
  52. }
  53. this.cancel();
  54. this.textDivs = [];
  55. var textLayerFrag = document.createDocumentFragment();
  56. this.textLayerRenderTask = pdfjsLib.renderTextLayer({
  57. textContent: this.textContent,
  58. container: textLayerFrag,
  59. viewport: this.viewport,
  60. textDivs: this.textDivs,
  61. timeout: timeout,
  62. enhanceTextSelection: this.enhanceTextSelection
  63. });
  64. this.textLayerRenderTask.promise.then(function () {
  65. this.textLayerDiv.appendChild(textLayerFrag);
  66. this._finishRendering();
  67. this.updateMatches();
  68. }.bind(this), function (reason) {
  69. });
  70. },
  71. cancel: function TextLayerBuilder_cancel() {
  72. if (this.textLayerRenderTask) {
  73. this.textLayerRenderTask.cancel();
  74. this.textLayerRenderTask = null;
  75. }
  76. },
  77. setTextContent: function TextLayerBuilder_setTextContent(textContent) {
  78. this.cancel();
  79. this.textContent = textContent;
  80. },
  81. convertMatches: function TextLayerBuilder_convertMatches(matches, matchesLength) {
  82. var i = 0;
  83. var iIndex = 0;
  84. var bidiTexts = this.textContent.items;
  85. var end = bidiTexts.length - 1;
  86. var queryLen = this.findController === null ? 0 : this.findController.state.query.length;
  87. var ret = [];
  88. if (!matches) {
  89. return ret;
  90. }
  91. for (var m = 0, len = matches.length; m < len; m++) {
  92. var matchIdx = matches[m];
  93. while (i !== end && matchIdx >= iIndex + bidiTexts[i].str.length) {
  94. iIndex += bidiTexts[i].str.length;
  95. i++;
  96. }
  97. if (i === bidiTexts.length) {
  98. console.error('Could not find a matching mapping');
  99. }
  100. var match = {
  101. begin: {
  102. divIdx: i,
  103. offset: matchIdx - iIndex
  104. }
  105. };
  106. if (matchesLength) {
  107. matchIdx += matchesLength[m];
  108. } else {
  109. matchIdx += queryLen;
  110. }
  111. while (i !== end && matchIdx > iIndex + bidiTexts[i].str.length) {
  112. iIndex += bidiTexts[i].str.length;
  113. i++;
  114. }
  115. match.end = {
  116. divIdx: i,
  117. offset: matchIdx - iIndex
  118. };
  119. ret.push(match);
  120. }
  121. return ret;
  122. },
  123. renderMatches: function TextLayerBuilder_renderMatches(matches) {
  124. if (matches.length === 0) {
  125. return;
  126. }
  127. var bidiTexts = this.textContent.items;
  128. var textDivs = this.textDivs;
  129. var prevEnd = null;
  130. var pageIdx = this.pageIdx;
  131. var isSelectedPage = this.findController === null ? false : pageIdx === this.findController.selected.pageIdx;
  132. var selectedMatchIdx = this.findController === null ? -1 : this.findController.selected.matchIdx;
  133. var highlightAll = this.findController === null ? false : this.findController.state.highlightAll;
  134. var infinity = {
  135. divIdx: -1,
  136. offset: undefined
  137. };
  138. function beginText(begin, className) {
  139. var divIdx = begin.divIdx;
  140. textDivs[divIdx].textContent = '';
  141. appendTextToDiv(divIdx, 0, begin.offset, className);
  142. }
  143. function appendTextToDiv(divIdx, fromOffset, toOffset, className) {
  144. var div = textDivs[divIdx];
  145. var content = bidiTexts[divIdx].str.substring(fromOffset, toOffset);
  146. var node = document.createTextNode(content);
  147. if (className) {
  148. var span = document.createElement('span');
  149. span.className = className;
  150. span.appendChild(node);
  151. div.appendChild(span);
  152. return;
  153. }
  154. div.appendChild(node);
  155. }
  156. var i0 = selectedMatchIdx, i1 = i0 + 1;
  157. if (highlightAll) {
  158. i0 = 0;
  159. i1 = matches.length;
  160. } else if (!isSelectedPage) {
  161. return;
  162. }
  163. for (var i = i0; i < i1; i++) {
  164. var match = matches[i];
  165. var begin = match.begin;
  166. var end = match.end;
  167. var isSelected = isSelectedPage && i === selectedMatchIdx;
  168. var highlightSuffix = isSelected ? ' selected' : '';
  169. if (this.findController) {
  170. this.findController.updateMatchPosition(pageIdx, i, textDivs, begin.divIdx);
  171. }
  172. if (!prevEnd || begin.divIdx !== prevEnd.divIdx) {
  173. if (prevEnd !== null) {
  174. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  175. }
  176. beginText(begin);
  177. } else {
  178. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, begin.offset);
  179. }
  180. if (begin.divIdx === end.divIdx) {
  181. appendTextToDiv(begin.divIdx, begin.offset, end.offset, 'highlight' + highlightSuffix);
  182. } else {
  183. appendTextToDiv(begin.divIdx, begin.offset, infinity.offset, 'highlight begin' + highlightSuffix);
  184. for (var n0 = begin.divIdx + 1, n1 = end.divIdx; n0 < n1; n0++) {
  185. textDivs[n0].className = 'highlight middle' + highlightSuffix;
  186. }
  187. beginText(end, 'highlight end' + highlightSuffix);
  188. }
  189. prevEnd = end;
  190. }
  191. if (prevEnd) {
  192. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  193. }
  194. },
  195. updateMatches: function TextLayerBuilder_updateMatches() {
  196. if (!this.renderingDone) {
  197. return;
  198. }
  199. var matches = this.matches;
  200. var textDivs = this.textDivs;
  201. var bidiTexts = this.textContent.items;
  202. var clearedUntilDivIdx = -1;
  203. for (var i = 0, len = matches.length; i < len; i++) {
  204. var match = matches[i];
  205. var begin = Math.max(clearedUntilDivIdx, match.begin.divIdx);
  206. for (var n = begin, end = match.end.divIdx; n <= end; n++) {
  207. var div = textDivs[n];
  208. div.textContent = bidiTexts[n].str;
  209. div.className = '';
  210. }
  211. clearedUntilDivIdx = match.end.divIdx + 1;
  212. }
  213. if (this.findController === null || !this.findController.active) {
  214. return;
  215. }
  216. var pageMatches, pageMatchesLength;
  217. if (this.findController !== null) {
  218. pageMatches = this.findController.pageMatches[this.pageIdx] || null;
  219. pageMatchesLength = this.findController.pageMatchesLength ? this.findController.pageMatchesLength[this.pageIdx] || null : null;
  220. }
  221. this.matches = this.convertMatches(pageMatches, pageMatchesLength);
  222. this.renderMatches(this.matches);
  223. },
  224. _bindMouse: function TextLayerBuilder_bindMouse() {
  225. var div = this.textLayerDiv;
  226. var self = this;
  227. var expandDivsTimer = null;
  228. div.addEventListener('mousedown', function (e) {
  229. if (self.enhanceTextSelection && self.textLayerRenderTask) {
  230. self.textLayerRenderTask.expandTextDivs(true);
  231. if (expandDivsTimer) {
  232. clearTimeout(expandDivsTimer);
  233. expandDivsTimer = null;
  234. }
  235. return;
  236. }
  237. var end = div.querySelector('.endOfContent');
  238. if (!end) {
  239. return;
  240. }
  241. var adjustTop = e.target !== div;
  242. adjustTop = adjustTop && window.getComputedStyle(end).getPropertyValue('-moz-user-select') !== 'none';
  243. if (adjustTop) {
  244. var divBounds = div.getBoundingClientRect();
  245. var r = Math.max(0, (e.pageY - divBounds.top) / divBounds.height);
  246. end.style.top = (r * 100).toFixed(2) + '%';
  247. }
  248. end.classList.add('active');
  249. });
  250. div.addEventListener('mouseup', function (e) {
  251. if (self.enhanceTextSelection && self.textLayerRenderTask) {
  252. expandDivsTimer = setTimeout(function () {
  253. if (self.textLayerRenderTask) {
  254. self.textLayerRenderTask.expandTextDivs(false);
  255. }
  256. expandDivsTimer = null;
  257. }, EXPAND_DIVS_TIMEOUT);
  258. return;
  259. }
  260. var end = div.querySelector('.endOfContent');
  261. if (!end) {
  262. return;
  263. }
  264. end.style.top = '';
  265. end.classList.remove('active');
  266. });
  267. }
  268. };
  269. return TextLayerBuilder;
  270. }();
  271. function DefaultTextLayerFactory() {
  272. }
  273. DefaultTextLayerFactory.prototype = {
  274. createTextLayerBuilder: function (textLayerDiv, pageIndex, viewport, enhanceTextSelection) {
  275. return new TextLayerBuilder({
  276. textLayerDiv: textLayerDiv,
  277. pageIndex: pageIndex,
  278. viewport: viewport,
  279. enhanceTextSelection: enhanceTextSelection
  280. });
  281. }
  282. };
  283. exports.TextLayerBuilder = TextLayerBuilder;
  284. exports.DefaultTextLayerFactory = DefaultTextLayerFactory;