@mizchi/vlmkit-core 0.8.0 → 0.8.1

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 (66) hide show
  1. package/dist/a11y-semantic.d.mts +34 -0
  2. package/dist/a11y-semantic.mjs +261 -0
  3. package/dist/authored-style-capture.d.mts +42 -0
  4. package/dist/authored-style-capture.mjs +117 -0
  5. package/dist/authored-style-diff.d.mts +15 -0
  6. package/dist/authored-style-diff.mjs +16 -0
  7. package/dist/cli-args.d.mts +11 -0
  8. package/dist/cli-args.mjs +21 -0
  9. package/dist/cli-error.d.mts +4 -0
  10. package/dist/cli-error.mjs +59 -0
  11. package/dist/computed-style-capture.d.mts +41 -0
  12. package/dist/computed-style-capture.mjs +589 -0
  13. package/dist/computed-style-diff.d.mts +83 -0
  14. package/dist/computed-style-diff.mjs +123 -0
  15. package/dist/diff-regions.d.mts +8 -0
  16. package/dist/diff-regions.mjs +52 -0
  17. package/dist/dom-equivalence.d.mts +51 -0
  18. package/dist/dom-equivalence.mjs +91 -0
  19. package/dist/dom-position-styles.d.mts +166 -0
  20. package/dist/dom-position-styles.mjs +296 -0
  21. package/dist/element-compare.d.mts +71 -0
  22. package/dist/element-compare.mjs +269 -0
  23. package/dist/heatmap-regions.d.mts +57 -0
  24. package/dist/heatmap-regions.mjs +158 -0
  25. package/dist/heatmap.d.mts +86 -0
  26. package/dist/heatmap.mjs +510 -0
  27. package/dist/image-resize.d.mts +34 -0
  28. package/dist/image-resize.mjs +92 -0
  29. package/dist/index.d.mts +15 -0
  30. package/dist/index.mjs +14 -0
  31. package/dist/mask.d.mts +18 -0
  32. package/dist/mask.mjs +27 -0
  33. package/dist/png-utils.d.mts +30 -0
  34. package/dist/png-utils.mjs +80 -0
  35. package/dist/region-shift.d.mts +20 -0
  36. package/dist/region-shift.mjs +128 -0
  37. package/dist/run-ledger.d.mts +14 -0
  38. package/dist/run-ledger.mjs +31 -0
  39. package/dist/terminal-colors.d.mts +12 -0
  40. package/dist/terminal-colors.mjs +14 -0
  41. package/dist/text-rows.d.mts +121 -0
  42. package/dist/text-rows.mjs +246 -0
  43. package/{src/types.ts → dist/types.d.mts} +88 -206
  44. package/dist/types.mjs +1 -0
  45. package/package.json +17 -5
  46. package/src/a11y-semantic.ts +0 -384
  47. package/src/authored-style-capture.ts +0 -185
  48. package/src/authored-style-diff.ts +0 -32
  49. package/src/cli-args.ts +0 -29
  50. package/src/cli-error.ts +0 -85
  51. package/src/computed-style-capture.ts +0 -717
  52. package/src/computed-style-diff.ts +0 -192
  53. package/src/diff-regions.ts +0 -70
  54. package/src/dom-equivalence.ts +0 -158
  55. package/src/dom-position-styles.ts +0 -457
  56. package/src/element-compare.ts +0 -354
  57. package/src/heatmap-regions.ts +0 -245
  58. package/src/heatmap.ts +0 -670
  59. package/src/image-resize.ts +0 -112
  60. package/src/index.ts +0 -105
  61. package/src/mask.ts +0 -41
  62. package/src/png-utils.ts +0 -86
  63. package/src/region-shift.ts +0 -173
  64. package/src/run-ledger.ts +0 -39
  65. package/src/terminal-colors.ts +0 -13
  66. package/src/text-rows.ts +0 -412
