modern-pdf-lib 0.15.0 → 0.19.9

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 (95) hide show
  1. package/README.md +106 -7
  2. package/dist/batchOptimize-7U_kD3_j.mjs +392 -0
  3. package/dist/batchOptimize-xo6BXbGZ.cjs +427 -0
  4. package/dist/{bridge-C7U4E7St.mjs → bridge-DTH5LMAK.mjs} +3 -3
  5. package/dist/{bridge-DUcJFVsk.cjs → bridge-DYCQzxF7.cjs} +2 -2
  6. package/dist/browser.cjs +621 -0
  7. package/dist/browser.d.cts +190 -0
  8. package/dist/browser.d.cts.map +1 -0
  9. package/dist/browser.d.mts +190 -0
  10. package/dist/browser.d.mts.map +1 -0
  11. package/dist/browser.mjs +212 -0
  12. package/dist/cli/index.cjs +247 -0
  13. package/dist/cli/index.d.cts +1 -0
  14. package/dist/cli/index.d.mts +1 -0
  15. package/dist/cli/index.mjs +248 -0
  16. package/dist/compressionAnalysis-BBv4BkQP.d.mts +261 -0
  17. package/dist/compressionAnalysis-BBv4BkQP.d.mts.map +1 -0
  18. package/dist/compressionAnalysis-Bw2alOxt.mjs +1490 -0
  19. package/dist/compressionAnalysis-CtJ2X9l2.d.cts +261 -0
  20. package/dist/compressionAnalysis-CtJ2X9l2.d.cts.map +1 -0
  21. package/dist/compressionAnalysis-eXYyDsrh.cjs +1525 -0
  22. package/dist/create.cjs +35 -0
  23. package/dist/create.d.cts +3 -0
  24. package/dist/create.d.mts +3 -0
  25. package/dist/create.mjs +5 -0
  26. package/dist/deduplicateImages-B5lmzL9j.cjs +113 -0
  27. package/dist/deduplicateImages-BX3Zg8Qp.mjs +102 -0
  28. package/dist/{fflateAdapter-DX0VqT5k.mjs → fflateAdapter-CBQpGTlx.mjs} +2 -2
  29. package/dist/{fflateAdapter-AHC_S3cb.cjs → fflateAdapter-LTAeAhaD.cjs} +1 -1
  30. package/dist/fieldAppearance-C8PoLFSc.d.mts +136 -0
  31. package/dist/fieldAppearance-C8PoLFSc.d.mts.map +1 -0
  32. package/dist/fieldAppearance-CdiGFG5e.d.cts +136 -0
  33. package/dist/fieldAppearance-CdiGFG5e.d.cts.map +1 -0
  34. package/dist/fontEmbed-Dsu9fo4U.d.mts +636 -0
  35. package/dist/fontEmbed-Dsu9fo4U.d.mts.map +1 -0
  36. package/dist/fontEmbed-LID6yG6g.d.cts +636 -0
  37. package/dist/fontEmbed-LID6yG6g.d.cts.map +1 -0
  38. package/dist/{fontSubset-pFc8Dueu.cjs → fontSubset-5SLWMmEw.cjs} +1 -1
  39. package/dist/{fontSubset-ZpLoOZ2e.mjs → fontSubset-DWpduoY2.mjs} +2 -2
  40. package/dist/forms.cjs +13 -0
  41. package/dist/forms.d.cts +3 -0
  42. package/dist/forms.d.mts +3 -0
  43. package/dist/forms.mjs +3 -0
  44. package/dist/grayscaleDetect-C2m-eEXR.cjs +96 -0
  45. package/dist/grayscaleDetect-C6kFF3dk.mjs +84 -0
  46. package/dist/imageExtract-B6OvUEp-.mjs +155 -0
  47. package/dist/imageExtract-PxdBvpHj.cjs +166 -0
  48. package/dist/index-BtYOx5wh.d.mts +4904 -0
  49. package/dist/index-BtYOx5wh.d.mts.map +1 -0
  50. package/dist/index-bpktKzCA.d.cts +4904 -0
  51. package/dist/index-bpktKzCA.d.cts.map +1 -0
  52. package/dist/index.cjs +288 -25851
  53. package/dist/index.d.cts +7 -9151
  54. package/dist/index.d.mts +7 -9151
  55. package/dist/index.mjs +17 -25665
  56. package/dist/layout-BZ8tTeAk.mjs +438 -0
  57. package/dist/layout-Inbqegsk.cjs +563 -0
  58. package/dist/{libdeflateWasm-Enus0G1k.cjs → libdeflateWasm-BdiDEJOj.cjs} +2 -2
  59. package/dist/{libdeflateWasm-82loOtIV.mjs → libdeflateWasm-rLppXytE.mjs} +3 -3
  60. package/dist/loader-3u6Tw5T-.mjs +328 -0
  61. package/dist/loader-I4zdkoWc.cjs +393 -0
  62. package/dist/parse.cjs +24 -0
  63. package/dist/parse.d.cts +4 -0
  64. package/dist/parse.d.mts +4 -0
  65. package/dist/parse.mjs +7 -0
  66. package/dist/pdfCatalog-CYy4NXEY.cjs +173 -0
  67. package/dist/pdfCatalog-IImGcMbR.mjs +138 -0
  68. package/dist/pdfDocument-BSiQdNZq.d.cts +4640 -0
  69. package/dist/pdfDocument-BSiQdNZq.d.cts.map +1 -0
  70. package/dist/pdfDocument-DOg240g9.mjs +13685 -0
  71. package/dist/pdfDocument-Duf9LelM.cjs +14110 -0
  72. package/dist/pdfDocument-i6U5fQ91.d.mts +4640 -0
  73. package/dist/pdfDocument-i6U5fQ91.d.mts.map +1 -0
  74. package/dist/pdfForm-9gd40uz9.cjs +1796 -0
  75. package/dist/pdfForm-BiyNtYem.d.mts +905 -0
  76. package/dist/pdfForm-BiyNtYem.d.mts.map +1 -0
  77. package/dist/pdfForm-Cn-cVicP.mjs +1695 -0
  78. package/dist/pdfForm-SOXJ72LW.d.cts +905 -0
  79. package/dist/pdfForm-SOXJ72LW.d.cts.map +1 -0
  80. package/dist/{pdfCatalog-COKoYQ8C.cjs → pdfObjects-1veop1_d.cjs} +2 -172
  81. package/dist/{pdfCatalog-BB2Wnmud.mjs → pdfObjects-uEsWlfzU.mjs} +3 -138
  82. package/dist/{pdfPage-N1K2U3jI.mjs → pdfPage-BacMkrLe.mjs} +3024 -4
  83. package/dist/{pdfPage-DBfdinTR.cjs → pdfPage-CirlQRzJ.cjs} +3148 -104
  84. package/dist/{pngEmbed-gaJ9S2Dk.mjs → pngEmbed-BLj2zi-5.mjs} +3 -3
  85. package/dist/{pngEmbed-10m4CfBU.cjs → pngEmbed-D4X4ZN-3.cjs} +2 -2
  86. package/dist/src-BLWEEbd7.cjs +11852 -0
  87. package/dist/src-x0g7wiRq.mjs +11103 -0
  88. package/dist/streamDecode-Bs0_MT_Q.cjs +4607 -0
  89. package/dist/streamDecode-CWN-nfPJ.mjs +4596 -0
  90. package/package.json +33 -1
  91. package/dist/index.d.cts.map +0 -1
  92. package/dist/index.d.mts.map +0 -1
  93. package/dist/loader-1VJXLlMZ.mjs +0 -164
  94. package/dist/loader-CKlBOHma.cjs +0 -166
  95. package/dist/rolldown-runtime-95iHPtFO.mjs +0 -18
@@ -1,5 +1,6 @@
1
- import { t as __exportAll } from "./rolldown-runtime-95iHPtFO.mjs";
2
- import { a as formatPdfDate, c as PdfBool, f as PdfNumber, g as PdfString, h as PdfStream, l as PdfDict, s as PdfArray, u as PdfName } from "./pdfCatalog-BB2Wnmud.mjs";
1
+ import { Ka as __exportAll } from "./index-BtYOx5wh.d.mts";
2
+ import { i as PdfName, l as PdfStream, n as PdfBool, o as PdfNumber, r as PdfDict, t as PdfArray, u as PdfString } from "./pdfObjects-uEsWlfzU.mjs";
3
+ import { a as formatPdfDate } from "./pdfCatalog-IImGcMbR.mjs";
3
4
 
4
5
  //#region src/core/operators/text.ts
