VexFlowGraphicalSymbolFactory.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import Vex = require("vexflow");
  2. import {IGraphicalSymbolFactory} from "../../Interfaces/IGraphicalSymbolFactory";
  3. import {MusicSystem} from "../MusicSystem";
  4. import {VexFlowMusicSystem} from "./VexFlowMusicSystem";
  5. import {Staff} from "../../VoiceData/Staff";
  6. import {StaffLine} from "../StaffLine";
  7. import {VexFlowStaffLine} from "./VexFlowStaffLine";
  8. import {SourceMeasure} from "../../VoiceData/SourceMeasure";
  9. import {GraphicalMeasure} from "../GraphicalMeasure";
  10. import {VexFlowMeasure} from "./VexFlowMeasure";
  11. import {SourceStaffEntry} from "../../VoiceData/SourceStaffEntry";
  12. import {GraphicalStaffEntry} from "../GraphicalStaffEntry";
  13. import {VexFlowStaffEntry} from "./VexFlowStaffEntry";
  14. import {Note} from "../../VoiceData/Note";
  15. import {ClefInstruction} from "../../VoiceData/Instructions/ClefInstruction";
  16. import {OctaveEnum} from "../../VoiceData/Expressions/ContinuousExpressions/OctaveShift";
  17. import {GraphicalNote} from "../GraphicalNote";
  18. import {Pitch} from "../../../Common/DataObjects/Pitch";
  19. import {VexFlowGraphicalNote} from "./VexFlowGraphicalNote";
  20. import {Fraction} from "../../../Common/DataObjects/Fraction";
  21. import {GraphicalChordSymbolContainer} from "../GraphicalChordSymbolContainer";
  22. import {GraphicalLabel} from "../GraphicalLabel";
  23. import {EngravingRules} from "../EngravingRules";
  24. import { TechnicalInstruction } from "../../VoiceData/Instructions/TechnicalInstruction";
  25. import { GraphicalVoiceEntry } from "../GraphicalVoiceEntry";
  26. import { VoiceEntry } from "../../VoiceData/VoiceEntry";
  27. import { VexFlowVoiceEntry } from "./VexFlowVoiceEntry";
  28. import { VexFlowConverter } from "./VexFlowConverter";
  29. export class VexFlowGraphicalSymbolFactory implements IGraphicalSymbolFactory {
  30. /**
  31. * Create a new music system for the given page.
  32. * Currently only one vertically endless page exists where all systems are put to.
  33. * @param page
  34. * @param systemIndex
  35. * @returns {VexFlowMusicSystem}
  36. */
  37. public createMusicSystem(systemIndex: number): MusicSystem {
  38. return new VexFlowMusicSystem(systemIndex);
  39. }
  40. /**
  41. * Create a staffline object containing all staff measures belonging to a given system and staff.
  42. * @param parentSystem
  43. * @param parentStaff
  44. * @returns {VexFlowStaffLine}
  45. */
  46. public createStaffLine(parentSystem: MusicSystem, parentStaff: Staff): StaffLine {
  47. return new VexFlowStaffLine(parentSystem, parentStaff);
  48. }
  49. /**
  50. * Construct an empty graphicalMeasure from the given source measure and staff.
  51. * @param sourceMeasure
  52. * @param staff
  53. * @returns {VexFlowMeasure}
  54. */
  55. public createGraphicalMeasure(sourceMeasure: SourceMeasure, staff: Staff): GraphicalMeasure {
  56. return new VexFlowMeasure(staff, undefined, sourceMeasure);
  57. }
  58. /**
  59. * Create empty measure, which will be used to show key, rhythm changes at the end of the system.
  60. * @param staffLine
  61. * @returns {VexFlowMeasure}
  62. */
  63. public createExtraGraphicalMeasure(staffLine: StaffLine): GraphicalMeasure {
  64. return new VexFlowMeasure(staffLine.ParentStaff, staffLine);
  65. }
  66. /**
  67. * Create a staffEntry in the given measure for a given sourceStaffEntry.
  68. * @param sourceStaffEntry
  69. * @param measure
  70. * @returns {VexFlowStaffEntry}
  71. */
  72. public createStaffEntry(sourceStaffEntry: SourceStaffEntry, measure: GraphicalMeasure): GraphicalStaffEntry {
  73. return new VexFlowStaffEntry(<VexFlowMeasure>measure, sourceStaffEntry, undefined);
  74. }
  75. public createVoiceEntry(parentVoiceEntry: VoiceEntry, parentStaffEntry: GraphicalStaffEntry): GraphicalVoiceEntry {
  76. return new VexFlowVoiceEntry(parentVoiceEntry, parentStaffEntry);
  77. }
  78. /**
  79. * Create a Graphical Note for given note and clef and as part of graphicalStaffEntry.
  80. * @param note
  81. * @param numberOfDots The number of dots the note has to increase its musical duration.
  82. * @param graphicalStaffEntry
  83. * @param activeClef The currently active clef, needed for positioning the note vertically
  84. * @param octaveShift The currently active octave transposition enum, needed for positioning the note vertically
  85. * @returns {GraphicalNote}
  86. */
  87. public createNote(note: Note, graphicalVoiceEntry: GraphicalVoiceEntry,
  88. activeClef: ClefInstruction, octaveShift: OctaveEnum = OctaveEnum.NONE, graphicalNoteLength: Fraction = undefined): GraphicalNote {
  89. // Creates and returns the note:
  90. return new VexFlowGraphicalNote(note, graphicalVoiceEntry, activeClef, octaveShift, graphicalNoteLength);
  91. }
  92. /**
  93. * Create a Graphical Grace Note (smaller head, stem...) for given note and clef and as part of graphicalStaffEntry.
  94. * @param note
  95. * @param numberOfDots
  96. * @param graphicalVoiceEntry
  97. * @param activeClef
  98. * @param octaveShift
  99. * @returns {GraphicalNote}
  100. */
  101. public createGraceNote(note: Note, graphicalVoiceEntry: GraphicalVoiceEntry,
  102. activeClef: ClefInstruction, octaveShift: OctaveEnum = OctaveEnum.NONE): GraphicalNote {
  103. return new VexFlowGraphicalNote(note, graphicalVoiceEntry, activeClef, octaveShift);
  104. }
  105. /**
  106. * Sets a pitch which will be used for rendering the given graphical note (not changing the original pitch of the note!!!).
  107. * Will be only called if the displayed accidental is different from the original (e.g. a C# with C# as key instruction)
  108. * @param graphicalNote
  109. * @param pitch The pitch which will be rendered.
  110. */
  111. public addGraphicalAccidental(graphicalNote: GraphicalNote, pitch: Pitch): void {
  112. const note: VexFlowGraphicalNote = (graphicalNote as VexFlowGraphicalNote);
  113. // accidental is added in setPitch
  114. note.setPitch(pitch);
  115. }
  116. /**
  117. * Adds a Fermata symbol at the last note of the given tied Note.
  118. * The last graphical note of this tied note is located at the given graphicalStaffEntry.
  119. * A Fermata has to be located at the last tied note.
  120. * @param tiedNote
  121. * @param graphicalStaffEntry
  122. */
  123. public addFermataAtTiedEndNote(tiedNote: Note, graphicalStaffEntry: GraphicalStaffEntry): void {
  124. return;
  125. }
  126. /**
  127. * Adds a clef change within a measure before the given staff entry.
  128. * @param graphicalStaffEntry
  129. * @param clefInstruction
  130. */
  131. public createInStaffClef(graphicalStaffEntry: GraphicalStaffEntry, clefInstruction: ClefInstruction): void {
  132. const se: VexFlowStaffEntry = graphicalStaffEntry as VexFlowStaffEntry;
  133. const vfClefParams: { type: string, size: string, annotation: string } = VexFlowConverter.Clef(clefInstruction, "small");
  134. se.vfClefBefore = new Vex.Flow.ClefNote(vfClefParams.type, vfClefParams.size, vfClefParams.annotation);
  135. return;
  136. }
  137. /**
  138. * Adds a chord symbol at the given staff entry
  139. * @param sourceStaffEntry
  140. * @param graphicalStaffEntry
  141. * @param transposeHalftones
  142. */
  143. public createChordSymbols(sourceStaffEntry: SourceStaffEntry, graphicalStaffEntry: GraphicalStaffEntry, transposeHalftones: number): void {
  144. let xShift: number = 0;
  145. const chordSymbolSpacing: number = EngravingRules.Rules.ChordSymbolXSpacing;
  146. for (const chordSymbolContainer of sourceStaffEntry.ChordContainers) {
  147. const graphicalChordSymbolContainer: GraphicalChordSymbolContainer =
  148. new GraphicalChordSymbolContainer(chordSymbolContainer,
  149. graphicalStaffEntry.PositionAndShape,
  150. EngravingRules.Rules.ChordSymbolTextHeight,
  151. transposeHalftones);
  152. const graphicalLabel: GraphicalLabel = graphicalChordSymbolContainer.GetGraphicalLabel;
  153. graphicalLabel.PositionAndShape.RelativePosition.y -= EngravingRules.Rules.ChordSymbolYOffset;
  154. graphicalLabel.PositionAndShape.RelativePosition.x += xShift;
  155. // TODO check for available space until next staffEntry or chord symbol (x direction)
  156. graphicalLabel.setLabelPositionAndShapeBorders();
  157. graphicalChordSymbolContainer.PositionAndShape.calculateBoundingBox();
  158. graphicalStaffEntry.graphicalChordContainers.push(graphicalChordSymbolContainer);
  159. xShift += graphicalLabel.PositionAndShape.Size.width + chordSymbolSpacing;
  160. }
  161. }
  162. /**
  163. * Adds a technical instruction at the given staff entry.
  164. * @param technicalInstruction
  165. * @param graphicalStaffEntry
  166. */
  167. public createGraphicalTechnicalInstruction(technicalInstruction: TechnicalInstruction, graphicalStaffEntry: GraphicalStaffEntry): void {
  168. return;
  169. }
  170. }