gamba 2.0.2 → 2.2.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +47 -1
- data/README.md +174 -31
- data/lib/gamba/annotations.rb +5 -0
- data/lib/gamba/barcodes/data_matrix.rb +204 -0
- data/lib/gamba/barcodes/linear.rb +182 -0
- data/lib/gamba/barcodes/qr_code.rb +339 -0
- data/lib/gamba/barcodes.rb +136 -0
- data/lib/gamba/combine.rb +13 -135
- data/lib/gamba/copier.rb +107 -0
- data/lib/gamba/document/internals.rb +5 -1
- data/lib/gamba/document.rb +36 -4
- data/lib/gamba/font.rb +7 -3
- data/lib/gamba/fonts/afm.rb +3 -2
- data/lib/gamba/fonts/ttf.rb +26 -20
- data/lib/gamba/images.rb +23 -1
- data/lib/gamba/imported_page.rb +137 -0
- data/lib/gamba/invoice/cross_industry_invoice.rb +255 -0
- data/lib/gamba/invoice/rules.rb +183 -0
- data/lib/gamba/invoice.rb +272 -0
- data/lib/gamba/minitest.rb +35 -0
- data/lib/gamba/native/band.rb +56 -0
- data/lib/gamba/native/document.rb +128 -0
- data/lib/gamba/native/layout.rb +1063 -0
- data/lib/gamba/native/table.rb +189 -0
- data/lib/gamba/page_boxes.rb +68 -0
- data/lib/gamba/pdf/decryption.rb +1 -20
- data/lib/gamba/pdf/reader.rb +8 -2
- data/lib/gamba/pdf/writer.rb +34 -5
- data/lib/gamba/pdf.rb +15 -1
- data/lib/gamba/pdf_a.rb +65 -21
- data/lib/gamba/pdf_check.rb +220 -0
- data/lib/gamba/railtie.rb +52 -0
- data/lib/gamba/rspec.rb +58 -0
- data/lib/gamba/security.rb +133 -5
- data/lib/gamba/table/cell/kinds.rb +45 -5
- data/lib/gamba/table/cell.rb +37 -0
- data/lib/gamba/table.rb +7 -0
- data/lib/gamba/tagging.rb +302 -0
- data/lib/gamba/testing.rb +79 -0
- data/lib/gamba/text/formatted/box.rb +5 -0
- data/lib/gamba/text/formatted/wrap.rb +1 -1
- data/lib/gamba/true_type/open_type_layout.rb +44 -17
- data/lib/gamba/true_type/simple_subsets.rb +11 -7
- data/lib/gamba/version.rb +1 -1
- data/lib/gamba/watermark.rb +108 -0
- data/lib/gamba.rb +22 -1
- metadata +294 -260
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6655a4d975cae61ca400cdac17500d7d448dc2afafa293a71391f2ed06d2c019
|
|
4
|
+
data.tar.gz: 0c5758306e6ad760436ab97a73294fb8221690d542a536d2bd954a12399bd35a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ab094dc3ea9b9dc2c46741166a87ed188711b15d4c92249c6e61956ef6e7d411205acdd7c2fc52d324984db2c385be482a772d343a567e7bea0138317f19a9d1
|
|
7
|
+
data.tar.gz: cc4254a7747560aa6e36fa213122afb8bed94bbce2d7d5d478d689ccaab4e3cfdcad97c066fab2af70a7afcf072181410d3110175d6fd3a7de0e242b03483d38
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,50 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [2.2.0] — 2026-11-27
|
|
6
|
+
|
|
7
|
+
Encrypted documents open in Preview and Safari, and can use AES-256; a watermark takes one option; invoices write their own Factur-X XML.
|
|
8
|
+
|
|
9
|
+
**New**
|
|
10
|
+
- `watermark: "Draft"`, or a hash with its `text`, `font`, `style`, `size`, `angle`, `color` and `opacity`, draws a text along each page's diagonal, beneath its content, on `Prawn::Document` and `Gamba.document`. It is drawn when the document renders, so a font registered later is used, written once per page size, and tagged as a watermark artifact.
|
|
11
|
+
- `encrypt_document(algorithm: :aes_256)` writes the standard security handler's revision 6 in place of 40-bit RC4, which stays the default. Its key, salts and initialization vectors are random, so two renders differ; modification and annotations refused also refuse assembly and form filling, as under RC4.
|
|
12
|
+
- `Gamba::Invoice` holds an invoice as data with exact amounts, computes its totals and VAT breakdown as EN 16931 sets, and writes its EN 16931 XML; `factur_x invoice` embeds it.
|
|
13
|
+
- Writing an invoice's XML checks the EN 16931 business rules its data can break, and with `rules: :fr` the French e-invoicing rules, raising `Gamba::Invoice::RuleViolation` with every broken rule by its id.
|
|
14
|
+
- Released gems carry a Sigstore attestation.
|
|
15
|
+
- The site publishes `llms.txt` and `llms-full.txt`, the guide and API for coding agents.
|
|
16
|
+
|
|
17
|
+
**Fixed**
|
|
18
|
+
- Encrypted documents open in Apple's PDF engine (Preview, Safari on macOS and iOS, Quick Look): their catalog is no longer packed in the object stream, which Core Graphics cannot read once encrypted.
|
|
19
|
+
- Strings in the dictionary of a stream, such as an imported image's indexed palette, are encrypted with the rest of an encrypted document, where they read back as garbage.
|
|
20
|
+
- A date in a time zone with minutes, such as +05:30, keeps them, where Prawn writes +05'00'.
|
|
21
|
+
|
|
22
|
+
## [2.1.0] — 2026-11-21
|
|
23
|
+
|
|
24
|
+
Gamba gets an API of its own next to Prawn's, `Gamba.document`: layout described in boxes instead of computed, tables with named columns, barcodes, PDF templates, Rails rendering, test assertions and tagged PDF/UA.
|
|
25
|
+
|
|
26
|
+
**Breaking**
|
|
27
|
+
- `pdf_a:` accepts only `true`, `false`, `nil`, `:a` and `:b`, and raises `ArgumentError` on anything else instead of ignoring it.
|
|
28
|
+
|
|
29
|
+
**New**
|
|
30
|
+
- `Gamba.document` builds a document with an explicit receiver (no `instance_eval`), named styles, and options checked against typos; `gamba { |document| ... }` opens it inside existing Prawn code, and `document.prawn` reaches Prawn's API from it.
|
|
31
|
+
- Layout in boxes: `stack`, `row` and `grid(columns:)`, with `gap`, `padding`, `background` and borders; pages break between children and inside paragraphs and tables, decorated stacks included, which draw their frame on each page.
|
|
32
|
+
- Typesetter page breaks: `widows` and `orphans` (2 lines each by default) and `keep_with_next`, usable in named styles; `page_break` starts a new page.
|
|
33
|
+
- `header` and `footer` built for each page with its `number` and the real `total`, their room taken from the page.
|
|
34
|
+
- Native tables with named columns: rows as hashes or objects, per-column `title`, `width`, `align` and font, a header repeated on every page, `stripes`, and cells measured in the font they draw in.
|
|
35
|
+
- `image`, `table`, `pdf_page`, `qr_code`, `barcode` and `data_matrix` are boxes of the layout, with `align`.
|
|
36
|
+
- `pdf_page` draws a page of an existing PDF like an image, and `background_pdf` puts a letterhead under every page (also `start_new_page(background_pdf:)`); each page is written once.
|
|
37
|
+
- Vector barcodes in pure Ruby: QR codes (versions 1 to 40, all levels), Code 128, EAN-13, EAN-8, Code 39, ITF-14 with check digits, and DataMatrix ECC 200.
|
|
38
|
+
- Tagged PDF: `pdf: :ua` writes PDF/UA-1 and `pdf_a: :a` PDF/A-3a from the boxes' `role:` (headings, paragraphs, lists, figures with `alt:`, tables) and `lang:`, headers, footers and decoration as artifacts; what breaks the standard raises `PdfUaViolation` or `PdfAViolation`. Headings also make the PDF's bookmarks, tagged or not.
|
|
39
|
+
- Rails: `render pdf:` in controllers, `.pdf.rb` templates drawing on `document` with the view's helpers, `Gamba.eager_load!` before Rails eager loads, and Active Storage attachments read by `image` and `pdf_page` without a temporary file. No Rails dependency.
|
|
40
|
+
- Test helpers: `require "gamba/minitest"` (`assert_pdf_text`, `assert_same_pages`, `assert_valid_pdf`, `assert_pdf_a`) and `require "gamba/rspec"` (`have_pdf_text`, `have_same_pages_as`, `be_a_valid_pdf`, `be_pdf_a`), with failures that say what the page reads instead.
|
|
41
|
+
- Unknown faces in Gamba's own API raise an `ArgumentError` saying what to name instead, such as the family `Courier` with `weight: :bold` rather than `Courier-Bold`.
|
|
42
|
+
|
|
43
|
+
**Faster**
|
|
44
|
+
- Text in a font loaded with `features: %i[liga kern]` is no longer about three times slower to render than without them: shaping builds nothing per glyph and reads each kerned pair once per document, for the same bytes.
|
|
45
|
+
|
|
46
|
+
**Fixed**
|
|
47
|
+
- `Gamba.combine` gives a copied page without resources an empty dictionary, as ISO 32000-1 requires; qpdf 12.4 warned about such pages.
|
|
48
|
+
|
|
5
49
|
## [2.0.2] — 2026-11-07
|
|
6
50
|
|
|
7
51
|
SVGs that users send can no longer crash or stall a server through `<use>`, `<image>` or `<tref>`.
|
|
@@ -104,7 +148,9 @@ speed. Same API and same pages as Prawn, pixel for pixel, in lighter files.
|
|
|
104
148
|
place of combine_pdf.
|
|
105
149
|
- `Gamba::VERSION` is the gem's; `Prawn::VERSION` stays 2.5.0, the API reproduced.
|
|
106
150
|
|
|
107
|
-
[Unreleased]: https://github.com/levups/gamba/compare/v2.0
|
|
151
|
+
[Unreleased]: https://github.com/levups/gamba/compare/v2.2.0...HEAD
|
|
152
|
+
[2.2.0]: https://github.com/levups/gamba/compare/v2.1.0...v2.2.0
|
|
153
|
+
[2.1.0]: https://github.com/levups/gamba/compare/v2.0.2...v2.1.0
|
|
108
154
|
[2.0.2]: https://github.com/levups/gamba/compare/v2.0.1...v2.0.2
|
|
109
155
|
[2.0.1]: https://github.com/levups/gamba/compare/v2.0.0...v2.0.1
|
|
110
156
|
[2.0.0]: https://github.com/levups/gamba/compare/v1.1.0...v2.0.0
|
data/README.md
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
# Gamba
|
|
2
2
|
|
|
3
3
|
Gamba is a Ruby library that writes PDF files: invoices, reports, labels, anything you can lay out as text, tables,
|
|
4
|
-
images and shapes.
|
|
5
|
-
|
|
4
|
+
images and shapes. You describe the layout, stacks, rows, tables, headers and footers, and Gamba measures it, places
|
|
5
|
+
it, breaks pages where a reader expects them, and tags the result for screen readers.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
[Performance](#performance)).
|
|
7
|
+
It also runs code written for [Prawn](https://github.com/prawnpdf/prawn) 2.5, the most widely used PDF library for
|
|
8
|
+
Ruby, and for its companions prawn-table and prawn-svg, unchanged and down to the pixel: a one-page invoice renders
|
|
9
|
+
about four times faster and the file is a quarter lighter (see [Performance](#performance)). An application moves
|
|
10
|
+
from one API to the other one block at a time ([the migration guide](https://levups.github.io/gamba/migration.html)).
|
|
10
11
|
|
|
11
12
|
Website and guide: https://levups.github.io/gamba/
|
|
12
13
|
|
|
14
|
+
Coding agents can start from <https://levups.github.io/gamba/llms.txt>, or read this README and the
|
|
15
|
+
website's pages as one Markdown file: <https://levups.github.io/gamba/llms-full.txt>.
|
|
16
|
+
|
|
13
17
|
## Installation
|
|
14
18
|
|
|
15
19
|
Gamba needs Ruby 3.3 or later. In your Gemfile, replace `prawn`, `prawn-table` and `prawn-svg` with one line:
|
|
@@ -24,6 +28,19 @@ without them.
|
|
|
24
28
|
|
|
25
29
|
## A first document
|
|
26
30
|
|
|
31
|
+
```ruby
|
|
32
|
+
require "gamba"
|
|
33
|
+
|
|
34
|
+
Gamba.document do |document|
|
|
35
|
+
document.footer { |footer, page| footer.text "Page #{page.number} of #{page.total}" }
|
|
36
|
+
document.text "Hello, World!", role: :h1
|
|
37
|
+
document.table([{sku: "A-1", quantity: 2}], columns: {sku: {title: "SKU"}, quantity: {title: "Qty"}})
|
|
38
|
+
end.write("hello.pdf")
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
This writes `hello.pdf` with a heading, which also makes the PDF's first bookmark, a two-row table and a footer
|
|
42
|
+
numbering the pages. The same document with Prawn's API, which Gamba runs as it is:
|
|
43
|
+
|
|
27
44
|
```ruby
|
|
28
45
|
require "prawn"
|
|
29
46
|
|
|
@@ -33,9 +50,9 @@ Prawn::Document.generate("hello.pdf") do
|
|
|
33
50
|
end
|
|
34
51
|
```
|
|
35
52
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
53
|
+
Everything else Prawn documents works the same way: fonts, colors, images, columns, page numbers, links,
|
|
54
|
+
encryption. Prawn's manual applies as it is, and [the guide](https://levups.github.io/gamba/guide.html) covers what
|
|
55
|
+
Gamba adds.
|
|
39
56
|
|
|
40
57
|
The constant is still `Prawn`, so you don't have to rename anything. `Prawn::VERSION` reports `2.5.0`, the
|
|
41
58
|
version of the API Gamba reproduces, for code that checks it. Gamba's own version is `Gamba::VERSION`.
|
|
@@ -44,6 +61,49 @@ version of the API Gamba reproduces, for code that checks it. Gamba's own versio
|
|
|
44
61
|
|
|
45
62
|
Anything that would change how a page looks compared with Prawn is off until you ask for it.
|
|
46
63
|
|
|
64
|
+
An API of its own, next to Prawn's. The block receives the document instead of running inside it, options are
|
|
65
|
+
keywords, and a mistyped one raises instead of being ignored:
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
document = Gamba.document(page_size: "A4", lang: "en") do |document|
|
|
69
|
+
document.style :title, size: 18, weight: :bold
|
|
70
|
+
document.text "Invoice INV-2026-0001", style: :title
|
|
71
|
+
document.draw InvoiceLines.new(invoice) # any object with call(document)
|
|
72
|
+
end
|
|
73
|
+
document.write("invoice.pdf")
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Layout is described instead of computed: a `stack` puts boxes one under the other and breaks pages between them, a
|
|
77
|
+
`row` puts them side by side, and each is measured before anything is drawn.
|
|
78
|
+
|
|
79
|
+
```ruby
|
|
80
|
+
document.row(gap: 24, padding: 12, background: "f5f5f5") do |row|
|
|
81
|
+
row.stack(gap: 4) { |stack| stack.text "Alice Martin", style: :title; stack.text "1 Example Street" }
|
|
82
|
+
row.text "Due on 1 December 2026", width: 150, align: :right
|
|
83
|
+
end
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Code written for Prawn adopts it one block at a time, `gamba { |document| document.text "Total", style: :title }`,
|
|
87
|
+
and `document.prawn` gives a native document all of Prawn's API. A header and a footer are declared once, and know
|
|
88
|
+
their page:
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
document.footer(gap: 12) { |footer, page| footer.text "Page #{page.number} of #{page.total}", align: :center }
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Tables have named columns, and rows that are hashes or objects, so nothing is counted by position:
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
document.table(lines, columns: {sku: {title: "SKU", width: 60}, description: {}, total: {align: :right, weight: :bold}})
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Pages break where a reader expects them: a paragraph leaves no single line at the bottom or the top of a page
|
|
101
|
+
(`widows` and `orphans`, 2 unless told), and a heading with `keep_with_next: true` moves with what follows it.
|
|
102
|
+
|
|
103
|
+
Text, styles, components, stacks, rows, grids, tables, images, codes, headers, footers, these page breaks and
|
|
104
|
+
semantic roles are there today; forms, signatures and complex scripts come next
|
|
105
|
+
([the plan](https://github.com/levups/gamba/issues/69)).
|
|
106
|
+
|
|
47
107
|
Ligatures and kerning from OpenType fonts, per font:
|
|
48
108
|
|
|
49
109
|
```ruby
|
|
@@ -51,13 +111,38 @@ font "Inter.ttf", features: %i[liga kern]
|
|
|
51
111
|
```
|
|
52
112
|
|
|
53
113
|
PDF/A-3b archival files, and Factur-X electronic invoices (the French and German standard that embeds the invoice
|
|
54
|
-
data as XML inside the PDF). Gamba
|
|
114
|
+
data as XML inside the PDF). Gamba writes the XML of the EN 16931 profile from a `Gamba::Invoice`, which computes the
|
|
115
|
+
totals and the VAT breakdown with EN 16931's rounding, in exact decimals; or it embeds the XML your invoicing system
|
|
116
|
+
produced, in any profile.
|
|
55
117
|
|
|
56
118
|
```ruby
|
|
119
|
+
invoice = Gamba::Invoice.new(number: "INV-2026-0001", issued_on: Date.new(2026, 11, 12), due_on: Date.new(2026, 12, 12),
|
|
120
|
+
seller: {name: "Example SAS", vat: "FR32123456789", address: {lines: "1 Example Street", postcode: "75001", city: "Paris", country: "FR"}},
|
|
121
|
+
buyer: {name: "Alice Martin", address: {lines: "2 Example Avenue", postcode: "69001", city: "Lyon", country: "FR"}},
|
|
122
|
+
lines: [{name: "Post", quantity: 2, unit_price: "50.00", vat_rate: 20}])
|
|
123
|
+
invoice.due # => (120/1)
|
|
124
|
+
|
|
57
125
|
Prawn::Document.new(pdf_a: true, info: {Title: "Invoice INV-2026-0001"}) do
|
|
58
126
|
font "Inter.ttf" # PDF/A needs embedded fonts: the 14 standard ones raise, and so does encryption
|
|
59
127
|
# ...
|
|
60
|
-
factur_x File.read("factur-x.xml"), profile: :
|
|
128
|
+
factur_x invoice # or File.read("factur-x.xml"), profile: :basic (minimum, basic_wl, basic, en16931, extended)
|
|
129
|
+
end
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
The invoice is checked against the EN 16931 business rules first, and with `factur_x invoice, rules: :fr` against those
|
|
133
|
+
of France's e-invoicing reform too; `Gamba::Invoice::RuleViolation` names each rule broken, such as
|
|
134
|
+
`BR-CO-25: an invoice with an amount due needs due_on or payment_terms`.
|
|
135
|
+
|
|
136
|
+
Tagged PDF for screen readers: `pdf: :ua` (PDF/UA-1) and `pdf_a: :a` (PDF/A-3a) tag the boxes of Gamba's own API
|
|
137
|
+
from their roles, and raise where a rule is broken (a figure without `alt`, a list item outside a list; under `pdf: :ua`
|
|
138
|
+
also no `lang`, no title, a skipped heading level).
|
|
139
|
+
|
|
140
|
+
```ruby
|
|
141
|
+
Gamba.document(pdf: :ua, lang: "en", info: {Title: "Quarterly report"}) do |document|
|
|
142
|
+
document.register_font "Inter", normal: "Inter.ttf"
|
|
143
|
+
document.style :default, font: "Inter"
|
|
144
|
+
document.text "Quarterly report", role: :h1
|
|
145
|
+
document.image "chart.png", alt: "Sales by month, 2026"
|
|
61
146
|
end
|
|
62
147
|
```
|
|
63
148
|
|
|
@@ -68,8 +153,40 @@ combine_pdf gem for that use:
|
|
|
68
153
|
Gamba.combine([label, other_label], page_size: [288, 432], orientation: :portrait)
|
|
69
154
|
```
|
|
70
155
|
|
|
71
|
-
|
|
72
|
-
|
|
156
|
+
Drawing on a page of an existing PDF, such as a letterhead or a pre-printed form, under every page or anywhere at
|
|
157
|
+
any size. It is written once, however many pages use it:
|
|
158
|
+
|
|
159
|
+
```ruby
|
|
160
|
+
Prawn::Document.new(background_pdf: "letterhead.pdf") do
|
|
161
|
+
pdf_page "terms.pdf", page: 2, width: 200
|
|
162
|
+
end
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
A watermark across every page, beneath its content, along the page's diagonal unless told otherwise. It is written
|
|
166
|
+
once per page size, and tagged documents mark it as an artifact:
|
|
167
|
+
|
|
168
|
+
```ruby
|
|
169
|
+
Prawn::Document.new(watermark: {text: "Courtesy translation", opacity: 0.2})
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
QR codes, DataMatrix and barcodes (Code 128, EAN-13, EAN-8, Code 39, ITF-14) in pure Ruby, drawn as vectors: one
|
|
173
|
+
fill per code, read back by zbar and libdmtx in CI:
|
|
174
|
+
|
|
175
|
+
```ruby
|
|
176
|
+
qr_code "https://example.com/pay/INV-2026-0001", at: [400, 700], size: 80, level: :m
|
|
177
|
+
barcode "0123456789012", type: :ean13, width: 120, height: 40, text: true
|
|
178
|
+
data_matrix "alice@example.com", size: 40
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Rails, 8.0 and later, with nothing to configure: controllers render PDFs, `.pdf.rb` templates draw them with the
|
|
182
|
+
view's helpers and translations, and Gamba loads before the server forks when the application eager loads:
|
|
183
|
+
|
|
184
|
+
```ruby
|
|
185
|
+
render pdf: InvoiceDocument.new(@invoice), filename: "invoice.pdf" # anything answering to_pdf, or a document
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Outside Rails, loading font and SVG support once, before a server such as Puma or Unicorn forks its workers, so they
|
|
189
|
+
share it in memory:
|
|
73
190
|
|
|
74
191
|
```ruby
|
|
75
192
|
Gamba.eager_load!
|
|
@@ -105,16 +222,17 @@ Any of it can be built to attack the server that renders it.
|
|
|
105
222
|
one another, and they draw within the budget of the SVG that embeds them.
|
|
106
223
|
- Fonts an SVG declares with `@font-face` disappear once it is drawn, and one that is not a font is left out. In
|
|
107
224
|
prawn-svg, an uploaded SVG declaring a "Helvetica" font changed the font of the application's own text.
|
|
108
|
-
- A damaged or malicious PNG, JPEG, TrueType or
|
|
109
|
-
(`Prawn::Errors::UnsupportedImageType`, `Prawn::Images::JPG::FormatError`, `Gamba::TrueType::Error`,
|
|
110
|
-
`Prawn::Errors::CannotFit` when a damaged font makes a glyph wider than the
|
|
225
|
+
- A damaged or malicious PNG, JPEG, TrueType, OpenType or PDF file either renders or raises an error Gamba names
|
|
226
|
+
(`Prawn::Errors::UnsupportedImageType`, `Prawn::Images::JPG::FormatError`, `Gamba::TrueType::Error`,
|
|
227
|
+
`Prawn::Errors::MalformedPDF`, or `Prawn::Errors::CannotFit` when a damaged font makes a glyph wider than the
|
|
228
|
+
page). A PNG
|
|
111
229
|
inflates no further than the size its header gives. CI corrupts sample files on every run to check this.
|
|
112
|
-
- `Gamba.combine`
|
|
230
|
+
- `Gamba.combine` and `pdf_page` stop at 64 levels of nested arrays and dictionaries and at 256 MB of decompressed data, and
|
|
113
231
|
ignores references that lead back to themselves, so a crafted PDF can't send it into a loop or fill
|
|
114
232
|
memory.
|
|
115
233
|
- Text with inline formatting (`inline_format: true`) is parsed in linear time, whatever the markup.
|
|
116
|
-
- The `/ID` of an encrypted PDF
|
|
117
|
-
without them.
|
|
234
|
+
- The `/ID` of an encrypted PDF mixes in a secret, its passwords or a random one under AES-256, so it reveals
|
|
235
|
+
nothing about the content to someone without them.
|
|
118
236
|
|
|
119
237
|
Inline formatting still turns `<link href="...">` into a clickable link. Escape user text before you put it inside
|
|
120
238
|
markup. A word with no space to break at, thousands of characters long, takes time that grows with the square of its
|
|
@@ -124,15 +242,38 @@ To report a vulnerability, see [SECURITY.md](SECURITY.md).
|
|
|
124
242
|
|
|
125
243
|
## Checking your app's PDFs
|
|
126
244
|
|
|
245
|
+
Tests that check a PDF's bytes break on every harmless change. Gamba's own checks, those its oracle compares it with
|
|
246
|
+
Prawn by, come as assertions for Minitest and matchers for RSpec, loaded only on request. They need `mutool`, and
|
|
247
|
+
`pdftoppm` and `qpdf` for pages and validity; a missing tool fails with its install command, or skips the check
|
|
248
|
+
with `Gamba::Testing.missing_tools = :skip`.
|
|
249
|
+
|
|
250
|
+
```ruby
|
|
251
|
+
require "gamba/minitest" # in test_helper.rb
|
|
252
|
+
|
|
253
|
+
assert_pdf_text pdf, "Invoice INV-2026-0001", page: 1 # bytes or a path; a Regexp works too
|
|
254
|
+
assert_same_pages expected_pdf, pdf # text, positions and pixels in MuPDF and Poppler
|
|
255
|
+
assert_valid_pdf pdf # qpdf and Poppler
|
|
256
|
+
assert_pdf_a pdf # veraPDF
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
```ruby
|
|
260
|
+
require "gamba/rspec" # in spec_helper.rb
|
|
261
|
+
|
|
262
|
+
expect(pdf).to have_pdf_text("Invoice INV-2026-0001", page: 1)
|
|
263
|
+
expect(pdf).to have_same_pages_as(expected_pdf)
|
|
264
|
+
expect(pdf).to be_a_valid_pdf
|
|
265
|
+
```
|
|
266
|
+
|
|
127
267
|
Before switching, you can compare what your app produces under both libraries. `script/capture_pdfs.rb` saves
|
|
128
|
-
every PDF your test suite renders, named after the test. Run the suite once with Prawn and once with Gamba
|
|
268
|
+
every PDF your test suite renders, named after the test. Run the suite once with Prawn and once with Gamba, then
|
|
269
|
+
compare the two directories with the same checks:
|
|
129
270
|
|
|
130
271
|
```sh
|
|
131
272
|
GAMBA_CAPTURE_DIRECTORY=tmp/prawn RUBYOPT=-r/path/to/gamba/script/capture_pdfs.rb bin/rails test
|
|
273
|
+
GAMBA_CAPTURE_DIRECTORY=tmp/gamba RUBYOPT=-r/path/to/gamba/script/capture_pdfs.rb bin/rails test
|
|
274
|
+
ruby /path/to/gamba/script/capture_pdfs.rb tmp/prawn tmp/gamba
|
|
132
275
|
```
|
|
133
276
|
|
|
134
|
-
Then compare the two directories with `Oracle.pdf_differences`, from Gamba's test support.
|
|
135
|
-
|
|
136
277
|
## Performance
|
|
137
278
|
|
|
138
279
|
`bundle exec rake bench` renders each document in `bench/documents` with Prawn 2.5 and with Gamba, in 7 fresh
|
|
@@ -141,12 +282,14 @@ Ruby 4.0.9:
|
|
|
141
282
|
|
|
142
283
|
| Document | Prawn | Gamba | Allocations, Prawn → Gamba | Weight, Prawn → Gamba |
|
|
143
284
|
|---|---:|---:|---:|---:|
|
|
144
|
-
| Invoice, one page | 22.
|
|
145
|
-
| Report, 101 pages |
|
|
146
|
-
| Catalog, 1,000 table rows |
|
|
285
|
+
| Invoice, one page | 22.5 ms | 5.2 ms | 163,879 → 26,718 | 38,766 → 28,990 bytes |
|
|
286
|
+
| Report, 101 pages | 529.0 ms | 152.8 ms | 3,350,867 → 766,398 | 207,815 → 164,923 bytes |
|
|
287
|
+
| Catalog, 1,000 table rows | 545.7 ms | 242.2 ms | 3,979,956 → 1,717,915 | 147,731 → 134,085 bytes |
|
|
147
288
|
|
|
148
289
|
Files are lighter because Gamba compresses streams and packs objects together, which PDF 1.5 allows and Prawn
|
|
149
|
-
doesn't do.
|
|
290
|
+
doesn't do. The same documents written with Gamba's own API are measured untagged and tagged for PDF/UA: tagging
|
|
291
|
+
costs 8 to 21% more allocations and 8 to 65% in weight, the catalog's structure tree holding each of its cells.
|
|
292
|
+
Charts and peak memory are on the [benchmarks page](https://levups.github.io/gamba/benchmarks.html).
|
|
150
293
|
|
|
151
294
|
## How we know the pages are the same
|
|
152
295
|
|
|
@@ -162,8 +305,8 @@ its proof.
|
|
|
162
305
|
## Development
|
|
163
306
|
|
|
164
307
|
Prawn runs from its own bundle, `oracle/Gemfile`, so the oracle can compare the two. The tests need `mutool`,
|
|
165
|
-
`pdftoppm` and `
|
|
166
|
-
`rake corpus:fetch` downloads into `corpus/cache`. Without that cache, the comparison tests are skipped.
|
|
308
|
+
`pdftoppm`, `qpdf`, `zbarimg` and `dmtxread` (`brew install mupdf-tools poppler qpdf zbar dmtx-utils`), and Prawn's
|
|
309
|
+
manual and sample data, which `rake corpus:fetch` downloads into `corpus/cache`. Without that cache, the comparison tests are skipped.
|
|
167
310
|
|
|
168
311
|
```sh
|
|
169
312
|
bundle install && BUNDLE_GEMFILE=oracle/Gemfile bundle install
|
|
@@ -206,10 +349,10 @@ bin/release minor # or patch, major, or an exact version such as 1.2
|
|
|
206
349
|
```
|
|
207
350
|
|
|
208
351
|
`main` takes no direct push, so `bin/release` dates the notes and bumps the version on a `release/vX.Y.Z` branch,
|
|
209
|
-
opens a pull request that merges itself once CI is green, and waits for it
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
the `.gem`.
|
|
352
|
+
opens a pull request that merges itself once CI is green, and waits for it (`bin/release --wait X.Y.Z` if you
|
|
353
|
+
stopped it). The merge starts the Release workflow, which waits for a maintainer's approval, tags the merge commit
|
|
354
|
+
`vX.Y.Z`, publishes the gem to RubyGems through trusted publishing and creates the GitHub release with the notes
|
|
355
|
+
and the `.gem`.
|
|
213
356
|
|
|
214
357
|
## License
|
|
215
358
|
|
data/lib/gamba/annotations.rb
CHANGED
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module PDF::Core
|
|
4
4
|
module Annotations
|
|
5
|
+
# Tagged documents would need each annotation in the structure tree, which Gamba does not write yet.
|
|
5
6
|
def annotate(options)
|
|
7
|
+
if tagged?
|
|
8
|
+
raise tagging_violation, "#{tagging_standard} tags annotations and links, which Gamba does not do yet: leave them out"
|
|
9
|
+
end
|
|
10
|
+
|
|
6
11
|
options = options.merge(Type: :Annot)
|
|
7
12
|
options[:F] ||= Gamba::PdfA::PRINT_FLAG if pdf_a?
|
|
8
13
|
options[:Dest] = PDF::Core::LiteralString.new(options[:Dest]) if options[:Dest].is_a?(String)
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gamba
|
|
4
|
+
module Barcodes
|
|
5
|
+
# A square DataMatrix ECC 200 symbol (ISO/IEC 16022:2006) as a grid of modules: the smallest size that holds the
|
|
6
|
+
# data in ASCII encodation, digits two to a codeword, other bytes past 127 behind an upper shift.
|
|
7
|
+
class DataMatrix
|
|
8
|
+
# Square sizes: modules on a side, data and error correction codewords, data region side, interleaved blocks.
|
|
9
|
+
Size = Data.define(:modules, :data, :ecc, :region, :blocks)
|
|
10
|
+
SIZES = [
|
|
11
|
+
[10, 3, 5, 8, 1], [12, 5, 7, 10, 1], [14, 8, 10, 12, 1], [16, 12, 12, 14, 1], [18, 18, 14, 16, 1],
|
|
12
|
+
[20, 22, 18, 18, 1], [22, 30, 20, 20, 1], [24, 36, 24, 22, 1], [26, 44, 28, 24, 1], [32, 62, 36, 14, 1],
|
|
13
|
+
[36, 86, 42, 16, 1], [40, 114, 48, 18, 1], [44, 144, 56, 20, 1], [48, 174, 68, 22, 1], [52, 204, 84, 24, 2],
|
|
14
|
+
[64, 280, 112, 14, 2], [72, 368, 144, 16, 4], [80, 456, 192, 18, 4], [88, 576, 224, 20, 4],
|
|
15
|
+
[96, 696, 272, 22, 4], [104, 816, 336, 24, 6], [120, 1050, 408, 18, 6], [132, 1304, 496, 20, 8],
|
|
16
|
+
[144, 1558, 620, 22, 10]
|
|
17
|
+
].map { |values| Size.new(*values) }.freeze
|
|
18
|
+
# The light margin around the symbol, in modules (§7.7).
|
|
19
|
+
QUIET_ZONE = 1
|
|
20
|
+
PAD = 129
|
|
21
|
+
UPPER_SHIFT = 235
|
|
22
|
+
DIGIT_PAIR = 130
|
|
23
|
+
# GF(2^8) modulo x^8 + x^5 + x^3 + x^2 + 1, the field of ECC 200's Reed-Solomon codes.
|
|
24
|
+
EXPONENTS = Array.new(510).tap do |powers|
|
|
25
|
+
value = 1
|
|
26
|
+
255.times do |exponent|
|
|
27
|
+
powers[exponent] = powers[exponent + 255] = value
|
|
28
|
+
value <<= 1
|
|
29
|
+
value ^= 0x12D if value > 0xFF
|
|
30
|
+
end
|
|
31
|
+
end.freeze
|
|
32
|
+
LOGARITHMS = Array.new(256).tap { |logarithms| 255.times { logarithms[EXPONENTS[_1]] = _1 } }.freeze
|
|
33
|
+
|
|
34
|
+
attr_reader :size
|
|
35
|
+
|
|
36
|
+
def initialize(data)
|
|
37
|
+
codewords = encode(data.to_s.b)
|
|
38
|
+
@format = SIZES.find { _1.data >= codewords.size } ||
|
|
39
|
+
raise(ArgumentError, "#{data.to_s.bytesize} bytes take #{codewords.size} codewords: a DataMatrix holds 1558 at most")
|
|
40
|
+
@size = @format.modules
|
|
41
|
+
codewords = pad(codewords)
|
|
42
|
+
mapping = place(interleaved(codewords))
|
|
43
|
+
@modules = draw(mapping)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def dark?(x, y)
|
|
47
|
+
@modules[y][x]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def quiet_zone
|
|
51
|
+
QUIET_ZONE
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def encode(bytes)
|
|
57
|
+
codewords = []
|
|
58
|
+
bytes.scan(/\d\d|./mn) do |chunk|
|
|
59
|
+
if chunk.size == 2
|
|
60
|
+
codewords << (DIGIT_PAIR + chunk.to_i)
|
|
61
|
+
elsif chunk.ord < 128
|
|
62
|
+
codewords << (chunk.ord + 1)
|
|
63
|
+
else
|
|
64
|
+
codewords << UPPER_SHIFT << (chunk.ord - 127)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
codewords
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# The first pad is 129; the next ones are scrambled by their position, from 1 (§5.2.3).
|
|
71
|
+
def pad(codewords)
|
|
72
|
+
codewords << PAD if codewords.size < @format.data
|
|
73
|
+
while codewords.size < @format.data
|
|
74
|
+
value = PAD + (((149 * (codewords.size + 1)) % 253) + 1)
|
|
75
|
+
codewords << ((value > 254) ? value - 254 : value)
|
|
76
|
+
end
|
|
77
|
+
codewords
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Codewords dealt to the blocks in turn, each block's error correction dealt back the same way.
|
|
81
|
+
def interleaved(data)
|
|
82
|
+
blocks = @format.blocks
|
|
83
|
+
ecc_length = @format.ecc / blocks
|
|
84
|
+
divisor = divisor(ecc_length)
|
|
85
|
+
result = data.dup
|
|
86
|
+
blocks.times do |block|
|
|
87
|
+
remainder = remainder(data.select.with_index { |_, index| index % blocks == block }, divisor)
|
|
88
|
+
remainder.each_with_index { |codeword, index| result[data.size + block + (index * blocks)] = codeword }
|
|
89
|
+
end
|
|
90
|
+
result
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# (x - a^1)(x - a^2)…(x - a^degree), highest degree first without its leading 1.
|
|
94
|
+
def divisor(degree)
|
|
95
|
+
coefficients = [1]
|
|
96
|
+
(1..degree).each do |root|
|
|
97
|
+
next_coefficients = Array.new(coefficients.size + 1, 0)
|
|
98
|
+
coefficients.each_with_index do |coefficient, index|
|
|
99
|
+
next_coefficients[index] ^= coefficient
|
|
100
|
+
next_coefficients[index + 1] ^= multiply(coefficient, EXPONENTS[root])
|
|
101
|
+
end
|
|
102
|
+
coefficients = next_coefficients
|
|
103
|
+
end
|
|
104
|
+
coefficients.drop(1)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def remainder(data, divisor)
|
|
108
|
+
result = Array.new(divisor.size, 0)
|
|
109
|
+
data.each do |codeword|
|
|
110
|
+
factor = codeword ^ result.shift
|
|
111
|
+
result << 0
|
|
112
|
+
divisor.each_with_index { |coefficient, index| result[index] ^= multiply(coefficient, factor) }
|
|
113
|
+
end
|
|
114
|
+
result
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def multiply(x, y)
|
|
118
|
+
(x.zero? || y.zero?) ? 0 : EXPONENTS[LOGARITHMS[x] + LOGARITHMS[y]]
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# The data regions put together, filled by the standard's diagonal placement (Annex F).
|
|
122
|
+
def place(codewords)
|
|
123
|
+
@rows = @columns = (@size / (@format.region + 2)) * @format.region
|
|
124
|
+
@mapping = Array.new(@rows * @columns)
|
|
125
|
+
@codewords = codewords
|
|
126
|
+
character = 0
|
|
127
|
+
row = 4
|
|
128
|
+
column = 0
|
|
129
|
+
loop do
|
|
130
|
+
corner(character += 1, CORNER_1) if row == @rows && column.zero?
|
|
131
|
+
corner(character += 1, CORNER_2) if row == @rows - 2 && column.zero? && @columns % 4 != 0
|
|
132
|
+
corner(character += 1, CORNER_3) if row == @rows - 2 && column.zero? && @columns % 8 == 4
|
|
133
|
+
corner(character += 1, CORNER_4) if row == @rows + 4 && column == 2 && (@columns % 8).zero?
|
|
134
|
+
loop do
|
|
135
|
+
utah(row, column, character += 1) if row < @rows && column >= 0 && @mapping[(row * @columns) + column].nil?
|
|
136
|
+
row -= 2
|
|
137
|
+
column += 2
|
|
138
|
+
break unless row >= 0 && column < @columns
|
|
139
|
+
end
|
|
140
|
+
row += 1
|
|
141
|
+
column += 3
|
|
142
|
+
loop do
|
|
143
|
+
utah(row, column, character += 1) if row >= 0 && column < @columns && @mapping[(row * @columns) + column].nil?
|
|
144
|
+
row += 2
|
|
145
|
+
column -= 2
|
|
146
|
+
break unless row < @rows && column >= 0
|
|
147
|
+
end
|
|
148
|
+
row += 3
|
|
149
|
+
column += 1
|
|
150
|
+
break unless row < @rows || column < @columns
|
|
151
|
+
end
|
|
152
|
+
@mapping[(@rows * @columns) - 1] = @mapping[(@rows * @columns) - @columns - 2] = true if @mapping.last.nil?
|
|
153
|
+
@mapping
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Corner shapes: [row, column] of bits 1 to 8, from the far edges as negative numbers count.
|
|
157
|
+
CORNER_1 = [[-1, 0], [-1, 1], [-1, 2], [0, -2], [0, -1], [1, -1], [2, -1], [3, -1]].freeze
|
|
158
|
+
CORNER_2 = [[-3, 0], [-2, 0], [-1, 0], [0, -4], [0, -3], [0, -2], [0, -1], [1, -1]].freeze
|
|
159
|
+
CORNER_3 = [[-3, 0], [-2, 0], [-1, 0], [0, -2], [0, -1], [1, -1], [2, -1], [3, -1]].freeze
|
|
160
|
+
CORNER_4 = [[-1, 0], [-1, -1], [0, -3], [0, -2], [0, -1], [1, -3], [1, -2], [1, -1]].freeze
|
|
161
|
+
UTAH = [[-2, -2], [-2, -1], [-1, -2], [-1, -1], [-1, 0], [0, -2], [0, -1], [0, 0]].freeze
|
|
162
|
+
|
|
163
|
+
def corner(character, shape)
|
|
164
|
+
shape.each_with_index do |(row, column), bit|
|
|
165
|
+
module_at(row.negative? ? @rows + row : row, column.negative? ? @columns + column : column, character, bit)
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def utah(row, column, character)
|
|
170
|
+
UTAH.each_with_index { |(dy, dx), bit| module_at(row + dy, column + dx, character, bit) }
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Bit 0 is the codeword's most significant; positions off the edges wrap as the standard says.
|
|
174
|
+
def module_at(row, column, character, bit)
|
|
175
|
+
if row.negative?
|
|
176
|
+
row += @rows
|
|
177
|
+
column += 4 - ((@rows + 4) % 8)
|
|
178
|
+
end
|
|
179
|
+
if column.negative?
|
|
180
|
+
column += @columns
|
|
181
|
+
row += 4 - ((@columns + 4) % 8)
|
|
182
|
+
end
|
|
183
|
+
codeword = @codewords[character - 1]
|
|
184
|
+
@mapping[(row * @columns) + column] = !codeword.nil? && codeword[7 - bit] == 1
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Each data region framed by its finder: solid left and bottom edges, alternating top and right ones.
|
|
188
|
+
def draw(mapping)
|
|
189
|
+
side = @format.region
|
|
190
|
+
Array.new(@size) do |y|
|
|
191
|
+
Array.new(@size) do |x|
|
|
192
|
+
region_y, inner_y = y.divmod(side + 2)
|
|
193
|
+
region_x, inner_x = x.divmod(side + 2)
|
|
194
|
+
if inner_x.zero? || inner_y == side + 1 then true
|
|
195
|
+
elsif inner_y.zero? then inner_x.even?
|
|
196
|
+
elsif inner_x == side + 1 then inner_y.odd?
|
|
197
|
+
else mapping[(((region_y * side) + inner_y - 1) * @columns) + (region_x * side) + inner_x - 1]
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
end
|