@versatiles/png-renderer 0.0.0 → 2.0.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/LICENSE +21 -0
- package/README.md +117 -1
- package/dist/index.d.ts +2764 -0
- package/dist/index.js +12502 -0
- package/package.json +44 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 VersaTiles
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,119 @@
|
|
|
1
|
+
[](https://www.npmjs.com/package/@versatiles/png-renderer)
|
|
2
|
+
[](https://github.com/versatiles-org/versatiles-svg-renderer/actions/workflows/ci.yml)
|
|
3
|
+
[](https://github.com/versatiles-org/versatiles-svg-renderer/blob/main/LICENSE)
|
|
4
|
+
|
|
1
5
|
# @versatiles/png-renderer
|
|
2
6
|
|
|
3
|
-
|
|
7
|
+
Renders MapLibre styles as PNG in Node.js, with no browser and no SVG rasterizer involved. Satellite and other raster tiles in WebP work too.
|
|
8
|
+
|
|
9
|
+
It also exports `renderToSVG`, so one package covers both formats.
|
|
10
|
+
|
|
11
|
+
**[API documentation](https://versatiles.org/versatiles-svg-renderer/doc-typescript/)** · **[Visual comparison with MapLibre](https://versatiles.org/versatiles-svg-renderer/e2e/report.html)**
|
|
12
|
+
|
|
13
|
+
> [!TIP]
|
|
14
|
+
> **Only need SVG, or want to avoid a native dependency?** Use [`@versatiles/svg-renderer`](https://www.npmjs.com/package/@versatiles/svg-renderer). It has the same `renderToSVG`, runs in the browser too, and contains no native code.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @versatiles/png-renderer
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The package draws with [`@napi-rs/canvas`](https://www.npmjs.com/package/@napi-rs/canvas) (Skia), which npm installs with it, including the prebuilt binary for your platform. There is no install script and nothing to compile. The package is ESM and needs Node.js 22 or later.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { renderToPNG } from '@versatiles/png-renderer';
|
|
28
|
+
import { inlineSources, osm } from '@versatiles/style';
|
|
29
|
+
import { writeFile } from 'node:fs/promises';
|
|
30
|
+
|
|
31
|
+
// inlineSources() resolves the style's TileJSON sources into tile URLs, which the
|
|
32
|
+
// renderer needs. Without it, the map comes out empty.
|
|
33
|
+
const style = await inlineSources(osm({ theme: 'colorful' }));
|
|
34
|
+
|
|
35
|
+
const png = await renderToPNG({
|
|
36
|
+
style,
|
|
37
|
+
width: 800,
|
|
38
|
+
height: 600,
|
|
39
|
+
lon: 13.4,
|
|
40
|
+
lat: 52.5,
|
|
41
|
+
zoom: 10,
|
|
42
|
+
scale: 2, // 1600 × 1200 pixels
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
await writeFile('map.png', png);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The same style, view and options also work with `renderToSVG`, imported from this package:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { renderToSVG } from '@versatiles/png-renderer';
|
|
52
|
+
|
|
53
|
+
const svg = await renderToSVG({ style, width: 800, height: 600, lon: 13.4, lat: 52.5, zoom: 10 });
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
> [!IMPORTANT]
|
|
57
|
+
> The style's sources must list their tile URLs directly (`"tiles": [...]`). A source that only points at a TileJSON document (`"url": ".../tiles.json"`) is **not fetched, and the map comes out empty without an error**. Styles built with `@versatiles/style` are in that form, so pass them through its `inlineSources()` as above.
|
|
58
|
+
|
|
59
|
+
### Labels in the style's own fonts
|
|
60
|
+
|
|
61
|
+
A style only _names_ its fonts (`"text-font": ["noto_sans_regular"]`). Map each name to a font file (TTF, OTF, WOFF or WOFF2) with `fonts`. A name left unmapped falls back to a font installed on the machine.
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
// The VersaTiles styles use Noto Sans, e.g. from `npm install @fontsource/noto-sans`.
|
|
65
|
+
const files = 'node_modules/@fontsource/noto-sans/files';
|
|
66
|
+
|
|
67
|
+
const png = await renderToPNG({
|
|
68
|
+
style,
|
|
69
|
+
lon: 13.4,
|
|
70
|
+
lat: 52.5,
|
|
71
|
+
zoom: 14,
|
|
72
|
+
renderLabels: true,
|
|
73
|
+
fonts: {
|
|
74
|
+
noto_sans_regular: `${files}/noto-sans-latin-400-normal.woff2`,
|
|
75
|
+
noto_sans_bold: `${files}/noto-sans-latin-700-normal.woff2`,
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## API
|
|
81
|
+
|
|
82
|
+
### `renderToPNG(options): Promise<Uint8Array>`
|
|
83
|
+
|
|
84
|
+
Takes every option of [`renderToSVG`](https://github.com/versatiles-org/versatiles-svg-renderer/blob/main/packages/svg-renderer/README.md#api) (`style`, `width`, `height`, `lon`, `lat`, `zoom`, `renderLabels`), plus:
|
|
85
|
+
|
|
86
|
+
| Option | Type | Default | Description |
|
|
87
|
+
| ------- | ------------------------ | ------- | ---------------------------------------------------------------------------------------------------- |
|
|
88
|
+
| `scale` | `number` | `1` | Pixel density: the image is `width × scale` by `height × scale` pixels. Use `2` for sharp output. |
|
|
89
|
+
| `fonts` | `Record<string, string>` | — | Font files for labels, by the `text-font` name the style uses: `{ noto_sans_regular: 'path.woff2' }` |
|
|
90
|
+
|
|
91
|
+
The result is typed as a `Uint8Array` so the package does not require Node's type definitions; at runtime it is a `Buffer` and can be written with `fs.writeFile` as is.
|
|
92
|
+
|
|
93
|
+
Label rendering has the same limitations as in SVG output: [see `renderLabels`](https://github.com/versatiles-org/versatiles-svg-renderer/blob/main/packages/svg-renderer/README.md#about-renderlabels).
|
|
94
|
+
|
|
95
|
+
### `renderToSVG(options): Promise<string>`
|
|
96
|
+
|
|
97
|
+
The same function as in [`@versatiles/svg-renderer`](https://github.com/versatiles-org/versatiles-svg-renderer/blob/main/packages/svg-renderer/README.md#api).
|
|
98
|
+
|
|
99
|
+
## Platforms
|
|
100
|
+
|
|
101
|
+
`@napi-rs/canvas` ships prebuilt binaries for:
|
|
102
|
+
|
|
103
|
+
- macOS: x64, arm64
|
|
104
|
+
- Linux: x64 and arm64 (glibc and musl, e.g. Debian and Alpine images), arm (glibc), riscv64 (glibc)
|
|
105
|
+
- Windows: x64, arm64
|
|
106
|
+
- Android: arm64
|
|
107
|
+
|
|
108
|
+
### "PNG rendering needs the native module @napi-rs/canvas …"
|
|
109
|
+
|
|
110
|
+
This error means the binary for the platform was not installed. It names the platform and the missing package. The usual causes:
|
|
111
|
+
|
|
112
|
+
- **Dependencies were installed with `--omit=optional`** (or `--no-optional`). npm installs the platform binaries as optional dependencies.
|
|
113
|
+
- **`node_modules` was installed on another OS or CPU architecture and copied**, e.g. installed on macOS and then copied into a Linux Docker image. Install the dependencies inside the image instead (`npm ci` in the Dockerfile).
|
|
114
|
+
|
|
115
|
+
`renderToSVG` keeps working in either case, because the binary is only loaded when a PNG is rendered.
|
|
116
|
+
|
|
117
|
+
## License
|
|
118
|
+
|
|
119
|
+
MIT. Part of [VersaTiles](https://github.com/versatiles-org/versatiles-svg-renderer): source code, issues and the changelog are on GitHub.
|