2
0

text_layer.js 21 KB

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