text.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var overflow_wrap_1 = require("../property-descriptors/overflow-wrap");
  4. var css_line_break_1 = require("css-line-break");
  5. var bounds_1 = require("./bounds");
  6. var features_1 = require("../../core/features");
  7. var TextBounds = /** @class */ (function () {
  8. function TextBounds(text, bounds) {
  9. this.text = text;
  10. this.bounds = bounds;
  11. }
  12. return TextBounds;
  13. }());
  14. exports.TextBounds = TextBounds;
  15. exports.parseTextBounds = function (value, styles, node) {
  16. var textList = breakText(value, styles);
  17. var textBounds = [];
  18. var offset = 0;
  19. textList.forEach(function (text) {
  20. if (styles.textDecorationLine.length || text.trim().length > 0) {
  21. if (features_1.FEATURES.SUPPORT_RANGE_BOUNDS) {
  22. textBounds.push(new TextBounds(text, getRangeBounds(node, offset, text.length)));
  23. }
  24. else {
  25. var replacementNode = node.splitText(text.length);
  26. textBounds.push(new TextBounds(text, getWrapperBounds(node)));
  27. node = replacementNode;
  28. }
  29. }
  30. else if (!features_1.FEATURES.SUPPORT_RANGE_BOUNDS) {
  31. node = node.splitText(text.length);
  32. }
  33. offset += text.length;
  34. });
  35. return textBounds;
  36. };
  37. var getWrapperBounds = function (node) {
  38. var ownerDocument = node.ownerDocument;
  39. if (ownerDocument) {
  40. var wrapper = ownerDocument.createElement('html2canvaswrapper');
  41. wrapper.appendChild(node.cloneNode(true));
  42. var parentNode = node.parentNode;
  43. if (parentNode) {
  44. parentNode.replaceChild(wrapper, node);
  45. var bounds = bounds_1.parseBounds(wrapper);
  46. if (wrapper.firstChild) {
  47. parentNode.replaceChild(wrapper.firstChild, wrapper);
  48. }
  49. return bounds;
  50. }
  51. }
  52. return new bounds_1.Bounds(0, 0, 0, 0);
  53. };
  54. var getRangeBounds = function (node, offset, length) {
  55. var ownerDocument = node.ownerDocument;
  56. if (!ownerDocument) {
  57. throw new Error('Node has no owner document');
  58. }
  59. var range = ownerDocument.createRange();
  60. range.setStart(node, offset);
  61. range.setEnd(node, offset + length);
  62. return bounds_1.Bounds.fromClientRect(range.getBoundingClientRect());
  63. };
  64. var breakText = function (value, styles) {
  65. return styles.letterSpacing !== 0 ? css_line_break_1.toCodePoints(value).map(function (i) { return css_line_break_1.fromCodePoint(i); }) : breakWords(value, styles);
  66. };
  67. var breakWords = function (str, styles) {
  68. var breaker = css_line_break_1.LineBreaker(str, {
  69. lineBreak: styles.lineBreak,
  70. wordBreak: styles.overflowWrap === overflow_wrap_1.OVERFLOW_WRAP.BREAK_WORD ? 'break-word' : styles.wordBreak
  71. });
  72. var words = [];
  73. var bk;
  74. while (!(bk = breaker.next()).done) {
  75. if (bk.value) {
  76. words.push(bk.value.slice());
  77. }
  78. }
  79. return words;
  80. };
  81. //# sourceMappingURL=text.js.map