/
VexFlowTextMeasurer.ts
41 lines (37 loc) · 1.81 KB
/
VexFlowTextMeasurer.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
import {ITextMeasurer} from "../../Interfaces/ITextMeasurer";
import {Fonts} from "../../../Common/Enums/Fonts";
import {FontStyles} from "../../../Common/Enums/FontStyles";
import {VexFlowConverter} from "./VexFlowConverter";
import { EngravingRules } from "../EngravingRules";
/**
* Created by Matthias on 21.06.2016.
*/
export class VexFlowTextMeasurer implements ITextMeasurer {
constructor(rules: EngravingRules) {
const canvas: HTMLCanvasElement = document.createElement("canvas");
this.context = canvas.getContext("2d");
this.rules = rules;
}
// The context of a canvas used internally to compute font sizes
private context: CanvasRenderingContext2D;
public fontSize: number = 20;
public fontSizeStandard: number = this.fontSize;
private rules: EngravingRules;
public computeTextWidthToHeightRatio(text: string, font: Fonts, style: FontStyles,
fontFamily: string = undefined,
fontSize: number = this.fontSize): number {
this.context.font = VexFlowConverter.font(fontSize, style, font, this.rules, fontFamily);
return this.context.measureText(text).width / fontSize;
}
// public computeTextWidth(text: string, font: Fonts, style: FontStyles,
// fontFamily: string = undefined,
// fontSize: number = this.fontSize): number {
// this.context.font = VexFlowConverter.font(fontSize, style, font, this.rules, fontFamily);
// return this.context.measureText(text).width / 10.0;
// // TODO this shifts the title text of sheets to the right for some reason, maybe because of bigger fontSize?
// }
public setFontSize(fontSize: number = this.fontSizeStandard): number {
this.fontSize = fontSize;
return fontSize;
}
}