text.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseTextBounds = exports.TextBounds = void 0;
  4. var css_line_break_1 = require("css-line-break");
  5. var text_segmentation_1 = require("text-segmentation");
  6. var bounds_1 = require("./bounds");
  7. var features_1 = require("../../core/features");
  8. var TextBounds = /** @class */ (function () {
  9. function TextBounds(text, bounds) {
  10. this.text = text;
  11. this.bounds = bounds;
  12. }
  13. return TextBounds;
  14. }());
  15. exports.TextBounds = TextBounds;
  16. var parseTextBounds = function (context, value, styles, node) {
  17. var textList = breakText(value, styles);
  18. var textBounds = [];
  19. var offset = 0;
  20. textList.forEach(function (text) {
  21. if (styles.textDecorationLine.length || text.trim().length > 0) {
  22. if (features_1.FEATURES.SUPPORT_RANGE_BOUNDS) {
  23. if (!features_1.FEATURES.SUPPORT_WORD_BREAKING) {
  24. textBounds.push(new TextBounds(text, bounds_1.Bounds.fromDOMRectList(context, createRange(node, offset, text.length).getClientRects())));
  25. }
  26. else {
  27. textBounds.push(new TextBounds(text, getRangeBounds(context, node, offset, text.length)));
  28. }
  29. }
  30. else {
  31. var replacementNode = node.splitText(text.length);
  32. textBounds.push(new TextBounds(text, getWrapperBounds(context, node)));
  33. node = replacementNode;
  34. }
  35. }
  36. else if (!features_1.FEATURES.SUPPORT_RANGE_BOUNDS) {
  37. node = node.splitText(text.length);
  38. }
  39. offset += text.length;
  40. });
  41. return textBounds;
  42. };
  43. exports.parseTextBounds = parseTextBounds;
  44. var getWrapperBounds = function (context, node) {
  45. var ownerDocument = node.ownerDocument;
  46. if (ownerDocument) {
  47. var wrapper = ownerDocument.createElement('html2canvaswrapper');
  48. wrapper.appendChild(node.cloneNode(true));
  49. var parentNode = node.parentNode;
  50. if (parentNode) {
  51. parentNode.replaceChild(wrapper, node);
  52. var bounds = bounds_1.parseBounds(context, wrapper);
  53. if (wrapper.firstChild) {
  54. parentNode.replaceChild(wrapper.firstChild, wrapper);
  55. }
  56. return bounds;
  57. }
  58. }
  59. return bounds_1.Bounds.EMPTY;
  60. };
  61. var createRange = function (node, offset, length) {
  62. var ownerDocument = node.ownerDocument;
  63. if (!ownerDocument) {
  64. throw new Error('Node has no owner document');
  65. }
  66. var range = ownerDocument.createRange();
  67. range.setStart(node, offset);
  68. range.setEnd(node, offset + length);
  69. return range;
  70. };
  71. var getRangeBounds = function (context, node, offset, length) {
  72. return bounds_1.Bounds.fromClientRect(context, createRange(node, offset, length).getBoundingClientRect());
  73. };
  74. var breakText = function (value, styles) {
  75. return styles.letterSpacing !== 0 ? text_segmentation_1.splitGraphemes(value) : breakWords(value, styles);
  76. };
  77. // https://drafts.csswg.org/css-text/#word-separator
  78. var wordSeparators = [0x0020, 0x00a0, 0x1361, 0x10100, 0x10101, 0x1039, 0x1091];
  79. var breakWords = function (str, styles) {
  80. var breaker = css_line_break_1.LineBreaker(str, {
  81. lineBreak: styles.lineBreak,
  82. wordBreak: styles.overflowWrap === "break-word" /* BREAK_WORD */ ? 'break-word' : styles.wordBreak
  83. });
  84. var words = [];
  85. var bk;
  86. var _loop_1 = function () {
  87. if (bk.value) {
  88. var value = bk.value.slice();
  89. var codePoints = css_line_break_1.toCodePoints(value);
  90. var word_1 = '';
  91. codePoints.forEach(function (codePoint) {
  92. if (wordSeparators.indexOf(codePoint) === -1) {
  93. word_1 += css_line_break_1.fromCodePoint(codePoint);
  94. }
  95. else {
  96. if (word_1.length) {
  97. words.push(word_1);
  98. }
  99. words.push(css_line_break_1.fromCodePoint(codePoint));
  100. word_1 = '';
  101. }
  102. });
  103. if (word_1.length) {
  104. words.push(word_1);
  105. }
  106. }
  107. };
  108. while (!(bk = breaker.next()).done) {
  109. _loop_1();
  110. }
  111. return words;
  112. };
  113. //# sourceMappingURL=text.js.map