text_layer.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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.renderTextLayer = renderTextLayer;
  27. var _util = require("../shared/util.js");
  28. const MAX_TEXT_DIVS_TO_RENDER = 100000;
  29. const DEFAULT_FONT_SIZE = 30;
  30. const DEFAULT_FONT_ASCENT = 0.8;
  31. const ascentCache = new Map();
  32. const AllWhitespaceRegexp = /^\s+$/g;
  33. function getAscent(fontFamily, ctx) {
  34. const cachedAscent = ascentCache.get(fontFamily);
  35. if (cachedAscent) {
  36. return cachedAscent;
  37. }
  38. ctx.save();
  39. ctx.font = `${DEFAULT_FONT_SIZE}px ${fontFamily}`;
  40. const metrics = ctx.measureText("");
  41. let ascent = metrics.fontBoundingBoxAscent;
  42. let descent = Math.abs(metrics.fontBoundingBoxDescent);
  43. if (ascent) {
  44. ctx.restore();
  45. const ratio = ascent / (ascent + descent);
  46. ascentCache.set(fontFamily, ratio);
  47. return ratio;
  48. }
  49. ctx.strokeStyle = "red";
  50. ctx.clearRect(0, 0, DEFAULT_FONT_SIZE, DEFAULT_FONT_SIZE);
  51. ctx.strokeText("g", 0, 0);
  52. let pixels = ctx.getImageData(0, 0, DEFAULT_FONT_SIZE, DEFAULT_FONT_SIZE).data;
  53. descent = 0;
  54. for (let i = pixels.length - 1 - 3; i >= 0; i -= 4) {
  55. if (pixels[i] > 0) {
  56. descent = Math.ceil(i / 4 / DEFAULT_FONT_SIZE);
  57. break;
  58. }
  59. }
  60. ctx.clearRect(0, 0, DEFAULT_FONT_SIZE, DEFAULT_FONT_SIZE);
  61. ctx.strokeText("A", 0, DEFAULT_FONT_SIZE);
  62. pixels = ctx.getImageData(0, 0, DEFAULT_FONT_SIZE, DEFAULT_FONT_SIZE).data;
  63. ascent = 0;
  64. for (let i = 0, ii = pixels.length; i < ii; i += 4) {
  65. if (pixels[i] > 0) {
  66. ascent = DEFAULT_FONT_SIZE - Math.floor(i / 4 / DEFAULT_FONT_SIZE);
  67. break;
  68. }
  69. }
  70. ctx.restore();
  71. if (ascent) {
  72. const ratio = ascent / (ascent + descent);
  73. ascentCache.set(fontFamily, ratio);
  74. return ratio;
  75. }
  76. ascentCache.set(fontFamily, DEFAULT_FONT_ASCENT);
  77. return DEFAULT_FONT_ASCENT;
  78. }
  79. function appendText(task, geom, styles, ctx) {
  80. const textDiv = document.createElement("span");
  81. const textDivProperties = {
  82. angle: 0,
  83. canvasWidth: 0,
  84. hasText: geom.str !== "",
  85. hasEOL: geom.hasEOL,
  86. originalTransform: null,
  87. paddingBottom: 0,
  88. paddingLeft: 0,
  89. paddingRight: 0,
  90. paddingTop: 0,
  91. scale: 1
  92. };
  93. task._textDivs.push(textDiv);
  94. const tx = _util.Util.transform(task._viewport.transform, geom.transform);
  95. let angle = Math.atan2(tx[1], tx[0]);
  96. const style = styles[geom.fontName];
  97. if (style.vertical) {
  98. angle += Math.PI / 2;
  99. }
  100. const fontHeight = Math.hypot(tx[2], tx[3]);
  101. const fontAscent = fontHeight * getAscent(style.fontFamily, ctx);
  102. let left, top;
  103. if (angle === 0) {
  104. left = tx[4];
  105. top = tx[5] - fontAscent;
  106. } else {
  107. left = tx[4] + fontAscent * Math.sin(angle);
  108. top = tx[5] - fontAscent * Math.cos(angle);
  109. }
  110. textDiv.style.left = `${left}px`;
  111. textDiv.style.top = `${top}px`;
  112. textDiv.style.fontSize = `${fontHeight}px`;
  113. textDiv.style.fontFamily = style.fontFamily;
  114. textDiv.setAttribute("role", "presentation");
  115. textDiv.textContent = geom.str;
  116. textDiv.dir = geom.dir;
  117. if (task._fontInspectorEnabled) {
  118. textDiv.dataset.fontName = geom.fontName;
  119. }
  120. if (angle !== 0) {
  121. textDivProperties.angle = angle * (180 / Math.PI);
  122. }
  123. let shouldScaleText = false;
  124. if (geom.str.length > 1 || task._enhanceTextSelection && AllWhitespaceRegexp.test(geom.str)) {
  125. shouldScaleText = true;
  126. } else if (geom.transform[0] !== geom.transform[3]) {
  127. const absScaleX = Math.abs(geom.transform[0]),
  128. absScaleY = Math.abs(geom.transform[3]);
  129. if (absScaleX !== absScaleY && Math.max(absScaleX, absScaleY) / Math.min(absScaleX, absScaleY) > 1.5) {
  130. shouldScaleText = true;
  131. }
  132. }
  133. if (shouldScaleText) {
  134. if (style.vertical) {
  135. textDivProperties.canvasWidth = geom.height * task._viewport.scale;
  136. } else {
  137. textDivProperties.canvasWidth = geom.width * task._viewport.scale;
  138. }
  139. }
  140. task._textDivProperties.set(textDiv, textDivProperties);
  141. if (task._textContentStream) {
  142. task._layoutText(textDiv);
  143. }
  144. if (task._enhanceTextSelection && textDivProperties.hasText) {
  145. let angleCos = 1,
  146. angleSin = 0;
  147. if (angle !== 0) {
  148. angleCos = Math.cos(angle);
  149. angleSin = Math.sin(angle);
  150. }
  151. const divWidth = (style.vertical ? geom.height : geom.width) * task._viewport.scale;
  152. const divHeight = fontHeight;
  153. let m, b;
  154. if (angle !== 0) {
  155. m = [angleCos, angleSin, -angleSin, angleCos, left, top];
  156. b = _util.Util.getAxialAlignedBoundingBox([0, 0, divWidth, divHeight], m);
  157. } else {
  158. b = [left, top, left + divWidth, top + divHeight];
  159. }
  160. task._bounds.push({
  161. left: b[0],
  162. top: b[1],
  163. right: b[2],
  164. bottom: b[3],
  165. div: textDiv,
  166. size: [divWidth, divHeight],
  167. m
  168. });
  169. }
  170. }
  171. function render(task) {
  172. if (task._canceled) {
  173. return;
  174. }
  175. const textDivs = task._textDivs;
  176. const capability = task._capability;
  177. const textDivsLength = textDivs.length;
  178. if (textDivsLength > MAX_TEXT_DIVS_TO_RENDER) {
  179. task._renderingDone = true;
  180. capability.resolve();
  181. return;
  182. }
  183. if (!task._textContentStream) {
  184. for (let i = 0; i < textDivsLength; i++) {
  185. task._layoutText(textDivs[i]);
  186. }
  187. }
  188. task._renderingDone = true;
  189. capability.resolve();
  190. }
  191. function findPositiveMin(ts, offset, count) {
  192. let result = 0;
  193. for (let i = 0; i < count; i++) {
  194. const t = ts[offset++];
  195. if (t > 0) {
  196. result = result ? Math.min(t, result) : t;
  197. }
  198. }
  199. return result;
  200. }
  201. function expand(task) {
  202. const bounds = task._bounds;
  203. const viewport = task._viewport;
  204. const expanded = expandBounds(viewport.width, viewport.height, bounds);
  205. for (let i = 0; i < expanded.length; i++) {
  206. const div = bounds[i].div;
  207. const divProperties = task._textDivProperties.get(div);
  208. if (divProperties.angle === 0) {
  209. divProperties.paddingLeft = bounds[i].left - expanded[i].left;
  210. divProperties.paddingTop = bounds[i].top - expanded[i].top;
  211. divProperties.paddingRight = expanded[i].right - bounds[i].right;
  212. divProperties.paddingBottom = expanded[i].bottom - bounds[i].bottom;
  213. task._textDivProperties.set(div, divProperties);
  214. continue;
  215. }
  216. const e = expanded[i],
  217. b = bounds[i];
  218. const m = b.m,
  219. c = m[0],
  220. s = m[1];
  221. const points = [[0, 0], [0, b.size[1]], [b.size[0], 0], b.size];
  222. const ts = new Float64Array(64);
  223. for (let j = 0, jj = points.length; j < jj; j++) {
  224. const t = _util.Util.applyTransform(points[j], m);
  225. ts[j + 0] = c && (e.left - t[0]) / c;
  226. ts[j + 4] = s && (e.top - t[1]) / s;
  227. ts[j + 8] = c && (e.right - t[0]) / c;
  228. ts[j + 12] = s && (e.bottom - t[1]) / s;
  229. ts[j + 16] = s && (e.left - t[0]) / -s;
  230. ts[j + 20] = c && (e.top - t[1]) / c;
  231. ts[j + 24] = s && (e.right - t[0]) / -s;
  232. ts[j + 28] = c && (e.bottom - t[1]) / c;
  233. ts[j + 32] = c && (e.left - t[0]) / -c;
  234. ts[j + 36] = s && (e.top - t[1]) / -s;
  235. ts[j + 40] = c && (e.right - t[0]) / -c;
  236. ts[j + 44] = s && (e.bottom - t[1]) / -s;
  237. ts[j + 48] = s && (e.left - t[0]) / s;
  238. ts[j + 52] = c && (e.top - t[1]) / -c;
  239. ts[j + 56] = s && (e.right - t[0]) / s;
  240. ts[j + 60] = c && (e.bottom - t[1]) / -c;
  241. }
  242. const boxScale = 1 + Math.min(Math.abs(c), Math.abs(s));
  243. divProperties.paddingLeft = findPositiveMin(ts, 32, 16) / boxScale;
  244. divProperties.paddingTop = findPositiveMin(ts, 48, 16) / boxScale;
  245. divProperties.paddingRight = findPositiveMin(ts, 0, 16) / boxScale;
  246. divProperties.paddingBottom = findPositiveMin(ts, 16, 16) / boxScale;
  247. task._textDivProperties.set(div, divProperties);
  248. }
  249. }
  250. function expandBounds(width, height, boxes) {
  251. const bounds = boxes.map(function (box, i) {
  252. return {
  253. x1: box.left,
  254. y1: box.top,
  255. x2: box.right,
  256. y2: box.bottom,
  257. index: i,
  258. x1New: undefined,
  259. x2New: undefined
  260. };
  261. });
  262. expandBoundsLTR(width, bounds);
  263. const expanded = new Array(boxes.length);
  264. for (const b of bounds) {
  265. const i = b.index;
  266. expanded[i] = {
  267. left: b.x1New,
  268. top: 0,
  269. right: b.x2New,
  270. bottom: 0
  271. };
  272. }
  273. boxes.map(function (box, i) {
  274. const e = expanded[i],
  275. b = bounds[i];
  276. b.x1 = box.top;
  277. b.y1 = width - e.right;
  278. b.x2 = box.bottom;
  279. b.y2 = width - e.left;
  280. b.index = i;
  281. b.x1New = undefined;
  282. b.x2New = undefined;
  283. });
  284. expandBoundsLTR(height, bounds);
  285. for (const b of bounds) {
  286. const i = b.index;
  287. expanded[i].top = b.x1New;
  288. expanded[i].bottom = b.x2New;
  289. }
  290. return expanded;
  291. }
  292. function expandBoundsLTR(width, bounds) {
  293. bounds.sort(function (a, b) {
  294. return a.x1 - b.x1 || a.index - b.index;
  295. });
  296. const fakeBoundary = {
  297. x1: -Infinity,
  298. y1: -Infinity,
  299. x2: 0,
  300. y2: Infinity,
  301. index: -1,
  302. x1New: 0,
  303. x2New: 0
  304. };
  305. const horizon = [{
  306. start: -Infinity,
  307. end: Infinity,
  308. boundary: fakeBoundary
  309. }];
  310. for (const boundary of bounds) {
  311. let i = 0;
  312. while (i < horizon.length && horizon[i].end <= boundary.y1) {
  313. i++;
  314. }
  315. let j = horizon.length - 1;
  316. while (j >= 0 && horizon[j].start >= boundary.y2) {
  317. j--;
  318. }
  319. let horizonPart, affectedBoundary;
  320. let q,
  321. k,
  322. maxXNew = -Infinity;
  323. for (q = i; q <= j; q++) {
  324. horizonPart = horizon[q];
  325. affectedBoundary = horizonPart.boundary;
  326. let xNew;
  327. if (affectedBoundary.x2 > boundary.x1) {
  328. xNew = affectedBoundary.index > boundary.index ? affectedBoundary.x1New : boundary.x1;
  329. } else if (affectedBoundary.x2New === undefined) {
  330. xNew = (affectedBoundary.x2 + boundary.x1) / 2;
  331. } else {
  332. xNew = affectedBoundary.x2New;
  333. }
  334. if (xNew > maxXNew) {
  335. maxXNew = xNew;
  336. }
  337. }
  338. boundary.x1New = maxXNew;
  339. for (q = i; q <= j; q++) {
  340. horizonPart = horizon[q];
  341. affectedBoundary = horizonPart.boundary;
  342. if (affectedBoundary.x2New === undefined) {
  343. if (affectedBoundary.x2 > boundary.x1) {
  344. if (affectedBoundary.index > boundary.index) {
  345. affectedBoundary.x2New = affectedBoundary.x2;
  346. }
  347. } else {
  348. affectedBoundary.x2New = maxXNew;
  349. }
  350. } else if (affectedBoundary.x2New > maxXNew) {
  351. affectedBoundary.x2New = Math.max(maxXNew, affectedBoundary.x2);
  352. }
  353. }
  354. const changedHorizon = [];
  355. let lastBoundary = null;
  356. for (q = i; q <= j; q++) {
  357. horizonPart = horizon[q];
  358. affectedBoundary = horizonPart.boundary;
  359. const useBoundary = affectedBoundary.x2 > boundary.x2 ? affectedBoundary : boundary;
  360. if (lastBoundary === useBoundary) {
  361. changedHorizon[changedHorizon.length - 1].end = horizonPart.end;
  362. } else {
  363. changedHorizon.push({
  364. start: horizonPart.start,
  365. end: horizonPart.end,
  366. boundary: useBoundary
  367. });
  368. lastBoundary = useBoundary;
  369. }
  370. }
  371. if (horizon[i].start < boundary.y1) {
  372. changedHorizon[0].start = boundary.y1;
  373. changedHorizon.unshift({
  374. start: horizon[i].start,
  375. end: boundary.y1,
  376. boundary: horizon[i].boundary
  377. });
  378. }
  379. if (boundary.y2 < horizon[j].end) {
  380. changedHorizon[changedHorizon.length - 1].end = boundary.y2;
  381. changedHorizon.push({
  382. start: boundary.y2,
  383. end: horizon[j].end,
  384. boundary: horizon[j].boundary
  385. });
  386. }
  387. for (q = i; q <= j; q++) {
  388. horizonPart = horizon[q];
  389. affectedBoundary = horizonPart.boundary;
  390. if (affectedBoundary.x2New !== undefined) {
  391. continue;
  392. }
  393. let used = false;
  394. for (k = i - 1; !used && k >= 0 && horizon[k].start >= affectedBoundary.y1; k--) {
  395. used = horizon[k].boundary === affectedBoundary;
  396. }
  397. for (k = j + 1; !used && k < horizon.length && horizon[k].end <= affectedBoundary.y2; k++) {
  398. used = horizon[k].boundary === affectedBoundary;
  399. }
  400. for (k = 0; !used && k < changedHorizon.length; k++) {
  401. used = changedHorizon[k].boundary === affectedBoundary;
  402. }
  403. if (!used) {
  404. affectedBoundary.x2New = maxXNew;
  405. }
  406. }
  407. Array.prototype.splice.apply(horizon, [i, j - i + 1].concat(changedHorizon));
  408. }
  409. for (const horizonPart of horizon) {
  410. const affectedBoundary = horizonPart.boundary;
  411. if (affectedBoundary.x2New === undefined) {
  412. affectedBoundary.x2New = Math.max(width, affectedBoundary.x2);
  413. }
  414. }
  415. }
  416. class TextLayerRenderTask {
  417. constructor({
  418. textContent,
  419. textContentStream,
  420. container,
  421. viewport,
  422. textDivs,
  423. textContentItemsStr,
  424. enhanceTextSelection
  425. }) {
  426. this._textContent = textContent;
  427. this._textContentStream = textContentStream;
  428. this._container = container;
  429. this._document = container.ownerDocument;
  430. this._viewport = viewport;
  431. this._textDivs = textDivs || [];
  432. this._textContentItemsStr = textContentItemsStr || [];
  433. this._enhanceTextSelection = !!enhanceTextSelection;
  434. this._fontInspectorEnabled = !!globalThis.FontInspector?.enabled;
  435. this._reader = null;
  436. this._layoutTextLastFontSize = null;
  437. this._layoutTextLastFontFamily = null;
  438. this._layoutTextCtx = null;
  439. this._textDivProperties = new WeakMap();
  440. this._renderingDone = false;
  441. this._canceled = false;
  442. this._capability = (0, _util.createPromiseCapability)();
  443. this._renderTimer = null;
  444. this._bounds = [];
  445. this._capability.promise.finally(() => {
  446. if (this._layoutTextCtx) {
  447. this._layoutTextCtx.canvas.width = 0;
  448. this._layoutTextCtx.canvas.height = 0;
  449. this._layoutTextCtx = null;
  450. }
  451. }).catch(() => {});
  452. }
  453. get promise() {
  454. return this._capability.promise;
  455. }
  456. cancel() {
  457. this._canceled = true;
  458. if (this._reader) {
  459. this._reader.cancel(new _util.AbortException("TextLayer task cancelled."));
  460. this._reader = null;
  461. }
  462. if (this._renderTimer !== null) {
  463. clearTimeout(this._renderTimer);
  464. this._renderTimer = null;
  465. }
  466. this._capability.reject(new Error("TextLayer task cancelled."));
  467. }
  468. _processItems(items, styleCache) {
  469. for (let i = 0, len = items.length; i < len; i++) {
  470. if (items[i].str === undefined) {
  471. if (items[i].type === "beginMarkedContentProps" || items[i].type === "beginMarkedContent") {
  472. const parent = this._container;
  473. this._container = document.createElement("span");
  474. this._container.classList.add("markedContent");
  475. if (items[i].id !== null) {
  476. this._container.setAttribute("id", `${items[i].id}`);
  477. }
  478. parent.appendChild(this._container);
  479. } else if (items[i].type === "endMarkedContent") {
  480. this._container = this._container.parentNode;
  481. }
  482. continue;
  483. }
  484. this._textContentItemsStr.push(items[i].str);
  485. appendText(this, items[i], styleCache, this._layoutTextCtx);
  486. }
  487. }
  488. _layoutText(textDiv) {
  489. const textDivProperties = this._textDivProperties.get(textDiv);
  490. let transform = "";
  491. if (textDivProperties.canvasWidth !== 0 && textDivProperties.hasText) {
  492. const {
  493. fontSize,
  494. fontFamily
  495. } = textDiv.style;
  496. if (fontSize !== this._layoutTextLastFontSize || fontFamily !== this._layoutTextLastFontFamily) {
  497. this._layoutTextCtx.font = `${fontSize} ${fontFamily}`;
  498. this._layoutTextLastFontSize = fontSize;
  499. this._layoutTextLastFontFamily = fontFamily;
  500. }
  501. const {
  502. width
  503. } = this._layoutTextCtx.measureText(textDiv.textContent);
  504. if (width > 0) {
  505. textDivProperties.scale = textDivProperties.canvasWidth / width;
  506. transform = `scaleX(${textDivProperties.scale})`;
  507. }
  508. }
  509. if (textDivProperties.angle !== 0) {
  510. transform = `rotate(${textDivProperties.angle}deg) ${transform}`;
  511. }
  512. if (transform.length > 0) {
  513. if (this._enhanceTextSelection) {
  514. textDivProperties.originalTransform = transform;
  515. }
  516. textDiv.style.transform = transform;
  517. }
  518. if (textDivProperties.hasText) {
  519. this._container.appendChild(textDiv);
  520. }
  521. if (textDivProperties.hasEOL) {
  522. const br = document.createElement("br");
  523. br.setAttribute("role", "presentation");
  524. this._container.appendChild(br);
  525. }
  526. }
  527. _render(timeout = 0) {
  528. const capability = (0, _util.createPromiseCapability)();
  529. let styleCache = Object.create(null);
  530. const canvas = this._document.createElement("canvas");
  531. canvas.height = canvas.width = DEFAULT_FONT_SIZE;
  532. canvas.mozOpaque = true;
  533. this._layoutTextCtx = canvas.getContext("2d", {
  534. alpha: false
  535. });
  536. if (this._textContent) {
  537. const textItems = this._textContent.items;
  538. const textStyles = this._textContent.styles;
  539. this._processItems(textItems, textStyles);
  540. capability.resolve();
  541. } else if (this._textContentStream) {
  542. const pump = () => {
  543. this._reader.read().then(({
  544. value,
  545. done
  546. }) => {
  547. if (done) {
  548. capability.resolve();
  549. return;
  550. }
  551. Object.assign(styleCache, value.styles);
  552. this._processItems(value.items, styleCache);
  553. pump();
  554. }, capability.reject);
  555. };
  556. this._reader = this._textContentStream.getReader();
  557. pump();
  558. } else {
  559. throw new Error('Neither "textContent" nor "textContentStream"' + " parameters specified.");
  560. }
  561. capability.promise.then(() => {
  562. styleCache = null;
  563. if (!timeout) {
  564. render(this);
  565. } else {
  566. this._renderTimer = setTimeout(() => {
  567. render(this);
  568. this._renderTimer = null;
  569. }, timeout);
  570. }
  571. }, this._capability.reject);
  572. }
  573. expandTextDivs(expandDivs = false) {
  574. if (!this._enhanceTextSelection || !this._renderingDone) {
  575. return;
  576. }
  577. if (this._bounds !== null) {
  578. expand(this);
  579. this._bounds = null;
  580. }
  581. const transformBuf = [],
  582. paddingBuf = [];
  583. for (let i = 0, ii = this._textDivs.length; i < ii; i++) {
  584. const div = this._textDivs[i];
  585. const divProps = this._textDivProperties.get(div);
  586. if (!divProps.hasText) {
  587. continue;
  588. }
  589. if (expandDivs) {
  590. transformBuf.length = 0;
  591. paddingBuf.length = 0;
  592. if (divProps.originalTransform) {
  593. transformBuf.push(divProps.originalTransform);
  594. }
  595. if (divProps.paddingTop > 0) {
  596. paddingBuf.push(`${divProps.paddingTop}px`);
  597. transformBuf.push(`translateY(${-divProps.paddingTop}px)`);
  598. } else {
  599. paddingBuf.push(0);
  600. }
  601. if (divProps.paddingRight > 0) {
  602. paddingBuf.push(`${divProps.paddingRight / divProps.scale}px`);
  603. } else {
  604. paddingBuf.push(0);
  605. }
  606. if (divProps.paddingBottom > 0) {
  607. paddingBuf.push(`${divProps.paddingBottom}px`);
  608. } else {
  609. paddingBuf.push(0);
  610. }
  611. if (divProps.paddingLeft > 0) {
  612. paddingBuf.push(`${divProps.paddingLeft / divProps.scale}px`);
  613. transformBuf.push(`translateX(${-divProps.paddingLeft / divProps.scale}px)`);
  614. } else {
  615. paddingBuf.push(0);
  616. }
  617. div.style.padding = paddingBuf.join(" ");
  618. if (transformBuf.length) {
  619. div.style.transform = transformBuf.join(" ");
  620. }
  621. } else {
  622. div.style.padding = null;
  623. div.style.transform = divProps.originalTransform;
  624. }
  625. }
  626. }
  627. }
  628. function renderTextLayer(renderParameters) {
  629. const task = new TextLayerRenderTask({
  630. textContent: renderParameters.textContent,
  631. textContentStream: renderParameters.textContentStream,
  632. container: renderParameters.container,
  633. viewport: renderParameters.viewport,
  634. textDivs: renderParameters.textDivs,
  635. textContentItemsStr: renderParameters.textContentItemsStr,
  636. enhanceTextSelection: renderParameters.enhanceTextSelection
  637. });
  638. task._render(renderParameters.timeout);
  639. return task;
  640. }