2020-08-05 05:37:12 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const mimeType = require('mime-types')
|
2020-08-12 10:03:35 +00:00
|
|
|
const sizeOf = require('image-size')
|
2020-08-05 05:37:12 +00:00
|
|
|
|
|
|
|
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 => {
|
2020-08-12 10:03:35 +00:00
|
|
|
const imgPath = path.resolve(themePath, theme, img)
|
2020-08-05 05:37:12 +00:00
|
|
|
const name = path.parse(img).name
|
2020-08-12 10:03:35 +00:00
|
|
|
const { width, height } = sizeOf(imgPath)
|
|
|
|
|
|
|
|
themeList[theme][name] = {
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
data: convertToDatauri(imgPath)
|
|
|
|
}
|
2020-08-05 05:37:12 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
function convertToDatauri(path){
|
|
|
|
const mime = mimeType.lookup(path)
|
|
|
|
const base64 = fs.readFileSync(path).toString('base64')
|
|
|
|
|
|
|
|
return `data:${mime};base64,${base64}`
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:03:35 +00:00
|
|
|
function getCountImage({ count, theme='moebooru', length=7 }) {
|
|
|
|
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(length, '0').split('')
|
|
|
|
|
|
|
|
let x = 0, y = 0
|
|
|
|
const parts = countArray.reduce((acc, next, index) => {
|
|
|
|
const { width, height, data } = themeList[theme][next]
|
|
|
|
|
|
|
|
const image = `${acc}
|
|
|
|
<image x="${x}" y="0" width="${width}" height="${height}" xlink:href="${data}" />`
|
|
|
|
|
|
|
|
x += width
|
|
|
|
|
|
|
|
if(height > y) y = height
|
|
|
|
|
|
|
|
return image
|
|
|
|
}, '')
|
|
|
|
|
|
|
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
2022-04-30 18:09:32 +00:00
|
|
|
<svg width="${x}" height="${y}" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="image-rendering: pixelated;">
|
2020-08-12 10:03:35 +00:00
|
|
|
<title>Moe Count</title>
|
|
|
|
<g>
|
|
|
|
${parts}
|
|
|
|
</g>
|
|
|
|
</svg>
|
|
|
|
`
|
2020-08-05 05:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2020-08-12 10:03:35 +00:00
|
|
|
getCountImage
|
2022-04-30 18:09:32 +00:00
|
|
|
}
|