Skip to content

Commit

Permalink
parenthesis as modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
rvilarl committed Jan 1, 2022
1 parent c303b6a commit 7197f3a
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/articulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Modifier } from './modifier';
import { ModifierContextState } from './modifiercontext';
import { Note } from './note';
import { Stave } from './stave';
import { isStaveNote, StaveNote } from './stavenote';
import { isStaveNote } from './stavenote';
import { Stem } from './stem';
import { StemmableNote } from './stemmablenote';
import { Tables } from './tables';
Expand Down
6 changes: 4 additions & 2 deletions src/dot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ export class Dot extends Modifier {
if (isStaveNote(note)) {
const index = dot.checkIndex();
props = note.getKeyProps()[index];
shift = note.getRightDisplacedHeadPx();
// consider right displaced head with no previous modifier
if (right_shift === 0) shift = note.getRightDisplacedHeadPx();
else shift = right_shift;
} else if (note.getCategory() === 'TabNote') {
props = { line: 0.5 }; // Shim key props for dot placement
shift = 0;
shift = right_shift;
} else {
// note object is not StaveNote or TabNote.
throw new RuntimeError('Internal', 'Unexpected instance.');
Expand Down
2 changes: 2 additions & 0 deletions src/modifiercontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Modifier } from './modifier';
import { Note } from './note';
import { NoteSubGroup } from './notesubgroup';
import { Ornament } from './ornament';
import { Parenthesis } from './parenthesis';
import { StaveNote } from './stavenote';
import { StringNumber } from './stringnumber';
import { Stroke } from './strokes';
Expand Down Expand Up @@ -80,6 +81,7 @@ export class ModifierContext {
// members are formatted and rendered before higher ones.
this.PREFORMAT = [
StaveNote,
Parenthesis,
Dot,
FretHandFinger,
Accidental,
Expand Down
10 changes: 0 additions & 10 deletions src/notehead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,6 @@ export class NoteHead extends Note {
if (this.noteType === 's') {
const staveSpace = this.checkStave().getSpacingBetweenLines();
drawSlashNoteHead(ctx, this.duration, head_x, y, stem_direction, staveSpace);
} else if (this.noteType === 'p') {
Glyph.renderGlyph(ctx, head_x, y, glyph_font_scale, 'noteheadParenthesisLeft', {
category: `noteHead.standard.noteheadParenthesisLeft`,
});
Glyph.renderGlyph(ctx, head_x + 5, y, glyph_font_scale, this.glyph_code, {
category: this.custom_glyph ? `noteHead.custom.${categorySuffix}` : `noteHead.standard.${categorySuffix}`,
});
Glyph.renderGlyph(ctx, head_x + 18, y, glyph_font_scale, 'noteheadParenthesisRight', {
category: `noteHead.standard.noteheadParenthesisRight`,
});
} else {
Glyph.renderGlyph(ctx, head_x, y, glyph_font_scale, this.glyph_code, {
category: this.custom_glyph ? `noteHead.custom.${categorySuffix}` : `noteHead.standard.${categorySuffix}`,
Expand Down
89 changes: 89 additions & 0 deletions src/parenthesis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// [VexFlow](https://vexflow.com) - Copyright (c) Mohit Muthanna 2010.
// Author: Rodrigo Vilar
// MIT License

import { Glyph } from './glyph';
import { Modifier, ModifierPosition } from './modifier';
import { ModifierContextState } from './modifiercontext';
import { Note } from './note';

/** Parenthesis implements parenthesis modifiers for notes.*/
export class Parenthesis extends Modifier {
static get CATEGORY(): string {
return 'Parenthesis';
}

protected scale: number;

// Arrange dots inside a ModifierContext.
static format(parentheses: Parenthesis[], state: ModifierContextState): boolean {
const { right_shift, left_shift } = state;

if (!parentheses || parentheses.length === 0) return false;

let x_widthL = 0;
let x_widthR = 0;

for (let i = 0; i < parentheses.length; ++i) {
const parenthesis = parentheses[i];
const note = parenthesis.getNote();
const pos = parenthesis.getPosition();
const index = parenthesis.checkIndex();
const props = note.getKeyProps()[index];

let shift = 0;

if (pos === ModifierPosition.RIGHT) {
shift = (props.displaced ? note.getRightDisplacedHeadPx() : 0) + right_shift;
x_widthR += shift + parenthesis.width;
}
if (pos === ModifierPosition.LEFT) {
shift = (props.displaced ? note.getLeftDisplacedHeadPx() : 0) + left_shift;
x_widthL += shift + parenthesis.width;
}
parenthesis.setXShift(shift);
}
state.left_shift += x_widthL;
state.right_shift += x_widthR;

return true;
}

constructor(position: ModifierPosition) {
super();

this.position = position ?? Modifier.Position.LEFT;

this.scale = 39;
this.setWidth(7);
}

setNote(note: Note): this {
this.note = note;
this.scale = 39;
if (note.getCategory() === 'GraceNote') {
this.scale = (39 * 3) / 5;
this.setWidth(3);
}
return this;
}

draw(): void {
const ctx = this.checkContext();
const note = this.checkAttachedNote();
this.setRendered();

const start = note.getModifierStartXY(this.position, this.index, { forceFlagRight: true });
const x = start.x + this.x_shift;
const y = start.y + this.y_shift;
if (this.position == Modifier.Position.RIGHT) {
Glyph.renderGlyph(ctx, x + 1, y, this.scale, 'noteheadParenthesisRight', {
category: `noteHead.standard.noteheadParenthesisRight`,
});
} else if (this.position == Modifier.Position.LEFT) {
Glyph.renderGlyph(ctx, x - 2, y, this.scale, 'noteheadParenthesisLeft', {
category: `noteHead.standard.noteheadParenthesisLeft`,
});
}
}
}
9 changes: 8 additions & 1 deletion src/stavenote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
import { Beam } from './beam';
import { BoundingBox } from './boundingbox';
import { ElementStyle } from './element';
import { Modifier } from './modifier';
import { Modifier, ModifierPosition } from './modifier';
import { ModifierContextState } from './modifiercontext';
import { Note, NoteStruct } from './note';
import { NoteHead } from './notehead';
import { Parenthesis } from './parenthesis';
import { Stave } from './stave';
import { Stem, StemOptions } from './stem';
import { StemmableNote } from './stemmablenote';
Expand Down Expand Up @@ -426,6 +427,12 @@ export class StaveNote extends StemmableNote {
}
this.reset();
this.buildFlag();
if (this.noteType === 'p') {
for (let i = 0; i < this.keys.length; i++) {
this.addModifier(new Parenthesis(ModifierPosition.LEFT), i);
this.addModifier(new Parenthesis(ModifierPosition.RIGHT), i);
}
}
}

reset(): this {
Expand Down
10 changes: 0 additions & 10 deletions src/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,6 @@ export class Tables {
static TABLATURE_FONT_SCALE = 39;

static SLASH_NOTEHEAD_WIDTH = 15;
static PARENTHESIS_NOTEHEAD_WIDTH = 20;
static STAVE_LINE_DISTANCE = 10;

// HACK:
Expand Down Expand Up @@ -946,7 +945,6 @@ const durationCodes: Record<string, any> = {
p: {
// Parenthesis drawn by notehead.draw()
code_head: 'noteheadDoubleWhole',
getWidth: () => Tables.PARENTHESIS_NOTEHEAD_WIDTH,
},
},
},
Expand Down Expand Up @@ -1008,7 +1006,6 @@ const durationCodes: Record<string, any> = {
p: {
// Parenthesis drawn by notehead.draw()
code_head: 'noteheadWhole',
getWidth: () => Tables.PARENTHESIS_NOTEHEAD_WIDTH,
},
},
},
Expand Down Expand Up @@ -1071,7 +1068,6 @@ const durationCodes: Record<string, any> = {
p: {
// Parenthesis drawn by notehead.draw()
code_head: 'noteheadHalf',
getWidth: () => Tables.PARENTHESIS_NOTEHEAD_WIDTH,
},
},
},
Expand Down Expand Up @@ -1135,7 +1131,6 @@ const durationCodes: Record<string, any> = {
p: {
// Parenthesis drawn by notehead.draw()
code_head: 'noteheadBlack',
getWidth: () => Tables.PARENTHESIS_NOTEHEAD_WIDTH,
},
},
},
Expand Down Expand Up @@ -1204,7 +1199,6 @@ const durationCodes: Record<string, any> = {
p: {
// Parenthesis drawn by notehead.draw()
code_head: 'noteheadBlack',
getWidth: () => Tables.PARENTHESIS_NOTEHEAD_WIDTH,
},
},
},
Expand Down Expand Up @@ -1273,7 +1267,6 @@ const durationCodes: Record<string, any> = {
p: {
// Parenthesis drawn by notehead.draw()
code_head: 'noteheadBlack',
getWidth: () => Tables.PARENTHESIS_NOTEHEAD_WIDTH,
},
},
},
Expand Down Expand Up @@ -1342,7 +1335,6 @@ const durationCodes: Record<string, any> = {
p: {
// Parenthesis drawn by notehead.draw()
code_head: 'noteheadBlack',
getWidth: () => Tables.PARENTHESIS_NOTEHEAD_WIDTH,
},
},
},
Expand Down Expand Up @@ -1411,7 +1403,6 @@ const durationCodes: Record<string, any> = {
p: {
// Parenthesis drawn by notehead.draw()
code_head: 'noteheadBlack',
getWidth: () => Tables.PARENTHESIS_NOTEHEAD_WIDTH,
},
},
},
Expand Down Expand Up @@ -1480,7 +1471,6 @@ const durationCodes: Record<string, any> = {
p: {
// Parenthesis drawn by notehead.draw()
code_head: 'noteheadBlack',
getWidth: () => Tables.PARENTHESIS_NOTEHEAD_WIDTH,
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const VERSION: string = '4.0.0';
export const ID: string = '109389bccab8a1bec421a9a4118515a3bc0e0de2';
export const DATE: string = '2021-12-24T09:52:12.732Z';
export const ID: string = '806f6abbacc9908136cc74b425982052218c0253';
export const DATE: string = '2021-12-29T17:59:12.128Z';

0 comments on commit 7197f3a

Please sign in to comment.