2
0

text_layer.js 19 KB

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