iqc-maker 0.0.0-stage → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/LICENSE +25 -0
- package/README.md +641 -2
- package/index.d.ts +222 -0
- package/index.mjs +21 -0
- package/package.json +68 -4
- package/src/assets.js +179 -0
- package/src/constants.js +109 -0
- package/src/icons.js +79 -0
- package/src/index.js +130 -0
- package/src/render.js +787 -0
- package/src/themes.js +292 -0
- package/src/utils.js +60 -0
package/src/index.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { LAYOUT, LOCALES, DEFAULT_REACTIONS, DEFAULTS } = require('./constants')
|
|
4
|
+
const { getTheme, createTheme, buildTheme, registerTheme, listThemes, resolveName } = require('./themes')
|
|
5
|
+
const { defaultCacheDir, ensureFonts, ensureEmoji, loadInputImage, clearCache } = require('./assets')
|
|
6
|
+
const { paint } = require('./render')
|
|
7
|
+
const { deepMerge } = require('./utils')
|
|
8
|
+
const pkg = require('../package.json')
|
|
9
|
+
|
|
10
|
+
const FORMATS = { png: 'png', jpg: 'jpeg', jpeg: 'jpeg', webp: 'webp', avif: 'avif' }
|
|
11
|
+
|
|
12
|
+
function currentTime(timeZone) {
|
|
13
|
+
try {
|
|
14
|
+
return new Date().toLocaleTimeString('id-ID', { hour: '2-digit', minute: '2-digit', hour12: false, timeZone }).replace(':', '.')
|
|
15
|
+
} catch {
|
|
16
|
+
const d = new Date()
|
|
17
|
+
return `${String(d.getHours()).padStart(2, '0')}.${String(d.getMinutes()).padStart(2, '0')}`
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class IQC {
|
|
22
|
+
constructor(options = {}) {
|
|
23
|
+
this.options = deepMerge(DEFAULTS, { cacheDir: defaultCacheDir(), timeout: 60000, offline: false, logger: null }, options)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get version() {
|
|
27
|
+
return pkg.version
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
_assetOpts(o) {
|
|
31
|
+
const logger = o.logger === false ? null : typeof o.logger === 'function' ? o.logger : msg => process.emitWarning(msg, 'IQCMakerWarning')
|
|
32
|
+
return { cacheDir: o.cacheDir, fonts: o.fonts, emoji: o.emoji, offline: o.offline, timeout: o.timeout, logger }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async preload(options = {}) {
|
|
36
|
+
const o = deepMerge(this.options, options)
|
|
37
|
+
const a = this._assetOpts(o)
|
|
38
|
+
const [families, emojiMap] = await Promise.all([ensureFonts(a), ensureEmoji(a)])
|
|
39
|
+
return {
|
|
40
|
+
fonts: { regular: Boolean(families.regular), semibold: Boolean(families.semibold), bold: Boolean(families.bold), fallback: Boolean(families.fallback) },
|
|
41
|
+
emoji: Object.keys(emojiMap).length,
|
|
42
|
+
cacheDir: o.cacheDir,
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async render(input = {}) {
|
|
47
|
+
if (typeof input === 'string') input = { text: input }
|
|
48
|
+
const o = deepMerge(this.options, input)
|
|
49
|
+
const text = o.text == null ? '' : String(o.text)
|
|
50
|
+
if (!text.trim() && !o.image) throw new TypeError('[iqc-maker] Isi `text` atau `image` wajib diisi')
|
|
51
|
+
|
|
52
|
+
const format = FORMATS[String(o.format).toLowerCase()]
|
|
53
|
+
if (!format) throw new RangeError(`[iqc-maker] Format "${o.format}" tidak didukung (png | jpeg | webp | avif)`)
|
|
54
|
+
|
|
55
|
+
const scale = Math.max(0.25, Math.min(4, Number(o.scale) || 1))
|
|
56
|
+
const theme = getTheme(o.theme, o.colors)
|
|
57
|
+
if (o.background) theme.background = deepMerge(theme.background, typeof o.background === 'string' || Buffer.isBuffer(o.background) ? { image: o.background } : o.background)
|
|
58
|
+
|
|
59
|
+
const a = this._assetOpts(o)
|
|
60
|
+
const [families, emojiMap] = await Promise.all([ensureFonts(a), ensureEmoji(a)])
|
|
61
|
+
|
|
62
|
+
let image = null
|
|
63
|
+
if (o.image) {
|
|
64
|
+
try { image = await loadInputImage(o.image, o.timeout) }
|
|
65
|
+
catch (e) {
|
|
66
|
+
if (!text.trim()) throw new Error(`[iqc-maker] Gambar gagal dimuat: ${e.message}`)
|
|
67
|
+
if (a.logger) a.logger(`gambar gagal dimuat, lanjut teks saja: ${e.message}`)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const locale = LOCALES[String(o.locale).toLowerCase()] || LOCALES.id
|
|
72
|
+
const labels = { ...locale, ...(o.labels || {}) }
|
|
73
|
+
const reactions = Array.isArray(o.reactions) && o.reactions.length ? o.reactions : DEFAULT_REACTIONS
|
|
74
|
+
|
|
75
|
+
const canvas = await paint({
|
|
76
|
+
text,
|
|
77
|
+
image,
|
|
78
|
+
reply: o.reply && (o.reply.name || o.reply.text) ? o.reply : null,
|
|
79
|
+
time: o.time || currentTime(o.timeZone),
|
|
80
|
+
bubbleTime: o.bubbleTime,
|
|
81
|
+
network: o.network,
|
|
82
|
+
signal: o.signal,
|
|
83
|
+
battery: o.battery,
|
|
84
|
+
charging: o.charging,
|
|
85
|
+
carrier: o.carrier,
|
|
86
|
+
wifi: o.wifi,
|
|
87
|
+
batteryPercent: o.batteryPercent,
|
|
88
|
+
style: o.style,
|
|
89
|
+
statusBarStyle: o.statusBarStyle,
|
|
90
|
+
reactions,
|
|
91
|
+
statusBar: o.statusBar,
|
|
92
|
+
reactionBar: o.reactionBar,
|
|
93
|
+
menu: o.menu,
|
|
94
|
+
theme,
|
|
95
|
+
families,
|
|
96
|
+
emojiMap,
|
|
97
|
+
labels,
|
|
98
|
+
scale,
|
|
99
|
+
timeout: o.timeout,
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
if (o.output === 'canvas') return canvas
|
|
103
|
+
return format === 'png' ? canvas.encode('png') : canvas.encode(format, Math.max(1, Math.min(100, Number(o.quality) || 92)))
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const shared = new IQC()
|
|
108
|
+
|
|
109
|
+
const render = input => shared.render(input)
|
|
110
|
+
const preload = options => shared.preload(options)
|
|
111
|
+
const configure = options => { shared.options = deepMerge(shared.options, options); return shared }
|
|
112
|
+
|
|
113
|
+
module.exports = {
|
|
114
|
+
IQC,
|
|
115
|
+
render,
|
|
116
|
+
preload,
|
|
117
|
+
configure,
|
|
118
|
+
getTheme,
|
|
119
|
+
createTheme,
|
|
120
|
+
buildTheme,
|
|
121
|
+
registerTheme,
|
|
122
|
+
listThemes,
|
|
123
|
+
resolveTheme: resolveName,
|
|
124
|
+
clearCache,
|
|
125
|
+
locales: LOCALES,
|
|
126
|
+
defaultReactions: DEFAULT_REACTIONS,
|
|
127
|
+
layout: LAYOUT,
|
|
128
|
+
version: pkg.version,
|
|
129
|
+
}
|
|
130
|
+
module.exports.default = module.exports
|