freshcoat 0.1.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 +202 -0
- package/NOTICE +17 -0
- package/README.md +110 -0
- package/package.json +53 -0
- package/src/adjust.d.ts +18 -0
- package/src/adjust.js +153 -0
- package/src/approx-layout.d.ts +2 -0
- package/src/approx-layout.js +91 -0
- package/src/bake-text.d.ts +12 -0
- package/src/bake-text.js +321 -0
- package/src/browser.d.ts +7 -0
- package/src/browser.js +79 -0
- package/src/canvaskit.d.ts +4 -0
- package/src/canvaskit.js +1674 -0
- package/src/compile-scene.d.ts +24 -0
- package/src/compile-scene.js +325 -0
- package/src/css.d.ts +5 -0
- package/src/css.js +153 -0
- package/src/decode.d.ts +22 -0
- package/src/decode.js +112 -0
- package/src/export-scale.d.ts +25 -0
- package/src/export-scale.js +74 -0
- package/src/font-bytes.d.ts +4 -0
- package/src/font-bytes.js +165 -0
- package/src/font-metrics.d.ts +5 -0
- package/src/font-metrics.js +62 -0
- package/src/headless.d.ts +24 -0
- package/src/headless.js +58 -0
- package/src/index.d.ts +25 -0
- package/src/index.js +29 -0
- package/src/jpeg.d.ts +5 -0
- package/src/jpeg.js +20 -0
- package/src/line-height.d.ts +7 -0
- package/src/line-height.js +80 -0
- package/src/node.d.ts +139 -0
- package/src/node.js +47 -0
- package/src/paint-cache.d.ts +49 -0
- package/src/paint-cache.js +109 -0
- package/src/paint-helpers.d.ts +21 -0
- package/src/paint-helpers.js +71 -0
- package/src/paragraph-layout.d.ts +6 -0
- package/src/paragraph-layout.js +283 -0
- package/src/path-data.d.ts +5 -0
- package/src/path-data.js +101 -0
- package/src/png.d.ts +23 -0
- package/src/png.js +189 -0
- package/src/resolve-layout.d.ts +9 -0
- package/src/resolve-layout.js +657 -0
- package/src/runtime.d.ts +6 -0
- package/src/runtime.js +44 -0
- package/src/squircle.d.ts +1 -0
- package/src/squircle.js +60 -0
- package/src/text-cache.d.ts +14 -0
- package/src/text-cache.js +46 -0
- package/src/text-engine.d.ts +26 -0
- package/src/text-engine.js +1 -0
- package/src/text-types.d.ts +30 -0
- package/src/text-types.js +1 -0
- package/src/types.d.ts +406 -0
- package/src/types.js +13 -0
- package/src/validate-commands.d.ts +8 -0
- package/src/validate-commands.js +206 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
freshcoat
|
|
2
|
+
Copyright 2026 Dotside Studios
|
|
3
|
+
|
|
4
|
+
This product includes software developed by Dotside Studios.
|
|
5
|
+
|
|
6
|
+
This package bundles the following fonts for its tests and conformance suite,
|
|
7
|
+
each under the SIL Open Font License 1.1 and not under the Apache License:
|
|
8
|
+
|
|
9
|
+
tests/fonts/Geist-Regular.ttf
|
|
10
|
+
Copyright 2024 The Geist Project Authors
|
|
11
|
+
(https://github.com/vercel/geist-font)
|
|
12
|
+
License: tests/fonts/Geist-OFL.txt
|
|
13
|
+
|
|
14
|
+
tests/fonts/VendSans-Variable-latin.woff2
|
|
15
|
+
Copyright 2025 The Vend Sans Project Authors
|
|
16
|
+
(https://github.com/ktkm/Vend-Sans)
|
|
17
|
+
License: tests/fonts/VendSans-OFL.txt
|
package/README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# freshcoat
|
|
2
|
+
|
|
3
|
+
The 2D rendering engine behind [Freshcoat](../../README.md), named
|
|
4
|
+
`freshcoat` in the workspace. It lays out a scene graph,
|
|
5
|
+
shapes text and paints with CanvasKit (Skia compiled to WASM).
|
|
6
|
+
|
|
7
|
+
Use it directly when your application supplies its own scene: previews,
|
|
8
|
+
generated images or an interactive canvas. For `.coat` templates and field
|
|
9
|
+
substitution, start with [`@freshcoat/coatfile`](../coatfile), which compiles
|
|
10
|
+
templates into engine nodes.
|
|
11
|
+
|
|
12
|
+
The scene and command types are independent of the backend; the supplied
|
|
13
|
+
painter uses CanvasKit. Browser and headless environments share that paint
|
|
14
|
+
path. Template fields, variants and frame selection belong to coatfile;
|
|
15
|
+
card-printer correction policy belongs to
|
|
16
|
+
[`@freshcoat/for-print`](../for-print).
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
19
|
+
|
|
20
|
+
With an initialized CanvasKit instance (`ck`), render a scene offscreen:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import type { RectNode } from "freshcoat";
|
|
24
|
+
import { renderSceneToPng } from "freshcoat/headless";
|
|
25
|
+
|
|
26
|
+
const root: RectNode = {
|
|
27
|
+
kind: "rect",
|
|
28
|
+
pos: { x: 0, y: 0 },
|
|
29
|
+
size: { width: 640, height: 360 },
|
|
30
|
+
fills: [{ kind: "solid", color: "#1f6fe8" }],
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const result = await renderSceneToPng(root, { width: 640, height: 360, ck });
|
|
34
|
+
// result.bytes — PNG bytes by default; result.warnings — paint diagnostics
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
You supply CanvasKit initialization and the WASM file location for your host.
|
|
38
|
+
For text, also supply a `fonts` map of family names to font byte arrays; the
|
|
39
|
+
helper derives font metrics and creates the Paragraph text engine.
|
|
40
|
+
|
|
41
|
+
## Node IR
|
|
42
|
+
|
|
43
|
+
Eight node kinds — `rect | ellipse | path | image | bitmap | text | group |
|
|
44
|
+
mask` — describe shapes, assets, text and composition. Nodes share positioning
|
|
45
|
+
and compositing properties, with fills, strokes and other options appropriate
|
|
46
|
+
to each kind. Groups support flex and grid layout.
|
|
47
|
+
|
|
48
|
+
`compileScene()` lowers a node tree to the flat `Command[]` list the painter
|
|
49
|
+
executes. Use this lower-level path when you need to inspect commands or keep
|
|
50
|
+
a runtime alive across renders. The types in [`src/node.ts`](src/node.ts)
|
|
51
|
+
define the scene model.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { compileScene } from "freshcoat";
|
|
55
|
+
import { createHeadlessEnv } from "freshcoat/headless";
|
|
56
|
+
|
|
57
|
+
const commands = compileScene(root, { width, height, textEngine });
|
|
58
|
+
const env = createHeadlessEnv({ fonts });
|
|
59
|
+
const result = await env.paint(commands, ck);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Painting and text
|
|
63
|
+
|
|
64
|
+
The supplied text engine uses CanvasKit Paragraph for layout and shaping,
|
|
65
|
+
keeping measurement and painting on the same text implementation. On the
|
|
66
|
+
lower-level path, pass a `textEngine` to resolve layout and unbaked text, or
|
|
67
|
+
supply a scene with resolved geometry and baked text.
|
|
68
|
+
|
|
69
|
+
Gradients, masks, blend modes and per-layer `Adjust` (color matrix, lookup
|
|
70
|
+
table and sharpening) are engine operations. `FrameFinish` applies operations
|
|
71
|
+
after the whole scene is composited. The engine implements these operations;
|
|
72
|
+
the caller decides when and where to use them.
|
|
73
|
+
|
|
74
|
+
## Subpaths
|
|
75
|
+
|
|
76
|
+
The barrel stays free of DOM and WASM weight; the paint target lives on its
|
|
77
|
+
own subpath.
|
|
78
|
+
|
|
79
|
+
| Subpath | What it is |
|
|
80
|
+
|---|---|
|
|
81
|
+
| `freshcoat` | node types, `compileScene`, layout, adjust and export-scale math |
|
|
82
|
+
| `freshcoat/browser` | a runtime backing a live DOM canvas — the editor's preview |
|
|
83
|
+
| `freshcoat/headless` | offscreen painting to PNG, napi-free — server previews, OG images |
|
|
84
|
+
| `freshcoat/runtime` | the backend seam: `Painter` and `makeRuntime` |
|
|
85
|
+
| `freshcoat/path` | SVG path data parsing and maths |
|
|
86
|
+
|
|
87
|
+
## Staying warm
|
|
88
|
+
|
|
89
|
+
For repeated renders, reuse a paint runtime with `createPaintCache` and a
|
|
90
|
+
text engine wrapped by `memoizeTextEngine`. The paint cache retains the
|
|
91
|
+
surface, font provider and decoded images; the text cache reuses unchanged
|
|
92
|
+
paragraph layouts. Studio's render session uses both. See
|
|
93
|
+
[Studio performance](../../apps/editor/docs/performance.md) for historical
|
|
94
|
+
integration benchmarks and their conditions.
|
|
95
|
+
|
|
96
|
+
For exports, `exportPixelSize` resolves density within
|
|
97
|
+
`MAX_EXPORT_DIMENSION`. The headless runtime encodes PNG by default, with
|
|
98
|
+
JPEG and WebP available through its `encode` options. Export scale changes
|
|
99
|
+
the output pixel dimensions; supersampling renders at a higher density and
|
|
100
|
+
reduces to the requested output size.
|
|
101
|
+
|
|
102
|
+
## Conformance
|
|
103
|
+
|
|
104
|
+
From this package directory, `bun run conformance` reports the golden-image
|
|
105
|
+
suite, and `bun run conformance:regen` authors cases and regenerates goldens.
|
|
106
|
+
|
|
107
|
+
## License
|
|
108
|
+
|
|
109
|
+
Apache-2.0, see [`LICENSE`](./LICENSE) and [`NOTICE`](./NOTICE). Part of
|
|
110
|
+
[Freshcoat](../../README.md).
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "freshcoat",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A composable 2D scene renderer powered by CanvasKit and Skia",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/dotside-studios/freshcoat.git",
|
|
9
|
+
"directory": "packages/engine"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"private": false,
|
|
13
|
+
"main": "./src/index.js",
|
|
14
|
+
"types": "./src/index.d.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"fixtures",
|
|
18
|
+
"schema",
|
|
19
|
+
"LICENSE",
|
|
20
|
+
"NOTICE",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./src/index.d.ts",
|
|
29
|
+
"import": "./src/index.js",
|
|
30
|
+
"default": "./src/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./browser": {
|
|
33
|
+
"types": "./src/browser.d.ts",
|
|
34
|
+
"import": "./src/browser.js",
|
|
35
|
+
"default": "./src/browser.js"
|
|
36
|
+
},
|
|
37
|
+
"./headless": {
|
|
38
|
+
"types": "./src/headless.d.ts",
|
|
39
|
+
"import": "./src/headless.js",
|
|
40
|
+
"default": "./src/headless.js"
|
|
41
|
+
},
|
|
42
|
+
"./path": {
|
|
43
|
+
"types": "./src/path-data.d.ts",
|
|
44
|
+
"import": "./src/path-data.js",
|
|
45
|
+
"default": "./src/path-data.js"
|
|
46
|
+
},
|
|
47
|
+
"./runtime": {
|
|
48
|
+
"types": "./src/runtime.d.ts",
|
|
49
|
+
"import": "./src/runtime.js",
|
|
50
|
+
"default": "./src/runtime.js"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
package/src/adjust.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Adjust, AdjustLut, AdjustLut3d } from "./types.js";
|
|
2
|
+
export type ColorMatrix = number[];
|
|
3
|
+
export declare function identityColorMatrix(): ColorMatrix;
|
|
4
|
+
export declare function concatColorMatrix(outer: ColorMatrix, inner: ColorMatrix): ColorMatrix;
|
|
5
|
+
export declare function saturationMatrix(s: number): ColorMatrix;
|
|
6
|
+
export declare function contrastMatrix(c: number): ColorMatrix;
|
|
7
|
+
export declare function brightnessMatrix(k: number): ColorMatrix;
|
|
8
|
+
export declare function gammaLut(gamma: number): AdjustLut;
|
|
9
|
+
export declare function identityLut3d(size: number): AdjustLut3d;
|
|
10
|
+
export type AdjustOptions = {
|
|
11
|
+
saturation?: number;
|
|
12
|
+
contrast?: number;
|
|
13
|
+
brightness?: number;
|
|
14
|
+
gamma?: number;
|
|
15
|
+
sharpen?: number;
|
|
16
|
+
preserveHue?: boolean;
|
|
17
|
+
};
|
|
18
|
+
export declare function buildAdjust(o: AdjustOptions): Adjust;
|
package/src/adjust.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Generic, domain-neutral per-layer color/tone adjustment — freshcoat's
|
|
2
|
+
// vocabulary for "transform this layer's pixels as it composites", the same way
|
|
3
|
+
// `opacity`, `blendMode`, and `blur` already are. It knows the MATH (color
|
|
4
|
+
// matrix, per-channel curve, sharpen), never the INTENT: color correction,
|
|
5
|
+
// theming, accessibility contrast, and preview effects all drive the same
|
|
6
|
+
// primitive. A consumer maps its own domain policy down to an `Adjust`; freshcoat
|
|
7
|
+
// applies it without knowing why.
|
|
8
|
+
//
|
|
9
|
+
// The primitive is the closure of per-pixel color transforms: a 4×5 color matrix
|
|
10
|
+
// (cross-channel — saturation/brightness/contrast) applied FIRST, then a
|
|
11
|
+
// per-channel LUT (nonlinear curves — gamma), matching SVG's
|
|
12
|
+
// feColorMatrix-then-feComponentTransfer ordering. `sharpen` is a spatial
|
|
13
|
+
// post-filter, a sibling of `blur`. Every field is optional; absent = identity.
|
|
14
|
+
//
|
|
15
|
+
// The matrix follows Skia's SkColorMatrix convention: 20 floats, row-major RGBA
|
|
16
|
+
// rows, and the 5th (translation) column expressed as a fraction of full scale
|
|
17
|
+
// ([0,1]), NOT 0–255.
|
|
18
|
+
// Rec.709 luma coefficients — the standard luminance weights used to keep a
|
|
19
|
+
// saturation change perceptually neutral.
|
|
20
|
+
const LUMA_R = 0.2126;
|
|
21
|
+
const LUMA_G = 0.7152;
|
|
22
|
+
const LUMA_B = 0.0722;
|
|
23
|
+
// Contrast pivots on the 8-bit midpoint 128 (in 0–255 space); normalized for the
|
|
24
|
+
// color matrix that is 128/255, not 0.5.
|
|
25
|
+
const PIVOT = 128 / 255;
|
|
26
|
+
export function identityColorMatrix() {
|
|
27
|
+
// biome-ignore format: matrix rows read clearer aligned
|
|
28
|
+
return [
|
|
29
|
+
1, 0, 0, 0, 0,
|
|
30
|
+
0, 1, 0, 0, 0,
|
|
31
|
+
0, 0, 1, 0, 0,
|
|
32
|
+
0, 0, 0, 1, 0,
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
// concat(outer, inner): the matrix that applies `inner` first, then `outer` — so
|
|
36
|
+
// concat(contrast, saturation) saturates then contrasts. Both are 4×5 affine
|
|
37
|
+
// color matrices; we extend each to 5×5 with an implicit [0,0,0,0,1] last row and
|
|
38
|
+
// multiply outer · innerExt.
|
|
39
|
+
export function concatColorMatrix(outer, inner) {
|
|
40
|
+
const out = new Array(20).fill(0);
|
|
41
|
+
for (let row = 0; row < 4; row++) {
|
|
42
|
+
for (let col = 0; col < 5; col++) {
|
|
43
|
+
let sum = 0;
|
|
44
|
+
for (let k = 0; k < 4; k++) {
|
|
45
|
+
sum += outer[row * 5 + k] * inner[k * 5 + col];
|
|
46
|
+
}
|
|
47
|
+
// The extended 5th row of `inner` is [0,0,0,0,1], so it contributes only
|
|
48
|
+
// to the bias (col 4) via outer's own bias term.
|
|
49
|
+
if (col === 4)
|
|
50
|
+
sum += outer[row * 5 + 4];
|
|
51
|
+
out[row * 5 + col] = sum;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
// Saturation as a color matrix: gray + s·(channel − gray), gray = luma. s=1 is
|
|
57
|
+
// identity, s=0 fully desaturates, s>1 boosts.
|
|
58
|
+
export function saturationMatrix(s) {
|
|
59
|
+
const ir = (1 - s) * LUMA_R;
|
|
60
|
+
const ig = (1 - s) * LUMA_G;
|
|
61
|
+
const ib = (1 - s) * LUMA_B;
|
|
62
|
+
// biome-ignore format: matrix rows read clearer aligned
|
|
63
|
+
return [
|
|
64
|
+
ir + s, ig, ib, 0, 0,
|
|
65
|
+
ir, ig + s, ib, 0, 0,
|
|
66
|
+
ir, ig, ib + s, 0, 0,
|
|
67
|
+
0, 0, 0, 1, 0,
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
// Contrast about the 128/255 pivot: v' = c·(v − pivot) + pivot.
|
|
71
|
+
export function contrastMatrix(c) {
|
|
72
|
+
const b = PIVOT * (1 - c);
|
|
73
|
+
// biome-ignore format: matrix rows read clearer aligned
|
|
74
|
+
return [
|
|
75
|
+
c, 0, 0, 0, b,
|
|
76
|
+
0, c, 0, 0, b,
|
|
77
|
+
0, 0, c, 0, b,
|
|
78
|
+
0, 0, 0, 1, 0,
|
|
79
|
+
];
|
|
80
|
+
}
|
|
81
|
+
// Brightness as a straight multiplier (v' = v·k); identity at k=1.
|
|
82
|
+
export function brightnessMatrix(k) {
|
|
83
|
+
// biome-ignore format: matrix rows read clearer aligned
|
|
84
|
+
return [
|
|
85
|
+
k, 0, 0, 0, 0,
|
|
86
|
+
0, k, 0, 0, 0,
|
|
87
|
+
0, 0, k, 0, 0,
|
|
88
|
+
0, 0, 0, 1, 0,
|
|
89
|
+
];
|
|
90
|
+
}
|
|
91
|
+
// Per-channel gamma curve as a 256-entry LUT: out = round(255·(i/255)^gamma),
|
|
92
|
+
// clamped. r/g/b share one table.
|
|
93
|
+
export function gammaLut(gamma) {
|
|
94
|
+
const table = new Uint8Array(256);
|
|
95
|
+
for (let i = 0; i < 256; i++) {
|
|
96
|
+
const v = Math.round(255 * (i / 255) ** gamma);
|
|
97
|
+
table[i] = v < 0 ? 0 : v > 255 ? 255 : v;
|
|
98
|
+
}
|
|
99
|
+
return { r: table, g: table, b: table };
|
|
100
|
+
}
|
|
101
|
+
// An identity RGB cube, useful as a starting point for an authored or measured
|
|
102
|
+
// device transform. The table format is shared with Adjust.lut3d (r-fast RGB
|
|
103
|
+
// triplets), deliberately independent of any print/profile vocabulary.
|
|
104
|
+
export function identityLut3d(size) {
|
|
105
|
+
if (!Number.isInteger(size) || size < 2) {
|
|
106
|
+
throw new Error("identityLut3d: size must be an integer of at least 2");
|
|
107
|
+
}
|
|
108
|
+
const data = new Uint8Array(size * size * size * 3);
|
|
109
|
+
const level = (i) => Math.round((i / (size - 1)) * 255);
|
|
110
|
+
for (let b = 0; b < size; b++) {
|
|
111
|
+
for (let g = 0; g < size; g++) {
|
|
112
|
+
for (let r = 0; r < size; r++) {
|
|
113
|
+
const at = ((b * size + g) * size + r) * 3;
|
|
114
|
+
data[at] = level(r);
|
|
115
|
+
data[at + 1] = level(g);
|
|
116
|
+
data[at + 2] = level(b);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return { size, data };
|
|
121
|
+
}
|
|
122
|
+
const isMatrixIdentity = (m) => {
|
|
123
|
+
const id = identityColorMatrix();
|
|
124
|
+
for (let i = 0; i < 20; i++)
|
|
125
|
+
if (Math.abs(m[i] - id[i]) > 1e-9)
|
|
126
|
+
return false;
|
|
127
|
+
return true;
|
|
128
|
+
};
|
|
129
|
+
// Build an `Adjust` from semantic knobs. Cross-channel/linear ops fold into one
|
|
130
|
+
// color matrix, applied in order (saturation → contrast → brightness); gamma
|
|
131
|
+
// becomes the LUT applied after; sharpen passes through. Fields that are at their
|
|
132
|
+
// identity value are omitted, so `buildAdjust({})` is `{}` (a no-op).
|
|
133
|
+
export function buildAdjust(o) {
|
|
134
|
+
let m = identityColorMatrix();
|
|
135
|
+
if (o.saturation !== undefined && o.saturation !== 1)
|
|
136
|
+
m = concatColorMatrix(saturationMatrix(o.saturation), m);
|
|
137
|
+
if (o.contrast !== undefined && o.contrast !== 1)
|
|
138
|
+
m = concatColorMatrix(contrastMatrix(o.contrast), m);
|
|
139
|
+
if (o.brightness !== undefined && o.brightness !== 1)
|
|
140
|
+
m = concatColorMatrix(brightnessMatrix(o.brightness), m);
|
|
141
|
+
const adjust = {};
|
|
142
|
+
if (!isMatrixIdentity(m)) {
|
|
143
|
+
adjust.colorMatrix = m;
|
|
144
|
+
// Only a matrix leaves the range, so the mode is meaningless without one.
|
|
145
|
+
if (o.preserveHue)
|
|
146
|
+
adjust.gamut = "preserve-hue";
|
|
147
|
+
}
|
|
148
|
+
if (o.gamma !== undefined && o.gamma !== 1)
|
|
149
|
+
adjust.lut = gammaLut(o.gamma);
|
|
150
|
+
if (o.sharpen !== undefined && o.sharpen > 0)
|
|
151
|
+
adjust.sharpen = o.sharpen;
|
|
152
|
+
return adjust;
|
|
153
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// Mean glyph advance as a fraction of font size — a serviceable stand-in for a
|
|
2
|
+
// proportional font's average character width.
|
|
3
|
+
const CHAR_W = 0.5;
|
|
4
|
+
const SHRINK_FLOOR_PX = 8;
|
|
5
|
+
function collapse(text) {
|
|
6
|
+
return text.replace(/\s+/g, " ").trim();
|
|
7
|
+
}
|
|
8
|
+
function advance(text, size, letterSpacing = 0) {
|
|
9
|
+
const n = [...text].length;
|
|
10
|
+
if (n === 0)
|
|
11
|
+
return 0;
|
|
12
|
+
return n * size * CHAR_W + Math.max(0, n - 1) * letterSpacing;
|
|
13
|
+
}
|
|
14
|
+
// Proportional char-count wrap: split into ceil(natural / maxWidth) even chunks.
|
|
15
|
+
// Bars, not glyphs, are drawn from this, so mid-word splits are invisible.
|
|
16
|
+
function wrap(text, size, letterSpacing, maxWidth) {
|
|
17
|
+
const norm = collapse(text);
|
|
18
|
+
const chars = [...norm];
|
|
19
|
+
const natural = advance(norm, size, letterSpacing);
|
|
20
|
+
if (natural <= maxWidth || chars.length <= 1) {
|
|
21
|
+
return [{ text: norm, width: natural }];
|
|
22
|
+
}
|
|
23
|
+
const lineCount = Math.max(1, Math.ceil(natural / maxWidth));
|
|
24
|
+
const perLine = Math.ceil(chars.length / lineCount);
|
|
25
|
+
const lines = [];
|
|
26
|
+
for (let i = 0; i < chars.length; i += perLine) {
|
|
27
|
+
const chunk = chars.slice(i, i + perLine).join("");
|
|
28
|
+
lines.push({
|
|
29
|
+
text: chunk,
|
|
30
|
+
width: Math.min(advance(chunk, size, letterSpacing), maxWidth),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return lines;
|
|
34
|
+
}
|
|
35
|
+
const measureText = (text, font, maxWidth) => {
|
|
36
|
+
const lineHeightPx = font.size * font.lineHeight;
|
|
37
|
+
if (text.length === 0)
|
|
38
|
+
return { width: 0, height: lineHeightPx };
|
|
39
|
+
if (maxWidth === null) {
|
|
40
|
+
return {
|
|
41
|
+
width: advance(collapse(text), font.size, font.letterSpacing ?? 0),
|
|
42
|
+
height: lineHeightPx,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const lines = wrap(text, font.size, font.letterSpacing ?? 0, maxWidth);
|
|
46
|
+
const width = lines.reduce((max, l) => Math.max(max, l.width), 0);
|
|
47
|
+
return { width, height: Math.max(1, lines.length) * lineHeightPx };
|
|
48
|
+
};
|
|
49
|
+
const measureSpanWidth = (text, font) => advance(collapse(text), font.size, font.letterSpacing ?? 0);
|
|
50
|
+
const layoutText = (input) => {
|
|
51
|
+
const lh = input.lineHeight;
|
|
52
|
+
const ls = input.font.letterSpacing ?? 0;
|
|
53
|
+
const once = (size) => {
|
|
54
|
+
const lines = wrap(input.value, size, ls, input.maxWidth);
|
|
55
|
+
return {
|
|
56
|
+
lines,
|
|
57
|
+
totalHeight: lines.length * size * lh,
|
|
58
|
+
effectiveFontSize: size,
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
if (input.fit !== "shrink" || input.font.size < SHRINK_FLOOR_PX) {
|
|
62
|
+
return { ...once(input.font.size), shrinkApplied: false };
|
|
63
|
+
}
|
|
64
|
+
const full = once(input.font.size);
|
|
65
|
+
if (full.totalHeight <= input.maxHeight)
|
|
66
|
+
return { ...full, shrinkApplied: false };
|
|
67
|
+
let lo = SHRINK_FLOOR_PX;
|
|
68
|
+
let hi = input.font.size;
|
|
69
|
+
let best = null;
|
|
70
|
+
while (lo <= hi) {
|
|
71
|
+
const mid = Math.floor((lo + hi) / 2);
|
|
72
|
+
const trial = once(mid);
|
|
73
|
+
if (trial.totalHeight <= input.maxHeight) {
|
|
74
|
+
best = trial;
|
|
75
|
+
lo = mid + 1;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
hi = mid - 1;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return best
|
|
82
|
+
? { ...best, shrinkApplied: true }
|
|
83
|
+
: { ...once(SHRINK_FLOOR_PX), shrinkApplied: true };
|
|
84
|
+
};
|
|
85
|
+
// compile's default engine. No layoutInline — compile falls back to
|
|
86
|
+
// independent-span single-line placement, which is fine for the skeleton.
|
|
87
|
+
export const approxEngine = {
|
|
88
|
+
measureText,
|
|
89
|
+
measureSpanWidth,
|
|
90
|
+
layoutText,
|
|
91
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { TextNode } from "./node.js";
|
|
2
|
+
import type { TextEngine } from "./text-engine.js";
|
|
3
|
+
import type { BakedTextLayout, ClipOutset, FontVMetrics, ResolvedFont } from "./types.js";
|
|
4
|
+
export type BakeTextOptions = {
|
|
5
|
+
textEngine: TextEngine;
|
|
6
|
+
leadingTrim?: boolean;
|
|
7
|
+
fontMetrics?: Record<string, FontVMetrics>;
|
|
8
|
+
deviceScale?: number;
|
|
9
|
+
};
|
|
10
|
+
export declare function resolveLeadingTrim(node: TextNode, fallback?: boolean): boolean;
|
|
11
|
+
export declare function textClipOutset(font: ResolvedFont, leadingTrim: boolean, metricsMap?: Record<string, FontVMetrics>): ClipOutset;
|
|
12
|
+
export declare function bakeText(node: TextNode, opts: BakeTextOptions): BakedTextLayout;
|