Skip to content

Commit b91feac

Browse files
committed
Refactor Code39View for improved barcode rendering
1 parent 3d49849 commit b91feac

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

Sources/Code39/Code39View.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,29 @@ import SwiftUI
99

1010
public struct Code39View: View {
1111

12-
private let barcode: [BarWidth]
12+
public let content: String
1313

1414
public init(_ content: String) {
15-
self.barcode = encodeToCode39(content)
15+
self.content = content
1616
}
1717

1818
public var body: some View {
1919
Canvas { context, size in
20-
let intercharacterGapCount = (barcode.count % 9) - 1
21-
let narrowBarWidth = size.width / CGFloat(((((barcode.count - intercharacterGapCount) / 9) * 15) + intercharacterGapCount))
20+
let prefixSuffixCount = 2
21+
22+
let characterCount = content.count + prefixSuffixCount
23+
24+
let gapCount = content.count + 1
25+
26+
let weightPerCharacter = 15
27+
28+
let totalUnits = (characterCount * weightPerCharacter) + gapCount
29+
30+
let narrowBarWidth = size.width / CGFloat(totalUnits)
31+
2232
var currentX: CGFloat = 0
23-
for (index, barWidth) in barcode.enumerated() {
33+
34+
for (index, barWidth) in encodeToCode39(content).enumerated() {
2435
let barColor: Color = index % 2 == 0 ? .black : .white
2536
let barWidth: CGFloat = barWidth == .narrow ? narrowBarWidth : narrowBarWidth * 3
2637
var barPath = Path()

0 commit comments

Comments
 (0)