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.
Files changed (138) hide show
  1. package/README.md +139 -21
  2. package/android/src/main/java/com/nanoicons/NanoIconFonts.kt +15 -0
  3. package/android/src/main/java/com/nanoicons/NanoIconView.kt +27 -4
  4. package/android/src/main/java/com/nanoicons/NanoIconViewManager.kt +7 -0
  5. package/android/src/main/java/com/nanoicons/NanoIconsFontLoaderModule.kt +3 -0
  6. package/common/cpp/react/renderer/components/RNNanoIconsSpec/NanoIconProps.cpp +6 -1
  7. package/common/cpp/react/renderer/components/RNNanoIconsSpec/NanoIconProps.h +4 -2
  8. package/ios/NanoIconView.mm +34 -44
  9. package/ios/NanoIconsFontLoader.mm +5 -0
  10. package/lib/commonjs/cli/build.d.ts +26 -0
  11. package/lib/commonjs/cli/build.js +174 -64
  12. package/lib/commonjs/cli/buildSymbols.d.ts +30 -1
  13. package/lib/commonjs/cli/buildSymbols.js +120 -42
  14. package/lib/commonjs/cli/expoConfig.d.ts +11 -0
  15. package/lib/commonjs/cli/expoConfig.js +28 -9
  16. package/lib/commonjs/cli/fingerprint.d.ts +15 -0
  17. package/lib/commonjs/cli/fingerprint.js +42 -0
  18. package/lib/commonjs/cli/index.d.ts +2 -2
  19. package/lib/commonjs/cli/index.js +3 -1
  20. package/lib/commonjs/cli/link.d.ts +4 -0
  21. package/lib/commonjs/cli/link.js +38 -1
  22. package/lib/commonjs/cli/sameOutputs.d.ts +5 -0
  23. package/lib/commonjs/cli/sameOutputs.js +45 -0
  24. package/lib/commonjs/index.node.js +1 -1
  25. package/lib/commonjs/plugin/src/buildFonts.d.ts +3 -2
  26. package/lib/commonjs/plugin/src/buildFonts.js +11 -3
  27. package/lib/commonjs/plugin/src/buildSymbols.d.ts +2 -2
  28. package/lib/commonjs/plugin/src/buildSymbols.js +10 -3
  29. package/lib/commonjs/plugin/src/frozen.d.ts +3 -0
  30. package/lib/commonjs/plugin/src/frozen.js +18 -0
  31. package/lib/commonjs/plugin/src/index.js +3 -3
  32. package/lib/commonjs/plugin/src/types.d.ts +22 -1
  33. package/lib/commonjs/plugin/src/withNanoIconsDrawableLinking.d.ts +1 -1
  34. package/lib/commonjs/plugin/src/withNanoIconsDrawableLinking.js +2 -2
  35. package/lib/commonjs/plugin/src/withNanoIconsFontLinking.d.ts +3 -3
  36. package/lib/commonjs/plugin/src/withNanoIconsFontLinking.js +8 -8
  37. package/lib/commonjs/plugin/src/withNanoIconsSymbolLinking.d.ts +1 -1
  38. package/lib/commonjs/plugin/src/withNanoIconsSymbolLinking.js +2 -2
  39. package/lib/commonjs/scripts/cli.js +31 -2
  40. package/lib/commonjs/src/core/flatten/dom.js +40 -5
  41. package/lib/commonjs/src/core/flatten/element.js +2 -1
  42. package/lib/commonjs/src/core/flatten/stages/collapse.js +12 -0
  43. package/lib/commonjs/src/core/font/compile.d.ts +0 -1
  44. package/lib/commonjs/src/core/font/compile.js +2 -8
  45. package/lib/commonjs/src/core/glyph/merge.d.ts +2 -1
  46. package/lib/commonjs/src/core/glyph/merge.js +27 -36
  47. package/lib/commonjs/src/core/glyph/parse.d.ts +1 -2
  48. package/lib/commonjs/src/core/glyph/parse.js +31 -18
  49. package/lib/commonjs/src/core/glyph/placement.d.ts +1 -0
  50. package/lib/commonjs/src/core/glyph/placement.js +9 -3
  51. package/lib/commonjs/src/core/pathkit/types.d.ts +1 -0
  52. package/lib/commonjs/src/core/pathkit/winding.d.ts +14 -3
  53. package/lib/commonjs/src/core/pathkit/winding.js +434 -7
  54. package/lib/commonjs/src/core/pipeline/config.d.ts +1 -0
  55. package/lib/commonjs/src/core/pipeline/prepare.d.ts +1 -1
  56. package/lib/commonjs/src/core/pipeline/prepare.js +4 -16
  57. package/lib/commonjs/src/core/pipeline/prepareIcon.d.ts +1 -0
  58. package/lib/commonjs/src/core/pipeline/prepareIcon.js +3 -1
  59. package/lib/commonjs/src/core/pipeline/prepareSymbol.d.ts +2 -0
  60. package/lib/commonjs/src/core/pipeline/prepareSymbol.js +15 -3
  61. package/lib/commonjs/src/core/pipeline/preparedSvgCache.js +2 -2
  62. package/lib/commonjs/src/core/pipeline/runFontPipeline.js +20 -12
  63. package/lib/commonjs/src/core/pipeline/runSymbolPipeline.d.ts +2 -0
  64. package/lib/commonjs/src/core/pipeline/runSymbolPipeline.js +2 -0
  65. package/lib/commonjs/src/core/symbols/contents.d.ts +1 -1
  66. package/lib/commonjs/src/core/symbols/contents.js +6 -2
  67. package/lib/commonjs/src/core/symbols/knockout.js +4 -3
  68. package/lib/commonjs/src/core/symbols/layers.d.ts +1 -0
  69. package/lib/commonjs/src/core/symbols/layers.js +133 -92
  70. package/lib/commonjs/src/core/symbols/template.d.ts +8 -3
  71. package/lib/commonjs/src/core/symbols/template.js +23 -8
  72. package/lib/commonjs/src/core/symbols/vectorDrawable.d.ts +8 -0
  73. package/lib/commonjs/src/core/symbols/vectorDrawable.js +28 -2
  74. package/lib/commonjs/src/utils/fingerPrint.d.ts +3 -0
  75. package/lib/commonjs/src/utils/fingerPrint.js +5 -1
  76. package/lib/commonjs/src/utils/fontIdentity.d.ts +1 -1
  77. package/lib/commonjs/src/utils/fontIdentity.js +2 -2
  78. package/lib/module/createNanoIconsSet.js +0 -1
  79. package/lib/module/createNanoIconsSet.native.js +22 -18
  80. package/lib/module/createNanoIconsSet.shared.js +32 -18
  81. package/lib/module/createNanoIconsSet.web.js +103 -61
  82. package/lib/module/devServerFont.js +1 -1
  83. package/lib/module/devServerFont.web.js +3 -5
  84. package/lib/module/fontIntegrity.js +20 -38
  85. package/lib/module/fontIntegrity.web.js +48 -0
  86. package/lib/module/fontIntegrityStore.js +65 -0
  87. package/lib/module/index.js +1 -1
  88. package/lib/module/index.node.js +1 -1
  89. package/lib/module/index.web.js +4 -0
  90. package/lib/module/loadDynamicFont.js +3 -15
  91. package/lib/module/specs/NanoIconViewNativeComponent.ts +2 -1
  92. package/lib/module/utils/fingerPrint.js +4 -3
  93. package/lib/module/utils/fontIdentity.js +2 -2
  94. package/lib/module/utils/fontScale.js +10 -2
  95. package/lib/module/utils/glyphRuntime.js +13 -0
  96. package/lib/module/utils/iconAccessibility.js +26 -0
  97. package/lib/module/utils/iconPropsEqual.js +27 -0
  98. package/lib/module/utils/webCss.js +26 -0
  99. package/lib/module/utils/webStyle.js +86 -0
  100. package/lib/module/webFont.js +33 -0
  101. package/lib/typescript/src/const/codegenPrimitives.d.ts +1 -0
  102. package/lib/typescript/src/core/glyph/merge.d.ts +2 -1
  103. package/lib/typescript/src/core/glyph/parse.d.ts +1 -2
  104. package/lib/typescript/src/core/glyph/placement.d.ts +1 -0
  105. package/lib/typescript/src/core/pathkit/types.d.ts +1 -0
  106. package/lib/typescript/src/core/pathkit/winding.d.ts +14 -3
  107. package/lib/typescript/src/core/symbols/contents.d.ts +1 -1
  108. package/lib/typescript/src/core/symbols/layers.d.ts +1 -0
  109. package/lib/typescript/src/core/symbols/template.d.ts +8 -3
  110. package/lib/typescript/src/core/symbols/vectorDrawable.d.ts +8 -0
  111. package/lib/typescript/src/createNanoIconsSet.d.ts +0 -1
  112. package/lib/typescript/src/createNanoIconsSet.native.d.ts +0 -2
  113. package/lib/typescript/src/createNanoIconsSet.shared.d.ts +1 -3
  114. package/lib/typescript/src/createNanoIconsSet.web.d.ts +1 -2
  115. package/lib/typescript/src/fontIntegrity.d.ts +2 -14
  116. package/lib/typescript/src/fontIntegrity.web.d.ts +7 -0
  117. package/lib/typescript/src/fontIntegrityStore.d.ts +19 -0
  118. package/lib/typescript/src/index.d.ts +2 -1
  119. package/lib/typescript/src/index.web.d.ts +5 -0
  120. package/lib/typescript/src/loadDynamicFont.d.ts +0 -1
  121. package/lib/typescript/src/specs/NanoIconViewNativeComponent.d.ts +2 -1
  122. package/lib/typescript/src/specs/NativeNanoIconsFontLoader.d.ts +1 -0
  123. package/lib/typescript/src/types.d.ts +37 -9
  124. package/lib/typescript/src/utils/fingerPrint.d.ts +3 -0
  125. package/lib/typescript/src/utils/fontIdentity.d.ts +1 -1
  126. package/lib/typescript/src/utils/glyphRuntime.d.ts +8 -0
  127. package/lib/typescript/src/utils/iconAccessibility.d.ts +4 -0
  128. package/lib/typescript/src/utils/iconPropsEqual.d.ts +4 -0
  129. package/lib/typescript/src/utils/webCss.d.ts +9 -0
  130. package/lib/typescript/src/utils/webStyle.d.ts +3 -0
  131. package/lib/typescript/src/webFont.d.ts +6 -0
  132. package/package.json +19 -3
  133. package/src/const/codegenPrimitives.ts +3 -3
  134. package/src/specs/NanoIconViewNativeComponent.ts +2 -1
  135. package/src/specs/NativeNanoIconsFontLoader.ts +1 -0
  136. package/lib/commonjs/src/core/pathkit/evenodd.d.ts +0 -12
  137. package/lib/commonjs/src/core/pathkit/evenodd.js +0 -37
  138. package/lib/typescript/src/core/pathkit/evenodd.d.ts +0 -13
