@kyralabs/m3e-icons 0.0.0-stage → 1.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 +201 -0
- package/NOTICE +50 -0
- package/README.md +20 -2
- package/dist/cli.js +180 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
m3exp-web
|
|
2
|
+
Copyright 2026 Kyra Labs
|
|
3
|
+
|
|
4
|
+
This product is licensed under the Apache License, Version 2.0 (see LICENSE).
|
|
5
|
+
|
|
6
|
+
This product includes software from the projects listed below, each licensed
|
|
7
|
+
under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0).
|
|
8
|
+
Portions have been modified and ported to TypeScript by Kyra Labs. Every ported
|
|
9
|
+
file keeps its original copyright and license header and names its source path
|
|
10
|
+
and commit.
|
|
11
|
+
|
|
12
|
+
--------------------------------------------------------------------------------
|
|
13
|
+
AndroidX Compose Material3 (design tokens, component logic, MaterialShapes)
|
|
14
|
+
https://android.googlesource.com/platform/frameworks/support/+/19991f2c0858aadfcb442ce02b2c8e4743c96898/compose/material3/
|
|
15
|
+
Copyright (C) The Android Open Source Project
|
|
16
|
+
Licensed under the Apache License, Version 2.0
|
|
17
|
+
|
|
18
|
+
--------------------------------------------------------------------------------
|
|
19
|
+
AndroidX Compose Animation (animation-core: spring simulation and duration estimate)
|
|
20
|
+
https://android.googlesource.com/platform/frameworks/support/+/19991f2c0858aadfcb442ce02b2c8e4743c96898/compose/animation/animation-core/
|
|
21
|
+
Copyright (C) The Android Open Source Project
|
|
22
|
+
Licensed under the Apache License, Version 2.0
|
|
23
|
+
|
|
24
|
+
--------------------------------------------------------------------------------
|
|
25
|
+
AndroidX Compose UI Graphics (Matrix: the subset MaterialShapes uses)
|
|
26
|
+
https://android.googlesource.com/platform/frameworks/support/+/19991f2c0858aadfcb442ce02b2c8e4743c96898/compose/ui/ui-graphics/
|
|
27
|
+
Copyright (C) The Android Open Source Project
|
|
28
|
+
Licensed under the Apache License, Version 2.0
|
|
29
|
+
|
|
30
|
+
--------------------------------------------------------------------------------
|
|
31
|
+
AndroidX graphics-shapes (androidx.graphics:graphics-shapes 1.0.1, sources jar)
|
|
32
|
+
https://dl.google.com/android/maven2/androidx/graphics/graphics-shapes/1.0.1/graphics-shapes-1.0.1-sources.jar
|
|
33
|
+
Copyright (C) The Android Open Source Project
|
|
34
|
+
Licensed under the Apache License, Version 2.0
|
|
35
|
+
|
|
36
|
+
--------------------------------------------------------------------------------
|
|
37
|
+
Material Web (@material/web 2.5.0: elevation shadow values in @kyralabs/m3e-tokens)
|
|
38
|
+
https://github.com/material-components/material-web
|
|
39
|
+
Copyright 2022 Google LLC
|
|
40
|
+
Licensed under the Apache License, Version 2.0
|
|
41
|
+
|
|
42
|
+
--------------------------------------------------------------------------------
|
|
43
|
+
material-color-utilities (@material/material-color-utilities 0.4.0, bundled in @kyralabs/m3e-tokens)
|
|
44
|
+
https://github.com/material-foundation/material-color-utilities
|
|
45
|
+
Copyright 2021 Google LLC
|
|
46
|
+
Licensed under the Apache License, Version 2.0
|
|
47
|
+
|
|
48
|
+
--------------------------------------------------------------------------------
|
|
49
|
+
Bundled fonts are distributed under their own licenses, included beside each
|
|
50
|
+
font file in packages/m3e-tokens/fonts/.
|
package/README.md
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kyralabs/m3e-icons
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Subset the Material Symbols variable font so an app only ships the icons it uses. The full fonts
|
|
4
|
+
come from `@kyralabs/m3e-tokens` (Rounded by default; Outlined and Sharp too).
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm install --save-dev @kyralabs/m3e-icons
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npx m3e-icons subset --names home,search,menu --out public/icons.woff2 --css public/icons.css
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The CSS is a drop-in `@font-face` for `.m3e-icon`. Options: `--names-file`,
|
|
17
|
+
`--style rounded|outlined|sharp`, `--fill`, `--wght`, `--grad`, `--opsz` (pin a value or narrow a
|
|
18
|
+
range), `--family` and `--font`; run `npx m3e-icons` for the full usage. Build scripts can call
|
|
19
|
+
the exported `subsetIcons()` instead.
|
|
20
|
+
|
|
21
|
+
Apache-2.0; see `LICENSE` and `NOTICE`. Material Symbols is Apache-2.0 (Google).
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync, mkdirSync, writeFileSync } from 'fs';
|
|
3
|
+
import { createRequire } from 'module';
|
|
4
|
+
import { resolve, dirname, relative } from 'path';
|
|
5
|
+
import { parseArgs } from 'util';
|
|
6
|
+
import subsetFont from 'subset-font';
|
|
7
|
+
|
|
8
|
+
function parseCodepoints(text) {
|
|
9
|
+
const map = /* @__PURE__ */ new Map();
|
|
10
|
+
for (const line of text.split(/\r?\n/)) {
|
|
11
|
+
const m = /^(\S+) ([0-9a-f]+)$/i.exec(line.trim());
|
|
12
|
+
if (m) map.set(m[1], parseInt(m[2], 16));
|
|
13
|
+
}
|
|
14
|
+
return map;
|
|
15
|
+
}
|
|
16
|
+
var UnknownIconError = class extends Error {
|
|
17
|
+
constructor(names) {
|
|
18
|
+
super(`Unknown Material Symbols icon name(s): ${names.join(", ")}`);
|
|
19
|
+
this.names = names;
|
|
20
|
+
this.name = "UnknownIconError";
|
|
21
|
+
}
|
|
22
|
+
names;
|
|
23
|
+
};
|
|
24
|
+
function subsetText(names, codepoints) {
|
|
25
|
+
const unique = [...new Set(names)].sort();
|
|
26
|
+
const unknown = unique.filter((n) => !codepoints.has(n));
|
|
27
|
+
if (unknown.length > 0) throw new UnknownIconError(unknown);
|
|
28
|
+
const chars = /* @__PURE__ */ new Set();
|
|
29
|
+
for (const name of unique) {
|
|
30
|
+
for (const ch of name) chars.add(ch);
|
|
31
|
+
chars.add(String.fromCodePoint(codepoints.get(name) ?? 0));
|
|
32
|
+
}
|
|
33
|
+
return [...chars].sort().join("");
|
|
34
|
+
}
|
|
35
|
+
async function subsetIcons(options) {
|
|
36
|
+
const { font, codepoints, names, axes, format = "woff2" } = options;
|
|
37
|
+
if (names.length === 0) throw new Error("subsetIcons: no icon names given");
|
|
38
|
+
return subsetFont(font, subsetText(names, codepoints), {
|
|
39
|
+
targetFormat: format,
|
|
40
|
+
noLayoutClosure: true,
|
|
41
|
+
...axes && Object.keys(axes).length > 0 ? { variationAxes: axes } : {}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function iconFontFace({ family, url }) {
|
|
45
|
+
return [
|
|
46
|
+
"@font-face {",
|
|
47
|
+
` font-family: "${family}";`,
|
|
48
|
+
" font-style: normal;",
|
|
49
|
+
" font-weight: 100 700;",
|
|
50
|
+
` src: url("${url}") format("woff2");`,
|
|
51
|
+
"}",
|
|
52
|
+
"",
|
|
53
|
+
"@layer m3e.tokens {",
|
|
54
|
+
" :root {",
|
|
55
|
+
` --m3e-ref-typeface-icon: "${family}";`,
|
|
56
|
+
" }",
|
|
57
|
+
"}",
|
|
58
|
+
""
|
|
59
|
+
].join("\n");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/args.ts
|
|
63
|
+
function parseAxis(value) {
|
|
64
|
+
const range = /^(-?\d+(?:\.\d+)?)\.\.(-?\d+(?:\.\d+)?)$/.exec(value);
|
|
65
|
+
if (range) return { min: Number(range[1]), max: Number(range[2]) };
|
|
66
|
+
const n = Number(value);
|
|
67
|
+
if (value.trim() === "" || Number.isNaN(n)) throw new Error(`invalid axis value "${value}"`);
|
|
68
|
+
return n;
|
|
69
|
+
}
|
|
70
|
+
function parseNames(text) {
|
|
71
|
+
return text.split(/\r?\n/).map((line) => line.replace(/#.*$/, "")).flatMap((line) => line.split(",")).map((n) => n.trim()).filter(Boolean);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/cli.ts
|
|
75
|
+
var STYLES = ["rounded", "outlined", "sharp"];
|
|
76
|
+
var USAGE = `usage: m3e-icons subset --names <a,b,\u2026> [options]
|
|
77
|
+
|
|
78
|
+
--names comma-separated icon names (e.g. home,arrow_back)
|
|
79
|
+
--names-file file with names (comma/newline separated, # comments)
|
|
80
|
+
--style ${STYLES.join(" | ")} (default rounded)
|
|
81
|
+
--fill pin (0 or 1) or narrow (0..1) the FILL axis
|
|
82
|
+
--wght pin (e.g. 400) or narrow (e.g. 300..500) the weight axis
|
|
83
|
+
--grad pin or narrow the GRAD axis (-50..200)
|
|
84
|
+
--opsz pin or narrow the optical size axis (20..48)
|
|
85
|
+
--out output woff2 (default material-symbols-subset.woff2)
|
|
86
|
+
--css also write @font-face CSS here (url relative to the CSS file)
|
|
87
|
+
--family font-family in the CSS (default "Material Symbols <Style>": drop-in)
|
|
88
|
+
--font source font (default: the full font from @kyralabs/m3e-tokens)`;
|
|
89
|
+
var title = (style) => style[0].toUpperCase() + style.slice(1);
|
|
90
|
+
async function subsetCommand(argv) {
|
|
91
|
+
const { values } = parseArgs({
|
|
92
|
+
args: argv,
|
|
93
|
+
options: {
|
|
94
|
+
names: { type: "string" },
|
|
95
|
+
"names-file": { type: "string" },
|
|
96
|
+
style: { type: "string", default: "rounded" },
|
|
97
|
+
fill: { type: "string" },
|
|
98
|
+
wght: { type: "string" },
|
|
99
|
+
grad: { type: "string" },
|
|
100
|
+
opsz: { type: "string" },
|
|
101
|
+
out: { type: "string", default: "material-symbols-subset.woff2" },
|
|
102
|
+
css: { type: "string" },
|
|
103
|
+
family: { type: "string" },
|
|
104
|
+
font: { type: "string" },
|
|
105
|
+
help: { type: "boolean", short: "h" }
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
if (values.help) {
|
|
109
|
+
console.log(USAGE);
|
|
110
|
+
return 0;
|
|
111
|
+
}
|
|
112
|
+
const style = values.style;
|
|
113
|
+
if (!STYLES.includes(style)) throw new Error(`unknown --style "${values.style}"`);
|
|
114
|
+
const names = [
|
|
115
|
+
...values.names ? parseNames(values.names) : [],
|
|
116
|
+
...values["names-file"] ? parseNames(readFileSync(values["names-file"], "utf8")) : []
|
|
117
|
+
];
|
|
118
|
+
if (names.length === 0) {
|
|
119
|
+
console.error(USAGE);
|
|
120
|
+
return 2;
|
|
121
|
+
}
|
|
122
|
+
const require2 = createRequire(import.meta.url);
|
|
123
|
+
const fontPath = values.font ?? require2.resolve(`@kyralabs/m3e-tokens/fonts/material-symbols-${style}.woff2`);
|
|
124
|
+
const codepointsPath = require2.resolve(
|
|
125
|
+
`@kyralabs/m3e-tokens/fonts/material-symbols-${style}.codepoints`
|
|
126
|
+
);
|
|
127
|
+
const axes = {};
|
|
128
|
+
for (const [flag, tag] of [
|
|
129
|
+
["fill", "FILL"],
|
|
130
|
+
["wght", "wght"],
|
|
131
|
+
["grad", "GRAD"],
|
|
132
|
+
["opsz", "opsz"]
|
|
133
|
+
]) {
|
|
134
|
+
const value = values[flag];
|
|
135
|
+
if (value !== void 0) axes[tag] = parseAxis(value);
|
|
136
|
+
}
|
|
137
|
+
const woff2 = await subsetIcons({
|
|
138
|
+
font: readFileSync(fontPath),
|
|
139
|
+
codepoints: parseCodepoints(readFileSync(codepointsPath, "utf8")),
|
|
140
|
+
names,
|
|
141
|
+
axes
|
|
142
|
+
});
|
|
143
|
+
const out = resolve(values.out);
|
|
144
|
+
mkdirSync(dirname(out), { recursive: true });
|
|
145
|
+
writeFileSync(out, woff2);
|
|
146
|
+
console.log(
|
|
147
|
+
`${relative(process.cwd(), out)}: ${String(new Set(names).size)} icons, ${String(woff2.length)} bytes`
|
|
148
|
+
);
|
|
149
|
+
if (values.css) {
|
|
150
|
+
const cssPath = resolve(values.css);
|
|
151
|
+
mkdirSync(dirname(cssPath), { recursive: true });
|
|
152
|
+
const url = relative(dirname(cssPath), out).replace(/\\/g, "/");
|
|
153
|
+
writeFileSync(
|
|
154
|
+
cssPath,
|
|
155
|
+
iconFontFace({
|
|
156
|
+
family: values.family ?? `Material Symbols ${title(style)}`,
|
|
157
|
+
url: url.startsWith(".") ? url : `./${url}`
|
|
158
|
+
})
|
|
159
|
+
);
|
|
160
|
+
console.log(`${relative(process.cwd(), cssPath)}: @font-face`);
|
|
161
|
+
}
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
|
164
|
+
async function main(argv) {
|
|
165
|
+
const [command, ...rest] = argv;
|
|
166
|
+
if (command === "subset") return subsetCommand(rest);
|
|
167
|
+
console.error(USAGE);
|
|
168
|
+
return command === "--help" || command === "-h" ? 0 : 2;
|
|
169
|
+
}
|
|
170
|
+
main(process.argv.slice(2)).then(
|
|
171
|
+
(code) => process.exitCode = code,
|
|
172
|
+
(error) => {
|
|
173
|
+
console.error(
|
|
174
|
+
error instanceof UnknownIconError || error instanceof Error ? error.message : error
|
|
175
|
+
);
|
|
176
|
+
process.exitCode = 1;
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
//# sourceMappingURL=cli.js.map
|
|
180
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/subset.ts","../src/args.ts","../src/cli.ts"],"names":["require"],"mappings":";;;;;;;AA2BO,SAAS,gBAAgB,IAAA,EAAmC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EAAA,OAAO,OAAA,KAAY,QAAA,IAAY,OAAA,KAAY,IAAA,GAAO,CAAA,GAAI,CAAA;AACxD;AAEA,IAAA,CAAK,OAAA,CAAQ,IAAA,CAAK,KAAA,CAAM,CAAC,CAAC,CAAA,CAAE,IAAA;AAAA,EAC1B,CAAC,IAAA,KAAU,OAAA,CAAQ,QAAA,GAAW,IAAA;AAAA,EAC9B,CAAC,KAAA,KAAmB;AAClB,IAAA,OAAA,CAAQ,KAAA;AAAA,MACN,KAAA,YAAiB,gBAAA,IAAoB,KAAA,YAAiB,KAAA,GAAQ,MAAM,OAAA,GAAU;AAAA,KAChF;AACA,IAAA,OAAA,CAAQ,QAAA,GAAW,CAAA;AAAA,EACrB;AACF,CAAA","file":"cli.js","sourcesContent":["// Material Symbols subsetting (D5): keep only the requested icons, reachable both by ligature\n// (\"home\") and by their private-use code point.\n//\n// HarfBuzz runs with layout closure *off*: the subset keeps the letters of the requested names\n// plus the requested icon glyphs, and every ligature whose output glyph was not requested is\n// pruned. With closure on, the letters of \"home\" + \"menu\" + \"search\" would also pull in\n// \"house\", \"mouse\", … (measured: 4 icons → 4.7 KB without closure vs 141 KB with).\nimport subsetFont from 'subset-font';\n\nexport type AxisTag = 'FILL' | 'GRAD' | 'opsz' | 'wght';\n/** A pinned value, or a narrowed range to keep variable. */\nexport type AxisSetting = number | { min: number; max: number; default?: number };\n\nexport interface SubsetIconsOptions {\n /** Source font (woff2/ttf), e.g. `@kyralabs/m3e-tokens/fonts/material-symbols-rounded.woff2`. */\n font: Uint8Array;\n /** Icon name → code point, from the matching `.codepoints` file. */\n codepoints: ReadonlyMap<string, number>;\n /** Icon names, e.g. `['home', 'arrow_back']`. */\n names: readonly string[];\n /** Pin or narrow variation axes to shrink the output further. */\n axes?: Partial<Record<AxisTag, AxisSetting>>;\n /** Output format. Defaults to woff2. */\n format?: 'woff2' | 'truetype';\n}\n\n/** Parses a Material Symbols `.codepoints` file (`name hex` per line). */\nexport function parseCodepoints(text: string): Map<string, number> {\n const map = new Map<string, number>();\n for (const line of text.split(/\\r?\\n/)) {\n const m = /^(\\S+) ([0-9a-f]+)$/i.exec(line.trim());\n if (m) map.set(m[1]!, parseInt(m[2]!, 16));\n }\n return map;\n}\n\nexport class UnknownIconError extends Error {\n constructor(readonly names: readonly string[]) {\n super(`Unknown Material Symbols icon name(s): ${names.join(', ')}`);\n this.name = 'UnknownIconError';\n }\n}\n\n/** The text HarfBuzz must keep: every character of the names plus each icon's code point. */\nexport function subsetText(\n names: readonly string[],\n codepoints: ReadonlyMap<string, number>,\n): string {\n const unique = [...new Set(names)].sort();\n const unknown = unique.filter((n) => !codepoints.has(n));\n if (unknown.length > 0) throw new UnknownIconError(unknown);\n const chars = new Set<string>();\n for (const name of unique) {\n for (const ch of name) chars.add(ch);\n chars.add(String.fromCodePoint(codepoints.get(name) ?? 0));\n }\n return [...chars].sort().join('');\n}\n\nexport async function subsetIcons(options: SubsetIconsOptions): Promise<Uint8Array> {\n const { font, codepoints, names, axes, format = 'woff2' } = options;\n if (names.length === 0) throw new Error('subsetIcons: no icon names given');\n return subsetFont(font, subsetText(names, codepoints), {\n targetFormat: format,\n noLayoutClosure: true,\n ...(axes && Object.keys(axes).length > 0 ? { variationAxes: axes } : {}),\n });\n}\n\nexport interface IconFontFaceOptions {\n family: string;\n /** URL of the woff2, relative to the CSS file. */\n url: string;\n}\n\n/** `@font-face` for a subset font. Declares Material Symbols' full weight range so browsers never\n * synthesize bold, even when wght is pinned. Also points `.m3e-icon` at the subset through\n * `--m3e-ref-typeface-icon`, as `material-symbols*.css` does for the full fonts. */\nexport function iconFontFace({ family, url }: IconFontFaceOptions): string {\n return [\n '@font-face {',\n ` font-family: \"${family}\";`,\n ' font-style: normal;',\n ' font-weight: 100 700;',\n ` src: url(\"${url}\") format(\"woff2\");`,\n '}',\n '',\n '@layer m3e.tokens {',\n ' :root {',\n ` --m3e-ref-typeface-icon: \"${family}\";`,\n ' }',\n '}',\n '',\n ].join('\\n');\n}\n","import type { AxisSetting } from './subset.js';\n\n/** `400` → 400; `300..500` → { min: 300, max: 500 }. */\nexport function parseAxis(value: string): AxisSetting {\n const range = /^(-?\\d+(?:\\.\\d+)?)\\.\\.(-?\\d+(?:\\.\\d+)?)$/.exec(value);\n if (range) return { min: Number(range[1]), max: Number(range[2]) };\n const n = Number(value);\n if (value.trim() === '' || Number.isNaN(n)) throw new Error(`invalid axis value \"${value}\"`);\n return n;\n}\n\nexport function parseNames(text: string): string[] {\n return text\n .split(/\\r?\\n/)\n .map((line) => line.replace(/#.*$/, ''))\n .flatMap((line) => line.split(','))\n .map((n) => n.trim())\n .filter(Boolean);\n}\n","// m3e-icons subset --names home,search,menu [options]\nimport { mkdirSync, readFileSync, writeFileSync } from 'node:fs';\nimport { createRequire } from 'node:module';\nimport { dirname, relative, resolve } from 'node:path';\nimport { parseArgs } from 'node:util';\nimport {\n type AxisSetting,\n type AxisTag,\n UnknownIconError,\n iconFontFace,\n parseCodepoints,\n subsetIcons,\n} from './subset.js';\nimport { parseAxis, parseNames } from './args.js';\n\nconst STYLES = ['rounded', 'outlined', 'sharp'] as const;\ntype Style = (typeof STYLES)[number];\n\nconst USAGE = `usage: m3e-icons subset --names <a,b,…> [options]\n\n --names comma-separated icon names (e.g. home,arrow_back)\n --names-file file with names (comma/newline separated, # comments)\n --style ${STYLES.join(' | ')} (default rounded)\n --fill pin (0 or 1) or narrow (0..1) the FILL axis\n --wght pin (e.g. 400) or narrow (e.g. 300..500) the weight axis\n --grad pin or narrow the GRAD axis (-50..200)\n --opsz pin or narrow the optical size axis (20..48)\n --out output woff2 (default material-symbols-subset.woff2)\n --css also write @font-face CSS here (url relative to the CSS file)\n --family font-family in the CSS (default \"Material Symbols <Style>\": drop-in)\n --font source font (default: the full font from @kyralabs/m3e-tokens)`;\n\nconst title = (style: Style) => style[0]!.toUpperCase() + style.slice(1);\n\nasync function subsetCommand(argv: string[]): Promise<number> {\n const { values } = parseArgs({\n args: argv,\n options: {\n names: { type: 'string' },\n 'names-file': { type: 'string' },\n style: { type: 'string', default: 'rounded' },\n fill: { type: 'string' },\n wght: { type: 'string' },\n grad: { type: 'string' },\n opsz: { type: 'string' },\n out: { type: 'string', default: 'material-symbols-subset.woff2' },\n css: { type: 'string' },\n family: { type: 'string' },\n font: { type: 'string' },\n help: { type: 'boolean', short: 'h' },\n },\n });\n if (values.help) {\n console.log(USAGE);\n return 0;\n }\n const style = values.style as Style;\n if (!STYLES.includes(style)) throw new Error(`unknown --style \"${values.style}\"`);\n const names = [\n ...(values.names ? parseNames(values.names) : []),\n ...(values['names-file'] ? parseNames(readFileSync(values['names-file'], 'utf8')) : []),\n ];\n if (names.length === 0) {\n console.error(USAGE);\n return 2;\n }\n const require = createRequire(import.meta.url);\n const fontPath =\n values.font ?? require.resolve(`@kyralabs/m3e-tokens/fonts/material-symbols-${style}.woff2`);\n const codepointsPath = require.resolve(\n `@kyralabs/m3e-tokens/fonts/material-symbols-${style}.codepoints`,\n );\n const axes: Partial<Record<AxisTag, AxisSetting>> = {};\n for (const [flag, tag] of [\n ['fill', 'FILL'],\n ['wght', 'wght'],\n ['grad', 'GRAD'],\n ['opsz', 'opsz'],\n ] as const) {\n const value = values[flag];\n if (value !== undefined) axes[tag] = parseAxis(value);\n }\n\n const woff2 = await subsetIcons({\n font: readFileSync(fontPath),\n codepoints: parseCodepoints(readFileSync(codepointsPath, 'utf8')),\n names,\n axes,\n });\n const out = resolve(values.out);\n mkdirSync(dirname(out), { recursive: true });\n writeFileSync(out, woff2);\n console.log(\n `${relative(process.cwd(), out)}: ${String(new Set(names).size)} icons, ${String(woff2.length)} bytes`,\n );\n if (values.css) {\n const cssPath = resolve(values.css);\n mkdirSync(dirname(cssPath), { recursive: true });\n const url = relative(dirname(cssPath), out).replace(/\\\\/g, '/');\n writeFileSync(\n cssPath,\n iconFontFace({\n family: values.family ?? `Material Symbols ${title(style)}`,\n url: url.startsWith('.') ? url : `./${url}`,\n }),\n );\n console.log(`${relative(process.cwd(), cssPath)}: @font-face`);\n }\n return 0;\n}\n\nasync function main(argv: string[]): Promise<number> {\n const [command, ...rest] = argv;\n if (command === 'subset') return subsetCommand(rest);\n console.error(USAGE);\n return command === '--help' || command === '-h' ? 0 : 2;\n}\n\nmain(process.argv.slice(2)).then(\n (code) => (process.exitCode = code),\n (error: unknown) => {\n console.error(\n error instanceof UnknownIconError || error instanceof Error ? error.message : error,\n );\n process.exitCode = 1;\n },\n);\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
type AxisTag = 'FILL' | 'GRAD' | 'opsz' | 'wght';
|
|
2
|
+
/** A pinned value, or a narrowed range to keep variable. */
|
|
3
|
+
type AxisSetting = number | {
|
|
4
|
+
min: number;
|
|
5
|
+
max: number;
|
|
6
|
+
default?: number;
|
|
7
|
+
};
|
|
8
|
+
interface SubsetIconsOptions {
|
|
9
|
+
/** Source font (woff2/ttf), e.g. `@kyralabs/m3e-tokens/fonts/material-symbols-rounded.woff2`. */
|
|
10
|
+
font: Uint8Array;
|
|
11
|
+
/** Icon name → code point, from the matching `.codepoints` file. */
|
|
12
|
+
codepoints: ReadonlyMap<string, number>;
|
|
13
|
+
/** Icon names, e.g. `['home', 'arrow_back']`. */
|
|
14
|
+
names: readonly string[];
|
|
15
|
+
/** Pin or narrow variation axes to shrink the output further. */
|
|
16
|
+
axes?: Partial<Record<AxisTag, AxisSetting>>;
|
|
17
|
+
/** Output format. Defaults to woff2. */
|
|
18
|
+
format?: 'woff2' | 'truetype';
|
|
19
|
+
}
|
|
20
|
+
/** Parses a Material Symbols `.codepoints` file (`name hex` per line). */
|
|
21
|
+
declare function parseCodepoints(text: string): Map<string, number>;
|
|
22
|
+
declare class UnknownIconError extends Error {
|
|
23
|
+
readonly names: readonly string[];
|
|
24
|
+
constructor(names: readonly string[]);
|
|
25
|
+
}
|
|
26
|
+
/** The text HarfBuzz must keep: every character of the names plus each icon's code point. */
|
|
27
|
+
declare function subsetText(names: readonly string[], codepoints: ReadonlyMap<string, number>): string;
|
|
28
|
+
declare function subsetIcons(options: SubsetIconsOptions): Promise<Uint8Array>;
|
|
29
|
+
interface IconFontFaceOptions {
|
|
30
|
+
family: string;
|
|
31
|
+
/** URL of the woff2, relative to the CSS file. */
|
|
32
|
+
url: string;
|
|
33
|
+
}
|
|
34
|
+
/** `@font-face` for a subset font. Declares Material Symbols' full weight range so browsers never
|
|
35
|
+
* synthesize bold, even when wght is pinned. Also points `.m3e-icon` at the subset through
|
|
36
|
+
* `--m3e-ref-typeface-icon`, as `material-symbols*.css` does for the full fonts. */
|
|
37
|
+
declare function iconFontFace({ family, url }: IconFontFaceOptions): string;
|
|
38
|
+
|
|
39
|
+
export { type AxisSetting, type AxisTag, type IconFontFaceOptions, type SubsetIconsOptions, UnknownIconError, iconFontFace, parseCodepoints, subsetIcons, subsetText };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import subsetFont from 'subset-font';
|
|
2
|
+
|
|
3
|
+
// src/subset.ts
|
|
4
|
+
function parseCodepoints(text) {
|
|
5
|
+
const map = /* @__PURE__ */ new Map();
|
|
6
|
+
for (const line of text.split(/\r?\n/)) {
|
|
7
|
+
const m = /^(\S+) ([0-9a-f]+)$/i.exec(line.trim());
|
|
8
|
+
if (m) map.set(m[1], parseInt(m[2], 16));
|
|
9
|
+
}
|
|
10
|
+
return map;
|
|
11
|
+
}
|
|
12
|
+
var UnknownIconError = class extends Error {
|
|
13
|
+
constructor(names) {
|
|
14
|
+
super(`Unknown Material Symbols icon name(s): ${names.join(", ")}`);
|
|
15
|
+
this.names = names;
|
|
16
|
+
this.name = "UnknownIconError";
|
|
17
|
+
}
|
|
18
|
+
names;
|
|
19
|
+
};
|
|
20
|
+
function subsetText(names, codepoints) {
|
|
21
|
+
const unique = [...new Set(names)].sort();
|
|
22
|
+
const unknown = unique.filter((n) => !codepoints.has(n));
|
|
23
|
+
if (unknown.length > 0) throw new UnknownIconError(unknown);
|
|
24
|
+
const chars = /* @__PURE__ */ new Set();
|
|
25
|
+
for (const name of unique) {
|
|
26
|
+
for (const ch of name) chars.add(ch);
|
|
27
|
+
chars.add(String.fromCodePoint(codepoints.get(name) ?? 0));
|
|
28
|
+
}
|
|
29
|
+
return [...chars].sort().join("");
|
|
30
|
+
}
|
|
31
|
+
async function subsetIcons(options) {
|
|
32
|
+
const { font, codepoints, names, axes, format = "woff2" } = options;
|
|
33
|
+
if (names.length === 0) throw new Error("subsetIcons: no icon names given");
|
|
34
|
+
return subsetFont(font, subsetText(names, codepoints), {
|
|
35
|
+
targetFormat: format,
|
|
36
|
+
noLayoutClosure: true,
|
|
37
|
+
...axes && Object.keys(axes).length > 0 ? { variationAxes: axes } : {}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function iconFontFace({ family, url }) {
|
|
41
|
+
return [
|
|
42
|
+
"@font-face {",
|
|
43
|
+
` font-family: "${family}";`,
|
|
44
|
+
" font-style: normal;",
|
|
45
|
+
" font-weight: 100 700;",
|
|
46
|
+
` src: url("${url}") format("woff2");`,
|
|
47
|
+
"}",
|
|
48
|
+
"",
|
|
49
|
+
"@layer m3e.tokens {",
|
|
50
|
+
" :root {",
|
|
51
|
+
` --m3e-ref-typeface-icon: "${family}";`,
|
|
52
|
+
" }",
|
|
53
|
+
"}",
|
|
54
|
+
""
|
|
55
|
+
].join("\n");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { UnknownIconError, iconFontFace, parseCodepoints, subsetIcons, subsetText };
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
60
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/subset.ts"],"names":[],"mappings":";;;AA2BO,SAAS,gBAAgB,IAAA,EAAmC;AACjE,EAAA,MAAM,GAAA,uBAAU,GAAA,EAAoB;AACpC,EAAA,KAAA,MAAW,IAAA,IAAQ,IAAA,CAAK,KAAA,CAAM,OAAO,CAAA,EAAG;AACtC,IAAA,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AAE,CAAA;AAClC;AAEA,eAAsB,YAAY,OAAA,EAAkD;AAClF,EAAA,MAAM,EAAE,IAAA,EAAM,UAAA,EAAY,OAAO,IAAA,EAAM,MAAA,GAAS,SAAQ,GAAI,OAAA;AAC5D,EAAA,IAAI,MAAM,MAAA,KAAW,CAAA,EAAG,MAAM,IAAI,MAAM,kCAAkC,CAAA;AAC1E,EAAA,OAAO,UAAA,CAAW,IAAA,EAAM,UAAA,CAAW,KAAA,EAAO,UAAU,CAAA,EAAG;AAAA,IACrD,YAAA,EAAc,MAAA;AAAA,IACd,eAAA,EAAiB,IAAA;AAAA,IACjB,GAAI,IAAA,IAAQ,MAAA,CAAO,IAAA,CAAK,IAAI,CAAA,CAAE,MAAA,GAAS,CAAA,GAAI,EAAE,aAAA,EAAe,IAAA,EAAK,GAAI;AAAC,GACvE,CAAA;AACH;AAWO,SAAS,YAAA,CAAa,EAAE,MAAA,EAAQ,GAAA,EAAI,EAAgC;AACzE,EAAA,OAAO;AAAA,IACL,cAAA;AAAA,IACA,mBAAmB,MAAM,CAAA,EAAA,CAAA;AAAA,IACzB,uBAAA;AAAA,IACA,yBAAA;AAAA,IACA,eAAe,GAAG,CAAA,mBAAA,CAAA;AAAA,IAClB,GAAA;AAAA,IACA,EAAA;AAAA,IACA,qBAAA;AAAA,IACA,WAAA;AAAA,IACA,iCAAiC,MAAM,CAAA,EAAA,CAAA;AAAA,IACvC,KAAA;AAAA,IACA,GAAA;AAAA,IACA;AAAA,GACF,CAAE,KAAK,IAAI,CAAA;AACb","file":"index.js","sourcesContent":["// Material Symbols subsetting (D5): keep only the requested icons, reachable both by ligature\n// (\"home\") and by their private-use code point.\n//\n// HarfBuzz runs with layout closure *off*: the subset keeps the letters of the requested names\n// plus the requested icon glyphs, and every ligature whose output glyph was not requested is\n// pruned. With closure on, the letters of \"home\" + \"menu\" + \"search\" would also pull in\n// \"house\", \"mouse\", … (measured: 4 icons → 4.7 KB without closure vs 141 KB with).\nimport subsetFont from 'subset-font';\n\nexport type AxisTag = 'FILL' | 'GRAD' | 'opsz' | 'wght';\n/** A pinned value, or a narrowed range to keep variable. */\nexport type AxisSetting = number | { min: number; max: number; default?: number };\n\nexport interface SubsetIconsOptions {\n /** Source font (woff2/ttf), e.g. `@kyralabs/m3e-tokens/fonts/material-symbols-rounded.woff2`. */\n font: Uint8Array;\n /** Icon name → code point, from the matching `.codepoints` file. */\n codepoints: ReadonlyMap<string, number>;\n /** Icon names, e.g. `['home', 'arrow_back']`. */\n names: readonly string[];\n /** Pin or narrow variation axes to shrink the output further. */\n axes?: Partial<Record<AxisTag, AxisSetting>>;\n /** Output format. Defaults to woff2. */\n format?: 'woff2' | 'truetype';\n}\n\n/** Parses a Material Symbols `.codepoints` file (`name hex` per line). */\nexport function parseCodepoints(text: string): Map<string, number> {\n const map = new Map<string, number>();\n for (const line of text.split(/\\r?\\n/)) {\n const m = /^(\\S+) ([0-9a-f]+)$/i.exec(line.trim());\n if (m) map.set(m[1]!, parseInt(m[2]!, 16));\n }\n return map;\n}\n\nexport class UnknownIconError extends Error {\n constructor(readonly names: readonly string[]) {\n super(`Unknown Material Symbols icon name(s): ${names.join(', ')}`);\n this.name = 'UnknownIconError';\n }\n}\n\n/** The text HarfBuzz must keep: every character of the names plus each icon's code point. */\nexport function subsetText(\n names: readonly string[],\n codepoints: ReadonlyMap<string, number>,\n): string {\n const unique = [...new Set(names)].sort();\n const unknown = unique.filter((n) => !codepoints.has(n));\n if (unknown.length > 0) throw new UnknownIconError(unknown);\n const chars = new Set<string>();\n for (const name of unique) {\n for (const ch of name) chars.add(ch);\n chars.add(String.fromCodePoint(codepoints.get(name) ?? 0));\n }\n return [...chars].sort().join('');\n}\n\nexport async function subsetIcons(options: SubsetIconsOptions): Promise<Uint8Array> {\n const { font, codepoints, names, axes, format = 'woff2' } = options;\n if (names.length === 0) throw new Error('subsetIcons: no icon names given');\n return subsetFont(font, subsetText(names, codepoints), {\n targetFormat: format,\n noLayoutClosure: true,\n ...(axes && Object.keys(axes).length > 0 ? { variationAxes: axes } : {}),\n });\n}\n\nexport interface IconFontFaceOptions {\n family: string;\n /** URL of the woff2, relative to the CSS file. */\n url: string;\n}\n\n/** `@font-face` for a subset font. Declares Material Symbols' full weight range so browsers never\n * synthesize bold, even when wght is pinned. Also points `.m3e-icon` at the subset through\n * `--m3e-ref-typeface-icon`, as `material-symbols*.css` does for the full fonts. */\nexport function iconFontFace({ family, url }: IconFontFaceOptions): string {\n return [\n '@font-face {',\n ` font-family: \"${family}\";`,\n ' font-style: normal;',\n ' font-weight: 100 700;',\n ` src: url(\"${url}\") format(\"woff2\");`,\n '}',\n '',\n '@layer m3e.tokens {',\n ' :root {',\n ` --m3e-ref-typeface-icon: \"${family}\";`,\n ' }',\n '}',\n '',\n ].join('\\n');\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kyralabs/m3e-icons",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Material Symbols subsetting (m3e-icons subset) for m3exp-web.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"material-design",
|
|
7
|
+
"material-3",
|
|
8
|
+
"material-you",
|
|
9
|
+
"m3-expressive",
|
|
10
|
+
"design-system",
|
|
11
|
+
"material-symbols",
|
|
12
|
+
"icons",
|
|
13
|
+
"font-subsetting",
|
|
14
|
+
"cli"
|
|
15
|
+
],
|
|
16
|
+
"license": "Apache-2.0",
|
|
17
|
+
"author": "Kyra Labs",
|
|
18
|
+
"homepage": "https://github.com/kyra-labs/m3exp-web/tree/main/packages/m3e-icons#readme",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/kyra-labs/m3exp-web",
|
|
22
|
+
"directory": "packages/m3e-icons"
|
|
23
|
+
},
|
|
24
|
+
"bugs": "https://github.com/kyra-labs/m3exp-web/issues",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"bin": {
|
|
31
|
+
"m3e-icons": "./dist/cli.js"
|
|
32
|
+
},
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"@kyralabs/source": "./src/index.ts",
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"import": "./dist/index.js",
|
|
38
|
+
"default": "./dist/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./package.json": "./package.json"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist",
|
|
44
|
+
"LICENSE",
|
|
45
|
+
"NOTICE",
|
|
46
|
+
"README.md"
|
|
47
|
+
],
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public",
|
|
50
|
+
"provenance": true
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@kyralabs/m3e-tokens": "^1.0.0",
|
|
54
|
+
"subset-font": "2.9.0"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"harfbuzzjs": "1.6.3"
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsup",
|
|
61
|
+
"typecheck": "tsc --noEmit -p tsconfig.json"
|
|
62
|
+
}
|
|
6
63
|
}
|