react-native-nano-icons 1.1.0-beta.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 +2 -0
- package/ios/NanoIconView.mm +22 -44
- package/lib/commonjs/cli/buildSymbols.d.ts +13 -1
- package/lib/commonjs/cli/buildSymbols.js +24 -7
- package/lib/commonjs/cli/expoConfig.d.ts +1 -0
- package/lib/commonjs/cli/expoConfig.js +6 -2
- package/lib/commonjs/cli/fingerprint.js +7 -1
- package/lib/commonjs/cli/link.js +13 -1
- package/lib/commonjs/plugin/src/types.d.ts +12 -1
- package/lib/commonjs/src/core/glyph/merge.d.ts +1 -1
- package/lib/commonjs/src/core/glyph/merge.js +2 -2
- 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 +9 -4
- package/lib/commonjs/src/core/pathkit/winding.js +303 -48
- package/lib/commonjs/src/core/pipeline/prepare.js +2 -1
- package/lib/commonjs/src/core/pipeline/prepareIcon.js +2 -0
- 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 +1 -1
- 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/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 +2 -0
- package/lib/commonjs/src/utils/fingerPrint.js +2 -0
- package/lib/module/utils/fingerPrint.js +1 -1
- package/lib/typescript/src/core/glyph/merge.d.ts +1 -1
- 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 +9 -4
- 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/utils/fingerPrint.d.ts +2 -0
- package/package.json +1 -1
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.orientedPathData = orientedPathData;
|
|
4
|
-
exports.
|
|
5
|
-
exports.reversedWinding = reversedWinding;
|
|
4
|
+
exports.touchTolerance = touchTolerance;
|
|
6
5
|
exports.analyzeWinding = analyzeWinding;
|
|
6
|
+
exports.reversedContour = reversedContour;
|
|
7
7
|
exports.pathDataWithWinding = pathDataWithWinding;
|
|
8
|
+
exports.rasterSafePathData = rasterSafePathData;
|
|
9
|
+
exports.canonicalWinding = canonicalWinding;
|
|
10
|
+
exports.reversedWinding = reversedWinding;
|
|
11
|
+
exports.pathOpData = pathOpData;
|
|
8
12
|
const contours_1 = require("./contours");
|
|
9
13
|
function conicsToCubics(cmds, V) {
|
|
10
14
|
let x = 0;
|
|
@@ -42,35 +46,38 @@ function orientedContoursOf(PathKit, path) {
|
|
|
42
46
|
function orientedPathData(PathKit, path) {
|
|
43
47
|
return (0, contours_1.cmdsToPathData)(orientedContoursOf(PathKit, path).flatMap((c) => c.cmds), (0, contours_1.verbMap)(PathKit));
|
|
44
48
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return out;
|
|
52
|
-
}
|
|
53
|
-
function reversedWinding(PathKit, d) {
|
|
54
|
-
const path = PathKit.FromSVGString(d);
|
|
55
|
-
if (!path)
|
|
56
|
-
return d;
|
|
57
|
-
const V = (0, contours_1.verbMap)(PathKit);
|
|
58
|
-
const contours = orientedContoursOf(PathKit, path);
|
|
59
|
-
path.delete?.();
|
|
60
|
-
return (0, contours_1.cmdsToPathData)(contours.flatMap((c) => (0, contours_1.reverseClosedContourKeepStart)(c.cmds, c.explicitCloseWanted, V)), V);
|
|
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;
|
|
61
55
|
}
|
|
62
|
-
const BOUNDARY_TOLERANCE = 2e-3;
|
|
63
56
|
function boxOf(points) {
|
|
64
|
-
let
|
|
57
|
+
let minX = Infinity;
|
|
58
|
+
let minY = Infinity;
|
|
59
|
+
let maxX = -Infinity;
|
|
60
|
+
let maxY = -Infinity;
|
|
65
61
|
for (const [x, y] of points) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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;
|
|
72
70
|
}
|
|
73
|
-
return
|
|
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;
|
|
74
81
|
}
|
|
75
82
|
function boxesOverlap(a, b, margin = 0) {
|
|
76
83
|
return (a[0] <= b[2] + margin &&
|
|
@@ -90,40 +97,51 @@ function distanceToSegment(p, a, b) {
|
|
|
90
97
|
function cross(o, a, b) {
|
|
91
98
|
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
|
|
92
99
|
}
|
|
93
|
-
function segmentsTouch(a, b, c, d) {
|
|
100
|
+
function segmentsTouch(a, b, c, d, tolerance) {
|
|
94
101
|
if (Math.sign(cross(a, b, c)) * Math.sign(cross(a, b, d)) < 0 &&
|
|
95
102
|
Math.sign(cross(c, d, a)) * Math.sign(cross(c, d, b)) < 0) {
|
|
96
103
|
return true;
|
|
97
104
|
}
|
|
98
|
-
return (distanceToSegment(a, c, d) <=
|
|
99
|
-
distanceToSegment(b, c, d) <=
|
|
100
|
-
distanceToSegment(c, a, b) <=
|
|
101
|
-
distanceToSegment(d, a, b) <=
|
|
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);
|
|
102
109
|
}
|
|
103
110
|
function distance(a, b) {
|
|
104
111
|
return Math.hypot(a[0] - b[0], a[1] - b[1]);
|
|
105
112
|
}
|
|
106
|
-
function ringOf(polyline) {
|
|
113
|
+
function ringOf(polyline, tolerance) {
|
|
107
114
|
const ring = [];
|
|
108
115
|
for (const p of polyline) {
|
|
109
116
|
const last = ring[ring.length - 1];
|
|
110
|
-
if (!last || distance(p, last) >
|
|
117
|
+
if (!last || distance(p, last) > tolerance)
|
|
111
118
|
ring.push(p);
|
|
112
119
|
}
|
|
113
120
|
if (ring.length > 1 &&
|
|
114
|
-
distance(ring[0], ring[ring.length - 1]) <=
|
|
121
|
+
distance(ring[0], ring[ring.length - 1]) <= tolerance) {
|
|
115
122
|
ring.pop();
|
|
116
123
|
}
|
|
117
124
|
return ring;
|
|
118
125
|
}
|
|
119
|
-
function contoursTouch(rings) {
|
|
126
|
+
function contoursTouch(rings, tolerance) {
|
|
120
127
|
const edges = [];
|
|
121
128
|
for (const [ring, points] of rings.entries()) {
|
|
122
129
|
if (points.length < 3)
|
|
123
130
|
continue;
|
|
124
131
|
for (const [index, a] of points.entries()) {
|
|
125
132
|
const b = points[(index + 1) % points.length];
|
|
126
|
-
edges.push({
|
|
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
|
+
});
|
|
127
145
|
}
|
|
128
146
|
}
|
|
129
147
|
edges.sort((e, f) => e.box[0] - f.box[0]);
|
|
@@ -136,11 +154,11 @@ function contoursTouch(rings) {
|
|
|
136
154
|
for (const [i, edge] of edges.entries()) {
|
|
137
155
|
for (let j = i + 1; j < edges.length; j++) {
|
|
138
156
|
const other = edges[j];
|
|
139
|
-
if (other.box[0] > edge.box[2] +
|
|
157
|
+
if (other.box[0] > edge.box[2] + tolerance)
|
|
140
158
|
break;
|
|
141
|
-
if (boxesOverlap(edge.box, other.box,
|
|
159
|
+
if (boxesOverlap(edge.box, other.box, tolerance) &&
|
|
142
160
|
!adjacent(edge, other) &&
|
|
143
|
-
segmentsTouch(edge.a, edge.b, other.a, other.b)) {
|
|
161
|
+
segmentsTouch(edge.a, edge.b, other.a, other.b, tolerance)) {
|
|
144
162
|
return true;
|
|
145
163
|
}
|
|
146
164
|
}
|
|
@@ -153,22 +171,25 @@ function isInside(inner, outer) {
|
|
|
153
171
|
const [x, y] = inner.polygon[0];
|
|
154
172
|
return (0, contours_1.pointInPolygon)(x, y, outer.polygon);
|
|
155
173
|
}
|
|
156
|
-
function analyzeWinding(PathKit, d) {
|
|
174
|
+
function analyzeWinding(PathKit, d, tolerance = TOUCH_TOLERANCE_ON_24_UNIT_VIEWBOX) {
|
|
157
175
|
const path = PathKit.FromSVGString(d);
|
|
158
176
|
if (!path)
|
|
159
177
|
return null;
|
|
160
178
|
const V = (0, contours_1.verbMap)(PathKit);
|
|
161
179
|
const contours = (0, contours_1.splitContours)(conicsToCubics(path.toCmds(), V), V);
|
|
162
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) {
|
|
163
185
|
const closed = contours.map((contour) => (0, contours_1.ensureClosed)(contour, V));
|
|
164
|
-
const rings = closed.map((contour) => ringOf((0, contours_1.contourToPolyline)(contour, V)));
|
|
165
|
-
if (contoursTouch(rings))
|
|
186
|
+
const rings = closed.map((contour) => ringOf((0, contours_1.contourToPolyline)(contour, V), tolerance));
|
|
187
|
+
if (contoursTouch(rings, tolerance))
|
|
166
188
|
return null;
|
|
167
|
-
const outlines =
|
|
168
|
-
const area = (
|
|
189
|
+
const outlines = rings.map((polygon) => {
|
|
190
|
+
const area = polygonArea(polygon);
|
|
169
191
|
if (area === 0)
|
|
170
192
|
return null;
|
|
171
|
-
const polygon = rings[i];
|
|
172
193
|
return { area, polygon, box: boxOf(polygon) };
|
|
173
194
|
});
|
|
174
195
|
const outerSigns = [];
|
|
@@ -223,9 +244,243 @@ function reversedContour(contour, V) {
|
|
|
223
244
|
return out;
|
|
224
245
|
}
|
|
225
246
|
function pathDataWithWinding(PathKit, d, analysis, sign) {
|
|
226
|
-
|
|
227
|
-
if (outerSigns.every((s) => s === 0 || s === sign))
|
|
247
|
+
if (analysis.outerSigns.every((s) => s === 0 || s === sign))
|
|
228
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) {
|
|
229
253
|
const V = (0, contours_1.verbMap)(PathKit);
|
|
230
254
|
return (0, contours_1.cmdsToPathData)(contours.flatMap((contour, i) => outerSigns[i] === -sign ? reversedContour(contour, V) : contour), V);
|
|
231
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) {
|
|
376
|
+
const path = PathKit.FromSVGString(d);
|
|
377
|
+
if (!path)
|
|
378
|
+
return d;
|
|
379
|
+
const { contours } = repairedContours(PathKit, path, tolerance, sign);
|
|
380
|
+
path.delete?.();
|
|
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;
|
|
486
|
+
}
|
|
@@ -9,6 +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 winding_1 = require("../pathkit/winding");
|
|
12
13
|
const knockout_1 = require("../symbols/knockout");
|
|
13
14
|
function tagKnockoutPaths(paths, svg, pathkit, fileLabel, logger) {
|
|
14
15
|
const marked = (0, knockout_1.markKnockoutFills)(svg);
|
|
@@ -56,6 +57,6 @@ async function prepareSvgLayers(opts) {
|
|
|
56
57
|
if (opts.tagKnockouts) {
|
|
57
58
|
tagKnockoutPaths(parsed.paths, preprocessed, pathkit, fileLabel, logger);
|
|
58
59
|
}
|
|
59
|
-
const mergedPaths = (0, merge_1.mergeSameColorPaths)(parsed.paths, pathkit, logger);
|
|
60
|
+
const mergedPaths = (0, merge_1.mergeSameColorPaths)(parsed.paths, pathkit, logger, (0, winding_1.touchTolerance)(parsed.viewBox));
|
|
60
61
|
return { viewBox: parsed.viewBox, paths: mergedPaths };
|
|
61
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,6 +69,7 @@ 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,
|
|
@@ -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,7 +15,7 @@ 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
20
|
return `${task.file}:${task.upm}:${task.safeZone}:${task.curveTolerance}:${contentHash}`;
|
|
21
21
|
}
|
|
@@ -70,6 +70,8 @@ async function runSymbolPipeline(config, paths, options) {
|
|
|
70
70
|
filePath: node_path_1.default.join(paths.inputDir, file),
|
|
71
71
|
setName: config.name,
|
|
72
72
|
prefix: config.prefix,
|
|
73
|
+
fit: config.fit ?? 'content',
|
|
74
|
+
scale: config.scale ?? 1,
|
|
73
75
|
})), options?.concurrency ?? (0, iconPool_1.defaultConcurrency)(), options?.preparedSvgCache, options?.svgWorkerPool, options?.svgHashByFile);
|
|
74
76
|
const resourceNameToFile = new Map();
|
|
75
77
|
const assets = [];
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function symbolsetContentsJson(svgFilename: string): string;
|
|
1
|
+
export declare function symbolsetContentsJson(svgFilename: string, multicolor?: boolean): string;
|
|
2
2
|
export declare function catalogRootContentsJson(): string;
|
|
@@ -3,10 +3,14 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.symbolsetContentsJson = symbolsetContentsJson;
|
|
5
5
|
exports.catalogRootContentsJson = catalogRootContentsJson;
|
|
6
|
-
|
|
6
|
+
// A symbol drawn in more than one colour prefers multicolor, so it keeps its
|
|
7
|
+
// colours wherever it is shown in its original rendering.
|
|
8
|
+
function symbolsetContentsJson(svgFilename, multicolor = false) {
|
|
7
9
|
return JSON.stringify({
|
|
8
10
|
info: { author: 'xcode', version: 1 },
|
|
9
|
-
properties: {
|
|
11
|
+
properties: {
|
|
12
|
+
'symbol-rendering-intent': multicolor ? 'multicolor' : 'template',
|
|
13
|
+
},
|
|
10
14
|
symbols: [{ filename: svgFilename, idiom: 'universal' }],
|
|
11
15
|
}, null, 2);
|
|
12
16
|
}
|
|
@@ -28,6 +28,7 @@ export type ResolvedSymbolLayers = {
|
|
|
28
28
|
export declare function isKnockoutFill(fill: string | null): boolean;
|
|
29
29
|
export declare function resolveSymbolLayers(PathKit: PathKitModule, layers: SymbolLayerInput[], options?: {
|
|
30
30
|
onKnockoutLayer?: (index: number) => void;
|
|
31
|
+
touchTolerance?: number;
|
|
31
32
|
}): ResolvedSymbolLayers;
|
|
32
33
|
/**
|
|
33
34
|
* Tight bounding box [x, y, w, h] of path `d` strings in source coords, or null.
|