Consolidate some repeated code

This commit is contained in:
Nate Weaver 2020-04-30 02:34:45 -05:00
parent 74513d2661
commit ec49030081

View File

@ -227,24 +227,22 @@ extension NSAttributedString {
var attributes: [NSAttributedString.Key: Any] = [:] var attributes: [NSAttributedString.Key: Any] = [:]
var symbolicTraits = currentDescriptor.symbolicTraits
if styles.contains(.bold) { if styles.contains(.bold) {
let traits: [FontDescriptor.TraitKey: Any] = [.weight: Font.Weight.bold] symbolicTraits.insert(.bold)
let descriptorAttributes: [FontDescriptor.AttributeName: Any] = [.traits: traits]
descriptor = descriptor.addingAttributes(descriptorAttributes)
} }
if styles.contains(.italic) { if styles.contains(.italic) {
var symbolicTraits = currentDescriptor.symbolicTraits
symbolicTraits.insert(.italic) symbolicTraits.insert(.italic)
descriptor = descriptor.withSymbolicTraits(symbolicTraits)
} }
if styles.contains(.monospace) { if styles.contains(.monospace) {
var symbolicTraits = currentDescriptor.symbolicTraits
symbolicTraits.insert(.monoSpace) symbolicTraits.insert(.monoSpace)
descriptor = descriptor.withSymbolicTraits(symbolicTraits)
} }
descriptor = descriptor.withSymbolicTraits(symbolicTraits)
func verticalPositionFeature(forSuperscript: Bool) -> [FontDescriptor.FeatureKey: Any] { func verticalPositionFeature(forSuperscript: Bool) -> [FontDescriptor.FeatureKey: Any] {
#if canImport(AppKit) #if canImport(AppKit)
let features: [FontDescriptor.FeatureKey: Any] = [.typeIdentifier: kVerticalPositionType, .selectorIdentifier: forSuperscript ? kSuperiorsSelector : kInferiorsSelector] let features: [FontDescriptor.FeatureKey: Any] = [.typeIdentifier: kVerticalPositionType, .selectorIdentifier: forSuperscript ? kSuperiorsSelector : kInferiorsSelector]
@ -254,18 +252,12 @@ extension NSAttributedString {
return features return features
} }
if styles.contains(.superscript) { if styles.contains(.superscript) || styles.contains(.subscript) {
let features = verticalPositionFeature(forSuperscript: true) let features = verticalPositionFeature(forSuperscript: styles.contains(.superscript))
let descriptorAttributes: [FontDescriptor.AttributeName: Any] = [.featureSettings: [features]] let descriptorAttributes: [FontDescriptor.AttributeName: Any] = [.featureSettings: [features]]
descriptor = descriptor.addingAttributes(descriptorAttributes) descriptor = descriptor.addingAttributes(descriptorAttributes)
} }
if styles.contains(.subscript) {
let features = verticalPositionFeature(forSuperscript: false)
let descriptorAttributes: [FontDescriptor.AttributeName: Any] = [.featureSettings: [features]]
descriptor = currentDescriptor.addingAttributes(descriptorAttributes)
}
attributes[.font] = Font(descriptor: descriptor, size: baseFont.pointSize) attributes[.font] = Font(descriptor: descriptor, size: baseFont.pointSize)
if styles.contains(.strikethrough) { if styles.contains(.strikethrough) {