ソースを参照

Added very partial typing for VexFlow; travis ci config

Andrea Condoluci 9 年 前
コミット
4ecaa2d084

+ 6 - 0
.travis.yml

@@ -0,0 +1,6 @@
+sudo: false
+language: node_js
+node_js:
+  - "0.10"
+before_script:
+  - npm install

+ 39 - 3
src/MusicalScore/Calculation/VexFlowMeasureSizeCalculator.ts

@@ -1,5 +1,14 @@
-interface VexFlowVoice {
+interface VexFlowBoundingBox {
+  mergeWith(bb: VexFlowBoundingBox): VexFlowBoundingBox;
+  getX(): number;
+  getY(): number;
+  getW(): number;
+  getH(): number;
+}
 
+interface VexFlowVoice {
+  getBoundingBox(): VexFlowBoundingBox;
+  setStave(stave: VexFlowStave): VexFlowVoice;
 }
 
 interface VexFlowStave {
@@ -11,11 +20,15 @@ interface VexFlowStave {
   setWidth(width: number): VexFlowStave;
   format(): void;
   getSpacingBetweenLines(): number;
+  getNumLines(): number;
+  getLineForY(y: number): number;
 }
 
+// Usage:
+/// TODO
 class MeasureSizeCalculator {
   public stave: VexFlowStave;
-  public voices: VexFlowVoice;
+  public voices: VexFlowVoice[];
   public formatter: any;
 
   private offsetLeft: number;
@@ -44,6 +57,8 @@ class MeasureSizeCalculator {
   }
 
   public getHeight(): number {
+    // FIXME this formula does not take into account
+    // other things like staves and ties!
     return this.stave.getSpacingBetweenLines()
       * (this.topBorder - this.bottomBorder);
   }
@@ -61,6 +76,9 @@ class MeasureSizeCalculator {
 
   private format(): void {
     let stave: VexFlowStave = this.stave;
+    let voices: VexFlowVoice[] = this.voices;
+    let voicesBoundingBox: VexFlowBoundingBox;
+    let bb: VexFlowBoundingBox;
     // Compute widths
     this.voicesWidth = this.formatter.minTotalWidth;
     stave.setWidth(this.voicesWidth);
@@ -70,6 +88,24 @@ class MeasureSizeCalculator {
     // Compute heights
     // Height is:
     //// height of StaveModifiers + BoundingBox of notes + height of NoteMod's
-    // TODO
+    for (let i: number = 0; i < this.voices.length; i ++) {
+      voices[i].setStave(stave);
+      bb = voices[i].getBoundingBox();
+      if (voicesBoundingBox === undefined) {
+        voicesBoundingBox = bb;
+      } else {
+        voicesBoundingBox = voicesBoundingBox.mergeWith(bb);
+      }
+    }
+    // TODO voicesBoundingBox.getW() should be similar to this.voicesWidth?
+    // FIXME the following: should consider stave modifiers
+    //this.height = voicesBoundingBox.getH(); FIXME
+    this.topBorder = Math.min(
+      0, stave.getLineForY(voicesBoundingBox.getY())
+    );
+    this.bottomBorder = Math.max(
+      stave.getNumLines(),
+      stave.getLineForY(voicesBoundingBox.getY() + voicesBoundingBox.getH())
+    )
   }
 }

+ 46 - 0
typings/vexflow.d.ts

@@ -0,0 +1,46 @@
+declare namespace VexFlow {
+  export module Flow {
+
+    export class Formatter {
+      hasMinTotalWidth: boolean;
+      minTotalWidth: number;
+
+      //preCalculateMinTotalWidth(voices: Voices[]);
+      constructor();
+    }
+
+    export class BoundingBox {
+      mergeWith(bb: BoundingBox): BoundingBox;
+      getX(): number;
+      getY(): number;
+      getW(): number;
+      getH(): number;
+    }
+
+    export class Voice {
+      getBoundingBox(): BoundingBox;
+      setStave(stave: Stave): Voice;
+      constructor(note_struct: any);
+    }
+
+    export class Stave {
+      x: number;
+      start_x: number;
+      end_x: number;
+
+      getWidth(): number;
+      setWidth(width: number): Stave;
+      format(): void;
+      getSpacingBetweenLines(): number;
+      getNumLines(): number;
+      getLineForY(y: number): number;
+
+      constructor();
+    }
+
+  }
+}
+
+declare module "vexflow" {
+    export = VexFlow;
+}