Skip to content

Commit

Permalink
[merge] PR #23 from aruZeta/feature/more-intuitive-radius/1
Browse files Browse the repository at this point in the history
Feature: more intuitive radius (SVG)
  • Loading branch information
aruZeta committed Sep 11, 2022
2 parents 2c9140c + b64fc10 commit 93bedf4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
28 changes: 16 additions & 12 deletions src/QRgen/private/DrawedQRCode/print.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const modulePathEnd: string =
const moduleRect: string =
"""<rect class="qrDark qrRounded qrModule"""" &
""" fill="{dark}" x="{float(x)+0.1}" y="{float(y)+0.1}"""" &
""" width="0.8" height="0.8" rx="{moRad:<.3}"></rect>"""
""" width="0.8" height="0.8" rx="{moRadPx:<.3}"></rect>"""

const alignmentPatternRect: string =
"""<rect class="qr{m} qrRounded qrAlignment"""" &
Expand All @@ -72,12 +72,12 @@ func roundedRect(x, y, w, h: uint8, r: float, m, c: string): string =
fmt(alignmentPatternRect)

template checkRadius(lvl: range[1'i8..2'i8]): float32 =
if alRad == 0: 0f
elif alRad-lvl <= 0: 1f / (lvl * 2)
else: alRad-lvl
if alRadPx == 0: 0f
elif alRadPx-lvl <= 0: 1f / (lvl * 2)
else: alRadPx-lvl

template drawRoundedAlignmentPattern(x, y: uint8): string =
roundedRect(x, y, 7'u8, 7'u8, alRad, "dark", dark) &
roundedRect(x, y, 7'u8, 7'u8, alRadPx, "dark", dark) &
roundedRect(x+1, y+1, 5'u8, 5'u8, checkRadius 1, "light", light) &
roundedRect(x+2, y+2, 3'u8, 3'u8, checkRadius 2, "dark", dark)

Expand Down Expand Up @@ -114,15 +114,16 @@ func printSvg*(
self: DrawedQRCode,
light = "#ffffff",
dark = "#000000",
alRad: range[0f32..3.5f32],
alRad: range[0f32..100f32],
class: string = "qrCode",
id: string = ""
): string =
## Same as `print<#printSvg%2CDrawedQRCode%2Cstring%2Cstring>`_
## but with rounded alignment patterns determined by `alRad` which
## can be from `0` (a square) up to `3.5`, which would make it a perfect
## circle.
## is a percentage (from `0.0` to `100.0`), being `0.0` a square and `100.0`
## a perfect circle.
result = fmt(svgHeader)
let alRadPx: float32 = 3.5 * alRad / 100
result.add fmt(modulePathStart)
drawRegionWithoutAlPatterns modulePath
result.add fmt(modulePathEnd)
Expand All @@ -133,15 +134,18 @@ func printSvg*(
self: DrawedQRCode,
light = "#ffffff",
dark = "#000000",
alRad: range[0f32..3.5f32],
moRad: range[0f32..0.4f32],
alRad: range[0f32..100f32],
moRad: range[0f32..100f32],
class: string = "qrCode",
id: string = ""
): string =
## Same as `print<#printSvg%2CDrawedQRCode%2Cstring%2Cstring%2Crange[]>`_
## but with with rounded modules determined by `moRad` which can be
## from `0` (a square) up to `0.4`, which would make it a perfect circle.
## but with rounded modules determined by `moRad` which is a percentage
## (from `0.0` to `100.0`), being `0.0` a square and `100.0` a perfect circle.
result = fmt(svgHeader)
let
alRadPx: float32 = 3.5 * alRad / 100
moRadPx: float32 = 0.4 * moRad / 100
drawRegionWithoutAlPatterns moduleRect
drawRoundedAlignmentPatterns
result.add svgEnd
5 changes: 2 additions & 3 deletions tests/testQRGen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ benchmarkTest "Testing rounded svg":
let qr = newQR("https://github.com/aruZeta/QRgen")
writeFile(
"build" / "testingRoundedSvg.svg",
qr.printSvg("#1d2021", "#98971a", alRad = 2)
qr.printSvg("#1d2021", "#98971a", alRad = 60)
)

benchmarkTest "Testing very rounded svg":
let qr = newQR("https://github.com/aruZeta/QRgen")
writeFile(
"build" / "testingVeryRoundedSvg.svg",
qr.printSvg("#1d2021", "#98971a", alRad = 3.5, moRad = 0.4)
qr.printSvg("#1d2021", "#98971a", alRad = 100, moRad = 100)
)

0 comments on commit 93bedf4

Please sign in to comment.