VexFlowStaffEntry.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import {GraphicalStaffEntry} from "../GraphicalStaffEntry";
  2. import {VexFlowMeasure} from "./VexFlowMeasure";
  3. import {SourceStaffEntry} from "../../VoiceData/SourceStaffEntry";
  4. import {unitInPixels} from "./VexFlowMusicSheetDrawer";
  5. import { VexFlowVoiceEntry } from "./VexFlowVoiceEntry";
  6. import { Note } from "../../VoiceData/Note";
  7. import { EngravingRules } from "../EngravingRules";
  8. export class VexFlowStaffEntry extends GraphicalStaffEntry {
  9. constructor(measure: VexFlowMeasure, sourceStaffEntry: SourceStaffEntry, staffEntryParent: VexFlowStaffEntry) {
  10. super(measure, sourceStaffEntry, staffEntryParent);
  11. }
  12. // if there is a in-measure clef given before this staffEntry,
  13. // it will be converted to a Vex.Flow.ClefNote and assigned to this variable:
  14. public vfClefBefore: Vex.Flow.ClefNote;
  15. /**
  16. * Calculates the staff entry positions from the VexFlow stave information and the tickabels inside the staff.
  17. * This is needed in order to set the OSMD staff entries (which are almost the same as tickables) to the correct positionts.
  18. * It is also needed to be done after formatting!
  19. */
  20. public calculateXPosition(): void {
  21. const stave: Vex.Flow.Stave = (this.parentMeasure as VexFlowMeasure).getVFStave();
  22. // sets the vexflow x positions back into the bounding boxes of the staff entries in the osmd object model.
  23. // The positions are needed for cursor placement and mouse/tap interactions
  24. let lastBorderLeft: number = 0;
  25. for (const gve of this.graphicalVoiceEntries as VexFlowVoiceEntry[]) {
  26. if (gve.vfStaveNote) {
  27. gve.vfStaveNote.setStave(stave);
  28. if (!gve.vfStaveNote.preFormatted) {
  29. continue;
  30. }
  31. gve.applyBordersFromVexflow();
  32. this.PositionAndShape.RelativePosition.x = gve.vfStaveNote.getBoundingBox().x / unitInPixels;
  33. const sourceNote: Note = gve.notes[0].sourceNote;
  34. if (sourceNote.isRest() && sourceNote.Length.WholeValue === 1) { // whole rest
  35. this.PositionAndShape.RelativePosition.x +=
  36. EngravingRules.Rules.WholeRestXShiftVexflow - 0.1; // xShift from VexFlowConverter
  37. gve.PositionAndShape.BorderLeft = -0.7;
  38. gve.PositionAndShape.BorderRight = 0.7;
  39. }
  40. if (gve.PositionAndShape.BorderLeft < lastBorderLeft) {
  41. lastBorderLeft = gve.PositionAndShape.BorderLeft;
  42. }
  43. }
  44. }
  45. this.PositionAndShape.RelativePosition.x -= lastBorderLeft;
  46. this.PositionAndShape.calculateBoundingBox();
  47. }
  48. }