forked from 0xfe/vexflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglyphnote.ts
90 lines (75 loc) · 2.25 KB
/
glyphnote.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
// [VexFlow](https://vexflow.com) - Copyright (c) Mohit Muthanna 2010.
//
// Any glyph that is set to appear on a Stave and take up musical time and graphical space.
import { BoundingBox } from './boundingbox';
import { Glyph } from './glyph';
import { Note, NoteStruct } from './note';
import { Category } from './typeguard';
export interface GlyphNoteOptions {
ignoreTicks?: boolean;
line?: number;
}
export class GlyphNote extends Note {
static get CATEGORY(): string {
return Category.GlyphNote;
}
protected options: Required<GlyphNoteOptions>;
protected glyph!: Glyph;
constructor(glyph: Glyph, noteStruct: NoteStruct, options?: GlyphNoteOptions) {
super(noteStruct);
this.options = {
ignoreTicks: false,
line: 2,
...options,
};
// Note properties
this.ignore_ticks = this.options.ignoreTicks;
this.setGlyph(glyph);
}
setGlyph(glyph: Glyph): this {
this.glyph = glyph;
this.setWidth(this.glyph.getMetrics().width);
return this;
}
getBoundingBox(): BoundingBox | undefined {
return this.glyph.getBoundingBox();
}
preFormat(): this {
if (!this.preFormatted && this.modifierContext) {
this.modifierContext.preFormat();
}
this.preFormatted = true;
return this;
}
drawModifiers(): void {
const ctx = this.checkContext();
for (let i = 0; i < this.modifiers.length; i++) {
const modifier = this.modifiers[i];
modifier.setContext(ctx);
modifier.drawWithStyle();
}
}
/** Get the glyph width. */
getGlyphWidth(): number {
return this.glyph.getMetrics().width;
}
draw(): void {
const stave = this.checkStave();
const ctx = stave.checkContext();
this.setRendered();
this.applyStyle(ctx);
ctx.openGroup('glyphNote', this.getAttribute('id'));
// Context is set when setStave is called on Note
const glyph = this.glyph;
if (!glyph.getContext()) {
glyph.setContext(ctx);
}
glyph.setStave(stave);
glyph.setYShift(stave.getYForLine(this.options.line) - stave.getYForGlyphs());
const x = this.isCenterAligned() ? this.getAbsoluteX() - this.getWidth() / 2 : this.getAbsoluteX();
glyph.renderToStave(x);
this.drawModifiers();
ctx.closeGroup();
this.restoreStyle(ctx);
}
}