@flighthq/bitmaptext 0.5.1-next.920.da8f530 → 0.6.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.
Files changed (40) hide show
  1. package/README.md +2 -2
  2. package/dist/.tsbuildinfo.contract +1 -0
  3. package/dist/.tsbuildinfo.runtime +1 -0
  4. package/dist/bitmapText.d.ts +10 -3
  5. package/dist/bitmapText.d.ts.map +1 -1
  6. package/dist/bitmapText.js +33 -13
  7. package/dist/bitmapText.js.map +1 -1
  8. package/dist/contract.bitmaptext-types.d.ts +2 -0
  9. package/dist/contract.bitmaptext-types.d.ts.map +1 -0
  10. package/dist/contract.bitmaptext-types.js +4 -0
  11. package/dist/contract.bitmaptext-types.js.map +1 -0
  12. package/dist/contract.d.ts +5 -4
  13. package/dist/contract.d.ts.map +1 -1
  14. package/dist/contract.js +5 -4
  15. package/dist/contract.js.map +1 -1
  16. package/dist/enableBitmapTextGuards.d.ts +2 -1
  17. package/dist/enableBitmapTextGuards.d.ts.map +1 -1
  18. package/dist/enableBitmapTextGuards.js +15 -7
  19. package/dist/enableBitmapTextGuards.js.map +1 -1
  20. package/dist/explainBitmapTextMissingGlyphs.d.ts +4 -0
  21. package/dist/explainBitmapTextMissingGlyphs.d.ts.map +1 -0
  22. package/dist/explainBitmapTextMissingGlyphs.js +21 -0
  23. package/dist/explainBitmapTextMissingGlyphs.js.map +1 -0
  24. package/dist/index.d.ts +6 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +5 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/types.d.ts +46 -0
  29. package/dist/types.d.ts.map +1 -0
  30. package/dist/types.js +2 -0
  31. package/dist/types.js.map +1 -0
  32. package/dist/updateBitmapText.d.ts +2 -1
  33. package/dist/updateBitmapText.d.ts.map +1 -1
  34. package/dist/updateBitmapText.js +80 -9
  35. package/dist/updateBitmapText.js.map +1 -1
  36. package/package.json +15 -11
  37. package/src/bitmapText.test.ts +94 -8
  38. package/src/enableBitmapTextGuards.test.ts +70 -12
  39. package/src/explainBitmapTextMissingGlyphs.test.ts +57 -0
  40. package/src/updateBitmapText.test.ts +106 -15
@@ -1,22 +1,37 @@
1
+ import { EntityRuntimeKey } from '@flighthq/entity/contract';
2
+ import type { HostGlyphRasterizerCapability } from '@flighthq/font/contract';
1
3
  import { createGlyphAtlas, createGlyphSourceFromGlyphAtlas } from '@flighthq/glyphatlas/contract';
2
- import { setLogSink } from '@flighthq/log/contract';
3
- import type { GlyphAtlas, GlyphRasterizerBackend, LogEntry } from '@flighthq/types/contract';
4
+ import type { GlyphAtlas, GlyphEntry, GlyphSource } from '@flighthq/glyphatlas/contract';
5
+ import type { LogState } from '@flighthq/log/contract';
6
+ import { createLogState, getMemoryLogSinkEntries, setLogSink } from '@flighthq/log/contract';
7
+ import type { LogEntry, MemoryLogSink } from '@flighthq/log/contract';
4
8
  import { afterEach, beforeEach, describe, expect, it } from 'vitest';
5
9
 
6
- import { createBitmapText } from './bitmapText';
7
- import { disableBitmapTextGuards, enableBitmapTextGuards } from './enableBitmapTextGuards';
8
- import { updateBitmapText } from './updateBitmapText';
10
+ import { createBitmapText } from './bitmapText.ts';
11
+ import { disableBitmapTextGuards, enableBitmapTextGuards } from './enableBitmapTextGuards.ts';
12
+ import { updateBitmapText } from './updateBitmapText.ts';
9
13
 
10
14
  let entries: LogEntry[];
11
15
 
16
+ function clock(): number {
17
+ return 0;
18
+ }
19
+
20
+ function readLogEntries(sink: Readonly<MemoryLogSink>): LogEntry[] {
21
+ const entries: LogEntry[] = [];
22
+ getMemoryLogSinkEntries(entries, sink);
23
+ return entries;
24
+ }
25
+
12
26
  beforeEach(() => {
27
+ logState = createLogState();
13
28
  entries = [];
14
- setLogSink((entry) => entries.push(entry));
29
+ setLogSink(logState, (entry) => entries.push(entry));
15
30
  });
