VexFlowMusicSheetDrawer.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. import Vex from "vexflow";
  2. import { LabelRenderSpecs, MusicSheetDrawer } from "../MusicSheetDrawer";
  3. import { RectangleF2D } from "../../../Common/DataObjects/RectangleF2D";
  4. import { VexFlowMeasure } from "./VexFlowMeasure";
  5. import { PointF2D } from "../../../Common/DataObjects/PointF2D";
  6. import { GraphicalLabel } from "../GraphicalLabel";
  7. import { VexFlowTextMeasurer } from "./VexFlowTextMeasurer";
  8. import { MusicSystem } from "../MusicSystem";
  9. import { GraphicalObject } from "../GraphicalObject";
  10. import { GraphicalLayers } from "../DrawingEnums";
  11. import { GraphicalStaffEntry } from "../GraphicalStaffEntry";
  12. import { VexFlowBackend } from "./VexFlowBackend";
  13. import { VexFlowOctaveShift } from "./VexFlowOctaveShift";
  14. import { VexFlowInstantaneousDynamicExpression } from "./VexFlowInstantaneousDynamicExpression";
  15. import { VexFlowInstrumentBracket } from "./VexFlowInstrumentBracket";
  16. import { VexFlowInstrumentBrace } from "./VexFlowInstrumentBrace";
  17. import { GraphicalLyricEntry } from "../GraphicalLyricEntry";
  18. import { VexFlowStaffLine } from "./VexFlowStaffLine";
  19. import { StaffLine } from "../StaffLine";
  20. import { GraphicalSlur } from "../GraphicalSlur";
  21. import { PlacementEnum } from "../../VoiceData/Expressions/AbstractExpression";
  22. import { GraphicalInstantaneousTempoExpression } from "../GraphicalInstantaneousTempoExpression";
  23. import { GraphicalInstantaneousDynamicExpression } from "../GraphicalInstantaneousDynamicExpression";
  24. import log from "loglevel";
  25. import { GraphicalContinuousDynamicExpression } from "../GraphicalContinuousDynamicExpression";
  26. import { VexFlowContinuousDynamicExpression } from "./VexFlowContinuousDynamicExpression";
  27. import { DrawingParameters } from "../DrawingParameters";
  28. import { GraphicalMusicPage } from "../GraphicalMusicPage";
  29. import { GraphicalMusicSheet } from "../GraphicalMusicSheet";
  30. import { GraphicalUnknownExpression } from "../GraphicalUnknownExpression";
  31. import { VexFlowPedal } from "./VexFlowPedal";
  32. import { VexflowVibratoBracket } from "./VexflowVibratoBracket";
  33. /**
  34. * This is a global constant which denotes the height in pixels of the space between two lines of the stave
  35. * (when zoom = 1.0)
  36. * @type number
  37. */
  38. export const unitInPixels: number = 10;
  39. export class VexFlowMusicSheetDrawer extends MusicSheetDrawer {
  40. private backend: VexFlowBackend;
  41. private backends: VexFlowBackend[] = [];
  42. private zoom: number = 1.0;
  43. public get Zoom(): number {
  44. return this.zoom;
  45. }
  46. private pageIdx: number = 0; // this is a bad solution, should use MusicPage.PageNumber instead.
  47. constructor(drawingParameters: DrawingParameters = new DrawingParameters()) {
  48. super(new VexFlowTextMeasurer(drawingParameters.Rules), drawingParameters);
  49. }
  50. public get Backends(): VexFlowBackend[] {
  51. return this.backends;
  52. }
  53. protected initializeBackendForPage(page: GraphicalMusicPage): void {
  54. this.backend = this.backends[page.PageNumber - 1];
  55. }
  56. public drawSheet(graphicalMusicSheet: GraphicalMusicSheet): void {
  57. // vexflow 3.x: change default font
  58. if (this.rules.DefaultVexFlowNoteFont === "gonville") {
  59. (Vex.Flow as any).DEFAULT_FONT_STACK = [(Vex.Flow as any).Fonts?.Gonville, (Vex.Flow as any).Fonts?.Bravura, (Vex.Flow as any).Fonts?.Custom];
  60. } // else keep new vexflow default Bravura (more cursive, bold).
  61. // sizing defaults in Vexflow
  62. (Vex.Flow as any).STAVE_LINE_THICKNESS = this.rules.StaffLineWidth * unitInPixels;
  63. (Vex.Flow as any).STEM_WIDTH = this.rules.StemWidth * unitInPixels;
  64. // sets scale/size of notes/rest notes:
  65. (Vex.Flow as any).DEFAULT_NOTATION_FONT_SCALE = this.rules.VexFlowDefaultNotationFontScale; // default 39
  66. (Vex.Flow as any).DEFAULT_TAB_FONT_SCALE = this.rules.VexFlowDefaultTabFontScale; // default 39 // TODO doesn't seem to do anything
  67. this.pageIdx = 0;
  68. for (const graphicalMusicPage of graphicalMusicSheet.MusicPages) {
  69. if (graphicalMusicPage.PageNumber > this.rules.MaxPageToDrawNumber) {
  70. break;
  71. }
  72. const backend: VexFlowBackend = this.backends[this.pageIdx];
  73. backend.graphicalMusicPage = graphicalMusicPage;
  74. backend.scale(this.zoom);
  75. //backend.resize(graphicalMusicSheet.ParentMusicSheet.pageWidth * unitInPixels * this.zoom,
  76. // EngravingRules.Rules.PageHeight * unitInPixels * this.zoom);
  77. this.pageIdx += 1;
  78. }
  79. this.pageIdx = 0;
  80. this.backend = this.backends[0];
  81. super.drawSheet(graphicalMusicSheet);
  82. }
  83. protected drawPage(page: GraphicalMusicPage): void {
  84. if (!page) {
  85. return;
  86. }
  87. this.backend = this.backends[page.PageNumber - 1]; // TODO we may need to set this in a couple of other places. this.pageIdx is a bad solution
  88. super.drawPage(page);
  89. this.pageIdx += 1;
  90. }
  91. public clear(): void {
  92. for (const backend of this.backends) {
  93. backend.clear();
  94. }
  95. }
  96. public setZoom(zoom: number): void {
  97. this.zoom = zoom;
  98. }
  99. /**
  100. * Converts a distance from unit to pixel space.
  101. * @param unitDistance the distance in units
  102. * @returns {number} the distance in pixels
  103. */
  104. public calculatePixelDistance(unitDistance: number): number {
  105. return unitDistance * unitInPixels;
  106. }
  107. protected drawStaffLine(staffLine: StaffLine): void {
  108. const stafflineNode: Node = this.backend.getContext().openGroup();
  109. if (stafflineNode) {
  110. (stafflineNode as SVGGElement).classList.add("staffline");
  111. }
  112. super.drawStaffLine(staffLine);
  113. const absolutePos: PointF2D = staffLine.PositionAndShape.AbsolutePosition;
  114. if (this.rules.RenderSlurs) {
  115. this.drawSlurs(staffLine as VexFlowStaffLine, absolutePos);
  116. }
  117. this.backend.getContext().closeGroup();
  118. }
  119. private drawSlurs(vfstaffLine: VexFlowStaffLine, absolutePos: PointF2D): void {
  120. for (const graphicalSlur of vfstaffLine.GraphicalSlurs) {
  121. // don't draw crossed slurs, as their curve calculation is not implemented yet:
  122. if (graphicalSlur.slur.isCrossed()) {
  123. continue;
  124. }
  125. this.drawSlur(graphicalSlur, absolutePos);
  126. }
  127. }
  128. private drawSlur(graphicalSlur: GraphicalSlur, abs: PointF2D): void {
  129. const curvePointsInPixels: PointF2D[] = [];
  130. // 1) create inner or original curve:
  131. const p1: PointF2D = new PointF2D(graphicalSlur.bezierStartPt.x + abs.x, graphicalSlur.bezierStartPt.y + abs.y);
  132. const p2: PointF2D = new PointF2D(graphicalSlur.bezierStartControlPt.x + abs.x, graphicalSlur.bezierStartControlPt.y + abs.y);
  133. const p3: PointF2D = new PointF2D(graphicalSlur.bezierEndControlPt.x + abs.x, graphicalSlur.bezierEndControlPt.y + abs.y);
  134. const p4: PointF2D = new PointF2D(graphicalSlur.bezierEndPt.x + abs.x, graphicalSlur.bezierEndPt.y + abs.y);
  135. // put screen transformed points into array
  136. curvePointsInPixels.push(this.applyScreenTransformation(p1));
  137. curvePointsInPixels.push(this.applyScreenTransformation(p2));
  138. curvePointsInPixels.push(this.applyScreenTransformation(p3));
  139. curvePointsInPixels.push(this.applyScreenTransformation(p4));
  140. //DEBUG: Render control points
  141. /*
  142. for (const point of curvePointsInPixels) {
  143. const pointRect: RectangleF2D = new RectangleF2D(point.x - 2, point.y - 2, 4, 4);
  144. this.backend.renderRectangle(pointRect, 3, "#000000", 1);
  145. }*/
  146. // 2) create second outer curve to create a thickness for the curve:
  147. if (graphicalSlur.placement === PlacementEnum.Above) {
  148. p1.y -= 0.05;
  149. p2.y -= 0.3;
  150. p3.y -= 0.3;
  151. p4.y -= 0.05;
  152. } else {
  153. p1.y += 0.05;
  154. p2.y += 0.3;
  155. p3.y += 0.3;
  156. p4.y += 0.05;
  157. }
  158. // put screen transformed points into array
  159. curvePointsInPixels.push(this.applyScreenTransformation(p1));
  160. curvePointsInPixels.push(this.applyScreenTransformation(p2));
  161. curvePointsInPixels.push(this.applyScreenTransformation(p3));
  162. curvePointsInPixels.push(this.applyScreenTransformation(p4));
  163. graphicalSlur.SVGElement = this.backend.renderCurve(curvePointsInPixels);
  164. }
  165. protected drawMeasure(measure: VexFlowMeasure): void {
  166. measure.setAbsoluteCoordinates(
  167. measure.PositionAndShape.AbsolutePosition.x * unitInPixels,
  168. measure.PositionAndShape.AbsolutePosition.y * unitInPixels
  169. );
  170. const context: Vex.IRenderContext = this.backend.getContext();
  171. try {
  172. measure.draw(context);
  173. // Vexflow errors can happen here. If we don't catch errors, rendering will stop after this measure.
  174. } catch (ex) {
  175. log.warn("VexFlowMusicSheetDrawer.drawMeasure", ex);
  176. }
  177. // Draw the StaffEntries
  178. for (const staffEntry of measure.staffEntries) {
  179. this.drawStaffEntry(staffEntry);
  180. }
  181. }
  182. // private drawPixel(coord: PointF2D): void {
  183. // coord = this.applyScreenTransformation(coord);
  184. // const ctx: any = this.backend.getContext();
  185. // const oldStyle: string = ctx.fillStyle;
  186. // ctx.fillStyle = "#00FF00FF";
  187. // ctx.fillRect( coord.x, coord.y, 2, 2 );
  188. // ctx.fillStyle = oldStyle;
  189. // }
  190. /** Draws a line in the current backend. Only usable while pages are drawn sequentially, because backend reference is updated in that process.
  191. * To add your own lines after rendering, use DrawOverlayLine.
  192. */
  193. protected drawLine(start: PointF2D, stop: PointF2D, color: string = "#FF0000FF", lineWidth: number = 0.2): Node {
  194. // TODO maybe the backend should be given as an argument here as well, otherwise this can't be used after rendering of multiple pages is done.
  195. start = this.applyScreenTransformation(start);
  196. stop = this.applyScreenTransformation(stop);
  197. /*if (!this.backend) {
  198. this.backend = this.backends[0];
  199. }*/
  200. return this.backend.renderLine(start, stop, color, lineWidth * unitInPixels);
  201. }
  202. /** Lets a user/developer draw an overlay line on the score. Use this instead of drawLine, which is for OSMD internally only.
  203. * The MusicPage has to be specified, because each page and Vexflow backend has its own relative coordinates.
  204. * (the AbsolutePosition of a GraphicalNote is relative to its backend)
  205. * To get a MusicPage, use GraphicalNote.ParentMusicPage.
  206. */
  207. public DrawOverlayLine(start: PointF2D, stop: PointF2D, musicPage: GraphicalMusicPage,
  208. color: string = "#FF0000FF", lineWidth: number = 0.2): Node {
  209. if (!musicPage.PageNumber || musicPage.PageNumber > this.backends.length || musicPage.PageNumber < 1) {
  210. console.log("VexFlowMusicSheetDrawer.drawOverlayLine: invalid page number / music page number doesn't correspond to an existing backend.");
  211. return;
  212. }
  213. const musicPageIndex: number = musicPage.PageNumber - 1;
  214. const backendToUse: VexFlowBackend = this.backends[musicPageIndex];
  215. start = this.applyScreenTransformation(start);
  216. stop = this.applyScreenTransformation(stop);
  217. return backendToUse.renderLine(start, stop, color, lineWidth * unitInPixels);
  218. }
  219. protected drawSkyLine(staffline: StaffLine): void {
  220. const startPosition: PointF2D = staffline.PositionAndShape.AbsolutePosition;
  221. const width: number = staffline.PositionAndShape.Size.width;
  222. this.drawSampledLine(staffline.SkyLine, startPosition, width);
  223. }
  224. protected drawBottomLine(staffline: StaffLine): void {
  225. const startPosition: PointF2D = new PointF2D(staffline.PositionAndShape.AbsolutePosition.x,
  226. staffline.PositionAndShape.AbsolutePosition.y);
  227. const width: number = staffline.PositionAndShape.Size.width;
  228. this.drawSampledLine(staffline.BottomLine, startPosition, width, "#0000FFFF");
  229. }
  230. /**
  231. * Draw a line with a width and start point in a chosen color (used for skyline/bottom line debugging) from
  232. * a simple array
  233. * @param line numeric array. 0 marks the base line. Direction given by sign. Dimensions in units
  234. * @param startPosition Start position in units
  235. * @param width Max line width in units
  236. * @param color Color to paint in. Default is red
  237. */
  238. private drawSampledLine(line: number[], startPosition: PointF2D, width: number, color: string = "#FF0000FF"): void {
  239. const indices: number[] = [];
  240. let currentValue: number = 0;
  241. //Loops through bottom line, grabs all indices that don't equal the previously grabbed index
  242. //Starting with 0 (gets index of all line changes)
  243. for (let i: number = 0; i < line.length; i++) {
  244. if (line[i] !== currentValue) {
  245. indices.push(i);
  246. currentValue = line[i];
  247. }
  248. }
  249. const absolute: PointF2D = startPosition;
  250. if (indices.length > 0) {
  251. const samplingUnit: number = this.rules.SamplingUnit;
  252. let horizontalStart: PointF2D = new PointF2D(absolute.x, absolute.y);
  253. let horizontalEnd: PointF2D = new PointF2D(indices[0] / samplingUnit + absolute.x, absolute.y);
  254. this.drawLine(horizontalStart, horizontalEnd, color);
  255. let verticalStart: PointF2D;
  256. let verticalEnd: PointF2D;
  257. if (line[0] >= 0) {
  258. verticalStart = new PointF2D(indices[0] / samplingUnit + absolute.x, absolute.y);
  259. verticalEnd = new PointF2D(indices[0] / samplingUnit + absolute.x, absolute.y + line[indices[0]]);
  260. this.drawLine(verticalStart, verticalEnd, color);
  261. }
  262. for (let i: number = 1; i < indices.length; i++) {
  263. horizontalStart = new PointF2D(indices[i - 1] / samplingUnit + absolute.x, absolute.y + line[indices[i - 1]]);
  264. horizontalEnd = new PointF2D(indices[i] / samplingUnit + absolute.x, absolute.y + line[indices[i - 1]]);
  265. this.drawLine(horizontalStart, horizontalEnd, color);
  266. verticalStart = new PointF2D(indices[i] / samplingUnit + absolute.x, absolute.y + line[indices[i - 1]]);
  267. verticalEnd = new PointF2D(indices[i] / samplingUnit + absolute.x, absolute.y + line[indices[i]]);
  268. this.drawLine(verticalStart, verticalEnd, color);
  269. }
  270. if (indices[indices.length - 1] < line.length) {
  271. horizontalStart = new PointF2D(indices[indices.length - 1] / samplingUnit + absolute.x, absolute.y + line[indices[indices.length - 1]]);
  272. horizontalEnd = new PointF2D(absolute.x + width, absolute.y + line[indices[indices.length - 1]]);
  273. this.drawLine(horizontalStart, horizontalEnd, color);
  274. } else {
  275. horizontalStart = new PointF2D(indices[indices.length - 1] / samplingUnit + absolute.x, absolute.y);
  276. horizontalEnd = new PointF2D(absolute.x + width, absolute.y);
  277. this.drawLine(horizontalStart, horizontalEnd, color);
  278. }
  279. } else {
  280. // Flat line
  281. const start: PointF2D = new PointF2D(absolute.x, absolute.y);
  282. const end: PointF2D = new PointF2D(absolute.x + width, absolute.y);
  283. this.drawLine(start, end, color);
  284. }
  285. }
  286. private drawStaffEntry(staffEntry: GraphicalStaffEntry): void {
  287. if (staffEntry.FingeringEntries.length > 0) {
  288. for (const fingeringEntry of staffEntry.FingeringEntries) {
  289. fingeringEntry.SVGNode = this.drawLabel(fingeringEntry, GraphicalLayers.Notes);
  290. }
  291. }
  292. // Draw ChordSymbols
  293. if (staffEntry.graphicalChordContainers !== undefined && staffEntry.graphicalChordContainers.length > 0) {
  294. for (const graphicalChordContainer of staffEntry.graphicalChordContainers) {
  295. const label: GraphicalLabel = graphicalChordContainer.GraphicalLabel;
  296. label.SVGNode = this.drawLabel(label, <number>GraphicalLayers.Notes);
  297. }
  298. }
  299. if (this.rules.RenderLyrics) {
  300. if (staffEntry.LyricsEntries.length > 0) {
  301. this.drawLyrics(staffEntry.LyricsEntries, <number>GraphicalLayers.Notes);
  302. }
  303. }
  304. }
  305. /**
  306. * Draw all lyrics to the canvas
  307. * @param lyricEntries Array of lyric entries to be drawn
  308. * @param layer Number of the layer that the lyrics should be drawn in
  309. */
  310. private drawLyrics(lyricEntries: GraphicalLyricEntry[], layer: number): void {
  311. lyricEntries.forEach(lyricsEntry => {
  312. const label: GraphicalLabel = lyricsEntry.GraphicalLabel;
  313. label.SVGNode = this.drawLabel(label, layer);
  314. });
  315. }
  316. protected drawInstrumentBrace(brace: GraphicalObject, system: MusicSystem): void {
  317. // Draw InstrumentBrackets at beginning of line
  318. const vexBrace: VexFlowInstrumentBrace = (brace as VexFlowInstrumentBrace);
  319. vexBrace.draw(this.backend.getContext());
  320. }
  321. protected drawGroupBracket(bracket: GraphicalObject, system: MusicSystem): void {
  322. // Draw InstrumentBrackets at beginning of line
  323. const vexBrace: VexFlowInstrumentBracket = (bracket as VexFlowInstrumentBracket);
  324. vexBrace.draw(this.backend.getContext());
  325. }
  326. protected drawOctaveShifts(staffLine: StaffLine): void {
  327. for (const graphicalOctaveShift of staffLine.OctaveShifts) {
  328. if (graphicalOctaveShift) {
  329. const vexFlowOctaveShift: VexFlowOctaveShift = graphicalOctaveShift as VexFlowOctaveShift;
  330. const ctx: Vex.IRenderContext = this.backend.getContext();
  331. const textBracket: Vex.Flow.TextBracket = vexFlowOctaveShift.getTextBracket();
  332. textBracket.setContext(ctx);
  333. try {
  334. textBracket.draw();
  335. } catch (ex) {
  336. log.warn(ex);
  337. }
  338. }
  339. }
  340. }
  341. protected drawPedals(staffLine: StaffLine): void {
  342. for (const graphicalPedal of staffLine.Pedals) {
  343. if (graphicalPedal) {
  344. const vexFlowPedal: VexFlowPedal = graphicalPedal as VexFlowPedal;
  345. const ctx: Vex.IRenderContext = this.backend.getContext();
  346. const pedalMarking: Vex.Flow.PedalMarking = vexFlowPedal.getPedalMarking();
  347. pedalMarking.setContext(ctx);
  348. pedalMarking.draw();
  349. }
  350. }
  351. }
  352. protected drawWavyLines(staffLine: StaffLine): void {
  353. for (const graphicalWavyLine of staffLine.WavyLines) {
  354. if (graphicalWavyLine) {
  355. const vexFlowVibratoBracket: VexflowVibratoBracket = graphicalWavyLine as VexflowVibratoBracket;
  356. const ctx: Vex.IRenderContext = this.backend.getContext();
  357. const vfVibratoBracket: Vex.Flow.VibratoBracket = vexFlowVibratoBracket.getVibratoBracket();
  358. (vfVibratoBracket as any).setContext(ctx);
  359. vfVibratoBracket.draw();
  360. }
  361. }
  362. }
  363. protected drawExpressions(staffline: StaffLine): void {
  364. // Draw all Expressions
  365. for (const abstractGraphicalExpression of staffline.AbstractExpressions) {
  366. // Draw InstantaniousDynamics
  367. if (abstractGraphicalExpression instanceof GraphicalInstantaneousDynamicExpression) {
  368. this.drawInstantaneousDynamic((abstractGraphicalExpression as VexFlowInstantaneousDynamicExpression));
  369. // Draw InstantaniousTempo
  370. } else if (abstractGraphicalExpression instanceof GraphicalInstantaneousTempoExpression) {
  371. const label: GraphicalLabel = (abstractGraphicalExpression as GraphicalInstantaneousTempoExpression).GraphicalLabel;
  372. label.SVGNode = this.drawLabel(label, GraphicalLayers.Notes);
  373. // Draw ContinuousDynamics
  374. } else if (abstractGraphicalExpression instanceof GraphicalContinuousDynamicExpression) {
  375. this.drawContinuousDynamic((abstractGraphicalExpression as VexFlowContinuousDynamicExpression));
  376. // Draw ContinuousTempo
  377. // } else if (abstractGraphicalExpression instanceof GraphicalContinuousTempoExpression) {
  378. // this.drawLabel((abstractGraphicalExpression as GraphicalContinuousTempoExpression).GraphicalLabel, GraphicalLayers.Notes);
  379. // // Draw Mood
  380. // } else if (abstractGraphicalExpression instanceof GraphicalMoodExpression) {
  381. // GraphicalMoodExpression; graphicalMood = (GraphicalMoodExpression); abstractGraphicalExpression;
  382. // drawLabel(graphicalMood.GetGraphicalLabel, <number>GraphicalLayers.Notes);
  383. // Draw Unknown
  384. } else if (abstractGraphicalExpression instanceof GraphicalUnknownExpression) {
  385. const label: GraphicalLabel = abstractGraphicalExpression.Label;
  386. label.SVGNode = this.drawLabel(label, <number>GraphicalLayers.Notes);
  387. } else {
  388. log.warn("Unkown type of expression!");
  389. }
  390. }
  391. }
  392. protected drawInstantaneousDynamic(instantaneousDynamic: GraphicalInstantaneousDynamicExpression): void {
  393. const label: GraphicalLabel = (instantaneousDynamic as VexFlowInstantaneousDynamicExpression).Label;
  394. label.SVGNode = this.drawLabel(label, <number>GraphicalLayers.Notes);
  395. }
  396. protected drawContinuousDynamic(graphicalExpression: VexFlowContinuousDynamicExpression): void {
  397. if (graphicalExpression.IsVerbal) {
  398. const label: GraphicalLabel = graphicalExpression.Label;
  399. label.SVGNode = this.drawLabel(label, <number>GraphicalLayers.Notes);
  400. } else {
  401. for (const line of graphicalExpression.Lines) {
  402. const start: PointF2D = new PointF2D(graphicalExpression.ParentStaffLine.PositionAndShape.AbsolutePosition.x + line.Start.x,
  403. graphicalExpression.ParentStaffLine.PositionAndShape.AbsolutePosition.y + line.Start.y);
  404. const end: PointF2D = new PointF2D(graphicalExpression.ParentStaffLine.PositionAndShape.AbsolutePosition.x + line.End.x,
  405. graphicalExpression.ParentStaffLine.PositionAndShape.AbsolutePosition.y + line.End.y);
  406. this.drawLine(start, end, "black", line.Width);
  407. }
  408. }
  409. }
  410. /**
  411. * Renders a Label to the screen (e.g. Title, composer..)
  412. * @param graphicalLabel holds the label string, the text height in units and the font parameters
  413. * @param layer is the current rendering layer. There are many layers on top of each other to which can be rendered. Not needed for now.
  414. * @param bitmapWidth Not needed for now.
  415. * @param bitmapHeight Not needed for now.
  416. * @param heightInPixel the height of the text in screen coordinates
  417. * @param screenPosition the position of the lower left corner of the text in screen coordinates
  418. */
  419. protected renderLabel(graphicalLabel: GraphicalLabel, layer: GraphicalLayers, specs: LabelRenderSpecs): Node {
  420. return this._renderLabel(graphicalLabel, specs);
  421. }
  422. private _renderLabel(graphicalLabel: GraphicalLabel, specs: LabelRenderSpecs): Node {
  423. if (!graphicalLabel.Label.print) {
  424. return undefined;
  425. }
  426. const height: number = graphicalLabel.Label.fontHeight * unitInPixels;
  427. const { font } = graphicalLabel.Label;
  428. let color: string;
  429. if (this.rules.ColoringEnabled) {
  430. color = graphicalLabel.Label.colorDefault;
  431. if (graphicalLabel.Label.color) {
  432. color = graphicalLabel.Label.color.toString();
  433. }
  434. if (!color) {
  435. color = this.rules.DefaultColorLabel;
  436. }
  437. }
  438. let { fontStyle, fontFamily } = graphicalLabel.Label;
  439. if (!fontStyle) {
  440. fontStyle = this.rules.DefaultFontStyle;
  441. }
  442. if (!fontFamily) {
  443. fontFamily = this.rules.DefaultFontFamily;
  444. }
  445. let node: Node;
  446. for (let i: number = 0; i < graphicalLabel.TextLines?.length; i++) {
  447. const currLine: {text: string, xOffset: number, width: number} = graphicalLabel.TextLines[i];
  448. const xOffsetInPixel: number = this.calculatePixelDistance(currLine.xOffset);
  449. const linePosition: PointF2D = new PointF2D(specs.ScreenPosition.x + xOffsetInPixel, specs.ScreenPosition.y);
  450. const newNode: Node =
  451. this.backend.renderText(height, fontStyle, font, currLine.text, specs.FontHeightInPixel, linePosition, color, graphicalLabel.Label.fontFamily);
  452. if (!node) {
  453. node = newNode;
  454. } else {
  455. node.appendChild(newNode);
  456. }
  457. specs.ScreenPosition.y = specs.ScreenPosition.y + specs.FontHeightInPixel;
  458. if (graphicalLabel.TextLines.length > 1) {
  459. specs.ScreenPosition.y += this.rules.SpacingBetweenTextLines;
  460. }
  461. }
  462. // font currently unused, replaced by fontFamily
  463. return node; // this will be a merge conflict with annotations, refactor there to handle node array instead of single node
  464. }
  465. /**
  466. * Renders a rectangle with the given style to the screen.
  467. * It is given in screen coordinates.
  468. * @param rectangle the rect in screen coordinates
  469. * @param layer is the current rendering layer. There are many layers on top of each other to which can be rendered. Not needed for now.
  470. * @param styleId the style id
  471. * @param alpha alpha value between 0 and 1
  472. */
  473. protected renderRectangle(rectangle: RectangleF2D, layer: number, styleId: number, colorHex: string, alpha: number): Node {
  474. return this.backend.renderRectangle(rectangle, styleId, colorHex, alpha);
  475. }
  476. /**
  477. * Converts a point from unit to pixel space.
  478. * @param point
  479. * @returns {PointF2D}
  480. */
  481. protected applyScreenTransformation(point: PointF2D): PointF2D {
  482. return new PointF2D(point.x * unitInPixels, point.y * unitInPixels);
  483. }
  484. /**
  485. * Converts a rectangle from unit to pixel space.
  486. * @param rectangle
  487. * @returns {RectangleF2D}
  488. */
  489. protected applyScreenTransformationForRect(rectangle: RectangleF2D): RectangleF2D {
  490. return new RectangleF2D(rectangle.x * unitInPixels, rectangle.y * unitInPixels, rectangle.width * unitInPixels, rectangle.height * unitInPixels);
  491. }
  492. }