Sfoglia il codice sorgente

Fixed bug with undefined notes in voiceEntries

Andrea Condoluci 9 anni fa
parent
commit
973a9868f5

+ 2 - 0
src/MusicSheetAPI.ts

@@ -65,6 +65,8 @@ export class MusicSheetAPI {
             this.width,
             height
         );
+        // Fix the label problem
+        this.drawer.translate(0, 100);
         this.drawer.scale(this.zoom);
         this.drawer.drawSheet(this.graphic);
     }

+ 2 - 2
src/MusicalScore/Graphical/VexFlow/VexFlowConverter.ts

@@ -25,7 +25,7 @@ export class VexFlowConverter {
 
     public static duration(fraction: Fraction): string {
         let dur: number = fraction.RealValue;
-        if (dur === 1) {
+        if (dur >= 1) {
             return "w";
         } else if (dur < 1 && dur >= 0.5) {
             return "h";
@@ -132,7 +132,7 @@ export class VexFlowConverter {
                 type = "bass";
                 break;
             case ClefEnum.C:
-                type = "baritone-c";
+                type = "alto";
                 break;
             case ClefEnum.percussion:
                 type = "percussion";

+ 5 - 1
src/MusicalScore/Graphical/VexFlow/VexFlowMeasure.ts

@@ -238,7 +238,11 @@ export class VexFlowMeasure extends StaffMeasure {
                     for (let entry of beam[1]) {
                         notes.push((<VexFlowStaffEntry>entry).vfNotes[voiceID]);
                     }
-                    vfbeams.push(new Vex.Flow.Beam(notes, true));
+                    if (notes.length > 1) {
+                        vfbeams.push(new Vex.Flow.Beam(notes, true));
+                    } else {
+                        console.log("Warning! Beam with no notes! Trying to ignore, but this is a serious problem.");
+                    }
                 }
             }
         }

+ 12 - 3
src/MusicalScore/Graphical/VexFlow/VexFlowMusicSheetDrawer.ts

@@ -26,6 +26,11 @@ export class VexFlowMusicSheetDrawer extends MusicSheetDrawer {
         this.renderer.resize(x, y);
     }
 
+    public translate(x: number, y: number): void {
+        // FIXME
+        (this.ctx as any).vexFlowCanvasContext.translate(x, y);
+    }
+
     /**
      * Converts a distance from unit to pixel space.
      * @param unitDistance the distance in units
@@ -56,6 +61,10 @@ export class VexFlowMusicSheetDrawer extends MusicSheetDrawer {
     protected renderLabel(graphicalLabel: GraphicalLabel, layer: number, bitmapWidth: number,
                           bitmapHeight: number, heightInPixel: number, screenPosition: PointF2D): void {
         // ToDo: implement!
+        let ctx: CanvasRenderingContext2D = (this.ctx as any).vexFlowCanvasContext;
+        ctx.font = Math.floor(graphicalLabel.Label.fontHeight * 10) + "px 'Times New Roman'";
+        console.log(graphicalLabel.Label.text, screenPosition.x, screenPosition.y);
+        ctx.fillText(graphicalLabel.Label.text, screenPosition.x, screenPosition.y);
     }
 
     /**
@@ -76,7 +85,7 @@ export class VexFlowMusicSheetDrawer extends MusicSheetDrawer {
      */
     protected applyScreenTransformation(point: PointF2D): PointF2D {
         // ToDo: implement!
-        return point;
+        return new PointF2D(point.x * 10.0, point.y * 10.0);
     }
 
     /**
@@ -85,7 +94,7 @@ export class VexFlowMusicSheetDrawer extends MusicSheetDrawer {
      * @returns {RectangleF2D}
      */
     protected applyScreenTransformationForRect(rectangle: RectangleF2D): RectangleF2D {
-        // ToDo: implement!
-        return rectangle;
+        // FIXME Check if correct
+        return new RectangleF2D(rectangle.x * 10, rectangle.y * 10, rectangle.width * 10, rectangle.height * 10);
     }
 }

+ 9 - 1
src/MusicalScore/Graphical/VexFlow/VexFlowTextMeasurer.ts

@@ -6,7 +6,15 @@ import {FontStyles} from "../../../Common/Enums/FontStyles";
  */
 
 export class VexFlowTextMeasurer implements ITextMeasurer {
+    constructor() {
+        let canvas: HTMLCanvasElement = document.createElement("canvas");
+        this.context = canvas.getContext("2d");
+        this.context.font = "20px 'Times New Roman'";
+    }
+    private context: CanvasRenderingContext2D;
+
     public computeTextWidthToHeightRatio(text: string, font: Fonts, style: FontStyles): number {
-        return text.length / 2;
+        let size: any = this.context.measureText(text);
+        return size.width / 20;
     }
 }

+ 2 - 2
src/MusicalScore/ScoreIO/VoiceGenerator.ts

@@ -724,7 +724,7 @@ export class VoiceGenerator {
                                     maxTieNoteFraction = Fraction.plus(this.currentStaffEntry.Timestamp, this.currentNote.Length);
                                 }
                                 let i: number = this.currentVoiceEntry.Notes.indexOf(this.currentNote);
-                                if (i !== -1) { delete this.currentVoiceEntry.Notes[i]; }
+                                if (i !== -1) { this.currentVoiceEntry.Notes.splice(i, 1); }
                                 if (
                                     this.currentVoiceEntry.Articulations.length === 1
                                     && this.currentVoiceEntry.Articulations[0] === ArticulationEnum.fermata
@@ -817,7 +817,7 @@ export class VoiceGenerator {
                     }
                     // delete currentNote from Notes:
                     let i: number = this.currentVoiceEntry.Notes.indexOf(this.currentNote);
-                    if (i !== -1) { delete this.currentVoiceEntry.Notes[i]; }
+                    if (i !== -1) { this.currentVoiceEntry.Notes.splice(i, 1); }
                 }
             }
         }