'use strict' const fs = require('fs') const path = require('path') const mimeType = require('mime-types') const sizeOf = require('image-size') const themePath = path.resolve(__dirname, '../assets/theme') const themeList = {} fs.readdirSync(themePath).forEach(theme => { if (!(theme in themeList)) themeList[theme] = {} const imgList = fs.readdirSync(path.resolve(themePath, theme)) imgList.forEach(img => { const imgPath = path.resolve(themePath, theme, img) const name = path.parse(img).name const { width, height } = sizeOf(imgPath) themeList[theme][name] = { width, height, data: convertToDatauri(imgPath) } }) }) function convertToDatauri(path) { const mime = mimeType.lookup(path) const base64 = fs.readFileSync(path).toString('base64') return `data:${mime};base64,${base64}` } function getCountImage({ count, theme = 'moebooru', padding = 7, pixelated = true, darkmode = 'auto' }) { if (!(theme in themeList)) theme = 'moebooru' // This is not the greatest way for generating an SVG but it'll do for now const countArray = count.toString().padStart(padding, '0').split('') const uniqueChar = [...new Set(countArray)] let x = 0, y = 0 const defs = uniqueChar.reduce((ret, cur) => { const { width, height, data } = themeList[theme][cur] if (height > y) y = height ret = `${ret} ` return ret }, '') const parts = countArray.reduce((ret, cur) => { const { width } = themeList[theme][cur] const image = `${ret} ` x += width return image }, '') const style = ` svg { ${pixelated ? 'image-rendering: pixelated;' : ''} ${darkmode === '1' ? 'filter: brightness(.6);' : ''} } ${darkmode === 'auto' ? `@media (prefers-color-scheme: dark) { svg { filter: brightness(.6); } }` : ''} ` return ` Moe Counter! ${defs} ${parts} ` } module.exports = { getCountImage }