MeasureSizeCalculator.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import {
  2. MeasureSizeCalculator,
  3. } from "../../../src/MusicalScore/Calculation/MeasureSizeCalculator.ts";
  4. import Vex = require("vexflow");
  5. describe("Measure Size Calculator Tests", () => {
  6. // Initialization
  7. let stave: Vex.Flow.Stave = new Vex.Flow.Stave(0, 0, 0);
  8. let voices: Vex.Flow.Voice[];
  9. let formatter: Vex.Flow.Formatter;
  10. let voice: Vex.Flow.Voice;
  11. let note: Vex.Flow.StaveNote;
  12. let calc: MeasureSizeCalculator;
  13. it("One note", (done: MochaDone) => {
  14. formatter = new Vex.Flow.Formatter();
  15. voice = new Vex.Flow.Voice(undefined);
  16. note = new Vex.Flow.StaveNote({ keys: ["b/4"], "duration": "1" });
  17. voice.addTickables([note]);
  18. voices = [voice];
  19. chai.expect(formatter.preCalculateMinTotalWidth(voices)).to.equal(22);
  20. calc = new MeasureSizeCalculator(stave, voices, formatter);
  21. chai.expect(calc.getBottomBorder()).to.equal(5);
  22. done();
  23. });
  24. it("Four quarter notes", (done: MochaDone) => {
  25. formatter = new Vex.Flow.Formatter();
  26. voice = new Vex.Flow.Voice(undefined);
  27. voice.addTickables([
  28. new Vex.Flow.StaveNote({ keys: ["c/4"], "duration": "q" }),
  29. new Vex.Flow.StaveNote({ keys: ["d/4"], "duration": "q" }),
  30. new Vex.Flow.StaveNote({ keys: ["e/4"], "duration": "q" }),
  31. new Vex.Flow.StaveNote({ keys: ["f/4"], "duration": "q" }),
  32. ]);
  33. voices = [voice];
  34. chai.expect(formatter.preCalculateMinTotalWidth(voices)).to.equal(64);
  35. calc = new MeasureSizeCalculator(stave, voices, formatter);
  36. chai.expect(calc.getWidth()).to.equal(64);
  37. chai.expect(calc.getBottomBorder()).to.equal(6);
  38. chai.expect(calc.getTopBorder()).to.equal(0);
  39. done();
  40. });
  41. it("Will certainly pass", (done: MochaDone) => {
  42. let visual: (testfun: (r: any, ctx: any) => void ) => void;
  43. visual = function(func: (r: any, ctx: any) => void): void {
  44. let canvas: HTMLCanvasElement = document.createElement("canvas");
  45. document.body.appendChild(canvas);
  46. let renderer: any = new Vex.Flow.Renderer(
  47. canvas,
  48. Vex.Flow.Renderer.Backends.CANVAS
  49. );
  50. renderer.resize(300, 100);
  51. let ctx: any = renderer.getContext();
  52. ctx.setFont("Arial", 10, "").setBackgroundFillStyle("#eed");
  53. func(renderer, ctx);
  54. };
  55. visual((renderer: any, ctx: any): void => {
  56. renderer.resize(420, 120);
  57. let stave2: Vex.Flow.Stave = new Vex.Flow.Stave(10, 0, 410);
  58. stave2.setContext(ctx);
  59. for (let t in Vex.Flow.Clef.types) {
  60. if (Vex.Flow.Clef.types.hasOwnProperty(t)) {
  61. let clef: Vex.Flow.Clef = new Vex.Flow.Clef(t);
  62. stave2.addModifier(clef, Vex.Flow.StaveModifier.Position.BEGIN);
  63. stave2.format();
  64. clef.setStave(stave2);
  65. let bb: Vex.Flow.BoundingBox =
  66. MeasureSizeCalculator.getClefBoundingBox(clef);
  67. //console.log(bb);
  68. ctx.rect(bb.getX(), bb.getY(), bb.getW(), bb.getH());
  69. ctx.stroke();
  70. }
  71. }
  72. stave2.draw();
  73. });
  74. done();
  75. });
  76. });