MusicSheetDrawer.ts 31 KB

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