@formancy/report-spec 0.1.0 → 0.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.
- package/NOTICE +8 -0
- package/README.md +377 -6
- package/dist/index.d.mts +1404 -24
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +8413 -1341
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -2
package/NOTICE
CHANGED
|
@@ -7,3 +7,11 @@ This product is distributed under the Formancy Report Source-Available Licence
|
|
|
7
7
|
This product includes software developed by the formancy project
|
|
8
8
|
(https://github.com/sharkysan/formancy.ai), licensed under the Apache License,
|
|
9
9
|
Version 2.0. Those packages keep their licence; nothing here changes it.
|
|
10
|
+
|
|
11
|
+
@formancy/report-render-pdf includes the Liberation Sans fonts, version
|
|
12
|
+
2.1.5, Regular and Bold, in its fonts directory and, as base64, in its
|
|
13
|
+
browser build. Digitized data copyright (c) 2010 Google Corporation, with
|
|
14
|
+
Reserved Font Arimo, Tinos and Cousine. Copyright (c) 2012 Red Hat, Inc.,
|
|
15
|
+
with Reserved Font Name Liberation. They are licensed under the SIL Open
|
|
16
|
+
Font License, Version 1.1, whose text travels with them as
|
|
17
|
+
fonts/LICENSE-LiberationSans.txt; nothing here changes it.
|
package/README.md
CHANGED
|
@@ -28,13 +28,380 @@ if (!outcome.valid) {
|
|
|
28
28
|
- anything a shape does not have, and every reference to something the
|
|
29
29
|
report does not declare;
|
|
30
30
|
- a value standing where it has none: a detail field outside the table's
|
|
31
|
-
cells, a page number outside the page header and footer
|
|
32
|
-
|
|
31
|
+
cells, a page number outside the page header and footer or anywhere in a
|
|
32
|
+
QR code, an EPC QR code or a QR-bill, whose content is resolved before
|
|
33
|
+
there are pages;
|
|
34
|
+
- a QR code of no runs, which would encode nothing;
|
|
35
|
+
- a fixed size the page cannot hold, a QR code's and an EPC QR code's
|
|
36
|
+
square measured against `squareRoom`;
|
|
37
|
+
- a payment part on a page other than A4 in portrait, or whose top margin
|
|
38
|
+
reaches past the slip's top (`QR_BILL_TOP`, 544.25 pt), refused as
|
|
39
|
+
`payment-part-page` on `/page` or `/page/margins/top`; and a text of its
|
|
40
|
+
QR-bill, written in literal runs, that SIX's rules refuse
|
|
41
|
+
(`invalid-qr-bill`);
|
|
42
|
+
- an EPC QR code that the EPC's rules refuse where it is written
|
|
43
|
+
(`invalid-epc-qr-code`): a literal text that breaks its rule, a reference
|
|
44
|
+
and a text together, a BIC its IBAN does not go with or none where one is
|
|
45
|
+
needed, literal texts already past 331 bytes, or, with Austria's caption,
|
|
46
|
+
a size in which no version prints the modules the PSA allows with a
|
|
47
|
+
symbol as tall as the caption beside it;
|
|
48
|
+
- an e-invoice that EN 16931's rules, or this version's narrower ones,
|
|
49
|
+
refuse where it is written (`invalid-e-invoice`), each refusal naming the
|
|
50
|
+
business term and the rule: a literal text, code or rate that breaks its
|
|
51
|
+
rule; parties, a time of supply, a period or VAT categories that cannot
|
|
52
|
+
make a valid invoice; lines that are not what the table over the detail
|
|
53
|
+
dataset prints; a QR-bill or an EPC QR code that asks to be paid to
|
|
54
|
+
another account, or a QR-bill in another currency; or a title holding
|
|
55
|
+
`&`, `<` or `>`.
|
|
33
56
|
|
|
34
|
-
|
|
35
|
-
|
|
57
|
+
A message names the field and the rule, and never quotes a payment code's
|
|
58
|
+
text. It reports up to `MAX_ERRORS` errors and says when there were more.
|
|
59
|
+
It checks structure, and what a payment code's literal texts say: whether
|
|
60
|
+
an expression compiles, what a code's data resolves to, and whether the
|
|
36
61
|
rendered output fits, are the runtime's and the renderer's questions.
|
|
37
62
|
|
|
63
|
+
## A QR-bill
|
|
64
|
+
|
|
65
|
+
A report may carry one payment part, `paymentPart`: a Swiss QR-bill, printed
|
|
66
|
+
at the foot of its last page
|
|
67
|
+
([0022](../../docs/decisions/0022-a-report-carries-one-swiss-qr-bill-at-the-foot-of-its-last-page.md)).
|
|
68
|
+
Its texts are runs, as a paragraph's: the account, both parties' structured
|
|
69
|
+
addresses, the reference, the message and the billing information. Its
|
|
70
|
+
amount is an expression; its language is one of SIX's five, its currency
|
|
71
|
+
CHF or EUR.
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { checkQrBillAccount, checkQrBillAmount, checkQrBillReference, checkQrBillText, emptyReport, QR_BILL_ADDRESS_RULES, QR_BILL_SIZE, QR_BILL_TOP, qrBillPageBreak, qrBillPairing } from '@formancy/report-spec'
|
|
75
|
+
|
|
76
|
+
checkQrBillAccount('CH44 3199 9123 0008 8901 2') // { ok: true, value: { iban: 'CH4431999123000889012', qrIban: true } }
|
|
77
|
+
checkQrBillReference('RF18 5390 0754 7034') // { ok: true, value: { type: 'SCOR', reference: 'RF18539007547034' } }
|
|
78
|
+
checkQrBillText('Seldwyla ', QR_BILL_ADDRESS_RULES.town) // { ok: true, value: 'Seldwyla' }
|
|
79
|
+
checkQrBillAmount('1191.005') // { ok: false, rule: 'has more than two decimals; ...' }
|
|
80
|
+
qrBillPairing('qrIban', 'SCOR', 'CHF') // [{ field: 'reference', rule: 'a QR-IBAN is paid to with a QR reference, ...' }]
|
|
81
|
+
QR_BILL_SIZE // { width: 595.28, height: 297.64 }: 210 by 105 mm
|
|
82
|
+
QR_BILL_TOP // 544.25: the slip's top, in points down the A4 page
|
|
83
|
+
const { page } = emptyReport('invoice', 'Rechnung', 'de-CH') // A4 portrait, 20 mm margins
|
|
84
|
+
qrBillPageBreak(page) // undefined: the slip has its room
|
|
85
|
+
qrBillPageBreak({ ...page, margins: { ...page.margins, top: 544.26 } }) // { field: 'topMargin', rule: "the top margin reaches 544.26 pt down the page, past the top of the QR-bill's slip at 544.25 pt, ..." }
|
|
86
|
+
qrBillPageBreak(page, 500) // the same rule, 500 pt a render measured above the slip counted with the margin: 'the top margin, with the page header ..., reaches 556.69 pt ...'
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
These are the rules of SIX's Swiss Implementation Guidelines for the
|
|
90
|
+
QR-bill, version 2.3, and version 2.4's QR-IBAN and QR reference for CHF
|
|
91
|
+
only. The validator applies them to a text no data can change; the runtime
|
|
92
|
+
applies the same functions to what the data resolves a text to. A rule
|
|
93
|
+
broken is said without the value. `qrBillPageBreak` is the page's rule:
|
|
94
|
+
the validator passes the page alone, and the renderer, which reaches it
|
|
95
|
+
through the runtime, adds the page header's height and the instruction to
|
|
96
|
+
separate above the slip, so a header that reaches into the slip is
|
|
97
|
+
refused only by a render, in the same words. What they do not check:
|
|
98
|
+
|
|
99
|
+
- that a country code is one ISO 3166-1 assigns, beyond two capitals;
|
|
100
|
+
- the syntax of the billing information past its leading `//` and the
|
|
101
|
+
two characters that name it;
|
|
102
|
+
- a postal code written with its country's prefix, which SIX forbids and
|
|
103
|
+
no pattern tells from a country whose codes begin with letters;
|
|
104
|
+
- the code's 997 characters, which only the whole bill has.
|
|
105
|
+
|
|
106
|
+
## An EPC QR code
|
|
107
|
+
|
|
108
|
+
A component, `epcQrCode`, prints the code a SEPA credit transfer is
|
|
109
|
+
initiated from: the European Payments Council's EPC069-12, version 3.1
|
|
110
|
+
([0025](../../docs/decisions/0025-a-report-prints-an-epc-qr-code-held-to-the-epc-rules-beside-the-qr-bill.md)).
|
|
111
|
+
It stands wherever a QR code may; Austria's PSA asks for the lower edge of
|
|
112
|
+
the page, where the author puts it. Its beneficiary, IBAN, BIC, reference,
|
|
113
|
+
text and information are runs, as a paragraph's; its amount is an
|
|
114
|
+
expression; its purpose is four capital letters; and its `caption`,
|
|
115
|
+
`'zahlenMitCode'`, draws Austria's frame and "Zahlen mit Code" inside its
|
|
116
|
+
square. A reference and a text are never both given.
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import { checkEpcAmount, checkEpcField, checkEpcIban, checkEpcReference, epcCaptionBreak, epcLeastVersion, epcPairing, epcPayload } from '@formancy/report-spec'
|
|
120
|
+
|
|
121
|
+
checkEpcIban('de71 1102 2033 0123 4567 89') // { ok: true, value: 'DE71110220330123456789' }
|
|
122
|
+
checkEpcIban('FO2000400440116243') // { ok: false, rule: 'is not the IBAN of an account in SEPA: ...' }
|
|
123
|
+
checkEpcIban('CH44 3199 9123 0008 8901 2') // { ok: false, rule: 'is a QR-IBAN, which SIX keeps for QR-bills in francs ...' }
|
|
124
|
+
checkEpcReference('rf18 5390 0754 7034') // { ok: true, value: 'RF18539007547034' }: the QR-bill's own check, then capitals
|
|
125
|
+
checkEpcField('information', ' ') // { ok: false, rule: 'is blank, and an element the code holds has one character at least ...' }
|
|
126
|
+
checkEpcAmount('184.60') // { ok: true, value: '184.6' }: as short as it can be written
|
|
127
|
+
epcPairing('CH9300762011623852957', null) // [{ field: 'bic', rule: "an account outside the EEA is paid to with its bank's BIC: ..." }]
|
|
128
|
+
epcPayload({ bic: 'BHBLDEHHXXX', beneficiary: 'Franz Mustermänn', iban: 'DE71110220330123456789', amount: '12.3', purpose: 'GDDS', reference: 'RF18539007547034' })
|
|
129
|
+
// 'BCD\n001\n1\nSCT\nBHBLDEHHXXX\nFranz Mustermänn\n...\nRF18539007547034': the EPC's first sample, byte for byte
|
|
130
|
+
epcCaptionBreak(68.05) // 'with the caption, a code needs a square of at least 68.06 pt, and this one is 68.05 pt: ...'
|
|
131
|
+
epcLeastVersion(85.04, 'zahlenMitCode') // 8: version 7's modules would be 0.515 mm
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
What the code holds is SCT in euro, in UTF-8, at most 331 bytes, version
|
|
135
|
+
002 without a BIC and 001 with one. An IBAN is of a country of SEPA, of
|
|
136
|
+
its length and with its check digits right, and no QR-IBAN, which SIX
|
|
137
|
+
keeps for QR-bills in francs; a BIC is of a bank in SEPA, of the IBAN's
|
|
138
|
+
country -- this module's inference from EPC409-09's territory codes --
|
|
139
|
+
and given for an account outside the EEA. Which countries those are, and
|
|
140
|
+
each IBAN's length, is one table, recorded from EPC409-09 version 8.0 and,
|
|
141
|
+
for the lengths, python-stdnum's copy of release 101 of SWIFT's IBAN
|
|
142
|
+
Registry; SWIFT's own file was not read. The IBAN's check digits and the
|
|
143
|
+
creditor reference are checked by the QR-bill's own functions, and the
|
|
144
|
+
QR-IBAN's range is the QR-bill's; the IBAN and the reference are written
|
|
145
|
+
in capitals. An amount is from 0.01 to 999999999.99, never rounded, and
|
|
146
|
+
written as the PSA writes one: `EUR45`, `EUR184.6`. A free text holds no
|
|
147
|
+
control character or line break, which would end its line of the code.
|
|
148
|
+
No text is blank: `checkEpcField` refuses a blank name or IBAN as needed,
|
|
149
|
+
and a blank optional text as the text left out, by `EPC_TEXT_FIELDS`, the
|
|
150
|
+
one table of the code's texts the validator and the runtime read.
|
|
151
|
+
With the caption, `epcModule` and `epcCaptionBreak` hold the modules to
|
|
152
|
+
15 to 20 mil and the symbol to at least the caption's length, 51.36 pt in
|
|
153
|
+
Liberation Sans at 7 pt, since the caption stands beside it: the validator
|
|
154
|
+
refuses a size in which no version does both, which leaves squares of
|
|
155
|
+
68.06 to 118.6 pt, and the renderer the version it encodes. A payload too
|
|
156
|
+
short for its square is drawn in a larger version of itself, from
|
|
157
|
+
`epcLeastVersion`, rather than refused.
|
|
158
|
+
|
|
159
|
+
What they do not check:
|
|
160
|
+
|
|
161
|
+
- a purpose code against ISO 20022's list, beyond four capitals;
|
|
162
|
+
- that the amount a template binds is in euro: it is a decimal, and has no
|
|
163
|
+
currency;
|
|
164
|
+
- that a payer's bank is in the EEA, where a code without a BIC may be
|
|
165
|
+
refused outside it;
|
|
166
|
+
- a structured reference other than a creditor reference: a Belgian
|
|
167
|
+
structured communication is refused, and goes in the text;
|
|
168
|
+
- the payload's bytes before a render, beyond what the literal texts
|
|
169
|
+
write alone;
|
|
170
|
+
- that a bank's app accepts the code, which nothing here can show.
|
|
171
|
+
|
|
172
|
+
## An e-invoice
|
|
173
|
+
|
|
174
|
+
A report may carry one e-invoice, `eInvoice`: an EN 16931 invoice, which the
|
|
175
|
+
runtime writes as Factur-X 1.09.2 / ZUGFeRD 2.5.2 CII in the EN 16931 profile
|
|
176
|
+
([0027](../../docs/decisions/0027-a-report-carries-one-en-16931-invoice-written-from-the-values-its-page-prints.md)). It is of kind `facturX`, type 380, in euro, its VAT categories
|
|
177
|
+
standard rated, exempt or reverse-charged, with no allowances or charges: a
|
|
178
|
+
discount is a line of negative quantity at a positive price. Its seller, its
|
|
179
|
+
buyer, their addresses and its payment instructions are runs, as a
|
|
180
|
+
paragraph's, read in the report's scope and written into the XML, so none
|
|
181
|
+
holds a page number or count; its dates and its paid amount are
|
|
182
|
+
expressions. Its lines are the rows of the table over the detail dataset:
|
|
183
|
+
`lines` names that table and the four columns that print each line's name,
|
|
184
|
+
quantity, net price and net amount, so the page and the XML read one
|
|
185
|
+
evaluation of each line, and reads the line's identifier, unit and VAT
|
|
186
|
+
category from each row.
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
import { checkCountryCode, checkEInvoiceDecimal, checkEInvoiceText, checkVatIdentifier, checkVatRate, FACTUR_X, FACTUR_X_PROFILES, sellerIdentificationBreaks, vatCategoryBreaks } from '@formancy/report-spec'
|
|
190
|
+
|
|
191
|
+
checkCountryCode('UK') // { ok: false, rule: "is not a country code of those this release takes, a list narrower than ISO 3166-1's (BR-CL-14)" }
|
|
192
|
+
checkVatIdentifier('DE 123 456 789') // { ok: true, value: 'DE123456789' }
|
|
193
|
+
checkEInvoiceText('BT-1', ' ') // { ok: false, rule: 'is empty, and an e-invoice needs it (BR-02)' }
|
|
194
|
+
checkEInvoiceText('BT-84', 'DE83 0123 4567 0012 3456 78') // { ok: true, value: 'DE83012345670012345678' }: the EPC QR code's account rule
|
|
195
|
+
checkEInvoiceDecimal('BT-131', '10.500') // { ok: true, value: '10.50' }
|
|
196
|
+
checkEInvoiceDecimal('BT-131', '10.505') // { ok: false, rule: 'is not a whole number of cents, ... (BR-DEC-23)' }: never rounded
|
|
197
|
+
checkEInvoiceDecimal('BT-146', '-1') // { ok: false, rule: 'is negative, and a net price never is: ... (BR-27)' }
|
|
198
|
+
checkVatRate('S', '19.00') // { ok: true, value: '19' }: compared by value
|
|
199
|
+
checkVatRate('S', '0.4') // { ok: false, rule: 'is below 0.5, which BR-CO-17 rounds to 0, ... (BR-CO-17)' }
|
|
200
|
+
checkEInvoiceText('BT-37', ' ') // { ok: false, rule: 'is empty, and a German invoice gives the full address ... (§ 14 Abs. 4 Satz 1 Nr. 1 UStG)' }
|
|
201
|
+
checkEInvoiceText('BT-1', 'RE-2026 0820') // { ok: false, rule: 'holds white space the XML would not keep: ...' }: a token
|
|
202
|
+
vatCategoryBreaks([{ code: 'S', rate: '19' }, { code: 'S', rate: '19.00' }]) // [{ index: 1, field: 'rate', rule: '... (BR-S-08)' }]
|
|
203
|
+
sellerIdentificationBreaks({ vatId: false, taxNumber: true, legalId: false }, ['S']) // ['the seller has neither a VAT identifier (BT-31) nor a legal registration identifier (BT-30), ... (BR-CO-26)']
|
|
204
|
+
FACTUR_X_PROFILES.en16931.guideline // 'urn:cen.eu:en16931:2017': BT-24
|
|
205
|
+
FACTUR_X.fileName // 'factur-x.xml'
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
`en16931.ts` holds EN 16931's rules as functions over text, each answering
|
|
209
|
+
the value the XML is written with or the rule broken, named by its business
|
|
210
|
+
term and its business rule and never by the value. The validator applies
|
|
211
|
+
them to a literal; the runtime applies the same functions to what the data
|
|
212
|
+
resolves a text to, and names its refusals by the same tables,
|
|
213
|
+
`E_INVOICE_TEXTS`, `E_INVOICE_DECIMALS` and `E_INVOICE_DATES`, and
|
|
214
|
+
reads which term each text is by where it stands from one table,
|
|
215
|
+
`E_INVOICE_TEXT_TERMS`, as each line term names its own; a date
|
|
216
|
+
the data resolved is held to a day on the calendar by `checkEInvoiceDate`,
|
|
217
|
+
since the editing core types a date term as any text.
|
|
218
|
+
`EN16931_RULES` is every rule they name, and the e-invoice gate holds each
|
|
219
|
+
to a fatal rule of its pinned schematron. Two things they hold are not
|
|
220
|
+
CEN's: the seller's and the buyer's street, post code and city, which
|
|
221
|
+
§ 14 Abs. 4 Satz 1 Nr. 1 UStG asks for (`FULL_ADDRESS`), where CEN asks
|
|
222
|
+
only the country; and an identifier or a code the XSD types as a token,
|
|
223
|
+
which holds no tab, line break, double space or space at either end, since
|
|
224
|
+
a schema-aware reader collapses them. The payment account and its BIC are
|
|
225
|
+
the EPC QR code's rules. The totals and the VAT breakdown are not here: the
|
|
226
|
+
runtime computes them once.
|
|
227
|
+
|
|
228
|
+
A report that is an e-invoice gives its expressions three names beside
|
|
229
|
+
`EXPRESSION_NAMES`'s, and no other report has them (`E_INVOICE_NAMES`):
|
|
230
|
+
`invoice`, whose members `INVOICE_TOTALS` names by business term, and
|
|
231
|
+
`vatBasis` and `vatAmount`, each declared category's taxable and tax
|
|
232
|
+
amount by its id (`VAT_BREAKDOWN`). Whether an expression reads them
|
|
233
|
+
where it may is the editing core's to check, as every name is.
|
|
234
|
+
`E_INVOICE_LINE_COLUMNS` and `E_INVOICE_LINE_CODES` say which types of
|
|
235
|
+
field each term of a line is read from: the validator holds a field to
|
|
236
|
+
them, and the editing core an expression.
|
|
237
|
+
|
|
238
|
+
The codes come from closed lists that someone chose, narrower than CEN's,
|
|
239
|
+
and held to CEN's 1.3.17 and the pinned 1.3.16 lists on 17 October 2026:
|
|
240
|
+
`UNIT_CODES`, the units of UN/ECE Recommendation 20 a German invoice
|
|
241
|
+
counts, times, weighs and measures in; `COUNTRY_CODES`, the
|
|
242
|
+
EU's members, the EEA's, Switzerland and the United Kingdom; `VAT_PREFIXES`,
|
|
243
|
+
those and Greece's `EL`, which CEN's 1.3.16 takes and its 1.3.17 does not;
|
|
244
|
+
and `SCHEME_IDS`, SIREN, SIRET, D-U-N-S and GLN. A rate is a percentage
|
|
245
|
+
below 100 with two decimals at most, and a standard rate 0.5 or more, which
|
|
246
|
+
BR-CO-17 would otherwise round to 0. `FACTUR_X` and `FACTUR_X_PROFILES` are
|
|
247
|
+
the names the PDF carries -- the file, its relationship and the XMP's values
|
|
248
|
+
-- which rest on a transcription of the specification, not on the
|
|
249
|
+
specification itself, until the owner's copy is read.
|
|
250
|
+
|
|
251
|
+
What they do not check:
|
|
252
|
+
|
|
253
|
+
- that a VAT identifier is one its country issued, past its prefix;
|
|
254
|
+
- that a rate is one a country applies, or the right one for a line;
|
|
255
|
+
- that an exemption reason says what the law says;
|
|
256
|
+
- the amount a payment code asks for, which is an expression: the render
|
|
257
|
+
compares it with the amount due;
|
|
258
|
+
- an account, a date or a total read from the data, before a render;
|
|
259
|
+
- France's rules (BR-FR), and XRechnung's.
|
|
260
|
+
|
|
261
|
+
## An SVG asset
|
|
262
|
+
|
|
263
|
+
An image asset may be `image/svg+xml`, held to one form: what `cleanSvg`
|
|
264
|
+
writes ([0030](../../docs/decisions/0030-an-image-asset-may-be-an-svg-of-drawing-elements-alone-cleaned-once-and-refused-at-print-in-any-other-form.md)).
|
|
265
|
+
A host approves a customer's SVG by cleaning it and keeping the bytes
|
|
266
|
+
`cleanSvg` returns, whose digest the asset carries; the renderer draws an
|
|
267
|
+
SVG only when `checkSvg` finds it already in that form, and refuses it
|
|
268
|
+
otherwise, so nothing is cleaned, dropped or rewritten at print.
|
|
269
|
+
|
|
270
|
+
```ts
|
|
271
|
+
import { checkSvg, cleanSvg } from '@formancy/report-spec'
|
|
272
|
+
|
|
273
|
+
const approved = cleanSvg(file) // the file's bytes; there is no TextDecoder in this package's world, and none is needed
|
|
274
|
+
// { ok: true, bytes: <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40"><circle cx="20" cy="20" r="18" fill="#1f4e79"/></svg>,
|
|
275
|
+
// viewBox: { x: 0, y: 0, width: 40, height: 40 }, segments: 20, groups: 0, drawn: { lines: 2, curves: 6, elements: 2, stops: 0 },
|
|
276
|
+
// extent: { groups: { inverse: 0, origin: 0 }, patterns: { linear: 0, offset: 0 }, local: 0, own: 39.11426010499247, nesting: 3 },
|
|
277
|
+
// removed: [declaration, attribute width, attribute height, element title], removedMore: 0 }
|
|
278
|
+
checkSvg(approved.bytes) // { ok: true, … }: what cleanSvg wrote, unchanged
|
|
279
|
+
checkSvg(file) // { ok: false, code: 'unsupported', message: 'the drawing holds a declaration, which cleaning removes; clean it with cleanSvg first' }
|
|
280
|
+
cleanSvg(lettered) // { ok: false, code: 'text', message: 'svg/text is text; convert the text to outlines' }
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
What a cleaned SVG holds, in `svg-allow.ts`'s tables:
|
|
284
|
+
|
|
285
|
+
- **Drawing elements alone**, `SVG_ELEMENTS`: `g`, `defs`, the seven
|
|
286
|
+
shapes, linear and radial gradients and their stops, clip paths of
|
|
287
|
+
shapes. Text, embedded pictures, `use`, `symbol`, patterns, masks,
|
|
288
|
+
markers, filters, `foreignObject`, `switch` and a nested `svg` are
|
|
289
|
+
refused. Every refusal says what to do after what it found -- convert
|
|
290
|
+
the text to outlines, add the picture as an asset of its own, save it
|
|
291
|
+
without Illustrator's editing capabilities -- or, where it knows nothing
|
|
292
|
+
more particular, its code's own advice.
|
|
293
|
+
- **What never draws is removed and listed** -- comments, the declaration,
|
|
294
|
+
a plain DOCTYPE, titles, metadata, scripts, animations, an editor's
|
|
295
|
+
elements and attributes in their own namespaces, event handlers, data and
|
|
296
|
+
accessibility attributes, rendering hints, text and vendor-prefixed
|
|
297
|
+
properties, what draws nowhere on the element it is written on (paint
|
|
298
|
+
and clips on a gradient, a stop or `defs`, a stop's colour anywhere
|
|
299
|
+
else), hidden elements, shapes of no size, what nothing that draws
|
|
300
|
+
refers to, ids nothing refers to --
|
|
301
|
+
by name and count, never by value, the first `MAX_SVG_REMOVED_NAMES`
|
|
302
|
+
names and a count of the rest. A link is the group it draws as, its
|
|
303
|
+
address removed.
|
|
304
|
+
- **CSS flattened into attributes**: a `style` attribute and `<style>`
|
|
305
|
+
rules of types, classes, ids and their compounds, in CSS's cascade. A
|
|
306
|
+
combinator, an at-rule, `!important` or an escape is refused.
|
|
307
|
+
- **Colours as `#rrggbb`**, from `#rgb`, `#rrggbb`, `rgb()` and CSS
|
|
308
|
+
Color 4's names; one with an alpha, `hsl()`, `transparent` and the system
|
|
309
|
+
colours are refused. `currentColor` is written as the colour it stands
|
|
310
|
+
for.
|
|
311
|
+
- **References inside the file alone**: a fill or a stroke may name a
|
|
312
|
+
gradient, a clip path a clip path; a gradient's `href` chain is flattened
|
|
313
|
+
into it. Anything else that names anything is refused, so nothing outside
|
|
314
|
+
the file is ever read. A clip rule a clip path would inherit from what
|
|
315
|
+
holds it is written on it, so that its clip never rests on `defs`.
|
|
316
|
+
- **Absolute numbers in one written form**: path data in absolute commands
|
|
317
|
+
whose relative steps sum exactly, transforms composed into one matrix,
|
|
318
|
+
units converted, nothing below a millionth, and nothing past
|
|
319
|
+
`MAX_SVG_COORDINATE`: no number, no transform composed from the root and
|
|
320
|
+
no inverse of one, which the converter computes for every clip, and no
|
|
321
|
+
matrix a gradient is drawn under in any box inside its shape's, which
|
|
322
|
+
the converter's box of a curve is. The root keeps its namespace and its
|
|
323
|
+
viewBox alone; its paint and transform move onto a group inside it, so a
|
|
324
|
+
transform can never move the drawing off the report's box.
|
|
325
|
+
- **Bounded**: `MAX_SVG_BYTES` in and out, `MAX_SVG_DEPTH`,
|
|
326
|
+
`MAX_SVG_ELEMENTS`, `MAX_SVG_STOPS`, `MAX_SVG_DASHES`, `MAX_SVG_RULES`,
|
|
327
|
+
the cascade's work (`MAX_SVG_CASCADE`), and what a reader will paint:
|
|
328
|
+
`MAX_SVG_SEGMENTS` weighted segments, a clip path counted for every
|
|
329
|
+
element it clips and a gradient's stops for every shape painted with it,
|
|
330
|
+
and `MAX_SVG_GROUPS` units of page-sized transparency groups. Each bound
|
|
331
|
+
but `MAX_SVG_REMOVED_NAMES` says in `limits.ts` how it was measured, and
|
|
332
|
+
the reader's time is linear in the bytes: a mebibyte of each shape of
|
|
333
|
+
value a pattern could have made quadratic is cleaned in a test's
|
|
334
|
+
ceiling.
|
|
335
|
+
- **Its extent measured, `SvgExtent`** (`svg-extent.ts`): how far the
|
|
336
|
+
numbers svg-to-pdfkit writes for the drawing reach wherever it is
|
|
337
|
+
placed -- the inverses of the matrices its page-sized groups are drawn
|
|
338
|
+
under and where they take the viewBox's origin, the matrices its
|
|
339
|
+
gradients are drawn under outside a group and inside one, and the box
|
|
340
|
+
a clip in objectBoundingBox units clips to, each over every box inside
|
|
341
|
+
a shape's, and the points the converter draws its outlines with, an
|
|
342
|
+
arc's grown to reach its ends among them -- and how deep it nests the
|
|
343
|
+
graphics state. Nothing is refused by it here; the renderer holds an
|
|
344
|
+
image in an e-invoice to it, whose PDF/A-3 holds an integer to
|
|
345
|
+
2,147,483,647 and the nesting to 28.
|
|
346
|
+
|
|
347
|
+
Cleaning is a fixed point: `cleanSvg` of what it wrote gives the same bytes
|
|
348
|
+
with nothing removed, which is what `checkSvg` tests for.
|
|
349
|
+
|
|
350
|
+
**A host shows the approver the cleaned drawing, or the PDF, never the
|
|
351
|
+
file as uploaded.** A browser draws the file as it came, with what
|
|
352
|
+
cleaning removes: an animation runs to its end, where the PDF shows the
|
|
353
|
+
drawing before it starts, and a script may change what is drawn. The
|
|
354
|
+
cleaned bytes are what prints, and what an approver says yes to.
|
|
355
|
+
|
|
356
|
+
What it does not do: draw text, inline `use`, convert
|
|
357
|
+
a colour with an alpha into an opacity, or follow a reference out of the
|
|
358
|
+
file; and that a browser draws the cleaned drawing as the PDF does is held
|
|
359
|
+
by the tables and review, not by comparing the two.
|
|
360
|
+
|
|
361
|
+
## An image's bytes
|
|
362
|
+
|
|
363
|
+
What an image a person adds is, and the digest a report pins it by, are
|
|
364
|
+
read here, so that a builder refuses at the door what the renderer would
|
|
365
|
+
refuse at every print, and names the image as the validator reads a name
|
|
366
|
+
(0033):
|
|
367
|
+
|
|
368
|
+
```ts
|
|
369
|
+
import { assetDigest, imageHeader, sniffImage } from '@formancy/report-spec'
|
|
370
|
+
|
|
371
|
+
sniffImage(bytes) // 'image/png', 'image/jpeg' or 'image/svg+xml', by the first bytes alone; undefined for anything else
|
|
372
|
+
imageHeader(png) // { ok: true, mediaType: 'image/png', width: 120, height: 40, bitsPerPixel: 32, interlaced: false, data: every IDAT chunk's bytes, joined }
|
|
373
|
+
imageHeader(jpeg) // { ok: true, mediaType: 'image/jpeg', width: 40, height: 120, components: 3 }: as it is shown, after its Exif turn
|
|
374
|
+
imageHeader(wide) // { ok: false, code: 'too-large', message: 'is 5000 × 300 pixels; the limit is 4096 pixels a side', width: 5000, height: 300 }
|
|
375
|
+
assetDigest(bytes) // 'sha256:' and 64 lower-case hex digits, what an asset's digest is
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
- **`sniffImage`** judges by a PNG's signature, a JPEG's start of image,
|
|
379
|
+
and `<` after an optional UTF-8 byte order mark and white space, never by
|
|
380
|
+
a file's name or a type a browser guessed. A UTF-16 or UTF-32 byte order
|
|
381
|
+
mark is an SVG too, so that `cleanSvg` can tell the person to save it as
|
|
382
|
+
UTF-8. A sniff is no check: the bytes are read whole next.
|
|
383
|
+
- **`imageHeader`** reads a PNG's chunks, each with its length and CRC,
|
|
384
|
+
the header, the palette it needs and the end chunk with nothing after
|
|
385
|
+
it; and a JPEG's segments up to its scan, the frame header where PDFKit
|
|
386
|
+
reads it, the first Exif block's orientation and the end marker. Its
|
|
387
|
+
refusals, `IMAGE_REFUSAL_CODES`, are in the words the renderer says them
|
|
388
|
+
in after an asset's name, and one of a picture's size carries the size.
|
|
389
|
+
A picture is held to `MAX_ASSET_SIDE` pixels a side, and every reader of
|
|
390
|
+
an asset's bytes to `MAX_ASSET_BYTES`. A PNG's image data comes back in
|
|
391
|
+
one array, copied once however many chunks it was split over, and
|
|
392
|
+
nothing of a chunk is kept but its length: a person's 20 MiB file can
|
|
393
|
+
hold 1,747,622 empty ones, which a view kept of each held in 237 MB of
|
|
394
|
+
heap, and which are now read in 38 ms keeping nothing (Node 24.21.0 on
|
|
395
|
+
an Intel i9-10900K, 19 October 2026).
|
|
396
|
+
- **`assetDigest`** is `@noble/hashes`' SHA-256, synchronous, on the
|
|
397
|
+
caller's thread: Web Crypto's is asynchronous and no part of this
|
|
398
|
+
package's library. A TypeError for anything that is not a `Uint8Array`.
|
|
399
|
+
|
|
400
|
+
What it does not do: inflate a PNG's image data, which needs a
|
|
401
|
+
decompressor ES2023 does not have. The renderer does at print, so a PNG
|
|
402
|
+
whose data does not inflate to the scanlines its header promises passes
|
|
403
|
+
here and is refused there. Nothing decodes a JPEG's scan.
|
|
404
|
+
|
|
38
405
|
## Hash
|
|
39
406
|
|
|
40
407
|
```ts
|
|
@@ -51,16 +418,20 @@ validator refuses: only a valid report has a hash.
|
|
|
51
418
|
## Start, find and measure
|
|
52
419
|
|
|
53
420
|
```ts
|
|
54
|
-
import { componentAt, emptyReport, mmToPt, printableArea, walkComponents } from '@formancy/report-spec'
|
|
421
|
+
import { componentAt, emptyReport, mmToPt, printableArea, squareRoom, walkComponents } from '@formancy/report-spec'
|
|
55
422
|
|
|
56
423
|
const blank = emptyReport('site-visit', 'Site visit', 'de-CH') // A4 portrait, 20 mm margins
|
|
57
424
|
const margin = mmToPt(15) // 42.52: lengths are points, stored to the hundredth
|
|
58
425
|
const { width } = printableArea(blank.page) // what the margins leave
|
|
426
|
+
const { side, length } = squareRoom(printableArea(blank.page)) // the largest square, a QR code's room
|
|
59
427
|
for (const { component, path, depth } of walkComponents(blank)) console.log(depth, path, component.id)
|
|
60
428
|
componentAt(blank, 'title') // where a component is, by id: the path its errors use
|
|
61
429
|
```
|
|
62
430
|
|
|
63
431
|
`REPORT_SPEC_VERSION` is `0`, the draft: nothing written under it is promised
|
|
64
|
-
to be read by a later release.
|
|
432
|
+
to be read by a later release. Nor by an earlier one: a kind the draft gains
|
|
433
|
+
is refused as `unknown-kind` by a release that predates it, never skipped. A
|
|
434
|
+
report with a `qrCode` or an `epcQrCode` is refused so by 0.1.0, and one
|
|
435
|
+
with a `paymentPart` or an `eInvoice` as `unknown-property`.
|
|
65
436
|
|
|
66
437
|
Source-available, not open source: see [`LICENSE.md`](../../LICENSE.md).
|