text_layer_builder.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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.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. 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;
  186. span.appendChild(node);
  187. div.appendChild(span);
  188. return;
  189. }
  190. div.appendChild(node);
  191. }
  192. let i0 = selectedMatchIdx,
  193. i1 = i0 + 1;
  194. if (highlightAll) {
  195. i0 = 0;
  196. i1 = matches.length;
  197. } else if (!isSelectedPage) {
  198. return;
  199. }
  200. for (let i = i0; i < i1; i++) {
  201. const match = matches[i];
  202. const begin = match.begin;
  203. const end = match.end;
  204. const isSelected = isSelectedPage && i === selectedMatchIdx;
  205. const highlightSuffix = isSelected ? " selected" : "";
  206. if (isSelected) {
  207. findController.scrollMatchIntoView({
  208. element: textDivs[begin.divIdx],
  209. pageIndex: pageIdx,
  210. matchIndex: selectedMatchIdx
  211. });
  212. }
  213. if (!prevEnd || begin.divIdx !== prevEnd.divIdx) {
  214. if (prevEnd !== null) {
  215. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  216. }
  217. beginText(begin);
  218. } else {
  219. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, begin.offset);
  220. }
  221. if (begin.divIdx === end.divIdx) {
  222. appendTextToDiv(begin.divIdx, begin.offset, end.offset, "highlight" + highlightSuffix);
  223. } else {
  224. appendTextToDiv(begin.divIdx, begin.offset, infinity.offset, "highlight begin" + highlightSuffix);
  225. for (let n0 = begin.divIdx + 1, n1 = end.divIdx; n0 < n1; n0++) {
  226. textDivs[n0].className = "highlight middle" + highlightSuffix;
  227. }
  228. beginText(end, "highlight end" + highlightSuffix);
  229. }
  230. prevEnd = end;
  231. }
  232. if (prevEnd) {
  233. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  234. }
  235. }
  236. _updateMatches() {
  237. if (!this.renderingDone) {
  238. return;
  239. }
  240. const {
  241. findController,
  242. matches,
  243. pageIdx,
  244. textContentItemsStr,
  245. textDivs
  246. } = this;
  247. let clearedUntilDivIdx = -1;
  248. for (let i = 0, ii = matches.length; i < ii; i++) {
  249. const match = matches[i];
  250. const begin = Math.max(clearedUntilDivIdx, match.begin.divIdx);
  251. for (let n = begin, end = match.end.divIdx; n <= end; n++) {
  252. const div = textDivs[n];
  253. div.textContent = textContentItemsStr[n];
  254. div.className = "";
  255. }
  256. clearedUntilDivIdx = match.end.divIdx + 1;
  257. }
  258. if (!findController || !findController.highlightMatches) {
  259. return;
  260. }
  261. const pageMatches = findController.pageMatches[pageIdx] || null;
  262. const pageMatchesLength = findController.pageMatchesLength[pageIdx] || null;
  263. this.matches = this._convertMatches(pageMatches, pageMatchesLength);
  264. this._renderMatches(this.matches);
  265. }
  266. _bindMouse() {
  267. const div = this.textLayerDiv;
  268. let expandDivsTimer = null;
  269. div.addEventListener("mousedown", evt => {
  270. if (this.enhanceTextSelection && this.textLayerRenderTask) {
  271. this.textLayerRenderTask.expandTextDivs(true);
  272. if (expandDivsTimer) {
  273. clearTimeout(expandDivsTimer);
  274. expandDivsTimer = null;
  275. }
  276. return;
  277. }
  278. const end = div.querySelector(".endOfContent");
  279. if (!end) {
  280. return;
  281. }
  282. let adjustTop = evt.target !== div;
  283. adjustTop = adjustTop && window.getComputedStyle(end).getPropertyValue("-moz-user-select") !== "none";
  284. if (adjustTop) {
  285. const divBounds = div.getBoundingClientRect();
  286. const r = Math.max(0, (evt.pageY - divBounds.top) / divBounds.height);
  287. end.style.top = (r * 100).toFixed(2) + "%";
  288. }
  289. end.classList.add("active");
  290. });
  291. div.addEventListener("mouseup", () => {
  292. if (this.enhanceTextSelection && this.textLayerRenderTask) {
  293. expandDivsTimer = setTimeout(() => {
  294. if (this.textLayerRenderTask) {
  295. this.textLayerRenderTask.expandTextDivs(false);
  296. }
  297. expandDivsTimer = null;
  298. }, EXPAND_DIVS_TIMEOUT);
  299. return;
  300. }
  301. const end = div.querySelector(".endOfContent");
  302. if (!end) {
  303. return;
  304. }
  305. end.style.top = "";
  306. end.classList.remove("active");
  307. });
  308. }
  309. }
  310. exports.TextLayerBuilder = TextLayerBuilder;
  311. class DefaultTextLayerFactory {
  312. createTextLayerBuilder(textLayerDiv, pageIndex, viewport, enhanceTextSelection = false, eventBus) {
  313. return new TextLayerBuilder({
  314. textLayerDiv,
  315. pageIndex,
  316. viewport,
  317. enhanceTextSelection,
  318. eventBus
  319. });
  320. }
  321. }
  322. exports.DefaultTextLayerFactory = DefaultTextLayerFactory;