Skip to content

Commit

Permalink
Standardize CATEGORY / getCategory() / this.setAttribute('type', ...).
Browse files Browse the repository at this point in the history
Improve the typeguard functions to check for .CATEGORY property
directly.

Fixes #1107
  • Loading branch information
ronyeh committed Sep 18, 2021
1 parent 46e4cac commit b39973e
Show file tree
Hide file tree
Showing 71 changed files with 587 additions and 611 deletions.
19 changes: 5 additions & 14 deletions src/accidental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import { Flow } from './flow';
import { Music } from './music';
import { Modifier } from './modifier';
import { Glyph } from './glyph';
import { GraceNoteGroup } from './gracenotegroup';
import { GraceNote } from './gracenote';
import { ModifierContextState } from './modifiercontext';
import { Voice } from './voice';
import { Note } from './note';
import { StaveNote } from './stavenote';
import { Tickable } from './tickable';
import { isCategory, isStaveNote } from './typeguard';
import { isCategory, isGraceNote, isGraceNoteGroup, isStaveNote } from './typeguard';

type Line = {
column: number;
Expand Down Expand Up @@ -63,7 +61,7 @@ export class Accidental extends Modifier {

/** Accidentals category string. */
static get CATEGORY(): string {
return 'accidentals';
return 'Accidental';
}

/** Arrange accidentals inside a ModifierContext. */
Expand Down Expand Up @@ -462,9 +460,8 @@ export class Accidental extends Modifier {

// process grace notes
staveNote.getModifiers().forEach((modifier: Modifier) => {
// TODO: Replace with isCategory()?
if (modifier.getCategory() === GraceNoteGroup.CATEGORY) {
(modifier as GraceNoteGroup).getGraceNotes().forEach(processNote);
if (isGraceNoteGroup(modifier)) {
modifier.getGraceNotes().forEach(processNote);
}
});
};
Expand All @@ -480,7 +477,6 @@ export class Accidental extends Modifier {
*/
constructor(type: string) {
super();
this.setAttribute('type', 'Accidental');

L('New accidental: ', type);

Expand Down Expand Up @@ -521,11 +517,6 @@ export class Accidental extends Modifier {
}
}

/** Get element category string. */
getCategory(): string {
return Accidental.CATEGORY;
}

/** Get width in pixels. */
getWidth(): number {
if (this.cautionary) {
Expand All @@ -549,7 +540,7 @@ export class Accidental extends Modifier {
this.note = note;

// Accidentals attached to grace notes are rendered smaller.
if (note.getCategory() === GraceNote.CATEGORY) {
if (isGraceNote(note)) {
this.render_options.font_scale = 25;
this.reset();
}
Expand Down
8 changes: 1 addition & 7 deletions src/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class Annotation extends Modifier {

/** Annotations category string. */
static get CATEGORY(): string {
return 'annotations';
return 'Annotation';
}

/** Text annotations can be positioned and justified relative to the note. */
Expand Down Expand Up @@ -108,7 +108,6 @@ export class Annotation extends Modifier {
*/
constructor(text: string) {
super();
this.setAttribute('type', 'Annotation');

this.text = text;
this.justification = Annotation.Justify.CENTER;
Expand All @@ -119,11 +118,6 @@ export class Annotation extends Modifier {
this.setWidth(Flow.textWidth(text));
}

/** Get element category string. */
getCategory(): string {
return Annotation.CATEGORY;
}

/** Set font family, size, and weight. E.g., `Arial`, `10pt`, `Bold`. */
setFont(family: string, size: number, weight: string = ''): this {
this.font = { family, size, weight };
Expand Down
42 changes: 17 additions & 25 deletions src/articulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { StaveNote } from './stavenote';
import { ModifierContextState } from './modifiercontext';
import { Builder } from './easyscore';
import { TabNote } from './tabnote';
import { GraceNote } from './gracenote';
import { isTabNote } from './typeguard';
import { isGraceNote, isStaveNote, isTabNote } from './typeguard';

export interface ArticulationStruct {
code?: string;
Expand Down Expand Up @@ -64,11 +63,14 @@ function snapLineToStaff(canSitBetweenLines: boolean, line: number, position: nu
}
}

// Helper function for checking if a Note object is either a StaveNote or a GraceNote.
const isStaveOrGraceNote = (note: Note) => isStaveNote(note) || isGraceNote(note);

function getTopY(note: Note, textLine: number): number {
const stemDirection = note.getStemDirection();
const { topY: stemTipY, baseY: stemBaseY } = note.getStemExtents();

if (isStaveOrGraceNoteCategory(note)) {
if (isStaveOrGraceNote(note)) {
if (note.hasStem()) {
if (stemDirection === Stem.UP) {
return stemTipY;
Expand Down Expand Up @@ -97,7 +99,7 @@ function getBottomY(note: Note, textLine: number): number {
const stemDirection = note.getStemDirection();
const { topY: stemTipY, baseY: stemBaseY } = note.getStemExtents();

if (isStaveOrGraceNoteCategory(note)) {
if (isStaveOrGraceNote(note)) {
if (note.hasStem()) {
if (stemDirection === Stem.UP) {
return stemBaseY;
Expand Down Expand Up @@ -137,7 +139,7 @@ function getInitialOffset(note: Note, position: number): number {
(position === ABOVE && note.getStemDirection() === Stem.UP) ||
(position === BELOW && note.getStemDirection() === Stem.DOWN);

if (isStaveOrGraceNoteCategory(note)) {
if (isStaveOrGraceNote(note)) {
if (note.hasStem() && isOnStemTip) {
return 0.5;
} else {
Expand All @@ -154,12 +156,6 @@ function getInitialOffset(note: Note, position: number): number {
}
}

// Helper function for checking if a Note object is either a StaveNote or a GraceNote.
function isStaveOrGraceNoteCategory(note: Note): boolean {
const category = note.getCategory();
return category === StaveNote.CATEGORY || category === GraceNote.CATEGORY;
}

/**
* Articulations and Accents are modifiers that can be
* attached to notes. The complete list of articulations is available in
Expand All @@ -168,22 +164,24 @@ function isStaveOrGraceNoteCategory(note: Note): boolean {
* See `tests/articulation_tests.ts` for usage examples.
*/
export class Articulation extends Modifier {
/** To enable logging for this class. Set `Vex.Flow.Articulation.DEBUG` to `true`. */
static DEBUG: boolean;

/** Articulations category string. */
static get CATEGORY(): string {
return 'Articulation';
}

protected static readonly INITIAL_OFFSET: number = -0.5;

/** Articulation code provided to the constructor. */
readonly type: string;
protected static readonly INITIAL_OFFSET: number = -0.5;

protected render_options: { font_scale: number };
// articulation defined calling reset in constructor
protected articulation!: ArticulationStruct;
// glyph defined calling reset in constructor
protected glyph!: Glyph;
/** To enable logging for this class. Set `Vex.Flow.Articulation.DEBUG` to `true`. */
static DEBUG: boolean;

/** Articulations category string. */
static get CATEGORY(): string {
return 'articulations';
}

/**
* FIXME:
Expand Down Expand Up @@ -260,7 +258,6 @@ export class Articulation extends Modifier {
*/
constructor(type: string) {
super();
this.setAttribute('type', 'Articulation');

this.type = type;
this.position = BELOW;
Expand All @@ -280,11 +277,6 @@ export class Articulation extends Modifier {
this.setWidth(defined(this.glyph.getMetrics().width));
}

/** Get element category string. */
getCategory(): string {
return Articulation.CATEGORY;
}

/** Render articulation in position next to note. */
draw(): void {
const ctx = this.checkContext();
Expand Down
8 changes: 6 additions & 2 deletions src/barnote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ function L(...args: any[]) {
* See `tests/barnote_tests.ts` for usage examples.
*/
export class BarNote extends Note {
protected metrics: { widths: Record<string, number> };
/** To enable logging for this class. Set `Vex.Flow.BarNote.DEBUG` to `true`. */
static DEBUG: boolean;

static get CATEGORY(): string {
return 'BarNote';
}

protected metrics: { widths: Record<string, number> };
// Initialized by the constructor via this.setType(type)
protected type!: BarlineType;

constructor(type = BarlineType.SINGLE) {
super({ duration: 'b' });
this.setAttribute('type', 'BarNote');

this.metrics = {
widths: {},
Expand Down
27 changes: 14 additions & 13 deletions src/beam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { Note } from './note';
import { StemmableNote } from './stemmablenote';
import { Voice } from './voice';
import { RenderContext } from './types/common';
import { TabNote } from './tabnote';
import { StaveNote } from './stavenote';
import { isStaveNote, isTabNote } from 'typeguard';

function calculateStemDirection(notes: StemmableNote[]) {
let lineSum = 0;
Expand All @@ -30,20 +29,24 @@ function calculateStemDirection(notes: StemmableNote[]) {
return Stem.UP;
}

const getStemSlope = (firstNote: StemmableNote, lastNote: StemmableNote) => {
function getStemSlope(firstNote: StemmableNote, lastNote: StemmableNote) {
const firstStemTipY = firstNote.getStemExtents().topY;
const firstStemX = firstNote.getStemX();
const lastStemTipY = lastNote.getStemExtents().topY;
const lastStemX = lastNote.getStemX();
return (lastStemTipY - firstStemTipY) / (lastStemX - firstStemX);
};
}

const BEAM_LEFT = 'L';
const BEAM_RIGHT = 'R';
const BEAM_BOTH = 'B';

/** `Beams` span over a set of `StemmableNotes`. */
export class Beam extends Element {
static get CATEGORY(): string {
return 'Beam';
}

render_options: {
flat_beam_offset?: number;
flat_beams: boolean;
Expand All @@ -58,6 +61,7 @@ export class Beam extends Element {
partial_beam_length: number;
min_flat_beam_offset: number;
};

notes: StemmableNote[];
postFormatted: boolean;
slope: number = 0;
Expand All @@ -69,6 +73,7 @@ export class Beam extends Element {
private break_on_indices: number[];
private beam_count: number;
private unbeamable?: boolean;

/**
* Get the default beam groups for a provided time signature.
* Attempt to guess if the time signature is not found in table.
Expand Down Expand Up @@ -133,10 +138,7 @@ export class Beam extends Element {
* @param groups an array of `Fraction` representing beat groupings for the beam
*/
static applyAndGetBeams(voice: Voice, stem_direction?: number, groups?: Fraction[]): Beam[] {
return Beam.generateBeams(voice.getTickables() as StemmableNote[], {
groups,
stem_direction,
});
return Beam.generateBeams(voice.getTickables() as StemmableNote[], { groups, stem_direction });
}

/**
Expand Down Expand Up @@ -432,7 +434,6 @@ export class Beam extends Element {

constructor(notes: StemmableNote[], auto_stem: boolean = false) {
super();
this.setAttribute('type', 'Beam');

if (!notes || notes.length === 0) {
throw new RuntimeError('BadArguments', 'No notes provided for beam.');
Expand Down Expand Up @@ -464,12 +465,12 @@ export class Beam extends Element {

let stem_direction = this.stem_direction;
// Figure out optimal stem direction based on given notes
if (auto_stem && notes[0].getCategory() === StaveNote.CATEGORY) {

if (auto_stem && isStaveNote(notes[0])) {
stem_direction = calculateStemDirection(notes);
} else if (auto_stem && notes[0].getCategory() === TabNote.CATEGORY) {
} else if (auto_stem && isTabNote(notes[0])) {
// Auto Stem TabNotes
const stem_weight = notes.reduce((memo, note) => memo + note.getStemDirection(), 0);

stem_direction = stem_weight > -1 ? Stem.UP : Stem.DOWN;
}

Expand Down Expand Up @@ -903,7 +904,7 @@ export class Beam extends Element {
if (this.postFormatted) return;

// Calculate a smart slope if we're not forcing the beams to be flat.
if (this.notes[0].getCategory() === TabNote.CATEGORY || this.render_options.flat_beams) {
if (isTabNote(this.notes[0]) || this.render_options.flat_beams) {
this.calculateFlatSlope();
} else {
this.calculateSlope();
Expand Down
24 changes: 9 additions & 15 deletions src/bend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,8 @@ export interface BendRenderOptions {

/** Bend implements tablature bends. */
export class Bend extends Modifier {
protected text: string;
protected release: boolean;
protected phrase: BendPhrase[];
protected font: string;
protected render_options: BendRenderOptions;

static get CATEGORY(): string {
return 'bends';
return 'Bend';
}

static get UP(): number {
Expand All @@ -41,7 +35,6 @@ export class Bend extends Modifier {
return 1;
}

// ## Static Methods
// Arrange bends in `ModifierContext`
static format(bends: Bend[], state: ModifierContextState): boolean {
if (!bends || bends.length === 0) return false;
Expand All @@ -63,9 +56,15 @@ export class Bend extends Modifier {
return true;
}

protected text: string;
protected release: boolean;
protected phrase: BendPhrase[];
protected font: string;
protected render_options: BendRenderOptions;

/**
* Constructor.
* Example of a phrase:
* ```
* [{
* type: UP,
* text: "whole"
Expand All @@ -91,13 +90,13 @@ export class Bend extends Modifier {
* text: "1 1/2"
* width: 8;
* }]
* ```
* @param text text for bend ("Full", "Half", etc.) (DEPRECATED)
* @param release if true, render a release. (DEPRECATED)
* @param phrase if set, ignore "text" and "release", and use the more sophisticated phrase specified
*/
constructor(text: string, release: boolean = false, phrase?: BendPhrase[]) {
super();
this.setAttribute('type', 'Bend');

this.text = text;
this.x_shift = 0;
Expand All @@ -121,11 +120,6 @@ export class Bend extends Modifier {
this.updateWidth();
}

/** Get element category string. */
getCategory(): string {
return Bend.CATEGORY;
}

/** Set horizontal shift in pixels. */
setXShift(value: number): this {
this.x_shift = value;
Expand Down
Loading

0 comments on commit b39973e

Please sign in to comment.