1
0
mirror of https://github.com/journey-ad/Moe-Counter.git synced 2024-11-28 01:10:49 +01:00
Moe-Counter/utils/themify.js

109 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-08-05 07:37:12 +02:00
'use strict'
const fs = require('fs')
const path = require('path')
const mimeType = require('mime-types')
const sizeOf = require('image-size')
2020-08-05 07:37:12 +02:00
const themePath = path.resolve(__dirname, '../assets/theme')
2024-10-25 06:53:34 +02:00
const imgExts = ['.jpg', '.jpeg', '.png', '.gif', '.webp']
2020-08-05 07:37:12 +02:00
const themeList = {}
fs.readdirSync(themePath).forEach(theme => {
2024-10-25 06:53:34 +02:00
const currentThemePath = path.resolve(themePath, theme)
// skip non-directory
if (!fs.statSync(currentThemePath).isDirectory()) return
if (!(theme in themeList)) themeList[theme] = {}
2024-10-25 06:53:34 +02:00
const imgList = fs.readdirSync(currentThemePath)
2020-08-05 07:37:12 +02:00
imgList.forEach(img => {
2024-10-25 06:53:34 +02:00
// skip non-image files
if (!imgExts.includes(path.extname(img).toLowerCase())) return
const imgPath = path.resolve(currentThemePath, img)
const char = path.parse(img).name
const { width, height } = sizeOf(imgPath)
2024-10-25 06:53:34 +02:00
themeList[theme][char] = {
width,
height,
data: convertToDatauri(imgPath)
}
2020-08-05 07:37:12 +02:00
})
})
function convertToDatauri(path) {
2020-08-05 07:37:12 +02:00
const mime = mimeType.lookup(path)
const base64 = fs.readFileSync(path).toString('base64')
return `data:${mime};base64,${base64}`
}
2024-10-20 02:29:58 +02:00
function getCountImage(params) {
let { count, theme = 'moebooru', padding = 7, offset = 0, scale = 1, pixelated = '1', darkmode = 'auto' } = params
if (!(theme in themeList)) theme = 'moebooru'
2024-10-20 02:29:58 +02:00
padding = parseInt(padding, 10)
offset = parseInt(offset, 10)
// 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) => {
2024-10-20 02:29:58 +02:00
let { width, height, data } = themeList[theme][cur]
width *= scale
height *= scale
if (height > y) y = height
ret = `${ret}
<image id="${cur}" width="${width}" height="${height}" xlink:href="${data}" />`
return ret
}, '')
const parts = countArray.reduce((ret, cur) => {
2024-10-20 02:29:58 +02:00
let { width } = themeList[theme][cur]
width *= scale
const image = `${ret}
<use x="${x}" xlink:href="#${cur}" />`
2024-10-20 02:29:58 +02:00
x += width + offset
return image
}, '')
2024-10-20 02:29:58 +02:00
// Fix the last image offset
x -= offset
const style = `
svg {
2024-10-20 02:29:58 +02:00
${pixelated === '1' ? 'image-rendering: pixelated;' : ''}
${darkmode === '1' ? 'filter: brightness(.6);' : ''}
}
${darkmode === 'auto' ? `@media (prefers-color-scheme: dark) { svg { filter: brightness(.6); } }` : ''}
`
return `<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated by https://github.com/journey-ad/Moe-Counter -->
<svg viewBox="0 0 ${x} ${y}" width="${x}" height="${y}" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Moe Counter!</title>
<style>${style}</style>
<defs>${defs}
</defs>
<g>${parts}
</g>
</svg>
`
2020-08-05 07:37:12 +02:00
}
module.exports = {
2024-10-20 02:29:58 +02:00
themeList,
getCountImage
}