text_layer.js 21 KB

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