|
@@ -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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|