MusicSheetCalculator.ts 129 KB

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