MusicSheetCalculator.ts 164 KB

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