VexFlowGraphicalSymbolFactory.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 {SourceMeasure} from "../../VoiceData/SourceMeasure";
  8. import {GraphicalMeasure} from "../GraphicalMeasure";
  9. import {VexFlowMeasure} from "./VexFlowMeasure";
  10. import {SourceStaffEntry} from "../../VoiceData/SourceStaffEntry";
  11. import {GraphicalStaffEntry} from "../GraphicalStaffEntry";
  12. import {VexFlowStaffEntry} from "./VexFlowStaffEntry";
  13. import {Note} from "../../VoiceData/Note";
  14. import {ClefInstruction} from "../../VoiceData/Instructions/ClefInstruction";
  15. import {OctaveEnum} from "../../VoiceData/Expressions/ContinuousExpressions/OctaveShift";
  16. import {GraphicalNote} from "../GraphicalNote";
  17. import {Pitch} from "../../../Common/DataObjects/Pitch";
  18. import {VexFlowGraphicalNote} from "./VexFlowGraphicalNote";
  19. import {Fraction} from "../../../Common/DataObjects/Fraction";
  20. import {GraphicalChordSymbolContainer} from "../GraphicalChordSymbolContainer";
  21. import {GraphicalLabel} from "../GraphicalLabel";
  22. import {EngravingRules} from "../EngravingRules";
  23. import { TechnicalInstruction } from "../../VoiceData/Instructions/TechnicalInstruction";
  24. import { GraphicalVoiceEntry } from "../GraphicalVoiceEntry";
  25. import { VoiceEntry } from "../../VoiceData/VoiceEntry";
  26. import { VexFlowVoiceEntry } from "./VexFlowVoiceEntry";
  27. import { VexFlowConverter } from "./VexFlowConverter";
  28. import { VexFlowTabMeasure } from "./VexFlowTabMeasure";
  29. import { VexFlowStaffLine } from "./VexFlowStaffLine";
  30. export class VexFlowGraphicalSymbolFactory implements IGraphicalSymbolFactory {
  31. /**
  32. * Create a new music system for the given page.
  33. * Currently only one vertically endless page exists where all systems are put to.
  34. * @param page
  35. * @param systemIndex
  36. * @returns {VexFlowMusicSystem}
  37. */
  38. public createMusicSystem(systemIndex: number, rules: EngravingRules): MusicSystem {
  39. return new VexFlowMusicSystem(systemIndex, rules);
  40. }
  41. /**
  42. * Create a staffline object containing all staff measures belonging to a given system and staff.
  43. * @param parentSystem
  44. * @param parentStaff
  45. * @returns {VexFlowStaffLine}
  46. */
  47. public createStaffLine(parentSystem: MusicSystem, parentStaff: Staff): StaffLine {
  48. return new VexFlowStaffLine(parentSystem, parentStaff);
  49. }
  50. /**
  51. * Construct an empty graphicalMeasure from the given source measure and staff.
  52. * @param sourceMeasure
  53. * @param staff
  54. * @returns {VexFlowMeasure}
  55. */
  56. public createGraphicalMeasure(sourceMeasure: SourceMeasure, staff: Staff, isTabMeasure: boolean = false): GraphicalMeasure {
  57. return new VexFlowMeasure(staff, sourceMeasure, undefined);
  58. }
  59. /**
  60. * Construct an empty Tab staffMeasure from the given source measure and staff.
  61. * @param sourceMeasure
  62. * @param staff
  63. * @returns {VexFlowTabMeasure}
  64. */
  65. public createTabStaffMeasure(sourceMeasure: SourceMeasure, staff: Staff): GraphicalMeasure {
  66. return new VexFlowTabMeasure(staff, sourceMeasure);
  67. }
  68. /**
  69. * Create empty measure, which will be used to show key, rhythm changes at the end of the system.
  70. * @param staffLine
  71. * @returns {VexFlowMeasure}
  72. */
  73. public createExtraGraphicalMeasure(staffLine: StaffLine): GraphicalMeasure {
  74. return new VexFlowMeasure(staffLine.ParentStaff, undefined, staffLine);
  75. }
  76. /**
  77. * Create a staffEntry in the given measure for a given sourceStaffEntry.
  78. * @param sourceStaffEntry
  79. * @param measure
  80. * @returns {VexFlowStaffEntry}
  81. */
  82. public createStaffEntry(sourceStaffEntry: SourceStaffEntry, measure: GraphicalMeasure): GraphicalStaffEntry {
  83. return new VexFlowStaffEntry(<VexFlowMeasure>measure, sourceStaffEntry, undefined);
  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. */
  121. public addGraphicalAccidental(graphicalNote: GraphicalNote, pitch: Pitch): void {
  122. const note: VexFlowGraphicalNote = (graphicalNote as VexFlowGraphicalNote);
  123. // accidental is added in setPitch
  124. note.setPitch(pitch);
  125. }
  126. /**
  127. * Adds a Fermata symbol at the last note of the given tied Note.
  128. * The last graphical note of this tied note is located at the given graphicalStaffEntry.
  129. * A Fermata has to be located at the last tied note.
  130. * @param tiedNote
  131. * @param graphicalStaffEntry
  132. */
  133. public addFermataAtTiedEndNote(tiedNote: Note, graphicalStaffEntry: GraphicalStaffEntry): void {
  134. return;
  135. }
  136. /**
  137. * Adds a clef change within a measure before the given staff entry.
  138. * @param graphicalStaffEntry
  139. * @param clefInstruction
  140. */
  141. public createInStaffClef(graphicalStaffEntry: GraphicalStaffEntry, clefInstruction: ClefInstruction): void {
  142. const se: VexFlowStaffEntry = graphicalStaffEntry as VexFlowStaffEntry;
  143. const vfClefParams: { type: string, size: string, annotation: string } = VexFlowConverter.Clef(clefInstruction, "small");
  144. se.vfClefBefore = new Vex.Flow.ClefNote(vfClefParams.type, vfClefParams.size, vfClefParams.annotation);
  145. return;
  146. }
  147. /**
  148. * Adds a chord symbol at the given staff entry
  149. * @param sourceStaffEntry
  150. * @param graphicalStaffEntry
  151. * @param transposeHalftones
  152. */
  153. public createChordSymbols(sourceStaffEntry: SourceStaffEntry, graphicalStaffEntry: GraphicalStaffEntry, transposeHalftones: number): void {
  154. const rules: EngravingRules = graphicalStaffEntry.parentMeasure.parentSourceMeasure.Rules;
  155. let xShift: number = 0;
  156. const chordSymbolSpacing: number = rules.ChordSymbolXSpacing;
  157. for (const chordSymbolContainer of sourceStaffEntry.ChordContainers) {
  158. const graphicalChordSymbolContainer: GraphicalChordSymbolContainer =
  159. new GraphicalChordSymbolContainer(chordSymbolContainer,
  160. graphicalStaffEntry.PositionAndShape,
  161. rules.ChordSymbolTextHeight,
  162. transposeHalftones,
  163. graphicalStaffEntry.parentMeasure.parentSourceMeasure.Rules
  164. );
  165. const graphicalLabel: GraphicalLabel = graphicalChordSymbolContainer.GetGraphicalLabel;
  166. graphicalLabel.PositionAndShape.RelativePosition.y -= rules.ChordSymbolYOffset;
  167. graphicalLabel.PositionAndShape.RelativePosition.x += xShift;
  168. // TODO check for available space until next staffEntry or chord symbol (x direction)
  169. graphicalLabel.setLabelPositionAndShapeBorders();
  170. graphicalChordSymbolContainer.PositionAndShape.calculateBoundingBox();
  171. graphicalStaffEntry.graphicalChordContainers.push(graphicalChordSymbolContainer);
  172. xShift += graphicalLabel.PositionAndShape.Size.width + chordSymbolSpacing;
  173. }
  174. }
  175. /**
  176. * Adds a technical instruction at the given staff entry.
  177. * @param technicalInstruction
  178. * @param graphicalStaffEntry
  179. */
  180. public createGraphicalTechnicalInstruction(technicalInstruction: TechnicalInstruction, graphicalStaffEntry: GraphicalStaffEntry): void {
  181. return;
  182. }
  183. }