2020-08-05 07:37:12 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const mimeType = require('mime-types')
|
2020-08-12 12:03:35 +02:00
|
|
|
const sizeOf = require('image-size')
|
2024-10-30 08:46:17 +01:00
|
|
|
const { toFixed } = require('./index')
|
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
|
|
|
|
|
2024-10-19 23:11:14 +02:00
|
|
|
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
|
2020-08-12 12:03:35 +02:00
|
|
|
const { width, height } = sizeOf(imgPath)
|
|
|
|
|
2024-10-25 06:53:34 +02:00
|
|
|
themeList[theme][char] = {
|
2020-08-12 12:03:35 +02:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
data: convertToDatauri(imgPath)
|
|
|
|
}
|
2020-08-05 07:37:12 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-10-19 23:11:14 +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) {
|
2024-11-02 14:44:48 +01:00
|
|
|
let { count, theme = 'moebooru', padding = 7, prefix = -1, offset = 0, align = 'top', scale = 1, pixelated = '1', darkmode = 'auto' } = params
|
2024-10-20 02:29:58 +02:00
|
|
|
|
2024-10-19 23:11:14 +02:00
|
|
|
if (!(theme in themeList)) theme = 'moebooru'
|
2024-10-31 09:53:35 +01:00
|
|
|
padding = parseInt(Number(padding), 10)
|
|
|
|
offset = parseFloat(Number(offset), 10)
|
|
|
|
scale = parseFloat(Number(scale), 10)
|
2020-08-12 12:03:35 +02:00
|
|
|
|
|
|
|
// This is not the greatest way for generating an SVG but it'll do for now
|
2024-10-19 23:11:14 +02:00
|
|
|
const countArray = count.toString().padStart(padding, '0').split('')
|
2024-11-02 14:44:48 +01:00
|
|
|
|
|
|
|
// Add prefix if exist
|
|
|
|
if (prefix >= 0) {
|
|
|
|
countArray.unshift(...String(prefix).split(''))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add _start and _end if exist
|
|
|
|
if (themeList[theme]['_start']) {
|
|
|
|
countArray.unshift('_start')
|
|
|
|
}
|
|
|
|
if (themeList[theme]['_end']) {
|
|
|
|
countArray.push('_end')
|
|
|
|
}
|
|
|
|
|
2024-10-19 23:11:14 +02:00
|
|
|
const uniqueChar = [...new Set(countArray)]
|
2020-08-12 12:03:35 +02:00
|
|
|
|
|
|
|
let x = 0, y = 0
|
|
|
|
|
2024-10-19 23:11:14 +02:00
|
|
|
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
|
2020-08-12 12:03:35 +02:00
|
|
|
|
2024-10-30 08:46:17 +01:00
|
|
|
y = Math.max(y, height)
|
2024-10-19 23:11:14 +02:00
|
|
|
|
|
|
|
ret = `${ret}
|
2024-10-30 08:46:17 +01:00
|
|
|
<image id="${cur}" width="${toFixed(width, 5)}" height="${toFixed(height, 5)}" xlink:href="${data}" />`
|
2024-10-19 23:11:14 +02:00
|
|
|
|
|
|
|
return ret
|
|
|
|
}, '')
|
2020-08-12 12:03:35 +02:00
|
|
|
|
2024-10-19 23:11:14 +02:00
|
|
|
const parts = countArray.reduce((ret, cur) => {
|
2024-10-30 08:46:17 +01:00
|
|
|
let { width, height } = themeList[theme][cur]
|
2024-10-20 02:29:58 +02:00
|
|
|
width *= scale
|
2024-10-30 08:46:17 +01:00
|
|
|
height *= scale
|
|
|
|
|
|
|
|
let yOffset = 0
|
|
|
|
|
|
|
|
if (align === 'center') {
|
|
|
|
yOffset = (y - height) / 2
|
|
|
|
} else if (align === 'bottom') {
|
|
|
|
yOffset = y - height
|
|
|
|
}
|
2024-10-19 23:11:14 +02:00
|
|
|
|
|
|
|
const image = `${ret}
|
2024-10-30 08:46:17 +01:00
|
|
|
<use x="${toFixed(x, 5)}"${yOffset ? ` y="${toFixed(yOffset, 5)}"` : ''} xlink:href="#${cur}" />`
|
2024-10-19 23:11:14 +02:00
|
|
|
|
2024-10-20 02:29:58 +02:00
|
|
|
x += width + offset
|
2020-08-12 12:03:35 +02:00
|
|
|
|
|
|
|
return image
|
|
|
|
}, '')
|
|
|
|
|
2024-10-20 02:29:58 +02:00
|
|
|
// Fix the last image offset
|
|
|
|
x -= offset
|
|
|
|
|
2024-10-19 23:11:14 +02:00
|
|
|
const style = `
|
|
|
|
svg {
|
2024-10-20 02:29:58 +02:00
|
|
|
${pixelated === '1' ? 'image-rendering: pixelated;' : ''}
|
2024-10-19 23:11:14 +02:00
|
|
|
${darkmode === '1' ? 'filter: brightness(.6);' : ''}
|
|
|
|
}
|
|
|
|
${darkmode === 'auto' ? `@media (prefers-color-scheme: dark) { svg { filter: brightness(.6); } }` : ''}
|
|
|
|
`
|
|
|
|
|
2020-08-12 12:03:35 +02:00
|
|
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
2024-10-19 23:11:14 +02:00
|
|
|
<!-- Generated by https://github.com/journey-ad/Moe-Counter -->
|
2024-10-30 08:46:17 +01:00
|
|
|
<svg viewBox="0 0 ${toFixed(x, 5)} ${toFixed(y, 5)}" width="${toFixed(x, 5)}" height="${toFixed(y, 5)}" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
2024-10-19 23:11:14 +02:00
|
|
|
<title>Moe Counter!</title>
|
|
|
|
<style>${style}</style>
|
|
|
|
<defs>${defs}
|
|
|
|
</defs>
|
|
|
|
<g>${parts}
|
|
|
|
</g>
|
2020-08-12 12:03:35 +02:00
|
|
|
</svg>
|
|
|
|
`
|
2020-08-05 07:37:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2024-10-20 02:29:58 +02:00
|
|
|
themeList,
|
2020-08-12 12:03:35 +02:00
|
|
|
getCountImage
|
2022-04-30 20:09:32 +02:00
|
|
|
}
|