@corbet-labs/cink 0.1.0 → 0.1.2

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.
@@ -0,0 +1,105 @@
1
+ # Functional Source License, Version 1.1, ALv2 Future License
2
+
3
+ ## Abbreviation
4
+
5
+ FSL-1.1-ALv2
6
+
7
+ ## Notice
8
+
9
+ Copyright 2026 Julian Y. Richard Corbet
10
+
11
+ ## Terms and Conditions
12
+
13
+ ### Licensor ("We")
14
+
15
+ The party offering the Software under these Terms and Conditions.
16
+
17
+ ### The Software
18
+
19
+ The "Software" is each version of the software that we make available under
20
+ these Terms and Conditions, as indicated by our inclusion of these Terms and
21
+ Conditions with the Software.
22
+
23
+ ### License Grant
24
+
25
+ Subject to your compliance with this License Grant and the Patents,
26
+ Redistribution and Trademark clauses below, we hereby grant you the right to
27
+ use, copy, modify, create derivative works, publicly perform, publicly display
28
+ and redistribute the Software for any Permitted Purpose identified below.
29
+
30
+ ### Permitted Purpose
31
+
32
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
33
+ means making the Software available to others in a commercial product or
34
+ service that:
35
+
36
+ 1. substitutes for the Software;
37
+
38
+ 2. substitutes for any other product or service we offer using the Software
39
+ that exists as of the date we make the Software available; or
40
+
41
+ 3. offers the same or substantially similar functionality as the Software.
42
+
43
+ Permitted Purposes specifically include using the Software:
44
+
45
+ 1. for your internal use and access;
46
+
47
+ 2. for non-commercial education;
48
+
49
+ 3. for non-commercial research; and
50
+
51
+ 4. in connection with professional services that you provide to a licensee
52
+ using the Software in accordance with these Terms and Conditions.
53
+
54
+ ### Patents
55
+
56
+ To the extent your use for a Permitted Purpose would necessarily infringe our
57
+ patents, the license grant above includes a license under our patents. If you
58
+ make a claim against any party that the Software infringes or contributes to
59
+ the infringement of any patent, then your patent license to the Software ends
60
+ immediately.
61
+
62
+ ### Redistribution
63
+
64
+ The Terms and Conditions apply to all copies, modifications and derivatives of
65
+ the Software.
66
+
67
+ If you redistribute any copies, modifications or derivatives of the Software,
68
+ you must include a copy of or a link to these Terms and Conditions and not
69
+ remove any copyright notices provided in or with the Software.
70
+
71
+ ### Disclaimer
72
+
73
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
74
+ IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
75
+ PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
76
+
77
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
78
+ SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
79
+ EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
80
+
81
+ ### Trademarks
82
+
83
+ Except for displaying the License Details and identifying us as the origin of
84
+ the Software, you have no right under these Terms and Conditions to use our
85
+ trademarks, trade names, service marks or product names.
86
+
87
+ ## Grant of Future License
88
+
89
+ We hereby irrevocably grant you an additional license to use the Software under
90
+ the Apache License, Version 2.0 that is effective on the second anniversary of
91
+ the date we make the Software available. On or after that date, you may use the
92
+ Software under the Apache License, Version 2.0, in which case the following
93
+ will apply:
94
+
95
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
96
+ this file except in compliance with the License.
97
+
98
+ You may obtain a copy of the License at
99
+
100
+ http://www.apache.org/licenses/LICENSE-2.0
101
+
102
+ Unless required by applicable law or agreed to in writing, software distributed
103
+ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
104
+ CONDITIONS OF ANY KIND, either express or implied. See the License for the
105
+ specific language governing permissions and limitations under the License.
package/README.md CHANGED
@@ -1,27 +1,99 @@
1
- # @corbet-labs/cink
1
+ # cink
2
2
 
