MusicSheetCalculator.ts 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817
  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, ClefEnum} 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, MultiExpressionEntry } 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 { TextAlignmentEnum } 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 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. import { PlacementEnum } from "../VoiceData/Expressions/AbstractExpression";
  60. import { AbstractGraphicalInstruction } from "./AbstractGraphicalInstruction";
  61. import { GraphicalInstantaneousTempoExpression } from "./GraphicalInstantaneousTempoExpression";
  62. import { InstantaneousTempoExpression, TempoEnum } from "../VoiceData/Expressions/InstantaneousTempoExpression";
  63. import { ContinuousTempoExpression } from "../VoiceData/Expressions/ContinuousExpressions/ContinuousTempoExpression";
  64. import { FontStyles } from "../../Common/Enums/FontStyles";
  65. import { AbstractTempoExpression } from "../VoiceData/Expressions/AbstractTempoExpression";
  66. import { GraphicalInstantaneousDynamicExpression } from "./GraphicalInstantaneousDynamicExpression";
  67. import { ContDynamicEnum } from "../VoiceData/Expressions/ContinuousExpressions/ContinuousDynamicExpression";
  68. import { GraphicalContinuousDynamicExpression } from "./GraphicalContinuousDynamicExpression";
  69. import { FillEmptyMeasuresWithWholeRests } from "../../OpenSheetMusicDisplay/OSMDOptions";
  70. import { IStafflineNoteCalculator } from "../Interfaces/IStafflineNoteCalculator";
  71. import { GraphicalUnknownExpression } from "./GraphicalUnknownExpression";
  72. /**
  73. * Class used to do all the calculations in a MusicSheet, which in the end populates a GraphicalMusicSheet.
  74. */
  75. export abstract class MusicSheetCalculator {
  76. public static symbolFactory: IGraphicalSymbolFactory;
  77. public static transposeCalculator: ITransposeCalculator;
  78. public static stafflineNoteCalculator: IStafflineNoteCalculator;
  79. protected static textMeasurer: ITextMeasurer;
  80. protected staffEntriesWithGraphicalTies: GraphicalStaffEntry[] = [];
  81. protected staffEntriesWithOrnaments: GraphicalStaffEntry[] = [];
  82. protected staffEntriesWithChordSymbols: GraphicalStaffEntry[] = [];
  83. protected staffLinesWithLyricWords: StaffLine[] = [];
  84. protected graphicalLyricWords: GraphicalLyricWord[] = [];
  85. protected graphicalMusicSheet: GraphicalMusicSheet;
  86. protected rules: EngravingRules;
  87. protected musicSystems: MusicSystem[];
  88. public static get TextMeasurer(): ITextMeasurer {
  89. return MusicSheetCalculator.textMeasurer;
  90. }
  91. public static set TextMeasurer(value: ITextMeasurer) {
  92. MusicSheetCalculator.textMeasurer = value;
  93. }
  94. protected get leadSheet(): boolean {
  95. return this.graphicalMusicSheet.LeadSheet;
  96. }
  97. protected static setMeasuresMinStaffEntriesWidth(measures: GraphicalMeasure[], minimumStaffEntriesWidth: number): void {
  98. for (let idx: number = 0, len: number = measures.length; idx < len; ++idx) {
  99. const measure: GraphicalMeasure = measures[idx];
  100. measure.minimumStaffEntriesWidth = minimumStaffEntriesWidth;
  101. }
  102. }
  103. public initialize(graphicalMusicSheet: GraphicalMusicSheet): void {
  104. this.graphicalMusicSheet = graphicalMusicSheet;
  105. this.rules = graphicalMusicSheet.ParentMusicSheet.Rules;
  106. this.prepareGraphicalMusicSheet();
  107. //this.calculate();
  108. }
  109. /**
  110. * Build the 2D [[GraphicalMeasure]] list needed for the [[MusicSheetCalculator]].
  111. * Internally it creates [[GraphicalMeasure]]s, [[GraphicalStaffEntry]]'s and [[GraphicalNote]]s.
  112. */
  113. public prepareGraphicalMusicSheet(): void {
  114. // Clear the stored system images dict - all systems have to be redrawn.
  115. // Not necessary now. TODO Check
  116. // this.graphicalMusicSheet.SystemImages.length = 0;
  117. const musicSheet: MusicSheet = this.graphicalMusicSheet.ParentMusicSheet;
  118. this.staffEntriesWithGraphicalTies = [];
  119. this.staffEntriesWithOrnaments = [];
  120. this.staffEntriesWithChordSymbols = [];
  121. this.staffLinesWithLyricWords = [];
  122. // this.staffLinesWithGraphicalExpressions = [];
  123. this.graphicalMusicSheet.Initialize();
  124. const measureList: GraphicalMeasure[][] = this.graphicalMusicSheet.MeasureList;
  125. // one AccidentalCalculator for each Staff (regardless of Instrument)
  126. const accidentalCalculators: AccidentalCalculator[] = this.createAccidentalCalculators();
  127. // List of Active ClefInstructions
  128. const activeClefs: ClefInstruction[] = this.graphicalMusicSheet.initializeActiveClefs();
  129. // LyricWord - GraphicalLyricWord Lists
  130. const lyricWords: LyricWord[] = [];
  131. const completeNumberOfStaves: number = musicSheet.getCompleteNumberOfStaves();
  132. // Octave Shifts List
  133. const openOctaveShifts: OctaveShiftParams[] = [];
  134. // TieList - timestampsArray
  135. for (let i: number = 0; i < completeNumberOfStaves; i++) {
  136. openOctaveShifts.push(undefined);
  137. }
  138. // go through all SourceMeasures (taking into account normal SourceMusicParts and Repetitions)
  139. for (let idx: number = 0, len: number = musicSheet.SourceMeasures.length; idx < len; ++idx) {
  140. const sourceMeasure: SourceMeasure = musicSheet.SourceMeasures[idx];
  141. const graphicalMeasures: GraphicalMeasure[] = this.createGraphicalMeasuresForSourceMeasure(
  142. sourceMeasure,
  143. accidentalCalculators,
  144. lyricWords,
  145. openOctaveShifts,
  146. activeClefs
  147. );
  148. measureList.push(graphicalMeasures);
  149. }
  150. this.handleStaffEntries();
  151. this.calculateVerticalContainersList();
  152. this.setIndicesToVerticalGraphicalContainers();
  153. }
  154. /**
  155. * The main method for the Calculator.
  156. */
  157. public calculate(): void {
  158. this.musicSystems = [];
  159. this.clearSystemsAndMeasures();
  160. // delete graphicalObjects (currently: ties) that will be recalculated, newly create GraphicalObjects streching over a single StaffEntry
  161. this.clearRecreatedObjects();
  162. this.createGraphicalTies();
  163. // calculate SheetLabelBoundingBoxes
  164. this.calculateSheetLabelBoundingBoxes();
  165. this.calculateXLayout(this.graphicalMusicSheet, this.maxInstrNameLabelLength());
  166. // create List<MusicPage>
  167. this.graphicalMusicSheet.MusicPages.length = 0;
  168. // create new MusicSystems and StaffLines (as many as necessary) and populate them with Measures from measureList
  169. this.calculateMusicSystems();
  170. // Add some white space at the end of the piece:
  171. //this.graphicalMusicSheet.MusicPages[0].PositionAndShape.BorderMarginBottom += 9;
  172. // transform Relative to Absolute Positions
  173. GraphicalMusicSheet.transformRelativeToAbsolutePosition(this.graphicalMusicSheet);
  174. }
  175. public calculateXLayout(graphicalMusicSheet: GraphicalMusicSheet, maxInstrNameLabelLength: number): void {
  176. // for each inner List in big Measure List calculate new Positions for the StaffEntries
  177. // and adjust Measures sizes
  178. // calculate max measure length for maximum zoom in.
  179. let minLength: number = 0;
  180. const maxInstructionsLength: number = this.rules.MaxInstructionsConstValue;
  181. if (this.graphicalMusicSheet.MeasureList.length > 0) {
  182. /** list of vertically ordered measures belonging to one bar */
  183. let measures: GraphicalMeasure[] = this.graphicalMusicSheet.MeasureList[0];
  184. let minimumStaffEntriesWidth: number = this.calculateMeasureXLayout(measures);
  185. minimumStaffEntriesWidth = this.calculateMeasureWidthFromLyrics(measures, minimumStaffEntriesWidth);
  186. MusicSheetCalculator.setMeasuresMinStaffEntriesWidth(measures, minimumStaffEntriesWidth);
  187. minLength = minimumStaffEntriesWidth * 1.2 + maxInstrNameLabelLength + maxInstructionsLength;
  188. for (let i: number = 1; i < this.graphicalMusicSheet.MeasureList.length; i++) {
  189. measures = this.graphicalMusicSheet.MeasureList[i];
  190. minimumStaffEntriesWidth = this.calculateMeasureXLayout(measures);
  191. minimumStaffEntriesWidth = this.calculateMeasureWidthFromLyrics(measures, minimumStaffEntriesWidth);
  192. MusicSheetCalculator.setMeasuresMinStaffEntriesWidth(measures, minimumStaffEntriesWidth);
  193. minLength = Math.max(minLength, minimumStaffEntriesWidth * 1.2 + maxInstructionsLength);
  194. }
  195. }
  196. this.graphicalMusicSheet.MinAllowedSystemWidth = minLength;
  197. }
  198. public calculateMeasureWidthFromLyrics(measuresVertical: GraphicalMeasure[], oldMinimumStaffEntriesWidth: number): number {
  199. throw new Error("abstract, not implemented");
  200. }
  201. protected formatMeasures(): void {
  202. throw new Error("abstract, not implemented");
  203. }
  204. /**
  205. * Calculates the x layout of the staff entries within the staff measures belonging to one source measure.
  206. * All staff entries are x-aligned throughout all the measures.
  207. * @param measures - The minimum required x width of the source measure
  208. */
  209. protected calculateMeasureXLayout(measures: GraphicalMeasure[]): number {
  210. throw new Error("abstract, not implemented");
  211. }
  212. /**
  213. * Called for every source measure when generating the list of staff measures for it.
  214. */
  215. protected initGraphicalMeasuresCreation(): void {
  216. throw new Error("abstract, not implemented");
  217. }
  218. protected handleBeam(graphicalNote: GraphicalNote, beam: Beam, openBeams: Beam[]): void {
  219. throw new Error("abstract, not implemented");
  220. }
  221. /**
  222. * Check if the tied graphical note belongs to any beams or tuplets and react accordingly.
  223. * @param tiedGraphicalNote
  224. * @param beams
  225. * @param activeClef
  226. * @param octaveShiftValue
  227. * @param graphicalStaffEntry
  228. * @param duration
  229. * @param openTie
  230. * @param isLastTieNote
  231. */
  232. protected handleTiedGraphicalNote(tiedGraphicalNote: GraphicalNote, beams: Beam[], activeClef: ClefInstruction,
  233. octaveShiftValue: OctaveEnum, graphicalStaffEntry: GraphicalStaffEntry, duration: Fraction,
  234. openTie: Tie, isLastTieNote: boolean): void {
  235. throw new Error("abstract, not implemented");
  236. }
  237. protected handleVoiceEntryLyrics(voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry,
  238. openLyricWords: LyricWord[]): void {
  239. throw new Error("abstract, not implemented");
  240. }
  241. protected handleVoiceEntryOrnaments(ornamentContainer: OrnamentContainer, voiceEntry: VoiceEntry,
  242. graphicalStaffEntry: GraphicalStaffEntry): void {
  243. throw new Error("abstract, not implemented");
  244. }
  245. protected handleVoiceEntryArticulations(articulations: ArticulationEnum[],
  246. voiceEntry: VoiceEntry,
  247. staffEntry: GraphicalStaffEntry): void {
  248. throw new Error("abstract, not implemented");
  249. }
  250. /**
  251. * Adds a technical instruction at the given staff entry.
  252. * @param technicalInstructions
  253. * @param voiceEntry
  254. * @param staffEntry
  255. */
  256. protected handleVoiceEntryTechnicalInstructions(technicalInstructions: TechnicalInstruction[],
  257. voiceEntry: VoiceEntry, staffEntry: GraphicalStaffEntry): void {
  258. throw new Error("abstract, not implemented");
  259. }
  260. protected handleTuplet(graphicalNote: GraphicalNote, tuplet: Tuplet, openTuplets: Tuplet[]): void {
  261. throw new Error("abstract, not implemented");
  262. }
  263. protected layoutVoiceEntry(voiceEntry: VoiceEntry, graphicalNotes: GraphicalNote[],
  264. graphicalStaffEntry: GraphicalStaffEntry, hasPitchedNote: boolean): void {
  265. throw new Error("abstract, not implemented");
  266. }
  267. protected layoutStaffEntry(graphicalStaffEntry: GraphicalStaffEntry): void {
  268. throw new Error("abstract, not implemented");
  269. }
  270. protected createGraphicalTie(tie: Tie, startGse: GraphicalStaffEntry, endGse: GraphicalStaffEntry, startNote: GraphicalNote,
  271. endNote: GraphicalNote): GraphicalTie {
  272. throw new Error("abstract, not implemented");
  273. }
  274. protected updateStaffLineBorders(staffLine: StaffLine): void {
  275. throw new Error("abstract, not implemented");
  276. }
  277. /**
  278. * Iterate through all Measures and calculates the MeasureNumberLabels.
  279. * @param musicSystem
  280. */
  281. protected calculateMeasureNumberPlacement(musicSystem: MusicSystem): void {
  282. const staffLine: StaffLine = musicSystem.StaffLines[0];
  283. let currentMeasureNumber: number = staffLine.Measures[0].MeasureNumber;
  284. for (const measure of staffLine.Measures) {
  285. if (measure.MeasureNumber === 0 || measure.MeasureNumber === 1) {
  286. currentMeasureNumber = measure.MeasureNumber;
  287. }
  288. if ((measure.MeasureNumber === currentMeasureNumber ||
  289. measure.MeasureNumber === currentMeasureNumber + this.rules.MeasureNumberLabelOffset) &&
  290. !measure.parentSourceMeasure.ImplicitMeasure) {
  291. if (measure.MeasureNumber !== 1 ||
  292. (measure.MeasureNumber === 1 && measure !== staffLine.Measures[0])) {
  293. this.calculateSingleMeasureNumberPlacement(measure, staffLine, musicSystem);
  294. }
  295. currentMeasureNumber = measure.MeasureNumber;
  296. }
  297. }
  298. }
  299. /// <summary>
  300. /// This method calculates a single MeasureNumberLabel and adds it to the graphical label list of the music system
  301. /// </summary>
  302. /// <param name="measure"></param>
  303. /// <param name="staffLine"></param>
  304. /// <param name="musicSystem"></param>
  305. private calculateSingleMeasureNumberPlacement(measure: GraphicalMeasure, staffLine: StaffLine, musicSystem: MusicSystem): void {
  306. const labelNumber: string = measure.MeasureNumber.toString();
  307. const label: Label = new Label(labelNumber);
  308. // maybe give rules as argument instead of just setting fontStyle and maybe other settings manually afterwards
  309. const graphicalLabel: GraphicalLabel = new GraphicalLabel(label, this.rules.MeasureNumberLabelHeight,
  310. TextAlignmentEnum.LeftBottom, this.rules);
  311. const skyBottomLineCalculator: SkyBottomLineCalculator = staffLine.SkyBottomLineCalculator;
  312. // calculate LabelBoundingBox and set PSI parent
  313. graphicalLabel.setLabelPositionAndShapeBorders();
  314. graphicalLabel.PositionAndShape.Parent = musicSystem.PositionAndShape;
  315. // calculate relative Position
  316. const relativeX: number = staffLine.PositionAndShape.RelativePosition.x +
  317. measure.PositionAndShape.RelativePosition.x - graphicalLabel.PositionAndShape.BorderMarginLeft;
  318. let relativeY: number;
  319. // and the corresponding SkyLine indices
  320. let start: number = relativeX;
  321. let end: number = relativeX - graphicalLabel.PositionAndShape.BorderLeft + graphicalLabel.PositionAndShape.BorderMarginRight;
  322. // take into account the InstrumentNameLabel's at the beginning of the first MusicSystem
  323. if (staffLine === musicSystem.StaffLines[0] && musicSystem === this.musicSystems[0]) {
  324. start -= staffLine.PositionAndShape.RelativePosition.x;
  325. end -= staffLine.PositionAndShape.RelativePosition.x;
  326. }
  327. // get the minimum corresponding SkyLine value
  328. const skyLineMinValue: number = skyBottomLineCalculator.getSkyLineMinInRange(start, end);
  329. if (measure === staffLine.Measures[0]) {
  330. // must take into account possible MusicSystem Bracket's
  331. let minBracketTopBorder: number = 0;
  332. if (musicSystem.GroupBrackets.length > 0) {
  333. for (const groupBracket of musicSystem.GroupBrackets) {
  334. minBracketTopBorder = Math.min(minBracketTopBorder, groupBracket.PositionAndShape.BorderTop);
  335. }
  336. }
  337. relativeY = Math.min(skyLineMinValue, minBracketTopBorder);
  338. } else {
  339. relativeY = skyLineMinValue;
  340. }
  341. relativeY = Math.min(0, relativeY);
  342. graphicalLabel.PositionAndShape.RelativePosition = new PointF2D(relativeX, relativeY);
  343. skyBottomLineCalculator.updateSkyLineInRange(start, end, relativeY + graphicalLabel.PositionAndShape.BorderMarginTop);
  344. musicSystem.MeasureNumberLabels.push(graphicalLabel);
  345. }
  346. /**
  347. * Calculate the shape (Bézier curve) for this tie.
  348. * @param tie
  349. * @param tieIsAtSystemBreak
  350. */
  351. protected layoutGraphicalTie(tie: GraphicalTie, tieIsAtSystemBreak: boolean): void {
  352. throw new Error("abstract, not implemented");
  353. }
  354. /**
  355. * Calculate the Lyrics YPositions for a single [[StaffLine]].
  356. * @param staffLine
  357. * @param lyricVersesNumber
  358. */
  359. protected calculateSingleStaffLineLyricsPosition(staffLine: StaffLine, lyricVersesNumber: number[]): GraphicalStaffEntry[] {
  360. let numberOfVerses: number = 0;
  361. let lyricsStartYPosition: number = this.rules.StaffHeight; // Add offset to prevent collision
  362. const lyricsStaffEntriesList: GraphicalStaffEntry[] = [];
  363. const skyBottomLineCalculator: SkyBottomLineCalculator = staffLine.SkyBottomLineCalculator;
  364. // first find maximum Ycoordinate for the whole StaffLine
  365. let len: number = staffLine.Measures.length;
  366. for (let idx: number = 0; idx < len; ++idx) {
  367. const measure: GraphicalMeasure = staffLine.Measures[idx];
  368. const measureRelativePosition: PointF2D = measure.PositionAndShape.RelativePosition;
  369. const len2: number = measure.staffEntries.length;
  370. for (let idx2: number = 0; idx2 < len2; ++idx2) {
  371. const staffEntry: GraphicalStaffEntry = measure.staffEntries[idx2];
  372. if (staffEntry.LyricsEntries.length > 0) {
  373. lyricsStaffEntriesList.push(staffEntry);
  374. numberOfVerses = Math.max(numberOfVerses, staffEntry.LyricsEntries.length);
  375. // Position of Staffentry relative to StaffLine
  376. const staffEntryPositionX: number = staffEntry.PositionAndShape.RelativePosition.x +
  377. measureRelativePosition.x;
  378. let minMarginLeft: number = Number.MAX_VALUE;
  379. let maxMarginRight: number = Number.MIN_VALUE;
  380. // if more than one LyricEntry in StaffEntry, find minMarginLeft, maxMarginRight of all corresponding Labels
  381. for (let i: number = 0; i < staffEntry.LyricsEntries.length; i++) {
  382. const lyricsEntryLabel: GraphicalLabel = staffEntry.LyricsEntries[i].GraphicalLabel;
  383. minMarginLeft = Math.min(minMarginLeft, staffEntryPositionX + lyricsEntryLabel.PositionAndShape.BorderMarginLeft);
  384. maxMarginRight = Math.max(maxMarginRight, staffEntryPositionX + lyricsEntryLabel.PositionAndShape.BorderMarginRight);
  385. }
  386. // check BottomLine in this range and take the maximum between the two values
  387. const bottomLineMax: number = skyBottomLineCalculator.getBottomLineMaxInRange(minMarginLeft, maxMarginRight);
  388. lyricsStartYPosition = Math.max(lyricsStartYPosition, bottomLineMax);
  389. }
  390. }
  391. }
  392. let maxPosition: number = 0;
  393. // iterate again through the Staffentries with LyricEntries
  394. len = lyricsStaffEntriesList.length;
  395. for (const staffEntry of lyricsStaffEntriesList) {
  396. // set LyricEntryLabel RelativePosition
  397. for (let i: number = 0; i < staffEntry.LyricsEntries.length; i++) {
  398. const lyricEntry: GraphicalLyricEntry = staffEntry.LyricsEntries[i];
  399. const lyricsEntryLabel: GraphicalLabel = lyricEntry.GraphicalLabel;
  400. // read the verseNumber and get index of this number in the sorted LyricVerseNumbersList of Instrument
  401. // eg verseNumbers: 2,3,4,6 => 1,2,3,4
  402. const verseNumber: number = lyricEntry.LyricsEntry.VerseNumber;
  403. const sortedLyricVerseNumberIndex: number = lyricVersesNumber.indexOf(verseNumber);
  404. const firstPosition: number = lyricsStartYPosition + this.rules.LyricsHeight + this.rules.VerticalBetweenLyricsDistance;
  405. // Y-position calculated according to aforementioned mapping
  406. let position: number = firstPosition + (this.rules.VerticalBetweenLyricsDistance + this.rules.LyricsHeight) * sortedLyricVerseNumberIndex;
  407. if (this.leadSheet) {
  408. position = 3.4 + (this.rules.VerticalBetweenLyricsDistance + this.rules.LyricsHeight) * (sortedLyricVerseNumberIndex);
  409. }
  410. const previousRelativeX: number = lyricsEntryLabel.PositionAndShape.RelativePosition.x;
  411. lyricsEntryLabel.PositionAndShape.RelativePosition = new PointF2D(previousRelativeX, position);
  412. maxPosition = Math.max(maxPosition, position);
  413. }
  414. }
  415. // update BottomLine (on the whole StaffLine's length)
  416. if (lyricsStaffEntriesList.length > 0) {
  417. const endX: number = staffLine.PositionAndShape.Size.width;
  418. let startX: number = lyricsStaffEntriesList[0].PositionAndShape.RelativePosition.x +
  419. lyricsStaffEntriesList[0].PositionAndShape.BorderMarginLeft +
  420. lyricsStaffEntriesList[0].parentMeasure.PositionAndShape.RelativePosition.x;
  421. startX = startX > endX ? endX : startX;
  422. skyBottomLineCalculator.updateBottomLineInRange(startX, endX, maxPosition);
  423. }
  424. return lyricsStaffEntriesList;
  425. }
  426. /**
  427. * calculates the dashes of lyric words and the extending underscore lines of syllables sung on more than one note.
  428. * @param lyricsStaffEntries
  429. */
  430. protected calculateLyricsExtendsAndDashes(lyricsStaffEntries: GraphicalStaffEntry[]): void {
  431. // iterate again to create now the extend lines and dashes for words
  432. for (let idx: number = 0, len: number = lyricsStaffEntries.length; idx < len; ++idx) {
  433. const staffEntry: GraphicalStaffEntry = lyricsStaffEntries[idx];
  434. // set LyricEntryLabel RelativePosition
  435. for (let i: number = 0; i < staffEntry.LyricsEntries.length; i++) {
  436. const lyricEntry: GraphicalLyricEntry = staffEntry.LyricsEntries[i];
  437. // calculate LyricWord's Dashes and underscoreLine
  438. if (lyricEntry.ParentLyricWord !== undefined &&
  439. lyricEntry.ParentLyricWord.GraphicalLyricsEntries[lyricEntry.ParentLyricWord.GraphicalLyricsEntries.length - 1] !== lyricEntry) {
  440. this.calculateSingleLyricWord(lyricEntry);
  441. }
  442. // calculate the underscore line extend if needed
  443. if (lyricEntry.LyricsEntry.extend) {
  444. this.calculateLyricExtend(lyricEntry);
  445. }
  446. }
  447. }
  448. }
  449. /**
  450. * Calculate a single OctaveShift for a [[MultiExpression]].
  451. * @param sourceMeasure
  452. * @param multiExpression
  453. * @param measureIndex
  454. * @param staffIndex
  455. */
  456. protected calculateSingleOctaveShift(sourceMeasure: SourceMeasure, multiExpression: MultiExpression,
  457. measureIndex: number, staffIndex: number): void {
  458. throw new Error("abstract, not implemented");
  459. }
  460. /**
  461. * Calculate all the textual [[RepetitionInstruction]]s (e.g. dal segno) for a single [[SourceMeasure]].
  462. * @param repetitionInstruction
  463. * @param measureIndex
  464. */
  465. protected calculateWordRepetitionInstruction(repetitionInstruction: RepetitionInstruction,
  466. measureIndex: number): void {
  467. throw new Error("abstract, not implemented");
  468. }
  469. /**
  470. * Calculate all the Mood and Unknown Expressions for a single [[MultiExpression]].
  471. * @param multiExpression
  472. * @param measureIndex
  473. * @param staffIndex
  474. */
  475. protected calculateMoodAndUnknownExpression(multiExpression: MultiExpression, measureIndex: number, staffIndex: number): void {
  476. // calculate absolute Timestamp
  477. const absoluteTimestamp: Fraction = multiExpression.AbsoluteTimestamp;
  478. const measures: GraphicalMeasure[] = this.graphicalMusicSheet.MeasureList[measureIndex];
  479. let relative: PointF2D = new PointF2D();
  480. if ((multiExpression.MoodList.length > 0) || (multiExpression.UnknownList.length > 0)) {
  481. let combinedExprString: string = "";
  482. for (let idx: number = 0, len: number = multiExpression.EntriesList.length; idx < len; ++idx) {
  483. const entry: MultiExpressionEntry = multiExpression.EntriesList[idx];
  484. if (entry.prefix !== "") {
  485. if (combinedExprString === "") {
  486. combinedExprString += entry.prefix;
  487. } else {
  488. combinedExprString += " " + entry.prefix;
  489. }
  490. }
  491. if (combinedExprString === "") {
  492. combinedExprString += entry.label;
  493. } else {
  494. combinedExprString += " " + entry.label;
  495. }
  496. }
  497. const staffLine: StaffLine = measures[staffIndex].ParentStaffLine;
  498. relative = this.getRelativePositionInStaffLineFromTimestamp(absoluteTimestamp, staffIndex, staffLine, staffLine.isPartOfMultiStaffInstrument());
  499. if (Math.abs(relative.x - 0) < 0.0001) {
  500. relative.x = measures[staffIndex].beginInstructionsWidth + this.rules.RhythmRightMargin;
  501. }
  502. const fontHeight: number = this.rules.UnknownTextHeight;
  503. const graphLabel: GraphicalLabel = this.calculateLabel(staffLine,
  504. relative, combinedExprString,
  505. multiExpression.getFontstyleOfFirstEntry(),
  506. multiExpression.getPlacementOfFirstEntry(),
  507. fontHeight);
  508. const gue: GraphicalUnknownExpression = new GraphicalUnknownExpression(staffLine, graphLabel);
  509. staffLine.AbstractExpressions.push(gue);
  510. }
  511. }
  512. /**
  513. * Delete all Objects that must be recalculated.
  514. * If graphicalMusicSheet.reCalculate has been called, then this method will be called to reset or remove all flexible
  515. * graphical music symbols (e.g. Ornaments, Lyrics, Slurs) graphicalMusicSheet will have MusicPages, they will have MusicSystems etc...
  516. */
  517. protected clearRecreatedObjects(): void {
  518. // Clear StaffEntries with GraphicalTies
  519. for (let idx: number = 0, len: number = this.staffEntriesWithGraphicalTies.length; idx < len; ++idx) {
  520. const staffEntriesWithGraphicalTie: GraphicalStaffEntry = this.staffEntriesWithGraphicalTies[idx];
  521. staffEntriesWithGraphicalTie.GraphicalTies.length = 0;
  522. }
  523. this.staffEntriesWithGraphicalTies.length = 0;
  524. return;
  525. }
  526. /**
  527. * This method handles a [[StaffEntryLink]].
  528. * @param graphicalStaffEntry
  529. * @param staffEntryLinks
  530. */
  531. protected handleStaffEntryLink(graphicalStaffEntry: GraphicalStaffEntry,
  532. staffEntryLinks: StaffEntryLink[]): void {
  533. log.debug("handleStaffEntryLink not implemented");
  534. }
  535. /**
  536. * Store the newly computed [[Measure]]s in newly created [[MusicSystem]]s.
  537. */
  538. protected calculateMusicSystems(): void {
  539. if (this.graphicalMusicSheet.MeasureList === undefined) {
  540. return;
  541. }
  542. const allMeasures: GraphicalMeasure[][] = this.graphicalMusicSheet.MeasureList;
  543. if (allMeasures === undefined) {
  544. return;
  545. }
  546. if (this.rules.MinMeasureToDrawIndex > allMeasures.length - 1) {
  547. log.debug("minimum measure to draw index out of range. resetting min measure index to limit.");
  548. this.rules.MinMeasureToDrawIndex = allMeasures.length - 1;
  549. }
  550. // visible 2D-MeasureList
  551. const visibleMeasureList: GraphicalMeasure[][] = [];
  552. for (let idx: number = this.rules.MinMeasureToDrawIndex, len: number = allMeasures.length;
  553. idx < len && idx <= this.rules.MaxMeasureToDrawIndex; ++idx) {
  554. const graphicalMeasures: GraphicalMeasure[] = allMeasures[idx];
  555. const visiblegraphicalMeasures: GraphicalMeasure[] = [];
  556. for (let idx2: number = 0, len2: number = graphicalMeasures.length; idx2 < len2; ++idx2) {
  557. const graphicalMeasure: GraphicalMeasure = allMeasures[idx][idx2];
  558. if (graphicalMeasure.isVisible()) {
  559. visiblegraphicalMeasures.push(graphicalMeasure);
  560. if (this.rules.ColoringEnabled) {
  561. // (re-)color notes
  562. for (const staffEntry of graphicalMeasure.staffEntries) {
  563. for (const gve of staffEntry.graphicalVoiceEntries) {
  564. gve.color();
  565. }
  566. }
  567. }
  568. }
  569. }
  570. visibleMeasureList.push(visiblegraphicalMeasures);
  571. }
  572. // find out how many StaffLine Instances we need
  573. let numberOfStaffLines: number = 0;
  574. for (let idx: number = 0, len: number = visibleMeasureList.length; idx < len; ++idx) {
  575. const gmlist: GraphicalMeasure[] = visibleMeasureList[idx];
  576. numberOfStaffLines = Math.max(gmlist.length, numberOfStaffLines);
  577. break;
  578. }
  579. if (numberOfStaffLines === 0) {
  580. return;
  581. }
  582. // build the MusicSystems
  583. const musicSystemBuilder: MusicSystemBuilder = new MusicSystemBuilder();
  584. musicSystemBuilder.initialize(this.graphicalMusicSheet, visibleMeasureList, numberOfStaffLines);
  585. this.musicSystems = musicSystemBuilder.buildMusicSystems();
  586. this.formatMeasures();
  587. // check for Measures with only WholeRestNotes and correct their X-Position (middle of Measure)
  588. // this.checkMeasuresForWholeRestNotes(); // this currently does nothing
  589. if (!this.leadSheet) {
  590. // calculate Beam Placement
  591. // this.calculateBeams(); // does nothing for now, because layoutBeams() is an empty method
  592. // possible Displacement of RestNotes
  593. this.optimizeRestPlacement();
  594. // possible Displacement of RestNotes
  595. this.calculateStaffEntryArticulationMarks();
  596. if (this.rules.RenderSlurs) { // technically we should separate slurs and ties, but shouldn't be relevant for now
  597. // calculate Ties
  598. this.calculateTieCurves();
  599. }
  600. }
  601. // calculate Sky- and BottomLine
  602. // will have reasonable values only between ObjectsBorders (eg StaffEntries)
  603. this.calculateSkyBottomLines();
  604. // calculate TupletsNumbers
  605. this.calculateTupletNumbers();
  606. // calculate MeasureNumbers
  607. if (this.rules.RenderMeasureNumbers) {
  608. for (let idx: number = 0, len: number = this.musicSystems.length; idx < len; ++idx) {
  609. const musicSystem: MusicSystem = this.musicSystems[idx];
  610. this.calculateMeasureNumberPlacement(musicSystem);
  611. }
  612. }
  613. // calculate Slurs
  614. if (!this.leadSheet && this.rules.RenderSlurs) {
  615. this.calculateSlurs();
  616. }
  617. // calculate StaffEntry Ornaments
  618. // (must come after Slurs)
  619. if (!this.leadSheet) {
  620. this.calculateOrnaments();
  621. }
  622. // calculate StaffEntry ChordSymbols
  623. this.calculateChordSymbols();
  624. if (!this.leadSheet) {
  625. // calculate all Instantaneous/Continuous Dynamics Expressions
  626. this.calculateDynamicExpressions();
  627. // calculate all Mood and Unknown Expression
  628. this.calculateMoodAndUnknownExpressions();
  629. // Calculate the alignment of close expressions
  630. this.calculateExpressionAlignements();
  631. // calculate all OctaveShifts
  632. this.calculateOctaveShifts();
  633. // calcualte RepetitionInstructions (Dal Segno, Coda, etc)
  634. this.calculateWordRepetitionInstructions();
  635. }
  636. // calculate endings last, so they appear above measure numbers
  637. this.calculateRepetitionEndings();
  638. // calcualte all Tempo Expressions
  639. if (!this.leadSheet) {
  640. this.calculateTempoExpressions();
  641. }
  642. // calculate all LyricWords Positions
  643. this.calculateLyricsPosition();
  644. // update all StaffLine's Borders
  645. // create temporary Object, just to call the methods (in order to avoid declaring them static)
  646. for (let idx2: number = 0, len2: number = this.musicSystems.length; idx2 < len2; ++idx2) {
  647. const musicSystem: MusicSystem = this.musicSystems[idx2];
  648. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  649. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  650. this.updateStaffLineBorders(staffLine);
  651. }
  652. }
  653. // calculate Y-spacing -> MusicPages are created here
  654. musicSystemBuilder.calculateSystemYLayout();
  655. // calculate Comments for each Staffline
  656. this.calculateComments();
  657. // calculate marked Areas for Systems
  658. this.calculateMarkedAreas();
  659. // the following must be done after Y-spacing, when the MusicSystems's final Dimensions are set
  660. // set the final yPositions of Objects such as SystemLabels and SystemLinesContainers,
  661. // create all System Lines, Brackets and MeasureNumbers (for all systems and for all pages)
  662. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  663. const graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  664. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  665. const isFirstSystem: boolean = idx === 0 && idx2 === 0;
  666. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  667. musicSystem.setMusicSystemLabelsYPosition();
  668. if (!this.leadSheet) {
  669. musicSystem.setYPositionsToVerticalLineObjectsAndCreateLines(this.rules);
  670. musicSystem.createSystemLeftLine(this.rules.SystemThinLineWidth, this.rules.SystemLabelsRightMargin, isFirstSystem);
  671. musicSystem.createInstrumentBrackets(this.graphicalMusicSheet.ParentMusicSheet.Instruments, this.rules.StaffHeight);
  672. musicSystem.createGroupBrackets(this.graphicalMusicSheet.ParentMusicSheet.InstrumentalGroups, this.rules.StaffHeight, 0);
  673. musicSystem.alignBeginInstructions();
  674. } else if (musicSystem === musicSystem.Parent.MusicSystems[0]) {
  675. musicSystem.createSystemLeftLine(this.rules.SystemThinLineWidth, this.rules.SystemLabelsRightMargin, isFirstSystem);
  676. }
  677. musicSystem.calculateBorders(this.rules);
  678. }
  679. const distance: number = graphicalMusicPage.MusicSystems[0].PositionAndShape.BorderTop;
  680. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  681. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  682. // let newPosition: PointF2D = new PointF2D(musicSystem.PositionAndShape.RelativePosition.x,
  683. // musicSystem.PositionAndShape.RelativePosition.y - distance);
  684. musicSystem.PositionAndShape.RelativePosition =
  685. new PointF2D(musicSystem.PositionAndShape.RelativePosition.x, musicSystem.PositionAndShape.RelativePosition.y - distance);
  686. }
  687. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  688. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  689. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  690. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  691. staffLine.addActivitySymbolClickArea();
  692. }
  693. }
  694. // calculate TopBottom Borders for all elements recursively
  695. graphicalMusicPage.PositionAndShape.calculateTopBottomBorders(); // necessary for composer label (page labels) for high notes in first system
  696. // TODO how much performance does this cost? can we reduce the amount of calculations, e.g. only checking top?
  697. // calculate all Labels's Positions for the first Page
  698. if (graphicalMusicPage === this.graphicalMusicSheet.MusicPages[0]) {
  699. this.calculatePageLabels(graphicalMusicPage);
  700. }
  701. // calculate TopBottom Borders for all elements recursively
  702. graphicalMusicPage.PositionAndShape.calculateTopBottomBorders(); // this is where top bottom borders were originally calculated (only once)
  703. }
  704. }
  705. protected calculateMarkedAreas(): void {
  706. //log.debug("calculateMarkedAreas not implemented");
  707. return;
  708. }
  709. protected calculateComments(): void {
  710. //log.debug("calculateComments not implemented");
  711. return;
  712. }
  713. protected calculateChordSymbols(): void {
  714. for (const musicSystem of this.musicSystems) {
  715. for (const staffLine of musicSystem.StaffLines) {
  716. const sbc: SkyBottomLineCalculator = staffLine.SkyBottomLineCalculator;
  717. for (const measure of staffLine.Measures) {
  718. for (const staffEntry of measure.staffEntries) {
  719. if (!staffEntry.graphicalChordContainers || staffEntry.graphicalChordContainers.length === 0) {
  720. continue;
  721. }
  722. for (const graphicalChordContainer of staffEntry.graphicalChordContainers) {
  723. const sps: BoundingBox = staffEntry.PositionAndShape;
  724. const gps: BoundingBox = graphicalChordContainer.PositionAndShape;
  725. const start: number = gps.BorderMarginLeft + sps.AbsolutePosition.x;
  726. const end: number = gps.BorderMarginRight + sps.AbsolutePosition.x;
  727. sbc.updateSkyLineInRange(start, end, sps.BorderMarginTop);
  728. }
  729. }
  730. }
  731. }
  732. }
  733. }
  734. /**
  735. * Do layout on staff measures which only consist of a full rest.
  736. * @param rest
  737. * @param gse
  738. * @param measure
  739. */
  740. protected layoutMeasureWithWholeRest(rest: GraphicalNote, gse: GraphicalStaffEntry,
  741. measure: GraphicalMeasure): void {
  742. return;
  743. }
  744. protected layoutBeams(staffEntry: GraphicalStaffEntry): void {
  745. return;
  746. }
  747. protected layoutArticulationMarks(articulations: ArticulationEnum[], voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry): void {
  748. return;
  749. }
  750. protected layoutOrnament(ornaments: OrnamentContainer, voiceEntry: VoiceEntry,
  751. graphicalStaffEntry: GraphicalStaffEntry): void {
  752. return;
  753. }
  754. protected calculateRestNotePlacementWithinGraphicalBeam(graphicalStaffEntry: GraphicalStaffEntry,
  755. restNote: GraphicalNote,
  756. previousNote: GraphicalNote,
  757. nextStaffEntry: GraphicalStaffEntry,
  758. nextNote: GraphicalNote): void {
  759. return;
  760. }
  761. protected calculateTupletNumbers(): void {
  762. return;
  763. }
  764. protected calculateSlurs(): void {
  765. return;
  766. }
  767. protected calculateDynamicExpressionsForMultiExpression(multiExpression: MultiExpression, measureIndex: number, staffIndex: number): void {
  768. return;
  769. }
  770. /**
  771. * This method calculates the RelativePosition of a single verbal GraphicalContinuousDynamic.
  772. * @param graphicalContinuousDynamic Graphical continous dynamic to be calculated
  773. * @param startPosInStaffline Starting point in staff line
  774. */
  775. protected calculateGraphicalVerbalContinuousDynamic(graphicalContinuousDynamic: GraphicalContinuousDynamicExpression,
  776. startPosInStaffline: PointF2D): void {
  777. // if ContinuousDynamicExpression is given from words
  778. const graphLabel: GraphicalLabel = graphicalContinuousDynamic.Label;
  779. const left: number = startPosInStaffline.x + graphLabel.PositionAndShape.BorderMarginLeft;
  780. const right: number = startPosInStaffline.x + graphLabel.PositionAndShape.BorderMarginRight;
  781. // placement always below the currentStaffLine, with the exception of Voice Instrument (-> above)
  782. const placement: PlacementEnum = graphicalContinuousDynamic.ContinuousDynamic.Placement;
  783. const staffLine: StaffLine = graphicalContinuousDynamic.ParentStaffLine;
  784. const skyBottomLineCalculator: SkyBottomLineCalculator = staffLine.SkyBottomLineCalculator;
  785. let drawingHeight: number;
  786. if (placement === PlacementEnum.Below) {
  787. drawingHeight = skyBottomLineCalculator.getBottomLineMaxInRange(left, right); // Bottom line
  788. graphLabel.PositionAndShape.RelativePosition = new PointF2D(startPosInStaffline.x, drawingHeight - graphLabel.PositionAndShape.BorderMarginTop);
  789. } else {
  790. drawingHeight = skyBottomLineCalculator.getSkyLineMinInRange(left, right);
  791. graphLabel.PositionAndShape.RelativePosition = new PointF2D(startPosInStaffline.x, drawingHeight - graphLabel.PositionAndShape.BorderMarginBottom);
  792. }
  793. }
  794. /**
  795. * This method calculates the RelativePosition of a single GraphicalContinuousDynamic.
  796. * @param graphicalContinuousDynamic Graphical continous dynamic to be calculated
  797. * @param startPosInStaffline Starting point in staff line
  798. */
  799. public calculateGraphicalContinuousDynamic(graphicalContinuousDynamic: GraphicalContinuousDynamicExpression, startPosInStaffline: PointF2D): void {
  800. const staffIndex: number = graphicalContinuousDynamic.ParentStaffLine.ParentStaff.idInMusicSheet;
  801. // TODO: Previously the staffIndex was passed down. BUT you can (and this function actually does this) get it from
  802. // the musicSystem OR from the ParentStaffLine. Is this the same index?
  803. // const staffIndex: number = musicSystem.StaffLines.indexOf(staffLine);
  804. // We know we have an end measure because otherwise we won't be called
  805. const endMeasure: GraphicalMeasure = this.graphicalMusicSheet.getGraphicalMeasureFromSourceMeasureAndIndex(
  806. graphicalContinuousDynamic.ContinuousDynamic.EndMultiExpression.SourceMeasureParent, staffIndex);
  807. if (!endMeasure) {
  808. log.warn("Not working");
  809. return;
  810. }
  811. graphicalContinuousDynamic.EndMeasure = endMeasure;
  812. const staffLine: StaffLine = graphicalContinuousDynamic.ParentStaffLine;
  813. const endStaffLine: StaffLine = endMeasure.ParentStaffLine;
  814. // check if Expression spreads over the same StaffLine or not
  815. const sameStaffLine: boolean = endStaffLine !== undefined && staffLine === endStaffLine;
  816. let isPartOfMultiStaffInstrument: boolean = false;
  817. if (endStaffLine) { // unfortunately we can't do something like (endStaffLine?.check() || staffLine?.check()) in this typescript version
  818. isPartOfMultiStaffInstrument = endStaffLine.isPartOfMultiStaffInstrument();
  819. } else if (staffLine) {
  820. isPartOfMultiStaffInstrument = staffLine.isPartOfMultiStaffInstrument();
  821. }
  822. const endAbsoluteTimestamp: Fraction = Fraction.createFromFraction(graphicalContinuousDynamic.ContinuousDynamic.EndMultiExpression.AbsoluteTimestamp);
  823. const endPosInStaffLine: PointF2D = this.getRelativePositionInStaffLineFromTimestamp(
  824. endAbsoluteTimestamp, staffIndex, endStaffLine, isPartOfMultiStaffInstrument, 0);
  825. //currentMusicSystem and currentStaffLine
  826. const musicSystem: MusicSystem = staffLine.ParentMusicSystem;
  827. const currentStaffLineIndex: number = musicSystem.StaffLines.indexOf(staffLine);
  828. const skyBottomLineCalculator: SkyBottomLineCalculator = staffLine.SkyBottomLineCalculator;
  829. // let expressionIndex: number;
  830. // placement always below the currentStaffLine, with the exception of Voice Instrument (-> above)
  831. const placement: PlacementEnum = graphicalContinuousDynamic.ContinuousDynamic.Placement;
  832. // if ContinuousDynamicExpression is given from wedge
  833. let secondGraphicalContinuousDynamic: GraphicalContinuousDynamicExpression = undefined;
  834. // last length check
  835. if (sameStaffLine && endPosInStaffLine.x - startPosInStaffline.x < this.rules.WedgeMinLength) {
  836. endPosInStaffLine.x = startPosInStaffline.x + this.rules.WedgeMinLength;
  837. }
  838. // Upper staff wedge always starts at the given position and the lower staff wedge always starts at the begin of measure
  839. const upperStartX: number = startPosInStaffline.x;
  840. const lowerStartX: number = endStaffLine.Measures[0].beginInstructionsWidth - this.rules.WedgeHorizontalMargin - 2;
  841. //TODO fix this when a range of measures to draw is given that doesn't include all the dynamic's measures (e.g. for crescendo)
  842. let upperEndX: number = 0;
  843. let lowerEndX: number = 0;
  844. if (!sameStaffLine) {
  845. upperEndX = staffLine.PositionAndShape.Size.width;
  846. lowerEndX = endPosInStaffLine.x;
  847. // must create a new Wedge
  848. secondGraphicalContinuousDynamic = new GraphicalContinuousDynamicExpression(graphicalContinuousDynamic.ContinuousDynamic, endStaffLine);
  849. secondGraphicalContinuousDynamic.IsSplittedPart = true;
  850. graphicalContinuousDynamic.IsSplittedPart = true;
  851. } else {
  852. upperEndX = endPosInStaffLine.x;
  853. }
  854. // the Height of the Expression's placement
  855. let idealY: number = 0;
  856. let secondIdealY: number = 0;
  857. if (placement === PlacementEnum.Below) {
  858. // can be a single Staff Instrument or an Instrument with 2 Staves
  859. let nextStaffLineIndex: number = 0;
  860. if (currentStaffLineIndex < musicSystem.StaffLines.length - 1) {
  861. nextStaffLineIndex = currentStaffLineIndex + 1;
  862. }
  863. // check, maybe currentStaffLine is the last of the MusicSystem (and it has a ContinuousDynamicExpression with placement below)
  864. if (nextStaffLineIndex > currentStaffLineIndex) {
  865. // currentStaffLine isn't the last of the MusicSystem
  866. const nextStaffLine: StaffLine = musicSystem.StaffLines[nextStaffLineIndex];
  867. const distanceBetweenStaffLines: number = nextStaffLine.PositionAndShape.RelativePosition.y -
  868. staffLine.PositionAndShape.RelativePosition.y -
  869. this.rules.StaffHeight;
  870. // ideal Height is exactly between the two StaffLines
  871. idealY = this.rules.StaffHeight + distanceBetweenStaffLines / 2;
  872. } else {
  873. // currentStaffLine is the MusicSystem's last
  874. idealY = this.rules.WedgePlacementBelowY;
  875. }
  876. // must consider the upperWedge starting/ending tip for the comparison with the BottomLine
  877. idealY -= this.rules.WedgeOpeningLength / 2;
  878. if (!sameStaffLine) {
  879. // Set the value for the splitted y position to the ideal position before we check and modify it with
  880. // the skybottom calculator detection
  881. secondIdealY = idealY;
  882. }
  883. // must check BottomLine for possible collisions within the Length of the Expression
  884. // find the corresponding max value for the given Length
  885. let maxBottomLineValueForExpressionLength: number = skyBottomLineCalculator.getBottomLineMaxInRange(upperStartX, upperEndX);
  886. // if collisions, then set the Height accordingly
  887. if (maxBottomLineValueForExpressionLength > idealY) {
  888. idealY = maxBottomLineValueForExpressionLength;
  889. }
  890. // special case - wedge must be drawn within the boundaries of a crossedBeam
  891. const withinCrossedBeam: boolean = false;
  892. if (currentStaffLineIndex < musicSystem.StaffLines.length - 1) {
  893. // find GraphicalStaffEntries closest to wedge's xPositions
  894. const closestToEndStaffEntry: GraphicalStaffEntry = staffLine.findClosestStaffEntry(upperEndX);
  895. const closestToStartStaffEntry: GraphicalStaffEntry = staffLine.findClosestStaffEntry(upperStartX);
  896. if (closestToStartStaffEntry && closestToEndStaffEntry) {
  897. // must check both StaffLines
  898. const startVerticalContainer: VerticalGraphicalStaffEntryContainer = closestToStartStaffEntry.parentVerticalContainer;
  899. // const endVerticalContainer: VerticalGraphicalStaffEntryContainer = closestToEndStaffEntry.parentVerticalContainer;
  900. if (startVerticalContainer) {
  901. // TODO: Needs to be implemented?
  902. // withinCrossedBeam = areStaffEntriesWithinCrossedBeam(startVerticalContainer,
  903. // endVerticalContainer, currentStaffLineIndex, nextStaffLineIndex);
  904. }
  905. if (withinCrossedBeam) {
  906. const nextStaffLine: StaffLine = musicSystem.StaffLines[nextStaffLineIndex];
  907. const nextStaffLineMinSkyLineValue: number = nextStaffLine.SkyBottomLineCalculator.getSkyLineMinInRange(upperStartX, upperEndX);
  908. const distanceBetweenStaffLines: number = nextStaffLine.PositionAndShape.RelativePosition.y -
  909. staffLine.PositionAndShape.RelativePosition.y;
  910. const relativeSkyLineHeight: number = distanceBetweenStaffLines + nextStaffLineMinSkyLineValue;
  911. if (relativeSkyLineHeight - this.rules.WedgeOpeningLength > this.rules.StaffHeight) {
  912. idealY = relativeSkyLineHeight - this.rules.WedgeVerticalMargin;
  913. } else {
  914. idealY = this.rules.StaffHeight + this.rules.WedgeOpeningLength;
  915. }
  916. graphicalContinuousDynamic.NotToBeRemoved = true;
  917. }
  918. }
  919. }
  920. // do the same in case of a Wedge ending at another StaffLine
  921. if (!sameStaffLine) {
  922. maxBottomLineValueForExpressionLength = endStaffLine.SkyBottomLineCalculator.getBottomLineMaxInRange(lowerStartX, lowerEndX);
  923. if (maxBottomLineValueForExpressionLength > secondIdealY) {
  924. secondIdealY = maxBottomLineValueForExpressionLength;
  925. }
  926. secondIdealY += this.rules.WedgeOpeningLength / 2;
  927. secondIdealY += this.rules.WedgeVerticalMargin;
  928. }
  929. if (!withinCrossedBeam) {
  930. idealY += this.rules.WedgeOpeningLength / 2;
  931. idealY += this.rules.WedgeVerticalMargin;
  932. }
  933. } else if (placement === PlacementEnum.Above) {
  934. // single Staff Instrument (eg Voice)
  935. if (staffLine.ParentStaff.ParentInstrument.Staves.length === 1) {
  936. // single Staff Voice Instrument
  937. idealY = this.rules.WedgePlacementAboveY;
  938. } else {
  939. // Staff = not the first Staff of a 2-staved Instrument
  940. let previousStaffLineIndex: number = 0;
  941. if (currentStaffLineIndex > 0) {
  942. previousStaffLineIndex = currentStaffLineIndex - 1;
  943. }
  944. const previousStaffLine: StaffLine = musicSystem.StaffLines[previousStaffLineIndex];
  945. const distanceBetweenStaffLines: number = staffLine.PositionAndShape.RelativePosition.y -
  946. previousStaffLine.PositionAndShape.RelativePosition.y -
  947. this.rules.StaffHeight;
  948. // ideal Height is exactly between the two StaffLines
  949. idealY = -distanceBetweenStaffLines / 2;
  950. }
  951. // must consider the upperWedge starting/ending tip for the comparison with the SkyLine
  952. idealY += this.rules.WedgeOpeningLength / 2;
  953. if (!sameStaffLine) {
  954. secondIdealY = idealY;
  955. }
  956. // must check SkyLine for possible collisions within the Length of the Expression
  957. // find the corresponding min value for the given Length
  958. let minSkyLineValueForExpressionLength: number = skyBottomLineCalculator.getSkyLineMinInRange(upperStartX, upperEndX);
  959. // if collisions, then set the Height accordingly
  960. if (minSkyLineValueForExpressionLength < idealY) {
  961. idealY = minSkyLineValueForExpressionLength;
  962. }
  963. const withinCrossedBeam: boolean = false;
  964. // special case - wedge must be drawn within the boundaries of a crossedBeam
  965. if (staffLine.ParentStaff.ParentInstrument.Staves.length > 1 && currentStaffLineIndex > 0) {
  966. // find GraphicalStaffEntries closest to wedge's xPositions
  967. const closestToStartStaffEntry: GraphicalStaffEntry = staffLine.findClosestStaffEntry(upperStartX);
  968. const closestToEndStaffEntry: GraphicalStaffEntry = staffLine.findClosestStaffEntry(upperEndX);
  969. if (closestToStartStaffEntry && closestToEndStaffEntry) {
  970. // must check both StaffLines
  971. const startVerticalContainer: VerticalGraphicalStaffEntryContainer = closestToStartStaffEntry.parentVerticalContainer;
  972. // const endVerticalContainer: VerticalGraphicalStaffEntryContainer = closestToEndStaffEntry.parentVerticalContainer;
  973. const formerStaffLineIndex: number = currentStaffLineIndex - 1;
  974. if (startVerticalContainer) {
  975. // withinCrossedBeam = this.areStaffEntriesWithinCrossedBeam(startVerticalContainer,
  976. // endVerticalContainer, currentStaffLineIndex, formerStaffLineIndex);
  977. }
  978. if (withinCrossedBeam) {
  979. const formerStaffLine: StaffLine = musicSystem.StaffLines[formerStaffLineIndex];
  980. const formerStaffLineMaxBottomLineValue: number = formerStaffLine.SkyBottomLineCalculator.
  981. getBottomLineMaxInRange(upperStartX, upperEndX);
  982. const distanceBetweenStaffLines: number = staffLine.PositionAndShape.RelativePosition.y -
  983. formerStaffLine.PositionAndShape.RelativePosition.y;
  984. const relativeSkyLineHeight: number = distanceBetweenStaffLines - formerStaffLineMaxBottomLineValue;
  985. idealY = (relativeSkyLineHeight - this.rules.StaffHeight) / 2 + this.rules.StaffHeight;
  986. }
  987. }
  988. }
  989. // do the same in case of a Wedge ending at another StaffLine
  990. if (!sameStaffLine) {
  991. minSkyLineValueForExpressionLength = endStaffLine.SkyBottomLineCalculator.getSkyLineMinInRange(lowerStartX, lowerEndX);
  992. if (minSkyLineValueForExpressionLength < secondIdealY) {
  993. secondIdealY = minSkyLineValueForExpressionLength;
  994. }
  995. secondIdealY -= this.rules.WedgeOpeningLength / 2;
  996. }
  997. if (!withinCrossedBeam) {
  998. idealY -= this.rules.WedgeOpeningLength / 2;
  999. idealY -= this.rules.WedgeVerticalMargin;
  1000. }
  1001. if (!sameStaffLine) {
  1002. secondIdealY -= this.rules.WedgeVerticalMargin;
  1003. }
  1004. }
  1005. // now we have the correct placement Height for the Expression
  1006. // the idealY is calculated relative to the currentStaffLine
  1007. // Crescendo (point to the left, opening to the right)
  1008. graphicalContinuousDynamic.Lines.clear();
  1009. if (graphicalContinuousDynamic.ContinuousDynamic.DynamicType === ContDynamicEnum.crescendo) {
  1010. if (sameStaffLine) {
  1011. graphicalContinuousDynamic.createCrescendoLines(upperStartX, upperEndX, idealY);
  1012. graphicalContinuousDynamic.calcPsi();
  1013. } else {
  1014. // two different Wedges
  1015. graphicalContinuousDynamic.createFirstHalfCrescendoLines(upperStartX, upperEndX, idealY);
  1016. graphicalContinuousDynamic.calcPsi();
  1017. secondGraphicalContinuousDynamic.createSecondHalfCrescendoLines(lowerStartX, lowerEndX, secondIdealY);
  1018. secondGraphicalContinuousDynamic.calcPsi();
  1019. }
  1020. } else if (graphicalContinuousDynamic.ContinuousDynamic.DynamicType === ContDynamicEnum.diminuendo) {
  1021. if (sameStaffLine) {
  1022. graphicalContinuousDynamic.createDiminuendoLines(upperStartX, upperEndX, idealY);
  1023. graphicalContinuousDynamic.calcPsi();
  1024. } else {
  1025. graphicalContinuousDynamic.createFirstHalfDiminuendoLines(upperStartX, upperEndX, idealY);
  1026. graphicalContinuousDynamic.calcPsi();
  1027. secondGraphicalContinuousDynamic.createSecondHalfDiminuendoLines(lowerStartX, lowerEndX, secondIdealY);
  1028. secondGraphicalContinuousDynamic.calcPsi();
  1029. }
  1030. } //End Diminuendo
  1031. }
  1032. /**
  1033. * This method calculates the RelativePosition of a single GraphicalInstantaneousDynamicExpression.
  1034. * @param graphicalInstantaneousDynamic Dynamic expression to be calculated
  1035. * @param startPosInStaffline Starting point in staff line
  1036. */
  1037. protected calculateGraphicalInstantaneousDynamicExpression(graphicalInstantaneousDynamic: GraphicalInstantaneousDynamicExpression,
  1038. startPosInStaffline: PointF2D): void {
  1039. // get Margin Dimensions
  1040. const staffLine: StaffLine = graphicalInstantaneousDynamic.ParentStaffLine;
  1041. const left: number = startPosInStaffline.x + graphicalInstantaneousDynamic.PositionAndShape.BorderMarginLeft;
  1042. const right: number = startPosInStaffline.x + graphicalInstantaneousDynamic.PositionAndShape.BorderMarginRight;
  1043. const skyBottomLineCalculator: SkyBottomLineCalculator = staffLine.SkyBottomLineCalculator;
  1044. let yPosition: number = 0;
  1045. // calculate yPosition according to Placement
  1046. if (graphicalInstantaneousDynamic.Placement === PlacementEnum.Above) {
  1047. const skyLineValue: number = skyBottomLineCalculator.getSkyLineMinInRange(left, right);
  1048. // if StaffLine part of multiStaff Instrument and not the first one, ideal yPosition middle of distance between Staves
  1049. if (staffLine.isPartOfMultiStaffInstrument() && staffLine.ParentStaff !== staffLine.ParentStaff.ParentInstrument.Staves[0]) {
  1050. const formerStaffLine: StaffLine = staffLine.ParentMusicSystem.StaffLines[staffLine.ParentMusicSystem.StaffLines.indexOf(staffLine) - 1];
  1051. const difference: number = staffLine.PositionAndShape.RelativePosition.y -
  1052. formerStaffLine.PositionAndShape.RelativePosition.y - this.rules.StaffHeight;
  1053. // take always into account the size of the Dynamic
  1054. if (skyLineValue > -difference / 2) {
  1055. yPosition = -difference / 2;
  1056. } else {
  1057. yPosition = skyLineValue - graphicalInstantaneousDynamic.PositionAndShape.BorderMarginBottom;
  1058. }
  1059. } else {
  1060. yPosition = skyLineValue - graphicalInstantaneousDynamic.PositionAndShape.BorderMarginBottom;
  1061. }
  1062. graphicalInstantaneousDynamic.PositionAndShape.RelativePosition = new PointF2D(startPosInStaffline.x, yPosition);
  1063. } else if (graphicalInstantaneousDynamic.Placement === PlacementEnum.Below) {
  1064. const bottomLineValue: number = skyBottomLineCalculator.getBottomLineMaxInRange(left, right);
  1065. // if StaffLine part of multiStaff Instrument and not the last one, ideal yPosition middle of distance between Staves
  1066. const lastStaff: Staff = staffLine.ParentStaff.ParentInstrument.Staves[staffLine.ParentStaff.ParentInstrument.Staves.length - 1];
  1067. if (staffLine.isPartOfMultiStaffInstrument() && staffLine.ParentStaff !== lastStaff) {
  1068. const nextStaffLine: StaffLine = staffLine.ParentMusicSystem.StaffLines[staffLine.ParentMusicSystem.StaffLines.indexOf(staffLine) + 1];
  1069. const difference: number = nextStaffLine.PositionAndShape.RelativePosition.y -
  1070. staffLine.PositionAndShape.RelativePosition.y - this.rules.StaffHeight;
  1071. const border: number = graphicalInstantaneousDynamic.PositionAndShape.BorderMarginBottom;
  1072. // take always into account the size of the Dynamic
  1073. if (bottomLineValue + border < this.rules.StaffHeight + difference / 2) {
  1074. yPosition = this.rules.StaffHeight + difference / 2;
  1075. } else {
  1076. yPosition = bottomLineValue - graphicalInstantaneousDynamic.PositionAndShape.BorderMarginTop;
  1077. }
  1078. } else {
  1079. yPosition = bottomLineValue - graphicalInstantaneousDynamic.PositionAndShape.BorderMarginTop;
  1080. }
  1081. graphicalInstantaneousDynamic.PositionAndShape.RelativePosition = new PointF2D(startPosInStaffline.x, yPosition);
  1082. }
  1083. graphicalInstantaneousDynamic.updateSkyBottomLine();
  1084. }
  1085. protected calcGraphicalRepetitionEndingsRecursively(repetition: Repetition): void {
  1086. return;
  1087. }
  1088. /**
  1089. * Calculate a single GraphicalRepetition.
  1090. * @param start
  1091. * @param end
  1092. * @param numberText
  1093. * @param offset
  1094. * @param leftOpen
  1095. * @param rightOpen
  1096. */
  1097. protected layoutSingleRepetitionEnding(start: GraphicalMeasure, end: GraphicalMeasure, numberText: string,
  1098. offset: number, leftOpen: boolean, rightOpen: boolean): void {
  1099. return;
  1100. }
  1101. protected calculateLabel(staffLine: StaffLine,
  1102. relative: PointF2D,
  1103. combinedString: string,
  1104. style: FontStyles,
  1105. placement: PlacementEnum,
  1106. fontHeight: number,
  1107. textAlignment: TextAlignmentEnum = TextAlignmentEnum.CenterBottom): GraphicalLabel {
  1108. const label: Label = new Label(combinedString, textAlignment);
  1109. label.fontStyle = style;
  1110. label.fontHeight = fontHeight;
  1111. // TODO_RR: TextHeight from first Entry
  1112. const graphLabel: GraphicalLabel = new GraphicalLabel(label, fontHeight, label.textAlignment, this.rules, staffLine.PositionAndShape);
  1113. const marginFactor: number = 1.1;
  1114. if (placement === PlacementEnum.Below) {
  1115. graphLabel.Label.textAlignment = TextAlignmentEnum.LeftTop;
  1116. }
  1117. graphLabel.setLabelPositionAndShapeBorders();
  1118. graphLabel.PositionAndShape.BorderMarginBottom *= marginFactor;
  1119. graphLabel.PositionAndShape.BorderMarginTop *= marginFactor;
  1120. graphLabel.PositionAndShape.BorderMarginLeft *= marginFactor;
  1121. graphLabel.PositionAndShape.BorderMarginRight *= marginFactor;
  1122. let left: number = relative.x + graphLabel.PositionAndShape.BorderMarginLeft;
  1123. let right: number = relative.x + graphLabel.PositionAndShape.BorderMarginRight;
  1124. // check if GraphicalLabel exceeds the StaffLine's borders.
  1125. if (right > staffLine.PositionAndShape.Size.width) {
  1126. right = staffLine.PositionAndShape.Size.width - this.rules.MeasureRightMargin;
  1127. left = right - graphLabel.PositionAndShape.MarginSize.width;
  1128. relative.x = left - graphLabel.PositionAndShape.BorderMarginLeft;
  1129. }
  1130. // find allowed position (where the Label can be positioned) from Sky- BottomLine
  1131. let drawingHeight: number;
  1132. const skyBottomLineCalculator: SkyBottomLineCalculator = staffLine.SkyBottomLineCalculator;
  1133. if (placement === PlacementEnum.Below) {
  1134. drawingHeight = skyBottomLineCalculator.getBottomLineMaxInRange(left, right);
  1135. } else {
  1136. drawingHeight = skyBottomLineCalculator.getSkyLineMinInRange(left, right);
  1137. }
  1138. // set RelativePosition
  1139. graphLabel.PositionAndShape.RelativePosition = new PointF2D(relative.x, drawingHeight);
  1140. // update Sky- BottomLine
  1141. if (placement === PlacementEnum.Below) {
  1142. skyBottomLineCalculator.updateBottomLineInRange(left, right, graphLabel.PositionAndShape.BorderMarginBottom + drawingHeight);
  1143. } else {
  1144. skyBottomLineCalculator.updateSkyLineInRange(left, right, graphLabel.PositionAndShape.BorderMarginTop + drawingHeight);
  1145. }
  1146. return graphLabel;
  1147. }
  1148. protected calculateTempoExpressionsForMultiTempoExpression(sourceMeasure: SourceMeasure, multiTempoExpression: MultiTempoExpression,
  1149. measureIndex: number): void {
  1150. // calculate absolute Timestamp
  1151. const absoluteTimestamp: Fraction = Fraction.plus(sourceMeasure.AbsoluteTimestamp, multiTempoExpression.Timestamp);
  1152. const measures: GraphicalMeasure[] = this.graphicalMusicSheet.MeasureList[measureIndex];
  1153. let relative: PointF2D = new PointF2D();
  1154. if (multiTempoExpression.ContinuousTempo || multiTempoExpression.InstantaneousTempo) {
  1155. // TempoExpressions always on the first visible System's StaffLine // TODO is it though?
  1156. if (this.rules.MinMeasureToDrawIndex > 0) {
  1157. return; // assuming that the tempo is always in measure 1 (idx 0), adding the expression causes issues when we don't draw measure 1
  1158. }
  1159. let staffLine: StaffLine = measures[0].ParentStaffLine;
  1160. let firstVisibleMeasureX: number = measures[0].PositionAndShape.RelativePosition.x;
  1161. let verticalIndex: number = 0;
  1162. for (let j: number = 0; j < measures.length; j++) {
  1163. if (!measures[j].ParentStaffLine || measures[j].ParentStaffLine.Measures.length === 0) {
  1164. continue;
  1165. }
  1166. if (measures[j].ParentStaffLine.Measures.length > 0) {
  1167. staffLine = measures[j].ParentStaffLine;
  1168. firstVisibleMeasureX = measures[j].PositionAndShape.RelativePosition.x;
  1169. verticalIndex = j;
  1170. break;
  1171. }
  1172. }
  1173. relative = this.getRelativePositionInStaffLineFromTimestamp(absoluteTimestamp,
  1174. verticalIndex,
  1175. staffLine,
  1176. staffLine.isPartOfMultiStaffInstrument(),
  1177. firstVisibleMeasureX);
  1178. // also placement Above
  1179. if (multiTempoExpression.EntriesList.length > 0 &&
  1180. multiTempoExpression.EntriesList[0].Expression instanceof InstantaneousTempoExpression) {
  1181. const instantaniousTempo: InstantaneousTempoExpression = (multiTempoExpression.EntriesList[0].Expression as InstantaneousTempoExpression);
  1182. instantaniousTempo.Placement = PlacementEnum.Above;
  1183. // if an InstantaniousTempoExpression exists at the very beginning then
  1184. // check if expression is positioned at first ever StaffEntry and
  1185. // check if MusicSystem is first MusicSystem
  1186. if (staffLine.Measures[0].staffEntries.length > 0 &&
  1187. Math.abs(relative.x - staffLine.Measures[0].staffEntries[0].PositionAndShape.RelativePosition.x) === 0 &&
  1188. staffLine.ParentMusicSystem === this.musicSystems[0]) {
  1189. const firstInstructionEntry: GraphicalStaffEntry = staffLine.Measures[0].FirstInstructionStaffEntry;
  1190. if (firstInstructionEntry) {
  1191. const lastInstruction: AbstractGraphicalInstruction = firstInstructionEntry.GraphicalInstructions.last();
  1192. relative.x = lastInstruction.PositionAndShape.RelativePosition.x;
  1193. }
  1194. if (this.rules.CompactMode) {
  1195. relative.x = staffLine.PositionAndShape.RelativePosition.x +
  1196. staffLine.Measures[0].PositionAndShape.RelativePosition.x;
  1197. }
  1198. }
  1199. }
  1200. // const addAtLastList: GraphicalObject[] = [];
  1201. for (const entry of multiTempoExpression.EntriesList) {
  1202. let textAlignment: TextAlignmentEnum = TextAlignmentEnum.CenterBottom;
  1203. if (this.rules.CompactMode) {
  1204. textAlignment = TextAlignmentEnum.LeftBottom;
  1205. }
  1206. const graphLabel: GraphicalLabel = this.calculateLabel(staffLine,
  1207. relative,
  1208. entry.label,
  1209. multiTempoExpression.getFontstyleOfFirstEntry(),
  1210. entry.Expression.Placement,
  1211. this.rules.UnknownTextHeight,
  1212. textAlignment);
  1213. if (entry.Expression instanceof InstantaneousTempoExpression) {
  1214. //already added?
  1215. for (const expr of staffLine.AbstractExpressions) {
  1216. if (expr instanceof GraphicalInstantaneousTempoExpression &&
  1217. (expr.SourceExpression as AbstractTempoExpression).Label === entry.Expression.Label) {
  1218. //already added
  1219. continue;
  1220. }
  1221. }
  1222. const graphicalTempoExpr: GraphicalInstantaneousTempoExpression = new GraphicalInstantaneousTempoExpression(entry.Expression, graphLabel);
  1223. if (graphicalTempoExpr.ParentStaffLine === undefined) {
  1224. log.warn("Adding staffline didn't work");
  1225. // I am actually fooling the linter here and use the created object. This method needs refactoring,
  1226. // all graphical expression creations should be in one place and have basic stuff like labels, lines, ...
  1227. // in their constructor
  1228. }
  1229. // in case of metronome mark:
  1230. if (this.rules.MetronomeMarksDrawn) {
  1231. if ((entry.Expression as InstantaneousTempoExpression).Enum === TempoEnum.metronomeMark) {
  1232. this.createMetronomeMark((entry.Expression as InstantaneousTempoExpression));
  1233. continue;
  1234. }
  1235. }
  1236. } else if (entry.Expression instanceof ContinuousTempoExpression) {
  1237. // FIXME: Not yet implemented
  1238. // let alreadyAdded: boolean = false;
  1239. // for (const expr of staffLine.AbstractExpressions) {
  1240. // if (expr instanceof GraphicalContinuousTempoExpression &&
  1241. // expr.GetContinuousTempoExpression.Label === entry.Expression.Label) {
  1242. // alreadyAdded = true;
  1243. // }
  1244. // }
  1245. // if (alreadyAdded) {
  1246. // continue;
  1247. // }
  1248. // staffLine.AbstractExpressions.push(new GraphicalContinuousTempoExpression((ContinuousTempoExpression)(entry.Expression), graphLabel));
  1249. }
  1250. }
  1251. }
  1252. }
  1253. protected createMetronomeMark(metronomeExpression: InstantaneousTempoExpression): void {
  1254. throw new Error("abstract, not implemented");
  1255. }
  1256. protected graphicalMeasureCreatedCalculations(measure: GraphicalMeasure): void {
  1257. return;
  1258. }
  1259. protected clearSystemsAndMeasures(): void {
  1260. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MusicPages.length; idx < len; ++idx) {
  1261. const graphicalMusicPage: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[idx];
  1262. for (let idx2: number = 0, len2: number = graphicalMusicPage.MusicSystems.length; idx2 < len2; ++idx2) {
  1263. const musicSystem: MusicSystem = graphicalMusicPage.MusicSystems[idx2];
  1264. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  1265. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  1266. for (let idx4: number = 0, len4: number = staffLine.Measures.length; idx4 < len4; ++idx4) {
  1267. const graphicalMeasure: GraphicalMeasure = staffLine.Measures[idx4];
  1268. if (graphicalMeasure.FirstInstructionStaffEntry !== undefined) {
  1269. const index: number = graphicalMeasure.PositionAndShape.ChildElements.indexOf(
  1270. graphicalMeasure.FirstInstructionStaffEntry.PositionAndShape
  1271. );
  1272. if (index > -1) {
  1273. graphicalMeasure.PositionAndShape.ChildElements.splice(index, 1);
  1274. }
  1275. graphicalMeasure.FirstInstructionStaffEntry = undefined;
  1276. graphicalMeasure.beginInstructionsWidth = 0.0;
  1277. }
  1278. if (graphicalMeasure.LastInstructionStaffEntry !== undefined) {
  1279. const index: number = graphicalMeasure.PositionAndShape.ChildElements.indexOf(
  1280. graphicalMeasure.LastInstructionStaffEntry.PositionAndShape
  1281. );
  1282. if (index > -1) {
  1283. graphicalMeasure.PositionAndShape.ChildElements.splice(index, 1);
  1284. }
  1285. graphicalMeasure.LastInstructionStaffEntry = undefined;
  1286. graphicalMeasure.endInstructionsWidth = 0.0;
  1287. }
  1288. }
  1289. staffLine.Measures = [];
  1290. staffLine.PositionAndShape.ChildElements = [];
  1291. }
  1292. musicSystem.StaffLines.length = 0;
  1293. musicSystem.PositionAndShape.ChildElements = [];
  1294. }
  1295. graphicalMusicPage.MusicSystems = [];
  1296. graphicalMusicPage.PositionAndShape.ChildElements = [];
  1297. }
  1298. this.graphicalMusicSheet.MusicPages = [];
  1299. }
  1300. protected handleVoiceEntry(voiceEntry: VoiceEntry, graphicalStaffEntry: GraphicalStaffEntry,
  1301. accidentalCalculator: AccidentalCalculator, openLyricWords: LyricWord[],
  1302. activeClef: ClefInstruction,
  1303. openTuplets: Tuplet[], openBeams: Beam[],
  1304. octaveShiftValue: OctaveEnum, linkedNotes: Note[] = undefined,
  1305. sourceStaffEntry: SourceStaffEntry = undefined): OctaveEnum {
  1306. if (voiceEntry.StemDirectionXml !== StemDirectionType.Undefined &&
  1307. this.rules.SetWantedStemDirectionByXml &&
  1308. voiceEntry.StemDirectionXml !== undefined) {
  1309. voiceEntry.WantedStemDirection = voiceEntry.StemDirectionXml;
  1310. } else {
  1311. this.calculateStemDirectionFromVoices(voiceEntry);
  1312. }
  1313. // if GraphicalStaffEntry has been created earlier (because of Tie), then the GraphicalNotesLists have also been created
  1314. const gve: GraphicalVoiceEntry = graphicalStaffEntry.findOrCreateGraphicalVoiceEntry(voiceEntry);
  1315. gve.octaveShiftValue = octaveShiftValue;
  1316. // check for Tabs:
  1317. const tabStaffEntry: GraphicalStaffEntry = graphicalStaffEntry.tabStaffEntry;
  1318. let graphicalTabVoiceEntry: GraphicalVoiceEntry;
  1319. if (tabStaffEntry !== undefined) {
  1320. graphicalTabVoiceEntry = tabStaffEntry.findOrCreateGraphicalVoiceEntry(voiceEntry);
  1321. }
  1322. for (let idx: number = 0, len: number = voiceEntry.Notes.length; idx < len; ++idx) {
  1323. const note: Note = voiceEntry.Notes[idx];
  1324. if (note === undefined) {
  1325. continue;
  1326. }
  1327. if (sourceStaffEntry !== undefined && sourceStaffEntry.Link !== undefined && linkedNotes !== undefined && linkedNotes.indexOf(note) > -1) {
  1328. continue;
  1329. }
  1330. let graphicalNote: GraphicalNote;
  1331. if (voiceEntry.IsGrace) {
  1332. graphicalNote = MusicSheetCalculator.symbolFactory.createGraceNote(note, gve, activeClef, octaveShiftValue);
  1333. } else {
  1334. graphicalNote = MusicSheetCalculator.symbolFactory.createNote(note, gve, activeClef, octaveShiftValue, undefined);
  1335. const staffLineCount: number = voiceEntry.ParentSourceStaffEntry.ParentStaff.StafflineCount;
  1336. graphicalNote = MusicSheetCalculator.stafflineNoteCalculator.positionNote(graphicalNote, activeClef, staffLineCount);
  1337. }
  1338. if (note.Pitch !== undefined) {
  1339. this.checkNoteForAccidental(graphicalNote, accidentalCalculator, activeClef, octaveShiftValue);
  1340. }
  1341. this.resetYPositionForLeadSheet(graphicalNote.PositionAndShape);
  1342. graphicalStaffEntry.addGraphicalNoteToListAtCorrectYPosition(gve, graphicalNote);
  1343. graphicalNote.PositionAndShape.calculateBoundingBox();
  1344. if (!this.leadSheet) {
  1345. if (note.NoteBeam !== undefined && note.PrintObject) {
  1346. this.handleBeam(graphicalNote, note.NoteBeam, openBeams);
  1347. }
  1348. if (note.NoteTuplet !== undefined && note.PrintObject) {
  1349. this.handleTuplet(graphicalNote, note.NoteTuplet, openTuplets);
  1350. }
  1351. }
  1352. // handle TabNotes:
  1353. if (graphicalTabVoiceEntry) {
  1354. // notes should be either TabNotes or RestNotes -> add all:
  1355. const graphicalTabNote: GraphicalNote = MusicSheetCalculator.symbolFactory.createNote( note,
  1356. graphicalTabVoiceEntry,
  1357. activeClef,
  1358. octaveShiftValue,
  1359. undefined);
  1360. tabStaffEntry.addGraphicalNoteToListAtCorrectYPosition(graphicalTabVoiceEntry, graphicalTabNote);
  1361. graphicalTabNote.PositionAndShape.calculateBoundingBox();
  1362. if (!this.leadSheet) {
  1363. if (note.NoteTuplet) {
  1364. this.handleTuplet(graphicalTabNote, note.NoteTuplet, openTuplets);
  1365. }
  1366. }
  1367. }
  1368. }
  1369. if (voiceEntry.Articulations.length > 0) {
  1370. this.handleVoiceEntryArticulations(voiceEntry.Articulations, voiceEntry, graphicalStaffEntry);
  1371. }
  1372. if (voiceEntry.TechnicalInstructions.length > 0) {
  1373. this.handleVoiceEntryTechnicalInstructions(voiceEntry.TechnicalInstructions, voiceEntry, graphicalStaffEntry);
  1374. }
  1375. if (voiceEntry.LyricsEntries.size() > 0) {
  1376. this.handleVoiceEntryLyrics(voiceEntry, graphicalStaffEntry, openLyricWords);
  1377. }
  1378. if (voiceEntry.OrnamentContainer !== undefined) {
  1379. this.handleVoiceEntryOrnaments(voiceEntry.OrnamentContainer, voiceEntry, graphicalStaffEntry);
  1380. }
  1381. return octaveShiftValue;
  1382. }
  1383. protected resetYPositionForLeadSheet(psi: BoundingBox): void {
  1384. if (this.leadSheet) {
  1385. psi.RelativePosition = new PointF2D(psi.RelativePosition.x, 0.0);
  1386. }
  1387. }
  1388. protected layoutVoiceEntries(graphicalStaffEntry: GraphicalStaffEntry): void {
  1389. graphicalStaffEntry.PositionAndShape.RelativePosition = new PointF2D(0.0, 0.0);
  1390. if (!this.leadSheet) {
  1391. for (const gve of graphicalStaffEntry.graphicalVoiceEntries) {
  1392. const graphicalNotes: GraphicalNote[] = gve.notes;
  1393. if (graphicalNotes.length === 0) {
  1394. continue;
  1395. }
  1396. const voiceEntry: VoiceEntry = graphicalNotes[0].sourceNote.ParentVoiceEntry;
  1397. const hasPitchedNote: boolean = graphicalNotes[0].sourceNote.Pitch !== undefined;
  1398. this.layoutVoiceEntry(voiceEntry, graphicalNotes, graphicalStaffEntry, hasPitchedNote);
  1399. }
  1400. }
  1401. }
  1402. protected maxInstrNameLabelLength(): number {
  1403. let maxLabelLength: number = 0.0;
  1404. for (const instrument of this.graphicalMusicSheet.ParentMusicSheet.Instruments) {
  1405. if (instrument.Voices.length > 0 && instrument.Voices[0].Visible) {
  1406. let renderedLabel: Label = instrument.NameLabel;
  1407. if (!this.rules.RenderPartNames) {
  1408. renderedLabel = new Label("", renderedLabel.textAlignment, renderedLabel.font);
  1409. }
  1410. const graphicalLabel: GraphicalLabel = new GraphicalLabel(
  1411. renderedLabel, this.rules.InstrumentLabelTextHeight, TextAlignmentEnum.LeftCenter, this.rules);
  1412. graphicalLabel.setLabelPositionAndShapeBorders();
  1413. maxLabelLength = Math.max(maxLabelLength, graphicalLabel.PositionAndShape.MarginSize.width);
  1414. }
  1415. }
  1416. if (!this.rules.RenderPartNames) {
  1417. return 0;
  1418. }
  1419. return maxLabelLength;
  1420. }
  1421. protected calculateSheetLabelBoundingBoxes(): void {
  1422. const musicSheet: MusicSheet = this.graphicalMusicSheet.ParentMusicSheet;
  1423. const defaultColorTitle: string = this.rules.DefaultColorTitle; // can be undefined => black
  1424. if (musicSheet.Title !== undefined && this.rules.RenderTitle) {
  1425. const title: GraphicalLabel = new GraphicalLabel(musicSheet.Title, this.rules.SheetTitleHeight, TextAlignmentEnum.CenterBottom, this.rules);
  1426. title.Label.colorDefault = defaultColorTitle;
  1427. this.graphicalMusicSheet.Title = title;
  1428. title.setLabelPositionAndShapeBorders();
  1429. } else if (!this.rules.RenderTitle) {
  1430. this.graphicalMusicSheet.Title = undefined; // clear label if rendering it was disabled after last render
  1431. }
  1432. if (musicSheet.Subtitle !== undefined && this.rules.RenderSubtitle) {
  1433. const subtitle: GraphicalLabel = new GraphicalLabel(
  1434. musicSheet.Subtitle, this.rules.SheetSubtitleHeight, TextAlignmentEnum.CenterCenter, this.rules);
  1435. subtitle.Label.colorDefault = defaultColorTitle;
  1436. this.graphicalMusicSheet.Subtitle = subtitle;
  1437. subtitle.setLabelPositionAndShapeBorders();
  1438. } else if (!this.rules.RenderSubtitle) {
  1439. this.graphicalMusicSheet.Subtitle = undefined;
  1440. }
  1441. if (musicSheet.Composer !== undefined && this.rules.RenderComposer) {
  1442. const composer: GraphicalLabel = new GraphicalLabel(
  1443. musicSheet.Composer, this.rules.SheetComposerHeight, TextAlignmentEnum.RightCenter, this.rules);
  1444. composer.Label.colorDefault = defaultColorTitle;
  1445. this.graphicalMusicSheet.Composer = composer;
  1446. composer.setLabelPositionAndShapeBorders();
  1447. } else if (!this.rules.RenderComposer) {
  1448. this.graphicalMusicSheet.Composer = undefined;
  1449. }
  1450. if (musicSheet.Lyricist !== undefined && this.rules.RenderLyricist) {
  1451. const lyricist: GraphicalLabel = new GraphicalLabel(
  1452. musicSheet.Lyricist, this.rules.SheetAuthorHeight, TextAlignmentEnum.LeftCenter, this.rules);
  1453. lyricist.Label.colorDefault = defaultColorTitle;
  1454. this.graphicalMusicSheet.Lyricist = lyricist;
  1455. lyricist.setLabelPositionAndShapeBorders();
  1456. } else if (!this.rules.RenderLyricist) {
  1457. this.graphicalMusicSheet.Lyricist = undefined;
  1458. }
  1459. }
  1460. protected checkMeasuresForWholeRestNotes(): void {
  1461. for (let idx2: number = 0, len2: number = this.musicSystems.length; idx2 < len2; ++idx2) {
  1462. const musicSystem: MusicSystem = this.musicSystems[idx2];
  1463. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  1464. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  1465. for (let idx4: number = 0, len4: number = staffLine.Measures.length; idx4 < len4; ++idx4) {
  1466. const measure: GraphicalMeasure = staffLine.Measures[idx4];
  1467. if (measure.staffEntries.length === 1) {
  1468. const gse: GraphicalStaffEntry = measure.staffEntries[0];
  1469. if (gse.graphicalVoiceEntries.length > 0 && gse.graphicalVoiceEntries[0].notes.length === 1) {
  1470. const graphicalNote: GraphicalNote = gse.graphicalVoiceEntries[0].notes[0];
  1471. if (graphicalNote.sourceNote.Pitch === undefined && (new Fraction(1, 2)).lt(graphicalNote.sourceNote.Length)) {
  1472. this.layoutMeasureWithWholeRest(graphicalNote, gse, measure);
  1473. }
  1474. }
  1475. }
  1476. }
  1477. }
  1478. }
  1479. }
  1480. protected optimizeRestNotePlacement(graphicalStaffEntry: GraphicalStaffEntry, measure: GraphicalMeasure): void {
  1481. if (graphicalStaffEntry.graphicalVoiceEntries.length === 0) {
  1482. return;
  1483. }
  1484. const voice1Notes: GraphicalNote[] = graphicalStaffEntry.graphicalVoiceEntries[0].notes;
  1485. if (voice1Notes.length === 0) {
  1486. return;
  1487. }
  1488. const voice1Note1: GraphicalNote = voice1Notes[0];
  1489. const voice1Note1IsRest: boolean = voice1Note1.sourceNote.Pitch === undefined;
  1490. if (graphicalStaffEntry.graphicalVoiceEntries.length === 2) {
  1491. let voice2Note1IsRest: boolean = false;
  1492. const voice2Notes: GraphicalNote[] = graphicalStaffEntry.graphicalVoiceEntries[1].notes;
  1493. if (voice2Notes.length > 0) {
  1494. const voice2Note1: GraphicalNote = voice2Notes[0];
  1495. voice2Note1IsRest = voice2Note1.sourceNote.Pitch === undefined;
  1496. }
  1497. if (voice1Note1IsRest && voice2Note1IsRest) {
  1498. this.calculateTwoRestNotesPlacementWithCollisionDetection(graphicalStaffEntry);
  1499. } else if (voice1Note1IsRest || voice2Note1IsRest) {
  1500. this.calculateRestNotePlacementWithCollisionDetectionFromGraphicalNote(graphicalStaffEntry);
  1501. }
  1502. } else if (voice1Note1IsRest && graphicalStaffEntry !== measure.staffEntries[0] &&
  1503. graphicalStaffEntry !== measure.staffEntries[measure.staffEntries.length - 1]) {
  1504. const staffEntryIndex: number = measure.staffEntries.indexOf(graphicalStaffEntry);
  1505. const previousStaffEntry: GraphicalStaffEntry = measure.staffEntries[staffEntryIndex - 1];
  1506. const nextStaffEntry: GraphicalStaffEntry = measure.staffEntries[staffEntryIndex + 1];
  1507. if (previousStaffEntry.graphicalVoiceEntries.length === 1) {
  1508. const previousNote: GraphicalNote = previousStaffEntry.graphicalVoiceEntries[0].notes[0];
  1509. if (previousNote.sourceNote.NoteBeam !== undefined && nextStaffEntry.graphicalVoiceEntries.length === 1) {
  1510. const nextNote: GraphicalNote = nextStaffEntry.graphicalVoiceEntries[0].notes[0];
  1511. if (nextNote.sourceNote.NoteBeam !== undefined && previousNote.sourceNote.NoteBeam === nextNote.sourceNote.NoteBeam) {
  1512. this.calculateRestNotePlacementWithinGraphicalBeam(
  1513. graphicalStaffEntry, voice1Note1, previousNote,
  1514. nextStaffEntry, nextNote
  1515. );
  1516. graphicalStaffEntry.PositionAndShape.calculateBoundingBox();
  1517. }
  1518. }
  1519. }
  1520. }
  1521. }
  1522. protected getRelativePositionInStaffLineFromTimestamp(timestamp: Fraction, verticalIndex: number, staffLine: StaffLine,
  1523. multiStaffInstrument: boolean, firstVisibleMeasureRelativeX: number = 0.0): PointF2D {
  1524. let relative: PointF2D = new PointF2D();
  1525. let leftStaffEntry: GraphicalStaffEntry = undefined;
  1526. let rightStaffEntry: GraphicalStaffEntry = undefined;
  1527. const numEntries: number = this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length;
  1528. const index: number = this.graphicalMusicSheet.GetInterpolatedIndexInVerticalContainers(timestamp);
  1529. const leftIndex: number = Math.min(Math.floor(index), numEntries - 1);
  1530. const rightIndex: number = Math.min(Math.ceil(index), numEntries - 1);
  1531. if (leftIndex < 0 || verticalIndex < 0) {
  1532. return relative;
  1533. }
  1534. leftStaffEntry = this.getFirstLeftNotNullStaffEntryFromContainer(leftIndex, verticalIndex, multiStaffInstrument);
  1535. rightStaffEntry = this.getFirstRightNotNullStaffEntryFromContainer(rightIndex, verticalIndex, multiStaffInstrument);
  1536. if (leftStaffEntry !== undefined && rightStaffEntry !== undefined) {
  1537. let measureRelativeX: number = leftStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x;
  1538. if (firstVisibleMeasureRelativeX > 0) {
  1539. measureRelativeX = firstVisibleMeasureRelativeX;
  1540. }
  1541. let leftX: number = leftStaffEntry.PositionAndShape.RelativePosition.x + measureRelativeX;
  1542. let rightX: number = rightStaffEntry.PositionAndShape.RelativePosition.x + rightStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x;
  1543. if (firstVisibleMeasureRelativeX > 0) {
  1544. rightX = rightStaffEntry.PositionAndShape.RelativePosition.x + measureRelativeX;
  1545. }
  1546. let timestampQuotient: number = 0.0;
  1547. if (leftStaffEntry !== rightStaffEntry) {
  1548. const leftTimestamp: Fraction = leftStaffEntry.getAbsoluteTimestamp();
  1549. const rightTimestamp: Fraction = rightStaffEntry.getAbsoluteTimestamp();
  1550. const leftDifference: Fraction = Fraction.minus(timestamp, leftTimestamp);
  1551. timestampQuotient = leftDifference.RealValue / Fraction.minus(rightTimestamp, leftTimestamp).RealValue;
  1552. }
  1553. if (leftStaffEntry.parentMeasure.ParentStaffLine !== rightStaffEntry.parentMeasure.ParentStaffLine) {
  1554. if (leftStaffEntry.parentMeasure.ParentStaffLine === staffLine) {
  1555. rightX = staffLine.PositionAndShape.Size.width;
  1556. } else {
  1557. leftX = staffLine.PositionAndShape.RelativePosition.x;
  1558. }
  1559. }
  1560. relative = new PointF2D(leftX + (rightX - leftX) * timestampQuotient, 0.0);
  1561. }
  1562. return relative;
  1563. }
  1564. protected getRelativeXPositionFromTimestamp(timestamp: Fraction): number {
  1565. const numEntries: number = this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length;
  1566. const index: number = this.graphicalMusicSheet.GetInterpolatedIndexInVerticalContainers(timestamp);
  1567. const discreteIndex: number = Math.max(0, Math.min(Math.round(index), numEntries - 1));
  1568. const gse: GraphicalStaffEntry = this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[discreteIndex].getFirstNonNullStaffEntry();
  1569. const posX: number = gse.PositionAndShape.RelativePosition.x + gse.parentMeasure.PositionAndShape.RelativePosition.x;
  1570. return posX;
  1571. }
  1572. protected calculatePageLabels(page: GraphicalMusicPage): void {
  1573. if (this.rules.RenderSingleHorizontalStaffline) {
  1574. page.PositionAndShape.BorderRight = page.PositionAndShape.Size.width;
  1575. page.PositionAndShape.calculateBoundingBox();
  1576. this.graphicalMusicSheet.ParentMusicSheet.pageWidth = page.PositionAndShape.Size.width;
  1577. }
  1578. // The PositionAndShape child elements of page need to be manually connected to the lyricist, composer, subtitle, etc.
  1579. // because the page is only available now
  1580. let firstSystemAbsoluteTopMargin: number = 10;
  1581. if (page.MusicSystems.length > 0) {
  1582. const firstMusicSystem: MusicSystem = page.MusicSystems[0];
  1583. firstSystemAbsoluteTopMargin = firstMusicSystem.PositionAndShape.RelativePosition.y + firstMusicSystem.PositionAndShape.BorderTop;
  1584. }
  1585. //const firstStaffLine: StaffLine = this.graphicalMusicSheet.MusicPages[0].MusicSystems[0].StaffLines[0];
  1586. if (this.graphicalMusicSheet.Title !== undefined) {
  1587. const title: GraphicalLabel = this.graphicalMusicSheet.Title;
  1588. title.PositionAndShape.Parent = page.PositionAndShape;
  1589. //title.PositionAndShape.Parent = firstStaffLine.PositionAndShape;
  1590. const relative: PointF2D = new PointF2D();
  1591. relative.x = this.graphicalMusicSheet.ParentMusicSheet.pageWidth / 2;
  1592. //relative.x = firstStaffLine.PositionAndShape.RelativePosition.x + firstStaffLine.PositionAndShape.Size.width / 2; // half of first staffline width
  1593. relative.y = this.rules.TitleTopDistance + this.rules.SheetTitleHeight;
  1594. title.PositionAndShape.RelativePosition = relative;
  1595. page.Labels.push(title);
  1596. }
  1597. if (this.graphicalMusicSheet.Subtitle !== undefined) {
  1598. const subtitle: GraphicalLabel = this.graphicalMusicSheet.Subtitle;
  1599. //subtitle.PositionAndShape.Parent = firstStaffLine.PositionAndShape;
  1600. subtitle.PositionAndShape.Parent = page.PositionAndShape;
  1601. const relative: PointF2D = new PointF2D();
  1602. relative.x = this.graphicalMusicSheet.ParentMusicSheet.pageWidth / 2;
  1603. //relative.x = firstStaffLine.PositionAndShape.RelativePosition.x + firstStaffLine.PositionAndShape.Size.width / 2; // half of first staffline width
  1604. relative.y = this.rules.TitleTopDistance + this.rules.SheetTitleHeight + this.rules.SheetMinimumDistanceBetweenTitleAndSubtitle;
  1605. subtitle.PositionAndShape.RelativePosition = relative;
  1606. page.Labels.push(subtitle);
  1607. }
  1608. const composer: GraphicalLabel = this.graphicalMusicSheet.Composer;
  1609. if (composer !== undefined) {
  1610. composer.PositionAndShape.Parent = page.PositionAndShape; // if using pageWidth. (which can currently be too wide) TODO fix pageWidth (#578)
  1611. //composer.PositionAndShape.Parent = firstStaffLine.PositionAndShape; if using firstStaffLine...width.
  1612. // y-collision problems, harder to y-align with lyrics
  1613. composer.setLabelPositionAndShapeBorders();
  1614. const relative: PointF2D = new PointF2D();
  1615. //const firstStaffLineEndX: number = this.rules.PageLeftMargin + this.rules.SystemLeftMargin + this.rules.left
  1616. // firstStaffLine.PositionAndShape.RelativePosition.x + firstStaffLine.PositionAndShape.Size.width;
  1617. //relative.x = Math.min(this.graphicalMusicSheet.ParentMusicSheet.pageWidth - this.rules.PageRightMargin,
  1618. // firstStaffLineEndX); // awkward with 2-bar score
  1619. relative.x = this.graphicalMusicSheet.ParentMusicSheet.pageWidth - this.rules.PageRightMargin;
  1620. //relative.x = firstStaffLine.PositionAndShape.Size.width;
  1621. relative.y = firstSystemAbsoluteTopMargin - this.rules.SystemComposerDistance;
  1622. //relative.y = - this.rules.SystemComposerDistance;
  1623. //relative.y = -firstStaffLine.PositionAndShape.Size.height;
  1624. // TODO only add measure label height if rendering labels and composer measure has label
  1625. // TODO y-align with lyricist? which is harder if they have different bbox parents (page and firstStaffLine).
  1626. // when the pageWidth gets fixed, we could use page as parent again.
  1627. composer.PositionAndShape.RelativePosition = relative;
  1628. page.Labels.push(composer);
  1629. }
  1630. const lyricist: GraphicalLabel = this.graphicalMusicSheet.Lyricist;
  1631. if (lyricist !== undefined) {
  1632. lyricist.PositionAndShape.Parent = page.PositionAndShape;
  1633. lyricist.setLabelPositionAndShapeBorders();
  1634. const relative: PointF2D = new PointF2D();
  1635. relative.x = this.rules.PageLeftMargin;
  1636. relative.y = firstSystemAbsoluteTopMargin - this.rules.SystemComposerDistance;
  1637. //relative.y = Math.max(relative.y, composer.PositionAndShape.RelativePosition.y);
  1638. lyricist.PositionAndShape.RelativePosition = relative;
  1639. page.Labels.push(lyricist);
  1640. }
  1641. }
  1642. protected createGraphicalTies(): void {
  1643. for (let measureIndex: number = 0; measureIndex < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; measureIndex++) {
  1644. const sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[measureIndex];
  1645. for (let staffIndex: number = 0; staffIndex < sourceMeasure.CompleteNumberOfStaves; staffIndex++) {
  1646. for (let j: number = 0; j < sourceMeasure.VerticalSourceStaffEntryContainers.length; j++) {
  1647. const sourceStaffEntry: SourceStaffEntry = sourceMeasure.VerticalSourceStaffEntryContainers[j].StaffEntries[staffIndex];
  1648. if (sourceStaffEntry !== undefined) {
  1649. const startStaffEntry: GraphicalStaffEntry = this.graphicalMusicSheet.findGraphicalStaffEntryFromMeasureList(
  1650. staffIndex, measureIndex, sourceStaffEntry
  1651. );
  1652. for (let idx: number = 0, len: number = sourceStaffEntry.VoiceEntries.length; idx < len; ++idx) {
  1653. const voiceEntry: VoiceEntry = sourceStaffEntry.VoiceEntries[idx];
  1654. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  1655. const note: Note = voiceEntry.Notes[idx2];
  1656. if (note.NoteTie !== undefined) {
  1657. const tie: Tie = note.NoteTie;
  1658. this.handleTie(tie, startStaffEntry, staffIndex, measureIndex);
  1659. }
  1660. }
  1661. }
  1662. }
  1663. }
  1664. }
  1665. }
  1666. }
  1667. private handleTie(tie: Tie, startGraphicalStaffEntry: GraphicalStaffEntry, staffIndex: number, measureIndex: number): void {
  1668. let startGse: GraphicalStaffEntry = startGraphicalStaffEntry;
  1669. let startNote: GraphicalNote = startGse.findEndTieGraphicalNoteFromNote(tie.StartNote);
  1670. let endGse: GraphicalStaffEntry = undefined;
  1671. let endNote: GraphicalNote = undefined;
  1672. for (let i: number = 1; i < tie.Notes.length; i++) {
  1673. startNote = startGse.findEndTieGraphicalNoteFromNote(tie.Notes[i - 1]);
  1674. endGse = this.graphicalMusicSheet.GetGraphicalFromSourceStaffEntry(tie.Notes[i].ParentStaffEntry);
  1675. if (!endGse) {
  1676. continue;
  1677. }
  1678. endNote = endGse.findEndTieGraphicalNoteFromNote(tie.Notes[i]);
  1679. if (startNote !== undefined && endNote !== undefined && endGse !== undefined) {
  1680. if (!startNote.sourceNote.PrintObject || !endNote.sourceNote.PrintObject) {
  1681. continue;
  1682. }
  1683. const graphicalTie: GraphicalTie = this.createGraphicalTie(tie, startGse, endGse, startNote, endNote);
  1684. startGse.GraphicalTies.push(graphicalTie);
  1685. if (this.staffEntriesWithGraphicalTies.indexOf(startGse) >= 0) {
  1686. this.staffEntriesWithGraphicalTies.push(startGse);
  1687. }
  1688. }
  1689. startGse = endGse;
  1690. }
  1691. }
  1692. private createAccidentalCalculators(): AccidentalCalculator[] {
  1693. const accidentalCalculators: AccidentalCalculator[] = [];
  1694. const firstSourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.getFirstSourceMeasure();
  1695. if (firstSourceMeasure !== undefined) {
  1696. for (let i: number = 0; i < firstSourceMeasure.CompleteNumberOfStaves; i++) {
  1697. const accidentalCalculator: AccidentalCalculator = new AccidentalCalculator();
  1698. accidentalCalculators.push(accidentalCalculator);
  1699. if (firstSourceMeasure.FirstInstructionsStaffEntries[i] !== undefined) {
  1700. for (let idx: number = 0, len: number = firstSourceMeasure.FirstInstructionsStaffEntries[i].Instructions.length; idx < len; ++idx) {
  1701. const abstractNotationInstruction: AbstractNotationInstruction = firstSourceMeasure.FirstInstructionsStaffEntries[i].Instructions[idx];
  1702. if (abstractNotationInstruction instanceof KeyInstruction) {
  1703. const keyInstruction: KeyInstruction = <KeyInstruction>abstractNotationInstruction;
  1704. accidentalCalculator.ActiveKeyInstruction = keyInstruction;
  1705. }
  1706. }
  1707. }
  1708. }
  1709. }
  1710. return accidentalCalculators;
  1711. }
  1712. private calculateVerticalContainersList(): void {
  1713. const numberOfEntries: number = this.graphicalMusicSheet.MeasureList[0].length;
  1714. for (let i: number = 0; i < this.graphicalMusicSheet.MeasureList.length; i++) {
  1715. for (let j: number = 0; j < numberOfEntries; j++) {
  1716. const measure: GraphicalMeasure = this.graphicalMusicSheet.MeasureList[i][j];
  1717. for (let idx: number = 0, len: number = measure.staffEntries.length; idx < len; ++idx) {
  1718. const graphicalStaffEntry: GraphicalStaffEntry = measure.staffEntries[idx];
  1719. const verticalContainer: VerticalGraphicalStaffEntryContainer =
  1720. this.graphicalMusicSheet.getOrCreateVerticalContainer(graphicalStaffEntry.getAbsoluteTimestamp());
  1721. if (verticalContainer !== undefined) {
  1722. verticalContainer.StaffEntries[j] = graphicalStaffEntry;
  1723. graphicalStaffEntry.parentVerticalContainer = verticalContainer;
  1724. }
  1725. }
  1726. }
  1727. }
  1728. }
  1729. private setIndicesToVerticalGraphicalContainers(): void {
  1730. for (let i: number = 0; i < this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length; i++) {
  1731. this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].Index = i;
  1732. }
  1733. }
  1734. private createGraphicalMeasuresForSourceMeasure(sourceMeasure: SourceMeasure, accidentalCalculators: AccidentalCalculator[],
  1735. openLyricWords: LyricWord[],
  1736. openOctaveShifts: OctaveShiftParams[], activeClefs: ClefInstruction[]): GraphicalMeasure[] {
  1737. this.initGraphicalMeasuresCreation();
  1738. const verticalMeasureList: GraphicalMeasure[] = []; // (VexFlowMeasure, extends GraphicalMeasure)
  1739. const openBeams: Beam[] = [];
  1740. const openTuplets: Tuplet[] = [];
  1741. const staffEntryLinks: StaffEntryLink[] = [];
  1742. for (let staffIndex: number = 0; staffIndex < sourceMeasure.CompleteNumberOfStaves; staffIndex++) {
  1743. const measure: GraphicalMeasure = this.createGraphicalMeasure( // (VexFlowMeasure)
  1744. sourceMeasure, openTuplets, openBeams,
  1745. accidentalCalculators[staffIndex], activeClefs, openOctaveShifts, openLyricWords, staffIndex, staffEntryLinks
  1746. );
  1747. this.graphicalMeasureCreatedCalculations(measure);
  1748. verticalMeasureList.push(measure);
  1749. }
  1750. sourceMeasure.VerticalMeasureList = verticalMeasureList; // much easier way to link sourceMeasure to graphicalMeasures than Dictionary
  1751. //this.graphicalMusicSheet.sourceToGraphicalMeasureLinks.setValue(sourceMeasure, verticalMeasureList); // overwrites entries because:
  1752. //this.graphicalMusicSheet.sourceToGraphicalMeasureLinks[sourceMeasure] = verticalMeasureList; // can't use SourceMeasure as key.
  1753. // to save the reference by dictionary we would need two Dictionaries, id -> sourceMeasure and id -> GraphicalMeasure.
  1754. return verticalMeasureList;
  1755. }
  1756. private createGraphicalMeasure(sourceMeasure: SourceMeasure, openTuplets: Tuplet[], openBeams: Beam[],
  1757. accidentalCalculator: AccidentalCalculator, activeClefs: ClefInstruction[],
  1758. openOctaveShifts: OctaveShiftParams[], openLyricWords: LyricWord[], staffIndex: number,
  1759. staffEntryLinks: StaffEntryLink[]): GraphicalMeasure {
  1760. const staff: Staff = this.graphicalMusicSheet.ParentMusicSheet.getStaffFromIndex(staffIndex);
  1761. let measure: GraphicalMeasure = undefined;
  1762. //This property is active...
  1763. if (this.rules.PercussionOneLineCutoff !== undefined && this.rules.PercussionOneLineCutoff !== 0) {
  1764. //We have a percussion clef, check to see if this property applies...
  1765. if (activeClefs[staffIndex].ClefType === ClefEnum.percussion) {
  1766. //-1 means always trigger, or we are under the cutoff number specified
  1767. if (this.rules.PercussionOneLineCutoff === -1 ||
  1768. staff.ParentInstrument.SubInstruments.length < this.rules.PercussionOneLineCutoff) {
  1769. staff.StafflineCount = 1;
  1770. }
  1771. }
  1772. }
  1773. if (activeClefs[staffIndex].ClefType === ClefEnum.TAB) {
  1774. staff.isTab = true;
  1775. measure = MusicSheetCalculator.symbolFactory.createTabStaffMeasure(sourceMeasure, staff);
  1776. } else {
  1777. measure = MusicSheetCalculator.symbolFactory.createGraphicalMeasure(sourceMeasure, staff);
  1778. }
  1779. measure.hasError = sourceMeasure.getErrorInMeasure(staffIndex);
  1780. // check for key instruction changes
  1781. if (sourceMeasure.FirstInstructionsStaffEntries[staffIndex] !== undefined) {
  1782. for (let idx: number = 0, len: number = sourceMeasure.FirstInstructionsStaffEntries[staffIndex].Instructions.length; idx < len; ++idx) {
  1783. const instruction: AbstractNotationInstruction = sourceMeasure.FirstInstructionsStaffEntries[staffIndex].Instructions[idx];
  1784. if (instruction instanceof KeyInstruction) {
  1785. const key: KeyInstruction = KeyInstruction.copy(instruction);
  1786. if (this.graphicalMusicSheet.ParentMusicSheet.Transpose !== 0 &&
  1787. measure.ParentStaff.ParentInstrument.MidiInstrumentId !== MidiInstrument.Percussion &&
  1788. MusicSheetCalculator.transposeCalculator !== undefined) {
  1789. MusicSheetCalculator.transposeCalculator.transposeKey(
  1790. key, this.graphicalMusicSheet.ParentMusicSheet.Transpose
  1791. );
  1792. }
  1793. accidentalCalculator.ActiveKeyInstruction = key;
  1794. }
  1795. }
  1796. }
  1797. // check for octave shifts
  1798. for (let idx: number = 0, len: number = sourceMeasure.StaffLinkedExpressions[staffIndex].length; idx < len; ++idx) {
  1799. const multiExpression: MultiExpression = sourceMeasure.StaffLinkedExpressions[staffIndex][idx];
  1800. if (multiExpression.OctaveShiftStart !== undefined) {
  1801. const openOctaveShift: OctaveShift = multiExpression.OctaveShiftStart;
  1802. openOctaveShifts[staffIndex] = new OctaveShiftParams(
  1803. openOctaveShift, multiExpression.AbsoluteTimestamp,
  1804. openOctaveShift.ParentEndMultiExpression.AbsoluteTimestamp
  1805. // TODO check if octaveshift end exists, otherwise set to last measure end. only necessary if xml was cut manually and is incomplete
  1806. );
  1807. }
  1808. }
  1809. // create GraphicalStaffEntries - always check for possible null Entry
  1810. for (let entryIndex: number = 0; entryIndex < sourceMeasure.VerticalSourceStaffEntryContainers.length; entryIndex++) {
  1811. const sourceStaffEntry: SourceStaffEntry = sourceMeasure.VerticalSourceStaffEntryContainers[entryIndex].StaffEntries[staffIndex];
  1812. // is there a SourceStaffEntry at this Index
  1813. if (sourceStaffEntry !== undefined) {
  1814. // a SourceStaffEntry exists
  1815. // is there an inStaff ClefInstruction? -> update activeClef
  1816. for (let idx: number = 0, len: number = sourceStaffEntry.Instructions.length; idx < len; ++idx) {
  1817. const abstractNotationInstruction: AbstractNotationInstruction = sourceStaffEntry.Instructions[idx];
  1818. if (abstractNotationInstruction instanceof ClefInstruction) {
  1819. activeClefs[staffIndex] = <ClefInstruction>abstractNotationInstruction;
  1820. }
  1821. }
  1822. // create new GraphicalStaffEntry
  1823. const graphicalStaffEntry: GraphicalStaffEntry = MusicSheetCalculator.symbolFactory.createStaffEntry(sourceStaffEntry, measure);
  1824. if (entryIndex < measure.staffEntries.length) {
  1825. // a GraphicalStaffEntry has been inserted already at this Index (from Tie)
  1826. measure.addGraphicalStaffEntryAtTimestamp(graphicalStaffEntry);
  1827. } else {
  1828. measure.addGraphicalStaffEntry(graphicalStaffEntry);
  1829. }
  1830. const linkedNotes: Note[] = [];
  1831. if (sourceStaffEntry.Link !== undefined) {
  1832. sourceStaffEntry.findLinkedNotes(linkedNotes);
  1833. this.handleStaffEntryLink(graphicalStaffEntry, staffEntryLinks);
  1834. }
  1835. // check for possible OctaveShift
  1836. let octaveShiftValue: OctaveEnum = OctaveEnum.NONE;
  1837. if (openOctaveShifts[staffIndex] !== undefined) {
  1838. if (openOctaveShifts[staffIndex].getAbsoluteStartTimestamp.lte(sourceStaffEntry.AbsoluteTimestamp) &&
  1839. sourceStaffEntry.AbsoluteTimestamp.lte(openOctaveShifts[staffIndex].getAbsoluteEndTimestamp)) {
  1840. octaveShiftValue = openOctaveShifts[staffIndex].getOpenOctaveShift.Type;
  1841. }
  1842. }
  1843. // for each visible Voice create the corresponding GraphicalNotes
  1844. for (let idx: number = 0, len: number = sourceStaffEntry.VoiceEntries.length; idx < len; ++idx) {
  1845. const voiceEntry: VoiceEntry = sourceStaffEntry.VoiceEntries[idx];
  1846. // Normal Notes...
  1847. octaveShiftValue = this.handleVoiceEntry(
  1848. voiceEntry, graphicalStaffEntry,
  1849. accidentalCalculator, openLyricWords,
  1850. activeClefs[staffIndex], openTuplets,
  1851. openBeams, octaveShiftValue, linkedNotes,
  1852. sourceStaffEntry
  1853. );
  1854. }
  1855. // SourceStaffEntry has inStaff ClefInstruction -> create graphical clef
  1856. if (sourceStaffEntry.Instructions.length > 0) {
  1857. const clefInstruction: ClefInstruction = <ClefInstruction>sourceStaffEntry.Instructions[0];
  1858. MusicSheetCalculator.symbolFactory.createInStaffClef(graphicalStaffEntry, clefInstruction);
  1859. }
  1860. if (sourceStaffEntry.ChordContainers && sourceStaffEntry.ChordContainers.length > 0) {
  1861. sourceStaffEntry.ParentStaff.ParentInstrument.HasChordSymbols = true;
  1862. MusicSheetCalculator.symbolFactory.createChordSymbols(
  1863. sourceStaffEntry,
  1864. graphicalStaffEntry,
  1865. accidentalCalculator.ActiveKeyInstruction,
  1866. this.graphicalMusicSheet.ParentMusicSheet.Transpose);
  1867. }
  1868. }
  1869. }
  1870. accidentalCalculator.doCalculationsAtEndOfMeasure();
  1871. // update activeClef given at end of measure if needed
  1872. if (sourceMeasure.LastInstructionsStaffEntries[staffIndex] !== undefined) {
  1873. const lastStaffEntry: SourceStaffEntry = sourceMeasure.LastInstructionsStaffEntries[staffIndex];
  1874. for (let idx: number = 0, len: number = lastStaffEntry.Instructions.length; idx < len; ++idx) {
  1875. const abstractNotationInstruction: AbstractNotationInstruction = lastStaffEntry.Instructions[idx];
  1876. if (abstractNotationInstruction instanceof ClefInstruction) {
  1877. activeClefs[staffIndex] = <ClefInstruction>abstractNotationInstruction;
  1878. }
  1879. }
  1880. }
  1881. for (let idx: number = 0, len: number = sourceMeasure.StaffLinkedExpressions[staffIndex].length; idx < len; ++idx) {
  1882. const multiExpression: MultiExpression = sourceMeasure.StaffLinkedExpressions[staffIndex][idx];
  1883. if (multiExpression.OctaveShiftEnd !== undefined && openOctaveShifts[staffIndex] !== undefined &&
  1884. multiExpression.OctaveShiftEnd === openOctaveShifts[staffIndex].getOpenOctaveShift) {
  1885. openOctaveShifts[staffIndex] = undefined;
  1886. }
  1887. }
  1888. // check wantedStemDirections of beam notes at end of measure (e.g. for beam with grace notes)
  1889. for (const staffEntry of measure.staffEntries) {
  1890. for (const voiceEntry of staffEntry.graphicalVoiceEntries) {
  1891. this.setBeamNotesWantedStemDirections(voiceEntry.parentVoiceEntry);
  1892. }
  1893. }
  1894. // if there are no staffEntries in this measure, create a rest for the whole measure:
  1895. // check OSMDOptions.fillEmptyMeasuresWithWholeRest
  1896. if (this.rules.FillEmptyMeasuresWithWholeRest >= 1) { // fill measures with no notes given with whole rests, visible (1) or invisible (2)
  1897. if (measure.staffEntries.length === 0) {
  1898. const sourceStaffEntry: SourceStaffEntry = new SourceStaffEntry(
  1899. new VerticalSourceStaffEntryContainer(measure.parentSourceMeasure,
  1900. measure.parentSourceMeasure.AbsoluteTimestamp,
  1901. measure.parentSourceMeasure.CompleteNumberOfStaves),
  1902. staff);
  1903. const voiceEntry: VoiceEntry = new VoiceEntry(new Fraction(0, 1), staff.Voices[0], sourceStaffEntry);
  1904. const note: Note = new Note(voiceEntry, sourceStaffEntry, Fraction.createFromFraction(sourceMeasure.Duration), undefined);
  1905. note.PrintObject = this.rules.FillEmptyMeasuresWithWholeRest === FillEmptyMeasuresWithWholeRests.YesVisible;
  1906. // don't display whole rest that wasn't given in XML, only for layout/voice completion
  1907. voiceEntry.Notes.push(note);
  1908. const graphicalStaffEntry: GraphicalStaffEntry = MusicSheetCalculator.symbolFactory.createStaffEntry(sourceStaffEntry, measure);
  1909. measure.addGraphicalStaffEntry(graphicalStaffEntry);
  1910. graphicalStaffEntry.relInMeasureTimestamp = voiceEntry.Timestamp;
  1911. const gve: GraphicalVoiceEntry = MusicSheetCalculator.symbolFactory.createVoiceEntry(voiceEntry, graphicalStaffEntry);
  1912. graphicalStaffEntry.graphicalVoiceEntries.push(gve);
  1913. let graphicalNote: GraphicalNote = MusicSheetCalculator.symbolFactory.createNote(note,
  1914. gve,
  1915. new ClefInstruction(),
  1916. OctaveEnum.NONE, undefined);
  1917. const staffLineCount: number = voiceEntry.ParentSourceStaffEntry.ParentStaff.StafflineCount;
  1918. graphicalNote = MusicSheetCalculator.stafflineNoteCalculator.positionNote(graphicalNote, activeClefs[staffIndex], staffLineCount);
  1919. gve.notes.push(graphicalNote);
  1920. }
  1921. }
  1922. return measure;
  1923. }
  1924. private checkNoteForAccidental(graphicalNote: GraphicalNote, accidentalCalculator: AccidentalCalculator, activeClef: ClefInstruction,
  1925. octaveEnum: OctaveEnum): void {
  1926. let pitch: Pitch = graphicalNote.sourceNote.Pitch;
  1927. const transpose: number = this.graphicalMusicSheet.ParentMusicSheet.Transpose;
  1928. if (transpose !== 0 && graphicalNote.sourceNote.ParentStaffEntry.ParentStaff.ParentInstrument.MidiInstrumentId !== MidiInstrument.Percussion) {
  1929. pitch = graphicalNote.Transpose(
  1930. accidentalCalculator.ActiveKeyInstruction, activeClef, transpose, octaveEnum
  1931. );
  1932. }
  1933. graphicalNote.sourceNote.halfTone = pitch.getHalfTone();
  1934. accidentalCalculator.checkAccidental(graphicalNote, pitch);
  1935. }
  1936. // // needed to disable linter, as it doesn't recognize the existing usage of this method.
  1937. // // ToDo: check if a newer version doesn't have the problem.
  1938. // /* tslint:disable:no-unused-variable */
  1939. // private createStaffEntryForTieNote(measure: StaffMeasure, absoluteTimestamp: Fraction, openTie: Tie): GraphicalStaffEntry {
  1940. // /* tslint:enable:no-unused-variable */
  1941. // let graphicalStaffEntry: GraphicalStaffEntry;
  1942. // graphicalStaffEntry = MusicSheetCalculator.symbolFactory.createStaffEntry(openTie.Start.ParentStaffEntry, measure);
  1943. // graphicalStaffEntry.relInMeasureTimestamp = Fraction.minus(absoluteTimestamp, measure.parentSourceMeasure.AbsoluteTimestamp);
  1944. // this.resetYPositionForLeadSheet(graphicalStaffEntry.PositionAndShape);
  1945. // measure.addGraphicalStaffEntryAtTimestamp(graphicalStaffEntry);
  1946. // return graphicalStaffEntry;
  1947. // }
  1948. private handleStaffEntries(): void {
  1949. for (let idx: number = 0, len: number = this.graphicalMusicSheet.MeasureList.length; idx < len; ++idx) {
  1950. const measures: GraphicalMeasure[] = this.graphicalMusicSheet.MeasureList[idx];
  1951. for (let idx2: number = 0, len2: number = measures.length; idx2 < len2; ++idx2) {
  1952. const measure: GraphicalMeasure = measures[idx2];
  1953. for (const graphicalStaffEntry of measure.staffEntries) {
  1954. if (graphicalStaffEntry.parentMeasure !== undefined
  1955. && graphicalStaffEntry.graphicalVoiceEntries.length > 0
  1956. && graphicalStaffEntry.graphicalVoiceEntries[0].notes.length > 0) {
  1957. this.layoutVoiceEntries(graphicalStaffEntry);
  1958. this.layoutStaffEntry(graphicalStaffEntry);
  1959. }
  1960. }
  1961. }
  1962. }
  1963. }
  1964. private calculateSkyBottomLines(): void {
  1965. for (const musicSystem of this.musicSystems) {
  1966. for (const staffLine of musicSystem.StaffLines) {
  1967. staffLine.SkyBottomLineCalculator.calculateLines();
  1968. }
  1969. }
  1970. }
  1971. /**
  1972. * Re-adjust the x positioning of expressions.
  1973. */
  1974. protected calculateExpressionAlignements(): void {
  1975. // override
  1976. }
  1977. // does nothing for now, because layoutBeams() is an empty method
  1978. // private calculateBeams(): void {
  1979. // for (let idx2: number = 0, len2: number = this.musicSystems.length; idx2 < len2; ++idx2) {
  1980. // const musicSystem: MusicSystem = this.musicSystems[idx2];
  1981. // for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  1982. // const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  1983. // for (let idx4: number = 0, len4: number = staffLine.Measures.length; idx4 < len4; ++idx4) {
  1984. // const measure: GraphicalMeasure = staffLine.Measures[idx4];
  1985. // for (let idx5: number = 0, len5: number = measure.staffEntries.length; idx5 < len5; ++idx5) {
  1986. // const staffEntry: GraphicalStaffEntry = measure.staffEntries[idx5];
  1987. // this.layoutBeams(staffEntry);
  1988. // }
  1989. // }
  1990. // }
  1991. // }
  1992. // }
  1993. private calculateStaffEntryArticulationMarks(): void {
  1994. for (let idx2: number = 0, len2: number = this.musicSystems.length; idx2 < len2; ++idx2) {
  1995. const system: MusicSystem = this.musicSystems[idx2];
  1996. for (let idx3: number = 0, len3: number = system.StaffLines.length; idx3 < len3; ++idx3) {
  1997. const line: StaffLine = system.StaffLines[idx3];
  1998. for (let idx4: number = 0, len4: number = line.Measures.length; idx4 < len4; ++idx4) {
  1999. const measure: GraphicalMeasure = line.Measures[idx4];
  2000. for (let idx5: number = 0, len5: number = measure.staffEntries.length; idx5 < len5; ++idx5) {
  2001. const graphicalStaffEntry: GraphicalStaffEntry = measure.staffEntries[idx5];
  2002. for (let idx6: number = 0, len6: number = graphicalStaffEntry.sourceStaffEntry.VoiceEntries.length; idx6 < len6; ++idx6) {
  2003. const voiceEntry: VoiceEntry = graphicalStaffEntry.sourceStaffEntry.VoiceEntries[idx6];
  2004. if (voiceEntry.Articulations.length > 0) {
  2005. this.layoutArticulationMarks(voiceEntry.Articulations, voiceEntry, graphicalStaffEntry);
  2006. }
  2007. }
  2008. }
  2009. }
  2010. }
  2011. }
  2012. }
  2013. private calculateOrnaments(): void {
  2014. for (let idx2: number = 0, len2: number = this.musicSystems.length; idx2 < len2; ++idx2) {
  2015. const system: MusicSystem = this.musicSystems[idx2];
  2016. for (let idx3: number = 0, len3: number = system.StaffLines.length; idx3 < len3; ++idx3) {
  2017. const line: StaffLine = system.StaffLines[idx3];
  2018. for (let idx4: number = 0, len4: number = line.Measures.length; idx4 < len4; ++idx4) {
  2019. const measure: GraphicalMeasure = line.Measures[idx4];
  2020. for (let idx5: number = 0, len5: number = measure.staffEntries.length; idx5 < len5; ++idx5) {
  2021. const graphicalStaffEntry: GraphicalStaffEntry = measure.staffEntries[idx5];
  2022. for (let idx6: number = 0, len6: number = graphicalStaffEntry.sourceStaffEntry.VoiceEntries.length; idx6 < len6; ++idx6) {
  2023. const voiceEntry: VoiceEntry = graphicalStaffEntry.sourceStaffEntry.VoiceEntries[idx6];
  2024. if (voiceEntry.OrnamentContainer !== undefined) {
  2025. if (voiceEntry.hasTie() && !graphicalStaffEntry.relInMeasureTimestamp.Equals(voiceEntry.Timestamp)) {
  2026. continue;
  2027. }
  2028. this.layoutOrnament(voiceEntry.OrnamentContainer, voiceEntry, graphicalStaffEntry);
  2029. if (!(this.staffEntriesWithOrnaments.indexOf(graphicalStaffEntry) !== -1)) {
  2030. this.staffEntriesWithOrnaments.push(graphicalStaffEntry);
  2031. }
  2032. }
  2033. }
  2034. }
  2035. }
  2036. }
  2037. }
  2038. }
  2039. private optimizeRestPlacement(): void {
  2040. for (let idx2: number = 0, len2: number = this.musicSystems.length; idx2 < len2; ++idx2) {
  2041. const system: MusicSystem = this.musicSystems[idx2];
  2042. for (let idx3: number = 0, len3: number = system.StaffLines.length; idx3 < len3; ++idx3) {
  2043. const line: StaffLine = system.StaffLines[idx3];
  2044. for (let idx4: number = 0, len4: number = line.Measures.length; idx4 < len4; ++idx4) {
  2045. const measure: GraphicalMeasure = line.Measures[idx4];
  2046. for (let idx5: number = 0, len5: number = measure.staffEntries.length; idx5 < len5; ++idx5) {
  2047. const graphicalStaffEntry: GraphicalStaffEntry = measure.staffEntries[idx5];
  2048. this.optimizeRestNotePlacement(graphicalStaffEntry, measure);
  2049. }
  2050. }
  2051. }
  2052. }
  2053. }
  2054. private calculateTwoRestNotesPlacementWithCollisionDetection(graphicalStaffEntry: GraphicalStaffEntry): void {
  2055. const firstRestNote: GraphicalNote = graphicalStaffEntry.graphicalVoiceEntries[0].notes[0];
  2056. const secondRestNote: GraphicalNote = graphicalStaffEntry.graphicalVoiceEntries[1].notes[0];
  2057. secondRestNote.PositionAndShape.RelativePosition = new PointF2D(0.0, 2.5);
  2058. graphicalStaffEntry.PositionAndShape.calculateAbsolutePositionsRecursiveWithoutTopelement();
  2059. firstRestNote.PositionAndShape.computeNonOverlappingPositionWithMargin(
  2060. graphicalStaffEntry.PositionAndShape, ColDirEnum.Up,
  2061. new PointF2D(0.0, secondRestNote.PositionAndShape.RelativePosition.y)
  2062. );
  2063. const relative: PointF2D = firstRestNote.PositionAndShape.RelativePosition;
  2064. relative.y -= 1.0;
  2065. firstRestNote.PositionAndShape.RelativePosition = relative;
  2066. graphicalStaffEntry.PositionAndShape.calculateBoundingBox();
  2067. }
  2068. private calculateRestNotePlacementWithCollisionDetectionFromGraphicalNote(graphicalStaffEntry: GraphicalStaffEntry): void {
  2069. let restNote: GraphicalNote;
  2070. let graphicalNotes: GraphicalNote[];
  2071. if (graphicalStaffEntry.graphicalVoiceEntries[0].notes[0].sourceNote.isRest()) {
  2072. restNote = graphicalStaffEntry.graphicalVoiceEntries[0].notes[0];
  2073. graphicalNotes = graphicalStaffEntry.graphicalVoiceEntries[1].notes;
  2074. } else {
  2075. graphicalNotes = graphicalStaffEntry.graphicalVoiceEntries[0].notes;
  2076. restNote = graphicalStaffEntry.graphicalVoiceEntries[1].notes[0];
  2077. }
  2078. //restNote.parallelVoiceEntryNotes = graphicalNotes; // TODO maybe save potentially colliding notes, check them in VexFlowConverter.StaveNote
  2079. let collision: boolean = false;
  2080. graphicalStaffEntry.PositionAndShape.calculateAbsolutePositionsRecursiveWithoutTopelement();
  2081. for (let idx: number = 0, len: number = graphicalNotes.length; idx < len; ++idx) {
  2082. const graphicalNote: GraphicalNote = graphicalNotes[idx];
  2083. if (restNote.PositionAndShape.marginCollisionDetection(graphicalNote.PositionAndShape)) {
  2084. // TODO bounding box of graphical note isn't set correctly yet.
  2085. // we could do manual collision checking here
  2086. collision = true;
  2087. break;
  2088. }
  2089. }
  2090. if (collision) {
  2091. if (restNote.sourceNote.ParentVoiceEntry.ParentVoice instanceof LinkedVoice) {
  2092. const bottomBorder: number = graphicalNotes[0].PositionAndShape.BorderMarginBottom + graphicalNotes[0].PositionAndShape.RelativePosition.y;
  2093. restNote.PositionAndShape.RelativePosition = new PointF2D(0.0, bottomBorder - restNote.PositionAndShape.BorderMarginTop + 0.5);
  2094. } else {
  2095. const last: GraphicalNote = graphicalNotes[graphicalNotes.length - 1];
  2096. const topBorder: number = last.PositionAndShape.BorderMarginTop + last.PositionAndShape.RelativePosition.y;
  2097. if (graphicalNotes[0].sourceNote.ParentVoiceEntry.ParentVoice instanceof LinkedVoice) {
  2098. restNote.PositionAndShape.RelativePosition = new PointF2D(0.0, topBorder - restNote.PositionAndShape.BorderMarginBottom - 0.5);
  2099. } else {
  2100. const bottomBorder: number = graphicalNotes[0].PositionAndShape.BorderMarginBottom + graphicalNotes[0].PositionAndShape.RelativePosition.y;
  2101. if (bottomBorder < 2.0) {
  2102. restNote.PositionAndShape.RelativePosition = new PointF2D(0.0, bottomBorder - restNote.PositionAndShape.BorderMarginTop + 0.5);
  2103. } else {
  2104. restNote.PositionAndShape.RelativePosition = new PointF2D(0.0, topBorder - restNote.PositionAndShape.BorderMarginBottom - 0.0);
  2105. }
  2106. }
  2107. }
  2108. }
  2109. graphicalStaffEntry.PositionAndShape.calculateBoundingBox();
  2110. }
  2111. private calculateTieCurves(): void {
  2112. for (let idx2: number = 0, len2: number = this.musicSystems.length; idx2 < len2; ++idx2) {
  2113. const musicSystem: MusicSystem = this.musicSystems[idx2];
  2114. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  2115. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  2116. for (let idx4: number = 0, len5: number = staffLine.Measures.length; idx4 < len5; ++idx4) {
  2117. const measure: GraphicalMeasure = staffLine.Measures[idx4];
  2118. for (let idx6: number = 0, len6: number = measure.staffEntries.length; idx6 < len6; ++idx6) {
  2119. const staffEntry: GraphicalStaffEntry = measure.staffEntries[idx6];
  2120. const graphicalTies: GraphicalTie[] = staffEntry.GraphicalTies;
  2121. for (let idx7: number = 0, len7: number = graphicalTies.length; idx7 < len7; ++idx7) {
  2122. const graphicalTie: GraphicalTie = graphicalTies[idx7];
  2123. if (graphicalTie.StartNote !== undefined && graphicalTie.StartNote.parentVoiceEntry.parentStaffEntry === staffEntry) {
  2124. const tieIsAtSystemBreak: boolean = (
  2125. graphicalTie.StartNote.parentVoiceEntry.parentStaffEntry.parentMeasure.ParentStaffLine !==
  2126. graphicalTie.EndNote.parentVoiceEntry.parentStaffEntry.parentMeasure.ParentStaffLine
  2127. );
  2128. this.layoutGraphicalTie(graphicalTie, tieIsAtSystemBreak);
  2129. }
  2130. }
  2131. }
  2132. }
  2133. }
  2134. }
  2135. }
  2136. private calculateLyricsPosition(): void {
  2137. const lyricStaffEntriesDict: Dictionary<StaffLine, GraphicalStaffEntry[]> = new Dictionary<StaffLine, GraphicalStaffEntry[]>();
  2138. // sort the lyriceVerseNumbers for every Instrument that has Lyrics
  2139. for (let idx: number = 0, len: number = this.graphicalMusicSheet.ParentMusicSheet.Instruments.length; idx < len; ++idx) {
  2140. const instrument: Instrument = this.graphicalMusicSheet.ParentMusicSheet.Instruments[idx];
  2141. if (instrument.HasLyrics && instrument.LyricVersesNumbers.length > 0) {
  2142. instrument.LyricVersesNumbers.sort();
  2143. }
  2144. }
  2145. // first calc lyrics text positions
  2146. for (let idx2: number = 0, len2: number = this.musicSystems.length; idx2 < len2; ++idx2) {
  2147. const musicSystem: MusicSystem = this.musicSystems[idx2];
  2148. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  2149. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  2150. const lyricsStaffEntries: GraphicalStaffEntry[] =
  2151. this.calculateSingleStaffLineLyricsPosition(staffLine, staffLine.ParentStaff.ParentInstrument.LyricVersesNumbers);
  2152. lyricStaffEntriesDict.setValue(staffLine, lyricsStaffEntries);
  2153. this.calculateLyricsExtendsAndDashes(lyricStaffEntriesDict.getValue(staffLine));
  2154. }
  2155. }
  2156. // then fill in the lyric word dashes and lyrics extends/underscores
  2157. for (let idx2: number = 0, len2: number = this.musicSystems.length; idx2 < len2; ++idx2) {
  2158. const musicSystem: MusicSystem = this.musicSystems[idx2];
  2159. for (let idx3: number = 0, len3: number = musicSystem.StaffLines.length; idx3 < len3; ++idx3) {
  2160. const staffLine: StaffLine = musicSystem.StaffLines[idx3];
  2161. this.calculateLyricsExtendsAndDashes(lyricStaffEntriesDict.getValue(staffLine));
  2162. }
  2163. }
  2164. }
  2165. /**
  2166. * This method calculates the dashes within the syllables of a LyricWord
  2167. * @param lyricEntry
  2168. */
  2169. private calculateSingleLyricWord(lyricEntry: GraphicalLyricEntry): void {
  2170. // const skyBottomLineCalculator: SkyBottomLineCalculator = new SkyBottomLineCalculator (this.rules);
  2171. const graphicalLyricWord: GraphicalLyricWord = lyricEntry.ParentLyricWord;
  2172. const index: number = graphicalLyricWord.GraphicalLyricsEntries.indexOf(lyricEntry);
  2173. let nextLyricEntry: GraphicalLyricEntry = undefined;
  2174. if (index >= 0) {
  2175. nextLyricEntry = graphicalLyricWord.GraphicalLyricsEntries[index + 1];
  2176. }
  2177. if (nextLyricEntry === undefined) {
  2178. return;
  2179. }
  2180. const startStaffLine: StaffLine = <StaffLine>lyricEntry.StaffEntryParent.parentMeasure.ParentStaffLine;
  2181. const nextStaffLine: StaffLine = <StaffLine>nextLyricEntry.StaffEntryParent.parentMeasure.ParentStaffLine;
  2182. const startStaffEntry: GraphicalStaffEntry = lyricEntry.StaffEntryParent;
  2183. const endStaffentry: GraphicalStaffEntry = nextLyricEntry.StaffEntryParent;
  2184. // if on the same StaffLine
  2185. if (lyricEntry.StaffEntryParent.parentMeasure.ParentStaffLine === nextLyricEntry.StaffEntryParent.parentMeasure.ParentStaffLine) {
  2186. // start- and End margins from the text Labels
  2187. const startX: number = startStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x +
  2188. startStaffEntry.PositionAndShape.RelativePosition.x +
  2189. lyricEntry.GraphicalLabel.PositionAndShape.RelativePosition.x +
  2190. lyricEntry.GraphicalLabel.PositionAndShape.BorderMarginRight;
  2191. const endX: number = endStaffentry.parentMeasure.PositionAndShape.RelativePosition.x +
  2192. endStaffentry.PositionAndShape.RelativePosition.x +
  2193. lyricEntry.GraphicalLabel.PositionAndShape.RelativePosition.x +
  2194. nextLyricEntry.GraphicalLabel.PositionAndShape.BorderMarginLeft;
  2195. const y: number = lyricEntry.GraphicalLabel.PositionAndShape.RelativePosition.y;
  2196. let numberOfDashes: number = 1;
  2197. if ((endX - startX) > this.rules.MinimumDistanceBetweenDashes * 3) {
  2198. // *3: need distance between word to first dash, dash to dash, dash to next word
  2199. numberOfDashes = Math.floor((endX - startX) / this.rules.MinimumDistanceBetweenDashes) - 1;
  2200. }
  2201. // check distance and create the adequate number of Dashes
  2202. if (numberOfDashes === 1) {
  2203. // distance between the two GraphicalLyricEntries is big for only one Dash, position in the middle
  2204. this.calculateSingleDashForLyricWord(startStaffLine, startX, endX, y);
  2205. } else {
  2206. // distance is big enough for more Dashes
  2207. // calculate the adequate number of Dashes from the distance between the two LyricEntries
  2208. // distance between the Dashes should be equal
  2209. this.calculateDashes(startStaffLine, startX, endX, y);
  2210. }
  2211. } else {
  2212. // start and end on different StaffLines
  2213. // start margin from the text Label until the End of StaffLine
  2214. const startX: number = startStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x +
  2215. startStaffEntry.PositionAndShape.RelativePosition.x +
  2216. lyricEntry.GraphicalLabel.PositionAndShape.BorderMarginRight;
  2217. const lastGraphicalMeasure: GraphicalMeasure = startStaffLine.Measures[startStaffLine.Measures.length - 1];
  2218. const endX: number = lastGraphicalMeasure.PositionAndShape.RelativePosition.x + lastGraphicalMeasure.PositionAndShape.Size.width;
  2219. let y: number = lyricEntry.GraphicalLabel.PositionAndShape.RelativePosition.y;
  2220. // calculate Dashes for the first StaffLine
  2221. this.calculateDashes(startStaffLine, startX, endX, y);
  2222. // calculate Dashes for the second StaffLine (only if endStaffEntry isn't the first StaffEntry of the StaffLine)
  2223. if (nextStaffLine && // check for undefined objects e.g. when drawingRange given
  2224. nextStaffLine.Measures[0] &&
  2225. endStaffentry.parentMeasure.ParentStaffLine &&
  2226. !(endStaffentry === endStaffentry.parentMeasure.staffEntries[0] &&
  2227. endStaffentry.parentMeasure === endStaffentry.parentMeasure.ParentStaffLine.Measures[0])) {
  2228. const secondStartX: number = nextStaffLine.Measures[0].staffEntries[0].PositionAndShape.RelativePosition.x;
  2229. const secondEndX: number = endStaffentry.parentMeasure.PositionAndShape.RelativePosition.x +
  2230. endStaffentry.PositionAndShape.RelativePosition.x +
  2231. nextLyricEntry.GraphicalLabel.PositionAndShape.BorderMarginLeft;
  2232. y = nextLyricEntry.GraphicalLabel.PositionAndShape.RelativePosition.y;
  2233. this.calculateDashes(nextStaffLine, secondStartX, secondEndX, y);
  2234. }
  2235. }
  2236. }
  2237. /**
  2238. * This method calculates Dashes for a LyricWord.
  2239. * @param staffLine
  2240. * @param startX
  2241. * @param endX
  2242. * @param y
  2243. */
  2244. private calculateDashes(staffLine: StaffLine, startX: number, endX: number, y: number): void {
  2245. let distance: number = endX - startX;
  2246. if (distance < this.rules.MinimumDistanceBetweenDashes * 3) {
  2247. this.calculateSingleDashForLyricWord(staffLine, startX, endX, y);
  2248. } else {
  2249. // enough distance for more Dashes
  2250. const numberOfDashes: number = Math.floor(distance / this.rules.MinimumDistanceBetweenDashes) - 1;
  2251. const distanceBetweenDashes: number = distance / (numberOfDashes + 1);
  2252. let counter: number = 0;
  2253. startX += distanceBetweenDashes;
  2254. endX -= distanceBetweenDashes;
  2255. while (counter <= Math.floor(numberOfDashes / 2.0) && endX > startX) {
  2256. distance = this.calculateRightAndLeftDashesForLyricWord(staffLine, startX, endX, y);
  2257. startX += distanceBetweenDashes;
  2258. endX -= distanceBetweenDashes;
  2259. counter++;
  2260. }
  2261. // if the remaining distance isn't big enough for two Dashes,
  2262. // but long enough for a middle dash inbetween,
  2263. // then put the last Dash in the middle of the remaining distance
  2264. if (distance > distanceBetweenDashes * 2) {
  2265. this.calculateSingleDashForLyricWord(staffLine, startX, endX, y);
  2266. }
  2267. }
  2268. }
  2269. /**
  2270. * This method calculates a single Dash for a LyricWord, positioned in the middle of the given distance.
  2271. * @param {StaffLine} staffLine
  2272. * @param {number} startX
  2273. * @param {number} endX
  2274. * @param {number} y
  2275. */
  2276. private calculateSingleDashForLyricWord(staffLine: StaffLine, startX: number, endX: number, y: number): void {
  2277. const label: Label = new Label("-");
  2278. const dash: GraphicalLabel = new GraphicalLabel(
  2279. label, this.rules.LyricsHeight, TextAlignmentEnum.CenterBottom, this.rules);
  2280. dash.setLabelPositionAndShapeBorders();
  2281. staffLine.LyricsDashes.push(dash);
  2282. if (this.staffLinesWithLyricWords.indexOf(staffLine) === -1) {
  2283. this.staffLinesWithLyricWords.push(staffLine);
  2284. }
  2285. dash.PositionAndShape.Parent = staffLine.PositionAndShape;
  2286. const relative: PointF2D = new PointF2D(startX + (endX - startX) / 2, y);
  2287. dash.PositionAndShape.RelativePosition = relative;
  2288. }
  2289. /**
  2290. * Layouts the underscore line when a lyric entry is marked as extend
  2291. * @param {GraphicalLyricEntry} lyricEntry
  2292. */
  2293. private calculateLyricExtend(lyricEntry: GraphicalLyricEntry): void {
  2294. let startY: number = lyricEntry.GraphicalLabel.PositionAndShape.RelativePosition.y;
  2295. const startStaffEntry: GraphicalStaffEntry = lyricEntry.StaffEntryParent;
  2296. const startStaffLine: StaffLine = startStaffEntry.parentMeasure.ParentStaffLine;
  2297. // find endstaffEntry and staffLine
  2298. let endStaffEntry: GraphicalStaffEntry = undefined;
  2299. let endStaffLine: StaffLine = undefined;
  2300. const staffIndex: number = startStaffEntry.parentMeasure.ParentStaff.idInMusicSheet;
  2301. for (let index: number = startStaffEntry.parentVerticalContainer.Index + 1;
  2302. index < this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length;
  2303. ++index) {
  2304. const gse: GraphicalStaffEntry = this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[index].StaffEntries[staffIndex];
  2305. if (gse === undefined) {
  2306. continue;
  2307. }
  2308. if (gse.hasOnlyRests()) {
  2309. break;
  2310. }
  2311. if (gse.LyricsEntries.length > 0) {
  2312. break;
  2313. }
  2314. endStaffEntry = gse;
  2315. endStaffLine = <StaffLine>endStaffEntry.parentMeasure.ParentStaffLine;
  2316. }
  2317. if (endStaffEntry === undefined) {
  2318. return;
  2319. }
  2320. // if on the same StaffLine
  2321. if (startStaffLine === endStaffLine) {
  2322. // start- and End margins from the text Labels
  2323. const startX: number = startStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x +
  2324. startStaffEntry.PositionAndShape.RelativePosition.x +
  2325. lyricEntry.GraphicalLabel.PositionAndShape.BorderMarginRight;
  2326. // + startStaffLine.PositionAndShape.AbsolutePosition.x; // doesn't work, done in drawer
  2327. const endX: number = endStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x +
  2328. endStaffEntry.PositionAndShape.RelativePosition.x +
  2329. endStaffEntry.PositionAndShape.BorderMarginRight;
  2330. // + endStaffLine.PositionAndShape.AbsolutePosition.x; // doesn't work, done in drawer
  2331. // TODO maybe add half-width of following note.
  2332. // though we don't have the vexflow note's bbox yet and extend layouting is unconstrained,
  2333. // we have more room for spacing without it.
  2334. // needed in order to line up with the Label's text bottom line (is the y position of the underscore)
  2335. startY -= lyricEntry.GraphicalLabel.PositionAndShape.Size.height / 4;
  2336. // create a Line (as underscore after the LyricLabel's End)
  2337. this.calculateSingleLyricWordWithUnderscore(startStaffLine, startX, endX, startY);
  2338. } else { // start and end on different StaffLines
  2339. // start margin from the text Label until the End of StaffLine
  2340. const lastMeasureBb: BoundingBox = startStaffLine.Measures[startStaffLine.Measures.length - 1].PositionAndShape;
  2341. const startX: number = startStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x +
  2342. startStaffEntry.PositionAndShape.RelativePosition.x +
  2343. lyricEntry.GraphicalLabel.PositionAndShape.BorderMarginRight;
  2344. const endX: number = lastMeasureBb.RelativePosition.x +
  2345. lastMeasureBb.Size.width;
  2346. // needed in order to line up with the Label's text bottom line
  2347. startY -= lyricEntry.GraphicalLabel.PositionAndShape.Size.height / 4;
  2348. // first Underscore until the StaffLine's End
  2349. this.calculateSingleLyricWordWithUnderscore(startStaffLine, startX, endX, startY);
  2350. if (endStaffEntry === undefined) {
  2351. return;
  2352. }
  2353. // second Underscore in the endStaffLine until endStaffEntry (if endStaffEntry isn't the first StaffEntry of the StaffLine))
  2354. if (!(endStaffEntry === endStaffEntry.parentMeasure.staffEntries[0] &&
  2355. endStaffEntry.parentMeasure === endStaffEntry.parentMeasure.ParentStaffLine.Measures[0])) {
  2356. const secondStartX: number = endStaffLine.Measures[0].staffEntries[0].PositionAndShape.RelativePosition.x;
  2357. const secondEndX: number = endStaffEntry.parentMeasure.PositionAndShape.RelativePosition.x +
  2358. endStaffEntry.PositionAndShape.RelativePosition.x +
  2359. endStaffEntry.PositionAndShape.BorderMarginRight;
  2360. this.calculateSingleLyricWordWithUnderscore(endStaffLine, secondStartX, secondEndX, startY);
  2361. }
  2362. }
  2363. }
  2364. /**
  2365. * This method calculates a single underscoreLine.
  2366. * @param staffLine
  2367. * @param startX
  2368. * @param end
  2369. * @param y
  2370. */
  2371. private calculateSingleLyricWordWithUnderscore(staffLine: StaffLine, startX: number, endX: number, y: number): void {
  2372. const lineStart: PointF2D = new PointF2D(startX, y);
  2373. const lineEnd: PointF2D = new PointF2D(endX, y);
  2374. const graphicalLine: GraphicalLine = new GraphicalLine(lineStart, lineEnd, this.rules.LyricUnderscoreLineWidth);
  2375. staffLine.LyricLines.push(graphicalLine);
  2376. if (this.staffLinesWithLyricWords.indexOf(staffLine) === -1) {
  2377. this.staffLinesWithLyricWords.push(staffLine);
  2378. }
  2379. }
  2380. /**
  2381. * This method calculates two Dashes for a LyricWord, positioned at the the two ends of the given distance.
  2382. * @param {StaffLine} staffLine
  2383. * @param {number} startX
  2384. * @param {number} endX
  2385. * @param {number} y
  2386. * @returns {number}
  2387. */
  2388. private calculateRightAndLeftDashesForLyricWord(staffLine: StaffLine, startX: number, endX: number, y: number): number {
  2389. const leftLabel: Label = new Label("-");
  2390. const leftDash: GraphicalLabel = new GraphicalLabel(
  2391. leftLabel, this.rules.LyricsHeight, TextAlignmentEnum.CenterBottom, this.rules);
  2392. leftDash.setLabelPositionAndShapeBorders();
  2393. staffLine.LyricsDashes.push(leftDash);
  2394. if (this.staffLinesWithLyricWords.indexOf(staffLine) === -1) {
  2395. this.staffLinesWithLyricWords.push(staffLine);
  2396. }
  2397. leftDash.PositionAndShape.Parent = staffLine.PositionAndShape;
  2398. const leftDashRelative: PointF2D = new PointF2D(startX, y);
  2399. leftDash.PositionAndShape.RelativePosition = leftDashRelative;
  2400. const rightLabel: Label = new Label("-");
  2401. const rightDash: GraphicalLabel = new GraphicalLabel(
  2402. rightLabel, this.rules.LyricsHeight, TextAlignmentEnum.CenterBottom, this.rules);
  2403. rightDash.setLabelPositionAndShapeBorders();
  2404. staffLine.LyricsDashes.push(rightDash);
  2405. rightDash.PositionAndShape.Parent = staffLine.PositionAndShape;
  2406. const rightDashRelative: PointF2D = new PointF2D(endX, y);
  2407. rightDash.PositionAndShape.RelativePosition = rightDashRelative;
  2408. return (rightDash.PositionAndShape.RelativePosition.x - leftDash.PositionAndShape.RelativePosition.x);
  2409. }
  2410. private calculateDynamicExpressions(): void {
  2411. const maxIndex: number = Math.min(this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length - 1, this.rules.MaxMeasureToDrawIndex);
  2412. const minIndex: number = Math.min(this.rules.MinMeasureToDrawIndex, this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length);
  2413. for (let i: number = minIndex; i <= maxIndex; i++) {
  2414. const sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  2415. for (let j: number = 0; j < sourceMeasure.StaffLinkedExpressions.length; j++) {
  2416. if (this.graphicalMusicSheet.MeasureList[i][j].ParentStaff.ParentInstrument.Visible) {
  2417. for (let k: number = 0; k < sourceMeasure.StaffLinkedExpressions[j].length; k++) {
  2418. if (sourceMeasure.StaffLinkedExpressions[j][k].InstantaneousDynamic !== undefined ||
  2419. (sourceMeasure.StaffLinkedExpressions[j][k].StartingContinuousDynamic !== undefined &&
  2420. sourceMeasure.StaffLinkedExpressions[j][k].StartingContinuousDynamic.StartMultiExpression ===
  2421. sourceMeasure.StaffLinkedExpressions[j][k] && sourceMeasure.StaffLinkedExpressions[j][k].UnknownList.length === 0)
  2422. ) {
  2423. this.calculateDynamicExpressionsForMultiExpression(sourceMeasure.StaffLinkedExpressions[j][k], i, j);
  2424. }
  2425. }
  2426. }
  2427. }
  2428. }
  2429. }
  2430. private calculateOctaveShifts(): void {
  2431. for (let i: number = 0; i < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; i++) {
  2432. const sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  2433. for (let j: number = 0; j < sourceMeasure.StaffLinkedExpressions.length; j++) {
  2434. if (this.graphicalMusicSheet.MeasureList[i][j].ParentStaff.ParentInstrument.Visible) {
  2435. for (let k: number = 0; k < sourceMeasure.StaffLinkedExpressions[j].length; k++) {
  2436. if ((sourceMeasure.StaffLinkedExpressions[j][k].OctaveShiftStart !== undefined)) {
  2437. this.calculateSingleOctaveShift(sourceMeasure, sourceMeasure.StaffLinkedExpressions[j][k], i, j);
  2438. }
  2439. }
  2440. }
  2441. }
  2442. }
  2443. }
  2444. private getFirstLeftNotNullStaffEntryFromContainer(horizontalIndex: number, verticalIndex: number, multiStaffInstrument: boolean): GraphicalStaffEntry {
  2445. if (this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[horizontalIndex].StaffEntries[verticalIndex] !== undefined) {
  2446. return this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[horizontalIndex].StaffEntries[verticalIndex];
  2447. }
  2448. for (let i: number = horizontalIndex - 1; i >= 0; i--) {
  2449. if (this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].StaffEntries[verticalIndex] !== undefined) {
  2450. return this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].StaffEntries[verticalIndex];
  2451. }
  2452. }
  2453. return undefined;
  2454. }
  2455. private getFirstRightNotNullStaffEntryFromContainer(horizontalIndex: number, verticalIndex: number, multiStaffInstrument: boolean): GraphicalStaffEntry {
  2456. if (this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[horizontalIndex].StaffEntries[verticalIndex] !== undefined) {
  2457. return this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[horizontalIndex].StaffEntries[verticalIndex];
  2458. }
  2459. for (let i: number = horizontalIndex + 1; i < this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers.length; i++) {
  2460. if (this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].StaffEntries[verticalIndex] !== undefined) {
  2461. return this.graphicalMusicSheet.VerticalGraphicalStaffEntryContainers[i].StaffEntries[verticalIndex];
  2462. }
  2463. }
  2464. return undefined;
  2465. }
  2466. private calculateWordRepetitionInstructions(): void {
  2467. for (let i: number = 0; i < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; i++) {
  2468. const sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  2469. for (let idx: number = 0, len: number = sourceMeasure.FirstRepetitionInstructions.length; idx < len; ++idx) {
  2470. const instruction: RepetitionInstruction = sourceMeasure.FirstRepetitionInstructions[idx];
  2471. this.calculateWordRepetitionInstruction(instruction, i);
  2472. }
  2473. for (let idx: number = 0, len: number = sourceMeasure.LastRepetitionInstructions.length; idx < len; ++idx) {
  2474. const instruction: RepetitionInstruction = sourceMeasure.LastRepetitionInstructions[idx];
  2475. this.calculateWordRepetitionInstruction(instruction, i);
  2476. }
  2477. }
  2478. }
  2479. private calculateRepetitionEndings(): void {
  2480. const musicsheet: MusicSheet = this.graphicalMusicSheet.ParentMusicSheet;
  2481. for (let idx: number = 0, len: number = musicsheet.Repetitions.length; idx < len; ++idx) {
  2482. const repetition: Repetition = musicsheet.Repetitions[idx];
  2483. this.calcGraphicalRepetitionEndingsRecursively(repetition);
  2484. }
  2485. }
  2486. private calculateTempoExpressions(): void {
  2487. const maxIndex: number = Math.min(this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length - 1, this.rules.MaxMeasureToDrawIndex);
  2488. const minIndex: number = this.rules.MinMeasureToDrawIndex;
  2489. for (let i: number = minIndex; i <= maxIndex; i++) {
  2490. const sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  2491. for (let j: number = 0; j < sourceMeasure.TempoExpressions.length; j++) {
  2492. this.calculateTempoExpressionsForMultiTempoExpression(sourceMeasure, sourceMeasure.TempoExpressions[j], i);
  2493. }
  2494. }
  2495. }
  2496. private calculateMoodAndUnknownExpressions(): void {
  2497. for (let i: number = 0; i < this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.length; i++) {
  2498. const sourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures[i];
  2499. for (let j: number = 0; j < sourceMeasure.StaffLinkedExpressions.length; j++) {
  2500. if (this.graphicalMusicSheet.MeasureList[i][j].ParentStaff.ParentInstrument.Visible) {
  2501. for (let k: number = 0; k < sourceMeasure.StaffLinkedExpressions[j].length; k++) {
  2502. if ((sourceMeasure.StaffLinkedExpressions[j][k].MoodList.length > 0) ||
  2503. (sourceMeasure.StaffLinkedExpressions[j][k].UnknownList.length > 0)) {
  2504. this.calculateMoodAndUnknownExpression(sourceMeasure.StaffLinkedExpressions[j][k], i, j);
  2505. }
  2506. }
  2507. }
  2508. }
  2509. }
  2510. }
  2511. /**
  2512. * Calculates the desired stem direction depending on the number (or type) of voices.
  2513. * If more than one voice is there, the main voice (typically the first or upper voice) will get stem up direction.
  2514. * The others get stem down direction.
  2515. * @param voiceEntry the voiceEntry for which the stem direction has to be calculated
  2516. */
  2517. private calculateStemDirectionFromVoices(voiceEntry: VoiceEntry): void {
  2518. // Stem direction calculation:
  2519. const hasLink: boolean = voiceEntry.ParentSourceStaffEntry.Link !== undefined;
  2520. if (hasLink) {
  2521. // in case of StaffEntryLink don't check mainVoice / linkedVoice
  2522. if (voiceEntry === voiceEntry.ParentSourceStaffEntry.VoiceEntries[0]) {
  2523. // set stem up:
  2524. voiceEntry.WantedStemDirection = StemDirectionType.Up;
  2525. return;
  2526. } else {
  2527. // set stem down:
  2528. voiceEntry.WantedStemDirection = StemDirectionType.Down;
  2529. return;
  2530. }
  2531. } else {
  2532. if (voiceEntry.ParentVoice instanceof LinkedVoice) {
  2533. // Linked voice: set stem down:
  2534. voiceEntry.WantedStemDirection = StemDirectionType.Down;
  2535. } else {
  2536. // if this voiceEntry belongs to the mainVoice:
  2537. // check first that there are also more voices present:
  2538. if (voiceEntry.ParentSourceStaffEntry.VoiceEntries.length > 1) {
  2539. // as this voiceEntry belongs to the mainVoice: stem Up
  2540. voiceEntry.WantedStemDirection = StemDirectionType.Up;
  2541. }
  2542. }
  2543. }
  2544. // setBeamNotesWantedStemDirections() will be called at end of measure (createGraphicalMeasure)
  2545. }
  2546. /** Sets a voiceEntry's stem direction to one already set in other notes in its beam, if it has one. */
  2547. private setBeamNotesWantedStemDirections(voiceEntry: VoiceEntry): void {
  2548. if (voiceEntry.WantedStemDirection === StemDirectionType.Undefined &&
  2549. voiceEntry.Notes.length > 0) {
  2550. const beam: Beam = voiceEntry.Notes[0].NoteBeam;
  2551. if (beam !== undefined) {
  2552. // if there is a beam, find any already set stemDirection in the beam:
  2553. for (const note of beam.Notes) {
  2554. if (note.ParentVoiceEntry === voiceEntry) {
  2555. continue;
  2556. } else if (note.ParentVoiceEntry.WantedStemDirection !== StemDirectionType.Undefined) {
  2557. // set the stem direction
  2558. voiceEntry.WantedStemDirection = note.ParentVoiceEntry.WantedStemDirection;
  2559. break;
  2560. }
  2561. }
  2562. }
  2563. }
  2564. }
  2565. }