report-studio 0.0.1 → 1.0.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 (63) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +238 -4
  3. package/dist/designer.js +3209 -0
  4. package/dist/index.js +2 -4
  5. package/dist/viewer-D-c0lKzQ.js +1576 -0
  6. package/package.json +66 -33
  7. package/src/asset/images/icon.png +0 -0
  8. package/src/asset/images/logo.png +0 -0
  9. package/src/cli/index.js +282 -0
  10. package/src/cli/serve.js +135 -0
  11. package/src/designer/blank.js +53 -0
  12. package/src/designer/canvas.js +518 -0
  13. package/src/designer/designer.js +1900 -0
  14. package/src/designer/dialog.js +708 -0
  15. package/src/designer/fields.js +584 -0
  16. package/src/designer/geometry.js +154 -0
  17. package/src/designer/guides.js +370 -0
  18. package/src/designer/history.js +113 -0
  19. package/src/designer/icons.js +118 -0
  20. package/src/designer/index.html +28 -0
  21. package/src/designer/main.js +39 -0
  22. package/src/designer/menu.js +126 -0
  23. package/src/designer/panel.js +478 -0
  24. package/src/designer/preview.js +95 -0
  25. package/src/designer/rulers.js +231 -0
  26. package/src/designer/sample-data.js +240 -0
  27. package/src/designer/select.js +699 -0
  28. package/src/designer/structure.js +604 -0
  29. package/src/designer/styles.css +1616 -0
  30. package/src/designer/toast.js +62 -0
  31. package/src/designer/tokens.js +284 -0
  32. package/src/engine/csv.js +315 -0
  33. package/src/engine/group.js +304 -0
  34. package/src/engine/index.js +34 -0
  35. package/src/engine/measure.js +390 -0
  36. package/src/engine/paginate.js +861 -0
  37. package/src/engine/regions.js +175 -0
  38. package/src/engine/resolve.js +171 -0
  39. package/src/engine/search.js +129 -0
  40. package/src/engine/text-metrics.js +206 -0
  41. package/src/engine/validate.js +328 -0
  42. package/src/index.html +79 -0
  43. package/src/index.js +50 -0
  44. package/src/preview/chrome.css +505 -0
  45. package/src/preview/chrome.js +147 -0
  46. package/src/preview/index.html +28 -0
  47. package/src/preview/main.js +66 -0
  48. package/src/preview/styles.css +16 -0
  49. package/src/preview/viewer.js +745 -0
  50. package/src/render/items.js +425 -0
  51. package/src/render/render.js +98 -0
  52. package/src/render/report.css +202 -0
  53. package/src/server/routes.js +331 -0
  54. package/src/shared/brand.css +29 -0
  55. package/src/shared/dropdown.css +136 -0
  56. package/src/shared/dropdown.js +166 -0
  57. package/src/shared/modal.css +125 -0
  58. package/src/shared/prefix.js +13 -0
  59. package/src/shared/store.js +145 -0
  60. package/src/shared/theme.css +72 -0
  61. package/src/shared/window.js +138 -0
  62. package/src/vite/index.js +53 -0
  63. package/dist/react.js +0 -4
