VexFlowMusicSheetDrawer.ts 23 KB

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