@lightningjs/renderer 2.22.1 → 2.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/core/CoreNode.d.ts +1 -1
- package/dist/src/core/lib/WebGlContextWrapper.d.ts +5 -5
- package/dist/src/core/platform.js +5 -2
- package/dist/src/core/platform.js.map +1 -1
- package/dist/src/core/renderers/webgl/WebGlCoreShader.destroy.d.ts +1 -0
- package/dist/src/core/renderers/webgl/WebGlCoreShader.destroy.js +2 -0
- package/dist/src/core/renderers/webgl/WebGlCoreShader.destroy.js.map +1 -0
- package/dist/src/core/shaders/webgl/Border.js +7 -13
- package/dist/src/core/shaders/webgl/Border.js.map +1 -1
- package/dist/src/core/shaders/webgl/RoundedWithBorder.js +6 -17
- package/dist/src/core/shaders/webgl/RoundedWithBorder.js.map +1 -1
- package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +4 -8
- package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js.map +1 -1
- package/dist/src/core/text-rendering/CanvasTextRenderer.d.ts +5 -8
- package/dist/src/core/text-rendering/CanvasTextRenderer.js +13 -30
- package/dist/src/core/text-rendering/CanvasTextRenderer.js.map +1 -1
- package/dist/src/core/text-rendering/SdfFontHandler.d.ts +125 -28
- package/dist/src/core/text-rendering/SdfFontHandler.js +337 -4
- package/dist/src/core/text-rendering/SdfFontHandler.js.map +1 -1
- package/dist/src/core/text-rendering/SdfTextRenderer.d.ts +5 -6
- package/dist/src/core/text-rendering/SdfTextRenderer.js +15 -19
- package/dist/src/core/text-rendering/SdfTextRenderer.js.map +1 -1
- package/dist/src/core/text-rendering/TextLayoutEngine.d.ts +11 -12
- package/dist/src/core/text-rendering/TextLayoutEngine.js +29 -29
- package/dist/src/core/text-rendering/TextLayoutEngine.js.map +1 -1
- package/dist/src/core/text-rendering/TextRenderer.d.ts +16 -6
- package/dist/tsconfig.dist.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/core/platform.ts +6 -2
- package/dist/src/core/renderers/webgl/WebGlRenderOp.d.ts +0 -45
- package/dist/src/core/renderers/webgl/WebGlRenderOp.js +0 -127
- package/dist/src/core/renderers/webgl/WebGlRenderOp.js.map +0 -1
- package/dist/src/core/text-rendering/CanvasFont.d.ts +0 -14
- package/dist/src/core/text-rendering/CanvasFont.js +0 -119
- package/dist/src/core/text-rendering/CanvasFont.js.map +0 -1
- package/dist/src/core/text-rendering/CoreFont.d.ts +0 -33
- package/dist/src/core/text-rendering/CoreFont.js +0 -48
- package/dist/src/core/text-rendering/CoreFont.js.map +0 -1
- package/dist/src/core/text-rendering/FontManager.d.ts +0 -11
- package/dist/src/core/text-rendering/FontManager.js +0 -41
- package/dist/src/core/text-rendering/FontManager.js.map +0 -1
- package/dist/src/core/text-rendering/SdfFont.d.ts +0 -29
- package/dist/src/core/text-rendering/SdfFont.js +0 -142
- package/dist/src/core/text-rendering/SdfFont.js.map +0 -1
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { CoreRenderOp } from '../CoreRenderOp.js';
|
|
2
|
-
import type { WebGlCtxTexture } from './WebGlCtxTexture.js';
|
|
3
|
-
import type { WebGlRenderer } from './WebGlRenderer.js';
|
|
4
|
-
import type { BufferCollection } from './internal/BufferCollection.js';
|
|
5
|
-
import type { WebGlShaderNode } from './WebGlShaderNode.js';
|
|
6
|
-
import type { QuadOptions } from '../CoreRenderer.js';
|
|
7
|
-
import type { CoreTextNode } from '../../CoreTextNode.js';
|
|
8
|
-
import type { RectWithValid } from '../../lib/utils.js';
|
|
9
|
-
import type { Dimensions } from '../../../common/CommonTypes.js';
|
|
10
|
-
type ReqQuad = 'alpha' | 'shader' | 'parentHasRenderTexture' | 'rtt' | 'clippingRect' | 'height' | 'width' | 'time';
|
|
11
|
-
type RenderOpQuadOptions = Pick<QuadOptions, ReqQuad> & Partial<Omit<QuadOptions, ReqQuad>> & {
|
|
12
|
-
sdfShaderProps?: Record<string, unknown>;
|
|
13
|
-
sdfBuffers?: BufferCollection;
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Can render multiple quads with multiple textures (up to vertex shader texture limit)
|
|
17
|
-
*
|
|
18
|
-
*/
|
|
19
|
-
export declare class WebGlRenderOp extends CoreRenderOp {
|
|
20
|
-
readonly renderer: WebGlRenderer;
|
|
21
|
-
readonly bufferIdx: number;
|
|
22
|
-
numQuads: number;
|
|
23
|
-
textures: WebGlCtxTexture[];
|
|
24
|
-
/**
|
|
25
|
-
* need to improve this when TextRenderers are refactored
|
|
26
|
-
*/
|
|
27
|
-
readonly sdfShaderProps: Record<string, unknown> | undefined;
|
|
28
|
-
readonly sdfNode: CoreTextNode | undefined;
|
|
29
|
-
readonly maxTextures: number;
|
|
30
|
-
readonly buffers: BufferCollection;
|
|
31
|
-
readonly shader: WebGlShaderNode;
|
|
32
|
-
readonly width: number;
|
|
33
|
-
readonly height: number;
|
|
34
|
-
readonly clippingRect: RectWithValid;
|
|
35
|
-
readonly rtt: boolean;
|
|
36
|
-
readonly parentHasRenderTexture: boolean;
|
|
37
|
-
readonly framebufferDimensions: Dimensions | null;
|
|
38
|
-
readonly alpha: number;
|
|
39
|
-
readonly pixelRatio: number;
|
|
40
|
-
readonly time?: number | null;
|
|
41
|
-
constructor(renderer: WebGlRenderer, quad: RenderOpQuadOptions, bufferIdx: number);
|
|
42
|
-
addTexture(texture: WebGlCtxTexture): number;
|
|
43
|
-
draw(): void;
|
|
44
|
-
}
|
|
45
|
-
export {};
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* If not stated otherwise in this file or this component's LICENSE file the
|
|
3
|
-
* following copyright and licenses apply:
|
|
4
|
-
*
|
|
5
|
-
* Copyright 2023 Comcast Cable Communications Management, LLC.
|
|
6
|
-
*
|
|
7
|
-
* Licensed under the Apache License, Version 2.0 (the License);
|
|
8
|
-
* you may not use this file except in compliance with the License.
|
|
9
|
-
* You may obtain a copy of the License at
|
|
10
|
-
*
|
|
11
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
*
|
|
13
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
-
* See the License for the specific language governing permissions and
|
|
17
|
-
* limitations under the License.
|
|
18
|
-
*/
|
|
19
|
-
import { CoreRenderOp } from '../CoreRenderOp.js';
|
|
20
|
-
/**
|
|
21
|
-
* Can render multiple quads with multiple textures (up to vertex shader texture limit)
|
|
22
|
-
*
|
|
23
|
-
*/
|
|
24
|
-
export class WebGlRenderOp extends CoreRenderOp {
|
|
25
|
-
renderer;
|
|
26
|
-
bufferIdx;
|
|
27
|
-
numQuads = 0;
|
|
28
|
-
textures = [];
|
|
29
|
-
/**
|
|
30
|
-
* need to improve this when TextRenderers are refactored
|
|
31
|
-
*/
|
|
32
|
-
sdfShaderProps;
|
|
33
|
-
sdfNode;
|
|
34
|
-
maxTextures;
|
|
35
|
-
buffers;
|
|
36
|
-
shader;
|
|
37
|
-
width;
|
|
38
|
-
height;
|
|
39
|
-
clippingRect;
|
|
40
|
-
rtt;
|
|
41
|
-
parentHasRenderTexture;
|
|
42
|
-
framebufferDimensions;
|
|
43
|
-
alpha;
|
|
44
|
-
pixelRatio;
|
|
45
|
-
time;
|
|
46
|
-
constructor(renderer, quad, bufferIdx) {
|
|
47
|
-
super();
|
|
48
|
-
this.renderer = renderer;
|
|
49
|
-
this.bufferIdx = bufferIdx;
|
|
50
|
-
this.buffers = quad.sdfBuffers || renderer.quadBufferCollection;
|
|
51
|
-
this.shader = quad.shader;
|
|
52
|
-
this.width = quad.width;
|
|
53
|
-
this.height = quad.height;
|
|
54
|
-
this.clippingRect = quad.clippingRect;
|
|
55
|
-
this.parentHasRenderTexture = quad.parentHasRenderTexture;
|
|
56
|
-
this.framebufferDimensions = quad.framebufferDimensions || null;
|
|
57
|
-
this.rtt = quad.rtt;
|
|
58
|
-
this.alpha = quad.alpha;
|
|
59
|
-
this.pixelRatio =
|
|
60
|
-
this.parentHasRenderTexture === true ? 1 : renderer.stage.pixelRatio;
|
|
61
|
-
this.time = quad.time;
|
|
62
|
-
/**
|
|
63
|
-
* related to line 51
|
|
64
|
-
*/
|
|
65
|
-
this.sdfShaderProps = quad.sdfShaderProps;
|
|
66
|
-
this.maxTextures = this.shader.program.supportsIndexedTextures
|
|
67
|
-
? renderer.glw.getParameter(renderer.glw.MAX_VERTEX_TEXTURE_IMAGE_UNITS)
|
|
68
|
-
: 1;
|
|
69
|
-
}
|
|
70
|
-
addTexture(texture) {
|
|
71
|
-
const { textures, maxTextures } = this;
|
|
72
|
-
let existingIdx = -1;
|
|
73
|
-
const texturesLength = textures.length;
|
|
74
|
-
for (let i = 0; i < texturesLength; i++) {
|
|
75
|
-
const t = textures[i];
|
|
76
|
-
if (t === texture) {
|
|
77
|
-
existingIdx = i;
|
|
78
|
-
break;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
if (existingIdx !== -1) {
|
|
82
|
-
return existingIdx;
|
|
83
|
-
}
|
|
84
|
-
if (texturesLength >= maxTextures) {
|
|
85
|
-
return 0xffffffff;
|
|
86
|
-
}
|
|
87
|
-
this.textures.push(texture);
|
|
88
|
-
return texturesLength;
|
|
89
|
-
}
|
|
90
|
-
draw() {
|
|
91
|
-
const { glw, options, stage } = this.renderer;
|
|
92
|
-
stage.shManager.useShader(this.shader.program);
|
|
93
|
-
this.shader.program.bindRenderOp(this);
|
|
94
|
-
// Clipping
|
|
95
|
-
if (this.clippingRect.valid === true) {
|
|
96
|
-
const clipX = Math.round(this.clippingRect.x * this.pixelRatio);
|
|
97
|
-
const clipWidth = Math.round(this.clippingRect.width * this.pixelRatio);
|
|
98
|
-
const clipHeight = Math.round(this.clippingRect.height * this.pixelRatio);
|
|
99
|
-
let clipY = Math.round(options.canvas.height -
|
|
100
|
-
clipHeight -
|
|
101
|
-
this.clippingRect.y * this.pixelRatio);
|
|
102
|
-
// if parent has render texture, we need to adjust the scissor rect
|
|
103
|
-
// to be relative to the parent's framebuffer
|
|
104
|
-
if (this.parentHasRenderTexture) {
|
|
105
|
-
clipY = this.framebufferDimensions
|
|
106
|
-
? this.framebufferDimensions.h - this.height
|
|
107
|
-
: 0;
|
|
108
|
-
}
|
|
109
|
-
glw.setScissorTest(true);
|
|
110
|
-
glw.scissor(clipX, clipY, clipWidth, clipHeight);
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
glw.setScissorTest(false);
|
|
114
|
-
}
|
|
115
|
-
// Check if this is SDF rendering (has sdfBuffers)
|
|
116
|
-
if (this.sdfShaderProps !== undefined) {
|
|
117
|
-
// SDF rendering uses drawArrays with explicit triangle vertices (6 vertices per quad)
|
|
118
|
-
glw.drawArrays(glw.TRIANGLES, 0, 6 * this.numQuads);
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
// Regular rendering uses drawElements with index buffer (4 vertices per quad)
|
|
122
|
-
const quadIdx = (this.bufferIdx / 32) * 6 * 2;
|
|
123
|
-
glw.drawElements(glw.TRIANGLES, 6 * this.numQuads, glw.UNSIGNED_SHORT, quadIdx);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
//# sourceMappingURL=WebGlRenderOp.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"WebGlRenderOp.js","sourceRoot":"","sources":["../../../../../src/core/renderers/webgl/WebGlRenderOp.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAyBlD;;;GAGG;AACH,MAAM,OAAO,aAAc,SAAQ,YAAY;IAuBlC;IAEA;IAxBX,QAAQ,GAAG,CAAC,CAAC;IACb,QAAQ,GAAsB,EAAE,CAAC;IAEjC;;OAEG;IACM,cAAc,CAAsC;IACpD,OAAO,CAA2B;IAClC,WAAW,CA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AAG,CAAC,SAAS,EACb,CAAC,GAAG,IAAI,CAAC,QAAQ,EACjB,GAAG,CAAC,cAAc,EAClB,OAAO,CACR,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { CoreFont, type CoreFontProps } from './CoreFont.js';
|
|
2
|
-
import type { NormalizedFontMetrics, TextRenderer } from './TextRenderer.js';
|
|
3
|
-
export type CanvasFontProps = CoreFontProps & {
|
|
4
|
-
fontUrl: string;
|
|
5
|
-
};
|
|
6
|
-
export declare class CanvasFont extends CoreFont {
|
|
7
|
-
private measureContext;
|
|
8
|
-
type: string;
|
|
9
|
-
url: string;
|
|
10
|
-
constructor(textRenderer: TextRenderer, props: CanvasFontProps, measureContext: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D);
|
|
11
|
-
load(): void;
|
|
12
|
-
measureText(text: string, letterSpacing: number): number;
|
|
13
|
-
getMetrics(fontSize: number): NormalizedFontMetrics;
|
|
14
|
-
}
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* If not stated otherwise in this file or this component's LICENSE file the
|
|
3
|
-
* following copyright and licenses apply:
|
|
4
|
-
*
|
|
5
|
-
* Copyright 2025 Comcast Cable Communications Management, LLC.
|
|
6
|
-
*
|
|
7
|
-
* Licensed under the Apache License, Version 2.0 (the License);
|
|
8
|
-
* you may not use this file except in compliance with the License.
|
|
9
|
-
* You may obtain a copy of the License at
|
|
10
|
-
*
|
|
11
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
*
|
|
13
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
-
* See the License for the specific language governing permissions and
|
|
17
|
-
* limitations under the License.
|
|
18
|
-
*/
|
|
19
|
-
import { CoreFont, FontState } from './CoreFont.js';
|
|
20
|
-
import { normalizeFontMetrics } from './TextLayoutEngine.js';
|
|
21
|
-
import { hasZeroWidthSpace } from './Utils.js';
|
|
22
|
-
export class CanvasFont extends CoreFont {
|
|
23
|
-
measureContext;
|
|
24
|
-
type = 'canvas';
|
|
25
|
-
url;
|
|
26
|
-
constructor(textRenderer, props, measureContext) {
|
|
27
|
-
super(textRenderer, props);
|
|
28
|
-
this.measureContext = measureContext;
|
|
29
|
-
this.url = props.fontUrl;
|
|
30
|
-
this.metrics = props.metrics ?? null;
|
|
31
|
-
}
|
|
32
|
-
load() {
|
|
33
|
-
if (this.state !== FontState.Created) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
if (this.family === 'sans-serif') {
|
|
37
|
-
// Default font, skip loading
|
|
38
|
-
this.onLoaded();
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
this.state = FontState.Loading;
|
|
42
|
-
new FontFace(this.family, `url(${this.url})`)
|
|
43
|
-
.load()
|
|
44
|
-
.then((loadedFont) => {
|
|
45
|
-
document.fonts.add(loadedFont);
|
|
46
|
-
this.onLoaded();
|
|
47
|
-
})
|
|
48
|
-
.catch((error) => {
|
|
49
|
-
this.state = FontState.Failed;
|
|
50
|
-
console.error(`Failed to load font: ${this.family}`, error);
|
|
51
|
-
this.emit('failed');
|
|
52
|
-
throw error;
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
measureText(text, letterSpacing) {
|
|
56
|
-
if (letterSpacing === 0) {
|
|
57
|
-
return this.measureContext.measureText(text).width;
|
|
58
|
-
}
|
|
59
|
-
if (hasZeroWidthSpace(text) === false) {
|
|
60
|
-
return (this.measureContext.measureText(text).width +
|
|
61
|
-
letterSpacing * text.length);
|
|
62
|
-
}
|
|
63
|
-
return text.split('').reduce((acc, char) => {
|
|
64
|
-
if (hasZeroWidthSpace(char) === true) {
|
|
65
|
-
return acc;
|
|
66
|
-
}
|
|
67
|
-
return acc + this.measureContext.measureText(char).width + letterSpacing;
|
|
68
|
-
}, 0);
|
|
69
|
-
}
|
|
70
|
-
getMetrics(fontSize) {
|
|
71
|
-
let m = this.normalizedMetrics[fontSize];
|
|
72
|
-
if (m !== undefined) {
|
|
73
|
-
return m;
|
|
74
|
-
}
|
|
75
|
-
let metrics = this.metrics;
|
|
76
|
-
if (metrics === null) {
|
|
77
|
-
metrics = calculateCanvasMetrics(this.family, fontSize, this.measureContext);
|
|
78
|
-
}
|
|
79
|
-
m = this.normalizedMetrics[fontSize] = normalizeFontMetrics(metrics, fontSize);
|
|
80
|
-
return m;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
function calculateCanvasMetrics(fontFamily, fontSize, measureContext) {
|
|
84
|
-
// If the font face doesn't have metrics defined, we fallback to using the
|
|
85
|
-
// browser's measureText method to calculate take a best guess at the font
|
|
86
|
-
// actual font's metrics.
|
|
87
|
-
// - fontBoundingBox[Ascent|Descent] is the best estimate but only supported
|
|
88
|
-
// in Chrome 87+ (2020), Firefox 116+ (2023), and Safari 11.1+ (2018).
|
|
89
|
-
// - It is an estimate as it can vary between browsers.
|
|
90
|
-
// - actualBoundingBox[Ascent|Descent] is less accurate and supported in
|
|
91
|
-
// Chrome 77+ (2019), Firefox 74+ (2020), and Safari 11.1+ (2018).
|
|
92
|
-
// - If neither are supported, we'll use some default values which will
|
|
93
|
-
// get text on the screen but likely not be great.
|
|
94
|
-
// NOTE: It's been decided not to rely on fontBoundingBox[Ascent|Descent]
|
|
95
|
-
// as it's browser support is limited and it also tends to produce higher than
|
|
96
|
-
// expected values. It is instead HIGHLY RECOMMENDED that developers provide
|
|
97
|
-
// explicit metrics in the font face definition.
|
|
98
|
-
// Ensure font is loaded by checking document.fonts
|
|
99
|
-
if (!document.fonts.check(`${fontSize}px ${fontFamily}`)) {
|
|
100
|
-
console.warn(`Font ${fontFamily} may not be fully loaded yet when calculating metrics`);
|
|
101
|
-
}
|
|
102
|
-
measureContext.font = `${fontSize}px ${fontFamily}`;
|
|
103
|
-
const metrics = measureContext.measureText('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');
|
|
104
|
-
console.warn(`Font metrics not provided for Canvas Web font ${fontFamily}. ` +
|
|
105
|
-
'Using fallback values. It is HIGHLY recommended you use the latest ' +
|
|
106
|
-
'version of the Lightning 3 `msdf-generator` tool to extract the default ' +
|
|
107
|
-
'metrics for the font and provide them in the Canvas Web font definition.');
|
|
108
|
-
const ascender = metrics.fontBoundingBoxAscent ?? metrics.actualBoundingBoxAscent ?? 0;
|
|
109
|
-
const descender = metrics.fontBoundingBoxDescent ?? metrics.actualBoundingBoxDescent ?? 0;
|
|
110
|
-
const emHeight = (metrics.emHeightAscent ?? 0) + (metrics.emHeightDescent ?? 0);
|
|
111
|
-
const unitsPerEm = emHeight > 0 ? emHeight : ascender + descender;
|
|
112
|
-
return {
|
|
113
|
-
ascender,
|
|
114
|
-
descender: -descender,
|
|
115
|
-
lineGap: emHeight - (ascender + descender),
|
|
116
|
-
unitsPerEm,
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
//# sourceMappingURL=CanvasFont.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CanvasFont.js","sourceRoot":"","sources":["../../../../src/core/text-rendering/CanvasFont.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAsB,MAAM,eAAe,CAAC;AACxE,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAE7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAa/C,MAAM,OAAO,UAAW,SAAQ,QAAQ;IAO5B;IANH,IAAI,GAAG,QAAQ,CAAC;IAChB,GAAG,CAAS;IAEnB,YACE,YAA0B,EAC1B,KAAsB,EACd,cAE6B;QAErC,KAAK,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;QAJnB,mBAAc,GAAd,cAAc,CAEe;QAGrC,IAAI,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D;IAEhD,mDAAmD;IACnD,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,QAAQ,MAAM,UAAU,EAAE,CAAC,EAAE,CAAC;QACzD,OAAO,CAAC,IAAI,CACV,QAAQ,UAAU,uDAAuD,CAC1E,CAAC;IACJ,CAAC;IAED,cAAc,CAAC,IAAI,GAAG,GAAG,QAAQ,MAAM,UAAU,EAAE,CAAC;IAEpD,MAAM,OAAO,GAAG,cAAc,CAAC,WAAW,CACxC,sDAAsD,CACvD,CAAC;IACF,OAAO,CAAC,IAAI,CACV,iDAAiD,UAAU,IAAI;QAC7D,qEAAqE;QACrE,0EAA0E;QAC1E,0EAA0E,CAC7E,CAAC;IACF,MAAM,QAAQ,GACZ,OAAO,CAAC,qBAAqB,IAAI,OAAO,CAAC,uBAAuB,IAAI,CAAC,CAAC;IACxE,MAAM,SAAS,GACb,OAAO,CAAC,sBAAsB,IAAI,OAAO,CAAC,wBAAwB,IAAI,CAAC,CAAC;IAE1E,MAAM,QAAQ,GACZ,CAAC,OAAO,CAAC,cAAc,IAAI,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,eAAe,IAAI,CAAC,CAAC,CAAC;IACjE,MAAM,UAAU,GAAG,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,GAAG,SAAS,CAAC;IAElE,OAAO;QACL,QAAQ;QACR,SAAS,EAAE,CAAC,SAAS;QACrB,OAAO,EAAE,QAAQ,GAAG,CAAC,QAAQ,GAAG,SAAS,CAAC;QAC1C,UAAU;KACX,CAAC;AACJ,CAAC"}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { CoreTextNode } from '../CoreTextNode.js';
|
|
2
|
-
import type { FontMetrics, NormalizedFontMetrics, TextRenderer } from './TextRenderer.js';
|
|
3
|
-
import { EventEmitter } from '../../common/EventEmitter.js';
|
|
4
|
-
export declare enum FontState {
|
|
5
|
-
Created = 0,
|
|
6
|
-
Loading = 1,
|
|
7
|
-
Loaded = 2,
|
|
8
|
-
Failed = 3
|
|
9
|
-
}
|
|
10
|
-
export interface CoreFontProps {
|
|
11
|
-
fontFamily: string;
|
|
12
|
-
metrics?: FontMetrics;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* EventEmiter only intended to communicated with FontManager
|
|
16
|
-
*/
|
|
17
|
-
export declare abstract class CoreFont extends EventEmitter {
|
|
18
|
-
protected waitingNodes: Record<number, CoreTextNode | null> | null;
|
|
19
|
-
protected normalizedMetrics: Record<number, NormalizedFontMetrics> | null;
|
|
20
|
-
textRenderer: TextRenderer;
|
|
21
|
-
state: FontState;
|
|
22
|
-
family: string;
|
|
23
|
-
metrics: FontMetrics | null;
|
|
24
|
-
constructor(textRenderer: TextRenderer, props: CoreFontProps);
|
|
25
|
-
protected onLoaded(): void;
|
|
26
|
-
waiting(node: CoreTextNode): void;
|
|
27
|
-
stopWaiting(node: CoreTextNode): void;
|
|
28
|
-
destroy(): void;
|
|
29
|
-
abstract type: string;
|
|
30
|
-
abstract load(): void;
|
|
31
|
-
abstract measureText(text: string, letterSpacing: number): number;
|
|
32
|
-
abstract getMetrics(fontSize: number): NormalizedFontMetrics;
|
|
33
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { UpdateType } from '../CoreNode.js';
|
|
2
|
-
import { EventEmitter } from '../../common/EventEmitter.js';
|
|
3
|
-
export var FontState;
|
|
4
|
-
(function (FontState) {
|
|
5
|
-
FontState[FontState["Created"] = 0] = "Created";
|
|
6
|
-
FontState[FontState["Loading"] = 1] = "Loading";
|
|
7
|
-
FontState[FontState["Loaded"] = 2] = "Loaded";
|
|
8
|
-
FontState[FontState["Failed"] = 3] = "Failed";
|
|
9
|
-
})(FontState || (FontState = {}));
|
|
10
|
-
/**
|
|
11
|
-
* EventEmiter only intended to communicated with FontManager
|
|
12
|
-
*/
|
|
13
|
-
export class CoreFont extends EventEmitter {
|
|
14
|
-
waitingNodes = Object.create(null);
|
|
15
|
-
normalizedMetrics = Object.create(null);
|
|
16
|
-
textRenderer;
|
|
17
|
-
state;
|
|
18
|
-
family;
|
|
19
|
-
metrics = null;
|
|
20
|
-
constructor(textRenderer, props) {
|
|
21
|
-
super();
|
|
22
|
-
this.family = props.fontFamily;
|
|
23
|
-
this.state = FontState.Created;
|
|
24
|
-
this.textRenderer = textRenderer;
|
|
25
|
-
}
|
|
26
|
-
onLoaded() {
|
|
27
|
-
const waitingNodes = this.waitingNodes;
|
|
28
|
-
for (let key in waitingNodes) {
|
|
29
|
-
waitingNodes[key].setUpdateType(UpdateType.Local);
|
|
30
|
-
waitingNodes[key] = null;
|
|
31
|
-
}
|
|
32
|
-
this.state = FontState.Loaded;
|
|
33
|
-
}
|
|
34
|
-
waiting(node) {
|
|
35
|
-
this.waitingNodes[node.id] = node;
|
|
36
|
-
}
|
|
37
|
-
stopWaiting(node) {
|
|
38
|
-
if (this.waitingNodes[node.id] !== null) {
|
|
39
|
-
this.waitingNodes[node.id] = null;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
destroy() {
|
|
43
|
-
this.waitingNodes = null;
|
|
44
|
-
this.normalizedMetrics = null;
|
|
45
|
-
this.metrics = null;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
//# sourceMappingURL=CoreFont.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CoreFont.js","sourceRoot":"","sources":["../../../../src/core/text-rendering/CoreFont.ts"],"names":[],"mappings":"AAwBA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAE5D,MAAM,CAAN,IAAY,SAKX;AALD,WAAY,SAAS;IACnB,+CAAO,CAAA;IACP,+CAAO,CAAA;IACP,6CAAM,CAAA;IACN,6CAAM,CAAA;AACR,CAAC,EALW,SAAS,KAAT,SAAS,QAKpB;AAOD;;GAEG;AACH,MAAM,O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tB,CAAC;CAMF"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { Stage } from '../Stage.js';
|
|
2
|
-
import type { CoreFont } from './CoreFont.js';
|
|
3
|
-
import type { FontLoadOptions, TextRenderer, TextRenderers } from './TextRenderer.js';
|
|
4
|
-
export declare class CoreFontManager {
|
|
5
|
-
private fonts;
|
|
6
|
-
private renderers;
|
|
7
|
-
constructor(stage: Stage, textRenderers: TextRenderer[]);
|
|
8
|
-
loadFont(type: TextRenderers, options: FontLoadOptions): void;
|
|
9
|
-
unloadFont(fontFamily: string): void;
|
|
10
|
-
getFont(fontFamily: string): CoreFont | null;
|
|
11
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
export class CoreFontManager {
|
|
2
|
-
fonts = Object.create(null);
|
|
3
|
-
renderers = Object.create(null);
|
|
4
|
-
constructor(stage, textRenderers) {
|
|
5
|
-
for (let i = 0; i < textRenderers.length; i++) {
|
|
6
|
-
const renderer = textRenderers[i];
|
|
7
|
-
this.renderers[renderer.type] = renderer;
|
|
8
|
-
renderer.init(stage, this);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
loadFont(type, options) {
|
|
12
|
-
const targetRenderer = this.renderers[type];
|
|
13
|
-
if (targetRenderer === undefined) {
|
|
14
|
-
console.error('renderer type for this font does not exist');
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
const font = targetRenderer.createFont(options);
|
|
18
|
-
if (font === undefined) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
font.load();
|
|
22
|
-
this.fonts[options.fontFamily] = font;
|
|
23
|
-
}
|
|
24
|
-
unloadFont(fontFamily) {
|
|
25
|
-
const targetFont = this.fonts[fontFamily];
|
|
26
|
-
if (targetFont === undefined) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
targetFont.destroy();
|
|
30
|
-
delete this.fonts[fontFamily];
|
|
31
|
-
}
|
|
32
|
-
getFont(fontFamily) {
|
|
33
|
-
const font = this.fonts[fontFamily];
|
|
34
|
-
if (font === undefined) {
|
|
35
|
-
console.warn('fontFamily not registered');
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
return font;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
//# sourceMappingURL=FontManager.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FontManager.js","sourceRoot":"","sources":["../../../../src/core/text-rendering/FontManager.ts"],"names":[],"mappings":"AAQA,MAAM,OAAO,eAAe;IAClB,KAAK,GAA6B,MAAM,CAAC,MAAM,CAAC,IAAI,CAG3D,CAAC;IACM,SAAS,GAAiC,MAAM,CAAC,MAAM,CAC7D,IAAI,CAC2B,CAAC;IAElC,YAAY,KAAY,EAAE,aAA6B;QACrD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,aAAa,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAC9C,MAAM,QAAQ,GAAG,aAAa,CAAC,CAAC,CAAE,CAAC;YACnC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC;YACzC,QAAQ,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC;IAED,QAAQ,CAAC,IAAmB,EAAE,OAAwB;QACpD,MAAM,cAAc,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QAC5C,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;YACjC,OAAO,CAAC,KAAK,CAAC,4CAA4C,CAAC,CAAC;YAC5D,OAAO;QACT,CAAC;QACD,MAAM,IAAI,GAAG,cAAc,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QAChD,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,OAAO;QACT,CAAC;QACD,IAAI,CAAC,IAAI,EAAE,CAAC;QACZ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;IACxC,CAAC;IAED,UAAU,CAAC,UAAkB;QAC3B,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QAC1C,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;YAC7B,OAAO;QACT,CAAC;QACD,UAAU,CAAC,OAAO,EAAE,CAAC;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IAChC,CAAC;IAED,OAAO,CAAC,UAAkB;QACxB,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QACpC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,OAAO,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;YAC1C,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { Stage } from '../Stage.js';
|
|
2
|
-
import type { ImageTexture } from '../textures/ImageTexture.js';
|
|
3
|
-
import { CoreFont, type CoreFontProps } from './CoreFont.js';
|
|
4
|
-
import { type SdfFontData } from './SdfFontHandler.js';
|
|
5
|
-
import type { NormalizedFontMetrics, TextRenderer } from './TextRenderer.js';
|
|
6
|
-
export type SdfFontProps = CoreFontProps & {
|
|
7
|
-
atlasUrl: string;
|
|
8
|
-
atlasDataUrl: string;
|
|
9
|
-
};
|
|
10
|
-
export declare class SdfFont extends CoreFont {
|
|
11
|
-
private stage;
|
|
12
|
-
type: string;
|
|
13
|
-
atlasUrl: string;
|
|
14
|
-
atlasDataUrl: string;
|
|
15
|
-
atlasTexture?: ImageTexture;
|
|
16
|
-
private glyphMap?;
|
|
17
|
-
private kerningTable?;
|
|
18
|
-
private data?;
|
|
19
|
-
constructor(textRenderer: TextRenderer, props: SdfFontProps, stage: Stage);
|
|
20
|
-
load(): void;
|
|
21
|
-
private hardFail;
|
|
22
|
-
private processFontData;
|
|
23
|
-
measureText(text: string, letterSpacing: number): number;
|
|
24
|
-
getMetrics(fontSize: number): NormalizedFontMetrics;
|
|
25
|
-
getGlyph(codepoint: number): import("./SdfFontHandler.js").SdfGlyph | null;
|
|
26
|
-
getKerning(firstGlyph: number, secondGlyph: number): number;
|
|
27
|
-
getAtlas(): ImageTexture;
|
|
28
|
-
getData(): SdfFontData;
|
|
29
|
-
}
|
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* If not stated otherwise in this file or this component's LICENSE file the
|
|
3
|
-
* following copyright and licenses apply:
|
|
4
|
-
*
|
|
5
|
-
* Copyright 2025 Comcast Cable Communications Management, LLC.
|
|
6
|
-
*
|
|
7
|
-
* Licensed under the Apache License, Version 2.0 (the License);
|
|
8
|
-
* you may not use this file except in compliance with the License.
|
|
9
|
-
* You may obtain a copy of the License at
|
|
10
|
-
*
|
|
11
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
*
|
|
13
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
-
* See the License for the specific language governing permissions and
|
|
17
|
-
* limitations under the License.
|
|
18
|
-
*/
|
|
19
|
-
import { CoreFont, FontState } from './CoreFont.js';
|
|
20
|
-
import { buildGlyphMap, buildKerningTable, } from './SdfFontHandler.js';
|
|
21
|
-
import { normalizeFontMetrics } from './TextLayoutEngine.js';
|
|
22
|
-
import { hasZeroWidthSpace } from './Utils.js';
|
|
23
|
-
export class SdfFont extends CoreFont {
|
|
24
|
-
stage;
|
|
25
|
-
type = 'sdf';
|
|
26
|
-
atlasUrl;
|
|
27
|
-
atlasDataUrl;
|
|
28
|
-
atlasTexture;
|
|
29
|
-
glyphMap;
|
|
30
|
-
kerningTable;
|
|
31
|
-
data;
|
|
32
|
-
constructor(textRenderer, props, stage) {
|
|
33
|
-
super(textRenderer, props);
|
|
34
|
-
this.stage = stage;
|
|
35
|
-
this.atlasUrl = props.atlasUrl;
|
|
36
|
-
this.atlasDataUrl = props.atlasDataUrl;
|
|
37
|
-
}
|
|
38
|
-
load() {
|
|
39
|
-
new Promise(async () => {
|
|
40
|
-
const atlasData = await fetch(this.atlasDataUrl);
|
|
41
|
-
if (atlasData.ok === false) {
|
|
42
|
-
this.hardFail(`Failed to load font data: ${atlasData.statusText}`);
|
|
43
|
-
}
|
|
44
|
-
const fontData = (await atlasData.json());
|
|
45
|
-
if (fontData.chars === undefined) {
|
|
46
|
-
this.hardFail('Invalid SDF font data format');
|
|
47
|
-
}
|
|
48
|
-
const atlasTexture = this.stage.txManager.createTexture('ImageTexture', {
|
|
49
|
-
src: this.atlasUrl,
|
|
50
|
-
premultiplyAlpha: false,
|
|
51
|
-
});
|
|
52
|
-
atlasTexture.setRenderableOwner(this.family, true);
|
|
53
|
-
atlasTexture.preventCleanup = true;
|
|
54
|
-
atlasTexture.on('loaded', () => {
|
|
55
|
-
this.onLoaded();
|
|
56
|
-
});
|
|
57
|
-
atlasTexture.on('failed', (error) => {
|
|
58
|
-
console.error(`Failed to load SDF font: ${this.family}`, error);
|
|
59
|
-
this.emit('failed');
|
|
60
|
-
});
|
|
61
|
-
this.atlasTexture = atlasTexture;
|
|
62
|
-
this.processFontData(fontData);
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
hardFail(message) {
|
|
66
|
-
this.state = FontState.Failed;
|
|
67
|
-
throw new Error(message);
|
|
68
|
-
}
|
|
69
|
-
processFontData(fontData) {
|
|
70
|
-
this.glyphMap = buildGlyphMap(fontData.chars);
|
|
71
|
-
this.kerningTable = buildKerningTable(fontData.kernings);
|
|
72
|
-
this.metrics = this.metrics ||
|
|
73
|
-
fontData.lightningMetrics || {
|
|
74
|
-
ascender: 800,
|
|
75
|
-
descender: -200,
|
|
76
|
-
lineGap: 200,
|
|
77
|
-
unitsPerEm: 1000,
|
|
78
|
-
};
|
|
79
|
-
this.data = fontData;
|
|
80
|
-
}
|
|
81
|
-
measureText(text, letterSpacing) {
|
|
82
|
-
if (text.length === 1) {
|
|
83
|
-
const char = text.charAt(0);
|
|
84
|
-
const codepoint = text.codePointAt(0);
|
|
85
|
-
if (codepoint === undefined)
|
|
86
|
-
return 0;
|
|
87
|
-
if (hasZeroWidthSpace(char) === true)
|
|
88
|
-
return 0;
|
|
89
|
-
const glyph = this.getGlyph(codepoint);
|
|
90
|
-
if (glyph === null)
|
|
91
|
-
return 0;
|
|
92
|
-
return glyph.xadvance + letterSpacing;
|
|
93
|
-
}
|
|
94
|
-
let width = 0;
|
|
95
|
-
let prevCodepoint = 0;
|
|
96
|
-
for (let i = 0; i < text.length; i++) {
|
|
97
|
-
const char = text.charAt(i);
|
|
98
|
-
const codepoint = text.codePointAt(i);
|
|
99
|
-
if (codepoint === undefined)
|
|
100
|
-
continue;
|
|
101
|
-
// Skip zero-width spaces in width calculations
|
|
102
|
-
if (hasZeroWidthSpace(char)) {
|
|
103
|
-
continue;
|
|
104
|
-
}
|
|
105
|
-
const glyph = this.getGlyph(codepoint);
|
|
106
|
-
if (glyph === null)
|
|
107
|
-
continue;
|
|
108
|
-
let advance = glyph.xadvance;
|
|
109
|
-
// Add kerning if there's a previous character
|
|
110
|
-
if (prevCodepoint !== 0) {
|
|
111
|
-
const kerning = this.getKerning(prevCodepoint, codepoint);
|
|
112
|
-
advance += kerning;
|
|
113
|
-
}
|
|
114
|
-
width += advance + letterSpacing;
|
|
115
|
-
prevCodepoint = codepoint;
|
|
116
|
-
}
|
|
117
|
-
return width;
|
|
118
|
-
}
|
|
119
|
-
getMetrics(fontSize) {
|
|
120
|
-
let m = this.normalizedMetrics[fontSize];
|
|
121
|
-
if (m !== undefined) {
|
|
122
|
-
return m;
|
|
123
|
-
}
|
|
124
|
-
m = this.normalizedMetrics[fontSize] = normalizeFontMetrics(this.metrics, fontSize);
|
|
125
|
-
return m;
|
|
126
|
-
}
|
|
127
|
-
getGlyph(codepoint) {
|
|
128
|
-
const gm = this.glyphMap;
|
|
129
|
-
return gm[codepoint] || gm[63] || null;
|
|
130
|
-
}
|
|
131
|
-
getKerning(firstGlyph, secondGlyph) {
|
|
132
|
-
const seconds = this.kerningTable[secondGlyph];
|
|
133
|
-
return (seconds !== undefined && seconds[firstGlyph]) || 0;
|
|
134
|
-
}
|
|
135
|
-
getAtlas() {
|
|
136
|
-
return this.atlasTexture;
|
|
137
|
-
}
|
|
138
|
-
getData() {
|
|
139
|
-
return this.data;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
//# sourceMappingURL=SdfFont.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SdfFont.js","sourceRoot":"","sources":["../../../../src/core/text-rendering/SdfFont.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAIH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAsB,MAAM,eAAe,CAAC;AACxE,OAAO,EACL,aAAa,EACb,iBAAiB,GAIlB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAE7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAO/C,MAAM,OAAO,OAAQ,SAAQ,QAAQ;IAazB;IAZH,IAAI,GAAG,KAAK,CAAC;IACb,QAAQ,CAAS;IACjB,YAAY,CAAS;IACrB,YAAY,CAAgB;IAE3B,QAAQ,CAAe;IACvB,YAAY,CAAgB;IAC5B,IAAI,CAAe;IAE3B,YACE,YAA0B,EAC1B,KAAmB,EACX,KAAY;QAEpB,KAAK,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;QAFnB,UAAK,GAAL,KAAK,CAAO;QAGpB,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC;QAC/B,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC,YAAY,CAAC;IACzC,CAAC;IAED,IAAI;QACF,IA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