5
6
  /**
@@ -3418,6 +3419,2973 @@ function buildPatternObjects(pattern, registry) {
3418
3419
  };
3419
3420
  }
3420
3421
 
3422
+ //#endregion
3423
+ //#region src/barcode/qr.ts
3424
+ /** Characters valid in alphanumeric mode and their values. */
3425
+ const ALPHANUMERIC_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
3426
+ /** Mode indicators (4-bit). */
3427
+ const MODE_NUMERIC = 1;
3428
+ const MODE_ALPHANUMERIC = 2;
3429
+ const MODE_BYTE = 4;
3430
+ /** EC level ordinal (used for table indexing). */
3431
+ const EC_ORDINAL = {
3432
+ L: 0,
3433
+ M: 1,
3434
+ Q: 2,
3435
+ H: 3
3436
+ };
3437
+ /**
3438
+ * Total data codewords per version/EC level.
3439
+ * Index: [version-1][ecOrdinal]
3440
+ */
3441
+ const TOTAL_DATA_CODEWORDS = [
3442
+ [
3443
+ 19,
3444
+ 16,
3445
+ 13,
3446
+ 9
3447
+ ],
3448
+ [
3449
+ 34,
3450
+ 28,
3451
+ 22,
3452
+ 16
3453
+ ],
3454
+ [
3455
+ 55,
3456
+ 44,
3457
+ 34,
3458
+ 26
3459
+ ],
3460
+ [
3461
+ 80,
3462
+ 64,
3463
+ 48,
3464
+ 36
3465
+ ],
3466
+ [
3467
+ 108,
3468
+ 86,
3469
+ 62,
3470
+ 46
3471
+ ],
3472
+ [
3473
+ 136,
3474
+ 108,
3475
+ 76,
3476
+ 60
3477
+ ],
3478
+ [
3479
+ 156,
3480
+ 124,
3481
+ 88,
3482
+ 66
3483
+ ],
3484
+ [
3485
+ 194,
3486
+ 154,
3487
+ 110,
3488
+ 86
3489
+ ],
3490
+ [
3491
+ 232,
3492
+ 182,
3493
+ 132,
3494
+ 100
3495
+ ],
3496
+ [
3497
+ 274,
3498
+ 216,
3499
+ 154,
3500
+ 122
3501
+ ],
3502
+ [
3503
+ 324,
3504
+ 254,
3505
+ 180,
3506
+ 140
3507
+ ],
3508
+ [
3509
+ 370,
3510
+ 290,
3511
+ 206,
3512
+ 158
3513
+ ],
3514
+ [
3515
+ 428,
3516
+ 334,
3517
+ 244,
3518
+ 180
3519
+ ],
3520
+ [
3521
+ 461,
3522
+ 365,
3523
+ 261,
3524
+ 197
3525
+ ],
3526
+ [
3527
+ 523,
3528
+ 415,
3529
+ 295,
3530
+ 223
3531
+ ],
3532
+ [
3533
+ 589,
3534
+ 453,
3535
+ 325,
3536
+ 253
3537
+ ],
3538
+ [
3539
+ 647,
3540
+ 507,
3541
+ 367,
3542
+ 283
3543
+ ],
3544
+ [
3545
+ 721,
3546
+ 563,
3547
+ 397,
3548
+ 313
3549
+ ],
3550
+ [
3551
+ 795,
3552
+ 627,
3553
+ 445,
3554
+ 341
3555
+ ],
3556
+ [
3557
+ 861,
3558
+ 669,
3559
+ 485,
3560
+ 385
3561
+ ],
3562
+ [
3563
+ 932,
3564
+ 714,
3565
+ 512,
3566
+ 406
3567
+ ],
3568
+ [
3569
+ 1006,
3570
+ 782,
3571
+ 568,
3572
+ 442
3573
+ ],
3574
+ [
3575
+ 1094,
3576
+ 860,
3577
+ 614,
3578
+ 464
3579
+ ],
3580
+ [
3581
+ 1174,
3582
+ 914,
3583
+ 664,
3584
+ 514
3585
+ ],
3586
+ [
3587
+ 1276,
3588
+ 1e3,
3589
+ 718,
3590
+ 538
3591
+ ],
3592
+ [
3593
+ 1370,
3594
+ 1062,
3595
+ 754,
3596
+ 596
3597
+ ],
3598
+ [
3599
+ 1468,
3600
+ 1128,
3601
+ 808,
3602
+ 628
3603
+ ],
3604
+ [
3605
+ 1531,
3606
+ 1193,
3607
+ 871,
3608
+ 661
3609
+ ],
3610
+ [
3611
+ 1631,
3612
+ 1267,
3613
+ 911,
3614
+ 701
3615
+ ],
3616
+ [
3617
+ 1735,
3618
+ 1373,
3619
+ 985,
3620
+ 745
3621
+ ],
3622
+ [
3623
+ 1843,
3624
+ 1455,
3625
+ 1033,
3626
+ 793
3627
+ ],
3628
+ [
3629
+ 1955,
3630
+ 1541,
3631
+ 1115,
3632
+ 845
3633
+ ],
3634
+ [
3635
+ 2071,
3636
+ 1631,
3637
+ 1171,
3638
+ 901
3639
+ ],
3640
+ [
3641
+ 2191,
3642
+ 1725,
3643
+ 1231,
3644
+ 961
3645
+ ],
3646
+ [
3647
+ 2306,
3648
+ 1812,
3649
+ 1286,
3650
+ 986
3651
+ ],
3652
+ [
3653
+ 2434,
3654
+ 1914,
3655
+ 1354,
3656
+ 1054
3657
+ ],
3658
+ [
3659
+ 2566,
3660
+ 1992,
3661
+ 1426,
3662
+ 1096
3663
+ ],
3664
+ [
3665
+ 2702,
3666
+ 2102,
3667
+ 1502,
3668
+ 1142
3669
+ ],
3670
+ [
3671
+ 2812,
3672
+ 2216,
3673
+ 1582,
3674
+ 1222
3675
+ ],
3676
+ [
3677
+ 2956,
3678
+ 2334,
3679
+ 1666,
3680
+ 1276
3681
+ ]
3682
+ ];
3683
+ /**
3684
+ * EC codewords per RS block for each version/EC level.
3685
+ * Index: [version-1][ecOrdinal]
3686
+ */
3687
+ const EC_CODEWORDS_PER_BLOCK = [
3688
+ [
3689
+ 7,
3690
+ 10,
3691
+ 13,
3692
+ 17
3693
+ ],
3694
+ [
3695
+ 10,
3696
+ 16,
3697
+ 22,
3698
+ 28
3699
+ ],
3700
+ [
3701
+ 15,
3702
+ 26,
3703
+ 18,
3704
+ 22
3705
+ ],
3706
+ [
3707
+ 20,
3708
+ 18,
3709
+ 26,
3710
+ 16
3711
+ ],
3712
+ [
3713
+ 26,
3714
+ 24,
3715
+ 18,
3716
+ 22
3717
+ ],
3718
+ [
3719
+ 18,
3720
+ 16,
3721
+ 24,
3722
+ 28
3723
+ ],
3724
+ [
3725
+ 20,
3726
+ 18,
3727
+ 18,
3728
+ 26
3729
+ ],
3730
+ [
3731
+ 24,
3732
+ 22,
3733
+ 22,
3734
+ 26
3735
+ ],
3736
+ [
3737
+ 30,
3738
+ 22,
3739
+ 20,
3740
+ 24
3741
+ ],
3742
+ [
3743
+ 18,
3744
+ 26,
3745
+ 24,
3746
+ 28
3747
+ ],
3748
+ [
3749
+ 20,
3750
+ 30,
3751
+ 28,
3752
+ 24
3753
+ ],
3754
+ [
3755
+ 24,
3756
+ 22,
3757
+ 26,
3758
+ 28
3759
+ ],
3760
+ [
3761
+ 26,
3762
+ 22,
3763
+ 24,
3764
+ 22
3765
+ ],
3766
+ [
3767
+ 30,
3768
+ 24,
3769
+ 20,
3770
+ 24
3771
+ ],
3772
+ [
3773
+ 22,
3774
+ 24,
3775
+ 30,
3776
+ 24
3777
+ ],
3778
+ [
3779
+ 24,
3780
+ 28,
3781
+ 24,
3782
+ 30
3783
+ ],
3784
+ [
3785
+ 28,
3786
+ 28,
3787
+ 28,
3788
+ 28
3789
+ ],
3790
+ [
3791
+ 30,
3792
+ 26,
3793
+ 28,
3794
+ 28
3795
+ ],
3796
+ [
3797
+ 28,
3798
+ 26,
3799
+ 26,
3800
+ 26
3801
+ ],
3802
+ [
3803
+ 28,
3804
+ 26,
3805
+ 28,
3806
+ 28
3807
+ ],
3808
+ [
3809
+ 28,
3810
+ 26,
3811
+ 30,
3812
+ 28
3813
+ ],
3814
+ [
3815
+ 28,
3816
+ 28,
3817
+ 24,
3818
+ 30
3819
+ ],
3820
+ [
3821
+ 30,
3822
+ 28,
3823
+ 30,
3824
+ 30
3825
+ ],
3826
+ [
3827
+ 30,
3828
+ 28,
3829
+ 30,
3830
+ 30
3831
+ ],
3832
+ [
3833
+ 26,
3834
+ 28,
3835
+ 30,
3836
+ 30
3837
+ ],
3838
+ [
3839
+ 28,
3840
+ 28,
3841
+ 28,
3842
+ 30
3843
+ ],
3844
+ [
3845
+ 30,
3846
+ 28,
3847
+ 30,
3848
+ 30
3849
+ ],
3850
+ [
3851
+ 30,
3852
+ 28,
3853
+ 30,
3854
+ 30
3855
+ ],
3856
+ [
3857
+ 30,
3858
+ 28,
3859
+ 30,
3860
+ 30
3861
+ ],
3862
+ [
3863
+ 30,
3864
+ 28,
3865
+ 30,
3866
+ 30
3867
+ ],
3868
+ [
3869
+ 30,
3870
+ 28,
3871
+ 30,
3872
+ 30
3873
+ ],
3874
+ [
3875
+ 30,
3876
+ 28,
3877
+ 30,
3878
+ 30
3879
+ ],
3880
+ [
3881
+ 30,
3882
+ 28,
3883
+ 30,
3884
+ 30
3885
+ ],
3886
+ [
3887
+ 30,
3888
+ 28,
3889
+ 30,
3890
+ 30
3891
+ ],
3892
+ [
3893
+ 30,
3894
+ 28,
3895
+ 30,
3896
+ 30
3897
+ ],
3898
+ [
3899
+ 30,
3900
+ 28,
3901
+ 30,
3902
+ 30
3903
+ ],
3904
+ [
3905
+ 30,
3906
+ 28,
3907
+ 30,
3908
+ 30
3909
+ ],
3910
+ [
3911
+ 30,
3912
+ 28,
3913
+ 30,
3914
+ 30
3915
+ ],
3916
+ [
3917
+ 30,
3918
+ 28,
3919
+ 30,
3920
+ 30
3921
+ ],
3922
+ [
3923
+ 30,
3924
+ 28,
3925
+ 30,
3926
+ 30
3927
+ ]
3928
+ ];
3929
+ /**
3930
+ * Block structure: [group1Blocks, group1DataCW, group2Blocks, group2DataCW]
3931
+ * Index: [version-1][ecOrdinal]
3932
+ */
3933
+ const BLOCK_STRUCTURE = [
3934
+ [
3935
+ [
3936
+ 1,
3937
+ 19,
3938
+ 0,
3939
+ 0
3940
+ ],
3941
+ [
3942
+ 1,
3943
+ 16,
3944
+ 0,
3945
+ 0
3946
+ ],
3947
+ [
3948
+ 1,
3949
+ 13,
3950
+ 0,
3951
+ 0
3952
+ ],
3953
+ [
3954
+ 1,
3955
+ 9,
3956
+ 0,
3957
+ 0
3958
+ ]
3959
+ ],
3960
+ [
3961
+ [
3962
+ 1,
3963
+ 34,
3964
+ 0,
3965
+ 0
3966
+ ],
3967
+ [
3968
+ 1,
3969
+ 28,
3970
+ 0,
3971
+ 0
3972
+ ],
3973
+ [
3974
+ 1,
3975
+ 22,
3976
+ 0,
3977
+ 0
3978
+ ],
3979
+ [
3980
+ 1,
3981
+ 16,
3982
+ 0,
3983
+ 0
3984
+ ]
3985
+ ],
3986
+ [
3987
+ [
3988
+ 1,
3989
+ 55,
3990
+ 0,
3991
+ 0
3992
+ ],
3993
+ [
3994
+ 1,
3995
+ 44,
3996
+ 0,
3997
+ 0
3998
+ ],
3999
+ [
4000
+ 2,
4001
+ 17,
4002
+ 0,
4003
+ 0
4004
+ ],
4005
+ [
4006
+ 2,
4007
+ 13,
4008
+ 0,
4009
+ 0
4010
+ ]
4011
+ ],
4012
+ [
4013
+ [
4014
+ 1,
4015
+ 80,
4016
+ 0,
4017
+ 0
4018
+ ],
4019
+ [
4020
+ 2,
4021
+ 32,
4022
+ 0,
4023
+ 0
4024
+ ],
4025
+ [
4026
+ 2,
4027
+ 24,
4028
+ 0,
4029
+ 0
4030
+ ],
4031
+ [
4032
+ 4,
4033
+ 9,
4034
+ 0,
4035
+ 0
4036
+ ]
4037
+ ],
4038
+ [
4039
+ [
4040
+ 1,
4041
+ 108,
4042
+ 0,
4043
+ 0
4044
+ ],
4045
+ [
4046
+ 2,
4047
+ 43,
4048
+ 0,
4049
+ 0
4050
+ ],
4051
+ [
4052
+ 2,
4053
+ 15,
4054
+ 2,
4055
+ 16
4056
+ ],
4057
+ [
4058
+ 2,
4059
+ 11,
4060
+ 2,
4061
+ 12
4062
+ ]
4063
+ ],
4064
+ [
4065
+ [
4066
+ 2,
4067
+ 68,
4068
+ 0,
4069
+ 0
4070
+ ],
4071
+ [
4072
+ 4,
4073
+ 27,
4074
+ 0,
4075
+ 0
4076
+ ],
4077
+ [
4078
+ 4,
4079
+ 19,
4080
+ 0,
4081
+ 0
4082
+ ],
4083
+ [
4084
+ 4,
4085
+ 15,
4086
+ 0,
4087
+ 0
4088
+ ]
4089
+ ],
4090
+ [
4091
+ [
4092
+ 2,
4093
+ 78,
4094
+ 0,
4095
+ 0
4096
+ ],
4097
+ [
4098
+ 4,
4099
+ 31,
4100
+ 0,
4101
+ 0
4102
+ ],
4103
+ [
4104
+ 2,
4105
+ 14,
4106
+ 4,
4107
+ 15
4108
+ ],
4109
+ [
4110
+ 4,
4111
+ 13,
4112
+ 1,
4113
+ 14
4114
+ ]
4115
+ ],
4116
+ [
4117
+ [
4118
+ 2,
4119
+ 97,
4120
+ 0,
4121
+ 0
4122
+ ],
4123
+ [
4124
+ 2,
4125
+ 38,
4126
+ 2,
4127
+ 39
4128
+ ],
4129
+ [
4130
+ 4,
4131
+ 18,
4132
+ 2,
4133
+ 19
4134
+ ],
4135
+ [
4136
+ 4,
4137
+ 14,
4138
+ 2,
4139
+ 15
4140
+ ]
4141
+ ],
4142
+ [
4143
+ [
4144
+ 2,
4145
+ 116,
4146
+ 0,
4147
+ 0
4148
+ ],
4149
+ [
4150
+ 3,
4151
+ 36,
4152
+ 2,
4153
+ 37
4154
+ ],
4155
+ [
4156
+ 4,
4157
+ 16,
4158
+ 4,
4159
+ 17
4160
+ ],
4161
+ [
4162
+ 4,
4163
+ 12,
4164
+ 4,
4165
+ 13
4166
+ ]
4167
+ ],
4168
+ [
4169
+ [
4170
+ 2,
4171
+ 68,
4172
+ 2,
4173
+ 69
4174
+ ],
4175
+ [
4176
+ 4,
4177
+ 43,
4178
+ 1,
4179
+ 44
4180
+ ],
4181
+ [
4182
+ 6,
4183
+ 19,
4184
+ 2,
4185
+ 20
4186
+ ],
4187
+ [
4188
+ 6,
4189
+ 15,
4190
+ 2,
4191
+ 16
4192
+ ]
4193
+ ],
4194
+ [
4195
+ [
4196
+ 4,
4197
+ 81,
4198
+ 0,
4199
+ 0
4200
+ ],
4201
+ [
4202
+ 1,
4203
+ 50,
4204
+ 4,
4205
+ 51
4206
+ ],
4207
+ [
4208
+ 4,
4209
+ 22,
4210
+ 4,
4211
+ 23
4212
+ ],
4213
+ [
4214
+ 3,
4215
+ 12,
4216
+ 8,
4217
+ 13
4218
+ ]
4219
+ ],
4220
+ [
4221
+ [
4222
+ 2,
4223
+ 92,
4224
+ 2,
4225
+ 93
4226
+ ],
4227
+ [
4228
+ 6,
4229
+ 36,
4230
+ 2,
4231
+ 37
4232
+ ],
4233
+ [
4234
+ 4,
4235
+ 20,
4236
+ 6,
4237
+ 21
4238
+ ],
4239
+ [
4240
+ 7,
4241
+ 14,
4242
+ 4,
4243
+ 15
4244
+ ]
4245
+ ],
4246
+ [
4247
+ [
4248
+ 4,
4249
+ 107,
4250
+ 0,
4251
+ 0
4252
+ ],
4253
+ [
4254
+ 8,
4255
+ 37,
4256
+ 1,
4257
+ 38
4258
+ ],
4259
+ [
4260
+ 8,
4261
+ 20,
4262
+ 4,
4263
+ 21
4264
+ ],
4265
+ [
4266
+ 12,
4267
+ 11,
4268
+ 4,
4269
+ 12
4270
+ ]
4271
+ ],
4272
+ [
4273
+ [
4274
+ 3,
4275
+ 115,
4276
+ 1,
4277
+ 116
4278
+ ],
4279
+ [
4280
+ 4,
4281
+ 40,
4282
+ 5,
4283
+ 41
4284
+ ],
4285
+ [
4286
+ 11,
4287
+ 16,
4288
+ 5,
4289
+ 17
4290
+ ],
4291
+ [
4292
+ 11,
4293
+ 12,
4294
+ 5,
4295
+ 13
4296
+ ]
4297
+ ],
4298
+ [
4299
+ [
4300
+ 5,
4301
+ 87,
4302
+ 1,
4303
+ 88
4304
+ ],
4305
+ [
4306
+ 5,
4307
+ 41,
4308
+ 5,
4309
+ 42
4310
+ ],
4311
+ [
4312
+ 5,
4313
+ 24,
4314
+ 7,
4315
+ 25
4316
+ ],
4317
+ [
4318
+ 11,
4319
+ 12,
4320
+ 7,
4321
+ 13
4322
+ ]
4323
+ ],
4324
+ [
4325
+ [
4326
+ 5,
4327
+ 98,
4328
+ 1,
4329
+ 99
4330
+ ],
4331
+ [
4332
+ 7,
4333
+ 45,
4334
+ 3,
4335
+ 46
4336
+ ],
4337
+ [
4338
+ 15,
4339
+ 19,
4340
+ 2,
4341
+ 20
4342
+ ],
4343
+ [
4344
+ 3,
4345
+ 15,
4346
+ 13,
4347
+ 16
4348
+ ]
4349
+ ],
4350
+ [
4351
+ [
4352
+ 1,
4353
+ 107,
4354
+ 5,
4355
+ 108
4356
+ ],
4357
+ [
4358
+ 10,
4359
+ 46,
4360
+ 1,
4361
+ 47
4362
+ ],
4363
+ [
4364
+ 1,
4365
+ 22,
4366
+ 15,
4367
+ 23
4368
+ ],
4369
+ [
4370
+ 2,
4371
+ 14,
4372
+ 17,
4373
+ 15
4374
+ ]
4375
+ ],
4376
+ [
4377
+ [
4378
+ 5,
4379
+ 120,
4380
+ 1,
4381
+ 121
4382
+ ],
4383
+ [
4384
+ 9,
4385
+ 43,
4386
+ 4,
4387
+ 44
4388
+ ],
4389
+ [
4390
+ 17,
4391
+ 22,
4392
+ 1,
4393
+ 23
4394
+ ],
4395
+ [
4396
+ 2,
4397
+ 14,
4398
+ 19,
4399
+ 15
4400
+ ]
4401
+ ],
4402
+ [
4403
+ [
4404
+ 3,
4405
+ 113,
4406
+ 4,
4407
+ 114
4408
+ ],
4409
+ [
4410
+ 3,
4411
+ 44,
4412
+ 11,
4413
+ 45
4414
+ ],
4415
+ [
4416
+ 17,
4417
+ 21,
4418
+ 4,
4419
+ 22
4420
+ ],
4421
+ [
4422
+ 9,
4423
+ 13,
4424
+ 16,
4425
+ 14
4426
+ ]
4427
+ ],
4428
+ [
4429
+ [
4430
+ 3,
4431
+ 107,
4432
+ 5,
4433
+ 108
4434
+ ],
4435
+ [
4436
+ 3,
4437
+ 41,
4438
+ 13,
4439
+ 42
4440
+ ],
4441
+ [
4442
+ 15,
4443
+ 24,
4444
+ 5,
4445
+ 25
4446
+ ],
4447
+ [
4448
+ 15,
4449
+ 15,
4450
+ 10,
4451
+ 16
4452
+ ]
4453
+ ],
4454
+ [
4455
+ [
4456
+ 4,
4457
+ 116,
4458
+ 4,
4459
+ 117
4460
+ ],
4461
+ [
4462
+ 17,
4463
+ 42,
4464
+ 0,
4465
+ 0
4466
+ ],
4467
+ [
4468
+ 17,
4469
+ 22,
4470
+ 6,
4471
+ 23
4472
+ ],
4473
+ [
4474
+ 19,
4475
+ 16,
4476
+ 6,
4477
+ 17
4478
+ ]
4479
+ ],
4480
+ [
4481
+ [
4482
+ 2,
4483
+ 111,
4484
+ 7,
4485
+ 112
4486
+ ],
4487
+ [
4488
+ 17,
4489
+ 46,
4490
+ 0,
4491
+ 0
4492
+ ],
4493
+ [
4494
+ 7,
4495
+ 24,
4496
+ 16,
4497
+ 25
4498
+ ],
4499
+ [
4500
+ 34,
4501
+ 13,
4502
+ 0,
4503
+ 0
4504
+ ]
4505
+ ],
4506
+ [
4507
+ [
4508
+ 4,
4509
+ 121,
4510
+ 5,
4511
+ 122
4512
+ ],
4513
+ [
4514
+ 4,
4515
+ 47,
4516
+ 14,
4517
+ 48
4518
+ ],
4519
+ [
4520
+ 11,
4521
+ 24,
4522
+ 14,
4523
+ 25
4524
+ ],
4525
+ [
4526
+ 16,
4527
+ 15,
4528
+ 14,
4529
+ 16
4530
+ ]
4531
+ ],
4532
+ [
4533
+ [
4534
+ 6,
4535
+ 117,
4536
+ 4,
4537
+ 118
4538
+ ],
4539
+ [
4540
+ 6,
4541
+ 45,
4542
+ 14,
4543
+ 46
4544
+ ],
4545
+ [
4546
+ 11,
4547
+ 24,
4548
+ 16,
4549
+ 25
4550
+ ],
4551
+ [
4552
+ 30,
4553
+ 16,
4554
+ 2,
4555
+ 17
4556
+ ]
4557
+ ],
4558
+ [
4559
+ [
4560
+ 8,
4561
+ 106,
4562
+ 4,
4563
+ 107
4564
+ ],
4565
+ [
4566
+ 8,
4567
+ 47,
4568
+ 13,
4569
+ 48
4570
+ ],
4571
+ [
4572
+ 7,
4573
+ 24,
4574
+ 22,
4575
+ 25
4576
+ ],
4577
+ [
4578
+ 22,
4579
+ 15,
4580
+ 13,
4581
+ 16
4582
+ ]
4583
+ ],
4584
+ [
4585
+ [
4586
+ 10,
4587
+ 114,
4588
+ 2,
4589
+ 115
4590
+ ],
4591
+ [
4592
+ 19,
4593
+ 46,
4594
+ 4,
4595
+ 47
4596
+ ],
4597
+ [
4598
+ 28,
4599
+ 22,
4600
+ 6,
4601
+ 23
4602
+ ],
4603
+ [
4604
+ 33,
4605
+ 16,
4606
+ 4,
4607
+ 17
4608
+ ]
4609
+ ],
4610
+ [
4611
+ [
4612
+ 8,
4613
+ 122,
4614
+ 4,
4615
+ 123
4616
+ ],
4617
+ [
4618
+ 22,
4619
+ 45,
4620
+ 3,
4621
+ 46
4622
+ ],
4623
+ [
4624
+ 8,
4625
+ 23,
4626
+ 26,
4627
+ 24
4628
+ ],
4629
+ [
4630
+ 12,
4631
+ 15,
4632
+ 28,
4633
+ 16
4634
+ ]
4635
+ ],
4636
+ [
4637
+ [
4638
+ 3,
4639
+ 117,
4640
+ 10,
4641
+ 118
4642
+ ],
4643
+ [
4644
+ 3,
4645
+ 45,
4646
+ 23,
4647
+ 46
4648
+ ],
4649
+ [
4650
+ 4,
4651
+ 24,
4652
+ 31,
4653
+ 25
4654
+ ],
4655
+ [
4656
+ 11,
4657
+ 15,
4658
+ 31,
4659
+ 16
4660
+ ]
4661
+ ],
4662
+ [
4663
+ [
4664
+ 7,
4665
+ 116,
4666
+ 7,
4667
+ 117
4668
+ ],
4669
+ [
4670
+ 21,
4671
+ 45,
4672
+ 7,
4673
+ 46
4674
+ ],
4675
+ [
4676
+ 1,
4677
+ 23,
4678
+ 37,
4679
+ 24
4680
+ ],
4681
+ [
4682
+ 19,
4683
+ 15,
4684
+ 26,
4685
+ 16
4686
+ ]
4687
+ ],
4688
+ [
4689
+ [
4690
+ 5,
4691
+ 115,
4692
+ 10,
4693
+ 116
4694
+ ],
4695
+ [
4696
+ 19,
4697
+ 47,
4698
+ 10,
4699
+ 48
4700
+ ],
4701
+ [
4702
+ 15,
4703
+ 24,
4704
+ 25,
4705
+ 25
4706
+ ],
4707
+ [
4708
+ 23,
4709
+ 15,
4710
+ 25,
4711
+ 16
4712
+ ]
4713
+ ],
4714
+ [
4715
+ [
4716
+ 13,
4717
+ 115,
4718
+ 3,
4719
+ 116
4720
+ ],
4721
+ [
4722
+ 2,
4723
+ 46,
4724
+ 29,
4725
+ 47
4726
+ ],
4727
+ [
4728
+ 42,
4729
+ 24,
4730
+ 1,
4731
+ 25
4732
+ ],
4733
+ [
4734
+ 23,
4735
+ 15,
4736
+ 28,
4737
+ 16
4738
+ ]
4739
+ ],
4740
+ [
4741
+ [
4742
+ 17,
4743
+ 115,
4744
+ 0,
4745
+ 0
4746
+ ],
4747
+ [
4748
+ 10,
4749
+ 46,
4750
+ 23,
4751
+ 47
4752
+ ],
4753
+ [
4754
+ 10,
4755
+ 24,
4756
+ 35,
4757
+ 25
4758
+ ],
4759
+ [
4760
+ 19,
4761
+ 15,
4762
+ 35,
4763
+ 16
4764
+ ]
4765
+ ],
4766
+ [
4767
+ [
4768
+ 17,
4769
+ 115,
4770
+ 1,
4771
+ 116
4772
+ ],
4773
+ [
4774
+ 14,
4775
+ 46,
4776
+ 21,
4777
+ 47
4778
+ ],
4779
+ [
4780
+ 29,
4781
+ 24,
4782
+ 19,
4783
+ 25
4784
+ ],
4785
+ [
4786
+ 11,
4787
+ 15,
4788
+ 46,
4789
+ 16
4790
+ ]
4791
+ ],
4792
+ [
4793
+ [
4794
+ 13,
4795
+ 115,
4796
+ 6,
4797
+ 116
4798
+ ],
4799
+ [
4800
+ 14,
4801
+ 46,
4802
+ 23,
4803
+ 47
4804
+ ],
4805
+ [
4806
+ 44,
4807
+ 24,
4808
+ 7,
4809
+ 25
4810
+ ],
4811
+ [
4812
+ 59,
4813
+ 16,
4814
+ 1,
4815
+ 17
4816
+ ]
4817
+ ],
4818
+ [
4819
+ [
4820
+ 12,
4821
+ 121,
4822
+ 7,
4823
+ 122
4824
+ ],
4825
+ [
4826
+ 12,
4827
+ 47,
4828
+ 26,
4829
+ 48
4830
+ ],
4831
+ [
4832
+ 39,
4833
+ 24,
4834
+ 14,
4835
+ 25
4836
+ ],
4837
+ [
4838
+ 22,
4839
+ 15,
4840
+ 41,
4841
+ 16
4842
+ ]
4843
+ ],
4844
+ [
4845
+ [
4846
+ 6,
4847
+ 121,
4848
+ 14,
4849
+ 122
4850
+ ],
4851
+ [
4852
+ 6,
4853
+ 47,
4854
+ 34,
4855
+ 48
4856
+ ],
4857
+ [
4858
+ 46,
4859
+ 24,
4860
+ 10,
4861
+ 25
4862
+ ],
4863
+ [
4864
+ 2,
4865
+ 15,
4866
+ 64,
4867
+ 16
4868
+ ]
4869
+ ],
4870
+ [
4871
+ [
4872
+ 17,
4873
+ 122,
4874
+ 4,
4875
+ 123
4876
+ ],
4877
+ [
4878
+ 29,
4879
+ 46,
4880
+ 14,
4881
+ 47
4882
+ ],
4883
+ [
4884
+ 49,
4885
+ 24,
4886
+ 10,
4887
+ 25
4888
+ ],
4889
+ [
4890
+ 24,
4891
+ 15,
4892
+ 46,
4893
+ 16
4894
+ ]
4895
+ ],
4896
+ [
4897
+ [
4898
+ 4,
4899
+ 122,
4900
+ 18,
4901
+ 123
4902
+ ],
4903
+ [
4904
+ 13,
4905
+ 46,
4906
+ 32,
4907
+ 47
4908
+ ],
4909
+ [
4910
+ 48,
4911
+ 24,
4912
+ 14,
4913
+ 25
4914
+ ],
4915
+ [
4916
+ 42,
4917
+ 15,
4918
+ 32,
4919
+ 16
4920
+ ]
4921
+ ],
4922
+ [
4923
+ [
4924
+ 20,
4925
+ 117,
4926
+ 4,
4927
+ 118
4928
+ ],
4929
+ [
4930
+ 40,
4931
+ 47,
4932
+ 7,
4933
+ 48
4934
+ ],
4935
+ [
4936
+ 43,
4937
+ 24,
4938
+ 22,
4939
+ 25
4940
+ ],
4941
+ [
4942
+ 10,
4943
+ 15,
4944
+ 67,
4945
+ 16
4946
+ ]
4947
+ ],
4948
+ [
4949
+ [
4950
+ 19,
4951
+ 118,
4952
+ 6,
4953
+ 119
4954
+ ],
4955
+ [
4956
+ 18,
4957
+ 47,
4958
+ 31,
4959
+ 48
4960
+ ],
4961
+ [
4962
+ 34,
4963
+ 24,
4964
+ 34,
4965
+ 25
4966
+ ],
4967
+ [
4968
+ 20,
4969
+ 15,
4970
+ 61,
4971
+ 16
4972
+ ]
4973
+ ]
4974
+ ];
4975
+ const ALIGNMENT_POSITIONS = [
4976
+ [],
4977
+ [6, 18],
4978
+ [6, 22],
4979
+ [6, 26],
4980
+ [6, 30],
4981
+ [6, 34],
4982
+ [
4983
+ 6,
4984
+ 22,
4985
+ 38
4986
+ ],
4987
+ [
4988
+ 6,
4989
+ 24,
4990
+ 42
4991
+ ],
4992
+ [
4993
+ 6,
4994
+ 26,
4995
+ 46
4996
+ ],
4997
+ [
4998
+ 6,
4999
+ 28,
5000
+ 50
5001
+ ],
5002
+ [
5003
+ 6,
5004
+ 30,
5005
+ 54
5006
+ ],
5007
+ [
5008
+ 6,
5009
+ 32,
5010
+ 58
5011
+ ],
5012
+ [
5013
+ 6,
5014
+ 34,
5015
+ 62
5016
+ ],
5017
+ [
5018
+ 6,
5019
+ 26,
5020
+ 46,
5021
+ 66
5022
+ ],
5023
+ [
5024
+ 6,
5025
+ 26,
5026
+ 48,
5027
+ 70
5028
+ ],
5029
+ [
5030
+ 6,
5031
+ 26,
5032
+ 50,
5033
+ 74
5034
+ ],
5035
+ [
5036
+ 6,
5037
+ 30,
5038
+ 54,
5039
+ 78
5040
+ ],
5041
+ [
5042
+ 6,
5043
+ 30,
5044
+ 56,
5045
+ 82
5046
+ ],
5047
+ [
5048
+ 6,
5049
+ 30,
5050
+ 58,
5051
+ 86
5052
+ ],
5053
+ [
5054
+ 6,
5055
+ 34,
5056
+ 62,
5057
+ 90
5058
+ ],
5059
+ [
5060
+ 6,
5061
+ 28,
5062
+ 50,
5063
+ 72,
5064
+ 94
5065
+ ],
5066
+ [
5067
+ 6,
5068
+ 26,
5069
+ 50,
5070
+ 74,
5071
+ 98
5072
+ ],
5073
+ [
5074
+ 6,
5075
+ 30,
5076
+ 54,
5077
+ 78,
5078
+ 102
5079
+ ],
5080
+ [
5081
+ 6,
5082
+ 28,
5083
+ 54,
5084
+ 80,
5085
+ 106
5086
+ ],
5087
+ [
5088
+ 6,
5089
+ 32,
5090
+ 58,
5091
+ 84,
5092
+ 110
5093
+ ],
5094
+ [
5095
+ 6,
5096
+ 30,
5097
+ 58,
5098
+ 86,
5099
+ 114
5100
+ ],
5101
+ [
5102
+ 6,
5103
+ 34,
5104
+ 62,
5105
+ 90,
5106
+ 118
5107
+ ],
5108
+ [
5109
+ 6,
5110
+ 26,
5111
+ 50,
5112
+ 74,
5113
+ 98,
5114
+ 122
5115
+ ],
5116
+ [
5117
+ 6,
5118
+ 30,
5119
+ 54,
5120
+ 78,
5121
+ 102,
5122
+ 126
5123
+ ],
5124
+ [
5125
+ 6,
5126
+ 26,
5127
+ 52,
5128
+ 78,
5129
+ 104,
5130
+ 130
5131
+ ],
5132
+ [
5133
+ 6,
5134
+ 30,
5135
+ 56,
5136
+ 82,
5137
+ 108,
5138
+ 134
5139
+ ],
5140
+ [
5141
+ 6,
5142
+ 34,
5143
+ 60,
5144
+ 86,
5145
+ 112,
5146
+ 138
5147
+ ],
5148
+ [
5149
+ 6,
5150
+ 30,
5151
+ 58,
5152
+ 86,
5153
+ 114,
5154
+ 142
5155
+ ],
5156
+ [
5157
+ 6,
5158
+ 34,
5159
+ 62,
5160
+ 90,
5161
+ 118,
5162
+ 146
5163
+ ],
5164
+ [
5165
+ 6,
5166
+ 30,
5167
+ 54,
5168
+ 78,
5169
+ 102,
5170
+ 126,
5171
+ 150
5172
+ ],
5173
+ [
5174
+ 6,
5175
+ 24,
5176
+ 50,
5177
+ 76,
5178
+ 102,
5179
+ 128,
5180
+ 154
5181
+ ],
5182
+ [
5183
+ 6,
5184
+ 28,
5185
+ 54,
5186
+ 80,
5187
+ 106,
5188
+ 132,
5189
+ 158
5190
+ ],
5191
+ [
5192
+ 6,
5193
+ 32,
5194
+ 58,
5195
+ 84,
5196
+ 110,
5197
+ 136,
5198
+ 162
5199
+ ],
5200
+ [
5201
+ 6,
5202
+ 26,
5203
+ 54,
5204
+ 82,
5205
+ 110,
5206
+ 138,
5207
+ 166
5208
+ ],
5209
+ [
5210
+ 6,
5211
+ 30,
5212
+ 58,
5213
+ 86,
5214
+ 114,
5215
+ 142,
5216
+ 170
5217
+ ]
5218
+ ];
5219
+ const FORMAT_INFO = [
5220
+ 30660,
5221
+ 29427,
5222
+ 32170,
5223
+ 30877,
5224
+ 26159,
5225
+ 25368,
5226
+ 27713,
5227
+ 26998,
5228
+ 21522,
5229
+ 20773,
5230
+ 24188,
5231
+ 23371,
5232
+ 17913,
5233
+ 16590,
5234
+ 20375,
5235
+ 19104,
5236
+ 13663,
5237
+ 12392,
5238
+ 16177,
5239
+ 14854,
5240
+ 9396,
5241
+ 8579,
5242
+ 11994,
5243
+ 11245,
5244
+ 5769,
5245
+ 5054,
5246
+ 7399,
5247
+ 6608,
5248
+ 1890,
5249
+ 597,
5250
+ 3340,
5251
+ 2107
5252
+ ];
5253
+ const VERSION_INFO = [
5254
+ 31892,
5255
+ 34236,
5256
+ 39577,
5257
+ 42195,
5258
+ 48118,
5259
+ 51042,
5260
+ 55367,
5261
+ 58893,
5262
+ 63784,
5263
+ 68472,
5264
+ 70749,
5265
+ 76311,
5266
+ 79154,
5267
+ 84390,
5268
+ 87683,
5269
+ 92361,
5270
+ 96236,
5271
+ 102084,
5272
+ 102881,
5273
+ 110507,
5274
+ 110734,
5275
+ 117786,
5276
+ 119615,
5277
+ 126325,
5278
+ 127568,
5279
+ 133589,
5280
+ 136944,
5281
+ 141498
5282
+ ];
5283
+ /** Log and antilog tables for GF(256) with primitive polynomial 0x11d. */
5284
+ const GF_EXP = new Uint8Array(512);
5285
+ const GF_LOG = new Uint8Array(256);
5286
+ {
5287
+ let x = 1;
5288
+ for (let i = 0; i < 255; i++) {
5289
+ GF_EXP[i] = x;
5290
+ GF_LOG[x] = i;
5291
+ x <<= 1;
5292
+ if (x >= 256) x ^= 285;
5293
+ }
5294
+ for (let i = 255; i < 512; i++) GF_EXP[i] = GF_EXP[i - 255];
5295
+ }
5296
+ /** Multiply two GF(256) elements. */
5297
+ function gfMul(a, b) {
5298
+ if (a === 0 || b === 0) return 0;
5299
+ return GF_EXP[(GF_LOG[a] + GF_LOG[b]) % 255];
5300
+ }
5301
+ /**
5302
+ * Generate a Reed-Solomon generator polynomial for `numEC` error
5303
+ * correction codewords. Returns coefficients in descending order.
5304
+ */
5305
+ function rsGeneratorPoly(numEC) {
5306
+ const gen = new Uint8Array(numEC + 1);
5307
+ gen[0] = 1;
5308
+ for (let i = 0; i < numEC; i++) {
5309
+ for (let j = numEC; j >= 1; j--) gen[j] = gen[j - 1] ^ gfMul(gen[j], GF_EXP[i]);
5310
+ gen[0] = gfMul(gen[0], GF_EXP[i]);
5311
+ }
5312
+ return gen;
5313
+ }
5314
+ /**
5315
+ * Compute Reed-Solomon error correction codewords for the given data.
5316
+ */
5317
+ function rsEncode(data, numEC) {
5318
+ const gen = rsGeneratorPoly(numEC);
5319
+ const result = new Uint8Array(numEC);
5320
+ for (let i = 0; i < data.length; i++) {
5321
+ const coeff = data[i] ^ result[0];
5322
+ for (let j = 0; j < numEC - 1; j++) result[j] = result[j + 1] ^ gfMul(coeff, gen[numEC - 1 - j]);
5323
+ result[numEC - 1] = gfMul(coeff, gen[0]);
5324
+ }
5325
+ return result;
5326
+ }
5327
+ function detectMode(data) {
5328
+ if (/^\d*$/.test(data)) return "numeric";
5329
+ if (data.split("").every((ch) => ALPHANUMERIC_CHARS.includes(ch))) return "alphanumeric";
5330
+ return "byte";
5331
+ }
5332
+ /** Number of bits for the character count indicator. */
5333
+ function charCountBits(version, mode) {
5334
+ if (version <= 9) return mode === "numeric" ? 10 : mode === "alphanumeric" ? 9 : 8;
5335
+ else if (version <= 26) return mode === "numeric" ? 12 : mode === "alphanumeric" ? 11 : 16;
5336
+ else return mode === "numeric" ? 14 : mode === "alphanumeric" ? 13 : 16;
5337
+ }
5338
+ /** A simple bit buffer. */
5339
+ var BitBuffer = class {
5340
+ bits = [];
5341
+ get length() {
5342
+ return this.bits.length;
5343
+ }
5344
+ put(value, numBits) {
5345
+ for (let i = numBits - 1; i >= 0; i--) this.bits.push(value >>> i & 1);
5346
+ }
5347
+ getBit(index) {
5348
+ return this.bits[index];
5349
+ }
5350
+ /** Convert to byte array (padded to full bytes with trailing zeros). */
5351
+ toBytes() {
5352
+ const byteLen = Math.ceil(this.bits.length / 8);
5353
+ const bytes = new Uint8Array(byteLen);
5354
+ for (let i = 0; i < this.bits.length; i++) if (this.bits[i]) bytes[i >>> 3] |= 128 >>> (i & 7);
5355
+ return bytes;
5356
+ }
5357
+ };
5358
+ /** Encode data into the bit buffer for the given mode. */
5359
+ function encodeData(data, mode, buf) {
5360
+ switch (mode) {
5361
+ case "numeric":
5362
+ encodeNumeric(data, buf);
5363
+ break;
5364
+ case "alphanumeric":
5365
+ encodeAlphanumeric(data, buf);
5366
+ break;
5367
+ case "byte":
5368
+ encodeByte(data, buf);
5369
+ break;
5370
+ }
5371
+ }
5372
+ function encodeNumeric(data, buf) {
5373
+ let i = 0;
5374
+ while (i + 2 < data.length) {
5375
+ const group = parseInt(data.substring(i, i + 3), 10);
5376
+ buf.put(group, 10);
5377
+ i += 3;
5378
+ }
5379
+ if (data.length - i === 2) buf.put(parseInt(data.substring(i, i + 2), 10), 7);
5380
+ else if (data.length - i === 1) buf.put(parseInt(data.substring(i, i + 1), 10), 4);
5381
+ }
5382
+ function encodeAlphanumeric(data, buf) {
5383
+ let i = 0;
5384
+ while (i + 1 < data.length) {
5385
+ const a = ALPHANUMERIC_CHARS.indexOf(data[i]);
5386
+ const b = ALPHANUMERIC_CHARS.indexOf(data[i + 1]);
5387
+ buf.put(a * 45 + b, 11);
5388
+ i += 2;
5389
+ }
5390
+ if (i < data.length) buf.put(ALPHANUMERIC_CHARS.indexOf(data[i]), 6);
5391
+ }
5392
+ function encodeByte(data, buf) {
5393
+ const bytes = new TextEncoder().encode(data);
5394
+ for (const byte of bytes) buf.put(byte, 8);
5395
+ }
5396
+ /** Get the data length for character count purposes. */
5397
+ function getDataLength(data, mode) {
5398
+ if (mode === "byte") return new TextEncoder().encode(data).length;
5399
+ return data.length;
5400
+ }
5401
+ function modeIndicator(mode) {
5402
+ switch (mode) {
5403
+ case "numeric": return MODE_NUMERIC;
5404
+ case "alphanumeric": return MODE_ALPHANUMERIC;
5405
+ case "byte": return MODE_BYTE;
5406
+ }
5407
+ }
5408
+ /**
5409
+ * Select the smallest QR version that can hold the data at the
5410
+ * given EC level.
5411
+ */
5412
+ function selectVersion(data, mode, ecLevel) {
5413
+ const ecOrd = EC_ORDINAL[ecLevel];
5414
+ const dataLen = getDataLength(data, mode);
5415
+ for (let version = 1; version <= 40; version++) {
5416
+ const totalDataBits = TOTAL_DATA_CODEWORDS[version - 1][ecOrd] * 8;
5417
+ const ccBits = charCountBits(version, mode);
5418
+ let dataBits;
5419
+ switch (mode) {
5420
+ case "numeric":
5421
+ dataBits = Math.floor(dataLen / 3) * 10 + (dataLen % 3 === 2 ? 7 : dataLen % 3 === 1 ? 4 : 0);
5422
+ break;
5423
+ case "alphanumeric":
5424
+ dataBits = Math.floor(dataLen / 2) * 11 + (dataLen % 2 === 1 ? 6 : 0);
5425
+ break;
5426
+ case "byte":
5427
+ dataBits = dataLen * 8;
5428
+ break;
5429
+ }
5430
+ if (4 + ccBits + dataBits <= totalDataBits) return version;
5431
+ }
5432
+ throw new Error(`Data too long for QR code (max version 40 with EC level ${ecLevel})`);
5433
+ }
5434
+ function buildDataCodewords(data, mode, version, ecLevel) {
5435
+ const ecOrd = EC_ORDINAL[ecLevel];
5436
+ const totalDataBits = TOTAL_DATA_CODEWORDS[version - 1][ecOrd] * 8;
5437
+ const buf = new BitBuffer();
5438
+ buf.put(modeIndicator(mode), 4);
5439
+ const dataLen = getDataLength(data, mode);
5440
+ buf.put(dataLen, charCountBits(version, mode));
5441
+ encodeData(data, mode, buf);
5442
+ const terminatorLen = Math.min(4, totalDataBits - buf.length);
5443
+ buf.put(0, terminatorLen);
5444
+ while (buf.length % 8 !== 0) buf.put(0, 1);
5445
+ const padBytes = [236, 17];
5446
+ let padIdx = 0;
5447
+ while (buf.length < totalDataBits) {
5448
+ buf.put(padBytes[padIdx % 2], 8);
5449
+ padIdx++;
5450
+ }
5451
+ return buf.toBytes();
5452
+ }
5453
+ function getBlockStructure(version, ecLevel) {
5454
+ const ecOrd = EC_ORDINAL[ecLevel];
5455
+ const bs = BLOCK_STRUCTURE[version - 1][ecOrd];
5456
+ const blocks = [];
5457
+ if (bs[0] > 0) blocks.push({
5458
+ numBlocks: bs[0],
5459
+ dataCodewords: bs[1]
5460
+ });
5461
+ if (bs[2] > 0) blocks.push({
5462
+ numBlocks: bs[2],
5463
+ dataCodewords: bs[3]
5464
+ });
5465
+ return blocks;
5466
+ }
5467
+ /**
5468
+ * Generate final codeword sequence (data + EC, interleaved).
5469
+ */
5470
+ function generateCodewords(data, mode, version, ecLevel) {
5471
+ const ecOrd = EC_ORDINAL[ecLevel];
5472
+ const ecCWPerBlock = EC_CODEWORDS_PER_BLOCK[version - 1][ecOrd];
5473
+ const blockGroups = getBlockStructure(version, ecLevel);
5474
+ const dataCW = buildDataCodewords(data, mode, version, ecLevel);
5475
+ const dataBlocks = [];
5476
+ const ecBlocks = [];
5477
+ let offset = 0;
5478
+ for (const group of blockGroups) for (let b = 0; b < group.numBlocks; b++) {
5479
+ const blockData = dataCW.slice(offset, offset + group.dataCodewords);
5480
+ dataBlocks.push(blockData);
5481
+ ecBlocks.push(rsEncode(blockData, ecCWPerBlock));
5482
+ offset += group.dataCodewords;
5483
+ }
5484
+ const maxDataLen = Math.max(...dataBlocks.map((b) => b.length));
5485
+ const result = [];
5486
+ for (let i = 0; i < maxDataLen; i++) for (const block of dataBlocks) if (i < block.length) result.push(block[i]);
5487
+ for (let i = 0; i < ecCWPerBlock; i++) for (const block of ecBlocks) if (i < block.length) result.push(block[i]);
5488
+ return new Uint8Array(result);
5489
+ }
5490
+ /** Matrix cell states. */
5491
+ const EMPTY = -1;
5492
+ const LIGHT = 0;
5493
+ const DARK = 1;
5494
+ function createMatrix(size) {
5495
+ const matrix = new Int8Array(size * size);
5496
+ matrix.fill(EMPTY);
5497
+ return matrix;
5498
+ }
5499
+ function getCell(matrix, size, row, col) {
5500
+ return matrix[row * size + col];
5501
+ }
5502
+ function setCell(matrix, size, row, col, value) {
5503
+ matrix[row * size + col] = value;
5504
+ }
5505
+ /** Place a finder pattern with its top-left corner at (row, col). */
5506
+ function placeFinderPattern(matrix, size, row, col) {
5507
+ for (let r = -1; r <= 7; r++) for (let c = -1; c <= 7; c++) {
5508
+ const mr = row + r;
5509
+ const mc = col + c;
5510
+ if (mr < 0 || mr >= size || mc < 0 || mc >= size) continue;
5511
+ if (r >= 0 && r <= 6 && (c === 0 || c === 6) || c >= 0 && c <= 6 && (r === 0 || r === 6) || r >= 2 && r <= 4 && c >= 2 && c <= 4) setCell(matrix, size, mr, mc, DARK);
5512
+ else setCell(matrix, size, mr, mc, LIGHT);
5513
+ }
5514
+ }
5515
+ /** Place an alignment pattern centred at (row, col). */
5516
+ function placeAlignmentPattern(matrix, size, centerRow, centerCol) {
5517
+ for (let r = -2; r <= 2; r++) for (let c = -2; c <= 2; c++) {
5518
+ const mr = centerRow + r;
5519
+ const mc = centerCol + c;
5520
+ if (mr < 0 || mr >= size || mc < 0 || mc >= size) continue;
5521
+ if (Math.abs(r) === 2 || Math.abs(c) === 2 || r === 0 && c === 0) setCell(matrix, size, mr, mc, DARK);
5522
+ else setCell(matrix, size, mr, mc, LIGHT);
5523
+ }
5524
+ }
5525
+ /** Place timing patterns (row 6, col 6). */
5526
+ function placeTimingPatterns(matrix, size) {
5527
+ for (let i = 8; i < size - 8; i++) {
5528
+ const val = i % 2 === 0 ? DARK : LIGHT;
5529
+ if (getCell(matrix, size, 6, i) === EMPTY) setCell(matrix, size, 6, i, val);
5530
+ if (getCell(matrix, size, i, 6) === EMPTY) setCell(matrix, size, i, 6, val);
5531
+ }
5532
+ }
5533
+ /** Reserve format information areas (set to LIGHT temporarily). */
5534
+ function reserveFormatAreas(matrix, size) {
5535
+ for (let i = 0; i <= 8; i++) {
5536
+ if (getCell(matrix, size, 8, i) === EMPTY) setCell(matrix, size, 8, i, LIGHT);
5537
+ if (getCell(matrix, size, i, 8) === EMPTY) setCell(matrix, size, i, 8, LIGHT);
5538
+ }
5539
+ for (let i = 0; i <= 7; i++) if (getCell(matrix, size, size - 1 - i, 8) === EMPTY) setCell(matrix, size, size - 1 - i, 8, LIGHT);
5540
+ for (let i = 0; i <= 7; i++) if (getCell(matrix, size, 8, size - 1 - i) === EMPTY) setCell(matrix, size, 8, size - 1 - i, LIGHT);
5541
+ setCell(matrix, size, size - 8, 8, DARK);
5542
+ }
5543
+ /** Reserve version information areas (versions >= 7). */
5544
+ function reserveVersionAreas(matrix, size, version) {
5545
+ if (version < 7) return;
5546
+ for (let r = 0; r < 6; r++) for (let c = 0; c < 3; c++) if (getCell(matrix, size, size - 11 + c, r) === EMPTY) setCell(matrix, size, size - 11 + c, r, LIGHT);
5547
+ for (let r = 0; r < 3; r++) for (let c = 0; c < 6; c++) if (getCell(matrix, size, r, size - 11 + c) === EMPTY) setCell(matrix, size, r, size - 11 + c, LIGHT);
5548
+ }
5549
+ /** Check if an alignment pattern centre conflicts with finder patterns. */
5550
+ function conflictsWithFinder(row, col, size) {
5551
+ if (row <= 8 && col <= 8) return true;
5552
+ if (row <= 8 && col >= size - 9) return true;
5553
+ if (row >= size - 9 && col <= 8) return true;
5554
+ return false;
5555
+ }
5556
+ /**
5557
+ * Place all function patterns on the matrix.
5558
+ */
5559
+ function placeFunctionPatterns(matrix, size, version) {
5560
+ placeFinderPattern(matrix, size, 0, 0);
5561
+ placeFinderPattern(matrix, size, 0, size - 7);
5562
+ placeFinderPattern(matrix, size, size - 7, 0);
5563
+ placeTimingPatterns(matrix, size);
5564
+ if (version >= 2) {
5565
+ const positions = ALIGNMENT_POSITIONS[version - 1];
5566
+ for (const row of positions) for (const col of positions) if (!conflictsWithFinder(row, col, size)) placeAlignmentPattern(matrix, size, row, col);
5567
+ }
5568
+ reserveFormatAreas(matrix, size);
5569
+ reserveVersionAreas(matrix, size, version);
5570
+ }
5571
+ /**
5572
+ * Place data bits into the matrix, following the zigzag pattern
5573
+ * specified by ISO 18004.
5574
+ */
5575
+ function placeDataBits(matrix, size, codewords) {
5576
+ let bitIndex = 0;
5577
+ const totalBits = codewords.length * 8;
5578
+ let col = size - 1;
5579
+ let goingUp = true;
5580
+ while (col >= 0) {
5581
+ if (col === 6) {
5582
+ col--;
5583
+ continue;
5584
+ }
5585
+ const startRow = goingUp ? size - 1 : 0;
5586
+ const endRow = goingUp ? -1 : size;
5587
+ const step = goingUp ? -1 : 1;
5588
+ for (let row = startRow; row !== endRow; row += step) for (let dc = 0; dc <= 1; dc++) {
5589
+ const c = col - dc;
5590
+ if (c < 0) continue;
5591
+ if (getCell(matrix, size, row, c) !== EMPTY) continue;
5592
+ if (bitIndex < totalBits) {
5593
+ const byteIdx = bitIndex >>> 3;
5594
+ const bitPos = 7 - (bitIndex & 7);
5595
+ const bit = codewords[byteIdx] >>> bitPos & 1;
5596
+ setCell(matrix, size, row, c, bit ? DARK : LIGHT);
5597
+ bitIndex++;
5598
+ } else setCell(matrix, size, row, c, LIGHT);
5599
+ }
5600
+ col -= 2;
5601
+ goingUp = !goingUp;
5602
+ }
5603
+ }
5604
+ const MASK_FUNCTIONS = [
5605
+ (r, c) => (r + c) % 2 === 0,
5606
+ (r, _c) => r % 2 === 0,
5607
+ (_r, c) => c % 3 === 0,
5608
+ (r, c) => (r + c) % 3 === 0,
5609
+ (r, c) => (Math.floor(r / 2) + Math.floor(c / 3)) % 2 === 0,
5610
+ (r, c) => r * c % 2 + r * c % 3 === 0,
5611
+ (r, c) => (r * c % 2 + r * c % 3) % 2 === 0,
5612
+ (r, c) => ((r + c) % 2 + r * c % 3) % 2 === 0
5613
+ ];
5614
+ /**
5615
+ * Create a mask of which cells are data cells (not function patterns).
5616
+ */
5617
+ function createFunctionPatternMask(size, version) {
5618
+ const mask = new Uint8Array(size * size);
5619
+ const tempMatrix = createMatrix(size);
5620
+ placeFunctionPatterns(tempMatrix, size, version);
5621
+ for (let i = 0; i < size * size; i++) mask[i] = tempMatrix[i] !== EMPTY ? 1 : 0;
5622
+ return mask;
5623
+ }
5624
+ /**
5625
+ * Apply a mask pattern to the matrix, only affecting data cells.
5626
+ */
5627
+ function applyMask(matrix, size, maskIndex, functionMask) {
5628
+ const maskFn = MASK_FUNCTIONS[maskIndex];
5629
+ for (let row = 0; row < size; row++) for (let col = 0; col < size; col++) {
5630
+ const idx = row * size + col;
5631
+ if (functionMask[idx]) continue;
5632
+ if (maskFn(row, col)) matrix[idx] = matrix[idx] === DARK ? LIGHT : DARK;
5633
+ }
5634
+ }
5635
+ function embedFormatInfo(matrix, size, ecLevel, maskIndex) {
5636
+ const formatBits = FORMAT_INFO[EC_ORDINAL[ecLevel] * 8 + maskIndex];
5637
+ const horizontalPositions = [
5638
+ 0,
5639
+ 1,
5640
+ 2,
5641
+ 3,
5642
+ 4,
5643
+ 5,
5644
+ 7,
5645
+ 8,
5646
+ size - 8,
5647
+ size - 7,
5648
+ size - 6,
5649
+ size - 5,
5650
+ size - 4,
5651
+ size - 3,
5652
+ size - 2,
5653
+ size - 1
5654
+ ];
5655
+ for (let i = 0; i < 15; i++) {
5656
+ const bit = formatBits >>> 14 - i & 1;
5657
+ setCell(matrix, size, 8, horizontalPositions[i], bit ? DARK : LIGHT);
5658
+ }
5659
+ const verticalPositions = [
5660
+ size - 1,
5661
+ size - 2,
5662
+ size - 3,
5663
+ size - 4,
5664
+ size - 5,
5665
+ size - 6,
5666
+ size - 7,
5667
+ size - 8,
5668
+ 7,
5669
+ 5,
5670
+ 4,
5671
+ 3,
5672
+ 2,
5673
+ 1,
5674
+ 0
5675
+ ];
5676
+ for (let i = 0; i < 15; i++) {
5677
+ const bit = formatBits >>> 14 - i & 1;
5678
+ setCell(matrix, size, verticalPositions[i], 8, bit ? DARK : LIGHT);
5679
+ }
5680
+ }
5681
+ function embedVersionInfo(matrix, size, version) {
5682
+ if (version < 7) return;
5683
+ const versionBits = VERSION_INFO[version - 7];
5684
+ for (let i = 0; i < 18; i++) {
5685
+ const bit = versionBits >>> i & 1;
5686
+ const row = Math.floor(i / 3);
5687
+ const col = i % 3;
5688
+ const value = bit ? DARK : LIGHT;
5689
+ setCell(matrix, size, size - 11 + col, row, value);
5690
+ setCell(matrix, size, row, size - 11 + col, value);
5691
+ }
5692
+ }
5693
+ function computePenalty(matrix, size) {
5694
+ let penalty = 0;
5695
+ for (let row = 0; row < size; row++) {
5696
+ let count = 1;
5697
+ for (let col = 1; col < size; col++) if (getCell(matrix, size, row, col) === getCell(matrix, size, row, col - 1)) count++;
5698
+ else {
5699
+ if (count >= 5) penalty += count - 2;
5700
+ count = 1;
5701
+ }
5702
+ if (count >= 5) penalty += count - 2;
5703
+ }
5704
+ for (let col = 0; col < size; col++) {
5705
+ let count = 1;
5706
+ for (let row = 1; row < size; row++) if (getCell(matrix, size, row, col) === getCell(matrix, size, row - 1, col)) count++;
5707
+ else {
5708
+ if (count >= 5) penalty += count - 2;
5709
+ count = 1;
5710
+ }
5711
+ if (count >= 5) penalty += count - 2;
5712
+ }
5713
+ for (let row = 0; row < size - 1; row++) for (let col = 0; col < size - 1; col++) {
5714
+ const val = getCell(matrix, size, row, col);
5715
+ if (val === getCell(matrix, size, row, col + 1) && val === getCell(matrix, size, row + 1, col) && val === getCell(matrix, size, row + 1, col + 1)) penalty += 3;
5716
+ }
5717
+ for (let row = 0; row < size; row++) for (let col = 0; col <= size - 11; col++) {
5718
+ if (getCell(matrix, size, row, col) === DARK && getCell(matrix, size, row, col + 1) === LIGHT && getCell(matrix, size, row, col + 2) === DARK && getCell(matrix, size, row, col + 3) === DARK && getCell(matrix, size, row, col + 4) === DARK && getCell(matrix, size, row, col + 5) === LIGHT && getCell(matrix, size, row, col + 6) === DARK && getCell(matrix, size, row, col + 7) === LIGHT && getCell(matrix, size, row, col + 8) === LIGHT && getCell(matrix, size, row, col + 9) === LIGHT && getCell(matrix, size, row, col + 10) === LIGHT) penalty += 40;
5719
+ if (getCell(matrix, size, row, col) === LIGHT && getCell(matrix, size, row, col + 1) === LIGHT && getCell(matrix, size, row, col + 2) === LIGHT && getCell(matrix, size, row, col + 3) === LIGHT && getCell(matrix, size, row, col + 4) === DARK && getCell(matrix, size, row, col + 5) === LIGHT && getCell(matrix, size, row, col + 6) === DARK && getCell(matrix, size, row, col + 7) === DARK && getCell(matrix, size, row, col + 8) === DARK && getCell(matrix, size, row, col + 9) === LIGHT && getCell(matrix, size, row, col + 10) === DARK) penalty += 40;
5720
+ }
5721
+ for (let col = 0; col < size; col++) for (let row = 0; row <= size - 11; row++) {
5722
+ if (getCell(matrix, size, row, col) === DARK && getCell(matrix, size, row + 1, col) === LIGHT && getCell(matrix, size, row + 2, col) === DARK && getCell(matrix, size, row + 3, col) === DARK && getCell(matrix, size, row + 4, col) === DARK && getCell(matrix, size, row + 5, col) === LIGHT && getCell(matrix, size, row + 6, col) === DARK && getCell(matrix, size, row + 7, col) === LIGHT && getCell(matrix, size, row + 8, col) === LIGHT && getCell(matrix, size, row + 9, col) === LIGHT && getCell(matrix, size, row + 10, col) === LIGHT) penalty += 40;
5723
+ if (getCell(matrix, size, row, col) === LIGHT && getCell(matrix, size, row + 1, col) === LIGHT && getCell(matrix, size, row + 2, col) === LIGHT && getCell(matrix, size, row + 3, col) === LIGHT && getCell(matrix, size, row + 4, col) === DARK && getCell(matrix, size, row + 5, col) === LIGHT && getCell(matrix, size, row + 6, col) === DARK && getCell(matrix, size, row + 7, col) === DARK && getCell(matrix, size, row + 8, col) === DARK && getCell(matrix, size, row + 9, col) === LIGHT && getCell(matrix, size, row + 10, col) === DARK) penalty += 40;
5724
+ }
5725
+ let darkCount = 0;
5726
+ const total = size * size;
5727
+ for (let i = 0; i < total; i++) if (matrix[i] === DARK) darkCount++;
5728
+ const percent = darkCount * 100 / total;
5729
+ const prev5 = Math.floor(percent / 5) * 5;
5730
+ const next5 = prev5 + 5;
5731
+ const prevDiff = Math.abs(prev5 - 50) / 5;
5732
+ const nextDiff = Math.abs(next5 - 50) / 5;
5733
+ penalty += Math.min(prevDiff, nextDiff) * 10;
5734
+ return penalty;
5735
+ }
5736
+ /**
5737
+ * Encode a string as a QR code matrix.
5738
+ *
5739
+ * @param data The string to encode.
5740
+ * @param errorCorrection Error correction level (default: `'M'`).
5741
+ * @returns A {@link QrCodeMatrix} with the encoded data.
5742
+ */
5743
+ function encodeQrCode(data, errorCorrection = "M") {
5744
+ const mode = detectMode(data);
5745
+ const version = selectVersion(data, mode, errorCorrection);
5746
+ const size = version * 4 + 17;
5747
+ const codewords = generateCodewords(data, mode, version, errorCorrection);
5748
+ const functionMask = createFunctionPatternMask(size, version);
5749
+ let bestPenalty = Infinity;
5750
+ let bestMatrix = null;
5751
+ for (let maskIdx = 0; maskIdx < 8; maskIdx++) {
5752
+ const matrix = createMatrix(size);
5753
+ placeFunctionPatterns(matrix, size, version);
5754
+ placeDataBits(matrix, size, codewords);
5755
+ applyMask(matrix, size, maskIdx, functionMask);
5756
+ embedFormatInfo(matrix, size, errorCorrection, maskIdx);
5757
+ embedVersionInfo(matrix, size, version);
5758
+ const penalty = computePenalty(matrix, size);
5759
+ if (penalty < bestPenalty) {
5760
+ bestPenalty = penalty;
5761
+ bestMatrix = matrix;
5762
+ }
5763
+ }
5764
+ const modules = new Array(size * size);
5765
+ for (let i = 0; i < size * size; i++) modules[i] = bestMatrix[i] === DARK;
5766
+ return {
5767
+ size,
5768
+ modules,
5769
+ version
5770
+ };
5771
+ }
5772
+ /**
5773
+ * Convert a {@link QrCodeMatrix} to PDF content-stream operators.
5774
+ *
5775
+ * The QR code is rendered as filled rectangles (one per dark module),
5776
+ * positioned at `(x, y)` in PDF user-space coordinates. The `y`
5777
+ * coordinate refers to the **bottom-left** corner of the QR code.
5778
+ *
5779
+ * @param matrix The QR code matrix from {@link encodeQrCode}.
5780
+ * @param x X position in PDF points.
5781
+ * @param y Y position in PDF points.
5782
+ * @param options Rendering options (module size, quiet zone, colours).
5783
+ * @returns A string of PDF content-stream operators.
5784
+ */
5785
+ function qrCodeToOperators(matrix, x, y, options = {}) {
5786
+ const moduleSize = options.moduleSize ?? 2;
5787
+ const quietZone = options.quietZone ?? 4;
5788
+ const fgColor = options.color ?? {
5789
+ type: "grayscale",
5790
+ gray: 0
5791
+ };
5792
+ const bgColor = options.backgroundColor ?? {
5793
+ type: "grayscale",
5794
+ gray: 1
5795
+ };
5796
+ const totalSize = (matrix.size + 2 * quietZone) * moduleSize;
5797
+ let ops = "";
5798
+ ops += saveState();
5799
+ ops += applyFillColor(bgColor);
5800
+ ops += rectangle(x, y, totalSize, totalSize);
5801
+ ops += fill();
5802
+ ops += applyFillColor(fgColor);
5803
+ for (let row = 0; row < matrix.size; row++) for (let col = 0; col < matrix.size; col++) if (matrix.modules[row * matrix.size + col]) {
5804
+ const px = x + (quietZone + col) * moduleSize;
5805
+ const py = y + (quietZone + matrix.size - 1 - row) * moduleSize;
5806
+ ops += rectangle(px, py, moduleSize, moduleSize);
5807
+ }
5808
+ ops += fill();
5809
+ ops += restoreState();
5810
+ return ops;
5811
+ }
5812
+
5813
+ //#endregion
5814
+ //#region src/layout/table.ts
5815
+ /**
5816
+ * @module layout/table
5817
+ *
5818
+ * Table layout engine for PDF rendering.
5819
+ *
5820
+ * Renders tables as native PDF vector graphics with cells,
5821
+ * borders, text, and basic styling. All output is a string of
5822
+ * raw PDF content-stream operators that can be appended directly
5823
+ * to a page's operator buffer.
5824
+ *
5825
+ * Supports colspan, rowspan, rich cell content (styled text runs),
5826
+ * text overflow modes (wrap, truncate, ellipsis, shrink), and
5827
+ * automatic page breaks with header-row repetition.
5828
+ */
5829
+ function isNestedTable(content) {
5830
+ return typeof content === "object" && content !== null && !Array.isArray(content) && content.type === "table";
5831
+ }
5832
+ function isTextRunArray(content) {
5833
+ return Array.isArray(content);
5834
+ }
5835
+ function cellText(content) {
5836
+ if (typeof content === "string") return content;
5837
+ if (isTextRunArray(content)) return content.map((r) => r.text).join("");
5838
+ return "";
5839
+ }
5840
+ function estimateContentWidth(content, defaultFontSize) {
5841
+ if (typeof content === "string") return content.length * defaultFontSize * .5;
5842
+ if (isTextRunArray(content)) {
5843
+ let total = 0;
5844
+ for (const run of content) total += run.text.length * (run.fontSize ?? defaultFontSize) * .5;
5845
+ return total;
5846
+ }
5847
+ return 0;
5848
+ }
5849
+ function normalizeCell(cell) {
5850
+ if (cell === void 0) return { content: "" };
5851
+ if (typeof cell === "string") return { content: cell };
5852
+ return cell;
5853
+ }
5854
+ function resolvePadding(padding, defaultPadding) {
5855
+ if (padding === void 0) return {
5856
+ top: defaultPadding,
5857
+ right: defaultPadding,
5858
+ bottom: defaultPadding,
5859
+ left: defaultPadding
5860
+ };
5861
+ if (typeof padding === "number") return {
5862
+ top: padding,
5863
+ right: padding,
5864
+ bottom: padding,
5865
+ left: padding
5866
+ };
5867
+ return {
5868
+ top: padding.top ?? defaultPadding,
5869
+ right: padding.right ?? defaultPadding,
5870
+ bottom: padding.bottom ?? defaultPadding,
5871
+ left: padding.left ?? defaultPadding
5872
+ };
5873
+ }
5874
+ function resolveColumnCount(options) {
5875
+ let maxCols = 0;
5876
+ for (const row of options.rows) {
5877
+ let cols = 0;
5878
+ for (const cell of row.cells) {
5879
+ const c = normalizeCell(cell);
5880
+ cols += c.colSpan ?? 1;
5881
+ }
5882
+ if (cols > maxCols) maxCols = cols;
5883
+ }
5884
+ return Math.max(maxCols, 1);
5885
+ }
5886
+ function parsePercentage(value) {
5887
+ const trimmed = value.trim();
5888
+ if (!trimmed.endsWith("%")) return NaN;
5889
+ const num = Number(trimmed.slice(0, -1));
5890
+ if (!Number.isFinite(num) || num < 0) return NaN;
5891
+ return num / 100;
5892
+ }
5893
+ function estimateAutoFitWidth(colIndex, options, fontSize, defaultPadding) {
5894
+ let maxWidth = 0;
5895
+ for (const row of options.rows) {
5896
+ let ci = 0;
5897
+ for (const rawCell of row.cells) {
5898
+ const cell = normalizeCell(rawCell);
5899
+ const span = cell.colSpan ?? 1;
5900
+ if (ci === colIndex && span === 1) {
5901
+ const p = resolvePadding(cell.padding, defaultPadding);
5902
+ const cellFontSize = cell.fontSize ?? fontSize;
5903
+ const needed = estimateContentWidth(cell.content, cellFontSize) + p.left + p.right;
5904
+ if (needed > maxWidth) maxWidth = needed;
5905
+ }
5906
+ ci += span;
5907
+ if (ci > colIndex) break;
5908
+ }
5909
+ }
5910
+ return maxWidth || defaultPadding * 2;
5911
+ }
5912
+ function resolveColumnWidths(options) {
5913
+ const numCols = resolveColumnCount(options);
5914
+ const tableWidth = options.width;
5915
+ const fontSize = options.fontSize ?? 12;
5916
+ const defaultPadding = options.padding ?? 4;
5917
+ if (!options.columns || options.columns.length === 0) {
5918
+ const colWidth = tableWidth / numCols;
5919
+ return Array.from({ length: numCols }, () => colWidth);
5920
+ }
5921
+ const widths = new Array(numCols).fill(-1);
5922
+ const modes = new Array(numCols).fill("flex");
5923
+ let consumed = 0;
5924
+ for (let i = 0; i < numCols; i++) {
5925
+ const col = options.columns[i];
5926
+ if (col?.width != null && col.width > 0) {
5927
+ widths[i] = col.width;
5928
+ modes[i] = "fixed";
5929
+ consumed += col.width;
5930
+ }
5931
+ }
5932
+ for (let i = 0; i < numCols; i++) {
5933
+ if (modes[i] !== "flex") continue;
5934
+ const col = options.columns[i];
5935
+ if (col?.percentage != null) {
5936
+ const fraction = parsePercentage(col.percentage);
5937
+ if (Number.isFinite(fraction)) {
5938
+ widths[i] = fraction * tableWidth;
5939
+ modes[i] = "percentage";
5940
+ consumed += widths[i];
5941
+ }
5942
+ }
5943
+ }
5944
+ for (let i = 0; i < numCols; i++) {
5945
+ if (modes[i] !== "flex") continue;
5946
+ if (options.columns[i]?.autoFit) {
5947
+ widths[i] = estimateAutoFitWidth(i, options, fontSize, defaultPadding);
5948
+ modes[i] = "autofit";
5949
+ consumed += widths[i];
5950
+ }
5951
+ }
5952
+ const flexIndices = [];
5953
+ let totalFlex = 0;
5954
+ for (let i = 0; i < numCols; i++) if (modes[i] === "flex") {
5955
+ const weight = options.columns[i]?.flex ?? 1;
5956
+ totalFlex += weight;
5957
+ flexIndices.push(i);
5958
+ }
5959
+ const remaining = Math.max(0, tableWidth - consumed);
5960
+ for (const idx of flexIndices) {
5961
+ const weight = options.columns[idx]?.flex ?? 1;
5962
+ widths[idx] = totalFlex > 0 ? weight / totalFlex * remaining : 0;
5963
+ }
5964
+ for (let i = 0; i < numCols; i++) {
5965
+ const col = options.columns[i];
5966
+ if (col?.minWidth != null && widths[i] < col.minWidth) widths[i] = col.minWidth;
5967
+ if (col?.maxWidth != null && widths[i] > col.maxWidth) widths[i] = col.maxWidth;
5968
+ }
5969
+ return widths;
5970
+ }
5971
+ function computeNestedTableHeight(nested, width) {
5972
+ const { result } = renderTable({
5973
+ ...nested.table,
5974
+ x: 0,
5975
+ y: 0,
5976
+ width
5977
+ });
5978
+ return result.height;
5979
+ }
5980
+ function wrapTextInternal(text, maxWidth, fontSize) {
5981
+ const charWidth = fontSize * .5;
5982
+ const maxChars = Math.max(1, Math.floor(maxWidth / charWidth));
5983
+ const lines = [];
5984
+ const words = text.split(" ");
5985
+ let currentLine = "";
5986
+ for (const word of words) {
5987
+ const candidate = currentLine ? currentLine + " " + word : word;
5988
+ if (candidate.length > maxChars && currentLine) {
5989
+ lines.push(currentLine);
5990
+ currentLine = word;
5991
+ } else currentLine = candidate;
5992
+ }
5993
+ if (currentLine) lines.push(currentLine);
5994
+ return lines.length > 0 ? lines : [""];
5995
+ }
5996
+ function truncateWithEllipsis(text, maxWidth, fontSize) {
5997
+ const charWidth = fontSize * .5;
5998
+ const maxChars = Math.max(1, Math.floor(maxWidth / charWidth));
5999
+ if (text.length <= maxChars) return text;
6000
+ const cutoff = Math.max(0, maxChars - 3);
6001
+ return text.slice(0, cutoff) + "...";
6002
+ }
6003
+ function shrinkFontSize(text, maxWidth, fontSize) {
6004
+ if (text.length * fontSize * .5 <= maxWidth || text.length === 0) return fontSize;
6005
+ const needed = maxWidth / (text.length * .5);
6006
+ return Math.max(4, Math.min(fontSize, needed));
6007
+ }
6008
+ function resolveRowHeights(options, fontSize, defaultPadding, colWidths) {
6009
+ return options.rows.map((row) => {
6010
+ if (row.height) return row.height;
6011
+ let maxHeight = defaultPadding * 2 + fontSize;
6012
+ let colIdx = 0;
6013
+ for (const rawCell of row.cells) {
6014
+ const c = normalizeCell(rawCell);
6015
+ const p = resolvePadding(c.padding, defaultPadding);
6016
+ const span = c.colSpan ?? 1;
6017
+ let cellWidth = 0;
6018
+ for (let s = 0; s < span && colIdx + s < colWidths.length; s++) cellWidth += colWidths[colIdx + s];
6019
+ if (isNestedTable(c.content)) {
6020
+ const nestedWidth = cellWidth - p.left - p.right;
6021
+ const nestedHeight = computeNestedTableHeight(c.content, Math.max(0, nestedWidth));
6022
+ const h = p.top + p.bottom + nestedHeight;
6023
+ if (h > maxHeight) maxHeight = h;
6024
+ } else {
6025
+ const cellFontSize = c.fontSize ?? fontSize;
6026
+ const contentText = cellText(c.content);
6027
+ if (c.overflow === "wrap" && contentText) {
6028
+ const lines = wrapTextInternal(contentText, cellWidth - p.left - p.right, cellFontSize);
6029
+ const lineHeight = cellFontSize * 1.2;
6030
+ const h = p.top + p.bottom + lines.length * lineHeight;
6031
+ if (h > maxHeight) maxHeight = h;
6032
+ } else {
6033
+ const h = p.top + p.bottom + cellFontSize;
6034
+ if (h > maxHeight) maxHeight = h;
6035
+ }
6036
+ }
6037
+ colIdx += span;
6038
+ }
6039
+ return maxHeight;
6040
+ });
6041
+ }
6042
+ function buildOccupationGrid(rows, numCols) {
6043
+ const grid = Array.from({ length: rows.length }, () => Array.from({ length: numCols }, () => null));
6044
+ for (let r = 0; r < rows.length; r++) {
6045
+ let logicalCol = 0;
6046
+ for (const rawCell of rows[r].cells) {
6047
+ while (logicalCol < numCols && grid[r][logicalCol] !== null) logicalCol++;
6048
+ if (logicalCol >= numCols) break;
6049
+ const cell = normalizeCell(rawCell);
6050
+ const cs = Math.max(1, cell.colSpan ?? 1);
6051
+ const rs = Math.max(1, cell.rowSpan ?? 1);
6052
+ const entry = {
6053
+ cell,
6054
+ originRow: r,
6055
+ originCol: logicalCol
6056
+ };
6057
+ for (let dr = 0; dr < rs; dr++) for (let dc = 0; dc < cs; dc++) {
6058
+ const tr = r + dr;
6059
+ const tc = logicalCol + dc;
6060
+ if (tr < rows.length && tc < numCols) grid[tr][tc] = entry;
6061
+ }
6062
+ logicalCol += cs;
6063
+ }
6064
+ }
6065
+ return grid;
6066
+ }
6067
+ function spanWidth(colWidths, startCol, span) {
6068
+ let w = 0;
6069
+ for (let i = 0; i < span && startCol + i < colWidths.length; i++) w += colWidths[startCol + i];
6070
+ return w;
6071
+ }
6072
+ function spanHeight(rowHeights, startRow, span) {
6073
+ let h = 0;
6074
+ for (let i = 0; i < span && startRow + i < rowHeights.length; i++) h += rowHeights[startRow + i];
6075
+ return h;
6076
+ }
6077
+ function columnX(baseX, colWidths, col) {
6078
+ let x = baseX;
6079
+ for (let i = 0; i < col; i++) x += colWidths[i];
6080
+ return x;
6081
+ }
6082
+ function renderPlainTextOps(cell, text, cellX, cellY, cellWidth, cellHeight, fontName, fontSize, padding, textColor, colDef) {
6083
+ let ops = "";
6084
+ const cellTextColor = cell.textColor ?? textColor;
6085
+ let cellFontSize = cell.fontSize ?? fontSize;
6086
+ const align = cell.align ?? colDef?.align ?? "left";
6087
+ const verticalAlign = cell.verticalAlign ?? "top";
6088
+ const contentWidth = cellWidth - padding.left - padding.right;
6089
+ const overflow = cell.overflow ?? "truncate";
6090
+ if (overflow === "wrap") {
6091
+ const lines = wrapTextInternal(text, contentWidth, cellFontSize);
6092
+ const lineHeight = cellFontSize * 1.2;
6093
+ let startY;
6094
+ if (verticalAlign === "middle") {
6095
+ const totalTextHeight = lines.length * lineHeight;
6096
+ startY = cellY - cellHeight / 2 + totalTextHeight / 2 - cellFontSize;
6097
+ } else if (verticalAlign === "bottom") startY = cellY - cellHeight + padding.bottom + (lines.length - 1) * lineHeight;
6098
+ else startY = cellY - padding.top - cellFontSize;
6099
+ ops += saveState();
6100
+ ops += applyFillColor(cellTextColor);
6101
+ for (let li = 0; li < lines.length; li++) {
6102
+ const line = lines[li];
6103
+ const approxLineWidth = line.length * cellFontSize * .5;
6104
+ let textX;
6105
+ if (align === "center") textX = cellX + padding.left + Math.max(0, contentWidth - approxLineWidth) / 2;
6106
+ else if (align === "right") textX = cellX + cellWidth - padding.right - approxLineWidth;
6107
+ else textX = cellX + padding.left;
6108
+ const textY = startY - li * lineHeight;
6109
+ ops += beginText();
6110
+ ops += setFont(fontName, cellFontSize);
6111
+ ops += moveText(textX, textY);
6112
+ ops += showText(line);
6113
+ ops += endText();
6114
+ }
6115
+ ops += restoreState();
6116
+ } else {
6117
+ let displayText = text;
6118
+ if (overflow === "ellipsis") displayText = truncateWithEllipsis(text, contentWidth, cellFontSize);
6119
+ else if (overflow === "shrink") cellFontSize = shrinkFontSize(text, contentWidth, cellFontSize);
6120
+ const approxTextWidth = displayText.length * cellFontSize * .5;
6121
+ let textX;
6122
+ if (align === "center") textX = cellX + padding.left + Math.max(0, contentWidth - approxTextWidth) / 2;
6123
+ else if (align === "right") textX = cellX + cellWidth - padding.right - approxTextWidth;
6124
+ else textX = cellX + padding.left;
6125
+ let textY;
6126
+ if (verticalAlign === "middle") textY = cellY - cellHeight / 2 - cellFontSize / 2 + cellFontSize * .2;
6127
+ else if (verticalAlign === "bottom") textY = cellY - cellHeight + padding.bottom;
6128
+ else textY = cellY - padding.top - cellFontSize;
6129
+ ops += saveState();
6130
+ if (overflow === "truncate") {
6131
+ ops += rectangle(cellX, cellY - cellHeight, cellWidth, cellHeight);
6132
+ ops += clip();
6133
+ ops += endPath();
6134
+ }
6135
+ ops += applyFillColor(cellTextColor);
6136
+ ops += beginText();
6137
+ ops += setFont(fontName, cellFontSize);
6138
+ ops += moveText(textX, textY);
6139
+ ops += showText(displayText);
6140
+ ops += endText();
6141
+ ops += restoreState();
6142
+ }
6143
+ return ops;
6144
+ }
6145
+ function renderCellOps(cell, cellX, cellY, cellWidth, cellHeight, fontName, fontSize, defaultPadding, borderWidth, borderColor, textColor, colDef) {
6146
+ let ops = "";
6147
+ const padding = resolvePadding(cell.padding, defaultPadding);
6148
+ if (cell.backgroundColor) {
6149
+ ops += saveState();
6150
+ ops += applyFillColor(cell.backgroundColor);
6151
+ ops += rectangle(cellX, cellY - cellHeight, cellWidth, cellHeight);
6152
+ ops += fill();
6153
+ ops += restoreState();
6154
+ }
6155
+ if (borderWidth > 0) {
6156
+ ops += saveState();
6157
+ ops += applyStrokeColor(borderColor);
6158
+ ops += setLineWidth(borderWidth);
6159
+ ops += rectangle(cellX, cellY - cellHeight, cellWidth, cellHeight);
6160
+ ops += stroke();
6161
+ ops += restoreState();
6162
+ }
6163
+ const content = cell.content;
6164
+ if (isNestedTable(content)) {
6165
+ const nestedWidth = cellWidth - padding.left - padding.right;
6166
+ const nestedX = cellX + padding.left;
6167
+ const nestedY = cellY - padding.top;
6168
+ const { ops: nestedOps } = renderTable({
6169
+ ...content.table,
6170
+ x: nestedX,
6171
+ y: nestedY,
6172
+ width: Math.max(0, nestedWidth)
6173
+ });
6174
+ ops += nestedOps;
6175
+ } else if (isTextRunArray(content)) {
6176
+ if (content.length > 0) {
6177
+ const cellTextColor = cell.textColor ?? textColor;
6178
+ const cellFontSize = cell.fontSize ?? fontSize;
6179
+ const align = cell.align ?? colDef?.align ?? "left";
6180
+ const verticalAlign = cell.verticalAlign ?? "top";
6181
+ const approxTextWidth = estimateContentWidth(content, cellFontSize);
6182
+ const contentWidth = cellWidth - padding.left - padding.right;
6183
+ let textXPos;
6184
+ if (align === "center") textXPos = cellX + padding.left + Math.max(0, contentWidth - approxTextWidth) / 2;
6185
+ else if (align === "right") textXPos = cellX + cellWidth - padding.right - approxTextWidth;
6186
+ else textXPos = cellX + padding.left;
6187
+ let textYPos;
6188
+ if (verticalAlign === "middle") textYPos = cellY - cellHeight / 2 - cellFontSize / 2 + cellFontSize * .2;
6189
+ else if (verticalAlign === "bottom") textYPos = cellY - cellHeight + padding.bottom;
6190
+ else textYPos = cellY - padding.top - cellFontSize;
6191
+ ops += saveState();
6192
+ ops += beginText();
6193
+ ops += moveText(textXPos, textYPos);
6194
+ for (const run of content) {
6195
+ ops += applyFillColor(run.color ?? cellTextColor);
6196
+ ops += setFont(run.fontName ?? fontName, run.fontSize ?? cellFontSize);
6197
+ ops += showText(run.text);
6198
+ }
6199
+ ops += endText();
6200
+ ops += restoreState();
6201
+ }
6202
+ } else {
6203
+ const text = typeof content === "string" ? content : "";
6204
+ if (text) ops += renderPlainTextOps(cell, text, cellX, cellY, cellWidth, cellHeight, fontName, fontSize, padding, textColor, colDef);
6205
+ }
6206
+ return ops;
6207
+ }
6208
+ function renderTable(options) {
6209
+ const fontSize = options.fontSize ?? 12;
6210
+ const defaultPadding = options.padding ?? 4;
6211
+ const borderWidth = options.borderWidth ?? .5;
6212
+ const borderColor = options.borderColor ?? {
6213
+ type: "grayscale",
6214
+ gray: 0
6215
+ };
6216
+ const textColor = options.textColor ?? {
6217
+ type: "grayscale",
6218
+ gray: 0
6219
+ };
6220
+ const fontName = options.fontName ?? "Helvetica";
6221
+ const headerRows = options.headerRows ?? 0;
6222
+ const colWidths = resolveColumnWidths(options);
6223
+ const numCols = colWidths.length;
6224
+ const rowHeights = resolveRowHeights(options, fontSize, defaultPadding, colWidths);
6225
+ const grid = buildOccupationGrid(options.rows, numCols);
6226
+ let ops = saveState();
6227
+ let currentY = options.y;
6228
+ for (let rowIdx = 0; rowIdx < options.rows.length; rowIdx++) {
6229
+ const row = options.rows[rowIdx];
6230
+ const rowHeight = rowHeights[rowIdx];
6231
+ const isHeader = rowIdx < headerRows;
6232
+ let effectiveRowBg = row.backgroundColor;
6233
+ if (!effectiveRowBg) {
6234
+ if (isHeader && options.headerBackgroundColor) effectiveRowBg = options.headerBackgroundColor;
6235
+ else if (!isHeader && options.alternateRowColors) {
6236
+ const dataRowIdx = rowIdx - headerRows;
6237
+ effectiveRowBg = options.alternateRowColors[dataRowIdx % 2 === 0 ? 0 : 1];
6238
+ }
6239
+ }
6240
+ if (effectiveRowBg) {
6241
+ ops += saveState();
6242
+ ops += applyFillColor(effectiveRowBg);
6243
+ ops += rectangle(options.x, currentY - rowHeight, options.width, rowHeight);
6244
+ ops += fill();
6245
+ ops += restoreState();
6246
+ }
6247
+ const rowTextColor = isHeader && options.headerTextColor ? options.headerTextColor : textColor;
6248
+ for (let colIdx = 0; colIdx < numCols; colIdx++) {
6249
+ const entry = grid[rowIdx][colIdx];
6250
+ if (!entry) continue;
6251
+ if (entry.originRow !== rowIdx || entry.originCol !== colIdx) continue;
6252
+ const cell = entry.cell;
6253
+ const cs = Math.max(1, cell.colSpan ?? 1);
6254
+ const rs = Math.max(1, cell.rowSpan ?? 1);
6255
+ const cellX = columnX(options.x, colWidths, colIdx);
6256
+ const cellWidth = spanWidth(colWidths, colIdx, cs);
6257
+ const cellHeight = spanHeight(rowHeights, rowIdx, rs);
6258
+ const colDef = options.columns?.[colIdx];
6259
+ ops += renderCellOps(cell, cellX, currentY, cellWidth, cellHeight, fontName, fontSize, defaultPadding, borderWidth, borderColor, rowTextColor, colDef);
6260
+ }
6261
+ currentY -= rowHeight;
6262
+ }
6263
+ ops += restoreState();
6264
+ const totalHeight = options.y - currentY;
6265
+ return {
6266
+ ops,
6267
+ result: {
6268
+ width: options.width,
6269
+ height: totalHeight,
6270
+ rowHeights,
6271
+ columnWidths: colWidths,
6272
+ pagesUsed: 1
6273
+ }
6274
+ };
6275
+ }
6276
+ /**
6277
+ * Render a table across multiple pages, breaking rows when content
6278
+ * would exceed the available vertical space.
6279
+ *
6280
+ * Header rows (specified by `options.headerRows`) are repeated at the
6281
+ * top of each new page. Rows are never split across pages.
6282
+ */
6283
+ function renderMultiPageTable(options, bottomMargin = 40) {
6284
+ const fontSize = options.fontSize ?? 12;
6285
+ const defaultPadding = options.padding ?? 4;
6286
+ const borderWidth = options.borderWidth ?? .5;
6287
+ const borderColor = options.borderColor ?? {
6288
+ type: "grayscale",
6289
+ gray: 0
6290
+ };
6291
+ const textColor = options.textColor ?? {
6292
+ type: "grayscale",
6293
+ gray: 0
6294
+ };
6295
+ const fontName = options.fontName ?? "Helvetica";
6296
+ const headerRowCount = options.headerRows ?? 0;
6297
+ const colWidths = resolveColumnWidths(options);
6298
+ const numCols = colWidths.length;
6299
+ const rowHeights = resolveRowHeights(options, fontSize, defaultPadding, colWidths);
6300
+ const grid = buildOccupationGrid(options.rows, numCols);
6301
+ const pages = [];
6302
+ let currentPageOps = saveState();
6303
+ let currentY = options.y;
6304
+ let totalHeight = 0;
6305
+ function renderHeaders() {
6306
+ for (let hIdx = 0; hIdx < headerRowCount && hIdx < options.rows.length; hIdx++) {
6307
+ const hRow = options.rows[hIdx];
6308
+ const hRowHeight = rowHeights[hIdx];
6309
+ if (hRow.backgroundColor) {
6310
+ currentPageOps += saveState();
6311
+ currentPageOps += applyFillColor(hRow.backgroundColor);
6312
+ currentPageOps += rectangle(options.x, currentY - hRowHeight, options.width, hRowHeight);
6313
+ currentPageOps += fill();
6314
+ currentPageOps += restoreState();
6315
+ }
6316
+ for (let colIdx = 0; colIdx < numCols; colIdx++) {
6317
+ const entry = grid[hIdx][colIdx];
6318
+ if (!entry) continue;
6319
+ if (entry.originRow !== hIdx || entry.originCol !== colIdx) continue;
6320
+ const cell = entry.cell;
6321
+ const cs = Math.max(1, cell.colSpan ?? 1);
6322
+ const rs = Math.min(Math.max(1, cell.rowSpan ?? 1), headerRowCount - hIdx);
6323
+ const cellX = columnX(options.x, colWidths, colIdx);
6324
+ const cellWidth = spanWidth(colWidths, colIdx, cs);
6325
+ const cellHeight = spanHeight(rowHeights, hIdx, rs);
6326
+ const colDef = options.columns?.[colIdx];
6327
+ currentPageOps += renderCellOps(cell, cellX, currentY, cellWidth, cellHeight, fontName, fontSize, defaultPadding, borderWidth, borderColor, textColor, colDef);
6328
+ }
6329
+ currentY -= hRowHeight;
6330
+ }
6331
+ }
6332
+ function startNewPage() {
6333
+ currentPageOps += restoreState();
6334
+ pages.push({
6335
+ ops: currentPageOps,
6336
+ pageIndex: pages.length
6337
+ });
6338
+ currentPageOps = saveState();
6339
+ currentY = options.y;
6340
+ renderHeaders();
6341
+ }
6342
+ for (let rowIdx = 0; rowIdx < options.rows.length; rowIdx++) {
6343
+ const row = options.rows[rowIdx];
6344
+ const rowHeight = rowHeights[rowIdx];
6345
+ if (currentY - rowHeight < bottomMargin && rowIdx > 0) {
6346
+ startNewPage();
6347
+ if (rowIdx < headerRowCount) continue;
6348
+ }
6349
+ if (row.backgroundColor) {
6350
+ currentPageOps += saveState();
6351
+ currentPageOps += applyFillColor(row.backgroundColor);
6352
+ currentPageOps += rectangle(options.x, currentY - rowHeight, options.width, rowHeight);
6353
+ currentPageOps += fill();
6354
+ currentPageOps += restoreState();
6355
+ }
6356
+ for (let colIdx = 0; colIdx < numCols; colIdx++) {
6357
+ const entry = grid[rowIdx][colIdx];
6358
+ if (!entry) continue;
6359
+ if (entry.originRow !== rowIdx || entry.originCol !== colIdx) continue;
6360
+ const cell = entry.cell;
6361
+ const cs = Math.max(1, cell.colSpan ?? 1);
6362
+ const rs = Math.max(1, cell.rowSpan ?? 1);
6363
+ const cellX = columnX(options.x, colWidths, colIdx);
6364
+ const cellWidth = spanWidth(colWidths, colIdx, cs);
6365
+ const cellHeight = spanHeight(rowHeights, rowIdx, rs);
6366
+ const colDef = options.columns?.[colIdx];
6367
+ currentPageOps += renderCellOps(cell, cellX, currentY, cellWidth, cellHeight, fontName, fontSize, defaultPadding, borderWidth, borderColor, textColor, colDef);
6368
+ }
6369
+ currentY -= rowHeight;
6370
+ totalHeight += rowHeight;
6371
+ }
6372
+ currentPageOps += restoreState();
6373
+ pages.push({
6374
+ ops: currentPageOps,
6375
+ pageIndex: pages.length
6376
+ });
6377
+ return {
6378
+ pages,
6379
+ result: {
6380
+ width: options.width,
6381
+ height: totalHeight,
6382
+ rowHeights,
6383
+ columnWidths: colWidths,
6384
+ pagesUsed: pages.length
6385
+ }
6386
+ };
6387
+ }
6388
+
3421
6389
  //#endregion
