Skip to content

Commit

Permalink
Merge pull request #995 from AaronDavidNewman/master
Browse files Browse the repository at this point in the history
Allow modifiers like ChordSymbol to be attached to GlyphNotes.
  • Loading branch information
0xfe authored May 29, 2021
2 parents f5bb3f2 + a5408ac commit 0387d18
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/glyphnote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import { BoundingBox } from './boundingbox';
import { Glyph } from './glyph';
import { Note, NoteStruct } from './note';
import { ModifierContext } from './modifiercontext';

export interface GlyphNoteOptions {
ignoreTicks?: boolean;
line?: number;
}

export class GlyphNote extends Note {
protected options: GlyphNoteOptions;

constructor(glyph: Glyph | undefined, noteStruct: NoteStruct, options?: GlyphNoteOptions) {
super(noteStruct);
this.options = {
Expand All @@ -36,21 +39,40 @@ export class GlyphNote extends Note {
return this.glyph.getBoundingBox();
}

/*
addToModifierContext() {
// Add self to modifier context. `mContext` is the `ModifierContext`
// to be added to.
addToModifierContext(mContext: ModifierContext): this {
this.modifierContext = mContext;
for (let i = 0; i < this.modifiers.length; ++i) {
this.modifierContext.addMember(this.modifiers[i]);
}
this.setPreFormatted(false);
return this;
}
*/

preFormat(): this {
if (!this.preFormatted && this.modifierContext) {
this.modifierContext.preFormat();
}
this.setPreFormatted(true);
return this;
}

drawModifiers(): void {
const ctx = this.checkContext();
ctx.openGroup('modifiers');
for (let i = 0; i < this.modifiers.length; i++) {
const modifier = this.modifiers[i];
modifier.setContext(ctx);
modifier.drawWithStyle();
}
ctx.closeGroup();
}
draw(): void {
const stave = this.checkStave();
const ctx = stave.checkContext();
this.setRendered();
ctx.openGroup('glypheNote', this.getAttribute('id'));

// Context is set when setStave is called on Note
if (!this.glyph.getContext()) {
Expand All @@ -62,5 +84,7 @@ export class GlyphNote extends Note {

const x = this.isCenterAligned() ? this.getAbsoluteX() - this.getWidth() / 2 : this.getAbsoluteX();
this.glyph.renderToStave(x);
this.drawModifiers();
ctx.closeGroup();
}
}
41 changes: 41 additions & 0 deletions tests/glyphnote_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,52 @@ const GlyphNoteTests = (function () {
var GlyphNote = {
Start: function () {
QUnit.module('GlyphNote');
run('GlyphNote with ChordSymbols', GlyphNote.chordChanges, { debug: false, noPadding: false });
run('GlyphNote Positioning', GlyphNote.basic, { debug: false, noPadding: false });
run('GlyphNote No Stave Padding', GlyphNote.basic, { debug: true, noPadding: true });
run('GlyphNote RepeatNote', GlyphNote.repeatNote, { debug: false, noPadding: true });
},
chordChanges: function (options) {
VF.Registry.enableDefaultRegistry(new VF.Registry());

var vf = VF.Test.makeFactory(options, 300, 200);
var system = vf.System({
x: 50,
width: 250,
debugFormatter: options.params.debug,
noPadding: options.params.noPadding,
options: { alpha: options.params.alpha },
});

var score = vf.EasyScore();

var notes = [
vf.GlyphNote(new VF.Glyph('repeatBarSlash', 40), { duration: 'q' }),
vf.GlyphNote(new VF.Glyph('repeatBarSlash', 40), { duration: 'q' }),
vf.GlyphNote(new VF.Glyph('repeatBarSlash', 40), { duration: 'q' }),
vf.GlyphNote(new VF.Glyph('repeatBarSlash', 40), { duration: 'q' }),
];
const chord1 = vf
.ChordSymbol({ reportWidth: false })
.addText('F7')
.setHorizontal('left')
.addGlyphOrText('(#11b9)', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUPERSCRIPT });
const chord2 = vf
.ChordSymbol()
.addText('F7')
.setHorizontal('left')
.addGlyphOrText('#11', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUPERSCRIPT })
.addGlyphOrText('b9', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUBSCRIPT });

notes[0].addModifier(chord1, 0);
notes[2].addModifier(chord2, 0);
const voice = score.voice(notes);
system.addStave({ voices: [voice], debugNoteMetrics: options.params.debug });
system.addConnector().setType(VF.StaveConnector.type.BRACKET);
vf.draw();
VF.Registry.disableDefaultRegistry();
ok(true);
},
basic: function (options) {
VF.Registry.enableDefaultRegistry(new VF.Registry());

Expand Down

0 comments on commit 0387d18

Please sign in to comment.