forked from 0xfe/vexflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtickcontext.ts
280 lines (232 loc) · 8.06 KB
/
tickcontext.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// [VexFlow](https://vexflow.com) - Copyright (c) Mohit Muthanna 2010.
//
// ## Description
// A formatter for abstract tickable objects, such as notes, chords,
// tabs, etc.
import { Fraction } from './fraction';
import { NoteMetrics } from './note';
import { Tickable } from './tickable';
import { RuntimeError } from './util';
export interface TickContextMetrics extends NoteMetrics {
totalLeftPx: number;
totalRightPx: number;
}
export interface TickContextOptions {
tickID: number;
}
/**
* TickContext formats abstract tickable objects, such as notes, chords, tabs, etc.
*/
export class TickContext {
protected readonly tickID: number;
protected readonly tickables: Tickable[];
protected readonly tickablesByVoice: Record<string, Tickable>;
protected currentTick: Fraction;
protected maxTicks: Fraction;
protected padding: number;
protected xBase: number;
protected x: number;
protected xOffset: number;
protected notePx: number;
protected glyphPx: number;
protected leftDisplacedHeadPx: number;
protected rightDisplacedHeadPx: number;
protected modLeftPx: number;
protected modRightPx: number;
protected totalLeftPx: number;
protected totalRightPx: number;
protected maxTickable?: Tickable;
protected minTicks?: Fraction;
protected minTickable?: Tickable;
tContexts: TickContext[];
protected preFormatted: boolean = false;
protected postFormatted: boolean = false;
protected width: number;
protected formatterMetrics: { freedom: { left: number; right: number } };
static getNextContext(tContext: TickContext): TickContext | undefined {
const contexts = tContext.tContexts;
const index = contexts.indexOf(tContext);
if (index + 1 < contexts.length) return contexts[index + 1];
}
constructor(options?: TickContextOptions) {
this.tickID = options && options.tickID ? options.tickID : 0;
this.currentTick = new Fraction(0, 1);
this.maxTicks = new Fraction(0, 1);
this.maxTickable = undefined; // Biggest tickable
this.minTicks = undefined; // this can remian null if all tickables have ignore_ticks
this.minTickable = undefined;
this.padding = 1; // padding on each side (width += padding * 2)
this.x = 0;
this.xBase = 0; // base x position without xOffset
this.xOffset = 0; // xBase and xOffset are an alternative way to describe x (x = xB + xO)
this.tickables = []; // Notes, tabs, chords, lyrics.
this.tickablesByVoice = {}; // Tickables indexed by voice number
// Formatting metrics
this.notePx = 0; // width of widest note in this context
this.glyphPx = 0; // width of glyph (note head)
this.leftDisplacedHeadPx = 0; // Extra left pixels for displaced notes
this.rightDisplacedHeadPx = 0; // Extra right pixels for displaced notes
this.modLeftPx = 0; // Left modifier pixels
this.modRightPx = 0; // Right modifier pixels
this.totalLeftPx = 0; // Total left pixels
this.totalRightPx = 0; // Total right pixels
this.tContexts = []; // Parent array of tick contexts
this.width = 0;
this.formatterMetrics = {
// The freedom of a tickcontext is the distance it can move without colliding
// with neighboring elements. A formatter can set these values during its
// formatting pass, which a different formatter can then use to fine tune.
freedom: { left: 0, right: 0 },
};
}
getTickID(): number {
return this.tickID;
}
getX(): number {
return this.x;
}
setX(x: number): this {
this.x = x;
this.xBase = x;
this.xOffset = 0;
return this;
}
getXBase(): number {
return this.xBase;
} // use of xBase and xOffset is optional, avoids offset creep
setXBase(xBase: number): void {
this.xBase = xBase;
this.x = xBase + this.xOffset;
}
getXOffset(): number {
return this.xOffset;
}
setXOffset(xOffset: number): void {
this.xOffset = xOffset;
this.x = this.xBase + xOffset;
}
getWidth(): number {
return this.width + this.padding * 2;
}
setPadding(padding: number): this {
this.padding = padding;
return this;
}
getMaxTicks(): Fraction {
return this.maxTicks;
}
getMinTicks(): Fraction | undefined {
return this.minTicks;
}
getMaxTickable(): Tickable | undefined {
return this.maxTickable;
}
getMinTickable(): Tickable | undefined {
return this.minTickable;
}
getTickables(): Tickable[] {
return this.tickables;
}
/**
* Introduced on 2020-04-17 as getTickablesForVoice(voiceIndex).
* https://github.com/0xfe/vexflow/blame/dc97b0cc5bb93171c0038638c34362dc958222ca/src/tickcontext.js#L63
* Renamed on 2021-08-05 to getTickableForVoice(voiceIndex). Method renamed to singular, since it returns one Tickable.
*/
getTickableForVoice(voiceIndex: number): Tickable {
return this.tickablesByVoice[voiceIndex];
}
getTickablesByVoice(): Record<string, Tickable> {
return this.tickablesByVoice;
}
getCenterAlignedTickables(): Tickable[] {
return this.tickables.filter((tickable) => tickable.isCenterAligned());
}
/** Gets widths context, note and left/right modifiers for formatting. */
getMetrics(): TickContextMetrics {
const {
width,
glyphPx,
notePx,
leftDisplacedHeadPx,
rightDisplacedHeadPx,
modLeftPx,
modRightPx,
totalLeftPx,
totalRightPx,
} = this;
return {
width, // Width of largest tickable in context
glyphPx, // Width of largest glyph (note head)
notePx, // Width of notehead + stem
leftDisplacedHeadPx, // Left modifiers
rightDisplacedHeadPx, // Right modifiers
modLeftPx,
modRightPx,
totalLeftPx,
totalRightPx,
};
}
getCurrentTick(): Fraction {
return this.currentTick;
}
setCurrentTick(tick: Fraction): void {
this.currentTick = tick;
this.preFormatted = false;
}
addTickable(tickable: Tickable, voiceIndex?: number): this {
if (!tickable) {
throw new RuntimeError('BadArgument', 'Invalid tickable added.');
}
if (!tickable.shouldIgnoreTicks()) {
const ticks = tickable.getTicks();
if (ticks.greaterThan(this.maxTicks)) {
this.maxTicks = ticks.clone();
this.maxTickable = tickable;
}
if (this.minTicks == null) {
this.minTicks = ticks.clone();
this.minTickable = tickable;
} else if (ticks.lessThan(this.minTicks)) {
this.minTicks = ticks.clone();
this.minTickable = tickable;
}
}
tickable.setTickContext(this);
this.tickables.push(tickable);
this.tickablesByVoice[voiceIndex || 0] = tickable;
this.preFormatted = false;
return this;
}
preFormat(): this {
if (this.preFormatted) return this;
for (let i = 0; i < this.tickables.length; ++i) {
const tickable = this.tickables[i];
tickable.preFormat();
const metrics = tickable.getMetrics();
// Maintain max displaced head pixels from all tickables in the context
this.leftDisplacedHeadPx = Math.max(this.leftDisplacedHeadPx, metrics.leftDisplacedHeadPx);
this.rightDisplacedHeadPx = Math.max(this.rightDisplacedHeadPx, metrics.rightDisplacedHeadPx);
// Maintain the widest note for all tickables in the context
this.notePx = Math.max(this.notePx, metrics.notePx);
// Maintain the widest note head
this.glyphPx = Math.max(this.glyphPx, metrics.glyphWidth || 0);
// Total modifier shift
this.modLeftPx = Math.max(this.modLeftPx, metrics.modLeftPx);
this.modRightPx = Math.max(this.modRightPx, metrics.modRightPx);
// Total shift
this.totalLeftPx = Math.max(this.totalLeftPx, metrics.modLeftPx + metrics.leftDisplacedHeadPx);
this.totalRightPx = Math.max(this.totalRightPx, metrics.modRightPx + metrics.rightDisplacedHeadPx);
// Recalculate the tick context total width
this.width = this.notePx + this.totalLeftPx + this.totalRightPx;
}
return this;
}
postFormat(): this {
if (this.postFormatted) return this;
this.postFormatted = true;
return this;
}
getFormatterMetrics(): { freedom: { left: number; right: number } } {
return this.formatterMetrics;
}
}