-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathCssFontSvgCanvas.ts
67 lines (62 loc) · 1.99 KB
/
CssFontSvgCanvas.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
import { TextAlign } from '@src/platform/ICanvas';
import { SvgCanvas } from '@src/platform/svg/SvgCanvas';
import { MusicFontSymbol } from '@src/model/MusicFontSymbol';
/**
* This SVG canvas renders the music symbols by adding a CSS class 'at' to all elements.
*/
export class CssFontSvgCanvas extends SvgCanvas {
public constructor() {
super();
}
public fillMusicFontSymbol(
x: number,
y: number,
relativeScale: number,
symbol: MusicFontSymbol,
centerAtPosition?: boolean
): void {
if (symbol === MusicFontSymbol.None) {
return;
}
this.fillMusicFontSymbolText(x, y, relativeScale, `&#${symbol};`, centerAtPosition);
}
public fillMusicFontSymbols(
x: number,
y: number,
relativeScale: number,
symbols: MusicFontSymbol[],
centerAtPosition?: boolean
): void {
let s: string = '';
for (let symbol of symbols) {
if (symbol !== MusicFontSymbol.None) {
s += `&#${symbol};`;
}
}
this.fillMusicFontSymbolText(x, y, relativeScale, s, centerAtPosition);
}
private fillMusicFontSymbolText(
x: number,
y: number,
relativeScale: number,
symbols: string,
centerAtPosition?: boolean
): void {
x *= this.scale;
y *= this.scale;
this.buffer += `<g transform="translate(${x} ${y})" class="at" ><text`;
const scale = this.scale * relativeScale;
if (scale !== 1) {
this.buffer += ` style="font-size: ${scale * 100}%; stroke:none"`;
} else {
this.buffer += ' style="stroke:none"';
}
if (this.color.rgba !== '#000000') {
this.buffer += ` fill="${this.color.rgba}"`;
}
if (centerAtPosition) {
this.buffer += ' text-anchor="' + this.getSvgTextAlignment(TextAlign.Center) + '"';
}
this.buffer += `>${symbols}</text></g>`;
}
}