Text.js 23 KB

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