-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
179 lines (143 loc) · 4.28 KB
/
worker.js
File metadata and controls
179 lines (143 loc) · 4.28 KB
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
function prepareWorker() {
function runWorker() {
var font,
handlers = {},
fontsMap = {},
currValues,
currSubset = [],
translateSubset = function() {
if ( !currSubset.length ) {
return;
}
font.subset = currSubset.map(function( glyph ) {
return font.charMap[ glyph.ot.unicode ];
}).filter(Boolean);
currSubset = font.subset;
};
prototypo.paper.setup({
width: 1024,
height: 1024
});
// mini router
self.onmessage = function(e) {
var result;
if ( e.data.type && e.data.type in handlers ) {
result = handlers[ e.data.type ]( e.data.data, e.data.name );
if ( result === null ) {
return;
}
self.postMessage(
result,
result instanceof ArrayBuffer ? [ result ] : undefined );
}
};
handlers.font = function( fontSource, name ) {
// TODO: this should be done using a memoizing table of limited size
if ( name in fontsMap ) {
font = fontsMap[name];
translateSubset();
return null;
}
var fontObj = JSON.parse( fontSource );
font = prototypo.parametricFont(fontObj);
fontsMap[name] = font;
translateSubset();
var solvingOrders = {};
Object.keys( font.glyphMap ).forEach(function(key) {
solvingOrders[key] = font.glyphMap[key].solvingOrder;
});
return solvingOrders;
};
handlers.update = function( params ) {
currValues = params;
font.update( currValues );
font.updateOTCommands();
var result = font.toArrayBuffer();
return result;
};
handlers.soloAlternate = function( params ) {
font.setAlternateFor( params.unicode, params.glyphName );
if (!currValues) {
return true;
}
font.subset = font.subset.map(function( glyph ) {
return String.fromCharCode(glyph.unicode);
}).join('');
var altGlyph = font.glyphMap[params.glyphName];
altGlyph.update( currValues );
altGlyph.updateOTCommands();
// Recreate the correct font.ot.glyphs.glyphs object, without
// touching the ot commands
font.updateOT({ set: undefined });
return font.toArrayBuffer();
};
handlers.alternate = function( params ) {
if ( params.altList ) {
Object.keys( params.altList ).forEach(function( unicode ) {
handlers.soloAlternate({
unicode: unicode,
glyphName: params.altList[unicode]
});
});
} else {
handlers.soloAlternate( params );
}
};
handlers.subset = function( set ) {
var prevGlyphs = currSubset.map(function( glyph ) {
return glyph.name;
});
font.subset = set;
currSubset = font.subset;
if ( !currValues ) {
return true;
}
// search for glyphs *added* to the subset
currSubset.filter(function( glyph ) {
return prevGlyphs.indexOf( glyph.name ) === -1;
// update those glyphs
}).forEach(function( glyph ) {
glyph.update( currValues );
glyph.updateOTCommands();
});
// Recreate the correct font.ot.glyphs.glyphs object, without
// touching the ot commands
font.updateOT({ set: undefined });
return font.toArrayBuffer();
};
handlers.otfFont = function(data) {
// force-update of the whole font, ignoring the current subset
var allChars = font.getGlyphSubset( false );
var fontValues = data && data.values || currValues;
font.update( fontValues, allChars );
font.updateOTCommands( allChars, data && data.merged || false );
var family = font.ot.familyName;
var style = font.ot.styleName;
//TODO: understand why we need to save the familyName and
//and set them back into the font.ot for it to be able to
//export multiple font
font.ot.familyName = data && data.family || 'Prototypo';
font.ot.styleName = data && data.style || 'regular';
var result = font.toArrayBuffer();
font.ot.familyName = family;
font.ot.styleName = style;
return result;
};
}
// This is how bundle dependencies are loaded
if ( typeof global === 'undefined' && 'importScripts' in self ) {
var handler = function initWorker( e ) {
self.removeEventListener('message', handler);
self.importScripts( e.data );
runWorker();
self.postMessage('ready');
};
self.addEventListener('message', handler);
}
}
// When the worker is loaded from URL, worker() needs to be called explicitely
if ( typeof global === 'undefined' && 'importScripts' in self ) {
prepareWorker();
} else {
module.exports = prepareWorker;
}