-
Notifications
You must be signed in to change notification settings - Fork 0
/
opentype.js
101 lines (77 loc) · 2.04 KB
/
opentype.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
var express = require('express');
var app = express();
var opentype = require('opentype.js')
var swig = require('swig');
app.get('/', function(req, res) {
opentype.load('./fonts/bello_script.ttf', function(err, font) {
if (err) {
console.log(err);
} else {
var word = "My word";
var fontSize = 70;
var fontScale = 1 / font.unitsPerEm * fontSize;
// Note that ascender / descender are bottom-up, so we need to flip (negate) them.
var topY = -(font.ascender * fontScale);
var bottomY = -(font.descender * fontScale);
var pathObj = font.getPath(word, 0,60, fontSize);
var pathData = pathObj.toPathData(1);
var path = generateTag('path', {
d: pathData,
});
var topLine = generateTag('line', {
'x1':0,
'y1':topY,
'x2':1000,
'y2':topY,
'stroke-width':3,
'stroke':'blue'
});
var zeroLine = generateTag('line', {
'x1':0,
'y1':0,
'x2':1000,
'y2':0,
'stroke-width':3,
'stroke':'green'
});
var bottomLine = generateTag('line', {
'x1':0,
'y1':bottomY,
'x2':1000,
'y2':bottomY,
'stroke-width':3,
'stroke':'red'
});
var svg = generateTag('svg', {
'fill':'black',
'stroke':'black',
'stroke-width':0.1
}, [path, zeroLine,topLine,bottomLine]);
paramsRenderingPage = {
"svg": svg
};
var template = swig.renderFile('template.html', paramsRenderingPage);
res.write(template);
res.end();
}
});
});
var generateTag = function(nameTag, propertiesObj, insideTags) {
var finalString = "<" + nameTag + " ";
for (property in propertiesObj) {
finalString += property + '="' + propertiesObj[property] + '" ';
}
finalString += ">";
if (insideTags != undefined) {
for (var i = 0; i < insideTags.length; i++) {
finalString += insideTags[i];
}
}
finalString += "</" + nameTag + ">";
return finalString;
}
var server = app.listen(8900, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Logo generator listening at http://localhost:8900');
});