SPB Git

Repository Public

Properties.swift 13.6 KB 305 lines swift

1// ORIX Docs2// File: Packages/ORIXDocsKit/Sources/DocModel/Properties.swift3// Description: Primitive value types and the character / paragraph property sets. Every field of a property set is optional: nil means "inherit".4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation89/// All lengths in the model are PostScript points (1/72 inch).10public typealias Points = Double1112public enum DocID {13    /// A short unique identifier (22 hex characters would be overkill; 12 are plenty inside one document).14    public static func make() -> String {15        var g = SystemRandomNumberGenerator()16        return String(g.next() & 0xFFFF_FFFF_FFFF, radix: 36)17    }18}1920public struct RGBA: Codable, Hashable, Sendable {21    public var r: Double, g: Double, b: Double, a: Double22    public init(r: Double, g: Double, b: Double, a: Double = 1) { self.r = r; self.g = g; self.b = b; self.a = a }23    public init(hex: UInt32, alpha: Double = 1) {24        self.init(r: Double((hex >> 16) & 0xFF) / 255, g: Double((hex >> 8) & 0xFF) / 255, b: Double(hex & 0xFF) / 255, a: alpha)25    }26    /// Parses "RRGGBB" or "#RRGGBB".27    public init?(hexString: String) {28        let s = hexString.hasPrefix("#") ? String(hexString.dropFirst()) : hexString29        guard s.count == 6, let v = UInt32(s, radix: 16) else { return nil }30        self.init(hex: v)31    }32    public var hexString: String {33        func c(_ v: Double) -> Int { max(0, min(255, Int((v * 255).rounded()))) }34        return String(format: "%02X%02X%02X", c(r), c(g), c(b))35    }36    public static let black = RGBA(hex: 0x000000)37    public static let white = RGBA(hex: 0xFFFFFF)38}3940public struct Insets: Codable, Hashable, Sendable {41    public var top: Points, left: Points, bottom: Points, right: Points42    public init(top: Points = 0, left: Points = 0, bottom: Points = 0, right: Points = 0) { self.top = top; self.left = left; self.bottom = bottom; self.right = right }43    public init(all v: Points) { self.init(top: v, left: v, bottom: v, right: v) }44    public static let zero = Insets()45}4647public enum VerticalPosition: String, Codable, Sendable { case baseline, superscript, `subscript` }48public enum TextAlignment: String, Codable, Sendable { case left, center, right, justified }4950public enum LineSpacing: Codable, Hashable, Sendable {51    /// A multiple of the natural line height (1.0 = single).52    case multiple(Double)53    /// An exact line height in points.54    case exact(Points)55    /// A minimum line height in points.56    case atLeast(Points)57}5859public struct TabStop: Codable, Hashable, Sendable {60    public enum Kind: String, Codable, Sendable { case left, center, right, decimal }61    public enum Leader: String, Codable, Sendable { case none, dots, dashes, line }62    public var position: Points63    public var kind: Kind64    public var leader: Leader65    public init(position: Points, kind: Kind = .left, leader: Leader = .none) { self.position = position; self.kind = kind; self.leader = leader }66}6768public struct BorderLine: Codable, Hashable, Sendable {69    public enum Style: String, Codable, Sendable { case solid, dashed, dotted, double }70    public var width: Points71    public var color: RGBA72    public var style: Style73    public init(width: Points = 0.5, color: RGBA = .black, style: Style = .solid) { self.width = width; self.color = color; self.style = style }74}7576/// Character-level formatting. nil = inherit from the style chain.77public struct CharProps: Codable, Hashable, Sendable {78    public var fontFamily: String?79    public var fontSize: Points?80    public var bold: Bool?81    public var italic: Bool?82    /// CSS-like weight 100...900 for families that offer more than regular/bold (and variable fonts). Overrides `bold` when set.83    public var weight: Int?84    public var underline: Bool?85    public var strikethrough: Bool?86    public var color: RGBA?87    public var highlight: RGBA?88    public var vertical: VerticalPosition?89    /// Extra baseline shift in points (positive = up).90    public var baselineShift: Points?91    /// Extra space between characters in points.92    public var tracking: Points?93    public var kerning: Bool?94    public var ligatures: Bool?95    public var smallCaps: Bool?96    public var allCaps: Bool?97    /// Hyperlink target (URL string, or "#bookmark" for an internal link).98    public var link: String?99    /// BCP 47 language tag used for spell checking and hyphenation.100    public var language: String?101102    public init(fontFamily: String? = nil, fontSize: Points? = nil, bold: Bool? = nil, italic: Bool? = nil, weight: Int? = nil,103                underline: Bool? = nil, strikethrough: Bool? = nil, color: RGBA? = nil, highlight: RGBA? = nil,104                vertical: VerticalPosition? = nil, baselineShift: Points? = nil, tracking: Points? = nil, kerning: Bool? = nil,105                ligatures: Bool? = nil, smallCaps: Bool? = nil, allCaps: Bool? = nil, link: String? = nil, language: String? = nil) {106        self.fontFamily = fontFamily; self.fontSize = fontSize; self.bold = bold; self.italic = italic; self.weight = weight107        self.underline = underline; self.strikethrough = strikethrough; self.color = color; self.highlight = highlight108        self.vertical = vertical; self.baselineShift = baselineShift; self.tracking = tracking; self.kerning = kerning109        self.ligatures = ligatures; self.smallCaps = smallCaps; self.allCaps = allCaps; self.link = link; self.language = language110    }111112    public static let none = CharProps()113    public var isEmpty: Bool { self == .none }114115    /// Returns `self` with every nil field filled from `base`.116    public func over(_ base: CharProps) -> CharProps {117        var o = base118        o.apply(self)119        return o120    }121122    /// Overwrites the fields that `other` defines.123    public mutating func apply(_ other: CharProps) {124        if let v = other.fontFamily { fontFamily = v }125        if let v = other.fontSize { fontSize = v }126        if let v = other.bold { bold = v }127        if let v = other.italic { italic = v }128        if let v = other.weight { weight = v }129        if let v = other.underline { underline = v }130        if let v = other.strikethrough { strikethrough = v }131        if let v = other.color { color = v }132        if let v = other.highlight { highlight = v }133        if let v = other.vertical { vertical = v }134        if let v = other.baselineShift { baselineShift = v }135        if let v = other.tracking { tracking = v }136        if let v = other.kerning { kerning = v }137        if let v = other.ligatures { ligatures = v }138        if let v = other.smallCaps { smallCaps = v }139        if let v = other.allCaps { allCaps = v }140        if let v = other.link { link = v }141        if let v = other.language { language = v }142    }143}144145/// Paragraph-level formatting. nil = inherit from the style chain.146public struct ParaProps: Codable, Hashable, Sendable {147    public var alignment: TextAlignment?148    public var indentLeft: Points?149    public var indentRight: Points?150    /// Positive = first-line indent, negative = hanging indent.151    public var firstLineIndent: Points?152    public var spaceBefore: Points?153    public var spaceAfter: Points?154    public var lineSpacing: LineSpacing?155    public var keepWithNext: Bool?156    public var keepLinesTogether: Bool?157    public var pageBreakBefore: Bool?158    /// Widow and orphan control (at least two lines on each side of a page break).159    public var widowControl: Bool?160    public var tabs: [TabStop]?161    public var hyphenate: Bool?162    /// 1...9 for headings; nil or 0 for body text. Drives the outline, the table of contents and heading numbering.163    public var outlineLevel: Int?164    /// Number of lines spanned by a drop cap on the first character (0 or nil = none).165    public var dropCapLines: Int?166    public var shading: RGBA?167    public var borderTop: BorderLine?168    public var borderBottom: BorderLine?169    public var borderLeft: BorderLine?170    public var rightToLeft: Bool?171172    public init(alignment: TextAlignment? = nil, indentLeft: Points? = nil, indentRight: Points? = nil, firstLineIndent: Points? = nil,173                spaceBefore: Points? = nil, spaceAfter: Points? = nil, lineSpacing: LineSpacing? = nil, keepWithNext: Bool? = nil,174                keepLinesTogether: Bool? = nil, pageBreakBefore: Bool? = nil, widowControl: Bool? = nil, tabs: [TabStop]? = nil,175                hyphenate: Bool? = nil, outlineLevel: Int? = nil, dropCapLines: Int? = nil, shading: RGBA? = nil,176                borderTop: BorderLine? = nil, borderBottom: BorderLine? = nil, borderLeft: BorderLine? = nil, rightToLeft: Bool? = nil) {177        self.alignment = alignment; self.indentLeft = indentLeft; self.indentRight = indentRight; self.firstLineIndent = firstLineIndent178        self.spaceBefore = spaceBefore; self.spaceAfter = spaceAfter; self.lineSpacing = lineSpacing; self.keepWithNext = keepWithNext179        self.keepLinesTogether = keepLinesTogether; self.pageBreakBefore = pageBreakBefore; self.widowControl = widowControl; self.tabs = tabs180        self.hyphenate = hyphenate; self.outlineLevel = outlineLevel; self.dropCapLines = dropCapLines; self.shading = shading181        self.borderTop = borderTop; self.borderBottom = borderBottom; self.borderLeft = borderLeft; self.rightToLeft = rightToLeft182    }183184    public static let none = ParaProps()185    public var isEmpty: Bool { self == .none }186187    public func over(_ base: ParaProps) -> ParaProps {188        var o = base189        o.apply(self)190        return o191    }192193    public mutating func apply(_ other: ParaProps) {194        if let v = other.alignment { alignment = v }195        if let v = other.indentLeft { indentLeft = v }196        if let v = other.indentRight { indentRight = v }197        if let v = other.firstLineIndent { firstLineIndent = v }198        if let v = other.spaceBefore { spaceBefore = v }199        if let v = other.spaceAfter { spaceAfter = v }200        if let v = other.lineSpacing { lineSpacing = v }201        if let v = other.keepWithNext { keepWithNext = v }202        if let v = other.keepLinesTogether { keepLinesTogether = v }203        if let v = other.pageBreakBefore { pageBreakBefore = v }204        if let v = other.widowControl { widowControl = v }205        if let v = other.tabs { tabs = v }206        if let v = other.hyphenate { hyphenate = v }207        if let v = other.outlineLevel { outlineLevel = v }208        if let v = other.dropCapLines { dropCapLines = v }209        if let v = other.shading { shading = v }210        if let v = other.borderTop { borderTop = v }211        if let v = other.borderBottom { borderBottom = v }212        if let v = other.borderLeft { borderLeft = v }213        if let v = other.rightToLeft { rightToLeft = v }214    }215}216217/// Fully resolved character formatting (no optionals left). Produced by `StyleSheet.resolve`.218public struct ResolvedChar: Hashable, Sendable {219    public var fontFamily: String220    public var fontSize: Points221    public var weight: Int222    public var italic: Bool223    public var underline: Bool224    public var strikethrough: Bool225    public var color: RGBA226    public var highlight: RGBA?227    public var vertical: VerticalPosition228    public var baselineShift: Points229    public var tracking: Points230    public var kerning: Bool231    public var ligatures: Bool232    public var smallCaps: Bool233    public var allCaps: Bool234    public var link: String?235    public var language: String?236237    public init(_ p: CharProps) {238        fontFamily = p.fontFamily ?? "New York"239        fontSize = p.fontSize ?? 12240        weight = p.weight ?? ((p.bold ?? false) ? 700 : 400)241        italic = p.italic ?? false242        underline = p.underline ?? false243        strikethrough = p.strikethrough ?? false244        color = p.color ?? .black245        highlight = p.highlight246        vertical = p.vertical ?? .baseline247        baselineShift = p.baselineShift ?? 0248        tracking = p.tracking ?? 0249        kerning = p.kerning ?? true250        ligatures = p.ligatures ?? true251        smallCaps = p.smallCaps ?? false252        allCaps = p.allCaps ?? false253        link = p.link254        language = p.language255    }256    public var isBold: Bool { weight >= 600 }257}258259/// Fully resolved paragraph formatting.260public struct ResolvedPara: Hashable, Sendable {261    public var alignment: TextAlignment262    public var indentLeft: Points263    public var indentRight: Points264    public var firstLineIndent: Points265    public var spaceBefore: Points266    public var spaceAfter: Points267    public var lineSpacing: LineSpacing268    public var keepWithNext: Bool269    public var keepLinesTogether: Bool270    public var pageBreakBefore: Bool271    public var widowControl: Bool272    public var tabs: [TabStop]273    public var hyphenate: Bool274    public var outlineLevel: Int275    public var dropCapLines: Int276    public var shading: RGBA?277    public var borderTop: BorderLine?278    public var borderBottom: BorderLine?279    public var borderLeft: BorderLine?280    public var rightToLeft: Bool281282    public init(_ p: ParaProps) {283        alignment = p.alignment ?? .left284        indentLeft = p.indentLeft ?? 0285        indentRight = p.indentRight ?? 0286        firstLineIndent = p.firstLineIndent ?? 0287        spaceBefore = p.spaceBefore ?? 0288        spaceAfter = p.spaceAfter ?? 0289        lineSpacing = p.lineSpacing ?? .multiple(1.0)290        keepWithNext = p.keepWithNext ?? false291        keepLinesTogether = p.keepLinesTogether ?? false292        pageBreakBefore = p.pageBreakBefore ?? false293        widowControl = p.widowControl ?? true294        tabs = (p.tabs ?? []).sorted { $0.position < $1.position }295        hyphenate = p.hyphenate ?? false296        outlineLevel = p.outlineLevel ?? 0297        dropCapLines = p.dropCapLines ?? 0298        shading = p.shading299        borderTop = p.borderTop300        borderBottom = p.borderBottom301        borderLeft = p.borderLeft302        rightToLeft = p.rightToLeft ?? false303    }304}305
Files main
Menu

Theme