MusicSheetCalculator.ts 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  1. import {GraphicalStaffEntry} from "./GraphicalStaffEntry";
  2. import {StaffLine} from "./StaffLine";
  3. import {GraphicalMusicSheet} from "./GraphicalMusicSheet";
  4. import {EngravingRules} from "./EngravingRules";
  5. import {Tie} from "../VoiceData/Tie";
  6. import {Fraction} from "../../Common/DataObjects/Fraction";
  7. import {Note} from "../VoiceData/Note";
  8. import {MusicSheet} from "../MusicSheet";
  9. import {GraphicalMeasure} from "./GraphicalMeasure";
  10. import {ClefInstruction} from "../VoiceData/Instructions/ClefInstruction";
  11. import {LyricWord} from "../VoiceData/Lyrics/LyricsWord";
  12. import {SourceMeasure} from "../VoiceData/SourceMeasure";
  13. import {GraphicalMusicPage} from "./GraphicalMusicPage";
  14. import {GraphicalNote} from "./GraphicalNote";
  15. import {Beam} from "../VoiceData/Beam";
  16. import {OctaveEnum} from "../VoiceData/Expressions/ContinuousExpressions/OctaveShift";
  17. import {VoiceEntry, StemDirectionType} from "../VoiceData/VoiceEntry";
  18. import {OrnamentContainer} from "../VoiceData/OrnamentContainer";
  19. import {ArticulationEnum} from "../VoiceData/VoiceEntry";
  20. import {Tuplet} from "../VoiceData/Tuplet";
  21. import {MusicSystem} from "./MusicSystem";
  22. import {GraphicalTie} from "./GraphicalTie";
  23. import {RepetitionInstruction} from "../VoiceData/Instructions/RepetitionInstruction";
  24. import {MultiExpression} from "../VoiceData/Expressions/MultiExpression";
  25. import {StaffEntryLink} from "../VoiceData/StaffEntryLink";
  26. import {MusicSystemBuilder} from "./MusicSystemBuilder";
  27. import {MultiTempoExpression} from "../VoiceData/Expressions/MultiTempoExpression";
  28. import {Repetition} from "../MusicSource/Repetition";
  29. import {PointF2D} from "../../Common/DataObjects/PointF2D";
  30. import {SourceStaffEntry} from "../VoiceData/SourceStaffEntry";
  31. import {BoundingBox} from "./BoundingBox";
  32. import {Instrument} from "../Instrument";
  33. import {GraphicalLabel} from "./GraphicalLabel";
  34. import {TextAlignment} from "../../Common/Enums/TextAlignment";
  35. import {VerticalGraphicalStaffEntryContainer} from "./VerticalGraphicalStaffEntryContainer";
  36. import {KeyInstruction} from "../VoiceData/Instructions/KeyInstruction";
  37. import {AbstractNotationInstruction} from "../VoiceData/Instructions/AbstractNotationInstruction";
  38. import {TechnicalInstruction} from "../VoiceData/Instructions/TechnicalInstruction";
  39. import {Pitch} from "../../Common/DataObjects/Pitch";
  40. import {LinkedVoice} from "../VoiceData/LinkedVoice";
  41. import {ColDirEnum} from "./BoundingBox";
  42. import {IGraphicalSymbolFactory} from "../Interfaces/IGraphicalSymbolFactory";
  43. import {ITextMeasurer} from "../Interfaces/ITextMeasurer";
  44. import {ITransposeCalculator} from "../Interfaces/ITransposeCalculator";
  45. import {OctaveShiftParams} from "./OctaveShiftParams";
  46. import {AccidentalCalculator} from "./AccidentalCalculator";
  47. import {MidiInstrument} from "../VoiceData/Instructions/ClefInstruction";
  48. import {Staff} from "../VoiceData/Staff";
  49. import {OctaveShift} from "../VoiceData/Expressions/ContinuousExpressions/OctaveShift";
  50. import * as log from "loglevel";
  51. import Dictionary from "typescript-collections/dist/lib/Dictionary";
  52. import { GraphicalLyricEntry } from "./GraphicalLyricEntry";
  53. import { GraphicalLyricWord } from "./GraphicalLyricWord";
  54. import { GraphicalLine } from "./GraphicalLine";
  55. import { Label } from "../Label";
  56. import { GraphicalVoiceEntry } from "./GraphicalVoiceEntry";
  57. import { VerticalSourceStaffEntryContainer } from "../VoiceData/VerticalSourceStaffEntryContainer";
  58. import { SkyBottomLineCalculator } from "./SkyBottomLineCalculator";
  59. /**
  60. * Class used to do all the calculations in a MusicSheet, which in the end populates a GraphicalMusicSheet.
  61. */
  62. export abstract class MusicSheetCalculator {
  63. public static symbolFactory: IGraphicalSymbolFactory;
  64. public static transposeCalculator: ITransposeCalculator;
  65. protected static textMeasurer: ITextMeasurer;
  66. protected staffEntriesWithGraphicalTies: GraphicalStaffEntry[] = [];
  67. protected staffEntriesWithOrnaments: GraphicalStaffEntry[] = [];
  68. protected staffEntriesWithChordSymbols: GraphicalStaffEntry[] = [];
  69. protected staffLinesWithLyricWords: StaffLine[] = [];
  70. protected staffLinesWithGraphicalExpressions: StaffLine[] = [];
  71. protected graphicalLyricWords: GraphicalLyricWord[] = [];
  72. protected graphicalMusicSheet: GraphicalMusicSheet;
  73. protected rules: EngravingRules;
  74. //protected symbolFactory: IGraphicalSymbolFactory;
  75. public static get TextMeasurer(): ITextMeasurer {
  76. return MusicSheetCalculator.textMeasurer;
  77. }
  78. public static set TextMeasurer(value: ITextMeasurer) {
  79. MusicSheetCalculator.textMeasurer = value;
  80. }
  81. protected get leadSheet(): boolean {
  82. return this.graphicalMusicSheet.LeadSheet;
  83. }
  84. private static setMeasuresMinStaffEntriesWidth(measures: GraphicalMeasure[], minimumStaffEntriesWidth: number): void {
  85. for (let idx: number = 0, len: number = measures.length; idx < len; ++idx) {
  86. const measure: GraphicalMeasure = measures[idx];
  87. measure.minimumStaffEntriesWidth = minimumStaffEntriesWidth;
  88. }
  89. }
  90. public initialize(graphicalMusicSheet: GraphicalMusicSheet): void {
  91. this.graphicalMusicSheet = graphicalMusicSheet;
  92. this.rules = graphicalMusicSheet.ParentMusicSheet.rules;
  93. this.prepareGraphicalMusicSheet();
  94. //this.calculate();
  95. }
  96. /**
  97. * Build the 2D [[GraphicalMeasure]] list needed for the [[MusicSheetCalculator]].
  98. * Internally it creates [[GraphicalMeasure]]s, [[GraphicalStaffEntry]]'s and [[GraphicalNote]]s.
  99. */
  100. public prepareGraphicalMusicSheet(): void {
  101. // Clear the stored system images dict - all systems have to be redrawn.
  102. // Not necessary now. TODO Check
  103. // this.graphicalMusicSheet.SystemImages.length = 0;
  104. const musicSheet: MusicSheet = this.graphicalMusicSheet.ParentMusicSheet;
  105. this.staffEntriesWithGraphicalTies = [];
  106. this.staffEntriesWithOrnaments = [];
  107. this.staffEntriesWithChordSymbols = [];
  108. this.staffLinesWithLyricWords = [];
  109. this.staffLinesWithGraphicalExpressions = [];
  110. this.graphicalMusicSheet.Initialize();
  111. const measureList: GraphicalMeasure[][] = this.graphicalMusicSheet.MeasureList;
  112. // one AccidentalCalculator for each Staff (regardless of Instrument)
  113. const accidentalCalculators: AccidentalCalculator[] = this.createAccidentalCalculators();
  114. // List of Active ClefInstructions
  115. const activeClefs: ClefInstruction[] = this.graphicalMusicSheet.initializeActiveClefs();
  116. // LyricWord - GraphicalLyricWord Lists
  117. const lyricWords: LyricWord[] = [];
  118. const completeNumberOfStaves: number = musicSheet.getCompleteNumberOfStaves();
  119. // Octave Shifts List
  120. const openOctaveShifts: OctaveShiftParams[] = [];
  121. // TieList - timestampsArray
  122. for (let i: number = 0; i < completeNumberOfStaves; i++) {
  123. openOctaveShifts.push(undefined);
  124. }
  125. // go through all SourceMeasures (taking into account normal SourceMusicParts and Repetitions)
  126. for (let idx: number = 0, len: number = musicSheet.SourceMeasures.length; idx < len; ++idx) {
  127. const sourceMeasure: SourceMeasure = musicSheet.SourceMeasures[idx];
  128. const graphicalMeasures: GraphicalMeasure[] = this.createGraphicalMeasuresForSourceMeasure(
  129. sourceMeasure,
  130. accidentalCalculators,
  131. lyricWords,
  132. openOctaveShifts,
  133. activeClefs
  134. );
  135. measureList.push(graphicalMeasures);
  136. }
  137. this.handleStaffEntries();
  138. this.calculateVerticalContainersList();
  139. this.setIndecesToVerticalGraphicalContainers();
  140. }
  141. /**
  142. * The main method for the Calculator.
  143. */
  144. public calculate(): void {
  145. this.clearSystemsAndMeasures();
  146. // delete graphicalObjects that will be recalculated and create the GraphicalObjects that strech over a single StaffEntry new.
  147. this.clearRecreatedObjects();
  148. this.createGraphicalTies();
  149. // calculate SheetLabelBoundingBoxes
  150. this.calculateSheetLabelBoundingBoxes();
  151. this.calculateXLayout(this.graphicalMusicSheet, this.maxInstrNameLabelLength());
  152. // create List<MusicPage>
  153. this.graphicalMusicSheet.MusicPages.length = 0;
  154. // create new MusicSystems and StaffLines (as many as necessary) and populate them with Measures from measureList
  155. this.calculateMusicSystems();
  156. // Add some white space at the end of the piece:
  157. this.graphicalMusicSheet.MusicPages[0].PositionAndShape.BorderMarginBottom += 9;
  158. // transform Relative to Absolute Positions
  159. GraphicalMusicSheet.transformRelativeToAbsolutePosition(this.graphicalMusicSheet);
  160. }
  161. public calculateXLayout(graphicalMusicSheet: GraphicalMusicSheet, maxInstrNameLabelLength: number): void {
  162. // for each inner List in big Measure List calculate new Positions for the StaffEntries
  163. // and adjust Measures sizes
  164. // calculate max measure length for maximum zoom in.
  165. let minLength: number = 0;
  166. const maxInstructionsLength: number = this.rules.MaxInstructionsConstValue;
  167. if (this.graphicalMusicSheet.MeasureList.length > 0) {
  168. let measures: GraphicalMeasure[] = this.graphicalMusicSheet.MeasureList[0];
  169. let minimumStaffEntriesWidth: number = this.calculateMeasureXLayout(measures);
  170. MusicSheetCalculator.setMeasuresMinStaffEntriesWidth(measures, minimumStaffEntriesWidth);
  171. minLength = minimumStaffEntriesWidth * 1.2 + maxInstrNameLabelLength + maxInstructionsLength;
  172. for (let i: number = 1; i < this.graphicalMusicSheet.MeasureList.length; i++) {
  173. measures = this.graphicalMusicSheet.MeasureList[i];
  174. minimumStaffEntriesWidth = this.calculateMeasureXLayout(measures);
  175. MusicSheetCalculator.setMeasuresMinStaffEntriesWidth(measures, minimumStaffEntriesWidth);
  176. minLength = Math.max(minLength, minimumStaffEntriesWidth * 1.2 + maxInstructionsLength);
  177. }
  178. }
  179. this.graphicalMusicSheet.MinAllowedSystemWidth = minLength;
  180. }
  181. protected formatMeasures(): void {
  182. throw new Error("abstract, not implemented");
  183. }
  184. /// <summary>
  185. /// This method calculates the relative Positions of all MusicSystems.
  186. /// </summary>
  187. /// <param name="graphicalMusicPage"></param>
  188. protected calculateMusicSystemsRelativePositions(graphicalMusicPage: GraphicalMusicPage): void {
  189. // xPosition is always fix
  190. let relativePosition: PointF2D = new PointF2D(this.rules.PageLeftMargin + this.rules.SystemLeftMargin, 0);
  191. // first System is handled extra
  192. const firstMusicSystem: MusicSystem = graphicalMusicPage.MusicSystems[0];
  193. if (graphicalMusicPage === graphicalMusicPage.Parent.MusicPages[0]) {
  194. relativePosition.y = this.rules.PageTopMargin + this.rules.TitleTopDistance + this.rules.SheetTitleHeight +
  195. this.rules.TitleBottomDistance;
  196. } else {
  197. relativePosition.y = this.rules.PageTopMargin + this.rules.TitleTopDistance;
  198. }
  199. firstMusicSystem.PositionAndShape.RelativePosition = relativePosition;
  200. for (let i: number = 1; i < graphicalMusicPage.MusicSystems.length; i++) {
  201. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[i];
  202. relativePosition = new PointF2D(this.rules.PageLeftMargin + this.rules.SystemLeftMargin, 0);
  203. // find optimum distance between Systems
  204. const previousSystem: MusicSystem = graphicalMusicPage.MusicSystems[i - 1];
  205. const lastPreviousStaffLine: StaffLine = previousSystem.StaffLines[previousSystem.StaffLines.length - 1];
  206. const distance: number = (lastPreviousStaffLine.SkyBottomLineCalculator.getBottomLineMax() - this.rules.StaffHeight) +
  207. Math.abs(musicSystem.StaffLines[0].SkyBottomLineCalculator.getSkyLineMin()) +
  208. this.rules.MinimumAllowedDistanceBetweenSystems;
  209. relativePosition.y = previousSystem.PositionAndShape.RelativePosition.y +
  210. lastPreviousStaffLine.PositionAndShape.RelativePosition.y +
  211. this.rules.StaffHeight + Math.max(this.rules.SystemDistance, distance);
  212. musicSystem.PositionAndShape.RelativePosition = relativePosition;
  213. }
  214. }
  215. /**
  216. * Calculates the x layout of the staff entries within the staff measures belonging to one source measure.
  217. * All staff entries are x-aligned throughout all the measures.
  218. * @param measures - The minimum required x width of the source measure
  219. */
  220. protected calculateMeasureXLayout(measures: GraphicalMeasure[]): number {
  221. throw new Error("abstract, not implemented");
  222. }
  223. /**
  224. * This method checks the distances between two System's StaffLines and if needed, shifts the lower down.
  225. * @param musicSystem
  226. */
  227. protected optimizeDistanceBetweenStaffLines(musicSystem: MusicSystem): void {
  228. musicSystem.PositionAndShape.calculateAbsolutePositionsRecursive(0, 0);
  229. // don't perform any y-spacing in case of a StaffEntryLink (in both StaffLines)
  230. if (!musicSystem.checkStaffEntriesForStaffEntryLink()) {
  231. for (let i: number = 0; i < musicSystem.StaffLines.length - 1; i++) {
  232. const upperBottomLine: number = musicSystem.StaffLines[i].SkyBottomLineCalculator.getBottomLineMax();
  233. // TODO: Lower skyline should add to offset when there are items above the line. Currently no test
  234. // file available
  235. // const lowerSkyLine: number = Math.min(...musicSystem.StaffLines[i + 1].SkyLine);
  236. if (Math.abs(upperBottomLine) > this.rules.MinimumStaffLineDistance) {
  237. // Remove staffheight from offset. As it results in huge distances
  238. const offset: number = Math.abs(upperBottomLine) + this.rules.MinimumStaffLineDistance - this.rules.StaffHeight;
  239. this.updateStaffLinesRelativePosition(musicSystem, i + 1, offset);
  240. }
  241. }
  242. }
  243. }
  244. /**
  245. * This method updates the System's StaffLine's RelativePosition (starting from the given index).
  246. * @param musicSystem
  247. * @param index
  248. * @param value
  249. */
  250. protected updateStaffLinesRelativePosition(musicSystem: MusicSystem, index: number, value: number): void {
  251. for (let i: number = index; i < musicSystem.StaffLines.length; i++) {
  252. musicSystem.StaffLines[i].PositionAndShape.RelativePosition.y += value;
  253. }
  254. musicSystem.PositionAndShape.BorderBottom += value;
  255. }
  256. protected calculateSystemYLayout(): void {
  257. throw new Error("abstract, not implemented");
  258. }
  259. /**
  260. * Called for every source measure when generating the list of staff measures for it.
  261. */
  262. protected initGraphicalMeasuresCreation(): void {
  263. throw new Error("abstract, not implemented");
  264. }
  265. protected handleBeam(graphicalNote: GraphicalNote, beam: Beam, openBeams: Beam[]): void {
  266. throw new Error("abstract, not implemented");
  267. }
  268. /**
  269. * Check if the tied graphical note belongs to any beams or tuplets and react accordingly.
  270. * @param tiedGraphicalNote
  271. * @param beams
  272. * @param activeClef
  273. * @param octaveShiftValue
  274. * @param graphicalStaffEntry
  275. * @param duration
  276. * @param openTie
  277. * @param isLastTieNote
  278. */
  279. protected handleTiedGraphicalNote(tiedGraphicalNote: GraphicalNote, beams: Beam[], activeClef: ClefInstruction,
  280. octaveShiftValue: OctaveEnum, graphicalStaffEntry: GraphicalStaffEntry, duration: Fraction,
  281. openTie: Tie, isLastTieNote: boolean): void {
  282. throw new Error("abstract, not implemented");
  283. }
  284. protected handleVoiceEntryLyrics(voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry,
  285. openLyricWords: LyricWord[]): void {
  286. throw new Error("abstract, not implemented");
  287. }
  288. protected handleVoiceEntryOrnaments(ornamentContainer: OrnamentContainer, voiceEntry: VoiceEntry,
  289. graphicalStaffEntry: GraphicalStaffEntry): void {
  290. throw new Error("abstract, not implemented");
  291. }
  292. protected handleVoiceEntryArticulations(articulations: ArticulationEnum[],
  293. voiceEntry: VoiceEntry,
  294. staffEntry: GraphicalStaffEntry): void {
  295. throw new Error("abstract, not implemented");
  296. }
  297. /**
  298. * Adds a technical instruction at the given staff entry.
  299. * @param technicalInstructions
  300. * @param voiceEntry
  301. * @param staffEntry
  302. */
  303. protected handleVoiceEntryTechnicalInstructions(technicalInstructions: TechnicalInstruction[],
  304. voiceEntry: VoiceEntry, staffEntry: GraphicalStaffEntry): void {
  305. throw new Error("abstract, not implemented");
  306. }
  307. protected handleTuplet(graphicalNote: GraphicalNote, tuplet: Tuplet, openTuplets: Tuplet[]): void {
  308. throw new Error("abstract, not implemented");
  309. }
  310. protected layoutVoiceEntry(voiceEntry: VoiceEntry, graphicalNotes: GraphicalNote[],
  311. graphicalStaffEntry: GraphicalStaffEntry, hasPitchedNote: boolean): void {
  312. throw new Error("abstract, not implemented");
  313. }
  314. protected layoutStaffEntry(graphicalStaffEntry: GraphicalStaffEntry): void {
  315. throw new Error("abstract, not implemented");
  316. }
  317. protected createGraphicalTie(tie: Tie, startGse: GraphicalStaffEntry, endGse: GraphicalStaffEntry, startNote: GraphicalNote,
  318. endNote: GraphicalNote): GraphicalTie {
  319. throw new Error("abstract, not implemented");
  320. }
  321. protected updateStaffLineBorders(staffLine: StaffLine): void {
  322. throw new Error("abstract, not implemented");
  323. }
  324. /**
  325. * Iterate through all Measures and calculates the MeasureNumberLabels.
  326. * @param musicSystem
  327. */
  328. protected calculateMeasureNumberPlacement(musicSystem: MusicSystem): void {
  329. const staffLine: StaffLine = musicSystem.StaffLines[0];
  330. let currentMeasureNumber: number = staffLine.Measures[0].MeasureNumber;
  331. for (const measure of staffLine.Measures) {
  332. if (measure.MeasureNumber === 0 || measure.MeasureNumber === 1) {
  333. currentMeasureNumber = measure.MeasureNumber;
  334. }
  335. if ((measure.MeasureNumber === currentMeasureNumber ||
  336. measure.MeasureNumber === currentMeasureNumber + this.rules.MeasureNumberLabelOffset) &&
  337. !measure.parentSourceMeasure.ImplicitMeasure) {
  338. if (measure.MeasureNumber !== 1 ||
  339. (measure.MeasureNumber === 1 && measure !== staffLine.Measures[0])) {
  340. this.calculateSingleMeasureNumberPlacement(measure, staffLine, musicSystem);
  341. }
  342. currentMeasureNumber = measure.MeasureNumber;
  343. }
  344. }
  345. }
  346. /// <summary>
  347. /// This method calculates a single MeasureNumberLabel and adds it to the graphical label list of the music system
  348. /// </summary>
  349. /// <param name="measure"></param>
  350. /// <param name="staffLine"></param>
  351. /// <param name="musicSystem"></param>
  352. private calculateSingleMeasureNumberPlacement(measure: GraphicalMeasure, staffLine: StaffLine, musicSystem: MusicSystem): void {
  353. const labelNumber: string = measure.MeasureNumber.toString();
  354. const graphicalLabel: GraphicalLabel = new GraphicalLabel(new Label(labelNumber), this.rules.MeasureNumberLabelHeight,
  355. TextAlignment.LeftBottom);
  356. const skyBottomLineCalculator: SkyBottomLineCalculator = staffLine.SkyBottomLineCalculator;
  357. // calculate LabelBoundingBox and set PSI parent
  358. graphicalLabel.setLabelPositionAndShapeBorders();
  359. graphicalLabel.PositionAndShape.Parent = musicSystem.PositionAndShape;
  360. // calculate relative Position
  361. const relativeX: number = staffLine.PositionAndShape.RelativePosition.x +
  362. measure.PositionAndShape.RelativePosition.x - graphicalLabel.PositionAndShape.BorderMarginLeft;
  363. let relativeY: number;
  364. // and the corresponding SkyLine indeces
  365. let start: number = relativeX;
  366. let end: number = relativeX - graphicalLabel.PositionAndShape.BorderLeft + graphicalLabel.PositionAndShape.BorderMarginRight;
  367. // take into account the InstrumentNameLabel's at the beginning of the first MusicSystem
  368. if (staffLine === musicSystem.StaffLines[0] && musicSystem === musicSystem.Parent.MusicSystems[0]) {
  369. start -= staffLine.PositionAndShape.RelativePosition.x;
  370. end -= staffLine.PositionAndShape.RelativePosition.x;
  371. }
  372. // get the minimum corresponding SkyLine value
  373. const skyLineMinValue: number = skyBottomLineCalculator.getSkyLineMinInRange(start, end);
  374. if (measure === staffLine.Measures[0]) {
  375. // must take into account possible MusicSystem Bracket's
  376. let minBracketTopBorder: number = 0;
  377. if (musicSystem.GroupBrackets.length > 0) {
  378. for (const groupBracket of musicSystem.GroupBrackets) {
  379. minBracketTopBorder = Math.min(minBracketTopBorder, groupBracket.PositionAndShape.BorderTop);
  380. }
  381. }
  382. relativeY = Math.min(skyLineMinValue, minBracketTopBorder);
  383. } else {
  384. relativeY = skyLineMinValue;
  385. }
  386. relativeY = Math.min(0, relativeY);
  387. graphicalLabel.PositionAndShape.RelativePosition = new PointF2D(relativeX, relativeY);
  388. skyBottomLineCalculator.updateSkyLineInRange(start, end, relativeY + graphicalLabel.PositionAndShape.BorderMarginTop);
  389. musicSystem.MeasureNumberLabels.push(graphicalLabel);
  390. }
  391. /**
  392. * Calculate the shape (Bézier curve) for this tie.
  393. * @param tie
  394. * @param tieIsAtSystemBreak
  395. */
  396. protected layoutGraphicalTie(tie: GraphicalTie, tieIsAtSystemBreak: boolean): void {
  397. throw new Error("abstract, not implemented");
  398. }
  399. /**
  400. * Calculate the Lyrics YPositions for a single [[StaffLine]].
  401. * @param staffLine
  402. * @param lyricVersesNumber
  403. */
  404. protected calculateSingleStaffLineLyricsPosition(staffLine: StaffLine, lyricVersesNumber: number[]): GraphicalStaffEntry[] {
  405. let numberOfVerses: number = 0;
  406. let lyricsStartYPosition: number = this.rules.StaffHeight; // Add offset to prevent collision
  407. const lyricsStaffEntriesList: GraphicalStaffEntry[] = [];
  408. const skyBottomLineCalculator: SkyBottomLineCalculator = staffLine.SkyBottomLineCalculator;
  409. // first find maximum Ycoordinate for the whole StaffLine
  410. let len: number = staffLine.Measures.length;
  411. for (let idx: number = 0; idx < len; ++idx) {
  412. const measure: GraphicalMeasure = staffLine.Measures[idx];
  413. const measureRelativePosition: PointF2D = measure.PositionAndShape.RelativePosition;
  414. const len2: number = measure.staffEntries.length;
  415. for (let idx2: number = 0; idx2 < len2; ++idx2) {
  416. const staffEntry: GraphicalStaffEntry = measure.staffEntries[idx2];
  417. if (staffEntry.LyricsEntries.length > 0) {
  418. lyricsStaffEntriesList.push(staffEntry);
  419. numberOfVerses = Math.max(numberOfVerses, staffEntry.LyricsEntries.length);
  420. // Position of Staffentry relative to StaffLine
  421. const staffEntryPositionX: number = staffEntry.PositionAndShape.RelativePosition.x +
  422. measureRelativePosition.x;
  423. let minMarginLeft: number = Number.MAX_VALUE;
  424. let maxMarginRight: number = Number.MAX_VALUE;
  425. // if more than one LyricEntry in StaffEntry, find minMarginLeft, maxMarginRight of all corresponding Labels
  426. for (let i: number = 0; i < staffEntry.LyricsEntries.length; i++) {
  427. const lyricsEntryLabel: GraphicalLabel = staffEntry.LyricsEntries[i].GraphicalLabel;
  428. minMarginLeft = Math.min(minMarginLeft, staffEntryPositionX + lyricsEntryLabel.PositionAndShape.BorderMarginLeft);
  429. maxMarginRight = Math.max(maxMarginRight, staffEntryPositionX + lyricsEntryLabel.PositionAndShape.BorderMarginRight);
  430. }
  431. // check BottomLine in this range and take the maximum between the two values
  432. const bottomLineMax: number = skyBottomLineCalculator.getBottomLineMaxInRange(minMarginLeft, maxMarginRight);
  433. lyricsStartYPosition = Math.max(lyricsStartYPosition, bottomLineMax);
  434. }
  435. }
  436. }
  437. let maxPosition: number = 0;
  438. // iterate again through the Staffentries with LyricEntries
  439. len = lyricsStaffEntriesList.length;
  440. for (const staffEntry of lyricsStaffEntriesList) {
  441. // set LyricEntryLabel RelativePosition
  442. for (let i: number = 0; i < staffEntry.LyricsEntries.length; i++) {
  443. const lyricEntry: GraphicalLyricEntry = staffEntry.LyricsEntries[i];
  444. const lyricsEntryLabel: GraphicalLabel = lyricEntry.GraphicalLabel;
  445. // read the verseNumber and get index of this number in the sorted LyricVerseNumbersList of Instrument
  446. // eg verseNumbers: 2,3,4,6 => 1,2,3,4
  447. const verseNumber: number = lyricEntry.GetLyricsEntry.VerseNumber;
  448. const sortedLyricVerseNumberIndex: number = lyricVersesNumber.indexOf(verseNumber);
  449. const firstPosition: number = lyricsStartYPosition + this.rules.LyricsHeight + this.rules.VerticalBetweenLyricsDistance;
  450. // Y-position calculated according to aforementioned mapping
  451. let position: number = firstPosition + (this.rules.VerticalBetweenLyricsDistance + this.rules.LyricsHeight) * sortedLyricVerseNumberIndex;
  452. if (this.leadSheet) {
  453. position = 3.4 + (this.rules.VerticalBetweenLyricsDistance + this.rules.LyricsHeight) * (sortedLyricVerseNumberIndex);
  454. }
  455. lyricsEntryLabel.PositionAndShape.RelativePosition = new PointF2D(0, position);
  456. maxPosition = Math.max(maxPosition, position);
  457. }
  458. }
  459. // update BottomLine (on the whole StaffLine's length)
  460. if (lyricsStaffEntriesList.length > 0) {
  461. const endX: number = staffLine.PositionAndShape.Size.width;
  462. const startX: number = lyricsStaffEntriesList[0].PositionAndShape.RelativePosition.x +
  463. lyricsStaffEntriesList[0].PositionAndShape.BorderMarginLeft +
  464. lyricsStaffEntriesList[0].parentMeasure.PositionAndShape.RelativePosition.x;
  465. skyBottomLineCalculator.updateBottomLineInRange(startX, endX, maxPosition);
  466. }
  467. return lyricsStaffEntriesList;
  468. }
  469. /**
  470. * calculates the dashes of lyric words and the extending underscore lines of syllables sung on more than one note.
  471. * @param lyricsStaffEntries
  472. */
  473. protected calculateLyricsExtendsAndDashes(lyricsStaffEntries: GraphicalStaffEntry[]): void {
  474. // iterate again to create now the extend lines and dashes for words
  475. for (let idx: number = 0, len: number = lyricsStaffEntries.length; idx < len; ++idx) {
  476. const staffEntry: GraphicalStaffEntry = lyricsStaffEntries[idx];
  477. // set LyricEntryLabel RelativePosition
  478. for (let i: number = 0; i < staffEntry.LyricsEntries.length; i++) {
  479. const lyricEntry: GraphicalLyricEntry = staffEntry.LyricsEntries[i];
  480. // calculate LyricWord's Dashes and underscoreLine
  481. if (lyricEntry.ParentLyricWord !== undefined &&
  482. lyricEntry.ParentLyricWord.GraphicalLyricsEntries[lyricEntry.ParentLyricWord.GraphicalLyricsEntries.length - 1] !== lyricEntry) {
  483. this.calculateSingleLyricWord(lyricEntry);
  484. }
  485. // calculate the underscore line extend if needed
  486. if (lyricEntry.GetLyricsEntry.extend) {
  487. this.calculateLyricExtend(lyricEntry);
  488. }
  489. }
  490. }
  491. }
  492. /**
  493. * Calculate a single OctaveShift for a [[MultiExpression]].
  494. * @param sourceMeasure
  495. * @param multiExpression
  496. * @param measureIndex
  497. * @param staffIndex
  498. */
  499. protected calculateSingleOctaveShift(sourceMeasure: SourceMeasure, multiExpression: MultiExpression,
  500. measureIndex: number, staffIndex: number): void {
  501. throw new Error("abstract, not implemented");
  502. }
  503. /**
  504. * Calculate all the textual [[RepetitionInstruction]]s (e.g. dal segno) for a single [[SourceMeasure]].
  505. * @param repetitionInstruction
  506. * @param measureIndex
  507. */
  508. protected calculateWordRepetitionInstruction(repetitionInstruction: RepetitionInstruction,
  509. measureIndex: number): void {
  510. throw new Error("abstract, not implemented");
  511. }
  512. /**
  513. * Calculate all the Mood and Unknown Expressions for a single [[MultiExpression]].
  514. * @param multiExpression
  515. * @param measureIndex
  516. * @param staffIndex
  517. */
  518. protected calculateMoodAndUnknownExpression(multiExpression: MultiExpression, measureIndex: number, staffIndex: number): void {
  519. throw new Error("abstract, not implemented");
  520. }
  521. /**
  522. * Delete all Objects that must be recalculated.
  523. * If graphicalMusicSheet.reCalculate has been called, then this method will be called to reset or remove all flexible
  524. * graphical music symbols (e.g. Ornaments, Lyrics, Slurs) graphicalMusicSheet will have MusicPages, they will have MusicSystems etc...
  525. */
  526. protected clearRecreatedObjects(): void {
  527. // Clear StaffEntries with GraphicalTies
  528. for (let idx: number = 0, len: number = this.staffEntriesWithGraphicalTies.length; idx < len; ++idx) {
  529. const staffEntriesWithGraphicalTie: GraphicalStaffEntry = this.staffEntriesWithGraphicalTies[idx];
  530. staffEntriesWithGraphicalTie.GraphicalTies.length = 0;
  531. }
  532. this.staffEntriesWithGraphicalTies.length = 0;
  533. return;
  534. }
  535. /**
  536. * This method handles a [[StaffEntryLink]].
  537. * @param graphicalStaffEntry
  538. * @param staffEntryLinks
  539. */
  540. protected handleStaffEntryLink(graphicalStaffEntry: GraphicalStaffEntry,
  541. staffEntryLinks: StaffEntryLink[]): void {
  542. log.debug("handleStaffEntryLink not implemented");
  543. }
  544. /**
  545. * Store the newly computed [[Measure]]s in newly created [[MusicSystem]]s.
  546. */
  547. protected calculateMusicSystems(): void {
  548. if (this.graphicalMusicSheet.MeasureList === undefined) {
  549. return;
  550. }
  551. const allMeasures: GraphicalMeasure[][] = this.graphicalMusicSheet.MeasureList;
  552. if (allMeasures === undefined) {
  553. return;
  554. }
  555. // visible 2D-MeasureList
  556. const visibleMeasureList: GraphicalMeasure[][] = [];
  557. for (let idx: number = 0, len: number = allMeasures.length; idx < len; ++idx) {
  558. const graphicalMeasures: GraphicalMeasure[] = allMeasures[idx];
  559. const visiblegraphicalMeasures: GraphicalMeasure[] = [];
  560. for (let idx2: number = 0, len2: number = graphicalMeasures.length; idx2 < len2; ++idx2) {
  561. const graphicalMeasure: GraphicalMeasure = allMeasures[idx][idx2];
  562. if (graphicalMeasure.isVisible()) {
  563. visiblegraphicalMeasures.push(graphicalMeasure);
  564. }
  565. }
  566. visibleMeasureList.push(visiblegraphicalMeasures);
  567. }
  568. // find out how many StaffLine Instances we need
  569. let numberOfStaffLines: number = 0;
  570. for (let idx: number = 0, len: number = visibleMeasureList.length; idx < len; ++idx) {
  571. const gmlist: GraphicalMeasure[] = visibleMeasureList[idx];
  572. numberOfStaffLines = Math.max(gmlist.length, numberOfStaffLines);
  573. break;
  574. }
  575. if (numberOfStaffLines === 0) {
  576. return;
  577. }
  578. // build the MusicSystems
  579. const musicSystemBuilder: MusicSystemBuilder = new MusicSystemBuilder();
  580. musicSystemBuilder.initialize(this.graphicalMusicSheet, visibleMeasureList, numberOfStaffLines);
  581. musicSystemBuilder.buildMusicSystems();
  582. // check for Measures with only WholeRestNotes and correct their X-Position (middle of Measure)
  583. this.checkMeasuresForWholeRestNotes();
  584. if (!this.leadSheet) {
  585. // calculate Beam Placement
  586. this.calculateBeams();
  587. // possible Displacement of RestNotes
  588. this.optimizeRestPlacement();
  589. // possible Displacement of RestNotes
  590. this.calculateStaffEntryArticulationMarks();
  591. // calculate Ties
  592. this.calculateTieCurves();
  593. }
  594. // calculate Sky- and BottomLine
  595. // will have reasonable values only between ObjectsBorders (eg StaffEntries)
  596. this.calculateSkyBottomLines();
  597. // calculate TupletsNumbers
  598. this.calculateTupletNumbers();
  599. // calculate MeasureNumbers
  600. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  601. const graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  602. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  603. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  604. this.calculateMeasureNumberPlacement(musicSystem);
  605. }
  606. }
  607. // calculate Slurs
  608. if (!this.leadSheet) {
  609. this.calculateSlurs();
  610. }
  611. // calculate StaffEntry Ornaments
  612. // (must come after Slurs)
  613. if (!this.leadSheet) {
  614. this.calculateOrnaments();
  615. }
  616. // calculate StaffEntry ChordSymbols
  617. this.calculateChordSymbols();
  618. if (!this.leadSheet) {
  619. // calculate all Instantanious/Continuous Dynamics Expressions
  620. this.calculateDynamicExpressions();
  621. // place neighbouring DynamicExpressions at the same height
  622. this.optimizeStaffLineDynamicExpressionsPositions();
  623. // calculate all Mood and Unknown Expression
  624. this.calculateMoodAndUnknownExpressions();
  625. // calculate all OctaveShifts
  626. this.calculateOctaveShifts();
  627. // calucalte RepetitionInstructions (Dal Segno, Coda, etc)
  628. this.calculateWordRepetitionInstructions();
  629. }
  630. // calculate endings last, so they appear above measure numbers
  631. this.calculateRepetitionEndings();
  632. // calcualte all Tempo Expressions
  633. if (!this.leadSheet) {
  634. this.calculateTempoExpressions();
  635. }
  636. this.formatMeasures();
  637. // calculate all LyricWords Positions
  638. this.calculateLyricsPosition();
  639. // update all StaffLine's Borders
  640. // create temporary Object, just to call the methods (in order to avoid declaring them static)
  641. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  642. const graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  643. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  644. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  645. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  646. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  647. this.updateStaffLineBorders(staffLine);
  648. }
  649. }
  650. }
  651. // Y-spacing
  652. this.calculateSystemYLayout();
  653. // calculate Comments for each Staffline
  654. this.calculateComments();
  655. // calculate marked Areas for Systems
  656. this.calculateMarkedAreas();
  657. // the following must be done after Y-spacing, when the MusicSystems's final Dimensions are set
  658. // set the final yPositions of Objects such as SystemLabels and SystemLinesContainers,
  659. // create all System Lines, Brackets and MeasureNumbers (for all systems and for all pages)
  660. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  661. const graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  662. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  663. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  664. musicSystem.setMusicSystemLabelsYPosition();
  665. if (!this.leadSheet) {
  666. musicSystem.setYPositionsToVerticalLineObjectsAndCreateLines(this.rules);
  667. musicSystem.createSystemLeftLine(this.rules.SystemThinLineWidth, this.rules.SystemLabelsRightMargin);
  668. musicSystem.createInstrumentBrackets(this.graphicalMusicSheet.ParentMusicSheet.Instruments, this.rules.StaffHeight);
  669. musicSystem.createGroupBrackets(this.graphicalMusicSheet.ParentMusicSheet.InstrumentalGroups, this.rules.StaffHeight, 0);
  670. musicSystem.alignBeginInstructions();
  671. } else if (musicSystem === musicSystem.Parent.MusicSystems[0]) {
  672. musicSystem.createSystemLeftLine(this.rules.SystemThinLineWidth, this.rules.SystemLabelsRightMargin);
  673. }
  674. musicSystem.calculateBorders(this.rules);
  675. }
  676. const distance: number = graphicalMusicPage.MusicSystems[0].PositionAndShape.BorderTop;
  677. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  678. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  679. // let newPosition: PointF2D = new PointF2D(musicSystem.PositionAndShape.RelativePosition.x,
  680. // musicSystem.PositionAndShape.RelativePosition.y - distance);
  681. musicSystem.PositionAndShape.RelativePosition =
  682. new PointF2D(musicSystem.PositionAndShape.RelativePosition.x, musicSystem.PositionAndShape.RelativePosition.y - distance);
  683. }
  684. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  685. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  686. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  687. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  688. staffLine.addActivitySymbolClickArea();
  689. }
  690. }
  691. // calculate all Labels's Positions for the first Page
  692. if (graphicalMusicPage === this.graphicalMusicSheet.MusicPages[0]) {
  693. this.calculatePageLabels(graphicalMusicPage);
  694. }
  695. // calculate TopBottom Borders for all elements recursively
  696. graphicalMusicPage.PositionAndShape.calculateTopBottomBorders();
  697. }
  698. }
  699. protected calculateMarkedAreas(): void {
  700. //log.debug("calculateMarkedAreas not implemented");
  701. return;
  702. }
  703. protected calculateComments(): void {
  704. //log.debug("calculateComments not implemented");
  705. return;
  706. }
  707. /**
  708. * Iterate through all the [[StaffLine]]s in order to check for possible optimizations in the placement of the [[GraphicalExpression]]s.
  709. */
  710. protected optimizeStaffLineDynamicExpressionsPositions(): void {
  711. return;
  712. }
  713. protected calculateChordSymbols(): void {
  714. return;
  715. }
  716. /**
  717. * Do layout on staff measures which only consist of a full rest.
  718. * @param rest
  719. * @param gse
  720. * @param measure
  721. */
  722. protected layoutMeasureWithWholeRest(rest: GraphicalNote, gse: GraphicalStaffEntry,
  723. measure: GraphicalMeasure): void {
  724. return;
  725. }
  726. protected layoutBeams(staffEntry: GraphicalStaffEntry): void {
  727. return;
  728. }
  729. protected layoutArticulationMarks(articulations: ArticulationEnum[], voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry): void {
  730. return;
  731. }
  732. protected layoutOrnament(ornaments: OrnamentContainer, voiceEntry: VoiceEntry,
  733. graphicalStaffEntry: GraphicalStaffEntry): void {
  734. return;
  735. }
  736. protected calculateRestNotePlacementWithinGraphicalBeam(graphicalStaffEntry: GraphicalStaffEntry,
  737. restNote: GraphicalNote,
  738. previousNote: GraphicalNote,
  739. nextStaffEntry: GraphicalStaffEntry,
  740. nextNote: GraphicalNote): void {
  741. return;
  742. }
  743. protected calculateTupletNumbers(): void {
  744. return;
  745. }
  746. protected calculateSlurs(): void {
  747. return;
  748. }
  749. protected calculateDynamicExpressionsForSingleMultiExpression(multiExpression: MultiExpression, measureIndex: number, staffIndex: number): void {
  750. return;
  751. }
  752. protected calcGraphicalRepetitionEndingsRecursively(repetition: Repetition): void {
  753. return;
  754. }
  755. /**
  756. * Calculate a single GraphicalRepetition.
  757. * @param start
  758. * @param end
  759. * @param numberText
  760. * @param offset
  761. * @param leftOpen
  762. * @param rightOpen
  763. */
  764. protected layoutSingleRepetitionEnding(start: GraphicalMeasure, end: GraphicalMeasure, numberText: string,
  765. offset: number, leftOpen: boolean, rightOpen: boolean): void {
  766. return;
  767. }
  768. protected calculateTempoExpressionsForSingleMultiTempoExpression(sourceMeasure: SourceMeasure, multiTempoExpression: MultiTempoExpression,
  769. measureIndex: number): void {
  770. return;
  771. }
  772. protected graphicalMeasureCreatedCalculations(measure: GraphicalMeasure): void {
  773. return;
  774. }
  775. protected clearSystemsAndMeasures(): void {
  776. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  777. const graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  778. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  779. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  780. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  781. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  782. for (let idx4: number = 0, len4: number = staffLine.Measures.length; idx4 < len4; ++idx4) {
  783. const graphicalMeasure: GraphicalMeasure = staffLine.Measures[idx4];
  784. if (graphicalMeasure.FirstInstructionStaffEntry !== undefined) {
  785. const index: number = graphicalMeasure.PositionAndShape.ChildElements.indexOf(
  786. graphicalMeasure.FirstInstructionStaffEntry.PositionAndShape
  787. );
  788. if (index > -1) {
  789. graphicalMeasure.PositionAndShape.ChildElements.splice(index, 1);
  790. }
  791. graphicalMeasure.FirstInstructionStaffEntry = undefined;
  792. graphicalMeasure.beginInstructionsWidth = 0.0;
  793. }
  794. if (graphicalMeasure.LastInstructionStaffEntry !== undefined) {
  795. const index: number = graphicalMeasure.PositionAndShape.ChildElements.indexOf(
  796. graphicalMeasure.LastInstructionStaffEntry.PositionAndShape
  797. );
  798. if (index > -1) {
  799. graphicalMeasure.PositionAndShape.ChildElements.splice(index, 1);
  800. }
  801. graphicalMeasure.LastInstructionStaffEntry = undefined;
  802. graphicalMeasure.endInstructionsWidth = 0.0;
  803. }
  804. }
  805. staffLine.Measures = [];
  806. staffLine.PositionAndShape.ChildElements = [];
  807. }
  808. musicSystem.StaffLines.length = 0;
  809. musicSystem.PositionAndShape.ChildElements = [];
  810. }
  811. graphicalMusicPage.MusicSystems = [];
  812. graphicalMusicPage.PositionAndShape.ChildElements = [];
  813. }
  814. this.graphicalMusicSheet.MusicPages = [];
  815. }
  816. protected handleVoiceEntry(voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry,
  817. accidentalCalculator: AccidentalCalculator, openLyricWords: LyricWord[],
  818. activeClef: ClefInstruction,
  819. openTuplets: Tuplet[], openBeams: Beam[],
  820. octaveShiftValue: OctaveEnum, linkedNotes: Note[] = undefined,
  821. sourceStaffEntry: SourceStaffEntry = undefined): OctaveEnum {
  822. this.calculateStemDirectionFromVoices(voiceEntry);
  823. const gve: GraphicalVoiceEntry = graphicalStaffEntry.findOrCreateGraphicalVoiceEntry(voiceEntry);
  824. for (let idx: number = 0, len: number = voiceEntry.Notes.length; idx < len; ++idx) {
  825. const note: Note = voiceEntry.Notes[idx];
  826. if (sourceStaffEntry !== undefined && sourceStaffEntry.Link !== undefined && linkedNotes !== undefined && linkedNotes.indexOf(note) > -1) {
  827. continue;
  828. }
  829. let graphicalNote: GraphicalNote;
  830. if (voiceEntry.IsGrace) {
  831. graphicalNote = MusicSheetCalculator.symbolFactory.createGraceNote(note, gve, activeClef, octaveShiftValue);
  832. } else {
  833. graphicalNote = MusicSheetCalculator.symbolFactory.createNote(note, gve, activeClef, octaveShiftValue, undefined);
  834. }
  835. if (note.Pitch !== undefined) {
  836. this.checkNoteForAccidental(graphicalNote, accidentalCalculator, activeClef, octaveShiftValue);
  837. }
  838. this.resetYPositionForLeadSheet(graphicalNote.PositionAndShape);
  839. graphicalStaffEntry.addGraphicalNoteToListAtCorrectYPosition(gve, graphicalNote);
  840. graphicalNote.PositionAndShape.calculateBoundingBox();
  841. if (!this.leadSheet) {
  842. if (note.NoteBeam !== undefined) {
  843. this.handleBeam(graphicalNote, note.NoteBeam, openBeams);
  844. }
  845. if (note.NoteTuplet !== undefined) {
  846. this.handleTuplet(graphicalNote, note.NoteTuplet, openTuplets);
  847. }
  848. }
  849. }
  850. if (voiceEntry.Articulations.length > 0) {
  851. this.handleVoiceEntryArticulations(voiceEntry.Articulations, voiceEntry, graphicalStaffEntry);
  852. }
  853. if (voiceEntry.TechnicalInstructions.length > 0) {
  854. this.handleVoiceEntryTechnicalInstructions(voiceEntry.TechnicalInstructions, voiceEntry, graphicalStaffEntry);
  855. }
  856. if (voiceEntry.LyricsEntries.size() > 0) {
  857. this.handleVoiceEntryLyrics(voiceEntry, graphicalStaffEntry, openLyricWords);
  858. }
  859. if (voiceEntry.OrnamentContainer !== undefined) {
  860. this.handleVoiceEntryOrnaments(voiceEntry.OrnamentContainer, voiceEntry, graphicalStaffEntry);
  861. }
  862. return octaveShiftValue;
  863. }
  864. protected resetYPositionForLeadSheet(psi: BoundingBox): void {
  865. if (this.leadSheet) {
  866. psi.RelativePosition = new PointF2D(psi.RelativePosition.x, 0.0);
  867. }
  868. }
  869. protected layoutVoiceEntries(graphicalStaffEntry: GraphicalStaffEntry): void {
  870. graphicalStaffEntry.PositionAndShape.RelativePosition = new PointF2D(0.0, 0.0);
  871. if (!this.leadSheet) {
  872. for (const gve of graphicalStaffEntry.graphicalVoiceEntries) {
  873. const graphicalNotes: GraphicalNote[] = gve.notes;
  874. if (graphicalNotes.length === 0) {
  875. continue;
  876. }
  877. const voiceEntry: VoiceEntry = graphicalNotes[0].sourceNote.ParentVoiceEntry;
  878. const hasPitchedNote: boolean = graphicalNotes[0].sourceNote.Pitch !== undefined;
  879. this.layoutVoiceEntry(voiceEntry, graphicalNotes, graphicalStaffEntry, hasPitchedNote);
  880. }
  881. }
  882. }
  883. protected maxInstrNameLabelLength(): number {
  884. let maxLabelLength: number = 0.0;
  885. for (const instrument of this.graphicalMusicSheet.ParentMusicSheet.Instruments) {
  886. if (instrument.Voices.length > 0 && instrument.Voices[0].Visible) {
  887. const graphicalLabel: GraphicalLabel = new GraphicalLabel(instrument.NameLabel, this.rules.InstrumentLabelTextHeight, TextAlignment.LeftCenter);
  888. graphicalLabel.setLabelPositionAndShapeBorders();
  889. maxLabelLength = Math.max(maxLabelLength, graphicalLabel.PositionAndShape.MarginSize.width);
  890. }
  891. }
  892. return maxLabelLength;
  893. }
  894. protected calculateSheetLabelBoundingBoxes(): void {
  895. const musicSheet: MusicSheet = this.graphicalMusicSheet.ParentMusicSheet;
  896. if (musicSheet.Title !== undefined) {
  897. const title: GraphicalLabel = new GraphicalLabel(musicSheet.Title, this.rules.SheetTitleHeight, TextAlignment.CenterBottom);
  898. this.graphicalMusicSheet.Title = title;
  899. title.setLabelPositionAndShapeBorders();
  900. }
  901. if (musicSheet.Subtitle !== undefined) {
  902. const subtitle: GraphicalLabel = new GraphicalLabel(musicSheet.Subtitle, this.rules.SheetSubtitleHeight, TextAlignment.CenterCenter);
  903. this.graphicalMusicSheet.Subtitle = subtitle;
  904. subtitle.setLabelPositionAndShapeBorders();
  905. }
  906. if (musicSheet.Composer !== undefined) {
  907. const composer: GraphicalLabel = new GraphicalLabel(musicSheet.Composer, this.rules.SheetComposerHeight, TextAlignment.RightCenter);
  908. this.graphicalMusicSheet.Composer = composer;
  909. composer.setLabelPositionAndShapeBorders();
  910. }
  911. if (musicSheet.Lyricist !== undefined) {
  912. const lyricist: GraphicalLabel = new GraphicalLabel(musicSheet.Lyricist, this.rules.SheetAuthorHeight, TextAlignment.LeftCenter);
  913. this.graphicalMusicSheet.Lyricist = lyricist;
  914. lyricist.setLabelPositionAndShapeBorders();
  915. }
  916. }
  917. protected checkMeasuresForWholeRestNotes(): void {
  918. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  919. const musicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  920. for (let idx2: number = 0, len2: number = musicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  921. const musicSystem: MusicSystem = musicPage.MusicSystems[idx2];
  922. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  923. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  924. for (let idx4: number = 0, len4: number = staffLine.Measures.length; idx4 < len4; ++idx4) {
  925. const measure: GraphicalMeasure = staffLine.Measures[idx4];
  926. if (measure.staffEntries.length === 1) {
  927. const gse: GraphicalStaffEntry = measure.staffEntries[0];
  928. if (gse.graphicalVoiceEntries.length > 0 && gse.graphicalVoiceEntries[0].notes.length === 1) {
  929. const graphicalNote: GraphicalNote = gse.graphicalVoiceEntries[0].notes[0];
  930. if (graphicalNote.sourceNote.Pitch === undefined && (new Fraction(1, 2)).lt(graphicalNote.sourceNote.Length)) {
  931. this.layoutMeasureWithWholeRest(graphicalNote, gse, measure);
  932. }
  933. }
  934. }
  935. }
  936. }
  937. }
  938. }
  939. }
  940. protected optimizeRestNotePlacement(graphicalStaffEntry: GraphicalStaffEntry, measure: GraphicalMeasure): void {
  941. if (graphicalStaffEntry.graphicalVoiceEntries.length === 0) {
  942. return;
  943. }
  944. const voice1Notes: GraphicalNote[] = graphicalStaffEntry.graphicalVoiceEntries[0].notes;
  945. if (voice1Notes.length === 0) {
  946. return;
  947. }
  948. const voice1Note1: GraphicalNote = voice1Notes[0];
  949. const voice1Note1IsRest: boolean = voice1Note1.sourceNote.Pitch === undefined;
  950. if (graphicalStaffEntry.graphicalVoiceEntries.length === 2) {
  951. let voice2Note1IsRest: boolean = false;
  952. const voice2Notes: GraphicalNote[] = graphicalStaffEntry.graphicalVoiceEntries[1].notes;
  953. if (voice2Notes.length > 0) {
  954. const voice2Note1: GraphicalNote = voice2Notes[0];
  955. voice2Note1IsRest = voice2Note1.sourceNote.Pitch === undefined;
  956. }
  957. if (voice1Note1IsRest && voice2Note1IsRest) {
  958. this.calculateTwoRestNotesPlacementWithCollisionDetection(graphicalStaffEntry);
  959. } else if (voice1Note1IsRest || voice2Note1IsRest) {
  960. this.calculateRestNotePlacementWithCollisionDetectionFromGraphicalNote(graphicalStaffEntry);
  961. }
  962. } else if (voice1Note1IsRest && graphicalStaffEntry !== measure.staffEntries[0] &&
  963. graphicalStaffEntry !== measure.staffEntries[measure.staffEntries.length - 1]) {
  964. const staffEntryIndex: number = measure.staffEntries.indexOf(graphicalStaffEntry);
  965. const previousStaffEntry: GraphicalStaffEntry = measure.staffEntries[staffEntryIndex - 1];
  966. const nextStaffEntry: GraphicalStaffEntry = measure.staffEntries[staffEntryIndex + 1];
  967. if (previousStaffEntry.graphicalVoiceEntries.length === 1) {
  968. const previousNote: GraphicalNote = previousStaffEntry.graphicalVoiceEntries[0].notes[0];
  969. if (previousNote.sourceNote.NoteBeam !== undefined && nextStaffEntry.graphicalVoiceEntries.length === 1) {
  970. const nextNote: GraphicalNote = nextStaffEntry.graphicalVoiceEntries[0].notes[0];
  971. if (nextNote.sourceNote.NoteBeam !== undefined && previousNote.sourceNote.NoteBeam === nextNote.sourceNote.NoteBeam) {
  972. this.calculateRestNotePlacementWithinGraphicalBeam(
  973. graphicalStaffEntry, voice1Note1, previousNote,
  974. nextStaffEntry, nextNote
  975. );
  976. graphicalStaffEntry.PositionAndShape.calculateBoundingBox();
  977. }
  978. }
  979. }
  980. }
  981. }
  982. protected getRelativePositionInStaffLineFromTimestamp(timestamp: Fraction, verticalIndex: number, staffLine: StaffLine,
  983. multiStaffInstrument: boolean, firstVisibleMeasureRelativeX: number = 0.0): PointF2D {
  984. let relative: PointF2D = new PointF2D();
  985. let leftStaffEntry: GraphicalStaffEntry = undefined;
  986. let rightStaffEntry: GraphicalStaffEntry = undefined;
  987. const numEntries: number = this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length;
  988. const index: number = this.graphicalMusicSheet.GetInterpolatedIndexInVerticalContainers(timestamp);
  989. const leftIndex: number = Math.min(Math.floor(index), numEntries - 1);
  990. const rightIndex: number = Math.min(Math.ceil(index), numEntries - 1);
  991. if (leftIndex < 0 || verticalIndex < 0) {
  992. return relative;
  993. }
  994. leftStaffEntry = this.getFirstLeftNotNullStaffEntryFromContainer(leftIndex, verticalIndex, multiStaffInstrument);
  995. rightStaffEntry = this.getFirstRightNotNullStaffEntryFromContainer(rightIndex, verticalIndex, multiStaffInstrument);
  996. if (leftStaffEntry !== undefined && rightStaffEntry !== undefined) {
  997. let measureRelativeX: number = leftStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x;
  998. if (firstVisibleMeasureRelativeX > 0) {
  999. measureRelativeX = firstVisibleMeasureRelativeX;
  1000. }
  1001. let leftX: number = leftStaffEntry.PositionAndShape.RelativePosition.x + measureRelativeX;
  1002. let rightX: number = rightStaffEntry.PositionAndShape.RelativePosition.x + rightStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x;
  1003. if (firstVisibleMeasureRelativeX > 0) {
  1004. rightX = rightStaffEntry.PositionAndShape.RelativePosition.x + measureRelativeX;
  1005. }
  1006. let timestampQuotient: number = 0.0;
  1007. if (leftStaffEntry !== rightStaffEntry) {
  1008. const leftTimestamp: Fraction = leftStaffEntry.getAbsoluteTimestamp();
  1009. const rightTimestamp: Fraction = rightStaffEntry.getAbsoluteTimestamp();
  1010. const leftDifference: Fraction = Fraction.minus(timestamp, leftTimestamp);
  1011. timestampQuotient = leftDifference.RealValue / Fraction.minus(rightTimestamp, leftTimestamp).RealValue;
  1012. }
  1013. if (leftStaffEntry.parentMeasure.ParentStaffLine !== rightStaffEntry.parentMeasure.ParentStaffLine) {
  1014. if (leftStaffEntry.parentMeasure.ParentStaffLine === staffLine) {
  1015. rightX = staffLine.PositionAndShape.Size.width;
  1016. } else {
  1017. leftX = staffLine.PositionAndShape.RelativePosition.x;
  1018. }
  1019. }
  1020. relative = new PointF2D(leftX + (rightX - leftX) * timestampQuotient, 0.0);
  1021. }
  1022. return relative;
  1023. }
  1024. protected getRelativeXPositionFromTimestamp(timestamp: Fraction): number {
  1025. const numEntries: number = this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length;
  1026. const index: number = this.graphicalMusicSheet.GetInterpolatedIndexInVerticalContainers(timestamp);
  1027. const discreteIndex: number = Math.max(0, Math.min(Math.round(index), numEntries - 1));
  1028. const gse: GraphicalStaffEntry = this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[discreteIndex].getFirstNonNullStaffEntry();
  1029. const posX: number = gse.PositionAndShape.RelativePosition.x + gse.parentMeasure.PositionAndShape.RelativePosition.x;
  1030. return posX;
  1031. }
  1032. protected calculatePageLabels(page: GraphicalMusicPage): void {
  1033. // The PositionAndShape child elements of page need to be manually connected to the lyricist, composer, subtitle, etc.
  1034. // because the page is only available now
  1035. let firstSystemAbsoluteTopMargin: number = 10;
  1036. if (page.MusicSystems.length > 0) {
  1037. const firstMusicSystem: MusicSystem = page.MusicSystems[0];
  1038. firstSystemAbsoluteTopMargin = firstMusicSystem.PositionAndShape.RelativePosition.y + firstMusicSystem.PositionAndShape.BorderTop;
  1039. }
  1040. if (this.graphicalMusicSheet.Title !== undefined) {
  1041. const title: GraphicalLabel = this.graphicalMusicSheet.Title;
  1042. title.PositionAndShape.Parent = page.PositionAndShape;
  1043. const relative: PointF2D = new PointF2D();
  1044. relative.x = this.graphicalMusicSheet.ParentMusicSheet.pageWidth / 2;
  1045. relative.y = this.rules.TitleTopDistance + this.rules.SheetTitleHeight;
  1046. title.PositionAndShape.RelativePosition = relative;
  1047. page.Labels.push(title);
  1048. }
  1049. if (this.graphicalMusicSheet.Subtitle !== undefined) {
  1050. const subtitle: GraphicalLabel = this.graphicalMusicSheet.Subtitle;
  1051. subtitle.PositionAndShape.Parent = page.PositionAndShape;
  1052. const relative: PointF2D = new PointF2D();
  1053. relative.x = this.graphicalMusicSheet.ParentMusicSheet.pageWidth / 2;
  1054. relative.y = this.rules.TitleTopDistance + this.rules.SheetTitleHeight + this.rules.SheetMinimumDistanceBetweenTitleAndSubtitle;
  1055. subtitle.PositionAndShape.RelativePosition = relative;
  1056. page.Labels.push(subtitle);
  1057. }
  1058. if (this.graphicalMusicSheet.Composer !== undefined) {
  1059. const composer: GraphicalLabel = this.graphicalMusicSheet.Composer;
  1060. composer.PositionAndShape.Parent = page.PositionAndShape;
  1061. composer.setLabelPositionAndShapeBorders();
  1062. const relative: PointF2D = new PointF2D();
  1063. relative.x = this.graphicalMusicSheet.ParentMusicSheet.pageWidth - this.rules.PageRightMargin;
  1064. relative.y = firstSystemAbsoluteTopMargin - this.rules.SystemComposerDistance;
  1065. composer.PositionAndShape.RelativePosition = relative;
  1066. page.Labels.push(composer);
  1067. }
  1068. if (this.graphicalMusicSheet.Lyricist !== undefined) {
  1069. const lyricist: GraphicalLabel = this.graphicalMusicSheet.Lyricist;
  1070. lyricist.PositionAndShape.Parent = page.PositionAndShape;
  1071. lyricist.setLabelPositionAndShapeBorders();
  1072. const relative: PointF2D = new PointF2D();
  1073. relative.x = this.rules.PageLeftMargin;
  1074. relative.y = firstSystemAbsoluteTopMargin - this.rules.SystemComposerDistance;
  1075. lyricist.PositionAndShape.RelativePosition = relative;
  1076. page.Labels.push(lyricist);
  1077. }
  1078. }
  1079. protected createGraphicalTies(): void {
  1080. for (let measureIndex: number = 0; measureIndex < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; measureIndex++) {
  1081. const sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[measureIndex];
  1082. for (let staffIndex: number = 0; staffIndex < sourceMeasure.CompleteNumberOfStaves; staffIndex++) {
  1083. for (let j: number = 0; j < sourceMeasure.VerticalSourceStaffEntryContainers.length; j++) {
  1084. const sourceStaffEntry: SourceStaffEntry = sourceMeasure.VerticalSourceStaffEntryContainers[j].StaffEntries[staffIndex];
  1085. if (sourceStaffEntry !== undefined) {
  1086. const startStaffEntry: GraphicalStaffEntry = this.graphicalMusicSheet.findGraphicalStaffEntryFromMeasureList(
  1087. staffIndex, measureIndex, sourceStaffEntry
  1088. );
  1089. for (let idx: number = 0, len: number = sourceStaffEntry.VoiceEntries.length; idx < len; ++idx) {
  1090. const voiceEntry: VoiceEntry = sourceStaffEntry.VoiceEntries[idx];
  1091. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  1092. const note: Note = voiceEntry.Notes[idx2];
  1093. if (note.NoteTie !== undefined) {
  1094. const tie: Tie = note.NoteTie;
  1095. this.handleTie(tie, startStaffEntry, staffIndex, measureIndex);
  1096. }
  1097. }
  1098. }
  1099. }
  1100. }
  1101. }
  1102. }
  1103. }
  1104. private handleTie(tie: Tie, startGraphicalStaffEntry: GraphicalStaffEntry, staffIndex: number, measureIndex: number): void {
  1105. let startGse: GraphicalStaffEntry = startGraphicalStaffEntry;
  1106. let startNote: GraphicalNote = startGse.findEndTieGraphicalNoteFromNote(tie.StartNote);
  1107. let endGse: GraphicalStaffEntry = undefined;
  1108. let endNote: GraphicalNote = undefined;
  1109. for (let i: number = 1; i < tie.Notes.length; i++) {
  1110. startNote = startGse.findEndTieGraphicalNoteFromNote(tie.Notes[i - 1]);
  1111. endGse = this.graphicalMusicSheet.GetGraphicalFromSourceStaffEntry(tie.Notes[i].ParentStaffEntry);
  1112. endNote = endGse.findEndTieGraphicalNoteFromNote(tie.Notes[i]);
  1113. if (startNote !== undefined && endNote !== undefined && endGse !== undefined) {
  1114. const graphicalTie: GraphicalTie = this.createGraphicalTie(tie, startGse, endGse, startNote, endNote);
  1115. startGse.GraphicalTies.push(graphicalTie);
  1116. if (this.staffEntriesWithGraphicalTies.indexOf(startGse) >= 0) {
  1117. this.staffEntriesWithGraphicalTies.push(startGse);
  1118. }
  1119. }
  1120. startGse = endGse;
  1121. }
  1122. }
  1123. private createAccidentalCalculators(): AccidentalCalculator[] {
  1124. const accidentalCalculators: AccidentalCalculator[] = [];
  1125. const firstSourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.getFirstSourceMeasure();
  1126. if (firstSourceMeasure !== undefined) {
  1127. for (let i: number = 0; i < firstSourceMeasure.CompleteNumberOfStaves; i++) {
  1128. const accidentalCalculator: AccidentalCalculator = new AccidentalCalculator();
  1129. accidentalCalculators.push(accidentalCalculator);
  1130. if (firstSourceMeasure.FirstInstructionsStaffEntries[i] !== undefined) {
  1131. for (let idx: number = 0, len: number = firstSourceMeasure.FirstInstructionsStaffEntries[i].Instructions.length; idx < len; ++idx) {
  1132. const abstractNotationInstruction: AbstractNotationInstruction = firstSourceMeasure.FirstInstructionsStaffEntries[i].Instructions[idx];
  1133. if (abstractNotationInstruction instanceof KeyInstruction) {
  1134. const keyInstruction: KeyInstruction = <KeyInstruction>abstractNotationInstruction;
  1135. accidentalCalculator.ActiveKeyInstruction = keyInstruction;
  1136. }
  1137. }
  1138. }
  1139. }
  1140. }
  1141. return accidentalCalculators;
  1142. }
  1143. private calculateVerticalContainersList(): void {
  1144. const numberOfEntries: number = this.graphicalMusicSheet.MeasureList[0].length;
  1145. for (let i: number = 0; i < this.graphicalMusicSheet.MeasureList.length; i++) {
  1146. for (let j: number = 0; j < numberOfEntries; j++) {
  1147. const measure: GraphicalMeasure = this.graphicalMusicSheet.MeasureList[i][j];
  1148. for (let idx: number = 0, len: number = measure.staffEntries.length; idx < len; ++idx) {
  1149. const graphicalStaffEntry: GraphicalStaffEntry = measure.staffEntries[idx];
  1150. const verticalContainer: VerticalGraphicalStaffEntryContainer =
  1151. this.graphicalMusicSheet.getOrCreateVerticalContainer(graphicalStaffEntry.getAbsoluteTimestamp());
  1152. if (verticalContainer !== undefined) {
  1153. verticalContainer.StaffEntries[j] = graphicalStaffEntry;
  1154. graphicalStaffEntry.parentVerticalContainer = verticalContainer;
  1155. }
  1156. }
  1157. }
  1158. }
  1159. }
  1160. private setIndecesToVerticalGraphicalContainers(): void {
  1161. for (let i: number = 0; i < this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length; i++) {
  1162. this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].Index = i;
  1163. }
  1164. }
  1165. private createGraphicalMeasuresForSourceMeasure(sourceMeasure: SourceMeasure, accidentalCalculators: AccidentalCalculator[],
  1166. openLyricWords: LyricWord[],
  1167. openOctaveShifts: OctaveShiftParams[], activeClefs: ClefInstruction[]): GraphicalMeasure[] {
  1168. this.initGraphicalMeasuresCreation();
  1169. const verticalMeasureList: GraphicalMeasure[] = [];
  1170. const openBeams: Beam[] = [];
  1171. const openTuplets: Tuplet[] = [];
  1172. const staffEntryLinks: StaffEntryLink[] = [];
  1173. for (let staffIndex: number = 0; staffIndex < sourceMeasure.CompleteNumberOfStaves; staffIndex++) {
  1174. const measure: GraphicalMeasure = this.createGraphicalMeasure(
  1175. sourceMeasure, openTuplets, openBeams,
  1176. accidentalCalculators[staffIndex], activeClefs, openOctaveShifts, openLyricWords, staffIndex, staffEntryLinks
  1177. );
  1178. this.graphicalMeasureCreatedCalculations(measure);
  1179. verticalMeasureList.push(measure);
  1180. }
  1181. this.graphicalMusicSheet.sourceToGraphicalMeasureLinks.setValue(sourceMeasure, verticalMeasureList);
  1182. return verticalMeasureList;
  1183. }
  1184. private createGraphicalMeasure(sourceMeasure: SourceMeasure, openTuplets: Tuplet[], openBeams: Beam[],
  1185. accidentalCalculator: AccidentalCalculator, activeClefs: ClefInstruction[],
  1186. openOctaveShifts: OctaveShiftParams[], openLyricWords: LyricWord[], staffIndex: number,
  1187. staffEntryLinks: StaffEntryLink[]): GraphicalMeasure {
  1188. const staff: Staff = this.graphicalMusicSheet.ParentMusicSheet.getStaffFromIndex(staffIndex);
  1189. const measure: GraphicalMeasure = MusicSheetCalculator.symbolFactory.createGraphicalMeasure(sourceMeasure, staff);
  1190. measure.hasError = sourceMeasure.getErrorInMeasure(staffIndex);
  1191. if (sourceMeasure.FirstInstructionsStaffEntries[staffIndex] !== undefined) {
  1192. for (let idx: number = 0, len: number = sourceMeasure.FirstInstructionsStaffEntries[staffIndex].Instructions.length; idx < len; ++idx) {
  1193. const instruction: AbstractNotationInstruction = sourceMeasure.FirstInstructionsStaffEntries[staffIndex].Instructions[idx];
  1194. if (instruction instanceof KeyInstruction) {
  1195. const key: KeyInstruction = KeyInstruction.copy(instruction);
  1196. if (this.graphicalMusicSheet.ParentMusicSheet.Transpose !== 0 &&
  1197. measure.ParentStaff.ParentInstrument.MidiInstrumentId !== MidiInstrument.Percussion &&
  1198. MusicSheetCalculator.transposeCalculator !== undefined) {
  1199. MusicSheetCalculator.transposeCalculator.transposeKey(
  1200. key, this.graphicalMusicSheet.ParentMusicSheet.Transpose
  1201. );
  1202. }
  1203. accidentalCalculator.ActiveKeyInstruction = key;
  1204. }
  1205. }
  1206. }
  1207. for (let idx: number = 0, len: number = sourceMeasure.StaffLinkedExpressions[staffIndex].length; idx < len; ++idx) {
  1208. const multiExpression: MultiExpression = sourceMeasure.StaffLinkedExpressions[staffIndex][idx];
  1209. if (multiExpression.OctaveShiftStart !== undefined) {
  1210. const openOctaveShift: OctaveShift = multiExpression.OctaveShiftStart;
  1211. openOctaveShifts[staffIndex] = new OctaveShiftParams(
  1212. openOctaveShift, multiExpression.AbsoluteTimestamp,
  1213. openOctaveShift.ParentEndMultiExpression.AbsoluteTimestamp
  1214. );
  1215. }
  1216. }
  1217. for (let entryIndex: number = 0; entryIndex < sourceMeasure.VerticalSourceStaffEntryContainers.length; entryIndex++) {
  1218. const sourceStaffEntry: SourceStaffEntry = sourceMeasure.VerticalSourceStaffEntryContainers[entryIndex].StaffEntries[staffIndex];
  1219. if (sourceStaffEntry !== undefined) {
  1220. for (let idx: number = 0, len: number = sourceStaffEntry.Instructions.length; idx < len; ++idx) {
  1221. const abstractNotationInstruction: AbstractNotationInstruction = sourceStaffEntry.Instructions[idx];
  1222. if (abstractNotationInstruction instanceof ClefInstruction) {
  1223. activeClefs[staffIndex] = <ClefInstruction>abstractNotationInstruction;
  1224. }
  1225. }
  1226. const graphicalStaffEntry: GraphicalStaffEntry = MusicSheetCalculator.symbolFactory.createStaffEntry(sourceStaffEntry, measure);
  1227. if (measure.staffEntries.length > entryIndex) {
  1228. measure.addGraphicalStaffEntryAtTimestamp(graphicalStaffEntry);
  1229. } else {
  1230. measure.addGraphicalStaffEntry(graphicalStaffEntry);
  1231. }
  1232. const linkedNotes: Note[] = [];
  1233. if (sourceStaffEntry.Link !== undefined) {
  1234. sourceStaffEntry.findLinkedNotes(linkedNotes);
  1235. this.handleStaffEntryLink(graphicalStaffEntry, staffEntryLinks);
  1236. }
  1237. let octaveShiftValue: OctaveEnum = OctaveEnum.NONE;
  1238. if (openOctaveShifts[staffIndex] !== undefined) {
  1239. const octaveShiftParams: OctaveShiftParams = openOctaveShifts[staffIndex];
  1240. if (octaveShiftParams.getAbsoluteStartTimestamp.lte(sourceStaffEntry.AbsoluteTimestamp) &&
  1241. sourceStaffEntry.AbsoluteTimestamp.lte(octaveShiftParams.getAbsoluteEndTimestamp)) {
  1242. octaveShiftValue = octaveShiftParams.getOpenOctaveShift.Type;
  1243. }
  1244. }
  1245. for (let idx: number = 0, len: number = sourceStaffEntry.VoiceEntries.length; idx < len; ++idx) {
  1246. const voiceEntry: VoiceEntry = sourceStaffEntry.VoiceEntries[idx];
  1247. octaveShiftValue = this.handleVoiceEntry(
  1248. voiceEntry, graphicalStaffEntry,
  1249. accidentalCalculator, openLyricWords,
  1250. activeClefs[staffIndex], openTuplets,
  1251. openBeams, octaveShiftValue, linkedNotes,
  1252. sourceStaffEntry
  1253. );
  1254. }
  1255. if (sourceStaffEntry.Instructions.length > 0) {
  1256. const clefInstruction: ClefInstruction = <ClefInstruction>sourceStaffEntry.Instructions[0];
  1257. MusicSheetCalculator.symbolFactory.createInStaffClef(graphicalStaffEntry, clefInstruction);
  1258. }
  1259. if (sourceStaffEntry.ChordContainer !== undefined) {
  1260. sourceStaffEntry.ParentStaff.ParentInstrument.HasChordSymbols = true;
  1261. MusicSheetCalculator.symbolFactory.createChordSymbol(
  1262. sourceStaffEntry,
  1263. graphicalStaffEntry,
  1264. this.graphicalMusicSheet.ParentMusicSheet.Transpose);
  1265. }
  1266. }
  1267. }
  1268. accidentalCalculator.doCalculationsAtEndOfMeasure();
  1269. if (sourceMeasure.LastInstructionsStaffEntries[staffIndex] !== undefined) {
  1270. const lastStaffEntry: SourceStaffEntry = sourceMeasure.LastInstructionsStaffEntries[staffIndex];
  1271. for (let idx: number = 0, len: number = lastStaffEntry.Instructions.length; idx < len; ++idx) {
  1272. const abstractNotationInstruction: AbstractNotationInstruction = lastStaffEntry.Instructions[idx];
  1273. if (abstractNotationInstruction instanceof ClefInstruction) {
  1274. activeClefs[staffIndex] = <ClefInstruction>abstractNotationInstruction;
  1275. }
  1276. }
  1277. }
  1278. for (let idx: number = 0, len: number = sourceMeasure.StaffLinkedExpressions[staffIndex].length; idx < len; ++idx) {
  1279. const multiExpression: MultiExpression = sourceMeasure.StaffLinkedExpressions[staffIndex][idx];
  1280. if (multiExpression.OctaveShiftEnd !== undefined && openOctaveShifts[staffIndex] !== undefined &&
  1281. multiExpression.OctaveShiftEnd === openOctaveShifts[staffIndex].getOpenOctaveShift) {
  1282. openOctaveShifts[staffIndex] = undefined;
  1283. }
  1284. }
  1285. // if there are no staffEntries in this measure, create a rest for the whole measure:
  1286. if (measure.staffEntries.length === 0) {
  1287. const sourceStaffEntry: SourceStaffEntry = new SourceStaffEntry(
  1288. new VerticalSourceStaffEntryContainer(measure.parentSourceMeasure,
  1289. measure.parentSourceMeasure.AbsoluteTimestamp,
  1290. measure.parentSourceMeasure.CompleteNumberOfStaves),
  1291. staff);
  1292. const voiceEntry: VoiceEntry = new VoiceEntry(new Fraction(0, 1), staff.Voices[0], sourceStaffEntry);
  1293. const note: Note = new Note(voiceEntry, sourceStaffEntry, Fraction.createFromFraction(sourceMeasure.Duration), undefined);
  1294. voiceEntry.Notes.push(note);
  1295. const graphicalStaffEntry: GraphicalStaffEntry = MusicSheetCalculator.symbolFactory.createStaffEntry(sourceStaffEntry, measure);
  1296. measure.addGraphicalStaffEntry(graphicalStaffEntry);
  1297. graphicalStaffEntry.relInMeasureTimestamp = voiceEntry.Timestamp;
  1298. const gve: GraphicalVoiceEntry = MusicSheetCalculator.symbolFactory.createVoiceEntry(voiceEntry, graphicalStaffEntry);
  1299. graphicalStaffEntry.graphicalVoiceEntries.push(gve);
  1300. const graphicalNote: GraphicalNote = MusicSheetCalculator.symbolFactory.createNote(note,
  1301. gve,
  1302. new ClefInstruction(),
  1303. OctaveEnum.NONE, undefined);
  1304. gve.notes.push(graphicalNote);
  1305. }
  1306. return measure;
  1307. }
  1308. private checkNoteForAccidental(graphicalNote: GraphicalNote, accidentalCalculator: AccidentalCalculator, activeClef: ClefInstruction,
  1309. octaveEnum: OctaveEnum): void {
  1310. let pitch: Pitch = graphicalNote.sourceNote.Pitch;
  1311. const transpose: number = this.graphicalMusicSheet.ParentMusicSheet.Transpose;
  1312. if (transpose !== 0 && graphicalNote.sourceNote.ParentStaffEntry.ParentStaff.ParentInstrument.MidiInstrumentId !== MidiInstrument.Percussion) {
  1313. pitch = graphicalNote.Transpose(
  1314. accidentalCalculator.ActiveKeyInstruction, activeClef, transpose, octaveEnum
  1315. );
  1316. }
  1317. graphicalNote.sourceNote.halfTone = pitch.getHalfTone();
  1318. accidentalCalculator.checkAccidental(graphicalNote, pitch);
  1319. }
  1320. // // needed to disable linter, as it doesn't recognize the existing usage of this method.
  1321. // // ToDo: check if a newer version doesn't have the problem.
  1322. // /* tslint:disable:no-unused-variable */
  1323. // private createStaffEntryForTieNote(measure: StaffMeasure, absoluteTimestamp: Fraction, openTie: Tie): GraphicalStaffEntry {
  1324. // /* tslint:enable:no-unused-variable */
  1325. // let graphicalStaffEntry: GraphicalStaffEntry;
  1326. // graphicalStaffEntry = MusicSheetCalculator.symbolFactory.createStaffEntry(openTie.Start.ParentStaffEntry, measure);
  1327. // graphicalStaffEntry.relInMeasureTimestamp = Fraction.minus(absoluteTimestamp, measure.parentSourceMeasure.AbsoluteTimestamp);
  1328. // this.resetYPositionForLeadSheet(graphicalStaffEntry.PositionAndShape);
  1329. // measure.addGraphicalStaffEntryAtTimestamp(graphicalStaffEntry);
  1330. // return graphicalStaffEntry;
  1331. // }
  1332. private handleStaffEntries(): void {
  1333. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MeasureList.length; idx < len; ++idx) {
  1334. const measures: GraphicalMeasure[] = this.graphicalMusicSheet.MeasureList[idx];
  1335. for (let idx2: number = 0, len2: number = measures.length; idx2 < len2; ++idx2) {
  1336. const measure: GraphicalMeasure = measures[idx2];
  1337. for (const graphicalStaffEntry of measure.staffEntries) {
  1338. if (graphicalStaffEntry.parentMeasure !== undefined
  1339. && graphicalStaffEntry.graphicalVoiceEntries.length > 0
  1340. && graphicalStaffEntry.graphicalVoiceEntries[0].notes.length > 0) {
  1341. this.layoutVoiceEntries(graphicalStaffEntry);
  1342. this.layoutStaffEntry(graphicalStaffEntry);
  1343. }
  1344. }
  1345. }
  1346. }
  1347. }
  1348. private calculateSkyBottomLines(): void {
  1349. for (const graphicalMusicPage of this.graphicalMusicSheet.MusicPages) {
  1350. for (const musicSystem of graphicalMusicPage.MusicSystems) {
  1351. for (const staffLine of musicSystem.StaffLines) {
  1352. staffLine.SkyBottomLineCalculator.calculateLines();
  1353. }
  1354. }
  1355. }
  1356. }
  1357. private calculateBeams(): void {
  1358. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1359. const musicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1360. for (let idx2: number = 0, len2: number = musicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  1361. const musicSystem: MusicSystem = musicPage.MusicSystems[idx2];
  1362. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  1363. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  1364. for (let idx4: number = 0, len4: number = staffLine.Measures.length; idx4 < len4; ++idx4) {
  1365. const measure: GraphicalMeasure = staffLine.Measures[idx4];
  1366. for (let idx5: number = 0, len5: number = measure.staffEntries.length; idx5 < len5; ++idx5) {
  1367. const staffEntry: GraphicalStaffEntry = measure.staffEntries[idx5];
  1368. this.layoutBeams(staffEntry);
  1369. }
  1370. }
  1371. }
  1372. }
  1373. }
  1374. }
  1375. private calculateStaffEntryArticulationMarks(): void {
  1376. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1377. const page: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1378. for (let idx2: number = 0, len2: number = page.MusicSystems.length; idx2 < len2; ++idx2) {
  1379. const system: MusicSystem = page.MusicSystems[idx2];
  1380. for (let idx3: number = 0, len3: number = system.StaffLines.length; idx3 < len3; ++idx3) {
  1381. const line: StaffLine = system.StaffLines[idx3];
  1382. for (let idx4: number = 0, len4: number = line.Measures.length; idx4 < len4; ++idx4) {
  1383. const measure: GraphicalMeasure = line.Measures[idx4];
  1384. for (let idx5: number = 0, len5: number = measure.staffEntries.length; idx5 < len5; ++idx5) {
  1385. const graphicalStaffEntry: GraphicalStaffEntry = measure.staffEntries[idx5];
  1386. for (let idx6: number = 0, len6: number = graphicalStaffEntry.sourceStaffEntry.VoiceEntries.length; idx6 < len6; ++idx6) {
  1387. const voiceEntry: VoiceEntry = graphicalStaffEntry.sourceStaffEntry.VoiceEntries[idx6];
  1388. if (voiceEntry.Articulations.length > 0) {
  1389. this.layoutArticulationMarks(voiceEntry.Articulations, voiceEntry, graphicalStaffEntry);
  1390. }
  1391. }
  1392. }
  1393. }
  1394. }
  1395. }
  1396. }
  1397. }
  1398. private calculateOrnaments(): void {
  1399. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1400. const page: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1401. for (let idx2: number = 0, len2: number = page.MusicSystems.length; idx2 < len2; ++idx2) {
  1402. const system: MusicSystem = page.MusicSystems[idx2];
  1403. for (let idx3: number = 0, len3: number = system.StaffLines.length; idx3 < len3; ++idx3) {
  1404. const line: StaffLine = system.StaffLines[idx3];
  1405. for (let idx4: number = 0, len4: number = line.Measures.length; idx4 < len4; ++idx4) {
  1406. const measure: GraphicalMeasure = line.Measures[idx4];
  1407. for (let idx5: number = 0, len5: number = measure.staffEntries.length; idx5 < len5; ++idx5) {
  1408. const graphicalStaffEntry: GraphicalStaffEntry = measure.staffEntries[idx5];
  1409. for (let idx6: number = 0, len6: number = graphicalStaffEntry.sourceStaffEntry.VoiceEntries.length; idx6 < len6; ++idx6) {
  1410. const voiceEntry: VoiceEntry = graphicalStaffEntry.sourceStaffEntry.VoiceEntries[idx6];
  1411. if (voiceEntry.OrnamentContainer !== undefined) {
  1412. if (voiceEntry.hasTie() && !graphicalStaffEntry.relInMeasureTimestamp.Equals(voiceEntry.Timestamp)) {
  1413. continue;
  1414. }
  1415. this.layoutOrnament(voiceEntry.OrnamentContainer, voiceEntry, graphicalStaffEntry);
  1416. if (!(this.staffEntriesWithOrnaments.indexOf(graphicalStaffEntry) !== -1)) {
  1417. this.staffEntriesWithOrnaments.push(graphicalStaffEntry);
  1418. }
  1419. }
  1420. }
  1421. }
  1422. }
  1423. }
  1424. }
  1425. }
  1426. }
  1427. private optimizeRestPlacement(): void {
  1428. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1429. const page: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1430. for (let idx2: number = 0, len2: number = page.MusicSystems.length; idx2 < len2; ++idx2) {
  1431. const system: MusicSystem = page.MusicSystems[idx2];
  1432. for (let idx3: number = 0, len3: number = system.StaffLines.length; idx3 < len3; ++idx3) {
  1433. const line: StaffLine = system.StaffLines[idx3];
  1434. for (let idx4: number = 0, len4: number = line.Measures.length; idx4 < len4; ++idx4) {
  1435. const measure: GraphicalMeasure = line.Measures[idx4];
  1436. for (let idx5: number = 0, len5: number = measure.staffEntries.length; idx5 < len5; ++idx5) {
  1437. const graphicalStaffEntry: GraphicalStaffEntry = measure.staffEntries[idx5];
  1438. this.optimizeRestNotePlacement(graphicalStaffEntry, measure);
  1439. }
  1440. }
  1441. }
  1442. }
  1443. }
  1444. }
  1445. private calculateTwoRestNotesPlacementWithCollisionDetection(graphicalStaffEntry: GraphicalStaffEntry): void {
  1446. const firstRestNote: GraphicalNote = graphicalStaffEntry.graphicalVoiceEntries[0].notes[0];
  1447. const secondRestNote: GraphicalNote = graphicalStaffEntry.graphicalVoiceEntries[1].notes[0];
  1448. secondRestNote.PositionAndShape.RelativePosition = new PointF2D(0.0, 2.5);
  1449. graphicalStaffEntry.PositionAndShape.calculateAbsolutePositionsRecursiveWithoutTopelement();
  1450. firstRestNote.PositionAndShape.computeNonOverlappingPositionWithMargin(
  1451. graphicalStaffEntry.PositionAndShape, ColDirEnum.Up,
  1452. new PointF2D(0.0, secondRestNote.PositionAndShape.RelativePosition.y)
  1453. );
  1454. const relative: PointF2D = firstRestNote.PositionAndShape.RelativePosition;
  1455. relative.y -= 1.0;
  1456. firstRestNote.PositionAndShape.RelativePosition = relative;
  1457. graphicalStaffEntry.PositionAndShape.calculateBoundingBox();
  1458. }
  1459. private calculateRestNotePlacementWithCollisionDetectionFromGraphicalNote(graphicalStaffEntry: GraphicalStaffEntry): void {
  1460. let restNote: GraphicalNote;
  1461. let graphicalNotes: GraphicalNote[];
  1462. if (graphicalStaffEntry.graphicalVoiceEntries[0].notes[0].sourceNote.Pitch === undefined) {
  1463. restNote = graphicalStaffEntry.graphicalVoiceEntries[0].notes[0];
  1464. graphicalNotes = graphicalStaffEntry.graphicalVoiceEntries[1].notes;
  1465. } else {
  1466. graphicalNotes = graphicalStaffEntry.graphicalVoiceEntries[0].notes;
  1467. restNote = graphicalStaffEntry.graphicalVoiceEntries[1].notes[0];
  1468. }
  1469. let collision: boolean = false;
  1470. graphicalStaffEntry.PositionAndShape.calculateAbsolutePositionsRecursiveWithoutTopelement();
  1471. for (let idx: number = 0, len: number = graphicalNotes.length; idx < len; ++idx) {
  1472. const graphicalNote: GraphicalNote = graphicalNotes[idx];
  1473. if (restNote.PositionAndShape.marginCollisionDetection(graphicalNote.PositionAndShape)) {
  1474. collision = true;
  1475. break;
  1476. }
  1477. }
  1478. if (collision) {
  1479. if (restNote.sourceNote.ParentVoiceEntry.ParentVoice instanceof LinkedVoice) {
  1480. const bottomBorder: number = graphicalNotes[0].PositionAndShape.BorderMarginBottom + graphicalNotes[0].PositionAndShape.RelativePosition.y;
  1481. restNote.PositionAndShape.RelativePosition = new PointF2D(0.0, bottomBorder - restNote.PositionAndShape.BorderMarginTop + 0.5);
  1482. } else {
  1483. const last: GraphicalNote = graphicalNotes[graphicalNotes.length - 1];
  1484. const topBorder: number = last.PositionAndShape.BorderMarginTop + last.PositionAndShape.RelativePosition.y;
  1485. if (graphicalNotes[0].sourceNote.ParentVoiceEntry.ParentVoice instanceof LinkedVoice) {
  1486. restNote.PositionAndShape.RelativePosition = new PointF2D(0.0, topBorder - restNote.PositionAndShape.BorderMarginBottom - 0.5);
  1487. } else {
  1488. const bottomBorder: number = graphicalNotes[0].PositionAndShape.BorderMarginBottom + graphicalNotes[0].PositionAndShape.RelativePosition.y;
  1489. if (bottomBorder < 2.0) {
  1490. restNote.PositionAndShape.RelativePosition = new PointF2D(0.0, bottomBorder - restNote.PositionAndShape.BorderMarginTop + 0.5);
  1491. } else {
  1492. restNote.PositionAndShape.RelativePosition = new PointF2D(0.0, topBorder - restNote.PositionAndShape.BorderMarginBottom - 0.0);
  1493. }
  1494. }
  1495. }
  1496. }
  1497. graphicalStaffEntry.PositionAndShape.calculateBoundingBox();
  1498. }
  1499. private calculateTieCurves(): void {
  1500. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1501. const graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1502. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  1503. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  1504. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  1505. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  1506. for (let idx4: number = 0, len5: number = staffLine.Measures.length; idx4 < len5; ++idx4) {
  1507. const measure: GraphicalMeasure = staffLine.Measures[idx4];
  1508. for (let idx6: number = 0, len6: number = measure.staffEntries.length; idx6 < len6; ++idx6) {
  1509. const staffEntry: GraphicalStaffEntry = measure.staffEntries[idx6];
  1510. const graphicalTies: GraphicalTie[] = staffEntry.GraphicalTies;
  1511. for (let idx7: number = 0, len7: number = graphicalTies.length; idx7 < len7; ++idx7) {
  1512. const graphicalTie: GraphicalTie = graphicalTies[idx7];
  1513. if (graphicalTie.StartNote !== undefined && graphicalTie.StartNote.parentVoiceEntry.parentStaffEntry === staffEntry) {
  1514. const tieIsAtSystemBreak: boolean = (
  1515. graphicalTie.StartNote.parentVoiceEntry.parentStaffEntry.parentMeasure.ParentStaffLine !==
  1516. graphicalTie.EndNote.parentVoiceEntry.parentStaffEntry.parentMeasure.ParentStaffLine
  1517. );
  1518. this.layoutGraphicalTie(graphicalTie, tieIsAtSystemBreak);
  1519. }
  1520. }
  1521. }
  1522. }
  1523. }
  1524. }
  1525. }
  1526. }
  1527. private calculateLyricsPosition(): void {
  1528. const lyricStaffEntriesDict: Dictionary<StaffLine, GraphicalStaffEntry[]> = new Dictionary<StaffLine, GraphicalStaffEntry[]>();
  1529. // sort the lyriceVerseNumbers for every Instrument that has Lyrics
  1530. for (let idx: number = 0, len: number = this.graphicalMusicSheet.ParentMusicSheet.Instruments.length; idx < len; ++idx) {
  1531. const instrument: Instrument = this.graphicalMusicSheet.ParentMusicSheet.Instruments[idx];
  1532. if (instrument.HasLyrics && instrument.LyricVersesNumbers.length > 0) {
  1533. instrument.LyricVersesNumbers.sort();
  1534. }
  1535. }
  1536. // first calc lyrics text positions
  1537. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1538. const graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1539. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  1540. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  1541. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  1542. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  1543. const lyricsStaffEntries: GraphicalStaffEntry[] =
  1544. this.calculateSingleStaffLineLyricsPosition(staffLine, staffLine.ParentStaff.ParentInstrument.LyricVersesNumbers);
  1545. lyricStaffEntriesDict.setValue(staffLine, lyricsStaffEntries);
  1546. this.calculateLyricsExtendsAndDashes(lyricStaffEntriesDict.getValue(staffLine));
  1547. }
  1548. }
  1549. }
  1550. // then fill in the lyric word dashes and lyrics extends/underscores
  1551. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1552. const graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1553. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  1554. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  1555. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  1556. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  1557. this.calculateLyricsExtendsAndDashes(lyricStaffEntriesDict.getValue(staffLine));
  1558. }
  1559. }
  1560. }
  1561. }
  1562. /**
  1563. * This method calculates the dashes within the syllables of a LyricWord
  1564. * @param lyricEntry
  1565. */
  1566. private calculateSingleLyricWord(lyricEntry: GraphicalLyricEntry): void {
  1567. // const skyBottomLineCalculator: SkyBottomLineCalculator = new SkyBottomLineCalculator (this.rules);
  1568. const graphicalLyricWord: GraphicalLyricWord = lyricEntry.ParentLyricWord;
  1569. const index: number = graphicalLyricWord.GraphicalLyricsEntries.indexOf(lyricEntry);
  1570. let nextLyricEntry: GraphicalLyricEntry = undefined;
  1571. if (index >= 0) {
  1572. nextLyricEntry = graphicalLyricWord.GraphicalLyricsEntries[index + 1];
  1573. }
  1574. if (nextLyricEntry === undefined) {
  1575. return;
  1576. }
  1577. const startStaffLine: StaffLine = <StaffLine>lyricEntry.StaffEntryParent.parentMeasure.ParentStaffLine;
  1578. const nextStaffLine: StaffLine = <StaffLine>nextLyricEntry.StaffEntryParent.parentMeasure.ParentStaffLine;
  1579. const startStaffEntry: GraphicalStaffEntry = lyricEntry.StaffEntryParent;
  1580. const endStaffentry: GraphicalStaffEntry = nextLyricEntry.StaffEntryParent;
  1581. // if on the same StaffLine
  1582. if (lyricEntry.StaffEntryParent.parentMeasure.ParentStaffLine === nextLyricEntry.StaffEntryParent.parentMeasure.ParentStaffLine) {
  1583. // start- and End margins from the text Labels
  1584. const startX: number = startStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x +
  1585. startStaffEntry.PositionAndShape.RelativePosition.x +
  1586. lyricEntry.GraphicalLabel.PositionAndShape.BorderMarginRight;
  1587. const endX: number = endStaffentry.parentMeasure.PositionAndShape.RelativePosition.x +
  1588. endStaffentry.PositionAndShape.RelativePosition.x +
  1589. nextLyricEntry.GraphicalLabel.PositionAndShape.BorderMarginLeft;
  1590. const y: number = lyricEntry.GraphicalLabel.PositionAndShape.RelativePosition.y;
  1591. let numberOfDashes: number = 1;
  1592. if ((endX - startX) > this.rules.BetweenSyllabelMaximumDistance) {
  1593. numberOfDashes = Math.ceil((endX - startX) / this.rules.BetweenSyllabelMaximumDistance);
  1594. }
  1595. // check distance and create the adequate number of Dashes
  1596. if (numberOfDashes === 1) {
  1597. // distance between the two GraphicalLyricEntries is big for only one Dash, position in the middle
  1598. this.calculateSingleDashForLyricWord(startStaffLine, startX, endX, y);
  1599. } else {
  1600. // distance is big enough for more Dashes
  1601. // calculate the adequate number of Dashes from the distance between the two LyricEntries
  1602. // distance between the Dashes should be equal
  1603. this.calculateDashes(startStaffLine, startX, endX, y);
  1604. }
  1605. } else {
  1606. // start and end on different StaffLines
  1607. // start margin from the text Label until the End of StaffLine
  1608. const startX: number = startStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x +
  1609. startStaffEntry.PositionAndShape.RelativePosition.x +
  1610. lyricEntry.GraphicalLabel.PositionAndShape.BorderMarginRight;
  1611. const lastGraphicalMeasure: GraphicalMeasure = startStaffLine.Measures[startStaffLine.Measures.length - 1];
  1612. const endX: number = lastGraphicalMeasure.PositionAndShape.RelativePosition.x + lastGraphicalMeasure.PositionAndShape.Size.width;
  1613. let y: number = lyricEntry.GraphicalLabel.PositionAndShape.RelativePosition.y;
  1614. // calculate Dashes for the first StaffLine
  1615. this.calculateDashes(startStaffLine, startX, endX, y);
  1616. // calculate Dashes for the second StaffLine (only if endStaffEntry isn't the first StaffEntry of the StaffLine)
  1617. if (!(endStaffentry === endStaffentry.parentMeasure.staffEntries[0] &&
  1618. endStaffentry.parentMeasure === endStaffentry.parentMeasure.ParentStaffLine.Measures[0])) {
  1619. const secondStartX: number = nextStaffLine.Measures[0].staffEntries[0].PositionAndShape.RelativePosition.x;
  1620. const secondEndX: number = endStaffentry.parentMeasure.PositionAndShape.RelativePosition.x +
  1621. endStaffentry.PositionAndShape.RelativePosition.x +
  1622. nextLyricEntry.GraphicalLabel.PositionAndShape.BorderMarginLeft;
  1623. y = nextLyricEntry.GraphicalLabel.PositionAndShape.RelativePosition.y;
  1624. this.calculateDashes(nextStaffLine, secondStartX, secondEndX, y);
  1625. }
  1626. }
  1627. }
  1628. /**
  1629. * This method calculates Dashes for a LyricWord.
  1630. * @param staffLine
  1631. * @param startX
  1632. * @param endX
  1633. * @param y
  1634. */
  1635. private calculateDashes(staffLine: StaffLine, startX: number, endX: number, y: number): void {
  1636. let distance: number = endX - startX;
  1637. if (distance < this.rules.MinimumDistanceBetweenDashes) {
  1638. this.calculateSingleDashForLyricWord(staffLine, startX, endX, y);
  1639. } else {
  1640. // enough distance for more Dashes
  1641. const numberOfDashes: number = Math.floor(distance / this.rules.MinimumDistanceBetweenDashes);
  1642. const distanceBetweenDashes: number = distance / this.rules.MinimumDistanceBetweenDashes;
  1643. let counter: number = 0;
  1644. startX += distanceBetweenDashes / 2;
  1645. endX -= distanceBetweenDashes / 2;
  1646. while (counter <= Math.floor(numberOfDashes / 2.0) && endX > startX) {
  1647. distance = this.calculateRightAndLeftDashesForLyricWord(staffLine, startX, endX, y);
  1648. startX += distanceBetweenDashes;
  1649. endX -= distanceBetweenDashes;
  1650. counter++;
  1651. }
  1652. // if the remaining distance isn't big enough for two Dashes (another check would be if numberOfDashes is uneven),
  1653. // then put the last Dash in the middle of the remaining distance
  1654. if (distance > distanceBetweenDashes) {
  1655. this.calculateSingleDashForLyricWord(staffLine, startX, endX, y);
  1656. }
  1657. }
  1658. }
  1659. /**
  1660. * This method calculates a single Dash for a LyricWord, positioned in the middle of the given distance.
  1661. * @param {StaffLine} staffLine
  1662. * @param {number} startX
  1663. * @param {number} endX
  1664. * @param {number} y
  1665. */
  1666. private calculateSingleDashForLyricWord(staffLine: StaffLine, startX: number, endX: number, y: number): void {
  1667. const dash: GraphicalLabel = new GraphicalLabel(new Label("-"), this.rules.LyricsHeight, TextAlignment.CenterBottom);
  1668. dash.setLabelPositionAndShapeBorders();
  1669. staffLine.LyricsDashes.push(dash);
  1670. if (this.staffLinesWithLyricWords.indexOf(staffLine) === -1) {
  1671. this.staffLinesWithLyricWords.push(staffLine);
  1672. }
  1673. dash.PositionAndShape.Parent = staffLine.PositionAndShape;
  1674. const relative: PointF2D = new PointF2D(startX + (endX - startX) / 2, y);
  1675. dash.PositionAndShape.RelativePosition = relative;
  1676. }
  1677. /**
  1678. * Layouts the underscore line when a lyric entry is marked as extend
  1679. * @param {GraphicalLyricEntry} lyricEntry
  1680. */
  1681. private calculateLyricExtend(lyricEntry: GraphicalLyricEntry): void {
  1682. let startY: number = lyricEntry.GraphicalLabel.PositionAndShape.RelativePosition.y;
  1683. const startStaffEntry: GraphicalStaffEntry = lyricEntry.StaffEntryParent;
  1684. const startStaffLine: StaffLine = <StaffLine>lyricEntry.StaffEntryParent.parentMeasure.ParentStaffLine;
  1685. // find endstaffEntry and staffLine
  1686. let endStaffEntry: GraphicalStaffEntry = undefined;
  1687. let endStaffLine: StaffLine = undefined;
  1688. const staffIndex: number = startStaffEntry.parentMeasure.ParentStaff.idInMusicSheet;
  1689. for (let index: number = startStaffEntry.parentVerticalContainer.Index + 1;
  1690. index < this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length;
  1691. ++index) {
  1692. const gse: GraphicalStaffEntry = this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[index].StaffEntries[staffIndex];
  1693. if (gse === undefined) {
  1694. continue;
  1695. }
  1696. if (gse.hasOnlyRests()) {
  1697. break;
  1698. }
  1699. if (gse.LyricsEntries.length > 0) {
  1700. break;
  1701. }
  1702. endStaffEntry = gse;
  1703. endStaffLine = <StaffLine>endStaffEntry.parentMeasure.ParentStaffLine;
  1704. }
  1705. if (endStaffEntry === undefined) {
  1706. return;
  1707. }
  1708. // if on the same StaffLine
  1709. if (startStaffLine === endStaffLine) {
  1710. // start- and End margins from the text Labels
  1711. const startX: number = startStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x +
  1712. startStaffEntry.PositionAndShape.RelativePosition.x +
  1713. lyricEntry.GraphicalLabel.PositionAndShape.BorderMarginRight;
  1714. const endX: number = endStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x +
  1715. endStaffEntry.PositionAndShape.RelativePosition.x +
  1716. endStaffEntry.PositionAndShape.BorderMarginRight;
  1717. // needed in order to line up with the Label's text bottom line (is the y psoition of the underscore)
  1718. startY -= lyricEntry.GraphicalLabel.PositionAndShape.Size.height / 4;
  1719. // create a Line (as underscope after the LyricLabel's End)
  1720. this.calculateSingleLyricWordWithUnderscore(startStaffLine, startX, endX, startY);
  1721. } else { // start and end on different StaffLines
  1722. // start margin from the text Label until the End of StaffLine
  1723. const lastMeasureBb: BoundingBox = startStaffLine.Measures[startStaffLine.Measures.length - 1].PositionAndShape;
  1724. const startX: number = startStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x +
  1725. startStaffEntry.PositionAndShape.RelativePosition.x +
  1726. lyricEntry.GraphicalLabel.PositionAndShape.BorderMarginRight;
  1727. const endX: number = lastMeasureBb.RelativePosition.x +
  1728. lastMeasureBb.Size.width;
  1729. // needed in order to line up with the Label's text bottom line
  1730. startY -= lyricEntry.GraphicalLabel.PositionAndShape.Size.height / 4;
  1731. // first Underscore until the StaffLine's End
  1732. this.calculateSingleLyricWordWithUnderscore(startStaffLine, startX, endX, startY);
  1733. if (endStaffEntry === undefined) {
  1734. return;
  1735. }
  1736. // second Underscore in the endStaffLine until endStaffEntry (if endStaffEntry isn't the first StaffEntry of the StaffLine))
  1737. if (!(endStaffEntry === endStaffEntry.parentMeasure.staffEntries[0] &&
  1738. endStaffEntry.parentMeasure === endStaffEntry.parentMeasure.ParentStaffLine.Measures[0])) {
  1739. const secondStartX: number = endStaffLine.Measures[0].staffEntries[0].PositionAndShape.RelativePosition.x;
  1740. const secondEndX: number = endStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x +
  1741. endStaffEntry.PositionAndShape.RelativePosition.x +
  1742. endStaffEntry.PositionAndShape.BorderMarginRight;
  1743. this.calculateSingleLyricWordWithUnderscore(endStaffLine, secondStartX, secondEndX, startY);
  1744. }
  1745. }
  1746. }
  1747. /**
  1748. * This method calculates a single underscoreLine.
  1749. * @param staffLine
  1750. * @param startX
  1751. * @param end
  1752. * @param y
  1753. */
  1754. private calculateSingleLyricWordWithUnderscore(staffLine: StaffLine, startX: number, endX: number, y: number): void {
  1755. const lineStart: PointF2D = new PointF2D(startX, y);
  1756. const lineEnd: PointF2D = new PointF2D(endX, y);
  1757. const graphicalLine: GraphicalLine = new GraphicalLine(lineStart, lineEnd, this.rules.LyricUnderscoreLineWidth);
  1758. staffLine.LyricLines.push(graphicalLine);
  1759. if (this.staffLinesWithLyricWords.indexOf(staffLine) === -1) {
  1760. this.staffLinesWithLyricWords.push(staffLine);
  1761. }
  1762. }
  1763. /**
  1764. * This method calculates two Dashes for a LyricWord, positioned at the the two ends of the given distance.
  1765. * @param {StaffLine} staffLine
  1766. * @param {number} startX
  1767. * @param {number} endX
  1768. * @param {number} y
  1769. * @returns {number}
  1770. */
  1771. private calculateRightAndLeftDashesForLyricWord(staffLine: StaffLine, startX: number, endX: number, y: number): number {
  1772. const leftDash: GraphicalLabel = new GraphicalLabel(new Label("-"), this.rules.LyricsHeight, TextAlignment.CenterBottom);
  1773. leftDash.setLabelPositionAndShapeBorders();
  1774. staffLine.LyricsDashes.push(leftDash);
  1775. if (this.staffLinesWithLyricWords.indexOf(staffLine) === -1) {
  1776. this.staffLinesWithLyricWords.push(staffLine);
  1777. }
  1778. leftDash.PositionAndShape.Parent = staffLine.PositionAndShape;
  1779. const leftDashRelative: PointF2D = new PointF2D(startX, y);
  1780. leftDash.PositionAndShape.RelativePosition = leftDashRelative;
  1781. const rightDash: GraphicalLabel = new GraphicalLabel(new Label("-"), this.rules.LyricsHeight, TextAlignment.CenterBottom);
  1782. rightDash.setLabelPositionAndShapeBorders();
  1783. staffLine.LyricsDashes.push(rightDash);
  1784. rightDash.PositionAndShape.Parent = staffLine.PositionAndShape;
  1785. const rightDashRelative: PointF2D = new PointF2D(endX, y);
  1786. rightDash.PositionAndShape.RelativePosition = rightDashRelative;
  1787. return (rightDash.PositionAndShape.RelativePosition.x - leftDash.PositionAndShape.RelativePosition.x);
  1788. }
  1789. private calculateDynamicExpressions(): void {
  1790. for (let i: number = 0; i < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; i++) {
  1791. const sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  1792. for (let j: number = 0; j < sourceMeasure.StaffLinkedExpressions.length; j++) {
  1793. if (this.graphicalMusicSheet.MeasureList[i][j].ParentStaff.ParentInstrument.Visible) {
  1794. for (let k: number = 0; k < sourceMeasure.StaffLinkedExpressions[j].length; k++) {
  1795. if (sourceMeasure.StaffLinkedExpressions[j][k].InstantaniousDynamic !== undefined ||
  1796. (sourceMeasure.StaffLinkedExpressions[j][k].StartingContinuousDynamic !== undefined &&
  1797. sourceMeasure.StaffLinkedExpressions[j][k].StartingContinuousDynamic.StartMultiExpression ===
  1798. sourceMeasure.StaffLinkedExpressions[j][k] && sourceMeasure.StaffLinkedExpressions[j][k].UnknownList.length === 0)
  1799. ) {
  1800. this.calculateDynamicExpressionsForSingleMultiExpression(sourceMeasure.StaffLinkedExpressions[j][k], i, j);
  1801. }
  1802. }
  1803. }
  1804. }
  1805. }
  1806. }
  1807. private calculateOctaveShifts(): void {
  1808. for (let i: number = 0; i < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; i++) {
  1809. const sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  1810. for (let j: number = 0; j < sourceMeasure.StaffLinkedExpressions.length; j++) {
  1811. if (this.graphicalMusicSheet.MeasureList[i][j].ParentStaff.ParentInstrument.Visible) {
  1812. for (let k: number = 0; k < sourceMeasure.StaffLinkedExpressions[j].length; k++) {
  1813. if ((sourceMeasure.StaffLinkedExpressions[j][k].OctaveShiftStart !== undefined)) {
  1814. this.calculateSingleOctaveShift(sourceMeasure, sourceMeasure.StaffLinkedExpressions[j][k], i, j);
  1815. }
  1816. }
  1817. }
  1818. }
  1819. }
  1820. }
  1821. private getFirstLeftNotNullStaffEntryFromContainer(horizontalIndex: number, verticalIndex: number, multiStaffInstrument: boolean): GraphicalStaffEntry {
  1822. if (this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[horizontalIndex].StaffEntries[verticalIndex] !== undefined) {
  1823. return this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[horizontalIndex].StaffEntries[verticalIndex];
  1824. }
  1825. for (let i: number = horizontalIndex - 1; i >= 0; i--) {
  1826. if (this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].StaffEntries[verticalIndex] !== undefined) {
  1827. return this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].StaffEntries[verticalIndex];
  1828. }
  1829. }
  1830. return undefined;
  1831. }
  1832. private getFirstRightNotNullStaffEntryFromContainer(horizontalIndex: number, verticalIndex: number, multiStaffInstrument: boolean): GraphicalStaffEntry {
  1833. if (this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[horizontalIndex].StaffEntries[verticalIndex] !== undefined) {
  1834. return this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[horizontalIndex].StaffEntries[verticalIndex];
  1835. }
  1836. for (let i: number = horizontalIndex + 1; i < this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length; i++) {
  1837. if (this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].StaffEntries[verticalIndex] !== undefined) {
  1838. return this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].StaffEntries[verticalIndex];
  1839. }
  1840. }
  1841. return undefined;
  1842. }
  1843. private calculateWordRepetitionInstructions(): void {
  1844. for (let i: number = 0; i < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; i++) {
  1845. const sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  1846. for (let idx: number = 0, len: number = sourceMeasure.FirstRepetitionInstructions.length; idx < len; ++idx) {
  1847. const instruction: RepetitionInstruction = sourceMeasure.FirstRepetitionInstructions[idx];
  1848. this.calculateWordRepetitionInstruction(instruction, i);
  1849. }
  1850. for (let idx: number = 0, len: number = sourceMeasure.LastRepetitionInstructions.length; idx < len; ++idx) {
  1851. const instruction: RepetitionInstruction = sourceMeasure.LastRepetitionInstructions[idx];
  1852. this.calculateWordRepetitionInstruction(instruction, i);
  1853. }
  1854. }
  1855. }
  1856. private calculateRepetitionEndings(): void {
  1857. const musicsheet: MusicSheet = this.graphicalMusicSheet.ParentMusicSheet;
  1858. for (let idx: number = 0, len: number = musicsheet.Repetitions.length; idx < len; ++idx) {
  1859. const partListEntry: Repetition = musicsheet.Repetitions[idx];
  1860. this.calcGraphicalRepetitionEndingsRecursively(partListEntry);
  1861. }
  1862. }
  1863. private calculateTempoExpressions(): void {
  1864. for (let i: number = 0; i < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; i++) {
  1865. const sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  1866. for (let j: number = 0; j < sourceMeasure.TempoExpressions.length; j++) {
  1867. this.calculateTempoExpressionsForSingleMultiTempoExpression(sourceMeasure, sourceMeasure.TempoExpressions[j], i);
  1868. }
  1869. }
  1870. }
  1871. private calculateMoodAndUnknownExpressions(): void {
  1872. for (let i: number = 0; i < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; i++) {
  1873. const sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  1874. for (let j: number = 0; j < sourceMeasure.StaffLinkedExpressions.length; j++) {
  1875. if (this.graphicalMusicSheet.MeasureList[i][j].ParentStaff.ParentInstrument.Visible) {
  1876. for (let k: number = 0; k < sourceMeasure.StaffLinkedExpressions[j].length; k++) {
  1877. if ((sourceMeasure.StaffLinkedExpressions[j][k].MoodList.length > 0) ||
  1878. (sourceMeasure.StaffLinkedExpressions[j][k].UnknownList.length > 0)) {
  1879. this.calculateMoodAndUnknownExpression(sourceMeasure.StaffLinkedExpressions[j][k], i, j);
  1880. }
  1881. }
  1882. }
  1883. }
  1884. }
  1885. }
  1886. /**
  1887. * Calculates the desired stem direction depending on the number (or type) of voices.
  1888. * If more than one voice is there, the main voice (typically the first or upper voice) will get stem up direction.
  1889. * The others get stem down direction.
  1890. * @param voiceEntry the voiceEntry for which the stem direction has to be calculated
  1891. */
  1892. private calculateStemDirectionFromVoices(voiceEntry: VoiceEntry): void {
  1893. // Stem direction calculation:
  1894. const hasLink: boolean = voiceEntry.ParentSourceStaffEntry.Link !== undefined;
  1895. if (hasLink) {
  1896. // in case of StaffEntryLink don't check mainVoice / linkedVoice
  1897. if (voiceEntry === voiceEntry.ParentSourceStaffEntry.VoiceEntries[0]) {
  1898. // set stem up:
  1899. voiceEntry.StemDirection = StemDirectionType.Up;
  1900. return;
  1901. } else {
  1902. // set stem down:
  1903. voiceEntry.StemDirection = StemDirectionType.Down;
  1904. return;
  1905. }
  1906. } else {
  1907. if (voiceEntry.ParentVoice instanceof LinkedVoice) {
  1908. // Linked voice: set stem down:
  1909. voiceEntry.StemDirection = StemDirectionType.Down;
  1910. } else {
  1911. // if this voiceEntry belongs to the mainVoice:
  1912. // check first that there are also more voices present:
  1913. if (voiceEntry.ParentSourceStaffEntry.VoiceEntries.length > 1) {
  1914. // as this voiceEntry belongs to the mainVoice: stem Up
  1915. voiceEntry.StemDirection = StemDirectionType.Up;
  1916. }
  1917. }
  1918. }
  1919. // ToDo: shift code to end of measure to only check once for all beams
  1920. // check for a beam:
  1921. // if this voice entry currently has no desired direction yet:
  1922. if (voiceEntry.StemDirection === StemDirectionType.Undefined &&
  1923. voiceEntry.Notes.length > 0) {
  1924. const beam: Beam = voiceEntry.Notes[0].NoteBeam;
  1925. if (beam !== undefined) {
  1926. // if there is a beam, find any already set stemDirection in the beam:
  1927. for (const note of beam.Notes) {
  1928. if (note.ParentVoiceEntry === voiceEntry) {
  1929. continue;
  1930. } else if (note.ParentVoiceEntry.StemDirection !== StemDirectionType.Undefined) {
  1931. // set the stem direction
  1932. voiceEntry.StemDirection = note.ParentVoiceEntry.StemDirection;
  1933. break;
  1934. }
  1935. }
  1936. }
  1937. }
  1938. }
  1939. }