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.

pull/118/head
Project Nayuki 3 years ago
parent 3531fda14f
commit 3dcac1db78

@ -87,7 +87,7 @@ Python language:
# Simple operation # Simple operation
qr0 = QrCode.encode_text("Hello, world!", QrCode.Ecc.MEDIUM) 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 # Manual operation
segs = QrSegment.make_segments("3141592653589793238462643383") segs = QrSegment.make_segments("3141592653589793238462643383")

@ -46,7 +46,7 @@ def do_basic_demo() -> None:
# Make and print the QR Code symbol # Make and print the QR Code symbol
qr = QrCode.encode_text(text, errcorlvl) qr = QrCode.encode_text(text, errcorlvl)
print_qr(qr) print_qr(qr)
print(qr.to_svg_str(4)) print(to_svg_str(qr, 4))
def do_variety_demo() -> None: def do_variety_demo() -> None:
@ -170,6 +170,25 @@ def do_mask_demo() -> None:
# ---- Utilities ---- # ---- 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 """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 {0} {0}" stroke="none">
<rect width="100%" height="100%" fill="#FFFFFF"/>
<path d="{1}" fill="#000000"/>
</svg>
""".format(qr.get_size() + border * 2, " ".join(parts))
def print_qr(qrcode: QrCode) -> None: def print_qr(qrcode: QrCode) -> None:
"""Prints the given QrCode object to the console.""" """Prints the given QrCode object to the console."""
border = 4 border = 4

@ -232,27 +232,6 @@ class QrCode:
return (0 <= x < self._size) and (0 <= y < self._size) and self._modules[y][x] 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 """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 {0} {0}" stroke="none">
<rect width="100%" height="100%" fill="#FFFFFF"/>
<path d="{1}" fill="#000000"/>
</svg>
""".format(self._size + border * 2, " ".join(parts))
# ---- Private helper methods for constructor: Drawing function modules ---- # ---- Private helper methods for constructor: Drawing function modules ----
def _draw_function_patterns(self) -> None: def _draw_function_patterns(self) -> None:

@ -69,7 +69,7 @@ Core features:
* Available in 6 programming languages, all with nearly equal functionality: Java, TypeScript/JavaScript, Python, Rust, C++, C * 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 * 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 * 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 * Encodes numeric and special-alphanumeric text in less space than general text
* Open source code under the permissive MIT License * Open source code under the permissive MIT License
@ -94,7 +94,7 @@ Examples:
# Simple operation # Simple operation
qr0 = QrCode.encode_text("Hello, world!", QrCode.Ecc.MEDIUM) 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 # Manual operation
segs = QrSegment.make_segments("3141592653589793238462643383") segs = QrSegment.make_segments("3141592653589793238462643383")

Loading…
Cancel
Save