forked from danmarshall/google-font-to-svg-path
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
104 lines (83 loc) · 3.83 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
var makerjs = require('makerjs') as typeof makerjs;
class App {
public fontList: google.fonts.WebfontList;
private selectFamily: HTMLSelectElement;
private selectVariant: HTMLSelectElement;
private unionCheckbox: HTMLInputElement;
private separateCheckbox: HTMLInputElement;
private textInput: HTMLInputElement;
private bezierAccuracy: HTMLInputElement;
private sizeInput: HTMLInputElement;
private renderDiv: HTMLDivElement;
private outputTextarea: HTMLTextAreaElement;
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.separateCheckbox.checked, parseFloat(this.bezierAccuracy.value) || undefined);
};
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();
};
constructor() {
}
init() {
this.selectFamily = this.$('#font-select') as HTMLSelectElement;
this.selectVariant = this.$('#font-variant') as HTMLSelectElement;
this.unionCheckbox = this.$('#input-union') 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.sizeInput = this.$('#input-size') as HTMLInputElement;
this.renderDiv = this.$('#svg-render') as HTMLDivElement;
this.outputTextarea = this.$('#output-svg') as HTMLTextAreaElement;
}
handleEvents() {
this.selectFamily.onchange = this.loadVariants;
this.selectVariant.onchange = this.textInput.onchange = this.textInput.onkeyup = this.sizeInput.onchange = this.unionCheckbox.onchange = this.separateCheckbox.onchange = this.bezierAccuracy.onchange = this.renderCurrent;
}
$(selector: string) {
return document.querySelector(selector);
}
addOption(select: HTMLSelectElement, optionText: string) {
var option = document.createElement('option');
option.text = 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();
};
xhr.send();
}
render(fontIndex: number, variantIndex: number, text: string, size: number, union: boolean, separate: boolean, bezierAccuracy: number) {
var f = this.fontList.items[fontIndex];
var v = f.variants[variantIndex];
var url = f.files[v].substring(5); //remove http:
opentype.load(url, (err, font) => {
//generate the text using a font
var textModel = new makerjs.models.Text(font, text, size, union, false, bezierAccuracy);
if (separate) {
for (var i in textModel.models) {
textModel.models[i].layer = i;
}
}
var svg = makerjs.exporter.toSVG(textModel);
this.renderDiv.innerHTML = svg;
this.outputTextarea.value = svg;
});
}
}
var app = new App();
window.onload = () => {
app.init();
app.getGoogleFonts('AIzaSyAOES8EmKhuJEnsn9kS1XKBpxxp-TgN8Jc');
};