16
31
 
17
32
  afterEach(() => {
18
33
  disableBitmapTextGuards();
19
- setLogSink(null);
34
+ setLogSink(logState, null);
20
35
  });
21
36
 
22
37
  function messages(): string {
@@ -27,13 +42,13 @@ function messages(): string {
27
42
  // of them resident at once: laying out its tail evicts its head, every pass invalidates the last, and
28
43
  // the layout cannot settle however many passes it is given.
29
44
  function createTinyGlyphAtlas(): GlyphAtlas {
30
- const backend: GlyphRasterizerBackend = {
45
+ const backend: HostGlyphRasterizerCapability = {
31
46
  rasterize: () => ({
32
47
  advance: 8,
33
48
  bearingX: 0,
34
49
  bearingY: 8,
35
50
  height: 8,
36
- pixels: new Uint8ClampedArray(8 * 8 * 4),
51
+ pixels: new Uint8Array(8 * 8 * 4),
37
52
  width: 8,
38
53
  }),
39
54
  };
@@ -47,19 +62,42 @@ function createTinyGlyphAtlas(): GlyphAtlas {
47
62
  });
48
63
  }
49
64
 
65
+ function createLimitedGlyphSource(codepoints: readonly number[]): GlyphSource {
66
+ const glyphs = new Map<number, GlyphEntry>();
67
+ for (const cp of codepoints) {
68
+ glyphs.set(cp, { advance: 10, bearingX: 0, bearingY: 8, height: 8, page: 0, width: 6, x: 0, y: 0 });
69
+ }
70
+ return {
71
+ [EntityRuntimeKey]: undefined,
72
+ getGlyphAtlasImage: () => null,
73
+ getGlyphEntry: (cp) => glyphs.get(cp) ?? null,
74
+ getGlyphKerning: () => 0,
75
+ getGlyphLayoutVersion: () => 0,
76
+ getGlyphMetrics: () => ({ ascent: 8, descent: 2, lineGap: 0 }),
77
+ };
78
+ }
79
+
50
80
  describe('disableBitmapTextGuards', () => {
51
81
  it('stops the guard reporting later layouts', () => {
52
- enableBitmapTextGuards();
82
+ enableBitmapTextGuards(logState, clock);
53
83
  disableBitmapTextGuards();
54
84
  const source = createGlyphSourceFromGlyphAtlas(createTinyGlyphAtlas());
55
85
  updateBitmapText(createBitmapText(source, { text: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' }));
56
86
  expect(messages()).toBe('');
57
87
  });
88
+
89
+ it('stops the missing-glyph guard from reporting', () => {
90
+ enableBitmapTextGuards(logState, clock);
91
+ disableBitmapTextGuards();
92
+ const source = createLimitedGlyphSource([0x41]);
93
+ updateBitmapText(createBitmapText(source, { text: 'AB' }));
94
+ expect(messages()).toBe('');
95
+ });
58
96
  });
59
97
 
60
98
  describe('enableBitmapTextGuards', () => {
61
99
  it('says nothing for a layout that settles', () => {
62
- enableBitmapTextGuards();
100
+ enableBitmapTextGuards(logState, clock);
63
101
  const source = createGlyphSourceFromGlyphAtlas(createTinyGlyphAtlas());
64
102
  updateBitmapText(createBitmapText(source, { text: 'ABC' }));
65
103
  expect(messages()).toBe('');
@@ -68,10 +106,30 @@ describe('enableBitmapTextGuards', () => {
68
106
  // Without this the failure is silent and looks like a rendering bug: the text draws, every quad is
69
107
  // where layout put it, and the pixels under them belong to other glyphs.
70
108
  it('warns when a string needs more glyphs at once than the atlas can hold', () => {
71
- enableBitmapTextGuards();
109
+ enableBitmapTextGuards(logState, clock);
72
110
  const source = createGlyphSourceFromGlyphAtlas(createTinyGlyphAtlas());
73
111
  updateBitmapText(createBitmapText(source, { text: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' }));
74
112
  expect(messages()).toContain('never settled');
75
113
  expect(messages()).toContain('Enlarge the glyph atlas');
76
114
  });
115
+
116
+ it('warns when a codepoint has no glyph in the source', () => {
117
+ enableBitmapTextGuards(logState, clock);
118
+ const source = createLimitedGlyphSource([0x41]);
119
+ updateBitmapText(createBitmapText(source, { text: 'AB' }));
120
+ expect(messages()).toContain('U+0042');
121
+ expect(messages()).toContain('explainBitmapTextMissingGlyphs');
122
+ });
123
+
124
+ it('says nothing when all codepoints have glyphs', () => {
125
+ enableBitmapTextGuards(logState, clock);
126
+ const source = createLimitedGlyphSource([0x41, 0x42]);
127
+ updateBitmapText(createBitmapText(source, { text: 'AB' }));
128
+ const missingMessages = entries.filter((e) =>
129
+ String((e.data as { message?: unknown } | undefined)?.message ?? '').includes('no glyph'),
130
+ );
131
+ expect(missingMessages).toHaveLength(0);
132
+ });
77
133
  });
134
+
135
+ let logState: LogState = createLogState();
@@ -0,0 +1,57 @@
1
+ import { EntityRuntimeKey } from '@flighthq/entity/contract';
2
+ import type { GlyphEntry, GlyphSource } from '@flighthq/glyphatlas/contract';
3
+ import { describe, expect, it } from 'vitest';
4
+
5
+ import { explainBitmapTextMissingGlyphs } from './explainBitmapTextMissingGlyphs.ts';
6
+
7
+ function createSource(codepoints: readonly number[]): GlyphSource {
8
+ const entries = new Map<number, GlyphEntry>();
9
+ for (const cp of codepoints) {
10
+ entries.set(cp, { advance: 10, bearingX: 0, bearingY: 8, height: 8, page: 0, width: 6, x: 0, y: 0 });
11
+ }
12
+ return {
13
+ [EntityRuntimeKey]: undefined,
14
+ getGlyphAtlasImage: () => null,
15
+ getGlyphEntry: (cp) => entries.get(cp) ?? null,
16
+ getGlyphKerning: () => 0,
17
+ getGlyphLayoutVersion: () => 0,
18
+ getGlyphMetrics: () => ({ ascent: 8, descent: 2, lineGap: 0 }),
19
+ };
20
+ }
21
+
22
+ describe('explainBitmapTextMissingGlyphs', () => {
23
+ it('returns empty when all codepoints have glyphs', () => {
24
+ const source = createSource([0x41, 0x42]);
25
+ const result = explainBitmapTextMissingGlyphs(source, 'AB');
26
+ expect(result.missingCodepoints).toEqual([]);
27
+ expect(result.totalCodepoints).toBe(2);
28
+ });
29
+
30
+ it('reports missing codepoints', () => {
31
+ const source = createSource([0x41]);
32
+ const result = explainBitmapTextMissingGlyphs(source, 'ABC');
33
+ expect(result.missingCodepoints).toEqual([0x42, 0x43]);
34
+ expect(result.totalCodepoints).toBe(3);
35
+ });
36
+
37
+ it('deduplicates repeated missing codepoints', () => {
38
+ const source = createSource([0x41]);
39
+ const result = explainBitmapTextMissingGlyphs(source, 'ABBA');
40
+ expect(result.missingCodepoints).toEqual([0x42]);
41
+ expect(result.totalCodepoints).toBe(4);
42
+ });
43
+
44
+ it('skips newlines and carriage returns', () => {
45
+ const source = createSource([0x41, 0x42]);
46
+ const result = explainBitmapTextMissingGlyphs(source, 'A\nB\r');
47
+ expect(result.missingCodepoints).toEqual([]);
48
+ expect(result.totalCodepoints).toBe(2);
49
+ });
50
+
51
+ it('returns zero totals for empty string', () => {
52
+ const source = createSource([]);
53
+ const result = explainBitmapTextMissingGlyphs(source, '');
54
+ expect(result.missingCodepoints).toEqual([]);
55
+ expect(result.totalCodepoints).toBe(0);
56
+ });
57
+ });
@@ -1,23 +1,28 @@
1
1
  import {
2
2
  createGlyphAtlas,
3
3
  createGlyphSourceFromGlyphAtlas,
4
- createStubGlyphRasterizerBackend,
4
+ allocateStubGlyphRasterizerBackend,
5
5
  getGlyphAtlasBitmap,
6
6
  } from '@flighthq/glyphatlas/contract';
7
+ import type { GlyphAtlas, GlyphEntry, GlyphSource } from '@flighthq/glyphatlas/contract';
8
+ import type { ImageResource } from '@flighthq/image/contract';
7
9
  import { getTextureSource } from '@flighthq/texture/contract';
8
- import type {
9
- Bitmap,
10
- GlyphAtlas,
11
- GlyphEntry,
12
- GlyphRasterizerBackend,
13
- GlyphSource,
14
- ImageResource,
15
- } from '@flighthq/types/contract';
16
- import { EntityRuntimeKey } from '@flighthq/types/contract';
17
10
  import { afterEach, describe, expect, it } from 'vitest';
18
11
 
19
- import { createBitmapText, getBitmapTextBounds, getBitmapTextPages, isBitmapTextGlyphLayoutStale } from './bitmapText';
20
- import { refreshBitmapTextGlyphLayout, setBitmapTextLayoutGuard, updateBitmapText } from './updateBitmapText';
12
+ import {
13
+ createBitmapText,
14
+ getBitmapTextBounds,
15
+ getBitmapTextLineCount,
16
+ getBitmapTextPages,
17
+ isBitmapTextGlyphLayoutStale,
18
+ isBitmapTextTruncated,
19
+ } from './bitmapText.ts';
20
+ import {
21
+ refreshBitmapTextGlyphLayout,
22
+ setBitmapTextLayoutGuard,
23
+ setBitmapTextMissingGlyphGuard,
24
+ updateBitmapText,
25
+ } from './updateBitmapText.ts';
21
26
 
22
27
  // A deterministic single-page glyph source: every visible glyph is 6x8 with advance 10 and bearingY 8
23
28
  // (so line tops sit at y=0), a space advances 5 with no pixels, and the pair (A, B) kerns by -2. All
@@ -66,10 +71,10 @@ function createTwoPageGlyphSource(): { source: GlyphSource; page0Image: ImageRes
66
71
  // repack every rect is still a well-formed rect over real pixels, so only reading the pixels can tell a
67
72
  // correct region from one that now covers the wrong glyph. Codepoints stay under 0x100 for the channel
68
73
  // to hold them exactly.
69
- function createCodepointColorRasterizerBackend(size: number): GlyphRasterizerBackend {
74
+ function createCodepointColorRasterizerBackend(size: number): HostGlyphRasterizerCapability {
70
75
  return {
71
76
  rasterize(codepoint) {
72
- const pixels = new Uint8ClampedArray(size * size * 4);
77
+ const pixels = new Uint8Array(size * size * 4);
73
78
  for (let i = 0; i < size * size; i++) {
74
79
  pixels[i * 4] = codepoint;
75
80
  pixels[i * 4 + 3] = 0xff;
@@ -230,6 +235,26 @@ describe('setBitmapTextLayoutGuard', () => {
230
235
  });
231
236
  });
232
237
 
238
+ describe('setBitmapTextMissingGlyphGuard', () => {
239
+ afterEach(() => setBitmapTextMissingGlyphGuard(null));
240
+
241
+ it('fires for each missing codepoint during layout', () => {
242
+ const seen: number[] = [];
243
+ setBitmapTextMissingGlyphGuard((cp) => seen.push(cp));
244
+ const source = createTestGlyphSource();
245
+ updateBitmapText(createBitmapText(source, { text: 'AC' }));
246
+ expect(seen).toEqual([0x43]);
247
+ });
248
+
249
+ it('stays silent when all codepoints have glyphs', () => {
250
+ const seen: number[] = [];
251
+ setBitmapTextMissingGlyphGuard((cp) => seen.push(cp));
252
+ const source = createTestGlyphSource();
253
+ updateBitmapText(createBitmapText(source, { text: 'AB' }));
254
+ expect(seen).toEqual([]);
255
+ });
256
+ });
257
+
233
258
  describe('updateBitmapText', () => {
234
259
  // A repack can land DURING a layout — a glyph late in the string evicting one placed early in it —
235
260
  // and the pass that saw both placements is wrong about the first. The retry is what makes a single
@@ -289,7 +314,7 @@ describe('updateBitmapText', () => {
289
314
  fontFamily: 'unavailable',
290
315
  fontSize: 24,
291
316
  height: 256,
292
- rasterizerBackend: createStubGlyphRasterizerBackend(),
317
+ rasterizerBackend: allocateStubGlyphRasterizerBackend(),
293
318
  width: 256,
294
319
  });
295
320
  const text = createBitmapText(createGlyphSourceFromGlyphAtlas(atlas), { text: 'Hi' });
@@ -477,8 +502,74 @@ describe('updateBitmapText', () => {
477
502
  expect(bounds.width).toBe(16); // B (page 1) at x=10, width 6 → right edge 16
478
503
  expect(bounds.height).toBe(8);
479
504
  });
505
+
506
+ it('limits visible lines to maxLines', () => {
507
+ const text = createBitmapText(createTestGlyphSource(), { text: 'A\nB\nA', maxLines: 2 });
508
+ updateBitmapText(text);
509
+ expect(getBitmapTextLineCount(text)).toBe(2);
510
+ expect(isBitmapTextTruncated(text)).toBe(true);
511
+ const page = getBitmapTextPages(text)[0]!;
512
+ expect(page.instanceCount).toBe(2);
513
+ });
514
+
515
+ it('does not truncate when line count fits within maxLines', () => {
516
+ const text = createBitmapText(createTestGlyphSource(), { text: 'A\nB', maxLines: 3 });
517
+ updateBitmapText(text);
518
+ expect(getBitmapTextLineCount(text)).toBe(2);
519
+ expect(isBitmapTextTruncated(text)).toBe(false);
520
+ const page = getBitmapTextPages(text)[0]!;
521
+ expect(page.instanceCount).toBe(2);
522
+ });
523
+
524
+ it('truncates word-wrapped lines at maxLines', () => {
525
+ const text = createBitmapText(createTestGlyphSource(), { text: 'AA AA AA', wrapWidth: 30, maxLines: 2 });
526
+ updateBitmapText(text);
527
+ expect(getBitmapTextLineCount(text)).toBe(2);
528
+ expect(isBitmapTextTruncated(text)).toBe(true);
529
+ });
530
+
531
+ it('appends ellipsis glyphs to the last visible line when truncated', () => {
532
+ const source = createTestGlyphSource();
533
+ const text = createBitmapText(source, { text: 'A\nB\nA', maxLines: 1, ellipsis: 'B' });
534
+ updateBitmapText(text);
535
+ const page = getBitmapTextPages(text)[0]!;
536
+ expect(page.instanceCount).toBe(2);
537
+ expect(isBitmapTextTruncated(text)).toBe(true);
538
+ });
539
+
540
+ it('treats tab as whitespace word break', () => {
541
+ const text = createBitmapText(createTestGlyphSource(), { text: 'A\tB' });
542
+ updateBitmapText(text);
543
+ const page = getBitmapTextPages(text)[0]!;
544
+ expect(page.instanceCount).toBe(2);
545
+ expect(page.transforms[0]).toBe(0);
546
+ expect(page.transforms[2]).toBeGreaterThan(0);
547
+ });
548
+
549
+ it('treats non-breaking space as whitespace word break', () => {
550
+ const text = createBitmapText(createTestGlyphSource(), { text: 'A B' });
551
+ updateBitmapText(text);
552
+ const page = getBitmapTextPages(text)[0]!;
553
+ expect(page.instanceCount).toBe(2);
554
+ });
555
+
556
+ it('sets lineCount to 0 for empty text', () => {
557
+ const text = createBitmapText(createTestGlyphSource(), { text: '' });
558
+ updateBitmapText(text);
559
+ expect(getBitmapTextLineCount(text)).toBe(0);
560
+ expect(isBitmapTextTruncated(text)).toBe(false);
561
+ });
562
+
563
+ it('reports lineCount accurately for multi-line text', () => {
564
+ const text = createBitmapText(createTestGlyphSource(), { text: 'A\nB\nA\nB' });
565
+ updateBitmapText(text);
566
+ expect(getBitmapTextLineCount(text)).toBe(4);
567
+ });
480
568
  });
481
569
 
482
570
  // Where the mid-layout relocation moves 'A' to — distinct from every rect the source starts with, so
483
571
  // a region carrying it can only have come from a pass that ran AFTER the move.
484
572
  const RELOCATED_GLYPH_X = 17;
573
+ import type { Bitmap } from '@flighthq/bitmap/contract';
574
+ import { EntityRuntimeKey } from '@flighthq/entity/contract';
575
+ import type { HostGlyphRasterizerCapability } from '@flighthq/font/contract';