GraphicalLabel.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {Label} from "../Label";
  2. import {TextAlignment} from "../../Common/Enums/TextAlignment";
  3. import {Clickable} from "./Clickable";
  4. import {BoundingBox} from "./BoundingBox";
  5. import {EngravingRules} from "./EngravingRules";
  6. import {MusicSheetCalculator} from "./MusicSheetCalculator";
  7. /**
  8. * The graphical counterpart of a Label
  9. */
  10. export class GraphicalLabel extends Clickable {
  11. private label: Label;
  12. constructor(label: Label, textHeight: number, alignment: TextAlignment, parent: BoundingBox = undefined) {
  13. super();
  14. this.label = label;
  15. this.boundingBox = new BoundingBox(this, parent);
  16. this.label.fontHeight = textHeight;
  17. this.label.textAlignment = alignment;
  18. }
  19. public get Label(): Label {
  20. return this.label;
  21. }
  22. public toString(): string {
  23. return this.label.text;
  24. }
  25. /**
  26. * Calculate GraphicalLabel's Borders according to its Alignment
  27. */
  28. public setLabelPositionAndShapeBorders(): void {
  29. if (this.Label.text.trim() === "") {
  30. return;
  31. }
  32. const labelMarginBorderFactor: number = EngravingRules.Rules.LabelMarginBorderFactor;
  33. const widthToHeightRatio: number =
  34. MusicSheetCalculator.TextMeasurer.computeTextWidthToHeightRatio(this.Label.text, this.Label.font, this.Label.fontStyle);
  35. const height: number = this.Label.fontHeight;
  36. const width: number = height * widthToHeightRatio;
  37. const psi: BoundingBox = this.PositionAndShape;
  38. switch (this.Label.textAlignment) {
  39. case TextAlignment.CenterBottom:
  40. psi.BorderTop = -height;
  41. psi.BorderLeft = -width / 2;
  42. psi.BorderBottom = 0;
  43. psi.BorderRight = width / 2;
  44. break;
  45. case TextAlignment.CenterCenter:
  46. psi.BorderTop = -height / 2;
  47. psi.BorderLeft = -width / 2;
  48. psi.BorderBottom = height / 2;
  49. psi.BorderRight = width / 2;
  50. break;
  51. case TextAlignment.CenterTop:
  52. psi.BorderTop = 0;
  53. psi.BorderLeft = -width / 2;
  54. psi.BorderBottom = height;
  55. psi.BorderRight = width / 2;
  56. break;
  57. case TextAlignment.LeftBottom:
  58. psi.BorderTop = -height;
  59. psi.BorderLeft = 0;
  60. psi.BorderBottom = 0;
  61. psi.BorderRight = width;
  62. break;
  63. case TextAlignment.LeftCenter:
  64. psi.BorderTop = -height / 2;
  65. psi.BorderLeft = 0;
  66. psi.BorderBottom = height / 2;
  67. psi.BorderRight = width;
  68. break;
  69. case TextAlignment.LeftTop:
  70. psi.BorderTop = 0;
  71. psi.BorderLeft = 0;
  72. psi.BorderBottom = height;
  73. psi.BorderRight = width;
  74. break;
  75. case TextAlignment.RightBottom:
  76. psi.BorderTop = -height;
  77. psi.BorderLeft = -width;
  78. psi.BorderBottom = 0;
  79. psi.BorderRight = 0;
  80. break;
  81. case TextAlignment.RightCenter:
  82. psi.BorderTop = -height / 2;
  83. psi.BorderLeft = -width;
  84. psi.BorderBottom = height / 2;
  85. psi.BorderRight = 0;
  86. break;
  87. case TextAlignment.RightTop:
  88. psi.BorderTop = 0;
  89. psi.BorderLeft = -width;
  90. psi.BorderBottom = height;
  91. psi.BorderRight = 0;
  92. break;
  93. default:
  94. }
  95. psi.BorderMarginTop = psi.BorderTop - height * labelMarginBorderFactor;
  96. psi.BorderMarginLeft = psi.BorderLeft - height * labelMarginBorderFactor;
  97. psi.BorderMarginBottom = psi.BorderBottom + height * labelMarginBorderFactor;
  98. psi.BorderMarginRight = psi.BorderRight + height * labelMarginBorderFactor;
  99. }
  100. }