Browse Source

refactor: Removed some unwanted vexflow dependencies in general graphical objects

Matthias Uiberacker 5 years ago
parent
commit
90d93b9073

+ 2 - 121
src/MusicalScore/Graphical/GraphicalVoiceEntry.ts

@@ -4,11 +4,6 @@ import { BoundingBox } from "./BoundingBox";
 import { GraphicalNote } from "./GraphicalNote";
 import { GraphicalStaffEntry } from "./GraphicalStaffEntry";
 import { OctaveEnum } from "../VoiceData/Expressions/ContinuousExpressions/OctaveShift";
-import { VexFlowVoiceEntry } from "./VexFlow/VexFlowVoiceEntry";
-import { EngravingRules } from "./EngravingRules";
-import { ColoringModes } from "./DrawingParameters";
-import { NoteEnum } from "../../Common/DataObjects/Pitch";
-import { Note } from "..";
 
 /**
  * The graphical counterpart of a [[VoiceEntry]].
@@ -38,123 +33,9 @@ export class GraphicalVoiceEntry extends GraphicalObject {
         });
     }
 
-    /** (Re-)color notes and stems by setting their Vexflow styles.
-     * Could be made redundant by a Vexflow PR, but Vexflow needs more solid and permanent color methods/variables for that
-     * See VexFlowConverter.StaveNote()
+    /** (Re-)color notes and stems
      */
     public color(): void {
-        const defaultColorNotehead: string = EngravingRules.Rules.DefaultColorNotehead;
-        const defaultColorRest: string = EngravingRules.Rules.DefaultColorRest;
-        const defaultColorStem: string = EngravingRules.Rules.DefaultColorStem;
-        const transparentColor: string = "#00000000"; // transparent color in vexflow
-        let noteheadColor: string; // if null: no noteheadcolor to set (stays black)
-
-        const vfStaveNote: any = (<VexFlowVoiceEntry>(this as any)).vfStaveNote;
-        for (let i: number = 0; i < this.notes.length; i++) {
-            const note: GraphicalNote = this.notes[i];
-
-            noteheadColor = note.sourceNote.NoteheadColor;
-            // Switch between XML colors and automatic coloring
-            if (EngravingRules.Rules.ColoringMode === ColoringModes.AutoColoring ||
-                EngravingRules.Rules.ColoringMode === ColoringModes.CustomColorSet) {
-                if (note.sourceNote.isRest()) {
-                    noteheadColor = EngravingRules.Rules.ColoringSetCurrent.getValue(-1);
-                } else {
-                    const fundamentalNote: NoteEnum = note.sourceNote.Pitch.FundamentalNote;
-                    noteheadColor = EngravingRules.Rules.ColoringSetCurrent.getValue(fundamentalNote);
-                }
-            }
-            if (!note.sourceNote.PrintObject) {
-                noteheadColor = transparentColor; // transparent
-            } else if (!noteheadColor // revert transparency after PrintObject was set to false, then true again
-                || noteheadColor === "#000000" // questionable, because you might want to set specific notes to black,
-                                               // but unfortunately some programs export everything explicitly as black
-                ) {
-                noteheadColor = EngravingRules.Rules.DefaultColorNotehead;
-            }
-
-            // DEBUG runtime coloring test
-            /*const testColor: string = "#FF0000";
-            if (i === 2 && Math.random() < 0.1 && note.sourceNote.NoteheadColor !== testColor) {
-                const measureNumber: number = note.parentVoiceEntry.parentStaffEntry.parentMeasure.MeasureNumber;
-                noteheadColor = testColor;
-                console.log("color changed to " + noteheadColor + " of this note:\n" + note.sourceNote.Pitch.ToString() +
-                    ", in measure #" + measureNumber);
-            }*/
-
-            if (!noteheadColor) {
-                if (!note.sourceNote.isRest() && defaultColorNotehead) {
-                    noteheadColor = defaultColorNotehead;
-                } else if (note.sourceNote.isRest() && defaultColorRest) {
-                    noteheadColor = defaultColorRest;
-                }
-            }
-            if (noteheadColor && note.sourceNote.PrintObject) {
-                note.sourceNote.NoteheadColor = noteheadColor;
-            } else if (!noteheadColor) {
-                continue;
-            }
-
-            // color notebeam if all noteheads have same color and stem coloring enabled
-            if (EngravingRules.Rules.ColoringEnabled && note.sourceNote.NoteBeam && EngravingRules.Rules.ColorStemsLikeNoteheads) {
-                const beamNotes: Note[] = note.sourceNote.NoteBeam.Notes;
-                let colorBeam: boolean = true;
-                for (let j: number = 0; j < beamNotes.length; j++) {
-                    if (beamNotes[j].NoteheadColor !== noteheadColor) {
-                        colorBeam = false;
-                    }
-                }
-                if (colorBeam) {
-                    if (vfStaveNote.beam !== null && vfStaveNote.beam.setStyle) {
-                        vfStaveNote.beam.setStyle({ fillStyle: noteheadColor, strokeStyle: noteheadColor});
-                    }
-                }
-            }
-
-            if (vfStaveNote) {
-                if (vfStaveNote.note_heads) { // see VexFlowConverter, needs Vexflow PR
-                    const notehead: any = vfStaveNote.note_heads[i];
-                    if (notehead) {
-                        notehead.setStyle({ fillStyle: noteheadColor, strokeStyle: noteheadColor });
-                    }
-                }
-            }
-        }
-
-        // color stems
-        let stemColor: string = EngravingRules.Rules.DefaultColorStem; // reset to black/default when coloring was disabled. maybe needed elsewhere too
-        if (EngravingRules.Rules.ColoringEnabled) {
-            stemColor = this.parentVoiceEntry.StemColor; // TODO: once coloringSetCustom gets stem color, respect it
-            if (!stemColor || EngravingRules.Rules.ColorStemsLikeNoteheads
-                || stemColor === "#000000") { // see above, noteheadColor === "#000000"
-                // condition could be even more fine-grained by only recoloring if there was no custom StemColor set. will be more complex though
-                if (noteheadColor) {
-                    stemColor = noteheadColor;
-                } else if (defaultColorStem) {
-                    stemColor = defaultColorStem;
-                }
-            }
-        }
-        let stemTransparent: boolean = true;
-        for (const note of this.parentVoiceEntry.Notes) {
-            if (note.PrintObject) {
-                stemTransparent = false;
-                break;
-            }
-        }
-        if (stemTransparent) {
-            stemColor = transparentColor;
-        }
-        const stemStyle: Object = { fillStyle: stemColor, strokeStyle: stemColor };
-
-        if (vfStaveNote && vfStaveNote.setStemStyle) {
-            if (!stemTransparent) {
-                this.parentVoiceEntry.StemColor = stemColor;
-            }
-            vfStaveNote.setStemStyle(stemStyle);
-            if (vfStaveNote.flag && vfStaveNote.setFlagStyle && EngravingRules.Rules.ColorFlags) {
-                vfStaveNote.setFlagStyle(stemStyle);
-            }
-        }
+        // override
     }
 }

