resvg-napi 0.2.0 → 0.3.1

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/README.md CHANGED
@@ -1,15 +1,17 @@
1
- # resvg-napi
1
+ # Render and inspect SVG
2
2
 
3
3
  [![npm](https://img.shields.io/npm/v/resvg-napi)](https://www.npmjs.com/package/resvg-napi)
4
4
  [![conformance](https://img.shields.io/badge/resvg%20conformance-1715%2F1715-brightgreen)](#conformance)
5
5
  [![licence](https://img.shields.io/npm/l/resvg-napi)](#licence)
6
6
 
7
- Node.js bindings for [resvg](https://github.com/linebender/resvg) 0.48: render
8
- SVG to PNG, and read back what usvg made of the document — the resolved tree,
9
- text metrics, paint, geometry.
7
+ Render an SVG to PNG, and read back what the renderer made of the document — the
8
+ resolved tree, text metrics, paint, geometry. Node.js bindings for
9
+ [resvg](https://github.com/linebender/resvg) 0.48, generated from the upstream
10
+ Rust sources, so the TypeScript surface is usvg's own, names and doc comments
11
+ included.
10
12
 
11
- Generated from the upstream Rust sources, so the TypeScript surface is usvg's
12
- own, names and doc comments included.
13
+ **[Try it in the browser](https://p8nut.github.io/resvg-napi/)** — the same
14
+ bindings compiled to WASI, rendering in the page, nothing to install.
13
15
 
14
16
  ```bash
15
17
  npm install resvg-napi
@@ -24,26 +26,42 @@ doc.renderRaw({ crop: doc.absLayerBoundingBox() }) // RGBA8, trimmed
24
26
  await renderAsync(svg, options, { width: 1200 }) // off the event loop
25
27
  ```
26
28
 
27
- ## What you can read back
29
+ `index.d.ts` is the reference — it is generated, so it cannot drift. What follows
30
+ is the map.
28
31
 
29
- `index.d.ts` is the reference — it is generated, so it cannot drift. This is
30
- the map.
32
+ ## Render SVG to PNG
31
33
 
32
- **Render.** Parse once with `new Resvg(svg, options?, fonts?, images?)`, then
33
- render as often as you like.
34
+ Parse once with `new Resvg(svg, options?, fonts?, images?)`, then render as often
35
+ as you like.
34
36
 
35
37
  | | |
36
38
  |---|---|
37
39
  | `renderPng(params?)` | `Buffer` |
38
40
  | `renderRaw(params?)` | `RawImage`, un-premultiplied RGBA8 |
39
- | `toString(opts?)` | the resolved tree, back as SVG |
41
+ | `toString(opts?)` | the resolved tree, back as SVG — text as paths unless `preserveText` |
40
42
  | `renderPngAsync` · `renderRawAsync` · `toStringAsync` | the same, off the event loop, `AbortSignal`-aware |
41
43
 
42
44
  `params` carries `width` / `height` / `scale`, a `background` colour and a
43
45
  `crop` box. `Resvg.parseAsync` moves the parse off the loop too.
44
46
 
45
- **Walk the tree.** `node(id)` and `children()` hand back `SvgNode`s, and each
46
- one knows what it is:
47
+ ## Text as outlines
48
+
49
+ `toString()` hands back the resolved tree with **the text converted to paths** —
50
+ an SVG that draws the same where the font is not installed, which is what a
51
+ print or PDF pipeline needs from a template:
52
+
53
+ ```js
54
+ const doc = new Resvg(svg, { fontFamily: 'DejaVu Sans' }, fonts)
55
+ doc.toString() // one <path> per glyph, no <text>
56
+ doc.toString({ preserveText: true }) // <text> kept, resolved but not outlined
57
+ ```
58
+
59
+ The outlines come from the same layout `renderPng` draws, so a glyph sits where
60
+ the raster puts it — and `extent()` measured it before either.
61
+
62
+ ## Inspect the usvg tree
63
+
64
+ `node(id)` and `children()` hand back `SvgNode`s, and each one knows what it is:
47
65
 
48
66
  ```js
49
67
  const n = doc.node('surname')
@@ -68,24 +86,75 @@ if (paint?.type === 'color') paint.value // { red, green, blue }
68
86
  else paint?.id // the gradient or pattern it names
69
87
  ```
70
88
 
71
- **Measure.** Every node reports `boundingBox()`, `absBoundingBox()`,
72
- `strokeBoundingBox()`, `absLayerBoundingBox()` and `extent()` — in the
73
- document's own units, which is not the canvas. See *Fitting text* below for why
89
+ ## Measure text and geometry
90
+
91
+ Every node reports `boundingBox()`, `absBoundingBox()`, `strokeBoundingBox()`,
92
+ `absLayerBoundingBox()` and `extent()` — in the document's own units, which is
93
+ not the canvas. See [Fitting text to a width](#fitting-text-to-a-width) for why
74
94
  that distinction bites.
75
95
 
76
- **Fonts.** `FontDatabase` is `fontdb` itself: `loadSystemFonts()`,
77
- `loadFontData(buffer)`, `loadFontFile(path)`, `faces()`, `query()`, and the
78
- generic-family setters. A face reports its `families`, `weight`, `style`
79
- (`'normal' | 'italic' | 'oblique'`) and whether it is `monospaced`. `pendingFonts()` on a parsed document names the
80
- families it wanted and did not get; `pendingImages()` does the same for hrefs.
96
+ ## Fonts
97
+
98
+ `FontDatabase` is `fontdb` itself: `loadSystemFonts()`, `loadFontData(buffer)`,
99
+ `loadFontFile(path)`, `faces()`, `query()`, and the generic-family setters. A
100
+ face reports its `families`, `weight`, `style` (`'normal' | 'italic' |
101
+ 'oblique'`) and whether it is `monospaced`. `pendingFonts()` on a parsed document
102
+ names the families it wanted and did not get; `pendingImages()` does the same for
103
+ hrefs.
104
+
105
+ `pendingFonts()` names the families nobody had. `faceOf(glyph)` names the face
106
+ that drew the text instead — the substitution a document-wide list cannot show,
107
+ and the one that survives to print:
108
+
109
+ ```js
110
+ const text = doc.node('title').text()
111
+ text.chunks[0].spans[0].font.families // ['Brand Sans', 'DejaVu Sans'] — asked for
112
+ for (const glyph of text.layouted[0].positionedGlyphs) {
113
+ doc.faceOf(glyph)?.postScriptName // 'DejaVuSans' — what actually drew it
114
+ }
115
+ ```
116
+
117
+ `faceOf` returns `null` only for a glyph from another document: the id a glyph
118
+ carries is a key into the font database its own document was parsed with.
119
+
120
+ ## Paint servers
121
+
122
+ `linearGradients()`, `radialGradients()`, `patterns()`, `clipPaths()`, `masks()`,
123
+ `filters()` — the paint servers an element's `id` refers to.
124
+
125
+ ## Examples
126
+
127
+ Four templates in [`demo/examples/`](demo/examples), each rendered by
128
+ `node demo/render.mjs <template.svg>`: Liquid fills the template from the JSON
129
+ file next to it, this binding rasterises the result. The images below are that
130
+ command's output.
131
+
132
+ ### Roster — a table from a JSON array
133
+
134
+ ![roster](https://raw.githubusercontent.com/p8nut/resvg-napi/main/demo/assets/roster.png)
81
135
 
82
- **Definitions.** `linearGradients()`, `radialGradients()`, `patterns()`,
83
- `clipPaths()`, `masks()`, `filters()` — the paint servers an element's `id`
84
- refers to.
136
+ A `staff` array becomes rows, with an embedded logo and a QR code generated
137
+ during templating. The fourth name runs straight into the column beside it —
138
+ measuring that overflow, and compressing it away, is what [Fitting text to a
139
+ width](#fitting-text-to-a-width) below is for.
85
140
 
86
- Something missing? The generator keeps a report of every upstream member it
87
- left alone, with the reason. `RESVG_NAPI_CODEGEN_LOG=1 cargo build` prints it,
88
- and [CONTRIBUTING.md](CONTRIBUTING.md) explains what the reasons mean.
141
+ ### Photo card — images and glyphs
142
+
143
+ ![photo card](https://raw.githubusercontent.com/p8nut/resvg-napi/main/demo/assets/photo-card.png)
144
+
145
+ Everything a card does with raster images and type, and nothing it does with
146
+ data.
147
+
148
+ ### Badge sheet — one template, stamped per record
149
+
150
+ ![badge sheet](https://raw.githubusercontent.com/p8nut/resvg-napi/main/demo/assets/badge-sheet.png)
151
+
152
+ Pulls `badge.svg` in as a partial and repeats it. A guided tour, where every zone
153
+ of the page is one Liquid capability.
154
+
155
+ `sheet.svg` covers the data half — pagination, grouping, theming, and
156
+ `--lang fr` driving `<switch systemLanguage>`, which usvg picks nothing from
157
+ without a language.
89
158
 
90
159
  ## Conformance
91
160
 
@@ -139,7 +208,6 @@ generated shims and test run are a different code path. The other eleven build
139
208
  on the merge, on a `v*` tag, on a manual dispatch, or on a pull request labelled
140
209
  `full-matrix` when the proof is wanted before merging rather than after.
141
210
 
142
-
143
211
  ## Fitting text to a width
144
212
 
145
213
  An Illustrator template says how wide a field may be on the element itself
@@ -174,7 +242,7 @@ left untouched, and a constraint that cannot be honoured says why: no `id` to
174
242
  measure by, no visible extent, or an unusable width. It compresses glyphs rather
175
243
  than reducing `font-size` — the choice the templates already made.
176
244
 
177
- ## Diagnostics
245
+ ## Warnings and logs
178
246
 
179
247
  usvg and resvg report every recoverable problem through the `log` crate —
180
248
  unparsable values, skipped shapes, unmatched font families, images that could not
@@ -193,12 +261,50 @@ takeLogs()
193
261
  `takeLogs()` drains the buffer, which is capped at 500 entries so a pathological
194
262
  document cannot grow it without end.
195
263
 
196
- ## Elsewhere
264
+ ## Troubleshooting
265
+
266
+ **A font family did not apply.** `pendingFonts()` on the parsed document names
267
+ the families it asked for and did not get, and `faceOf(glyph)` names the face
268
+ that was used in their place. Supply them with
269
+ `loadFontFile(path)` or `loadFontData(buffer)` before rendering. Under WASI,
270
+ `loadSystemFonts()` finds nothing inside the sandbox, so every face has to be
271
+ loaded by hand.
272
+
273
+ **An image did not appear.** `pendingImages()` names the hrefs that could not be
274
+ resolved.
275
+
276
+ **A measurement disagrees with the numbers in the file.** Bounding boxes are in
277
+ the document's own units and usvg normalises the tree, so they are not canvas
278
+ pixels. See [Fitting text to a width](#fitting-text-to-a-width) for the arithmetic.
279
+
280
+ **Something was dropped and nothing said so.** usvg reports it through the `log`
281
+ crate and nothing consumes that by default — see [Warnings and
282
+ logs](#warnings-and-logs).
283
+
284
+ **A member is missing from the TypeScript surface.** The generator keeps a report
285
+ of every upstream member it left alone, with the reason.
286
+ `RESVG_NAPI_CODEGEN_LOG=1 cargo build` prints it, and
287
+ [CONTRIBUTING.md](CONTRIBUTING.md) explains what the reasons mean.
288
+
289
+ ## Demo
290
+
291
+ A browser proof bench for the same bindings compiled to WASI: Liquid templating,
292
+ an array editor, per-element export, live diagnostics. Deployed from `main` at
293
+ **[p8nut.github.io/resvg-napi](https://p8nut.github.io/resvg-napi/)**, and it
294
+ runs locally too:
295
+
296
+ ```bash
297
+ npm run demo
298
+ ```
299
+
300
+ ![the proof bench](https://raw.githubusercontent.com/p8nut/resvg-napi/main/demo/assets/screenshot.png)
301
+
302
+ [demo/README.md](demo/README.md) describes the page.
303
+
304
+ ## Contributing
197
305
 
198
- - **[demo/](demo/README.md)** — a browser proof bench: Liquid templating, an
199
- array editor, per-element export, live diagnostics. `npm run demo`.
200
- - **[CONTRIBUTING.md](CONTRIBUTING.md)** — how the bindings are generated, and
201
- what to read before touching `build.rs`.
306
+ [CONTRIBUTING.md](CONTRIBUTING.md) — how the bindings are generated, and what to
307
+ read before touching the generator.
202
308
 
203
309
  ## Licence
204
310