MusicSystem.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. import {StaffLine} from "./StaffLine";
  2. import {Instrument} from "../Instrument";
  3. import {BoundingBox} from "./BoundingBox";
  4. import {Fraction} from "../../Common/DataObjects/Fraction";
  5. import {SourceMeasure} from "../VoiceData/SourceMeasure";
  6. import {InstrumentalGroup} from "../InstrumentalGroup";
  7. import {TextAlignment} from "../../Common/Enums/TextAlignment";
  8. import {GraphicalMusicPage} from "./GraphicalMusicPage";
  9. import {GraphicalLabel} from "./GraphicalLabel";
  10. import {StaffMeasure} from "./StaffMeasure";
  11. import {GraphicalObject} from "./GraphicalObject";
  12. import {EngravingRules} from "./EngravingRules";
  13. import {PointF2D} from "../../Common/DataObjects/PointF2D";
  14. import {GraphicalStaffEntry} from "./GraphicalStaffEntry";
  15. import {SystemLinesEnum} from "./SystemLinesEnum";
  16. import Dictionary from "typescript-collections/dist/lib/Dictionary";
  17. import {GraphicalComment} from "./GraphicalComment";
  18. import {GraphicalMarkedArea} from "./GraphicalMarkedArea";
  19. import {SystemLine} from "./SystemLine";
  20. import {SystemLinePosition} from "./SystemLinePosition";
  21. import {Staff} from "../VoiceData/Staff";
  22. /**
  23. * A MusicSystem contains the [[StaffLine]]s for all instruments, until a line break
  24. */
  25. export abstract class MusicSystem extends GraphicalObject {
  26. public needsToBeRedrawn: boolean = true;
  27. protected parent: GraphicalMusicPage;
  28. protected id: number;
  29. protected staffLines: StaffLine[] = [];
  30. protected graphicalMeasures: StaffMeasure[][] = [];
  31. protected labels: Dictionary<GraphicalLabel, Instrument> = new Dictionary<GraphicalLabel, Instrument>();
  32. protected measureNumberLabels: GraphicalLabel[] = [];
  33. protected maxLabelLength: number;
  34. protected objectsToRedraw: [Object[], Object][] = [];
  35. protected instrumentBrackets: GraphicalObject[] = [];
  36. protected groupBrackets: GraphicalObject[] = [];
  37. protected graphicalMarkedAreas: GraphicalMarkedArea[] = [];
  38. protected graphicalComments: GraphicalComment[] = [];
  39. protected systemLines: SystemLine[] = [];
  40. protected rules: EngravingRules;
  41. constructor(parent: GraphicalMusicPage, id: number) {
  42. super();
  43. this.parent = parent;
  44. this.id = id;
  45. this.boundingBox = new BoundingBox(this, parent.PositionAndShape);
  46. this.maxLabelLength = 0.0;
  47. this.rules = this.parent.Parent.ParentMusicSheet.Rules;
  48. }
  49. public get Parent(): GraphicalMusicPage {
  50. return this.parent;
  51. }
  52. public set Parent(value: GraphicalMusicPage) {
  53. this.parent = value;
  54. }
  55. public get StaffLines(): StaffLine[] {
  56. return this.staffLines;
  57. }
  58. public get GraphicalMeasures(): StaffMeasure[][] {
  59. return this.graphicalMeasures;
  60. }
  61. public get MeasureNumberLabels(): GraphicalLabel[] {
  62. return this.measureNumberLabels;
  63. }
  64. public get Labels(): GraphicalLabel[] {
  65. return this.labels.keys();
  66. }
  67. public get ObjectsToRedraw(): [Object[], Object][] {
  68. return this.objectsToRedraw;
  69. }
  70. public get InstrumentBrackets(): GraphicalObject[] {
  71. return this.instrumentBrackets;
  72. }
  73. public get GroupBrackets(): GraphicalObject[] {
  74. return this.groupBrackets;
  75. }
  76. public get GraphicalMarkedAreas(): GraphicalMarkedArea[] {
  77. return this.graphicalMarkedAreas;
  78. }
  79. public get GraphicalComments(): GraphicalComment[] {
  80. return this.graphicalComments;
  81. }
  82. public get SystemLines(): SystemLine[] {
  83. return this.systemLines;
  84. }
  85. public get Id(): number {
  86. return this.id;
  87. }
  88. /**
  89. * Create the left vertical Line connecting all staves of the [[MusicSystem]].
  90. * @param lineWidth
  91. * @param systemLabelsRightMargin
  92. */
  93. public createSystemLeftLine(lineWidth: number, systemLabelsRightMargin: number): void {
  94. let xPosition: number = -lineWidth / 2;
  95. if (this === this.parent.MusicSystems[0] && this.parent === this.parent.Parent.MusicPages[0]) {
  96. xPosition = this.maxLabelLength + systemLabelsRightMargin - lineWidth / 2;
  97. }
  98. const top: StaffMeasure = this.staffLines[0].Measures[0];
  99. let bottom: StaffMeasure = undefined;
  100. if (this.staffLines.length > 1) {
  101. bottom = this.staffLines[this.staffLines.length - 1].Measures[0];
  102. }
  103. const leftSystemLine: SystemLine = this.createSystemLine(xPosition, lineWidth, SystemLinesEnum.SingleThin,
  104. SystemLinePosition.MeasureBegin, this, top, bottom);
  105. this.SystemLines.push(leftSystemLine);
  106. leftSystemLine.PositionAndShape.RelativePosition = new PointF2D(xPosition, 0);
  107. leftSystemLine.PositionAndShape.BorderLeft = 0;
  108. leftSystemLine.PositionAndShape.BorderRight = lineWidth;
  109. leftSystemLine.PositionAndShape.BorderTop = 0;
  110. leftSystemLine.PositionAndShape.BorderBottom = this.boundingBox.Size.height;
  111. this.createLinesForSystemLine(leftSystemLine);
  112. }
  113. /**
  114. * Create the vertical Lines after the End of all [[StaffLine]]'s Measures
  115. * @param xPosition
  116. * @param lineWidth
  117. * @param lineType
  118. * @param linePosition indicates if the line belongs to start or end of measure
  119. * @param measureIndex the measure index within the staffline
  120. * @param measure
  121. */
  122. public createVerticalLineForMeasure(xPosition: number, lineWidth: number, lineType: SystemLinesEnum, linePosition: SystemLinePosition,
  123. measureIndex: number, measure: StaffMeasure): void {
  124. const staffLine: StaffLine = measure.ParentStaffLine;
  125. const staffLineRelative: PointF2D = new PointF2D(staffLine.PositionAndShape.RelativePosition.x,
  126. staffLine.PositionAndShape.RelativePosition.y);
  127. const staves: Staff[] = staffLine.ParentStaff.ParentInstrument.Staves;
  128. if (staffLine.ParentStaff === staves[0]) {
  129. let bottomMeasure: StaffMeasure = undefined;
  130. if (staves.length > 1) {
  131. bottomMeasure = this.getBottomStaffLine(staffLine).Measures[measureIndex];
  132. }
  133. const singleVerticalLineAfterMeasure: SystemLine = this.createSystemLine(xPosition, lineWidth, lineType,
  134. linePosition, this, measure, bottomMeasure);
  135. const systemXPosition: number = staffLineRelative.x + xPosition;
  136. singleVerticalLineAfterMeasure.PositionAndShape.RelativePosition = new PointF2D(systemXPosition, 0);
  137. singleVerticalLineAfterMeasure.PositionAndShape.BorderLeft = 0;
  138. singleVerticalLineAfterMeasure.PositionAndShape.BorderRight = lineWidth;
  139. this.SystemLines.push(singleVerticalLineAfterMeasure);
  140. }
  141. }
  142. /**
  143. * Set the y-Positions of all the system lines in the system and creates the graphical Lines and dots within.
  144. * @param rules
  145. */
  146. public setYPositionsToVerticalLineObjectsAndCreateLines(rules: EngravingRules): void {
  147. // empty
  148. }
  149. public calculateBorders(rules: EngravingRules): void {
  150. // empty
  151. }
  152. public alignBeginInstructions(): void {
  153. // empty
  154. }
  155. public GetLeftBorderAbsoluteXPosition(): number {
  156. return this.StaffLines[0].PositionAndShape.AbsolutePosition.x + this.StaffLines[0].Measures[0].beginInstructionsWidth;
  157. }
  158. public GetRightBorderAbsoluteXPosition(): number {
  159. return this.StaffLines[0].PositionAndShape.AbsolutePosition.x + this.StaffLines[0].StaffLines[0].End.x;
  160. }
  161. public AddStaffMeasures(graphicalMeasures: StaffMeasure[]): void {
  162. for (let idx: number = 0, len: number = graphicalMeasures.length; idx < len; ++idx) {
  163. const graphicalMeasure: StaffMeasure = graphicalMeasures[idx];
  164. graphicalMeasure.parentMusicSystem = this;
  165. }
  166. this.graphicalMeasures.push(graphicalMeasures);
  167. }
  168. public GetSystemsFirstTimeStamp(): Fraction {
  169. return this.graphicalMeasures[0][0].parentSourceMeasure.AbsoluteTimestamp;
  170. }
  171. public GetSystemsLastTimeStamp(): Fraction {
  172. const m: SourceMeasure = this.graphicalMeasures[this.graphicalMeasures.length - 1][0].parentSourceMeasure;
  173. return Fraction.plus(m.AbsoluteTimestamp, m.Duration);
  174. }
  175. /**
  176. * Create an InstrumentBracket for each multiStave Instrument.
  177. * @param instruments
  178. * @param staffHeight
  179. */
  180. public createInstrumentBrackets(instruments: Instrument[], staffHeight: number): void {
  181. for (let idx: number = 0, len: number = instruments.length; idx < len; ++idx) {
  182. const instrument: Instrument = instruments[idx];
  183. if (instrument.Staves.length > 1) {
  184. let firstStaffLine: StaffLine = undefined, lastStaffLine: StaffLine = undefined;
  185. for (let idx2: number = 0, len2: number = this.staffLines.length; idx2 < len2; ++idx2) {
  186. const staffLine: StaffLine = this.staffLines[idx2];
  187. if (staffLine.ParentStaff === instrument.Staves[0]) {
  188. firstStaffLine = staffLine;
  189. }
  190. if (staffLine.ParentStaff === instrument.Staves[instrument.Staves.length - 1]) {
  191. lastStaffLine = staffLine;
  192. }
  193. }
  194. if (firstStaffLine !== undefined && lastStaffLine !== undefined) {
  195. this.createInstrumentBracket(firstStaffLine, lastStaffLine);
  196. }
  197. }
  198. }
  199. }
  200. /**
  201. * Create a GroupBracket for an [[InstrumentalGroup]].
  202. * @param instrumentGroups
  203. * @param staffHeight
  204. * @param recursionDepth
  205. */
  206. public createGroupBrackets(instrumentGroups: InstrumentalGroup[], staffHeight: number, recursionDepth: number): void {
  207. for (let idx: number = 0, len: number = instrumentGroups.length; idx < len; ++idx) {
  208. const instrumentGroup: InstrumentalGroup = instrumentGroups[idx];
  209. if (instrumentGroup.InstrumentalGroups.length < 1) {
  210. continue;
  211. }
  212. const instrument1: Instrument = this.findFirstVisibleInstrumentInInstrumentalGroup(instrumentGroup);
  213. const instrument2: Instrument = this.findLastVisibleInstrumentInInstrumentalGroup(instrumentGroup);
  214. if (instrument1 === undefined || instrument2 === undefined) {
  215. continue;
  216. }
  217. let firstStaffLine: StaffLine = undefined;
  218. for (let idx2: number = 0, len2: number = this.staffLines.length; idx2 < len2; ++idx2) {
  219. const staffLine: StaffLine = this.staffLines[idx2];
  220. if (staffLine.ParentStaff === instrument1.Staves[0]) {
  221. firstStaffLine = staffLine;
  222. }
  223. }
  224. if (firstStaffLine !== undefined && firstStaffLine !== undefined) {
  225. this.createGroupBracket(firstStaffLine, firstStaffLine, recursionDepth);
  226. }
  227. if (instrumentGroup.InstrumentalGroups.length < 1) {
  228. continue;
  229. }
  230. this.createGroupBrackets(instrumentGroup.InstrumentalGroups, staffHeight, recursionDepth + 1);
  231. }
  232. }
  233. /**
  234. * Create the Instrument's Labels (only for the first [[MusicSystem]] of the first MusicPage).
  235. * @param instrumentLabelTextHeight
  236. * @param systemLabelsRightMargin
  237. * @param labelMarginBorderFactor
  238. */
  239. public createMusicSystemLabel(instrumentLabelTextHeight: number, systemLabelsRightMargin: number, labelMarginBorderFactor: number): void {
  240. if (this.parent === this.parent.Parent.MusicPages[0] && this === this.parent.MusicSystems[0]) {
  241. const instruments: Instrument[] = this.parent.Parent.ParentMusicSheet.getVisibleInstruments();
  242. for (let idx: number = 0, len: number = instruments.length; idx < len; ++idx) {
  243. const instrument: Instrument = instruments[idx];
  244. const graphicalLabel: GraphicalLabel = new GraphicalLabel(
  245. instrument.NameLabel, instrumentLabelTextHeight, TextAlignment.LeftCenter, this.boundingBox
  246. );
  247. graphicalLabel.setLabelPositionAndShapeBorders();
  248. this.labels.setValue(graphicalLabel, instrument);
  249. //graphicalLabel.PositionAndShape.Parent = this.PositionAndShape;
  250. // X-Position will be 0 (Label starts at the same PointF_2D with MusicSystem)
  251. // Y-Position will be calculated after the y-Spacing
  252. graphicalLabel.PositionAndShape.RelativePosition = new PointF2D(0.0, 0.0);
  253. }
  254. // calculate maxLabelLength (needed for X-Spacing)
  255. this.maxLabelLength = 0.0;
  256. const labels: GraphicalLabel[] = this.labels.keys();
  257. for (let idx: number = 0, len: number = labels.length; idx < len; ++idx) {
  258. const label: GraphicalLabel = labels[idx];
  259. if (label.PositionAndShape.Size.width > this.maxLabelLength) {
  260. this.maxLabelLength = label.PositionAndShape.Size.width;
  261. }
  262. }
  263. this.updateMusicSystemStaffLineXPosition(systemLabelsRightMargin);
  264. }
  265. }
  266. /**
  267. * Set the Y-Positions for the MusicSystem's Labels.
  268. */
  269. public setMusicSystemLabelsYPosition(): void {
  270. if (this.parent === this.parent.Parent.MusicPages[0] && this === this.parent.MusicSystems[0]) {
  271. this.labels.forEach((key: GraphicalLabel, value: Instrument): void => {
  272. let ypositionSum: number = 0;
  273. let staffCounter: number = 0;
  274. for (let i: number = 0; i < this.staffLines.length; i++) {
  275. if (this.staffLines[i].ParentStaff.ParentInstrument === value) {
  276. for (let j: number = i; j < this.staffLines.length; j++) {
  277. const staffLine: StaffLine = this.staffLines[j];
  278. if (staffLine.ParentStaff.ParentInstrument !== value) {
  279. break;
  280. }
  281. ypositionSum += staffLine.PositionAndShape.RelativePosition.y;
  282. staffCounter++;
  283. }
  284. break;
  285. }
  286. }
  287. if (staffCounter > 0) {
  288. key.PositionAndShape.RelativePosition = new PointF2D(0.0, ypositionSum / staffCounter + 2.0);
  289. }
  290. });
  291. }
  292. }
  293. /**
  294. * Check if two "adjacent" StaffLines have BOTH a StaffEntry with a StaffEntryLink.
  295. * This is needed for the y-spacing algorithm.
  296. * @returns {boolean}
  297. */
  298. public checkStaffEntriesForStaffEntryLink(): boolean {
  299. let first: boolean = false;
  300. let second: boolean = false;
  301. for (let i: number = 0; i < this.staffLines.length - 1; i++) {
  302. for (let idx: number = 0, len: number = this.staffLines[i].Measures.length; idx < len; ++idx) {
  303. const measure: StaffMeasure = this.staffLines[i].Measures[idx];
  304. for (let idx2: number = 0, len2: number = measure.staffEntries.length; idx2 < len2; ++idx2) {
  305. const staffEntry: GraphicalStaffEntry = measure.staffEntries[idx2];
  306. if (staffEntry.sourceStaffEntry.Link !== undefined) {
  307. first = true;
  308. }
  309. }
  310. }
  311. for (let idx: number = 0, len: number = this.staffLines[i + 1].Measures.length; idx < len; ++idx) {
  312. const measure: StaffMeasure = this.staffLines[i + 1].Measures[idx];
  313. for (let idx2: number = 0, len2: number = measure.staffEntries.length; idx2 < len2; ++idx2) {
  314. const staffEntry: GraphicalStaffEntry = measure.staffEntries[idx2];
  315. if (staffEntry.sourceStaffEntry.Link !== undefined) {
  316. second = true;
  317. }
  318. }
  319. }
  320. }
  321. if (first && second) {
  322. return true;
  323. }
  324. return false;
  325. }
  326. public getBottomStaffLine(topStaffLine: StaffLine): StaffLine {
  327. const staves: Staff[] = topStaffLine.ParentStaff.ParentInstrument.Staves;
  328. const last: Staff = staves[staves.length - 1];
  329. for (const line of topStaffLine.ParentMusicSystem.staffLines) {
  330. if (line.ParentStaff === last) {
  331. return line;
  332. }
  333. }
  334. return undefined;
  335. }
  336. /**
  337. * Here the system line is generated, which acts as the container of graphical lines and dots that will be finally rendered.
  338. * It holds al the logical parameters of the system line.
  339. * @param xPosition The x position within the system
  340. * @param lineWidth The total x width
  341. * @param lineType The line type enum
  342. * @param linePosition indicates if the line belongs to start or end of measure
  343. * @param musicSystem
  344. * @param topMeasure
  345. * @param bottomMeasure
  346. */
  347. protected createSystemLine(xPosition: number, lineWidth: number, lineType: SystemLinesEnum, linePosition: SystemLinePosition,
  348. musicSystem: MusicSystem, topMeasure: StaffMeasure, bottomMeasure: StaffMeasure = undefined): SystemLine {
  349. throw new Error("not implemented");
  350. }
  351. /**
  352. * Create all the graphical lines and dots needed to render a system line (e.g. bold-thin-dots..).
  353. * @param systemLine
  354. */
  355. protected createLinesForSystemLine(systemLine: SystemLine): void {
  356. //Empty
  357. }
  358. /**
  359. * Calculates the summed x-width of a possibly given Instrument Brace and/or Group Bracket(s).
  360. * @returns {number} the x-width
  361. */
  362. protected calcBracketsWidth(): number {
  363. let width: number = 0;
  364. for (let idx: number = 0, len: number = this.GroupBrackets.length; idx < len; ++idx) {
  365. const groupBracket: GraphicalObject = this.GroupBrackets[idx];
  366. width = Math.max(width, groupBracket.PositionAndShape.Size.width);
  367. }
  368. for (let idx2: number = 0, len2: number = this.InstrumentBrackets.length; idx2 < len2; ++idx2) {
  369. const instrumentBracket: GraphicalObject = this.InstrumentBrackets[idx2];
  370. width = Math.max(width, instrumentBracket.PositionAndShape.Size.width);
  371. }
  372. return width;
  373. }
  374. protected createInstrumentBracket(firstStaffLine: StaffLine, lastStaffLine: StaffLine): void {
  375. // no impl here
  376. }
  377. protected createGroupBracket(firstStaffLine: StaffLine, lastStaffLine: StaffLine, recursionDepth: number): void {
  378. // no impl here
  379. }
  380. private findFirstVisibleInstrumentInInstrumentalGroup(instrumentalGroup: InstrumentalGroup): Instrument {
  381. for (let idx: number = 0, len: number = instrumentalGroup.InstrumentalGroups.length; idx < len; ++idx) {
  382. const groupOrInstrument: InstrumentalGroup = instrumentalGroup.InstrumentalGroups[idx];
  383. if (groupOrInstrument instanceof Instrument) {
  384. if ((<Instrument>groupOrInstrument).Visible === true) {
  385. return <Instrument>groupOrInstrument;
  386. }
  387. continue;
  388. }
  389. return this.findFirstVisibleInstrumentInInstrumentalGroup(groupOrInstrument);
  390. }
  391. return undefined;
  392. }
  393. private findLastVisibleInstrumentInInstrumentalGroup(instrumentalGroup: InstrumentalGroup): Instrument {
  394. let groupOrInstrument: InstrumentalGroup;
  395. for (let i: number = instrumentalGroup.InstrumentalGroups.length - 1; i >= 0; i--) {
  396. groupOrInstrument = instrumentalGroup.InstrumentalGroups[i];
  397. if (groupOrInstrument instanceof Instrument) {
  398. if ((<Instrument>groupOrInstrument).Visible === true) {
  399. return <Instrument>groupOrInstrument;
  400. }
  401. continue;
  402. }
  403. return this.findLastVisibleInstrumentInInstrumentalGroup(groupOrInstrument);
  404. }
  405. return undefined;
  406. }
  407. /**
  408. * Update the xPosition of the [[MusicSystem]]'s [[StaffLine]]'s due to [[Label]] positioning.
  409. * @param systemLabelsRightMargin
  410. */
  411. private updateMusicSystemStaffLineXPosition(systemLabelsRightMargin: number): void {
  412. for (let idx: number = 0, len: number = this.StaffLines.length; idx < len; ++idx) {
  413. const staffLine: StaffLine = this.StaffLines[idx];
  414. const relative: PointF2D = staffLine.PositionAndShape.RelativePosition;
  415. relative.x = this.maxLabelLength + systemLabelsRightMargin;
  416. staffLine.PositionAndShape.RelativePosition = relative;
  417. staffLine.PositionAndShape.BorderRight = this.boundingBox.Size.width - this.maxLabelLength - systemLabelsRightMargin;
  418. for (let i: number = 0; i < staffLine.StaffLines.length; i++) {
  419. const lineEnd: PointF2D = new PointF2D(staffLine.PositionAndShape.Size.width, staffLine.StaffLines[i].End.y);
  420. staffLine.StaffLines[i].End = lineEnd;
  421. }
  422. }
  423. }
  424. }