RichText.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. "use strict";
  2. exports.__esModule = true;
  3. var tslib_1 = require("tslib");
  4. var Element_1 = require("../Element");
  5. var parse_1 = require("./text/parse");
  6. var Text_1 = require("../graphic/Text");
  7. var util_1 = require("../core/util");
  8. var text_1 = require("../contain/text");
  9. var Image_1 = require("../graphic/Image");
  10. var Rect_1 = require("../graphic/shape/Rect");
  11. var RichText = (function (_super) {
  12. tslib_1.__extends(RichText, _super);
  13. function RichText(opts) {
  14. var _this = _super.call(this) || this;
  15. _this.isGroup = true;
  16. _this._children = [];
  17. _this._styleChanged = true;
  18. _this.attr(opts);
  19. return _this;
  20. }
  21. RichText.prototype.update = function () {
  22. if (this._styleChanged) {
  23. this._children = [];
  24. normalizeTextStyle(this.style);
  25. this.style.rich
  26. ? this._updateRichTexts()
  27. : this._updatePlainTexts();
  28. }
  29. _super.prototype.update.call(this);
  30. };
  31. RichText.prototype.attrKV = function (key, value) {
  32. if (key !== 'style') {
  33. _super.prototype.attrKV.call(this, key, value);
  34. }
  35. else {
  36. if (!this.style) {
  37. this.style = value;
  38. }
  39. else {
  40. this.setStyle(value);
  41. }
  42. }
  43. };
  44. RichText.prototype.setStyle = function (obj, value) {
  45. if (typeof obj === 'string') {
  46. this.style[obj] = value;
  47. }
  48. else {
  49. for (var key in obj) {
  50. if (obj.hasOwnProperty(key)) {
  51. this.style[key] = obj[key];
  52. }
  53. }
  54. }
  55. this.dirty();
  56. return this;
  57. };
  58. RichText.prototype.dirtyStyle = function () {
  59. this._styleChanged = true;
  60. this.dirty();
  61. };
  62. RichText.prototype.children = function () {
  63. return this._children;
  64. };
  65. RichText.prototype._addChild = function (child) {
  66. this._children.push(child);
  67. child.parent = this;
  68. };
  69. RichText.prototype._updatePlainTexts = function () {
  70. var style = this.style;
  71. var text = style.text || '';
  72. var textFont = style.font || text_1.DEFAULT_FONT;
  73. var textPadding = style.textPadding;
  74. var textLineHeight = style.textLineHeight;
  75. var contentBlock = parse_1.parsePlainText(text, textFont, textPadding, textLineHeight, style.truncate);
  76. var needDrawBg = needDrawBackground(style);
  77. var outerHeight = contentBlock.outerHeight;
  78. var textLines = contentBlock.lines;
  79. var lineHeight = contentBlock.lineHeight;
  80. var baseX = style.x || 0;
  81. var baseY = style.y || 0;
  82. var textAlign = style.textAlign || 'left';
  83. var textVerticalAlign = style.textVerticalAlign;
  84. var boxY = text_1.adjustTextY(baseY, outerHeight, textVerticalAlign);
  85. var textX = baseX;
  86. var textY = boxY;
  87. if (needDrawBg || textPadding) {
  88. var textWidth = text_1.getWidth(text, textFont);
  89. var outerWidth_1 = textWidth;
  90. textPadding && (outerWidth_1 += textPadding[1] + textPadding[3]);
  91. var boxX = text_1.adjustTextX(baseX, outerWidth_1, textAlign);
  92. needDrawBg && this._renderBackground(style, boxX, boxY, outerWidth_1, outerHeight);
  93. if (textPadding) {
  94. textX = getTextXForPadding(baseX, textAlign, textPadding);
  95. textY += textPadding[0];
  96. }
  97. }
  98. textY += lineHeight / 2;
  99. var textStrokeWidth = style.textStrokeWidth;
  100. var textStroke = getStroke(style.textStroke, textStrokeWidth);
  101. var textFill = getFill(style.textFill);
  102. var hasStroke = 'textStroke' in style;
  103. var hasFill = 'textFill' in style;
  104. var hasShadow = style.textShadowBlur > 0;
  105. for (var i = 0; i < textLines.length; i++) {
  106. var el = new Text_1["default"]();
  107. var subElStyle = el.style;
  108. subElStyle.text = textLines[i];
  109. subElStyle.x = textX;
  110. subElStyle.y = textY;
  111. if (textAlign) {
  112. subElStyle.textAlign = textAlign;
  113. }
  114. subElStyle.textBaseline = 'middle';
  115. subElStyle.opacity = style.opacity;
  116. subElStyle.strokeFirst = true;
  117. if (hasShadow) {
  118. subElStyle.shadowBlur = style.textShadowBlur || 0;
  119. subElStyle.shadowColor = style.textShadowColor || 'transparent';
  120. subElStyle.shadowOffsetX = style.textShadowOffsetX || 0;
  121. subElStyle.shadowOffsetY = style.textShadowOffsetY || 0;
  122. }
  123. if (hasStroke) {
  124. subElStyle.stroke = textStroke;
  125. subElStyle.lineWidth = textStrokeWidth;
  126. }
  127. if (hasFill) {
  128. subElStyle.fill = textFill;
  129. }
  130. subElStyle.font = textFont;
  131. textY += lineHeight;
  132. this._addChild(el);
  133. }
  134. };
  135. RichText.prototype._updateRichTexts = function () {
  136. var style = this.style;
  137. var contentBlock = parse_1.parseRichText(style.text || '', style);
  138. var contentWidth = contentBlock.width;
  139. var outerWidth = contentBlock.outerWidth;
  140. var outerHeight = contentBlock.outerHeight;
  141. var textPadding = style.textPadding;
  142. var baseX = style.x || 0;
  143. var baseY = style.y || 0;
  144. var textAlign = style.textAlign;
  145. var textVerticalAlign = style.textVerticalAlign;
  146. var boxX = text_1.adjustTextX(baseX, outerWidth, textAlign);
  147. var boxY = text_1.adjustTextY(baseY, outerHeight, textVerticalAlign);
  148. var xLeft = boxX;
  149. var lineTop = boxY;
  150. if (textPadding) {
  151. xLeft += textPadding[3];
  152. lineTop += textPadding[0];
  153. }
  154. var xRight = xLeft + contentWidth;
  155. for (var i = 0; i < contentBlock.lines.length; i++) {
  156. var line = contentBlock.lines[i];
  157. var tokens = line.tokens;
  158. var tokenCount = tokens.length;
  159. var lineHeight = line.lineHeight;
  160. var usedWidth = line.width;
  161. var leftIndex = 0;
  162. var lineXLeft = xLeft;
  163. var lineXRight = xRight;
  164. var rightIndex = tokenCount - 1;
  165. var token = void 0;
  166. while (leftIndex < tokenCount
  167. && (token = tokens[leftIndex], !token.textAlign || token.textAlign === 'left')) {
  168. this._placeToken(token, style, lineHeight, lineTop, lineXLeft, 'left');
  169. usedWidth -= token.width;
  170. lineXLeft += token.width;
  171. leftIndex++;
  172. }
  173. while (rightIndex >= 0
  174. && (token = tokens[rightIndex], token.textAlign === 'right')) {
  175. this._placeToken(token, style, lineHeight, lineTop, lineXRight, 'right');
  176. usedWidth -= token.width;
  177. lineXRight -= token.width;
  178. rightIndex--;
  179. }
  180. lineXLeft += (contentWidth - (lineXLeft - xLeft) - (xRight - lineXRight) - usedWidth) / 2;
  181. while (leftIndex <= rightIndex) {
  182. token = tokens[leftIndex];
  183. this._placeToken(token, style, lineHeight, lineTop, lineXLeft + token.width / 2, 'center');
  184. lineXLeft += token.width;
  185. leftIndex++;
  186. }
  187. lineTop += lineHeight;
  188. }
  189. };
  190. RichText.prototype._placeToken = function (token, style, lineHeight, lineTop, x, textAlign) {
  191. var tokenStyle = style.rich[token.styleName] || {};
  192. tokenStyle.text = token.text;
  193. var textVerticalAlign = token.textVerticalAlign;
  194. var y = lineTop + lineHeight / 2;
  195. if (textVerticalAlign === 'top') {
  196. y = lineTop + token.height / 2;
  197. }
  198. else if (textVerticalAlign === 'bottom') {
  199. y = lineTop + lineHeight - token.height / 2;
  200. }
  201. !token.isLineHolder && needDrawBackground(tokenStyle) && this._renderBackground(tokenStyle, textAlign === 'right'
  202. ? x - token.width
  203. : textAlign === 'center'
  204. ? x - token.width / 2
  205. : x, y - token.height / 2, token.width, token.height);
  206. var textPadding = token.textPadding;
  207. if (textPadding) {
  208. x = getTextXForPadding(x, textAlign, textPadding);
  209. y -= token.height / 2 - textPadding[2] - token.textHeight / 2;
  210. }
  211. var el = new Text_1["default"]();
  212. var subElStyle = el.style;
  213. var hasStroke = 'textStroke' in tokenStyle || 'textStroke' in style;
  214. var hasFill = 'textFill' in tokenStyle || 'textFill' in style;
  215. var hasShadow = tokenStyle.textShadowBlur > 0
  216. || style.textShadowBlur > 0;
  217. subElStyle.text = token.text;
  218. subElStyle.x = x;
  219. subElStyle.y = y;
  220. if (hasShadow) {
  221. subElStyle.shadowBlur = tokenStyle.textShadowBlur || style.textShadowBlur || 0;
  222. subElStyle.shadowColor = tokenStyle.textShadowColor || style.textShadowColor || 'transparent';
  223. subElStyle.shadowOffsetX = tokenStyle.textShadowOffsetX || style.textShadowOffsetX || 0;
  224. subElStyle.shadowOffsetY = tokenStyle.textShadowOffsetY || style.textShadowOffsetY || 0;
  225. }
  226. subElStyle.textAlign = textAlign;
  227. subElStyle.textBaseline = 'middle';
  228. subElStyle.font = token.font || text_1.DEFAULT_FONT;
  229. if (hasStroke) {
  230. subElStyle.lineWidth = util_1.retrieve2(tokenStyle.textStrokeWidth, style.textStrokeWidth);
  231. subElStyle.stroke = getStroke(tokenStyle.textStroke || style.textStroke, subElStyle.lineWidth) || null;
  232. }
  233. if (hasFill) {
  234. subElStyle.fill = getFill(tokenStyle.textFill || style.textFill) || null;
  235. }
  236. this._addChild(el);
  237. };
  238. RichText.prototype._renderBackground = function (style, x, y, width, height) {
  239. var textBackgroundColor = style.textBackgroundColor;
  240. var textBorderWidth = style.textBorderWidth;
  241. var textBorderColor = style.textBorderColor;
  242. var isPlainBg = util_1.isString(textBackgroundColor);
  243. var textBorderRadius = style.textBorderRadius;
  244. var rectEl;
  245. var imgEl;
  246. if (isPlainBg || (textBorderWidth && textBorderColor)) {
  247. rectEl = new Rect_1["default"]();
  248. var rectShape = rectEl.shape;
  249. rectShape.x = x;
  250. rectShape.y = y;
  251. rectShape.width = width;
  252. rectShape.height = height;
  253. rectShape.r = textBorderRadius;
  254. this._addChild(rectEl);
  255. }
  256. if (isPlainBg) {
  257. var rectStyle = rectEl.style;
  258. rectStyle.fill = textBackgroundColor;
  259. rectStyle.opacity = util_1.retrieve2(style.opacity, 1);
  260. rectStyle.fillOpacity = util_1.retrieve2(style.fillOpacity, 1);
  261. }
  262. else if (textBackgroundColor && textBackgroundColor.image) {
  263. imgEl = new Image_1["default"]();
  264. var imgStyle = imgEl.style;
  265. imgStyle.image = textBackgroundColor.image;
  266. imgStyle.x = x;
  267. imgStyle.y = y;
  268. imgStyle.width = width;
  269. imgStyle.height = height;
  270. this._addChild(imgEl);
  271. }
  272. if (textBorderWidth && textBorderColor) {
  273. var rectStyle = rectEl.style;
  274. rectStyle.lineWidth = textBorderWidth;
  275. rectStyle.stroke = textBorderColor;
  276. rectStyle.strokeOpacity = util_1.retrieve2(style.strokeOpacity, 1);
  277. }
  278. var shadowStyle = (rectEl || imgEl).style;
  279. shadowStyle.shadowBlur = style.textBoxShadowBlur || 0;
  280. shadowStyle.shadowColor = style.textBoxShadowColor || 'transparent';
  281. shadowStyle.shadowOffsetX = style.textBoxShadowOffsetX || 0;
  282. shadowStyle.shadowOffsetY = style.textBoxShadowOffsetY || 0;
  283. };
  284. return RichText;
  285. }(Element_1["default"]));
  286. var VALID_TEXT_ALIGN = { left: true, right: 1, center: 1 };
  287. var VALID_TEXT_VERTICAL_ALIGN = { top: 1, bottom: 1, middle: 1 };
  288. function normalizeTextStyle(style) {
  289. normalizeStyle(style);
  290. util_1.each(style.rich, normalizeStyle);
  291. return style;
  292. }
  293. exports.normalizeTextStyle = normalizeTextStyle;
  294. function normalizeStyle(style) {
  295. if (style) {
  296. style.font = makeFont(style);
  297. var textAlign = style.textAlign;
  298. textAlign === 'middle' && (textAlign = 'center');
  299. style.textAlign = (textAlign == null || VALID_TEXT_ALIGN[textAlign]) ? textAlign : 'left';
  300. var textVerticalAlign = style.textVerticalAlign;
  301. textVerticalAlign === 'center' && (textVerticalAlign = 'middle');
  302. style.textVerticalAlign = (textVerticalAlign == null || VALID_TEXT_VERTICAL_ALIGN[textVerticalAlign]) ? textVerticalAlign : 'top';
  303. var textPadding = style.textPadding;
  304. if (textPadding) {
  305. style.textPadding = util_1.normalizeCssArray(style.textPadding);
  306. }
  307. }
  308. }
  309. function getStroke(stroke, lineWidth) {
  310. return (stroke == null || lineWidth <= 0 || stroke === 'transparent' || stroke === 'none')
  311. ? null
  312. : (stroke.image || stroke.colorStops)
  313. ? '#000'
  314. : stroke;
  315. }
  316. function getFill(fill) {
  317. return (fill == null || fill === 'none')
  318. ? null
  319. : (fill.image || fill.colorStops)
  320. ? '#000'
  321. : fill;
  322. }
  323. function getTextXForPadding(x, textAlign, textPadding) {
  324. return textAlign === 'right'
  325. ? (x - textPadding[1])
  326. : textAlign === 'center'
  327. ? (x + textPadding[3] / 2 - textPadding[1] / 2)
  328. : (x + textPadding[3]);
  329. }
  330. function needDrawBackground(style) {
  331. return !!(style.textBackgroundColor
  332. || (style.textBorderWidth && style.textBorderColor));
  333. }
  334. function makeFont(style) {
  335. var font = (style.fontSize || style.fontFamily) && [
  336. style.fontStyle,
  337. style.fontWeight,
  338. (style.fontSize || 12) + 'px',
  339. style.fontFamily || 'sans-serif'
  340. ].join(' ');
  341. return font && util_1.trim(font) || style.textFont || style.font;
  342. }
  343. exports["default"] = RichText;
  344. //# sourceMappingURL=RichText.js.map