VexFlowMusicSheetDrawer.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /**
  13. * This is a global constant which denotes the height in pixels of the space between two lines of the stave
  14. * (when zoom = 1.0)
  15. * @type number
  16. */
  17. export const unitInPixels: number = 10;
  18. export class VexFlowMusicSheetDrawer extends MusicSheetDrawer {
  19. private backend: VexFlowBackend;
  20. private zoom: number = 1.0;
  21. constructor(element: HTMLElement,
  22. backend: VexFlowBackend,
  23. isPreviewImageDrawer: boolean = false) {
  24. super(new VexFlowTextMeasurer(), isPreviewImageDrawer);
  25. this.backend = backend;
  26. }
  27. public clear(): void {
  28. this.backend.clear();
  29. }
  30. /**
  31. * Zoom the rendering areas
  32. * @param k is the zoom factor
  33. */
  34. public scale(k: number): void {
  35. this.zoom = k;
  36. this.backend.scale(this.zoom);
  37. }
  38. /**
  39. * Resize the rendering areas
  40. * @param x
  41. * @param y
  42. */
  43. public resize(x: number, y: number): void {
  44. this.backend.resize(x, y);
  45. }
  46. public translate(x: number, y: number): void {
  47. this.backend.translate(x, y);
  48. }
  49. /**
  50. * Converts a distance from unit to pixel space.
  51. * @param unitDistance the distance in units
  52. * @returns {number} the distance in pixels
  53. */
  54. public calculatePixelDistance(unitDistance: number): number {
  55. return unitDistance * unitInPixels;
  56. }
  57. protected drawMeasure(measure: VexFlowMeasure): void {
  58. measure.setAbsoluteCoordinates(
  59. measure.PositionAndShape.AbsolutePosition.x * unitInPixels,
  60. measure.PositionAndShape.AbsolutePosition.y * unitInPixels
  61. );
  62. measure.draw(this.backend.getContext());
  63. // Draw the StaffEntries
  64. for (let staffEntry of measure.staffEntries) {
  65. this.drawStaffEntry(staffEntry);
  66. }
  67. }
  68. private drawStaffEntry(staffEntry: GraphicalStaffEntry): void {
  69. // Draw ChordSymbol
  70. if (staffEntry.graphicalChordContainer !== undefined) {
  71. this.drawLabel(staffEntry.graphicalChordContainer.GetGraphicalLabel, <number>GraphicalLayers.Notes);
  72. }
  73. }
  74. protected drawInstrumentBrace(bracket: GraphicalObject, system: MusicSystem): void {
  75. // empty
  76. }
  77. protected drawGroupBracket(bracket: GraphicalObject, system: MusicSystem): void {
  78. // empty
  79. }
  80. /**
  81. * Renders a Label to the screen (e.g. Title, composer..)
  82. * @param graphicalLabel holds the label string, the text height in units and the font parameters
  83. * @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.
  84. * @param bitmapWidth Not needed for now.
  85. * @param bitmapHeight Not needed for now.
  86. * @param heightInPixel the height of the text in screen coordinates
  87. * @param screenPosition the position of the lower left corner of the text in screen coordinates
  88. */
  89. protected renderLabel(graphicalLabel: GraphicalLabel, layer: number, bitmapWidth: number,
  90. bitmapHeight: number, heightInPixel: number, screenPosition: PointF2D): void {
  91. const height: number = graphicalLabel.Label.fontHeight * unitInPixels;
  92. const { fontStyle, font, text } = graphicalLabel.Label;
  93. this.backend.renderText(height, fontStyle, font, text, heightInPixel, screenPosition);
  94. }
  95. /**
  96. * Renders a rectangle with the given style to the screen.
  97. * It is given in screen coordinates.
  98. * @param rectangle the rect in screen coordinates
  99. * @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.
  100. * @param styleId the style id
  101. */
  102. protected renderRectangle(rectangle: RectangleF2D, layer: number, styleId: number): void {
  103. this.backend.renderRectangle(rectangle, styleId);
  104. }
  105. /**
  106. * Converts a point from unit to pixel space.
  107. * @param point
  108. * @returns {PointF2D}
  109. */
  110. protected applyScreenTransformation(point: PointF2D): PointF2D {
  111. return new PointF2D(point.x * unitInPixels, point.y * unitInPixels);
  112. }
  113. /**
  114. * Converts a rectangle from unit to pixel space.
  115. * @param rectangle
  116. * @returns {RectangleF2D}
  117. */
  118. protected applyScreenTransformationForRect(rectangle: RectangleF2D): RectangleF2D {
  119. return new RectangleF2D(rectangle.x * unitInPixels, rectangle.y * unitInPixels, rectangle.width * unitInPixels, rectangle.height * unitInPixels);
  120. }
  121. }