3422
6390
  //#region src/core/pdfPageText.ts
3423
6391
  /**
@@ -4655,6 +7623,58 @@ var PdfPage = class PdfPage {
4655
7623
  this.ops += restoreState();
4656
7624
  }
4657
7625
  /**
7626
+ * Draw a QR code at the specified position.
7627
+ *
7628
+ * The QR code is rendered as native PDF vector graphics (filled
7629
+ * rectangles for each dark module), producing resolution-independent
7630
+ * output.
7631
+ *
7632
+ * @param data The string to encode in the QR code.
7633
+ * @param options Position, error correction level, colours, module size.
7634
+ */
7635
+ drawQrCode(data, options = {}) {
7636
+ const x = options.x ?? this._cursorX;
7637
+ const y = options.y ?? this._cursorY;
7638
+ const ops = qrCodeToOperators(encodeQrCode(data, options.errorCorrection ?? "M"), x, y, {
7639
+ ...options.moduleSize !== void 0 && { moduleSize: options.moduleSize },
7640
+ ...options.quietZone !== void 0 && { quietZone: options.quietZone },
7641
+ ...options.color !== void 0 && { color: options.color },
7642
+ ...options.backgroundColor !== void 0 && { backgroundColor: options.backgroundColor }
7643
+ });
7644
+ this.ops += ops;
7645
+ }
7646
+ /**
7647
+ * Draw a table on this page.
7648
+ *
7649
+ * The table is rendered as native PDF vector graphics (rectangles,
7650
+ * borders, and text operators). The top-left corner of the table is
7651
+ * at `(options.x, options.y)` and rows extend downward.
7652
+ *
7653
+ * @param options Table configuration: position, size, rows, columns,
7654
+ * fonts, colours, borders, and padding.
7655
+ * @returns A {@link TableRenderResult} with the computed
7656
+ * dimensions and layout metrics.
7657
+ *
7658
+ * @example
7659
+ * ```ts
7660
+ * const result = page.drawTable({
7661
+ * x: 50,
7662
+ * y: 700,
7663
+ * width: 500,
7664
+ * rows: [
7665
+ * { cells: ['Name', 'Age', 'City'] },
7666
+ * { cells: ['Alice', '30', 'London'] },
7667
+ * { cells: ['Bob', '25', 'Paris'] },
7668
+ * ],
7669
+ * });
7670
+ * ```
7671
+ */
7672
+ drawTable(options) {
7673
+ const { ops, result } = renderTable(options);
7674
+ this.ops += ops;
7675
+ return result;
7676
+ }
7677
+ /**
4658
7678
  * Draw a gradient fill (linear or radial) clipped to a rectangle.
4659
7679
  *
4660
7680
  * The gradient is registered as a `/Pattern` resource on this page
@@ -4991,5 +8011,5 @@ var PdfPage = class PdfPage {
4991
8011
  };
4992
8012
 
4993
8013
  //#endregion
4994
- export { colorToComponents as $, setTextMatrix as $t, beginMarkedContent as A, fill as At, radians as B, setLineJoin as Bt, AnnotationFlags as C, closeFillEvenOddAndStroke as Ct, createAnnotation as D, curveToInitial as Dt, buildAnnotationDict as E, curveToFinal as Et, endMarkedContent as F, moveTo as Ft, saveState as G, endText as Gt, restoreState as H, setMiterLimit as Ht, wrapInMarkedContent as I, rectangle as It, skew as J, nextLine as Jt, scale as K, moveText as Kt, concatMatrix as L, setDashPattern as Lt, beginMarkedContentWithProperties as M, fillEvenOdd as Mt, createMarkedContentScope as N, fillEvenOddAndStroke as Nt, beginArtifact as O, ellipsePath as Ot, endArtifact as P, lineTo as Pt, cmyk as Q, setLeading as Qt, degrees as R, setFlatness as Rt, parseSvgTransform as S, closeFillAndStroke as St, annotationFromDict as T, curveTo as Tt, rotate as U, stroke as Ut, radiansToDegrees as V, setLineWidth as Vt, rotationMatrix as W, beginText as Wt, applyFillColor as X, setFont as Xt, translate as Y, setCharacterSpacing as Yt, applyStrokeColor as Z, setFontSize as Zt, drawSvgOnPage as _, drawXObject as _t, buildGradientObjects as a, showTextHex as an, setFillColorCmyk as at, parseSvgColor as b, clipEvenOdd as bt, radialGradient as c, setFillingColor as ct, getRedactionMarks as d, setStrokeColorGray as dt, setTextRenderingMode as en, componentsToColor as et, markForRedaction as f, setStrokeColorRgb as ft, endLayerContent as g, drawImageXObject as gt, beginLayerContent as h, drawImageWithMatrix as ht, wrapText as i, showTextArray as in, setFillColor as it, beginMarkedContentSequence as j, fillAndStroke as jt, beginArtifactWithType as k, endPath as kt, tilingPattern as l, setStrokeColor as lt, PdfLayerManager as m, setStrokingColor as mt, PdfPage as n, setWordSpacing as nn, rgb as nt, buildPatternObjects as o, showTextNextLine as on, setFillColorGray as ot, PdfLayer as p, setStrokeColorSpace as pt, setGraphicsState as q, moveTextSetLeading as qt, pdfPage_exports as r, showText as rn, setColorSpace as rt, linearGradient as s, showTextWithSpacing as sn, setFillColorRgb as st, PageSizes as t, setTextRise as tn, grayscale as tt, applyRedactions as u, setStrokeColorCmyk as ut, svgToPdfOperators as v, circlePath as vt, PdfAnnotation as w, closePath as wt, parseSvgPath as x, closeAndStroke as xt, parseSvg as y, clip as yt, degreesToRadians as z, setLineCap as zt };
4995
- //# sourceMappingURL=pdfPage-N1K2U3jI.mjs.map
8014
+ export { translate as $, setCharacterSpacing as $t, buildAnnotationDict as A, curveToFinal as At, wrapInMarkedContent as B, rectangle as Bt, parseSvg as C, clip as Ct, AnnotationFlags as D, closeFillEvenOddAndStroke as Dt, parseSvgTransform as E, closeFillAndStroke as Et, beginMarkedContentSequence as F, fillAndStroke as Ft, radiansToDegrees as G, setLineWidth as Gt, degrees as H, setFlatness as Ht, beginMarkedContentWithProperties as I, fillEvenOdd as It, rotationMatrix as J, beginText as Jt, restoreState as K, setMiterLimit as Kt, createMarkedContentScope as L, fillEvenOddAndStroke as Lt, beginArtifact as M, ellipsePath as Mt, beginArtifactWithType as N, endPath as Nt, PdfAnnotation as O, closePath as Ot, beginMarkedContent as P, fill as Pt, skew as Q, nextLine as Qt, endArtifact as R, lineTo as Rt, svgToPdfOperators as S, circlePath as St, parseSvgPath as T, closeAndStroke as Tt, degreesToRadians as U, setLineCap as Ut, concatMatrix as V, setDashPattern as Vt, radians as W, setLineJoin as Wt, scale as X, moveText as Xt, saveState as Y, endText as Yt, setGraphicsState as Z, moveTextSetLeading as Zt, PdfLayer as _, setStrokeColorSpace as _t, renderMultiPageTable as a, setTextRise as an, grayscale as at, endLayerContent as b, drawImageXObject as bt, qrCodeToOperators as c, showTextArray as cn, setFillColor as ct, linearGradient as d, showTextWithSpacing as dn, setFillColorRgb as dt, setFont as en, applyFillColor as et, radialGradient as f, setFillingColor as ft, markForRedaction as g, setStrokeColorRgb as gt, getRedactionMarks as h, setStrokeColorGray as ht, wrapText as i, setTextRenderingMode as in, componentsToColor as it, createAnnotation as j, curveToInitial as jt, annotationFromDict as k, curveTo as kt, buildGradientObjects as l, showTextHex as ln, setFillColorCmyk as lt, applyRedactions as m, setStrokeColorCmyk as mt, PdfPage as n, setLeading as nn, cmyk as nt, renderTable as o, setWordSpacing as on, rgb as ot, tilingPattern as p, setStrokeColor as pt, rotate as q, stroke as qt, pdfPage_exports as r, setTextMatrix as rn, colorToComponents as rt, encodeQrCode as s, showText as sn, setColorSpace as st, PageSizes as t, setFontSize as tn, applyStrokeColor as tt, buildPatternObjects as u, showTextNextLine as un, setFillColorGray as ut, PdfLayerManager as v, setStrokingColor as vt, parseSvgColor as w, clipEvenOdd as wt, drawSvgOnPage as x, drawXObject as xt, beginLayerContent as y, drawImageWithMatrix as yt, endMarkedContent as z, moveTo as zt };
8015
+ //# sourceMappingURL=pdfPage-BacMkrLe.mjs.map