+ 3 - 10
src/MusicalScore/Graphical/MusicSheetCalculator.ts

@@ -2166,17 +2166,10 @@ export abstract class MusicSheetCalculator {
     }
 
     /**
-     * Re-adjust the x positioning of expressions. Update the skyline afterwards
+     * Re-adjust the x positioning of expressions.
      */
-    private calculateExpressionAlignements(): void {
-        for (const graphicalMusicPage of this.graphicalMusicSheet.MusicPages) {
-            for (const musicSystem of graphicalMusicPage.MusicSystems) {
-                for (const staffLine of musicSystem.StaffLines) {
-                    staffLine.AlignmentManager.alignDynamicExpressions();
-                    staffLine.AbstractExpressions.forEach(ae => ae.updateSkyBottomLine());
-                }
-            }
-        }
+    protected calculateExpressionAlignements(): void {
+        // override
     }
 
     private calculateBeams(): void {

+ 0 - 7
src/MusicalScore/Graphical/StaffLine.ts

@@ -12,7 +12,6 @@ import {GraphicalLabel} from "./GraphicalLabel";
 import { SkyBottomLineCalculator } from "./SkyBottomLineCalculator";
 import { GraphicalOctaveShift } from "./GraphicalOctaveShift";
 import { GraphicalSlur } from "./GraphicalSlur";
-import { AlignmentManager } from "./AlignmentManager";
 import { AbstractGraphicalExpression } from "./AbstractGraphicalExpression";
 
 /**
@@ -26,7 +25,6 @@ export abstract class StaffLine extends GraphicalObject {
     protected parentStaff: Staff;
     protected octaveShifts: GraphicalOctaveShift[] = [];
     protected skyBottomLine: SkyBottomLineCalculator;
-    protected alignmentManager: AlignmentManager;
     protected lyricLines: GraphicalLine[] = [];
     protected lyricsDashes: GraphicalLabel[] = [];
     protected abstractExpressions: AbstractGraphicalExpression[] = [];
@@ -40,7 +38,6 @@ export abstract class StaffLine extends GraphicalObject {
         this.parentStaff = parentStaff;
         this.boundingBox = new BoundingBox(this, parentSystem.PositionAndShape);
         this.skyBottomLine = new SkyBottomLineCalculator(this);
-        this.alignmentManager = new AlignmentManager(this);
     }
 
     public get Measures(): GraphicalMeasure[] {
@@ -104,10 +101,6 @@ export abstract class StaffLine extends GraphicalObject {
         this.parentStaff = value;
     }
 
-    public get AlignmentManager(): AlignmentManager {
-        return this.alignmentManager;
-    }
-
     public get SkyBottomLineCalculator(): SkyBottomLineCalculator {
         return this.skyBottomLine;
     }

+ 6 - 6
src/MusicalScore/Graphical/AlignmentManager.ts → src/MusicalScore/Graphical/VexFlow/AlignmentManager.ts

@@ -1,9 +1,9 @@
-import { StaffLine } from "./StaffLine";
-import { BoundingBox } from "./BoundingBox";
-import { VexFlowContinuousDynamicExpression } from "./VexFlow/VexFlowContinuousDynamicExpression";
-import { AbstractGraphicalExpression } from "./AbstractGraphicalExpression";
-import { PointF2D } from "../../Common/DataObjects/PointF2D";
-import { EngravingRules } from "./EngravingRules";
+import { StaffLine } from "../StaffLine";
+import { BoundingBox } from "../BoundingBox";
+import { VexFlowContinuousDynamicExpression } from "./VexFlowContinuousDynamicExpression";
+import { AbstractGraphicalExpression } from "../AbstractGraphicalExpression";
+import { PointF2D } from "../../../Common/DataObjects/PointF2D";
+import { EngravingRules } from "../EngravingRules";
 
 export class AlignmentManager {
     private parentStaffline: StaffLine;

+ 15 - 0
src/MusicalScore/Graphical/VexFlow/VexFlowMusicSheetCalculator.ts

@@ -47,6 +47,7 @@ import { ContinuousDynamicExpression } from "../../VoiceData/Expressions/Continu
 import { VexFlowContinuousDynamicExpression } from "./VexFlowContinuousDynamicExpression";
 import { InstantaneousTempoExpression } from "../../VoiceData/Expressions";
 import { AlignRestOption } from "../../../OpenSheetMusicDisplay";
+import { VexFlowStaffLine } from "./VexFlowStaffLine";
 
 export class VexFlowMusicSheetCalculator extends MusicSheetCalculator {
   /** space needed for a dash for lyrics spacing, calculated once */
@@ -685,6 +686,20 @@ export class VexFlowMusicSheetCalculator extends MusicSheetCalculator {
   }
 
   /**
+   * Re-adjust the x positioning of expressions. Update the skyline afterwards
+   */
+  protected calculateExpressionAlignements(): void {
+    for (const graphicalMusicPage of this.graphicalMusicSheet.MusicPages) {
+        for (const musicSystem of graphicalMusicPage.MusicSystems) {
+            for (const staffLine of musicSystem.StaffLines) {
+                (<VexFlowStaffLine>staffLine).AlignmentManager.alignDynamicExpressions();
+                staffLine.AbstractExpressions.forEach(ae => ae.updateSkyBottomLine());
+            }
+        }
+    }
+  }
+
+  /**
    * Check if the tied graphical note belongs to any beams or tuplets and react accordingly.
    * @param tiedGraphicalNote
    * @param beams

+ 7 - 0
src/MusicalScore/Graphical/VexFlow/VexFlowStaffLine.ts

@@ -2,13 +2,16 @@ import {StaffLine} from "../StaffLine";
 import {MusicSystem} from "../MusicSystem";
 import {Staff} from "../../VoiceData/Staff";
 import { VexFlowSlur } from "./VexFlowSlur";
+import { AlignmentManager } from "./AlignmentManager";
 
 export class VexFlowStaffLine extends StaffLine {
     constructor(parentSystem: MusicSystem, parentStaff: Staff) {
         super(parentSystem, parentStaff);
+        this.alignmentManager = new AlignmentManager(this);
     }
 
     protected slursInVFStaffLine: VexFlowSlur[] = [];
+    protected alignmentManager: AlignmentManager;
 
     public get SlursInVFStaffLine(): VexFlowSlur[] {
         return this.slursInVFStaffLine;
@@ -16,4 +19,8 @@ export class VexFlowStaffLine extends StaffLine {
     public addVFSlurToVFStaffline(vfSlur: VexFlowSlur): void {
         this.slursInVFStaffLine.push(vfSlur);
     }
+
+    public get AlignmentManager(): AlignmentManager {
+        return this.alignmentManager;
+    }
 }

+ 125 - 0
src/MusicalScore/Graphical/VexFlow/VexFlowVoiceEntry.ts

@@ -3,6 +3,11 @@ import { VoiceEntry } from "../../VoiceData/VoiceEntry";
 import { GraphicalVoiceEntry } from "../GraphicalVoiceEntry";
 import { GraphicalStaffEntry } from "../GraphicalStaffEntry";
 import { unitInPixels } from "./VexFlowMusicSheetDrawer";
+import { EngravingRules } from "../EngravingRules";
+import { GraphicalNote } from "..";
+import { NoteEnum } from "../../../Common/DataObjects/Pitch";
+import { Note } from "../../VoiceData/Note";
+import { ColoringModes } from "./../DrawingParameters";
 
 export class VexFlowVoiceEntry extends GraphicalVoiceEntry {
     private mVexFlowStaveNote: Vex.Flow.StemmableNote;
@@ -33,4 +38,124 @@ export class VexFlowVoiceEntry extends GraphicalVoiceEntry {
     public get vfStaveNote(): Vex.Flow.StemmableNote {
         return this.mVexFlowStaveNote;
     }
+
+    /** (Re-)color notes and stems by setting their Vexflow styles.
+     * Could be made redundant by a Vexflow PR, but Vexflow needs more solid and permanent color methods/variables for that
+     * See VexFlowConverter.StaveNote()
+     */
+    public color(): void {
+        const defaultColorNotehead: string = EngravingRules.Rules.DefaultColorNotehead;
+        const defaultColorRest: string = EngravingRules.Rules.DefaultColorRest;
+        const defaultColorStem: string = EngravingRules.Rules.DefaultColorStem;
+        const transparentColor: string = "#00000000"; // transparent color in vexflow
+        let noteheadColor: string; // if null: no noteheadcolor to set (stays black)
+
+        const vfStaveNote: any = (<VexFlowVoiceEntry>(this as any)).vfStaveNote;
+        for (let i: number = 0; i < this.notes.length; i++) {
+            const note: GraphicalNote = this.notes[i];
+
+            noteheadColor = note.sourceNote.NoteheadColor;
+            // Switch between XML colors and automatic coloring
+            if (EngravingRules.Rules.ColoringMode === ColoringModes.AutoColoring ||
+                EngravingRules.Rules.ColoringMode === ColoringModes.CustomColorSet) {
+                if (note.sourceNote.isRest()) {
+                    noteheadColor = EngravingRules.Rules.ColoringSetCurrent.getValue(-1);
+                } else {
+                    const fundamentalNote: NoteEnum = note.sourceNote.Pitch.FundamentalNote;
+                    noteheadColor = EngravingRules.Rules.ColoringSetCurrent.getValue(fundamentalNote);
+                }
+            }
+            if (!note.sourceNote.PrintObject) {
+                noteheadColor = transparentColor; // transparent
+            } else if (!noteheadColor // revert transparency after PrintObject was set to false, then true again
+                || noteheadColor === "#000000" // questionable, because you might want to set specific notes to black,
+                                               // but unfortunately some programs export everything explicitly as black
+                ) {
+                noteheadColor = EngravingRules.Rules.DefaultColorNotehead;
+            }
+
+            // DEBUG runtime coloring test
+            /*const testColor: string = "#FF0000";
+            if (i === 2 && Math.random() < 0.1 && note.sourceNote.NoteheadColor !== testColor) {
+                const measureNumber: number = note.parentVoiceEntry.parentStaffEntry.parentMeasure.MeasureNumber;
+                noteheadColor = testColor;
+                console.log("color changed to " + noteheadColor + " of this note:\n" + note.sourceNote.Pitch.ToString() +
+                    ", in measure #" + measureNumber);
+            }*/
+
+            if (!noteheadColor) {
+                if (!note.sourceNote.isRest() && defaultColorNotehead) {
+                    noteheadColor = defaultColorNotehead;
+                } else if (note.sourceNote.isRest() && defaultColorRest) {
+                    noteheadColor = defaultColorRest;
+                }
+            }
+            if (noteheadColor && note.sourceNote.PrintObject) {
+                note.sourceNote.NoteheadColor = noteheadColor;
+            } else if (!noteheadColor) {
+                continue;
+            }
+
+            // color notebeam if all noteheads have same color and stem coloring enabled
+            if (EngravingRules.Rules.ColoringEnabled && note.sourceNote.NoteBeam && EngravingRules.Rules.ColorStemsLikeNoteheads) {
+                const beamNotes: Note[] = note.sourceNote.NoteBeam.Notes;
+                let colorBeam: boolean = true;
+                for (let j: number = 0; j < beamNotes.length; j++) {
+                    if (beamNotes[j].NoteheadColor !== noteheadColor) {
+                        colorBeam = false;
+                    }
+                }
+                if (colorBeam) {
+                    if (vfStaveNote.beam !== null && vfStaveNote.beam.setStyle) {
+                        vfStaveNote.beam.setStyle({ fillStyle: noteheadColor, strokeStyle: noteheadColor});
+                    }
+                }
+            }
+
+            if (vfStaveNote) {
+                if (vfStaveNote.note_heads) { // see VexFlowConverter, needs Vexflow PR
+                    const notehead: any = vfStaveNote.note_heads[i];
+                    if (notehead) {
+                        notehead.setStyle({ fillStyle: noteheadColor, strokeStyle: noteheadColor });
+                    }
+                }
+            }
+        }
+
+        // color stems
+        let stemColor: string = EngravingRules.Rules.DefaultColorStem; // reset to black/default when coloring was disabled. maybe needed elsewhere too
+        if (EngravingRules.Rules.ColoringEnabled) {
+            stemColor = this.parentVoiceEntry.StemColor; // TODO: once coloringSetCustom gets stem color, respect it
+            if (!stemColor || EngravingRules.Rules.ColorStemsLikeNoteheads
+                || stemColor === "#000000") { // see above, noteheadColor === "#000000"
+                // condition could be even more fine-grained by only recoloring if there was no custom StemColor set. will be more complex though
+                if (noteheadColor) {
+                    stemColor = noteheadColor;
+                } else if (defaultColorStem) {
+                    stemColor = defaultColorStem;
+                }
+            }
+        }
+        let stemTransparent: boolean = true;
+        for (const note of this.parentVoiceEntry.Notes) {
+            if (note.PrintObject) {
+                stemTransparent = false;
+                break;
+            }
+        }
+        if (stemTransparent) {
+            stemColor = transparentColor;
+        }
+        const stemStyle: Object = { fillStyle: stemColor, strokeStyle: stemColor };
+
+        if (vfStaveNote && vfStaveNote.setStemStyle) {
+            if (!stemTransparent) {
+                this.parentVoiceEntry.StemColor = stemColor;
+            }
+            vfStaveNote.setStemStyle(stemStyle);
+            if (vfStaveNote.flag && vfStaveNote.setFlagStyle && EngravingRules.Rules.ColorFlags) {
+                vfStaveNote.setFlagStyle(stemStyle);
+            }
+        }
+    }
 }

+ 1 - 1
src/MusicalScore/Graphical/index.ts

@@ -3,7 +3,7 @@
 export * from "./AbstractGraphicalExpression";
 export * from "./AbstractGraphicalInstruction";
 export * from "./AccidentalCalculator";
-export * from "./AlignmentManager";
+export * from "./VexFlow/AlignmentManager";
 export * from "./BoundingBox";
 export * from "./Clickable";
 export * from "./DrawingEnums";