VexFlowMusicSheetDrawer.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. 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. // Draw ChordSymbols
  288. if (staffEntry.graphicalChordContainers !== undefined && staffEntry.graphicalChordContainers.length > 0) {
  289. for (const graphicalChordContainer of staffEntry.graphicalChordContainers) {
  290. this.drawLabel(graphicalChordContainer.GraphicalLabel, <number>GraphicalLayers.Notes);
  291. }
  292. }
  293. if (this.rules.RenderLyrics) {
  294. if (staffEntry.LyricsEntries.length > 0) {
  295. this.drawLyrics(staffEntry.LyricsEntries, <number>GraphicalLayers.Notes);
  296. }
  297. }
  298. }
  299. /**
  300. * Draw all lyrics to the canvas
  301. * @param lyricEntries Array of lyric entries to be drawn
  302. * @param layer Number of the layer that the lyrics should be drawn in
  303. */
  304. private drawLyrics(lyricEntries: GraphicalLyricEntry[], layer: number): void {
  305. lyricEntries.forEach(lyricsEntry => this.drawLabel(lyricsEntry.GraphicalLabel, layer));
  306. }
  307. protected drawInstrumentBrace(brace: GraphicalObject, system: MusicSystem): void {
  308. // Draw InstrumentBrackets at beginning of line
  309. const vexBrace: VexFlowInstrumentBrace = (brace as VexFlowInstrumentBrace);
  310. vexBrace.draw(this.backend.getContext());
  311. }
  312. protected drawGroupBracket(bracket: GraphicalObject, system: MusicSystem): void {
  313. // Draw InstrumentBrackets at beginning of line
  314. const vexBrace: VexFlowInstrumentBracket = (bracket as VexFlowInstrumentBracket);
  315. vexBrace.draw(this.backend.getContext());
  316. }
  317. protected drawOctaveShifts(staffLine: StaffLine): void {
  318. for (const graphicalOctaveShift of staffLine.OctaveShifts) {
  319. if (graphicalOctaveShift) {
  320. const vexFlowOctaveShift: VexFlowOctaveShift = graphicalOctaveShift as VexFlowOctaveShift;
  321. const ctx: Vex.IRenderContext = this.backend.getContext();
  322. const textBracket: Vex.Flow.TextBracket = vexFlowOctaveShift.getTextBracket();
  323. textBracket.setContext(ctx);
  324. textBracket.draw();
  325. }
  326. }
  327. }
  328. protected drawPedals(staffLine: StaffLine): void {
  329. for (const graphicalPedal of staffLine.Pedals) {
  330. if (graphicalPedal) {
  331. const vexFlowPedal: VexFlowPedal = graphicalPedal as VexFlowPedal;
  332. const ctx: Vex.IRenderContext = this.backend.getContext();
  333. const pedalMarking: Vex.Flow.PedalMarking = vexFlowPedal.getPedalMarking();
  334. pedalMarking.setContext(ctx);
  335. pedalMarking.draw();
  336. }
  337. }
  338. }
  339. protected drawWavyLines(staffLine: StaffLine): void {
  340. for (const graphicalWavyLine of staffLine.WavyLines) {
  341. if (graphicalWavyLine) {
  342. const vexFlowVibratoBracket: VexflowVibratoBracket = graphicalWavyLine as VexflowVibratoBracket;
  343. const ctx: Vex.IRenderContext = this.backend.getContext();
  344. const vfVibratoBracket: Vex.Flow.VibratoBracket = vexFlowVibratoBracket.getVibratoBracket();
  345. (vfVibratoBracket as any).setContext(ctx);
  346. vfVibratoBracket.draw();
  347. }
  348. }
  349. }
  350. protected drawExpressions(staffline: StaffLine): void {
  351. // Draw all Expressions
  352. for (const abstractGraphicalExpression of staffline.AbstractExpressions) {
  353. // Draw InstantaniousDynamics
  354. if (abstractGraphicalExpression instanceof GraphicalInstantaneousDynamicExpression) {
  355. this.drawInstantaneousDynamic((abstractGraphicalExpression as VexFlowInstantaneousDynamicExpression));
  356. // Draw InstantaniousTempo
  357. } else if (abstractGraphicalExpression instanceof GraphicalInstantaneousTempoExpression) {
  358. this.drawLabel((abstractGraphicalExpression as GraphicalInstantaneousTempoExpression).GraphicalLabel, GraphicalLayers.Notes);
  359. // Draw ContinuousDynamics
  360. } else if (abstractGraphicalExpression instanceof GraphicalContinuousDynamicExpression) {
  361. this.drawContinuousDynamic((abstractGraphicalExpression as VexFlowContinuousDynamicExpression));
  362. // Draw ContinuousTempo
  363. // } else if (abstractGraphicalExpression instanceof GraphicalContinuousTempoExpression) {
  364. // this.drawLabel((abstractGraphicalExpression as GraphicalContinuousTempoExpression).GraphicalLabel, GraphicalLayers.Notes);
  365. // // Draw Mood
  366. // } else if (abstractGraphicalExpression instanceof GraphicalMoodExpression) {
  367. // GraphicalMoodExpression; graphicalMood = (GraphicalMoodExpression); abstractGraphicalExpression;
  368. // drawLabel(graphicalMood.GetGraphicalLabel, <number>GraphicalLayers.Notes);
  369. // Draw Unknown
  370. } else if (abstractGraphicalExpression instanceof GraphicalUnknownExpression) {
  371. this.drawLabel(abstractGraphicalExpression.Label, <number>GraphicalLayers.Notes);
  372. } else {
  373. log.warn("Unkown type of expression!");
  374. }
  375. }
  376. }
  377. protected drawInstantaneousDynamic(instantaneousDynamic: GraphicalInstantaneousDynamicExpression): void {
  378. this.drawLabel((instantaneousDynamic as VexFlowInstantaneousDynamicExpression).Label, <number>GraphicalLayers.Notes);
  379. }
  380. protected drawContinuousDynamic(graphicalExpression: VexFlowContinuousDynamicExpression): void {
  381. if (graphicalExpression.IsVerbal) {
  382. this.drawLabel(graphicalExpression.Label, <number>GraphicalLayers.Notes);
  383. } else {
  384. for (const line of graphicalExpression.Lines) {
  385. const start: PointF2D = new PointF2D(graphicalExpression.ParentStaffLine.PositionAndShape.AbsolutePosition.x + line.Start.x,
  386. graphicalExpression.ParentStaffLine.PositionAndShape.AbsolutePosition.y + line.Start.y);
  387. const end: PointF2D = new PointF2D(graphicalExpression.ParentStaffLine.PositionAndShape.AbsolutePosition.x + line.End.x,
  388. graphicalExpression.ParentStaffLine.PositionAndShape.AbsolutePosition.y + line.End.y);
  389. this.drawLine(start, end, "black", line.Width);
  390. }
  391. }
  392. }
  393. /**
  394. * Renders a Label to the screen (e.g. Title, composer..)
  395. * @param graphicalLabel holds the label string, the text height in units and the font parameters
  396. * @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.
  397. * @param bitmapWidth Not needed for now.
  398. * @param bitmapHeight Not needed for now.
  399. * @param heightInPixel the height of the text in screen coordinates
  400. * @param screenPosition the position of the lower left corner of the text in screen coordinates
  401. */
  402. protected renderLabel(graphicalLabel: GraphicalLabel, layer: GraphicalLayers, specs: LabelRenderSpecs): Node {
  403. return this._renderLabel(graphicalLabel, specs);
  404. }
  405. private _renderLabel(graphicalLabel: GraphicalLabel, specs: LabelRenderSpecs): Node {
  406. if (!graphicalLabel.Label.print) {
  407. return undefined;
  408. }
  409. const height: number = graphicalLabel.Label.fontHeight * unitInPixels;
  410. const { font } = graphicalLabel.Label;
  411. let color: string;
  412. if (this.rules.ColoringEnabled) {
  413. color = graphicalLabel.Label.colorDefault;
  414. if (graphicalLabel.Label.color) {
  415. color = graphicalLabel.Label.color.toString();
  416. }
  417. if (!color) {
  418. color = this.rules.DefaultColorLabel;
  419. }
  420. }
  421. let { fontStyle, fontFamily } = graphicalLabel.Label;
  422. if (!fontStyle) {
  423. fontStyle = this.rules.DefaultFontStyle;
  424. }
  425. if (!fontFamily) {
  426. fontFamily = this.rules.DefaultFontFamily;
  427. }
  428. let node: Node;
  429. for (let i: number = 0; i < graphicalLabel.TextLines?.length; i++) {
  430. const currLine: {text: string, xOffset: number, width: number} = graphicalLabel.TextLines[i];
  431. const xOffsetInPixel: number = this.calculatePixelDistance(currLine.xOffset);
  432. const linePosition: PointF2D = new PointF2D(specs.ScreenPosition.x + xOffsetInPixel, specs.ScreenPosition.y);
  433. const newNode: Node =
  434. this.backend.renderText(height, fontStyle, font, currLine.text, specs.FontHeightInPixel, linePosition, color, graphicalLabel.Label.fontFamily);
  435. if (!node) {
  436. node = newNode;
  437. } else {
  438. node.appendChild(newNode);
  439. }
  440. specs.ScreenPosition.y = specs.ScreenPosition.y + specs.FontHeightInPixel;
  441. if (graphicalLabel.TextLines.length > 1) {
  442. specs.ScreenPosition.y += this.rules.SpacingBetweenTextLines;
  443. }
  444. }
  445. // font currently unused, replaced by fontFamily
  446. return node; // this will be a merge conflict with annotations, refactor there to handle node array instead of single node
  447. }
  448. /**
  449. * Renders a rectangle with the given style to the screen.
  450. * It is given in screen coordinates.
  451. * @param rectangle the rect in screen coordinates
  452. * @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.
  453. * @param styleId the style id
  454. * @param alpha alpha value between 0 and 1
  455. */
  456. protected renderRectangle(rectangle: RectangleF2D, layer: number, styleId: number, colorHex: string, alpha: number): Node {
  457. return this.backend.renderRectangle(rectangle, styleId, colorHex, alpha);
  458. }
  459. /**
  460. * Converts a point from unit to pixel space.
  461. * @param point
  462. * @returns {PointF2D}
  463. */
  464. protected applyScreenTransformation(point: PointF2D): PointF2D {
  465. return new PointF2D(point.x * unitInPixels, point.y * unitInPixels);
  466. }
  467. /**
  468. * Converts a rectangle from unit to pixel space.
  469. * @param rectangle
  470. * @returns {RectangleF2D}
  471. */
  472. protected applyScreenTransformationForRect(rectangle: RectangleF2D): RectangleF2D {
  473. return new RectangleF2D(rectangle.x * unitInPixels, rectangle.y * unitInPixels, rectangle.width * unitInPixels, rectangle.height * unitInPixels);
  474. }
  475. }