// COTTON SITES' LOOK (assets/design/sites/DECISIONS.md "The look", sites.html palette B): Rose by day, Night by dark, // Cranberry the accent, Instrument Sans throughout. Thin 1 pt rules and space; no cards, shadows or gradients. The // accent goes only on the thread, a sheet's main button, "Here" in the switcher and the caret; rows, marks and // switches are ink. Every Sites screen draws from this file, and every value here is sites.html's `:root` or one of // its rules, by name. The colours are the core's `look {app: sites}` as the person's Look stands (`LookModel`). #if os(iOS) import CoreText import CottonWriting import SwiftUI import UIKit /// sites.html's `:root`, as Look stands: the chosen paper's half for the phone's appearance, in the chosen ink. @MainActor enum Cranberry { private static var look: LookModel { LookModel.standing("sites") } /// `--ground`: every Sites screen is drawn on it. static var ground: Color { look.colour("ground", 0xF5F1F1) } static var ink: Color { look.colour("ink", 0x221A1C) } static var quiet: Color { look.colour("quiet", 0x65595C) } static var faint: Color { look.colour("faint", 0x9A8E91) } static var rule: Color { look.colour("rule", 0xDDD3D5) } /// `--acc`: the thread, a sheet's main button, "Here", the caret. Nowhere else. static var accent: Color { look.colour("accent", 0x9A2C4C) } static var onAccent: Color { look.colour("on_accent", 0xFFFFFF) } /// `--veil`: behind a sheet raised over a page. static var veil: Color { ink.opacity(look.groundIsLight ? 0.30 : 0.55) } /// Whether the standing paper is light (keyboards, the status bar). static var light: Bool { look.groundIsLight } /// `.ph` padding: the screen's side margin. static let side: CGFloat = 22 /// A button's and a round's outline (`1.5px solid`). static let stroke: CGFloat = 1.5 } /// Instrument Sans (SIL OFL, `assets/fonts/look`), one variable file, registered once for the process. enum SitesFonts { static let upright: String? = register("InstrumentSans") static let italic: String? = register("InstrumentSans-Italic") private static func register(_ file: String) -> String? { guard let url = Bundle.main.url(forResource: file, withExtension: "ttf", subdirectory: "look") else { return nil } CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil) let descriptors = CTFontManagerCreateFontDescriptorsFromURL(url as CFURL) as? [CTFontDescriptor] ?? [] return descriptors.first.flatMap { CTFontDescriptorCopyAttribute($0, kCTFontNameAttribute) as? String } } /// Instrument Sans at `size` and `weight`, scaled with the phone's text size. static func font(_ size: CGFloat, _ weight: UIFont.Weight, style: UIFont.TextStyle) -> UIFont { let wght: Double = switch weight { case .medium: 500 case .semibold: 600 case .bold, .heavy, .black: 700 default: 400 } let base: UIFont if let name = upright { let variation = UIFontDescriptor.AttributeName(rawValue: kCTFontVariationAttribute as String) base = UIFont(descriptor: UIFontDescriptor(name: name, size: size).addingAttributes([variation: [0x7767_6874: wght]]), size: size) } else { base = .systemFont(ofSize: size, weight: weight) } return UIFontMetrics(forTextStyle: style).scaledFont(for: base) } } /// sites.html's type. Every size scales with the phone's text size and Look's size, up to the largest /// accessibility size; another face chosen in Look stands in for Instrument Sans. @MainActor enum SitesType { static func sans(_ size: CGFloat, _ weight: UIFont.Weight = .regular, _ style: UIFont.TextStyle = .body) -> Font { let look = LookModel.standing("sites") let size = size * look.scale if look.face != LookFaces.own { return Font(WritingFonts.font(LookFaces.drawn(look.face, own: "sans"), size: size, weight: weight)) } return Font(SitesFonts.font(size, weight, style: style)) } /// Instrument Sans itself, whatever Look's face (the Look screen's chip for it). static func own(_ size: CGFloat) -> Font { Font(SitesFonts.font(size, .regular, style: .body)) } /// `.ph h3`: a screen's title. static var title: Font { sans(31, .bold, .largeTitle) } /// `.sheet h3`: a sheet's title. static var sheetTitle: Font { sans(24, .bold, .title2) } /// `.r .t`: a row's name. static var name: Font { sans(17, .semibold, .body) } /// A quiet row's name (Quick visit, Hidden): `font-weight:500`. static var plainName: Font { sans(17, .medium, .body) } /// `.r .s`, `.note`: a second line. static var line: Font { sans(14, .regular, .subheadline) } /// `.ph h4`: a group's heading. static var group: Font { sans(14, .semibold, .subheadline) } /// `body`, `.big`. static var body: Font { sans(17, .regular, .body) } static var bodySmall: Font { sans(16, .regular, .callout) } /// `.btn`. static var button: Font { sans(17, .bold, .body) } static var buttonQuiet: Font { sans(17, .semibold, .body) } /// `.pillx .nm` / `.ad`: the bar's name and address. static var barName: Font { sans(15, .semibold, .subheadline) } static var barAddress: Font { sans(12.5, .regular, .caption1) } /// `.mg`: a site's letter. static func letter(_ size: CGFloat) -> Font { sans(size, .semibold, .footnote) } } /// The words of the `sites` line set (`ShellLines`), the same as Android's. enum SitesWords { static func line(_ path: String, _ fills: [String: String] = [:]) -> String { ShellLines.line("sites", path, fills) } } // MARK: - The page's icons /// sites.html's line icons (``, viewBox 22), drawn at 1.6 in the ink. enum SitesIcon: String { case search, plus, sites, back, next, more, reload, find, key, words, share, copy, move, gear, lock, face, quick, close, open, check, pin, pinon, wifi, rest var path: String { switch self { case .search: "M15.5 9.5a6 6 0 1 1-12 0a6 6 0 1 1 12 0M14 14l5 5" case .plus: "M11 4v14M4 11h14" case .sites: "M4 6.5h14M4 11h14M4 15.5h9" case .back: "M13.5 4.5L7 11l6.5 6.5" case .next: "M8.5 4.5L15 11l-6.5 6.5" case .more: "" case .reload: "M17.5 11a6.5 6.5 0 1 1-2-4.7M16 3.5v3.5h-3.5" case .find: "M4 5h8M4 9h5M4 13h4M18 13a3.5 3.5 0 1 1-7 0a3.5 3.5 0 1 1 7 0M17 15.5l2.5 2.5" case .key: "M10.8 11a3.8 3.8 0 1 1-7.6 0a3.8 3.8 0 1 1 7.6 0M10.8 11h8.2M16.5 11v3M19 11v2" case .words: "M5 3h12v16H5zM8 7.5h6M8 11h4M14.5 13v4" case .share: "M11 3v11M7 7l4-4 4 4M5 11v7h12v-7" case .copy: "M9.5 7.5h6a2 2 0 0 1 2 2v7a2 2 0 0 1-2 2h-6a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2zM14.5 7.5V5.5a2 2 0 0 0-2-2h-6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h1" case .move: "M3.5 7h11M11 3.5L14.5 7 11 10.5M18.5 15h-11M11 11.5L7.5 15l3.5 3.5" case .gear: "M4 6h8M16 6h2M4 16h2M10 16h8M16 6a2 2 0 1 1-4 0a2 2 0 1 1 4 0M10 16a2 2 0 1 1-4 0a2 2 0 1 1 4 0" case .lock: "M6.5 9.5h9a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-9a2 2 0 0 1-2-2v-5a2 2 0 0 1 2-2zM7.5 9.5V7a3.5 3.5 0 0 1 7 0v2.5" case .face: "M3 7V5a2 2 0 0 1 2-2h2M15 3h2a2 2 0 0 1 2 2v2M19 15v2a2 2 0 0 1-2 2h-2M7 19H5a2 2 0 0 1-2-2v-2M8 8v1.5M14 8v1.5M11 8v4h-1M8.5 14.5c0 0 1 1.2 2.5 1.2s2.5-1.2 2.5-1.2" case .quick: "" case .close: "M5.5 5.5l11 11M16.5 5.5l-11 11" case .open: "M12 4h6v6M18 4l-8 8M15 13v5H4V7h5" case .check: "M4.5 11.5l4 4 9-9" case .pin, .pinon: "M8 3.5h6M9 3.5v5L6 12h10l-3-3.5v-5M11 12v6.5" case .wifi: "M3 9a11.5 11.5 0 0 1 16 0M6 12.2a7 7 0 0 1 10 0M8.8 15.2a3 3 0 0 1 4.4 0" case .rest: "M17 13.5A7 7 0 1 1 8.5 5a5.5 5.5 0 0 0 8.5 8.5z" } } } /// One of the page's icons, `size` points square, stroked in the current foreground. struct SitesGlyph: View { let icon: SitesIcon var size: CGFloat = 22 var weight: CGFloat = 1.6 /// Grows with the phone's text size (to twice at the largest), as the words beside it do. @ScaledMetric(relativeTo: .body) private var grow: CGFloat = 1 var body: some View { let size = size * min(max(grow, 1), 2) Canvas { context, box in let scale = box.width / 22 let transform = CGAffineTransform(scaleX: scale, y: scale) let style = StrokeStyle(lineWidth: weight, lineCap: .round, lineJoin: .round) let shading = GraphicsContext.Shading.foreground switch icon { case .more: for x in [5.0, 11, 17] { context.fill(Path(ellipseIn: CGRect(x: x - 1.4, y: 9.6, width: 2.8, height: 2.8)).applying(transform), with: shading) } case .quick: context.stroke(Path(ellipseIn: CGRect(x: 3.5, y: 3.5, width: 15, height: 15)).applying(transform), with: shading, style: StrokeStyle(lineWidth: weight, lineCap: .round, dash: [2.2 * scale, 2.6 * scale])) case .pinon: let path = SVGPath.parse(icon.path).applying(transform) context.fill(SVGPath.parse("M8 3.5h6M9 3.5v5L6 12h10l-3-3.5v-5z").applying(transform), with: shading) context.stroke(path, with: shading, style: style) case .wifi: context.stroke(SVGPath.parse(icon.path).applying(transform), with: shading, style: style) context.fill(Path(ellipseIn: CGRect(x: 10.2, y: 17.2, width: 1.6, height: 1.6)).applying(transform), with: shading) default: context.stroke(SVGPath.parse(icon.path).applying(transform), with: shading, style: style) } } .frame(width: size, height: size) .accessibilityHidden(true) } } /// The few SVG path commands sites.html's icons use (M L H V C S A Z, absolute and relative), as a SwiftUI `Path`. enum SVGPath { static func parse(_ d: String) -> Path { var path = Path() var tokens: [String] = [] var number = "" func flush() { if !number.isEmpty { tokens.append(number); number = "" } } for ch in d { if ch.isLetter && ch != "e" { flush(); tokens.append(String(ch)) } else if ch == "-" && !number.isEmpty && number.last != "e" { flush(); number = "-" } else if ch == "," || ch == " " { flush() } else if ch == "." && number.contains(".") { flush(); number = "." } else { number.append(ch) } } flush() var i = 0, command: Character = "M" var current = CGPoint.zero, start = CGPoint.zero, lastControl: CGPoint? func next() -> CGFloat { defer { i += 1 }; return i < tokens.count ? CGFloat(Double(tokens[i]) ?? 0) : 0 } while i < tokens.count { if let c = tokens[i].first, c.isLetter { command = c; i += 1; if c == "z" || c == "Z" { path.closeSubpath(); current = start; lastControl = nil; continue } } let relative = command.isLowercase func point(_ x: CGFloat, _ y: CGFloat) -> CGPoint { relative ? CGPoint(x: current.x + x, y: current.y + y) : CGPoint(x: x, y: y) } switch command.uppercased().first! { case "M": current = point(next(), next()); start = current; path.move(to: current) command = relative ? "l" : "L"; lastControl = nil case "L": current = point(next(), next()); path.addLine(to: current); lastControl = nil case "H": let x = next(); current = CGPoint(x: relative ? current.x + x : x, y: current.y); path.addLine(to: current); lastControl = nil case "V": let y = next(); current = CGPoint(x: current.x, y: relative ? current.y + y : y); path.addLine(to: current); lastControl = nil case "C": let c1 = point(next(), next()), c2 = point(next(), next()), end = point(next(), next()) path.addCurve(to: end, control1: c1, control2: c2); current = end; lastControl = c2 case "S": let c1 = lastControl.map { CGPoint(x: 2 * current.x - $0.x, y: 2 * current.y - $0.y) } ?? current let c2 = point(next(), next()), end = point(next(), next()) path.addCurve(to: end, control1: c1, control2: c2); current = end; lastControl = c2 case "A": let rx = next(), ry = next(), angle = next(), large = next() != 0, sweep = next() != 0 let end = point(next(), next()) arc(&path, from: current, to: end, rx: rx, ry: ry, angle: angle, large: large, sweep: sweep) current = end; lastControl = nil default: i += 1 } } return path } /// An SVG elliptical arc (endpoint form) as cubic curves, by the SVG spec's centre conversion. private static func arc(_ path: inout Path, from p1: CGPoint, to p2: CGPoint, rx: CGFloat, ry: CGFloat, angle: CGFloat, large: Bool, sweep: Bool) { var rx = abs(rx), ry = abs(ry) guard rx > 0, ry > 0, p1 != p2 else { path.addLine(to: p2); return } let phi = angle * .pi / 180, c = cos(phi), s = sin(phi) let dx = (p1.x - p2.x) / 2, dy = (p1.y - p2.y) / 2 let x1 = c * dx + s * dy, y1 = -s * dx + c * dy let lambda = x1 * x1 / (rx * rx) + y1 * y1 / (ry * ry) if lambda > 1 { rx *= sqrt(lambda); ry *= sqrt(lambda) } let num = rx * rx * ry * ry - rx * rx * y1 * y1 - ry * ry * x1 * x1 let den = rx * rx * y1 * y1 + ry * ry * x1 * x1 var co = sqrt(max(0, num / den)); if large == sweep { co = -co } let cx1 = co * rx * y1 / ry, cy1 = -co * ry * x1 / rx let cx = c * cx1 - s * cy1 + (p1.x + p2.x) / 2, cy = s * cx1 + c * cy1 + (p1.y + p2.y) / 2 func angleOf(_ ux: CGFloat, _ uy: CGFloat, _ vx: CGFloat, _ vy: CGFloat) -> CGFloat { let a = atan2(ux * vy - uy * vx, ux * vx + uy * vy); return a } let theta1 = angleOf(1, 0, (x1 - cx1) / rx, (y1 - cy1) / ry) var delta = angleOf((x1 - cx1) / rx, (y1 - cy1) / ry, (-x1 - cx1) / rx, (-y1 - cy1) / ry) if !sweep && delta > 0 { delta -= 2 * .pi } else if sweep && delta < 0 { delta += 2 * .pi } let pieces = Int(ceil(abs(delta) / (.pi / 2))) let step = delta / CGFloat(pieces) let k = 4 / 3 * tan(step / 4) var t = theta1 func on(_ a: CGFloat) -> CGPoint { CGPoint(x: cx + rx * cos(a) * c - ry * sin(a) * s, y: cy + rx * cos(a) * s + ry * sin(a) * c) } func d(_ a: CGFloat) -> CGPoint { CGPoint(x: -rx * sin(a) * c - ry * cos(a) * s, y: -rx * sin(a) * s + ry * cos(a) * c) } for _ in 0..