-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
139 lines (119 loc) · 4.22 KB
/
server.js
File metadata and controls
139 lines (119 loc) · 4.22 KB
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
const { raw } = require('body-parser')
const express = require('express')
const fs = require('fs')
const app = express()
const port = 3000
app.use(express.static('fonts/ofl/'))
function renderFont(fontInfo,variationInfo) {
// console.log(fontInfo.meta.fonts[0])
if( variationInfo ) {
parsedVariationInfo = `font-variation-settings:
${Object.entries(variationInfo).map(([k,v])=>`"k" v`).join(',\n')};`
}
firstFont = fontInfo.meta.fonts[0]
let out = `<style>
@font-face {
font-family: "${firstFont.name}";
font-style: ${firstFont.style};
font-weight: ${firstFont.weight};
src: url(/${fontInfo.dir}/${firstFont.filename})
}
</style>
<div style="font-family: '${firstFont.name}'">${firstFont.name} - The quick brown fox jumps over the lazy dog</div>
`
return out
}
// create memory db for queries
// Require minimongo
const minimongo = require("minimongo");
const LocalDb = minimongo.MemoryDb;
db = new LocalDb();
db.addCollection('fonts')
const gf = require('./gf.js')
gf.listMetaData().then(fonts => {
db.fonts.upsert(fonts, (docs) => { console.log(docs.length) }, (err) => { console.log(err) })
})
app.get('/axes', (req, res) => {
db.fonts.find({ 'meta.axes': { $exists: true } }).fetch()
.then(docs => {
const axes = new Map()
for (const doc of docs) {
for (const axe of doc.meta.axes) {
if (!axes.has(axe.tag)) {
axes.set(axe.tag, { count: 0, min: 1000, max: 0 })
}
const axstat = axes.get(axe.tag)
axstat.count++
if (axe.min_value < axstat.min) {
axstat.min = axe.min_value
}
if (axe.max_value > axstat.max) {
axstat.max = axe.max_value
}
}
}
res.send(JSON.stringify([...axes.entries()]))
})
})
app.get('/tags', (req, res) => {
const csvparse = require('csv-parse/sync')
const content = fs.readFileSync('fonts/tags/all/families.csv')
const records = csvparse.parse(content,
{ delimiter: ',', from: 2 })
const lookup = {}
// res.send(JSON.stringify(records))
for (const row of records) {
const [fontFamilyName, groupAndTag, weight] = row
const [, group, tag] = groupAndTag.split('/')
if (!(group in lookup)) {
lookup[group] = {}
}
if (!(tag in lookup[group])) {
lookup[group][tag] = []
}
lookup[group][tag].push([fontFamilyName, weight])
}
res.send(lookup)
})
app.get('/', (req, res) => {
res.write('<html><body><ul>')
filter = req.query
const selector = {}
const variationInfos = []
for ([param, value] of Object.entries(filter)) {
if (param.startsWith('a_')) { // axes
const name = param.substring(2)
selector['meta.axes'] = { $elemMatch: { tag: name } } // cutting off 'a_'
if (value) {
const [min, max] = value.split(':')
if (min) {
selector['meta.axes']['$elemMatch']['min_value'] = { $lte: parseFloat(min) }
variationInfos.push({name: parseFloat(min)})
}
if (max) {
selector['meta.axes']['$elemMatch']['max_value'] = { $gte: parseFloat(max) }
variationInfos.push({name: parseFloat(max)})
}
}
}
}
// { 'meta.axes': { $elemMatch: { 'tag': "wght", 'max_value': { $gt: 800 } } } }
db.fonts.find(selector).fetch()
.then(fonts => {
for (const font of fonts) {
if( variationInfos.length > 0 ) {
// todo: combinatoric
for(const variationInfo of variationInfos) {
res.write(`<li>${renderFont(font, variationInfo)}</li>`)
}
} else {
res.write(`<li>${renderFont(font)}</li>`)
}
}
res.write('</ul></body></html>')
res.end()
})
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})