GraphicalLabel.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { TextAlignmentEnum } from "../../Common/Enums/TextAlignment";
  2. import { Label } from "../Label";
  3. import { BoundingBox } from "./BoundingBox";
  4. import { Clickable } from "./Clickable";
  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. private rules: EngravingRules;
  13. /**
  14. * Creates a new GraphicalLabel from a Label
  15. * @param label label object containing text
  16. * @param textHeight Height of text
  17. * @param alignment Alignement like left, right, top, ...
  18. * @param parent Parent Bounding Box where the label is attached to
  19. */
  20. constructor(label: Label, textHeight: number, alignment: TextAlignmentEnum, rules: EngravingRules,
  21. parent: BoundingBox = undefined, ) {
  22. super();
  23. this.label = label;
  24. this.boundingBox = new BoundingBox(this, parent);
  25. this.label.fontHeight = textHeight;
  26. this.label.textAlignment = alignment;
  27. this.rules = rules;
  28. }
  29. public get Label(): Label {
  30. return this.label;
  31. }
  32. public toString(): string {
  33. return `${this.label.text} (${this.boundingBox.RelativePosition.x},${this.boundingBox.RelativePosition.y})`;
  34. }
  35. /**
  36. * Calculate GraphicalLabel's Borders according to its Alignment
  37. */
  38. public setLabelPositionAndShapeBorders(): void {
  39. if (this.Label.text.trim() === "") {
  40. return;
  41. }
  42. const labelMarginBorderFactor: number = this.rules?.LabelMarginBorderFactor ?? 0.1;
  43. const widthToHeightRatio: number =
  44. MusicSheetCalculator.TextMeasurer.computeTextWidthToHeightRatio(this.Label.text, this.Label.font, this.Label.fontStyle);
  45. const height: number = this.Label.fontHeight;
  46. const width: number = height * widthToHeightRatio;
  47. const bbox: BoundingBox = this.PositionAndShape;
  48. switch (this.Label.textAlignment) {
  49. case TextAlignmentEnum.CenterBottom:
  50. bbox.BorderTop = -height;
  51. bbox.BorderLeft = -width / 2;
  52. bbox.BorderBottom = 0;
  53. bbox.BorderRight = width / 2;
  54. break;
  55. case TextAlignmentEnum.CenterCenter:
  56. bbox.BorderTop = -height / 2;
  57. bbox.BorderLeft = -width / 2;
  58. bbox.BorderBottom = height / 2;
  59. bbox.BorderRight = width / 2;
  60. break;
  61. case TextAlignmentEnum.CenterTop:
  62. bbox.BorderTop = 0;
  63. bbox.BorderLeft = -width / 2;
  64. bbox.BorderBottom = height;
  65. bbox.BorderRight = width / 2;
  66. break;
  67. case TextAlignmentEnum.LeftBottom:
  68. bbox.BorderTop = -height;
  69. bbox.BorderLeft = 0;
  70. bbox.BorderBottom = 0;
  71. bbox.BorderRight = width;
  72. break;
  73. case TextAlignmentEnum.LeftCenter:
  74. bbox.BorderTop = -height / 2;
  75. bbox.BorderLeft = 0;
  76. bbox.BorderBottom = height / 2;
  77. bbox.BorderRight = width;
  78. break;
  79. case TextAlignmentEnum.LeftTop:
  80. bbox.BorderTop = 0;
  81. bbox.BorderLeft = 0;
  82. bbox.BorderBottom = height;
  83. bbox.BorderRight = width;
  84. break;
  85. case TextAlignmentEnum.RightBottom:
  86. bbox.BorderTop = -height;
  87. bbox.BorderLeft = -width;
  88. bbox.BorderBottom = 0;
  89. bbox.BorderRight = 0;
  90. break;
  91. case TextAlignmentEnum.RightCenter:
  92. bbox.BorderTop = -height / 2;
  93. bbox.BorderLeft = -width;
  94. bbox.BorderBottom = height / 2;
  95. bbox.BorderRight = 0;
  96. break;
  97. case TextAlignmentEnum.RightTop:
  98. bbox.BorderTop = 0;
  99. bbox.BorderLeft = -width;
  100. bbox.BorderBottom = height;
  101. bbox.BorderRight = 0;
  102. break;
  103. default:
  104. }
  105. bbox.BorderMarginTop = bbox.BorderTop - height * labelMarginBorderFactor;
  106. bbox.BorderMarginLeft = bbox.BorderLeft - height * labelMarginBorderFactor;
  107. bbox.BorderMarginBottom = bbox.BorderBottom + height * labelMarginBorderFactor;
  108. bbox.BorderMarginRight = bbox.BorderRight + height * labelMarginBorderFactor;
  109. }
  110. }