-
Notifications
You must be signed in to change notification settings - Fork 676
/
Copy pathbasic-cad-text.js
234 lines (210 loc) · 7.22 KB
/
basic-cad-text.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
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
function addText() {
var fontsize = $('#fontsize').val();
var font = $("#font").val().replace(/\+/g, ' ');
// split font into family and weight
font = font.split(':');
// set family on paragraphs
var string = $("#texttorender").val()
// console.log('font-family: ', font[0], " size: ", fontsize, " String: " + string)
// var textasSVG = getText(font[0], "regular", string, fontsize)
//
// Then, use it:
//
getText(font[0], "regular", string, fontsize).then(function(textasSVG) {
// console.log(textasSVG)
var svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 50 50\" width=\"150px\"> <path fill=\"#F7931E\" stroke=\"#000\" d=\"" + textasSVG + "\"/> </svg>"
// console.log(svg)
return lwsvgparser.loadFromString(svg).then(function(element) {
return lwsvgparser.parse().then(function(tags) {
lwsvgparser.editor = {
name: "Opentype.js",
version: "1.00"
};
drawFile("Text: " + string + " (" + font[0] + ")", tags, true);
resetView();
});
})
.catch(function(error) {
console.error('error:', error);
// $("#addShapeText").modal("hide");
resetView();
});
printLog('SVG Opened', msgcolor, "file");
})
// console.log(textasSVG)
// setTimeout(function() {
// var svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 50 50\" width=\"150px\"> <path fill=\"#F7931E\" stroke=\"#000\" d=\"" + textasSVG._result + "\"/> </svg>"
// // console.log(svg)
// return lwsvgparser.loadFromString(svg).then(function(element) {
// return lwsvgparser.parse().then(function(tags) {
// lwsvgparser.editor = {
// name: "Opentype.js",
// version: "1.00"
// };
// drawFile("Text: " + string + " (" + font[0] + ")", tags, true);
// resetView();
// });
// })
// .catch(function(error) {
// console.error('error:', error);
// // $("#addShapeText").modal("hide");
// resetView();
// });
//
// printLog('SVG Opened', msgcolor, "file");
// }, 2000);
}
// FONTS
// Fetch GoogleFonts List
// $.get('https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyB6CfNzjx-T-iGR4-1ECG1bmZ0eNU-4cTU', function (result) {
// console.log(result)
// });
// from https://github.com/nraynaud/webgcode/blob/66e2662fcb72219024976610a6c66d307af84882/webapp/cnc/cam/text.js
var getFont = function(url) {
if (url.match('^http://')) {
url = url.replace("http://", "//")
}
if (url.match('^https://')) {
url = url.replace("https://", "//")
}
return new RSVP.Promise(function(resolve, reject) {
opentype.load(url, function(err, font) {
if (err)
reject();
else
resolve(font);
});
});
};
function getFontList() {
return new RSVP.Promise(
function(resolve, reject) {
$.get('https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyCx9m5LJdE0eljjfS-6UMymK2uoeqhFzuo', function(result) {
resolve(result.items);
});
});
}
function getTextFromData(fontData, fontVariant, text, fontSize, x, y) {
if (fontVariant == null)
fontVariant = 'regular';
x = x == null ? 0 : x;
y = y == null ? 0 : y;
return getTextFromFile(fontData.files[fontVariant], text, fontSize, x, y);
}
function getTextFromFile(file, text, fontSize, offsetX, offsetY) {
return getFont(file).then(function(font) {
console.log(font)
var path = font.getPath(text, 0, 0, fontSize);
var res = '';
function xy(x, y) {
return (offsetX + x) + ',' + (offsetY - y);
}
for (var i = 0; i < path.commands.length; i++) {
var c = path.commands[i];
res += ' ' + c.type;
if (c.type == 'M' || c.type == 'L')
res += ' ' + xy(c.x, c.y);
else if (c.type == 'Q')
res += xy(c.x1, c.y1) + ' ' + xy(c.x, c.y);
else if (c.type == 'C')
res += xy(c.x1, c.y1) + ' ' + xy(c.x2, c.y2) + ' ' + xy(c.x, c.y);
}
// console.log(res)
return res;
})
}
function searchFontInList(fontList, fontFamily) {
for (var i = 0; i < fontList.length; i++) {
var font = fontList[i];
if (font.family == fontFamily)
return font;
}
throw {
name: 'FontNotFound'
};
}
function getText(fontFamily, fontVariant, text, fontSize) {
return new Promise(function(resolve, reject) {
/*stuff using username, password*/
return getFontList().then(function(fontList) {
return getTextFromData(searchFontInList(fontList, fontFamily), fontVariant, text, fontSize);
}).then(function(data) {
// console.log(data)
resolve(data);
})
});
}
// function getText(fontFamily, fontVariant, text, fontSize) {
// return getFontList().then(function(fontList) {
// return getTextFromData(searchFontInList(fontList, fontFamily), fontVariant, text, fontSize);
// })
// }
$(document).ready(function() {
var modal = `
<div class="dialog dark" data-overlay-click-close="true" data-role="dialog" id="addShapeText" data-to-top="true">
<div class="dialog-title" id="statusTitle">Add Text</div>
<div class="dialog-content">
<form>
<div class="form-group row">
Create a new set of text paths
<table>
<tr>
<th style="width: 150px;"></th><th style="width: 210px;"></th>
</tr>
<tr>
<td>Font: </td>
<td>
<div class="input-addon">
<span class="input-addon-label-left active-border"><i class="fas fa-font"></i></span>
<input id="font" type="text" class="cam-form-field active-border" />
</div>
</td>
</tr>
<tr>
<td>Size: </td>
<td>
<div class="input-addon">
<span class="input-addon-label-left active-border"><i class="fas fa-text-height"></i></span>
<input type="number" class="cam-form-field cam-form-field-right active-border" id="fontsize" value="20" />
</div>
</td>
</tr>
</table>
</div>
</form>
<hr/>
<div class="input-addon">
<input style="width: 100%;" id="texttorender" class="active-border" value="Type Here"></input>
</div>
</div>
<div class="dialog-actions" id="statusFooter">
<button class="button js-dialog-close">Cancel</button>
<button type="button" class="button js-dialog-close success" id="CreateText">Create</button>
</div>
</div>
`
$("body").append(modal);
$('#texttorender').css('font-family', "Bowlby One SC");
$('#font').fontselect({
placeholder: 'Bowlby One SC',
lookahead: 3
}).change(function() {
// replace + signs with spaces for css
var font = $(this).val().replace(/\+/g, ' ');
// split font into family and weight
font = font.split(':');
// set family on paragraphs
$('#texttorender').css('font-family', font[0]);
// console.log('font-family', font[0])
var fontsize = $('#fontsize').val();
$('#texttorender').css('font-size', fontsize + "px");
}).val("Bowlby+One+SC");
$('#fontsize').change(function() {
var fontsize = $('#fontsize').val();
$('#texttorender').css('font-size', fontsize + "px");
});
$("#CreateText").on("click", function() {
event.preventDefault();
addText();
});
});