From 3531fda14fa22b5a252b15f65233a41b629da011 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Wed, 28 Jul 2021 17:09:02 +0000 Subject: [PATCH] In TypeScript version: moved QrCode.toSvgString() out of the library and into a demo program, slightly adapted some code, updated documentation comments. --- Readme.markdown | 2 +- typescript-javascript/qrcodegen-input-demo.ts | 26 +++++++++++++++++-- typescript-javascript/qrcodegen.ts | 24 ----------------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/Readme.markdown b/Readme.markdown index 0282416..19939e7 100644 --- a/Readme.markdown +++ b/Readme.markdown @@ -70,7 +70,7 @@ TypeScript/JavaScript languages: // Simple operation var qr0 = QRC.encodeText("Hello, world!", QRC.Ecc.MEDIUM); - var svg = qr0.toSvgString(4); + var svg = toSvgString(qr0, 4); // See qrcodegen-input-demo // Manual operation var segs = qrcodegen.QrSegment.makeSegments("3141592653589793238462643383"); diff --git a/typescript-javascript/qrcodegen-input-demo.ts b/typescript-javascript/qrcodegen-input-demo.ts index ec4b83e..2de8341 100644 --- a/typescript-javascript/qrcodegen-input-demo.ts +++ b/typescript-javascript/qrcodegen-input-demo.ts @@ -95,13 +95,13 @@ namespace app { drawCanvas(qr, scale, border, canvas); canvas.style.removeProperty("display"); } else { - const code: string = qr.toSvgString(border); + const code: string = toSvgString(qr, 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); + svgXml.value = code; } // Returns a string to describe the given list of segments. @@ -168,6 +168,28 @@ namespace app { } + // Returns a string of SVG code for an image depicting the given QR Code, with the given number + // of border modules. The string always uses Unix newlines (\n), regardless of the platform. + function toSvgString(qr: qrcodegen.QrCode, border: number): string { + if (border < 0) + throw "Border must be non-negative"; + let parts: Array = []; + for (let y = 0; y < qr.size; y++) { + for (let x = 0; x < qr.size; x++) { + if (qr.getModule(x, y)) + parts.push(`M${x + border},${y + border}h1v1h-1z`); + } + } + return ` + + + + + +` + } + + export function handleVersionMinMax(which: "min"|"max"): void { const minElem: HTMLInputElement = getInput("version-min-input"); const maxElem: HTMLInputElement = getInput("version-max-input"); diff --git a/typescript-javascript/qrcodegen.ts b/typescript-javascript/qrcodegen.ts index c0e051c..053770c 100644 --- a/typescript-javascript/qrcodegen.ts +++ b/typescript-javascript/qrcodegen.ts @@ -239,30 +239,6 @@ namespace qrcodegen { } - /*-- Public instance methods --*/ - - // Returns a string of SVG code for an image depicting this QR Code, with the given number - // of border modules. The string always uses Unix newlines (\n), regardless of the platform. - public toSvgString(border: int): string { - if (border < 0) - throw "Border must be non-negative"; - let parts: Array = []; - for (let y = 0; y < this.size; y++) { - for (let x = 0; x < this.size; x++) { - if (this.getModule(x, y)) - parts.push(`M${x + border},${y + border}h1v1h-1z`); - } - } - return ` - - - - - -` - } - - /*-- Private helper methods for constructor: Drawing function modules --*/ // Reads this object's version field, and draws and marks all function modules.