// // ScriptPolicy.swift // App // // Created by James Magahern on 9/29/21. // import UIKit struct ScriptPolicy: Hashable { enum PolicyType: Int, CaseIterable { case alpha case bravo case charlie case delta case echo } public let policyType: PolicyType public let securityOrigin: String public static func title(forPolicyType type: PolicyType) -> String { switch type { case .alpha: return "Alpha" case .bravo: return "Bravo" case .charlie: return "Charlie" case .delta: return "Delta" case .echo: return "Echo" } } public static func localizedDescription(forPolicyType type: PolicyType) -> String { switch type { case .alpha: return "All scripts blocked." case .bravo: return "Scripts on page are allowed." case .charlie: return "Allow scripts from the same security origin." case .delta: return "Allow scripts from common and host CDNs." case .echo: return "All scripts are allowed." } } public static func iconRepresentation(forPolicyType type: PolicyType, size: CGSize) -> UIImage? { let font = UIFont.boldSystemFont(ofSize: size.height - 2) let attrs: [NSAttributedString.Key : Any] = [ .font : font, .foregroundColor: UIColor.white, ] let rect = CGRect(origin: .zero, size: size) UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale) if let _ = UIGraphicsGetCurrentContext() { let backgroundPath = UIBezierPath.init(roundedRect: rect, cornerRadius: 4.0) // backgroundPath.usesEvenOddFillRule = true backgroundPath.fill() let character = NSString(string: { () -> String in switch type { case .alpha: return "𝝰" case .bravo: return "𝝱" case .charlie: return "𝝲" case .delta: return "𝝳" case .echo: return "𝝴" } }()) let charSize = character.size(withAttributes: attrs) let charRect = CGRect(origin: .init(x: (size.width - charSize.width) / 2 , y: -(charSize.height - size.height) / 2), size: charSize) character.draw(in: charRect, withAttributes: attrs) } let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } }