Skip to content

Commit

Permalink
clef migrated
Browse files Browse the repository at this point in the history
  • Loading branch information
rvilarl committed May 3, 2021
1 parent cb6f687 commit 8e9c5e3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
49 changes: 36 additions & 13 deletions src/clef.js → src/clef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,41 @@
import { Vex } from './vex';
import { StaveModifier } from './stavemodifier';
import { Glyph } from './glyph';
import { Stave } from './stave';
import { AnnotationInfo } from './types/common';

export interface ClefType {
// eslint-disable-next-line
point?: any;
code: string;
line?: number;
}

// To enable logging for this class, set `Vex.Flow.Clef.DEBUG` to `true`.
function L(...args) {
function L(
// eslint-disable-next-line
...args: any []) {
if (Clef.DEBUG) Vex.L('Vex.Flow.Clef', args);
}

export class Clef extends StaveModifier {
static get CATEGORY() {
static DEBUG: boolean;

annotation?: AnnotationInfo;
clef: ClefType = Clef.types['treble'];

protected glyph?: Glyph;
protected attachment?: Glyph;
protected size?: string;
protected type?: string;

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

// Every clef name is associated with a glyph code from the font file
// and a default stave line number.
static get types() {
static get types(): Record<string, ClefType> {
return {
treble: {
code: 'gClef',
Expand Down Expand Up @@ -77,7 +98,7 @@ export class Clef extends StaveModifier {

// Create a new clef. The parameter `clef` must be a key from
// `Clef.types`.
constructor(type, size, annotation) {
constructor(type: string, size?: string, annotation?: string) {
super();
this.setAttribute('type', 'Clef');

Expand All @@ -87,11 +108,11 @@ export class Clef extends StaveModifier {
L('Creating clef:', type);
}

getCategory() {
getCategory(): string {
return Clef.CATEGORY;
}

setType(type, size, annotation) {
setType(type: string, size?: string, annotation?: string): this {
this.type = type;
this.clef = Clef.types[type];
if (size === undefined) {
Expand All @@ -114,7 +135,7 @@ export class Clef extends StaveModifier {
this.annotation = { code, point, line, x_shift };

this.attachment = new Glyph(this.annotation.code, this.annotation.point);
this.attachment.metrics.x_max = 0;
if (this.attachment.metrics) this.attachment.metrics.x_max = 0;
this.attachment.setXShift(this.annotation.x_shift);
} else {
this.annotation = undefined;
Expand All @@ -123,17 +144,18 @@ export class Clef extends StaveModifier {
return this;
}

getWidth() {
getWidth(): number {
if (this.type === 'tab' && !this.stave) {
throw new Vex.RERR('ClefError', "Can't get width without stave.");
}

return this.width;
}

setStave(stave) {
setStave(stave: Stave): this {
this.stave = stave;
if (this.type !== 'tab') return this;
if (!this.glyph) throw new Vex.RERR('ClefError', "Can't set stave without glyph.");

const numLines = this.stave.getOptions().num_lines;
const point = this.musicFont.lookupMetric(`clef.lineCount.${numLines}.point`);
Expand All @@ -144,23 +166,24 @@ export class Clef extends StaveModifier {
return this;
}

draw() {
draw(): void {
if (!this.x) throw new Vex.RERR('ClefError', "Can't draw clef without x.");
if (!this.stave) throw new Vex.RERR('ClefError', "Can't draw clef without stave.");
if (!this.glyph) throw new Vex.RERR('ClefError', "Can't draw clef without glyph.");
this.setRendered();

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

this.glyph.renderToStave(this.x);

if (this.annotation !== undefined) {
if (this.annotation !== undefined && this.attachment !== undefined) {
this.placeGlyphOnLine(this.attachment, this.stave, this.annotation.line);
this.attachment.setStave(this.stave);
this.attachment.setContext(this.stave.context);
this.attachment.setContext(this.stave.getContext());
this.attachment.renderToStave(this.x);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/clefnote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { Vex } from './vex';
import { Note } from './note';
import { Clef } from './clef';
import { Glyph } from './glyph';
import { RenderContext, ClefType } from './types/common';
import { RenderContext } from './types/common';
import { BoundingBox } from './boundingbox';
import { ClefType } from './clef';

/** @constructor */
export class ClefNote extends Note {
Expand Down Expand Up @@ -82,7 +83,7 @@ export class ClefNote extends Note {
const abs_x = this.getAbsoluteX();

this.glyph.setStave(this.stave);
this.glyph.setYShift(this.stave.getYForLine(this.clef.line) - this.stave.getYForGlyphs());
this.glyph.setYShift(this.stave.getYForLine(this.clef.line ?? 0) - this.stave.getYForGlyphs());
this.glyph.renderToStave(abs_x);

// If the Vex.Flow.Clef has an annotation, such as 8va, draw it.
Expand Down
9 changes: 4 additions & 5 deletions src/types/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ export interface ModifierContextState {
top_text_line: number;
}

/** TODO: Move to ModifierContext */
export interface ClefType {
// eslint-disable-next-line
point: any;
/** TODO: Move to Annotation */
export interface AnnotationInfo {
code: string;
line: number;
x_shift: number;
point: number;
}

0 comments on commit 8e9c5e3

Please sign in to comment.