@suzhou-numerals/raster 0.0.0-stage → 0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Karl Norling
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software or 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,23 @@
1
- # Temporary Holding Version
1
+ # @suzhou-numerals/raster
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Render any number as [Suzhou numerals](https://en.wikipedia.org/wiki/Suzhou_numerals) in PNG, JPEG or WebP, using [sharp](https://sharp.pixelplumbing.com/).
4
+
5
+ ```sh
6
+ npm install @suzhou-numerals/raster
7
+ ```
8
+
9
+ ```js
10
+ import { toImage } from '@suzhou-numerals/raster';
11
+ import { writeFile } from 'node:fs/promises';
12
+
13
+ await writeFile('1993.png', await toImage(1993));
14
+ await writeFile('pi.webp', await toImage('3.14159', { format: 'webp', size: 400 }));
15
+ ```
16
+
17
+ `toImage` takes every option of `toSvg` from [`@suzhou-numerals/core`](https://www.npmjs.com/package/@suzhou-numerals/core), plus `format` (`png`, `jpeg`, `jpg` or `webp`; default `png`) and `quality` (1–100). Colours default to black, and JPEG gets a white background unless you pass one.
18
+
19
+ See the [docs](https://karlnorling.github.io/suzhou-numerals/packages/raster/).
20
+
21
+ ## License
22
+
23
+ MIT
@@ -0,0 +1,16 @@
1
+ import { type NumberInput, type SvgOptions } from '@suzhou-numerals/core';
2
+ export type RasterFormat = 'png' | 'jpeg' | 'jpg' | 'webp';
3
+ export interface RasterOptions extends SvgOptions {
4
+ /** Default `png`. */
5
+ format?: RasterFormat;
6
+ /** JPEG and WebP quality from 1 to 100. Uses sharp's default when left out. */
7
+ quality?: number;
8
+ }
9
+ /**
10
+ * Renders a number as a PNG, JPEG or WebP image of Suzhou numerals.
11
+ *
12
+ * Rasterises with sharp. The colour defaults to black, and JPEG,
13
+ * which has no transparency, gets a white background unless you pass one.
14
+ */
15
+ export declare function toImage(value: NumberInput, options?: RasterOptions): Promise<Buffer>;
16
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,38 @@
1
+ import { toSvg } from '@suzhou-numerals/core';
2
+ /**
3
+ * Renders a number as a PNG, JPEG or WebP image of Suzhou numerals.
4
+ *
5
+ * Rasterises with sharp. The colour defaults to black, and JPEG,
6
+ * which has no transparency, gets a white background unless you pass one.
7
+ */
8
+ export async function toImage(value, options = {}) {
9
+ const { format = 'png', quality, ...svgOptions } = options;
10
+ const sharp = await loadSharp();
11
+ const isJpeg = format === 'jpeg' || format === 'jpg';
12
+ const svg = toSvg(value, {
13
+ ...svgOptions,
14
+ color: svgOptions.color ?? '#000000',
15
+ background: svgOptions.background ?? (isJpeg ? '#ffffff' : undefined),
16
+ });
17
+ const image = sharp(Buffer.from(svg));
18
+ // Edge pixels can come out partly transparent when the size isn't a whole number of
19
+ // pixels, so JPEG, which has no alpha, is flattened onto white rather than black.
20
+ if (isJpeg)
21
+ return image.flatten({ background: '#ffffff' }).jpeg({ quality }).toBuffer();
22
+ if (format === 'webp')
23
+ return image.webp({ quality }).toBuffer();
24
+ if (format === 'png')
25
+ return image.png().toBuffer();
26
+ throw new TypeError(`Unsupported format: ${String(format)}`);
27
+ }
28
+ async function loadSharp() {
29
+ try {
30
+ return (await import('sharp')).default;
31
+ }
32
+ catch (error) {
33
+ throw new Error('@suzhou-numerals/raster could not load sharp. Reinstall it, or see https://sharp.pixelplumbing.com/install', {
34
+ cause: error,
35
+ });
36
+ }
37
+ }
38
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,63 @@
1
1
  {
2
2
  "name": "@suzhou-numerals/raster",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.0",
4
+ "description": "Render any number as Suzhou numerals in PNG, JPEG or WebP",
5
+ "keywords": [
6
+ "chinese",
7
+ "decimal",
8
+ "hangzhou-numerals",
9
+ "huama",
10
+ "jpeg",
11
+ "number",
12
+ "numerals",
13
+ "png",
14
+ "sharp",
15
+ "suzhou",
16
+ "suzhou-numerals",
17
+ "webp"
18
+ ],
19
+ "homepage": "https://karlnorling.github.io/suzhou-numerals/",
20
+ "bugs": {
21
+ "url": "https://github.com/karlnorling/suzhou-numerals/issues"
22
+ },
23
+ "license": "MIT",
24
+ "author": {
25
+ "name": "Karl Norling",
26
+ "url": "https://github.com/karlnorling"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/karlnorling/suzhou-numerals.git",
31
+ "directory": "packages/@suzhou-numerals/raster"
32
+ },
33
+ "files": [
34
+ "dist",
35
+ "!dist/**/*.map",
36
+ "!dist/**/*.tsbuildinfo",
37
+ "README.md",
38
+ "LICENSE"
39
+ ],
40
+ "type": "module",
41
+ "sideEffects": false,
42
+ "exports": {
43
+ ".": {
44
+ "types": "./dist/index.d.ts",
45
+ "default": "./dist/index.js"
46
+ },
47
+ "./package.json": "./package.json"
48
+ },
49
+ "scripts": {
50
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
51
+ "prepack": "yarn build"
52
+ },
53
+ "dependencies": {
54
+ "@suzhou-numerals/core": "workspace:^",
55
+ "sharp": "^0.35.5"
56
+ },
57
+ "devDependencies": {
58
+ "typescript": "^6.0.2"
59
+ },
60
+ "engines": {
61
+ "node": ">= 20.0.0"
62
+ }
63
+ }