This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
forked from danmarshall/google-font-to-svg-path
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
executable file
·392 lines (334 loc) · 15.6 KB
/
index.ts
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
///<reference path="node_modules/makerjs/index.d.ts" />
////<reference path="node_modules/@jscad/stl-serializer/index.d.ts" />
var makerjs = require('makerjs') as typeof MakerJs;
// const stlSerializer = require('@jscad/stl-serializer');
type FillRule = 'nonzero' | 'evenodd';
class App {
public fontList: google.fonts.WebfontList;
private fileUpload: HTMLInputElement
private fileUploadRemove: HTMLInputElement
private customFont: opentype.Font;
private selectFamily: HTMLSelectElement;
private selectVariant: HTMLSelectElement;
private unionCheckbox: HTMLInputElement;
private filledCheckbox: HTMLInputElement;
private kerningCheckbox: HTMLInputElement;
private separateCheckbox: HTMLInputElement;
private textInput: HTMLInputElement;
private bezierAccuracy: HTMLInputElement;
private selectUnits: HTMLSelectElement;
private sizeInput: HTMLInputElement;
private renderDiv: HTMLDivElement;
private outputTextarea: HTMLTextAreaElement;
private copyToClipboardBtn: HTMLButtonElement;
private downloadButton: HTMLAnchorElement;
private dxfButton: HTMLAnchorElement;
private createLinkButton: HTMLAnchorElement;
private dummy: HTMLInputElement;
private fillInput: HTMLInputElement;
private strokeInput: HTMLInputElement;
private strokeWidthInput: HTMLInputElement;
private strokeNonScalingCheckbox: HTMLInputElement;
private fillRuleInput: HTMLSelectElement;
private renderCurrent = () => {
var size = this.sizeInput.valueAsNumber;
if (!size) size = parseFloat(this.sizeInput.value);
if (!size) size = 100;
this.render(
this.selectFamily.selectedIndex,
this.selectVariant.selectedIndex,
this.textInput.value,
size,
this.unionCheckbox.checked,
this.filledCheckbox.checked,
this.kerningCheckbox.checked,
this.separateCheckbox.checked,
parseFloat(this.bezierAccuracy.value) || undefined,
this.selectUnits.value,
this.fillInput.value,
this.strokeInput.value,
this.strokeWidthInput.value,
this.strokeNonScalingCheckbox.checked,
this.fillRuleInput.value,
);
};
private loadVariants = () => {
this.selectVariant.options.length = 0;
var f = this.fontList.items[this.selectFamily.selectedIndex];
var v = f.variants.forEach(v => this.addOption(this.selectVariant, v));
this.renderCurrent();
};
private downloadSvg = () => {
var SvgFile = window.btoa(this.outputTextarea.value);
this.downloadButton.href = 'data:image/svg+xml;base64,' + SvgFile;
this.downloadButton.download = this.textInput.value;
};
private downloadDxf = () => {
var dxfFile = window.btoa(this.renderDiv.getAttribute('data-dxf'));
this.dxfButton.href = 'data:application/dxf;base64,' + dxfFile;
this.dxfButton.download = this.textInput.value + '.dxf';
}
private copyToClipboard = () => {
this.outputTextarea.select();
document.execCommand('copy');
this.copyToClipboardBtn.innerText = 'copied';
setTimeout(() => {
this.copyToClipboardBtn.innerText = 'copy to clipboard';
}, 2000)
};
private updateUrl = () => {
var urlSearchParams = new URLSearchParams(window.location.search);
urlSearchParams.set('font-select', this.selectFamily.value);
urlSearchParams.set('font-variant', this.selectVariant.value);
urlSearchParams.set('input-union', String(this.unionCheckbox.checked));
urlSearchParams.set('input-filled', String(this.filledCheckbox.checked));
urlSearchParams.set('input-kerning', String(this.kerningCheckbox.checked));
urlSearchParams.set('input-separate', String(this.separateCheckbox.checked));
urlSearchParams.set('input-text', this.textInput.value);
urlSearchParams.set('input-bezier-accuracy', this.bezierAccuracy.value);
urlSearchParams.set('dxf-units', this.selectUnits.value);
urlSearchParams.set('input-size', this.sizeInput.value);
urlSearchParams.set('input-fill', this.fillInput.value);
urlSearchParams.set('input-stroke', this.strokeInput.value);
urlSearchParams.set('input-strokeWidth', this.strokeWidthInput.value);
urlSearchParams.set('input-fill-rule', this.fillRuleInput.value);
const url = window.location.protocol
+ "//" + window.location.host
+ window.location.pathname
+ "?"
+ urlSearchParams.toString();
window.history.replaceState({path: url}, "", url)
this.copyString(window.location.href)
this.createLinkButton.innerText = 'copied';
setTimeout(() => {
this.createLinkButton.innerText = 'create link';
}, 2000)
}
private copyString = (string: string) => {
this.dummy.value = string;
this.dummy.type = 'text';
this.dummy.select();
document.execCommand('copy');
this.dummy.type = 'hidden';
}
private readUploadedFile = async (event: Event) => {
const element = event.currentTarget as HTMLInputElement;
if (element.files.length === 0) {
this.customFont = undefined;
} else {
var files = element.files[0];
var buffer = await files.arrayBuffer();
var font = opentype.parse(buffer);
this.customFont = font;
}
this.renderCurrent();
}
private removeUploadedFont = () => {
this.fileUpload.value = null;
this.customFont = undefined;
this.renderCurrent();
}
constructor() {
}
init() {
this.fileUpload = this.$('#font-upload') as HTMLInputElement;
this.fileUploadRemove = this.$('#font-upload-remove') as HTMLInputElement;
this.selectFamily = this.$('#font-select') as HTMLSelectElement;
this.selectVariant = this.$('#font-variant') as HTMLSelectElement;
this.unionCheckbox = this.$('#input-union') as HTMLInputElement;
this.filledCheckbox = this.$('#input-filled') as HTMLInputElement;
this.kerningCheckbox = this.$('#input-kerning') as HTMLInputElement;
this.separateCheckbox = this.$('#input-separate') as HTMLInputElement;
this.textInput = this.$('#input-text') as HTMLInputElement;
this.bezierAccuracy = this.$('#input-bezier-accuracy') as HTMLInputElement;
this.selectUnits = this.$('#dxf-units') as HTMLSelectElement;
this.sizeInput = this.$('#input-size') as HTMLInputElement;
this.renderDiv = this.$('#svg-render') as HTMLDivElement;
this.outputTextarea = this.$('#output-svg') as HTMLTextAreaElement;
this.downloadButton = this.$("#download-btn") as HTMLAnchorElement;
this.dxfButton = this.$("#dxf-btn") as HTMLAnchorElement;
this.createLinkButton = this.$("#create-link") as HTMLAnchorElement;
this.copyToClipboardBtn = this.$("#copy-to-clipboard-btn") as HTMLButtonElement;
this.dummy = this.$('#dummy') as HTMLInputElement;
this.fillInput = this.$('#input-fill') as HTMLInputElement;
this.strokeInput = this.$('#input-stroke') as HTMLInputElement;
this.strokeWidthInput = this.$('#input-stroke-width') as HTMLInputElement;
this.strokeNonScalingCheckbox = this.$('#input-stroke-non-scaling') as HTMLInputElement;
this.fillRuleInput = this.$("#input-fill-rule") as HTMLSelectElement;
// Init units select.
Object.values(makerjs.unitType).forEach(unit => this.addOption(this.selectUnits, unit));
}
readQueryParams() {
var urlSearchParams = new URLSearchParams(window.location.search);
var selectFamily = urlSearchParams.get('font-select');
var selectVariant = urlSearchParams.get('font-variant');
var unionCheckbox = urlSearchParams.get('input-union');
var filledCheckbox = urlSearchParams.get('input-filled');
var kerningCheckbox = urlSearchParams.get('input-kerning');
var separateCheckbox = urlSearchParams.get('input-separate');
var textInput = urlSearchParams.get('input-text');
var bezierAccuracy = urlSearchParams.get('input-bezier-accuracy');
var selectUnits = urlSearchParams.get('dxf-units');
var sizeInput = urlSearchParams.get('input-size');
var fillInput = urlSearchParams.get('input-fill');
var strokeInput = urlSearchParams.get('input-stroke');
var strokeWidthInput = urlSearchParams.get('input-stroke-width');
var strokeNonScalingCheckbox = urlSearchParams.get('input-stroke-non-scaling');
var fillRuleInput = urlSearchParams.get('input-fill-rule');
if (selectFamily !== "" && selectFamily !== null)
this.selectFamily.value = selectFamily;
if (selectVariant !== "" && selectVariant !== null)
this.selectVariant.value = selectVariant;
if (selectUnits !== "" && selectUnits !== null)
this.selectUnits.value = selectUnits;
if (unionCheckbox !== "" && unionCheckbox !== null)
this.unionCheckbox.checked = unionCheckbox === "true" ? true : false;
if (filledCheckbox !== "" && filledCheckbox !== null)
this.filledCheckbox.checked = filledCheckbox === "true" ? true : false;
if (kerningCheckbox !== "" && kerningCheckbox !== null)
this.kerningCheckbox.checked = kerningCheckbox === "true" ? true : false;
if (separateCheckbox !== "" && separateCheckbox !== null)
this.separateCheckbox.checked = separateCheckbox === "true" ? true : false;
if (textInput !== "" && textInput !== null)
this.textInput.value = textInput;
if (bezierAccuracy !== "" && bezierAccuracy !== null)
this.bezierAccuracy.value = bezierAccuracy;
if (sizeInput !== "" && sizeInput !== null)
this.sizeInput.value = sizeInput;
if (fillInput !== "" && fillInput !== null)
this.fillInput.value = fillInput;
if (strokeInput !== "" && strokeInput !== null)
this.strokeInput.value = strokeInput;
if (strokeWidthInput !== "" && strokeWidthInput !== null)
this.strokeWidthInput.value = strokeWidthInput;
if (strokeNonScalingCheckbox !== "" && strokeNonScalingCheckbox !== null)
this.strokeNonScalingCheckbox.checked = strokeNonScalingCheckbox === "true" ? true : false;
if (fillRuleInput !== "" && fillRuleInput !== null)
this.fillRuleInput.value = fillRuleInput;
}
handleEvents() {
this.fileUpload.onchange = this.readUploadedFile;
this.fileUploadRemove.onclick = this.removeUploadedFont
this.selectFamily.onchange = this.loadVariants;
this.selectVariant.onchange =
this.textInput.onchange =
this.textInput.onkeyup =
this.sizeInput.onkeyup =
this.unionCheckbox.onchange =
this.filledCheckbox.onchange =
this.kerningCheckbox.onchange =
this.separateCheckbox.onchange =
this.bezierAccuracy.onchange =
this.bezierAccuracy.onkeyup =
this.selectUnits.onchange =
this.fillInput.onchange =
this.fillInput.onkeyup =
this.strokeInput.onchange =
this.strokeInput.onkeyup =
this.strokeWidthInput.onchange =
this.strokeWidthInput.onkeyup =
this.strokeNonScalingCheckbox.onchange =
this.fillRuleInput.onchange =
this.renderCurrent
;
// Is triggered on the document whenever a new color is picked
document.addEventListener("coloris:pick", debounce(this.renderCurrent))
this.copyToClipboardBtn.onclick = this.copyToClipboard;
this.downloadButton.onclick = this.downloadSvg;
this.dxfButton.onclick = this.downloadDxf;
this.createLinkButton.onclick = this.updateUrl;
}
$(selector: string) {
return document.querySelector(selector);
}
addOption(select: HTMLSelectElement, optionText: string) {
var option = document.createElement('option');
option.text = optionText;
option.value = optionText;
select.options.add(option);
}
getGoogleFonts(apiKey: string) {
var xhr = new XMLHttpRequest();
xhr.open('get', 'https://www.googleapis.com/webfonts/v1/webfonts?key=' + apiKey, true);
xhr.onloadend = () => {
this.fontList = JSON.parse(xhr.responseText);
this.fontList.items.forEach(font => this.addOption(this.selectFamily, font.family));
this.loadVariants();
this.handleEvents();
this.readQueryParams();
this.renderCurrent();
};
xhr.send();
}
callMakerjs(font: opentype.Font, text: string, size: number, union: boolean, filled: boolean, kerning: boolean, separate: boolean,
bezierAccuracy: number, units: string, fill: string, stroke: string, strokeWidth: string, strokeNonScaling: boolean, fillRule: FillRule) {
//generate the text using a font
var textModel = new makerjs.models.Text(font, text, size, union, false, bezierAccuracy, { kerning });
if (separate) {
for (var i in textModel.models) {
textModel.models[i].layer = i;
}
}
var svg = makerjs.exporter.toSVG(textModel, {
fill: filled ? fill : undefined,
stroke: stroke ? stroke : undefined,
strokeWidth: strokeWidth ? strokeWidth : undefined,
fillRule: fillRule ? fillRule : undefined,
scalingStroke: !strokeNonScaling,
});
var dxf = makerjs.exporter.toDXF(textModel, {units: units, usePOLYLINE: true});
this.renderDiv.innerHTML = svg;
this.renderDiv.setAttribute('data-dxf', dxf);
this.outputTextarea.value = svg;
}
render(
fontIndex: number,
variantIndex: number,
text: string,
size: number,
union: boolean,
filled: boolean,
kerning: boolean,
separate: boolean,
bezierAccuracy: number,
units: string,
fill: string,
stroke: string,
strokeWidth: string,
strokeNonScaling: boolean,
fillRule: string,
) {
var f = this.fontList.items[fontIndex];
var v = f.variants[variantIndex];
var url = f.files[v].substring(5); //remove http:
if (this.customFont !== undefined) {
this.callMakerjs(this.customFont, text, size, union, filled, kerning, separate, bezierAccuracy, units, fill, stroke, strokeWidth, strokeNonScaling, fillRule);
} else {
opentype.load(url, (err, font) => {
this.callMakerjs(font, text, size, union, filled, kerning, separate, bezierAccuracy, units, fill, stroke, strokeWidth, strokeNonScaling, fillRule);
});
}
}
}
var app = new App();
window.onload = () => {
app.init();
app.getGoogleFonts('AIzaSyAOES8EmKhuJEnsn9kS1XKBpxxp-TgN8Jc');
};
/**
* Creates and returns a new debounced version of the passed function that will
* postpone its execution until after wait milliseconds have elapsed since the last time it was invoked.
*
* @param callback
* @param wait
* @returns
*/
function debounce(callback, wait = 200) {
let timeoutId = null;
return (...args) => {
window.clearTimeout(timeoutId);
timeoutId = window.setTimeout(() => {
callback.apply(null, args);
}, wait);
};
}