Skip to content

Commit ca49a6e

Browse files
committed
More docs fix plus move sumArray
1 parent c8a9298 commit ca49a6e

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

src/element.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export interface ElementStyle {
5858
/**
5959
* Element implements a generic base class for VexFlow, with implementations
6060
* of general functions and properties that can be inherited by all VexFlow elements.
61+
*
62+
* The Element is an abstract class that needs to be subclassed to work. It handles
63+
* style and text-font properties for the Element and any child elements, along with
64+
* working with the Registry to create unique ids, but does not have any tools for
65+
* formatting x or y positions or connections to a Stave.
6166
*/
6267
export abstract class Element {
6368
static get CATEGORY(): string {
@@ -66,7 +71,6 @@ export abstract class Element {
6671

6772
// all Element objects keep a list of children that they are responsible and which
6873
// inherit the style of their parents.
69-
// Currently used by StaveNote for noteheads and
7074
protected children: Element[] = [];
7175
protected static ID: number = 1000;
7276
protected static newID(): string {
@@ -270,7 +274,7 @@ export abstract class Element {
270274
return this.attrs;
271275
}
272276

273-
/** Return an attribute. */
277+
/** Return an attribute, such as 'id', 'type' or 'class'. */
274278
// eslint-disable-next-line
275279
getAttribute(name: string): any {
276280
return this.attrs[name];
@@ -283,7 +287,7 @@ export abstract class Element {
283287
if (element) return element as unknown as SVGElement;
284288
}
285289

286-
/** Set an attribute. */
290+
/** Set an attribute such as 'id', 'class', or 'type'. */
287291
setAttribute(name: string, value: string | undefined): this {
288292
const oldID = this.attrs.id;
289293
const oldValue = this.attrs[name];
@@ -298,18 +302,18 @@ export abstract class Element {
298302
return this.boundingBox;
299303
}
300304

301-
/** Return the context. */
305+
/** Return the context, such as an SVGContext or CanvasContext object. */
302306
getContext(): RenderContext | undefined {
303307
return this.context;
304308
}
305309

306-
/** Set the context. */
310+
/** Set the context to an SVGContext or CanvasContext object */
307311
setContext(context?: RenderContext): this {
308312
this.context = context;
309313
return this;
310314
}
311315

312-
/** Validate and return the context. */
316+
/** Validate and return the rendering context. */
313317
checkContext(): RenderContext {
314318
return defined(this.context, 'NoContext', 'No rendering context attached to instance.');
315319
}
@@ -318,19 +322,24 @@ export abstract class Element {
318322
// Font Handling
319323

320324
/**
321-
* Provide a CSS compatible font string (e.g., 'bold 16px Arial').
325+
* Provide a CSS compatible font string (e.g., 'bold 16px Arial') that will be applied
326+
* to text (not glyphs).
322327
*/
323328
set font(f: string) {
324329
this.setFont(f);
325330
}
326331

327-
/** Returns the CSS compatible font string. */
332+
/** Returns the CSS compatible font string for the text font. */
328333
get font(): string {
329334
return Font.toCSSString(this.textFont);
330335
}
331336

332337
/**
333-
* Set the element's font family, size, weight, style (e.g., `Arial`, `10pt`, `bold`, `italic`).
338+
* Set the element's text font family, size, weight, style
339+
* (e.g., `Arial`, `10pt`, `bold`, `italic`).
340+
*
341+
* This attribute does not determine the font used for musical Glyphs like treble clefs.
342+
*
334343
* @param font is 1) a `FontInfo` object or
335344
* 2) a string formatted as CSS font shorthand (e.g., 'bold 10pt Arial') or
336345
* 3) a string representing the font family (at least one of `size`, `weight`, or `style` must also be provided).
@@ -376,6 +385,10 @@ export abstract class Element {
376385
return this;
377386
}
378387

388+
/**
389+
* Get the css string describing this Element's text font. e.g.,
390+
* 'bold 10pt Arial'.
391+
*/
379392
getFont(): string {
380393
if (!this.textFont) {
381394
this.resetFont();

src/formatter.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { TabStave } from './tabstave';
1616
import { Tickable } from './tickable';
1717
import { TickContext } from './tickcontext';
1818
import { isNote, isStaveNote } from './typeguard';
19-
import { defined, log, midLine, RuntimeError } from './util';
19+
import { defined, log, midLine, RuntimeError, sumArray } from './util';
2020
import { Voice } from './voice';
2121

2222
interface Distance {
@@ -61,9 +61,6 @@ export interface AlignmentModifierContexts {
6161
type addToContextFn<T> = (tickable: Tickable, context: T, voiceIndex: number) => void;
6262
type makeContextFn<T> = (tick?: { tickID: number }) => T;
6363

64-
// Helper function
65-
const sumArray = (arr: number[]) => arr.reduce((a, b) => a + b, 0);
66-
6764
/**
6865
* Create `Alignment`s for each tick in `voices`. Also calculate the
6966
* total number of ticks in voices.

src/util.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,10 @@ export function normalizeAngle(a: number): number {
8989
}
9090
return a;
9191
}
92+
93+
/**
94+
* Return the sum of an array of numbers.
95+
*/
96+
export function sumArray(arr: number[]): number {
97+
return arr.reduce((a, b) => a + b, 0);
98+
}

src/voice.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Stave } from './stave';
99
import { Tables } from './tables';
1010
import { Tickable } from './tickable';
1111
import { Category } from './typeguard';
12-
import { defined, RuntimeError } from './util';
12+
import { defined, RuntimeError, sumArray } from './util';
1313

1414
export interface VoiceTime {
1515
num_beats: number;
@@ -88,6 +88,7 @@ export class Voice extends Element {
8888

8989
// Recalculate total ticks.
9090
this.totalTicks = new Fraction(this.time.num_beats * (this.time.resolution / this.time.beat_value), 1);
91+
// until tickables are added, the smallestTickCount is the same as the stated totalTicks duration.
9192
this.smallestTickCount = this.totalTicks.clone();
9293
}
9394

@@ -116,14 +117,14 @@ export class Voice extends Element {
116117
return this.tickables;
117118
}
118119

119-
/** Get the voice mode. */
120+
/** Get the voice mode (Voice.Mode.SOFT, STRICT, or FULL) */
120121
getMode(): number {
121122
return this.mode;
122123
}
123124

124125
/**
125126
* Set the voice mode.
126-
* @param mode value from `VoiceMode`
127+
* @param mode value from `VoiceMode` or Voice.Mode
127128
*/
128129
setMode(mode: number): this {
129130
this.mode = mode;
@@ -204,7 +205,7 @@ export class Voice extends Element {
204205
protected reCalculateExpTicksUsed(): number {
205206
const totalTicks = this.ticksUsed.value();
206207
const exp = (tickable: Tickable) => Math.pow(this.options.softmaxFactor, tickable.getTicks().value() / totalTicks);
207-
this.expTicksUsed = this.tickables.map(exp).reduce((a, b) => a + b, 0);
208+
this.expTicksUsed = sumArray(this.tickables.map(exp));
208209
return this.expTicksUsed;
209210
}
210211

0 commit comments

Comments
 (0)