MeasureSizeCalculator.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // (*&^%$#@) //
  65. // FIXME HERE? NaN FIXME FIXME FIXME //
  66. clef.setStave(stave2);
  67. let bb: Vex.Flow.BoundingBox =
  68. MeasureSizeCalculator.getClefBoundingBox(clef);
  69. //console.log(bb);
  70. ctx.rect(bb.getX(), bb.getY(), bb.getW(), bb.getH());
  71. ctx.stroke();
  72. }
  73. }
  74. stave2.draw();
  75. });
  76. done();
  77. });
  78. });