UNPKG

4.26 kBJavaScriptView Raw
1import { __decorate, __metadata } from "tslib";
2import { vec3 } from 'gl-matrix';
3import { singleton, inject } from 'mana-syringe';
4import { GeometryAABBUpdater } from './interfaces';
5import { TextService } from '../TextService';
6import { Shape } from '../../types';
7import { CSSUnitValue } from '../../css';
8
9var TextUpdater =
10/** @class */
11function () {
12 function TextUpdater() {}
13
14 TextUpdater.prototype.isReadyToMeasure = function (parsedStyle, object) {
15 var text = parsedStyle.text,
16 textAlign = parsedStyle.textAlign,
17 textBaseline = parsedStyle.textBaseline,
18 fontSize = parsedStyle.fontSize,
19 fontStyle = parsedStyle.fontStyle,
20 fontWeight = parsedStyle.fontWeight,
21 fontVariant = parsedStyle.fontVariant;
22 return text && fontSize && fontStyle && fontWeight && fontVariant && textAlign && textBaseline;
23 };
24
25 TextUpdater.prototype.update = function (parsedStyle, object) {
26 var _a, _b;
27
28 var text = parsedStyle.text,
29 textAlign = parsedStyle.textAlign,
30 lineWidth = parsedStyle.lineWidth,
31 textBaseline = parsedStyle.textBaseline,
32 dx = parsedStyle.dx,
33 dy = parsedStyle.dy;
34 var offscreenCanvas = (((_b = (_a = object === null || object === void 0 ? void 0 : object.ownerDocument) === null || _a === void 0 ? void 0 : _a.defaultView) === null || _b === void 0 ? void 0 : _b.getConfig()) || {}).offscreenCanvas;
35
36 if (!this.isReadyToMeasure(parsedStyle, object)) {
37 // if (!text || !textAlign || !textBaseline ||) {
38 parsedStyle.metrics = {
39 font: '',
40 width: 0,
41 height: 0,
42 lines: [],
43 lineWidths: [],
44 lineHeight: 0,
45 maxLineWidth: 0,
46 fontProperties: {
47 ascent: 0,
48 descent: 0,
49 fontSize: 0
50 },
51 lineMetrics: []
52 };
53 return {
54 width: 0,
55 height: 0,
56 x: 0,
57 y: 0,
58 offsetX: 0,
59 offsetY: 0
60 };
61 }
62
63 var metrics = this.textService.measureText(text, parsedStyle, offscreenCanvas);
64 parsedStyle.metrics = metrics;
65 var width = metrics.width,
66 height = metrics.height,
67 lineHeight = metrics.lineHeight,
68 fontProperties = metrics.fontProperties; // anchor is left-top by default
69
70 var halfExtents = vec3.fromValues(width / 2, height / 2, 0); // default 'left'
71
72 var anchor = [0, 1];
73 var lineXOffset = 0;
74
75 if (textAlign.value === 'center') {
76 lineXOffset = lineWidth.value / 2;
77 anchor = [0.5, 1];
78 } else if (textAlign.value === 'right' || textAlign.value === 'end') {
79 lineXOffset = lineWidth.value;
80 anchor = [1, 1];
81 }
82
83 var lineYOffset = 0;
84
85 if (textBaseline.value === 'middle') {
86 // eslint-disable-next-line prefer-destructuring
87 lineYOffset = halfExtents[1];
88 } else if (textBaseline.value === 'top' || textBaseline.value === 'hanging') {
89 lineYOffset = halfExtents[1] * 2;
90 } else if (textBaseline.value === 'alphabetic') {
91 lineYOffset = lineHeight - fontProperties.ascent;
92 } else if (textBaseline.value === 'bottom' || textBaseline.value === 'ideographic') {
93 lineYOffset = 0;
94 } // TODO: ideographic & bottom
95
96
97 if (dx) {
98 lineXOffset += dx.value;
99 }
100
101 if (dy) {
102 lineYOffset += dy.value;
103 } // update anchor
104
105
106 parsedStyle.anchor = [new CSSUnitValue(anchor[0]), new CSSUnitValue(anchor[1]), new CSSUnitValue(0)]; // console.log(parsedStyle.anchor);
107 // if (!parsedStyle.transformOrigin) {
108 // parsedStyle.transformOrigin = [
109 // // new CSSUnitValue(anchor[0] * 100, '%'),
110 // // new CSSUnitValue(anchor[1] * 100, '%'),
111 // new CSSUnitValue(0, '%'),
112 // new CSSUnitValue(0, '%'),
113 // new CSSUnitValue(0, '%'),
114 // ];
115 // }
116
117 return {
118 width: halfExtents[0] * 2,
119 height: halfExtents[1] * 2,
120 offsetX: lineXOffset,
121 offsetY: lineYOffset
122 };
123 };
124
125 __decorate([inject(TextService), __metadata("design:type", TextService)], TextUpdater.prototype, "textService", void 0);
126
127 TextUpdater = __decorate([singleton({
128 token: {
129 token: GeometryAABBUpdater,
130 named: Shape.TEXT
131 }
132 })], TextUpdater);
133 return TextUpdater;
134}();
135
136export { TextUpdater };
\No newline at end of file