@swf2es/render 0.1.34 → 0.1.36

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.
Files changed (45) hide show
  1. package/dist/commands.d.ts +11 -2
  2. package/dist/commands.d.ts.map +1 -1
  3. package/dist/commands.js +36 -6
  4. package/dist/commands.js.map +1 -1
  5. package/dist/geometry.d.ts +62 -5
  6. package/dist/geometry.d.ts.map +1 -1
  7. package/dist/geometry.js +100 -17
  8. package/dist/geometry.js.map +1 -1
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +1 -1
  12. package/dist/index.js.map +1 -1
  13. package/dist/measure.d.ts.map +1 -1
  14. package/dist/measure.js +118 -61
  15. package/dist/measure.js.map +1 -1
  16. package/dist/plan.js +4 -4
  17. package/dist/plan.js.map +1 -1
  18. package/dist/rows.d.ts +4 -0
  19. package/dist/rows.d.ts.map +1 -1
  20. package/dist/rows.js +59 -10
  21. package/dist/rows.js.map +1 -1
  22. package/dist/scene.d.ts.map +1 -1
  23. package/dist/scene.js +37 -5
  24. package/dist/scene.js.map +1 -1
  25. package/dist/webgl/atlas.d.ts +89 -16
  26. package/dist/webgl/atlas.d.ts.map +1 -1
  27. package/dist/webgl/atlas.js +285 -109
  28. package/dist/webgl/atlas.js.map +1 -1
  29. package/dist/webgl/backend.d.ts +5 -3
  30. package/dist/webgl/backend.d.ts.map +1 -1
  31. package/dist/webgl/backend.js +9 -9
  32. package/dist/webgl/backend.js.map +1 -1
  33. package/dist/webgl/draws.js +1 -1
  34. package/dist/webgl/draws.js.map +1 -1
  35. package/package.json +2 -2
  36. package/src/commands.ts +48 -6
  37. package/src/geometry.ts +109 -19
  38. package/src/index.ts +1 -0
  39. package/src/measure.ts +132 -68
  40. package/src/plan.ts +4 -4
  41. package/src/rows.ts +62 -19
  42. package/src/scene.ts +41 -5
  43. package/src/webgl/atlas.ts +334 -118
  44. package/src/webgl/backend.ts +9 -9
  45. package/src/webgl/draws.ts +1 -1
@@ -1,4 +1,4 @@
1
- import { GLYPH_FLOATS, samplesStage } from "../geometry.js";
1
+ import { GLYPH_FLOATS, RELEASE_FRAMES, samplesStage, } from "../geometry.js";
2
2
  import { FreeRanges } from "./free-ranges.js";
3
3
  import { GlyphTables } from "./glyph-tables.js";
4
4
  import { COLOR_AT, DEPTH_AT, GLYPH_AT, ORIGIN_AT, POSITION_AT, RUN_AT, TEXEL_AT, TINT_AT, } from "./programs.js";
@@ -6,7 +6,7 @@ import { COLOR_AT, DEPTH_AT, GLYPH_AT, ORIGIN_AT, POSITION_AT, RUN_AT, TEXEL_AT,
6
6
  * Bytes a vertex in the atlas: its position's two floats, its texel's two, then its colour's four
7
7
  * bytes, or a mesh's depth's float.
8
8
  */
9
- const VERTEX_BYTES = 20;
9
+ export const VERTEX_BYTES = 20;
10
10
  /** Bytes a vertex of advanced text's glyph quads, in an atlas of their own. */
11
11
  const GLYPH_BYTES = GLYPH_FLOATS * 4;
12
12
  /** The fewest glyph vertices the glyph atlas holds once a text needs it. */
@@ -49,14 +49,29 @@ function interleave(data, count, floats, words, at) {
49
49
  }
50
50
  }
51
51
  }
