VexFlowMeasure.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import {StaffMeasure} from "../StaffMeasure";
  2. import {SourceMeasure} from "../../VoiceData/SourceMeasure";
  3. import {Staff} from "../../VoiceData/Staff";
  4. import {StaffLine} from "../StaffLine";
  5. import {SystemLinesEnum} from "../SystemLinesEnum";
  6. import {ClefInstruction} from "../../VoiceData/Instructions/ClefInstruction";
  7. import {KeyInstruction} from "../../VoiceData/Instructions/KeyInstruction";
  8. import {RhythmInstruction} from "../../VoiceData/Instructions/RhythmInstruction";
  9. import {VexFlowConverter} from "./VexFlowConverter";
  10. import {VexFlowStaffEntry} from "./VexFlowStaffEntry";
  11. //import {Fraction} from "../../../Common/DataObjects/fraction";
  12. import Vex = require("vexflow");
  13. export class VexFlowMeasure extends StaffMeasure {
  14. constructor(staff: Staff, staffLine: StaffLine = undefined, sourceMeasure: SourceMeasure = undefined) {
  15. super(staff, sourceMeasure, staffLine);
  16. // this.MinimumStaffEntriesWidth =
  17. this.stave = new Vex.Flow.Stave(0, 0, 0);
  18. this.voices = {};
  19. //this.duration = this.parentSourceMeasure.Duration;
  20. }
  21. private stave: Vex.Flow.Stave;
  22. private voices: { [voiceID: number]: Vex.Flow.Voice; };
  23. //private duration: Fraction;
  24. /**
  25. * Reset all the geometric values and parameters of this measure and put it in an initialized state.
  26. * This is needed to evaluate a measure a second time by system builder.
  27. */
  28. public resetLayout(): void {
  29. this.beginInstructionsWidth = 0;
  30. this.endInstructionsWidth = 0;
  31. }
  32. /**
  33. * returns the x-width of a given measure line.
  34. * @param line
  35. * @returns {SystemLinesEnum} the x-width
  36. */
  37. public getLineWidth(line: SystemLinesEnum): number {
  38. // FIXME: See values in VexFlow's stavebarline.js
  39. switch (line) {
  40. case SystemLinesEnum.SingleThin:
  41. return 5;
  42. case SystemLinesEnum.DoubleThin:
  43. return 5;
  44. case SystemLinesEnum.ThinBold:
  45. return 5;
  46. case SystemLinesEnum.BoldThinDots:
  47. return 5;
  48. case SystemLinesEnum.DotsThinBold:
  49. return 5;
  50. case SystemLinesEnum.DotsBoldBoldDots:
  51. return 5;
  52. case SystemLinesEnum.None:
  53. return 0;
  54. default:
  55. return 0;
  56. }
  57. }
  58. /**
  59. * adds the given clef to the begin of the measure.
  60. * This has to update/increase BeginInstructionsWidth.
  61. * @param clef
  62. */
  63. public addClefAtBegin(clef: ClefInstruction): void {
  64. let vfclef: Vex.Flow.Clef = VexFlowConverter.Clef(clef);
  65. this.stave.addClef(vfclef, undefined, undefined, Vex.Flow.Modifier.Position.BEGIN);
  66. this.increaseBeginInstructionWidth(vfclef);
  67. }
  68. /**
  69. * adds the given key to the begin of the measure.
  70. * This has to update/increase BeginInstructionsWidth.
  71. * @param currentKey the new valid key.
  72. * @param previousKey the old cancelled key. Needed to show which accidentals are not valid any more.
  73. * @param currentClef the valid clef. Needed to put the accidentals on the right y-positions.
  74. */
  75. public addKeyAtBegin(currentKey: KeyInstruction, previousKey: KeyInstruction, currentClef: ClefInstruction): void {
  76. let keySig: Vex.Flow.KeySignature = new Vex.Flow.KeySignature(
  77. VexFlowConverter.keySignature(currentKey),
  78. VexFlowConverter.keySignature(previousKey)
  79. );
  80. this.stave.addModifier(keySig, Vex.Flow.Modifier.Position.BEGIN);
  81. }
  82. /**
  83. * adds the given rhythm to the begin of the measure.
  84. * This has to update/increase BeginInstructionsWidth.
  85. * @param rhythm
  86. */
  87. public addRhythmAtBegin(rhythm: RhythmInstruction): void {
  88. let timeSig: Vex.Flow.TimeSignature = VexFlowConverter.TimeSignature(rhythm);
  89. this.stave.addModifier(
  90. timeSig,
  91. Vex.Flow.Modifier.Position.BEGIN
  92. );
  93. this.increaseBeginInstructionWidth(timeSig);
  94. }
  95. /**
  96. * adds the given clef to the end of the measure.
  97. * This has to update/increase EndInstructionsWidth.
  98. * @param clef
  99. */
  100. public addClefAtEnd(clef: ClefInstruction): void {
  101. let vfclef: Vex.Flow.Clef = VexFlowConverter.Clef(clef);
  102. this.stave.addClef(vfclef, undefined, undefined, Vex.Flow.Modifier.Position.END);
  103. this.increaseEndInstructionWidth(vfclef);
  104. }
  105. /**
  106. * Set the x-position relative to the staffline.
  107. * (y-Position is always 0 relative to the staffline)
  108. * @param x
  109. */
  110. public setPositionInStaffline(x: number): void {
  111. // Already implemented in VexFlow, it does _not_ call .format()
  112. this.stave.setX(x);
  113. }
  114. /**
  115. * Sets the overall x-width of the measure.
  116. * @param width
  117. */
  118. public SetWidth(width: number): void {
  119. // Widths in PS and VexFlow work differently.
  120. // In VexFlow, width is only the width of the actual voices, without considering
  121. // modifiers like clefs. In PS, width is the total width of the stave.
  122. // @Andrea: The following could be improved by storing the values in this object.
  123. // Now it calls .format() implicitly.
  124. this.stave.setWidth(width - this.beginInstructionsWidth - this.endInstructionsWidth);
  125. }
  126. /**
  127. * This method is called after the StaffEntriesScaleFactor has been set.
  128. * Here the final x-positions of the staff entries have to be set.
  129. * (multiply the minimal positions with the scaling factor, considering the BeginInstructionsWidth)
  130. */
  131. public layoutSymbols(): void {
  132. // This is already done in the MusicSystemBuilder!
  133. //this.setWidth(this.minimumStaffEntriesWidth * this.staffEntriesScaleFactor);
  134. this.stave.format();
  135. // Set context first!
  136. this.stave.draw();
  137. }
  138. public addGraphicalStaffEntry(entry: VexFlowStaffEntry): void {
  139. super.addGraphicalStaffEntry(entry);
  140. let vfnotes: { [voiceID: number]: Vex.Flow.StaveNote; } = entry.vfnotes;
  141. for (let id in vfnotes) {
  142. if (vfnotes.hasOwnProperty(id)) {
  143. if (!(id in this.voices)) {
  144. this.voices[id] = new Vex.Flow.Voice({
  145. beat_value: this.parentSourceMeasure.Duration.Denominator,
  146. num_beats: this.parentSourceMeasure.Duration.Numerator,
  147. resolution: Vex.Flow.RESOLUTION,
  148. }).setMode(Vex.Flow.Voice.Mode.SOFT);
  149. }
  150. this.voices[id].addTickable(vfnotes[id]);
  151. }
  152. }
  153. }
  154. public addGraphicalStaffEntryAtTimestamp(entry: VexFlowStaffEntry): void {
  155. super.addGraphicalStaffEntryAtTimestamp(entry);
  156. // TODO
  157. }
  158. private increaseBeginInstructionWidth(modifier: any): void {
  159. let padding: number = modifier.getCategory("") === "keysignatures" ? modifier.getPadding(2) : 0;
  160. //modifier.getPadding(this.begModifiers);
  161. let width: number = modifier.getWidth();
  162. this.beginInstructionsWidth += padding + width;
  163. //if (padding + width > 0) {
  164. // this.begModifiers += 1;
  165. //}
  166. }
  167. private increaseEndInstructionWidth(modifier: any): void {
  168. let padding: number = 0; //modifier.getPadding(this.endModifiers++);
  169. let width: number = modifier.getWidth();
  170. this.endInstructionsWidth += padding + width;
  171. }
  172. }