VexFlowMusicSystem.ts 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import {MusicSystem} from "../MusicSystem";
  2. import {GraphicalMusicPage} from "../GraphicalMusicPage";
  3. import {SystemLinesEnum} from "../SystemLinesEnum";
  4. import {SystemLinePosition} from "../SystemLinePosition";
  5. import {StaffMeasure} from "../StaffMeasure";
  6. import {SystemLine} from "../SystemLine";
  7. import {VexFlowStaffLine} from "./VexFlowStaffLine";
  8. import {VexFlowMeasure} from "./VexFlowMeasure";
  9. import {VexFlowConverter} from "./VexFlowConverter";
  10. import {StaffLine} from "../StaffLine";
  11. import {EngravingRules} from "../EngravingRules";
  12. import { VexFlowInstrumentBracket } from "./VexFlowInstrumentBracket";
  13. import { VexFlowInstrumentBrace } from "./VexFlowInstrumentBrace";
  14. export class VexFlowMusicSystem extends MusicSystem {
  15. constructor(parent: GraphicalMusicPage, id: number) {
  16. super(parent, id);
  17. }
  18. public calculateBorders(rules: EngravingRules): void {
  19. if (this.staffLines.length === 0) {
  20. return;
  21. }
  22. const width: number = this.calcBracketsWidth();
  23. this.boundingBox.BorderLeft = -width;
  24. this.boundingBox.BorderMarginLeft = -width;
  25. this.boundingBox.XBordersHaveBeenSet = true;
  26. }
  27. /**
  28. * This method creates all the graphical lines and dots needed to render a system line (e.g. bold-thin-dots..).
  29. * @param xPosition
  30. * @param lineWidth
  31. * @param lineType
  32. * @param linePosition indicates if the line belongs to start or end of measure
  33. * @param musicSystem
  34. * @param topMeasure
  35. * @param bottomMeasure
  36. */
  37. protected createSystemLine(xPosition: number, lineWidth: number, lineType: SystemLinesEnum, linePosition: SystemLinePosition,
  38. musicSystem: MusicSystem, topMeasure: StaffMeasure, bottomMeasure: StaffMeasure = undefined): SystemLine {
  39. const vfMeasure: VexFlowMeasure = topMeasure as VexFlowMeasure;
  40. vfMeasure.addMeasureLine(lineType, linePosition);
  41. if (bottomMeasure) {
  42. // ToDo: feature/Repetitions
  43. // create here the correct lines according to the given lineType.
  44. (bottomMeasure as VexFlowMeasure).lineTo(topMeasure as VexFlowMeasure, VexFlowConverter.line(lineType));
  45. (bottomMeasure as VexFlowMeasure).addMeasureLine(lineType, linePosition);
  46. }
  47. return new SystemLine(lineType, linePosition, this, topMeasure, bottomMeasure);
  48. }
  49. /**
  50. * creates an instrument brace for the given dimension.
  51. * The height and positioning can be inferred from the given staff lines.
  52. * @param firstStaffLine the upper StaffLine (use a cast to get the VexFlowStaffLine) of the brace to create
  53. * @param lastStaffLine the lower StaffLine (use a cast to get the VexFlowStaffLine) of the brace to create
  54. */
  55. protected createInstrumentBracket(firstStaffLine: StaffLine, lastStaffLine: StaffLine): void {
  56. // You could write this in one line but the linter doesn't let me.
  57. const firstVexStaff: VexFlowStaffLine = (firstStaffLine as VexFlowStaffLine);
  58. const lastVexStaff: VexFlowStaffLine = (lastStaffLine as VexFlowStaffLine);
  59. const vexFlowBracket: VexFlowInstrumentBrace = new VexFlowInstrumentBrace(firstVexStaff, lastVexStaff);
  60. this.InstrumentBrackets.push(vexFlowBracket);
  61. return;
  62. }
  63. /**
  64. * creates an instrument group bracket for the given dimension.
  65. * There can be cascaded bracket (e.g. a group of 2 in a group of 4) -
  66. * The recursion depth informs about the current depth level (needed for positioning)
  67. * @param firstStaffLine the upper staff line of the bracket to create
  68. * @param lastStaffLine the lower staff line of the bracket to create
  69. * @param recursionDepth
  70. */
  71. protected createGroupBracket(firstStaffLine: StaffLine, lastStaffLine: StaffLine, recursionDepth: number): void {
  72. const firstVexStaff: VexFlowStaffLine = (firstStaffLine as VexFlowStaffLine);
  73. const lastVexStaff: VexFlowStaffLine = (lastStaffLine as VexFlowStaffLine);
  74. if (recursionDepth === 0) {
  75. const vexFlowBracket: VexFlowInstrumentBracket = new VexFlowInstrumentBracket(firstVexStaff, lastVexStaff, recursionDepth);
  76. this.GroupBrackets.push(vexFlowBracket);
  77. } else {
  78. const vexFlowBrace: VexFlowInstrumentBrace = new VexFlowInstrumentBrace(firstVexStaff, lastVexStaff, recursionDepth);
  79. this.GroupBrackets.push(vexFlowBrace);
  80. }
  81. return;
  82. }
  83. }