MusicSheetCalculator.ts 151 KB

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