VexFlowMusicSheetDrawer.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import {MusicSheetDrawer} from "../MusicSheetDrawer";
  2. import {RectangleF2D} from "../../../Common/DataObjects/RectangleF2D";
  3. import {VexFlowMeasure} from "./VexFlowMeasure";
  4. import {PointF2D} from "../../../Common/DataObjects/PointF2D";
  5. import {GraphicalLabel} from "../GraphicalLabel";
  6. import {VexFlowTextMeasurer} from "./VexFlowTextMeasurer";
  7. import {MusicSystem} from "../MusicSystem";
  8. import {GraphicalObject} from "../GraphicalObject";
  9. import {GraphicalLayers} from "../DrawingEnums";
  10. import {GraphicalStaffEntry} from "../GraphicalStaffEntry";
  11. import {VexFlowBackend} from "./VexFlowBackend";
  12. import {VexFlowInstrumentBracket} from "./VexFlowInstrumentBracket";
  13. import {VexFlowInstrumentBrace} from "./VexFlowInstrumentBrace";
  14. import {GraphicalLyricEntry} from "../GraphicalLyricEntry";
  15. import {StaffLine} from "../StaffLine";
  16. import {EngravingRules} from "../EngravingRules";
  17. /**
  18. * This is a global constant which denotes the height in pixels of the space between two lines of the stave
  19. * (when zoom = 1.0)
  20. * @type number
  21. */
  22. export const unitInPixels: number = 10;
  23. export class VexFlowMusicSheetDrawer extends MusicSheetDrawer {
  24. private backend: VexFlowBackend;
  25. private zoom: number = 1.0;
  26. constructor(element: HTMLElement,
  27. backend: VexFlowBackend,
  28. isPreviewImageDrawer: boolean = false) {
  29. super(new VexFlowTextMeasurer(), isPreviewImageDrawer);
  30. this.backend = backend;
  31. }
  32. public clear(): void {
  33. this.backend.clear();
  34. }
  35. /**
  36. * Zoom the rendering areas
  37. * @param k is the zoom factor
  38. */
  39. public scale(k: number): void {
  40. this.zoom = k;
  41. this.backend.scale(this.zoom);
  42. }
  43. /**
  44. * Resize the rendering areas
  45. * @param x
  46. * @param y
  47. */
  48. public resize(x: number, y: number): void {
  49. this.backend.resize(x, y);
  50. }
  51. public translate(x: number, y: number): void {
  52. this.backend.translate(x, y);
  53. }
  54. /**
  55. * Converts a distance from unit to pixel space.
  56. * @param unitDistance the distance in units
  57. * @returns {number} the distance in pixels
  58. */
  59. public calculatePixelDistance(unitDistance: number): number {
  60. return unitDistance * unitInPixels;
  61. }
  62. protected drawMeasure(measure: VexFlowMeasure): void {
  63. measure.setAbsoluteCoordinates(
  64. measure.PositionAndShape.AbsolutePosition.x * unitInPixels,
  65. measure.PositionAndShape.AbsolutePosition.y * unitInPixels
  66. );
  67. measure.draw(this.backend.getContext());
  68. // Draw the StaffEntries
  69. for (const staffEntry of measure.staffEntries) {
  70. this.drawStaffEntry(staffEntry);
  71. }
  72. }
  73. // private drawPixel(coord: PointF2D): void {
  74. // coord = this.applyScreenTransformation(coord);
  75. // const ctx: any = this.backend.getContext();
  76. // const oldStyle: string = ctx.fillStyle;
  77. // ctx.fillStyle = "#00FF00FF";
  78. // ctx.fillRect( coord.x, coord.y, 2, 2 );
  79. // ctx.fillStyle = oldStyle;
  80. // }
  81. public drawLine(start: PointF2D, stop: PointF2D, color: string = "#FF0000FF"): void {
  82. start = this.applyScreenTransformation(start);
  83. stop = this.applyScreenTransformation(stop);
  84. this.backend.renderLine(start, stop, color);
  85. }
  86. protected drawSkyLine(staffline: StaffLine): void {
  87. const startPosition: PointF2D = staffline.PositionAndShape.AbsolutePosition;
  88. const width: number = staffline.PositionAndShape.Size.width;
  89. this.drawSampledLine(staffline.SkyLine, startPosition, width);
  90. }
  91. protected drawBottomLine(staffline: StaffLine): void {
  92. const startPosition: PointF2D = new PointF2D(staffline.PositionAndShape.AbsolutePosition.x,
  93. staffline.PositionAndShape.AbsolutePosition.y);
  94. const width: number = staffline.PositionAndShape.Size.width;
  95. this.drawSampledLine(staffline.BottomLine, startPosition, width, "#0000FFFF");
  96. }
  97. /**
  98. * Draw a line with a width and start point in a chosen color (used for skyline/bottom line debugging) from
  99. * a simple array
  100. * @param line numeric array. 0 marks the base line. Direction given by sign. Dimensions in units
  101. * @param startPosition Start position in units
  102. * @param width Max line width in units
  103. * @param color Color to paint in. Default is red
  104. */
  105. private drawSampledLine(line: number[], startPosition: PointF2D, width: number, color: string = "#FF0000FF"): void {
  106. const indices: number[] = [];
  107. let currentValue: number = 0;
  108. for (let i: number = 0; i < line.length; i++) {
  109. if (line[i] !== currentValue) {
  110. indices.push(i);
  111. currentValue = line[i];
  112. }
  113. }
  114. const absolute: PointF2D = startPosition;
  115. if (indices.length > 0) {
  116. const samplingUnit: number = EngravingRules.Rules.SamplingUnit;
  117. let horizontalStart: PointF2D = new PointF2D(absolute.x, absolute.y);
  118. let horizontalEnd: PointF2D = new PointF2D(indices[0] / samplingUnit + absolute.x, absolute.y);
  119. this.drawLine(horizontalStart, horizontalEnd, color);
  120. let verticalStart: PointF2D;
  121. let verticalEnd: PointF2D;
  122. if (line[0] >= 0) {
  123. verticalStart = new PointF2D(indices[0] / samplingUnit + absolute.x, absolute.y);
  124. verticalEnd = new PointF2D(indices[0] / samplingUnit + absolute.x, absolute.y + line[indices[0]]);
  125. this.drawLine(verticalStart, verticalEnd, color);
  126. }
  127. for (let i: number = 1; i < indices.length; i++) {
  128. horizontalStart = new PointF2D(indices[i - 1] / samplingUnit + absolute.x, absolute.y + line[indices[i - 1]]);
  129. horizontalEnd = new PointF2D(indices[i] / samplingUnit + absolute.x, absolute.y + line[indices[i - 1]]);
  130. this.drawLine(horizontalStart, horizontalEnd, color);
  131. verticalStart = new PointF2D(indices[i] / samplingUnit + absolute.x, absolute.y + line[indices[i - 1]]);
  132. verticalEnd = new PointF2D(indices[i] / samplingUnit + absolute.x, absolute.y + line[indices[i]]);
  133. this.drawLine(verticalStart, verticalEnd, color);
  134. }
  135. if (indices[indices.length - 1] < line.length) {
  136. horizontalStart = new PointF2D(indices[indices.length - 1] / samplingUnit + absolute.x, absolute.y + line[indices[indices.length - 1]]);
  137. horizontalEnd = new PointF2D(absolute.x + width, absolute.y + line[indices[indices.length - 1]]);
  138. this.drawLine(horizontalStart, horizontalEnd, color);
  139. } else {
  140. horizontalStart = new PointF2D(indices[indices.length - 1] / samplingUnit + absolute.x, absolute.y);
  141. horizontalEnd = new PointF2D(absolute.x + width, absolute.y);
  142. this.drawLine(horizontalStart, horizontalEnd, color);
  143. }
  144. } else {
  145. // Flat line
  146. const start: PointF2D = new PointF2D(absolute.x, absolute.y);
  147. const end: PointF2D = new PointF2D(absolute.x + width, absolute.y);
  148. this.drawLine(start, end, color);
  149. }
  150. }
  151. private drawStaffEntry(staffEntry: GraphicalStaffEntry): void {
  152. // Draw ChordSymbol
  153. if (staffEntry.graphicalChordContainer !== undefined) {
  154. this.drawLabel(staffEntry.graphicalChordContainer.GetGraphicalLabel, <number>GraphicalLayers.Notes);
  155. }
  156. if (staffEntry.LyricsEntries.length > 0) {
  157. this.drawLyrics(staffEntry.LyricsEntries, <number>GraphicalLayers.Notes);
  158. }
  159. }
  160. /**
  161. * Draw all lyrics to the canvas
  162. * @param lyricEntries Array of lyric entries to be drawn
  163. * @param layer Number of the layer that the lyrics should be drawn in
  164. */
  165. private drawLyrics(lyricEntries: GraphicalLyricEntry[], layer: number): void {
  166. lyricEntries.forEach(lyricsEntry => this.drawLabel(lyricsEntry.GraphicalLabel, layer));
  167. }
  168. protected drawInstrumentBrace(brace: GraphicalObject, system: MusicSystem): void {
  169. // Draw InstrumentBrackets at beginning of line
  170. const vexBrace: VexFlowInstrumentBrace = (brace as VexFlowInstrumentBrace);
  171. vexBrace.draw(this.backend.getContext());
  172. }
  173. protected drawGroupBracket(bracket: GraphicalObject, system: MusicSystem): void {
  174. // Draw InstrumentBrackets at beginning of line
  175. const vexBrace: VexFlowInstrumentBracket = (bracket as VexFlowInstrumentBracket);
  176. vexBrace.draw(this.backend.getContext());
  177. }
  178. /**
  179. * Renders a Label to the screen (e.g. Title, composer..)
  180. * @param graphicalLabel holds the label string, the text height in units and the font parameters
  181. * @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.
  182. * @param bitmapWidth Not needed for now.
  183. * @param bitmapHeight Not needed for now.
  184. * @param heightInPixel the height of the text in screen coordinates
  185. * @param screenPosition the position of the lower left corner of the text in screen coordinates
  186. */
  187. protected renderLabel(graphicalLabel: GraphicalLabel, layer: number, bitmapWidth: number,
  188. bitmapHeight: number, heightInPixel: number, screenPosition: PointF2D): void {
  189. const height: number = graphicalLabel.Label.fontHeight * unitInPixels;
  190. const { fontStyle, font, text } = graphicalLabel.Label;
  191. this.backend.renderText(height, fontStyle, font, text, heightInPixel, screenPosition);
  192. }
  193. /**
  194. * Renders a rectangle with the given style to the screen.
  195. * It is given in screen coordinates.
  196. * @param rectangle the rect in screen coordinates
  197. * @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.
  198. * @param styleId the style id
  199. * @param alpha alpha value between 0 and 1
  200. */
  201. protected renderRectangle(rectangle: RectangleF2D, layer: number, styleId: number, alpha: number): void {
  202. this.backend.renderRectangle(rectangle, styleId, alpha);
  203. }
  204. /**
  205. * Converts a point from unit to pixel space.
  206. * @param point
  207. * @returns {PointF2D}
  208. */
  209. protected applyScreenTransformation(point: PointF2D): PointF2D {
  210. return new PointF2D(point.x * unitInPixels, point.y * unitInPixels);
  211. }
  212. /**
  213. * Converts a rectangle from unit to pixel space.
  214. * @param rectangle
  215. * @returns {RectangleF2D}
  216. */
  217. protected applyScreenTransformationForRect(rectangle: RectangleF2D): RectangleF2D {
  218. return new RectangleF2D(rectangle.x * unitInPixels, rectangle.y * unitInPixels, rectangle.width * unitInPixels, rectangle.height * unitInPixels);
  219. }
  220. }