/
GraphicalLabel.ts
164 lines (156 loc) · 6.7 KB
/
GraphicalLabel.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
import { TextAlignmentEnum } from "../../Common/Enums/TextAlignment";
import { Label } from "../Label";
import { BoundingBox } from "./BoundingBox";
import { Clickable } from "./Clickable";
import { EngravingRules } from "./EngravingRules";
import { MusicSheetCalculator } from "./MusicSheetCalculator";
/**
* The graphical counterpart of a Label
*/
export class GraphicalLabel extends Clickable {
private label: Label;
private rules: EngravingRules;
public TextLines: {text: string, xOffset: number, width: number}[];
/** A reference to the Node in the SVG, if SVGBackend, otherwise undefined.
* Allows manipulation without re-rendering, e.g. for dynamics, lyrics, etc.
* For the Canvas backend, this is unfortunately not possible.
*/
public SVGNode: Node;
/** Read-only informational variable only set once by lyrics centering algorithm. */
public CenteringXShift: number = 0;
public ColorXML: string;
/**
* Creates a new GraphicalLabel from a Label
* @param label label object containing text
* @param textHeight Height of text
* @param alignment Alignement like left, right, top, ...
* @param parent Parent Bounding Box where the label is attached to
*/
constructor(label: Label, textHeight: number, alignment: TextAlignmentEnum, rules: EngravingRules,
parent: BoundingBox = undefined, ) {
super();
this.label = label;
this.boundingBox = new BoundingBox(this, parent);
this.label.fontHeight = textHeight;
this.label.textAlignment = alignment;
this.rules = rules;
}
public get Label(): Label {
return this.label;
}
public toString(): string {
return `${this.label.text} (${this.boundingBox.RelativePosition.x},${this.boundingBox.RelativePosition.y})`;
}
/**
* Calculate GraphicalLabel's Borders according to its Alignment
* Create also the text-lines and their offsets here
*/
public setLabelPositionAndShapeBorders(): void {
if (this.Label.text.trim() === "") {
return;
}
this.TextLines = [];
const labelMarginBorderFactor: number = this.rules?.LabelMarginBorderFactor ?? 0.1;
const lines: string[] = this.Label.text.split(/[\n\r]+/g);
const numOfLines: number = lines.length;
let maxWidth: number = 0;
for (let i: number = 0; i < numOfLines; i++) {
const line: string = lines[i].trim();
const widthToHeightRatio: number =
MusicSheetCalculator.TextMeasurer.computeTextWidthToHeightRatio(
line, this.Label.font, this.Label.fontStyle, this.label.fontFamily);
const currWidth: number = this.Label.fontHeight * widthToHeightRatio;
// const currWidth: number = MusicSheetCalculator.TextMeasurer.computeTextWidth(
// line, this.Label.font, this.Label.fontStyle, this.label.fontFamily);
maxWidth = Math.max(maxWidth, currWidth);
// here push only text and width of the text:
this.TextLines.push({text: line, xOffset: 0, width: currWidth});
}
// maxWidth is calculated ->
// now also set the x-offsets:
for (const line of this.TextLines) {
let xOffset: number = 0;
switch (this.Label.textAlignment) {
case TextAlignmentEnum.RightBottom:
case TextAlignmentEnum.RightCenter:
case TextAlignmentEnum.RightTop:
xOffset = maxWidth - line.width;
break;
case TextAlignmentEnum.CenterBottom:
case TextAlignmentEnum.CenterCenter:
case TextAlignmentEnum.CenterTop:
xOffset = (maxWidth - line.width) / 2;
break;
default:
break;
}
line.xOffset = xOffset;
}
let height: number = this.Label.fontHeight * numOfLines;
if (this.rules.SpacingBetweenTextLines > 0 && this.TextLines.length > 1) {
height += (this.rules.SpacingBetweenTextLines * numOfLines) / 10;
}
const bbox: BoundingBox = this.PositionAndShape;
switch (this.Label.textAlignment) {
case TextAlignmentEnum.CenterBottom:
bbox.BorderTop = -height;
bbox.BorderLeft = -maxWidth / 2;
bbox.BorderBottom = 0;
bbox.BorderRight = maxWidth / 2;
break;
case TextAlignmentEnum.CenterCenter:
bbox.BorderTop = -height / 2;
bbox.BorderLeft = -maxWidth / 2;
bbox.BorderBottom = height / 2;
bbox.BorderRight = maxWidth / 2;
break;
case TextAlignmentEnum.CenterTop:
bbox.BorderTop = 0;
bbox.BorderLeft = -maxWidth / 2;
bbox.BorderBottom = height;
bbox.BorderRight = maxWidth / 2;
break;
case TextAlignmentEnum.LeftBottom:
bbox.BorderTop = -height;
bbox.BorderLeft = 0;
bbox.BorderBottom = 0;
bbox.BorderRight = maxWidth;
break;
case TextAlignmentEnum.LeftCenter:
bbox.BorderTop = -height / 2;
bbox.BorderLeft = 0;
bbox.BorderBottom = height / 2;
bbox.BorderRight = maxWidth;
break;
case TextAlignmentEnum.LeftTop:
bbox.BorderTop = 0;
bbox.BorderLeft = 0;
bbox.BorderBottom = height;
bbox.BorderRight = maxWidth;
break;
case TextAlignmentEnum.RightBottom:
bbox.BorderTop = -height;
bbox.BorderLeft = -maxWidth;
bbox.BorderBottom = 0;
bbox.BorderRight = 0;
break;
case TextAlignmentEnum.RightCenter:
bbox.BorderTop = -height / 2;
bbox.BorderLeft = -maxWidth;
bbox.BorderBottom = height / 2;
bbox.BorderRight = 0;
break;
case TextAlignmentEnum.RightTop:
bbox.BorderTop = 0;
bbox.BorderLeft = -maxWidth;
bbox.BorderBottom = height;
bbox.BorderRight = 0;
break;
default:
}
bbox.BorderMarginTop = bbox.BorderTop - height * labelMarginBorderFactor;
bbox.BorderMarginLeft = bbox.BorderLeft - height * labelMarginBorderFactor;
bbox.BorderMarginBottom = bbox.BorderBottom + height * labelMarginBorderFactor;
bbox.BorderMarginRight = bbox.BorderRight + height * labelMarginBorderFactor;
}
}