-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsufonify.js
206 lines (164 loc) · 5.41 KB
/
jsufonify.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
// through2 is a thin wrapper around node transform streams
var through = require('through2'),
vm = require('vm'),
// gutil = require('gulp-util'),
_ = require('lodash');
// consts
const PLUGIN_NAME = 'gulp-jsufonify';
function addComponents( glyph ) {
_(glyph.components).forEach(function( component, i ) {
glyph.outline.component[i] = component;
component.anchor = [];
_(component.parentAnchors).forEach(function( anchor, i ) {
component.anchor[i] = anchor;
});
delete component.parentAnchors;
component.parameter = {};
_(component.parentParameters).forEach(function( parameter, i ) {
component.parameter[i] = parameter;
});
delete component.parentParameters;
});
}
function linkToRelatedGlyphs(glyph, glyphsByName) {
var base = glyphsByName[glyph.base];
base.relatedGlyphs = base.relatedGlyphs || [];
glyph.relatedGlyphs = glyph.relatedGlyphs || [];
base.relatedGlyphs.forEach(function(name) {
glyphsByName[name].relatedGlyphs = glyphsByName[name].relatedGlyphs || [];
glyphsByName[name].relatedGlyphs.push(glyph.name);
});
glyph.relatedGlyphs = glyph.relatedGlyphs.concat(base.name, base.relatedGlyphs);
base.relatedGlyphs.push(glyph.name);
}
// plugin level function (dealing with files)
function jsufonify(/*prefixText*/free) {
// creating a stream through which each file will pass
var stream = through.obj(function(file, enc, cb) {
var sandbox = { exports: { glyphs: {} } },
font;
vm.runInNewContext( file.contents, sandbox );
font = sandbox.exports;
var charMap = {};
if (free) {
font.glyphs = _.mapValues(font.glyphs, (glyph) => {
if (glyph.unicode === undefined) {
return glyph;
}
else {
if (typeof glyph.unicode === 'number') {
return (glyph.unicode >=65 && glyph.unicode <= 90) ||
(glyph.unicode >= 87 && glyph.unicode <= 122) ? glyph : undefined;
}
else {
return (glyph.unicode.charCodeAt(0) >=65 && glyph.unicode.charCodeAt(0) <= 90) ||
(glyph.unicode.charCodeAt(0) >= 87 && glyph.unicode.charCodeAt(0) <= 122) ? glyph : undefined;
}
}
return glyph.unicode === undefined ||
(glyph.unicode.charCodeAt(0) >=65 && glyph.unicode.charCodeAt(0) <= 90) ||
(glyph.unicode.charCodeAt(0) >= 87 && glyph.unicode.charCodeAt(0) <= 122) ? glyph : undefined;
});
}
// WIP: convert ptf object to jsufon
_.forEach(font.glyphs, function( glyph, name ) {
if (glyph === undefined) {
delete font.glyphs[name];
return;
}
glyph.name = name;
if ( glyph.name.length === 1 ) {
glyph.unicode = glyph.name.charCodeAt(0);
} else if ( typeof glyph.unicode === 'string' ) {
glyph.unicode = glyph.unicode.charCodeAt(0);
}
charMap[glyph.unicode] = glyph;
// glyph.anchors -> glyph.anchor
if ( glyph.anchors ) {
glyph.anchor = [];
_(glyph.anchors).forEach(function( anchor, i ) {
glyph.anchor[i] = anchor;
});
}
delete glyph.anchors;
// glyph.parameters -> glyph.anchor
if ( glyph.parameters ) {
glyph.parameter = {};
_(glyph.parameters).forEach(function( parameter, i ) {
glyph.parameter[i] = parameter;
});
}
delete glyph.parameters;
if ( !glyph.outline ) {
glyph.outline = {};
}
// glyph.contours -> glyph.outline.contour
if ( !glyph.outline.contour ) {
glyph.outline.contour = [];
}
_(glyph.contours).forEach(function( contour, i ) {
contour.point = [];
// contour.nodes -> contour.point
_(contour.nodes).forEach(function( node, j ) {
contour.point[j] = node;
});
delete contour.nodes;
glyph.outline.contour[i] = contour;
});
delete glyph.contours;
// glyph.components -> glyph.outline.component
if ( !glyph.outline.component ) {
glyph.outline.component = [];
}
addComponents( glyph );
delete glyph.components;
if ( !glyph.lib ) {
glyph.lib = {};
}
// glyph.transform -> glyph.outline.transform
if ( glyph.transformList ) {
glyph.lib = {
transformList: glyph.transformList
};
delete glyph.transformList;
}
return glyph;
});
// temporary workaround, add diacritics base handling here
_.forEach(font.glyphs, function( _glyph ) {
// Temporary workaround: deal with diacritics here.
if ( _glyph.base === undefined ) {
return;
}
// we first try to get the base from name (to differenciate alternates) otherwise we look in the unicode map
var base = font.glyphs[_glyph.base] || charMap[_glyph.base.charCodeAt(0)]
// we'll save the diacritics sourcs, replace it with the base glyph
// source and then restore/merge the properties we're interested in
var glyph = _.clone(base, true);
glyph.name = _glyph.name;
glyph.base = base.name;
glyph.unicode = _glyph.unicode;
glyph.tags = _glyph.tags;
glyph.glyphName = _glyph.glyphName;
glyph.characterName = _glyph.characterName;
// merge all parameters (diacritic will overwrite base parameters)
_.assign( glyph.parameter, _glyph.parameter );
// merge the two array of components
[].push.apply(glyph.outline.component, _glyph.outline.component );
font.glyphs[_glyph.name] = glyph;
});
_.forEach(font.glyphs, function(glyph) {
if(glyph.base === undefined) {
return;
}
linkToRelatedGlyphs(glyph, font.glyphs);
})
file.contents = new Buffer( JSON.stringify( sandbox.exports ) );
this.push(file);
return cb();
});
// returning the file stream
return stream;
}
// exporting the plugin main function
module.exports = jsufonify;