mokee commited on
Commit
078e82c
·
verified ·
1 Parent(s): dcbbc03

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +28 -32
server.js CHANGED
@@ -70,32 +70,39 @@ app.post('/fill', (req, res) => {
70
  let { svg } = req.body;
71
  if (!svg) return res.status(400).send('No SVG provided');
72
 
73
- // 预处理:强制设置黑色并确保有描边
74
- let processedSvg = svg.replace(/currentColor/g, '#000000');
75
- // 如果原 SVG 没写 fill,默认给它一个黑色填充,这样联合后才是实心的
76
- if (!processedSvg.includes('fill=')) {
77
- processedSvg = processedSvg.replace(/<path/g, '<path fill="#000000" ');
78
- }
 
 
 
 
 
 
 
 
 
79
 
80
  const id = crypto.randomUUID();
81
  const inputFile = path.join('/tmp', `${id}_in.svg`);
82
  const outputFile = path.join('/tmp', `${id}_out.svg`);
83
 
84
- fs.writeFileSync(inputFile, processedSvg);
85
 
86
- // 进阶版指令链:
87
- // 1. 打开文件并全选
88
- // 2. 复制一份 (此时有两层完全重合的对象)
89
- // 3. 将其中一层执行描边转路径 (此时是一层填充 + 一层轮廓)
90
- // 4. 再次全选并执行 union (将填充和轮廓融合成一个复杂路径)
91
- const actions = [
92
  `file-open:${inputFile}`,
93
- 'select-all',
94
- 'selection-duplicate',
 
95
  'object-stroke-to-path',
96
- 'select-all',
97
  'path-union',
98
- 'path-simplify', // 新增:简化路径,有助于闭合那些因为方向问题导致的镂空
99
  `export-filename:${outputFile}`,
100
  'export-do',
101
  'file-close'
@@ -103,29 +110,18 @@ app.post('/fill', (req, res) => {
103
 
104
  const command = `xvfb-run -a inkscape --actions="${actions}"`;
105
 
106
- exec(command, (error, stdout, stderr) => {
107
- if (error) {
108
- console.error(`Inkscape Error: ${error.message}`);
109
- return res.status(500).send('转换执行失败');
110
- }
111
-
112
- if (!fs.existsSync(outputFile)) {
113
- return res.status(500).send('生成失败');
114
- }
115
 
116
  let result = fs.readFileSync(outputFile, 'utf8');
117
 
118
- // 后处理
119
  result = result.replace(/fill="#000000"/g, 'fill="currentColor"')
120
  .replace(/stroke=".*?"/g, 'stroke="none"')
121
  .replace(/stroke-width=".*?"/g, 'stroke-width="0"');
122
 
123
  res.send(result);
124
-
125
- try {
126
- if(fs.existsSync(inputFile)) fs.unlinkSync(inputFile);
127
- if(fs.existsSync(outputFile)) fs.unlinkSync(outputFile);
128
- } catch (e) {}
129
  });
130
  });
131
 
 
70
  let { svg } = req.body;
71
  if (!svg) return res.status(400).send('No SVG provided');
72
 
73
+ // --- 步骤 1: 预处理源码,构建“填充+描边”的重叠结构 ---
74
+ let cleanSvg = svg.replace(/currentColor/g, '#000000');
75
+
76
+ // 我们将每个 <path> 复制一份
77
+ // $& 代表匹配到的原字符串。这里构造:<原path(实心)> + <原path(带描边)>
78
+ let solidSvg = cleanSvg.replace(/<path\s+[^>]*>/g, (match) => {
79
+ // 第一层:纯填充,无描边
80
+ const layerFill = match.replace(/stroke-width=".*?"/g, 'stroke-width="0"')
81
+ .replace(/fill="none"/g, 'fill="#000000"');
82
+ // 第二层:纯描边,线宽为 2
83
+ const layerStroke = match.replace(/fill=".*?"/g, 'fill="none"')
84
+ .replace(/stroke-width=".*?"/g, 'stroke-width="2"')
85
+ .replace(/stroke="none"/g, 'stroke="#000000"');
86
+ return layerFill + layerStroke;
87
+ });
88
 
89
  const id = crypto.randomUUID();
90
  const inputFile = path.join('/tmp', `${id}_in.svg`);
91
  const outputFile = path.join('/tmp', `${id}_out.svg`);
92
 
93
+ fs.writeFileSync(inputFile, solidSvg);
94
 
95
+ // --- 步骤 2: 指令链 ---
96
+ // 先把所有东西转为 Path,再全选 Union
97
+ const actions = [
 
 
 
98
  `file-open:${inputFile}`,
99
+ 'select-all:all',
100
+ 'object-to-path',
101
+ 'select-all:all',
102
  'object-stroke-to-path',
103
+ 'select-all:all',
104
  'path-union',
105
+ 'path-simplify', // 关键:简化路径通常能自动闭合镂空
106
  `export-filename:${outputFile}`,
107
  'export-do',
108
  'file-close'
 
110
 
111
  const command = `xvfb-run -a inkscape --actions="${actions}"`;
112
 
113
+ exec(command, (error) => {
114
+ if (!fs.existsSync(outputFile)) return res.status(500).send('Error');
 
 
 
 
 
 
 
115
 
116
  let result = fs.readFileSync(outputFile, 'utf8');
117
 
118
+ // 后处理:转回 iconfont 格式
119
  result = result.replace(/fill="#000000"/g, 'fill="currentColor"')
120
  .replace(/stroke=".*?"/g, 'stroke="none"')
121
  .replace(/stroke-width=".*?"/g, 'stroke-width="0"');
122
 
123
  res.send(result);
124
+ [inputFile, outputFile].forEach(f => { if(fs.existsSync(f)) fs.unlinkSync(f); });
 
 
 
 
125
  });
126
  });
127