forked from 0xfe/vexflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclef.ts
206 lines (181 loc) · 5.11 KB
/
clef.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// [VexFlow](https://vexflow.com) - Copyright (c) Mohit Muthanna Cheppudira 2013.
// Co-author: Benjamin W. Bohl
// MIT License
import { Glyph } from './glyph';
import { Stave } from './stave';
import { StaveModifier, StaveModifierPosition } from './stavemodifier';
import { Tables } from './tables';
import { Category } from './typeguard';
import { defined, log } from './util';
export interface ClefType {
code: string;
line: number;
}
export interface ClefAnnotatiomType extends ClefType {
x_shift: number;
point: number;
}
export interface ClefMetrics {
width: number;
annotations: {
[key: string]: {
[type: string]: { line?: number; shiftX?: number } | number;
};
};
}
// eslint-disable-next-line
function L(...args: any[]) {
if (Clef.DEBUG) log('Vex.Flow.Clef', args);
}
/**
* Clef implements various types of clefs that can be rendered on a stave.
*
* See `tests/clef_tests.ts` for usage examples.
*/
export class Clef extends StaveModifier {
/** To enable logging for this class, set `Vex.Flow.Clef.DEBUG` to `true`. */
static DEBUG: boolean = false;
static get CATEGORY(): string {
return Category.Clef;
}
annotation?: ClefAnnotatiomType;
/**
* The attribute `clef` must be a key from
* `Clef.types`
*/
clef: ClefType = Clef.types['treble'];
protected attachment?: Glyph;
protected size?: string;
protected type?: string;
/**
* Every clef name is associated with a glyph code from the font file
* and a default stave line number.
*/
static get types(): Record<string, ClefType> {
return {
treble: {
code: 'gClef',
line: 3,
},
bass: {
code: 'fClef',
line: 1,
},
alto: {
code: 'cClef',
line: 2,
},
tenor: {
code: 'cClef',
line: 1,
},
percussion: {
code: 'unpitchedPercussionClef1',
line: 2,
},
soprano: {
code: 'cClef',
line: 4,
},
'mezzo-soprano': {
code: 'cClef',
line: 3,
},
'baritone-c': {
code: 'cClef',
line: 0,
},
'baritone-f': {
code: 'fClef',
line: 2,
},
subbass: {
code: 'fClef',
line: 0,
},
french: {
code: 'gClef',
line: 4,
},
tab: {
code: '6stringTabClef',
line: 2.5,
},
};
}
static get annotationSmufl(): Record<string, string> {
return {
'8va': 'timeSig8',
'8vb': 'timeSig8',
};
}
/** Create a new clef. */
constructor(type: string, size?: string, annotation?: string) {
super();
this.setPosition(StaveModifierPosition.BEGIN);
this.setType(type, size, annotation);
this.setWidth(Glyph.getWidth(this.clef.code, Clef.getPoint(this.size), `clef_${this.size}`));
L('Creating clef:', type);
}
/** Set clef type, size and annotation. */
setType(type: string, size?: string, annotation?: string): this {
this.type = type;
this.clef = Clef.types[type];
if (size === undefined) {
this.size = 'default';
} else {
this.size = size;
}
const musicFont = Tables.currentMusicFont();
// If an annotation, such as 8va, is specified, add it to the Clef object.
if (annotation !== undefined) {
const code = Clef.annotationSmufl[annotation];
const point = (Clef.getPoint(this.size) / 5) * 3;
const line = musicFont.lookupMetric(`clef_${this.size}.annotations.${annotation}.${this.type}.line`);
const x_shift = musicFont.lookupMetric(`clef_${this.size}.annotations.${annotation}.${this.type}.shiftX`);
this.annotation = { code, point, line, x_shift };
this.attachment = new Glyph(this.annotation.code, this.annotation.point);
this.attachment.metrics.x_max = 0;
this.attachment.setXShift(this.annotation.x_shift);
} else {
this.annotation = undefined;
}
return this;
}
/** Get clef width. */
getWidth(): number {
if (this.type === 'tab') {
defined(this.stave, 'ClefError', "Can't get width without stave.");
}
return this.width;
}
/** Get point for clefs. */
static getPoint(size?: string): number {
// for sizes other than 'default', clef is 2/3 of the default value
return size == 'default' ? Tables.NOTATION_FONT_SCALE : (Tables.NOTATION_FONT_SCALE / 3) * 2;
}
/** Set associated stave. */
setStave(stave: Stave): this {
this.stave = stave;
return this;
}
/** Render clef. */
draw(): void {
const stave = this.checkStave();
const ctx = stave.checkContext();
this.setRendered();
this.applyStyle(ctx);
ctx.openGroup('clef', this.getAttribute('id'));
Glyph.renderGlyph(ctx, this.x, stave.getYForLine(this.clef.line), Clef.getPoint(this.size), this.clef.code, {
category: `clef_${this.size}`,
});
if (this.annotation !== undefined && this.attachment !== undefined) {
this.placeGlyphOnLine(this.attachment, stave, this.annotation.line);
this.attachment.setStave(stave);
this.attachment.setContext(ctx);
this.attachment.renderToStave(this.x);
}
ctx.closeGroup();
this.restoreStyle(ctx);
}
}