-
Notifications
You must be signed in to change notification settings - Fork 633
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Miloslav Mrvík
committed
May 26, 2020
1 parent
f829dd2
commit 77ec831
Showing
6 changed files
with
205 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,9 @@ | ||
// let Utils = require('./utils') | ||
const big = require('./terminal/terminal') | ||
const small = require('./terminal/terminal-small') | ||
|
||
exports.render = function (qrData, options, cb) { | ||
const size = qrData.modules.size | ||
const data = qrData.modules.data | ||
|
||
// let opts = Utils.getOptions(options) | ||
|
||
// use same scheme as https://github.com/gtanner/qrcode-terminal because it actually works! =) | ||
const black = '\x1b[40m \x1b[0m' | ||
const white = '\x1b[47m \x1b[0m' | ||
|
||
let output = '' | ||
const hMargin = Array(size + 3).join(white) | ||
const vMargin = Array(2).join(white) | ||
|
||
output += hMargin + '\n' | ||
for (let i = 0; i < size; ++i) { | ||
output += white | ||
for (let j = 0; j < size; j++) { | ||
// let topModule = data[i * size + j] | ||
// let bottomModule = data[(i + 1) * size + j] | ||
|
||
output += data[i * size + j] ? black : white// getBlockChar(topModule, bottomModule) | ||
} | ||
// output += white+'\n' | ||
output += vMargin + '\n' | ||
} | ||
|
||
output += hMargin + '\n' | ||
|
||
if (typeof cb === 'function') { | ||
cb(null, output) | ||
} | ||
|
||
return output | ||
} | ||
/* | ||
exports.renderToFile = function renderToFile (path, qrData, options, cb) { | ||
if (typeof cb === 'undefined') { | ||
cb = options | ||
options = undefined | ||
if (options && options.small) { | ||
return small.render(qrData, options, cb) | ||
} | ||
let fs = require('fs') | ||
let utf8 = exports.render(qrData, options) | ||
fs.writeFile(path, utf8, cb) | ||
return big.render(qrData, options, cb) | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const backgroundWhite = '\x1b[47m' | ||
const backgroundBlack = '\x1b[40m' | ||
const foregroundWhite = '\x1b[37m' | ||
const foregroundBlack = '\x1b[30m' | ||
const reset = '\x1b[0m' | ||
const lineSetupNormal = backgroundWhite + foregroundBlack // setup colors | ||
const lineSetupInverse = backgroundBlack + foregroundWhite // setup colors | ||
|
||
const createPalette = function (lineSetup, foregroundWhite, foregroundBlack) { | ||
return { | ||
// 1 ... white, 2 ... black, 0 ... transparent (default) | ||
|
||
'00': reset + ' ' + lineSetup, | ||
'01': reset + foregroundWhite + '▄' + lineSetup, | ||
'02': reset + foregroundBlack + '▄' + lineSetup, | ||
10: reset + foregroundWhite + '▀' + lineSetup, | ||
11: ' ', | ||
12: '▄', | ||
20: reset + foregroundBlack + '▀' + lineSetup, | ||
21: '▀', | ||
22: '█' | ||
} | ||
} | ||
|
||
/** | ||
* Returns code for QR pixel | ||
* @param {boolean[][]} modules | ||
* @param {number} size | ||
* @param {number} x | ||
* @param {number} y | ||
* @return {'0' | '1' | '2'} | ||
*/ | ||
const mkCodePixel = function (modules, size, x, y) { | ||
const sizePlus = size + 1 | ||
if ((x >= sizePlus) || (y >= sizePlus) || (y < -1) || (x < -1)) return '0' | ||
if ((x >= size) || (y >= size) || (y < 0) || (x < 0)) return '1' | ||
const idx = (y * size) + x | ||
return modules[idx] ? '2' : '1' | ||
} | ||
|
||
/** | ||
* Returns code for four QR pixels. Suitable as key in palette. | ||
* @param {boolean[][]} modules | ||
* @param {number} size | ||
* @param {number} x | ||
* @param {number} y | ||
* @return {keyof palette} | ||
*/ | ||
const mkCode = function (modules, size, x, y) { | ||
return ( | ||
mkCodePixel(modules, size, x, y) + | ||
mkCodePixel(modules, size, x, y + 1) | ||
) | ||
} | ||
|
||
exports.render = function (qrData, options, cb) { | ||
const size = qrData.modules.size | ||
const data = qrData.modules.data | ||
|
||
const inverse = !!(options && options.inverse) | ||
const lineSetup = options && options.inverse ? lineSetupInverse : lineSetupNormal | ||
const white = inverse ? foregroundBlack : foregroundWhite | ||
const black = inverse ? foregroundWhite : foregroundBlack | ||
|
||
const palette = createPalette(lineSetup, white, black) | ||
const newLine = reset + '\n' + lineSetup | ||
|
||
let output = lineSetup // setup colors | ||
|
||
for (let y = -1; y < size + 1; y += 2) { | ||
for (let x = -1; x < size; x++) { | ||
output += palette[mkCode(data, size, x, y)] | ||
} | ||
|
||
output += palette[mkCode(data, size, size, y)] + newLine | ||
} | ||
|
||
output += reset | ||
|
||
if (typeof cb === 'function') { | ||
cb(null, output) | ||
} | ||
|
||
return output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// let Utils = require('./utils') | ||
|
||
exports.render = function (qrData, options, cb) { | ||
const size = qrData.modules.size | ||
const data = qrData.modules.data | ||
|
||
// let opts = Utils.getOptions(options) | ||
|
||
// use same scheme as https://github.com/gtanner/qrcode-terminal because it actually works! =) | ||
const black = '\x1b[40m \x1b[0m' | ||
const white = '\x1b[47m \x1b[0m' | ||
|
||
let output = '' | ||
const hMargin = Array(size + 3).join(white) | ||
const vMargin = Array(2).join(white) | ||
|
||
output += hMargin + '\n' | ||
for (let i = 0; i < size; ++i) { | ||
output += white | ||
for (let j = 0; j < size; j++) { | ||
// let topModule = data[i * size + j] | ||
// let bottomModule = data[(i + 1) * size + j] | ||
|
||
output += data[i * size + j] ? black : white// getBlockChar(topModule, bottomModule) | ||
} | ||
// output += white+'\n' | ||
output += vMargin + '\n' | ||
} | ||
|
||
output += hMargin + '\n' | ||
|
||
if (typeof cb === 'function') { | ||
cb(null, output) | ||
} | ||
|
||
return output | ||
} | ||
/* | ||
exports.renderToFile = function renderToFile (path, qrData, options, cb) { | ||
if (typeof cb === 'undefined') { | ||
cb = options | ||
options = undefined | ||
} | ||
let fs = require('fs') | ||
let utf8 = exports.render(qrData, options) | ||
fs.writeFile(path, utf8, cb) | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters