-
Notifications
You must be signed in to change notification settings - Fork 24
/
layoutText.ts
403 lines (364 loc) · 13.8 KB
/
layoutText.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2023 Comcast Cable Communications Management, LLC.
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { assertTruthy } from '../../../../../utils.js';
import type { Bound } from '../../../../lib/utils.js';
import type {
FontShaperProps,
MappedGlyphInfo,
UnmappedCharacterInfo,
} from '../../../font-face-types/SdfTrFontFace/internal/FontShaper.js';
import type { TrProps, TextRendererState } from '../../TextRenderer.js';
import type { SdfTextRendererState } from '../SdfTextRenderer.js';
import { PeekableIterator } from './PeekableGenerator.js';
import { getUnicodeCodepoints } from './getUnicodeCodepoints.js';
import { measureText } from './measureText.js';
export function layoutText(
curLineIndex: number,
startX: number,
startY: number,
text: TrProps['text'],
textAlign: TrProps['textAlign'],
width: TrProps['width'],
height: TrProps['height'],
fontSize: TrProps['fontSize'],
lineHeight: number,
letterSpacing: TrProps['letterSpacing'],
/**
* Mutated
*/
vertexBuffer: NonNullable<SdfTextRendererState['vertexBuffer']>,
contain: TrProps['contain'],
/**
* Mutated
*/
lineCache: SdfTextRendererState['lineCache'],
rwSdf: Bound,
trFontFace: SdfTextRendererState['trFontFace'],
forceFullLayoutCalc: TextRendererState['forceFullLayoutCalc'],
scrollable: TrProps['scrollable'],
overflowSuffix: TrProps['overflowSuffix'],
maxLines: TrProps['maxLines'],
): {
bufferNumFloats: number;
bufferNumQuads: number;
layoutNumCharacters: number;
fullyProcessed: boolean;
maxX: number;
maxY: number;
numLines: number;
} {
assertTruthy(trFontFace, 'Font face must be loaded');
assertTruthy(trFontFace.loaded, 'Font face must be loaded');
assertTruthy(trFontFace.data, 'Font face must be loaded');
assertTruthy(trFontFace.shaper, 'Font face must be loaded');
// Regardless of fontSize (or other scaling properties), we layout the vertices of each glyph
// using the fixed coordinate space determined by font size used to produce the atlas.
// Scaling for display is handled by shader uniforms inexpensively.
// So we have:
// - vertex space: the space in which the vertices of each glyph are laid out
// - screen space: the screen pixel space
// Input properties such as x, y, w, fontSize, letterSpacing, etc. are all expressed in screen space.
// We convert these to the vertex space by dividing them the `fontSizeRatio` factor.
/**
* See above
*/
const fontSizeRatio = fontSize / trFontFace.data.info.size;
/**
* `lineHeight` in vertex coordinates
*/
const vertexLineHeight = lineHeight / fontSizeRatio;
/**
* `w` in vertex coordinates
*/
const vertexW = width / fontSizeRatio;
/**
* `letterSpacing` in vertex coordinates
*/
const vertexLSpacing = letterSpacing / fontSizeRatio;
const startingLineCacheEntry = lineCache[curLineIndex];
const startingCodepointIndex = startingLineCacheEntry?.codepointIndex || 0;
const startingMaxX = startingLineCacheEntry?.maxX || 0;
const startingMaxY = startingLineCacheEntry?.maxY || 0;
let maxX = startingMaxX;
let maxY = startingMaxY;
let curX = startX;
let curY = startY;
let bufferOffset = 0;
/**
* Buffer offset to last word boundry. This is -1 when we aren't in a word boundry.
*/
const lastWord: {
codepointIndex: number;
bufferOffset: number;
xStart: number;
} = {
codepointIndex: -1,
bufferOffset: -1,
xStart: -1,
};
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const shaper = trFontFace.shaper;
const shaperProps: FontShaperProps = {
letterSpacing: vertexLSpacing,
};
// Get glyphs
let glyphs = shaper.shapeText(
shaperProps,
new PeekableIterator(
getUnicodeCodepoints(text, startingCodepointIndex),
startingCodepointIndex,
),
);
let glyphResult:
| IteratorResult<MappedGlyphInfo | UnmappedCharacterInfo, void>
| undefined;
let curLineBufferStart = -1;
const bufferLineInfos: {
bufferStart: number;
bufferEnd: number;
}[] = [];
const vertexTruncateHeight = height / fontSizeRatio;
const overflowSuffVertexWidth = measureText(
overflowSuffix,
shaperProps,
shaper,
);
// Line-by-line layout
let moreLines = true;
while (moreLines) {
const nextLineWillFit =
(maxLines === 0 || curLineIndex + 1 < maxLines) &&
(contain !== 'both' ||
scrollable ||
curY + vertexLineHeight + trFontFace.maxCharHeight <=
vertexTruncateHeight);
const lineVertexW = nextLineWillFit
? vertexW
: vertexW - overflowSuffVertexWidth;
/**
* Vertex X position to the beginning of the last word boundary. This becomes -1 when we start traversing a word.
*/
let xStartLastWordBoundary = 0;
const lineIsBelowWindowTop = curY + trFontFace.maxCharHeight >= rwSdf.y1;
const lineIsAboveWindowBottom = curY <= rwSdf.y2;
const lineIsWithinWindow = lineIsBelowWindowTop && lineIsAboveWindowBottom;
// Layout glyphs in this line
// Any break statements in this while loop will trigger a line break
while ((glyphResult = glyphs.next()) && !glyphResult.done) {
const glyph = glyphResult.value;
if (curLineIndex === lineCache.length) {
lineCache.push({
codepointIndex: glyph.cluster,
maxY,
maxX,
});
} else if (curLineIndex > lineCache.length) {
throw new Error('Unexpected lineCache length');
}
// If we encounter a word boundary (white space or newline) we invalidate
// the lastWord and set the xStartLastWordBoundary if we haven't already.
if (
glyph.codepoint === 32 ||
glyph.codepoint === 10 ||
glyph.codepoint === 8203
) {
if (lastWord.codepointIndex !== -1) {
lastWord.codepointIndex = -1;
xStartLastWordBoundary = curX;
}
} else if (lastWord.codepointIndex === -1) {
lastWord.codepointIndex = glyph.cluster;
lastWord.bufferOffset = bufferOffset;
lastWord.xStart = xStartLastWordBoundary;
}
if (glyph.mapped) {
// Mapped glyph
const charEndX = curX + glyph.xOffset + glyph.width;
// Word wrap check
if (
// We are containing the text
contain !== 'none' &&
// The current glyph reaches outside the contained width
charEndX >= lineVertexW &&
// There is a last word that we can break to the next line
lastWord.codepointIndex !== -1 &&
// Prevents infinite loop when a single word is longer than the width
lastWord.xStart > 0
) {
// The current word is about to go off the edge of the container width
// Reinitialize the iterator starting at the last word
// and proceeding to the next line
if (nextLineWillFit) {
glyphs = shaper.shapeText(
shaperProps,
new PeekableIterator(
getUnicodeCodepoints(text, lastWord.codepointIndex),
lastWord.codepointIndex,
),
);
bufferOffset = lastWord.bufferOffset;
break;
} else {
glyphs = shaper.shapeText(
shaperProps,
new PeekableIterator(getUnicodeCodepoints(overflowSuffix, 0), 0),
);
curX = lastWord.xStart;
bufferOffset = lastWord.bufferOffset;
// HACK: For the rest of the line when inserting the overflow suffix,
// set contain = 'none' to prevent an infinite loop.
contain = 'none';
}
} else {
// This glyph fits, so we can add it to the buffer
const quadX = curX + glyph.xOffset;
const quadY = curY + glyph.yOffset;
// Only add to buffer for rendering if the line is within the render window
if (lineIsWithinWindow) {
if (curLineBufferStart === -1) {
curLineBufferStart = bufferOffset;
}
const atlasEntry = trFontFace.getAtlasEntry(glyph.glyphId);
// Add texture coordinates
const u = atlasEntry.x / trFontFace.data.common.scaleW;
const v = atlasEntry.y / trFontFace.data.common.scaleH;
const uvWidth = atlasEntry.width / trFontFace.data.common.scaleW;
const uvHeight = atlasEntry.height / trFontFace.data.common.scaleH;
// TODO: (Performance) We can optimize this by using ELEMENT_ARRAY_BUFFER
// eliminating the need to duplicate vertices
// Top-left
vertexBuffer[bufferOffset++] = quadX;
vertexBuffer[bufferOffset++] = quadY;
vertexBuffer[bufferOffset++] = u;
vertexBuffer[bufferOffset++] = v;
// Top-right
vertexBuffer[bufferOffset++] = quadX + glyph.width;
vertexBuffer[bufferOffset++] = quadY;
vertexBuffer[bufferOffset++] = u + uvWidth;
vertexBuffer[bufferOffset++] = v;
// Bottom-left
vertexBuffer[bufferOffset++] = quadX;
vertexBuffer[bufferOffset++] = quadY + glyph.height;
vertexBuffer[bufferOffset++] = u;
vertexBuffer[bufferOffset++] = v + uvHeight;
// Bottom-right
vertexBuffer[bufferOffset++] = quadX + glyph.width;
vertexBuffer[bufferOffset++] = quadY + glyph.height;
vertexBuffer[bufferOffset++] = u + uvWidth;
vertexBuffer[bufferOffset++] = v + uvHeight;
}
maxY = Math.max(maxY, quadY + glyph.height);
maxX = Math.max(maxX, quadX + glyph.width);
curX += glyph.xAdvance;
}
} else {
// Unmapped character
// Handle newlines
if (glyph.codepoint === 10) {
if (nextLineWillFit) {
// The whole line fit, so we can break to the next line
break;
} else {
// The whole line won't fit, so we need to add the overflow suffix
glyphs = shaper.shapeText(
shaperProps,
new PeekableIterator(getUnicodeCodepoints(overflowSuffix, 0), 0),
);
// HACK: For the rest of the line when inserting the overflow suffix,
// set contain = 'none' to prevent an infinite loop.
contain = 'none';
}
}
}
}
// Prepare for the next line...
if (curLineBufferStart !== -1) {
bufferLineInfos.push({
bufferStart: curLineBufferStart,
bufferEnd: bufferOffset,
});
curLineBufferStart = -1;
}
curX = 0;
curY += vertexLineHeight;
curLineIndex++;
lastWord.codepointIndex = -1;
xStartLastWordBoundary = 0;
// Figure out if there are any more lines to render...
if (!forceFullLayoutCalc && contain === 'both' && curY > rwSdf.y2) {
// Stop layout calculation early (for performance purposes) if:
// - We're not forcing a full layout calculation (for width/height calculation)
// - ...and we're containing the text vertically+horizontally (contain === 'both')
// - ...and we have a render window
// - ...and the next line is below the bottom of the render window
moreLines = false;
} else if (glyphResult && glyphResult.done) {
// If we've reached the end of the text, we know we're done
moreLines = false;
} else if (!nextLineWillFit) {
// If we're contained vertically+horizontally (contain === 'both')
// but not scrollable and the next line won't fit, we're done.
moreLines = false;
}
}
// Use textAlign to determine if we need to adjust the x position of the text
// in the buffer line by line
if (textAlign === 'center') {
const vertexTextW = contain === 'none' ? maxX : vertexW;
for (let i = 0; i < bufferLineInfos.length; i++) {
const line = bufferLineInfos[i]!;
// - 4 = the x position of a rightmost vertex
const lineWidth =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
vertexBuffer[line.bufferEnd - 4]! - vertexBuffer[line.bufferStart]!;
const xOffset = (vertexTextW - lineWidth) / 2;
for (let j = line.bufferStart; j < line.bufferEnd; j += 4) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
vertexBuffer[j]! += xOffset;
}
}
} else if (textAlign === 'right') {
const vertexTextW = contain === 'none' ? maxX : vertexW;
for (let i = 0; i < bufferLineInfos.length; i++) {
const line = bufferLineInfos[i]!;
const lineWidth =
line.bufferEnd === line.bufferStart
? 0
: // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
vertexBuffer[line.bufferEnd - 4]! - vertexBuffer[line.bufferStart]!;
const xOffset = vertexTextW - lineWidth;
for (let j = line.bufferStart; j < line.bufferEnd; j += 4) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
vertexBuffer[j]! += xOffset;
}
}
}
assertTruthy(glyphResult);
return {
bufferNumFloats: bufferOffset,
bufferNumQuads: bufferOffset / 16,
layoutNumCharacters: glyphResult.done
? text.length - startingCodepointIndex
: glyphResult.value.cluster - startingCodepointIndex + 1,
fullyProcessed: !!glyphResult.done,
maxX,
maxY,
numLines: lineCache.length,
};
}