Text.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. import { __extends } from "tslib";
  2. import { parseRichText, parsePlainText } from './helper/parseText.js';
  3. import TSpan from './TSpan.js';
  4. import { retrieve2, each, normalizeCssArray, trim, retrieve3, extend, keys, defaults } from '../core/util.js';
  5. import { adjustTextX, adjustTextY } from '../contain/text.js';
  6. import ZRImage from './Image.js';
  7. import Rect from './shape/Rect.js';
  8. import BoundingRect from '../core/BoundingRect.js';
  9. import Displayable, { DEFAULT_COMMON_ANIMATION_PROPS } from './Displayable.js';
  10. import { DEFAULT_FONT, DEFAULT_FONT_SIZE } from '../core/platform.js';
  11. var DEFAULT_RICH_TEXT_COLOR = {
  12. fill: '#000'
  13. };
  14. var DEFAULT_STROKE_LINE_WIDTH = 2;
  15. export var DEFAULT_TEXT_ANIMATION_PROPS = {
  16. style: defaults({
  17. fill: true,
  18. stroke: true,
  19. fillOpacity: true,
  20. strokeOpacity: true,
  21. lineWidth: true,
  22. fontSize: true,
  23. lineHeight: true,
  24. width: true,
  25. height: true,
  26. textShadowColor: true,
  27. textShadowBlur: true,
  28. textShadowOffsetX: true,
  29. textShadowOffsetY: true,
  30. backgroundColor: true,
  31. padding: true,
  32. borderColor: true,
  33. borderWidth: true,
  34. borderRadius: true
  35. }, DEFAULT_COMMON_ANIMATION_PROPS.style)
  36. };
  37. var ZRText = (function (_super) {
  38. __extends(ZRText, _super);
  39. function ZRText(opts) {
  40. var _this = _super.call(this) || this;
  41. _this.type = 'text';
  42. _this._children = [];
  43. _this._defaultStyle = DEFAULT_RICH_TEXT_COLOR;
  44. _this.attr(opts);
  45. return _this;
  46. }
  47. ZRText.prototype.childrenRef = function () {
  48. return this._children;
  49. };
  50. ZRText.prototype.update = function () {
  51. _super.prototype.update.call(this);
  52. if (this.styleChanged()) {
  53. this._updateSubTexts();
  54. }
  55. for (var i = 0; i < this._children.length; i++) {
  56. var child = this._children[i];
  57. child.zlevel = this.zlevel;
  58. child.z = this.z;
  59. child.z2 = this.z2;
  60. child.culling = this.culling;
  61. child.cursor = this.cursor;
  62. child.invisible = this.invisible;
  63. }
  64. };
  65. ZRText.prototype.updateTransform = function () {
  66. var innerTransformable = this.innerTransformable;
  67. if (innerTransformable) {
  68. innerTransformable.updateTransform();
  69. if (innerTransformable.transform) {
  70. this.transform = innerTransformable.transform;
  71. }
  72. }
  73. else {
  74. _super.prototype.updateTransform.call(this);
  75. }
  76. };
  77. ZRText.prototype.getLocalTransform = function (m) {
  78. var innerTransformable = this.innerTransformable;
  79. return innerTransformable
  80. ? innerTransformable.getLocalTransform(m)
  81. : _super.prototype.getLocalTransform.call(this, m);
  82. };
  83. ZRText.prototype.getComputedTransform = function () {
  84. if (this.__hostTarget) {
  85. this.__hostTarget.getComputedTransform();
  86. this.__hostTarget.updateInnerText(true);
  87. }
  88. return _super.prototype.getComputedTransform.call(this);
  89. };
  90. ZRText.prototype._updateSubTexts = function () {
  91. this._childCursor = 0;
  92. normalizeTextStyle(this.style);
  93. this.style.rich
  94. ? this._updateRichTexts()
  95. : this._updatePlainTexts();
  96. this._children.length = this._childCursor;
  97. this.styleUpdated();
  98. };
  99. ZRText.prototype.addSelfToZr = function (zr) {
  100. _super.prototype.addSelfToZr.call(this, zr);
  101. for (var i = 0; i < this._children.length; i++) {
  102. this._children[i].__zr = zr;
  103. }
  104. };
  105. ZRText.prototype.removeSelfFromZr = function (zr) {
  106. _super.prototype.removeSelfFromZr.call(this, zr);
  107. for (var i = 0; i < this._children.length; i++) {
  108. this._children[i].__zr = null;
  109. }
  110. };
  111. ZRText.prototype.getBoundingRect = function () {
  112. if (this.styleChanged()) {
  113. this._updateSubTexts();
  114. }
  115. if (!this._rect) {
  116. var tmpRect = new BoundingRect(0, 0, 0, 0);
  117. var children = this._children;
  118. var tmpMat = [];
  119. var rect = null;
  120. for (var i = 0; i < children.length; i++) {
  121. var child = children[i];
  122. var childRect = child.getBoundingRect();
  123. var transform = child.getLocalTransform(tmpMat);
  124. if (transform) {
  125. tmpRect.copy(childRect);
  126. tmpRect.applyTransform(transform);
  127. rect = rect || tmpRect.clone();
  128. rect.union(tmpRect);
  129. }
  130. else {
  131. rect = rect || childRect.clone();
  132. rect.union(childRect);
  133. }
  134. }
  135. this._rect = rect || tmpRect;
  136. }
  137. return this._rect;
  138. };
  139. ZRText.prototype.setDefaultTextStyle = function (defaultTextStyle) {
  140. this._defaultStyle = defaultTextStyle || DEFAULT_RICH_TEXT_COLOR;
  141. };
  142. ZRText.prototype.setTextContent = function (textContent) {
  143. if (process.env.NODE_ENV !== 'production') {
  144. throw new Error('Can\'t attach text on another text');
  145. }
  146. };
  147. ZRText.prototype._mergeStyle = function (targetStyle, sourceStyle) {
  148. if (!sourceStyle) {
  149. return targetStyle;
  150. }
  151. var sourceRich = sourceStyle.rich;
  152. var targetRich = targetStyle.rich || (sourceRich && {});
  153. extend(targetStyle, sourceStyle);
  154. if (sourceRich && targetRich) {
  155. this._mergeRich(targetRich, sourceRich);
  156. targetStyle.rich = targetRich;
  157. }
  158. else if (targetRich) {
  159. targetStyle.rich = targetRich;
  160. }
  161. return targetStyle;
  162. };
  163. ZRText.prototype._mergeRich = function (targetRich, sourceRich) {
  164. var richNames = keys(sourceRich);
  165. for (var i = 0; i < richNames.length; i++) {
  166. var richName = richNames[i];
  167. targetRich[richName] = targetRich[richName] || {};
  168. extend(targetRich[richName], sourceRich[richName]);
  169. }
  170. };
  171. ZRText.prototype.getAnimationStyleProps = function () {
  172. return DEFAULT_TEXT_ANIMATION_PROPS;
  173. };
  174. ZRText.prototype._getOrCreateChild = function (Ctor) {
  175. var child = this._children[this._childCursor];
  176. if (!child || !(child instanceof Ctor)) {
  177. child = new Ctor();
  178. }
  179. this._children[this._childCursor++] = child;
  180. child.__zr = this.__zr;
  181. child.parent = this;
  182. return child;
  183. };
  184. ZRText.prototype._updatePlainTexts = function () {
  185. var style = this.style;
  186. var textFont = style.font || DEFAULT_FONT;
  187. var textPadding = style.padding;
  188. var text = getStyleText(style);
  189. var contentBlock = parsePlainText(text, style);
  190. var needDrawBg = needDrawBackground(style);
  191. var bgColorDrawn = !!(style.backgroundColor);
  192. var outerHeight = contentBlock.outerHeight;
  193. var outerWidth = contentBlock.outerWidth;
  194. var contentWidth = contentBlock.contentWidth;
  195. var textLines = contentBlock.lines;
  196. var lineHeight = contentBlock.lineHeight;
  197. var defaultStyle = this._defaultStyle;
  198. this.isTruncated = !!contentBlock.isTruncated;
  199. var baseX = style.x || 0;
  200. var baseY = style.y || 0;
  201. var textAlign = style.align || defaultStyle.align || 'left';
  202. var verticalAlign = style.verticalAlign || defaultStyle.verticalAlign || 'top';
  203. var textX = baseX;
  204. var textY = adjustTextY(baseY, contentBlock.contentHeight, verticalAlign);
  205. if (needDrawBg || textPadding) {
  206. var boxX = adjustTextX(baseX, outerWidth, textAlign);
  207. var boxY = adjustTextY(baseY, outerHeight, verticalAlign);
  208. needDrawBg && this._renderBackground(style, style, boxX, boxY, outerWidth, outerHeight);
  209. }
  210. textY += lineHeight / 2;
  211. if (textPadding) {
  212. textX = getTextXForPadding(baseX, textAlign, textPadding);
  213. if (verticalAlign === 'top') {
  214. textY += textPadding[0];
  215. }
  216. else if (verticalAlign === 'bottom') {
  217. textY -= textPadding[2];
  218. }
  219. }
  220. var defaultLineWidth = 0;
  221. var useDefaultFill = false;
  222. var textFill = getFill('fill' in style
  223. ? style.fill
  224. : (useDefaultFill = true, defaultStyle.fill));
  225. var textStroke = getStroke('stroke' in style
  226. ? style.stroke
  227. : (!bgColorDrawn
  228. && (!defaultStyle.autoStroke || useDefaultFill))
  229. ? (defaultLineWidth = DEFAULT_STROKE_LINE_WIDTH, defaultStyle.stroke)
  230. : null);
  231. var hasShadow = style.textShadowBlur > 0;
  232. var fixedBoundingRect = style.width != null
  233. && (style.overflow === 'truncate' || style.overflow === 'break' || style.overflow === 'breakAll');
  234. var calculatedLineHeight = contentBlock.calculatedLineHeight;
  235. for (var i = 0; i < textLines.length; i++) {
  236. var el = this._getOrCreateChild(TSpan);
  237. var subElStyle = el.createStyle();
  238. el.useStyle(subElStyle);
  239. subElStyle.text = textLines[i];
  240. subElStyle.x = textX;
  241. subElStyle.y = textY;
  242. if (textAlign) {
  243. subElStyle.textAlign = textAlign;
  244. }
  245. subElStyle.textBaseline = 'middle';
  246. subElStyle.opacity = style.opacity;
  247. subElStyle.strokeFirst = true;
  248. if (hasShadow) {
  249. subElStyle.shadowBlur = style.textShadowBlur || 0;
  250. subElStyle.shadowColor = style.textShadowColor || 'transparent';
  251. subElStyle.shadowOffsetX = style.textShadowOffsetX || 0;
  252. subElStyle.shadowOffsetY = style.textShadowOffsetY || 0;
  253. }
  254. subElStyle.stroke = textStroke;
  255. subElStyle.fill = textFill;
  256. if (textStroke) {
  257. subElStyle.lineWidth = style.lineWidth || defaultLineWidth;
  258. subElStyle.lineDash = style.lineDash;
  259. subElStyle.lineDashOffset = style.lineDashOffset || 0;
  260. }
  261. subElStyle.font = textFont;
  262. setSeparateFont(subElStyle, style);
  263. textY += lineHeight;
  264. if (fixedBoundingRect) {
  265. el.setBoundingRect(new BoundingRect(adjustTextX(subElStyle.x, contentWidth, subElStyle.textAlign), adjustTextY(subElStyle.y, calculatedLineHeight, subElStyle.textBaseline), contentWidth, calculatedLineHeight));
  266. }
  267. }
  268. };
  269. ZRText.prototype._updateRichTexts = function () {
  270. var style = this.style;
  271. var text = getStyleText(style);
  272. var contentBlock = parseRichText(text, style);
  273. var contentWidth = contentBlock.width;
  274. var outerWidth = contentBlock.outerWidth;
  275. var outerHeight = contentBlock.outerHeight;
  276. var textPadding = style.padding;
  277. var baseX = style.x || 0;
  278. var baseY = style.y || 0;
  279. var defaultStyle = this._defaultStyle;
  280. var textAlign = style.align || defaultStyle.align;
  281. var verticalAlign = style.verticalAlign || defaultStyle.verticalAlign;
  282. this.isTruncated = !!contentBlock.isTruncated;
  283. var boxX = adjustTextX(baseX, outerWidth, textAlign);
  284. var boxY = adjustTextY(baseY, outerHeight, verticalAlign);
  285. var xLeft = boxX;
  286. var lineTop = boxY;
  287. if (textPadding) {
  288. xLeft += textPadding[3];
  289. lineTop += textPadding[0];
  290. }
  291. var xRight = xLeft + contentWidth;
  292. if (needDrawBackground(style)) {
  293. this._renderBackground(style, style, boxX, boxY, outerWidth, outerHeight);
  294. }
  295. var bgColorDrawn = !!(style.backgroundColor);
  296. for (var i = 0; i < contentBlock.lines.length; i++) {
  297. var line = contentBlock.lines[i];
  298. var tokens = line.tokens;
  299. var tokenCount = tokens.length;
  300. var lineHeight = line.lineHeight;
  301. var remainedWidth = line.width;
  302. var leftIndex = 0;
  303. var lineXLeft = xLeft;
  304. var lineXRight = xRight;
  305. var rightIndex = tokenCount - 1;
  306. var token = void 0;
  307. while (leftIndex < tokenCount
  308. && (token = tokens[leftIndex], !token.align || token.align === 'left')) {
  309. this._placeToken(token, style, lineHeight, lineTop, lineXLeft, 'left', bgColorDrawn);
  310. remainedWidth -= token.width;
  311. lineXLeft += token.width;
  312. leftIndex++;
  313. }
  314. while (rightIndex >= 0
  315. && (token = tokens[rightIndex], token.align === 'right')) {
  316. this._placeToken(token, style, lineHeight, lineTop, lineXRight, 'right', bgColorDrawn);
  317. remainedWidth -= token.width;
  318. lineXRight -= token.width;
  319. rightIndex--;
  320. }
  321. lineXLeft += (contentWidth - (lineXLeft - xLeft) - (xRight - lineXRight) - remainedWidth) / 2;
  322. while (leftIndex <= rightIndex) {
  323. token = tokens[leftIndex];
  324. this._placeToken(token, style, lineHeight, lineTop, lineXLeft + token.width / 2, 'center', bgColorDrawn);
  325. lineXLeft += token.width;
  326. leftIndex++;
  327. }
  328. lineTop += lineHeight;
  329. }
  330. };
  331. ZRText.prototype._placeToken = function (token, style, lineHeight, lineTop, x, textAlign, parentBgColorDrawn) {
  332. var tokenStyle = style.rich[token.styleName] || {};
  333. tokenStyle.text = token.text;
  334. var verticalAlign = token.verticalAlign;
  335. var y = lineTop + lineHeight / 2;
  336. if (verticalAlign === 'top') {
  337. y = lineTop + token.height / 2;
  338. }
  339. else if (verticalAlign === 'bottom') {
  340. y = lineTop + lineHeight - token.height / 2;
  341. }
  342. var needDrawBg = !token.isLineHolder && needDrawBackground(tokenStyle);
  343. needDrawBg && this._renderBackground(tokenStyle, style, textAlign === 'right'
  344. ? x - token.width
  345. : textAlign === 'center'
  346. ? x - token.width / 2
  347. : x, y - token.height / 2, token.width, token.height);
  348. var bgColorDrawn = !!tokenStyle.backgroundColor;
  349. var textPadding = token.textPadding;
  350. if (textPadding) {
  351. x = getTextXForPadding(x, textAlign, textPadding);
  352. y -= token.height / 2 - textPadding[0] - token.innerHeight / 2;
  353. }
  354. var el = this._getOrCreateChild(TSpan);
  355. var subElStyle = el.createStyle();
  356. el.useStyle(subElStyle);
  357. var defaultStyle = this._defaultStyle;
  358. var useDefaultFill = false;
  359. var defaultLineWidth = 0;
  360. var textFill = getFill('fill' in tokenStyle ? tokenStyle.fill
  361. : 'fill' in style ? style.fill
  362. : (useDefaultFill = true, defaultStyle.fill));
  363. var textStroke = getStroke('stroke' in tokenStyle ? tokenStyle.stroke
  364. : 'stroke' in style ? style.stroke
  365. : (!bgColorDrawn
  366. && !parentBgColorDrawn
  367. && (!defaultStyle.autoStroke || useDefaultFill)) ? (defaultLineWidth = DEFAULT_STROKE_LINE_WIDTH, defaultStyle.stroke)
  368. : null);
  369. var hasShadow = tokenStyle.textShadowBlur > 0
  370. || style.textShadowBlur > 0;
  371. subElStyle.text = token.text;
  372. subElStyle.x = x;
  373. subElStyle.y = y;
  374. if (hasShadow) {
  375. subElStyle.shadowBlur = tokenStyle.textShadowBlur || style.textShadowBlur || 0;
  376. subElStyle.shadowColor = tokenStyle.textShadowColor || style.textShadowColor || 'transparent';
  377. subElStyle.shadowOffsetX = tokenStyle.textShadowOffsetX || style.textShadowOffsetX || 0;
  378. subElStyle.shadowOffsetY = tokenStyle.textShadowOffsetY || style.textShadowOffsetY || 0;
  379. }
  380. subElStyle.textAlign = textAlign;
  381. subElStyle.textBaseline = 'middle';
  382. subElStyle.font = token.font || DEFAULT_FONT;
  383. subElStyle.opacity = retrieve3(tokenStyle.opacity, style.opacity, 1);
  384. setSeparateFont(subElStyle, tokenStyle);
  385. if (textStroke) {
  386. subElStyle.lineWidth = retrieve3(tokenStyle.lineWidth, style.lineWidth, defaultLineWidth);
  387. subElStyle.lineDash = retrieve2(tokenStyle.lineDash, style.lineDash);
  388. subElStyle.lineDashOffset = style.lineDashOffset || 0;
  389. subElStyle.stroke = textStroke;
  390. }
  391. if (textFill) {
  392. subElStyle.fill = textFill;
  393. }
  394. var textWidth = token.contentWidth;
  395. var textHeight = token.contentHeight;
  396. el.setBoundingRect(new BoundingRect(adjustTextX(subElStyle.x, textWidth, subElStyle.textAlign), adjustTextY(subElStyle.y, textHeight, subElStyle.textBaseline), textWidth, textHeight));
  397. };
  398. ZRText.prototype._renderBackground = function (style, topStyle, x, y, width, height) {
  399. var textBackgroundColor = style.backgroundColor;
  400. var textBorderWidth = style.borderWidth;
  401. var textBorderColor = style.borderColor;
  402. var isImageBg = textBackgroundColor && textBackgroundColor.image;
  403. var isPlainOrGradientBg = textBackgroundColor && !isImageBg;
  404. var textBorderRadius = style.borderRadius;
  405. var self = this;
  406. var rectEl;
  407. var imgEl;
  408. if (isPlainOrGradientBg || style.lineHeight || (textBorderWidth && textBorderColor)) {
  409. rectEl = this._getOrCreateChild(Rect);
  410. rectEl.useStyle(rectEl.createStyle());
  411. rectEl.style.fill = null;
  412. var rectShape = rectEl.shape;
  413. rectShape.x = x;
  414. rectShape.y = y;
  415. rectShape.width = width;
  416. rectShape.height = height;
  417. rectShape.r = textBorderRadius;
  418. rectEl.dirtyShape();
  419. }
  420. if (isPlainOrGradientBg) {
  421. var rectStyle = rectEl.style;
  422. rectStyle.fill = textBackgroundColor || null;
  423. rectStyle.fillOpacity = retrieve2(style.fillOpacity, 1);
  424. }
  425. else if (isImageBg) {
  426. imgEl = this._getOrCreateChild(ZRImage);
  427. imgEl.onload = function () {
  428. self.dirtyStyle();
  429. };
  430. var imgStyle = imgEl.style;
  431. imgStyle.image = textBackgroundColor.image;
  432. imgStyle.x = x;
  433. imgStyle.y = y;
  434. imgStyle.width = width;
  435. imgStyle.height = height;
  436. }
  437. if (textBorderWidth && textBorderColor) {
  438. var rectStyle = rectEl.style;
  439. rectStyle.lineWidth = textBorderWidth;
  440. rectStyle.stroke = textBorderColor;
  441. rectStyle.strokeOpacity = retrieve2(style.strokeOpacity, 1);
  442. rectStyle.lineDash = style.borderDash;
  443. rectStyle.lineDashOffset = style.borderDashOffset || 0;
  444. rectEl.strokeContainThreshold = 0;
  445. if (rectEl.hasFill() && rectEl.hasStroke()) {
  446. rectStyle.strokeFirst = true;
  447. rectStyle.lineWidth *= 2;
  448. }
  449. }
  450. var commonStyle = (rectEl || imgEl).style;
  451. commonStyle.shadowBlur = style.shadowBlur || 0;
  452. commonStyle.shadowColor = style.shadowColor || 'transparent';
  453. commonStyle.shadowOffsetX = style.shadowOffsetX || 0;
  454. commonStyle.shadowOffsetY = style.shadowOffsetY || 0;
  455. commonStyle.opacity = retrieve3(style.opacity, topStyle.opacity, 1);
  456. };
  457. ZRText.makeFont = function (style) {
  458. var font = '';
  459. if (hasSeparateFont(style)) {
  460. font = [
  461. style.fontStyle,
  462. style.fontWeight,
  463. parseFontSize(style.fontSize),
  464. style.fontFamily || 'sans-serif'
  465. ].join(' ');
  466. }
  467. return font && trim(font) || style.textFont || style.font;
  468. };
  469. return ZRText;
  470. }(Displayable));
  471. var VALID_TEXT_ALIGN = { left: true, right: 1, center: 1 };
  472. var VALID_TEXT_VERTICAL_ALIGN = { top: 1, bottom: 1, middle: 1 };
  473. var FONT_PARTS = ['fontStyle', 'fontWeight', 'fontSize', 'fontFamily'];
  474. export function parseFontSize(fontSize) {
  475. if (typeof fontSize === 'string'
  476. && (fontSize.indexOf('px') !== -1
  477. || fontSize.indexOf('rem') !== -1
  478. || fontSize.indexOf('em') !== -1)) {
  479. return fontSize;
  480. }
  481. else if (!isNaN(+fontSize)) {
  482. return fontSize + 'px';
  483. }
  484. else {
  485. return DEFAULT_FONT_SIZE + 'px';
  486. }
  487. }
  488. function setSeparateFont(targetStyle, sourceStyle) {
  489. for (var i = 0; i < FONT_PARTS.length; i++) {
  490. var fontProp = FONT_PARTS[i];
  491. var val = sourceStyle[fontProp];
  492. if (val != null) {
  493. targetStyle[fontProp] = val;
  494. }
  495. }
  496. }
  497. export function hasSeparateFont(style) {
  498. return style.fontSize != null || style.fontFamily || style.fontWeight;
  499. }
  500. export function normalizeTextStyle(style) {
  501. normalizeStyle(style);
  502. each(style.rich, normalizeStyle);
  503. return style;
  504. }
  505. function normalizeStyle(style) {
  506. if (style) {
  507. style.font = ZRText.makeFont(style);
  508. var textAlign = style.align;
  509. textAlign === 'middle' && (textAlign = 'center');
  510. style.align = (textAlign == null || VALID_TEXT_ALIGN[textAlign]) ? textAlign : 'left';
  511. var verticalAlign = style.verticalAlign;
  512. verticalAlign === 'center' && (verticalAlign = 'middle');
  513. style.verticalAlign = (verticalAlign == null || VALID_TEXT_VERTICAL_ALIGN[verticalAlign]) ? verticalAlign : 'top';
  514. var textPadding = style.padding;
  515. if (textPadding) {
  516. style.padding = normalizeCssArray(style.padding);
  517. }
  518. }
  519. }
  520. function getStroke(stroke, lineWidth) {
  521. return (stroke == null || lineWidth <= 0 || stroke === 'transparent' || stroke === 'none')
  522. ? null
  523. : (stroke.image || stroke.colorStops)
  524. ? '#000'
  525. : stroke;
  526. }
  527. function getFill(fill) {
  528. return (fill == null || fill === 'none')
  529. ? null
  530. : (fill.image || fill.colorStops)
  531. ? '#000'
  532. : fill;
  533. }
  534. function getTextXForPadding(x, textAlign, textPadding) {
  535. return textAlign === 'right'
  536. ? (x - textPadding[1])
  537. : textAlign === 'center'
  538. ? (x + textPadding[3] / 2 - textPadding[1] / 2)
  539. : (x + textPadding[3]);
  540. }
  541. function getStyleText(style) {
  542. var text = style.text;
  543. text != null && (text += '');
  544. return text;
  545. }
  546. function needDrawBackground(style) {
  547. return !!(style.backgroundColor
  548. || style.lineHeight
  549. || (style.borderWidth && style.borderColor));
  550. }
  551. export default ZRText;