MusicSheetCalculator.ts 156 KB

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