3
- Handwritten signature image rendering for formal correspondence. Pure port
4
- of the [cink](https://github.com/corbet-labs/cink) Rust crate: zero
5
- dependencies, zero Node APIs, synchronous, no I/O. Pixels, not
6
- signatures — no cryptography, no identity, no legal ceremony.
3
+ **Prepare handwritten signature images for correspondence.**
7
4
 
8
- ```ts
9
- import { normalize, signatureSize, imageSnippet } from '@corbet-labs/cink';
5
+ [![crates.io](https://img.shields.io/crates/v/cink.svg)](https://crates.io/crates/cink) [![npm](https://img.shields.io/npm/v/@corbet-labs/cink.svg)](https://www.npmjs.com/package/@corbet-labs/cink) [![Rust API](https://docs.rs/cink/badge.svg)](https://docs.rs/cink) [![CI](https://github.com/corbet-labs/cink/actions/workflows/ci.yml/badge.svg)](https://github.com/corbet-labs/cink/actions/workflows/ci.yml)
10
6
 
11
- const img = normalize(dataUrl); // null when unusable
12
- const [w, h] = signatureSize(img, 31.5, 120);
13
- const snippet = imageSnippet('/signature.png', h, w === 31.5 ? undefined : w);
7
+ Recognize PNG, JPEG, and SVG input, inspect raster dimensions, calculate an aspect-preserving size, and generate a Typst image call. Runs without an image-decoding library.
8
+
9
+ ```js
10
+ import { imageSnippet } from '@corbet-labs/cink';
11
+
12
+ imageSnippet('/signature.png', 31.5);
13
+ // #image("/signature.png", height: 31.5pt)
14
+ ```
15
+
16
+ ## Install
17
+
18
+ | Environment | Command |
19
+ | --- | --- |
20
+ | Rust / Cargo | `cargo add cink` |
21
+ | Node.js / npm | `npm install @corbet-labs/cink` |
22
+ | pnpm | `pnpm add @corbet-labs/cink` |
23
+ | Yarn | `yarn add @corbet-labs/cink` |
24
+ | Bun | `bun add @corbet-labs/cink` |
25
+ | Deno | `deno add npm:@corbet-labs/cink` |
26
+
27
+ The 0.1.2 JavaScript distribution includes compiled ESM, CommonJS,
28
+ TypeScript declarations, and a standalone browser module. Node.js 20+ is
29
+ supported; no TypeScript loader is required.
30
+
31
+ ```js
32
+ // CommonJS
33
+ const { imageSnippet } = require('@corbet-labs/cink');
14
34
  ```
15
35
 
16
- PNG and JPEG dimensions are read from headers, never decoded; SVG passes
17
- through dimension-less. WebP, GIF, corrupt, and empty inputs are rejected.
18
- Oversized rasters are reported (`exceedsLimits`, `scaleToFit`), never
19
- silently resampled.
36
+ ```html
37
+ <script type="module">
38
+ import { imageSnippet } from 'https://cdn.jsdelivr.net/npm/@corbet-labs/cink@0.1.2/dist/browser.js';
39
+ console.log(imageSnippet('/signature.png', 31.5));
40
+ </script>
41
+ ```
42
+
43
+ Python wheels, source distributions, and CLI instructions are in the
44
+ [installation guide](https://github.com/corbet-labs/cink/blob/main/docs/installation.md).
45
+ JSR publication and Typst availability are listed there explicitly.
46
+
47
+ ## Rust
48
+
49
+ ```rust
50
+ use cink::image_snippet;
51
+
52
+ assert_eq!(image_snippet("/signature.png", 31.5, None), "#image(\"/signature.png\", height: 31.5pt)");
53
+ ```
54
+
55
+ ## Python
56
+
57
+ ```python
58
+ from cink import image_snippet
59
+
60
+ assert image_snippet("/signature.png", 31.5) == '#image("/signature.png", height: 31.5pt)'
61
+ ```
62
+
63
+ ## API
64
+
65
+ | JavaScript / Python or Rust | Purpose |
66
+ | --- | --- |
67
+ | `normalize` | Recognize a base64 image or data URL |
68
+ | `exceedsLimits` / `exceeds_limits` | Check a raster pixel budget |
69
+ | `scaleToFit` / `scale_to_fit` | Calculate a downscaling factor |
70
+ | `signatureSize` / `signature_size` | Fit dimensions to a point size |
71
+ | `imageSnippet` / `image_snippet` | Generate a Typst image call |
72
+
73
+ PNG and JPEG dimensions are read from headers; this is not a full raster integrity check. SVG passes through without sanitization or known dimensions. Unrecognized headers and unsupported formats return `null`/`None`. Resampling and placement belong to the caller. These are handwriting images, not cryptographic or electronic signatures.
74
+
75
+ ## Correspondence family
76
+
77
+ | Library | Responsibility |
78
+ | --- | --- |
79
+ | [cletter](https://github.com/corbet-labs/cletter) | Compose the correspondence helpers |
80
+ | [cgreet](https://github.com/corbet-labs/cgreet) | German salutations and titles |
81
+ | [cfarewell](https://github.com/corbet-labs/cfarewell) | Locale-specific closings |
82
+ | [cdate](https://github.com/corbet-labs/cdate) | Calendar-date formatting |
83
+ | [cink](https://github.com/corbet-labs/cink) | Handwritten signature images |
84
+
85
+
86
+ ## Development
87
+
88
+ Behavior is defined by [the locale tables](https://github.com/corbet-labs/cink/tree/main/tables)
89
+ and [shared conformance vectors](https://github.com/corbet-labs/cink/tree/main/tests/vectors).
90
+ Rust, JavaScript, and Python run the same vectors. CI also exercises installed
91
+ JavaScript tarballs across package managers and operating systems, and tests
92
+ Python wheels outside their source package.
20
93
 
21
- Run `bun ./scripts/conformance.mts` and `tsc --noEmit -p ./tsconfig.json`
22
- before pushing.
94
+ See [the release guide](https://github.com/corbet-labs/cink/blob/main/docs/releasing.md)
95
+ for generation, verification, and publication commands.
23
96
 
24
97
  ## License
25
98
 
26
- FSL-1.1-ALv2. Each published version becomes available under Apache-2.0
27
- two years after publication.
99
+ FSL-1.1-ALv2. Each release becomes available under Apache-2.0 two years after publication. See [the full license](https://github.com/corbet-labs/cink/blob/main/LICENSES/FSL-1.1-ALv2.txt) for permitted use.
@@ -0,0 +1,167 @@
1
+ // src/generated/tables.ts
2
+ var DEFAULTS = { max_pixels: 1600, formats: ["png", "jpeg", "svg"] };
3
+
4
+ // src/index.ts
5
+ var defaults = DEFAULTS;
6
+ function defaultMaxPixels() {
7
+ return defaults.max_pixels;
8
+ }
9
+ function supportedFormats() {
10
+ return [...defaults.formats].sort();
11
+ }
12
+ var PNG_MAGIC = [137, 80, 78, 71, 13, 10, 26, 10];
13
+ function pngDimensions(bytes) {
14
+ if (bytes.length < 24)
15
+ return null;
16
+ for (let i = 0;i < 8; i++)
17
+ if (bytes[i] !== PNG_MAGIC[i])
18
+ return null;
19
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
20
+ if (view.getUint32(8) !== 13)
21
+ return null;
22
+ if (view.getUint32(12) !== 1229472850)
23
+ return null;
24
+ const width = view.getUint32(16);
25
+ const height = view.getUint32(20);
26
+ if (width === 0 || height === 0)
27
+ return null;
28
+ return [width, height];
29
+ }
30
+ var SOF_MARKERS = new Set([
31
+ 192,
32
+ 193,
33
+ 194,
34
+ 195,
35
+ 197,
36
+ 198,
37
+ 199,
38
+ 201,
39
+ 202,
40
+ 203,
41
+ 205,
42
+ 206,
43
+ 207
44
+ ]);
45
+ function jpegDimensions(bytes) {
46
+ if (bytes.length < 2 || bytes[0] !== 255 || bytes[1] !== 216)
47
+ return null;
48
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
49
+ let pos = 2;
50
+ while (pos < bytes.length) {
51
+ while (pos < bytes.length && bytes[pos] === 255)
52
+ pos++;
53
+ if (pos >= bytes.length)
54
+ return null;
55
+ const marker = bytes[pos];
56
+ pos++;
57
+ if (SOF_MARKERS.has(marker)) {
58
+ if (pos + 7 > bytes.length)
59
+ return null;
60
+ const height = view.getUint16(pos + 3);
61
+ const width = view.getUint16(pos + 5);
62
+ if (width === 0 || height === 0)
63
+ return null;
64
+ return [width, height];
65
+ }
66
+ if (marker === 217 || marker === 218)
67
+ return null;
68
+ if (marker === 0 || marker === 1 || marker >= 208 && marker <= 215)
69
+ continue;
70
+ if (pos + 2 > bytes.length)
71
+ return null;
72
+ const len = view.getUint16(pos);
73
+ if (len < 2)
74
+ return null;
75
+ pos += len;
76
+ }
77
+ return null;
78
+ }
79
+ function sniff(bytes) {
80
+ if (bytes.length >= 8 && PNG_MAGIC.every((b, i) => bytes[i] === b)) {
81
+ const dims = pngDimensions(bytes);
82
+ if (dims === null)
83
+ return null;
84
+ return { mime: "png", width: dims[0], height: dims[1], bytes };
85
+ }
86
+ if (bytes.length >= 2 && bytes[0] === 255 && bytes[1] === 216) {
87
+ const dims = jpegDimensions(bytes);
88
+ if (dims === null)
89
+ return null;
90
+ return { mime: "jpeg", width: dims[0], height: dims[1], bytes };
91
+ }
92
+ let start = 0;
93
+ while (start < bytes.length && /\s/.test(String.fromCharCode(bytes[start])))
94
+ start++;
95
+ const head = String.fromCharCode(...bytes.slice(start, start + 4));
96
+ if (head === "<svg")
97
+ return { mime: "svg", width: null, height: null, bytes };
98
+ return null;
99
+ }
100
+ function decodeBase64(payload) {
101
+ if (payload === "")
102
+ return null;
103
+ try {
104
+ const text = atob(payload);
105
+ const bytes = new Uint8Array(text.length);
106
+ for (let i = 0;i < text.length; i++)
107
+ bytes[i] = text.charCodeAt(i);
108
+ return bytes.length > 0 ? bytes : null;
109
+ } catch {
110
+ return null;
111
+ }
112
+ }
113
+ function normalize(input) {
114
+ let payload = input;
115
+ const comma = input.indexOf(",");
116
+ if (comma >= 0 && input.startsWith("data:")) {
117
+ const meta = input.slice(0, comma);
118
+ if (!meta.includes(";base64"))
119
+ return null;
120
+ payload = input.slice(comma + 1);
121
+ }
122
+ const bytes = decodeBase64(payload);
123
+ if (bytes === null)
124
+ return null;
125
+ return sniff(bytes);
126
+ }
127
+ function exceedsLimits(image, maxPixels) {
128
+ if (image.width === null || image.height === null)
129
+ return false;
130
+ return image.width * image.height > maxPixels;
131
+ }
132
+ function scaleToFit(image, maxPixels) {
133
+ if (image.width === null || image.height === null)
134
+ return 1;
135
+ const pixels = image.width * image.height;
136
+ if (pixels <= maxPixels)
137
+ return 1;
138
+ return Math.sqrt(maxPixels / pixels);
139
+ }
140
+ function signatureSize(image, heightPt, maxWidthPt) {
141
+ if (image.width === null || image.height === null)
142
+ return null;
143
+ const natural = heightPt * image.width / image.height;
144
+ if (maxWidthPt !== undefined && natural > maxWidthPt) {
145
+ const scale = maxWidthPt / natural;
146
+ return [maxWidthPt, heightPt * scale];
147
+ }
148
+ return [natural, heightPt];
149
+ }
150
+ function escapePath(path) {
151
+ return path.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
152
+ }
153
+ function imageSnippet(path, heightPt, widthPt) {
154
+ const safe = escapePath(path);
155
+ if (widthPt === undefined)
156
+ return `#image("${safe}", height: ${heightPt}pt)`;
157
+ return `#image("${safe}", height: ${heightPt}pt, width: ${widthPt}pt)`;
158
+ }
159
+ export {
160
+ supportedFormats,
161
+ signatureSize,
162
+ scaleToFit,
163
+ normalize,
164
+ imageSnippet,
165
+ exceedsLimits,
166
+ defaultMaxPixels
167
+ };
package/dist/index.cjs ADDED
@@ -0,0 +1,209 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ function __accessProp(key) {
6
+ return this[key];
7
+ }
8
+ var __toCommonJS = (from) => {
9
+ var entry = (__moduleCache ??= new WeakMap).get(from), desc;
10
+ if (entry)
11
+ return entry;
12
+ entry = __defProp({}, "__esModule", { value: true });
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (var key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(entry, key))
16
+ __defProp(entry, key, {
17
+ get: __accessProp.bind(from, key),
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ }
21
+ __moduleCache.set(from, entry);
22
+ return entry;
23
+ };
24
+ var __moduleCache;
25
+ var __returnValue = (v) => v;
26
+ function __exportSetter(name, newValue) {
27
+ this[name] = __returnValue.bind(null, newValue);
28
+ }
29
+ var __export = (target, all) => {
30
+ for (var name in all)
31
+ __defProp(target, name, {
32
+ get: all[name],
33
+ enumerable: true,
34
+ configurable: true,
35
+ set: __exportSetter.bind(all, name)
36
+ });
37
+ };
38
+
39
+ // src/index.ts
40
+ var exports_src = {};
41
+ __export(exports_src, {
42
+ supportedFormats: () => supportedFormats,
43
+ signatureSize: () => signatureSize,
44
+ scaleToFit: () => scaleToFit,
45
+ normalize: () => normalize,
46
+ imageSnippet: () => imageSnippet,
47
+ exceedsLimits: () => exceedsLimits,
48
+ defaultMaxPixels: () => defaultMaxPixels
49
+ });
50
+ module.exports = __toCommonJS(exports_src);
51
+
52
+ // src/generated/tables.ts
53
+ var DEFAULTS = { max_pixels: 1600, formats: ["png", "jpeg", "svg"] };
54
+
55
+ // src/index.ts
56
+ var defaults = DEFAULTS;
57
+ function defaultMaxPixels() {
58
+ return defaults.max_pixels;
59
+ }
60
+ function supportedFormats() {
61
+ return [...defaults.formats].sort();
62
+ }
63
+ var PNG_MAGIC = [137, 80, 78, 71, 13, 10, 26, 10];
64
+ function pngDimensions(bytes) {
65
+ if (bytes.length < 24)
66
+ return null;
67
+ for (let i = 0;i < 8; i++)
68
+ if (bytes[i] !== PNG_MAGIC[i])
69
+ return null;
70
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
71
+ if (view.getUint32(8) !== 13)
72
+ return null;
73
+ if (view.getUint32(12) !== 1229472850)
74
+ return null;
75
+ const width = view.getUint32(16);
76
+ const height = view.getUint32(20);
77
+ if (width === 0 || height === 0)
78
+ return null;
79
+ return [width, height];
80
+ }
81
+ var SOF_MARKERS = new Set([
82
+ 192,
83
+ 193,
84
+ 194,
85
+ 195,
86
+ 197,
87
+ 198,
88
+ 199,
89
+ 201,
90
+ 202,
91
+ 203,
92
+ 205,
93
+ 206,
94
+ 207
95
+ ]);
96
+ function jpegDimensions(bytes) {
97
+ if (bytes.length < 2 || bytes[0] !== 255 || bytes[1] !== 216)
98
+ return null;
99
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
100
+ let pos = 2;
101
+ while (pos < bytes.length) {
102
+ while (pos < bytes.length && bytes[pos] === 255)
103
+ pos++;
104
+ if (pos >= bytes.length)
105
+ return null;
106
+ const marker = bytes[pos];
107
+ pos++;
108
+ if (SOF_MARKERS.has(marker)) {
109
+ if (pos + 7 > bytes.length)
110
+ return null;
111
+ const height = view.getUint16(pos + 3);
112
+ const width = view.getUint16(pos + 5);
113
+ if (width === 0 || height === 0)
114
+ return null;
115
+ return [width, height];
116
+ }
117
+ if (marker === 217 || marker === 218)
118
+ return null;
119
+ if (marker === 0 || marker === 1 || marker >= 208 && marker <= 215)
120
+ continue;
121
+ if (pos + 2 > bytes.length)
122
+ return null;
123
+ const len = view.getUint16(pos);
124
+ if (len < 2)
125
+ return null;
126
+ pos += len;
127
+ }
128
+ return null;
129
+ }
130
+ function sniff(bytes) {
131
+ if (bytes.length >= 8 && PNG_MAGIC.every((b, i) => bytes[i] === b)) {
132
+ const dims = pngDimensions(bytes);
133
+ if (dims === null)
134
+ return null;
135
+ return { mime: "png", width: dims[0], height: dims[1], bytes };
136
+ }
137
+ if (bytes.length >= 2 && bytes[0] === 255 && bytes[1] === 216) {
138
+ const dims = jpegDimensions(bytes);
139
+ if (dims === null)
140
+ return null;
141
+ return { mime: "jpeg", width: dims[0], height: dims[1], bytes };
142
+ }
143
+ let start = 0;
144
+ while (start < bytes.length && /\s/.test(String.fromCharCode(bytes[start])))
145
+ start++;
146
+ const head = String.fromCharCode(...bytes.slice(start, start + 4));
147
+ if (head === "<svg")
148
+ return { mime: "svg", width: null, height: null, bytes };
149
+ return null;
150
+ }
151
+ function decodeBase64(payload) {
152
+ if (payload === "")
153
+ return null;
154
+ try {
155
+ const text = atob(payload);
156
+ const bytes = new Uint8Array(text.length);
157
+ for (let i = 0;i < text.length; i++)
158
+ bytes[i] = text.charCodeAt(i);
159
+ return bytes.length > 0 ? bytes : null;
160
+ } catch {
161
+ return null;
162
+ }
163
+ }
164
+ function normalize(input) {
165
+ let payload = input;
166
+ const comma = input.indexOf(",");
167
+ if (comma >= 0 && input.startsWith("data:")) {
168
+ const meta = input.slice(0, comma);
169
+ if (!meta.includes(";base64"))
170
+ return null;
171
+ payload = input.slice(comma + 1);
172
+ }
173
+ const bytes = decodeBase64(payload);
174
+ if (bytes === null)
175
+ return null;
176
+ return sniff(bytes);
177
+ }
178
+ function exceedsLimits(image, maxPixels) {
179
+ if (image.width === null || image.height === null)
180
+ return false;
181
+ return image.width * image.height > maxPixels;
182
+ }
183
+ function scaleToFit(image, maxPixels) {
184
+ if (image.width === null || image.height === null)
185
+ return 1;
186
+ const pixels = image.width * image.height;
187
+ if (pixels <= maxPixels)
188
+ return 1;
189
+ return Math.sqrt(maxPixels / pixels);
190
+ }
191
+ function signatureSize(image, heightPt, maxWidthPt) {
192
+ if (image.width === null || image.height === null)
193
+ return null;
194
+ const natural = heightPt * image.width / image.height;
195
+ if (maxWidthPt !== undefined && natural > maxWidthPt) {
196
+ const scale = maxWidthPt / natural;
197
+ return [maxWidthPt, heightPt * scale];
198
+ }
199
+ return [natural, heightPt];
200
+ }
201
+ function escapePath(path) {
202
+ return path.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
203
+ }
204
+ function imageSnippet(path, heightPt, widthPt) {
205
+ const safe = escapePath(path);
206
+ if (widthPt === undefined)
207
+ return `#image("${safe}", height: ${heightPt}pt)`;
208
+ return `#image("${safe}", height: ${heightPt}pt, width: ${widthPt}pt)`;
209
+ }
package/dist/index.js ADDED
@@ -0,0 +1,167 @@
1
+ // src/generated/tables.ts
2
+ var DEFAULTS = { max_pixels: 1600, formats: ["png", "jpeg", "svg"] };
3
+
4
+ // src/index.ts
5
+ var defaults = DEFAULTS;
6
+ function defaultMaxPixels() {
7
+ return defaults.max_pixels;
8
+ }
9
+ function supportedFormats() {
10
+ return [...defaults.formats].sort();
11
+ }
12
+ var PNG_MAGIC = [137, 80, 78, 71, 13, 10, 26, 10];
13
+ function pngDimensions(bytes) {
14
+ if (bytes.length < 24)
15
+ return null;
16
+ for (let i = 0;i < 8; i++)
17
+ if (bytes[i] !== PNG_MAGIC[i])
18
+ return null;
19
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
20
+ if (view.getUint32(8) !== 13)
21
+ return null;
22
+ if (view.getUint32(12) !== 1229472850)
23
+ return null;
24
+ const width = view.getUint32(16);
25
+ const height = view.getUint32(20);
26
+ if (width === 0 || height === 0)
27
+ return null;
28
+ return [width, height];
29
+ }
30
+ var SOF_MARKERS = new Set([
31
+ 192,
32
+ 193,
33
+ 194,
34
+ 195,
35
+ 197,
36
+ 198,
37
+ 199,
38
+ 201,
39
+ 202,
40
+ 203,
41
+ 205,
42
+ 206,
43
+ 207
44
+ ]);
45
+ function jpegDimensions(bytes) {
46
+ if (bytes.length < 2 || bytes[0] !== 255 || bytes[1] !== 216)
47
+ return null;
48
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
49
+ let pos = 2;
50
+ while (pos < bytes.length) {
51
+ while (pos < bytes.length && bytes[pos] === 255)
52
+ pos++;
53
+ if (pos >= bytes.length)
54
+ return null;
55
+ const marker = bytes[pos];
56
+ pos++;
57
+ if (SOF_MARKERS.has(marker)) {
58
+ if (pos + 7 > bytes.length)
59
+ return null;
60
+ const height = view.getUint16(pos + 3);
61
+ const width = view.getUint16(pos + 5);
62
+ if (width === 0 || height === 0)
63
+ return null;
64
+ return [width, height];
65
+ }
66
+ if (marker === 217 || marker === 218)
67
+ return null;
68
+ if (marker === 0 || marker === 1 || marker >= 208 && marker <= 215)
69
+ continue;
70
+ if (pos + 2 > bytes.length)
71
+ return null;
72
+ const len = view.getUint16(pos);
73
+ if (len < 2)
74
+ return null;
75
+ pos += len;
76
+ }
77
+ return null;
78
+ }
79
+ function sniff(bytes) {
80
+ if (bytes.length >= 8 && PNG_MAGIC.every((b, i) => bytes[i] === b)) {
81
+ const dims = pngDimensions(bytes);
82
+ if (dims === null)
83
+ return null;
84
+ return { mime: "png", width: dims[0], height: dims[1], bytes };
85
+ }
86
+ if (bytes.length >= 2 && bytes[0] === 255 && bytes[1] === 216) {
87
+ const dims = jpegDimensions(bytes);
88
+ if (dims === null)
89
+ return null;
90
+ return { mime: "jpeg", width: dims[0], height: dims[1], bytes };
91
+ }
92
+ let start = 0;
93
+ while (start < bytes.length && /\s/.test(String.fromCharCode(bytes[start])))
94
+ start++;
95
+ const head = String.fromCharCode(...bytes.slice(start, start + 4));
96
+ if (head === "<svg")
97
+ return { mime: "svg", width: null, height: null, bytes };
98
+ return null;
99
+ }
100
+ function decodeBase64(payload) {
101
+ if (payload === "")
102
+ return null;
103
+ try {
104
+ const text = atob(payload);
105
+ const bytes = new Uint8Array(text.length);
106
+ for (let i = 0;i < text.length; i++)
107
+ bytes[i] = text.charCodeAt(i);
108
+ return bytes.length > 0 ? bytes : null;
109
+ } catch {
110
+ return null;
111
+ }
112
+ }
113
+ function normalize(input) {
114
+ let payload = input;
115
+ const comma = input.indexOf(",");
116
+ if (comma >= 0 && input.startsWith("data:")) {
117
+ const meta = input.slice(0, comma);
118
+ if (!meta.includes(";base64"))
119
+ return null;
120
+ payload = input.slice(comma + 1);
121
+ }
122
+ const bytes = decodeBase64(payload);
123
+ if (bytes === null)
124
+ return null;
125
+ return sniff(bytes);
126
+ }
127
+ function exceedsLimits(image, maxPixels) {
128
+ if (image.width === null || image.height === null)
129
+ return false;
130
+ return image.width * image.height > maxPixels;
131
+ }
132
+ function scaleToFit(image, maxPixels) {
133
+ if (image.width === null || image.height === null)
134
+ return 1;
135
+ const pixels = image.width * image.height;
136
+ if (pixels <= maxPixels)
137
+ return 1;
138
+ return Math.sqrt(maxPixels / pixels);
139
+ }
140
+ function signatureSize(image, heightPt, maxWidthPt) {
141
+ if (image.width === null || image.height === null)
142
+ return null;
143
+ const natural = heightPt * image.width / image.height;
144
+ if (maxWidthPt !== undefined && natural > maxWidthPt) {
145
+ const scale = maxWidthPt / natural;
146
+ return [maxWidthPt, heightPt * scale];
147
+ }
148
+ return [natural, heightPt];
149
+ }
150
+ function escapePath(path) {
151
+ return path.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
152
+ }
153
+ function imageSnippet(path, heightPt, widthPt) {
154
+ const safe = escapePath(path);
155
+ if (widthPt === undefined)
156
+ return `#image("${safe}", height: ${heightPt}pt)`;
157
+ return `#image("${safe}", height: ${heightPt}pt, width: ${widthPt}pt)`;
158
+ }
159
+ export {
160
+ supportedFormats,
161
+ signatureSize,
162
+ scaleToFit,
163
+ normalize,
164
+ imageSnippet,
165
+ exceedsLimits,
166
+ defaultMaxPixels
167
+ };
@@ -0,0 +1,2 @@
1
+ export declare const PACKAGE_VERSION: string;
2
+ export declare const DEFAULTS: unknown;
@@ -0,0 +1,41 @@
1
+ export interface Defaults {
2
+ max_pixels: number;
3
+ formats: string[];
4
+ }
5
+ export type ImageMime = 'png' | 'jpeg' | 'svg';
6
+ export interface DecodedImage {
7
+ mime: ImageMime;
8
+ width: number | null;
9
+ height: number | null;
10
+ bytes: Uint8Array;
11
+ }
12
+ /** Default oversize threshold in pixels, from `tables/defaults.json`. */
13
+ export declare function defaultMaxPixels(): number;
14
+ /** Accepted image formats, from `tables/defaults.json`. */
15
+ export declare function supportedFormats(): string[];
16
+ /**
17
+ * Validate a signature image given as a `data:` URL or raw base64.
18
+ * Returns `null` for empty, undecodable, corrupt, or unsupported inputs.
19
+ */
20
+ export declare function normalize(input: string): DecodedImage | null;
21
+ /**
22
+ * Whether the raster exceeds a pixel budget. Dimension-less formats (SVG)
23
+ * scale without loss and never exceed.
24
+ */
25
+ export declare function exceedsLimits(image: DecodedImage, maxPixels: number): boolean;
26
+ /**
27
+ * Linear scale factor that fits the image into a pixel budget, never
28
+ * above 1.0 (never upscales). The caller resamples and re-submits.
29
+ */
30
+ export declare function scaleToFit(image: DecodedImage, maxPixels: number): number;
31
+ /**
32
+ * Point size for a target height, preserving aspect ratio, clamped to an
33
+ * optional maximum width. Returns `null` when the image carries no
34
+ * dimensions: the caller must size it.
35
+ */
36
+ export declare function signatureSize(image: DecodedImage, heightPt: number, maxWidthPt?: number): [number, number] | null;
37
+ /**
38
+ * Typst `#image` call for a sized signature. Width is emitted only when a
39
+ * max-width clamp applied; layout and placement stay with the caller.
40
+ */
41
+ export declare function imageSnippet(path: string, heightPt: number, widthPt?: number): string;
@@ -0,0 +1,41 @@
1
+ export interface Defaults {
2
+ max_pixels: number;
3
+ formats: string[];
4
+ }
5
+ export type ImageMime = 'png' | 'jpeg' | 'svg';
6
+ export interface DecodedImage {
7
+ mime: ImageMime;
8
+ width: number | null;
9
+ height: number | null;
10
+ bytes: Uint8Array;
11
+ }
12
+ /** Default oversize threshold in pixels, from `tables/defaults.json`. */
13
+ export declare function defaultMaxPixels(): number;
14
+ /** Accepted image formats, from `tables/defaults.json`. */
15
+ export declare function supportedFormats(): string[];
16
+ /**
17
+ * Validate a signature image given as a `data:` URL or raw base64.
18
+ * Returns `null` for empty, undecodable, corrupt, or unsupported inputs.
19
+ */
20
+ export declare function normalize(input: string): DecodedImage | null;
21
+ /**
22
+ * Whether the raster exceeds a pixel budget. Dimension-less formats (SVG)
23
+ * scale without loss and never exceed.
24
+ */
25
+ export declare function exceedsLimits(image: DecodedImage, maxPixels: number): boolean;
26
+ /**
27
+ * Linear scale factor that fits the image into a pixel budget, never
28
+ * above 1.0 (never upscales). The caller resamples and re-submits.
29
+ */
30
+ export declare function scaleToFit(image: DecodedImage, maxPixels: number): number;
31
+ /**
32
+ * Point size for a target height, preserving aspect ratio, clamped to an
33
+ * optional maximum width. Returns `null` when the image carries no
34
+ * dimensions: the caller must size it.
35
+ */
36
+ export declare function signatureSize(image: DecodedImage, heightPt: number, maxWidthPt?: number): [number, number] | null;
37
+ /**
38
+ * Typst `#image` call for a sized signature. Width is emitted only when a
39
+ * max-width clamp applied; layout and placement stay with the caller.
40
+ */
41
+ export declare function imageSnippet(path: string, heightPt: number, widthPt?: number): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@corbet-labs/cink",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Handwritten signature image rendering for correspondence. Pixels, not signatures. Mirrors the cink Rust crate release line.",
5
5
  "license": "FSL-1.1-ALv2",
6
6
  "publishConfig": {
@@ -9,26 +9,62 @@
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
12
- "url": "https://github.com/corbet-labs/cink.git",
12
+ "url": "git+https://github.com/corbet-labs/cink.git",
13
13
  "directory": "js/@corbet-labs/cink"
14
14
  },
15
15
  "type": "module",
16
- "main": "./src/index.ts",
17
- "types": "./src/index.ts",
16
+ "main": "./dist/index.cjs",
17
+ "types": "./dist/types/index.d.ts",
18
18
  "exports": {
19
- ".": "./src/index.ts"
19
+ ".": {
20
+ "import": {
21
+ "types": "./dist/types/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ },
24
+ "require": {
25
+ "types": "./dist/types/index.d.cts",
26
+ "default": "./dist/index.cjs"
27
+ },
28
+ "default": "./dist/index.js"
29
+ },
30
+ "./browser": {
31
+ "types": "./dist/types/index.d.ts",
32
+ "default": "./dist/browser.js"
33
+ },
34
+ "./package.json": "./package.json"
20
35
  },
21
36
  "files": [
37
+ "dist/**/*",
22
38
  "src/**/*",
23
- "README.md"
39
+ "README.md",
40
+ "LICENSES/**/*"
24
41
  ],
25
42
  "scripts": {
26
- "prepack": "bash ./scripts/sync-assets.sh",
43
+ "prepack": "bun run build",
27
44
  "conformance": "bun ./scripts/conformance.mts",
28
- "typecheck": "tsc --noEmit -p ./tsconfig.json"
45
+ "typecheck": "tsc --noEmit -p ./tsconfig.json",
46
+ "build": "bun ./scripts/build.mjs"
29
47
  },
30
48
  "devDependencies": {
31
49
  "@types/node": "25.8.0",
32
50
  "typescript": "6.0.3"
33
- }
51
+ },
52
+ "module": "./dist/index.js",
53
+ "unpkg": "./dist/browser.js",
54
+ "jsdelivr": "./dist/browser.js",
55
+ "sideEffects": false,
56
+ "engines": {
57
+ "node": ">=20"
58
+ },
59
+ "homepage": "https://github.com/corbet-labs/cink#readme",
60
+ "bugs": {
61
+ "url": "https://github.com/corbet-labs/cink/issues"
62
+ },
63
+ "keywords": [
64
+ "i18n",
65
+ "correspondence",
66
+ "typescript",
67
+ "rust",
68
+ "cink"
69
+ ]
34
70
  }
