-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrender.ts
54 lines (42 loc) · 1.69 KB
/
render.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
import { Config, DEFAULT_CONFIG } from './config';
import { Logger, NoopLogger } from './debug';
import { Score } from './elements';
import { DefaultFormatter, Formatter, PanoramicFormatter } from './formatting';
import { MusicXMLParser, MXLParser } from './parsing';
import { Renderer } from './rendering';
export type RenderMusicXMLOptions = {
config?: Partial<Config>;
logger?: Logger;
};
export function renderMusicXML(musicXML: string, div: HTMLDivElement, opts?: RenderMusicXMLOptions): Score {
const config = { ...DEFAULT_CONFIG, ...opts?.config };
const logger = opts?.logger ?? new NoopLogger();
const parser = new MusicXMLParser({ config });
const renderer = new Renderer({ config });
let formatter: Formatter;
const width = config.WIDTH;
const height = config.HEIGHT;
if (width && height) {
formatter = new DefaultFormatter({ config, logger });
} else if (width) {
formatter = new DefaultFormatter({ config, logger });
} else if (height) {
formatter = new PanoramicFormatter({ config, logger });
} else {
formatter = new PanoramicFormatter({ config, logger });
}
const document = parser.parse(musicXML);
const formattedDocument = formatter.format(document);
return renderer.render(div, formattedDocument);
}
export type RenderMXLOptions = {
config?: Partial<Config>;
logger?: Logger;
};
export async function renderMXL(mxl: Blob, div: HTMLDivElement, opts?: RenderMXLOptions): Promise<Score> {
const config = { ...DEFAULT_CONFIG, ...opts?.config };
const logger = opts?.logger ?? new NoopLogger();
const parser = new MXLParser({ config });
const musicXML = await parser.raw(mxl);
return renderMusicXML(musicXML, div, { config, logger });
}