Skip to content

Commit

Permalink
Use defined() in Clef.
Browse files Browse the repository at this point in the history
  • Loading branch information
ronyeh committed Aug 31, 2021
1 parent 9f0f53d commit 8a3c6e8
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/clef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Co-author: Benjamin W. Bohl
// MIT License

import { RuntimeError, log } from './util';
import { RuntimeError, log, defined } from './util';
import { StaveModifier } from './stavemodifier';
import { Glyph } from './glyph';
import { Stave } from './stave';
Expand Down Expand Up @@ -169,41 +169,41 @@ export class Clef extends StaveModifier {

/** Get clef width. */
getWidth(): number {
if (this.type === 'tab' && !this.stave) {
throw new RuntimeError('ClefError', "Can't get width without stave.");
if (this.type === 'tab') {
defined(this.stave, 'ClefError', "Can't get width without stave.");
}
return this.width;
}

/** Set associated stave. */
setStave(stave: Stave): this {
this.stave = stave;
if (this.type !== 'tab') return this;
if (!this.glyph) throw new RuntimeError('ClefError', "Can't set stave without glyph.");

const numLines = this.stave.getOptions().num_lines;
const point = this.musicFont.lookupMetric(`clef.lineCount.${numLines}.point`);
const shiftY = this.musicFont.lookupMetric(`clef.lineCount.${numLines}.shiftY`);
this.glyph.setPoint(point);
this.glyph.setYShift(shiftY);

if (this.type === 'tab') {
const glyph = defined(this.glyph, 'ClefError', "Can't set stave without glyph.");

const numLines = this.stave.getOptions().num_lines;
const point = this.musicFont.lookupMetric(`clef.lineCount.${numLines}.point`);
const shiftY = this.musicFont.lookupMetric(`clef.lineCount.${numLines}.shiftY`);
glyph.setPoint(point);
glyph.setYShift(shiftY);
}
return this;
}

/** Render clef. */
draw(): void {
// TODO: Is this line buggy? this.x is a number, so !this.x returns true when x is 0.
if (!this.x) throw new RuntimeError('ClefError', "Can't draw clef without x.");
if (!this.glyph) throw new RuntimeError('ClefError', "Can't draw clef without glyph.");
const glyph = defined(this.glyph, 'ClefError', "Can't draw clef without glyph.");
const stave = this.checkStave();
this.setRendered();

this.glyph.setStave(stave);
this.glyph.setContext(stave.getContext());
glyph.setStave(stave);
glyph.setContext(stave.getContext());
if (this.clef.line !== undefined) {
this.placeGlyphOnLine(this.glyph, stave, this.clef.line);
this.placeGlyphOnLine(glyph, stave, this.clef.line);
}

this.glyph.renderToStave(this.x);
glyph.renderToStave(this.x);

if (this.annotation !== undefined && this.attachment !== undefined) {
this.placeGlyphOnLine(this.attachment, stave, this.annotation.line);
Expand Down

0 comments on commit 8a3c6e8

Please sign in to comment.