-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkerService.js
205 lines (188 loc) · 6.77 KB
/
markerService.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
/*jslint node: true */
"use strict";
/**
* Created by Jcarlos on 10/02/2017.
*/
var express = require("express");
var Jimp = require("jimp");
var async = require("async");
var path = require("path");
var sizeOf = require("image-size");
var svg_to_png = require("svg-to-png");
var fs = require("fs");
var DOMParser = require("xmldom").DOMParser;
var XMLSerializer = require("xmldom").XMLSerializer;
module.exports = {
/**
* changeAttr.
*
* change attributes os an svg file .
*
* @return {string} image Path.
* @param {string} image colour .
* @param {string} svg image Path.
* @param {function} cb callback.
*/
changeAttr: function (colour, newWidth, svg, cb) {
//check colour is a hexa code with regular expresion.
var isHex = /(^[0-9A-Fa-f]{6}$)|(^[0-9A-Fa-f]{3}$)/;
if (!isHex.test(colour)) {
cb("Colour not hexadecimal", null);
return null;
}
if (newWidth > 300) {
cb("Maximum width is 300", null)
return null;
}
fs.readFile(svg, "utf8", function (err, data) {
if (err) {
console.log(err, data);
cb("Error reading file svg", null);
return;
}
var svgFile = new DOMParser().parseFromString(data);
svgFile.getElementById("1").setAttribute("fill", "#" + colour);
var marker = svgFile.getElementById("marker");
var height = marker.getAttribute("height");
var witdh = marker.getAttribute("width");
height = height.replace("px", "");
witdh = witdh.replace("px", "");
var newHeight = (height / witdh) * newWidth;
if (newHeight === undefined || newWidth === undefined) {
cb("height or width undefined", null);
return null;
}
marker.setAttribute("width", newWidth + "px");
marker.setAttribute("height", newHeight + "px");
var newSvgFile = new XMLSerializer().serializeToString(svgFile);
var filename = svg.replace(/^.*[\\\/]/, "");
filename = filename.replace(".svg", "");
filename = filename + "_" + colour + "_" + newWidth;
filename = filename + ".svg";
var pathFile = path.join(__dirname, filename);
fs.writeFile(pathFile, newSvgFile, function (err) {
if (err) {
cb("Error writting file svg", null);
return null;
}
cb("", pathFile);
});
});
},
/**
* convertSvgToPng.
*
* Convert svg file in a png.
*
* @return {string} png image path.
* @param {string} svg image path
* @param {function} cb callback
*/
convertSvgToPng: function (svg, cb) {
if (!fs.existsSync(svg)) {
cb("Svg file doesn't exists", null);
return;
}
svg_to_png.convert(svg, __dirname)
.then(function () {
var filename = svg.replace(/^.*[\\\/]/, "");
filename = filename.replace(".svg", ".png");
fs.unlink(svg, function () {
cb("", path.join(__dirname, filename));
});
}).catch(function (err) {
if (err) {
cb(err, null);
}
});
},
/**
* It Creates an image with the text that is passed as a input
*
* @return {string} image path.
* @param {string} text
* @param {string} font font path
* @param {function} cb callback
*/
createTextImg: function (text, font, cb) {
new Jimp(300, 300, function (err, image) {
if (err) {
cb(err, null);
return
}
//load font of text in the marker (always .fnt)
Jimp.loadFont(font).then(function (font) {
image.print(font, 10, 10, text, function (err) {
if (err) {
cb(err, null);
return
}
var fileName = text.replace("-", "_") + ".png";
image.write(path.join(__dirname, fileName), function () {
cb("", path.join(__dirname, fileName));
});
});
}).catch(function (err) {
if (err) {
cb(err, null);
}
});
});
},
/**
* It combines marker image and text image. Delete temporary files
*
* @return {string} image path.
* @param {string} pngMarker image marker path
* @param {string} pngText image text path
* @param {int} widthMarker width that you want in final image
* @param {function} cb callback
*/
combinePngs: function (pngMarker, pngText, fileName, cb) {
if (!(fs.existsSync(pngMarker) && fs.existsSync(pngText))) {
cb("Png file doesn't exist", null);
return null;
}
var marker = Jimp.read(pngMarker);
var text = Jimp.read(pngText);
Promise.all([marker, text]).then(function (images) {
var marker = images[0];
var text = images[1];
sizeOf(pngMarker, function (err, dimensions) {
if (err) {
cb(err, null);
return null;
}
var heightMarker = dimensions.height;
var widthMarker = dimensions.width;
var widthText = widthMarker;
var heightText = heightMarker / 5;
text.autocrop(function (err) {
if (err) {
cb(err, null);
return null
}
text.contain(widthText, heightText, Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE);
//put text center in marker
var widthPosition = widthMarker / 2 - widthText / 2
var heightPosition = heightMarker / 3 - heightText / 2
marker.composite(text, widthPosition, heightPosition);
//delete temp files
fs.unlink(pngMarker, function () {
fs.unlink(pngText, function () {
//save in cache
var pathFile = path.join(__dirname, "cache", fileName)
marker.write(pathFile, function () {
cb("", pathFile);
});
});
});
});
});
}).catch(function (err) {
if (err) {
cb(err, null);
}
});
}
};