Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time Signature simplifications #1513

Merged
merged 4 commits into from
Jan 2, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
first time signature simplifications
mscuthbert committed Dec 31, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit e8ecc069f8af48a389d055387c6ff9519f937ec0
2 changes: 1 addition & 1 deletion src/fonts/bravura_metrics.ts
Original file line number Diff line number Diff line change
@@ -242,7 +242,7 @@ export const BravuraMetrics = {

// These are for numeric digits, such as in time signatures
digits: {
// used by timesig
// used by TimeSignature object
shiftLine: -1,
point: 34,

2 changes: 1 addition & 1 deletion src/fonts/leland_metrics.ts
Original file line number Diff line number Diff line change
@@ -237,7 +237,7 @@ export const LelandMetrics = {

// These are for numeric digits, such as in time signatures
digits: {
// used by timesig
// used by TimeSignature objects
shiftLine: -1,
point: 34,

2 changes: 1 addition & 1 deletion src/note.ts
Original file line number Diff line number Diff line change
@@ -488,7 +488,7 @@ export abstract class Note extends Tickable {

/** Accessor to isDotted. */
isDotted(): boolean {
return this.getModifiersByType('Dot').length > 0;
return this.getModifiersByType(Category.Dot).length > 0;
}

/** Accessor to hasStem. */
62 changes: 52 additions & 10 deletions src/timesignature.ts
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ import { defined, RuntimeError } from './util';

export interface TimeSignatureInfo {
glyph: Glyph;
line?: number;
line: number;
num: boolean;
}

@@ -60,13 +60,21 @@ export class TimeSignature extends StaveModifier {
bottomLine: number;
topLine: number;

protected info: TimeSignatureInfo;
protected line: number = 0;
protected glyph: Glyph;
protected is_numeric: boolean = true;
protected validate_args: boolean;

constructor(timeSpec: string = '4/4', customPadding = 15, validate_args = true) {
super();
this.validate_args = validate_args;

// violates DRY w/ setTimeSig(timeSpec) but needed to convince TypeScript that all is well.
const info = this.parseTimeSpec(timeSpec);
this.glyph = info.glyph;
this.is_numeric = info.num;
this.line = info.line;

const padding = customPadding;

const musicFont = Tables.currentMusicFont();
@@ -75,8 +83,7 @@ export class TimeSignature extends StaveModifier {
this.topLine = 2 + fontLineShift;
this.bottomLine = 4 + fontLineShift;
this.setPosition(StaveModifierPosition.BEGIN);
this.info = this.parseTimeSpec(timeSpec);
this.setWidth(defined(this.info.glyph.getMetrics().width));
this.setWidth(defined(this.glyph.getMetrics().width));
rvilarl marked this conversation as resolved.
Show resolved Hide resolved
this.setPadding(padding);
}

@@ -97,6 +104,7 @@ export class TimeSignature extends StaveModifier {
const parts = timeSpec.split('/');

return {
line: 0,
num: true,
glyph: this.makeTimeSignatureGlyph(parts[0] ?? '', parts[1] ?? ''),
};
@@ -106,26 +114,60 @@ export class TimeSignature extends StaveModifier {
return new TimeSignatureGlyph(this, topDigits, botDigits, 'timeSig0', this.point);
}

/**
* Returns line, num, glyph -- but these can be accessed directly w/ getters and setters.
*/
getInfo(): TimeSignatureInfo {
return this.info;
const { line, is_numeric, glyph } = this;
return { line, num: is_numeric, glyph };
}

/**
* Set a new time signature specification without changing customPadding, etc.
*/
setTimeSig(timeSpec: string): this {
this.info = this.parseTimeSpec(timeSpec);
const info = this.parseTimeSpec(timeSpec);
this.glyph = info.glyph;
this.is_numeric = info.num;
this.line = info.line;
return this;
}

getLine(): number {
return this.line;
}

setLine(line: number) {
this.line = line;
}

getGlyph(): Glyph {
return this.glyph;
}

setGlyph(glyph: Glyph) {
this.glyph = glyph;
mscuthbert marked this conversation as resolved.
Show resolved Hide resolved
}

getIsNumeric(): boolean {
return this.is_numeric;
}

setIsNumeric(isNumeric: boolean) {
this.is_numeric = isNumeric;
}

draw(): void {
const stave = this.checkStave();
const ctx = stave.checkContext();
this.setRendered();

this.applyStyle(ctx);
ctx.openGroup('timesignature', this.getAttribute('id'));
this.info.glyph.setStave(stave);
this.info.glyph.setContext(ctx);
this.placeGlyphOnLine(this.info.glyph, stave, this.info.line);
this.info.glyph.renderToStave(this.x);
this.glyph.setStave(stave);
this.glyph.setContext(ctx);
this.placeGlyphOnLine(this.glyph, stave, this.line);
this.glyph.renderToStave(this.x);
ctx.closeGroup();
this.restoreStyle(ctx);
}
20 changes: 10 additions & 10 deletions src/timesignote.ts
Original file line number Diff line number Diff line change
@@ -3,22 +3,21 @@

import { ModifierContext } from './modifiercontext';
import { Note } from './note';
import { TimeSignature, TimeSignatureInfo } from './timesignature';
import { TimeSignature } from './timesignature';
import { Category } from './typeguard';

export class TimeSigNote extends Note {
static get CATEGORY(): string {
return Category.TimeSigNote;
}

protected timeSigInfo: TimeSignatureInfo;
protected timeSig: TimeSignature;

constructor(timeSpec: string, customPadding?: number) {
super({ duration: 'b' });

const timeSignature = new TimeSignature(timeSpec, customPadding);
this.timeSigInfo = timeSignature.getInfo();
this.setWidth(this.timeSigInfo.glyph.getMetrics().width);
this.timeSig = new TimeSignature(timeSpec, customPadding);
this.setWidth(this.timeSig.getGlyph().getMetrics().width);

// Note properties
this.ignore_ticks = true;
@@ -41,12 +40,13 @@ export class TimeSigNote extends Note {
const ctx = this.checkContext();
this.setRendered();

if (!this.timeSigInfo.glyph.getContext()) {
this.timeSigInfo.glyph.setContext(ctx);
const tsGlyph = this.timeSig.getGlyph();
if (!tsGlyph.getContext()) {
tsGlyph.setContext(ctx);
}

this.timeSigInfo.glyph.setStave(stave);
this.timeSigInfo.glyph.setYShift(stave.getYForLine(2) - stave.getYForGlyphs());
this.timeSigInfo.glyph.renderToStave(this.getAbsoluteX());
tsGlyph.setStave(stave);
tsGlyph.setYShift(stave.getYForLine(2) - stave.getYForGlyphs());
tsGlyph.renderToStave(this.getAbsoluteX());
}
}
7 changes: 7 additions & 0 deletions tests/timesignature_tests.ts
Original file line number Diff line number Diff line change
@@ -38,6 +38,13 @@ function parser(): void {
const mustPass = ['4/4', '10/12', '1/8', '1234567890/1234567890', 'C', 'C|', '+'];
mustPass.forEach((validString) => timeSig.parseTimeSpec(validString));

timeSig.parseTimeSpec('4/4');
equal(timeSig.getIsNumeric(), true, '4/4 is numeric');
equal(timeSig.getLine(), 0, 'digits are on line 0');
timeSig.parseTimeSpec('C|');
equal(timeSig.getIsNumeric(), false, 'cut time is not numeric');
equal(timeSig.getLine(), 2, 'cut/common are on line 2');

ok(true, 'all pass');
}