52
+ const NO_INDICES = new Uint16Array(0);
53
+ /**
54
+ * `source`, indices into `count` vertices, copied into an array of their own: in 16 bits where
55
+ * they fit, and in a buffer of their own, not the geometry's, which is let go of.
56
+ */
57
+ function localIndices(source, count) {
58
+ const local = count <= 1 << 16 ? new Uint16Array(source.length) : new Uint32Array(source.length);
59
+ local.set(source);
60
+ return local;
61
+ }
52
62
  /**
53
63
  * The geometry atlas: every geometry copied once into a page's vertex and
54
64
  * index buffers, and advanced text's glyph quads into an atlas of their
55
- * own, where the draws find them by their places. A geometry let go of
56
- * leaves holes in its page, which later geometries are placed in once the
57
- * top is full; a page is packed again from the geometries' own arrays only
58
- * when what is added finds no room. Repacking every geometry for its holes
59
- * alone made frames that copied and uploaded the whole atlas again, tens of
65
+ * own, where the draws find them by their places. Once copied, a geometry
66
+ * too large to merge keeps only its indices on the CPU (Place.local),
67
+ * counted from its first vertex, and its registry lets go of its arrays
68
+ * (Geometries.release); one small enough to merge keeps its arrays, which a
69
+ * run's stream stages again whenever moving content makes its runs anew. A geometry
70
+ * let go of leaves holes in its page, which later geometries are placed in
71
+ * once the top is full; a page is packed again only when what is added
72
+ * finds no room, its vertices moved on the GPU and its indices written
73
+ * again from its own. Repacking every geometry for its holes alone made
74
+ * frames that copied and uploaded the whole atlas again, tens of
60
75
  * megabytes, every few seconds of a busy SWF. The glyph atlas is packed
61
76
  * again once its holes outweigh what is live, which also frees the texts'
62
77
  * tables' slots.
@@ -93,6 +108,15 @@ export class GeometryAtlas {
93
108
  };
94
109
  /** The texts' modes and tables, which the glyph program reads. */
95
110
  tables;
