MusicSheetDrawer.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. import {EngravingRules} from "./EngravingRules";
  2. import {ITextMeasurer} from "../Interfaces/ITextMeasurer";
  3. import {GraphicalMusicSheet} from "./GraphicalMusicSheet";
  4. import {BoundingBox} from "./BoundingBox";
  5. import {GraphicalLayers, OutlineAndFillStyleEnum} from "./DrawingEnums";
  6. import {DrawingParameters} from "./DrawingParameters";
  7. import {GraphicalLine} from "./GraphicalLine";
  8. import {RectangleF2D} from "../../Common/DataObjects/RectangleF2D";
  9. import {PointF2D} from "../../Common/DataObjects/PointF2D";
  10. import {GraphicalRectangle} from "./GraphicalRectangle";
  11. import {GraphicalLabel} from "./GraphicalLabel";
  12. import {Label} from "../Label";
  13. import {TextAlignmentAndPlacement} from "../../Common/Enums/TextAlignment";
  14. import {ArgumentOutOfRangeException} from "../Exceptions";
  15. import {SelectionStartSymbol} from "./SelectionStartSymbol";
  16. import {SelectionEndSymbol} from "./SelectionEndSymbol";
  17. import {MusicSystem} from "./MusicSystem";
  18. import {GraphicalMeasure} from "./GraphicalMeasure";
  19. import {StaffLine} from "./StaffLine";
  20. import {SystemLine} from "./SystemLine";
  21. import {MusicSymbol} from "./MusicSymbol";
  22. import {GraphicalMusicPage} from "./GraphicalMusicPage";
  23. import {Instrument} from "../Instrument";
  24. import {MusicSymbolDrawingStyle, PhonicScoreModes} from "./DrawingMode";
  25. import {GraphicalObject} from "./GraphicalObject";
  26. import { GraphicalInstantaneousDynamicExpression } from "./GraphicalInstantaneousDynamicExpression";
  27. import { unitInPixels } from "./VexFlow/VexFlowMusicSheetDrawer";
  28. /**
  29. * Draw a [[GraphicalMusicSheet]] (through the .drawSheet method)
  30. *
  31. * The drawing is implemented with a top-down approach, starting from a music sheet, going through pages, systems, staffs...
  32. * ... and ending in notes, beams, accidentals and other symbols.
  33. * It's worth to say, that this class just draws the symbols and graphical elements, using the positions that have been computed before.
  34. * But in any case, some of these previous positioning algorithms need the sizes of the concrete symbols (NoteHeads, sharps, flats, keys...).
  35. * Therefore, there are some static functions on the 'Bounding Boxes' section used to compute these symbol boxes at the
  36. * beginning for the later use in positioning algorithms.
  37. *
  38. * This class also includes the resizing and positioning of the symbols due to user interaction like zooming or panning.
  39. */
  40. export abstract class MusicSheetDrawer {
  41. public drawingParameters: DrawingParameters = new DrawingParameters();
  42. public splitScreenLineColor: number;
  43. public midiPlaybackAvailable: boolean;
  44. public drawableBoundingBoxElement: string = process.env.DRAW_BOUNDING_BOX_ELEMENT;
  45. public skyLineVisible: boolean = false;
  46. public bottomLineVisible: boolean = false;
  47. protected rules: EngravingRules;
  48. protected graphicalMusicSheet: GraphicalMusicSheet;
  49. protected textMeasurer: ITextMeasurer;
  50. private phonicScoreMode: PhonicScoreModes = PhonicScoreModes.Manual;
  51. constructor(textMeasurer: ITextMeasurer,
  52. isPreviewImageDrawer: boolean = false) {
  53. this.textMeasurer = textMeasurer;
  54. this.splitScreenLineColor = -1;
  55. if (isPreviewImageDrawer) {
  56. this.drawingParameters.setForThumbnail();
  57. } else {
  58. this.drawingParameters.setForAllOn();
  59. }
  60. }
  61. public set Mode(value: PhonicScoreModes) {
  62. this.phonicScoreMode = value;
  63. }
  64. public drawSheet(graphicalMusicSheet: GraphicalMusicSheet): void {
  65. this.graphicalMusicSheet = graphicalMusicSheet;
  66. this.rules = graphicalMusicSheet.ParentMusicSheet.Rules;
  67. this.drawSplitScreenLine();
  68. if (this.drawingParameters.drawCursors) {
  69. for (const line of graphicalMusicSheet.Cursors) {
  70. const psi: BoundingBox = new BoundingBox(line);
  71. psi.AbsolutePosition = line.Start;
  72. psi.BorderBottom = line.End.y - line.Start.y;
  73. psi.BorderRight = line.Width / 2.0;
  74. psi.BorderLeft = -line.Width / 2.0;
  75. if (this.isVisible(psi)) {
  76. this.drawLineAsVerticalRectangle(line, <number>GraphicalLayers.Cursor);
  77. }
  78. }
  79. }
  80. // Draw the vertical ScrollIndicator
  81. if (this.drawingParameters.drawScrollIndicator) {
  82. this.drawScrollIndicator();
  83. }
  84. // Draw all the pages
  85. for (const page of this.graphicalMusicSheet.MusicPages) {
  86. this.drawPage(page);
  87. }
  88. }
  89. public drawLineAsHorizontalRectangle(line: GraphicalLine, layer: number): void {
  90. let rectangle: RectangleF2D = new RectangleF2D(line.Start.x, line.End.y - line.Width / 2, line.End.x - line.Start.x, line.Width);
  91. rectangle = this.applyScreenTransformationForRect(rectangle);
  92. this.renderRectangle(rectangle, layer, line.styleId);
  93. }
  94. public drawLineAsVerticalRectangle(line: GraphicalLine, layer: number): void {
  95. const lineStart: PointF2D = line.Start;
  96. const lineWidth: number = line.Width;
  97. let rectangle: RectangleF2D = new RectangleF2D(lineStart.x - lineWidth / 2, lineStart.y, lineWidth, line.End.y - lineStart.y);
  98. rectangle = this.applyScreenTransformationForRect(rectangle);
  99. this.renderRectangle(rectangle, layer, line.styleId);
  100. }
  101. public drawLineAsHorizontalRectangleWithOffset(line: GraphicalLine, offset: PointF2D, layer: number): void {
  102. const start: PointF2D = new PointF2D(line.Start.x + offset.x, line.Start.y + offset.y);
  103. const end: PointF2D = new PointF2D(line.End.x + offset.x, line.End.y + offset.y);
  104. const width: number = line.Width;
  105. let rectangle: RectangleF2D = new RectangleF2D(start.x, end.y - width / 2, end.x - start.x, width);
  106. rectangle = this.applyScreenTransformationForRect(rectangle);
  107. this.renderRectangle(rectangle, layer, line.styleId);
  108. }
  109. public drawLineAsVerticalRectangleWithOffset(line: GraphicalLine, offset: PointF2D, layer: number): void {
  110. const start: PointF2D = new PointF2D(line.Start.x + offset.x, line.Start.y + offset.y);
  111. const end: PointF2D = new PointF2D(line.End.x + offset.x, line.End.y + offset.y);
  112. const width: number = line.Width;
  113. let rectangle: RectangleF2D = new RectangleF2D(start.x, start.y, width, end.y - start.y);
  114. rectangle = this.applyScreenTransformationForRect(rectangle);
  115. this.renderRectangle(rectangle, layer, line.styleId);
  116. }
  117. public drawRectangle(rect: GraphicalRectangle, layer: number): void {
  118. const psi: BoundingBox = rect.PositionAndShape;
  119. let rectangle: RectangleF2D = new RectangleF2D(psi.AbsolutePosition.x, psi.AbsolutePosition.y, psi.BorderRight, psi.BorderBottom);
  120. rectangle = this.applyScreenTransformationForRect(rectangle);
  121. this.renderRectangle(rectangle, layer, <number>rect.style);
  122. }
  123. public calculatePixelDistance(unitDistance: number): number {
  124. throw new Error("not implemented");
  125. }
  126. public drawLabel(graphicalLabel: GraphicalLabel, layer: number): void {
  127. if (!this.isVisible(graphicalLabel.PositionAndShape)) {
  128. return;
  129. }
  130. const label: Label = graphicalLabel.Label;
  131. if (label.text.trim() === "") {
  132. return;
  133. }
  134. const screenPosition: PointF2D = this.applyScreenTransformation(graphicalLabel.PositionAndShape.AbsolutePosition);
  135. const heightInPixel: number = this.calculatePixelDistance(label.fontHeight);
  136. const widthInPixel: number = heightInPixel * this.textMeasurer.computeTextWidthToHeightRatio(label.text, label.font, label.fontStyle);
  137. const bitmapWidth: number = Math.ceil(widthInPixel);
  138. const bitmapHeight: number = Math.ceil(heightInPixel * 1.2);
  139. switch (label.textAlignment) {
  140. // the following have to match the Border settings in GraphicalLabel.setLabelPositionAndShapeBorders()
  141. // TODO unify alignment shifts and our label/bbox position, which does not correspond to screenposition
  142. case TextAlignmentAndPlacement.LeftTop:
  143. break;
  144. case TextAlignmentAndPlacement.LeftCenter:
  145. screenPosition.y -= bitmapHeight / 2;
  146. break;
  147. case TextAlignmentAndPlacement.LeftBottom:
  148. screenPosition.y -= bitmapHeight;
  149. screenPosition.x -= unitInPixels; // lyrics-specific to align with notes
  150. break;
  151. case TextAlignmentAndPlacement.CenterTop:
  152. screenPosition.x -= bitmapWidth / 2;
  153. break;
  154. case TextAlignmentAndPlacement.CenterCenter:
  155. screenPosition.x -= bitmapWidth / 2;
  156. screenPosition.y -= bitmapHeight / 2;
  157. break;
  158. case TextAlignmentAndPlacement.CenterBottom:
  159. screenPosition.x -= bitmapWidth / 2;
  160. screenPosition.y -= bitmapHeight;
  161. break;
  162. case TextAlignmentAndPlacement.RightTop:
  163. screenPosition.x -= bitmapWidth;
  164. break;
  165. case TextAlignmentAndPlacement.RightCenter:
  166. screenPosition.x -= bitmapWidth;
  167. screenPosition.y -= bitmapHeight / 2;
  168. break;
  169. case TextAlignmentAndPlacement.RightBottom:
  170. screenPosition.x -= bitmapWidth;
  171. screenPosition.y -= bitmapHeight;
  172. break;
  173. default:
  174. throw new ArgumentOutOfRangeException("");
  175. }
  176. this.renderLabel(graphicalLabel, layer, bitmapWidth, bitmapHeight, heightInPixel, screenPosition);
  177. }
  178. protected applyScreenTransformation(point: PointF2D): PointF2D {
  179. throw new Error("not implemented");
  180. }
  181. protected applyScreenTransformations(points: PointF2D[]): PointF2D[] {
  182. const transformedPoints: PointF2D[] = [];
  183. for (const point of points) {
  184. transformedPoints.push(this.applyScreenTransformation(point));
  185. }
  186. return transformedPoints;
  187. }
  188. protected applyScreenTransformationForRect(rectangle: RectangleF2D): RectangleF2D {
  189. throw new Error("not implemented");
  190. }
  191. protected drawSplitScreenLine(): void {
  192. // empty
  193. }
  194. protected renderRectangle(rectangle: RectangleF2D, layer: number, styleId: number, alpha: number = 1): void {
  195. throw new Error("not implemented");
  196. }
  197. protected drawScrollIndicator(): void {
  198. // empty
  199. }
  200. protected drawSelectionStartSymbol(symbol: SelectionStartSymbol): void {
  201. // empty
  202. }
  203. protected drawSelectionEndSymbol(symbol: SelectionEndSymbol): void {
  204. // empty
  205. }
  206. protected renderLabel(graphicalLabel: GraphicalLabel, layer: number, bitmapWidth: number,
  207. bitmapHeight: number, heightInPixel: number, screenPosition: PointF2D): void {
  208. throw new Error("not implemented");
  209. }
  210. protected renderSystemToScreen(system: MusicSystem, systemBoundingBoxInPixels: RectangleF2D,
  211. absBoundingRectWithMargin: RectangleF2D): void {
  212. // empty
  213. }
  214. protected drawMeasure(measure: GraphicalMeasure): void {
  215. throw new Error("not implemented");
  216. }
  217. protected drawSkyLine(staffLine: StaffLine): void {
  218. // empty
  219. }
  220. protected drawBottomLine(staffLine: StaffLine): void {
  221. // empty
  222. }
  223. protected drawInstrumentBrace(brace: GraphicalObject, system: MusicSystem): void {
  224. // empty
  225. }
  226. protected drawGroupBracket(bracket: GraphicalObject, system: MusicSystem): void {
  227. // empty
  228. }
  229. protected isVisible(psi: BoundingBox): boolean {
  230. return true;
  231. }
  232. protected drawMusicSystem(system: MusicSystem): void {
  233. const absBoundingRectWithMargin: RectangleF2D = this.getSystemAbsBoundingRect(system);
  234. const systemBoundingBoxInPixels: RectangleF2D = this.getSytemBoundingBoxInPixels(absBoundingRectWithMargin);
  235. this.drawMusicSystemComponents(system, systemBoundingBoxInPixels, absBoundingRectWithMargin);
  236. }
  237. protected getSytemBoundingBoxInPixels(absBoundingRectWithMargin: RectangleF2D): RectangleF2D {
  238. const systemBoundingBoxInPixels: RectangleF2D = this.applyScreenTransformationForRect(absBoundingRectWithMargin);
  239. systemBoundingBoxInPixels.x = Math.round(systemBoundingBoxInPixels.x);
  240. systemBoundingBoxInPixels.y = Math.round(systemBoundingBoxInPixels.y);
  241. return systemBoundingBoxInPixels;
  242. }
  243. protected getSystemAbsBoundingRect(system: MusicSystem): RectangleF2D {
  244. const relBoundingRect: RectangleF2D = system.PositionAndShape.BoundingRectangle;
  245. const absBoundingRectWithMargin: RectangleF2D = new RectangleF2D(
  246. system.PositionAndShape.AbsolutePosition.x + system.PositionAndShape.BorderLeft - 1,
  247. system.PositionAndShape.AbsolutePosition.y + system.PositionAndShape.BorderTop - 1,
  248. (relBoundingRect.width + 6), (relBoundingRect.height + 2)
  249. );
  250. return absBoundingRectWithMargin;
  251. }
  252. protected drawMusicSystemComponents(musicSystem: MusicSystem, systemBoundingBoxInPixels: RectangleF2D,
  253. absBoundingRectWithMargin: RectangleF2D): void {
  254. const selectStartSymb: SelectionStartSymbol = this.graphicalMusicSheet.SelectionStartSymbol;
  255. const selectEndSymb: SelectionEndSymbol = this.graphicalMusicSheet.SelectionEndSymbol;
  256. if (this.drawingParameters.drawSelectionStartSymbol) {
  257. if (selectStartSymb !== undefined && this.isVisible(selectStartSymb.PositionAndShape)) {
  258. this.drawSelectionStartSymbol(selectStartSymb);
  259. }
  260. }
  261. if (this.drawingParameters.drawSelectionEndSymbol) {
  262. if (selectEndSymb !== undefined && this.isVisible(selectEndSymb.PositionAndShape)) {
  263. this.drawSelectionEndSymbol(selectEndSymb);
  264. }
  265. }
  266. for (const staffLine of musicSystem.StaffLines) {
  267. this.drawStaffLine(staffLine);
  268. // draw lyric dashes
  269. if (staffLine.LyricsDashes.length > 0) {
  270. this.drawDashes(staffLine.LyricsDashes);
  271. }
  272. // draw lyric lines (e.g. LyricExtends: "dich,___")
  273. if (staffLine.LyricLines.length > 0) {
  274. this.drawLyricLines(staffLine.LyricLines, staffLine);
  275. }
  276. }
  277. for (const systemLine of musicSystem.SystemLines) {
  278. this.drawSystemLineObject(systemLine);
  279. }
  280. if (musicSystem === musicSystem.Parent.MusicSystems[0] && musicSystem.Parent === musicSystem.Parent.Parent.MusicPages[0]) {
  281. for (const label of musicSystem.Labels) {
  282. this.drawLabel(label, <number>GraphicalLayers.Notes);
  283. }
  284. }
  285. for (const bracket of musicSystem.InstrumentBrackets) {
  286. this.drawInstrumentBrace(bracket, musicSystem);
  287. }
  288. for (const bracket of musicSystem.GroupBrackets) {
  289. this.drawGroupBracket(bracket, musicSystem);
  290. }
  291. if (!this.leadSheet) {
  292. for (const measureNumberLabel of musicSystem.MeasureNumberLabels) {
  293. this.drawLabel(measureNumberLabel, <number>GraphicalLayers.Notes);
  294. }
  295. }
  296. for (const staffLine of musicSystem.StaffLines) {
  297. this.drawStaffLineSymbols(staffLine);
  298. }
  299. if (this.drawingParameters.drawMarkedAreas) {
  300. this.drawMarkedAreas(musicSystem);
  301. }
  302. if (this.drawingParameters.drawComments) {
  303. this.drawComment(musicSystem);
  304. }
  305. }
  306. protected activateSystemRendering(systemId: number, absBoundingRect: RectangleF2D,
  307. systemBoundingBoxInPixels: RectangleF2D, createNewImage: boolean): boolean {
  308. return true;
  309. }
  310. protected drawSystemLineObject(systemLine: SystemLine): void {
  311. // empty
  312. }
  313. protected drawStaffLine(staffLine: StaffLine): void {
  314. for (const measure of staffLine.Measures) {
  315. this.drawMeasure(measure);
  316. }
  317. if (staffLine.LyricsDashes.length > 0) {
  318. this.drawDashes(staffLine.LyricsDashes);
  319. }
  320. this.drawOctaveShifts(staffLine);
  321. this.drawExpressions(staffLine);
  322. if (this.skyLineVisible) {
  323. this.drawSkyLine(staffLine);
  324. }
  325. if (this.bottomLineVisible) {
  326. this.drawBottomLine(staffLine);
  327. }
  328. }
  329. protected drawLyricLines(lyricLines: GraphicalLine[], staffLine: StaffLine): void {
  330. staffLine.LyricLines.forEach(lyricLine => {
  331. // TODO maybe we should put this in the calculation (MusicSheetCalculator.calculateLyricExtend)
  332. // then we can also remove staffLine argument
  333. // but same addition doesn't work in calculateLyricExtend, because y-spacing happens after lyrics positioning
  334. lyricLine.Start.y += staffLine.PositionAndShape.AbsolutePosition.y;
  335. lyricLine.End.y += staffLine.PositionAndShape.AbsolutePosition.y;
  336. lyricLine.Start.x += staffLine.PositionAndShape.AbsolutePosition.x;
  337. lyricLine.End.x += staffLine.PositionAndShape.AbsolutePosition.x;
  338. this.drawGraphicalLine(lyricLine, EngravingRules.Rules.LyricUnderscoreLineWidth);
  339. });
  340. }
  341. protected drawExpressions(staffline: StaffLine): void {
  342. // implemented by subclass (VexFlowMusicSheetDrawer)
  343. }
  344. protected drawGraphicalLine(graphicalLine: GraphicalLine, lineWidth: number, colorOrStyle: string = "black"): void {
  345. /* TODO similar checks as in drawLabel
  346. if (!this.isVisible(new BoundingBox(graphicalLine.Start,)) {
  347. return;
  348. }
  349. */
  350. this.drawLine(graphicalLine.Start, graphicalLine.End, colorOrStyle, lineWidth);
  351. }
  352. public drawLine(start: PointF2D, stop: PointF2D, color: string = "#FF0000FF", lineWidth: number): void {
  353. // implemented by subclass (VexFlowMusicSheetDrawer)
  354. }
  355. /**
  356. * Draw all dashes to the canvas
  357. * @param lyricsDashes Array of lyric dashes to be drawn
  358. * @param layer Number of the layer that the lyrics should be drawn in
  359. */
  360. protected drawDashes(lyricsDashes: GraphicalLabel[]): void {
  361. lyricsDashes.forEach(dash => this.drawLabel(dash, <number>GraphicalLayers.Notes));
  362. }
  363. // protected drawSlur(slur: GraphicalSlur, abs: PointF2D): void {
  364. //
  365. // }
  366. protected drawOctaveShifts(staffLine: StaffLine): void {
  367. return;
  368. }
  369. protected drawStaffLines(staffLine: StaffLine): void {
  370. if (staffLine.StaffLines !== undefined) {
  371. const position: PointF2D = staffLine.PositionAndShape.AbsolutePosition;
  372. for (let i: number = 0; i < 5; i++) {
  373. this.drawLineAsHorizontalRectangleWithOffset(staffLine.StaffLines[i], position, <number>GraphicalLayers.Notes);
  374. }
  375. }
  376. }
  377. // protected drawEnding(ending: GraphicalRepetitionEnding, absolutePosition: PointF2D): void {
  378. // if (undefined !== ending.Left)
  379. // drawLineAsVerticalRectangle(ending.Left, absolutePosition, <number>GraphicalLayers.Notes);
  380. // this.drawLineAsHorizontalRectangle(ending.Top, absolutePosition, <number>GraphicalLayers.Notes);
  381. // if (undefined !== ending.Right)
  382. // drawLineAsVerticalRectangle(ending.Right, absolutePosition, <number>GraphicalLayers.Notes);
  383. // this.drawLabel(ending.Label, <number>GraphicalLayers.Notes);
  384. // }
  385. protected drawInstantaneousDynamic(instantaneousDynamic: GraphicalInstantaneousDynamicExpression): void {
  386. // expression.ExpressionSymbols.forEach(function (expressionSymbol) {
  387. // let position: PointF2D = expressionSymbol.PositionAndShape.AbsolutePosition;
  388. // let symbol: MusicSymbol = expressionSymbol.GetSymbol;
  389. // drawSymbol(symbol, MusicSymbolDrawingStyle.Normal, position);
  390. // });
  391. }
  392. // protected drawContinuousDynamic(expression: GraphicalContinuousDynamicExpression,
  393. // absolute: PointF2D): void {
  394. // throw new Error("not implemented");
  395. // }
  396. protected drawSymbol(symbol: MusicSymbol, symbolStyle: MusicSymbolDrawingStyle, position: PointF2D,
  397. scalingFactor: number = 1, layer: number = <number>GraphicalLayers.Notes): void {
  398. //empty
  399. }
  400. protected get leadSheet(): boolean {
  401. return this.graphicalMusicSheet.LeadSheet;
  402. }
  403. protected set leadSheet(value: boolean) {
  404. this.graphicalMusicSheet.LeadSheet = value;
  405. }
  406. private drawPage(page: GraphicalMusicPage): void {
  407. if (!this.isVisible(page.PositionAndShape)) {
  408. return;
  409. }
  410. for (const system of page.MusicSystems) {
  411. if (this.isVisible(system.PositionAndShape)) {
  412. this.drawMusicSystem(system);
  413. }
  414. }
  415. if (page === page.Parent.MusicPages[0]) {
  416. for (const label of page.Labels) {
  417. this.drawLabel(label, <number>GraphicalLayers.Notes);
  418. }
  419. }
  420. // Draw bounding boxes for debug purposes. This has to be at the end because only
  421. // then all the calculations and recalculations are done
  422. if (this.drawableBoundingBoxElement) {
  423. this.drawBoundingBoxes(page.PositionAndShape, 0, this.drawableBoundingBoxElement);
  424. }
  425. }
  426. /**
  427. * Draw bounding boxes aroung GraphicalObjects
  428. * @param startBox Bounding Box that is used as a staring point to recursively go through all child elements
  429. * @param layer Layer to draw to
  430. * @param type Type of element to show bounding boxes for as string.
  431. */
  432. private drawBoundingBoxes(startBox: BoundingBox, layer: number = 0, type: string = "all"): void {
  433. const dataObjectString: string = (startBox.DataObject.constructor as any).name;
  434. if (dataObjectString === type || type === "all") {
  435. let tmpRect: RectangleF2D = new RectangleF2D(startBox.AbsolutePosition.x + startBox.BorderMarginLeft,
  436. startBox.AbsolutePosition.y + startBox.BorderMarginTop,
  437. startBox.BorderMarginRight - startBox.BorderMarginLeft,
  438. startBox.BorderMarginBottom - startBox.BorderMarginTop);
  439. this.drawLineAsHorizontalRectangle(new GraphicalLine(
  440. new PointF2D(startBox.AbsolutePosition.x - 1, startBox.AbsolutePosition.y),
  441. new PointF2D(startBox.AbsolutePosition.x + 1, startBox.AbsolutePosition.y),
  442. 0.1,
  443. OutlineAndFillStyleEnum.BaseWritingColor),
  444. layer - 1);
  445. this.drawLineAsVerticalRectangle(new GraphicalLine(
  446. new PointF2D(startBox.AbsolutePosition.x, startBox.AbsolutePosition.y - 1),
  447. new PointF2D(startBox.AbsolutePosition.x, startBox.AbsolutePosition.y + 1),
  448. 0.1,
  449. OutlineAndFillStyleEnum.BaseWritingColor),
  450. layer - 1);
  451. tmpRect = this.applyScreenTransformationForRect(tmpRect);
  452. this.renderRectangle(tmpRect, <number>GraphicalLayers.Background, layer, 0.5);
  453. this.renderLabel(new GraphicalLabel(new Label(dataObjectString), 0.8, TextAlignmentAndPlacement.CenterCenter),
  454. layer, tmpRect.width, tmpRect.height, tmpRect.height, new PointF2D(tmpRect.x, tmpRect.y + 12));
  455. }
  456. layer++;
  457. startBox.ChildElements.forEach(bb => this.drawBoundingBoxes(bb, layer, type));
  458. }
  459. private drawMarkedAreas(system: MusicSystem): void {
  460. for (const markedArea of system.GraphicalMarkedAreas) {
  461. if (markedArea !== undefined) {
  462. if (markedArea.systemRectangle !== undefined) {
  463. this.drawRectangle(markedArea.systemRectangle, <number>GraphicalLayers.Background);
  464. }
  465. if (markedArea.settings !== undefined) {
  466. this.drawLabel(markedArea.settings, <number>GraphicalLayers.Comment);
  467. }
  468. if (markedArea.labelRectangle !== undefined) {
  469. this.drawRectangle(markedArea.labelRectangle, <number>GraphicalLayers.Background);
  470. }
  471. if (markedArea.label !== undefined) {
  472. this.drawLabel(markedArea.label, <number>GraphicalLayers.Comment);
  473. }
  474. }
  475. }
  476. }
  477. private drawComment(system: MusicSystem): void {
  478. for (const comment of system.GraphicalComments) {
  479. if (comment !== undefined) {
  480. if (comment.settings !== undefined) {
  481. this.drawLabel(comment.settings, <number>GraphicalLayers.Comment);
  482. }
  483. if (comment.label !== undefined) {
  484. this.drawLabel(comment.label, <number>GraphicalLayers.Comment);
  485. }
  486. }
  487. }
  488. }
  489. private drawStaffLineSymbols(staffLine: StaffLine): void {
  490. const parentInst: Instrument = staffLine.ParentStaff.ParentInstrument;
  491. const absX: number = staffLine.PositionAndShape.AbsolutePosition.x;
  492. const absY: number = staffLine.PositionAndShape.AbsolutePosition.y + 2;
  493. const borderRight: number = staffLine.PositionAndShape.BorderRight;
  494. if (parentInst.highlight && this.drawingParameters.drawHighlights) {
  495. this.drawLineAsHorizontalRectangle(
  496. new GraphicalLine(
  497. new PointF2D(absX, absY),
  498. new PointF2D(absX + borderRight, absY),
  499. 4,
  500. OutlineAndFillStyleEnum.Highlighted
  501. ),
  502. <number>GraphicalLayers.Highlight
  503. );
  504. }
  505. let style: MusicSymbolDrawingStyle = MusicSymbolDrawingStyle.Disabled;
  506. let symbol: MusicSymbol = MusicSymbol.PLAY;
  507. let drawSymbols: boolean = this.drawingParameters.drawActivitySymbols;
  508. switch (this.phonicScoreMode) {
  509. case PhonicScoreModes.Midi:
  510. symbol = MusicSymbol.PLAY;
  511. if (this.midiPlaybackAvailable && staffLine.ParentStaff.audible) {
  512. style = MusicSymbolDrawingStyle.PlaybackSymbols;
  513. }
  514. break;
  515. case PhonicScoreModes.Following:
  516. symbol = MusicSymbol.MIC;
  517. if (staffLine.ParentStaff.following) {
  518. style = MusicSymbolDrawingStyle.FollowSymbols;
  519. }
  520. break;
  521. default:
  522. drawSymbols = false;
  523. break;
  524. }
  525. if (drawSymbols) {
  526. const p: PointF2D = new PointF2D(absX + borderRight + 2, absY);
  527. this.drawSymbol(symbol, style, p);
  528. }
  529. if (this.drawingParameters.drawErrors) {
  530. for (const measure of staffLine.Measures) {
  531. const measurePSI: BoundingBox = measure.PositionAndShape;
  532. const absXPSI: number = measurePSI.AbsolutePosition.x;
  533. const absYPSI: number = measurePSI.AbsolutePosition.y + 2;
  534. if (measure.hasError && this.graphicalMusicSheet.ParentMusicSheet.DrawErroneousMeasures) {
  535. this.drawLineAsHorizontalRectangle(
  536. new GraphicalLine(
  537. new PointF2D(absXPSI, absYPSI),
  538. new PointF2D(absXPSI + measurePSI.BorderRight, absYPSI),
  539. 4,
  540. OutlineAndFillStyleEnum.ErrorUnderlay
  541. ),
  542. <number>GraphicalLayers.MeasureError
  543. );
  544. }
  545. }
  546. }
  547. }
  548. }