Repository Public
1// SP Notes Core2// File: Sources/SPNotesDesign/NVFont.swift3// Description: Note Visual System 2.0 typography: UI sans, editorial serif for reading titles, mono; exact point sizes of4// the shared scale, scaled with Dynamic Type.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SwiftUI9#if canImport(UIKit)10import UIKit11#endif1213/// Typography of the Note Visual System 2.0. Prefer `View.nvFont(_:)`, which follows Dynamic Type; the static14/// `NVFont.fixed(_:)` fonts have the exact base size (macOS chrome, exports, snapshots).15public enum NVFont {16 /// A text role of the scale.17 public enum Style: String, Sendable, Hashable, CaseIterable {18 /// 11 pt labels, badges, metadata.19 case micro20 /// 13 pt interface text.21 case ui22 /// 13 pt semibold interface text.23 case uiStrong24 /// 16 pt body.25 case body26 /// Reading body in the editorial serif: 17 pt on iPhone, 16 pt elsewhere.27 case reading28 /// 13 pt monospaced code.29 case code30 /// Serif reading titles.31 case h3, h2, h1, display3233 /// Base size in points. `compact` = iPhone (only `reading` differs).34 public func size(compact: Bool = false) -> CGFloat {35 switch self {36 case .micro: return 1137 case .ui, .uiStrong, .code: return 1338 case .body: return 1639 case .reading: return compact ? 17 : 1640 case .h3: return 2041 case .h2: return 2442 case .h1: return 3043 case .display: return 4044 }45 }4647 public var weight: Font.Weight {48 switch self {49 case .micro: return .medium50 case .ui, .body, .reading, .code: return .regular51 case .uiStrong, .h3, .h2: return .semibold52 case .h1, .display: return .bold53 }54 }5556 /// Serif for reading titles and reading body, monospaced for code, the system sans otherwise.57 public var design: Font.Design {58 switch self {59 case .h3, .h2, .h1, .display, .reading: return .serif60 case .code: return .monospaced61 case .micro, .ui, .uiStrong, .body: return .default62 }63 }6465 /// The Dynamic Type text style the size scales with.66 public var textStyle: Font.TextStyle {67 switch self {68 case .micro: return .caption269 case .ui, .uiStrong, .code: return .footnote70 case .body, .reading: return .body71 case .h3: return .title372 case .h2: return .title273 case .h1: return .title74 case .display: return .largeTitle75 }76 }77 }7879 /// The font of a style at an explicit size (already scaled by the caller, or the base size).80 public static func font(_ style: Style, size: CGFloat) -> Font {81 .system(size: size, weight: style.weight, design: style.design)82 }8384 /// The font at its exact base size (does not follow Dynamic Type).85 public static func fixed(_ style: Style, compact: Bool = false) -> Font { font(style, size: style.size(compact: compact)) }8687 /// Whether the current device uses the compact (iPhone) reading size.88 @MainActor public static var isCompactDevice: Bool {89 #if os(iOS)90 return UIDevice.current.userInterfaceIdiom == .phone91 #else92 return false93 #endif94 }95}9697/// Applies a style of the scale, scaled with Dynamic Type relative to its text style.98public struct NVFontModifier: ViewModifier {99 private let style: NVFont.Style100 @ScaledMetric private var size: CGFloat101102 public init(_ style: NVFont.Style) {103 self.style = style104 _size = ScaledMetric(wrappedValue: style.size(compact: NVFont.isCompactDevice), relativeTo: style.textStyle)105 }106107 public func body(content: Content) -> some View { content.font(NVFont.font(style, size: size)) }108}109110extension View {111 /// Sets the font to a style of the Note Visual System scale (Dynamic Type aware).112 public func nvFont(_ style: NVFont.Style) -> some View { modifier(NVFontModifier(style)) }113}114