VexFlowGraphicalSymbolFactory.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import {IGraphicalSymbolFactory} from "../../Interfaces/IGraphicalSymbolFactory";
  2. import {GraphicalMusicPage} from "../GraphicalMusicPage";
  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 {StaffMeasure} from "../StaffMeasure";
  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. export class VexFlowGraphicalSymbolFactory implements IGraphicalSymbolFactory {
  29. /**
  30. * Create a new music system for the given page.
  31. * Currently only one vertically endless page exists where all systems are put to.
  32. * @param page
  33. * @param systemIndex
  34. * @returns {VexFlowMusicSystem}
  35. */
  36. public createMusicSystem(page: GraphicalMusicPage, systemIndex: number): MusicSystem {
  37. return new VexFlowMusicSystem(page, systemIndex);
  38. }
  39. /**
  40. * Create a staffline object containing all staff measures belonging to a given system and staff.
  41. * @param parentSystem
  42. * @param parentStaff
  43. * @returns {VexFlowStaffLine}
  44. */
  45. public createStaffLine(parentSystem: MusicSystem, parentStaff: Staff): StaffLine {
  46. return new VexFlowStaffLine(parentSystem, parentStaff);
  47. }
  48. /**
  49. * Construct an empty staffMeasure from the given source measure and staff.
  50. * @param sourceMeasure
  51. * @param staff
  52. * @returns {VexFlowMeasure}
  53. */
  54. public createStaffMeasure(sourceMeasure: SourceMeasure, staff: Staff): StaffMeasure {
  55. return new VexFlowMeasure(staff, undefined, sourceMeasure);
  56. }
  57. /**
  58. * Create empty measure, which will be used to show key, rhythm changes at the end of the system.
  59. * @param staffLine
  60. * @returns {VexFlowMeasure}
  61. */
  62. public createExtraStaffMeasure(staffLine: StaffLine): StaffMeasure {
  63. return new VexFlowMeasure(staffLine.ParentStaff, staffLine);
  64. }
  65. /**
  66. * Create a staffEntry in the given measure for a given sourceStaffEntry.
  67. * @param sourceStaffEntry
  68. * @param measure
  69. * @returns {VexFlowStaffEntry}
  70. */
  71. public createStaffEntry(sourceStaffEntry: SourceStaffEntry, measure: StaffMeasure): GraphicalStaffEntry {
  72. return new VexFlowStaffEntry(<VexFlowMeasure>measure, sourceStaffEntry, undefined);
  73. }
  74. /**
  75. * Create an empty staffEntry which will be used for grace notes.
  76. * it will be linked to the given staffEntryParent, which is a staffEntry for normal notes.
  77. * Grace notes are always given before (rarely also after) normal notes.
  78. * @param staffEntryParent
  79. * @param measure
  80. * @returns {VexFlowStaffEntry}
  81. */
  82. public createGraceStaffEntry(staffEntryParent: GraphicalStaffEntry, measure: StaffMeasure): GraphicalStaffEntry {
  83. return new VexFlowStaffEntry(<VexFlowMeasure>measure, undefined, <VexFlowStaffEntry>staffEntryParent);
  84. }
  85. public createVoiceEntry(parentVoiceEntry: VoiceEntry, parentStaffEntry: GraphicalStaffEntry): GraphicalVoiceEntry {
  86. return new VexFlowVoiceEntry(parentVoiceEntry, parentStaffEntry);
  87. }
  88. /**
  89. * Create a Graphical Note for given note and clef and as part of graphicalStaffEntry.
  90. * @param note
  91. * @param numberOfDots The number of dots the note has to increase its musical duration.
  92. * @param graphicalStaffEntry
  93. * @param activeClef The currently active clef, needed for positioning the note vertically
  94. * @param octaveShift The currently active octave transposition enum, needed for positioning the note vertically
  95. * @returns {GraphicalNote}
  96. */
  97. public createNote(note: Note, graphicalVoiceEntry: GraphicalVoiceEntry,
  98. activeClef: ClefInstruction, octaveShift: OctaveEnum = OctaveEnum.NONE, graphicalNoteLength: Fraction = undefined): GraphicalNote {
  99. // Creates and returns the note:
  100. return new VexFlowGraphicalNote(note, graphicalVoiceEntry, activeClef, octaveShift, graphicalNoteLength);
  101. }
  102. /**
  103. * Create a Graphical Grace Note (smaller head, stem...) for given note and clef and as part of graphicalStaffEntry.
  104. * @param note
  105. * @param numberOfDots
  106. * @param graphicalVoiceEntry
  107. * @param activeClef
  108. * @param octaveShift
  109. * @returns {GraphicalNote}
  110. */
  111. public createGraceNote(note: Note, graphicalVoiceEntry: GraphicalVoiceEntry,
  112. activeClef: ClefInstruction, octaveShift: OctaveEnum = OctaveEnum.NONE): GraphicalNote {
  113. return new VexFlowGraphicalNote(note, graphicalVoiceEntry, activeClef, octaveShift);
  114. }
  115. /**
  116. * Sets a pitch which will be used for rendering the given graphical note (not changing the original pitch of the note!!!).
  117. * Will be only called if the displayed accidental is different from the original (e.g. a C# with C# as key instruction)
  118. * @param graphicalNote
  119. * @param pitch The pitch which will be rendered.
  120. * @param grace
  121. * @param graceScalingFactor
  122. */
  123. public addGraphicalAccidental(graphicalNote: GraphicalNote, pitch: Pitch, grace: boolean, graceScalingFactor: number): void {
  124. // ToDo: set accidental here from pitch.Accidental
  125. const note: VexFlowGraphicalNote = (graphicalNote as VexFlowGraphicalNote);
  126. note.setPitch(pitch);
  127. }
  128. /**
  129. * Adds a Fermata symbol at the last note of the given tied Note.
  130. * The last graphical note of this tied note is located at the given graphicalStaffEntry.
  131. * A Fermata has to be located at the last tied note.
  132. * @param tiedNote
  133. * @param graphicalStaffEntry
  134. */
  135. public addFermataAtTiedEndNote(tiedNote: Note, graphicalStaffEntry: GraphicalStaffEntry): void {
  136. return;
  137. }
  138. /**
  139. * Adds a clef change within a measure before the given staff entry.
  140. * @param graphicalStaffEntry
  141. * @param clefInstruction
  142. */
  143. public createInStaffClef(graphicalStaffEntry: GraphicalStaffEntry, clefInstruction: ClefInstruction): void {
  144. return;
  145. }
  146. /**
  147. * Adds a chord symbol at the given staff entry
  148. * @param sourceStaffEntry
  149. * @param graphicalStaffEntry
  150. * @param transposeHalftones
  151. */
  152. public createChordSymbol(sourceStaffEntry: SourceStaffEntry, graphicalStaffEntry: GraphicalStaffEntry, transposeHalftones: number): void {
  153. const graphicalChordSymbolContainer: GraphicalChordSymbolContainer =
  154. new GraphicalChordSymbolContainer(sourceStaffEntry.ChordContainer,
  155. graphicalStaffEntry.PositionAndShape,
  156. EngravingRules.Rules.ChordSymbolTextHeight,
  157. transposeHalftones);
  158. const graphicalLabel: GraphicalLabel = graphicalChordSymbolContainer.GetGraphicalLabel;
  159. graphicalLabel.setLabelPositionAndShapeBorders();
  160. graphicalChordSymbolContainer.PositionAndShape.calculateBoundingBox();
  161. graphicalStaffEntry.graphicalChordContainer = graphicalChordSymbolContainer;
  162. }
  163. /**
  164. * Adds a technical instruction at the given staff entry.
  165. * @param technicalInstruction
  166. * @param graphicalStaffEntry
  167. */
  168. public createGraphicalTechnicalInstruction(technicalInstruction: TechnicalInstruction, graphicalStaffEntry: GraphicalStaffEntry): void {
  169. return;
  170. }
  171. }