import Vex from "vexflow" ; import { VoiceEntry } from "../../VoiceData/VoiceEntry" ; import { GraphicalVoiceEntry } from "../GraphicalVoiceEntry" ; import { GraphicalStaffEntry } from "../GraphicalStaffEntry" ; import { unitInPixels } from "./VexFlowMusicSheetDrawer" ; import { NoteEnum } from "../../../Common/DataObjects/Pitch" ; import { Note } from "../../VoiceData/Note" ; import { ColoringModes } from "./../DrawingParameters" ; import { GraphicalNote } from "../GraphicalNote" ; export class VexFlowVoiceEntry extends GraphicalVoiceEntry { private mVexFlowStaveNote : Vex . Flow . StemmableNote ; constructor ( parentVoiceEntry , parentStaffEntry ) { super ( parentVoiceEntry , parentStaffEntry ) ; } public applyBordersFromVexflow ( ) { const staveNote = ( this . vfStaveNote as any ) ; if ( ! staveNote . getNoteHeadBeginX ) { return ; } const boundingBox = staveNote . getBoundingBox ( ) ; const modifierWidth = staveNote . getNoteHeadBeginX ( ) - boundingBox . x ; this . PositionAndShape . RelativePosition . y = boundingBox . y / unitInPixels ; this . PositionAndShape . BorderTop = 0 ; this . PositionAndShape . BorderBottom = boundingBox . h / unitInPixels ; this . PositionAndShape . BorderLeft = - ( modifierWidth + staveNote . width / 2 ) / unitInPixels ; this . PositionAndShape . BorderRight = ( boundingBox . w - modifierWidth ) / unitInPixels ; } public set vfStaveNote ( value ) { this . mVexFlowStaveNote = value ; } 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 ( ) { const defaultColorNotehead = this . rules . DefaultColorNotehead ; const defaultColorRest = this . rules . DefaultColorRest ; const defaultColorStem = this . rules . DefaultColorStem ; const transparentColor = "#00000000" ; let noteheadColor ; let sourceNoteNoteheadColor ; const vfStaveNote = ( < VexFlowVoiceEntry > ( this as any ) ) . vfStaveNote ; for ( let i = 0 ; i < this . notes . length ; i ++ ) { const note = this . notes [ i ] ; sourceNoteNoteheadColor = note . sourceNote . NoteheadColor ; noteheadColor = sourceNoteNoteheadColor ; if ( this . rules . ColoringMode === ColoringModes . AutoColoring || this . rules . ColoringMode === ColoringModes . CustomColorSet ) { if ( note . sourceNote . isRest ( ) ) { noteheadColor = this . rules . ColoringSetCurrent . getValue ( - 1 ) ; } else { const fundamentalNote = note . sourceNote . Pitch . FundamentalNote ; noteheadColor = this . rules . ColoringSetCurrent . getValue ( fundamentalNote ) ; } } if ( ! note . sourceNote . PrintObject ) { noteheadColor = transparentColor ; } else if ( ! noteheadColor || noteheadColor === "#000000" ) { noteheadColor = this . rules . DefaultColorNotehead ; } if ( ! sourceNoteNoteheadColor && this . rules . ColoringMode === ColoringModes . XML && note . sourceNote . PrintObject ) { if ( ! note . sourceNote . isRest ( ) && defaultColorNotehead ) { noteheadColor = defaultColorNotehead ; } else if ( note . sourceNote . isRest ( ) && defaultColorRest ) { noteheadColor = defaultColorRest ; } } if ( noteheadColor && note . sourceNote . PrintObject ) { note . sourceNote . NoteheadColorCurrentlyRendered = noteheadColor ; } else if ( ! noteheadColor ) { continue ; } if ( this . rules . ColoringEnabled && note . sourceNote . NoteBeam && this . rules . ColorBeams ) { const beamNotes = note . sourceNote . NoteBeam . Notes ; let colorBeam = true ; for ( let j = 0 ; j < beamNotes . length ; j ++ ) { if ( beamNotes [ j ] . NoteheadColorCurrentlyRendered !== noteheadColor ) { colorBeam = false ; } } if ( colorBeam ) { if ( vfStaveNote ?. beam ?. setStyle ) { vfStaveNote . beam . setStyle ( { fillStyle : noteheadColor , strokeStyle : noteheadColor } ) ; } } } if ( vfStaveNote ) { if ( vfStaveNote . note_heads ) { const notehead = vfStaveNote . note_heads [ i ] ; if ( notehead ) { notehead . setStyle ( { fillStyle : noteheadColor , strokeStyle : noteheadColor } ) ; } } if ( ( vfStaveNote as any ) . setLedgerLineStyle ) { if ( noteheadColor === transparentColor ) { ( vfStaveNote as any ) . setLedgerLineStyle ( { fillStyle : noteheadColor , strokeStyle : noteheadColor , lineWidth : this . rules . LedgerLineWidth } ) ; } else { ( vfStaveNote as any ) . setLedgerLineStyle ( { fillStyle : this . rules . LedgerLineColorDefault , lineWidth : this . rules . LedgerLineWidth , strokeStyle : this . rules . LedgerLineColorDefault } ) ; } } } } let stemColor = defaultColorStem ; let setVoiceEntryStemColor = false ; if ( this . rules . ColoringEnabled ) { stemColor = this . parentVoiceEntry . StemColor ; if ( ! stemColor || stemColor === "#000000" ) { stemColor = defaultColorStem ; } if ( this . rules . ColorStemsLikeNoteheads && noteheadColor ) { stemColor = noteheadColor ; setVoiceEntryStemColor = true ; } } let stemTransparent = true ; for ( const note of this . parentVoiceEntry . Notes ) { if ( note . PrintObject ) { stemTransparent = false ; break ; } } if ( stemTransparent ) { stemColor = transparentColor ; } const stemStyle = { fillStyle : stemColor , strokeStyle : stemColor } ; if ( vfStaveNote && vfStaveNote . setStemStyle ) { if ( ! stemTransparent && setVoiceEntryStemColor ) { this . parentVoiceEntry . StemColor = stemColor ; } vfStaveNote . setStemStyle ( stemStyle ) ; if ( vfStaveNote . flag && vfStaveNote . setFlagStyle && this . rules . ColorFlags ) { vfStaveNote . setFlagStyle ( stemStyle ) ; } } } }