From 3dcac1db78c39f024878bee4a687ffef209b42b9 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Wed, 28 Jul 2021 17:09:05 +0000 Subject: [PATCH] In Python version: moved QrCode.to_svg_string() out of the library and into the runnable demo program, slightly adapted some code, updated documentation comments. --- Readme.markdown | 2 +- python/qrcodegen-demo.py | 21 ++++++++++++++++++++- python/qrcodegen.py | 21 --------------------- python/setup.py | 4 ++-- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Readme.markdown b/Readme.markdown index 19939e7..a060ca7 100644 --- a/Readme.markdown +++ b/Readme.markdown @@ -87,7 +87,7 @@ Python language: # Simple operation qr0 = QrCode.encode_text("Hello, world!", QrCode.Ecc.MEDIUM) - svg = qr0.to_svg_str(4) + svg = to_svg_str(qr0, 4) # See qrcodegen-demo # Manual operation segs = QrSegment.make_segments("3141592653589793238462643383") diff --git a/python/qrcodegen-demo.py b/python/qrcodegen-demo.py index 159dd56..ef6c098 100644 --- a/python/qrcodegen-demo.py +++ b/python/qrcodegen-demo.py @@ -46,7 +46,7 @@ def do_basic_demo() -> None: # Make and print the QR Code symbol qr = QrCode.encode_text(text, errcorlvl) print_qr(qr) - print(qr.to_svg_str(4)) + print(to_svg_str(qr, 4)) def do_variety_demo() -> None: @@ -170,6 +170,25 @@ def do_mask_demo() -> None: # ---- Utilities ---- +def to_svg_str(qr: QrCode, border: int) -> str: + """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.""" + if border < 0: + raise ValueError("Border must be non-negative") + parts: List[str] = [] + for y in range(qr.get_size()): + for x in range(qr.get_size()): + if qr.get_module(x, y): + parts.append("M{},{}h1v1h-1z".format(x + border, y + border)) + return """ + + + + + +""".format(qr.get_size() + border * 2, " ".join(parts)) + + def print_qr(qrcode: QrCode) -> None: """Prints the given QrCode object to the console.""" border = 4 diff --git a/python/qrcodegen.py b/python/qrcodegen.py index 7cbea57..48bf020 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -232,27 +232,6 @@ class QrCode: return (0 <= x < self._size) and (0 <= y < self._size) and self._modules[y][x] - # ---- Public instance methods ---- - - def to_svg_str(self, border: int) -> str: - """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.""" - if border < 0: - raise ValueError("Border must be non-negative") - parts: List[str] = [] - for y in range(self._size): - for x in range(self._size): - if self.get_module(x, y): - parts.append("M{},{}h1v1h-1z".format(x + border, y + border)) - return """ - - - - - -""".format(self._size + border * 2, " ".join(parts)) - - # ---- Private helper methods for constructor: Drawing function modules ---- def _draw_function_patterns(self) -> None: diff --git a/python/setup.py b/python/setup.py index 50272bd..787c79a 100644 --- a/python/setup.py +++ b/python/setup.py @@ -69,7 +69,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, SVG XML string +* Output format: Raw modules/pixels of the QR symbol * Encodes numeric and special-alphanumeric text in less space than general text * Open source code under the permissive MIT License @@ -94,7 +94,7 @@ Examples: # Simple operation qr0 = QrCode.encode_text("Hello, world!", QrCode.Ecc.MEDIUM) - svg = qr0.to_svg_str(4) + svg = to_svg_str(qr0, 4) # See qrcodegen-demo # Manual operation segs = QrSegment.make_segments("3141592653589793238462643383")