MusicSheetDrawer.ts 30 KB

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