From b91feac56059944e5e857c8576bc7708b2cf5b0f Mon Sep 17 00:00:00 2001 From: Jia-Han Wu Date: Wed, 24 Jul 2024 00:29:17 +0800 Subject: [PATCH] Refactor Code39View for improved barcode rendering --- Sources/Code39/Code39View.swift | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Sources/Code39/Code39View.swift b/Sources/Code39/Code39View.swift index 2094a94..77c5dcd 100644 --- a/Sources/Code39/Code39View.swift +++ b/Sources/Code39/Code39View.swift @@ -9,18 +9,29 @@ import SwiftUI public struct Code39View: View { - private let barcode: [BarWidth] + public let content: String public init(_ content: String) { - self.barcode = encodeToCode39(content) + self.content = content } public var body: some View { Canvas { context, size in - let intercharacterGapCount = (barcode.count % 9) - 1 - let narrowBarWidth = size.width / CGFloat(((((barcode.count - intercharacterGapCount) / 9) * 15) + intercharacterGapCount)) + let prefixSuffixCount = 2 + + let characterCount = content.count + prefixSuffixCount + + let gapCount = content.count + 1 + + let weightPerCharacter = 15 + + let totalUnits = (characterCount * weightPerCharacter) + gapCount + + let narrowBarWidth = size.width / CGFloat(totalUnits) + var currentX: CGFloat = 0 - for (index, barWidth) in barcode.enumerated() { + + for (index, barWidth) in encodeToCode39(content).enumerated() { let barColor: Color = index % 2 == 0 ? .black : .white let barWidth: CGFloat = barWidth == .narrow ? narrowBarWidth : narrowBarWidth * 3 var barPath = Path()