vexflow
Version:
A JavaScript library for rendering music notation and guitar tablature.
60 lines (59 loc) • 1.81 kB
JavaScript
import { Formatter } from './formatter.js';
import { Modifier } from './modifier.js';
import { Tables } from './tables.js';
import { Voice } from './voice.js';
export class NoteSubGroup extends Modifier {
static get CATEGORY() {
return "NoteSubGroup";
}
static format(groups, state) {
if (!groups || groups.length === 0)
return false;
let width = 0;
for (let i = 0; i < groups.length; ++i) {
const group = groups[i];
group.preFormat();
width += group.getWidth();
}
state.leftShift += width;
return true;
}
constructor(subNotes) {
super();
this.preFormatted = false;
this.position = Modifier.Position.LEFT;
this.subNotes = subNotes;
this.subNotes.forEach((subNote) => {
subNote.setIgnoreTicks(false);
});
this.width = 0;
this.formatter = new Formatter();
this.voice = new Voice({
numBeats: 4,
beatValue: 4,
resolution: Tables.RESOLUTION,
}).setStrict(false);
this.voice.addTickables(this.subNotes);
}
preFormat() {
if (this.preFormatted)
return;
this.formatter.joinVoices([this.voice]).format([this.voice], 0);
this.setWidth(this.formatter.getMinTotalWidth());
this.preFormatted = true;
}
setWidth(width) {
this.width = width;
return this;
}
getWidth() {
return this.width;
}
draw() {
const ctx = this.checkContext();
const note = this.checkAttachedNote();
this.setRendered();
this.alignSubNotesWithNote(this.subNotes, note);
this.subNotes.forEach((subNote) => subNote.setContext(ctx).drawWithStyle());
}
}