Skip to content

Commit e8ecc06

Browse files
committed
first time signature simplifications
1 parent 797758d commit e8ecc06

File tree

6 files changed

+72
-23
lines changed

6 files changed

+72
-23
lines changed

src/fonts/bravura_metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ export const BravuraMetrics = {
242242

243243
// These are for numeric digits, such as in time signatures
244244
digits: {
245-
// used by timesig
245+
// used by TimeSignature object
246246
shiftLine: -1,
247247
point: 34,
248248

src/fonts/leland_metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export const LelandMetrics = {
237237

238238
// These are for numeric digits, such as in time signatures
239239
digits: {
240-
// used by timesig
240+
// used by TimeSignature objects
241241
shiftLine: -1,
242242
point: 34,
243243

src/note.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ export abstract class Note extends Tickable {
488488

489489
/** Accessor to isDotted. */
490490
isDotted(): boolean {
491-
return this.getModifiersByType('Dot').length > 0;
491+
return this.getModifiersByType(Category.Dot).length > 0;
492492
}
493493

494494
/** Accessor to hasStem. */

src/timesignature.ts

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { defined, RuntimeError } from './util';
1414

1515
export interface TimeSignatureInfo {
1616
glyph: Glyph;
17-
line?: number;
17+
line: number;
1818
num: boolean;
1919
}
2020

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

63-
protected info: TimeSignatureInfo;
63+
protected line: number = 0;
64+
protected glyph: Glyph;
65+
protected is_numeric: boolean = true;
6466
protected validate_args: boolean;
6567

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

72+
// violates DRY w/ setTimeSig(timeSpec) but needed to convince TypeScript that all is well.
73+
const info = this.parseTimeSpec(timeSpec);
74+
this.glyph = info.glyph;
75+
this.is_numeric = info.num;
76+
this.line = info.line;
77+
7078
const padding = customPadding;
7179

7280
const musicFont = Tables.currentMusicFont();
@@ -75,8 +83,7 @@ export class TimeSignature extends StaveModifier {
7583
this.topLine = 2 + fontLineShift;
7684
this.bottomLine = 4 + fontLineShift;
7785
this.setPosition(StaveModifierPosition.BEGIN);
78-
this.info = this.parseTimeSpec(timeSpec);
79-
this.setWidth(defined(this.info.glyph.getMetrics().width));
86+
this.setWidth(defined(this.glyph.getMetrics().width));
8087
this.setPadding(padding);
8188
}
8289

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

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

117+
/**
118+
* Returns line, num, glyph -- but these can be accessed directly w/ getters and setters.
119+
*/
109120
getInfo(): TimeSignatureInfo {
110-
return this.info;
121+
const { line, is_numeric, glyph } = this;
122+
return { line, num: is_numeric, glyph };
111123
}
112124

125+
/**
126+
* Set a new time signature specification without changing customPadding, etc.
127+
*/
113128
setTimeSig(timeSpec: string): this {
114-
this.info = this.parseTimeSpec(timeSpec);
129+
const info = this.parseTimeSpec(timeSpec);
130+
this.glyph = info.glyph;
131+
this.is_numeric = info.num;
132+
this.line = info.line;
115133
return this;
116134
}
117135

136+
getLine(): number {
137+
return this.line;
138+
}
139+
140+
setLine(line: number) {
141+
this.line = line;
142+
}
143+
144+
getGlyph(): Glyph {
145+
return this.glyph;
146+
}
147+
148+
setGlyph(glyph: Glyph) {
149+
this.glyph = glyph;
150+
}
151+
152+
getIsNumeric(): boolean {
153+
return this.is_numeric;
154+
}
155+
156+
setIsNumeric(isNumeric: boolean) {
157+
this.is_numeric = isNumeric;
158+
}
159+
118160
draw(): void {
119161
const stave = this.checkStave();
120162
const ctx = stave.checkContext();
121163
this.setRendered();
122164

123165
this.applyStyle(ctx);
124166
ctx.openGroup('timesignature', this.getAttribute('id'));
125-
this.info.glyph.setStave(stave);
126-
this.info.glyph.setContext(ctx);
127-
this.placeGlyphOnLine(this.info.glyph, stave, this.info.line);
128-
this.info.glyph.renderToStave(this.x);
167+
this.glyph.setStave(stave);
168+
this.glyph.setContext(ctx);
169+
this.placeGlyphOnLine(this.glyph, stave, this.line);
170+
this.glyph.renderToStave(this.x);
129171
ctx.closeGroup();
130172
this.restoreStyle(ctx);
131173
}

src/timesignote.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33

44
import { ModifierContext } from './modifiercontext';
55
import { Note } from './note';
6-
import { TimeSignature, TimeSignatureInfo } from './timesignature';
6+
import { TimeSignature } from './timesignature';
77
import { Category } from './typeguard';
88

99
export class TimeSigNote extends Note {
1010
static get CATEGORY(): string {
1111
return Category.TimeSigNote;
1212
}
1313

14-
protected timeSigInfo: TimeSignatureInfo;
14+
protected timeSig: TimeSignature;
1515

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

19-
const timeSignature = new TimeSignature(timeSpec, customPadding);
20-
this.timeSigInfo = timeSignature.getInfo();
21-
this.setWidth(this.timeSigInfo.glyph.getMetrics().width);
19+
this.timeSig = new TimeSignature(timeSpec, customPadding);
20+
this.setWidth(this.timeSig.getGlyph().getMetrics().width);
2221

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

44-
if (!this.timeSigInfo.glyph.getContext()) {
45-
this.timeSigInfo.glyph.setContext(ctx);
43+
const tsGlyph = this.timeSig.getGlyph();
44+
if (!tsGlyph.getContext()) {
45+
tsGlyph.setContext(ctx);
4646
}
4747

48-
this.timeSigInfo.glyph.setStave(stave);
49-
this.timeSigInfo.glyph.setYShift(stave.getYForLine(2) - stave.getYForGlyphs());
50-
this.timeSigInfo.glyph.renderToStave(this.getAbsoluteX());
48+
tsGlyph.setStave(stave);
49+
tsGlyph.setYShift(stave.getYForLine(2) - stave.getYForGlyphs());
50+
tsGlyph.renderToStave(this.getAbsoluteX());
5151
}
5252
}

tests/timesignature_tests.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ function parser(): void {
3838
const mustPass = ['4/4', '10/12', '1/8', '1234567890/1234567890', 'C', 'C|', '+'];
3939
mustPass.forEach((validString) => timeSig.parseTimeSpec(validString));
4040

41+
timeSig.parseTimeSpec('4/4');
42+
equal(timeSig.getIsNumeric(), true, '4/4 is numeric');
43+
equal(timeSig.getLine(), 0, 'digits are on line 0');
44+
timeSig.parseTimeSpec('C|');
45+
equal(timeSig.getIsNumeric(), false, 'cut time is not numeric');
46+
equal(timeSig.getLine(), 2, 'cut/common are on line 2');
47+
4148
ok(true, 'all pass');
4249
}
4350

0 commit comments

Comments
 (0)