Przeglądaj źródła

Calculate clef sizes

Andrea Condoluci 9 lat temu
rodzic
commit
802e21f6be

+ 51 - 1
src/MusicalScore/Calculation/MeasureSizeCalculator.ts

@@ -99,8 +99,20 @@ export class MeasureSizeCalculator {
     //console.log("this.width", this.voicesWidth);
     //console.log("voicesBB", voicesBoundingBox.getW());
 
-    // FIXME the following: should consider stave modifiers
+
     //this.height = voicesBoundingBox.getH(); FIXME
+
+    // Consider clefs
+    let clefs: Vex.Flow.Clef[] = stave.getModifiers(
+      Vex.Flow.StaveModifier.Position.LEFT,
+      Vex.Flow.Clef.category
+    );
+    for (let clef of clefs) {
+      voicesBoundingBox = voicesBoundingBox.mergeWith(
+        this.getClefBoundingBox(clef)
+      );
+    }
+
     this.topBorder = Math.min(
       0,
       Math.floor(stave.getLineForY(voicesBoundingBox.getY()))
@@ -110,4 +122,42 @@ export class MeasureSizeCalculator {
       Math.ceil(stave.getLineForY(voicesBoundingBox.getY() + voicesBoundingBox.getH()))
     );
   }
+
+  private getClefBoundingBox(clef: Vex.Flow.Clef): Vex.Flow.BoundingBox {
+    let glyph: any = clef.glyph;
+    let x_pos: number = clef.x + glyph.x_shift;
+    let y_pos: number = clef.stave.getYForGlyphs() + glyph.y_shift;
+    let scale: number = glyph.scale;
+    let outline: any[] = glyph.metrics.outline;
+    let xmin: number = 0, xmax: number = 0,
+      ymin: number = 0, ymax: number = 0;
+
+    function update(i: number): void {
+      let x: number = outline[i];
+      let y: number = outline[i];
+      xmin = Math.min(xmin, x);
+      xmax = Math.max(xmax, x);
+      ymin = Math.min(ymin, y);
+      ymax = Math.max(ymax, y);
+    }
+
+    for (let i of outline) {
+      switch (<string> outline[i]) {
+        case "m": update(i); break;
+        case "l": update(i); break;
+        case "q": i++; update(i); break;
+        case "b": i++; i++; update(i); break;
+        default: break;
+      }
+
+    }
+    return new Vex.Flow.BoundingBox(
+      x_pos + xmin * scale,
+      y_pos - ymin * scale,
+      (xmax - xmin) * scale,
+      (ymin - ymax) * scale
+    );
+  }
+
+
 }

+ 20 - 2
typings/vexflow.d.ts

@@ -1,4 +1,4 @@
-declare namespace VexFlow {
+declare namespace Vex {
   export module Flow {
 
     export class Formatter {
@@ -15,6 +15,8 @@ declare namespace VexFlow {
       getY(): number;
       getW(): number;
       getH(): number;
+
+      constructor(x: number, y: number, w: number, h: number);
     }
 
     export class Voice {
@@ -33,6 +35,7 @@ declare namespace VexFlow {
       start_x: number;
       end_x: number;
 
+      getYForGlyphs(): number;
       getWidth(): number;
       setWidth(width: number): Stave;
       getNoteStartX(): number;
@@ -40,13 +43,28 @@ declare namespace VexFlow {
       getSpacingBetweenLines(): number;
       getNumLines(): number;
       getLineForY(y: number): number;
+      getModifiers(pos: any, cat: any): Vex.Flow.Clef[]; // FIXME
 
       constructor(x: number, y: number, width: number);
     }
 
+    export class StaveModifier {
+      public static Position: any;
+    }
+
+    export class Clef {
+      public static category: string;
+      public glyph: any;
+      public x: number;
+      public stave: Stave;
+
+      public getBoundingBox(): Vex.Flow.BoundingBox;
+
+    }
+
   }
 }
 
 declare module "vexflow" {
-    export = VexFlow;
+    export = Vex;
 }