-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathtest-real.js
More file actions
515 lines (476 loc) · 16.8 KB
/
Copy pathtest-real.js
File metadata and controls
515 lines (476 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Real rendering test using actual drawMultilineText
// Run: node test-real.js
const fs = require('fs')
const path = require('path')
const { createCanvas } = require('canvas')
const { drawQuote } = require('./utils/quote-generate/composer')
const { loadFonts, gradientTint } = require('./utils/quote-generate/index')
const { avatarImageLetters } = require('./utils/quote-generate/avatar')
const { drawAvatar } = require('./utils/quote-generate/avatar')
const { AVATAR_COLORS } = require('./utils/quote-generate/constants')
const { colorLuminance } = require('./utils/quote-generate/color')
const { loadIcons, drawVoiceRow, drawDocumentRow, drawAudioRow } = require('./utils/quote-generate/attachments')
const OUT = path.join(__dirname, 'test-output')
fs.mkdirSync(OUT, { recursive: true })
function makeAvatar (color1, color2, letter) {
const size = 100
const canvas = createCanvas(size, size)
const ctx = canvas.getContext('2d')
const grad = ctx.createLinearGradient(0, 0, size, size)
grad.addColorStop(0, color1)
grad.addColorStop(1, color2)
ctx.fillStyle = grad
ctx.beginPath()
ctx.arc(size / 2, size / 2, size / 2, 0, Math.PI * 2)
ctx.fill()
ctx.fillStyle = '#fff'
ctx.font = `bold ${size * 0.45}px sans-serif`
ctx.textAlign = 'center'
ctx.fillText(letter, size / 2, size / 2 + size * 0.16)
return canvas
}
async function main () {
await loadFonts()
await loadIcons()
const { drawMultilineText } = require('./utils/quote-generate/text-renderer')
const scale = 2
const width = 512 * scale
const height = 512 * scale
const tg = { getCustomEmojiStickers: async () => [] }
// Mirrors the production scale in utils/quote-generate/index.js
const nameSize = 18 * scale
const fontSize = 24 * scale
const replyNameSize = 14 * scale
const replyTextSize = 15 * scale
async function mkName (text, color) {
const canvas = await drawMultilineText(text, [{ type: 'bold', offset: 0, length: text.length }], nameSize, color, 0, nameSize, width, nameSize, 'apple', tg)
return gradientTint(canvas, color, colorLuminance(color, 0.25))
}
async function mkText (text, color) {
return drawMultilineText(text, [], fontSize, color || '#fff', 0, fontSize, width, height, 'apple', tg)
}
async function mkReplyName (text, color) {
return drawMultilineText(text, 'bold', replyNameSize, color, 0, replyNameSize, width * 0.9, replyNameSize, 'apple', tg)
}
async function mkReplyText (text, color) {
return drawMultilineText(text, [], replyTextSize, color || '#fff', 0, replyTextSize, width * 0.9, replyTextSize, 'apple', tg)
}
const bg = { colorOne: '#292232', colorTwo: '#292232', textColor: '#fff' }
const bgLight = { colorOne: '#e8e8e8', colorTwo: '#e8e8e8', textColor: '#000' }
const bgGrad = { colorOne: '#667eea', colorTwo: '#764ba2', textColor: '#fff' }
const tests = []
// 1. Basic dark
tests.push({
name: 'real-01-basic',
opts: {
scale, background: bg,
avatar: makeAvatar('#FF885E', '#FF516A', 'A'),
name: await mkName('assasinfil', '#FF8E86'),
text: await mkText('Переполнение вошло в чат'),
nameColor: '#FF8E86'
}
})
// 2. Basic light
tests.push({
name: 'real-02-light',
opts: {
scale, background: bgLight,
avatar: makeAvatar('#72D5FD', '#2A9EF1', 'N'),
name: await mkName('nowiks', '#3CA5EC'),
text: await mkText('Ну получить можно... не понял..', '#000'),
nameColor: '#3CA5EC'
}
})
// Debug reply canvas sizes
const dbgReplyName = await mkReplyName('Виталий', '#B18FFF')
const dbgReplyText = await mkReplyText('не понял..')
console.log(`Reply name canvas: ${dbgReplyName.width}x${dbgReplyName.height}`)
console.log(`Reply text canvas: ${dbgReplyText.width}x${dbgReplyText.height}`)
// 3. Reply
tests.push({
name: 'real-03-reply',
opts: {
scale, background: { colorOne: '#1a1a2e', colorTwo: '#16213e', textColor: '#fff' },
avatar: makeAvatar('#FF885E', '#FF516A', 'A'),
reply: {
name: await mkReplyName('Виталий', '#B18FFF'),
nameColor: '#B18FFF',
text: await mkReplyText('не понял..')
},
name: await mkName('assasinfil', '#FF8E86'),
text: await mkText('Переполнение вошло в чат'),
nameColor: '#FF8E86'
}
})
// 4. No avatar, no name (continuation)
tests.push({
name: 'real-04-noname',
opts: {
scale, background: bg,
avatar: null, name: null,
text: await mkText('понял..')
}
})
// 5. Gradient bg
tests.push({
name: 'real-05-gradient',
opts: {
scale, background: bgGrad,
avatar: makeAvatar('#FFCD6A', '#FFA85C', 'Y'),
name: await mkName('Yuri', '#FFCD6A'),
text: await mkText('This is a gradient background test with a longer message that wraps across multiple lines'),
nameColor: '#FFCD6A'
}
})
// 6. Forward
tests.push({
name: 'real-06-forward',
opts: {
scale, background: bg,
avatar: makeAvatar('#A0DE7E', '#54CB68', 'P'),
name: await mkName('Pavel Durov', '#4DD6BF'),
text: await mkText('Telegram is the most secure messenger in the world.'),
isForward: true,
forwardLabel: 'Forwarded from Durov\'s Channel',
nameColor: '#4DD6BF'
}
})
// 7. Forward + reply
tests.push({
name: 'real-07-fwd-reply',
opts: {
scale, background: { colorOne: '#1e1e2e', colorTwo: '#1e1e2e', textColor: '#fff' },
avatar: makeAvatar('#53EDD6', '#28C9B7', 'M'),
reply: {
name: await mkReplyName('Alice', '#7AC9FF'),
nameColor: '#7AC9FF',
text: await mkReplyText('What did he say?')
},
name: await mkName('Mike', '#45E8D1'),
text: await mkText('He said something very interesting about the project roadmap'),
isForward: true,
forwardLabel: 'Forwarded from Project Updates',
nameColor: '#45E8D1'
}
})
// 8. Sender tag
tests.push({
name: 'real-08-tag',
opts: {
scale, background: bg,
avatar: makeAvatar('#FF885E', '#FF516A', 'A'),
name: await mkName('Admin User', '#FF8E86'),
text: await mkText('Welcome to the group! Please read the rules.'),
nameColor: '#FF8E86',
senderTag: 'Admin'
}
})
// 9. Tag + forward
tests.push({
name: 'real-09-tag-fwd',
opts: {
scale, background: { colorOne: '#0d1117', colorTwo: '#161b22', textColor: '#e6edf3' },
avatar: makeAvatar('#FFA8A8', '#FF719A', 'C'),
name: await mkName('Channel Bot', '#FF7FD5'),
text: await mkText('Important announcement: server maintenance scheduled for tonight at 3 AM UTC'),
isForward: true,
forwardLabel: 'Forwarded from System Alerts',
nameColor: '#FF7FD5',
senderTag: 'NFC/SubGHz Dev'
}
})
// 10. Long text with reply
tests.push({
name: 'real-10-long-reply',
opts: {
scale, background: { colorOne: '#2d2d44', colorTwo: '#2d2d44', textColor: '#fff' },
avatar: makeAvatar('#72D5FD', '#2A9EF1', 'L'),
reply: {
name: await mkReplyName('Previous User', '#3CA5EC'),
nameColor: '#3CA5EC',
text: await mkReplyText('Can you explain?')
},
name: await mkName('Long Text User', '#7AC9FF'),
text: await mkText('So basically what happens is that the system processes the input through several stages. First it validates the data, then transforms it, and finally persists it to the database.'),
nameColor: '#7AC9FF'
}
})
// 11. Short text
tests.push({
name: 'real-11-short',
opts: {
scale, background: bg,
avatar: makeAvatar('#FFCD6A', '#FFA85C', 'S'),
name: await mkName('User', '#FFA357'),
text: await mkText('ok'),
nameColor: '#FFA357'
}
})
// 12. Long name + tag
tests.push({
name: 'real-12-longname-tag',
opts: {
scale, background: bg,
avatar: makeAvatar('#E0A2F3', '#D669ED', 'X'),
name: await mkName('Alexander Superlong Name', '#B18FFF'),
text: await mkText('Testing a very long name with a tag'),
nameColor: '#B18FFF',
senderTag: 'Lead Developer & Architect'
}
})
// 13. Partial quote (isQuote)
tests.push({
name: 'real-13-quote',
opts: {
scale, background: bg,
avatar: makeAvatar('#72D5FD', '#2A9EF1', 'Q'),
reply: {
name: await mkReplyName('Original Author', '#7AC9FF'),
nameColor: '#7AC9FF',
text: await mkReplyText('only this part was quoted')
},
name: await mkName('Quoter', '#45E8D1'),
text: await mkText('Exactly, this part is the key insight'),
nameColor: '#45E8D1',
isQuote: true
}
})
// 14. Reply no avatar
tests.push({
name: 'real-14-reply-noavatar',
opts: {
scale, background: bg,
avatar: null,
reply: {
name: await mkReplyName('Someone', '#FF8E86'),
nameColor: '#FF8E86',
text: await mkReplyText('Original message text here')
},
name: null,
text: await mkText('Reply to that')
}
})
// Helper: make circular avatar from avatarImageLetters (same as drawAvatar does)
function makeRealAvatar (letters, colorIndex) {
const avatarCanvas = avatarImageLetters(letters, AVATAR_COLORS[colorIndex % 7])
const size = avatarCanvas.height
const circle = createCanvas(size, size)
const cctx = circle.getContext('2d')
cctx.beginPath()
cctx.arc(size / 2, size / 2, size / 2, 0, Math.PI * 2, true)
cctx.clip()
cctx.drawImage(avatarCanvas, 0, 0, size, size)
return circle
}
// 16. Initials avatar — two letters
tests.push({
name: 'real-16-initials-2',
opts: {
scale, background: bg,
avatar: makeRealAvatar('YL', 3),
name: await mkName('Yuri Ly', '#4DD6BF'),
text: await mkText('Testing two-letter initials avatar'),
nameColor: '#4DD6BF'
}
})
// 17. Initials avatar — one letter
tests.push({
name: 'real-17-initials-1',
opts: {
scale, background: bg,
avatar: makeRealAvatar('А', 5),
name: await mkName('Антон', '#B18FFF'),
text: await mkText('Тестуємо аватарку з одною літерою'),
nameColor: '#B18FFF'
}
})
// 18. Initials avatar — Cyrillic two letters
tests.push({
name: 'real-18-initials-cyrillic',
opts: {
scale, background: bgLight,
avatar: makeRealAvatar('ВН', 2),
name: await mkName('Виктор Никитчук', '#3CA5EC'),
text: await mkText('Кириллические буквы в аватарке'),
nameColor: '#3CA5EC'
}
})
// 19. Standalone avatar renders (for direct comparison)
const avatarTests = [
{ letters: 'YL', idx: 0 },
{ letters: 'А', idx: 1 },
{ letters: 'ВН', idx: 2 },
{ letters: 'JD', idx: 3 },
{ letters: 'М', idx: 4 },
{ letters: '?', idx: 5 },
{ letters: 'PD', idx: 6 }
]
const avSize = 200
const avPad = 20
const avRow = createCanvas(avatarTests.length * (avSize + avPad), avSize)
const avCtx = avRow.getContext('2d')
for (let i = 0; i < avatarTests.length; i++) {
const av = makeRealAvatar(avatarTests[i].letters, avatarTests[i].idx)
avCtx.drawImage(av, i * (avSize + avPad), 0, avSize, avSize)
}
fs.writeFileSync(path.join(OUT, 'real-19-avatars-row.png'), avRow.toBuffer('image/png'))
console.log(` ✓ real-19-avatars-row (${avRow.width}x${avRow.height})`)
// 20. Voice message
tests.push({
name: 'real-20-voice',
opts: {
scale, background: bg,
avatar: makeAvatar('#FF885E', '#FF516A', 'A'),
name: await mkName('assasinfil', '#FF8E86'),
attachment: { canvas: drawVoiceRow(Array.from({ length: 50 }, (_, i) => 6 + ((i * 13) % 26), 0), 83, '#FF8E86', '#fff', scale, width * 2 / 3) },
nameColor: '#FF8E86'
}
})
// 21. Document
tests.push({
name: 'real-21-document',
opts: {
scale, background: bg,
avatar: makeAvatar('#72D5FD', '#2A9EF1', 'N'),
name: await mkName('nowiks', '#7AC9FF'),
attachment: { canvas: drawDocumentRow({ file_name: 'квартальний_звіт_2026.pdf', file_size: 2.4 * 1024 * 1024 }, '#7AC9FF', '#fff', scale, width * 2 / 3) },
nameColor: '#7AC9FF'
}
})
// 22. Audio (no cover → note disc)
tests.push({
name: 'real-22-audio',
opts: {
scale, background: bg,
avatar: makeAvatar('#A0DE7E', '#54CB68', 'M'),
name: await mkName('Музика', '#4DD6BF'),
attachment: { canvas: drawAudioRow({ title: 'Водограй', performer: 'Володимир Івасюк', duration: 215 }, '#4DD6BF', '#fff', scale, width * 2 / 3) },
nameColor: '#4DD6BF'
}
})
// 23. Video with play badge + duration, plus caption
const videoFrame = createCanvas(800, 450)
{
const vctx = videoFrame.getContext('2d')
const grad = vctx.createLinearGradient(0, 0, 800, 450)
grad.addColorStop(0, '#2b5876')
grad.addColorStop(1, '#4e4376')
vctx.fillStyle = grad
vctx.fillRect(0, 0, 800, 450)
}
tests.push({
name: 'real-23-video',
opts: {
scale, background: bg,
avatar: makeAvatar('#FFCD6A', '#FFA85C', 'V'),
name: await mkName('Відеограф', '#FFA357'),
media: { canvas: videoFrame, type: 'video', maxSize: width * 2 / 3, badge: { play: true, label: '0:42' } },
text: await mkText('подивіться до кінця 🔥'),
nameColor: '#FFA357'
}
})
// 24. GIF (no caption → flush media with GIF chip)
const gifFrame = createCanvas(640, 360)
{
const gctx = gifFrame.getContext('2d')
const grad = gctx.createLinearGradient(0, 360, 640, 0)
grad.addColorStop(0, '#f83600')
grad.addColorStop(1, '#f9d423')
gctx.fillStyle = grad
gctx.fillRect(0, 0, 640, 360)
}
tests.push({
name: 'real-24-gif',
opts: {
scale, background: bg,
avatar: makeAvatar('#E0A2F3', '#D669ED', 'G'),
name: await mkName('Гіфка', '#B18FFF'),
media: { canvas: gifFrame, type: 'animation', maxSize: width * 2 / 3, badge: { label: 'GIF' } },
nameColor: '#B18FFF'
}
})
// Generate single tests
for (const test of tests) {
try {
const canvas = drawQuote(test.opts)
fs.writeFileSync(path.join(OUT, `${test.name}.png`), canvas.toBuffer('image/png'))
console.log(` ✓ ${test.name} (${canvas.width}x${canvas.height})`)
} catch (err) {
console.error(` ✗ ${test.name}: ${err.message}`)
}
}
// 15. MULTI-MESSAGE DIALOG
const dialogOpts = [
{
scale, background: bg,
avatar: null, // avatar rides on the LAST message of a streak
name: await mkName('assasinfil', '#FF8E86'),
text: await mkText('Хто знає як зробити NFC клон?'),
nameColor: '#FF8E86',
senderTag: 'NFC/SubGHz Dev',
groupPos: 'first'
},
{
scale, background: bg,
avatar: makeAvatar('#FF885E', '#FF516A', 'A'),
name: null,
text: await mkText('Треба Flipper Zero для цього'),
groupPos: 'last'
},
{
scale, background: bg,
avatar: makeAvatar('#72D5FD', '#2A9EF1', 'N'),
reply: {
name: await mkReplyName('assasinfil', '#FF8E86'),
nameColor: '#FF8E86',
text: await mkReplyText('Хто знає як зробити NFC клон?')
},
name: await mkName('nowiks', '#7AC9FF'),
text: await mkText('Ну получить можно... не понял..'),
nameColor: '#7AC9FF',
isQuote: true
},
{
scale, background: bg,
avatar: makeAvatar('#E0A2F3', '#D669ED', 'В'),
name: await mkName('Виктор Никитчук', '#B18FFF'),
text: await mkText('Звучит очень по гейски'),
isForward: true,
forwardLabel: 'Forwarded from Misha Myte',
nameColor: '#B18FFF'
},
{
scale, background: bg,
avatar: makeAvatar('#FF885E', '#FF516A', 'A'),
name: await mkName('assasinfil', '#FF8E86'),
text: await mkText('Переполнение вошло в чат'),
nameColor: '#FF8E86',
senderTag: 'NFC/SubGHz Dev'
},
{
scale, background: bg,
avatar: makeAvatar('#72D5FD', '#2A9EF1', 'N'),
name: await mkName('nowiks', '#7AC9FF'),
text: await mkText('понял..'),
nameColor: '#7AC9FF'
}
]
const quoteMargin = 5 * scale
const dialogCanvases = dialogOpts.map(o => drawQuote(o))
let totalW = 0, totalH = 0
for (const c of dialogCanvases) {
if (c.width > totalW) totalW = c.width
totalH += c.height + quoteMargin
}
const dialogCanvas = createCanvas(totalW, totalH)
const dctx = dialogCanvas.getContext('2d')
let y = 0
for (const c of dialogCanvases) {
dctx.drawImage(c, 0, y)
y += c.height + quoteMargin
}
fs.writeFileSync(path.join(OUT, 'real-15-dialog.png'), dialogCanvas.toBuffer('image/png'))
console.log(` ✓ real-15-dialog (${dialogCanvas.width}x${dialogCanvas.height})`)
console.log('\nDone!')
}
main().catch(console.error)