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