@@ -1,3 +1,3 @@
1
1
  // GENERATED by scripts/sync-assets.sh — do not edit.
2
- export const PACKAGE_VERSION = "0.1.0";
3
- export const DEFAULTS = {"max_pixels": 1600, "formats": ["png", "jpeg", "svg"]};
2
+ export const PACKAGE_VERSION: string = "0.1.2";
3
+ export const DEFAULTS: unknown = {"max_pixels": 1600, "formats": ["png", "jpeg", "svg"]};
package/src/index.ts CHANGED
@@ -9,7 +9,7 @@
9
9
  * in `tables/README.md`; `tests/vectors/*.json` with fixtures in
10
10
  * `tests/fixtures/` is the shared conformance suite.
11
11
  */
12
- import { DEFAULTS } from './generated/tables.js';
12
+ import { DEFAULTS } from './generated/tables.ts';
13
13
 
14
14
  export interface Defaults {
15
15
  max_pixels: number;
@@ -32,6 +32,11 @@ export function defaultMaxPixels(): number {
32
32
  return defaults.max_pixels;
33
33
  }
34
34
 
35
+ /** Accepted image formats, from `tables/defaults.json`. */
36
+ export function supportedFormats(): string[] {
37
+ return [...defaults.formats].sort();
38
+ }
39
+
35
40
  const PNG_MAGIC = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
36
41
 
37
42
  function pngDimensions(bytes: Uint8Array): [number, number] | null {