MusicSheetCalculator.ts 159 KB

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