@lightningjs/renderer 2.18.2 → 2.18.4
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/exports/canvas-shaders.d.ts +10 -0
- package/dist/exports/canvas-shaders.js +27 -0
- package/dist/exports/canvas-shaders.js.map +1 -0
- package/dist/exports/index.d.ts +1 -0
- package/dist/exports/index.js +1 -0
- package/dist/exports/index.js.map +1 -1
- package/dist/exports/webgl-shaders.d.ts +11 -0
- package/dist/exports/webgl-shaders.js +28 -0
- package/dist/exports/webgl-shaders.js.map +1 -0
- package/dist/src/common/CommonTypes.d.ts +2 -1
- package/dist/src/core/CoreNode.js +8 -4
- package/dist/src/core/CoreNode.js.map +1 -1
- package/dist/src/core/CoreTextureManager.d.ts +2 -0
- package/dist/src/core/CoreTextureManager.js +8 -4
- package/dist/src/core/CoreTextureManager.js.map +1 -1
- package/dist/src/core/Stage.d.ts +1 -0
- package/dist/src/core/Stage.js +2 -1
- package/dist/src/core/Stage.js.map +1 -1
- package/dist/src/core/TextureError.d.ts +11 -0
- package/dist/src/core/TextureError.js +37 -0
- package/dist/src/core/TextureError.js.map +1 -0
- package/dist/src/core/TextureMemoryManager.d.ts +1 -19
- package/dist/src/core/TextureMemoryManager.js +16 -43
- package/dist/src/core/TextureMemoryManager.js.map +1 -1
- package/dist/src/core/lib/colorCache.d.ts +1 -0
- package/dist/src/core/lib/colorCache.js +19 -0
- package/dist/src/core/lib/colorCache.js.map +1 -0
- package/dist/src/core/lib/colorParser.d.ts +21 -0
- package/dist/src/core/lib/colorParser.js +72 -0
- package/dist/src/core/lib/colorParser.js.map +1 -0
- package/dist/src/core/platform.js +0 -3
- package/dist/src/core/platform.js.map +1 -1
- package/dist/src/core/platforms/Platform.d.ts +37 -0
- package/dist/src/core/platforms/Platform.js +22 -0
- package/dist/src/core/platforms/Platform.js.map +1 -0
- package/dist/src/core/platforms/web/WebPlatform.d.ts +9 -0
- package/dist/src/core/platforms/web/WebPlatform.js +84 -0
- package/dist/src/core/platforms/web/WebPlatform.js.map +1 -0
- package/dist/src/core/renderers/CoreShaderNode.d.ts +59 -0
- package/dist/src/core/renderers/CoreShaderNode.js +113 -0
- package/dist/src/core/renderers/CoreShaderNode.js.map +1 -0
- package/dist/src/core/renderers/CoreShaderProgram.d.ts +4 -0
- package/dist/src/core/renderers/CoreShaderProgram.js +20 -0
- package/dist/src/core/renderers/CoreShaderProgram.js.map +1 -0
- package/dist/src/core/renderers/canvas/CanvasRenderer.d.ts +35 -0
- package/dist/src/core/renderers/canvas/CanvasRenderer.js +201 -0
- package/dist/src/core/renderers/canvas/CanvasRenderer.js.map +1 -0
- package/dist/src/core/renderers/canvas/CanvasShaderNode.d.ts +21 -0
- package/dist/src/core/renderers/canvas/CanvasShaderNode.js +58 -0
- package/dist/src/core/renderers/canvas/CanvasShaderNode.js.map +1 -0
- package/dist/src/core/renderers/canvas/CanvasTexture.d.ts +16 -0
- package/dist/src/core/renderers/canvas/CanvasTexture.js +125 -0
- package/dist/src/core/renderers/canvas/CanvasTexture.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.d.ts +12 -0
- package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.js +57 -0
- package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.d.ts +22 -0
- package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.js +67 -0
- package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlCtxTexture.d.ts +56 -0
- package/dist/src/core/renderers/webgl/WebGlCtxTexture.js +236 -0
- package/dist/src/core/renderers/webgl/WebGlCtxTexture.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlRenderOp.d.ts +44 -0
- package/dist/src/core/renderers/webgl/WebGlRenderOp.js +125 -0
- package/dist/src/core/renderers/webgl/WebGlRenderOp.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlRenderer.d.ts +136 -0
- package/dist/src/core/renderers/webgl/WebGlRenderer.js +573 -0
- package/dist/src/core/renderers/webgl/WebGlRenderer.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlShaderNode.d.ts +222 -0
- package/dist/src/core/renderers/webgl/WebGlShaderNode.js +331 -0
- package/dist/src/core/renderers/webgl/WebGlShaderNode.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlShaderProgram.d.ts +37 -0
- package/dist/src/core/renderers/webgl/WebGlShaderProgram.js +218 -0
- package/dist/src/core/renderers/webgl/WebGlShaderProgram.js.map +1 -0
- package/dist/src/core/shaders/canvas/Border.d.ts +9 -0
- package/dist/src/core/shaders/canvas/Border.js +57 -0
- package/dist/src/core/shaders/canvas/Border.js.map +1 -0
- package/dist/src/core/shaders/canvas/HolePunch.d.ts +7 -0
- package/dist/src/core/shaders/canvas/HolePunch.js +38 -0
- package/dist/src/core/shaders/canvas/HolePunch.js.map +1 -0
- package/dist/src/core/shaders/canvas/LinearGradient.d.ts +10 -0
- package/dist/src/core/shaders/canvas/LinearGradient.js +48 -0
- package/dist/src/core/shaders/canvas/LinearGradient.js.map +1 -0
- package/dist/src/core/shaders/canvas/RadialGradient.d.ts +11 -0
- package/dist/src/core/shaders/canvas/RadialGradient.js +70 -0
- package/dist/src/core/shaders/canvas/RadialGradient.js.map +1 -0
- package/dist/src/core/shaders/canvas/Rounded.d.ts +7 -0
- package/dist/src/core/shaders/canvas/Rounded.js +33 -0
- package/dist/src/core/shaders/canvas/Rounded.js.map +1 -0
- package/dist/src/core/shaders/canvas/RoundedWithBorder.d.ts +7 -0
- package/dist/src/core/shaders/canvas/RoundedWithBorder.js +43 -0
- package/dist/src/core/shaders/canvas/RoundedWithBorder.js.map +1 -0
- package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.d.ts +8 -0
- package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.js +41 -0
- package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.js.map +1 -0
- package/dist/src/core/shaders/canvas/RoundedWithShadow.d.ts +7 -0
- package/dist/src/core/shaders/canvas/RoundedWithShadow.js +40 -0
- package/dist/src/core/shaders/canvas/RoundedWithShadow.js.map +1 -0
- package/dist/src/core/shaders/canvas/Shadow.d.ts +8 -0
- package/dist/src/core/shaders/canvas/Shadow.js +31 -0
- package/dist/src/core/shaders/canvas/Shadow.js.map +1 -0
- package/dist/src/core/shaders/canvas/utils/render.d.ts +5 -0
- package/dist/src/core/shaders/canvas/utils/render.js +84 -0
- package/dist/src/core/shaders/canvas/utils/render.js.map +1 -0
- package/dist/src/core/shaders/templates/BorderTemplate.d.ts +37 -0
- package/dist/src/core/shaders/templates/BorderTemplate.js +73 -0
- package/dist/src/core/shaders/templates/BorderTemplate.js.map +1 -0
- package/dist/src/core/shaders/templates/HolePunchTemplate.d.ts +46 -0
- package/dist/src/core/shaders/templates/HolePunchTemplate.js +35 -0
- package/dist/src/core/shaders/templates/HolePunchTemplate.js.map +1 -0
- package/dist/src/core/shaders/templates/LinearGradientTemplate.d.ts +23 -0
- package/dist/src/core/shaders/templates/LinearGradientTemplate.js +47 -0
- package/dist/src/core/shaders/templates/LinearGradientTemplate.js.map +1 -0
- package/dist/src/core/shaders/templates/RadialGradientTemplate.d.ts +31 -0
- package/dist/src/core/shaders/templates/RadialGradientTemplate.js +49 -0
- package/dist/src/core/shaders/templates/RadialGradientTemplate.js.map +1 -0
- package/dist/src/core/shaders/templates/RoundedTemplate.d.ts +29 -0
- package/dist/src/core/shaders/templates/RoundedTemplate.js +67 -0
- package/dist/src/core/shaders/templates/RoundedTemplate.js.map +1 -0
- package/dist/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.d.ts +7 -0
- package/dist/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.js +24 -0
- package/dist/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.js.map +1 -0
- package/dist/src/core/shaders/templates/RoundedWithBorderTemplate.d.ts +6 -0
- package/dist/src/core/shaders/templates/RoundedWithBorderTemplate.js +23 -0
- package/dist/src/core/shaders/templates/RoundedWithBorderTemplate.js.map +1 -0
- package/dist/src/core/shaders/templates/RoundedWithShadowTemplate.d.ts +6 -0
- package/dist/src/core/shaders/templates/RoundedWithShadowTemplate.js +23 -0
- package/dist/src/core/shaders/templates/RoundedWithShadowTemplate.js.map +1 -0
- package/dist/src/core/shaders/templates/ShadowTemplate.d.ts +34 -0
- package/dist/src/core/shaders/templates/ShadowTemplate.js +66 -0
- package/dist/src/core/shaders/templates/ShadowTemplate.js.map +1 -0
- package/dist/src/core/shaders/utils.d.ts +5 -0
- package/dist/src/core/shaders/utils.js +41 -0
- package/dist/src/core/shaders/utils.js.map +1 -0
- package/dist/src/core/shaders/webgl/Border.d.ts +3 -0
- package/dist/src/core/shaders/webgl/Border.js +110 -0
- package/dist/src/core/shaders/webgl/Border.js.map +1 -0
- package/dist/src/core/shaders/webgl/Default.d.ts +2 -0
- package/dist/src/core/shaders/webgl/Default.js +86 -0
- package/dist/src/core/shaders/webgl/Default.js.map +1 -0
- package/dist/src/core/shaders/webgl/DefaultBatched.d.ts +2 -0
- package/dist/src/core/shaders/webgl/DefaultBatched.js +104 -0
- package/dist/src/core/shaders/webgl/DefaultBatched.js.map +1 -0
- package/dist/src/core/shaders/webgl/HolePunch.d.ts +3 -0
- package/dist/src/core/shaders/webgl/HolePunch.js +65 -0
- package/dist/src/core/shaders/webgl/HolePunch.js.map +1 -0
- package/dist/src/core/shaders/webgl/LinearGradient.d.ts +3 -0
- package/dist/src/core/shaders/webgl/LinearGradient.js +76 -0
- package/dist/src/core/shaders/webgl/LinearGradient.js.map +1 -0
- package/dist/src/core/shaders/webgl/RadialGradient.d.ts +3 -0
- package/dist/src/core/shaders/webgl/RadialGradient.js +74 -0
- package/dist/src/core/shaders/webgl/RadialGradient.js.map +1 -0
- package/dist/src/core/shaders/webgl/Rounded.d.ts +7 -0
- package/dist/src/core/shaders/webgl/Rounded.js +86 -0
- package/dist/src/core/shaders/webgl/Rounded.js.map +1 -0
- package/dist/src/core/shaders/webgl/RoundedWithBorder.d.ts +3 -0
- package/dist/src/core/shaders/webgl/RoundedWithBorder.js +140 -0
- package/dist/src/core/shaders/webgl/RoundedWithBorder.js.map +1 -0
- package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.d.ts +3 -0
- package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +162 -0
- package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js.map +1 -0
- package/dist/src/core/shaders/webgl/RoundedWithShadow.d.ts +3 -0
- package/dist/src/core/shaders/webgl/RoundedWithShadow.js +84 -0
- package/dist/src/core/shaders/webgl/RoundedWithShadow.js.map +1 -0
- package/dist/src/core/shaders/webgl/SdfShader.d.ts +30 -0
- package/dist/src/core/shaders/webgl/SdfShader.js +107 -0
- package/dist/src/core/shaders/webgl/SdfShader.js.map +1 -0
- package/dist/src/core/shaders/webgl/Shadow.d.ts +3 -0
- package/dist/src/core/shaders/webgl/Shadow.js +110 -0
- package/dist/src/core/shaders/webgl/Shadow.js.map +1 -0
- package/dist/src/core/text-rendering/CanvasFontHandler.d.ts +59 -0
- package/dist/src/core/text-rendering/CanvasFontHandler.js +221 -0
- package/dist/src/core/text-rendering/CanvasFontHandler.js.map +1 -0
- package/dist/src/core/text-rendering/CanvasTextRenderer.d.ts +17 -0
- package/dist/src/core/text-rendering/CanvasTextRenderer.js +157 -0
- package/dist/src/core/text-rendering/CanvasTextRenderer.js.map +1 -0
- package/dist/src/core/text-rendering/SdfFontHandler.d.ts +182 -0
- package/dist/src/core/text-rendering/SdfFontHandler.js +388 -0
- package/dist/src/core/text-rendering/SdfFontHandler.js.map +1 -0
- package/dist/src/core/text-rendering/SdfTextRenderer.d.ts +17 -0
- package/dist/src/core/text-rendering/SdfTextRenderer.js +311 -0
- package/dist/src/core/text-rendering/SdfTextRenderer.js.map +1 -0
- package/dist/src/core/text-rendering/TextLayoutEngine.d.ts +19 -0
- package/dist/src/core/text-rendering/TextLayoutEngine.js +324 -0
- package/dist/src/core/text-rendering/TextLayoutEngine.js.map +1 -0
- package/dist/src/core/text-rendering/TextRenderer.d.ts +368 -0
- package/dist/src/core/text-rendering/TextRenderer.js +20 -0
- package/dist/src/core/text-rendering/TextRenderer.js.map +1 -0
- package/dist/src/core/text-rendering/Utils.d.ts +30 -0
- package/dist/src/core/text-rendering/Utils.js +84 -0
- package/dist/src/core/text-rendering/Utils.js.map +1 -0
- package/dist/src/core/textures/ImageTexture.js +3 -2
- package/dist/src/core/textures/ImageTexture.js.map +1 -1
- package/dist/src/core/textures/SubTexture.js +2 -0
- package/dist/src/core/textures/SubTexture.js.map +1 -1
- package/dist/src/core/textures/Texture.d.ts +2 -1
- package/dist/src/core/textures/Texture.js +2 -1
- package/dist/src/core/textures/Texture.js.map +1 -1
- package/dist/src/core/utils.d.ts +1 -1
- package/dist/src/main-api/Inspector.js +1 -1
- package/dist/src/main-api/Inspector.js.map +1 -1
- package/dist/src/main-api/Renderer.d.ts +13 -0
- package/dist/src/main-api/Renderer.js +2 -0
- package/dist/src/main-api/Renderer.js.map +1 -1
- package/dist/tsconfig.dist.tsbuildinfo +1 -1
- package/exports/index.ts +5 -0
- package/package.json +1 -1
- package/src/common/CommonTypes.ts +2 -1
- package/src/core/CoreNode.ts +10 -4
- package/src/core/CoreTextureManager.ts +19 -4
- package/src/core/Stage.ts +3 -0
- package/src/core/TextureError.ts +46 -0
- package/src/core/TextureMemoryManager.ts +18 -52
- package/src/core/platform.ts +0 -4
- package/src/core/textures/ImageTexture.ts +3 -2
- package/src/core/textures/SubTexture.ts +2 -0
- package/src/core/textures/Texture.ts +5 -3
- package/src/main-api/Inspector.ts +4 -1
- package/src/main-api/Renderer.ts +16 -0
- package/dist/src/core/renderers/webgl/WebGlCoreShader.destroy.d.ts +0 -1
- package/dist/src/core/renderers/webgl/WebGlCoreShader.destroy.js +0 -2
- package/dist/src/core/renderers/webgl/WebGlCoreShader.destroy.js.map +0 -1
|
@@ -0,0 +1,311 @@
|
|
|
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 { hasZeroWidthSpace } from './Utils.js';
|
|
20
|
+
import * as SdfFontHandler from './SdfFontHandler.js';
|
|
21
|
+
import { WebGlRenderer } from '../renderers/webgl/WebGlRenderer.js';
|
|
22
|
+
import { WebGlRenderOp } from '../renderers/webgl/WebGlRenderOp.js';
|
|
23
|
+
import { Sdf } from '../shaders/webgl/SdfShader.js';
|
|
24
|
+
import { BufferCollection } from '../renderers/webgl/internal/BufferCollection.js';
|
|
25
|
+
import { mergeColorAlpha } from '../../utils.js';
|
|
26
|
+
import { mapTextLayout } from './TextLayoutEngine.js';
|
|
27
|
+
// Each glyph requires 6 vertices (2 triangles) with 4 floats each (x, y, u, v)
|
|
28
|
+
const FLOATS_PER_VERTEX = 4;
|
|
29
|
+
const VERTICES_PER_GLYPH = 6;
|
|
30
|
+
// Type definition to match interface
|
|
31
|
+
const type = 'sdf';
|
|
32
|
+
let sdfShader = null;
|
|
33
|
+
// Initialize the SDF text renderer
|
|
34
|
+
const init = (stage) => {
|
|
35
|
+
SdfFontHandler.init();
|
|
36
|
+
// Register SDF shader with the shader manager
|
|
37
|
+
stage.shManager.registerShaderType('Sdf', Sdf);
|
|
38
|
+
sdfShader = stage.shManager.createShader('Sdf');
|
|
39
|
+
};
|
|
40
|
+
const font = SdfFontHandler;
|
|
41
|
+
/**
|
|
42
|
+
* SDF text renderer using MSDF/SDF fonts with WebGL
|
|
43
|
+
*
|
|
44
|
+
* @param stage - Stage instance for font resolution
|
|
45
|
+
* @param props - Text rendering properties
|
|
46
|
+
* @returns Object containing ImageData and dimensions
|
|
47
|
+
*/
|
|
48
|
+
const renderText = (props) => {
|
|
49
|
+
// Early return if no text
|
|
50
|
+
if (props.text.length === 0) {
|
|
51
|
+
return {
|
|
52
|
+
width: 0,
|
|
53
|
+
height: 0,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
// Get font cache for this font family
|
|
57
|
+
const fontData = SdfFontHandler.getFontData(props.fontFamily);
|
|
58
|
+
if (fontData === undefined) {
|
|
59
|
+
// Font not loaded, return empty result
|
|
60
|
+
return {
|
|
61
|
+
width: 0,
|
|
62
|
+
height: 0,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
// Calculate text layout and generate glyph data for caching
|
|
66
|
+
const layout = generateTextLayout(props, fontData);
|
|
67
|
+
// For SDF renderer, ImageData is null since we render via WebGL
|
|
68
|
+
return {
|
|
69
|
+
remainingLines: 0,
|
|
70
|
+
hasRemainingText: false,
|
|
71
|
+
width: layout.width,
|
|
72
|
+
height: layout.height,
|
|
73
|
+
layout, // Cache layout for addQuads
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Add quads for rendering using cached layout data
|
|
78
|
+
*/
|
|
79
|
+
const addQuads = (layout) => {
|
|
80
|
+
if (layout === undefined) {
|
|
81
|
+
return null; // No layout data available
|
|
82
|
+
}
|
|
83
|
+
const glyphs = layout.glyphs;
|
|
84
|
+
const glyphsLength = glyphs.length;
|
|
85
|
+
if (glyphsLength === 0) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
const vertexBuffer = new Float32Array(glyphsLength * VERTICES_PER_GLYPH * FLOATS_PER_VERTEX);
|
|
89
|
+
let bufferIndex = 0;
|
|
90
|
+
let glyphIndex = 0;
|
|
91
|
+
while (glyphIndex < glyphsLength) {
|
|
92
|
+
const glyph = glyphs[glyphIndex];
|
|
93
|
+
glyphIndex++;
|
|
94
|
+
if (glyph === undefined) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const x1 = glyph.x;
|
|
98
|
+
const y1 = glyph.y;
|
|
99
|
+
const x2 = x1 + glyph.width;
|
|
100
|
+
const y2 = y1 + glyph.height;
|
|
101
|
+
const u1 = glyph.atlasX;
|
|
102
|
+
const v1 = glyph.atlasY;
|
|
103
|
+
const u2 = u1 + glyph.atlasWidth;
|
|
104
|
+
const v2 = v1 + glyph.atlasHeight;
|
|
105
|
+
// Triangle 1: Top-left, top-right, bottom-left
|
|
106
|
+
// Vertex 1: Top-left
|
|
107
|
+
vertexBuffer[bufferIndex++] = x1;
|
|
108
|
+
vertexBuffer[bufferIndex++] = y1;
|
|
109
|
+
vertexBuffer[bufferIndex++] = u1;
|
|
110
|
+
vertexBuffer[bufferIndex++] = v1;
|
|
111
|
+
// Vertex 2: Top-right
|
|
112
|
+
vertexBuffer[bufferIndex++] = x2;
|
|
113
|
+
vertexBuffer[bufferIndex++] = y1;
|
|
114
|
+
vertexBuffer[bufferIndex++] = u2;
|
|
115
|
+
vertexBuffer[bufferIndex++] = v1;
|
|
116
|
+
// Vertex 3: Bottom-left
|
|
117
|
+
vertexBuffer[bufferIndex++] = x1;
|
|
118
|
+
vertexBuffer[bufferIndex++] = y2;
|
|
119
|
+
vertexBuffer[bufferIndex++] = u1;
|
|
120
|
+
vertexBuffer[bufferIndex++] = v2;
|
|
121
|
+
// Triangle 2: Top-right, bottom-right, bottom-left
|
|
122
|
+
// Vertex 4: Top-right (duplicate)
|
|
123
|
+
vertexBuffer[bufferIndex++] = x2;
|
|
124
|
+
vertexBuffer[bufferIndex++] = y1;
|
|
125
|
+
vertexBuffer[bufferIndex++] = u2;
|
|
126
|
+
vertexBuffer[bufferIndex++] = v1;
|
|
127
|
+
// Vertex 5: Bottom-right
|
|
128
|
+
vertexBuffer[bufferIndex++] = x2;
|
|
129
|
+
vertexBuffer[bufferIndex++] = y2;
|
|
130
|
+
vertexBuffer[bufferIndex++] = u2;
|
|
131
|
+
vertexBuffer[bufferIndex++] = v2;
|
|
132
|
+
// Vertex 6: Bottom-left (duplicate)
|
|
133
|
+
vertexBuffer[bufferIndex++] = x1;
|
|
134
|
+
vertexBuffer[bufferIndex++] = y2;
|
|
135
|
+
vertexBuffer[bufferIndex++] = u1;
|
|
136
|
+
vertexBuffer[bufferIndex++] = v2;
|
|
137
|
+
}
|
|
138
|
+
return vertexBuffer;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Create and submit WebGL render operations for SDF text
|
|
142
|
+
* This is called from CoreTextNode during rendering to add SDF text to the render pipeline
|
|
143
|
+
*/
|
|
144
|
+
const renderQuads = (renderer, layout, vertexBuffer, renderProps) => {
|
|
145
|
+
const fontFamily = renderProps.fontFamily;
|
|
146
|
+
const color = renderProps.color;
|
|
147
|
+
const worldAlpha = renderProps.worldAlpha;
|
|
148
|
+
const globalTransform = renderProps.globalTransform;
|
|
149
|
+
const atlasTexture = SdfFontHandler.getAtlas(fontFamily);
|
|
150
|
+
if (atlasTexture === null) {
|
|
151
|
+
console.warn(`SDF atlas texture not found for font: ${fontFamily}`);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
// We can safely assume this is a WebGL renderer else this wouldn't be called
|
|
155
|
+
const glw = renderer.glw;
|
|
156
|
+
const stride = 4 * Float32Array.BYTES_PER_ELEMENT;
|
|
157
|
+
const webGlBuffer = glw.createBuffer();
|
|
158
|
+
if (!webGlBuffer) {
|
|
159
|
+
console.warn('Failed to create WebGL buffer for SDF text');
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const webGlBuffers = new BufferCollection([
|
|
163
|
+
{
|
|
164
|
+
buffer: webGlBuffer,
|
|
165
|
+
attributes: {
|
|
166
|
+
a_position: {
|
|
167
|
+
name: 'a_position',
|
|
168
|
+
size: 2,
|
|
169
|
+
type: glw.FLOAT,
|
|
170
|
+
normalized: false,
|
|
171
|
+
stride,
|
|
172
|
+
offset: 0,
|
|
173
|
+
},
|
|
174
|
+
a_textureCoords: {
|
|
175
|
+
name: 'a_textureCoords',
|
|
176
|
+
size: 2,
|
|
177
|
+
type: glw.FLOAT,
|
|
178
|
+
normalized: false,
|
|
179
|
+
stride,
|
|
180
|
+
offset: 2 * Float32Array.BYTES_PER_ELEMENT,
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
]);
|
|
185
|
+
const buffer = webGlBuffers.getBuffer('a_position');
|
|
186
|
+
if (buffer !== undefined) {
|
|
187
|
+
glw.arrayBufferData(buffer, vertexBuffer, glw.STATIC_DRAW);
|
|
188
|
+
}
|
|
189
|
+
const renderOp = new WebGlRenderOp(renderer, {
|
|
190
|
+
sdfShaderProps: {
|
|
191
|
+
transform: globalTransform,
|
|
192
|
+
color: mergeColorAlpha(color, worldAlpha),
|
|
193
|
+
size: layout.fontScale, // Use proper font scaling in shader
|
|
194
|
+
distanceRange: layout.distanceRange,
|
|
195
|
+
},
|
|
196
|
+
sdfBuffers: webGlBuffers,
|
|
197
|
+
shader: sdfShader,
|
|
198
|
+
alpha: worldAlpha,
|
|
199
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment
|
|
200
|
+
clippingRect: renderProps.clippingRect,
|
|
201
|
+
height: layout.height,
|
|
202
|
+
width: layout.width,
|
|
203
|
+
rtt: false,
|
|
204
|
+
parentHasRenderTexture: renderProps.parentHasRenderTexture,
|
|
205
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment
|
|
206
|
+
framebufferDimensions: renderProps.framebufferDimensions,
|
|
207
|
+
}, 0);
|
|
208
|
+
// Add atlas texture and set quad count
|
|
209
|
+
renderOp.addTexture(atlasTexture.ctxTexture);
|
|
210
|
+
renderOp.numQuads = layout.glyphs.length;
|
|
211
|
+
renderer.addRenderOp(renderOp);
|
|
212
|
+
};
|
|
213
|
+
/**
|
|
214
|
+
* Generate complete text layout with glyph positioning for caching
|
|
215
|
+
*/
|
|
216
|
+
const generateTextLayout = (props, fontCache) => {
|
|
217
|
+
const fontSize = props.fontSize;
|
|
218
|
+
const fontFamily = props.fontFamily;
|
|
219
|
+
const lineHeight = props.lineHeight;
|
|
220
|
+
const metrics = SdfFontHandler.getFontMetrics(fontFamily, fontSize);
|
|
221
|
+
const verticalAlign = props.verticalAlign;
|
|
222
|
+
const fontData = fontCache.data;
|
|
223
|
+
const commonFontData = fontData.common;
|
|
224
|
+
const designFontSize = fontData.info.size;
|
|
225
|
+
const atlasWidth = commonFontData.scaleW;
|
|
226
|
+
const atlasHeight = commonFontData.scaleH;
|
|
227
|
+
// Calculate the pixel scale from design units to pixels
|
|
228
|
+
const fontScale = fontSize / designFontSize;
|
|
229
|
+
const letterSpacing = props.letterSpacing / fontScale;
|
|
230
|
+
const maxWidth = props.maxWidth / fontScale;
|
|
231
|
+
const maxHeight = props.maxHeight;
|
|
232
|
+
const [lines, remainingLines, hasRemainingText, bareLineHeight, lineHeightPx, effectiveWidth, effectiveHeight,] = mapTextLayout(SdfFontHandler.measureText, metrics, props.text, props.textAlign, verticalAlign, fontFamily, lineHeight, props.overflowSuffix, props.wordBreak, letterSpacing, props.maxLines, maxWidth, maxHeight);
|
|
233
|
+
const lineAmount = lines.length;
|
|
234
|
+
const glyphs = [];
|
|
235
|
+
let currentX = 0;
|
|
236
|
+
let currentY = 0;
|
|
237
|
+
for (let i = 0; i < lineAmount; i++) {
|
|
238
|
+
const line = lines[i];
|
|
239
|
+
const textLine = line[0];
|
|
240
|
+
const textLineLength = textLine.length;
|
|
241
|
+
let prevCodepoint = 0;
|
|
242
|
+
currentX = line[2];
|
|
243
|
+
//convert Y coord to vertex value
|
|
244
|
+
currentY = line[3] / fontScale;
|
|
245
|
+
for (let j = 0; j < textLineLength; j++) {
|
|
246
|
+
const char = textLine.charAt(j);
|
|
247
|
+
if (hasZeroWidthSpace(char) === true) {
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
const codepoint = char.codePointAt(0);
|
|
251
|
+
if (codepoint === undefined) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
// Get glyph data from font handler
|
|
255
|
+
const glyph = SdfFontHandler.getGlyph(fontFamily, codepoint);
|
|
256
|
+
if (glyph === null) {
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
// Calculate advance with kerning (in design units)
|
|
260
|
+
let advance = glyph.xadvance;
|
|
261
|
+
// Add kerning if there's a previous character
|
|
262
|
+
if (prevCodepoint !== 0) {
|
|
263
|
+
const kerning = SdfFontHandler.getKerning(fontFamily, prevCodepoint, codepoint);
|
|
264
|
+
advance += kerning;
|
|
265
|
+
}
|
|
266
|
+
// Calculate glyph position and atlas coordinates (in design units)
|
|
267
|
+
const glyphLayout = {
|
|
268
|
+
codepoint,
|
|
269
|
+
glyphId: glyph.id,
|
|
270
|
+
x: currentX + glyph.xoffset,
|
|
271
|
+
y: currentY + glyph.yoffset,
|
|
272
|
+
width: glyph.width,
|
|
273
|
+
height: glyph.height,
|
|
274
|
+
xOffset: glyph.xoffset,
|
|
275
|
+
yOffset: glyph.yoffset,
|
|
276
|
+
atlasX: glyph.x / atlasWidth,
|
|
277
|
+
atlasY: glyph.y / atlasHeight,
|
|
278
|
+
atlasWidth: glyph.width / atlasWidth,
|
|
279
|
+
atlasHeight: glyph.height / atlasHeight,
|
|
280
|
+
};
|
|
281
|
+
glyphs.push(glyphLayout);
|
|
282
|
+
// Advance position with letter spacing (in design units)
|
|
283
|
+
currentX += advance + letterSpacing;
|
|
284
|
+
prevCodepoint = codepoint;
|
|
285
|
+
}
|
|
286
|
+
currentY += lineHeightPx;
|
|
287
|
+
}
|
|
288
|
+
// Convert final dimensions to pixel space for the layout
|
|
289
|
+
return {
|
|
290
|
+
glyphs,
|
|
291
|
+
distanceRange: fontScale * fontData.distanceField.distanceRange,
|
|
292
|
+
width: maxWidth || effectiveWidth * fontScale,
|
|
293
|
+
height: maxHeight || effectiveHeight,
|
|
294
|
+
fontScale: fontScale,
|
|
295
|
+
lineHeight: lineHeightPx,
|
|
296
|
+
fontFamily,
|
|
297
|
+
};
|
|
298
|
+
};
|
|
299
|
+
/**
|
|
300
|
+
* SDF Text Renderer - implements TextRenderer interface
|
|
301
|
+
*/
|
|
302
|
+
const SdfTextRenderer = {
|
|
303
|
+
type,
|
|
304
|
+
font,
|
|
305
|
+
renderText,
|
|
306
|
+
addQuads,
|
|
307
|
+
renderQuads,
|
|
308
|
+
init,
|
|
309
|
+
};
|
|
310
|
+
export default SdfTextRenderer;
|
|
311
|
+
//# sourceMappingURL=SdfTextRenderer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SdfTextRenderer.js","sourceRoot":"","sources":["../../../../src/core/text-rendering/SdfTextRenderer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAUH,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,KAAK,cAAc,MAAM,qBAAqB,CAAC;AAEtD,OAAO,EAAE,aAAa,EAAE,MAAM,qCAAqC,CAAC;AACpE,OAAO,EAAE,aAAa,EAAE,MAAM,qCAAqC,CAAC;AACpE,OAAO,EAAE,GAAG,EAAuB,MAAM,+BAA+B,CAAC;AACzE,OAAO,EAAE,gBAAgB,EAAE,MAAM,iDAAiD,CAAC;AAGnF,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,+EAA+E;AAC/E,MAAM,iBAAiB,GAA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type { FontMetrics, MeasureTextFn, NormalizedFontMetrics, TextLayoutStruct, WrappedLinesStruct } from './TextRenderer.js';
|
|
2
|
+
export declare const defaultFontMetrics: FontMetrics;
|
|
3
|
+
export declare const normalizeFontMetrics: (metrics: FontMetrics, fontSize: number) => NormalizedFontMetrics;
|
|
4
|
+
export declare const mapTextLayout: (measureText: MeasureTextFn, metrics: NormalizedFontMetrics, text: string, textAlign: string, verticalAlign: string, fontFamily: string, lineHeight: number, overflowSuffix: string, wordBreak: string, letterSpacing: number, maxLines: number, maxWidth: number, maxHeight: number) => TextLayoutStruct;
|
|
5
|
+
export declare const measureLines: (measureText: MeasureTextFn, lines: string[], fontFamily: string, letterSpacing: number, maxLines: number) => WrappedLinesStruct;
|
|
6
|
+
export declare const wrapText: (measureText: MeasureTextFn, text: string, fontFamily: string, maxWidth: number, letterSpacing: number, overflowSuffix: string, wordBreak: string, maxLines: number) => WrappedLinesStruct;
|
|
7
|
+
export declare const wrapLine: (measureText: MeasureTextFn, line: string, fontFamily: string, maxWidth: number, letterSpacing: number, spaceWidth: number, overflowSuffix: string, wordBreak: string, remainingLines: number, hasMaxLines: boolean) => WrappedLinesStruct;
|
|
8
|
+
/**
|
|
9
|
+
* Truncate a line with overflow suffix to fit within width
|
|
10
|
+
*/
|
|
11
|
+
export declare const truncateLineWithSuffix: (measureText: MeasureTextFn, line: string, fontFamily: string, maxWidth: number, letterSpacing: number, overflowSuffix: string) => string;
|
|
12
|
+
/**
|
|
13
|
+
* wordbreak function: https://developer.mozilla.org/en-US/docs/Web/CSS/word-break#break-word
|
|
14
|
+
*/
|
|
15
|
+
export declare const breakWord: (measureText: MeasureTextFn, word: string, fontFamily: string, maxWidth: number, letterSpacing: number, remainingLines: number) => WrappedLinesStruct;
|
|
16
|
+
/**
|
|
17
|
+
* wordbreak function: https://developer.mozilla.org/en-US/docs/Web/CSS/word-break#break-word
|
|
18
|
+
*/
|
|
19
|
+
export declare const breakAll: (measureText: MeasureTextFn, word: string, fontFamily: string, initial: number, maxWidth: number, letterSpacing: number, remainingLines: number) => WrappedLinesStruct;
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
export const defaultFontMetrics = {
|
|
2
|
+
ascender: 800,
|
|
3
|
+
descender: -200,
|
|
4
|
+
lineGap: 200,
|
|
5
|
+
unitsPerEm: 1000,
|
|
6
|
+
};
|
|
7
|
+
export const normalizeFontMetrics = (metrics, fontSize) => {
|
|
8
|
+
const scale = fontSize / metrics.unitsPerEm;
|
|
9
|
+
return {
|
|
10
|
+
ascender: metrics.ascender * scale,
|
|
11
|
+
descender: metrics.descender * scale,
|
|
12
|
+
lineGap: metrics.lineGap * scale,
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export const mapTextLayout = (measureText, metrics, text, textAlign, verticalAlign, fontFamily, lineHeight, overflowSuffix, wordBreak, letterSpacing, maxLines, maxWidth, maxHeight) => {
|
|
16
|
+
const ascPx = metrics.ascender;
|
|
17
|
+
const descPx = metrics.descender;
|
|
18
|
+
const bareLineHeight = ascPx - descPx;
|
|
19
|
+
const lineHeightPx = lineHeight <= 3 ? lineHeight * bareLineHeight : lineHeight;
|
|
20
|
+
const lineHeightDelta = lineHeightPx - bareLineHeight;
|
|
21
|
+
const halfDelta = lineHeightDelta * 0.5;
|
|
22
|
+
let effectiveMaxLines = maxLines;
|
|
23
|
+
if (maxHeight > 0) {
|
|
24
|
+
const maxFromHeight = Math.floor(maxHeight / lineHeightPx);
|
|
25
|
+
if (effectiveMaxLines === 0 || maxFromHeight < effectiveMaxLines) {
|
|
26
|
+
effectiveMaxLines = maxFromHeight;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const wrappedText = maxWidth > 0;
|
|
30
|
+
//wrapText or just measureLines based on maxWidth
|
|
31
|
+
const [lines, remainingLines, remainingText] = wrappedText === true
|
|
32
|
+
? wrapText(measureText, text, fontFamily, maxWidth, letterSpacing, overflowSuffix, wordBreak, maxLines)
|
|
33
|
+
: measureLines(measureText, text.split('\n'), fontFamily, letterSpacing, maxLines);
|
|
34
|
+
let effectiveLineAmount = lines.length;
|
|
35
|
+
let effectiveMaxWidth = lines[0][1];
|
|
36
|
+
//check for longest line
|
|
37
|
+
if (effectiveLineAmount > 1) {
|
|
38
|
+
for (let i = 1; i < effectiveLineAmount; i++) {
|
|
39
|
+
effectiveMaxWidth = Math.max(effectiveMaxWidth, lines[i][1]);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
//update line x offsets
|
|
43
|
+
if (textAlign !== 'left') {
|
|
44
|
+
const maxW = wrappedText === true ? maxWidth : effectiveMaxWidth;
|
|
45
|
+
for (let i = 0; i < effectiveLineAmount; i++) {
|
|
46
|
+
const line = lines[i];
|
|
47
|
+
const w = line[1];
|
|
48
|
+
line[2] = textAlign === 'right' ? maxW - w : (maxW - w) / 2;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const effectiveMaxHeight = effectiveLineAmount * lineHeightPx;
|
|
52
|
+
let firstBaseLine = halfDelta;
|
|
53
|
+
if (maxHeight > 0 && verticalAlign !== 'top') {
|
|
54
|
+
if (verticalAlign === 'middle') {
|
|
55
|
+
firstBaseLine += (maxHeight - effectiveMaxHeight) / 2;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
firstBaseLine += maxHeight - effectiveMaxHeight;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const startY = firstBaseLine;
|
|
62
|
+
for (let i = 0; i < effectiveLineAmount; i++) {
|
|
63
|
+
const line = lines[i];
|
|
64
|
+
line[3] = startY + lineHeightPx * i;
|
|
65
|
+
}
|
|
66
|
+
return [
|
|
67
|
+
lines,
|
|
68
|
+
remainingLines,
|
|
69
|
+
remainingText,
|
|
70
|
+
bareLineHeight,
|
|
71
|
+
lineHeightPx,
|
|
72
|
+
effectiveMaxWidth,
|
|
73
|
+
effectiveMaxHeight,
|
|
74
|
+
];
|
|
75
|
+
};
|
|
76
|
+
export const measureLines = (measureText, lines, fontFamily, letterSpacing, maxLines) => {
|
|
77
|
+
const measuredLines = [];
|
|
78
|
+
let remainingLines = maxLines > 0 ? maxLines : lines.length;
|
|
79
|
+
let i = 0;
|
|
80
|
+
while (remainingLines > 0) {
|
|
81
|
+
const line = lines[i];
|
|
82
|
+
i++;
|
|
83
|
+
remainingLines--;
|
|
84
|
+
if (line === undefined) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const width = measureText(line, fontFamily, letterSpacing);
|
|
88
|
+
measuredLines.push([line, width, 0, 0]);
|
|
89
|
+
}
|
|
90
|
+
return [
|
|
91
|
+
measuredLines,
|
|
92
|
+
remainingLines,
|
|
93
|
+
maxLines > 0 ? lines.length - measuredLines.length > 0 : false,
|
|
94
|
+
];
|
|
95
|
+
};
|
|
96
|
+
export const wrapText = (measureText, text, fontFamily, maxWidth, letterSpacing, overflowSuffix, wordBreak, maxLines) => {
|
|
97
|
+
const lines = text.split('\n');
|
|
98
|
+
const wrappedLines = [];
|
|
99
|
+
// Calculate space width for line wrapping
|
|
100
|
+
const spaceWidth = measureText(' ', fontFamily, letterSpacing);
|
|
101
|
+
let wrappedLine = [];
|
|
102
|
+
let remainingLines = maxLines;
|
|
103
|
+
let hasRemainingText = true;
|
|
104
|
+
let hasMaxLines = maxLines > 0;
|
|
105
|
+
for (let i = 0; i < lines.length; i++) {
|
|
106
|
+
const line = lines[i];
|
|
107
|
+
if (line === undefined) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
[wrappedLine, remainingLines, hasRemainingText] =
|
|
111
|
+
line.length > 0
|
|
112
|
+
? wrapLine(measureText, line, fontFamily, maxWidth, letterSpacing, spaceWidth, overflowSuffix, wordBreak, remainingLines, hasMaxLines)
|
|
113
|
+
: [[['', 0, 0, 0]], remainingLines, i < lines.length - 1];
|
|
114
|
+
remainingLines--;
|
|
115
|
+
wrappedLines.push(...wrappedLine);
|
|
116
|
+
if (hasMaxLines === true && remainingLines <= 0) {
|
|
117
|
+
const lastLine = wrappedLines[wrappedLines.length - 1];
|
|
118
|
+
if (i < lines.length - 1) {
|
|
119
|
+
if (lastLine[0].endsWith(overflowSuffix) === false) {
|
|
120
|
+
lastLine[0] = truncateLineWithSuffix(measureText, lastLine[0], fontFamily, maxWidth, letterSpacing, overflowSuffix);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return [wrappedLines, remainingLines, hasRemainingText];
|
|
127
|
+
};
|
|
128
|
+
export const wrapLine = (measureText, line, fontFamily, maxWidth, letterSpacing, spaceWidth, overflowSuffix, wordBreak, remainingLines, hasMaxLines) => {
|
|
129
|
+
// Use the same space regex as Canvas renderer to handle ZWSP
|
|
130
|
+
const spaceRegex = / |\u200B/g;
|
|
131
|
+
const words = line.split(spaceRegex);
|
|
132
|
+
const spaces = line.match(spaceRegex) || [];
|
|
133
|
+
const wrappedLines = [];
|
|
134
|
+
let currentLine = '';
|
|
135
|
+
let currentLineWidth = 0;
|
|
136
|
+
let hasRemainingText = true;
|
|
137
|
+
let i = 0;
|
|
138
|
+
for (; i < words.length; i++) {
|
|
139
|
+
const word = words[i];
|
|
140
|
+
if (word === undefined) {
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
const space = spaces[i - 1] || '';
|
|
144
|
+
const wordWidth = measureText(word, fontFamily, letterSpacing);
|
|
145
|
+
// For width calculation, treat ZWSP as having 0 width but regular space functionality
|
|
146
|
+
const effectiveSpaceWidth = space === '\u200B' ? 0 : spaceWidth;
|
|
147
|
+
const totalWidth = currentLineWidth + effectiveSpaceWidth + wordWidth;
|
|
148
|
+
if ((i === 0 && wordWidth <= maxWidth) ||
|
|
149
|
+
(i > 0 && totalWidth <= maxWidth)) {
|
|
150
|
+
// Word fits on current line
|
|
151
|
+
if (currentLine.length > 0) {
|
|
152
|
+
// Add space - for ZWSP, don't add anything to output (it's invisible)
|
|
153
|
+
if (space !== '\u200B') {
|
|
154
|
+
currentLine += space;
|
|
155
|
+
currentLineWidth += effectiveSpaceWidth;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
currentLine += word;
|
|
159
|
+
currentLineWidth += wordWidth;
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
if (remainingLines === 1) {
|
|
163
|
+
if (currentLine.length > 0) {
|
|
164
|
+
// Add space - for ZWSP, don't add anything to output (it's invisible)
|
|
165
|
+
if (space !== '\u200B') {
|
|
166
|
+
currentLine += space;
|
|
167
|
+
currentLineWidth += effectiveSpaceWidth;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
currentLine += word;
|
|
171
|
+
currentLineWidth += wordWidth;
|
|
172
|
+
remainingLines = 0;
|
|
173
|
+
hasRemainingText = i < words.length;
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
if (wordBreak !== 'break-all' && currentLine.length > 0) {
|
|
177
|
+
wrappedLines.push([currentLine, currentLineWidth, 0, 0]);
|
|
178
|
+
}
|
|
179
|
+
if (wordBreak !== 'break-all') {
|
|
180
|
+
remainingLines--;
|
|
181
|
+
currentLine = word;
|
|
182
|
+
currentLineWidth = wordWidth;
|
|
183
|
+
}
|
|
184
|
+
if (wordBreak === 'break-word') {
|
|
185
|
+
const [lines, rl, rt] = breakWord(measureText, word, fontFamily, maxWidth, letterSpacing, remainingLines);
|
|
186
|
+
remainingLines = rl;
|
|
187
|
+
hasRemainingText = rt;
|
|
188
|
+
if (lines.length === 1) {
|
|
189
|
+
[currentLine, currentLineWidth] = lines[lines.length - 1];
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
for (let j = 0; j < lines.length; j++) {
|
|
193
|
+
[currentLine, currentLineWidth] = lines[j];
|
|
194
|
+
if (j < lines.length - 1) {
|
|
195
|
+
wrappedLines.push(lines[j]);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
else if (wordBreak === 'break-all') {
|
|
201
|
+
const firstLetterWidth = measureText(word.charAt(0), fontFamily, letterSpacing);
|
|
202
|
+
let linebreak = false;
|
|
203
|
+
if (currentLineWidth + firstLetterWidth + effectiveSpaceWidth >
|
|
204
|
+
maxWidth) {
|
|
205
|
+
wrappedLines.push([currentLine, currentLineWidth, 0, 0]);
|
|
206
|
+
remainingLines -= 1;
|
|
207
|
+
currentLine = '';
|
|
208
|
+
currentLineWidth = 0;
|
|
209
|
+
linebreak = true;
|
|
210
|
+
}
|
|
211
|
+
const initial = maxWidth - currentLineWidth;
|
|
212
|
+
const [lines, rl, rt] = breakAll(measureText, word, fontFamily, initial, maxWidth, letterSpacing, remainingLines);
|
|
213
|
+
remainingLines = rl;
|
|
214
|
+
hasRemainingText = rt;
|
|
215
|
+
if (linebreak === false) {
|
|
216
|
+
const [text, width] = lines[0];
|
|
217
|
+
currentLine += ' ' + text;
|
|
218
|
+
currentLineWidth = width;
|
|
219
|
+
wrappedLines.push([currentLine, currentLineWidth, 0, 0]);
|
|
220
|
+
}
|
|
221
|
+
for (let j = 1; j < lines.length; j++) {
|
|
222
|
+
[currentLine, currentLineWidth] = lines[j];
|
|
223
|
+
if (j < lines.length - 1) {
|
|
224
|
+
wrappedLines.push([currentLine, currentLineWidth, 0, 0]);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
// Add the last line if it has content
|
|
231
|
+
if (currentLine.length > 0 && hasMaxLines === true && remainingLines === 0) {
|
|
232
|
+
currentLine = truncateLineWithSuffix(measureText, currentLine, fontFamily, maxWidth, letterSpacing, overflowSuffix);
|
|
233
|
+
}
|
|
234
|
+
if (currentLine.length > 0) {
|
|
235
|
+
wrappedLines.push([currentLine, currentLineWidth, 0, 0]);
|
|
236
|
+
}
|
|
237
|
+
return [wrappedLines, remainingLines, hasRemainingText];
|
|
238
|
+
};
|
|
239
|
+
/**
|
|
240
|
+
* Truncate a line with overflow suffix to fit within width
|
|
241
|
+
*/
|
|
242
|
+
export const truncateLineWithSuffix = (measureText, line, fontFamily, maxWidth, letterSpacing, overflowSuffix) => {
|
|
243
|
+
const suffixWidth = measureText(overflowSuffix, fontFamily, letterSpacing);
|
|
244
|
+
if (suffixWidth >= maxWidth) {
|
|
245
|
+
return overflowSuffix.substring(0, Math.max(1, overflowSuffix.length - 1));
|
|
246
|
+
}
|
|
247
|
+
let truncatedLine = line;
|
|
248
|
+
while (truncatedLine.length > 0) {
|
|
249
|
+
const lineWidth = measureText(truncatedLine, fontFamily, letterSpacing);
|
|
250
|
+
if (lineWidth + suffixWidth <= maxWidth) {
|
|
251
|
+
return truncatedLine + overflowSuffix;
|
|
252
|
+
}
|
|
253
|
+
truncatedLine = truncatedLine.substring(0, truncatedLine.length - 1);
|
|
254
|
+
}
|
|
255
|
+
return overflowSuffix;
|
|
256
|
+
};
|
|
257
|
+
/**
|
|
258
|
+
* wordbreak function: https://developer.mozilla.org/en-US/docs/Web/CSS/word-break#break-word
|
|
259
|
+
*/
|
|
260
|
+
export const breakWord = (measureText, word, fontFamily, maxWidth, letterSpacing, remainingLines) => {
|
|
261
|
+
const lines = [];
|
|
262
|
+
let currentPart = '';
|
|
263
|
+
let currentWidth = 0;
|
|
264
|
+
let i = 0;
|
|
265
|
+
for (let i = 0; i < word.length; i++) {
|
|
266
|
+
const char = word.charAt(i);
|
|
267
|
+
const codepoint = char.codePointAt(0);
|
|
268
|
+
if (codepoint === undefined)
|
|
269
|
+
continue;
|
|
270
|
+
const charWidth = measureText(char, fontFamily, letterSpacing);
|
|
271
|
+
if (currentWidth + charWidth > maxWidth && currentPart.length > 0) {
|
|
272
|
+
remainingLines--;
|
|
273
|
+
if (remainingLines === 0) {
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
lines.push([currentPart, currentWidth, 0, 0]);
|
|
277
|
+
currentPart = char;
|
|
278
|
+
currentWidth = charWidth;
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
currentPart += char;
|
|
282
|
+
currentWidth += charWidth;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (currentPart.length > 0) {
|
|
286
|
+
lines.push([currentPart, currentWidth, 0, 0]);
|
|
287
|
+
}
|
|
288
|
+
return [lines, remainingLines, i < word.length - 1];
|
|
289
|
+
};
|
|
290
|
+
/**
|
|
291
|
+
* wordbreak function: https://developer.mozilla.org/en-US/docs/Web/CSS/word-break#break-word
|
|
292
|
+
*/
|
|
293
|
+
export const breakAll = (measureText, word, fontFamily, initial, maxWidth, letterSpacing, remainingLines) => {
|
|
294
|
+
const lines = [];
|
|
295
|
+
let currentPart = '';
|
|
296
|
+
let currentWidth = 0;
|
|
297
|
+
let max = initial;
|
|
298
|
+
let i = 0;
|
|
299
|
+
let hasRemainingText = false;
|
|
300
|
+
for (; i < word.length; i++) {
|
|
301
|
+
if (remainingLines === 0) {
|
|
302
|
+
hasRemainingText = true;
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
const char = word.charAt(i);
|
|
306
|
+
const charWidth = measureText(char, fontFamily, letterSpacing);
|
|
307
|
+
if (currentWidth + charWidth > max && currentPart.length > 0) {
|
|
308
|
+
lines.push([currentPart, currentWidth, 0, 0]);
|
|
309
|
+
currentPart = char;
|
|
310
|
+
currentWidth = charWidth;
|
|
311
|
+
max = maxWidth;
|
|
312
|
+
remainingLines--;
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
currentPart += char;
|
|
316
|
+
currentWidth += charWidth;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (currentPart.length > 0) {
|
|
320
|
+
lines.push([currentPart, currentWidth, 0, 0]);
|
|
321
|
+
}
|
|
322
|
+
return [lines, remainingLines, hasRemainingText];
|
|
323
|
+
};
|
|
324
|
+
//# sourceMappingURL=TextLayoutEngine.js.map
|