From 1954c534bf59c133bc7481aad866390b622775fe Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Wed, 28 Jul 2021 19:02:39 +0000 Subject: [PATCH] Moved QrCode.toSvgString() out of the library and into the runnable demo program, slightly adapted some code, updated documentation comments. --- Readme.markdown | 2 +- src/io/nayuki/fastqrcodegen/QrCode.java | 34 ---------------- .../fastqrcodegen/QrCodeGeneratorDemo.java | 39 ++++++++++++++++++- src/io/nayuki/fastqrcodegen/package-info.java | 2 +- 4 files changed, 40 insertions(+), 37 deletions(-) diff --git a/Readme.markdown b/Readme.markdown index c14bc8a..dfcff4c 100644 --- a/Readme.markdown +++ b/Readme.markdown @@ -18,7 +18,7 @@ Features Core features: * 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 Japanese Unicode text in kanji mode to save a lot of space compared to UTF-8 bytes * Computes optimal segment mode switching for text with mixed numeric/alphanumeric/general/kanji parts diff --git a/src/io/nayuki/fastqrcodegen/QrCode.java b/src/io/nayuki/fastqrcodegen/QrCode.java index 66abd48..75e5e0f 100644 --- a/src/io/nayuki/fastqrcodegen/QrCode.java +++ b/src/io/nayuki/fastqrcodegen/QrCode.java @@ -277,40 +277,6 @@ public final class QrCode { } - /** - * Returns a string of SVG code for an image depicting this QR Code, with the specified number - * of border modules. The string always uses Unix newlines (\n), regardless of the platform. - * @param border the number of border modules to add, which must be non-negative - * @return a string representing this QR Code as an SVG XML document - * @throws IllegalArgumentException if the border is negative - */ - public String toSvgString(int border) { - if (border < 0) - throw new IllegalArgumentException("Border must be non-negative"); - long brd = border; - StringBuilder sb = new StringBuilder() - .append("\n") - .append("\n") - .append(String.format("\n", - size + brd * 2)) - .append("\t\n") - .append("\t\n") - .append("\n") - .toString(); - } - - /*---- Private helper methods for constructor: Drawing function modules ----*/ diff --git a/src/io/nayuki/fastqrcodegen/QrCodeGeneratorDemo.java b/src/io/nayuki/fastqrcodegen/QrCodeGeneratorDemo.java index 792b42d..5e9e2a0 100644 --- a/src/io/nayuki/fastqrcodegen/QrCodeGeneratorDemo.java +++ b/src/io/nayuki/fastqrcodegen/QrCodeGeneratorDemo.java @@ -62,7 +62,7 @@ public final class QrCodeGeneratorDemo { File imgFile = new File("hello-world-QR.png"); // File path for output ImageIO.write(img, "png", imgFile); // Write image to file - String svg = qr.toSvgString(4); // Convert to SVG XML code + String svg = toSvgString(qr, 4); // Convert to SVG XML code File svgFile = new File("hello-world-QR.svg"); // File path for output Files.write(svgFile.toPath(), // Write image to file svg.getBytes(StandardCharsets.UTF_8)); @@ -215,4 +215,41 @@ public final class QrCodeGeneratorDemo { ImageIO.write(img, "png", new File(filepath)); } + + /** + * Returns a string of SVG code for an image depicting the specified QR Code, with the specified + * number of border modules. The string always uses Unix newlines (\n), regardless of the platform. + * @param qr the QR Code to render (not {@code null}) + * @param border the number of border modules to add, which must be non-negative + * @return a string representing the QR Code as an SVG XML document + * @throws NullPointerException if the QR Code is {@code null} + * @throws IllegalArgumentException if the border is negative + */ + private static String toSvgString(QrCode qr, int border) { + Objects.requireNonNull(qr); + if (border < 0) + throw new IllegalArgumentException("Border must be non-negative"); + long brd = border; + StringBuilder sb = new StringBuilder() + .append("\n") + .append("\n") + .append(String.format("\n", + qr.size + brd * 2)) + .append("\t\n") + .append("\t\n") + .append("\n") + .toString(); + } + } diff --git a/src/io/nayuki/fastqrcodegen/package-info.java b/src/io/nayuki/fastqrcodegen/package-info.java index 220b8b2..0ab23a6 100644 --- a/src/io/nayuki/fastqrcodegen/package-info.java +++ b/src/io/nayuki/fastqrcodegen/package-info.java @@ -9,7 +9,7 @@ *

Core features:

*