@@ -0,0 +1,246 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { PNG } from "pngjs";
3
+ //#region src/text-rows.ts
4
+ /**
5
+ * Text-row y-position extraction.
6
+ *
7
+ * Subagent F's wish-list item: "I need 'the `$24` text row is at y=420
8
+ * in the reference but y=380 in mine' as a first-class signal — without
9
+ * needing DOM correspondence."
10
+ *
11
+ * Compute the per-row mean luminance of an RGBA image. Text rows are
12
+ * darker than the surrounding whitespace (assuming light background;
13
+ * the dark-bg case is handled by symmetry: we just look for *dips*
14
+ * relative to the local average, regardless of polarity). Group
15
+ * consecutive dark rows into "text bands" and report each band's
16
+ * y-center.
17
+ *
18
+ * Pair baseline rows with variant rows by order (band #N in baseline ↔
19
+ * band #N in variant) and emit Δy. When the agent's rendering has the
20
+ * right *number* of text rows but wrong y-positions, this surfaces the
21
+ * shift directly.
22
+ */
23
+ const DEFAULT_MIN_LUMA_DIP = 12;
24
+ const DEFAULT_MIN_BAND_HEIGHT = 4;
25
+ const DEFAULT_MAX_BANDS = 64;
26
+ function rowStats(data, width, height) {
27
+ const mean = new Float32Array(height);
28
+ const range = new Float32Array(height);
29
+ for (let y = 0; y < height; y++) {
30
+ let sum = 0, mn = 255, mx = 0;
31
+ const rowStart = y * width * 4;
32
+ for (let x = 0; x < width; x++) {
33
+ const idx = rowStart + x * 4;
34
+ const L = .299 * data[idx] + .587 * data[idx + 1] + .114 * data[idx + 2];
35
+ sum += L;
36
+ if (L < mn) mn = L;
37
+ if (L > mx) mx = L;
38
+ }
39
+ mean[y] = sum / width;
40
+ range[y] = mx - mn;
41
+ }
42
+ return {
43
+ mean,
44
+ range
45
+ };
46
+ }
47
+ function median(values) {
48
+ if (values.length === 0) return 0;
49
+ const sorted = Float32Array.from(values).sort();
50
+ const mid = sorted.length >> 1;
51
+ return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
52
+ }
53
+ /** Snap a raw px height to the nearest common UI font-size bucket. */
54
+ function snapFontSize(rawPx) {
55
+ const buckets = [
56
+ 10,
57
+ 11,
58
+ 12,
59
+ 13,
60
+ 14,
61
+ 16,
62
+ 18,
63
+ 20,
64
+ 22,
65
+ 24,
66
+ 28,
67
+ 32,
68
+ 36,
69
+ 40,
70
+ 48,
71
+ 56,
72
+ 64,
73
+ 72
74
+ ];
75
+ let best = buckets[0];
76
+ let bestDist = Math.abs(rawPx - best);
77
+ for (const b of buckets) {
78
+ const d = Math.abs(rawPx - b);
79
+ if (d < bestDist) {
80
+ best = b;
81
+ bestDist = d;
82
+ }
83
+ }
84
+ return best;
85
+ }
86
+ function bucketWeight(density) {
87
+ if (density < .15) return "light";
88
+ if (density < .27) return "regular";
89
+ if (density < .36) return "medium";
90
+ return "bold";
91
+ }
92
+ /**
93
+ * Compute typography hints (font-size estimate, ink density, weight
94
+ * bucket) for a band by sampling its pixels. Mutates the band in
95
+ * place. From subagent G v3 dogfood: "next blocker is sub-pixel
96
+ * vertical spacing tuning — and a font-size / weight estimate per
97
+ * heatmap region would push this to < 1% in one more round."
98
+ */
99
+ function annotateTypography(band, data, width, height) {
100
+ const inkThreshold = Math.max(40, band.meanLuma - 50);
101
+ let inkLeft = width, inkRight = -1;
102
+ let inkPixels = 0;
103
+ for (let y = band.yStart; y <= band.yEnd && y < height; y++) for (let x = 0; x < width; x++) {
104
+ const i = (y * width + x) * 4;
105
+ if (.299 * data[i] + .587 * data[i + 1] + .114 * data[i + 2] < inkThreshold) {
106
+ if (x < inkLeft) inkLeft = x;
107
+ if (x > inkRight) inkRight = x;
108
+ inkPixels++;
109
+ }
110
+ }
111
+ if (inkRight < inkLeft) return;
112
+ const inkArea = (inkRight - inkLeft + 1) * (band.yEnd - band.yStart + 1);
113
+ const density = inkArea > 0 ? inkPixels / inkArea : 0;
114
+ band.estimatedFontSize = snapFontSize(band.height / .92);
115
+ band.inkDensity = density;
116
+ band.weightBucket = bucketWeight(density);
117
+ band.inkLeft = inkLeft;
118
+ band.inkRight = inkRight;
119
+ }
120
+ function extractTextRowsFromRgba(data, width, height, options = {}) {
121
+ if (width <= 0 || height <= 0) return [];
122
+ const minDip = options.minLumaDip ?? DEFAULT_MIN_LUMA_DIP;
123
+ const minBandH = options.minBandHeight ?? DEFAULT_MIN_BAND_HEIGHT;
124
+ const maxBands = options.maxBands ?? DEFAULT_MAX_BANDS;
125
+ const MIN_INK_RANGE = 80;
126
+ const { mean: rowMeans, range: rowRanges } = rowStats(data, width, height);
127
+ const med = median(rowMeans);
128
+ const bands = [];
129
+ let runStart = -1;
130
+ let runSum = 0;
131
+ for (let y = 0; y < height; y++) {
132
+ const meanDip = med - rowMeans[y] >= minDip;
133
+ const hasInk = rowRanges[y] >= MIN_INK_RANGE;
134
+ if (meanDip || hasInk) {
135
+ if (runStart < 0) {
136
+ runStart = y;
137
+ runSum = 0;
138
+ }
139
+ runSum += rowMeans[y];
140
+ } else if (runStart >= 0) {
141
+ const bandH = y - runStart;
142
+ if (bandH >= minBandH) bands.push({
143
+ yStart: runStart,
144
+ yEnd: y - 1,
145
+ yCenter: Math.round((runStart + y - 1) / 2),
146
+ height: bandH,
147
+ meanLuma: runSum / bandH
148
+ });
149
+ runStart = -1;
150
+ }
151
+ }
152
+ if (runStart >= 0) {
153
+ const bandH = height - runStart;
154
+ if (bandH >= minBandH) bands.push({
155
+ yStart: runStart,
156
+ yEnd: height - 1,
157
+ yCenter: Math.round((runStart + height - 1) / 2),
158
+ height: bandH,
159
+ meanLuma: runSum / bandH
160
+ });
161
+ }
162
+ const capped = bands.slice(0, maxBands);
163
+ for (const band of capped) annotateTypography(band, data, width, height);
164
+ return capped;
165
+ }
166
+ async function extractTextRowsFromFile(path, options = {}) {
167
+ const buf = await readFile(path);
168
+ const png = PNG.sync.read(buf);
169
+ return extractTextRowsFromRgba(png.data, png.width, png.height, options);
170
+ }
171
+ /**
172
+ * Pair text rows by ordered index. When baseline has 6 rows and variant
173
+ * has 6 rows, this maps row #0 → row #0, #1 → #1, etc. When the counts
174
+ * differ, only the prefix common to both is matched.
175
+ *
176
+ * Reports rows with |Δy| above `minDeltaY` to keep the table focused on
177
+ * actionable shifts (small subpixel deltas are excluded by default).
178
+ */
179
+ function matchTextRows(baseline, variant, minDeltaY = 2) {
180
+ const out = [];
181
+ const n = Math.min(baseline.length, variant.length);
182
+ for (let i = 0; i < n; i++) {
183
+ const dy = variant[i].yCenter - baseline[i].yCenter;
184
+ if (Math.abs(dy) >= minDeltaY) out.push({
185
+ rank: i,
186
+ baseline: baseline[i],
187
+ variant: variant[i],
188
+ deltaY: dy
189
+ });
190
+ }
191
+ return out;
192
+ }
193
+ /**
194
+ * Compare typography hints across ordered baseline ↔ variant pairs.
195
+ * Reports rows where:
196
+ * - estimated font-size buckets differ (different snapped px), OR
197
+ * - weight buckets differ ("regular" vs "bold").
198
+ *
199
+ * Same-content assumption: when baseline and variant render the same
200
+ * text content, ink density is directly comparable. When content
201
+ * differs, density can vary by character makeup ("iii" vs "WWW")
202
+ * and the comparison is noisier — only large bucket jumps are
203
+ * surfaced.
204
+ */
205
+ function compareRowTypography(baseline, variant, minDensityDelta = .04) {
206
+ const n = Math.min(baseline.length, variant.length);
207
+ const out = [];
208
+ for (let i = 0; i < n; i++) {
209
+ const b = baseline[i], v = variant[i];
210
+ if (b.estimatedFontSize === void 0 || v.estimatedFontSize === void 0) continue;
211
+ const sizeDiffers = b.estimatedFontSize !== v.estimatedFontSize;
212
+ const densityDelta = Math.abs((b.inkDensity ?? 0) - (v.inkDensity ?? 0));
213
+ const weightDiffers = b.weightBucket !== v.weightBucket && densityDelta >= minDensityDelta;
214
+ if (!sizeDiffers && !weightDiffers) continue;
215
+ out.push({
216
+ rank: i,
217
+ baselineFontSize: b.estimatedFontSize,
218
+ variantFontSize: v.estimatedFontSize,
219
+ baselineWeight: b.weightBucket,
220
+ variantWeight: v.weightBucket,
221
+ baselineDensity: b.inkDensity,
222
+ variantDensity: v.inkDensity,
223
+ kind: sizeDiffers && weightDiffers ? "both" : sizeDiffers ? "size" : "weight"
224
+ });
225
+ }
226
+ return out;
227
+ }
228
+ function computeRowGapDeltas(baseline, variant, minAbsoluteDelta = 2) {
229
+ const n = Math.min(baseline.length, variant.length);
230
+ const out = [];
231
+ for (let i = 0; i < n - 1; i++) {
232
+ const baseGap = baseline[i + 1].yCenter - baseline[i].yCenter;
233
+ const varGap = variant[i + 1].yCenter - variant[i].yCenter;
234
+ const delta = varGap - baseGap;
235
+ if (Math.abs(delta) >= minAbsoluteDelta) out.push({
236
+ aboveRank: i,
237
+ belowRank: i + 1,
238
+ baselineGap: baseGap,
239
+ variantGap: varGap,
240
+ delta
241
+ });
242
+ }
243
+ return out;
244
+ }
245
+ //#endregion
246
+ export { compareRowTypography, computeRowGapDeltas, extractTextRowsFromFile, extractTextRowsFromRgba, matchTextRows };