MusicSheetCalculator.ts 155 KB

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