MusicSystemBuilder.ts 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. import {StaffMeasure} from "./StaffMeasure";
  2. import {GraphicalMusicPage} from "./GraphicalMusicPage";
  3. import {EngravingRules} from "./EngravingRules";
  4. import {RhythmInstruction} from "../VoiceData/Instructions/RhythmInstruction";
  5. import {KeyInstruction} from "../VoiceData/Instructions/KeyInstruction";
  6. import {ClefInstruction} from "../VoiceData/Instructions/ClefInstruction";
  7. import {SourceMeasure} from "../VoiceData/SourceMeasure";
  8. import {MusicSystem} from "./MusicSystem";
  9. import {BoundingBox} from "./BoundingBox";
  10. import {Staff} from "../VoiceData/Staff";
  11. import {MusicSheet} from "../MusicSheet";
  12. import {Instrument} from "../Instrument";
  13. import {PointF_2D} from "../../Common/DataObjects/PointF_2D";
  14. import {StaffLine} from "./StaffLine";
  15. import {GraphicalLine} from "./GraphicalLine";
  16. import {SourceStaffEntry} from "../VoiceData/SourceStaffEntry";
  17. import {AbstractNotationInstruction} from "../VoiceData/Instructions/AbstractNotationInstruction";
  18. import {SystemLinesEnum} from "./SystemLinesEnum";
  19. export class MusicSystemBuilder {
  20. private measureList: List<List<StaffMeasure>>;
  21. private graphicalMusicSheet: GraphicalMusicSheet;
  22. private currentMusicPage: GraphicalMusicPage;
  23. private currentPageHeight: number;
  24. private currentSystemParams: SystemBuildParameters;
  25. private numberOfVisibleStaffLines: number;
  26. private rules: EngravingRules;
  27. private measureListIndex: number;
  28. private visibleStaffIndices: number[];
  29. private activeRhythm: RhythmInstruction[];
  30. private activeKeys: KeyInstruction[];
  31. private activeClefs: ClefInstruction[];
  32. private globalSystemIndex: number = 0;
  33. private leadSheet: boolean = false;
  34. private symbolFactory: IGraphicalSymbolFactory;
  35. public initialize(graphicalMusicSheet: GraphicalMusicSheet, measureList: List<List<StaffMeasure>>,
  36. numberOfStaffLines: number, symbolFactory: IGraphicalSymbolFactory): void {
  37. this.leadSheet = graphicalMusicSheet.LeadSheet;
  38. this.graphicalMusicSheet = graphicalMusicSheet;
  39. this.rules = this.graphicalMusicSheet.ParentMusicSheet.Rules;
  40. this.measureList = measureList;
  41. this.symbolFactory = symbolFactory;
  42. this.currentMusicPage = this.createMusicPage();
  43. this.currentPageHeight = 0.0f;
  44. this.numberOfVisibleStaffLines = numberOfStaffLines;
  45. this.activeRhythm = new Array(this.numberOfVisibleStaffLines);
  46. this.activeKeys = new Array(this.numberOfVisibleStaffLines);
  47. this.activeClefs = new Array(this.numberOfVisibleStaffLines);
  48. initializeActiveInstructions(this.measureList[0]);
  49. }
  50. public buildMusicSystems(): void {
  51. var previousMeasureEndsSystem: boolean = false;
  52. var systemMaxWidth: number = this.getFullPageSystemWidth();
  53. this.measureListIndex = 0;
  54. this.currentSystemParams = new SystemBuildParameters();
  55. this.currentSystemParams.currentSystem = this.initMusicSystem();
  56. this.layoutSystemStaves();
  57. this.currentSystemParams.currentSystem.createMusicSystemLabel(this.rules.InstrumentLabelTextHeight,
  58. this.rules.SystemLabelsRightMargin,
  59. this.rules.LabelMarginBorderFactor);
  60. this.currentPageHeight += this.currentSystemParams.currentSystem.PositionAndShape.RelativePosition.Y;
  61. var numberOfMeasures: number = this.measureList.Count(m => m.Any());
  62. while (this.measureListIndex < numberOfMeasures) {
  63. var staffMeasures: List<StaffMeasure> = this.measureList[this.measureListIndex];
  64. for (var idx: number = 0, len = staffMeasures.Count; idx < len; ++idx)
  65. staffMeasures[idx].ResetLayout();
  66. var sourceMeasure: SourceMeasure = staffMeasures[0].ParentSourceMeasure;
  67. var sourceMeasureEndsSystem: boolean = sourceMeasure.BreakSystemAfter;
  68. var isSystemStartMeasure: boolean = this.currentSystemParams.IsSystemStartMeasure();
  69. var isFirstSourceMeasure: boolean = sourceMeasure == this.graphicalMusicSheet.ParentMusicSheet.getFirstSourceMeasure();
  70. var currentMeasureBeginInstructionsWidth: number = this.rules.MeasureLeftMargin;
  71. var currentMeasureEndInstructionsWidth: number = 0;
  72. var measureStartLine: SystemLinesEnum = this.getMeasureStartLine();
  73. currentMeasureBeginInstructionsWidth += getLineWidth(staffMeasures[0], measureStartLine, isSystemStartMeasure);
  74. if (!this.leadSheet) {
  75. currentMeasureBeginInstructionsWidth += this.addBeginInstructions(staffMeasures, isSystemStartMeasure, isFirstSourceMeasure);
  76. currentMeasureEndInstructionsWidth += this.addEndInstructions(staffMeasures);
  77. }
  78. var currentMeasureVarWidth: number = 0;
  79. for (var i: number = 0; i < this.numberOfVisibleStaffLines; i++)
  80. currentMeasureVarWidth = Math.Max(currentMeasureVarWidth, staffMeasures[i].MinimumStaffEntriesWidth);
  81. var measureEndLine: SystemLinesEnum = this.getMeasureEndLine();
  82. currentMeasureEndInstructionsWidth += getLineWidth(staffMeasures[0], measureEndLine, isSystemStartMeasure);
  83. var nextMeasureBeginInstructionWidth: number = this.rules.MeasureLeftMargin;
  84. if (this.measureListIndex + 1 < this.measureList.Count) {
  85. var nextStaffMeasures: List<StaffMeasure> = this.measureList[this.measureListIndex + 1];
  86. var nextSourceMeasure: SourceMeasure = nextStaffMeasures[0].ParentSourceMeasure;
  87. if (nextSourceMeasure.hasBeginInstructions()) {
  88. nextMeasureBeginInstructionWidth += this.addBeginInstructions(nextStaffMeasures, false, false);
  89. }
  90. }
  91. var totalMeasureWidth: number = currentMeasureBeginInstructionsWidth + currentMeasureEndInstructionsWidth + currentMeasureVarWidth;
  92. var measureFitsInSystem: boolean = this.currentSystemParams.currentWidth + totalMeasureWidth + nextMeasureBeginInstructionWidth < systemMaxWidth;
  93. if (isSystemStartMeasure || measureFitsInSystem) {
  94. this.addMeasureToSystem(staffMeasures, measureStartLine, measureEndLine, totalMeasureWidth, currentMeasureBeginInstructionsWidth, currentMeasureVarWidth, currentMeasureEndInstructionsWidth);
  95. this.updateActiveClefs(sourceMeasure, staffMeasures);
  96. this.measureListIndex++;
  97. }
  98. else {
  99. this.finalizeCurrentAndCreateNewSystem(staffMeasures, previousMeasureEndsSystem);
  100. }
  101. previousMeasureEndsSystem = sourceMeasureEndsSystem;
  102. }
  103. finalizeCurrentAndCreateNewSystem(this.measureList[this.measureList.Count - 1], true);
  104. }
  105. private setMeasureWidth(staffMeasures: List<StaffMeasure>, width: number, beginInstrWidth: number, endInstrWidth: number): void {
  106. for (var idx: number = 0, len = staffMeasures.Count; idx < len; ++idx) {
  107. var measure: StaffMeasure = staffMeasures[idx];
  108. measure.SetWidth(width);
  109. if (beginInstrWidth > 0)
  110. measure.BeginInstructionsWidth = beginInstrWidth;
  111. if (endInstrWidth > 0)
  112. measure.EndInstructionsWidth = endInstrWidth;
  113. }
  114. }
  115. private finalizeCurrentAndCreateNewSystem(measures: List<StaffMeasure>, isPartEndingSystem: boolean = false): void {
  116. this.adaptRepetitionLineWithIfNeeded();
  117. if (!isPartEndingSystem) {
  118. this.checkAndCreateExtraInstructionMeasure(measures);
  119. }
  120. this.stretchMusicSystem(isPartEndingSystem);
  121. if (this.currentPageHeight + this.currentSystemParams.currentSystem.PositionAndShape.Size.Height + this.rules.SystemDistance <= this.rules.PageHeight) {
  122. this.currentPageHeight += this.currentSystemParams.currentSystem.PositionAndShape.Size.Height + this.rules.SystemDistance;
  123. if (this.currentPageHeight + this.currentSystemParams.currentSystem.PositionAndShape.Size.Height + this.rules.SystemDistance >= this.rules.PageHeight) {
  124. this.currentMusicPage = this.createMusicPage();
  125. this.currentPageHeight = this.rules.PageTopMargin + this.rules.TitleTopDistance;
  126. }
  127. }
  128. else {
  129. this.currentMusicPage = this.createMusicPage();
  130. this.currentPageHeight = this.rules.PageTopMargin + this.rules.TitleTopDistance;
  131. }
  132. this.currentSystemParams = new SystemBuildParameters();
  133. if (this.measureListIndex < this.measureList.Count) {
  134. this.currentSystemParams.currentSystem = this.initMusicSystem();
  135. this.layoutSystemStaves();
  136. }
  137. }
  138. private adaptRepetitionLineWithIfNeeded(): void {
  139. var systemMeasures: List<MeasureBuildParameters> = this.currentSystemParams.systemMeasures;
  140. if (systemMeasures.Count >= 1) {
  141. var measures: List<StaffMeasure> = this.currentSystemParams.currentSystem.GraphicalMeasures[this.currentSystemParams.currentSystem.GraphicalMeasures.Count - 1];
  142. var measureParams: MeasureBuildParameters = systemMeasures[systemMeasures.Count - 1];
  143. var diff: number = 0.0f;
  144. if (measureParams.endLine == SystemLinesEnum.DotsBoldBoldDots) {
  145. measureParams.endLine = SystemLinesEnum.DotsThinBold;
  146. diff = measures[0].GetLineWidth(SystemLinesEnum.DotsBoldBoldDots) / 2 - measures[0].GetLineWidth(SystemLinesEnum.DotsThinBold);
  147. }
  148. this.currentSystemParams.currentSystemFixWidth -= diff;
  149. for (var idx: number = 0, len = measures.Count; idx < len; ++idx) {
  150. var measure: StaffMeasure = measures[idx];
  151. measure.EndInstructionsWidth -= diff;
  152. }
  153. }
  154. }
  155. private addMeasureToSystem(staffMeasures: List<StaffMeasure>, measureStartLine: SystemLinesEnum, measureEndLine: SystemLinesEnum, totalMeasureWidth: number, currentMeasureBeginInstructionsWidth: number, currentVarWidth: number, currentMeasureEndInstructionsWidth: number): void {
  156. this.currentSystemParams.systemMeasures.Add(__init(new MeasureBuildParameters(), { beginLine: measureStartLine, endLine: measureEndLine }));
  157. this.setMeasureWidth(staffMeasures, totalMeasureWidth, currentMeasureBeginInstructionsWidth,
  158. currentMeasureEndInstructionsWidth);
  159. this.addStaveMeasuresToSystem(staffMeasures);
  160. this.currentSystemParams.currentWidth += totalMeasureWidth;
  161. this.currentSystemParams.currentSystemFixWidth += currentMeasureBeginInstructionsWidth + currentMeasureEndInstructionsWidth;
  162. this.currentSystemParams.currentSystemVarWidth += currentVarWidth;
  163. this.currentSystemParams.systemMeasureIndex++;
  164. }
  165. private createMusicPage(): GraphicalMusicPage {
  166. var page: GraphicalMusicPage = new GraphicalMusicPage(this.graphicalMusicSheet);
  167. this.graphicalMusicSheet.MusicPages.Add(page);
  168. page.PositionAndShape.BorderLeft = 0.0f;
  169. page.PositionAndShape.BorderRight = this.graphicalMusicSheet.ParentMusicSheet.PageWidth;
  170. page.PositionAndShape.BorderTop = 0.0f;
  171. page.PositionAndShape.BorderBottom = this.rules.PageHeight;
  172. page.PositionAndShape.RelativePosition = new PointF_2D(0.0f, 0.0f);
  173. return page;
  174. }
  175. private initMusicSystem(): MusicSystem {
  176. var musicSystem: MusicSystem = this.symbolFactory.createMusicSystem(this.currentMusicPage, this.globalSystemIndex++);
  177. this.currentMusicPage.MusicSystems.Add(musicSystem);
  178. var boundingBox: BoundingBox = musicSystem.PositionAndShape;
  179. this.currentMusicPage.PositionAndShape.ChildElements.Add(boundingBox);
  180. return musicSystem;
  181. }
  182. private getFullPageSystemWidth(): number {
  183. return this.currentMusicPage.PositionAndShape.Size.Width - this.rules.PageLeftMargin - this.rules.PageRightMargin - this.rules.SystemLeftMargin - this.rules.SystemRightMargin;
  184. }
  185. private layoutSystemStaves(): void {
  186. var systemWidth: number = this.getFullPageSystemWidth();
  187. var musicSystem: MusicSystem = this.currentSystemParams.currentSystem;
  188. var boundingBox: BoundingBox = musicSystem.PositionAndShape;
  189. boundingBox.BorderLeft = 0.0f;
  190. boundingBox.BorderRight = systemWidth;
  191. boundingBox.BorderTop = 0.0f;
  192. var staffList: List<Staff> = new List<Staff>();
  193. var musicSheet: MusicSheet = this.graphicalMusicSheet.ParentMusicSheet;
  194. var instruments: Instrument[] = musicSheet.Instruments.Where(i => i.Voices.Count > 0 && i.Voices[0].Visible).ToArray();
  195. for (var idx: number = 0, len = instruments.length; idx < len; ++idx) {
  196. var instrument: Instrument = instruments[idx];
  197. for (var idx2: number = 0, len2 = instrument.Staves.Count; idx2 < len2; ++idx2) {
  198. var staff: Staff = instrument.Staves[idx2];
  199. staffList.Add(staff);
  200. }
  201. }
  202. var multiLyrics: boolean = false;
  203. if (this.leadSheet) {
  204. for (var idx: number = 0, len = staffList.Count; idx < len; ++idx) {
  205. var staff: Staff = staffList[idx];
  206. if (staff.ParentInstrument.LyricVersesNumbers.Count > 1) {
  207. multiLyrics = true;
  208. break;
  209. }
  210. }
  211. }
  212. var yOffsetSum: number = 0;
  213. for (var i: number = 0; i < staffList.Count; i++) {
  214. this.addStaffLineToMusicSystem(musicSystem, yOffsetSum, staffList[i]);
  215. yOffsetSum += this.rules.StaffHeight;
  216. if (i + 1 < staffList.Count) {
  217. var yOffset: number = 0;
  218. if (this.leadSheet && !multiLyrics) {
  219. yOffset = 2.5f;
  220. }
  221. else {
  222. if (staffList[i].ParentInstrument == staffList[i + 1].ParentInstrument)
  223. yOffset = this.rules.BetweenStaffDistance;
  224. else yOffset = this.rules.StaffDistance;
  225. }
  226. yOffsetSum += yOffset;
  227. }
  228. }
  229. boundingBox.BorderBottom = yOffsetSum;
  230. }
  231. private addStaffLineToMusicSystem(musicSystem: MusicSystem, relativeYPosition: number, staff: Staff): void {
  232. if (musicSystem != null) {
  233. var staffLine: StaffLine = this.symbolFactory.createStaffLine(musicSystem, staff);
  234. musicSystem.StaffLines.Add(staffLine);
  235. var boundingBox: BoundingBox = staffLine.PositionAndShape;
  236. musicSystem.PositionAndShape.ChildElements.Add(boundingBox);
  237. var relativePosition: PointF_2D = new PointF_2D();
  238. if (musicSystem.Parent.MusicSystems[0] == musicSystem && musicSystem.Parent == musicSystem.Parent.Parent.MusicPages[0])
  239. relativePosition.X = this.rules.FirstSystemMargin;
  240. else relativePosition.X = 0.0f;
  241. relativePosition.Y = relativeYPosition;
  242. boundingBox.RelativePosition = relativePosition;
  243. if (musicSystem.Parent.MusicSystems[0] == musicSystem && musicSystem.Parent == musicSystem.Parent.Parent.MusicPages[0])
  244. boundingBox.BorderRight = musicSystem.PositionAndShape.Size.Width - this.rules.FirstSystemMargin;
  245. else boundingBox.BorderRight = musicSystem.PositionAndShape.Size.Width;
  246. boundingBox.BorderLeft = 0.0f;
  247. boundingBox.BorderTop = 0.0f;
  248. boundingBox.BorderBottom = this.rules.StaffHeight;
  249. for (var i: number = 0; i < 5; i++) {
  250. var start: PointF_2D = new PointF_2D();
  251. start.X = 0.0f;
  252. start.Y = i * this.rules.StaffHeight / 4;
  253. var end: PointF_2D = new PointF_2D();
  254. end.X = staffLine.PositionAndShape.Size.Width;
  255. end.Y = i * this.rules.StaffHeight / 4;
  256. if (this.leadSheet)
  257. start.Y = end.Y = 0;
  258. staffLine.StaffLines[i] = new GraphicalLine(start, end, this.rules.StaffLineWidth);
  259. }
  260. }
  261. }
  262. private initializeActiveInstructions(measureList: List<StaffMeasure>): void {
  263. var firstSourceMeasure: SourceMeasure = this.graphicalMusicSheet.ParentMusicSheet.getFirstSourceMeasure();
  264. if (firstSourceMeasure != null) {
  265. this.visibleStaffIndices = this.graphicalMusicSheet.getVisibleStavesIndecesFromSourceMeasure(measureList).ToArray();
  266. for (var i: number = 0, len = this.visibleStaffIndices.length; i < len; i++) {
  267. var staffIndex: number = this.visibleStaffIndices[i];
  268. var graphicalMeasure: StaffMeasure = this.graphicalMusicSheet.getGraphicalMeasureFromSourceMeasureAndIndex(firstSourceMeasure, staffIndex);
  269. this.activeClefs[i] = <ClefInstruction>firstSourceMeasure.FirstInstructionsStaffEntries[staffIndex].Instructions[0];
  270. var keyInstruction: KeyInstruction = new KeyInstruction(<KeyInstruction>firstSourceMeasure.FirstInstructionsStaffEntries[staffIndex].Instructions[1]);
  271. keyInstruction = this.transposeKeyInstruction(keyInstruction, graphicalMeasure);
  272. this.activeKeys[i] = keyInstruction;
  273. this.activeRhythm[i] = <RhythmInstruction>firstSourceMeasure.FirstInstructionsStaffEntries[staffIndex].Instructions[2];
  274. }
  275. }
  276. }
  277. private transposeKeyInstruction(keyInstruction: KeyInstruction, graphicalMeasure: StaffMeasure): KeyInstruction {
  278. if (this.graphicalMusicSheet.ParentMusicSheet.Transpose != 0 && graphicalMeasure.ParentStaff.ParentInstrument.MidiInstrumentId != Common.Enums.MidiInstrument.Percussion && MusicSheetCalculator.TransposeCalculator != null)
  279. MusicSheetCalculator.TransposeCalculator.TransposeKey(keyInstruction,
  280. this.graphicalMusicSheet.ParentMusicSheet.Transpose);
  281. return keyInstruction;
  282. }
  283. private addBeginInstructions(measures: List<StaffMeasure>, isSystemFirstMeasure: boolean, isFirstSourceMeasure: boolean): number {
  284. var measureCount: number = measures.Count;
  285. if (measureCount == 0)
  286. return 0;
  287. var totalBeginInstructionLengthX: number = 0.0f;
  288. var sourceMeasure: SourceMeasure = measures[0].ParentSourceMeasure;
  289. for (var idx: number = 0; idx < measureCount; ++idx) {
  290. var measure: StaffMeasure = measures[idx];
  291. var staffIndex: number = this.visibleStaffIndices[idx];
  292. var beginInstructionsStaffEntry: SourceStaffEntry = sourceMeasure.FirstInstructionsStaffEntries[staffIndex];
  293. var beginInstructionLengthX: number = this.AddInstructionsAtMeasureBegin(beginInstructionsStaffEntry, measure,
  294. idx, isFirstSourceMeasure,
  295. isSystemFirstMeasure);
  296. totalBeginInstructionLengthX = Math.Max(totalBeginInstructionLengthX, beginInstructionLengthX);
  297. }
  298. return totalBeginInstructionLengthX;
  299. }
  300. private addEndInstructions(measures: List<StaffMeasure>): number {
  301. var measureCount: number = measures.Count;
  302. if (measureCount == 0)
  303. return 0;
  304. var totalEndInstructionLengthX: number = 0.5f;
  305. var sourceMeasure: SourceMeasure = measures[0].ParentSourceMeasure;
  306. for (var idx: number = 0; idx < measureCount; idx++) {
  307. var measure: StaffMeasure = measures[idx];
  308. var staffIndex: number = this.visibleStaffIndices[idx];
  309. var endInstructionsStaffEntry: SourceStaffEntry = sourceMeasure.LastInstructionsStaffEntries[staffIndex];
  310. var endInstructionLengthX: number = this.addInstructionsAtMeasureEnd(endInstructionsStaffEntry, measure);
  311. totalEndInstructionLengthX = Math.Max(totalEndInstructionLengthX, endInstructionLengthX);
  312. }
  313. return totalEndInstructionLengthX;
  314. }
  315. private AddInstructionsAtMeasureBegin(firstEntry: SourceStaffEntry, measure: StaffMeasure,
  316. visibleStaffIdx: number, isFirstSourceMeasure: boolean, isSystemStartMeasure: boolean): number {
  317. var instructionsLengthX: number = 0;
  318. var currentClef: ClefInstruction = null;
  319. var currentKey: KeyInstruction = null;
  320. var currentRhythm: RhythmInstruction = null;
  321. if (firstEntry != null) {
  322. for (var idx: number = 0, len = firstEntry.Instructions.Count; idx < len; ++idx) {
  323. var abstractNotationInstruction: AbstractNotationInstruction = firstEntry.Instructions[idx];
  324. if (abstractNotationInstruction instanceof ClefInstruction) {
  325. currentClef = <ClefInstruction>abstractNotationInstruction;
  326. }
  327. else if (abstractNotationInstruction instanceof KeyInstruction) {
  328. currentKey = <KeyInstruction>abstractNotationInstruction;
  329. }
  330. else if (abstractNotationInstruction instanceof RhythmInstruction) {
  331. currentRhythm = <RhythmInstruction>abstractNotationInstruction;
  332. }
  333. }
  334. }
  335. if (isSystemStartMeasure) {
  336. if (currentClef == null) {
  337. currentClef = this.activeClefs[visibleStaffIdx];
  338. }
  339. if (currentKey == null) {
  340. currentKey = this.activeKeys[visibleStaffIdx];
  341. }
  342. if (isFirstSourceMeasure && currentRhythm == null) {
  343. currentRhythm = this.activeRhythm[visibleStaffIdx];
  344. }
  345. }
  346. var clefAdded: boolean = false;
  347. var keyAdded: boolean = false;
  348. var rhythmAdded: boolean = false;
  349. if (currentClef != null) {
  350. measure.AddClefAtBegin(currentClef);
  351. clefAdded = true;
  352. }
  353. else {
  354. currentClef = this.activeClefs[visibleStaffIdx];
  355. }
  356. if (currentKey != null) {
  357. currentKey = this.transposeKeyInstruction(currentKey, measure);
  358. var previousKey: KeyInstruction = isSystemStartMeasure ? null : this.activeKeys[visibleStaffIdx];
  359. measure.AddKeyAtBegin(currentKey, previousKey, currentClef);
  360. keyAdded = true;
  361. }
  362. if (currentRhythm != null) {
  363. measure.AddRhythmAtBegin(currentRhythm);
  364. rhythmAdded = true;
  365. }
  366. if (clefAdded || keyAdded || rhythmAdded) {
  367. instructionsLengthX += measure.BeginInstructionsWidth;
  368. if (rhythmAdded)
  369. instructionsLengthX += this.rules.RhythmRightMargin;
  370. }
  371. return instructionsLengthX;
  372. }
  373. private addInstructionsAtMeasureEnd(lastEntry: SourceStaffEntry, measure: StaffMeasure): number {
  374. if (lastEntry == null || lastEntry.Instructions == null || lastEntry.Instructions.Count == 0)
  375. return 0;
  376. for (var idx: number = 0, len = lastEntry.Instructions.Count; idx < len; ++idx) {
  377. var abstractNotationInstruction: AbstractNotationInstruction = lastEntry.Instructions[idx];
  378. if (abstractNotationInstruction instanceof ClefInstruction) {
  379. var activeClef: ClefInstruction = <ClefInstruction>abstractNotationInstruction;
  380. measure.AddClefAtEnd(activeClef);
  381. }
  382. }
  383. return this.rules.MeasureRightMargin + measure.EndInstructionsWidth;
  384. }
  385. private updateActiveClefs(measure: SourceMeasure, staffMeasures: List<StaffMeasure>): void {
  386. for (var visStaffIdx: number = 0, len = staffMeasures.Count; visStaffIdx < len; visStaffIdx++) {
  387. var staffIndex: number = this.visibleStaffIndices[visStaffIdx];
  388. var firstEntry: SourceStaffEntry = measure.FirstInstructionsStaffEntries[staffIndex];
  389. if (firstEntry != null) {
  390. for (var idx: number = 0, len2 = firstEntry.Instructions.Count; idx < len2; ++idx) {
  391. var abstractNotationInstruction: AbstractNotationInstruction = firstEntry.Instructions[idx];
  392. if (abstractNotationInstruction instanceof ClefInstruction) {
  393. this.activeClefs[visStaffIdx] = <ClefInstruction>abstractNotationInstruction;
  394. }
  395. else if (abstractNotationInstruction instanceof KeyInstruction) {
  396. this.activeKeys[visStaffIdx] = <KeyInstruction>abstractNotationInstruction;
  397. }
  398. else if (abstractNotationInstruction instanceof RhythmInstruction) {
  399. this.activeRhythm[visStaffIdx] = <RhythmInstruction>abstractNotationInstruction;
  400. }
  401. }
  402. }
  403. var entries: List<SourceStaffEntry> = measure.getEntriesPerStaff(staffIndex);
  404. for (var idx: number = 0, len2 = entries.Count; idx < len2; ++idx) {
  405. var staffEntry: SourceStaffEntry = entries[idx];
  406. if (staffEntry.Instructions != null) {
  407. for (var idx2: number = 0, len3 = staffEntry.Instructions.Count; idx2 < len3; ++idx2) {
  408. var abstractNotationInstruction: AbstractNotationInstruction = staffEntry.Instructions[idx2];
  409. if (abstractNotationInstruction instanceof ClefInstruction) {
  410. this.activeClefs[visStaffIdx] = <ClefInstruction>abstractNotationInstruction;
  411. }
  412. }
  413. }
  414. }
  415. var lastEntry: SourceStaffEntry = measure.LastInstructionsStaffEntries[staffIndex];
  416. if (lastEntry != null) {
  417. var instructions: List<AbstractNotationInstruction> = lastEntry.Instructions;
  418. for (var idx: number = 0, len3 = instructions.Count; idx < len3; ++idx) {
  419. var abstractNotationInstruction: AbstractNotationInstruction = instructions[idx];
  420. if (abstractNotationInstruction instanceof ClefInstruction) {
  421. this.activeClefs[visStaffIdx] = <ClefInstruction>abstractNotationInstruction;
  422. }
  423. }
  424. }
  425. }
  426. }
  427. private checkAndCreateExtraInstructionMeasure(measures: List<StaffMeasure>): void {
  428. var firstStaffEntries: List<SourceStaffEntry> = measures[0].ParentSourceMeasure.FirstInstructionsStaffEntries;
  429. var visibleInstructionEntries: List<SourceStaffEntry> = new List<SourceStaffEntry>();
  430. for (var idx: number = 0, len = measures.Count; idx < len; ++idx) {
  431. var measure: StaffMeasure = measures[idx];
  432. visibleInstructionEntries.Add(firstStaffEntries[measure.ParentStaff.IdInMusicSheet]);
  433. }
  434. var maxMeasureWidth: number = 0;
  435. for (var visStaffIdx: number = 0, len = visibleInstructionEntries.Count; visStaffIdx < len; ++visStaffIdx) {
  436. var sse: SourceStaffEntry = visibleInstructionEntries[visStaffIdx];
  437. if (sse == null)
  438. continue;
  439. var instructions: List<AbstractNotationInstruction> = sse.Instructions;
  440. var keyInstruction: KeyInstruction = null;
  441. var rhythmInstruction: RhythmInstruction = null;
  442. for (var idx2: number = 0, len2 = instructions.Count; idx2 < len2; ++idx2) {
  443. var instruction: AbstractNotationInstruction = instructions[idx2];
  444. if (instruction instanceof KeyInstruction && (<KeyInstruction>instruction).Key != this.activeKeys[visStaffIdx].Key)
  445. keyInstruction = <KeyInstruction>instruction;
  446. if (instruction instanceof RhythmInstruction && (<RhythmInstruction>instruction) != this.activeRhythm[visStaffIdx])
  447. rhythmInstruction = <RhythmInstruction>instruction;
  448. }
  449. if (keyInstruction != null || rhythmInstruction != null) {
  450. var measureWidth: number = this.addExtraInstructionMeasure(visStaffIdx, keyInstruction, rhythmInstruction);
  451. maxMeasureWidth = Math.Max(maxMeasureWidth, measureWidth);
  452. }
  453. }
  454. if (maxMeasureWidth > 0) {
  455. this.currentSystemParams.systemMeasures.Add(__init(new MeasureBuildParameters(), { beginLine: SystemLinesEnum.None, endLine: SystemLinesEnum.None }));
  456. this.currentSystemParams.currentWidth += maxMeasureWidth;
  457. this.currentSystemParams.currentSystemFixWidth += maxMeasureWidth;
  458. }
  459. }
  460. private addExtraInstructionMeasure(visStaffIdx: number, keyInstruction: KeyInstruction, rhythmInstruction: RhythmInstruction): number {
  461. var currentSystem: MusicSystem = this.currentSystemParams.currentSystem;
  462. var measures: List<StaffMeasure> = new List<StaffMeasure>();
  463. var measure: StaffMeasure = this.symbolFactory.createExtraStaffMeasure(currentSystem.StaffLines[visStaffIdx]);
  464. measures.Add(measure);
  465. if (keyInstruction != null) {
  466. measure.AddKeyAtBegin(keyInstruction, this.activeKeys[visStaffIdx], this.activeClefs[visStaffIdx]);
  467. }
  468. if (rhythmInstruction != null) {
  469. measure.AddRhythmAtBegin(rhythmInstruction);
  470. }
  471. measure.PositionAndShape.BorderLeft = 0.0f;
  472. measure.PositionAndShape.BorderTop = 0.0f;
  473. measure.PositionAndShape.BorderBottom = this.rules.StaffHeight;
  474. var width: number = this.rules.MeasureLeftMargin + measure.BeginInstructionsWidth + this.rules.MeasureRightMargin;
  475. measure.PositionAndShape.BorderRight = width;
  476. currentSystem.StaffLines[visStaffIdx].Measures.Add(measure);
  477. measure.ParentStaffLine = currentSystem.StaffLines[visStaffIdx];
  478. currentSystem.StaffLines[visStaffIdx].PositionAndShape.ChildElements.Add(measure.PositionAndShape);
  479. return width;
  480. }
  481. private addStaveMeasuresToSystem(staffMeasures: List<StaffMeasure>): void {
  482. if (staffMeasures[0] != null) {
  483. var gmeasures: List<StaffMeasure> = new List<StaffMeasure>();
  484. for (var i: number = 0; i < staffMeasures.Count; i++)
  485. gmeasures.Add(staffMeasures[i]);
  486. var currentSystem: MusicSystem = this.currentSystemParams.currentSystem;
  487. for (var visStaffIdx: number = 0; visStaffIdx < this.numberOfVisibleStaffLines; visStaffIdx++) {
  488. var measure: StaffMeasure = gmeasures[visStaffIdx];
  489. currentSystem.StaffLines[visStaffIdx].Measures.Add(measure);
  490. measure.ParentStaffLine = currentSystem.StaffLines[visStaffIdx];
  491. currentSystem.StaffLines[visStaffIdx].PositionAndShape.ChildElements.Add(measure.PositionAndShape);
  492. }
  493. currentSystem.AddStaffMeasures(gmeasures);
  494. }
  495. }
  496. private getMeasureStartLine(): SystemLinesEnum {
  497. var thisMeasureBeginsLineRep: boolean = this.thisMeasureBeginsLineRepetition();
  498. if (thisMeasureBeginsLineRep) {
  499. var isSystemStartMeasure: boolean = this.currentSystemParams.IsSystemStartMeasure();
  500. var isGlobalFirstMeasure: boolean = this.measureListIndex == 0;
  501. if (this.previousMeasureEndsLineRepetition() && !isSystemStartMeasure) {
  502. return SystemLinesEnum.DotsBoldBoldDots;
  503. }
  504. if (!isGlobalFirstMeasure)
  505. return SystemLinesEnum.BoldThinDots;
  506. }
  507. return SystemLinesEnum.None;
  508. }
  509. private getMeasureEndLine(): SystemLinesEnum {
  510. if (this.nextMeasureBeginsLineRepetition() && this.thisMeasureEndsLineRepetition()) {
  511. return SystemLinesEnum.DotsBoldBoldDots;
  512. }
  513. if (this.thisMeasureEndsLineRepetition()) {
  514. return SystemLinesEnum.DotsThinBold;
  515. }
  516. if (this.measureListIndex == this.measureList.Count - 1 || this.measureList[this.measureListIndex][0].ParentSourceMeasure.EndsPiece) {
  517. return SystemLinesEnum.ThinBold;
  518. }
  519. if (this.nextMeasureHasKeyInstructionChange() || this.thisMeasureEndsWordRepetition() || this.nextMeasureBeginsWordRepetition()) {
  520. return SystemLinesEnum.DoubleThin;
  521. }
  522. return SystemLinesEnum.SingleThin;
  523. }
  524. private getLineWidth(measure: StaffMeasure, systemLineEnum: SystemLinesEnum, isSystemStartMeasure: boolean): number {
  525. var width: number = measure.GetLineWidth(systemLineEnum);
  526. if (systemLineEnum == SystemLinesEnum.DotsBoldBoldDots) {
  527. width /= 2;
  528. }
  529. if (isSystemStartMeasure && systemLineEnum == SystemLinesEnum.BoldThinDots) {
  530. width += this.rules.DistanceBetweenLastInstructionAndRepetitionBarline;
  531. }
  532. return width;
  533. }
  534. private previousMeasureEndsLineRepetition(): boolean {
  535. if (this.measureListIndex == 0)
  536. return false;
  537. for (var idx: number = 0, len = this.measureList[this.measureListIndex - 1].Count; idx < len; ++idx) {
  538. var measure: StaffMeasure = this.measureList[this.measureListIndex - 1][idx];
  539. if (measure.endsWithLineRepetition())
  540. return true;
  541. }
  542. return false;
  543. }
  544. private thisMeasureBeginsLineRepetition(): boolean {
  545. for (var idx: number = 0, len = this.measureList[this.measureListIndex].Count; idx < len; ++idx) {
  546. var measure: StaffMeasure = this.measureList[this.measureListIndex][idx];
  547. if (measure.beginsWithLineRepetition())
  548. return true;
  549. }
  550. return false;
  551. }
  552. private nextMeasureBeginsLineRepetition(): boolean {
  553. var nextMeasureIndex: number = this.measureListIndex + 1;
  554. if (nextMeasureIndex >= this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.Count)
  555. return false;
  556. for (var idx: number = 0, len = this.measureList[nextMeasureIndex].Count; idx < len; ++idx) {
  557. var measure: StaffMeasure = this.measureList[nextMeasureIndex][idx];
  558. if (measure.beginsWithLineRepetition())
  559. return true;
  560. }
  561. return false;
  562. }
  563. private thisMeasureEndsLineRepetition(): boolean {
  564. for (var idx: number = 0, len = this.measureList[this.measureListIndex].Count; idx < len; ++idx) {
  565. var measure: StaffMeasure = this.measureList[this.measureListIndex][idx];
  566. if (measure.endsWithLineRepetition())
  567. return true;
  568. }
  569. return false;
  570. }
  571. private nextMeasureBeginsWordRepetition(): boolean {
  572. var nextMeasureIndex: number = this.measureListIndex + 1;
  573. if (nextMeasureIndex >= this.graphicalMusicSheet.ParentMusicSheet.SourceMeasures.Count)
  574. return false;
  575. for (var idx: number = 0, len = this.measureList[nextMeasureIndex].Count; idx < len; ++idx) {
  576. var measure: StaffMeasure = this.measureList[nextMeasureIndex][idx];
  577. if (measure.beginsWithWordRepetition())
  578. return true;
  579. }
  580. return false;
  581. }
  582. private thisMeasureEndsWordRepetition(): boolean {
  583. for (var idx: number = 0, len = this.measureList[this.measureListIndex].Count; idx < len; ++idx) {
  584. var measure: StaffMeasure = this.measureList[this.measureListIndex][idx];
  585. if (measure.endsWithWordRepetition())
  586. return true;
  587. }
  588. return false;
  589. }
  590. private nextMeasureHasKeyInstructionChange(): boolean {
  591. return this.getNextMeasureKeyInstruction() != null;
  592. }
  593. private getNextMeasureKeyInstruction(): KeyInstruction {
  594. if (this.measureListIndex < this.measureList.Count - 1) {
  595. for (var visIndex: number = 0; visIndex < this.measureList[this.measureListIndex].Count; visIndex++) {
  596. var sourceMeasure: SourceMeasure = this.measureList[this.measureListIndex + 1][visIndex].ParentSourceMeasure;
  597. if (sourceMeasure == null)
  598. return null;
  599. return sourceMeasure.getKeyInstruction(this.visibleStaffIndices[visIndex]);
  600. }
  601. }
  602. return null;
  603. }
  604. private calculateXScalingFactor(systemFixWidth: number, systemVarWidth: number): number {
  605. if (Math.Abs(systemVarWidth - 0) < 0.00001f || Math.Abs(systemFixWidth - 0) < 0.00001f)
  606. return 1.0f;
  607. var systemEndX: number;
  608. var currentSystem: MusicSystem = this.currentSystemParams.currentSystem;
  609. systemEndX = currentSystem.StaffLines[0].PositionAndShape.Size.Width;
  610. var scalingFactor: number = (systemEndX - systemFixWidth) / systemVarWidth;
  611. return scalingFactor;
  612. }
  613. private stretchMusicSystem(isPartEndingSystem: boolean): void {
  614. var scalingFactor: number = this.calculateXScalingFactor(this.currentSystemParams.currentSystemFixWidth, this.currentSystemParams.currentSystemVarWidth);
  615. if (isPartEndingSystem)
  616. scalingFactor = Math.Min(scalingFactor, this.rules.LastSystemMaxScalingFactor);
  617. var currentSystem: MusicSystem = this.currentSystemParams.currentSystem;
  618. for (var visStaffIdx: number = 0, len = currentSystem.StaffLines.Count; visStaffIdx < len; ++visStaffIdx) {
  619. var staffLine: StaffLine = currentSystem.StaffLines[visStaffIdx];
  620. var currentXPosition: number = 0.0f;
  621. for (var i: number = 0; i < staffLine.Measures.Count; i++) {
  622. var measure: StaffMeasure = staffLine.Measures[i];
  623. measure.SetPositionInStaffline(currentXPosition);
  624. measure.SetWidth(measure.BeginInstructionsWidth + measure.MinimumStaffEntriesWidth * scalingFactor + measure.EndInstructionsWidth);
  625. if (i < this.currentSystemParams.systemMeasures.Count) {
  626. var startLine: SystemLinesEnum = this.currentSystemParams.systemMeasures[i].beginLine;
  627. var lineWidth: number = measure.GetLineWidth(SystemLinesEnum.BoldThinDots);
  628. switch (startLine) {
  629. case SystemLinesEnum.BoldThinDots:
  630. {
  631. var xPosition: number = currentXPosition;
  632. if (i == 0) {
  633. xPosition = currentXPosition + measure.BeginInstructionsWidth - lineWidth;
  634. }
  635. currentSystem.createVerticalLineForMeasure(xPosition, SystemLinesEnum.BoldThinDots, lineWidth, visStaffIdx);
  636. break;
  637. }
  638. }
  639. }
  640. measure.StaffEntriesScaleFactor = scalingFactor;
  641. measure.LayoutSymbols();
  642. var nextMeasureHasRepStartLine: boolean = i + 1 < this.currentSystemParams.systemMeasures.Count && this.currentSystemParams.systemMeasures[i + 1].beginLine == SystemLinesEnum.BoldThinDots;
  643. if (!nextMeasureHasRepStartLine) {
  644. var endLine: SystemLinesEnum = SystemLinesEnum.SingleThin;
  645. if (i < this.currentSystemParams.systemMeasures.Count) {
  646. endLine = this.currentSystemParams.systemMeasures[i].endLine;
  647. }
  648. var lineWidth: number = measure.GetLineWidth(endLine);
  649. var xPos: number = measure.PositionAndShape.RelativePosition.X + measure.PositionAndShape.BorderRight - lineWidth;
  650. if (endLine == SystemLinesEnum.DotsBoldBoldDots)
  651. xPos -= lineWidth / 2;
  652. currentSystem.createVerticalLineForMeasure(xPos, endLine, lineWidth, visStaffIdx);
  653. }
  654. currentXPosition = measure.PositionAndShape.RelativePosition.X + measure.PositionAndShape.BorderRight;
  655. }
  656. }
  657. if (isPartEndingSystem)
  658. this.decreaseMusicSystemBorders();
  659. }
  660. private decreaseMusicSystemBorders(): void {
  661. var currentSystem: MusicSystem = this.currentSystemParams.currentSystem;
  662. var width: number = currentSystem.StaffLines[0].Measures.Last().PositionAndShape.RelativePosition.X + currentSystem.StaffLines[0].Measures.Last().PositionAndShape.Size.Width;
  663. for (var idx: number = 0, len = currentSystem.StaffLines.Count; idx < len; ++idx) {
  664. var staffLine: StaffLine = currentSystem.StaffLines[idx];
  665. staffLine.PositionAndShape.BorderRight = width;
  666. for (var idx2: number = 0, len2 = staffLine.StaffLines.Length; idx2 < len2; ++idx2) {
  667. var graphicalLine: GraphicalLine = staffLine.StaffLines[idx2];
  668. graphicalLine.End = new PointF_2D(width, graphicalLine.End.Y);
  669. }
  670. }
  671. currentSystem.PositionAndShape.BorderRight = width + this.currentSystemParams.MaxLabelLength + this.rules.SystemLabelsRightMargin;
  672. }
  673. }
  674. export class SystemBuildParameters {
  675. public currentSystem: MusicSystem;
  676. public systemMeasures: List<MeasureBuildParameters> = new List<MeasureBuildParameters>();
  677. public systemMeasureIndex: number = 0;
  678. public currentWidth: number = 0;
  679. public currentSystemFixWidth: number = 0;
  680. public currentSystemVarWidth: number = 0;
  681. public MaxLabelLength: number = 0;
  682. public IsSystemStartMeasure(): boolean {
  683. return this.systemMeasureIndex == 0;
  684. }
  685. }
  686. export class MeasureBuildParameters {
  687. public beginLine: SystemLinesEnum;
  688. public endLine: SystemLinesEnum;
  689. }