GraphicalLabel.ts 4.4 KB

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