iqc-maker 0.0.0-stage → 1.0.0

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/src/render.js ADDED
@@ -0,0 +1,597 @@
1
+ 'use strict'
2
+
3
+ const { createCanvas } = require('@napi-rs/canvas')
4
+ const { LAYOUT } = require('./constants')
5
+ const { drawIcon } = require('./icons')
6
+ const { getEmojiImage, loadInputImage } = require('./assets')
7
+ const { alpha } = require('./utils')
8
+
9
+ const SYSTEM_FALLBACK = '-apple-system, "Helvetica Neue", Helvetica, Arial, "Noto Sans", sans-serif'
10
+ const FAKE_WEIGHT = { regular: 0, semibold: 0.034, bold: 0.05 }
11
+ const SEGMENTER = new Intl.Segmenter(undefined, { granularity: 'grapheme' })
12
+ const EMOJI_TEST = /\p{Extended_Pictographic}|[\u{1F1E6}-\u{1F1FF}]|⃣/u
13
+ const bgCache = new Map()
14
+
15
+ class Painter {
16
+ constructor(ctx, { families, emojiMap, theme }) {
17
+ this.ctx = ctx
18
+ this.families = families
19
+ this.emojiMap = emojiMap
20
+ this.theme = theme
21
+ }
22
+
23
+ font(size, weight = 'regular') {
24
+ const f = this.families
25
+ const fam = f[weight] || f.regular || f.fallback
26
+ const list = [fam, f.regular, f.fallback].filter(Boolean).filter((v, i, a) => a.indexOf(v) === i).map(n => `"${n}"`)
27
+ return `${size}px ${list.concat(SYSTEM_FALLBACK).join(', ')}`
28
+ }
29
+
30
+ text(str, x, y, size, weight = 'regular') {
31
+ const ctx = this.ctx
32
+ ctx.font = this.font(size, weight)
33
+ ctx.fillText(str, x, y)
34
+ const w = this.families[weight] ? 0 : FAKE_WEIGHT[weight]
35
+ if (w) {
36
+ ctx.save()
37
+ ctx.strokeStyle = ctx.fillStyle
38
+ ctx.lineWidth = size * w
39
+ ctx.lineJoin = 'round'
40
+ ctx.strokeText(str, x, y)
41
+ ctx.restore()
42
+ }
43
+ }
44
+
45
+ runs(str) {
46
+ const out = []
47
+ for (const { segment } of SEGMENTER.segment(str)) {
48
+ const e = EMOJI_TEST.test(segment)
49
+ const last = out[out.length - 1]
50
+ if (!e && last && !last.e) last.t += segment
51
+ else out.push({ t: segment, e })
52
+ }
53
+ return out
54
+ }
55
+
56
+ measure(str, size, weight = 'regular') {
57
+ this.ctx.font = this.font(size, weight)
58
+ let w = 0
59
+ for (const r of this.runs(str)) w += r.e ? size * 1.05 : this.ctx.measureText(r.t).width
60
+ return w
61
+ }
62
+
63
+ async emoji(ch, cx, cy, size) {
64
+ const img = await getEmojiImage(this.emojiMap, ch)
65
+ const ctx = this.ctx
66
+ if (img) return ctx.drawImage(img, cx - size / 2, cy - size / 2, size, size)
67
+ ctx.save()
68
+ ctx.textAlign = 'center'
69
+ ctx.textBaseline = 'middle'
70
+ ctx.font = `${size * 0.85}px "Apple Color Emoji", "Noto Color Emoji", "Segoe UI Emoji", sans-serif`
71
+ ctx.fillText(ch, cx, cy)
72
+ ctx.restore()
73
+ }
74
+
75
+ async richText(str, x, cy, size, weight = 'regular') {
76
+ const ctx = this.ctx
77
+ ctx.textAlign = 'left'
78
+ ctx.textBaseline = 'alphabetic'
79
+ const base = cy + size * LAYOUT.bubble.base
80
+ let cx = x
81
+ for (const r of this.runs(str)) {
82
+ if (r.e) {
83
+ const s = size * 1.05
84
+ await this.emoji(r.t, cx + s / 2, cy, s)
85
+ cx += s
86
+ } else {
87
+ this.text(r.t, cx, base, size, weight)
88
+ cx += ctx.measureText(r.t).width
89
+ }
90
+ }
91
+ }
92
+
93
+ wrap(str, maxW, size, weight = 'regular') {
94
+ const lines = []
95
+ for (const para of String(str).split('\n')) {
96
+ let cur = ''
97
+ for (const word of para.split(' ')) {
98
+ const test = cur ? `${cur} ${word}` : word
99
+ if (!cur || this.measure(test, size, weight) <= maxW) { cur = test; continue }
100
+ lines.push(cur)
101
+ cur = word
102
+ }
103
+ lines.push(cur)
104
+ }
105
+ const out = []
106
+ for (const l of lines) {
107
+ if (this.measure(l, size, weight) <= maxW) { out.push(l); continue }
108
+ let chunk = ''
109
+ for (const { segment } of SEGMENTER.segment(l)) {
110
+ if (chunk && this.measure(chunk + segment, size, weight) > maxW) { out.push(chunk); chunk = '' }
111
+ chunk += segment
112
+ }
113
+ out.push(chunk)
114
+ }
115
+ return out
116
+ }
117
+
118
+ fit(str, maxW, size, weight = 'regular') {
119
+ if (this.measure(str, size, weight) <= maxW) return str
120
+ let out = ''
121
+ for (const { segment } of SEGMENTER.segment(str)) {
122
+ if (this.measure(`${out}${segment}…`, size, weight) > maxW) break
123
+ out += segment
124
+ }
125
+ return `${out.trimEnd()}…`
126
+ }
127
+
128
+ clamp(lines, max, maxW, size, weight = 'regular') {
129
+ if (lines.length <= max) return lines
130
+ const out = lines.slice(0, max)
131
+ out[max - 1] = this.fit(`${out[max - 1].trimEnd()}…`, maxW, size, weight)
132
+ return out
133
+ }
134
+ }
135
+
136
+ function rrPath(ctx, x, y, w, h, r) {
137
+ r = Math.max(0, Math.min(r, w / 2, h / 2))
138
+ ctx.moveTo(x + r, y)
139
+ ctx.arcTo(x + w, y, x + w, y + h, r)
140
+ ctx.arcTo(x + w, y + h, x, y + h, r)
141
+ ctx.arcTo(x, y + h, x, y, r)
142
+ ctx.arcTo(x, y, x + w, y, r)
143
+ ctx.closePath()
144
+ }
145
+
146
+ function roundRect(ctx, x, y, w, h, r) {
147
+ ctx.beginPath()
148
+ rrPath(ctx, x, y, w, h, r)
149
+ }
150
+
151
+ function blurBlob(ctx, b, color) {
152
+ ctx.save()
153
+ ctx.shadowColor = color
154
+ ctx.shadowBlur = b.blur
155
+ ctx.shadowOffsetX = 5000
156
+ ctx.fillStyle = color
157
+ roundRect(ctx, b.x - 5000, b.y, b.w, b.h, b.r)
158
+ ctx.fill()
159
+ ctx.restore()
160
+ }
161
+
162
+ async function makeBackground(theme, timeout) {
163
+ const bg = theme.background || {}
164
+ const { W, H } = LAYOUT
165
+ const key = bg.image ? null : JSON.stringify(bg)
166
+ if (key && bgCache.has(key)) return bgCache.get(key)
167
+
168
+ const c = createCanvas(W, H)
169
+ const g = c.getContext('2d')
170
+
171
+ if (bg.solid) {
172
+ g.fillStyle = bg.solid
173
+ g.fillRect(0, 0, W, H)
174
+ } else {
175
+ const stops = bg.gradient && bg.gradient.length ? bg.gradient : ['#ffffff']
176
+ const lg = g.createLinearGradient(0, 0, 0, H)
177
+ stops.forEach((s, i) => lg.addColorStop(stops.length === 1 ? 0 : i / (stops.length - 1), s))
178
+ g.fillStyle = lg
179
+ g.fillRect(0, 0, W, H)
180
+ ;(bg.blobs || []).slice(0, LAYOUT.blobs.length).forEach((color, i) => color && blurBlob(g, LAYOUT.blobs[i], color))
181
+ }
182
+
183
+ if (bg.image) {
184
+ const img = await loadInputImage(bg.image, timeout)
185
+ const s = Math.max(W / img.width, H / img.height)
186
+ const dw = img.width * s, dh = img.height * s
187
+ g.drawImage(img, (W - dw) / 2, (H - dh) / 2, dw, dh)
188
+ if (bg.dim) {
189
+ g.fillStyle = typeof bg.dim === 'string' ? bg.dim : `rgba(0,0,0,${bg.dim})`
190
+ g.fillRect(0, 0, W, H)
191
+ }
192
+ }
193
+
194
+ if (key) {
195
+ if (bgCache.size > 64) bgCache.delete(bgCache.keys().next().value)
196
+ bgCache.set(key, c)
197
+ }
198
+ return c
199
+ }
200
+
201
+ function drawWifi(ctx, cx, cy, color) {
202
+ ctx.save()
203
+ ctx.strokeStyle = color
204
+ ctx.fillStyle = color
205
+ ctx.lineWidth = 2.6
206
+ ctx.lineCap = 'round'
207
+ for (const r of [6, 11, 16]) {
208
+ ctx.beginPath()
209
+ ctx.arc(cx, cy, r, Math.PI * 1.25, Math.PI * 1.75)
210
+ ctx.stroke()
211
+ }
212
+ ctx.beginPath()
213
+ ctx.arc(cx, cy, 2.2, 0, Math.PI * 2)
214
+ ctx.fill()
215
+ ctx.restore()
216
+ }
217
+
218
+ function drawBattery(ctx, opt, t) {
219
+ const c = t.statusBar.battery
220
+ const lvl = Math.max(0, Math.min(100, Number(opt.battery) || 0))
221
+ const x = 497.4, y = 31, w = 37, h = 17
222
+ ctx.save()
223
+ ctx.fillStyle = c.body
224
+ roundRect(ctx, x, y, w, h, 5.5)
225
+ ctx.fill()
226
+ ctx.strokeStyle = c.border
227
+ ctx.lineWidth = 1.5
228
+ roundRect(ctx, x, y, w, h, 5.5)
229
+ ctx.stroke()
230
+ const fw = 32.6 * lvl / 100
231
+ if (fw > 0.5) {
232
+ ctx.fillStyle = opt.charging ? c.charging : lvl <= 20 ? c.low : c.fill
233
+ roundRect(ctx, 499.6, 33.2, fw, 13.1, Math.min(4.6, fw / 2))
234
+ ctx.fill()
235
+ }
236
+ ctx.fillStyle = c.cap
237
+ roundRect(ctx, 536.6, 36.5, 2, 6, 1)
238
+ ctx.fill()
239
+ if (opt.charging) {
240
+ ctx.translate(x + w / 2, y + h / 2)
241
+ ctx.beginPath()
242
+ ;[[1.6, -6.5], [-4, 1], [-0.6, 1], [-1.6, 6.5], [4, -1], [0.6, -1]].forEach(([px, py], i) => (i ? ctx.lineTo(px, py) : ctx.moveTo(px, py)))
243
+ ctx.closePath()
244
+ ctx.fillStyle = c.bolt
245
+ ctx.fill()
246
+ ctx.strokeStyle = c.boltStroke
247
+ ctx.lineWidth = 1
248
+ ctx.lineJoin = 'round'
249
+ ctx.stroke()
250
+ }
251
+ ctx.restore()
252
+ }
253
+
254
+ function drawStatusBar(P, opt) {
255
+ const { ctx, theme: t } = P
256
+ const s = LAYOUT.status
257
+ const bars = Math.max(0, Math.min(4, Math.round(Number(opt.signal) || 0)))
258
+ ctx.save()
259
+ ctx.fillStyle = t.statusBar.text
260
+ ctx.textBaseline = 'middle'
261
+ ctx.textAlign = 'left'
262
+ P.text(String(opt.time), s.x, s.y, 24, 'semibold')
263
+ ;[[419.6, 6.6], [427.9, 9.8], [436.2, 14], [444.5, 18]].forEach(([bx, bh], i) => {
264
+ ctx.fillStyle = i < bars ? t.statusBar.text : t.statusBar.inactive
265
+ roundRect(ctx, bx, 48.4 - bh, 4.8, bh, 1.3)
266
+ ctx.fill()
267
+ })
268
+ ctx.fillStyle = t.statusBar.text
269
+ const net = String(opt.network || '')
270
+ if (net.toLowerCase() === 'wifi') drawWifi(ctx, 472, 48, t.statusBar.text)
271
+ else if (net) {
272
+ ctx.textBaseline = 'alphabetic'
273
+ P.text(net, 458.3, 47.6, net.length > 2 ? 17 : 19, 'semibold')
274
+ }
275
+ ctx.restore()
276
+ drawBattery(ctx, opt, t)
277
+ }
278
+
279
+ async function drawEmojiBar(P, opt) {
280
+ const { ctx, theme: t } = P
281
+ const p = LAYOUT.pill
282
+ ctx.save()
283
+ ctx.shadowColor = t.pill.shadow
284
+ ctx.shadowBlur = 16
285
+ ctx.shadowOffsetY = 4
286
+ ctx.fillStyle = t.pill.underlay
287
+ roundRect(ctx, p.x, p.y + 2.5, p.w, p.h, p.r)
288
+ ctx.fill()
289
+ ctx.restore()
290
+
291
+ ctx.save()
292
+ ctx.fillStyle = t.pill.bg
293
+ roundRect(ctx, p.x, p.y, p.w, p.h, p.r)
294
+ ctx.fill()
295
+ ctx.restore()
296
+
297
+ const list = (opt.reactions || []).slice(0, p.xs.length)
298
+ for (let i = 0; i < list.length; i++) await P.emoji(list[i], p.xs[i], p.cy, p.size)
299
+
300
+ const { cx, cy, r } = p.plus
301
+ ctx.save()
302
+ roundRect(ctx, p.x, p.y, p.w, p.h, p.r)
303
+ ctx.clip()
304
+ const hg = ctx.createRadialGradient(cx, cy, r - 2, cx, cy, r + 20)
305
+ hg.addColorStop(0, alpha(t.pill.bg, 0.95))
306
+ hg.addColorStop(0.5, alpha(t.pill.bg, 0.7))
307
+ hg.addColorStop(1, alpha(t.pill.bg, 0))
308
+ ctx.fillStyle = hg
309
+ ctx.beginPath()
310
+ ctx.arc(cx, cy, r + 20, 0, Math.PI * 2)
311
+ ctx.fill()
312
+ ctx.restore()
313
+
314
+ ctx.save()
315
+ ctx.fillStyle = t.pill.plusBg
316
+ ctx.beginPath()
317
+ ctx.arc(cx, cy, r, 0, Math.PI * 2)
318
+ ctx.fill()
319
+ ctx.strokeStyle = t.pill.plusIcon
320
+ ctx.lineWidth = 3.4
321
+ ctx.lineCap = 'round'
322
+ ctx.beginPath()
323
+ ctx.moveTo(cx - 11, cy)
324
+ ctx.lineTo(cx + 11, cy)
325
+ ctx.moveTo(cx, cy - 11)
326
+ ctx.lineTo(cx, cy + 11)
327
+ ctx.stroke()
328
+ ctx.restore()
329
+ }
330
+
331
+ function layoutBubble(P, img, opt) {
332
+ const b = LAYOUT.bubble, R = b.reply
333
+ const ts = String(opt.bubbleTime || opt.time)
334
+ const text = String(opt.text || '').trim()
335
+ P.ctx.font = P.font(b.tsSize)
336
+ const tsW = P.ctx.measureText(ts).width
337
+ const lw = l => P.measure(l, b.fontSize)
338
+ const iw = img ? Math.min(Math.max(img.width, b.imgMinW), b.imgMaxW) : 0
339
+ const maxW = img ? iw + 8 : b.maxW
340
+ const inner = maxW - b.padL - b.padR
341
+
342
+ const reply = !img && opt.reply && (opt.reply.name || opt.reply.text)
343
+ ? { name: String(opt.reply.name || '').trim(), text: String(opt.reply.text || '').replace(/\s+/g, ' ').trim() }
344
+ : null
345
+
346
+ let repW = 0
347
+ if (reply) {
348
+ const pad = R.m * 2 + R.padL + R.padR
349
+ const nW = reply.name ? P.measure(reply.name, R.nameSize, 'semibold') : 0
350
+ const tW = reply.text ? P.measure(reply.text, R.textSize) : 0
351
+ repW = Math.min(Math.max(nW, tW), maxW - pad) + pad
352
+ }
353
+
354
+ const reserve = img
355
+ ? b.imgMinH + 8
356
+ : reply ? R.m + R.padT + (reply.name ? R.nameH : 0) + R.maxLines * R.lineH + R.padB + R.gap : 0
357
+ const maxLines = Math.max(1, Math.floor((b.maxH - reserve - b.padT - b.padB - b.tsRow) / b.lineH))
358
+ const lines = text ? P.clamp(P.wrap(text, inner, b.fontSize), maxLines, inner, b.fontSize) : []
359
+
360
+ const textW = lines.length ? Math.max(...lines.map(lw)) : 0
361
+ const lastW = lines.length ? lw(lines[lines.length - 1]) : 0
362
+ const need = lastW + b.tsGap + tsW <= inner ? Math.max(textW, lastW + b.tsGap + tsW) : Math.max(textW, tsW)
363
+ const w = img ? maxW : Math.min(maxW, Math.max(Math.ceil(need) + b.padL + b.padR, Math.ceil(repW), b.minW))
364
+ const inline = lines.length > 0 && lastW + b.tsGap + tsW <= w - b.padL - b.padR
365
+
366
+ let rep = null
367
+ if (reply) {
368
+ const ri = w - R.m * 2 - R.padL - R.padR
369
+ const name = reply.name ? P.fit(reply.name, ri, R.nameSize, 'semibold') : ''
370
+ const rl = reply.text ? P.clamp(P.wrap(reply.text, ri, R.textSize), R.maxLines, ri, R.textSize) : []
371
+ rep = { name, lines: rl, color: opt.reply.color, h: R.padT + (name ? R.nameH : 0) + rl.length * R.lineH + R.padB }
372
+ }
373
+
374
+ const textH = lines.length
375
+ ? b.padT + lines.length * b.lineH + (inline ? 0 : b.tsRow) + b.padB
376
+ : img ? 0 : b.padT + b.tsRow + b.padB
377
+
378
+ let ih = 0, head = 0
379
+ if (img) {
380
+ ih = Math.max(60, Math.min(Math.round(iw * img.height / img.width), b.maxH - textH - 8))
381
+ head = 4 + ih
382
+ } else if (rep) head = R.m + rep.h + R.gap
383
+
384
+ return { img, iw, ih, lines, w, h: head + (textH || 4), head, rep, ts, tsW, inline }
385
+ }
386
+
387
+ async function drawBubble(P, L, by) {
388
+ const { ctx, theme: t } = P
389
+ const b = LAYOUT.bubble, R = b.reply
390
+
391
+ ctx.save()
392
+ ctx.shadowColor = t.bubble.shadow
393
+ ctx.shadowBlur = 8
394
+ ctx.shadowOffsetY = 2
395
+ ctx.fillStyle = t.bubble.bg
396
+ roundRect(ctx, b.x, by, L.w, L.h, b.r)
397
+ ctx.fill()
398
+ ctx.restore()
399
+
400
+ ctx.save()
401
+ ctx.fillStyle = t.bubble.bg
402
+ ctx.beginPath()
403
+ ctx.moveTo(b.x + 12, by + L.h - 20)
404
+ ctx.quadraticCurveTo(b.x - 2, by + L.h - 4, b.x - 8, by + L.h)
405
+ ctx.quadraticCurveTo(b.x + 6, by + L.h, b.x + 22, by + L.h - 2)
406
+ ctx.closePath()
407
+ ctx.fill()
408
+ ctx.restore()
409
+
410
+ if (L.img) {
411
+ const { img, iw, ih } = L
412
+ const sc = Math.max(iw / img.width, ih / img.height)
413
+ const sw = iw / sc, sh = ih / sc
414
+ ctx.save()
415
+ roundRect(ctx, b.x + 4, by + 4, iw, ih, 16)
416
+ ctx.clip()
417
+ ctx.drawImage(img, (img.width - sw) / 2, (img.height - sh) / 2, sw, sh, b.x + 4, by + 4, iw, ih)
418
+ ctx.restore()
419
+ if (!L.lines.length) {
420
+ const pw = L.tsW + 18, ph = 24
421
+ const px = b.x + 4 + iw - 8 - pw, py = by + 4 + ih - 8 - ph
422
+ ctx.save()
423
+ ctx.fillStyle = t.bubble.mediaTimeBg
424
+ roundRect(ctx, px, py, pw, ph, 12)
425
+ ctx.fill()
426
+ ctx.fillStyle = t.bubble.mediaTimeText
427
+ ctx.textAlign = 'center'
428
+ ctx.textBaseline = 'middle'
429
+ P.text(L.ts, px + pw / 2, py + ph / 2 + 1, b.tsSize)
430
+ ctx.restore()
431
+ return
432
+ }
433
+ }
434
+
435
+ if (L.rep) {
436
+ const rx = b.x + R.m, ry = by + R.m, rw = L.w - R.m * 2
437
+ const bar = L.rep.color || t.reply.bar
438
+ ctx.save()
439
+ roundRect(ctx, rx, ry, rw, L.rep.h, R.r)
440
+ ctx.clip()
441
+ ctx.fillStyle = t.reply.bg
442
+ ctx.fillRect(rx, ry, rw, L.rep.h)
443
+ ctx.fillStyle = bar
444
+ ctx.fillRect(rx, ry, R.barW, L.rep.h)
445
+ let cy = ry + R.padT
446
+ if (L.rep.name) {
447
+ ctx.fillStyle = L.rep.color || t.reply.name
448
+ await P.richText(L.rep.name, rx + R.padL, cy + R.nameH / 2, R.nameSize, 'semibold')
449
+ cy += R.nameH
450
+ }
451
+ ctx.fillStyle = t.reply.text
452
+ for (const l of L.rep.lines) {
453
+ await P.richText(l, rx + R.padL, cy + R.lineH / 2, R.textSize)
454
+ cy += R.lineH
455
+ }
456
+ ctx.restore()
457
+ }
458
+
459
+ const top = by + L.head + b.padT
460
+ let lastBase = top
461
+ ctx.save()
462
+ ctx.fillStyle = t.bubble.text
463
+ for (let i = 0; i < L.lines.length; i++) {
464
+ const cy = top + b.lineH / 2 + i * b.lineH
465
+ await P.richText(L.lines[i], b.x + b.padL, cy, b.fontSize)
466
+ lastBase = cy + b.fontSize * b.base
467
+ }
468
+ ctx.restore()
469
+
470
+ const tsBase = L.inline ? lastBase + b.tsLift : top + L.lines.length * b.lineH + b.tsRow
471
+ ctx.save()
472
+ ctx.fillStyle = t.bubble.time
473
+ ctx.textAlign = 'right'
474
+ ctx.textBaseline = 'alphabetic'
475
+ P.text(L.ts, b.x + L.w - b.padR, tsBase, b.tsSize)
476
+ ctx.restore()
477
+ }
478
+
479
+ function drawGlassPanel(P, bg) {
480
+ const { ctx, theme: t } = P
481
+ const m = LAYOUT.menu, c = t.menu
482
+ const rr = () => roundRect(ctx, m.x, m.y, m.w, m.h, m.r)
483
+
484
+ ctx.save()
485
+ ctx.beginPath()
486
+ ctx.rect(0, 0, LAYOUT.W, LAYOUT.H)
487
+ rrPath(ctx, m.x, m.y, m.w, m.h, m.r)
488
+ ctx.clip('evenodd')
489
+ ctx.shadowColor = c.shadow
490
+ ctx.shadowBlur = 26
491
+ ctx.shadowOffsetY = 6
492
+ ctx.fillStyle = '#fff'
493
+ rr()
494
+ ctx.fill()
495
+ ctx.restore()
496
+
497
+ ctx.save()
498
+ rr()
499
+ ctx.clip()
500
+ ctx.drawImage(bg, 0, 0)
501
+ ctx.fillStyle = c.overlay
502
+ ctx.fillRect(m.x, m.y, m.w, m.h)
503
+ const tint = ctx.createLinearGradient(0, m.y, 0, m.y + m.h)
504
+ tint.addColorStop(0, c.tintTop)
505
+ tint.addColorStop(1, c.tintBottom)
506
+ ctx.fillStyle = tint
507
+ ctx.fillRect(m.x, m.y, m.w, m.h)
508
+ const sheen = ctx.createLinearGradient(0, m.y, 0, m.y + m.h)
509
+ sheen.addColorStop(0, c.sheen)
510
+ sheen.addColorStop(1, alpha(c.sheen, 0))
511
+ ctx.fillStyle = sheen
512
+ ctx.fillRect(m.x, m.y, m.w, m.h)
513
+ ctx.save()
514
+ ctx.translate(m.x + 18, m.y + 199)
515
+ ctx.scale(360, 220)
516
+ const glow = ctx.createRadialGradient(0, 0, 0, 0, 0, 1)
517
+ glow.addColorStop(0, c.glow)
518
+ glow.addColorStop(1, alpha(c.glow, 0))
519
+ ctx.fillStyle = glow
520
+ ctx.fillRect(-2, -2, 4, 4)
521
+ ctx.restore()
522
+ const spec = ctx.createLinearGradient(0, m.y, 0, m.y + 40)
523
+ spec.addColorStop(0, c.spec)
524
+ spec.addColorStop(1, alpha(c.spec, 0))
525
+ ctx.fillStyle = spec
526
+ ctx.fillRect(m.x, m.y, m.w, 40)
527
+ ctx.restore()
528
+
529
+ ctx.save()
530
+ const rim = ctx.createLinearGradient(0, m.y, 0, m.y + m.h)
531
+ rim.addColorStop(0, c.rimTop)
532
+ rim.addColorStop(1, c.rimBottom)
533
+ ctx.strokeStyle = rim
534
+ ctx.lineWidth = 1.2
535
+ roundRect(ctx, m.x + 0.6, m.y + 0.6, m.w - 1.2, m.h - 1.2, m.r)
536
+ ctx.stroke()
537
+ ctx.restore()
538
+ }
539
+
540
+ function drawMenu(P, bg, labels) {
541
+ const { ctx, theme: t } = P
542
+ const m = LAYOUT.menu
543
+ drawGlassPanel(P, bg)
544
+ for (const row of m.rows) {
545
+ if (row.divider) {
546
+ ctx.save()
547
+ ctx.strokeStyle = t.menu.divider
548
+ ctx.lineWidth = 1
549
+ ctx.beginPath()
550
+ ctx.moveTo(row.x1, row.divider)
551
+ ctx.lineTo(row.x2, row.divider)
552
+ ctx.stroke()
553
+ ctx.restore()
554
+ continue
555
+ }
556
+ const textColor = row.danger ? t.menu.danger : t.menu.text
557
+ const iconColor = row.danger ? t.menu.danger : t.menu.icon
558
+ drawIcon(ctx, row.icon, m.iconCx, row.y, iconColor)
559
+ ctx.save()
560
+ ctx.fillStyle = textColor
561
+ ctx.textAlign = 'left'
562
+ ctx.textBaseline = 'middle'
563
+ const label = P.fit(String(labels[row.key] ?? ''), m.w - (m.labelX - m.x) - 24, m.fontSize, 'semibold')
564
+ P.text(label, m.labelX, row.y, m.fontSize, 'semibold')
565
+ ctx.restore()
566
+ }
567
+ }
568
+
569
+ async function paint(opt) {
570
+ const { theme, families, emojiMap, labels, image, scale, timeout } = opt
571
+ const { W, H } = LAYOUT
572
+ const bg = await makeBackground(theme, timeout)
573
+
574
+ const canvas = createCanvas(Math.round(W * scale), Math.round(H * scale))
575
+ const ctx = canvas.getContext('2d')
576
+ ctx.scale(scale, scale)
577
+ const P = new Painter(ctx, { families, emojiMap, theme })
578
+
579
+ ctx.drawImage(bg, 0, 0)
580
+ if (opt.statusBar !== false) drawStatusBar(P, opt)
581
+
582
+ const L = layoutBubble(P, image, opt)
583
+ const by = LAYOUT.bubble.anchor - L.h
584
+ const p = LAYOUT.pill
585
+ if (opt.reactionBar !== false) {
586
+ const pillTop = Math.min(p.y, by - p.gap - p.h)
587
+ ctx.save()
588
+ ctx.translate(0, pillTop - p.y)
589
+ await drawEmojiBar(P, opt)
590
+ ctx.restore()
591
+ }
592
+ await drawBubble(P, L, by)
593
+ if (opt.menu !== false) drawMenu(P, bg, labels)
594
+ return canvas
595
+ }
596
+
597
+ module.exports = { paint }