Files
sst/Sources/SstUI/TextStyle.swift
T

79 lines
2.4 KiB
Swift

import SwiftUI
public struct TextStyle: Equatable {
public let family: FontFamily
public let size: CGFloat
public let weight: Int
/// In em.
public let tracking: CGFloat
public let line: CGFloat
public let caps: Bool
public let tabular: Bool
public init(family: FontFamily, size: CGFloat, weight: Int, tracking: CGFloat, line: CGFloat, caps: Bool, tabular: Bool) {
self.family = family
self.size = size
self.weight = weight
self.tracking = tracking
self.line = line
self.caps = caps
self.tabular = tabular
}
var fontWeight: Font.Weight {
switch weight {
case ..<450: .regular
case 450..<550: .medium
case 550..<650: .semibold
default: .bold
}
}
public var font: Font {
let base: Font
switch family {
case .mono: base = .system(size: size, weight: fontWeight, design: .monospaced)
case .text: base = .system(size: size, weight: fontWeight)
case .chrome: base = .custom(family.rawValue, size: size).weight(fontWeight)
}
return tabular ? base.monospacedDigit() : base
}
}
public extension View {
/// Font, tracking, line spacing and capitalisation from a token text style.
func textStyle(_ style: TextStyle) -> some View {
self.font(style.font)
.tracking(style.tracking * style.size)
.lineSpacing(max(0, style.line - style.size * 1.2))
.textCase(style.caps ? .uppercase : nil)
}
}
/// One layer of a layered shadow token. Inset layers are drawn inside the shape.
public struct ShadowLayer: Equatable {
public let inset: Bool
public let x: CGFloat
public let y: CGFloat
public let blur: CGFloat
public let spread: CGFloat
public let hex: UInt32
public let alphaLight: Double
public let alphaDark: Double
public init(inset: Bool, x: CGFloat, y: CGFloat, blur: CGFloat, spread: CGFloat, hex: UInt32, alphaLight: Double, alphaDark: Double) {
self.inset = inset
self.x = x
self.y = y
self.blur = blur
self.spread = spread
self.hex = hex
self.alphaLight = alphaLight
self.alphaDark = alphaDark
}
public func color(_ scheme: ColorScheme) -> Color {
Color.fixed(hex: hex).opacity(scheme == .dark ? alphaDark : alphaLight)
}
}