MusicSheetCalculator.ts 160 KB

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