MusicSheetCalculator.ts 155 KB

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