@@ -0,0 +1,861 @@
1
+
2
+ import { pageRegions, bandZoneHeight } from './regions.js';
3
+ import { designHeight, total, headerHeightFor } from './measure.js';
4
+
5
+
6
+ const REGEX = /\{([^{}]+)\}/g;
7
+
8
+ /** placeholders only this stage can resolve, once the page count is known */
9
+ const PAGE_KEYS = ['page', 'totalPages'];
10
+
11
+
12
+
13
+ export function paginate(json) {
14
+ const paginateJSON = structuredClone(json);
15
+
16
+
17
+ let context = {
18
+ page: paginateJSON.page,
19
+ bands: paginateJSON.bands,
20
+ group: paginateJSON.groupBy,
21
+ assignedBands: {
22
+ reportHeader: paginateJSON.bands.find(b => b.type === "reportHeader"),
23
+ reportFooter: paginateJSON.bands.find(b => b.type === "reportFooter"),
24
+ pageHeader: paginateJSON.bands.find(b => b.type === "pageHeader"),
25
+ pageFooter: paginateJSON.bands.find(b => b.type === "pageFooter"),
26
+ detail: paginateJSON.bands.find(b => b.type === "detail"),
27
+ groupHeader: paginateJSON.bands.find(b => b.type === "groupHeader") || null,
28
+ groupFooter: paginateJSON.bands.find(b => b.type === "groupFooter") || null,
29
+ }
30
+ };
31
+
32
+
33
+ context.availableHeight = calAvailableHeight(paginateJSON.page);
34
+
35
+ /**
36
+ * Bands are anchored to zones rather than stacked, so a short detail band
37
+ * no longer drags the page footer up off the bottom edge.
38
+ */
39
+ context.regionsFor = (pageNO, isLastPage) => pageRegions({
40
+ bands: context.assignedBands,
41
+ contentHeight: context.availableHeight,
42
+ isFirstPage: pageNO === 1,
43
+ isLastPage
44
+ });
45
+
46
+
47
+ const flowBands = paginateJSON.bands.filter(
48
+ b => ![
49
+ "reportHeader",
50
+ "reportFooter",
51
+ "pageHeader",
52
+ "pageFooter",
53
+ "groupHeader",
54
+ "groupFooter"
55
+ ].includes(b.type)
56
+ );
57
+
58
+ paginateJSON.pages = resolve(calPages(flowBands, context));
59
+
60
+ return paginateJSON;
61
+ }
62
+
63
+
64
+ /**
65
+ * How many rows starting at `from` fit in `room` px.
66
+ *
67
+ * A table used to be sliced with a division, because every row was the same
68
+ * height. A row is now as tall as its own text wraps to, so the only honest
69
+ * answer is to add them up until one does not fit.
70
+ *
71
+ * @param {number[]} heights every row's height, in order
72
+ * @param {number} from the first row still to be placed
73
+ * @param {number} room the space below the table's top on this page
74
+ * @param {number} [limit] never return more than this many
75
+ * @returns {number} rows to take, possibly zero
76
+ */
77
+ function rowsThatFit(heights, from, room, limit = Infinity) {
78
+ let used = 0;
79
+ let taken = 0;
80
+
81
+ while (from + taken < heights.length && taken < limit) {
82
+ const next = used + heights[from + taken];
83
+ if (next > room) break;
84
+
85
+ used = next;
86
+ taken++;
87
+ }
88
+
89
+ return taken;
90
+ }
91
+
92
+
93
+ /** the height of one slice of rows */
94
+ function sliceHeight(heights, from, count) {
95
+ return total(heights.slice(from, from + count));
96
+ }
97
+
98
+
99
+ /**
100
+ * A table's row heights, however the table came by them.
101
+ *
102
+ * measure.js writes them on; a table drawn straight from a layout - the
103
+ * designer canvas - has none, and every row is the height it declares.
104
+ *
105
+ * @param {object} item a table
106
+ * @param {object[]} rows the rows being placed
107
+ * @param {number[]} [heights] what measure.js worked out, if it ran
108
+ * @returns {number[]}
109
+ */
110
+ function heightsFor(item, rows, heights) {
111
+ if (Array.isArray(heights) && heights.length === (rows ?? []).length) {
112
+ return heights;
113
+ }
114
+
115
+ return (rows ?? []).map(() => item.rowHeight ?? 0);
116
+ }
117
+
118
+
119
+ function calPages(bands, context) {
120
+ let pages = [];
121
+
122
+ // initial page creation
123
+ let currentPage = newPage(1, context);
124
+ pages.push(currentPage);
125
+
126
+ // spreading the report header and footer from the context.assignedBands
127
+ const { assignedBands, availableHeight, group } = context;
128
+ const { reportFooter, reportHeader, groupHeader, groupFooter, pageHeader, pageFooter } = assignedBands;
129
+
130
+ /**
131
+ * The detail zone of a continuation page - no report header, no report
132
+ * footer, so it is the largest zone any page offers. Nothing can be split
133
+ * smaller than this, which makes it the yardstick for "will this fit at all".
134
+ */
135
+ const freshPageHeight = context.regionsFor(2, false).detail.height;
136
+
137
+ /** the report header now owns a zone; newPage placed it on page 1 already */
138
+ if (reportHeader && currentPage.regions.detail.height <= 0) {
139
+ console.warn(
140
+ `The header zones leave no room for detail on page 1. ` +
141
+ `Reduce the reportHeader or pageHeader height.`
142
+ );
143
+ }
144
+
145
+
146
+ // looping the flow band and calculation the items
147
+ // that can be fit inside the currentPage
148
+ // conditions : 1. when page has no more place left and incoming item is not table
149
+ // 2. when the is no more place and left and incoming item is table
150
+ // if the incoming item is table then checks the row length . and based on the available space allocate the row
151
+
152
+ /**
153
+ * The lowest edge, in band coordinates, that anything placed on the current
154
+ * page has reached. Items are drawn absolutely at their `y` inside the band
155
+ * box, so what an item costs its zone is `y + measuredHeight` - the same
156
+ * figure measure.js and render.js work from. Adding heights the way a flow
157
+ * layout would leaves the gap above an item unpaid for, and the band then
158
+ * runs past its zone straight over the page footer.
159
+ */
160
+ let bandBottom = 0;
161
+
162
+ /**
163
+ * How far the band's coordinate space has been shifted up on the current
164
+ * page. A band taller than a page is continued by moving whatever did not
165
+ * fit to the top of the next zone rather than redrawing it at the same `y`,
166
+ * which would only overflow the new page in the same place.
167
+ */
168
+ let carry = 0;
169
+
170
+ /**
171
+ * What every item placed on this page was designed to end at, and what it
172
+ * actually ended at - both in the band space of this page.
173
+ *
174
+ * Two kinds of item are only as tall as their data makes them: a table,
175
+ * which is its header plus however many rows arrive, and a text box, which
176
+ * wraps to as many lines as the words need. Neither is usually the last
177
+ * thing in a band - a total under the rows, a second line of the address
178
+ * under the first - and everything below was placed at the `y` it was
179
+ * given, which was measured against the size the designer drew.
180
+ *
181
+ * So what sits below something that grew has to float down with it, which
182
+ * is what this list is for.
183
+ */
184
+ let grown = [];
185
+
186
+
187
+ /** the detail zone of the page being filled, which page 1 may shrink */
188
+ const detailZone = () => currentPage.regions.detail.height;
189
+
190
+ /**
191
+ * Moves to a fresh page, handing the band built so far to the page being
192
+ * left behind. Everything a page break has to remember lives here so the
193
+ * call sites cannot drift apart.
194
+ * @param {object} calBand the band under construction
195
+ * @param {object} band its template, for the replacement
196
+ * @param {number} shiftTo the band-space y that becomes the top of the new page
197
+ * @returns {object} the new, empty calBand
198
+ */
199
+ const breakPage = (calBand, band, shiftTo) => {
200
+ if (calBand.items.length > 0) currentPage.bands.push(calBand);
201
+
202
+ currentPage = newPage(pages.length + 1, context);
203
+ pages.push(currentPage);
204
+ bandBottom = 0;
205
+ carry = shiftTo;
206
+
207
+ /** whatever grew on this page did not grow on the next one */
208
+ grown = [];
209
+
210
+ return { ...band, items: [] };
211
+ };
212
+
213
+ /**
214
+ * Records a placed item against the page and its band.
215
+ * @param {object} calBand
216
+ * @param {object} placed the item as it will be drawn, y and height final
217
+ */
218
+ const place = (calBand, placed) => {
219
+ calBand.items.push(placed);
220
+
221
+ bandBottom = Math.max(bandBottom, (placed.y ?? 0) + (placed.measuredHeight ?? 0));
222
+
223
+ calBand.measuredHeight = bandBottom;
224
+ currentPage.usedHeight = (availableHeight - detailZone()) + bandBottom;
225
+ };
226
+
227
+ /**
228
+ * How far down what is already on the page pushes something designed to
229
+ * start at `top`.
230
+ *
231
+ * Only what is *above* it counts, and above means the whole box is: an item
232
+ * level with a table's rows or beside a wrapping paragraph is not under it
233
+ * and is not moved by it.
234
+ *
235
+ * The shift is the difference between where those items were designed to
236
+ * end and where they really ended - not the sum of how much each one grew.
237
+ * Two paragraphs side by side that each wrap one extra line push what is
238
+ * under them down by one line, not by two; and a table with fewer rows than
239
+ * it was drawn with gives the space back rather than leaving a hole.
240
+ *
241
+ * @param {number} top the item's y, less the carry - so, on this page
242
+ * @returns {number} px, negative when what is above ended early
243
+ */
244
+ const shiftBelow = (top) => {
245
+ let designed = -Infinity;
246
+ let actual = -Infinity;
247
+
248
+ for (const box of grown) {
249
+ if (box.designBottom > top) continue;
250
+
251
+ designed = Math.max(designed, box.designBottom);
252
+ actual = Math.max(actual, box.actualBottom);
253
+ }
254
+
255
+ return designed === -Infinity ? 0 : actual - designed;
256
+ };
257
+
258
+ /**
259
+ * Where an item is drawn, in the band space of the page it lands on: the
260
+ * `y` in the file, less the carry of a band continued onto a new page, plus
261
+ * whatever grew above it. An item keeps the gap it was designed with - ten
262
+ * pixels under the last row stays ten pixels under the last row, however
263
+ * many rows there turn out to be.
264
+ *
265
+ * @param {number} itemY the item's y as designed
266
+ * @returns {number}
267
+ */
268
+ const pageY = (itemY) => {
269
+ const top = itemY - carry;
270
+ return top + shiftBelow(top);
271
+ };
272
+
273
+
274
+ for (const band of bands) {
275
+ let calBand = {
276
+ ...band,
277
+ items: []
278
+ };
279
+
280
+ /** the shift belongs to one band's coordinate space, not to the page */
281
+ carry = 0;
282
+
283
+ /** and so does what grew inside it */
284
+ grown = [];
285
+
286
+ /**
287
+ * Top to bottom, not file order. Items are absolutely positioned, so
288
+ * where one sits on the page is its `y` and nothing else - walking them
289
+ * in declaration order let a header text written after the table get
290
+ * carried to whatever page the table happened to end on.
291
+ */
292
+ const ordered = [...band.items].sort((a, b) => (a.y ?? 0) - (b.y ?? 0));
293
+
294
+ for (const item of ordered) {
295
+ const itemY = item.y ?? 0;
296
+ const localBottom = pageY(itemY) + item.measuredHeight;
297
+
298
+ if (localBottom > detailZone() && item.type != "table") {
299
+ /**
300
+ * An item taller than an empty zone overflows wherever it goes,
301
+ * so spending a page break on it only buys a blank page.
302
+ */
303
+ if (item.measuredHeight > freshPageHeight) {
304
+ console.warn(
305
+ `Item "${item.id}" is ${Math.ceil(item.measuredHeight)}px tall, past the ` +
306
+ `${freshPageHeight}px detail zone of an empty page; it will overflow.`
307
+ );
308
+ } else {
309
+ calBand = breakPage(calBand, band, itemY);
310
+ }
311
+
312
+ place(calBand, { ...structuredClone(item), y: pageY(itemY) });
313
+ }
314
+ else if (localBottom > detailZone() && item.type == "table") {
315
+
316
+ /**
317
+ * Where the next slice of this table starts inside the band. The
318
+ * first slice keeps the y the designer gave it, because whatever
319
+ * sits above it is on this page too. A continuation slice has
320
+ * nothing above it, so it starts at the top of the zone rather
321
+ * than leaving that gap blank on every page.
322
+ */
323
+ let tableY = itemY - carry;
324
+
325
+ // when the table has group
326
+ if (group !== null) {
327
+ /** reserved hgt for group table at least i a page we should fit this
328
+ * table header, group header, min of one row and the group footer
329
+ * also provide the table header hgt , group header and footer hgt
330
+ */
331
+ const grpHeaderHgt = groupHeader?.measuredHeight ?? 0;
332
+ const grpFooterHgt = groupFooter?.measuredHeight ?? 0;
333
+ const tblHeaderHgt = item.headerMeasured ?? headerHeightFor(item);
334
+
335
+ /** the tallest row anywhere in the table, since any group may hold it */
336
+ const tallest = Math.max(
337
+ item.rowHeight ?? 0,
338
+ ...(item.groups ?? []).flatMap(grp =>
339
+ heightsFor(item, grp.rows, grp.rowHeights)));
340
+
341
+ const reservedSpace =
342
+ tblHeaderHgt + grpHeaderHgt + tallest + grpFooterHgt;
343
+
344
+ /**
345
+ * same trap as the ungrouped path: if the minimum viable slice
346
+ * (table header + group header + one row + group footer) is taller
347
+ * than an empty page we would allocate pages forever.
348
+ */
349
+ const sliceFitsAnyPage = reservedSpace <= freshPageHeight;
350
+
351
+ if (!sliceFitsAnyPage) {
352
+ console.warn(
353
+ `Table "${item.id}": table header + group header + one row + group footer ` +
354
+ `(${reservedSpace}px) exceeds the ${freshPageHeight}px printable page. ` +
355
+ `Falling back to one row per page; rows will overflow their page.`
356
+ );
357
+ }
358
+
359
+ /**
360
+ * How many rows of a group fit in {room} px.
361
+ *
362
+ * The group footer only prints on the fragment that finishes the
363
+ * group, so it is reserved only when this page could actually
364
+ * finish it. Without that the last slice of a group added
365
+ * grpFooterHgt that nobody had budgeted for, and the detail band
366
+ * grew past its zone straight over the page footer.
367
+ *
368
+ * When the rows would fit but the footer would not, one row is
369
+ * deliberately held back so the footer lands on the next page
370
+ * beside a row rather than alone.
371
+ * @param {number[]} heights the group's row heights, in order
372
+ * @param {number} from the first row of this group still unplaced
373
+ * @param {number} room px left in the detail zone below the table top
374
+ * @param {number} rowsLeft rows still unplaced in this group
375
+ * @returns {number} rows to place now, possibly 0
376
+ */
377
+ const fitRows = (heights, from, room, rowsLeft) => {
378
+ const body = room - grpHeaderHgt - tblHeaderHgt;
379
+
380
+ const withFooter = rowsThatFit(
381
+ heights, from, body - grpFooterHgt, rowsLeft);
382
+
383
+ /** the group ends here, so its footer has to fit here too */
384
+ if (rowsLeft <= withFooter) return rowsLeft;
385
+
386
+ return Math.min(
387
+ rowsThatFit(heights, from, body, rowsLeft),
388
+ rowsLeft - 1
389
+ );
390
+ };
391
+
392
+
393
+ /**
394
+ * this groups variable helps us to know for current page how much groups are their
395
+ */
396
+ let calGroups = [];
397
+
398
+ let calItemHeight = 0; // used to cal the item hgt
399
+
400
+
401
+
402
+ /**
403
+ * Looping the group to allocate it to the bands
404
+ * so while looping we will gonna loop every groups (grp A, grp B etc...)
405
+ * at first push table and group header input the calBand
406
+ * then push the can fit rows to the calBand
407
+ */
408
+
409
+
410
+ for (const grp of item.groups) {
411
+
412
+
413
+ let currIdx = 0;
414
+ const rowLen = grp.rows.length;
415
+ const heights = heightsFor(item, grp.rows, grp.rowHeights);
416
+
417
+ while (currIdx < rowLen) {
418
+
419
+ /** the table starts at tableY, so only what is below it is usable */
420
+ const roomHgt = detailZone() - tableY - calItemHeight;
421
+
422
+ /** a page we have already emptied cannot be emptied again - take the slice as is */
423
+ const isFreshPage = calGroups.length === 0
424
+ && calBand.items.length === 0
425
+ && tableY === 0
426
+ && detailZone() >= freshPageHeight;
427
+
428
+ /**
429
+ * Rows this page can take. fitRows reserves the group
430
+ * footer whenever the group would end here, so the slice
431
+ * it hands back always fits the detail zone.
432
+ */
433
+ let canFitRows = fitRows(
434
+ heights, currIdx, roomHgt, rowLen - currIdx);
435
+
436
+ /**
437
+ * A page we have already emptied cannot be emptied again,
438
+ * so place a row regardless - an oversized slice overflows
439
+ * one page, while asking for another empty page forever
440
+ * hangs the whole render.
441
+ */
442
+ if (canFitRows < 1 && isFreshPage) canFitRows = 1;
443
+
444
+ /** nothing fits in what is left - carry the group to a new page */
445
+ if (canFitRows < 1) {
446
+ if (calGroups.length > 0) {
447
+ let dupItem = structuredClone(item);
448
+
449
+ dupItem.groups = calGroups;
450
+ dupItem.y = tableY;
451
+ dupItem.measuredHeight = calItemHeight;
452
+ place(calBand, dupItem);
453
+ }
454
+
455
+ calBand = breakPage(calBand, band, itemY);
456
+ calGroups = [];
457
+ calItemHeight = 0;
458
+ tableY = 0;
459
+ continue;
460
+ }
461
+
462
+ const rows = grp.rows.slice(currIdx, currIdx + canFitRows);
463
+ const slice = heights.slice(currIdx, currIdx + canFitRows);
464
+
465
+ currIdx += rows.length;
466
+
467
+ const hasMoreRows = currIdx < rowLen;
468
+
469
+ const occHgt = total(slice)
470
+ + (grpHeaderHgt + tblHeaderHgt)
471
+ + (!hasMoreRows ? grpFooterHgt : 0);
472
+
473
+ calItemHeight += occHgt;
474
+
475
+ /**
476
+ * groupHeader / groupFooter are templates, not flow bands - they never
477
+ * land on a page of their own. Resolve them per fragment here, where the
478
+ * group's own rows and aggregates are in hand, so render/ only has to draw.
479
+ */
480
+ let grpFragment = {
481
+ key: grp.key,
482
+ rows: rows,
483
+ rowHeights: slice,
484
+ aggregates: grp.aggregates,
485
+ showHeader: true,
486
+ showFooter: !hasMoreRows,
487
+ headerBand: resolveGroupBand(groupHeader, grp, rows[0]),
488
+ footerBand: !hasMoreRows
489
+ ? resolveGroupBand(groupFooter, grp, rows[0])
490
+ : null
491
+ }
492
+
493
+ calGroups.push(grpFragment);
494
+ }
495
+ }
496
+
497
+ if (calGroups.length > 0) {
498
+ const dupItem = structuredClone(item);
499
+
500
+ dupItem.groups = calGroups;
501
+ dupItem.y = tableY;
502
+ dupItem.measuredHeight = calItemHeight;
503
+
504
+ /** measure after the push so the band height includes its own table */
505
+ place(calBand, dupItem);
506
+
507
+ calGroups = [];
508
+ }
509
+
510
+ } else {
511
+ const rowLen = item.row.length;
512
+ let currIdx = 0;
513
+
514
+ const tblHeaderHgt = item.headerMeasured ?? headerHeightFor(item);
515
+
516
+ /** as tall as each row wrapped to, or its declared height */
517
+ const heights = heightsFor(item, item.row, item.rowHeights);
518
+
519
+ /**
520
+ * a row taller than an empty page can never be placed by the normal
521
+ * rule, and asking for a new page forever is an infinite loop.
522
+ * detect it once, up front, and fall back to one row per page.
523
+ *
524
+ * The tallest one is what decides it now: with wrapping, one
525
+ * row of a thousand can be the one that does not fit.
526
+ */
527
+ const tallest = Math.max(item.rowHeight ?? 0, ...heights);
528
+ const rowFitsAnyPage = freshPageHeight - tblHeaderHgt >= tallest;
529
+
530
+ if (!rowFitsAnyPage) {
531
+ console.warn(
532
+ `Table "${item.id}": its tallest row is ${Math.ceil(tallest)}px, ` +
533
+ `which with the ${tblHeaderHgt}px header exceeds the ` +
534
+ `${freshPageHeight}px printable page. Falling back to one row ` +
535
+ `per page; rows will overflow their page.`
536
+ );
537
+ }
538
+
539
+ while (currIdx < rowLen) {
540
+ /** the table starts at tableY, so only what is below it is usable */
541
+ const roomHgt = detailZone() - tableY;
542
+
543
+ let canFitRows = rowsThatFit(
544
+ heights, currIdx, roomHgt - tblHeaderHgt, rowLen - currIdx);
545
+
546
+ /** on a page we have already emptied, take one row anyway so currIdx advances */
547
+ const isFreshPage = calBand.items.length === 0
548
+ && tableY === 0
549
+ && detailZone() >= freshPageHeight;
550
+
551
+ if (canFitRows < 1 && isFreshPage) canFitRows = 1;
552
+
553
+ if (canFitRows < 1) {
554
+ /** anything already placed on this page must land before we leave it */
555
+ calBand = breakPage(calBand, band, itemY);
556
+ tableY = 0;
557
+ continue;
558
+ }
559
+
560
+ canFitRows = Math.min(canFitRows, rowLen - currIdx);
561
+
562
+ let dupItem = structuredClone(item);
563
+
564
+ const rows = item.row.slice(currIdx, currIdx + canFitRows);
565
+
566
+ const occHgt =
567
+ sliceHeight(heights, currIdx, canFitRows) + tblHeaderHgt;
568
+
569
+ dupItem.row = rows;
570
+ /** the slice carries its own heights, so the renderer needs no arithmetic */
571
+ dupItem.rowHeights = heights.slice(currIdx, currIdx + canFitRows);
572
+ dupItem.y = tableY;
573
+ dupItem.measuredHeight = occHgt;
574
+
575
+ place(calBand, dupItem);
576
+
577
+ currIdx += rows.length;
578
+ const hasMoreRows = currIdx < rowLen;
579
+
580
+ /**
581
+ * only push when we are leaving this page behind
582
+ * the final partial band is handed to the flush after the item loop
583
+ */
584
+ if (hasMoreRows) {
585
+ calBand = breakPage(calBand, band, itemY);
586
+ tableY = 0;
587
+ }
588
+ }
589
+ }
590
+
591
+ }
592
+ else {
593
+ /**
594
+ * a grouped table that never splits still has to reach render as
595
+ * fragments, otherwise its group bands only appear on reports long
596
+ * enough to overflow.
597
+ */
598
+ const placed = item.type === "table" && item.groups
599
+ ? { ...item, groups: wholeGroupFragments(item, groupHeader, groupFooter) }
600
+ : item;
601
+
602
+ place(calBand, { ...structuredClone(placed), y: pageY(itemY) });
603
+ }
604
+
605
+ /**
606
+ * The item has landed. Whatever it turned out to be worth is what
607
+ * the items still to be placed have to clear, so record it.
608
+ *
609
+ * It is read back out of the band rather than remembered, because
610
+ * the branches above place an item by several routes - whole,
611
+ * split, grouped - and what they all agree on is that the last
612
+ * thing to reach the band being built is this item.
613
+ */
614
+ const drawn = [...calBand.items]
615
+ .reverse()
616
+ .find(one => one.id === item.id);
617
+
618
+ if (drawn) {
619
+ grown.push({
620
+ designBottom: (itemY - carry) + designHeight(item),
621
+ actualBottom: (drawn.y ?? 0) + (drawn.measuredHeight ?? 0)
622
+ });
623
+ }
624
+ }
625
+
626
+ /** after the loop if their is any kind of data is their in calBand */
627
+ if (calBand.items.length > 0) {
628
+ currentPage.bands.push(calBand);
629
+ calBand = {
630
+ ...band, items: []
631
+ };
632
+ }
633
+
634
+ }
635
+
636
+
637
+ /**
638
+ * The report footer takes a zone above the page footer on the last page,
639
+ * which shrinks that page's detail zone. If the detail already filled more
640
+ * than the smaller zone allows, the footer starts a page of its own.
641
+ */
642
+ if (reportFooter) {
643
+ const zone = bandZoneHeight(reportFooter, availableHeight);
644
+
645
+ if (currentPage.usedHeight + zone > availableHeight) {
646
+ currentPage = newPage(pages.length + 1, context);
647
+ pages.push(currentPage);
648
+ }
649
+
650
+ currentPage.bands.push(structuredClone(reportFooter));
651
+ currentPage.usedHeight += zone;
652
+ }
653
+
654
+ /** the last page is only known now, so its zones are recomputed here */
655
+ const lastPage = pages[pages.length - 1];
656
+ lastPage.regions = context.regionsFor(lastPage.pageNO, Boolean(reportFooter));
657
+
658
+ anchorBands(pages);
659
+
660
+ return pages;
661
+ }
662
+
663
+
664
+ /**
665
+ * Gives every placed band the top of its zone, in coordinates relative to the
666
+ * printable area. This is what the renderer positions against, and it is the
667
+ * whole point of the exercise: the page footer's top comes from the bottom of
668
+ * the page, never from how far the detail happened to reach.
669
+ * @param {object[]} pages
670
+ */
671
+ function anchorBands(pages) {
672
+ for (const page of pages) {
673
+ for (const band of page.bands) {
674
+ const zone = page.regions[band.type] ?? page.regions.detail;
675
+
676
+ band.top = zone.top;
677
+ band.zoneHeight = zone.height;
678
+ }
679
+ }
680
+ }
681
+
682
+ /**
683
+ * Every group of a table that fits on one page, as unsplit fragments. Gives the
684
+ * no-overflow path the same shape the splitting path produces.
685
+ * @param {object} item the table item carrying groups
686
+ * @param {object|null} groupHeader
687
+ * @param {object|null} groupFooter
688
+ * @returns {object[]}
689
+ */
690
+ function wholeGroupFragments(item, groupHeader, groupFooter) {
691
+ return item.groups.map(grp => ({
692
+ key: grp.key,
693
+ rows: grp.rows,
694
+ rowHeights: grp.rowHeights,
695
+ aggregates: grp.aggregates,
696
+ showHeader: true,
697
+ showFooter: true,
698
+ headerBand: resolveGroupBand(groupHeader, grp, grp.rows[0]),
699
+ footerBand: resolveGroupBand(groupFooter, grp, grp.rows[0])
700
+ }));
701
+ }
702
+
703
+
704
+ /**
705
+ * Fills in a groupHeader / groupFooter template for one group fragment.
706
+ * Unlike the aggregate pass in group.js this keeps the literal text around a
707
+ * placeholder, so "Subtotal: {sum(price)}" stays "Subtotal: 160".
708
+ * @param {object|null} bandTemplate the shared groupHeader / groupFooter band
709
+ * @param {object} grp the group being placed, carrying its aggregates
710
+ * @param {object} sampleRow any row of the group - all share the groupBy value
711
+ * @returns {object|null} a resolved clone, or null when no template exists
712
+ */
713
+ function resolveGroupBand(bandTemplate, grp, sampleRow) {
714
+ if (!bandTemplate) return null;
715
+
716
+ const resolved = structuredClone(bandTemplate);
717
+
718
+ for (const item of resolved.items) {
719
+ if (item.type !== "text") continue;
720
+ /**
721
+ * Build on `text`, not `value` - resolve.js has already put the root
722
+ * data in and deliberately left everything group-scoped standing.
723
+ */
724
+ item.text = (item.text ?? item.value).replace(
725
+ REGEX,
726
+ (match, key) => groupPlaceholder(match, key.trim(), grp, sampleRow)
727
+ );
728
+ }
729
+
730
+ return resolved;
731
+ }
732
+
733
+
734
+ /**
735
+ * Resolves one placeholder inside a group band. An aggregate expression reads
736
+ * the group's own aggregates; anything else reads a field off the group's rows.
737
+ * {page} and {totalPages} are left standing - the page they land on is not
738
+ * known until every page exists, so resolve() fills those in at the end.
739
+ * @param {string} match the whole `{...}` token
740
+ * @param {string} key
741
+ * @param {object} grp
742
+ * @param {object} sampleRow
743
+ * @returns {string}
744
+ */
745
+ function groupPlaceholder(match, key, grp, sampleRow) {
746
+ if (PAGE_KEYS.includes(key)) return match;
747
+
748
+ const expr = key.match(/^(\w+)\((.*?)\)$/);
749
+
750
+ if (expr) {
751
+ const [, fn, field] = expr;
752
+ const value = grp.aggregates?.[field]?.[fn.trim()];
753
+ return value == null ? '' : String(value);
754
+ }
755
+
756
+ const value = sampleRow?.[key];
757
+ return value == null ? '' : String(value);
758
+ }
759
+
760
+
761
+ /**
762
+ * this is fn to create new pages whenever the report need
763
+ * so while creating the page itself we reserve the place for the
764
+ * header and footer of the page
765
+ * @param {Int16Array} pageNO
766
+ * @param {object} context
767
+ * @returns object
768
+ */
769
+ function newPage(pageNO, context) {
770
+ const { pageHeader, pageFooter, reportHeader } = context.assignedBands;
771
+
772
+ const regions = context.regionsFor(pageNO, false);
773
+ const bands = [];
774
+
775
+ if (pageHeader) bands.push(structuredClone(pageHeader));
776
+ if (pageFooter) bands.push(structuredClone(pageFooter));
777
+
778
+ /** the report header owns a zone of its own, on the first page only */
779
+ if (reportHeader && pageNO === 1) bands.push(structuredClone(reportHeader));
780
+
781
+ return {
782
+ pageNO,
783
+ bands,
784
+ regions,
785
+
786
+ /**
787
+ * Everything the header and footer zones have already taken. The loop
788
+ * reads capacity as availableHeight - usedHeight, which is now exactly
789
+ * the detail zone rather than whatever the stack happened to leave.
790
+ */
791
+ usedHeight: context.availableHeight - regions.detail.height
792
+ };
793
+ }
794
+
795
+
796
+ /**
797
+ * calculates the available space so assign the bands in a report
798
+ * so the available here actually calculated as
799
+ * page height - (every page will contain margin so) page margin
800
+ * here for margin we only consider the top and bottom because we are calculating the hgt
801
+ * @param {object} page
802
+ * @returns
803
+ */
804
+ function calAvailableHeight(page) {
805
+ return page.height - (page.margin.top + page.margin.bottom);
806
+ }
807
+
808
+ /**
809
+ * Fills in {page} and {totalPages}, which only become knowable once every page
810
+ * exists. Spec 3.4 allows both in any band, not just the page footer.
811
+ *
812
+ * This works on `text`, never on `value`. Re-resolving from `value` here would
813
+ * throw away everything resolve.js and group.js already put in, which is why
814
+ * those stages leave these two tokens standing rather than blanking them.
815
+ * @param {object[]} pages
816
+ * @returns {object[]} the same pages
817
+ */
818
+ function resolve(pages) {
819
+ for (const page of pages) {
820
+ for (const band of page.bands) {
821
+ resolvePageKeys(band, page.pageNO, pages.length);
822
+ }
823
+ }
824
+ return pages;
825
+ }
826
+
827
+
828
+ /**
829
+ * Walks one band, including the group header and footer bands carried inside a
830
+ * grouped table's fragments - those never appear in page.bands of their own.
831
+ * @param {object} band
832
+ * @param {number} pageNO
833
+ * @param {number} totalPages
834
+ */
835
+ function resolvePageKeys(band, pageNO, totalPages) {
836
+ for (const item of (band?.items ?? [])) {
837
+ if (item.type === "text") {
838
+ const source = item.text ?? item.value ?? '';
839
+ if (!source.includes('{')) continue;
840
+
841
+ item.text = source.replace(REGEX, (match, key) => {
842
+ switch (key.trim()) {
843
+ case "page":
844
+ return pageNO;
845
+ case "totalPages":
846
+ return totalPages;
847
+ default:
848
+ return match;
849
+ }
850
+ });
851
+ continue;
852
+ }
853
+
854
+ if (item.type === "table" && item.groups) {
855
+ for (const fragment of item.groups) {
856
+ resolvePageKeys(fragment.headerBand, pageNO, totalPages);
857
+ resolvePageKeys(fragment.footerBand, pageNO, totalPages);
858
+ }
859
+ }
860
+ }
861
+ }