QRCodeRenderer

struct QRCodeRenderer

Renders a QR code as a PNG, optionally in Twitarr style: outer corners of each connected shape rounded, and an “ARR” icon in the center built out of half-size blocks.

This rasterizes the image itself (via SwiftarrImage for PNG encoding) rather than using Core Graphics, because Linux has no access to it.

Yes this was some vibe-coded nonsense. What gave it away?

  • Round the outer corners of each connected shape of blocks.

    Declaration

    Swift

    var rounded: Bool
  • Put the “ARR” icon in the middle of the code.

    Declaration

    Swift

    var icon: Bool
  • Pixels per QR module. Must be even, as the icon is built from half-size blocks.

    Declaration

    Swift

    var moduleSize: Int
  • Width of the blank border, in modules. The QR spec calls for 4.

    Declaration

    Swift

    var quietZone: Int
  • Declaration

    Swift

    struct Output
  • Codes smaller than this don’t have enough redundancy to give up the center, and codes at or above maxIconModules are denser than we’ve verified. 29 is a version 3 code; 46 is just past version 7 (45 modules), the largest we do. Version 7 has an alignment pattern dead center that the icon erases; with level H error correction and its other alignment patterns it still decodes (checked with OpenCV’s detector, at full and quarter size).

    Declaration

    Swift

    static let minIconModules: Int
  • Declaration

    Swift

    static let maxIconModules: Int
  • Declaration

    Swift

    private static let glyphA: [String]
  • Declaration

    Swift

    private static let glyphR: [String]
  • Declaration

    Swift

    private static let iconGlyphs: [[String]]
  • Declaration

    Swift

    private static let iconWidth: Int
  • Declaration

    Swift

    private static let iconHeight: Int
  • A grid of square blocks, rasterized with the outer corners of connected shapes optionally rounded.

    See more

    Declaration

    Swift

    private struct BlockLayer
  • Renders text as a PNG QR code.

    See more

    Declaration

    Swift

    func render(_ text: String) throws -> Output
  • Clears the center of the module grid and returns the icon as a grid of half-size blocks (2n x 2n), centered on the code.

    Declaration

    Swift

    private static func placeIcon(in modules: inout [[Bool]]) -> [[Bool]]