VexFlowMusicSheetCalculator.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import {MusicSheetCalculator} from "../MusicSheetCalculator";
  2. import {VexFlowGraphicalSymbolFactory} from "./VexFlowGraphicalSymbolFactory";
  3. import {StaffMeasure} from "../StaffMeasure";
  4. import {StaffLine} from "../StaffLine";
  5. import {VoiceEntry} from "../../VoiceData/VoiceEntry";
  6. import {MusicSystem} from "../MusicSystem";
  7. import {GraphicalNote} from "../GraphicalNote";
  8. import {GraphicalStaffEntry} from "../GraphicalStaffEntry";
  9. import {GraphicalMusicPage} from "../GraphicalMusicPage";
  10. import {GraphicalTie} from "../GraphicalTie";
  11. import {Tie} from "../../VoiceData/Tie";
  12. import {SourceMeasure} from "../../VoiceData/SourceMeasure";
  13. import {MultiExpression} from "../../VoiceData/Expressions/MultiExpression";
  14. import {RepetitionInstruction} from "../../VoiceData/Instructions/RepetitionInstruction";
  15. import {Beam} from "../../VoiceData/Beam";
  16. import {ClefInstruction} from "../../VoiceData/Instructions/ClefInstruction";
  17. import {OctaveEnum} from "../../VoiceData/Expressions/ContinuousExpressions/OctaveShift";
  18. import {Fraction} from "../../../Common/DataObjects/Fraction";
  19. import {LyricsEntry} from "../../VoiceData/Lyrics/LyricsEntry";
  20. import {LyricWord} from "../../VoiceData/Lyrics/LyricsWord";
  21. import {OrnamentContainer} from "../../VoiceData/OrnamentContainer";
  22. import {ArticulationEnum} from "../../VoiceData/VoiceEntry";
  23. import {Tuplet} from "../../VoiceData/Tuplet";
  24. import Dictionary from "typescript-collections/dist/lib/Dictionary";
  25. import {VexFlowMeasure} from "./VexFlowMeasure";
  26. import {VexFlowTextMeasurer} from "./VexFlowTextMeasurer";
  27. import Vex = require("vexflow");
  28. import {Logging} from "../../../Common/Logging";
  29. import {UnitInPixels} from "./VexFlowMusicSheetDrawer";
  30. export class VexFlowMusicSheetCalculator extends MusicSheetCalculator {
  31. constructor() {
  32. super(new VexFlowGraphicalSymbolFactory());
  33. MusicSheetCalculator.TextMeasurer = new VexFlowTextMeasurer();
  34. }
  35. protected clearRecreatedObjects(): void {
  36. super.clearRecreatedObjects();
  37. for (let staffMeasures of this.graphicalMusicSheet.MeasureList) {
  38. for (let staffMeasure of staffMeasures) {
  39. (<VexFlowMeasure>staffMeasure).clean();
  40. }
  41. }
  42. }
  43. //protected clearSystemsAndMeasures(): void {
  44. // for (let measure of measures) {
  45. //
  46. // }
  47. //}
  48. /**
  49. * Calculates the x layout of the staff entries within the staff measures belonging to one source measure.
  50. * All staff entries are x-aligned throughout all vertically aligned staff measures.
  51. * This method is called within calculateXLayout.
  52. * The staff entries are aligned with minimum needed x distances.
  53. * The MinimumStaffEntriesWidth of every measure will be set - needed for system building.
  54. * @param measures
  55. * @returns the minimum required x width of the source measure (=list of staff measures)
  56. */
  57. protected calculateMeasureXLayout(measures: StaffMeasure[]): number {
  58. // Finalize beams
  59. for (let measure of measures) {
  60. (measure as VexFlowMeasure).finalizeBeams();
  61. }
  62. // Format the voices
  63. let allVoices: Vex.Flow.Voice[] = [];
  64. let formatter: Vex.Flow.Formatter = new Vex.Flow.Formatter({
  65. align_rests: true,
  66. });
  67. for (let measure of measures) {
  68. let mvoices: { [voiceID: number]: Vex.Flow.Voice; } = (measure as VexFlowMeasure).vfVoices;
  69. let voices: Vex.Flow.Voice[] = [];
  70. for (let voiceID in mvoices) {
  71. if (mvoices.hasOwnProperty(voiceID)) {
  72. voices.push(mvoices[voiceID]);
  73. allVoices.push(mvoices[voiceID]);
  74. }
  75. }
  76. if (voices.length === 0) {
  77. Logging.warn("Found a measure with no voices... Continuing anyway.", mvoices);
  78. continue;
  79. }
  80. formatter.joinVoices(voices);
  81. }
  82. let firstMeasure: VexFlowMeasure = measures[0] as VexFlowMeasure;
  83. // FIXME: The following ``+ 5.0'' is temporary: it was added as a workaround for
  84. // FIXME: a more relaxed formatting of voices
  85. let width: number = formatter.preCalculateMinTotalWidth(allVoices) / UnitInPixels + 5.0;
  86. for (let measure of measures) {
  87. measure.minimumStaffEntriesWidth = width;
  88. (measure as VexFlowMeasure).formatVoices = undefined;
  89. }
  90. firstMeasure.formatVoices = (w: number) => {
  91. formatter.format(allVoices, w);
  92. };
  93. return width;
  94. }
  95. protected updateStaffLineBorders(staffLine: StaffLine): void {
  96. return;
  97. }
  98. protected calculateMeasureNumberPlacement(musicSystem: MusicSystem): void {
  99. return;
  100. }
  101. /**
  102. * Can be used to calculate stem directions, helper(ledger) lines, and overlapping note x-displacement.
  103. * Is Excecuted per voice entry of a staff entry.
  104. * After that layoutStaffEntry is called.
  105. * @param voiceEntry
  106. * @param graphicalNotes
  107. * @param graphicalStaffEntry
  108. * @param hasPitchedNote
  109. * @param isGraceStaffEntry
  110. */
  111. protected layoutVoiceEntry(voiceEntry: VoiceEntry, graphicalNotes: GraphicalNote[], graphicalStaffEntry: GraphicalStaffEntry,
  112. hasPitchedNote: boolean, isGraceStaffEntry: boolean): void {
  113. return;
  114. }
  115. /**
  116. * Do all layout calculations that have to be done per staff entry, like dots, ornaments, arpeggios....
  117. * This method is called after the voice entries are handled by layoutVoiceEntry().
  118. * @param graphicalStaffEntry
  119. */
  120. protected layoutStaffEntry(graphicalStaffEntry: GraphicalStaffEntry): void {
  121. (graphicalStaffEntry.parentMeasure as VexFlowMeasure).layoutStaffEntry(graphicalStaffEntry);
  122. }
  123. /**
  124. * calculates the y positions of the staff lines within a system and
  125. * furthermore the y positions of the systems themselves.
  126. */
  127. protected calculateSystemYLayout(): void {
  128. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  129. let graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  130. if (!this.leadSheet) {
  131. let globalY: number = this.rules.PageTopMargin + this.rules.TitleTopDistance + this.rules.SheetTitleHeight +
  132. this.rules.TitleBottomDistance;
  133. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  134. let musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  135. // calculate y positions of stafflines within system
  136. let y: number = 0;
  137. for (let line of musicSystem.StaffLines) {
  138. line.PositionAndShape.RelativePosition.y = y;
  139. y += 10;
  140. }
  141. // set y positions of systems using the previous system and a fixed distance.
  142. musicSystem.PositionAndShape.BorderBottom = y + 0;
  143. musicSystem.PositionAndShape.RelativePosition.x = this.rules.PageLeftMargin + this.rules.SystemLeftMargin;
  144. musicSystem.PositionAndShape.RelativePosition.y = globalY;
  145. globalY += y + 5;
  146. }
  147. }
  148. }
  149. }
  150. /**
  151. * Is called at the begin of the method for creating the vertically aligned staff measures belonging to one source measure.
  152. */
  153. protected initStaffMeasuresCreation(): void {
  154. return;
  155. }
  156. protected handleTie(tie: Tie, startGraphicalStaffEntry: GraphicalStaffEntry, staffIndex: number, measureIndex: number): void {
  157. return;
  158. }
  159. protected layoutGraphicalTie(tie: GraphicalTie, tieIsAtSystemBreak: boolean): void {
  160. return;
  161. }
  162. protected calculateSingleStaffLineLyricsPosition(staffLine: StaffLine, lyricVersesNumber: number[]): void {
  163. return;
  164. }
  165. protected calculateSingleOctaveShift(sourceMeasure: SourceMeasure, multiExpression: MultiExpression, measureIndex: number, staffIndex: number): void {
  166. return;
  167. }
  168. protected calculateWordRepetitionInstruction(repetitionInstruction: RepetitionInstruction, measureIndex: number): void {
  169. return;
  170. }
  171. protected calculateMoodAndUnknownExpression(multiExpression: MultiExpression, measureIndex: number, staffIndex: number): void {
  172. return;
  173. }
  174. protected createGraphicalTieNote(beams: Beam[], activeClef: ClefInstruction,
  175. octaveShiftValue: OctaveEnum, graphicalStaffEntry: GraphicalStaffEntry, duration: Fraction, numberOfDots: number,
  176. openTie: Tie, isLastTieNote: boolean): void {
  177. return;
  178. }
  179. /**
  180. * Is called if a note is part of a beam.
  181. * @param graphicalNote
  182. * @param beam
  183. * @param openBeams a list of all currently open beams
  184. */
  185. protected handleBeam(graphicalNote: GraphicalNote, beam: Beam, openBeams: Beam[]): void {
  186. (graphicalNote.parentStaffEntry.parentMeasure as VexFlowMeasure).handleBeam(graphicalNote, beam);
  187. }
  188. protected handleVoiceEntryLyrics(lyricsEntries: Dictionary<number, LyricsEntry>, voiceEntry: VoiceEntry,
  189. graphicalStaffEntry: GraphicalStaffEntry, openLyricWords: LyricWord[]): void {
  190. return;
  191. }
  192. protected handleVoiceEntryOrnaments(ornamentContainer: OrnamentContainer, voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry): void {
  193. return;
  194. }
  195. protected handleVoiceEntryArticulations(articulations: ArticulationEnum[], voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry): void {
  196. return;
  197. }
  198. /**
  199. * Is called if a note is part of a tuplet.
  200. * @param graphicalNote
  201. * @param tuplet
  202. * @param openTuplets a list of all currently open tuplets
  203. */
  204. protected handleTuplet(graphicalNote: GraphicalNote, tuplet: Tuplet, openTuplets: Tuplet[]): void {
  205. return;
  206. }
  207. }