MusicSheetCalculator.ts 159 KB

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