123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- import {Label} from "../Label";
- import {TextAlignment} from "../../Common/Enums/TextAlignment";
- import {Clickable} from "./Clickable";
- import {BoundingBox} from "./BoundingBox";
- import {EngravingRules} from "./EngravingRules";
- import {MusicSheetCalculator} from "./MusicSheetCalculator";
- /**
- * The graphical counterpart of a Label
- */
- export class GraphicalLabel extends Clickable {
- private label: Label;
- constructor(label: Label, textHeight: number, alignment: TextAlignment, parent: BoundingBox = undefined) {
- super();
- this.label = label;
- this.boundingBox = new BoundingBox(this, parent);
- this.label.fontHeight = textHeight;
- this.label.textAlignment = alignment;
- }
- public get Label(): Label {
- return this.label;
- }
- public toString(): string {
- return this.label.text;
- }
- /**
- * Calculate GraphicalLabel's Borders according to its Alignment
- */
- public setLabelPositionAndShapeBorders(): void {
- if (this.Label.text.trim() === "") {
- return;
- }
- const labelMarginBorderFactor: number = EngravingRules.Rules.LabelMarginBorderFactor;
- const widthToHeightRatio: number =
- MusicSheetCalculator.TextMeasurer.computeTextWidthToHeightRatio(this.Label.text, this.Label.font, this.Label.fontStyle);
- const height: number = this.Label.fontHeight;
- const width: number = height * widthToHeightRatio;
- const psi: BoundingBox = this.PositionAndShape;
- switch (this.Label.textAlignment) {
- case TextAlignment.CenterBottom:
- psi.BorderTop = -height;
- psi.BorderLeft = -width / 2;
- psi.BorderBottom = 0;
- psi.BorderRight = width / 2;
- break;
- case TextAlignment.CenterCenter:
- psi.BorderTop = -height / 2;
- psi.BorderLeft = -width / 2;
- psi.BorderBottom = height / 2;
- psi.BorderRight = width / 2;
- break;
- case TextAlignment.CenterTop:
- psi.BorderTop = 0;
- psi.BorderLeft = -width / 2;
- psi.BorderBottom = height;
- psi.BorderRight = width / 2;
- break;
- case TextAlignment.LeftBottom:
- psi.BorderTop = -height;
- psi.BorderLeft = 0;
- psi.BorderBottom = 0;
- psi.BorderRight = width;
- break;
- case TextAlignment.LeftCenter:
- psi.BorderTop = -height / 2;
- psi.BorderLeft = 0;
- psi.BorderBottom = height / 2;
- psi.BorderRight = width;
- break;
- case TextAlignment.LeftTop:
- psi.BorderTop = 0;
- psi.BorderLeft = 0;
- psi.BorderBottom = height;
- psi.BorderRight = width;
- break;
- case TextAlignment.RightBottom:
- psi.BorderTop = -height;
- psi.BorderLeft = -width;
- psi.BorderBottom = 0;
- psi.BorderRight = 0;
- break;
- case TextAlignment.RightCenter:
- psi.BorderTop = -height / 2;
- psi.BorderLeft = -width;
- psi.BorderBottom = height / 2;
- psi.BorderRight = 0;
- break;
- case TextAlignment.RightTop:
- psi.BorderTop = 0;
- psi.BorderLeft = -width;
- psi.BorderBottom = height;
- psi.BorderRight = 0;
- break;
- default:
- }
- psi.BorderMarginTop = psi.BorderTop - height * labelMarginBorderFactor;
- psi.BorderMarginLeft = psi.BorderLeft - height * labelMarginBorderFactor;
- psi.BorderMarginBottom = psi.BorderBottom + height * labelMarginBorderFactor;
- psi.BorderMarginRight = psi.BorderRight + height * labelMarginBorderFactor;
- }
- }
|