111
+ /** The ids counted among a frame's additions, each once (place). */
112
+ counted = new Set();
113
+ /**
114
+ * The places made, with their ids and frames, whose geometries' arrays are let go of once they
115
+ * have stood RELEASE_FRAMES of their registry's (settle), oldest first.
116
+ */
117
+ settling = [];
118
+ settlingIds = [];
119
+ settlingFrames = [];
96
120
  constructor(gl, state) {
97
121
  this.gl = gl;
98
122
  this.state = state;
@@ -131,6 +155,9 @@ export class GeometryAtlas {
131
155
  this.stagedVertices = 0;
132
156
  this.stagedIndexCount = 0;
133
157
  this.places = [];
158
+ this.settling = [];
159
+ this.settlingIds = [];
160
+ this.settlingFrames = [];
134
161
  this.tables.setUp();
135
162
  }
136
163
  /** A page's buffers and vertex array made, with no room till it is packed. */
@@ -166,6 +193,9 @@ export class GeometryAtlas {
166
193
  return;
167
194
  }
168
195
  this.places[id] = null;
196
+ // Its geometry's own, where it has not settled: a settling entry would keep them alive.
197
+ place.local = NO_INDICES;
198
+ place.batch = null;
169
199
  if (place.glyphs) {
170
200
  this.glyphs.live -= place.vertices;
171
201
  this.glyphs.holes += place.vertices;
@@ -179,10 +209,11 @@ export class GeometryAtlas {
179
209
  }
180
210
  /**
181
211
  * The geometries removed since the last frame let go of, and those added
182
- * copied in: the shapes together at their page's top or in a hole that
183
- * holds them all, else each in the first hole that holds it, else into a
184
- * page packed again with room for twice what is live; a text at the
185
- * glyph atlas' top, else into one packed again.
212
+ * copied in, their arrays then let go of by the registry: the shapes
213
+ * together at their page's top or in a hole that holds them all, else
214
+ * each in the first hole that holds it, else into a page packed again
215
+ * with room for twice what is live; a text at the glyph atlas' top, else
216
+ * into one packed again.
186
217
  */
187
218
  place(geometries) {
188
219
  this.frame++;
@@ -197,7 +228,8 @@ export class GeometryAtlas {
197
228
  page.indices.reset(page.indices.capacity);
198
229
  }
199
230
  }
200
- // Changed in place: copied again where they lie, else as added.
231
+ // Changed in place, which only a geometry added to be kept is: copied again where they lie,
232
+ // else as added.
201
233
  for (const id of geometries.updated) {
202
234
  const place = this.places[id];
203
235
  const data = geometries.get(id);
@@ -212,25 +244,33 @@ export class GeometryAtlas {
212
244
  }
213
245
  }
214
246
  geometries.updated.length = 0;
215
- // Found flat: what is added after reads it from its data.
247
+ // Found flat: what is added after reads it from its registry.
216
248
  for (const id of geometries.flattened) {
217
249
  const place = this.places[id];
218
250
  if (place) {
219
- place.flat = geometries.get(id)?.flat === true;
251
+ place.flat = geometries.flat(id);
220
252
  }
221
253
  }
222
254
  geometries.flattened.length = 0;
255
+ // An id let go of and taken again in a frame comes twice, its arrays the second's: counted
256
+ // once, and copied once, as its arrays are let go of once copied.
257
+ const counted = this.counted;
258
+ counted.clear();
223
259
  let vertices = 0;
224
260
  let indices = 0;
225
261
  let glyphVertices = 0;
226
262
  let glyphIndices = 0;
227
263
  for (const id of geometries.added) {
228
264
  const data = geometries.get(id);
229
- if (data?.glyphs) {
265
+ if (!data || counted.has(id)) {
266
+ continue;
267
+ }
268
+ counted.add(id);
269
+ if (data.glyphs) {
230
270
  glyphVertices += data.glyphs.vertices.length / GLYPH_FLOATS;
231
271
  glyphIndices += data.indices.length;
232
272
  }
233
- else if (data) {
273
+ else {
234
274
  vertices += data.positions.length / 2;
235
275
  indices += data.indices.length;
236
276
  }
@@ -240,23 +280,21 @@ export class GeometryAtlas {
240
280
  // room, or below, when holes too small for one of them leave it none.
241
281
  const g = this.glyphs;
242
282
  const page = this.pages[0];
243
- let shapes = vertices > page.vertices.room() || indices > page.indices.room();
244
- const texts = g.top + glyphVertices > g.capacity ||
283
+ if (vertices > page.vertices.room() || indices > page.indices.room()) {
284
+ this.repack(vertices, indices);
285
+ }
286
+ if (g.top + glyphVertices > g.capacity ||
245
287
  g.indexTop + glyphIndices > g.indexCapacity ||
246
288
  (g.holes > GLYPHS_LEAST && g.holes > g.live) ||
247
- this.tables.full;
248
- if (shapes) {
249
- this.repack(geometries);
250
- }
251
- if (texts) {
252
- this.repackGlyphs(geometries);
289
+ this.tables.full) {
290
+ this.repackGlyphs(glyphVertices, glyphIndices);
253
291
  }
254
292
  // The shapes added together in one range where one holds them all, at the top or in a
255
293
  // hole, so that they go up in one upload each for vertices and indices; else each in the
256
294
  // first that holds it, which a churning atlas would leave to a call or two a geometry.
257
295
  let vertexAt = -1;
258
296
  let indexAt = -1;
259
- if (!shapes && vertices > 0) {
297
+ if (vertices > 0) {
260
298
  vertexAt = page.vertices.take(vertices);
261
299
  indexAt = vertexAt < 0 ? -1 : page.indices.take(indices);
262
300
  if (vertexAt >= 0 && indexAt < 0) {
@@ -266,89 +304,186 @@ export class GeometryAtlas {
266
304
  }
267
305
  for (const id of geometries.added) {
268
306
  const data = geometries.get(id);
269
- if (!data) {
307
+ if (!data || !counted.delete(id)) {
270
308
  continue;
271
309
  }
272
- // Added twice in a frame, as an id let go of and taken again can be: its first copy's
273
- // room given back, which only packing would otherwise find again. A packed atlas holds
274
- // its geometries already.
310
+ // Added again after it was placed, as a geometry kept and changed can be, or after a lost
311
+ // context: its first copy's room given back, which only packing would otherwise find again.
312
+ this.letGo(id);
275
313
  if (data.glyphs) {
276
- if (!texts) {
277
- this.letGo(id);
278
- this.copyGlyphs(id, data);
279
- }
280
- continue;
281
- }
282
- if (shapes) {
283
- continue;
314
+ this.copyGlyphs(id, data);
284
315
  }
285
- this.letGo(id);
286
- if (vertexAt >= 0) {
316
+ else if (vertexAt >= 0) {
287
317
  this.copyAt(page, id, data, vertexAt, indexAt);
288
318
  vertexAt += data.positions.length / 2;
289
319
  indexAt += data.indices.length;
290
320
  }
291
321
  else if (!this.copyIn(page, id, data)) {
292
- // Packing copies every live shape, those added after this one too.
293
- shapes = true;
294
- this.repack(geometries);
322
+ // Packed with room for this one and those after it, which it then holds.
323
+ this.repack(vertices, indices);
324
+ this.copyIn(page, id, data);
295
325
  }
326
+ if (!data.glyphs) {
327
+ vertices -= data.positions.length / 2;
328
+ indices -= data.indices.length;
329
+ }
330
+ this.settling.push(this.places[id]);
331
+ this.settlingIds.push(id);
332
+ this.settlingFrames.push(geometries.frame);
296
333
  }
297
334
  geometries.added.length = 0;
298
335
  this.flush();
299
336
  this.tables.upload();
337
+ this.settle(geometries);
300
338
  }
301
- /** Every live shape copied into its page's buffers made anew, with room for twice them. */
302
- repack(geometries) {
303
- const gl = this.gl;
304
- const ids = geometries.live();
305
- let vertices = 0;
306
- let indices = 0;
307
- for (const id of ids) {
308
- const data = geometries.get(id);
309
- if (!data.glyphs) {
310
- vertices += data.positions.length / 2;
311
- indices += data.indices.length;
339
+ /**
340
+ * The geometries placed RELEASE_FRAMES of their registry's ago or more, by its frames, not the
341
+ * atlas', which a BitmapData's draws advance too, placed still and too large to merge: their
342
+ * arrays let go of (Geometries.release) and their indices copied into arrays of their own. One
343
+ * small enough to merge keeps its arrays, which a run's stream stages again whenever moving
344
+ * content makes its runs anew: copying such geometries from the page on the GPU instead, a copy
345
+ * for each, made a frame of moving timelines thousands of copies and ANGLE's GL wait twice as
346
+ * long (0.1.35), and getting them back by tessellating them again stalls a frame wherever a
347
+ * still run changes or the stream is made anew. Only while the context
348
+ * is not known to be lost: copies made into a lost one are no copies, and the arrays stay for
349
+ * the restore. In Chrome's window before it flags a loss (webgl/losses.ts), arrays may go
350
+ * after copies that never reached the GPU; the restore's Geometries.lost rebuilds them.
351
+ */
352
+ settle(geometries) {
353
+ const places = this.settling;
354
+ const ids = this.settlingIds;
355
+ const frames = this.settlingFrames;
356
+ let k = 0;
357
+ while (k < places.length && geometries.frame - frames[k] >= RELEASE_FRAMES) {
358
+ k++;
359
+ }
360
+ if (k === 0 || this.gl.isContextLost()) {
361
+ return;
362
+ }
363
+ for (let i = 0; i < k; i++) {
364
+ const place = places[i];
365
+ // Small enough to merge (Place.batch): its arrays kept for a run's stream to stage.
366
+ if (this.places[ids[i]] === place && !place.batch && geometries.get(ids[i])) {
367
+ geometries.release(ids[i]);
368
+ if (geometries.released(ids[i])) {
369
+ place.local = localIndices(place.local, place.vertices);
370
+ }
312
371
  }
313
372
  }
373
+ // Moved down in place: a frame's settling allocates nothing.
374
+ places.copyWithin(0, k);
375
+ ids.copyWithin(0, k);
376
+ frames.copyWithin(0, k);
377
+ places.length -= k;
378
+ ids.length -= k;
379
+ frames.length -= k;
380
+ }
381
+ /**
382
+ * The shapes' page packed again, with room for twice its live shapes and
383
+ * `vertices` and `indices` more: its shapes moved in a row from its start,
384
+ * their vertices copied on the GPU, through a buffer of their own, as a
385
+ * new store loses the old one's, and their indices written again from
386
+ * their own (Place.local). Their places are moved, not made anew, so that a run's
387
+ * stream still finds them (Place.batch).
388
+ */
389
+ repack(vertices, indices) {
390
+ const gl = this.gl;
314
391
  const page = this.pages[0];
392
+ const moved = [];
393
+ let used = 0;
394
+ for (const place of this.places) {
395
+ if (place && !place.glyphs) {
396
+ moved.push(place);
397
+ vertices += place.vertices;
398
+ indices += place.indices;
399
+ used = Math.max(used, place.vertexStart + place.vertices);
400
+ }
401
+ }
402
+ // In their order in the page, so that shapes placed together move in one copy.
403
+ moved.sort((a, b) => a.vertexStart - b.vertexStart);
404
+ // What is staged goes up first, for the copy to hold it.
405
+ this.flush();
406
+ this.state.bindVertexArray(page.vertexArray);
407
+ gl.bindBuffer(gl.ARRAY_BUFFER, page.vertexBuffer);
408
+ const copy = used > 0 ? this.copyOf(used * VERTEX_BYTES) : null;
315
409
  page.vertices.reset(Math.max(LEAST, vertices * 2));
316
410
  page.indices.reset(Math.max(LEAST * 3, indices * 2));
317
411
  page.live = 0;
318
- this.state.bindVertexArray(page.vertexArray);
319
- gl.bindBuffer(gl.ARRAY_BUFFER, page.vertexBuffer);
320
412
  gl.bufferData(gl.ARRAY_BUFFER, page.vertices.capacity * VERTEX_BYTES, gl.STATIC_DRAW);
321
413
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, page.indices.capacity * 4, gl.STATIC_DRAW);
322
- // What was staged for the buffers replaced is copied again below.
323
- this.stagedVertices = 0;
324
- this.stagedIndexCount = 0;
325
- // Each at the top, which holds twice them all.
326
- for (const id of ids) {
327
- const data = geometries.get(id);
328
- if (!data.glyphs) {
329
- this.copyIn(page, id, data);
414
+ let from = 0;
415
+ let to = 0;
416
+ let run = 0;
417
+ for (const place of moved) {
418
+ const base = page.vertices.take(place.vertices);
419
+ const indexStart = page.indices.take(place.indices);
420
+ if (run > 0 && place.vertexStart === from + run && base === to + run) {
421
+ run += place.vertices;
422
+ }
423
+ else {
424
+ this.copyRun(from, to, run, VERTEX_BYTES);
425
+ from = place.vertexStart;
426
+ to = base;
427
+ run = place.vertices;
330
428
  }
429
+ this.writeIndices(page, place.local, base, indexStart);
430
+ place.vertexStart = base;
431
+ place.indexStart = indexStart;
432
+ page.live += place.vertices;
331
433
  }
434
+ this.copyRun(from, to, run, VERTEX_BYTES);
435
+ this.dropCopy(copy);
332
436
  this.flush();
333
437
  }
334
- /** Every live text's quads copied into new glyph buffers: none till a text needs them. */
335
- repackGlyphs(geometries) {
438
+ /**
439
+ * A copy on the GPU of the first `bytes` of the buffer bound to ARRAY_BUFFER, bound to
440
+ * COPY_READ_BUFFER, for copyRun to read from once the buffer is made anew.
441
+ */
442
+ copyOf(bytes) {
443
+ const gl = this.gl;
444
+ const copy = gl.createBuffer();
445
+ gl.bindBuffer(gl.COPY_WRITE_BUFFER, copy);
446
+ gl.bufferData(gl.COPY_WRITE_BUFFER, bytes, gl.STREAM_COPY);
447
+ gl.copyBufferSubData(gl.ARRAY_BUFFER, gl.COPY_WRITE_BUFFER, 0, 0, bytes);
448
+ gl.bindBuffer(gl.COPY_WRITE_BUFFER, null);
449
+ gl.bindBuffer(gl.COPY_READ_BUFFER, copy);
450
+ return copy;
451
+ }
452
+ /** `count` vertices of `bytes` each copied from vertex `from` of the copy (copyOf) to vertex `to` of ARRAY_BUFFER's. */
453
+ copyRun(from, to, count, bytes) {
454
+ if (count > 0) {
455
+ this.gl.copyBufferSubData(this.gl.COPY_READ_BUFFER, this.gl.ARRAY_BUFFER, from * bytes, to * bytes, count * bytes);
456
+ }
457
+ }
458
+ dropCopy(copy) {
459
+ if (copy) {
460
+ this.gl.bindBuffer(this.gl.COPY_READ_BUFFER, null);
461
+ this.gl.deleteBuffer(copy);
462
+ }
463
+ }
464
+ /**
465
+ * The glyph atlas packed again, with room for twice its live texts' quads
466
+ * and `vertices` and `indices` more, none till a text needs them: its
467
+ * texts moved in a row from its start as the shapes' are (repack), and
468
+ * their tables given slots again, only the live texts' kept.
469
+ */
470
+ repackGlyphs(vertices, indices) {
336
471
  const gl = this.gl;
337
472
  const g = this.glyphs;
338
- const ids = geometries.live();
339
- let vertices = 0;
340
- let indices = 0;
341
- for (const id of ids) {
342
- const data = geometries.get(id);
343
- if (data.glyphs) {
344
- vertices += data.glyphs.vertices.length / GLYPH_FLOATS;
345
- indices += data.indices.length;
473
+ const moved = [];
474
+ for (const place of this.places) {
475
+ if (place?.glyphs) {
476
+ moved.push(place);
477
+ vertices += place.vertices;
478
+ indices += place.indices;
346
479
  }
347
480
  }
348
- g.capacity = vertices > 0 ? Math.max(GLYPHS_LEAST, vertices * 2) : 0;
349
- g.indexCapacity = vertices > 0 ? Math.max(GLYPHS_LEAST * 2, indices * 2) : 0;
481
+ moved.sort((a, b) => a.vertexStart - b.vertexStart);
350
482
  this.state.bindVertexArray(this.glyphArray);
351
483
  gl.bindBuffer(gl.ARRAY_BUFFER, this.glyphBuffer);
484
+ const copy = moved.length > 0 && g.top > 0 ? this.copyOf(g.top * GLYPH_BYTES) : null;
485
+ g.capacity = vertices > 0 ? Math.max(GLYPHS_LEAST, vertices * 2) : 0;
486
+ g.indexCapacity = vertices > 0 ? Math.max(GLYPHS_LEAST * 2, indices * 2) : 0;
352
487
  gl.bufferData(gl.ARRAY_BUFFER, g.capacity * GLYPH_BYTES, gl.STATIC_DRAW);
353
488
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, g.indexCapacity * 4, gl.STATIC_DRAW);
354
489
  g.top = 0;
@@ -357,13 +492,41 @@ export class GeometryAtlas {
357
492
  g.holes = 0;
358
493
  // Only the live texts' tables kept.
359
494
  this.tables.reset();
360
- for (const id of ids) {
361
- const data = geometries.get(id);
362
- if (data.glyphs) {
363
- this.copyGlyphs(id, data);
495
+ let from = 0;
496
+ let to = 0;
497
+ let run = 0;
498
+ let written = 0;
499
+ for (const place of moved) {
500
+ written += place.indices;
501
+ }
502
+ const absolute = new Uint32Array(written);
503
+ for (const place of moved) {
504
+ if (run > 0 && place.vertexStart === from + run && g.top === to + run) {
505
+ run += place.vertices;
506
+ }
507
+ else {
508
+ this.copyRun(from, to, run, GLYPH_BYTES);
509
+ from = place.vertexStart;
510
+ to = g.top;
511
+ run = place.vertices;
512
+ }
513
+ const local = place.local;
514
+ for (let i = 0; i < local.length; i++) {
515
+ absolute[g.indexTop + i] = local[i] + g.top;
364
516
  }
517
+ const glyphs = place.glyphs;
518
+ glyphs.slot = this.tables.slot(glyphs.mode, glyphs.table);
519
+ place.vertexStart = g.top;
520
+ place.indexStart = g.indexTop;
521
+ g.top += place.vertices;
522
+ g.indexTop += place.indices;
523
+ g.live += place.vertices;
524
+ }
525
+ this.copyRun(from, to, run, GLYPH_BYTES);
526
+ this.dropCopy(copy);
527
+ if (written > 0) {
528
+ gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, absolute);
365
529
  }
366
- this.tables.upload();
367
530
  }
368
531
  /**
369
532
  * A shape's `data` copied into `page`, at its top or in a hole, its
@@ -384,48 +547,55 @@ export class GeometryAtlas {
384
547
  this.copyAt(page, id, data, base, indexStart);
385
548
  return true;
386
549
  }
387
- /** A shape's `data` copied into `page`'s room taken for it, from vertex `base` and index `indexStart`. */
550
+ /**
551
+ * A shape's `data` copied into `page`'s room taken for it, from vertex
552
+ * `base` and index `indexStart`, its indices kept in a copy of their own.
553
+ */
388
554
  copyAt(page, id, data, base, indexStart) {
389
555
  const count = data.positions.length / 2;
390
556
  const source = data.indices;
391
- const length = source.length;
392
557
  this.copyVertices(page, data, base);
393
- if (length > STAGED_INDICES) {
394
- const indices = new Uint32Array(length);
395
- for (let i = 0; i < length; i++) {
396
- indices[i] = source[i] + base;
397
- }
398
- this.flushIndices();
399
- this.state.bindVertexArray(page.vertexArray);
400
- this.gl.bufferSubData(this.gl.ELEMENT_ARRAY_BUFFER, indexStart * 4, indices);
401
- }
402
- else {
403
- this.stagePage(page);
404
- if (this.stagedIndexCount + length > STAGED_INDICES ||
405
- this.stagedIndexAt + this.stagedIndexCount !== indexStart) {
406
- this.flushIndices();
407
- this.stagedIndexAt = indexStart;
408
- }
409
- const staged = this.stagedIndices;
410
- const at = this.stagedIndexCount;
411
- for (let i = 0; i < length; i++) {
412
- staged[at + i] = source[i] + base;
413
- }
414
- this.stagedIndexCount = at + length;
415
- }
558
+ this.writeIndices(page, source, base, indexStart);
416
559
  this.places[id] = {
417
560
  page: page.index,
418
561
  vertexStart: base,
419
562
  vertices: count,
420
563
  indexStart,
421
- indices: length,
564
+ indices: source.length,
422
565
  glyphs: null,
423
566
  flat: data.flat === true,
424
567
  stage: samplesStage(data),
425
568
  batch: this.batch(data),
569
+ local: source,
426
570
  };
427
571
  page.live += count;
428
572
  }
573
+ /** `source`, counted from a geometry's first vertex, made absolute from `base` into `page` from index `indexStart`: staged, or uploaded if too many. */
574
+ writeIndices(page, source, base, indexStart) {
575
+ const length = source.length;
576
+ if (length > STAGED_INDICES) {
577
+ const indices = new Uint32Array(length);
578
+ for (let i = 0; i < length; i++) {
579
+ indices[i] = source[i] + base;
580
+ }
581
+ this.flushIndices();
582
+ this.state.bindVertexArray(page.vertexArray);
583
+ this.gl.bufferSubData(this.gl.ELEMENT_ARRAY_BUFFER, indexStart * 4, indices);
584
+ return;
585
+ }
586
+ this.stagePage(page);
587
+ if (this.stagedIndexCount + length > STAGED_INDICES ||
588
+ this.stagedIndexAt + this.stagedIndexCount !== indexStart) {
589
+ this.flushIndices();
590
+ this.stagedIndexAt = indexStart;
591
+ }
592
+ const staged = this.stagedIndices;
593
+ const at = this.stagedIndexCount;
594
+ for (let i = 0; i < length; i++) {
595
+ staged[at + i] = source[i] + base;
596
+ }
597
+ this.stagedIndexCount = at + length;
598
+ }
429
599
  /** `data`'s vertices interleaved for `page` from vertex `base`: staged, or uploaded if too many. */
430
600
  copyVertices(page, data, base) {
431
601
  const count = data.positions.length / 2;
@@ -479,15 +649,20 @@ export class GeometryAtlas {
479
649
  ? { data, frame: this.frame }
480
650
  : null;
481
651
  }
482
- /** Glyph quads copied in at the top of the glyph atlas, their indices made absolute in it. */
652
+ /**
653
+ * Glyph quads copied in at the top of the glyph atlas, their indices made
654
+ * absolute in it, and kept counted from the text's first vertex as a
655
+ * shape's are.
656
+ */
483
657
  copyGlyphs(id, data) {
484
658
  const gl = this.gl;
485
659
  const g = this.glyphs;
486
660
  const glyphs = data.glyphs;
487
661
  const count = glyphs.vertices.length / GLYPH_FLOATS;
488
- const indices = new Uint32Array(data.indices.length);
662
+ const source = data.indices;
663
+ const indices = new Uint32Array(source.length);
489
664
  for (let i = 0; i < indices.length; i++) {
490
- indices[i] = data.indices[i] + g.top;
665
+ indices[i] = source[i] + g.top;
491
666
  }
492
667
  this.state.bindVertexArray(this.glyphArray);
493
668
  gl.bindBuffer(gl.ARRAY_BUFFER, this.glyphBuffer);
@@ -507,6 +682,7 @@ export class GeometryAtlas {
507
682
  flat: false,
508
683
  stage: false,
509
684
  batch: null,
685
+ local: source,
510
686
  };
511
687
  g.top += count;
512
688
  g.indexTop += indices.length;