text_layer_builder.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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 = void 0;
  27. var _pdf = require("../pdf");
  28. class TextLayerBuilder {
  29. constructor({
  30. textLayerDiv,
  31. eventBus,
  32. pageIndex,
  33. viewport,
  34. highlighter = null,
  35. accessibilityManager = null
  36. }) {
  37. this.textLayerDiv = textLayerDiv;
  38. this.eventBus = eventBus;
  39. this.textContent = null;
  40. this.textContentItemsStr = [];
  41. this.textContentStream = null;
  42. this.renderingDone = false;
  43. this.pageNumber = pageIndex + 1;
  44. this.viewport = viewport;
  45. this.textDivs = [];
  46. this.textLayerRenderTask = null;
  47. this.highlighter = highlighter;
  48. this.accessibilityManager = accessibilityManager;
  49. this.#bindMouse();
  50. }
  51. #finishRendering() {
  52. this.renderingDone = true;
  53. const endOfContent = document.createElement("div");
  54. endOfContent.className = "endOfContent";
  55. this.textLayerDiv.append(endOfContent);
  56. this.eventBus.dispatch("textlayerrendered", {
  57. source: this,
  58. pageNumber: this.pageNumber,
  59. numTextDivs: this.textDivs.length
  60. });
  61. }
  62. render(timeout = 0) {
  63. if (!(this.textContent || this.textContentStream) || this.renderingDone) {
  64. return;
  65. }
  66. this.cancel();
  67. this.textDivs.length = 0;
  68. this.highlighter?.setTextMapping(this.textDivs, this.textContentItemsStr);
  69. this.accessibilityManager?.setTextMapping(this.textDivs);
  70. const textLayerFrag = document.createDocumentFragment();
  71. this.textLayerRenderTask = (0, _pdf.renderTextLayer)({
  72. textContent: this.textContent,
  73. textContentStream: this.textContentStream,
  74. container: textLayerFrag,
  75. viewport: this.viewport,
  76. textDivs: this.textDivs,
  77. textContentItemsStr: this.textContentItemsStr,
  78. timeout
  79. });
  80. this.textLayerRenderTask.promise.then(() => {
  81. this.textLayerDiv.append(textLayerFrag);
  82. this.#finishRendering();
  83. this.highlighter?.enable();
  84. this.accessibilityManager?.enable();
  85. }, function (reason) {});
  86. }
  87. cancel() {
  88. if (this.textLayerRenderTask) {
  89. this.textLayerRenderTask.cancel();
  90. this.textLayerRenderTask = null;
  91. }
  92. this.highlighter?.disable();
  93. this.accessibilityManager?.disable();
  94. }
  95. setTextContentStream(readableStream) {
  96. this.cancel();
  97. this.textContentStream = readableStream;
  98. }
  99. setTextContent(textContent) {
  100. this.cancel();
  101. this.textContent = textContent;
  102. }
  103. #bindMouse() {
  104. const div = this.textLayerDiv;
  105. div.addEventListener("mousedown", evt => {
  106. const end = div.querySelector(".endOfContent");
  107. if (!end) {
  108. return;
  109. }
  110. let adjustTop = evt.target !== div;
  111. adjustTop &&= getComputedStyle(end).getPropertyValue("-moz-user-select") !== "none";
  112. if (adjustTop) {
  113. const divBounds = div.getBoundingClientRect();
  114. const r = Math.max(0, (evt.pageY - divBounds.top) / divBounds.height);
  115. end.style.top = (r * 100).toFixed(2) + "%";
  116. }
  117. end.classList.add("active");
  118. });
  119. div.addEventListener("mouseup", () => {
  120. const end = div.querySelector(".endOfContent");
  121. if (!end) {
  122. return;
  123. }
  124. end.style.top = "";
  125. end.classList.remove("active");
  126. });
  127. }
  128. }
  129. exports.TextLayerBuilder = TextLayerBuilder;