text_layer_builder.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.DefaultTextLayerFactory = exports.TextLayerBuilder = void 0;
  27. var _pdf = require("../pdf");
  28. const EXPAND_DIVS_TIMEOUT = 300;
  29. class TextLayerBuilder {
  30. constructor({
  31. textLayerDiv,
  32. eventBus,
  33. pageIndex,
  34. viewport,
  35. findController = null,
  36. enhanceTextSelection = false
  37. }) {
  38. this.textLayerDiv = textLayerDiv;
  39. this.eventBus = eventBus;
  40. this.textContent = null;
  41. this.textContentItemsStr = [];
  42. this.textContentStream = null;
  43. this.renderingDone = false;
  44. this.pageIdx = pageIndex;
  45. this.pageNumber = this.pageIdx + 1;
  46. this.matches = [];
  47. this.viewport = viewport;
  48. this.textDivs = [];
  49. this.findController = findController;
  50. this.textLayerRenderTask = null;
  51. this.enhanceTextSelection = enhanceTextSelection;
  52. this._onUpdateTextLayerMatches = null;
  53. this._bindMouse();
  54. }
  55. _finishRendering() {
  56. this.renderingDone = true;
  57. if (!this.enhanceTextSelection) {
  58. const endOfContent = document.createElement("div");
  59. endOfContent.className = "endOfContent";
  60. this.textLayerDiv.appendChild(endOfContent);
  61. }
  62. this.eventBus.dispatch("textlayerrendered", {
  63. source: this,
  64. pageNumber: this.pageNumber,
  65. numTextDivs: this.textDivs.length
  66. });
  67. }
  68. render(timeout = 0) {
  69. if (!(this.textContent || this.textContentStream) || this.renderingDone) {
  70. return;
  71. }
  72. this.cancel();
  73. this.textDivs = [];
  74. const textLayerFrag = document.createDocumentFragment();
  75. this.textLayerRenderTask = (0, _pdf.renderTextLayer)({
  76. textContent: this.textContent,
  77. textContentStream: this.textContentStream,
  78. container: textLayerFrag,
  79. viewport: this.viewport,
  80. textDivs: this.textDivs,
  81. textContentItemsStr: this.textContentItemsStr,
  82. timeout,
  83. enhanceTextSelection: this.enhanceTextSelection
  84. });
  85. this.textLayerRenderTask.promise.then(() => {
  86. this.textLayerDiv.appendChild(textLayerFrag);
  87. this._finishRendering();
  88. this._updateMatches();
  89. }, function (reason) {});
  90. if (!this._onUpdateTextLayerMatches) {
  91. this._onUpdateTextLayerMatches = evt => {
  92. if (evt.pageIndex === this.pageIdx || evt.pageIndex === -1) {
  93. this._updateMatches();
  94. }
  95. };
  96. this.eventBus._on("updatetextlayermatches", this._onUpdateTextLayerMatches);
  97. }
  98. }
  99. cancel() {
  100. if (this.textLayerRenderTask) {
  101. this.textLayerRenderTask.cancel();
  102. this.textLayerRenderTask = null;
  103. }
  104. if (this._onUpdateTextLayerMatches) {
  105. this.eventBus._off("updatetextlayermatches", this._onUpdateTextLayerMatches);
  106. this._onUpdateTextLayerMatches = null;
  107. }
  108. }
  109. setTextContentStream(readableStream) {
  110. this.cancel();
  111. this.textContentStream = readableStream;
  112. }
  113. setTextContent(textContent) {
  114. this.cancel();
  115. this.textContent = textContent;
  116. }
  117. _convertMatches(matches, matchesLength) {
  118. if (!matches) {
  119. return [];
  120. }
  121. const {
  122. findController,
  123. textContentItemsStr
  124. } = this;
  125. let i = 0,
  126. iIndex = 0;
  127. const end = textContentItemsStr.length - 1;
  128. const queryLen = findController.state.query.length;
  129. const result = [];
  130. for (let m = 0, mm = matches.length; m < mm; m++) {
  131. let matchIdx = matches[m];
  132. while (i !== end && matchIdx >= iIndex + textContentItemsStr[i].length) {
  133. iIndex += textContentItemsStr[i].length;
  134. i++;
  135. }
  136. if (i === textContentItemsStr.length) {
  137. console.error("Could not find a matching mapping");
  138. }
  139. const match = {
  140. begin: {
  141. divIdx: i,
  142. offset: matchIdx - iIndex
  143. }
  144. };
  145. if (matchesLength) {
  146. matchIdx += matchesLength[m];
  147. } else {
  148. matchIdx += queryLen;
  149. }
  150. while (i !== end && matchIdx > iIndex + textContentItemsStr[i].length) {
  151. iIndex += textContentItemsStr[i].length;
  152. i++;
  153. }
  154. match.end = {
  155. divIdx: i,
  156. offset: matchIdx - iIndex
  157. };
  158. result.push(match);
  159. }
  160. return result;
  161. }
  162. _renderMatches(matches) {
  163. if (matches.length === 0) {
  164. return;
  165. }
  166. const {
  167. findController,
  168. pageIdx,
  169. textContentItemsStr,
  170. textDivs
  171. } = this;
  172. const isSelectedPage = pageIdx === findController.selected.pageIdx;
  173. const selectedMatchIdx = findController.selected.matchIdx;
  174. const highlightAll = findController.state.highlightAll;
  175. let prevEnd = null;
  176. const infinity = {
  177. divIdx: -1,
  178. offset: undefined
  179. };
  180. function beginText(begin, className) {
  181. const divIdx = begin.divIdx;
  182. textDivs[divIdx].textContent = "";
  183. appendTextToDiv(divIdx, 0, begin.offset, className);
  184. }
  185. function appendTextToDiv(divIdx, fromOffset, toOffset, className) {
  186. const div = textDivs[divIdx];
  187. const content = textContentItemsStr[divIdx].substring(fromOffset, toOffset);
  188. const node = document.createTextNode(content);
  189. if (className) {
  190. const span = document.createElement("span");
  191. span.className = className;
  192. span.appendChild(node);
  193. div.appendChild(span);
  194. return;
  195. }
  196. div.appendChild(node);
  197. }
  198. let i0 = selectedMatchIdx,
  199. i1 = i0 + 1;
  200. if (highlightAll) {
  201. i0 = 0;
  202. i1 = matches.length;
  203. } else if (!isSelectedPage) {
  204. return;
  205. }
  206. for (let i = i0; i < i1; i++) {
  207. const match = matches[i];
  208. const begin = match.begin;
  209. const end = match.end;
  210. const isSelected = isSelectedPage && i === selectedMatchIdx;
  211. const highlightSuffix = isSelected ? " selected" : "";
  212. if (isSelected) {
  213. findController.scrollMatchIntoView({
  214. element: textDivs[begin.divIdx],
  215. pageIndex: pageIdx,
  216. matchIndex: selectedMatchIdx
  217. });
  218. }
  219. if (!prevEnd || begin.divIdx !== prevEnd.divIdx) {
  220. if (prevEnd !== null) {
  221. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  222. }
  223. beginText(begin);
  224. } else {
  225. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, begin.offset);
  226. }
  227. if (begin.divIdx === end.divIdx) {
  228. appendTextToDiv(begin.divIdx, begin.offset, end.offset, "highlight" + highlightSuffix);
  229. } else {
  230. appendTextToDiv(begin.divIdx, begin.offset, infinity.offset, "highlight begin" + highlightSuffix);
  231. for (let n0 = begin.divIdx + 1, n1 = end.divIdx; n0 < n1; n0++) {
  232. textDivs[n0].className = "highlight middle" + highlightSuffix;
  233. }
  234. beginText(end, "highlight end" + highlightSuffix);
  235. }
  236. prevEnd = end;
  237. }
  238. if (prevEnd) {
  239. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  240. }
  241. }
  242. _updateMatches() {
  243. if (!this.renderingDone) {
  244. return;
  245. }
  246. const {
  247. findController,
  248. matches,
  249. pageIdx,
  250. textContentItemsStr,
  251. textDivs
  252. } = this;
  253. let clearedUntilDivIdx = -1;
  254. for (let i = 0, ii = matches.length; i < ii; i++) {
  255. const match = matches[i];
  256. const begin = Math.max(clearedUntilDivIdx, match.begin.divIdx);
  257. for (let n = begin, end = match.end.divIdx; n <= end; n++) {
  258. const div = textDivs[n];
  259. div.textContent = textContentItemsStr[n];
  260. div.className = "";
  261. }
  262. clearedUntilDivIdx = match.end.divIdx + 1;
  263. }
  264. if (!findController || !findController.highlightMatches) {
  265. return;
  266. }
  267. const pageMatches = findController.pageMatches[pageIdx] || null;
  268. const pageMatchesLength = findController.pageMatchesLength[pageIdx] || null;
  269. this.matches = this._convertMatches(pageMatches, pageMatchesLength);
  270. this._renderMatches(this.matches);
  271. }
  272. _bindMouse() {
  273. const div = this.textLayerDiv;
  274. let expandDivsTimer = null;
  275. div.addEventListener("mousedown", evt => {
  276. if (this.enhanceTextSelection && this.textLayerRenderTask) {
  277. this.textLayerRenderTask.expandTextDivs(true);
  278. if (expandDivsTimer) {
  279. clearTimeout(expandDivsTimer);
  280. expandDivsTimer = null;
  281. }
  282. return;
  283. }
  284. const end = div.querySelector(".endOfContent");
  285. if (!end) {
  286. return;
  287. }
  288. let adjustTop = evt.target !== div;
  289. adjustTop = adjustTop && window.getComputedStyle(end).getPropertyValue("-moz-user-select") !== "none";
  290. if (adjustTop) {
  291. const divBounds = div.getBoundingClientRect();
  292. const r = Math.max(0, (evt.pageY - divBounds.top) / divBounds.height);
  293. end.style.top = (r * 100).toFixed(2) + "%";
  294. }
  295. end.classList.add("active");
  296. });
  297. div.addEventListener("mouseup", () => {
  298. if (this.enhanceTextSelection && this.textLayerRenderTask) {
  299. expandDivsTimer = setTimeout(() => {
  300. if (this.textLayerRenderTask) {
  301. this.textLayerRenderTask.expandTextDivs(false);
  302. }
  303. expandDivsTimer = null;
  304. }, EXPAND_DIVS_TIMEOUT);
  305. return;
  306. }
  307. const end = div.querySelector(".endOfContent");
  308. if (!end) {
  309. return;
  310. }
  311. end.style.top = "";
  312. end.classList.remove("active");
  313. });
  314. }
  315. }
  316. exports.TextLayerBuilder = TextLayerBuilder;
  317. class DefaultTextLayerFactory {
  318. createTextLayerBuilder(textLayerDiv, pageIndex, viewport, enhanceTextSelection = false, eventBus) {
  319. return new TextLayerBuilder({
  320. textLayerDiv,
  321. pageIndex,
  322. viewport,
  323. enhanceTextSelection,
  324. eventBus
  325. });
  326. }
  327. }
  328. exports.DefaultTextLayerFactory = DefaultTextLayerFactory;