From cd037d914920420f611e8ec06e4f2fec33e6f61d Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Wed, 28 Jul 2021 17:09:16 +0000 Subject: [PATCH] In C++ version: moved QrCode.toSvgString() out of the library and into the runnable demo program, slightly adapted some code, updated documentation comments. --- Readme.markdown | 4 ++-- cpp/QrCode.cpp | 28 ---------------------------- cpp/QrCode.hpp | 7 ------- cpp/QrCodeGeneratorDemo.cpp | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 38 deletions(-) diff --git a/Readme.markdown b/Readme.markdown index 6fe5a38..c48fb61 100644 --- a/Readme.markdown +++ b/Readme.markdown @@ -18,7 +18,7 @@ Core features: * Available in 6 programming languages, all with nearly equal functionality: Java, TypeScript/JavaScript, Python, Rust, C++, C * Significantly shorter code but more documentation comments compared to competing libraries * Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard -* Output formats: Raw modules/pixels of the QR symbol (all languages), SVG XML string (all languages except C) +* Output format: Raw modules/pixels of the QR symbol * Detects finder-like penalty patterns more accurately than other implementations * Encodes numeric and special-alphanumeric text in less space than general text * Open source code under the permissive MIT License @@ -105,7 +105,7 @@ C++ language: // Simple operation QrCode qr0 = QrCode::encodeText("Hello, world!", QrCode::Ecc::MEDIUM); - std::string svg = qr0.toSvgString(4); + std::string svg = toSvgString(qr0, 4); // See QrCodeGeneratorDemo // Manual operation std::vector segs = diff --git a/cpp/QrCode.cpp b/cpp/QrCode.cpp index 538204e..ada5969 100644 --- a/cpp/QrCode.cpp +++ b/cpp/QrCode.cpp @@ -385,34 +385,6 @@ bool QrCode::getModule(int x, int y) const { } -std::string QrCode::toSvgString(int border) const { - if (border < 0) - throw std::domain_error("Border must be non-negative"); - if (border > INT_MAX / 2 || border * 2 > INT_MAX - size) - throw std::overflow_error("Border too large"); - - std::ostringstream sb; - sb << "\n"; - sb << "\n"; - sb << "\n"; - sb << "\t\n"; - sb << "\t\n"; - sb << "\n"; - return sb.str(); -} - - void QrCode::drawFunctionPatterns() { // Draw horizontal and vertical timing patterns for (int i = 0; i < size; i++) { diff --git a/cpp/QrCode.hpp b/cpp/QrCode.hpp index 2a1e987..8a71501 100644 --- a/cpp/QrCode.hpp +++ b/cpp/QrCode.hpp @@ -369,13 +369,6 @@ class QrCode final { public: bool getModule(int x, int y) const; - /* - * 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: std::string toSvgString(int border) const; - - /*---- Private helper methods for constructor: Drawing function modules ----*/ diff --git a/cpp/QrCodeGeneratorDemo.cpp b/cpp/QrCodeGeneratorDemo.cpp index 017dcbe..3419902 100644 --- a/cpp/QrCodeGeneratorDemo.cpp +++ b/cpp/QrCodeGeneratorDemo.cpp @@ -24,10 +24,12 @@ * Software. */ +#include #include #include #include #include +#include #include #include #include "QrCode.hpp" @@ -42,6 +44,7 @@ static void doBasicDemo(); static void doVarietyDemo(); static void doSegmentDemo(); static void doMaskDemo(); +static std::string toSvgString(const QrCode &qr, int border); static void printQr(const QrCode &qr); @@ -66,7 +69,7 @@ static void doBasicDemo() { // Make and print the QR Code symbol const QrCode qr = QrCode::encodeText(text, errCorLvl); printQr(qr); - std::cout << qr.toSvgString(4) << std::endl; + std::cout << toSvgString(qr, 4) << std::endl; } @@ -186,6 +189,36 @@ static void doMaskDemo() { /*---- Utilities ----*/ +// 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. +static std::string toSvgString(const QrCode &qr, int border) { + if (border < 0) + throw std::domain_error("Border must be non-negative"); + if (border > INT_MAX / 2 || border * 2 > INT_MAX - qr.getSize()) + throw std::overflow_error("Border too large"); + + std::ostringstream sb; + sb << "\n"; + sb << "\n"; + sb << "\n"; + sb << "\t\n"; + sb << "\t\n"; + sb << "\n"; + return sb.str(); +} + + // Prints the given QrCode object to the console. static void printQr(const QrCode &qr) { int border = 4;