-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
96 lines (84 loc) · 3.39 KB
/
Copy pathindex.js
File metadata and controls
96 lines (84 loc) · 3.39 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
const sharp = require('sharp');
const fs = require('fs');
const FONT_SIZE = 5;
const FONT_RATIO = 0.6;
const FONT_WIDTH = FONT_SIZE * FONT_RATIO;
const imageDir = './data/hbd.png';
const codeDir = './data/p2p.go';
const outputDir = './data/hbdOutput.xml';
sharp(imageDir)
.raw()
.toBuffer({ resolveWithObject: true })
.then((data) => {
const step = data.info.channels;
const width = data.info.width;
const height = data.info.height;
const pixels = [];
for (let i = 0; i < data.data.length; i += step) {
pixels.push(data.data.slice(i, i + step));
}
let code = fs.readFileSync(codeDir, 'utf8').replace(/\s*\n+\s*/g, ' ').replace(/\s+/g, ' ');
if (code.length < pixels.length) {
code = code.repeat(Math.ceil(pixels.length / code.length));
}
const isSameColor = (c1, c2) => {
return (c1[0] === c2[0] && c1[1] === c2[1] && c1[2] === c2[2]);
}
let result = '';
result += `<svg version="1.1"
viewBox="0 0 ${100 * width} ${100 * height}"
width="${100 * width}" height="${100 * height}"
xml:space="preserve"
style="font-family: 'Source Code Pro'; font-size: ${FONT_SIZE}px; font-weight: 500; white-space: normal;"
xmlns="http://www.w3.org/2000/svg">`;
for (let i = 0; i < height; i++) {
let lastPixel;
for (let j = 0; j < width; j++) {
let pixel = pixels[i * width + j];
let char = code[i * width + j];
switch (char) {
case '"':
char = '"';
break;
case '\'':
char = ''';
break;
case '<':
char = '<';
break;
case '>':
char = '>';
break;
case '&':
char = '&';
break;
default:
break;
}
if (j === 0) {
result += `<text x="${j * FONT_WIDTH}" y="${i * FONT_SIZE}" fill="rgb(${pixel[0]} ${pixel[1]} ${pixel[2]})">${char}`;
lastPixel = pixel;
} else if (j === width - 1) {
result += isSameColor(lastPixel, pixel) ?
`${char}</text>` :
`</text><text x="${j * FONT_WIDTH}" y="${i * FONT_SIZE}" fill="rgb(${pixel[0]} ${pixel[1]} ${pixel[2]})">${char}</text>`;
} else {
if (isSameColor(lastPixel, pixel)) {
result += char;
} else {
result += `</text><text x="${j * FONT_WIDTH}" y="${i * FONT_SIZE}" fill="rgb(${pixel[0]} ${pixel[1]} ${pixel[2]})">${char}`;
lastPixel = pixel;
}
}
}
}
result += "</svg>";
fs.open(outputDir, 'w', (err, fd) => {
if (err) {
throw (err);
}
fs.writeFileSync(fd, result);
})
}).catch((err) => {
console.log("error:", err.message);
});