diff --git a/Sources/Internal/RCDrawer.swift b/Sources/Internal/RCDrawer.swift index 58adbe5..dee2c65 100644 --- a/Sources/Internal/RCDrawer.swift +++ b/Sources/Internal/RCDrawer.swift @@ -39,29 +39,39 @@ class RCDrawer { UIColor.white.setFill() context.fill(rect) } - self.image.tintColor.setFill() - self.image.tintColor.setStroke() cgContext.translateBy(x: self.image.contentInsets.left, y: self.image.contentInsets.top) - let size = self.image.size - self.image.size * RCConstants.dotSizeScale - let halfSize = size / 2 - [(halfSize, 0), (size, halfSize), (halfSize, size), (0, halfSize)].map({CGPoint(x: $0.0, y: $0.1)}).forEach { point in - cgContext.saveGState() - cgContext.translateBy(x: point.x, y: point.y) - drawDot() - cgContext.restoreGState() - } + cgContext.saveGState() cgContext.saveGState() let imageTranslation = self.image.size * (1 - RCConstants.imageScale) / 2 cgContext.translateBy(x: imageTranslation, y: imageTranslation) cgContext.scaleBy(x: RCConstants.imageScale, y: RCConstants.imageScale) drawImage() cgContext.restoreGState() - for value in self.image.bits.enumerated() { - cgContext.saveGState() - cgContext.translateBy(x: self.image.size / 2, y: self.image.size / 2) - cgContext.rotate(by: CGFloat(value.offset - 1) * CGFloat.pi / 2) - drawMessage(group: value.element) - cgContext.restoreGState() + var adjustedRect = rect + adjustedRect.origin.x = -self.image.contentInsets.left + adjustedRect.origin.y = -self.image.contentInsets.top + let rectPath = UIBezierPath(rect: adjustedRect) + rectPath.addClip() + rectPath.usesEvenOddFillRule = true + let mainPath = UIBezierPath() + let size = self.image.size - self.image.size * RCConstants.dotSizeScale + let halfSize = size / 2 + [(halfSize, 0), (size, halfSize), (halfSize, size), (0, halfSize)].map({CGPoint(x: $0.0, y: $0.1)}).forEach { point in + drawDot(position: point, path: mainPath) + } + self.image.bits.enumerated().forEach { value in + drawMessage(group: value.element, angle: CGFloat(value.offset - 1) * CGFloat.pi / 2, path: mainPath) + } + mainPath.usesEvenOddFillRule = true + mainPath.addClip() + let colorSpace = CGColorSpaceCreateDeviceRGB() + let gradient = CGGradient(colorsSpace: colorSpace, colors: self.image.tintColors.map({$0.cgColor}) as CFArray, locations: nil)! + switch self.image.gradientType { + case .linear: + cgContext.drawLinearGradient(gradient, start: .zero, end: CGPoint(x: self.image.size, y: self.image.size), options: [.drawsBeforeStartLocation, .drawsAfterEndLocation]) + case .radial: + let startPoint = CGPoint(x: self.image.size / 2, y: self.image.size / 2) + cgContext.drawRadialGradient(gradient, startCenter: startPoint, startRadius: self.image.size * (1 - RCConstants.dotSizeScale) / 2, endCenter: startPoint, endRadius: self.image.size / 2, options: [.drawsBeforeStartLocation, .drawsAfterEndLocation]) } } return image @@ -69,17 +79,14 @@ class RCDrawer { } private extension RCDrawer { - func drawDot() { - let dot = UIBezierPath() + func drawDot(position: CGPoint, path: UIBezierPath) { let patternSingleSize = image.size * RCConstants.dotSizeScale / RCConstants.dotPatterns.max()! let patternSizes = RCConstants.dotPatterns.map({$0 * patternSingleSize}) - for value in patternSizes.enumerated() { - let path = UIBezierPath(ovalIn: CGRect(x: (patternSizes[0] - value.element) / 2, y: (patternSizes[0] - value.element) / 2, width: value.element, height: value.element)) - path.close() - value.element != patternSizes.last! ? dot.append(path) : path.fill() + patternSizes.enumerated().forEach { value in + let dotPath = UIBezierPath(ovalIn: CGRect(x: position.x + (patternSizes[0] - value.element) / 2, y: position.y + (patternSizes[0] - value.element) / 2, width: value.element, height: value.element)) + dotPath.close() + path.append(dotPath) } - dot.usesEvenOddFillRule = true - dot.fill() } func drawImage() { @@ -96,27 +103,28 @@ private extension RCDrawer { image.draw(in: scaledImageRect, blendMode: .normal, alpha: 1) } - func drawMessage(group: [[RCBit]]) { + func drawMessage(group: [[RCBitGroup]], angle: CGFloat, path: UIBezierPath) { guard !image.message.isEmpty else { return } let lineWidth = image.size * RCConstants.dotSizeScale / 11 * 2 //number of lines including spaces let mainRadius = (self.image.size - lineWidth) / 2 - let path = UIBezierPath() let rowRadius = group.indices.map({mainRadius - lineWidth * CGFloat($0 * 2)}) - let startAngle = asin(self.image.size * RCConstants.dotSizeScale / mainRadius) - let distancePerBit = (CGFloat.pi / 2 - startAngle * 2) / CGFloat(group.first!.count) + var startAngle = asin(self.image.size * RCConstants.dotSizeScale / mainRadius) + let distancePerBit = (CGFloat.pi / 2 - startAngle * 2) / CGFloat(RCConstants.level1BitesCount) + let center = CGPoint(x: self.image.size / 2, y: self.image.size / 2) + startAngle += angle zip(rowRadius, group).forEach { - let bits = $0.1 + let rowBitsGroup = $0.1 let radius = $0.0 - for (offset, bit) in bits.enumerated() { - guard bit.boolValue else { continue } - let startPosition = startAngle + distancePerBit * CGFloat(offset) - let endPosition = startPosition + distancePerBit - let linePath = UIBezierPath(arcCenter: .zero, radius: radius, startAngle: startPosition, endAngle: endPosition, clockwise: true) - path.append(linePath) + rowBitsGroup.forEach { bit in + guard bit.bit.boolValue else { return } + let startPosition = startAngle + distancePerBit * CGFloat(bit.offset) + let endPosition = startPosition + distancePerBit * CGFloat(bit.count) + let linePath = UIBezierPath(arcCenter: center, radius: radius, startAngle: startPosition, endAngle: endPosition, clockwise: true) + let cgPath = CGPath(__byStroking: linePath.cgPath, transform: nil, lineWidth: lineWidth, lineCap: .round, lineJoin: .round, miterLimit: 1.0)! + let combinedPath = UIBezierPath(cgPath: cgPath) + path.append(combinedPath) } } - path.lineWidth = lineWidth - path.lineCapStyle = .round - path.stroke() } } + diff --git a/Sources/Public/RCCoder.swift b/Sources/Public/RCCoder.swift index 1c24af6..44bce59 100644 --- a/Sources/Public/RCCoder.swift +++ b/Sources/Public/RCCoder.swift @@ -33,17 +33,15 @@ public final class RCCoder { public extension RCCoder { func encode(_ image: RCImage) throws -> UIImage { - let bits = try encode(message: image.message) - let bitChunks = stride(from: 0, to: bits.count, by: bits.count / 4).map({Array(bits[$0 ..< min($0 + bits.count / 4, bits.count)])}) - image.bits = bitChunks.map { chunk -> [[RCBit]] in - return [0.. String { - return try decode(data.bits.flatMap({$0.flatMap({$0})})) + func decode(_ image: UIImage) throws -> String { + let bits = try RCImageDecoder().decode(image) + let message = try decode(bits) + return message } func validate(_ text: String) -> Bool { @@ -52,7 +50,7 @@ public extension RCCoder { } extension RCCoder { - private func encode(message: String) throws -> [RCBit] { + private func encode(message: String) throws -> [[[RCBitGroup]]] { guard message.trimmingCharacters(in: configuration.characterSet).isEmpty else { throw RCCoderError.invalidCharacter } @@ -71,10 +69,28 @@ extension RCCoder { }.flatMap({$0}) var totalBits = [RCBit](repeating: .zero, count: RCConstants.maxBites) totalBits.insert(contentsOf: encodedBits, at: 0) - return Array(totalBits.prefix(RCConstants.maxBites)) + totalBits = Array(totalBits.prefix(RCConstants.maxBites)) + let bitChunks = [RCConstants.level1BitesCount, RCConstants.level2BitesCount, RCConstants.level3BitesCount].map { count in + return (0...3).map { _ -> [RCBit] in // 4 groups + let bitChunk = Array(totalBits.prefix(count)) + totalBits = Array(totalBits.dropFirst(count)) + return bitChunk + } + } + let bitGroups = zip(bitChunks[0], zip(bitChunks[1], bitChunks[2])).map {[$0.0, $0.1.0, $0.1.1]}.map { group in + group.map { row -> [RCBitGroup] in + var bitGroupRow = [RCBitGroup(bit: row[0], count: 0, offset: 0)] + row.enumerated().forEach { value in + bitGroupRow.last!.bit == value.element ? bitGroupRow.last!.count += 1 : bitGroupRow.append(RCBitGroup(bit: value.element, count: 1, offset: value.offset)) + } + return bitGroupRow + } + } + return bitGroups } private func decode(_ bits: [RCBit]) throws -> String { + guard bits.contains(.one) else { return "" } let emptyIndexes = configuration.emptySymbolsIndex() let bitChunks = stride(from: 0, to: bits.count, by: configuration.bitesPerSymbol).map { Array(bits[$0 ..< min($0 + configuration.bitesPerSymbol, bits.count)]) @@ -97,6 +113,7 @@ public extension RCCoder { case invalidCharacter case longText case wrongConfiguration + case decoding var localizedDescription: String { switch self { @@ -106,6 +123,8 @@ public extension RCCoder { return "message characters count exceeds configuration maximum characters" case .wrongConfiguration: return "Error decoding. The configuration is not matching to encoded image" + case .decoding: + return "Error decoding." } } }