pdf-kalem 0.1.1 → 0.5.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 (64) hide show
  1. package/README.md +33 -5
  2. package/dist/{Page-DqmtN2a2.d.ts → Page-Co_MgZDL.d.ts} +5 -1
  3. package/dist/{PdfDoc-KL5rp6kh.d.ts → PdfDoc-jjqPxRSC.d.ts} +20 -18
  4. package/dist/PdfPageSurface-1DVsuYJ2.d.ts +34 -0
  5. package/dist/{PdfRenderContext-DW6IqQ5q.d.ts → PdfRenderContext-C12DDr-w.d.ts} +20 -4
  6. package/dist/{Table-_ld_cPdR.d.ts → Table-DevmMUtU.d.ts} +1 -1
  7. package/dist/{doc-sizes-B1rAm5Po.d.ts → Widget-DzXseZEZ.d.ts} +68 -54
  8. package/dist/{chunk-4JABW7CO.js → chunk-5ZRRZRCD.js} +129 -359
  9. package/dist/chunk-5ZRRZRCD.js.map +1 -0
  10. package/dist/{chunk-PMF5IICV.js → chunk-6VRKPGB5.js} +75 -15
  11. package/dist/chunk-6VRKPGB5.js.map +1 -0
  12. package/dist/chunk-C4NWXBGN.js +58 -0
  13. package/dist/chunk-C4NWXBGN.js.map +1 -0
  14. package/dist/chunk-CGBZYXXD.js +81 -0
  15. package/dist/chunk-CGBZYXXD.js.map +1 -0
  16. package/dist/chunk-IGVDPDCZ.js +258 -0
  17. package/dist/chunk-IGVDPDCZ.js.map +1 -0
  18. package/dist/{chunk-FMU2QMVG.js → chunk-JU7VYYBJ.js} +10 -5
  19. package/dist/chunk-JU7VYYBJ.js.map +1 -0
  20. package/dist/chunk-LYSHPJ3R.js +107 -0
  21. package/dist/chunk-LYSHPJ3R.js.map +1 -0
  22. package/dist/{chunk-IAF6RHAY.js → chunk-OS4HN6KV.js} +62 -111
  23. package/dist/chunk-OS4HN6KV.js.map +1 -0
  24. package/dist/chunk-PCKIGCEJ.js +198 -0
  25. package/dist/chunk-PCKIGCEJ.js.map +1 -0
  26. package/dist/{chunk-AYT6JHLZ.js → chunk-PIBYCKYY.js} +15 -2
  27. package/dist/chunk-PIBYCKYY.js.map +1 -0
  28. package/dist/{chunk-4RZQEYIL.js → chunk-SEKYHTYU.js} +224 -81
  29. package/dist/chunk-SEKYHTYU.js.map +1 -0
  30. package/dist/doc-output-NLO_E7Qr.d.ts +34 -0
  31. package/dist/doc-sizes-BP4GVKGs.d.ts +14 -0
  32. package/dist/edit.d.ts +10 -3
  33. package/dist/edit.js +21 -5
  34. package/dist/edit.js.map +1 -1
  35. package/dist/forms.d.ts +294 -0
  36. package/dist/forms.js +324 -0
  37. package/dist/forms.js.map +1 -0
  38. package/dist/image-format-Dvz7_MH4.d.ts +8 -0
  39. package/dist/index.d.ts +10 -21
  40. package/dist/index.js +18 -13
  41. package/dist/index.js.map +1 -1
  42. package/dist/internals.d.ts +54 -10
  43. package/dist/internals.js +32 -5
  44. package/dist/markdown.d.ts +11 -13
  45. package/dist/markdown.js +75 -48
  46. package/dist/markdown.js.map +1 -1
  47. package/dist/preview.d.ts +38 -0
  48. package/dist/preview.js +76 -0
  49. package/dist/preview.js.map +1 -0
  50. package/dist/surface-hkLPm11r.d.ts +23 -0
  51. package/dist/{types-CWGD5wJk.d.ts → types-ChQa4dgU.d.ts} +5 -1
  52. package/dist/utils/color-utils.js +1 -1
  53. package/dist/widgets.d.ts +152 -25
  54. package/dist/widgets.js +170 -19
  55. package/dist/widgets.js.map +1 -1
  56. package/package.json +26 -4
  57. package/dist/chunk-4JABW7CO.js.map +0 -1
  58. package/dist/chunk-4RZQEYIL.js.map +0 -1
  59. package/dist/chunk-AYT6JHLZ.js.map +0 -1
  60. package/dist/chunk-B3LBPGQP.js +0 -112
  61. package/dist/chunk-B3LBPGQP.js.map +0 -1
  62. package/dist/chunk-FMU2QMVG.js.map +0 -1
  63. package/dist/chunk-IAF6RHAY.js.map +0 -1
  64. package/dist/chunk-PMF5IICV.js.map +0 -1
