File size: 3,750 Bytes
6f55a1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { DOMImplementation, XMLSerializer } = require('xmldom');
const JsBarcode = require('jsbarcode')
const { JSDOM } = require('jsdom')
const fs = require('fs')
const path = require('path')
const cp = require('child_process')

const src = path.join(__dirname, '..', 'src')
const _svg = fs.readFileSync(path.join(src, 'welcome.svg'), 'utf-8')
const barcode = data => {
    const xmlSerializer = new XMLSerializer();
    const document = new DOMImplementation().createDocument('http://www.w3.org/1999/xhtml', 'html', null);
    const svgNode = document.createElementNS('http://www.w3.org/2000/svg', 'svg');

    JsBarcode(svgNode, data, {
        xmlDocument: document,
    });

    return xmlSerializer.serializeToString(svgNode);
}
const imageSetter = (img, value) => img.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', value)
const textSetter = (el, value) => el.textContent = value

let { document: svg } = new JSDOM(_svg).window
/**
 * Generate SVG Welcome
 * @param {object} param0
 * @param {string} param0.wid
 * @param {string} param0.pp
 * @param {string} param0.name
 * @param {string} param0.text
 * @param {string} param0.background
 * @returns {string}
 */
const genSVG = async ({
    wid = '',
    pp = path.join(src, 'avatar_contact.png'),
    title = '',
    name = '',
    text = '',
    background = ''
} = {}) => {
    let el = {
        code: ['#_1661899539392 > g:nth-child(6) > image', imageSetter, toBase64(await toImg(barcode(wid.replace(/[^0-9]/g, '')), 'png'), 'image/png')],
        pp: ['#_1661899539392 > g:nth-child(3) > image', imageSetter, pp],
        text: ['#_1661899539392 > text.fil1.fnt0', textSetter, text],
        title: ['#_1661899539392 > text.fil2.fnt1', textSetter, title],
        name: ['#_1661899539392 > text.fil2.fnt2', textSetter, name],
        bg: ['#_1661899539392 > g:nth-child(2) > image', imageSetter, background],
    }
    for (let [selector, set, value] of Object.values(el)) {
        set(svg.querySelector(selector), value)
    }
    return svg.body.innerHTML
}

const toImg = (svg, format = 'png') => new Promise((resolve, reject) => {
    if (!svg) return resolve(Buffer.alloc(0))
    let bufs = []
    let im = cp.spawn('magick', ['convert', 'svg:-', format + ':-'])
    im.on('error', e => reject(e))
    im.stdout.on('data', chunk => bufs.push(chunk))
    im.stdin.write(Buffer.from(svg))
    im.stdin.end()
    im.on('close', code => {
        if (code !== 0) reject(code)
        resolve(Buffer.concat(bufs))
    })
})

const toBase64 = (buffer, mime) => `data:${mime};base64,${buffer.toString('base64')}`

/**
 * Render SVG Welcome
 * @param {object} param0
 * @param {string} param0.wid
 * @param {string} param0.pp
 * @param {string} param0.name
 * @param {string} param0.text
 * @param {string} param0.background
 * @returns {Promise<Buffer>}
 */
const render = async ({
    wid = '',
    pp = toBase64(fs.readFileSync(path.join(src, 'avatar_contact.png')), 'image/png'),
    name = '',
    title = '',
    text = '',
    background = toBase64(fs.readFileSync(path.join(src, 'Aesthetic', 'Aesthetic_000.jpeg')), 'image/jpeg'),
} = {}, format = 'png') => {
    let svg = await genSVG({
        wid, pp, name, text, background, title
    })
    return await toImg(svg, format)
}

if (require.main === module) {
    render({
        wid: '1234567890',
        // pp: '',
        name: 'John Doe',
        text: 'Lorem ipsum\ndot sit color',
        title: 'grup testing'
        // background: ''
    }, 'jpg').then(result => {
        // console.log(result)
        process.stdout.write(result)
    })
    // toImg(barcode('test')).then(result => {
    //     // console.log(result)
    //     process.stdout.write(result)

    // })
} else module.exports = render