MusicSheetCalculator.ts 169 KB

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