@@ -1,10 +1,14 @@
1
+ import {
2
+ Widget
3
+ } from "./chunk-PCKIGCEJ.js";
1
4
  import {
2
5
  PageScope,
3
- Paginator
4
- } from "./chunk-PMF5IICV.js";
6
+ Paginator,
7
+ canAddPages
8
+ } from "./chunk-6VRKPGB5.js";
5
9
  import {
6
10
  toPdfLibColor
7
- } from "./chunk-FMU2QMVG.js";
11
+ } from "./chunk-JU7VYYBJ.js";
8
12
 
9
13
  // src/lib/types/doc-fonts.ts
10
14
  import { StandardFonts } from "pdf-lib";
@@ -19,9 +23,18 @@ function faceId(family, style) {
19
23
  const suffix = STYLE_SUFFIXES.find(([s]) => s === style)?.[1];
20
24
  return suffix ? `${family}${suffix}` : family;
21
25
  }
22
- function combineFaceStyle(base, bold, italic) {
23
- const bits = (base === "bold" || base === "boldItalic" ? 1 : 0) | (base === "italic" || base === "boldItalic" ? 2 : 0) | (bold ? 1 : 0) | (italic ? 2 : 0);
24
- return bits === 0 ? "regular" : bits === 1 ? "bold" : bits === 2 ? "italic" : "boldItalic";
26
+ function faceStyleFromBits(bold, italic) {
27
+ return bold && italic ? "boldItalic" : bold ? "bold" : italic ? "italic" : "regular";
28
+ }
29
+ function snapFontWeight(weight) {
30
+ if (weight === "normal") return "regular";
31
+ if (weight === "bold") return "bold";
32
+ return weight >= 600 ? "bold" : "regular";
33
+ }
34
+ function resolveFaceStyle(base, weight, style) {
35
+ const bold = weight !== void 0 ? snapFontWeight(weight) === "bold" : base === "bold" || base === "boldItalic";
36
+ const italic = style !== void 0 ? style === "italic" : base === "italic" || base === "boldItalic";
37
+ return faceStyleFromBits(bold, italic);
25
38
  }
26
39
  function parseFaceId(id) {
27
40
  for (const [style, suffix] of STYLE_SUFFIXES) {
@@ -182,6 +195,7 @@ var FontRegistry = class _FontRegistry {
182
195
  fontSet.add(loadedFont);
183
196
  await fontSet.ready;
184
197
  } catch (err) {
198
+ this.browserFontLoads.delete(fontName);
185
199
  console.warn(`FontRegistry.loadBrowserFont: failed to load '${fontName}' into browser fonts`, err);
186
200
  }
187
201
  })();
@@ -241,6 +255,8 @@ var FontRegistry = class _FontRegistry {
241
255
  import {
242
256
  PDFArray,
243
257
  PDFName,
258
+ PDFObjectCopier,
259
+ PDFPage,
244
260
  PDFString,
245
261
  StandardFonts as StandardFonts3
246
262
  } from "pdf-lib";
@@ -267,6 +283,12 @@ var PdfRenderContext = class {
267
283
  fontkitRegisteredOnDoc = false;
268
284
  fontkitInstance;
269
285
  fonts;
286
+ /** User-space origin of the current page's MediaBox (0,0 for generated pages). */
287
+ pageOrigin = { x: 0, y: 0 };
288
+ /** Embedded images for the current document, keyed on the bytes identity. */
289
+ imageCache = /* @__PURE__ */ new WeakMap();
290
+ /** Copiers for adopted foreign documents (reset with the document). */
291
+ pageCopiers = /* @__PURE__ */ new Map();
270
292
  constructor(options) {
271
293
  this.options = options;
272
294
  this.page = null;
@@ -332,6 +354,14 @@ var PdfRenderContext = class {
332
354
  // --- PDF document / page lifecycle ---
333
355
  setDocument(doc) {
334
356
  this.doc = doc;
357
+ this.fontCache.clear();
358
+ this.textWidthCache.clear();
359
+ this.fontkitRegisteredOnDoc = false;
360
+ this.page = null;
361
+ this.dimensions = void 0;
362
+ this.pageOrigin = { x: 0, y: 0 };
363
+ this.imageCache = /* @__PURE__ */ new WeakMap();
364
+ this.pageCopiers = /* @__PURE__ */ new Map();
335
365
  }
336
366
  getDocument() {
337
367
  if (!this.doc) {
@@ -339,6 +369,15 @@ var PdfRenderContext = class {
339
369
  }
340
370
  return this.doc;
341
371
  }
372
+ /**
373
+ * The document's interactive form (created in the catalog on first use).
374
+ * Form widgets (`src/lib/forms/`) narrow to {@link PdfFormSurface} at render
375
+ * time and create their fields through it — PDF-typed per ADR 0001, never
376
+ * part of the neutral `RenderContext`.
377
+ */
378
+ getForm() {
379
+ return this.getDocument().getForm();
380
+ }
342
381
  addPage(dimensions) {
343
382
  const page = this.getDocument().addPage(dimensions);
344
383
  this.setCurrentPage(page);
@@ -350,6 +389,8 @@ var PdfRenderContext = class {
350
389
  setCurrentPage(currentPage) {
351
390
  this.page = currentPage;
352
391
  this.dimensions = [currentPage.getWidth(), currentPage.getHeight()];
392
+ const mediaBox = currentPage.getMediaBox();
393
+ this.pageOrigin = { x: mediaBox.x, y: mediaBox.y };
353
394
  }
354
395
  /**
355
396
  * Adopts a page from a foreign `PDFDocument` (e.g. one loaded by the edit
@@ -359,8 +400,24 @@ var PdfRenderContext = class {
359
400
  */
360
401
  async adoptPage(source, pageIndex) {
361
402
  const doc = this.getDocument();
362
- const [copied] = await doc.copyPages(source, [pageIndex]);
363
- this.setCurrentPage(doc.addPage(copied));
403
+ const src = source;
404
+ await src.flush();
405
+ const copier = this.copierFor(src);
406
+ const copiedPage = copier.copy(src.getPage(pageIndex).node);
407
+ const ref = doc.context.register(copiedPage);
408
+ this.setCurrentPage(doc.addPage(PDFPage.of(copiedPage, ref, doc)));
409
+ const acroForm = src.catalog.get(PDFName.of("AcroForm"));
410
+ if (acroForm) {
411
+ doc.catalog.set(PDFName.of("AcroForm"), copier.copy(acroForm));
412
+ }
413
+ }
414
+ copierFor(source) {
415
+ let copier = this.pageCopiers.get(source);
416
+ if (!copier) {
417
+ copier = PDFObjectCopier.for(source.context, this.getDocument().context);
418
+ this.pageCopiers.set(source, copier);
419
+ }
420
+ return copier;
364
421
  }
365
422
  getPageHeight() {
366
423
  return this.getCurrentPage()?.getSize().height || this.options.height || 0;
@@ -388,8 +445,8 @@ var PdfRenderContext = class {
388
445
  mapContentBox(box) {
389
446
  const pageHeight = this.getPageHeight();
390
447
  return {
391
- x: box.x,
392
- y: pageHeight - box.y - box.height,
448
+ x: box.x + this.pageOrigin.x,
449
+ y: pageHeight + this.pageOrigin.y - box.y - box.height,
393
450
  width: box.width,
394
451
  height: box.height
395
452
  };
@@ -409,21 +466,32 @@ var PdfRenderContext = class {
409
466
  }
410
467
  // --- Font embedding / measurement ---
411
468
  async getFont(fontName = StandardFonts3.Helvetica) {
412
- const requestedName = resolveBuiltinPdfFont(fontName) ?? StandardFonts3.Helvetica;
413
- const cached = this.fontCache.get(requestedName);
469
+ const cached = this.fontCache.get(fontName);
414
470
  if (cached) return cached;
415
471
  const doc = this.getDocument();
416
472
  const registry = FontRegistry.instance;
417
- let registered = this.fonts?.families.get(requestedName)?.regular;
473
+ const requestedName = resolveBuiltinPdfFont(fontName) ?? StandardFonts3.Helvetica;
474
+ let registered = this.fonts?.families.get(fontName)?.regular;
418
475
  let faceNote;
419
476
  if (!registered) {
420
- const face = parseFaceId(requestedName);
477
+ const face = parseFaceId(fontName);
421
478
  const variants = face ? this.fonts?.families.get(face.family) : void 0;
422
479
  if (face && variants) {
423
480
  const resolved = resolveFaceBytes(variants, face.style);
424
481
  registered = resolved.bytes;
425
482
  if (resolved.fellBack) {
426
- faceNote = `face '${requestedName}' is not available for family '${face.family}'; using its ${resolved.style} face`;
483
+ faceNote = `face '${fontName}' is not available for family '${face.family}'; using its ${resolved.style} face`;
484
+ }
485
+ }
486
+ }
487
+ if (!registered) {
488
+ const builtinFace = builtinPdfNameFace(fontName);
489
+ const variants = builtinFace ? this.fonts?.families.get(builtinFace.family) : void 0;
490
+ if (builtinFace && variants) {
491
+ const resolved = resolveFaceBytes(variants, builtinFace.style);
492
+ registered = resolved.bytes;
493
+ if (resolved.fellBack) {
494
+ faceNote = `face '${fontName}' is not available for family '${builtinFace.family}'; using its ${resolved.style} face`;
427
495
  }
428
496
  }
429
497
  }
@@ -451,21 +519,21 @@ var PdfRenderContext = class {
451
519
  this.fontkitRegisteredOnDoc = true;
452
520
  }
453
521
  const font2 = await doc.embedFont(registered);
454
- this.fontCache.set(requestedName, font2);
522
+ this.fontCache.set(fontName, font2);
455
523
  if (faceNote) {
456
524
  console.warn(`RenderContext.getFont: ${faceNote}.`);
457
525
  }
458
- await registry.loadBrowserFont(requestedName, registered);
526
+ await registry.loadBrowserFont(fontName, registered);
459
527
  return font2;
460
528
  }
461
529
  if (typeof requestedName === "string" && !FontRegistry.isStandardFontName(requestedName)) {
462
530
  console.warn(
463
- `RenderContext.getFont: font '${requestedName}' is not registered and is not a StandardFonts value. Falling back to '${StandardFonts3.Helvetica}'.`
531
+ `RenderContext.getFont: font '${fontName}' is not registered and is not a StandardFonts value. Falling back to '${StandardFonts3.Helvetica}'.`
464
532
  );
465
533
  return this.getFont(StandardFonts3.Helvetica);
466
534
  }
467
535
  const font = await doc.embedFont(requestedName);
468
- this.fontCache.set(requestedName, font);
536
+ this.fontCache.set(fontName, font);
469
537
  return font;
470
538
  }
471
539
  /**
@@ -504,13 +572,11 @@ var PdfRenderContext = class {
504
572
  if (cachedWidth !== void 0) {
505
573
  return cachedWidth;
506
574
  }
507
- let measuredWidth;
508
575
  const font = this.fontCache.get(fontName);
509
- if (font) {
510
- measuredWidth = font.widthOfTextAtSize(text, size);
511
- } else {
512
- measuredWidth = text.length * size * 0.6;
576
+ if (!font) {
577
+ return text.length * size * 0.6;
513
578
  }
579
+ const measuredWidth = font.widthOfTextAtSize(text, size);
514
580
  this.textWidthCache.set(cacheKey, measuredWidth);
515
581
  return measuredWidth;
516
582
  }
@@ -564,10 +630,18 @@ var PdfRenderContext = class {
564
630
  // --- Image embedding ---
565
631
  async embedImage(bytes, format) {
566
632
  const doc = this.getDocument();
567
- if (format === "png") {
568
- return doc.embedPng(bytes);
633
+ let byFormat = this.imageCache.get(bytes);
634
+ if (!byFormat) {
635
+ byFormat = /* @__PURE__ */ new Map();
636
+ this.imageCache.set(bytes, byFormat);
569
637
  }
570
- return doc.embedJpg(bytes);
638
+ const cached = byFormat.get(format);
639
+ if (cached) {
640
+ return cached;
641
+ }
642
+ const image = format === "png" ? await doc.embedPng(bytes) : await doc.embedJpg(bytes);
643
+ byFormat.set(format, image);
644
+ return image;
571
645
  }
572
646
  // --- Drawing primitives ---
573
647
  requireCurrentPage() {
@@ -657,320 +731,8 @@ var PDFDocSize = {
657
731
  // Letter size in points,
658
732
  };
659
733
 
660
- // src/lib/Widget.ts
661
- import Yoga, {
662
- Align,
663
- Direction,
664
- Edge,
665
- FlexDirection,
666
- Gutter,
667
- Justify,
668
- PositionType
669
- } from "yoga-layout";
670
- var Widget = class {
671
- children;
672
- node;
673
- context;
674
- parent;
675
- breakableOption;
676
- constructor(options = {}) {
677
- this.children = options.children || [];
678
- this.node = Yoga.Node.create();
679
- this.breakableOption = options.breakable;
680
- for (const child of this.children) {
681
- child.parent = this;
682
- this.node.insertChild(child.node, this.node.getChildCount());
683
- }
684
- }
685
- setContext(context) {
686
- this.context = context;
687
- for (const child of this.children) {
688
- child.setContext(context);
689
- }
690
- }
691
- /**
692
- * Optional async hook to load assets / set intrinsic sizes before Yoga layout.
693
- * Default implementation recurses into children.
694
- */
695
- async prepareLayout(context) {
696
- for (const child of this.children) {
697
- await child.prepareLayout(context);
698
- }
699
- }
700
- /**
701
- * Runs Yoga layout for this subtree (typically called once at the Page root).
702
- * `undefined` dimensions mean auto/intrinsic sizing (the pagination flow pass).
703
- */
704
- calculateLayout(width, height, direction = Direction.LTR) {
705
- this.node.calculateLayout(width, height, direction);
706
- }
707
- /**
708
- * The single styling entry: typed dispatch on `YogaStyle` keys. Unknown
709
- * keys are a compile error at typed call sites (no silent no-ops);
710
- * undefined/null values are skipped.
711
- */
712
- setYogaStyle(style) {
713
- for (const key of Object.keys(style)) {
714
- const value = style[key];
715
- if (value === void 0 || value === null) continue;
716
- this.applyStyle(key, value);
717
- }
718
- }
719
- applyStyle(key, value) {
720
- switch (key) {
721
- case "width":
722
- this.setDimension(
723
- value,
724
- (v) => this.node.setWidth(v),
725
- (v) => this.node.setWidthPercent(v),
726
- () => this.node.setWidthAuto()
727
- );
728
- return;
729
- case "height":
730
- this.setDimension(
731
- value,
732
- (v) => this.node.setHeight(v),
733
- (v) => this.node.setHeightPercent(v),
734
- () => this.node.setHeightAuto()
735
- );
736
- return;
737
- case "flexBasis":
738
- this.setDimension(
739
- value,
740
- (v) => this.node.setFlexBasis(v),
741
- (v) => this.node.setFlexBasisPercent(v),
742
- () => this.node.setFlexBasisAuto()
743
- );
744
- return;
745
- // Yoga's min/max setters accept numbers and percent strings natively.
746
- case "minWidth":
747
- this.node.setMinWidth(value);
748
- return;
749
- case "minHeight":
750
- this.node.setMinHeight(value);
751
- return;
752
- case "maxWidth":
753
- this.node.setMaxWidth(value);
754
- return;
755
- case "maxHeight":
756
- this.node.setMaxHeight(value);
757
- return;
758
- case "flexDirection":
759
- this.node.setFlexDirection(value === "row" ? FlexDirection.Row : FlexDirection.Column);
760
- return;
761
- case "justifyContent": {
762
- const map = {
763
- "flex-start": Justify.FlexStart,
764
- center: Justify.Center,
765
- "flex-end": Justify.FlexEnd,
766
- "space-between": Justify.SpaceBetween,
767
- "space-around": Justify.SpaceAround,
768
- "space-evenly": Justify.SpaceEvenly
769
- };
770
- this.node.setJustifyContent(map[value] ?? Justify.FlexStart);
771
- return;
772
- }
773
- case "alignItems": {
774
- const map = {
775
- "flex-start": Align.FlexStart,
776
- center: Align.Center,
777
- "flex-end": Align.FlexEnd,
778
- stretch: Align.Stretch
779
- };
780
- this.node.setAlignItems(map[value] ?? Align.FlexStart);
781
- return;
782
- }
783
- case "alignSelf": {
784
- const map = {
785
- auto: Align.Auto,
786
- "flex-start": Align.FlexStart,
787
- center: Align.Center,
788
- "flex-end": Align.FlexEnd,
789
- stretch: Align.Stretch
790
- };
791
- this.node.setAlignSelf(map[value] ?? Align.Auto);
792
- return;
793
- }
794
- case "flexGrow":
795
- this.node.setFlexGrow(value);
796
- return;
797
- case "flexShrink":
798
- this.node.setFlexShrink(value);
799
- return;
800
- case "gap": {
801
- if (typeof value === "string" && value.endsWith("%")) {
802
- const pct = Number(value.slice(0, -1));
803
- if (Number.isFinite(pct)) {
804
- this.node.setGapPercent?.(Gutter.All, pct);
805
- }
806
- return;
807
- }
808
- this.node.setGap(Gutter.All, value);
809
- return;
810
- }
811
- case "padding":
812
- this.node.setPadding(Edge.All, value);
813
- return;
814
- case "paddingTop":
815
- this.node.setPadding(Edge.Top, value);
816
- return;
817
- case "paddingRight":
818
- this.node.setPadding(Edge.Right, value);
819
- return;
820
- case "paddingBottom":
821
- this.node.setPadding(Edge.Bottom, value);
822
- return;
823
- case "paddingLeft":
824
- this.node.setPadding(Edge.Left, value);
825
- return;
826
- case "margin":
827
- this.node.setMargin(Edge.All, value);
828
- return;
829
- case "marginTop":
830
- this.node.setMargin(Edge.Top, value);
831
- return;
832
- case "marginRight":
833
- this.node.setMargin(Edge.Right, value);
834
- return;
835
- case "marginBottom":
836
- this.node.setMargin(Edge.Bottom, value);
837
- return;
838
- case "marginLeft":
839
- this.node.setMargin(Edge.Left, value);
840
- return;
841
- case "position":
842
- this.node.setPositionType(value === "absolute" ? PositionType.Absolute : PositionType.Relative);
843
- return;
844
- case "top":
845
- this.node.setPosition(Edge.Top, value);
846
- return;
847
- case "right":
848
- this.node.setPosition(Edge.Right, value);
849
- return;
850
- case "bottom":
851
- this.node.setPosition(Edge.Bottom, value);
852
- return;
853
- case "left":
854
- this.node.setPosition(Edge.Left, value);
855
- return;
856
- }
857
- }
858
- /**
859
- * The one shared dimension parser: `number` | `` `${number}%` `` | 'auto'
860
- * routed to the matching Yoga setter. Percent/auto only apply where the
861
- * caller passed a setter for them.
862
- */
863
- setDimension(value, set, setPercent, setAuto) {
864
- if (value === "auto") {
865
- setAuto?.();
866
- return;
867
- }
868
- if (typeof value === "string" && value.endsWith("%")) {
869
- const pct = Number(value.slice(0, -1));
870
- if (Number.isFinite(pct) && setPercent) {
871
- setPercent(pct);
872
- }
873
- return;
874
- }
875
- if (typeof value === "number" && Number.isFinite(value)) {
876
- set(value);
877
- }
878
- }
879
- /**
880
- * Post-layout computed size (0 before layout). Widgets with meaningful
881
- * intrinsics override — Text (measured lines), Table (row sums), and their
882
- * pagination callers rely on that.
883
- */
884
- getWidth() {
885
- return this.node.getComputedWidth();
886
- }
887
- getHeight() {
888
- return this.node.getComputedHeight();
889
- }
890
- async renderChildren(context) {
891
- const placed = context.getRenderChildren?.(this);
892
- for (const child of placed ?? this.children) {
893
- await child.render(context);
894
- }
895
- }
896
- getAbsoluteTopLeft() {
897
- let x = 0;
898
- let y = 0;
899
- let current = this;
900
- while (current) {
901
- x += current.node.getComputedLeft();
902
- y += current.node.getComputedTop();
903
- current = current.parent;
904
- }
905
- return { x, y };
906
- }
907
- getAbsoluteLayoutBox() {
908
- const { x, y } = this.getAbsoluteTopLeft();
909
- const width = this.node.getComputedWidth();
910
- const height = this.node.getComputedHeight();
911
- return { x, y, width, height };
912
- }
913
- // --- Package-internal accessors (used by the pagination module) ---
914
- getParent() {
915
- return this.parent;
916
- }
917
- getChildWidgets() {
918
- return this.children;
919
- }
920
- /** @internal — package-internal: append a child after construction (edit module overlays). */
921
- appendWidgetChild(widget) {
922
- widget.parent = this;
923
- this.children.push(widget);
924
- this.node.insertChild(widget.node, this.node.getChildCount());
925
- }
926
- /** Flow geometry from the last Yoga layout (positions are parent-border-box-relative). */
927
- getFlowGeometry() {
928
- return {
929
- top: this.node.getComputedTop(),
930
- left: this.node.getComputedLeft(),
931
- width: this.node.getComputedWidth(),
932
- height: this.node.getComputedHeight(),
933
- absolute: this.node.getPositionType() === PositionType.Absolute
934
- };
935
- }
936
- /**
937
- * Position edges this widget declared for absolute positioning, when it
938
- * anchors itself to the page box (e.g. FixedContainer). The pagination
939
- * walker uses them to re-anchor page-root fixed widgets against the real
940
- * page dimensions — the flow pass resolves `bottom`/`right` against the
941
- * auto-height flow canvas, which is not the page box. Default: undefined
942
- * (edges unknown — the flow-computed box is used as-is).
943
- */
944
- getPositionEdges() {
945
- return void 0;
946
- }
947
- /**
948
- * Break opportunities inside this widget's box, box-relative and sorted by
949
- * offset. null = atomic (move whole; clip if taller than a page).
950
- *
951
- * Default: a column-direction box breaks between its children (one unit
952
- * per child; offsets are Yoga computed tops, which include this box's
953
- * padding, so inter-child gaps ride on the offsets). Row-direction,
954
- * absolute-positioned, childless, and `breakable: false` widgets are
955
- * atomic. Only widgets whose content splits internally (Text, Table) need
956
- * to override.
957
- */
958
- getBreakUnits() {
959
- if (this.breakableOption === false) return null;
960
- if (this.children.length === 0) return null;
961
- if (this.node.getFlexDirection() !== FlexDirection.Column) return null;
962
- const units = [];
963
- for (const child of this.children) {
964
- const geometry = child.getFlowGeometry();
965
- if (geometry.absolute) continue;
966
- units.push({ offset: geometry.top, height: geometry.height, widget: child });
967
- }
968
- return units.length > 0 ? units : null;
969
- }
970
- };
971
-
972
734
  // src/lib/Page.ts
973
- import { Align as Align2, Direction as Direction2, Edge as Edge2, FlexDirection as FlexDirection2, Justify as Justify2 } from "yoga-layout";
735
+ import { Align, Direction, Edge, FlexDirection, Justify } from "yoga-layout";
974
736
  var PageWidget = class extends Widget {
975
737
  dimensions;
976
738
  size;
@@ -980,10 +742,10 @@ var PageWidget = class extends Widget {
980
742
  this.dimensions = options.dimensions;
981
743
  this.size = options.size;
982
744
  this.paginateContent = options.overflow ?? true;
983
- this.node.setFlexDirection(FlexDirection2.Column);
984
- this.node.setJustifyContent(Justify2.FlexStart);
985
- this.node.setAlignItems(Align2.Stretch);
986
- this.node.setPadding(Edge2.All, options.padding ?? 0);
745
+ this.node.setFlexDirection(FlexDirection.Column);
746
+ this.node.setJustifyContent(Justify.FlexStart);
747
+ this.node.setAlignItems(Align.Stretch);
748
+ this.node.setPadding(Edge.All, options.padding ?? 0);
987
749
  }
988
750
  getDimensions(context) {
989
751
  if (this.dimensions) {
@@ -995,8 +757,8 @@ var PageWidget = class extends Widget {
995
757
  return size;
996
758
  }
997
759
  }
998
- const docDimensions = context.getDimensions();
999
- return docDimensions;
760
+ const options = context.getOptions();
761
+ return [options.width || 0, options.height || 0];
1000
762
  }
1001
763
  /** Whether content overflowing this page splits across continuation pages. */
1002
764
  get paginates() {
@@ -1010,18 +772,18 @@ var PageWidget = class extends Widget {
1010
772
  } else {
1011
773
  this.node.setHeight(height);
1012
774
  }
1013
- this.calculateLayout(width, height, Direction2.LTR);
775
+ this.calculateLayout(width, height, Direction.LTR);
1014
776
  }
1015
777
  /** @internal — content box after layout, page-relative top-left origin. */
1016
778
  getContentBox(dimensions) {
1017
- const padTop = this.node.getComputedPadding(Edge2.Top);
1018
- const padBottom = this.node.getComputedPadding(Edge2.Bottom);
1019
- const padLeft = this.node.getComputedPadding(Edge2.Left);
1020
- const padRight = this.node.getComputedPadding(Edge2.Right);
1021
- const borderTop = this.node.getComputedBorder(Edge2.Top);
1022
- const borderBottom = this.node.getComputedBorder(Edge2.Bottom);
1023
- const borderLeft = this.node.getComputedBorder(Edge2.Left);
1024
- const borderRight = this.node.getComputedBorder(Edge2.Right);
779
+ const padTop = this.node.getComputedPadding(Edge.Top);
780
+ const padBottom = this.node.getComputedPadding(Edge.Bottom);
781
+ const padLeft = this.node.getComputedPadding(Edge.Left);
782
+ const padRight = this.node.getComputedPadding(Edge.Right);
783
+ const borderTop = this.node.getComputedBorder(Edge.Top);
784
+ const borderBottom = this.node.getComputedBorder(Edge.Bottom);
785
+ const borderLeft = this.node.getComputedBorder(Edge.Left);
786
+ const borderRight = this.node.getComputedBorder(Edge.Right);
1025
787
  return {
1026
788
  x: padLeft + borderLeft,
1027
789
  y: padTop + borderTop,
@@ -1033,8 +795,14 @@ var PageWidget = class extends Widget {
1033
795
  * Opens the backend surface this page paints onto. LoadedPage overrides
1034
796
  * this to a no-op — its surface is an adopted existing page, opened in
1035
797
  * render() before the layout pass.
798
+ *
799
+ * Page lifecycle is PDF mechanics (PdfPageSurface), not part of the
800
+ * neutral RenderContext — pages are where drawing surfaces are created.
1036
801
  */
1037
802
  async openPage(context, dimensions) {
803
+ if (!canAddPages(context)) {
804
+ throw new Error("Page rendering requires a context that can open pages (the PDF backend).");
805
+ }
1038
806
  context.addPage(dimensions);
1039
807
  }
1040
808
  async render(context) {
@@ -1046,7 +814,7 @@ var PageWidget = class extends Widget {
1046
814
  await this.prepareLayout(context);
1047
815
  this.node.setWidth(dimensions[0]);
1048
816
  this.node.setHeight(dimensions[1]);
1049
- this.calculateLayout(dimensions[0], dimensions[1], Direction2.LTR);
817
+ this.calculateLayout(dimensions[0], dimensions[1], Direction.LTR);
1050
818
  await this.renderChildren(context);
1051
819
  return;
1052
820
  }
@@ -1057,6 +825,9 @@ var PageWidget = class extends Widget {
1057
825
  return;
1058
826
  }
1059
827
  for (const plan of pagination.pages) {
828
+ if (!canAddPages(context)) {
829
+ throw new Error("Page rendering requires a context that can open pages (the PDF backend).");
830
+ }
1060
831
  context.addPage(plan.pageSize);
1061
832
  const scope = new PageScope(context, plan);
1062
833
  for (const fragment of plan.fixed) await fragment.widget.render(scope);
@@ -1070,7 +841,7 @@ function Page(options = {}) {
1070
841
 
1071
842
  export {
1072
843
  faceId,
1073
- combineFaceStyle,
844
+ resolveFaceStyle,
1074
845
  parseFaceId,
1075
846
  BuiltinPdfFonts,
1076
847
  builtinPdfNameFace,
@@ -1081,8 +852,7 @@ export {
1081
852
  PdfRenderContext,
1082
853
  PageSize,
1083
854
  PDFDocSize,
1084
- Widget,
1085
855
  PageWidget,
1086
856
  Page
1087
857
  };
1088
- //# sourceMappingURL=chunk-4JABW7CO.js.map
858
+ //# sourceMappingURL=chunk-5ZRRZRCD.js.map