@@ -9,7 +9,7 @@ exports.parseCliArgs = parseCliArgs;
9
9
  exports.main = main;
10
10
  exports.runCli = runCli;
11
11
  /**
12
- * Run from your app root: npx react-native-nano-icons [--verbose] [--path <dir|file>] [--dynamic] [--dynamic-app-config] [--help]
12
+ * Run from your app root: npx react-native-nano-icons [--verbose] [--path <dir|file>] [--dynamic] [--dynamic-app-config] [--check] [--help]
13
13
  *
14
14
  * Flags:
15
15
  * -h, --help Print usage and exit without building
@@ -21,6 +21,8 @@ exports.runCli = runCli;
21
21
  * use this for OTA font regeneration without running expo prebuild.
22
22
  * --dynamic-app-config Like --dynamic, but reads the config from the Expo app config
23
23
  * (app.json / app.config.js / app.config.ts) instead of .nanoicons.json.
24
+ * --check Report the sets whose files those flags would change or whose linked fonts are out of date,
25
+ * and exit with 1 if there are any. Writes and links nothing in the project.
24
26
  */
25
27
  const node_fs_1 = __importDefault(require("node:fs"));
26
28
  const node_path_1 = __importDefault(require("node:path"));
@@ -34,6 +36,9 @@ Options:
34
36
  --path <dir|file> Folder with .nanoicons.json, or the file itself. Default: current directory.
