Skip to content

Attribute の最適化 #35

@rrbox

Description

@rrbox

Attribute をビルダーパターンで設計できるようにしていますが、AttributeContext を直接ビルドできるようにして仕舞えばいいでのはないか? という試みです。

不変性も維持したいのであればこんな感じでしょうか。

public struct Attribute {
    let context: AttributeContext
}

public extension Attribute {
    static func fontName(_ value: String) -> Attribute {
        var newContext = AttributeContext()
        newContext.fontName = value
        return .init(context: newContext)
    }
    
    static func fontSize(_ value: NSNumber) -> Attribute {
        var newContext =  AttributeContext()
        newContext.fontSize = value
        return .init(context: newContext)
    }
    
    static func foregroundColor(_ value: Scope.ForegroundColorAttribute.Value) -> Attribute {
        var newContext =  AttributeContext()
        newContext.container.foregroundColor = value
        return .init(context: newContext)
    }
    
    static func backgroundColor(_ value: Scope.BackgroundColorAttribute.Value) -> Attribute {
        var newContext =  AttributeContext()
        newContext.container.backgroundColor = value
        return .init(context: newContext)
    }
    
    static func kern(_ value: Scope.KernAttribute.Value) -> Attribute {
        var newContext =  AttributeContext()
        newContext.container.kern = value
        return .init(context: newContext)
    }
    
    static func tracking(_ value: Scope.TrackingAttribute.Value) -> Attribute {
        var newContext =  AttributeContext()
        newContext.container.tracking = value
        return .init(context: newContext)
    }
    
    static func baselineOffset(_ value: Scope.BaselineOffsetAttribute.Value) -> Attribute {
        var newContext =  AttributeContext()
        newContext.container.baselineOffset = value
        return .init(context: newContext)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions