mirror of
https://github.com/status-im/QR-Code-generator.git
synced 2026-08-31 01:31:10 +00:00
Deleted hand-written JavaScript code in favor of compiling from TypeScript, updated directories and readme.
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
#
|
||||
# Build script for QR Code generator (TypeScript)
|
||||
#
|
||||
# Copyright (c) Project Nayuki. (MIT License)
|
||||
# https://www.nayuki.io/page/qr-code-generator-library
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
# this software and associated documentation files (the "Software"), to deal in
|
||||
# the Software without restriction, including without limitation the rights to
|
||||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
# the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
# subject to the following conditions:
|
||||
# - The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
# - The Software is provided "as is", without warranty of any kind, express or
|
||||
# implied, including but not limited to the warranties of merchantability,
|
||||
# fitness for a particular purpose and noninfringement. In no event shall the
|
||||
# authors or copyright holders be liable for any claim, damages or other
|
||||
# liability, whether in an action of contract, tort or otherwise, arising from,
|
||||
# out of or in connection with the Software or the use or other dealings in the
|
||||
# Software.
|
||||
#
|
||||
|
||||
tsc --strict --lib DOM,DOM.Iterable,ES6 --target ES6 qrcodegen.ts qrcodegen-demo.ts
|
||||
|
||||
if [ '!' -d node_modules ]; then
|
||||
npm install @types/node
|
||||
fi
|
||||
tsc --strict --target ES2017 --outFile qrcodegen-worker.js qrcodegen.ts qrcodegen-worker.ts
|
||||
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
* QR Code generator demo (TypeScript)
|
||||
*
|
||||
* Copyright (c) Project Nayuki. (MIT License)
|
||||
* https://www.nayuki.io/page/qr-code-generator-library
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
* - The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* - The Software is provided "as is", without warranty of any kind, express or
|
||||
* implied, including but not limited to the warranties of merchantability,
|
||||
* fitness for a particular purpose and noninfringement. In no event shall the
|
||||
* authors or copyright holders be liable for any claim, damages or other
|
||||
* liability, whether in an action of contract, tort or otherwise, arising from,
|
||||
* out of or in connection with the Software or the use or other dealings in the
|
||||
* Software.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
|
||||
namespace app {
|
||||
|
||||
function initialize(): void {
|
||||
let elems = document.querySelectorAll("input[type=number], textarea");
|
||||
for (let el of elems) {
|
||||
if (el.id.indexOf("version-") != 0)
|
||||
(el as any).oninput = redrawQrCode;
|
||||
}
|
||||
elems = document.querySelectorAll("input[type=radio], input[type=checkbox]");
|
||||
for (let el of elems)
|
||||
(el as HTMLInputElement).onchange = redrawQrCode;
|
||||
redrawQrCode();
|
||||
}
|
||||
|
||||
|
||||
function redrawQrCode(): void {
|
||||
// Show/hide rows based on bitmap/vector image output
|
||||
const bitmapOutput: boolean = getInput("output-format-bitmap").checked;
|
||||
const scaleRow : HTMLElement = getElem("scale-row");
|
||||
const svgXmlRow: HTMLElement = getElem("svg-xml-row");
|
||||
if (bitmapOutput) {
|
||||
scaleRow.style.removeProperty("display");
|
||||
svgXmlRow.style.display = "none";
|
||||
} else {
|
||||
scaleRow.style.display = "none";
|
||||
svgXmlRow.style.removeProperty("display");
|
||||
}
|
||||
const svgXml = getElem("svg-xml-output") as HTMLTextAreaElement;
|
||||
svgXml.value = "";
|
||||
|
||||
// Reset output images in case of early termination
|
||||
const canvas = getElem("qrcode-canvas") as HTMLCanvasElement;
|
||||
const svg = (document.getElementById("qrcode-svg") as Element) as SVGElement;
|
||||
canvas.style.display = "none";
|
||||
svg.style.display = "none";
|
||||
|
||||
// Returns a QrCode.Ecc object based on the radio buttons in the HTML form.
|
||||
function getInputErrorCorrectionLevel(): qrcodegen.QrCode.Ecc {
|
||||
if (getInput("errcorlvl-medium").checked)
|
||||
return qrcodegen.QrCode.Ecc.MEDIUM;
|
||||
else if (getInput("errcorlvl-quartile").checked)
|
||||
return qrcodegen.QrCode.Ecc.QUARTILE;
|
||||
else if (getInput("errcorlvl-high").checked)
|
||||
return qrcodegen.QrCode.Ecc.HIGH;
|
||||
else // In case no radio button is depressed
|
||||
return qrcodegen.QrCode.Ecc.LOW;
|
||||
}
|
||||
|
||||
// Get form inputs and compute QR Code
|
||||
const ecl: qrcodegen.QrCode.Ecc = getInputErrorCorrectionLevel();
|
||||
const text: string = (getElem("text-input") as HTMLTextAreaElement).value;
|
||||
const segs: Array<qrcodegen.QrSegment> = qrcodegen.QrSegment.makeSegments(text);
|
||||
const minVer: number = parseInt(getInput("version-min-input").value, 10);
|
||||
const maxVer: number = parseInt(getInput("version-max-input").value, 10);
|
||||
const mask: number = parseInt(getInput("mask-input").value, 10);
|
||||
const boostEcc: boolean = getInput("boost-ecc-input").checked;
|
||||
const qr: qrcodegen.QrCode = qrcodegen.QrCode.encodeSegments(segs, ecl, minVer, maxVer, mask, boostEcc);
|
||||
|
||||
// Draw image output
|
||||
const border: number = parseInt(getInput("border-input").value, 10);
|
||||
if (border < 0 || border > 100)
|
||||
return;
|
||||
if (bitmapOutput) {
|
||||
const scale: number = parseInt(getInput("scale-input").value, 10);
|
||||
if (scale <= 0 || scale > 30)
|
||||
return;
|
||||
qr.drawCanvas(scale, border, canvas);
|
||||
canvas.style.removeProperty("display");
|
||||
} else {
|
||||
const code: string = qr.toSvgString(border);
|
||||
const viewBox: string = (/ viewBox="([^"]*)"/.exec(code) as RegExpExecArray)[1];
|
||||
const pathD: string = (/ d="([^"]*)"/.exec(code) as RegExpExecArray)[1];
|
||||
svg.setAttribute("viewBox", viewBox);
|
||||
(svg.querySelector("path") as Element).setAttribute("d", pathD);
|
||||
svg.style.removeProperty("display");
|
||||
svgXml.value = qr.toSvgString(border);
|
||||
}
|
||||
|
||||
// Returns a string to describe the given list of segments.
|
||||
function describeSegments(segs: Array<qrcodegen.QrSegment>): string {
|
||||
if (segs.length == 0)
|
||||
return "none";
|
||||
else if (segs.length == 1) {
|
||||
const mode: qrcodegen.QrSegment.Mode = segs[0].mode;
|
||||
const Mode = qrcodegen.QrSegment.Mode;
|
||||
if (mode == Mode.NUMERIC ) return "numeric";
|
||||
if (mode == Mode.ALPHANUMERIC) return "alphanumeric";
|
||||
if (mode == Mode.BYTE ) return "byte";
|
||||
if (mode == Mode.KANJI ) return "kanji";
|
||||
return "unknown";
|
||||
} else
|
||||
return "multiple";
|
||||
}
|
||||
|
||||
// Returns the number of Unicode code points in the given UTF-16 string.
|
||||
function countUnicodeChars(str: string): number {
|
||||
let result: number = 0;
|
||||
for (let i = 0; i < str.length; i++, result++) {
|
||||
const c: number = str.charCodeAt(i);
|
||||
if (c < 0xD800 || c >= 0xE000)
|
||||
continue;
|
||||
else if (0xD800 <= c && c < 0xDC00 && i + 1 < str.length) { // High surrogate
|
||||
i++;
|
||||
const d: number = str.charCodeAt(i);
|
||||
if (0xDC00 <= d && d < 0xE000) // Low surrogate
|
||||
continue;
|
||||
}
|
||||
throw "Invalid UTF-16 string";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Show the QR Code symbol's statistics as a string
|
||||
getElem("statistics-output").textContent = `QR Code version = ${qr.version}, ` +
|
||||
`mask pattern = ${qr.mask}, ` +
|
||||
`character count = ${countUnicodeChars(text)},\n` +
|
||||
`encoding mode = ${describeSegments(segs)}, ` +
|
||||
`error correction = level ${"LMQH".charAt(qr.errorCorrectionLevel.ordinal)}, ` +
|
||||
`data bits = ${qrcodegen.QrSegment.getTotalBits(segs, qr.version) as number}.`;
|
||||
}
|
||||
|
||||
|
||||
export function handleVersionMinMax(which: "min"|"max"): void {
|
||||
const minElem: HTMLInputElement = getInput("version-min-input");
|
||||
const maxElem: HTMLInputElement = getInput("version-max-input");
|
||||
let minVal: number = parseInt(minElem.value, 10);
|
||||
let maxVal: number = parseInt(maxElem.value, 10);
|
||||
minVal = Math.max(Math.min(minVal, qrcodegen.QrCode.MAX_VERSION), qrcodegen.QrCode.MIN_VERSION);
|
||||
maxVal = Math.max(Math.min(maxVal, qrcodegen.QrCode.MAX_VERSION), qrcodegen.QrCode.MIN_VERSION);
|
||||
if (which == "min" && minVal > maxVal)
|
||||
maxVal = minVal;
|
||||
else if (which == "max" && maxVal < minVal)
|
||||
minVal = maxVal;
|
||||
minElem.value = minVal.toString();
|
||||
maxElem.value = maxVal.toString();
|
||||
redrawQrCode();
|
||||
}
|
||||
|
||||
|
||||
function getElem(id: string): HTMLElement {
|
||||
const result: HTMLElement|null = document.getElementById(id);
|
||||
if (result instanceof HTMLElement)
|
||||
return result;
|
||||
throw "Assertion error";
|
||||
}
|
||||
|
||||
|
||||
function getInput(id: string): HTMLInputElement {
|
||||
const result: HTMLElement = getElem(id);
|
||||
if (result instanceof HTMLInputElement)
|
||||
return result;
|
||||
throw "Assertion error";
|
||||
}
|
||||
|
||||
|
||||
initialize();
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<!--
|
||||
- QR Code generator demo (HTML+JavaScript)
|
||||
-
|
||||
- Copyright (c) Project Nayuki. (MIT License)
|
||||
- https://www.nayuki.io/page/qr-code-generator-library
|
||||
-
|
||||
- Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
- this software and associated documentation files (the "Software"), to deal in
|
||||
- the Software without restriction, including without limitation the rights to
|
||||
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
- the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
- subject to the following conditions:
|
||||
- * The above copyright notice and this permission notice shall be included in
|
||||
- all copies or substantial portions of the Software.
|
||||
- * The Software is provided "as is", without warranty of any kind, express or
|
||||
- implied, including but not limited to the warranties of merchantability,
|
||||
- fitness for a particular purpose and noninfringement. In no event shall the
|
||||
- authors or copyright holders be liable for any claim, damages or other
|
||||
- liability, whether in an action of contract, tort or otherwise, arising from,
|
||||
- out of or in connection with the Software or the use or other dealings in the
|
||||
- Software.
|
||||
-->
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>QR Code generator library demo (JavaScript)</title>
|
||||
<style type="text/css">
|
||||
html {
|
||||
font-family: sans-serif;
|
||||
}
|
||||
td {
|
||||
vertical-align: top;
|
||||
padding-top: 0.2em;
|
||||
padding-bottom: 0.2em;
|
||||
}
|
||||
td:first-child {
|
||||
white-space: pre;
|
||||
padding-right: 0.5em;
|
||||
}
|
||||
input[type=radio], input[type=checkbox] {
|
||||
margin: 0em;
|
||||
padding: 0em;
|
||||
}
|
||||
input[type=radio] + label, input[type=checkbox] + label {
|
||||
margin-right: 0.8em;
|
||||
padding-left: 0.2em;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>QR Code generator demo library (JavaScript)</h1>
|
||||
<form action="#" method="get" onsubmit="return false;">
|
||||
<table class="noborder" style="width:100%">
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><strong>Text string:</strong></td>
|
||||
<td style="width:100%"><textarea placeholder="Enter your text to be put into the QR Code" id="text-input" style="width:100%; max-width:30em; height:5em; font-family:inherit"></textarea></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><strong>QR Code:</strong></td>
|
||||
<td>
|
||||
<canvas id="qrcode-canvas" style="padding:1em; background-color:#E8E8E8"></canvas>
|
||||
<svg id="qrcode-svg" style="width:30em; height:30em; padding:1em; background-color:#E8E8E8">
|
||||
<rect width="100%" height="100%" fill="#FFFFFF" stroke-width="0"></rect>
|
||||
<path d="" fill="#000000" stroke-width="0"></path>
|
||||
</svg>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><strong>Error correction:</strong></td>
|
||||
<td>
|
||||
<input type="radio" name="errcorlvl" id="errcorlvl-low" checked="checked"><label for="errcorlvl-low">Low</label>
|
||||
<input type="radio" name="errcorlvl" id="errcorlvl-medium"><label for="errcorlvl-medium">Medium</label>
|
||||
<input type="radio" name="errcorlvl" id="errcorlvl-quartile"><label for="errcorlvl-quartile">Quartile</label>
|
||||
<input type="radio" name="errcorlvl" id="errcorlvl-high"><label for="errcorlvl-high">High</label>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Output format:</td>
|
||||
<td>
|
||||
<input type="radio" name="output-format" id="output-format-bitmap" checked="checked"><label for="output-format-bitmap">Bitmap</label>
|
||||
<input type="radio" name="output-format" id="output-format-vector"><label for="output-format-vector">Vector</label>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Border:</td>
|
||||
<td><input type="number" value="4" min="0" max="100" step="1" id="border-input" style="width:4em"> modules</td>
|
||||
</tr>
|
||||
<tr id="scale-row">
|
||||
<td>Scale:</td>
|
||||
<td><input type="number" value="8" min="1" max="30" step="1" id="scale-input" style="width:4em"> pixels per module</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Version range:</td>
|
||||
<td>
|
||||
Minimum = <input type="number" value="1" min="1" max="40" step="1" id="version-min-input" style="width:4em" oninput="app.handleVersionMinMax('min');">,
|
||||
maximum = <input type="number" value="40" min="1" max="40" step="1" id="version-max-input" style="width:4em" oninput="app.handleVersionMinMax('max');">
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Mask pattern:</td>
|
||||
<td><input type="number" value="-1" min="-1" max="7" step="1" id="mask-input" style="width:4em"> (−1 for automatic, 0 to 7 for manual)</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Boost ECC:</td>
|
||||
<td><input type="checkbox" checked="checked" id="boost-ecc-input"><label for="boost-ecc-input">Increase <abbr title="error-correcting code">ECC</abbr> level within same version</label></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Statistics:</td>
|
||||
<td id="statistics-output" style="white-space:pre"></td>
|
||||
</tr>
|
||||
<tr id="svg-xml-row">
|
||||
<td>SVG XML code:</td>
|
||||
<td>
|
||||
<textarea id="svg-xml-output" readonly="readonly" style="width:100%; max-width:50em; height:15em; font-family:monospace"></textarea>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</form>
|
||||
<script type="application/javascript" src="qrcodegen.js"></script>
|
||||
<script type="application/javascript" src="qrcodegen-demo.js"></script>
|
||||
|
||||
<hr>
|
||||
<p>Copyright © Project Nayuki – <a href="https://www.nayuki.io/page/qr-code-generator-library">https://www.nayuki.io/page/qr-code-generator-library</a></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* QR Code generator test worker (TypeScript)
|
||||
*
|
||||
* This program reads data and encoding parameters from standard input and writes
|
||||
* QR Code bitmaps to standard output. The I/O format is one integer per line.
|
||||
* Run with no command line arguments. The program is intended for automated
|
||||
* batch testing of end-to-end functionality of this QR Code generator library.
|
||||
*
|
||||
* Copyright (c) Project Nayuki. (MIT License)
|
||||
* https://www.nayuki.io/page/qr-code-generator-library
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
* - The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* - The Software is provided "as is", without warranty of any kind, express or
|
||||
* implied, including but not limited to the warranties of merchantability,
|
||||
* fitness for a particular purpose and noninfringement. In no event shall the
|
||||
* authors or copyright holders be liable for any claim, damages or other
|
||||
* liability, whether in an action of contract, tort or otherwise, arising from,
|
||||
* out of or in connection with the Software or the use or other dealings in the
|
||||
* Software.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
|
||||
async function main(): Promise<void> {
|
||||
while (true) {
|
||||
// Read data or exit
|
||||
const length: number = await input.readInt();
|
||||
if (length == -1)
|
||||
break;
|
||||
let data: Array<number> = [];
|
||||
for (let i = 0; i < length; i++)
|
||||
data.push(await input.readInt());
|
||||
|
||||
// Read encoding parameters
|
||||
const errCorLvl : number = await input.readInt();
|
||||
const minVersion: number = await input.readInt();
|
||||
const maxVersion: number = await input.readInt();
|
||||
const mask : number = await input.readInt();
|
||||
const boostEcl : number = await input.readInt();
|
||||
|
||||
// Make segments for encoding
|
||||
let segs: Array<qrcodegen.QrSegment>;
|
||||
if (data.every(b => b < 128)) { // Is ASCII
|
||||
const s: string = data.map(b => String.fromCharCode(b)).join("");
|
||||
segs = qrcodegen.QrSegment.makeSegments(s);
|
||||
} else
|
||||
segs = [qrcodegen.QrSegment.makeBytes(data)];
|
||||
|
||||
try { // Try to make QR Code symbol
|
||||
const qr = qrcodegen.QrCode.encodeSegments(
|
||||
segs, ECC_LEVELS[errCorLvl], minVersion, maxVersion, mask, boostEcl != 0);
|
||||
// Print grid of modules
|
||||
await printLine(qr.version);
|
||||
for (let y = 0; y < qr.size; y++) {
|
||||
for (let x = 0; x < qr.size; x++)
|
||||
await printLine(qr.getModule(x, y) ? 1 : 0);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
if (e == "Data too long")
|
||||
await printLine(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
namespace input {
|
||||
|
||||
let queue: Array<string> = [];
|
||||
let callback: ((line:string)=>void)|null = null;
|
||||
|
||||
const readline = require("readline");
|
||||
let reader = readline.createInterface({
|
||||
input: process.stdin,
|
||||
terminal: false,
|
||||
});
|
||||
reader.on("line", (line: string) => {
|
||||
queue.push(line);
|
||||
if (callback !== null) {
|
||||
callback(queue.shift() as string);
|
||||
callback = null;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async function readLine(): Promise<string> {
|
||||
return new Promise(resolve => {
|
||||
if (callback !== null)
|
||||
throw "Illegal state";
|
||||
if (queue.length > 0)
|
||||
resolve(queue.shift() as string);
|
||||
else
|
||||
callback = resolve;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export async function readInt(): Promise<number> {
|
||||
let s = await readLine();
|
||||
if (!/^-?\d+$/.test(s))
|
||||
throw "Invalid number syntax";
|
||||
return parseInt(s, 10);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
async function printLine(x: Object): Promise<void> {
|
||||
return new Promise(resolve =>
|
||||
process.stdout.write(x + "\n", "utf-8", ()=>resolve()));
|
||||
}
|
||||
|
||||
|
||||
const ECC_LEVELS: Array<qrcodegen.QrCode.Ecc> = [
|
||||
qrcodegen.QrCode.Ecc.LOW,
|
||||
qrcodegen.QrCode.Ecc.MEDIUM,
|
||||
qrcodegen.QrCode.Ecc.QUARTILE,
|
||||
qrcodegen.QrCode.Ecc.HIGH,
|
||||
];
|
||||
|
||||
|
||||
main();
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user