In Rust 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 3dcac1db78
commit 5bc7bce3c3

@ -158,7 +158,7 @@ Rust language:
// Simple operation
let qr = QrCode::encode_text("Hello, world!",
QrCodeEcc::Medium).unwrap();
let svg = qr.to_svg_string(4);
let svg = to_svg_string(&qr, 4); // See qrcodegen-demo
// Manual operation
let chrs: Vec<char> = "3141592653589793238462643383".chars().collect();

@ -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, SVG XML string
* 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
@ -42,7 +42,7 @@ Examples
// Simple operation
let qr = QrCode::encode_text("Hello, world!",
QrCodeEcc::Medium).unwrap();
let svg = qr.to_svg_string(4);
let svg = to_svg_string(&qr, 4); // See qrcodegen-demo
// Manual operation
let chrs: Vec<char> = "3141592653589793238462643383".chars().collect();

@ -52,7 +52,7 @@ fn do_basic_demo() {
// Make and print the QR Code symbol
let qr: QrCode = QrCode::encode_text(text, errcorlvl).unwrap();
print_qr(&qr);
println!("{}", qr.to_svg_string(4));
println!("{}", to_svg_string(&qr, 4));
}
@ -163,6 +163,35 @@ fn do_mask_demo() {
/*---- 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.
fn to_svg_string(qr: &QrCode, border: i32) -> String {
assert!(border >= 0, "Border must be non-negative");
let mut result = String::new();
result += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
result += "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
let dimension = qr.size().checked_add(border.checked_mul(2).unwrap()).unwrap();
result += &format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 {0} {0}\" stroke=\"none\">\n", dimension);
result += "\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n";
result += "\t<path d=\"";
for y in 0 .. qr.size() {
for x in 0 .. qr.size() {
if qr.get_module(x, y) {
if x != 0 || y != 0 {
result += " ";
}
result += &format!("M{},{}h1v1h-1z", x + border, y + border);
}
}
}
result += "\" fill=\"#000000\"/>\n";
result += "</svg>\n";
result
}
// Prints the given QrCode object to the console.
fn print_qr(qr: &QrCode) {
let border: i32 = 4;

@ -38,7 +38,7 @@
//! - 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
//! - 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
@ -64,7 +64,7 @@
//! ```
//! let qr = QrCode::encode_text("Hello, world!",
//! QrCodeEcc::Medium).unwrap();
//! let svg = qr.to_svg_string(4);
//! let svg = to_svg_string(&qr, 4); // See qrcodegen-demo
//! ```
//!
//! Manual operation:
@ -368,36 +368,6 @@ impl QrCode {
}
/// 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.
pub fn to_svg_string(&self, border: i32) -> String {
assert!(border >= 0, "Border must be non-negative");
let mut result = String::new();
result += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
result += "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
let dimension = self.size.checked_add(border.checked_mul(2).unwrap()).unwrap();
result += &format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 {0} {0}\" stroke=\"none\">\n", dimension);
result += "\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n";
result += "\t<path d=\"";
for y in 0 .. self.size {
for x in 0 .. self.size {
if self.get_module(x, y) {
if x != 0 || y != 0 {
result += " ";
}
result += &format!("M{},{}h1v1h-1z", x + border, y + border);
}
}
}
result += "\" fill=\"#000000\"/>\n";
result += "</svg>\n";
result
}
/*---- Private helper methods for constructor: Drawing function modules ----*/
// Reads this object's version field, and draws and marks all function modules.

Loading…
Cancel
Save