Text.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var Displayable = require("./Displayable");
  2. var zrUtil = require("../core/util");
  3. var textContain = require("../contain/text");
  4. var textHelper = require("./helper/text");
  5. /**
  6. * @alias zrender/graphic/Text
  7. * @extends module:zrender/graphic/Displayable
  8. * @constructor
  9. * @param {Object} opts
  10. */
  11. var Text = function (opts) {
  12. // jshint ignore:line
  13. Displayable.call(this, opts);
  14. };
  15. Text.prototype = {
  16. constructor: Text,
  17. type: 'text',
  18. brush: function (ctx, prevEl) {
  19. var style = this.style; // Optimize, avoid normalize every time.
  20. this.__dirty && textHelper.normalizeTextStyle(style, true); // Use props with prefix 'text'.
  21. style.fill = style.stroke = style.shadowBlur = style.shadowColor = style.shadowOffsetX = style.shadowOffsetY = null;
  22. var text = style.text; // Convert to string
  23. text != null && (text += ''); // Always bind style
  24. style.bind(ctx, this, prevEl);
  25. if (!textHelper.needDrawText(text, style)) {
  26. return;
  27. }
  28. this.setTransform(ctx);
  29. textHelper.renderText(this, ctx, text, style);
  30. this.restoreTransform(ctx);
  31. },
  32. getBoundingRect: function () {
  33. var style = this.style; // Optimize, avoid normalize every time.
  34. this.__dirty && textHelper.normalizeTextStyle(style, true);
  35. if (!this._rect) {
  36. var text = style.text;
  37. text != null ? text += '' : text = '';
  38. var rect = textContain.getBoundingRect(style.text + '', style.font, style.textAlign, style.textVerticalAlign, style.textPadding, style.rich);
  39. rect.x += style.x || 0;
  40. rect.y += style.y || 0;
  41. if (textHelper.getStroke(style.textStroke, style.textStrokeWidth)) {
  42. var w = style.textStrokeWidth;
  43. rect.x -= w / 2;
  44. rect.y -= w / 2;
  45. rect.width += w;
  46. rect.height += w;
  47. }
  48. this._rect = rect;
  49. }
  50. return this._rect;
  51. }
  52. };
  53. zrUtil.inherits(Text, Displayable);
  54. var _default = Text;
  55. module.exports = _default;