react-native-nano-icons 1.0.1 → 1.1.0-beta.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.
- package/README.md +139 -21
- package/android/src/main/java/com/nanoicons/NanoIconFonts.kt +15 -0
- package/android/src/main/java/com/nanoicons/NanoIconView.kt +27 -4
- package/android/src/main/java/com/nanoicons/NanoIconViewManager.kt +7 -0
- package/android/src/main/java/com/nanoicons/NanoIconsFontLoaderModule.kt +3 -0
- package/common/cpp/react/renderer/components/RNNanoIconsSpec/NanoIconProps.cpp +6 -1
- package/common/cpp/react/renderer/components/RNNanoIconsSpec/NanoIconProps.h +4 -2
- package/ios/NanoIconView.mm +34 -44
- package/ios/NanoIconsFontLoader.mm +5 -0
- package/lib/commonjs/cli/build.d.ts +26 -0
- package/lib/commonjs/cli/build.js +174 -64
- package/lib/commonjs/cli/buildSymbols.d.ts +30 -1
- package/lib/commonjs/cli/buildSymbols.js +120 -42
- package/lib/commonjs/cli/expoConfig.d.ts +11 -0
- package/lib/commonjs/cli/expoConfig.js +28 -9
- package/lib/commonjs/cli/fingerprint.d.ts +15 -0
- package/lib/commonjs/cli/fingerprint.js +42 -0
- package/lib/commonjs/cli/index.d.ts +2 -2
- package/lib/commonjs/cli/index.js +3 -1
- package/lib/commonjs/cli/link.d.ts +4 -0
- package/lib/commonjs/cli/link.js +38 -1
- package/lib/commonjs/cli/sameOutputs.d.ts +5 -0
- package/lib/commonjs/cli/sameOutputs.js +45 -0
- package/lib/commonjs/index.node.js +1 -1
- package/lib/commonjs/plugin/src/buildFonts.d.ts +3 -2
- package/lib/commonjs/plugin/src/buildFonts.js +11 -3
- package/lib/commonjs/plugin/src/buildSymbols.d.ts +2 -2
- package/lib/commonjs/plugin/src/buildSymbols.js +10 -3
- package/lib/commonjs/plugin/src/frozen.d.ts +3 -0
- package/lib/commonjs/plugin/src/frozen.js +18 -0
- package/lib/commonjs/plugin/src/index.js +3 -3
- package/lib/commonjs/plugin/src/types.d.ts +22 -1
- package/lib/commonjs/plugin/src/withNanoIconsDrawableLinking.d.ts +1 -1
- package/lib/commonjs/plugin/src/withNanoIconsDrawableLinking.js +2 -2
- package/lib/commonjs/plugin/src/withNanoIconsFontLinking.d.ts +3 -3
- package/lib/commonjs/plugin/src/withNanoIconsFontLinking.js +8 -8
- package/lib/commonjs/plugin/src/withNanoIconsSymbolLinking.d.ts +1 -1
- package/lib/commonjs/plugin/src/withNanoIconsSymbolLinking.js +2 -2
- package/lib/commonjs/scripts/cli.js +31 -2
- package/lib/commonjs/src/core/flatten/dom.js +40 -5
- package/lib/commonjs/src/core/flatten/element.js +2 -1
- package/lib/commonjs/src/core/flatten/stages/collapse.js +12 -0
- package/lib/commonjs/src/core/font/compile.d.ts +0 -1
- package/lib/commonjs/src/core/font/compile.js +2 -8
- package/lib/commonjs/src/core/glyph/merge.d.ts +2 -1
- package/lib/commonjs/src/core/glyph/merge.js +27 -36
- package/lib/commonjs/src/core/glyph/parse.d.ts +1 -2
- package/lib/commonjs/src/core/glyph/parse.js +31 -18
- package/lib/commonjs/src/core/glyph/placement.d.ts +1 -0
- package/lib/commonjs/src/core/glyph/placement.js +9 -3
- package/lib/commonjs/src/core/pathkit/types.d.ts +1 -0
- package/lib/commonjs/src/core/pathkit/winding.d.ts +14 -3
- package/lib/commonjs/src/core/pathkit/winding.js +434 -7
- package/lib/commonjs/src/core/pipeline/config.d.ts +1 -0
- package/lib/commonjs/src/core/pipeline/prepare.d.ts +1 -1
- package/lib/commonjs/src/core/pipeline/prepare.js +4 -16
- package/lib/commonjs/src/core/pipeline/prepareIcon.d.ts +1 -0
- package/lib/commonjs/src/core/pipeline/prepareIcon.js +3 -1
- package/lib/commonjs/src/core/pipeline/prepareSymbol.d.ts +2 -0
- package/lib/commonjs/src/core/pipeline/prepareSymbol.js +15 -3
- package/lib/commonjs/src/core/pipeline/preparedSvgCache.js +2 -2
- package/lib/commonjs/src/core/pipeline/runFontPipeline.js +20 -12
- package/lib/commonjs/src/core/pipeline/runSymbolPipeline.d.ts +2 -0
- package/lib/commonjs/src/core/pipeline/runSymbolPipeline.js +2 -0
- package/lib/commonjs/src/core/symbols/contents.d.ts +1 -1
- package/lib/commonjs/src/core/symbols/contents.js +6 -2
- package/lib/commonjs/src/core/symbols/knockout.js +4 -3
- package/lib/commonjs/src/core/symbols/layers.d.ts +1 -0
- package/lib/commonjs/src/core/symbols/layers.js +133 -92
- package/lib/commonjs/src/core/symbols/template.d.ts +8 -3
- package/lib/commonjs/src/core/symbols/template.js +23 -8
- package/lib/commonjs/src/core/symbols/vectorDrawable.d.ts +8 -0
- package/lib/commonjs/src/core/symbols/vectorDrawable.js +28 -2
- package/lib/commonjs/src/utils/fingerPrint.d.ts +3 -0
- package/lib/commonjs/src/utils/fingerPrint.js +5 -1
- package/lib/commonjs/src/utils/fontIdentity.d.ts +1 -1
- package/lib/commonjs/src/utils/fontIdentity.js +2 -2
- package/lib/module/createNanoIconsSet.js +0 -1
- package/lib/module/createNanoIconsSet.native.js +22 -18
- package/lib/module/createNanoIconsSet.shared.js +32 -18
- package/lib/module/createNanoIconsSet.web.js +103 -61
- package/lib/module/devServerFont.js +1 -1
- package/lib/module/devServerFont.web.js +3 -5
- package/lib/module/fontIntegrity.js +20 -38
- package/lib/module/fontIntegrity.web.js +48 -0
- package/lib/module/fontIntegrityStore.js +65 -0
- package/lib/module/index.js +1 -1
- package/lib/module/index.node.js +1 -1
- package/lib/module/index.web.js +4 -0
- package/lib/module/loadDynamicFont.js +3 -15
- package/lib/module/specs/NanoIconViewNativeComponent.ts +2 -1
- package/lib/module/utils/fingerPrint.js +4 -3
- package/lib/module/utils/fontIdentity.js +2 -2
- package/lib/module/utils/fontScale.js +10 -2
- package/lib/module/utils/glyphRuntime.js +13 -0
- package/lib/module/utils/iconAccessibility.js +26 -0
- package/lib/module/utils/iconPropsEqual.js +27 -0
- package/lib/module/utils/webCss.js +26 -0
- package/lib/module/utils/webStyle.js +86 -0
- package/lib/module/webFont.js +33 -0
- package/lib/typescript/src/const/codegenPrimitives.d.ts +1 -0
- package/lib/typescript/src/core/glyph/merge.d.ts +2 -1
- package/lib/typescript/src/core/glyph/parse.d.ts +1 -2
- package/lib/typescript/src/core/glyph/placement.d.ts +1 -0
- package/lib/typescript/src/core/pathkit/types.d.ts +1 -0
- package/lib/typescript/src/core/pathkit/winding.d.ts +14 -3
- package/lib/typescript/src/core/symbols/contents.d.ts +1 -1
- package/lib/typescript/src/core/symbols/layers.d.ts +1 -0
- package/lib/typescript/src/core/symbols/template.d.ts +8 -3
- package/lib/typescript/src/core/symbols/vectorDrawable.d.ts +8 -0
- package/lib/typescript/src/createNanoIconsSet.d.ts +0 -1
- package/lib/typescript/src/createNanoIconsSet.native.d.ts +0 -2
- package/lib/typescript/src/createNanoIconsSet.shared.d.ts +1 -3
- package/lib/typescript/src/createNanoIconsSet.web.d.ts +1 -2
- package/lib/typescript/src/fontIntegrity.d.ts +2 -14
- package/lib/typescript/src/fontIntegrity.web.d.ts +7 -0
- package/lib/typescript/src/fontIntegrityStore.d.ts +19 -0
- package/lib/typescript/src/index.d.ts +2 -1
- package/lib/typescript/src/index.web.d.ts +5 -0
- package/lib/typescript/src/loadDynamicFont.d.ts +0 -1
- package/lib/typescript/src/specs/NanoIconViewNativeComponent.d.ts +2 -1
- package/lib/typescript/src/specs/NativeNanoIconsFontLoader.d.ts +1 -0
- package/lib/typescript/src/types.d.ts +37 -9
- package/lib/typescript/src/utils/fingerPrint.d.ts +3 -0
- package/lib/typescript/src/utils/fontIdentity.d.ts +1 -1
- package/lib/typescript/src/utils/glyphRuntime.d.ts +8 -0
- package/lib/typescript/src/utils/iconAccessibility.d.ts +4 -0
- package/lib/typescript/src/utils/iconPropsEqual.d.ts +4 -0
- package/lib/typescript/src/utils/webCss.d.ts +9 -0
- package/lib/typescript/src/utils/webStyle.d.ts +3 -0
- package/lib/typescript/src/webFont.d.ts +6 -0
- package/package.json +19 -3
- package/src/const/codegenPrimitives.ts +3 -3
- package/src/specs/NanoIconViewNativeComponent.ts +2 -1
- package/src/specs/NativeNanoIconsFontLoader.ts +1 -0
- package/lib/commonjs/src/core/pathkit/evenodd.d.ts +0 -12
- package/lib/commonjs/src/core/pathkit/evenodd.js +0 -37
- package/lib/typescript/src/core/pathkit/evenodd.d.ts +0 -13
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.orientedPathData = orientedPathData;
|
|
4
|
+
exports.touchTolerance = touchTolerance;
|
|
5
|
+
exports.analyzeWinding = analyzeWinding;
|
|
6
|
+
exports.reversedContour = reversedContour;
|
|
7
|
+
exports.pathDataWithWinding = pathDataWithWinding;
|
|
8
|
+
exports.rasterSafePathData = rasterSafePathData;
|
|
4
9
|
exports.canonicalWinding = canonicalWinding;
|
|
5
10
|
exports.reversedWinding = reversedWinding;
|
|
11
|
+
exports.pathOpData = pathOpData;
|
|
6
12
|
const contours_1 = require("./contours");
|
|
7
13
|
function conicsToCubics(cmds, V) {
|
|
8
14
|
let x = 0;
|
|
@@ -40,20 +46,441 @@ function orientedContoursOf(PathKit, path) {
|
|
|
40
46
|
function orientedPathData(PathKit, path) {
|
|
41
47
|
return (0, contours_1.cmdsToPathData)(orientedContoursOf(PathKit, path).flatMap((c) => c.cmds), (0, contours_1.verbMap)(PathKit));
|
|
42
48
|
}
|
|
43
|
-
|
|
49
|
+
// Touch tolerance is the viewBox size divided by this.
|
|
50
|
+
const VIEWBOX_SIZES_PER_TOUCH_TOLERANCE = 12000;
|
|
51
|
+
const TOUCH_TOLERANCE_ON_24_UNIT_VIEWBOX = 24 / VIEWBOX_SIZES_PER_TOUCH_TOLERANCE;
|
|
52
|
+
// Distance below which two contours count as touching.
|
|
53
|
+
function touchTolerance(viewBox) {
|
|
54
|
+
return Math.max(viewBox[2], viewBox[3]) / VIEWBOX_SIZES_PER_TOUCH_TOLERANCE;
|
|
55
|
+
}
|
|
56
|
+
function boxOf(points) {
|
|
57
|
+
let minX = Infinity;
|
|
58
|
+
let minY = Infinity;
|
|
59
|
+
let maxX = -Infinity;
|
|
60
|
+
let maxY = -Infinity;
|
|
61
|
+
for (const [x, y] of points) {
|
|
62
|
+
if (x < minX)
|
|
63
|
+
minX = x;
|
|
64
|
+
if (y < minY)
|
|
65
|
+
minY = y;
|
|
66
|
+
if (x > maxX)
|
|
67
|
+
maxX = x;
|
|
68
|
+
if (y > maxY)
|
|
69
|
+
maxY = y;
|
|
70
|
+
}
|
|
71
|
+
return [minX, minY, maxX, maxY];
|
|
72
|
+
}
|
|
73
|
+
// Signed area of a polygon; the sign follows its direction.
|
|
74
|
+
function polygonArea(polygon) {
|
|
75
|
+
let doubled = 0;
|
|
76
|
+
for (const [i, [x, y]] of polygon.entries()) {
|
|
77
|
+
const [nextX, nextY] = polygon[(i + 1) % polygon.length];
|
|
78
|
+
doubled += x * nextY - nextX * y;
|
|
79
|
+
}
|
|
80
|
+
return doubled / 2;
|
|
81
|
+
}
|
|
82
|
+
function boxesOverlap(a, b, margin = 0) {
|
|
83
|
+
return (a[0] <= b[2] + margin &&
|
|
84
|
+
b[0] <= a[2] + margin &&
|
|
85
|
+
a[1] <= b[3] + margin &&
|
|
86
|
+
b[1] <= a[3] + margin);
|
|
87
|
+
}
|
|
88
|
+
function distanceToSegment(p, a, b) {
|
|
89
|
+
const dx = b[0] - a[0];
|
|
90
|
+
const dy = b[1] - a[1];
|
|
91
|
+
const lengthSq = dx * dx + dy * dy;
|
|
92
|
+
const t = lengthSq === 0
|
|
93
|
+
? 0
|
|
94
|
+
: Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / lengthSq));
|
|
95
|
+
return Math.hypot(p[0] - (a[0] + t * dx), p[1] - (a[1] + t * dy));
|
|
96
|
+
}
|
|
97
|
+
function cross(o, a, b) {
|
|
98
|
+
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
|
|
99
|
+
}
|
|
100
|
+
function segmentsTouch(a, b, c, d, tolerance) {
|
|
101
|
+
if (Math.sign(cross(a, b, c)) * Math.sign(cross(a, b, d)) < 0 &&
|
|
102
|
+
Math.sign(cross(c, d, a)) * Math.sign(cross(c, d, b)) < 0) {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
return (distanceToSegment(a, c, d) <= tolerance ||
|
|
106
|
+
distanceToSegment(b, c, d) <= tolerance ||
|
|
107
|
+
distanceToSegment(c, a, b) <= tolerance ||
|
|
108
|
+
distanceToSegment(d, a, b) <= tolerance);
|
|
109
|
+
}
|
|
110
|
+
function distance(a, b) {
|
|
111
|
+
return Math.hypot(a[0] - b[0], a[1] - b[1]);
|
|
112
|
+
}
|
|
113
|
+
function ringOf(polyline, tolerance) {
|
|
114
|
+
const ring = [];
|
|
115
|
+
for (const p of polyline) {
|
|
116
|
+
const last = ring[ring.length - 1];
|
|
117
|
+
if (!last || distance(p, last) > tolerance)
|
|
118
|
+
ring.push(p);
|
|
119
|
+
}
|
|
120
|
+
if (ring.length > 1 &&
|
|
121
|
+
distance(ring[0], ring[ring.length - 1]) <= tolerance) {
|
|
122
|
+
ring.pop();
|
|
123
|
+
}
|
|
124
|
+
return ring;
|
|
125
|
+
}
|
|
126
|
+
function contoursTouch(rings, tolerance) {
|
|
127
|
+
const edges = [];
|
|
128
|
+
for (const [ring, points] of rings.entries()) {
|
|
129
|
+
if (points.length < 3)
|
|
130
|
+
continue;
|
|
131
|
+
for (const [index, a] of points.entries()) {
|
|
132
|
+
const b = points[(index + 1) % points.length];
|
|
133
|
+
edges.push({
|
|
134
|
+
ring,
|
|
135
|
+
index,
|
|
136
|
+
a,
|
|
137
|
+
b,
|
|
138
|
+
box: [
|
|
139
|
+
Math.min(a[0], b[0]),
|
|
140
|
+
Math.min(a[1], b[1]),
|
|
141
|
+
Math.max(a[0], b[0]),
|
|
142
|
+
Math.max(a[1], b[1]),
|
|
143
|
+
],
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
edges.sort((e, f) => e.box[0] - f.box[0]);
|
|
148
|
+
const adjacent = (e, f) => {
|
|
149
|
+
if (e.ring !== f.ring)
|
|
150
|
+
return false;
|
|
151
|
+
const gap = Math.abs(e.index - f.index);
|
|
152
|
+
return gap === 1 || gap === rings[e.ring].length - 1;
|
|
153
|
+
};
|
|
154
|
+
for (const [i, edge] of edges.entries()) {
|
|
155
|
+
for (let j = i + 1; j < edges.length; j++) {
|
|
156
|
+
const other = edges[j];
|
|
157
|
+
if (other.box[0] > edge.box[2] + tolerance)
|
|
158
|
+
break;
|
|
159
|
+
if (boxesOverlap(edge.box, other.box, tolerance) &&
|
|
160
|
+
!adjacent(edge, other) &&
|
|
161
|
+
segmentsTouch(edge.a, edge.b, other.a, other.b, tolerance)) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
function isInside(inner, outer) {
|
|
169
|
+
if (!boxesOverlap(inner.box, outer.box))
|
|
170
|
+
return false;
|
|
171
|
+
const [x, y] = inner.polygon[0];
|
|
172
|
+
return (0, contours_1.pointInPolygon)(x, y, outer.polygon);
|
|
173
|
+
}
|
|
174
|
+
function analyzeWinding(PathKit, d, tolerance = TOUCH_TOLERANCE_ON_24_UNIT_VIEWBOX) {
|
|
44
175
|
const path = PathKit.FromSVGString(d);
|
|
45
176
|
if (!path)
|
|
46
|
-
return
|
|
47
|
-
const
|
|
177
|
+
return null;
|
|
178
|
+
const V = (0, contours_1.verbMap)(PathKit);
|
|
179
|
+
const contours = (0, contours_1.splitContours)(conicsToCubics(path.toCmds(), V), V);
|
|
48
180
|
path.delete?.();
|
|
181
|
+
return windingOfContours(contours, V, tolerance);
|
|
182
|
+
}
|
|
183
|
+
// Direction of each contour, or null when contours touch or cross.
|
|
184
|
+
function windingOfContours(contours, V, tolerance) {
|
|
185
|
+
const closed = contours.map((contour) => (0, contours_1.ensureClosed)(contour, V));
|
|
186
|
+
const rings = closed.map((contour) => ringOf((0, contours_1.contourToPolyline)(contour, V), tolerance));
|
|
187
|
+
if (contoursTouch(rings, tolerance))
|
|
188
|
+
return null;
|
|
189
|
+
const outlines = rings.map((polygon) => {
|
|
190
|
+
const area = polygonArea(polygon);
|
|
191
|
+
if (area === 0)
|
|
192
|
+
return null;
|
|
193
|
+
return { area, polygon, box: boxOf(polygon) };
|
|
194
|
+
});
|
|
195
|
+
const outerSigns = [];
|
|
196
|
+
const parents = [];
|
|
197
|
+
for (const [i, outline] of outlines.entries()) {
|
|
198
|
+
if (!outline) {
|
|
199
|
+
outerSigns.push(0);
|
|
200
|
+
parents.push(null);
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
let depth = 0;
|
|
204
|
+
let parent = null;
|
|
205
|
+
for (const [j, other] of outlines.entries()) {
|
|
206
|
+
if (!other || j === i)
|
|
207
|
+
continue;
|
|
208
|
+
if (isInside(outline, other)) {
|
|
209
|
+
depth++;
|
|
210
|
+
if (parent === null ||
|
|
211
|
+
Math.abs(other.area) < Math.abs(outlines[parent].area)) {
|
|
212
|
+
parent = j;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
outerSigns.push((Math.sign(outline.area) * (depth % 2 === 0 ? 1 : -1)));
|
|
217
|
+
parents.push(parent);
|
|
218
|
+
}
|
|
219
|
+
for (const [i, parent] of parents.entries()) {
|
|
220
|
+
if (parent !== null && outerSigns[i] !== outerSigns[parent])
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
if (outerSigns.every((sign) => sign === 0))
|
|
224
|
+
return null;
|
|
225
|
+
return { contours, outerSigns };
|
|
226
|
+
}
|
|
227
|
+
function reversedContour(contour, V) {
|
|
228
|
+
const drawn = contour.filter((cmd) => cmd[0] !== V.CLOSE);
|
|
229
|
+
const ends = drawn.map((cmd) => cmd.slice(-2));
|
|
230
|
+
const out = [[V.MOVE, ...ends[ends.length - 1]]];
|
|
231
|
+
for (let i = drawn.length - 1; i >= 1; i--) {
|
|
232
|
+
const cmd = drawn[i];
|
|
233
|
+
const start = ends[i - 1];
|
|
234
|
+
if (cmd[0] === V.LINE)
|
|
235
|
+
out.push([V.LINE, ...start]);
|
|
236
|
+
else if (cmd[0] === V.QUAD)
|
|
237
|
+
out.push([V.QUAD, cmd[1], cmd[2], ...start]);
|
|
238
|
+
else if (cmd[0] === V.CUBIC) {
|
|
239
|
+
out.push([V.CUBIC, cmd[3], cmd[4], cmd[1], cmd[2], ...start]);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (drawn.length < contour.length)
|
|
243
|
+
out.push([V.CLOSE]);
|
|
49
244
|
return out;
|
|
50
245
|
}
|
|
51
|
-
function
|
|
246
|
+
function pathDataWithWinding(PathKit, d, analysis, sign) {
|
|
247
|
+
if (analysis.outerSigns.every((s) => s === 0 || s === sign))
|
|
248
|
+
return d;
|
|
249
|
+
return rewoundPathData(PathKit, analysis, sign);
|
|
250
|
+
}
|
|
251
|
+
// Path data with the contours drawn against `sign` reversed.
|
|
252
|
+
function rewoundPathData(PathKit, { contours, outerSigns }, sign) {
|
|
253
|
+
const V = (0, contours_1.verbMap)(PathKit);
|
|
254
|
+
return (0, contours_1.cmdsToPathData)(contours.flatMap((contour, i) => outerSigns[i] === -sign ? reversedContour(contour, V) : contour), V);
|
|
255
|
+
}
|
|
256
|
+
// Resolution and tolerance of the fill check.
|
|
257
|
+
const FILL_CHECK_ROWS = 64;
|
|
258
|
+
const FILL_CHECK_CURVE_STEPS = 32;
|
|
259
|
+
const MAX_FILL_MISMATCH = 0.005;
|
|
260
|
+
// Sizes paths are scaled to before a PathKit op.
|
|
261
|
+
const RELIABLE_PATH_OPS_SIZE = 1024;
|
|
262
|
+
const PATH_OP_ATTEMPT_SIZES = [RELIABLE_PATH_OPS_SIZE, 4096, 256];
|
|
263
|
+
// Share of the `reference` fill where `wanted` and `got` differ.
|
|
264
|
+
function fillDisagreement(shapes, rowsSpan, wanted, got, reference) {
|
|
265
|
+
let minY = Infinity;
|
|
266
|
+
let maxY = -Infinity;
|
|
267
|
+
for (const polygons of rowsSpan) {
|
|
268
|
+
for (const polygon of polygons) {
|
|
269
|
+
for (const [, y] of polygon) {
|
|
270
|
+
minY = Math.min(minY, y);
|
|
271
|
+
maxY = Math.max(maxY, y);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
const rowHeight = (maxY - minY) / FILL_CHECK_ROWS;
|
|
276
|
+
if (!(rowHeight > 0))
|
|
277
|
+
return 0;
|
|
278
|
+
const rows = Array.from({ length: FILL_CHECK_ROWS }, () => []);
|
|
279
|
+
shapes.forEach((polygons, shape) => {
|
|
280
|
+
for (const polygon of polygons) {
|
|
281
|
+
for (const [i, [x1, y1]] of polygon.entries()) {
|
|
282
|
+
const [x2, y2] = polygon[(i + 1) % polygon.length];
|
|
283
|
+
if (y1 === y2)
|
|
284
|
+
continue;
|
|
285
|
+
const first = Math.ceil((Math.min(y1, y2) - minY) / rowHeight - 0.5);
|
|
286
|
+
const last = Math.ceil((Math.max(y1, y2) - minY) / rowHeight - 0.5) - 1;
|
|
287
|
+
for (let row = Math.max(first, 0); row <= last && row < FILL_CHECK_ROWS; row++) {
|
|
288
|
+
const y = minY + (row + 0.5) * rowHeight;
|
|
289
|
+
rows[row].push([
|
|
290
|
+
x1 + ((y - y1) * (x2 - x1)) / (y2 - y1),
|
|
291
|
+
shape,
|
|
292
|
+
y2 > y1 ? 1 : -1,
|
|
293
|
+
]);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
let referenceLength = 0;
|
|
299
|
+
let differing = 0;
|
|
300
|
+
for (const crossings of rows) {
|
|
301
|
+
crossings.sort((a, b) => a[0] - b[0]);
|
|
302
|
+
const windings = shapes.map(() => 0);
|
|
303
|
+
for (const [i, [x, shape, direction]] of crossings.entries()) {
|
|
304
|
+
if (i > 0) {
|
|
305
|
+
const length = x - crossings[i - 1][0];
|
|
306
|
+
if (reference(windings))
|
|
307
|
+
referenceLength += length;
|
|
308
|
+
if (wanted(windings) !== got(windings))
|
|
309
|
+
differing += length;
|
|
310
|
+
}
|
|
311
|
+
windings[shape] += direction;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return differing === 0 ? 0 : differing / referenceLength;
|
|
315
|
+
}
|
|
316
|
+
// Share of the fill of `before` that differs in `after`.
|
|
317
|
+
function fillMismatch(before, after) {
|
|
318
|
+
const drawn = (windings) => windings[0] !== 0;
|
|
319
|
+
return fillDisagreement([before, after], [before, after], drawn, (windings) => windings[1] !== 0, drawn);
|
|
320
|
+
}
|
|
321
|
+
// Contours drawn one way and free of overlaps, with the fill kept.
|
|
322
|
+
function repairedContours(PathKit, path, tolerance, sign) {
|
|
323
|
+
const V = (0, contours_1.verbMap)(PathKit);
|
|
324
|
+
const contours = (0, contours_1.splitContours)(conicsToCubics(path.toCmds(), V), V);
|
|
325
|
+
const analysis = windingOfContours(contours, V, tolerance);
|
|
326
|
+
if (analysis !== null) {
|
|
327
|
+
const wanted = sign ?? analysis.outerSigns.find((s) => s !== 0);
|
|
328
|
+
const stray = analysis.outerSigns.map((s) => s === -wanted);
|
|
329
|
+
return {
|
|
330
|
+
contours: contours.map((contour, i) => stray[i] ? reversedContour(contour, V) : contour),
|
|
331
|
+
changed: stray.some(Boolean),
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
const polygonsOf = (shape) => shape.map((contour) => (0, contours_1.contourToPolyline)((0, contours_1.ensureClosed)(contour, V), V, FILL_CHECK_CURVE_STEPS));
|
|
335
|
+
const drawn = polygonsOf(contours);
|
|
336
|
+
const enlargement = enlargementForPathOps([path]);
|
|
337
|
+
const enlarged = (contour) => scaledCmds(contour, enlargement);
|
|
338
|
+
const attempts = [
|
|
339
|
+
() => {
|
|
340
|
+
const copy = path.copy();
|
|
341
|
+
copy.transform(enlargement, 0, 0, 0, enlargement, 0, 0, 0, 1);
|
|
342
|
+
return copy;
|
|
343
|
+
},
|
|
344
|
+
() => contourUnion(PathKit, contours.map(enlarged), V),
|
|
345
|
+
];
|
|
346
|
+
for (const attempt of attempts) {
|
|
347
|
+
const overlapFree = attempt();
|
|
348
|
+
if (!overlapFree)
|
|
349
|
+
continue;
|
|
350
|
+
const merged = orientedContoursOf(PathKit, overlapFree).map((c) => ({
|
|
351
|
+
...c,
|
|
352
|
+
cmds: scaledCmds(c.cmds, 1 / enlargement),
|
|
353
|
+
}));
|
|
354
|
+
overlapFree.delete?.();
|
|
355
|
+
if (fillMismatch(drawn, polygonsOf(merged.map((c) => c.cmds))) <=
|
|
356
|
+
MAX_FILL_MISMATCH) {
|
|
357
|
+
return {
|
|
358
|
+
contours: merged.map((c) => sign === -1
|
|
359
|
+
? (0, contours_1.reverseClosedContourKeepStart)(c.cmds, c.explicitCloseWanted, V)
|
|
360
|
+
: c.cmds),
|
|
361
|
+
changed: true,
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return { contours, changed: false };
|
|
366
|
+
}
|
|
367
|
+
// Path data safe for glyph rasterizers, or null when unchanged.
|
|
368
|
+
function rasterSafePathData(PathKit, path, tolerance) {
|
|
369
|
+
const { contours, changed } = repairedContours(PathKit, path, tolerance);
|
|
370
|
+
return changed && contours.length > 0
|
|
371
|
+
? (0, contours_1.cmdsToPathData)(contours.flat(), (0, contours_1.verbMap)(PathKit))
|
|
372
|
+
: null;
|
|
373
|
+
}
|
|
374
|
+
// Path data of the repaired contours of `d`.
|
|
375
|
+
function repairedPathData(PathKit, d, tolerance, sign) {
|
|
52
376
|
const path = PathKit.FromSVGString(d);
|
|
53
377
|
if (!path)
|
|
54
378
|
return d;
|
|
55
|
-
const
|
|
56
|
-
const contours = orientedContoursOf(PathKit, path);
|
|
379
|
+
const { contours } = repairedContours(PathKit, path, tolerance, sign);
|
|
57
380
|
path.delete?.();
|
|
58
|
-
return (0, contours_1.cmdsToPathData)(contours.
|
|
381
|
+
return (0, contours_1.cmdsToPathData)(contours.flat(), (0, contours_1.verbMap)(PathKit));
|
|
382
|
+
}
|
|
383
|
+
// Outlines drawn one way, holes the other, no overlaps.
|
|
384
|
+
function canonicalWinding(PathKit, d, tolerance = TOUCH_TOLERANCE_ON_24_UNIT_VIEWBOX) {
|
|
385
|
+
return repairedPathData(PathKit, d, tolerance, 1);
|
|
386
|
+
}
|
|
387
|
+
// canonicalWinding with every contour reversed.
|
|
388
|
+
function reversedWinding(PathKit, d, tolerance = TOUCH_TOLERANCE_ON_24_UNIT_VIEWBOX) {
|
|
389
|
+
return repairedPathData(PathKit, d, tolerance, -1);
|
|
390
|
+
}
|
|
391
|
+
// Larger side of the bounding box around `paths`.
|
|
392
|
+
function extent(paths) {
|
|
393
|
+
const boxes = paths.map((path) => path.getBounds());
|
|
394
|
+
return Math.max(Math.max(...boxes.map((box) => box.fRight)) -
|
|
395
|
+
Math.min(...boxes.map((box) => box.fLeft)), Math.max(...boxes.map((box) => box.fBottom)) -
|
|
396
|
+
Math.min(...boxes.map((box) => box.fTop)));
|
|
397
|
+
}
|
|
398
|
+
// Scale factor that brings `paths` to RELIABLE_PATH_OPS_SIZE.
|
|
399
|
+
function enlargementForPathOps(paths) {
|
|
400
|
+
const size = extent(paths);
|
|
401
|
+
return size > 0 ? RELIABLE_PATH_OPS_SIZE / size : 1;
|
|
402
|
+
}
|
|
403
|
+
// PathKit constant and expected fill of each boolean op.
|
|
404
|
+
const PATH_OPS = {
|
|
405
|
+
union: {
|
|
406
|
+
constant: 'UNION',
|
|
407
|
+
fills: (a, b) => a || b,
|
|
408
|
+
within: (a, b) => a || b,
|
|
409
|
+
},
|
|
410
|
+
intersect: {
|
|
411
|
+
constant: 'INTERSECT',
|
|
412
|
+
fills: (a, b) => a && b,
|
|
413
|
+
within: (a) => a,
|
|
414
|
+
},
|
|
415
|
+
difference: {
|
|
416
|
+
constant: 'DIFFERENCE',
|
|
417
|
+
fills: (a, b) => a && !b,
|
|
418
|
+
within: (a) => a,
|
|
419
|
+
},
|
|
420
|
+
};
|
|
421
|
+
// Verified boolean op as canonical path data, or null on failure.
|
|
422
|
+
function pathOpData(PathKit, a, b, kind) {
|
|
423
|
+
const { constant, fills, within } = PATH_OPS[kind];
|
|
424
|
+
const op = PathKit.PathOp?.[constant];
|
|
425
|
+
const pathA = PathKit.FromSVGString(a);
|
|
426
|
+
const pathB = PathKit.FromSVGString(b);
|
|
427
|
+
let data = null;
|
|
428
|
+
if (pathA && pathB && op !== undefined) {
|
|
429
|
+
const V = (0, contours_1.verbMap)(PathKit);
|
|
430
|
+
const polygonsOf = (contours) => contours.map((contour) => (0, contours_1.contourToPolyline)((0, contours_1.ensureClosed)(contour, V), V, FILL_CHECK_CURVE_STEPS));
|
|
431
|
+
const operands = [pathA, pathB].map((path) => polygonsOf((0, contours_1.splitContours)(conicsToCubics(path.toCmds(), V), V)));
|
|
432
|
+
const rowsSpan = kind === 'union' ? operands : [operands[0]];
|
|
433
|
+
const size = extent([pathA, pathB]);
|
|
434
|
+
for (const opsSize of PATH_OP_ATTEMPT_SIZES) {
|
|
435
|
+
const enlargement = size > 0 ? opsSize / size : 1;
|
|
436
|
+
const [enlargedA, enlargedB] = [pathA, pathB].map((path) => {
|
|
437
|
+
const copy = path.copy();
|
|
438
|
+
copy.transform(enlargement, 0, 0, 0, enlargement, 0, 0, 0, 1);
|
|
439
|
+
return copy;
|
|
440
|
+
});
|
|
441
|
+
const result = PathKit.MakeFromOp(enlargedA, enlargedB, op);
|
|
442
|
+
enlargedA.delete?.();
|
|
443
|
+
enlargedB.delete?.();
|
|
444
|
+
if (!result)
|
|
445
|
+
continue;
|
|
446
|
+
const contours = orientedContoursOf(PathKit, result).map((c) => scaledCmds(c.cmds, 1 / enlargement));
|
|
447
|
+
result.delete?.();
|
|
448
|
+
const disagreement = fillDisagreement([...operands, polygonsOf(contours)], rowsSpan, (windings) => fills(windings[0] !== 0, windings[1] !== 0), (windings) => windings[2] !== 0, (windings) => within(windings[0] !== 0, windings[1] !== 0));
|
|
449
|
+
if (disagreement <= MAX_FILL_MISMATCH) {
|
|
450
|
+
data = (0, contours_1.cmdsToPathData)(contours.flat(), V);
|
|
451
|
+
break;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
pathA?.delete?.();
|
|
456
|
+
pathB?.delete?.();
|
|
457
|
+
return data;
|
|
458
|
+
}
|
|
459
|
+
// Path commands with every coordinate multiplied by `factor`.
|
|
460
|
+
function scaledCmds(cmds, factor) {
|
|
461
|
+
return cmds.map((cmd) => [cmd[0], ...cmd.slice(1).map((n) => n * factor)]);
|
|
462
|
+
}
|
|
463
|
+
// PathKit path built from path commands.
|
|
464
|
+
function pathOf(PathKit, cmds, V) {
|
|
465
|
+
return PathKit.FromSVGString((0, contours_1.cmdsToPathData)(cmds, V));
|
|
466
|
+
}
|
|
467
|
+
// Union of the contours, each treated as filled.
|
|
468
|
+
function contourUnion(PathKit, contours, V) {
|
|
469
|
+
let union = null;
|
|
470
|
+
for (const contour of contours) {
|
|
471
|
+
const next = pathOf(PathKit, contour, V);
|
|
472
|
+
if (!next)
|
|
473
|
+
continue;
|
|
474
|
+
if (union === null) {
|
|
475
|
+
union = next;
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
const joined = PathKit.MakeFromOp(union, next, PathKit.PathOp?.UNION ?? 1);
|
|
479
|
+
union.delete?.();
|
|
480
|
+
next.delete?.();
|
|
481
|
+
if (!joined)
|
|
482
|
+
return null;
|
|
483
|
+
union = joined;
|
|
484
|
+
}
|
|
485
|
+
return union;
|
|
59
486
|
}
|
|
@@ -8,7 +8,7 @@ export type PreparedSvg = {
|
|
|
8
8
|
};
|
|
9
9
|
/**
|
|
10
10
|
* Shared per-file SVG prep: validate → preprocess → flatten → parse →
|
|
11
|
-
*
|
|
11
|
+
* same-color merge. Returns null for unsupported SVGs.
|
|
12
12
|
*/
|
|
13
13
|
export declare function prepareSvgLayers(opts: {
|
|
14
14
|
filePath: string;
|
|
@@ -9,7 +9,7 @@ const index_1 = require("../flatten/index");
|
|
|
9
9
|
const merge_1 = require("../glyph/merge");
|
|
10
10
|
const parse_1 = require("../glyph/parse");
|
|
11
11
|
const validate_1 = require("../glyph/validate");
|
|
12
|
-
const
|
|
12
|
+
const winding_1 = require("../pathkit/winding");
|
|
13
13
|
const knockout_1 = require("../symbols/knockout");
|
|
14
14
|
function tagKnockoutPaths(paths, svg, pathkit, fileLabel, logger) {
|
|
15
15
|
const marked = (0, knockout_1.markKnockoutFills)(svg);
|
|
@@ -27,11 +27,11 @@ function tagKnockoutPaths(paths, svg, pathkit, fileLabel, logger) {
|
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
29
|
* Shared per-file SVG prep: validate → preprocess → flatten → parse →
|
|
30
|
-
*
|
|
30
|
+
* same-color merge. Returns null for unsupported SVGs.
|
|
31
31
|
*/
|
|
32
32
|
async function prepareSvgLayers(opts) {
|
|
33
33
|
const { filePath, fileLabel, pathkit, logger } = opts;
|
|
34
|
-
const rawContent = await promises_1.default.readFile(filePath, 'utf-8');
|
|
34
|
+
const rawContent = (await promises_1.default.readFile(filePath, 'utf-8')).replace(/^\uFEFF/, '');
|
|
35
35
|
const validation = (0, validate_1.validateSvg)(rawContent);
|
|
36
36
|
if (validation.valid === false) {
|
|
37
37
|
logger?.warn(`${fileLabel} skipped: ${validation.reason}`);
|
|
@@ -57,18 +57,6 @@ async function prepareSvgLayers(opts) {
|
|
|
57
57
|
if (opts.tagKnockouts) {
|
|
58
58
|
tagKnockoutPaths(parsed.paths, preprocessed, pathkit, fileLabel, logger);
|
|
59
59
|
}
|
|
60
|
-
|
|
61
|
-
// algorithm. Mark as noMerge — compound paths with holes must stay
|
|
62
|
-
// separate so their CW hole contours don't cancel adjacent paths' CCW
|
|
63
|
-
// contours.
|
|
64
|
-
for (const p of parsed.paths) {
|
|
65
|
-
if (p.fillRule === 'evenodd') {
|
|
66
|
-
logger?.info(` ↻ ${fileLabel} converting evenodd path to nonzero winding`);
|
|
67
|
-
p.d = (0, evenodd_1.convertEvenoddToWinding)(pathkit, p.d);
|
|
68
|
-
delete p.fillRule;
|
|
69
|
-
p.noMerge = true;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
const mergedPaths = (0, merge_1.mergeSameColorPaths)(parsed.paths, logger);
|
|
60
|
+
const mergedPaths = (0, merge_1.mergeSameColorPaths)(parsed.paths, pathkit, logger, (0, winding_1.touchTolerance)(parsed.viewBox));
|
|
73
61
|
return { viewBox: parsed.viewBox, paths: mergedPaths };
|
|
74
62
|
}
|
|
@@ -9,6 +9,7 @@ const node_path_1 = __importDefault(require("node:path"));
|
|
|
9
9
|
const quadratic_1 = require("../font/quadratic");
|
|
10
10
|
const parse_1 = require("../glyph/parse");
|
|
11
11
|
const placement_1 = require("../glyph/placement");
|
|
12
|
+
const winding_1 = require("../pathkit/winding");
|
|
12
13
|
const prepare_1 = require("./prepare");
|
|
13
14
|
function collectingLogger(logs) {
|
|
14
15
|
const push = (level) => (msg) => {
|
|
@@ -68,10 +69,11 @@ async function prepareIcon(task, pathkit) {
|
|
|
68
69
|
xOff,
|
|
69
70
|
yOff,
|
|
70
71
|
upm: task.upm,
|
|
72
|
+
touchTolerance: (0, winding_1.touchTolerance)(prepared.viewBox),
|
|
71
73
|
});
|
|
72
74
|
result.layers.push({
|
|
73
75
|
fill: p.fill,
|
|
74
|
-
d: (0, quadratic_1.toQuadraticPath)(placed, task.upm *
|
|
76
|
+
d: (0, quadratic_1.toQuadraticPath)(placed, task.upm * task.curveTolerance),
|
|
75
77
|
});
|
|
76
78
|
}
|
|
77
79
|
if (result.layers.length === 0) {
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.prepareSymbol = prepareSymbol;
|
|
7
7
|
const node_path_1 = __importDefault(require("node:path"));
|
|
8
8
|
const parse_1 = require("../glyph/parse");
|
|
9
|
+
const winding_1 = require("../pathkit/winding");
|
|
9
10
|
const contents_1 = require("../symbols/contents");
|
|
10
11
|
const layers_1 = require("../symbols/layers");
|
|
11
12
|
const template_1 = require("../symbols/template");
|
|
@@ -48,10 +49,16 @@ async function prepareSymbol(task, pathkit) {
|
|
|
48
49
|
}
|
|
49
50
|
const assetName = `${task.prefix}.${iconName}`;
|
|
50
51
|
const svgFilename = `${assetName}.svg`;
|
|
52
|
+
const knockoutLayers = new Set();
|
|
51
53
|
const { layers, monochrome } = (0, layers_1.resolveSymbolLayers)(pathkit, drawable, {
|
|
52
|
-
|
|
54
|
+
touchTolerance: (0, winding_1.touchTolerance)(prepared.viewBox),
|
|
55
|
+
onKnockoutLayer: (index) => {
|
|
56
|
+
knockoutLayers.add(index);
|
|
57
|
+
logger.info(` ⊖ Knockout layer: hole in monochrome, own fill in original`);
|
|
58
|
+
},
|
|
53
59
|
});
|
|
54
|
-
const
|
|
60
|
+
const ink = (0, layers_1.contentBounds)(pathkit, layers.map((l) => l.d)) ?? undefined;
|
|
61
|
+
const bounds = task.fit === 'viewBox' ? undefined : ink;
|
|
55
62
|
result.asset = {
|
|
56
63
|
assetName,
|
|
57
64
|
assetDirName: `${assetName}.symbolset`,
|
|
@@ -60,19 +67,24 @@ async function prepareSymbol(task, pathkit) {
|
|
|
60
67
|
layers,
|
|
61
68
|
viewBox: prepared.viewBox,
|
|
62
69
|
contentBounds: bounds,
|
|
70
|
+
scale: task.scale,
|
|
63
71
|
descriptiveName: assetName,
|
|
64
72
|
logger,
|
|
65
73
|
}),
|
|
66
|
-
contents: (0, contents_1.symbolsetContentsJson)(svgFilename),
|
|
74
|
+
contents: (0, contents_1.symbolsetContentsJson)(svgFilename, (0, template_1.isMulticolor)(drawable.filter((_, i) => !knockoutLayers.has(i)))),
|
|
67
75
|
vdXml: (0, vectorDrawable_1.buildVectorDrawableXml)({
|
|
68
76
|
layers: [{ d: monochrome, fill: null }],
|
|
69
77
|
viewBox: prepared.viewBox,
|
|
70
78
|
contentBounds: bounds,
|
|
79
|
+
scale: task.scale,
|
|
80
|
+
inkBounds: ink,
|
|
71
81
|
}),
|
|
72
82
|
vdOriginalXml: (0, vectorDrawable_1.buildVectorDrawableXml)({
|
|
73
83
|
layers,
|
|
74
84
|
viewBox: prepared.viewBox,
|
|
75
85
|
contentBounds: bounds,
|
|
86
|
+
scale: task.scale,
|
|
87
|
+
inkBounds: ink,
|
|
76
88
|
}),
|
|
77
89
|
};
|
|
78
90
|
return result;
|
|
@@ -15,9 +15,9 @@ function preparedSvgCacheKey(task, hash) {
|
|
|
15
15
|
.update(node_fs_1.default.readFileSync(task.filePath))
|
|
16
16
|
.digest('hex');
|
|
17
17
|
if (task.kind === 'symbol') {
|
|
18
|
-
return `symbol:${task.setName}:${task.file}:${task.prefix}:${contentHash}`;
|
|
18
|
+
return `symbol:${task.setName}:${task.file}:${task.prefix}:${task.fit ?? 'content'}:${task.scale ?? 1}:${contentHash}`;
|
|
19
19
|
}
|
|
20
|
-
return `${task.file}:${task.upm}:${task.safeZone}:${contentHash}`;
|
|
20
|
+
return `${task.file}:${task.upm}:${task.safeZone}:${task.curveTolerance}:${contentHash}`;
|
|
21
21
|
}
|
|
22
22
|
async function prepareIconsWithCache(tasks, concurrency, cache, pool, svgHashByFile) {
|
|
23
23
|
const prepare = (batch) => pool ? pool.prepare(batch) : (0, iconPool_1.prepareIcons)(batch, concurrency);
|