Text.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var Displayable = require("./Displayable");
  2. var zrUtil = require("../core/util");
  3. var textContain = require("../contain/text");
  4. var textHelper = require("./helper/text");
  5. var _constant = require("./constant");
  6. var ContextCachedBy = _constant.ContextCachedBy;
  7. /**
  8. * @alias zrender/graphic/Text
  9. * @extends module:zrender/graphic/Displayable
  10. * @constructor
  11. * @param {Object} opts
  12. */
  13. var Text = function (opts) {
  14. // jshint ignore:line
  15. Displayable.call(this, opts);
  16. };
  17. Text.prototype = {
  18. constructor: Text,
  19. type: 'text',
  20. brush: function (ctx, prevEl) {
  21. var style = this.style; // Optimize, avoid normalize every time.
  22. this.__dirty && textHelper.normalizeTextStyle(style, true); // Use props with prefix 'text'.
  23. style.fill = style.stroke = style.shadowBlur = style.shadowColor = style.shadowOffsetX = style.shadowOffsetY = null;
  24. var text = style.text; // Convert to string
  25. text != null && (text += ''); // Do not apply style.bind in Text node. Because the real bind job
  26. // is in textHelper.renderText, and performance of text render should
  27. // be considered.
  28. // style.bind(ctx, this, prevEl);
  29. if (!textHelper.needDrawText(text, style)) {
  30. // The current el.style is not applied
  31. // and should not be used as cache.
  32. ctx.__attrCachedBy = ContextCachedBy.NONE;
  33. return;
  34. }
  35. this.setTransform(ctx);
  36. textHelper.renderText(this, ctx, text, style, null, prevEl);
  37. this.restoreTransform(ctx);
  38. },
  39. getBoundingRect: function () {
  40. var style = this.style; // Optimize, avoid normalize every time.
  41. this.__dirty && textHelper.normalizeTextStyle(style, true);
  42. if (!this._rect) {
  43. var text = style.text;
  44. text != null ? text += '' : text = '';
  45. var rect = textContain.getBoundingRect(style.text + '', style.font, style.textAlign, style.textVerticalAlign, style.textPadding, style.textLineHeight, style.rich);
  46. rect.x += style.x || 0;
  47. rect.y += style.y || 0;
  48. if (textHelper.getStroke(style.textStroke, style.textStrokeWidth)) {
  49. var w = style.textStrokeWidth;
  50. rect.x -= w / 2;
  51. rect.y -= w / 2;
  52. rect.width += w;
  53. rect.height += w;
  54. }
  55. this._rect = rect;
  56. }
  57. return this._rect;
  58. }
  59. };
  60. zrUtil.inherits(Text, Displayable);
  61. var _default = Text;
  62. module.exports = _default;