浏览代码

Fixed bug with StaveModifiers

Andrea Condoluci 9 年之前
父节点
当前提交
8415ef5923

+ 6 - 3
external/vexflow/vexflow.d.ts

@@ -51,6 +51,7 @@ declare namespace Vex {
       public setX(x: number): Stave;
       public addClef(clefSpec: string, size: any, annotation: any, position: any): void;
       public setEndClef(clefSpec: string, size: any, annotation: any): void;
+      public getModifiers(): StaveModifier[];
       public getYForGlyphs(): number;
       public getWidth(): number;
       public setWidth(width: number): Stave;
@@ -69,11 +70,14 @@ declare namespace Vex {
 
     export class Modifier {
       public static Position: any;
+      public getCategory(): string;
+      public getWidth(): number;
+      public getPadding(index: number): number;
     }
 
-    export class StaveModifier implements Modifier {}
+    export class StaveModifier extends Modifier {}
 
-    export class Clef implements StaveModifier {
+    export class Clef extends StaveModifier {
       constructor(type: any);
 
       public static category: string;
@@ -84,7 +88,6 @@ declare namespace Vex {
 
       public getBoundingBox(): Vex.Flow.BoundingBox;
       public setStave(stave: Vex.Flow.Stave): void;
-      public getWidth(): number;
     }
 
     export class Renderer {

+ 3 - 0
src/MusicalScore/Graphical/VexFlow/VexFlowConverter.ts

@@ -121,6 +121,9 @@ export class VexFlowConverter {
     }
 
     public static keySignature(key: KeyInstruction): string {
+        if (key === undefined) {
+            return undefined;
+        }
         switch (key.Mode) {
             case KeyEnum.none:
                 return undefined;

+ 20 - 21
src/MusicalScore/Graphical/VexFlow/VexFlowMeasure.ts

@@ -11,6 +11,7 @@ import {VexFlowStaffEntry} from "./VexFlowStaffEntry";
 //import {Fraction} from "../../../Common/DataObjects/fraction";
 
 import Vex = require("vexflow");
+import StaveModifier = Vex.Flow.StaveModifier;
 
 export class VexFlowMeasure extends StaffMeasure {
     constructor(staff: Staff, staffLine: StaffLine = undefined, sourceMeasure: SourceMeasure = undefined) {
@@ -69,7 +70,7 @@ export class VexFlowMeasure extends StaffMeasure {
     public addClefAtBegin(clef: ClefInstruction): void {
         let vfclef: string = VexFlowConverter.Clef(clef);
         this.stave.addClef(vfclef, undefined, undefined, Vex.Flow.Modifier.Position.BEGIN);
-        //this.increaseBeginInstructionWidth(vfclef);
+        this.increaseBeginInstructionWidth();
     }
 
     /**
@@ -98,7 +99,7 @@ export class VexFlowMeasure extends StaffMeasure {
             timeSig,
             Vex.Flow.Modifier.Position.BEGIN
         );
-        //this.increaseBeginInstructionWidth(timeSig);
+        this.increaseBeginInstructionWidth();
     }
 
     /**
@@ -109,7 +110,7 @@ export class VexFlowMeasure extends StaffMeasure {
     public addClefAtEnd(clef: ClefInstruction): void {
         let vfclef: string = VexFlowConverter.Clef(clef);
         this.stave.setEndClef(vfclef, undefined, undefined);
-        //this.increaseEndInstructionWidth(vfclef);
+        this.increaseEndInstructionWidth();
     }
 
     /**
@@ -144,8 +145,6 @@ export class VexFlowMeasure extends StaffMeasure {
         // This is already done in the MusicSystemBuilder!
         //this.setWidth(this.minimumStaffEntriesWidth * this.staffEntriesScaleFactor);
         this.stave.format();
-        // Set context first!
-        this.stave.draw();
     }
 
     public addGraphicalStaffEntry(entry: VexFlowStaffEntry): void {
@@ -170,20 +169,20 @@ export class VexFlowMeasure extends StaffMeasure {
         // TODO
     }
 
-    //private increaseBeginInstructionWidth(modifier: StaveModifier): void {
-    //    let padding: number = modifier.getCategory("") === "keysignatures" ? modifier.getPadding(2) : 0;
-    //    //modifier.getPadding(this.begModifiers);
-    //    let width: number = modifier.getWidth();
-    //    this.beginInstructionsWidth += padding + width;
-    //
-    //    //if (padding + width > 0) {
-    //    //    this.begModifiers += 1;
-    //    //}
-    //}
-    //
-    //private increaseEndInstructionWidth(modifier: StaveModifier): void {
-    //    let padding: number = 0; //modifier.getPadding(this.endModifiers++);
-    //    let width: number = modifier.getWidth();
-    //    this.endInstructionsWidth += padding + width;
-    //}
+    private increaseBeginInstructionWidth(): void {
+        let modifiers: Vex.Flow.StaveModifier[] = this.stave.getModifiers();
+        let modifier: Vex.Flow.StaveModifier = modifiers[modifiers.length - 1];
+        let padding: number = modifier.getCategory() === "keysignatures" ? modifier.getPadding(2) : 0;
+        //modifier.getPadding(this.begModifiers);
+        let width: number = modifier.getWidth();
+        this.beginInstructionsWidth += padding + width;
+    }
+
+    private increaseEndInstructionWidth(): void {
+        let modifiers: Vex.Flow.StaveModifier[] = this.stave.getModifiers();
+        let modifier: Vex.Flow.StaveModifier = modifiers[modifiers.length - 1];
+        let padding: number = 0; //modifier.getPadding(this.endModifiers++);
+        let width: number = modifier.getWidth();
+        this.endInstructionsWidth += padding + width;
+    }
 }