-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
205 lines (188 loc) · 5.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var gulp = require('gulp');
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var needle = require('needle');
var unzip = require('unzip');
var SvgPath = require('svgpath');
var map = require('map-stream');
var utils = require('./lib/utils');
var svg_image_flatten = require('./lib/_svg_image_flatten');
var customIcons = [];
var fontelloIcons = [];
var maxCode = _.max(customIcons, function(glyph) {
return glyph.code;
}).code;
var allocatedRefCode = (!maxCode) ? 59392 : maxCode + 1;
var HOST = 'http://fontello.com';
function apiRequest(options, successCallback, errorCallback) {
var data;
if (options.host == null) {
options.host = HOST;
}
data = {
config: {
file: options.config,
content_type: 'application/json'
}
};
return needle.post(options.host, data, {
multipart: true
}, function(error, response, body) {
var sessionId, sessionUrl;
if (error) {
throw error;
}
sessionId = body;
if (response.statusCode === 200) {
sessionUrl = "" + options.host + "/" + sessionId;
return typeof successCallback === "function" ? successCallback(sessionUrl) : void 0;
} else {
return typeof errorCallback === "function" ? errorCallback(response) : void 0;
}
});
}
function getIconFont(options, cb) {
return apiRequest(options, function(sessionUrl) {
var zipFile;
zipFile = needle.get("" + sessionUrl + "/get", function(error, response, body) {
if (error) {
throw error;
}
});
if (options.css && options.font) {
return zipFile.pipe(unzip.Parse()).on('entry', (function(entry) {
var cssPath, dirName, fileName, fontPath, pathName, type, _ref;
pathName = entry.path, type = entry.type;
if (type === 'File') {
dirName = (_ref = path.dirname(pathName).match(/\/([^\/]*)$/)) != null ? _ref[1] : void 0;
fileName = path.basename(pathName);
switch (dirName) {
case 'css':
cssPath = path.join(options.css, fileName);
return entry.pipe(fs.createWriteStream(cssPath));
case 'font':
fontPath = path.join(options.font, fileName);
return entry.pipe(fs.createWriteStream(fontPath));
default:
return entry.autodrain();
}
}
})).on('finish', (function() {
console.log('Install complete.\n');
cb();
return;
}));
} else {
return zipFile.pipe(unzip.Extract({
path: 'icon-example'
})).on('finish', (function() {
console.log('Install complete.\n');
cb();
return;
}));
}
});
}
function getSvgSrcFiles(opts, cb) {
var config = JSON.parse(fs.readFileSync(opts.config || 'config.json'));
getCustomIcons(config);
var svgList = [];
var stream = gulp.src(path.join(opts.svgsrc, '*.svg')).pipe(map(function(data, callback) {
var name = path.basename(data.path, '.svg');
var content = data._contents.toString('utf8');
var obj = {
content: content,
name: name
};
svgList.push(obj);
// console.log(data._contents.toString('utf8'));
// console.log(path.basename(data.path, '.svg'));
callback(null, obj);
}));
stream.on('end', function() {
// console.log("stream finished");
// console.log(svgList);
_.each(svgList, function(data) {
var entry = processSvg(data);
//console.log(JSON.stringify(entry));
updateCustomIcons(entry);
});
config.glyphs = _.union(customIcons, fontelloIcons);
fs.writeFileSync(opts.config, JSON.stringify(config));
cb();
});
}
function updateCustomIcons(newGlyph) {
var found = false;
var entry = _.find(customIcons, function(glyph) {
return glyph.css === newGlyph.css;
});
if (entry) {
entry.svg = newGlyph.svg;
} else {
customIcons.push(newGlyph);
}
}
function processSvg(data) {
var result = svg_image_flatten(data.content);
if (result.error) {
throw result.error;
}
// var skipped = _.union(result.ignoredTags, result.ignoredAttrs);
// if (skipped.length > 0) {
// console.log('skipped' + skipped.toString());
// } else if (!result.guaranteed) {
// console.log('err_merge_path');
// }
// Scale to standard grid
var scale = 1000 / result.height;
var d = new SvgPath(result.d)
.translate(-result.x, -result.y)
.scale(scale)
.abs()
.round(1)
.toString();
var width = Math.round(result.width * scale); // new width
var glyphName = data.name.replace(/\s/g, '-');
var newGlyph = {
uid: uid(),
css: glyphName,
code: allocatedRefCode++,
src: 'custom_icons',
// charRef: allocatedRefCode++,
search: [glyphName],
selected: true,
svg: {
path: d,
width: width
}
};
return newGlyph;
}
function getCustomIcons(config) {
var allGlyphsArray = config.glyphs;
customIcons = [];
fontelloIcons = [];
for (var i = 0; i < allGlyphsArray.length; ++i) {
if (allGlyphsArray[i].src === "custom_icons") {
customIcons.push(allGlyphsArray[i]);
} else {
fontelloIcons.push(allGlyphsArray[i]);
}
}
}
function uid() {
/*jshint bitwise: false*/
return 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'.replace(/[x]/g, function() {
return ((Math.random() * 16) | 0).toString(16);
});
}
module.exports = {
importSvg: function (options, cb) {
getSvgSrcFiles(options, cb);
},
getFont: function (options, cb) {
getIconFont(options, cb);
}
};