text_layer.js 19 KB

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