TSpan.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { __extends } from "tslib";
  2. import Displayable from './Displayable';
  3. import { getBoundingRect, DEFAULT_FONT } from '../contain/text';
  4. import { DEFAULT_PATH_STYLE } from './Path';
  5. import { createObject, defaults } from '../core/util';
  6. export var DEFAULT_TSPAN_STYLE = defaults({
  7. strokeFirst: true,
  8. font: DEFAULT_FONT,
  9. x: 0,
  10. y: 0,
  11. textAlign: 'left',
  12. textBaseline: 'top',
  13. miterLimit: 2
  14. }, DEFAULT_PATH_STYLE);
  15. var TSpan = (function (_super) {
  16. __extends(TSpan, _super);
  17. function TSpan() {
  18. return _super !== null && _super.apply(this, arguments) || this;
  19. }
  20. TSpan.prototype.hasStroke = function () {
  21. var style = this.style;
  22. var stroke = style.stroke;
  23. return stroke != null && stroke !== 'none' && style.lineWidth > 0;
  24. };
  25. TSpan.prototype.hasFill = function () {
  26. var style = this.style;
  27. var fill = style.fill;
  28. return fill != null && fill !== 'none';
  29. };
  30. TSpan.prototype.createStyle = function (obj) {
  31. return createObject(DEFAULT_TSPAN_STYLE, obj);
  32. };
  33. TSpan.prototype.setBoundingRect = function (rect) {
  34. this._rect = rect;
  35. };
  36. TSpan.prototype.getBoundingRect = function () {
  37. var style = this.style;
  38. if (!this._rect) {
  39. var text = style.text;
  40. text != null ? (text += '') : (text = '');
  41. var rect = getBoundingRect(text, style.font, style.textAlign, style.textBaseline);
  42. rect.x += style.x || 0;
  43. rect.y += style.y || 0;
  44. if (this.hasStroke()) {
  45. var w = style.lineWidth;
  46. rect.x -= w / 2;
  47. rect.y -= w / 2;
  48. rect.width += w;
  49. rect.height += w;
  50. }
  51. this._rect = rect;
  52. }
  53. return this._rect;
  54. };
  55. TSpan.initDefaultProps = (function () {
  56. var tspanProto = TSpan.prototype;
  57. tspanProto.dirtyRectTolerance = 10;
  58. })();
  59. return TSpan;
  60. }(Displayable));
  61. TSpan.prototype.type = 'tspan';
  62. export default TSpan;