QR in real CMYK / grayscale

It is possible to get the path (set of rectangles) from SVG, parse it and draw it again in CMYK.

#import "@preview/tiaoma:0.3.0" : qrcode
#let black_cmyk_qrcode(text, width: 1.5cm, color: cmyk(0%, 0%, 0%, 100%)) = {
  let svg-bytes = qrcode(text).source
  let qr_path = str(svg-bytes).match(regex("<path d=\"([^\"]*)")).captures.at(0) // get the path part of code from SVG
  let qr_rectangles_text = qr_path.matches(regex("M(\d+) (\d+)h(\d+)v(\d+)h-\d+Z"))
  let qr_rectangles_coord = ()
  for qr in qr_rectangles_text {
    qr_rectangles_coord.push(("x", "y", "width", "height").zip(qr.captures.map(x => int(x))).to-dict())
  }
  let tiles = calc.max(..qr_rectangles_coord.map(x => (x.x + x.width)))
  let tile_size = width/tiles
  rect(width:width, height: width, inset: 0mm, stroke: 0pt,
    {
    for r in qr_rectangles_coord {
        place(dx: r.x*tile_size, dy: r.y*tile_size, rect(height: r.height*tile_size, width: r.width*tile_size, fill: color))
    }}
  )
}
1 Like