forked from launchaco/logo_builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont-selector.js
33 lines (24 loc) · 935 Bytes
/
font-selector.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
const fs = require("fs");
let KDTree = require('kd-tree-javascript/kdTree').kdTree;
// Euclidian distance is used
const distance = function(a, b){
return Math.pow(a.era - b.era, 2) + Math.pow(a.maturity - b.maturity, 2) +
Math.pow(a.weight - b.weight, 2) + Math.pow(a.personality - b.personality, 2) +
Math.pow(a.definition - b.definition, 2) + Math.pow(a.concept - b.concept, 2);
};
const getCol = (matrix, col) => {
var column = [];
for (var i = 0; i < matrix.length; i++){
column.push(matrix[i][col]);
}
return column;
}
const getRecommendedFont = (payload, amountNear) => {
const fonts = JSON.parse(fs.readFileSync(`./dataset/OFL/${payload.type}.json`));
const tree = new KDTree(fonts, distance, ["era", "maturity", "weight", "personality", "definition", "concept"]);
const nearest = tree.nearest(payload, amountNear);
return getCol(nearest, 0);
};
module.exports = {
getRecommendedFont,
};