MeasureSizeCalculator.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 = function(func: (r: any, ctx: any) => void) {
  43. let canvas: HTMLCanvasElement = document.createElement("canvas");
  44. document.body.appendChild(canvas);
  45. let renderer: any = new Vex.Flow.Renderer(
  46. canvas,
  47. Vex.Flow.Renderer.Backends.CANVAS
  48. );
  49. renderer.resize(300, 100);
  50. let ctx: any = renderer.getContext();
  51. ctx.setFont("Arial", 10, "").setBackgroundFillStyle("#eed");
  52. func(renderer, ctx);
  53. }
  54. visual(function(renderer, ctx) {
  55. renderer.resize(420, 120);
  56. let stave: Vex.Flow.Stave = new Vex.Flow.Stave(10, 0, 410);
  57. stave.setContext(ctx);
  58. for (var t in Vex.Flow.Clef.types) {
  59. let clef: Vex.Flow.Clef = new Vex.Flow.Clef(t);
  60. stave.addModifier(clef, Vex.Flow.StaveModifier.Position.BEGIN);
  61. stave.format();
  62. // (*&^%$#@) //
  63. // FIXME HERE? NaN FIXME FIXME FIXME //
  64. clef.setStave(stave);
  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. stave.draw();
  72. });
  73. done();
  74. });
  75. });