text_layer_builder.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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.TextLayerBuilder = exports.DefaultTextLayerFactory = 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. textContentItemsStr
  123. } = this;
  124. let i = 0,
  125. iIndex = 0;
  126. const end = textContentItemsStr.length - 1;
  127. const result = [];
  128. for (let m = 0, mm = matches.length; m < mm; m++) {
  129. let matchIdx = matches[m];
  130. while (i !== end && matchIdx >= iIndex + textContentItemsStr[i].length) {
  131. iIndex += textContentItemsStr[i].length;
  132. i++;
  133. }
  134. if (i === textContentItemsStr.length) {
  135. console.error("Could not find a matching mapping");
  136. }
  137. const match = {
  138. begin: {
  139. divIdx: i,
  140. offset: matchIdx - iIndex
  141. }
  142. };
  143. matchIdx += matchesLength[m];
  144. while (i !== end && matchIdx > iIndex + textContentItemsStr[i].length) {
  145. iIndex += textContentItemsStr[i].length;
  146. i++;
  147. }
  148. match.end = {
  149. divIdx: i,
  150. offset: matchIdx - iIndex
  151. };
  152. result.push(match);
  153. }
  154. return result;
  155. }
  156. _renderMatches(matches) {
  157. if (matches.length === 0) {
  158. return;
  159. }
  160. const {
  161. findController,
  162. pageIdx,
  163. textContentItemsStr,
  164. textDivs
  165. } = this;
  166. const isSelectedPage = pageIdx === findController.selected.pageIdx;
  167. const selectedMatchIdx = findController.selected.matchIdx;
  168. const highlightAll = findController.state.highlightAll;
  169. let prevEnd = null;
  170. const infinity = {
  171. divIdx: -1,
  172. offset: undefined
  173. };
  174. function beginText(begin, className) {
  175. const divIdx = begin.divIdx;
  176. textDivs[divIdx].textContent = "";
  177. return appendTextToDiv(divIdx, 0, begin.offset, className);
  178. }
  179. function appendTextToDiv(divIdx, fromOffset, toOffset, className) {
  180. const div = textDivs[divIdx];
  181. const content = textContentItemsStr[divIdx].substring(fromOffset, toOffset);
  182. const node = document.createTextNode(content);
  183. if (className) {
  184. const span = document.createElement("span");
  185. span.className = `${className} appended`;
  186. span.appendChild(node);
  187. div.appendChild(span);
  188. return className.includes("selected") ? span.offsetLeft : 0;
  189. }
  190. div.appendChild(node);
  191. return 0;
  192. }
  193. let i0 = selectedMatchIdx,
  194. i1 = i0 + 1;
  195. if (highlightAll) {
  196. i0 = 0;
  197. i1 = matches.length;
  198. } else if (!isSelectedPage) {
  199. return;
  200. }
  201. for (let i = i0; i < i1; i++) {
  202. const match = matches[i];
  203. const begin = match.begin;
  204. const end = match.end;
  205. const isSelected = isSelectedPage && i === selectedMatchIdx;
  206. const highlightSuffix = isSelected ? " selected" : "";
  207. let selectedLeft = 0;
  208. if (!prevEnd || begin.divIdx !== prevEnd.divIdx) {
  209. if (prevEnd !== null) {
  210. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  211. }
  212. beginText(begin);
  213. } else {
  214. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, begin.offset);
  215. }
  216. if (begin.divIdx === end.divIdx) {
  217. selectedLeft = appendTextToDiv(begin.divIdx, begin.offset, end.offset, "highlight" + highlightSuffix);
  218. } else {
  219. selectedLeft = appendTextToDiv(begin.divIdx, begin.offset, infinity.offset, "highlight begin" + highlightSuffix);
  220. for (let n0 = begin.divIdx + 1, n1 = end.divIdx; n0 < n1; n0++) {
  221. textDivs[n0].className = "highlight middle" + highlightSuffix;
  222. }
  223. beginText(end, "highlight end" + highlightSuffix);
  224. }
  225. prevEnd = end;
  226. if (isSelected) {
  227. findController.scrollMatchIntoView({
  228. element: textDivs[begin.divIdx],
  229. selectedLeft,
  230. pageIndex: pageIdx,
  231. matchIndex: selectedMatchIdx
  232. });
  233. }
  234. }
  235. if (prevEnd) {
  236. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  237. }
  238. }
  239. _updateMatches() {
  240. if (!this.renderingDone) {
  241. return;
  242. }
  243. const {
  244. findController,
  245. matches,
  246. pageIdx,
  247. textContentItemsStr,
  248. textDivs
  249. } = this;
  250. let clearedUntilDivIdx = -1;
  251. for (let i = 0, ii = matches.length; i < ii; i++) {
  252. const match = matches[i];
  253. const begin = Math.max(clearedUntilDivIdx, match.begin.divIdx);
  254. for (let n = begin, end = match.end.divIdx; n <= end; n++) {
  255. const div = textDivs[n];
  256. div.textContent = textContentItemsStr[n];
  257. div.className = "";
  258. }
  259. clearedUntilDivIdx = match.end.divIdx + 1;
  260. }
  261. if (!findController?.highlightMatches) {
  262. return;
  263. }
  264. const pageMatches = findController.pageMatches[pageIdx] || null;
  265. const pageMatchesLength = findController.pageMatchesLength[pageIdx] || null;
  266. this.matches = this._convertMatches(pageMatches, pageMatchesLength);
  267. this._renderMatches(this.matches);
  268. }
  269. _bindMouse() {
  270. const div = this.textLayerDiv;
  271. let expandDivsTimer = null;
  272. div.addEventListener("mousedown", evt => {
  273. if (this.enhanceTextSelection && this.textLayerRenderTask) {
  274. this.textLayerRenderTask.expandTextDivs(true);
  275. if (expandDivsTimer) {
  276. clearTimeout(expandDivsTimer);
  277. expandDivsTimer = null;
  278. }
  279. return;
  280. }
  281. const end = div.querySelector(".endOfContent");
  282. if (!end) {
  283. return;
  284. }
  285. let adjustTop = evt.target !== div;
  286. adjustTop = adjustTop && window.getComputedStyle(end).getPropertyValue("-moz-user-select") !== "none";
  287. if (adjustTop) {
  288. const divBounds = div.getBoundingClientRect();
  289. const r = Math.max(0, (evt.pageY - divBounds.top) / divBounds.height);
  290. end.style.top = (r * 100).toFixed(2) + "%";
  291. }
  292. end.classList.add("active");
  293. });
  294. div.addEventListener("mouseup", () => {
  295. if (this.enhanceTextSelection && this.textLayerRenderTask) {
  296. expandDivsTimer = setTimeout(() => {
  297. if (this.textLayerRenderTask) {
  298. this.textLayerRenderTask.expandTextDivs(false);
  299. }
  300. expandDivsTimer = null;
  301. }, EXPAND_DIVS_TIMEOUT);
  302. return;
  303. }
  304. const end = div.querySelector(".endOfContent");
  305. if (!end) {
  306. return;
  307. }
  308. end.style.top = "";
  309. end.classList.remove("active");
  310. });
  311. }
  312. }
  313. exports.TextLayerBuilder = TextLayerBuilder;
  314. class DefaultTextLayerFactory {
  315. createTextLayerBuilder(textLayerDiv, pageIndex, viewport, enhanceTextSelection = false, eventBus) {
  316. return new TextLayerBuilder({
  317. textLayerDiv,
  318. pageIndex,
  319. viewport,
  320. enhanceTextSelection,
  321. eventBus
  322. });
  323. }
  324. }
  325. exports.DefaultTextLayerFactory = DefaultTextLayerFactory;