27b180a641f4b2331dc04aa1161a41bac7039c3e9b7ba345caeab81bd32713425421a77699046fe17dad1b4b79bb52e7f13121f0969160c005a538a5bd0e97 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import Displayable, { DisplayableProps, DisplayableStatePropNames } from './Displayable';
  2. import { getBoundingRect } from '../contain/text';
  3. import BoundingRect from '../core/BoundingRect';
  4. import { PathStyleProps, DEFAULT_PATH_STYLE } from './Path';
  5. import { createObject, defaults } from '../core/util';
  6. import { FontStyle, FontWeight, TextAlign, TextVerticalAlign } from '../core/types';
  7. import { DEFAULT_FONT } from '../core/platform';
  8. export interface TSpanStyleProps extends PathStyleProps {
  9. x?: number
  10. y?: number
  11. // TODO Text is assigned inside zrender
  12. text?: string
  13. // Final generated font string
  14. // Used in canvas, and when developers specified it.
  15. font?: string
  16. // Value for each part of font
  17. // Used in svg.
  18. // NOTE: font should always been sync with these 4 properties.
  19. fontSize?: number
  20. fontWeight?: FontWeight
  21. fontStyle?: FontStyle
  22. fontFamily?: string
  23. textAlign?: CanvasTextAlign
  24. textBaseline?: CanvasTextBaseline
  25. }
  26. export const DEFAULT_TSPAN_STYLE: TSpanStyleProps = defaults({
  27. strokeFirst: true,
  28. font: DEFAULT_FONT,
  29. x: 0,
  30. y: 0,
  31. textAlign: 'left',
  32. textBaseline: 'top',
  33. miterLimit: 2
  34. } as TSpanStyleProps, DEFAULT_PATH_STYLE);
  35. export interface TSpanProps extends DisplayableProps {
  36. style?: TSpanStyleProps
  37. }
  38. export type TSpanState = Pick<TSpanProps, DisplayableStatePropNames>
  39. class TSpan extends Displayable<TSpanProps> {
  40. style: TSpanStyleProps
  41. hasStroke() {
  42. const style = this.style;
  43. const stroke = style.stroke;
  44. return stroke != null && stroke !== 'none' && style.lineWidth > 0;
  45. }
  46. hasFill() {
  47. const style = this.style;
  48. const fill = style.fill;
  49. return fill != null && fill !== 'none';
  50. }
  51. /**
  52. * Create an image style object with default values in it's prototype.
  53. * @override
  54. */
  55. createStyle(obj?: TSpanStyleProps) {
  56. return createObject(DEFAULT_TSPAN_STYLE, obj);
  57. }
  58. /**
  59. * Set bounding rect calculated from Text
  60. * For reducing time of calculating bounding rect.
  61. */
  62. setBoundingRect(rect: BoundingRect) {
  63. this._rect = rect;
  64. }
  65. getBoundingRect(): BoundingRect {
  66. const style = this.style;
  67. if (!this._rect) {
  68. let text = style.text;
  69. text != null ? (text += '') : (text = '');
  70. const rect = getBoundingRect(
  71. text,
  72. style.font,
  73. style.textAlign as TextAlign,
  74. style.textBaseline as TextVerticalAlign
  75. );
  76. rect.x += style.x || 0;
  77. rect.y += style.y || 0;
  78. if (this.hasStroke()) {
  79. const w = style.lineWidth;
  80. rect.x -= w / 2;
  81. rect.y -= w / 2;
  82. rect.width += w;
  83. rect.height += w;
  84. }
  85. this._rect = rect;
  86. }
  87. return this._rect;
  88. }
  89. protected static initDefaultProps = (function () {
  90. const tspanProto = TSpan.prototype;
  91. // TODO Calculate tolerance smarter
  92. tspanProto.dirtyRectTolerance = 10;
  93. })()
  94. }
  95. TSpan.prototype.type = 'tspan';
  96. export default TSpan;