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/src/render.js ADDED
@@ -0,0 +1,787 @@
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.chat && !bg.image) drawFakeChat(g, bg.chat)
184
+
185
+ if (bg.image) {
186
+ const img = await loadInputImage(bg.image, timeout)
187
+ const s = Math.max(W / img.width, H / img.height)
188
+ const dw = img.width * s, dh = img.height * s
189
+ g.drawImage(img, (W - dw) / 2, (H - dh) / 2, dw, dh)
190
+ if (bg.dim) {
191
+ g.fillStyle = typeof bg.dim === 'string' ? bg.dim : `rgba(0,0,0,${bg.dim})`
192
+ g.fillRect(0, 0, W, H)
193
+ }
194
+ }
195
+
196
+ if (key) {
197
+ if (bgCache.size > 64) bgCache.delete(bgCache.keys().next().value)
198
+ bgCache.set(key, c)
199
+ }
200
+ return c
201
+ }
202
+
203
+ function drawFakeChat(g, chat) {
204
+ const { W, H } = LAYOUT
205
+ const shapes = [
206
+ [chat.header, 0, 0, W, 132, 0],
207
+ [chat.avatar, 98, 58, 46, 46, 23],
208
+ [chat.avatar, 158, 70, 120, 18, 9],
209
+ [chat.accent, 476, 66, 34, 22, 11],
210
+ [chat.accent, 532, 66, 34, 22, 11],
211
+ ...LAYOUT.native.chat.map(([type, x, y, w, h]) => [chat[type], x, y, w, h, 20]),
212
+ [chat.header, 0, H - 92, W, 92, 0],
213
+ [chat.avatar, 70, H - 70, 400, 40, 20],
214
+ ]
215
+ g.save()
216
+ g.filter = `blur(${chat.blur == null ? 16 : chat.blur}px)`
217
+ for (const [color, x, y, w, h, r] of shapes) {
218
+ if (!color) continue
219
+ g.fillStyle = color
220
+ roundRect(g, x, y, w, h, r)
221
+ g.fill()
222
+ }
223
+ g.restore()
224
+ if (chat.overlay) {
225
+ g.fillStyle = chat.overlay
226
+ g.fillRect(0, 0, W, H)
227
+ }
228
+ }
229
+
230
+ function drawWifiClassic(ctx, cx, cy, color) {
231
+ const a0 = Math.PI * 1.25, a1 = Math.PI * 1.75
232
+ ctx.save()
233
+ ctx.fillStyle = color
234
+ ctx.strokeStyle = color
235
+ ctx.beginPath()
236
+ ctx.moveTo(cx, cy)
237
+ ctx.arc(cx, cy, 4.6, a0, a1)
238
+ ctx.closePath()
239
+ ctx.fill()
240
+ ctx.lineWidth = 2.6
241
+ ctx.lineCap = 'butt'
242
+ for (const r of [8.4, 12.8]) {
243
+ ctx.beginPath()
244
+ ctx.arc(cx, cy, r, a0 + 0.04, a1 - 0.04)
245
+ ctx.stroke()
246
+ }
247
+ ctx.restore()
248
+ }
249
+
250
+ function drawStatusBarClassic(P, opt) {
251
+ const { ctx, theme: t } = P
252
+ const sb = t.statusBar
253
+ const cy = 31
254
+ const bars = Math.max(0, Math.min(4, Math.round(Number(opt.signal) || 0)))
255
+ const lvl = Math.max(0, Math.min(100, Math.round(Number(opt.battery) || 0)))
256
+ const net = String(opt.network || '')
257
+ const isWifi = net.toLowerCase() === 'wifi'
258
+
259
+ ctx.save()
260
+ ;[5, 7.5, 10, 12.5].forEach((h, i) => {
261
+ ctx.fillStyle = i < bars ? sb.text : sb.inactive
262
+ roundRect(ctx, 18 + i * 6.2, cy + 6 - h, 4.4, h, 1.2)
263
+ ctx.fill()
264
+ })
265
+
266
+ ctx.fillStyle = sb.text
267
+ ctx.textBaseline = 'middle'
268
+ ctx.textAlign = 'left'
269
+ const carrier = [opt.carrier, isWifi ? '' : net].filter(Boolean).join(' ')
270
+ if (carrier) P.text(carrier.toUpperCase(), 50, cy + 1, 15.5, 'bold')
271
+
272
+ ctx.textAlign = 'center'
273
+ P.text(String(opt.time), LAYOUT.W / 2, cy, 18.5, 'bold')
274
+
275
+ const bx = 541, bw = 31, bh = 15, by = cy - bh / 2
276
+ const c = sb.battery
277
+ ctx.strokeStyle = c.border
278
+ ctx.lineWidth = 1.3
279
+ roundRect(ctx, bx, by, bw, bh, 4)
280
+ ctx.stroke()
281
+ const fw = (bw - 4) * lvl / 100
282
+ if (fw > 0.5) {
283
+ ctx.fillStyle = opt.charging ? c.charging : lvl <= 20 ? c.low : c.fill
284
+ roundRect(ctx, bx + 2, by + 2, fw, bh - 4, 2.4)
285
+ ctx.fill()
286
+ }
287
+ ctx.fillStyle = c.cap
288
+ roundRect(ctx, bx + bw + 1.5, cy - 3, 2, 6, 1)
289
+ ctx.fill()
290
+
291
+ let right = bx - 8
292
+ if (opt.batteryPercent !== false) {
293
+ ctx.fillStyle = sb.text
294
+ ctx.textAlign = 'right'
295
+ const pct = `${lvl}%`
296
+ P.text(pct, right, cy + 1, 15.5, 'bold')
297
+ ctx.font = P.font(15.5, 'bold')
298
+ right -= ctx.measureText(pct).width + 10
299
+ }
300
+ if (isWifi || opt.wifi) drawWifiClassic(ctx, right - 9, cy + 6, sb.text)
301
+ ctx.restore()
302
+ }
303
+
304
+ function drawWifi(ctx, cx, cy, color) {
305
+ ctx.save()
306
+ ctx.strokeStyle = color
307
+ ctx.fillStyle = color
308
+ ctx.lineWidth = 2.6
309
+ ctx.lineCap = 'round'
310
+ for (const r of [6, 11, 16]) {
311
+ ctx.beginPath()
312
+ ctx.arc(cx, cy, r, Math.PI * 1.25, Math.PI * 1.75)
313
+ ctx.stroke()
314
+ }
315
+ ctx.beginPath()
316
+ ctx.arc(cx, cy, 2.2, 0, Math.PI * 2)
317
+ ctx.fill()
318
+ ctx.restore()
319
+ }
320
+
321
+ function drawBattery(ctx, opt, t) {
322
+ const c = t.statusBar.battery
323
+ const lvl = Math.max(0, Math.min(100, Number(opt.battery) || 0))
324
+ const x = 497.4, y = 31, w = 37, h = 17
325
+ ctx.save()
326
+ ctx.fillStyle = c.body
327
+ roundRect(ctx, x, y, w, h, 5.5)
328
+ ctx.fill()
329
+ ctx.strokeStyle = c.border
330
+ ctx.lineWidth = 1.5
331
+ roundRect(ctx, x, y, w, h, 5.5)
332
+ ctx.stroke()
333
+ const fw = 32.6 * lvl / 100
334
+ if (fw > 0.5) {
335
+ ctx.fillStyle = opt.charging ? c.charging : lvl <= 20 ? c.low : c.fill
336
+ roundRect(ctx, 499.6, 33.2, fw, 13.1, Math.min(4.6, fw / 2))
337
+ ctx.fill()
338
+ }
339
+ ctx.fillStyle = c.cap
340
+ roundRect(ctx, 536.6, 36.5, 2, 6, 1)
341
+ ctx.fill()
342
+ if (opt.charging) {
343
+ ctx.translate(x + w / 2, y + h / 2)
344
+ ctx.beginPath()
345
+ ;[[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)))
346
+ ctx.closePath()
347
+ ctx.fillStyle = c.bolt
348
+ ctx.fill()
349
+ ctx.strokeStyle = c.boltStroke
350
+ ctx.lineWidth = 1
351
+ ctx.lineJoin = 'round'
352
+ ctx.stroke()
353
+ }
354
+ ctx.restore()
355
+ }
356
+
357
+ function drawStatusBar(P, opt) {
358
+ const { ctx, theme: t } = P
359
+ if ((opt.statusBarStyle || t.statusBar.style) === 'classic') return drawStatusBarClassic(P, opt)
360
+ const s = LAYOUT.status
361
+ const bars = Math.max(0, Math.min(4, Math.round(Number(opt.signal) || 0)))
362
+ ctx.save()
363
+ ctx.fillStyle = t.statusBar.text
364
+ ctx.textBaseline = 'middle'
365
+ ctx.textAlign = 'left'
366
+ P.text(String(opt.time), s.x, s.y, 24, 'semibold')
367
+ ;[[419.6, 6.6], [427.9, 9.8], [436.2, 14], [444.5, 18]].forEach(([bx, bh], i) => {
368
+ ctx.fillStyle = i < bars ? t.statusBar.text : t.statusBar.inactive
369
+ roundRect(ctx, bx, 48.4 - bh, 4.8, bh, 1.3)
370
+ ctx.fill()
371
+ })
372
+ ctx.fillStyle = t.statusBar.text
373
+ const net = String(opt.network || '')
374
+ if (net.toLowerCase() === 'wifi') drawWifi(ctx, 472, 48, t.statusBar.text)
375
+ else if (net) {
376
+ ctx.textBaseline = 'alphabetic'
377
+ P.text(net, 458.3, 47.6, net.length > 2 ? 17 : 19, 'semibold')
378
+ }
379
+ ctx.restore()
380
+ drawBattery(ctx, opt, t)
381
+ }
382
+
383
+ async function drawEmojiBar(P, opt) {
384
+ const { ctx, theme: t } = P
385
+ const p = LAYOUT.pill
386
+ ctx.save()
387
+ ctx.shadowColor = t.pill.shadow
388
+ ctx.shadowBlur = 16
389
+ ctx.shadowOffsetY = 4
390
+ ctx.fillStyle = t.pill.underlay || t.pill.bg
391
+ roundRect(ctx, p.x, p.y + (t.pill.underlay ? 2.5 : 0), p.w, p.h, p.r)
392
+ ctx.fill()
393
+ ctx.restore()
394
+
395
+ ctx.save()
396
+ ctx.fillStyle = t.pill.bg
397
+ roundRect(ctx, p.x, p.y, p.w, p.h, p.r)
398
+ ctx.fill()
399
+ if (t.pill.border) {
400
+ ctx.strokeStyle = t.pill.border
401
+ ctx.lineWidth = 1
402
+ roundRect(ctx, p.x + 0.5, p.y + 0.5, p.w - 1, p.h - 1, p.r)
403
+ ctx.stroke()
404
+ }
405
+ ctx.restore()
406
+
407
+ const native = opt.resolvedStyle === 'native'
408
+ const list = (opt.reactions || []).slice(0, p.xs.length)
409
+ for (let i = 0; i < list.length; i++) {
410
+ if (native) await P.emoji(list[i], 53 + i * 55.5, p.cy, 40)
411
+ else await P.emoji(list[i], p.xs[i], p.cy, p.size)
412
+ }
413
+
414
+ const { cx, cy, r } = p.plus
415
+ ctx.save()
416
+ roundRect(ctx, p.x, p.y, p.w, p.h, p.r)
417
+ ctx.clip()
418
+ const hg = ctx.createRadialGradient(cx, cy, r - 2, cx, cy, r + 20)
419
+ hg.addColorStop(0, alpha(t.pill.bg, 0.95))
420
+ hg.addColorStop(0.5, alpha(t.pill.bg, 0.7))
421
+ hg.addColorStop(1, alpha(t.pill.bg, 0))
422
+ ctx.fillStyle = hg
423
+ ctx.beginPath()
424
+ ctx.arc(cx, cy, r + 20, 0, Math.PI * 2)
425
+ ctx.fill()
426
+ ctx.restore()
427
+
428
+ ctx.save()
429
+ ctx.fillStyle = t.pill.plusBg
430
+ ctx.beginPath()
431
+ ctx.arc(cx, cy, r, 0, Math.PI * 2)
432
+ ctx.fill()
433
+ ctx.strokeStyle = t.pill.plusIcon
434
+ ctx.lineWidth = 3.4
435
+ ctx.lineCap = 'round'
436
+ ctx.beginPath()
437
+ ctx.moveTo(cx - 11, cy)
438
+ ctx.lineTo(cx + 11, cy)
439
+ ctx.moveTo(cx, cy - 11)
440
+ ctx.lineTo(cx, cy + 11)
441
+ ctx.stroke()
442
+ ctx.restore()
443
+ }
444
+
445
+ function layoutBubble(P, img, opt) {
446
+ const b = LAYOUT.bubble, R = b.reply
447
+ const ts = String(opt.bubbleTime || opt.time)
448
+ const text = String(opt.text || '').trim()
449
+ P.ctx.font = P.font(b.tsSize)
450
+ const tsW = P.ctx.measureText(ts).width
451
+ const lw = l => P.measure(l, b.fontSize)
452
+ const iw = img ? Math.min(Math.max(img.width, b.imgMinW), b.imgMaxW) : 0
453
+ const maxW = img ? iw + 8 : b.maxW
454
+ const inner = maxW - b.padL - b.padR
455
+
456
+ const reply = !img && opt.reply && (opt.reply.name || opt.reply.text)
457
+ ? { name: String(opt.reply.name || '').trim(), text: String(opt.reply.text || '').replace(/\s+/g, ' ').trim() }
458
+ : null
459
+
460
+ let repW = 0
461
+ if (reply) {
462
+ const pad = R.m * 2 + R.padL + R.padR
463
+ const nW = reply.name ? P.measure(reply.name, R.nameSize, 'semibold') : 0
464
+ const tW = reply.text ? P.measure(reply.text, R.textSize) : 0
465
+ repW = Math.min(Math.max(nW, tW), maxW - pad) + pad
466
+ }
467
+
468
+ const reserve = img
469
+ ? b.imgMinH + 8
470
+ : reply ? R.m + R.padT + (reply.name ? R.nameH : 0) + R.maxLines * R.lineH + R.padB + R.gap : 0
471
+ const maxLines = Math.max(1, Math.floor((b.maxH - reserve - b.padT - b.padB - b.tsRow) / b.lineH))
472
+ const lines = text ? P.clamp(P.wrap(text, inner, b.fontSize), maxLines, inner, b.fontSize) : []
473
+
474
+ const textW = lines.length ? Math.max(...lines.map(lw)) : 0
475
+ const lastW = lines.length ? lw(lines[lines.length - 1]) : 0
476
+ const need = lastW + b.tsGap + tsW <= inner ? Math.max(textW, lastW + b.tsGap + tsW) : Math.max(textW, tsW)
477
+ const w = img ? maxW : Math.min(maxW, Math.max(Math.ceil(need) + b.padL + b.padR, Math.ceil(repW), b.minW))
478
+ const inline = lines.length > 0 && lastW + b.tsGap + tsW <= w - b.padL - b.padR
479
+
480
+ let rep = null
481
+ if (reply) {
482
+ const ri = w - R.m * 2 - R.padL - R.padR
483
+ const name = reply.name ? P.fit(reply.name, ri, R.nameSize, 'semibold') : ''
484
+ const rl = reply.text ? P.clamp(P.wrap(reply.text, ri, R.textSize), R.maxLines, ri, R.textSize) : []
485
+ rep = { name, lines: rl, color: opt.reply.color, h: R.padT + (name ? R.nameH : 0) + rl.length * R.lineH + R.padB }
486
+ }
487
+
488
+ const textH = lines.length
489
+ ? b.padT + lines.length * b.lineH + (inline ? 0 : b.tsRow) + b.padB
490
+ : img ? 0 : b.padT + b.tsRow + b.padB
491
+
492
+ let ih = 0, head = 0
493
+ if (img) {
494
+ ih = Math.max(60, Math.min(Math.round(iw * img.height / img.width), b.maxH - textH - 8))
495
+ head = 4 + ih
496
+ } else if (rep) head = R.m + rep.h + R.gap
497
+
498
+ return { img, iw, ih, lines, w, h: head + (textH || 4), head, rep, ts, tsW, inline }
499
+ }
500
+
501
+ async function drawBubble(P, L, by) {
502
+ const { ctx, theme: t } = P
503
+ const b = LAYOUT.bubble, R = b.reply
504
+
505
+ ctx.save()
506
+ ctx.shadowColor = t.bubble.shadow
507
+ ctx.shadowBlur = 8
508
+ ctx.shadowOffsetY = 2
509
+ ctx.fillStyle = t.bubble.bg
510
+ roundRect(ctx, b.x, by, L.w, L.h, b.r)
511
+ ctx.fill()
512
+ ctx.restore()
513
+
514
+ if (t.bubble.tail !== false) {
515
+ ctx.save()
516
+ ctx.fillStyle = t.bubble.bg
517
+ ctx.beginPath()
518
+ ctx.moveTo(b.x + 12, by + L.h - 20)
519
+ ctx.quadraticCurveTo(b.x - 2, by + L.h - 4, b.x - 8, by + L.h)
520
+ ctx.quadraticCurveTo(b.x + 6, by + L.h, b.x + 22, by + L.h - 2)
521
+ ctx.closePath()
522
+ ctx.fill()
523
+ ctx.restore()
524
+ }
525
+
526
+ if (L.img) {
527
+ const { img, iw, ih } = L
528
+ const sc = Math.max(iw / img.width, ih / img.height)
529
+ const sw = iw / sc, sh = ih / sc
530
+ ctx.save()
531
+ roundRect(ctx, b.x + 4, by + 4, iw, ih, 16)
532
+ ctx.clip()
533
+ ctx.drawImage(img, (img.width - sw) / 2, (img.height - sh) / 2, sw, sh, b.x + 4, by + 4, iw, ih)
534
+ ctx.restore()
535
+ if (!L.lines.length) {
536
+ const pw = L.tsW + 18, ph = 24
537
+ const px = b.x + 4 + iw - 8 - pw, py = by + 4 + ih - 8 - ph
538
+ ctx.save()
539
+ ctx.fillStyle = t.bubble.mediaTimeBg
540
+ roundRect(ctx, px, py, pw, ph, 12)
541
+ ctx.fill()
542
+ ctx.fillStyle = t.bubble.mediaTimeText
543
+ ctx.textAlign = 'center'
544
+ ctx.textBaseline = 'middle'
545
+ P.text(L.ts, px + pw / 2, py + ph / 2 + 1, b.tsSize)
546
+ ctx.restore()
547
+ return
548
+ }
549
+ }
550
+
551
+ if (L.rep) {
552
+ const rx = b.x + R.m, ry = by + R.m, rw = L.w - R.m * 2
553
+ const bar = L.rep.color || t.reply.bar
554
+ ctx.save()
555
+ roundRect(ctx, rx, ry, rw, L.rep.h, R.r)
556
+ ctx.clip()
557
+ ctx.fillStyle = t.reply.bg
558
+ ctx.fillRect(rx, ry, rw, L.rep.h)
559
+ ctx.fillStyle = bar
560
+ ctx.fillRect(rx, ry, R.barW, L.rep.h)
561
+ let cy = ry + R.padT
562
+ if (L.rep.name) {
563
+ ctx.fillStyle = L.rep.color || t.reply.name
564
+ await P.richText(L.rep.name, rx + R.padL, cy + R.nameH / 2, R.nameSize, 'semibold')
565
+ cy += R.nameH
566
+ }
567
+ ctx.fillStyle = t.reply.text
568
+ for (const l of L.rep.lines) {
569
+ await P.richText(l, rx + R.padL, cy + R.lineH / 2, R.textSize)
570
+ cy += R.lineH
571
+ }
572
+ ctx.restore()
573
+ }
574
+
575
+ const top = by + L.head + b.padT
576
+ let lastBase = top
577
+ ctx.save()
578
+ ctx.fillStyle = t.bubble.text
579
+ for (let i = 0; i < L.lines.length; i++) {
580
+ const cy = top + b.lineH / 2 + i * b.lineH
581
+ await P.richText(L.lines[i], b.x + b.padL, cy, b.fontSize)
582
+ lastBase = cy + b.fontSize * b.base
583
+ }
584
+ ctx.restore()
585
+
586
+ const tsBase = L.inline ? lastBase + b.tsLift : top + L.lines.length * b.lineH + b.tsRow
587
+ ctx.save()
588
+ ctx.fillStyle = t.bubble.time
589
+ ctx.textAlign = 'right'
590
+ ctx.textBaseline = 'alphabetic'
591
+ P.text(L.ts, b.x + L.w - b.padR, tsBase, b.tsSize)
592
+ ctx.restore()
593
+ }
594
+
595
+ function drawGlassPanel(P, bg) {
596
+ const { ctx, theme: t } = P
597
+ const m = LAYOUT.menu, c = t.menu
598
+ const rr = () => roundRect(ctx, m.x, m.y, m.w, m.h, m.r)
599
+
600
+ ctx.save()
601
+ ctx.beginPath()
602
+ ctx.rect(0, 0, LAYOUT.W, LAYOUT.H)
603
+ rrPath(ctx, m.x, m.y, m.w, m.h, m.r)
604
+ ctx.clip('evenodd')
605
+ ctx.shadowColor = c.shadow
606
+ ctx.shadowBlur = 26
607
+ ctx.shadowOffsetY = 6
608
+ ctx.fillStyle = '#fff'
609
+ rr()
610
+ ctx.fill()
611
+ ctx.restore()
612
+
613
+ ctx.save()
614
+ rr()
615
+ ctx.clip()
616
+ ctx.drawImage(bg, 0, 0)
617
+ ctx.fillStyle = c.overlay
618
+ ctx.fillRect(m.x, m.y, m.w, m.h)
619
+ const tint = ctx.createLinearGradient(0, m.y, 0, m.y + m.h)
620
+ tint.addColorStop(0, c.tintTop)
621
+ tint.addColorStop(1, c.tintBottom)
622
+ ctx.fillStyle = tint
623
+ ctx.fillRect(m.x, m.y, m.w, m.h)
624
+ const sheen = ctx.createLinearGradient(0, m.y, 0, m.y + m.h)
625
+ sheen.addColorStop(0, c.sheen)
626
+ sheen.addColorStop(1, alpha(c.sheen, 0))
627
+ ctx.fillStyle = sheen
628
+ ctx.fillRect(m.x, m.y, m.w, m.h)
629
+ ctx.save()
630
+ ctx.translate(m.x + 18, m.y + 199)
631
+ ctx.scale(360, 220)
632
+ const glow = ctx.createRadialGradient(0, 0, 0, 0, 0, 1)
633
+ glow.addColorStop(0, c.glow)
634
+ glow.addColorStop(1, alpha(c.glow, 0))
635
+ ctx.fillStyle = glow
636
+ ctx.fillRect(-2, -2, 4, 4)
637
+ ctx.restore()
638
+ const spec = ctx.createLinearGradient(0, m.y, 0, m.y + 40)
639
+ spec.addColorStop(0, c.spec)
640
+ spec.addColorStop(1, alpha(c.spec, 0))
641
+ ctx.fillStyle = spec
642
+ ctx.fillRect(m.x, m.y, m.w, 40)
643
+ ctx.restore()
644
+
645
+ ctx.save()
646
+ const rim = ctx.createLinearGradient(0, m.y, 0, m.y + m.h)
647
+ rim.addColorStop(0, c.rimTop)
648
+ rim.addColorStop(1, c.rimBottom)
649
+ ctx.strokeStyle = rim
650
+ ctx.lineWidth = 1.2
651
+ roundRect(ctx, m.x + 0.6, m.y + 0.6, m.w - 1.2, m.h - 1.2, m.r)
652
+ ctx.stroke()
653
+ ctx.restore()
654
+ }
655
+
656
+ function drawMenu(P, bg, labels) {
657
+ const { ctx, theme: t } = P
658
+ const m = LAYOUT.menu
659
+ drawGlassPanel(P, bg)
660
+ for (const row of m.rows) {
661
+ if (row.divider) {
662
+ ctx.save()
663
+ ctx.strokeStyle = t.menu.divider
664
+ ctx.lineWidth = 1
665
+ ctx.beginPath()
666
+ ctx.moveTo(row.x1, row.divider)
667
+ ctx.lineTo(row.x2, row.divider)
668
+ ctx.stroke()
669
+ ctx.restore()
670
+ continue
671
+ }
672
+ const textColor = row.danger ? t.menu.danger : t.menu.text
673
+ const iconColor = row.danger ? t.menu.danger : t.menu.icon
674
+ drawIcon(ctx, row.icon, m.iconCx, row.y, iconColor)
675
+ ctx.save()
676
+ ctx.fillStyle = textColor
677
+ ctx.textAlign = 'left'
678
+ ctx.textBaseline = 'middle'
679
+ const label = P.fit(String(labels[row.key] ?? ''), m.w - (m.labelX - m.x) - 24, m.fontSize, 'semibold')
680
+ P.text(label, m.labelX, row.y, m.fontSize, 'semibold')
681
+ ctx.restore()
682
+ }
683
+ }
684
+
685
+ function drawNativeCard(P, bg, x, y, w, h, r) {
686
+ const { ctx, theme: t } = P
687
+ const c = t.menu
688
+ ctx.save()
689
+ ctx.shadowColor = c.shadow
690
+ ctx.shadowBlur = 30
691
+ ctx.shadowOffsetY = 8
692
+ ctx.fillStyle = c.card
693
+ roundRect(ctx, x, y, w, h, r)
694
+ ctx.fill()
695
+ ctx.restore()
696
+ ctx.save()
697
+ roundRect(ctx, x, y, w, h, r)
698
+ ctx.clip()
699
+ ctx.filter = 'blur(18px)'
700
+ ctx.drawImage(bg, 0, 0)
701
+ ctx.filter = 'none'
702
+ ctx.fillStyle = c.card
703
+ ctx.fillRect(x, y, w, h)
704
+ ctx.restore()
705
+ if (c.cardBorder) {
706
+ ctx.save()
707
+ ctx.strokeStyle = c.cardBorder
708
+ ctx.lineWidth = 1
709
+ roundRect(ctx, x + 0.5, y + 0.5, w - 1, h - 1, r)
710
+ ctx.stroke()
711
+ ctx.restore()
712
+ }
713
+ }
714
+
715
+ function drawNativeMenu(P, bg, labels, top) {
716
+ const { ctx, theme: t } = P
717
+ const n = LAYOUT.native.menu
718
+ const rows = n.rows
719
+ const mainH = rows.length * n.rowH
720
+ const y0 = top
721
+
722
+ drawNativeCard(P, bg, n.x, y0, n.w, mainH, n.r)
723
+ const moreY = y0 + mainH + n.gap
724
+ drawNativeCard(P, bg, n.x, moreY, n.w, n.rowH, n.r)
725
+
726
+ const items = rows.map((row, i) => ({ ...row, cy: y0 + i * n.rowH + n.rowH / 2 }))
727
+ items.push({ ...n.more, cy: moreY + n.rowH / 2 })
728
+
729
+ ctx.save()
730
+ ctx.strokeStyle = t.menu.separator
731
+ ctx.lineWidth = 1
732
+ for (let i = 1; i < rows.length; i++) {
733
+ const ly = Math.round(y0 + i * n.rowH) + 0.5
734
+ ctx.beginPath()
735
+ ctx.moveTo(n.x, ly)
736
+ ctx.lineTo(n.x + n.w, ly)
737
+ ctx.stroke()
738
+ }
739
+ ctx.restore()
740
+
741
+ for (const it of items) {
742
+ const color = it.danger ? t.menu.danger : t.menu.text
743
+ ctx.save()
744
+ ctx.fillStyle = color
745
+ ctx.textAlign = 'left'
746
+ ctx.textBaseline = 'middle'
747
+ const label = P.fit(String(labels[it.key] ?? ''), n.iconCx - n.labelX - 30, n.fontSize, n.weight)
748
+ P.text(label, n.labelX, it.cy + 1, n.fontSize, n.weight)
749
+ ctx.restore()
750
+ drawIcon(ctx, it.icon, n.iconCx, it.cy, it.danger ? t.menu.danger : t.menu.icon, 'native')
751
+ }
752
+ }
753
+
754
+ async function paint(opt) {
755
+ const { theme, families, emojiMap, labels, image, scale, timeout } = opt
756
+ const { W, H } = LAYOUT
757
+ const bg = await makeBackground(theme, timeout)
758
+
759
+ const canvas = createCanvas(Math.round(W * scale), Math.round(H * scale))
760
+ const ctx = canvas.getContext('2d')
761
+ ctx.scale(scale, scale)
762
+ const P = new Painter(ctx, { families, emojiMap, theme })
763
+
764
+ const style = opt.style || theme.style || 'glass'
765
+ opt.resolvedStyle = style
766
+ ctx.drawImage(bg, 0, 0)
767
+ if (opt.statusBar !== false) drawStatusBar(P, opt)
768
+
769
+ const L = layoutBubble(P, image, opt)
770
+ const by = LAYOUT.bubble.anchor - L.h
771
+ const p = LAYOUT.pill
772
+ if (opt.reactionBar !== false) {
773
+ const pillTop = Math.min(p.y, by - p.gap - p.h)
774
+ ctx.save()
775
+ ctx.translate(0, pillTop - p.y)
776
+ await drawEmojiBar(P, opt)
777
+ ctx.restore()
778
+ }
779
+ await drawBubble(P, L, by)
780
+ if (opt.menu !== false) {
781
+ if (style === 'native') drawNativeMenu(P, bg, labels, LAYOUT.bubble.anchor + LAYOUT.native.menu.top)
782
+ else drawMenu(P, bg, labels)
783
+ }
784
+ return canvas
785
+ }
786
+
787
+ module.exports = { paint }