Skip to content

Commit 8066271

Browse files
committed
Add docs, setWidth, one bug fixed
Document everything in timeSignature.ts Add general documentation for what StaveModifier.placeGlyphOnLine does. Reset width any time Glyph is changed Fix one bug in TimeSigGlyph where the `.line` of the TimeSignature was being ignored.
1 parent 2944b07 commit 8066271

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

src/stavemodifier.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ export class StaveModifier extends Element {
9090
return this;
9191
}
9292

93+
/**
94+
* Runs setYShift() for the Glyph object so that it matches the position of line for
95+
* the Stave provided. A `customShift` can also be given (measured in the same units
96+
* as `setYShift` not in lines) and this will be added after all other positions are
97+
* calculated from the Stave.
98+
*
99+
* Note that this routine only sets the yShift; it does not actually "place" (meaning
100+
* draw) the Glyph on the Stave. Call .draw() afterwards to do that.
101+
*/
93102
placeGlyphOnLine(glyph: Glyph, stave: Stave, line?: number, customShift = 0): void {
94103
glyph.setYShift(stave.getYForLine(line ?? 0) - stave.getYForGlyphs() + customShift);
95104
}

src/timesigglyph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class TimeSignatureGlyph extends Glyph {
104104
y = stave.getYForLine(this.timeSignature.bottomLine);
105105
for (let i = 0; i < this.botGlyphs.length; ++i) {
106106
const glyph = this.botGlyphs[i];
107-
this.timeSignature.placeGlyphOnLine(glyph, stave, 0);
107+
this.timeSignature.placeGlyphOnLine(glyph, stave, this.timeSignature.getLine());
108108
Glyph.renderOutline(ctx, glyph.getMetrics().outline, this.scale, start_x + glyph.getMetrics().x_shift, y);
109109
start_x += defined(glyph.getMetrics().width);
110110
}

src/timesignature.ts

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ const assertIsValidTimeSig = (timeSpec: string) => {
3636
});
3737
};
3838

39+
/**
40+
* A TimeSignature is a StaveModifier that can make its appropriate Glyphs directly from
41+
* a provided "timeSpec" such as "4/4", "C|" (cut time), or even something more advanced
42+
* such as "3/4(6/8)" or "2/4+5/8".
43+
*/
3944
export class TimeSignature extends StaveModifier {
4045
static get CATEGORY(): string {
4146
return Category.TimeSignature;
@@ -57,8 +62,8 @@ export class TimeSignature extends StaveModifier {
5762
}
5863

5964
point: number;
60-
bottomLine: number;
61-
topLine: number;
65+
bottomLine: number; // bottomLine and topLine are used to calculate the position of the
66+
topLine: number; // top row of digits in a numeric TimeSignature.
6267

6368
protected timeSpec: string = '4/4';
6469
protected line: number = 0;
@@ -81,10 +86,14 @@ export class TimeSignature extends StaveModifier {
8186
this.bottomLine = 4 + fontLineShift;
8287
this.setPosition(StaveModifierPosition.BEGIN);
8388
this.setTimeSig(timeSpec);
84-
this.setWidth(defined(this.glyph.getMetrics().width));
8589
this.setPadding(padding);
8690
}
8791

92+
/**
93+
* Return TimeSignatureInfo given a string, consisting of line (number),
94+
* num (boolean: same as TimeSignature.getIsNumeric()), and glyph (a Glyph or
95+
* TimeSignatureGlyph object).
96+
*/
8897
parseTimeSpec(timeSpec: string): TimeSignatureInfo {
8998
if (timeSpec === 'C' || timeSpec === 'C|') {
9099
const { line, code, point } = TimeSignature.glyphs[timeSpec];
@@ -108,12 +117,18 @@ export class TimeSignature extends StaveModifier {
108117
};
109118
}
110119

111-
makeTimeSignatureGlyph(topDigits: string, botDigits: string): Glyph {
120+
/**
121+
* Returns a new TimeSignatureGlyph (a Glyph subclass that knows how to draw both
122+
* top and bottom digits along with plus signs etc.)
123+
*/
124+
makeTimeSignatureGlyph(topDigits: string, botDigits: string): TimeSignatureGlyph {
125+
// note that 'code' is ignored by TimeSignatureGlyph when rendering.
112126
return new TimeSignatureGlyph(this, topDigits, botDigits, 'timeSig0', this.point);
113127
}
114128

115129
/**
116-
* Returns line, num, glyph -- but these can be accessed directly w/ getters and setters.
130+
* Returns {line, num (=getIsNumeric), glyph} --
131+
* but these can also be accessed directly w/ getters and setters.
117132
*/
118133
getInfo(): TimeSignatureInfo {
119134
const { line, is_numeric, glyph } = this;
@@ -128,40 +143,72 @@ export class TimeSignature extends StaveModifier {
128143
setTimeSig(timeSpec: string): this {
129144
this.timeSpec = timeSpec;
130145
const info = this.parseTimeSpec(timeSpec);
131-
this.glyph = info.glyph;
146+
this.setGlyph(info.glyph);
132147
this.is_numeric = info.num;
133148
this.line = info.line;
134149
return this;
135150
}
136151

152+
/**
153+
* Return the timeSpec (such as '4/4' or 'C|' or even '2/4+3/8') of the TimeSignature
154+
*/
137155
getTimeSpec(): string {
138156
return this.timeSpec;
139157
}
140158

159+
/**
160+
* Return the staff line that the TimeSignature sits on. Generally 0 for numerator/
161+
* denominator time signatures such as 3/4 and 2 for cut/common.
162+
*/
141163
getLine(): number {
142164
return this.line;
143165
}
144166

167+
/**
168+
* Set the line number that the TimeSignature sits on. Half-values are acceptable
169+
* for spaces, etc. Can be altered, for instance, for signatures that sit above the
170+
* staff in large orchestral scores.
171+
*/
145172
setLine(line: number) {
146173
this.line = line;
147174
}
148175

176+
/**
177+
* Get the Glyph object used to create the time signature. Numeric time signatures
178+
* such as 3/8 have a composite Glyph stored as a single Glyph object.
179+
*/
149180
getGlyph(): Glyph {
150181
return this.glyph;
151182
}
152183

184+
/**
185+
* Set the Glyph object used to draw the time signature, and update the width of the
186+
* TimeSignature to match. The Glyph must define width in its metrics.
187+
*/
153188
setGlyph(glyph: Glyph) {
154189
this.glyph = glyph;
190+
this.setWidth(defined(this.glyph.getMetrics().width));
155191
}
156192

193+
/**
194+
* Return a boolean on whether this TimeSignature is drawn with one or more numbers
195+
* (such as 4/4) or not (as in cut time).
196+
*/
157197
getIsNumeric(): boolean {
158198
return this.is_numeric;
159199
}
160200

201+
/**
202+
* Set whether this TimeSignature is drawn with one or more numbers.
203+
*/
161204
setIsNumeric(isNumeric: boolean) {
162205
this.is_numeric = isNumeric;
163206
}
164207

208+
/**
209+
* Draw the time signature on a Stave using its RenderContext. Both setStave
210+
* and setContext must already be run.
211+
*/
165212
draw(): void {
166213
const stave = this.checkStave();
167214
const ctx = stave.checkContext();

0 commit comments

Comments
 (0)