diff --git a/qrcode.zig b/qrcode.zig index 82dbaebfa7c857ef94d8b73c29c27b383ed64c1c..df8263d98b0495abfeab6c6221227e78756bfed3 100644 --- a/qrcode.zig +++ b/qrcode.zig @@ -564,6 +564,41 @@ fn BitGrid(comptime size: usize) type { } return content.toOwnedSlice(); } + + pub fn asSvg(grid: *const @This(), allocator: std.mem.Allocator) ![]u8 { + var content: std.ArrayList(u8) = .init(allocator); + errdefer content.deinit(); + try content.writer().print("\n", .{ size + 2, size + 1, size + 2, size + 1 }); + try content.writer().print("\n", .{ size + 2, size + 1 }); + var prev = false; + var run: u16 = 0; + for (0..size) |y| { + try content.appendSlice("\n"); + } + try content.appendSlice("\n"); + return content.toOwnedSlice(); + } }; } diff --git a/test.zig b/test.zig index 6b6a03e28becef96585cab4b01bb98c963dcff20..05c09d6d20222c18829300ee22b02b11ce5f90cc 100644 --- a/test.zig +++ b/test.zig @@ -32,6 +32,35 @@ test { \\⠉⠉⠉⠁⠁⠁⠀⠁⠈⠁⠁| \\ ); + const svg = try grid.asSvg(allocator); + defer allocator.free(svg); + try expect(svg).toEqualString( + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + ); } test { const grid = qrcode.encode(.latin1, .L, 2, "Version 2"); @@ -66,6 +95,39 @@ test { \\⠉⠉⠉⠁⠁⠉⠀⠀⠁⠈⠉⠉⠁| \\ ); + const svg = try grid.asSvg(allocator); + defer allocator.free(svg); + try expect(svg).toEqualString( + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + ); } test { const grid = qrcode.encode(.latin1, .L, 3, "Version 3 QR Code"); @@ -103,6 +165,43 @@ test { \\⠉⠉⠉⠁⠉⠉⠈⠉⠁⠁⠁⠀⠁⠈⠀| \\ ); + const svg = try grid.asSvg(allocator); + defer allocator.free(svg); + try expect(svg).toEqualString( + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + ); } test { const grid = qrcode.encode(.latin1, .L, 4, "Version 4 QR Code, up to 50 char"); @@ -143,6 +242,47 @@ test { \\⠉⠉⠉⠁⠉⠀⠁⠀⠁⠀⠁⠈⠉⠁⠉⠉⠁| \\ ); + const svg = try grid.asSvg(allocator); + defer allocator.free(svg); + try expect(svg).toEqualString( + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + ); } test { const grid = qrcode.encode(.latin1, .M, 10, "VERSION 10 QR CODE, UP TO 174 CHAR AT H LEVEL, WITH 57X57 MODULES AND PLENTY OF ERROR CORRECTION TO GO AROUND. NOTE THAT THERE ARE ADDITIONAL TRACKING BOXES"); @@ -201,6 +341,71 @@ test { \\⠉⠉⠉⠁⠁⠈⠀⠀⠈⠈⠁⠉⠈⠀⠈⠀⠀⠉⠈⠈⠁⠀⠀⠁⠁⠀⠁⠀⠀| \\ ); + const svg = try grid.asSvg(allocator); + defer allocator.free(svg); + try expect(svg).toEqualString( + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + ); } test { const grid = qrcode.encode( @@ -314,6 +519,131 @@ test { \\⠉⠉⠉⠁⠁⠉⠁⠉⠈⠀⠁⠁⠈⠁⠈⠉⠈⠀⠁⠈⠉⠈⠉⠈⠁⠉⠀⠉⠈⠁⠉⠁⠉⠈⠉⠀⠉⠉⠁⠉⠈⠀⠉⠈⠀⠁⠈⠀⠁⠉⠉⠈⠈⠀⠁⠁⠈⠈⠀| \\ ); + const svg = try grid.asSvg(allocator); + defer allocator.free(svg); + try expect(svg).toEqualString( + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + ); } test { const grid = qrcode.encode( @@ -472,4 +802,189 @@ test { \\⠉⠉⠉⠁⠉⠉⠁⠈⠁⠈⠀⠉⠀⠀⠀⠈⠉⠀⠁⠁⠁⠀⠀⠀⠈⠀⠈⠁⠁⠉⠈⠉⠀⠁⠈⠉⠉⠉⠁⠁⠁⠀⠁⠀⠈⠁⠁⠉⠈⠈⠀⠈⠉⠈⠈⠀⠈⠀⠁⠈⠀⠁⠈⠈⠈⠈⠉⠁⠉⠁⠈⠈⠈⠉⠁⠉⠁⠀⠉⠀⠀⠁⠉⠈⠉⠈⠈⠀⠁| \\ ); + const svg = try grid.asSvg(allocator); + defer allocator.free(svg); + try expect(svg).toEqualString( + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + ); }