35
37
  --dynamic Rebuild only icon sets with linking: "dynamic". Skips native linking.
36
38
  --dynamic-app-config Like --dynamic, but reads the sets from the Expo app config.
39
+ --check Report the sets whose files a build would change or whose linked fonts
40
+ are out of date, and exit with 1 if there are any. Writes and links
41
+ nothing in the project.
37
42
  --verbose Show per-SVG processing details.
38
43
  -h, --help Show this help.`;
39
44
  const CONFIG_FILE_NAME = '.nanoicons.json';
@@ -41,6 +46,7 @@ const OPTIONS = {
41
46
  path: { type: 'string' },
42
47
  dynamic: { type: 'boolean' },
43
48
  'dynamic-app-config': { type: 'boolean' },
49
+ check: { type: 'boolean' },
44
50
  verbose: { type: 'boolean' },
45
51
  help: { type: 'boolean', short: 'h' },
46
52
  };
@@ -61,6 +67,7 @@ function parseCliArgs(args) {
61
67
  const options = {
62
68
  dynamic: false,
63
69
  appConfig: false,
70
+ check: false,
64
71
  verbose: false,
65
72
  help: false,
66
73
  };
@@ -101,6 +108,8 @@ function parseCliArgs(args) {
101
108
  options.dynamic = true;
102
109
  options.appConfig = true;
103
110
  }
111
+ if (token.name === 'check')
112
+ options.check = true;
104
113
  if (token.name === 'verbose')
105
114
  options.verbose = true;
106
115
  }
@@ -110,7 +119,7 @@ function parseCliArgs(args) {
110
119
  return options;
111
120
  }
112
121
  async function main(logger, options) {
113
- const { dynamic, appConfig } = options;
122
+ const { dynamic, appConfig, check } = options;
114
123
  const { appRoot, configRoot } = resolveRoots(options.path);
115
124
  const buildRoot = appConfig ? appRoot : configRoot;
116
125
  if (dynamic) {
@@ -120,10 +129,21 @@ async function main(logger, options) {
120
129
  ? (0, index_1.loadDynamicSetsFromAppConfig)(appRoot)
121
130
  : (0, index_1.loadDynamicIconSets)(configRoot);
122
131
  logger.succeed(`Found ${dynamicIconSets.length} dynamic icon set(s) — skipping native linking.`);
132
+ if (check) {
133
+ failIfStale(await (0, index_1.checkAllFonts)(dynamicIconSets, buildRoot, { logger }));
134
+ return;
135
+ }
123
136
  await (0, index_1.buildAllFonts)(dynamicIconSets, buildRoot, { logger });
124
137
  }
125
138
  else {
126
139
  const config = (0, index_1.loadNanoIconsConfig)(configRoot);
140
+ if (check) {
141
+ failIfStale(await (0, index_1.checkAllFonts)(config.iconSets ?? [], buildRoot, {
142
+ logger,
143
+ appRoot,
144
+ }), await (0, index_1.checkAllSymbols)(config.symbolSets ?? [], buildRoot, { logger }));
145
+ return;
146
+ }
127
147
  let buildError;
128
148
  if (config.iconSets?.length) {
129
149
  let built;
@@ -158,6 +178,15 @@ async function main(logger, options) {
158
178
  throw buildError;
159
179
  }
160
180
  }
181
+ function failIfStale(fonts, symbols = []) {
182
+ const names = [
183
+ ...fonts.map((set) => `${set.name}.ttf`),
184
+ ...symbols.map((set) => `${set.name} symbols`),
185
+ ];
186
+ if (!names.length)
187
+ return;
188
+ throw new Error(`${names.length} set${names.length === 1 ? ' is' : 's are'} out of date: ${names.join(', ')}. Run the same command without --check to rebuild ${names.length === 1 ? 'it' : 'them'}.`);
189
+ }
161
190
  function resolveRoots(target) {
162
191
  const cwd = process.cwd();
163
192
  if (!target)
@@ -1,5 +1,38 @@
1
1
  "use strict";
2
2
  // Tags and attribute names use the "{namespace}local" convention.
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
3
36
  Object.defineProperty(exports, "__esModule", { value: true });
4
37
  exports.XEl = exports.XLINK_NS = exports.SVG_NS = void 0;
5
38
  exports.svgTag = svgTag;
@@ -11,6 +44,7 @@ exports.replaceEl = replaceEl;
11
44
  exports.delAttrs = delAttrs;
12
45
  exports.parseSvgDocument = parseSvgDocument;
13
46
  exports.serializeSvg = serializeSvg;
47
+ const xmldom = __importStar(require("@xmldom/xmldom"));
14
48
  const xmldom_1 = require("@xmldom/xmldom");
15
49
  const svgPatterns_1 = require("../../utils/svgPatterns");
16
50
  exports.SVG_NS = 'http://www.w3.org/2000/svg';
@@ -130,6 +164,7 @@ function convertElement(node) {
130
164
  }
131
165
  return el;
132
166
  }
167
+ const XMLDOM_08_MESSAGE_DECORATION = /^\[xmldom \w+\]\t|\n@[^\n]*$/g;
133
168
  function parseSvgDocument(content) {
134
169
  let text = content;
135
170
  // svgs are fond of not declaring xlink
@@ -137,11 +172,11 @@ function parseSvgDocument(content) {
137
172
  text = text.replace(/<svg([^>]*)>/, (_m, attrs) => `<svg${attrs} xmlns:xlink="${exports.XLINK_NS}">`);
138
173
  }
139
174
  let parseError = null;
140
- const parser = new xmldom_1.DOMParser({
141
- onError: (_level, message) => {
142
- parseError = parseError ?? message;
143
- },
144
- });
175
+ const onError = (_level, message) => {
176
+ parseError =
177
+ parseError ?? message.replace(XMLDOM_08_MESSAGE_DECORATION, '');
178
+ };
179
+ const parser = new xmldom_1.DOMParser('onErrorStopParsing' in xmldom ? { onError } : { errorHandler: onError });
145
180
  const doc = parser.parseFromString(text, 'text/xml');
146
181
  const root = doc.documentElement;
147
182
  if (!root || parseError) {
@@ -34,7 +34,8 @@ function clamp(value, minv = 0.0, maxv = 1.0) {
34
34
  return Math.max(Math.min(value, maxv), minv);
35
35
  }
36
36
  function elOpacity(el) {
37
- return clamp(numberAttrib(el, 'opacity', 1));
37
+ const opacity = numberAttrib(el, 'opacity', 1);
38
+ return Number.isFinite(opacity) ? clamp(opacity) : 1;
38
39
  }
39
40
  // Groups with 0 < opacity < 1 and >1 child must be retained.
40
41
  // This over-retains groups; no difference unless children overlap.
@@ -8,8 +8,20 @@ const shape_1 = require("../shape");
8
8
  const transform_1 = require("../transform");
9
9
  const defs_1 = require("./defs");
10
10
  const stroke_1 = require("./stroke");
11
+ function moveRootOpacityToGroup(root) {
12
+ const opacity = root.attrib.get('opacity');
13
+ if (opacity === undefined)
14
+ return;
15
+ const group = new dom_1.XEl((0, dom_1.svgTag)('g'), [['opacity', opacity]]);
16
+ for (const child of [...root.children]) {
17
+ group.append(child);
18
+ }
19
+ root.append(group);
20
+ root.attrib.delete('opacity');
21
+ }
11
22
  function collapse(doc) {
12
23
  doc.syncTree();
24
+ moveRootOpacityToGroup(doc.root);
13
25
  // Reversed: we want leaves first. Materialize BEFORE mutating.
14
26
  const toProcess = [...doc.breadthFirst()].reverse();
15
27
  const defs = new dom_1.XEl((0, dom_1.svgTag)('defs'));
@@ -11,7 +11,6 @@ export declare function parseCompileTtfFromGlyphsError(err: unknown, codepointTo
11
11
  */
12
12
  export declare function compileTtfFromGlyphs(opts: {
13
13
  glyphs: FontGlyph[];
14
- outTtfPath: string;
15
14
  fontName: string;
16
15
  upm: number;
17
16
  ascent: number;
@@ -6,10 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.parseCompileTtfFromGlyphsError = parseCompileTtfFromGlyphsError;
7
7
  exports.compileTtfFromGlyphs = compileTtfFromGlyphs;
8
8
  exports.compileWoff2FromTtf = compileWoff2FromTtf;
9
- const node_fs_1 = __importDefault(require("node:fs"));
10
- const node_path_1 = __importDefault(require("node:path"));
11
9
  const metrics_1 = require("./metrics");
12
- const config_1 = require("../pipeline/config");
13
10
  const svg2ttf_1 = __importDefault(require("svg2ttf"));
14
11
  const fonteditor_core_1 = require("fonteditor-core");
15
12
  const svgPatterns_1 = require("../../utils/svgPatterns");
@@ -57,7 +54,7 @@ function parseCompileTtfFromGlyphsError(err, codepointToIcon) {
57
54
  * Builds SVG font XML directly (no intermediate files), then converts via svg2ttf.
58
55
  */
59
56
  async function compileTtfFromGlyphs(opts) {
60
- const { glyphs, outTtfPath, fontName, upm, ascent, descent } = opts;
57
+ const { glyphs, fontName, upm, ascent, descent } = opts;
61
58
  const lineGap = opts.lineGap ?? 0;
62
59
  if (glyphs.length === 0)
63
60
  throw new Error('No glyphs to compile');
@@ -70,10 +67,7 @@ async function compileTtfFromGlyphs(opts) {
70
67
  });
71
68
  const ttfRaw = (0, svg2ttf_1.default)(svgFontString, { ts: 0 });
72
69
  const rawBuf = Buffer.from(ttfRaw.buffer);
73
- const fixedBuf = (0, metrics_1.forceTtfMetrics)(rawBuf, upm, ascent, descent, lineGap);
74
- node_fs_1.default.mkdirSync(node_path_1.default.dirname(outTtfPath), { recursive: true });
75
- (0, config_1.writeFileAtomic)(outTtfPath, fixedBuf);
76
- return fixedBuf;
70
+ return (0, metrics_1.forceTtfMetrics)(rawBuf, upm, ascent, descent, lineGap);
77
71
  }
78
72
  let woff2Ready;
79
73
  async function compileWoff2FromTtf(ttfBuffer) {
@@ -1,7 +1,8 @@
1
+ import type { PathKitModule } from '../pathkit/types';
1
2
  import type { NanoLogger } from '../types';
2
3
  import type { ParsedPath } from './parse';
3
4
  /**
4
5
  * Merge consecutive same-color paths into single compound paths.
5
6
  * Preserves z-order: only merges runs of adjacent paths with identical fill color.
6
7
  */
7
- export declare function mergeSameColorPaths(paths: ParsedPath[], logger?: NanoLogger): ParsedPath[];
8
+ export declare function mergeSameColorPaths(paths: ParsedPath[], pathkit: PathKitModule, logger?: NanoLogger, tolerance?: number): ParsedPath[];
@@ -1,57 +1,48 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mergeSameColorPaths = mergeSameColorPaths;
4
- /**
5
- * Concatenate multiple SVG path `d` strings into a single compound path.
6
- * This preserves the exact geometry of each path (no boolean operations)
7
- * while combining them into one glyph. Under nonzero winding, this renders
8
- * identically to drawing each path separately with the same color.
9
- */
10
- function concatPathDs(ds) {
11
- if (ds.length === 0)
12
- return null;
13
- if (ds.length === 1)
14
- return ds[0];
15
- return ds.join(' ');
16
- }
4
+ const winding_1 = require("../pathkit/winding");
17
5
  /**
18
6
  * Merge consecutive same-color paths into single compound paths.
19
7
  * Preserves z-order: only merges runs of adjacent paths with identical fill color.
20
8
  */
21
- function mergeSameColorPaths(paths, logger) {
9
+ function mergeSameColorPaths(paths, pathkit, logger, tolerance) {
22
10
  if (paths.length <= 1)
23
11
  return paths;
12
+ const analyses = new Map();
13
+ const analysisOf = (index) => {
14
+ if (!analyses.has(index)) {
15
+ analyses.set(index, (0, winding_1.analyzeWinding)(pathkit, paths[index].d, tolerance));
16
+ }
17
+ return analyses.get(index);
18
+ };
24
19
  const result = [];
25
20
  let i = 0;
26
21
  while (i < paths.length) {
27
- const fill = paths[i].fill;
28
- // Find consecutive run of same fill that are all mergeable.
29
- // Paths converted from evenodd have compound hole structure and must not
30
- // be merged — their CW hole contours would cancel CCW contours from
31
- // adjacent paths, producing incorrect fill.
32
- const knockout = paths[i].knockout;
22
+ const { fill, knockout } = paths[i];
23
+ const ds = [];
24
+ let sign = 1;
33
25
  let j = i + 1;
34
- if (!paths[i].noMerge) {
35
- while (j < paths.length &&
36
- paths[j].fill === fill &&
37
- paths[j].knockout === knockout &&
38
- !paths[j].noMerge) {
39
- j++;
26
+ while (j < paths.length &&
27
+ paths[j].fill === fill &&
28
+ paths[j].knockout === knockout &&
29
+ analysisOf(i) !== null &&
30
+ analysisOf(j) !== null) {
31
+ if (ds.length === 0) {
32
+ const first = analysisOf(i);
33
+ sign = first.outerSigns.find((s) => s !== 0);
34
+ ds.push((0, winding_1.pathDataWithWinding)(pathkit, paths[i].d, first, sign));
40
35
  }
36
+ ds.push((0, winding_1.pathDataWithWinding)(pathkit, paths[j].d, analysisOf(j), sign));
37
+ j++;
41
38
  }
42
- if (j - i === 1) {
39
+ if (ds.length === 0) {
43
40
  result.push(paths[i]);
44
41
  }
45
42
  else {
46
- const group = paths.slice(i, j);
47
- const merged = concatPathDs(group.map((p) => p.d));
48
- if (merged) {
49
- logger?.info(` ⊕ Merged ${group.length} same-color paths (fill=${fill})`);
50
- result.push(knockout ? { d: merged, fill, knockout } : { d: merged, fill });
51
- }
52
- else {
53
- result.push(...group);
54
- }
43
+ logger?.info(` ⊕ Merged ${ds.length} same-color paths (fill=${fill})`);
44
+ const d = ds.join(' ');
45
+ result.push(knockout ? { d, fill, knockout } : { d, fill });
55
46
  }
56
47
  i = j;
57
48
  }
@@ -2,14 +2,13 @@ import { type Element } from '@xmldom/xmldom';
2
2
  export type ParsedPath = {
3
3
  d: string;
4
4
  fill: string | null;
5
- fillRule?: 'evenodd';
6
- noMerge?: boolean;
7
5
  knockout?: boolean;
8
6
  };
9
7
  export type ParsedFlatSvg = {
10
8
  viewBox: [number, number, number, number];
11
9
  paths: ParsedPath[];
12
10
  };
11
+ export declare function attribute(el: Element, name: string): string | null;
13
12
  export declare function calculateOpColor(fill: string | null, opacity: number, el: Element): `rgba(${number},${number},${number},${number})`;
14
13
  /**
15
14
  * If a flattened path lost its initial moveto (e.g. an empty `Mx y z`
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.attribute = attribute;
3
4
  exports.calculateOpColor = calculateOpColor;
4
5
  exports.sanitizePathData = sanitizePathData;
5
6
  exports.parseFlattenedSvg = parseFlattenedSvg;
@@ -7,17 +8,32 @@ exports.shouldSkipPath = shouldSkipPath;
7
8
  const xmldom_1 = require("@xmldom/xmldom");
8
9
  const parse_1 = require("../../utils/parse");
9
10
  const svgPatterns_1 = require("../../utils/svgPatterns");
11
+ const ELEMENT_NODE = 1;
12
+ function attribute(el, name) {
13
+ return el.hasAttribute(name) ? el.getAttribute(name) : null;
14
+ }
10
15
  // if the fill is implicit, walk ancestors for the first explicit fill value
11
16
  function resolveInheritedFill(el) {
12
- let current = el.parentElement;
13
- while (current !== null) {
14
- const fill = current.getAttribute('fill');
17
+ let current = el.parentNode;
18
+ while (current?.nodeType === ELEMENT_NODE) {
19
+ const fill = attribute(current, 'fill');
15
20
  if (fill !== null && fill !== 'inherit')
16
21
  return fill;
17
- current = current.parentElement;
22
+ current = current.parentNode;
18
23
  }
19
24
  return 'black';
20
25
  }
26
+ function ancestorOpacity(el) {
27
+ let opacity = 1;
28
+ let current = el.parentNode;
29
+ while (current?.nodeType === ELEMENT_NODE) {
30
+ const value = attribute(current, 'opacity');
31
+ if (value !== null)
32
+ opacity *= parseFloat(value);
33
+ current = current.parentNode;
34
+ }
35
+ return opacity;
36
+ }
21
37
  // bake opacity into the fill as an rgba(...)
22
38
  function calculateOpColor(fill, opacity, el) {
23
39
  const resolvedFill = fill ?? resolveInheritedFill(el);
@@ -45,30 +61,28 @@ function sanitizePathData(d) {
45
61
  const y = nums[nums.length - 1];
46
62
  return { d: `M${x},${y} ${trimmed}`, sanitized: true };
47
63
  }
64
+ function isUnpainted(fill) {
65
+ const f = (fill ?? '').trim().toLowerCase();
66
+ return f === 'transparent' || f === 'none';
67
+ }
48
68
  const parsePath = (p) => {
49
69
  const d = p.getAttribute('d') ?? '';
50
- const op = p.getAttribute('opacity');
51
- const fillOp = p.getAttribute('fill-opacity');
52
- const fill = p.getAttribute('fill');
53
- // flattening may drop fill-rule but preserve clip-rule; treat either as evenodd
54
- const fillRule = p.getAttribute('fill-rule') === 'evenodd' ||
55
- p.getAttribute('clip-rule') === 'evenodd'
56
- ? 'evenodd'
57
- : undefined;
58
- if (op !== null || fillOp !== null) {
70
+ const op = attribute(p, 'opacity');
71
+ const fillOp = attribute(p, 'fill-opacity');
72
+ const fill = attribute(p, 'fill');
73
+ const groupOp = ancestorOpacity(p);
74
+ if (!isUnpainted(fill) && (op !== null || fillOp !== null || groupOp !== 1)) {
59
75
  const opVal = op !== null ? parseFloat(op) : 1;
60
76
  const fillOpVal = fillOp !== null ? parseFloat(fillOp) : 1;
61
- const combinedOpacity = opVal * fillOpVal;
77
+ const combinedOpacity = opVal * fillOpVal * groupOp;
62
78
  return {
63
79
  d,
64
80
  fill: calculateOpColor(fill, combinedOpacity, p),
65
- fillRule,
66
81
  };
67
82
  }
68
83
  return {
69
84
  d,
70
85
  fill,
71
- fillRule,
72
86
  };
73
87
  };
74
88
  function parseFlattenedSvg(flattenedSvg, options) {
@@ -104,6 +118,5 @@ function parseFlattenedSvg(flattenedSvg, options) {
104
118
  function shouldSkipPath(d, fill) {
105
119
  if (!d || d.trim() === '')
106
120
  return true;
107
- const f = (fill ?? '').trim().toLowerCase();
108
- return f === 'transparent' || f === 'none';
121
+ return isUnpainted(fill);
109
122
  }
@@ -25,4 +25,5 @@ export declare function transformPathForFont(PathKit: PathKitModule, d: string,
25
25
  xOff: number;
26
26
  yOff: number;
27
27
  upm: number;
28
+ touchTolerance: number;
28
29
  }): string;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.computePlacement = computePlacement;
4
4
  exports.transformPathForFont = transformPathForFont;
5
+ const winding_1 = require("../pathkit/winding");
5
6
  function roundInt(n) {
6
7
  return Math.round(n);
7
8
  }
@@ -32,10 +33,15 @@ function computePlacement(opts) {
32
33
  * SVG→font Y-axis flip into a single affine transform applied via PathKit.
33
34
  */
34
35
  function transformPathForFont(PathKit, d, opts) {
35
- const { vx, vy, scale, xOff, yOff, upm } = opts;
36
- const p = PathKit.FromSVGString(d);
37
- if (!p)
36
+ const { vx, vy, scale, xOff, yOff, upm, touchTolerance } = opts;
37
+ const parsed = PathKit.FromSVGString(d);
38
+ if (!parsed)
38
39
  return d;
40
+ // Repairs the outline before placement.
41
+ const safe = (0, winding_1.rasterSafePathData)(PathKit, parsed, touchTolerance);
42
+ const p = safe === null ? parsed : (PathKit.FromSVGString(safe) ?? parsed);
43
+ if (p !== parsed)
44
+ parsed.delete?.();
39
45
  // Combined affine: placement + Y-flip for font coordinates.
40
46
  // x' = scale * (x - vx) + xOff
41
47
  // y' = upm - (scale * (y - vy) + yOff)
@@ -10,6 +10,7 @@ export type VerbMap = {
10
10
  };
11
11
  export interface PathKitPath {
12
12
  delete?: () => void;
13
+ copy: () => PathKitPath;
13
14
  moveTo: (x: number, y: number) => void;
14
15
  lineTo: (x: number, y: number) => void;
15
16
  quadTo: (x1: number, y1: number, x2: number, y2: number) => void;
@@ -1,4 +1,15 @@
1
- import type { PathKitModule, PathKitPath } from './types';
1
+ import type { Cmd, PathKitModule, PathKitPath, VerbMap } from './types';
2
2
  export declare function orientedPathData(PathKit: PathKitModule, path: PathKitPath): string;
3
- export declare function canonicalWinding(PathKit: PathKitModule, d: string): string;
4
- export declare function reversedWinding(PathKit: PathKitModule, d: string): string;
3
+ export declare function touchTolerance(viewBox: readonly [number, number, number, number]): number;
4
+ export type WindingAnalysis = {
5
+ contours: Cmd[][];
6
+ outerSigns: (-1 | 0 | 1)[];
7
+ };
8
+ export declare function analyzeWinding(PathKit: PathKitModule, d: string, tolerance?: number): WindingAnalysis | null;
9
+ export declare function reversedContour(contour: readonly Cmd[], V: VerbMap): Cmd[];
10
+ export declare function pathDataWithWinding(PathKit: PathKitModule, d: string, analysis: WindingAnalysis, sign: -1 | 1): string;
11
+ export declare function rasterSafePathData(PathKit: PathKitModule, path: PathKitPath, tolerance: number): string | null;
12
+ export declare function canonicalWinding(PathKit: PathKitModule, d: string, tolerance?: number): string;
13
+ export declare function reversedWinding(PathKit: PathKitModule, d: string, tolerance?: number): string;
14
+ export type PathOpKind = 'union' | 'intersect' | 'difference';
15
+ export declare function pathOpData(PathKit: PathKitModule, a: string, b: string, kind: PathOpKind): string | null;