Skip to content

Commit

Permalink
Update MarkupStyleFont.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
zhgchgli0718 authored Nov 24, 2024
1 parent bcea69a commit 820ec74
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions Sources/ZMarkupParser/Core/MarkupStyle/MarkupStyleFont.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ public struct MarkupStyleFont: MarkupStyleItem {
if let traits = font.fontDescriptor.fontAttributes[.traits] as? [NSFontDescriptor.TraitKey: Any], let weight = traits[.weight] as? NSFont.Weight {
self = weight.convertFontWeightStyle()
return
} else if font.fontDescriptor.symbolicTraits.contains(.bold) {
self = .bold
return
} else if let weightName = font.fontDescriptor.object(forKey: .face) as? String, let weight = Self.init(rawValue: weightName) {
self = weight
return
Expand Down Expand Up @@ -273,7 +270,7 @@ extension MarkupStyleFont {
func getFont() -> NSFont? {
guard !isNil() else { return nil }

var traits: [NSFontDescriptor.SymbolicTraits] = []
var traits: NSFontDescriptor.SymbolicTraits = []

let size = (self.size ?? MarkupStyle.default.font.size) ?? NSFont.systemFontSize
let weight = self.weight?.convertToUIFontWeight() ?? .regular
Expand All @@ -289,24 +286,15 @@ extension MarkupStyleFont {
font = NSFont.systemFont(ofSize: size, weight: weight)
}

if weight.rawValue >= NSFont.Weight.medium.rawValue {
traits.append(.bold)
if bold == true {
traits.insert(.bold)
}

if let italic = self.italic, italic {
traits.append(.italic)
}

if traits.isEmpty {
return font
} else {
return withTraits(font: font, traits: traits)
traits.insert(.italic)
}
}

private func withTraits(font: NSFont, traits: [NSFontDescriptor.SymbolicTraits]) -> NSFont {
let descriptor = font.fontDescriptor.withSymbolicTraits(NSFontDescriptor.SymbolicTraits(traits))
return NSFont(descriptor: descriptor, size: font.pointSize) ?? font
return font.with(weight: weight, symbolicTraits: traits)
}
}

Expand Down Expand Up @@ -367,4 +355,26 @@ private extension NSFont.Weight {
}
}

private extension NSFont {

/// Returns a font object that is the same as the receiver but which has the specified weight and symbolic traits
func with(weight: Weight, symbolicTraits: NSFontDescriptor.SymbolicTraits) -> NSFont {

var mergedsymbolicTraits = fontDescriptor.symbolicTraits
mergedsymbolicTraits.formUnion(symbolicTraits)

var traits = fontDescriptor.fontAttributes[.traits] as? [NSFontDescriptor.TraitKey: Any] ?? [:]
traits[.weight] = weight
traits[.symbolic] = mergedsymbolicTraits.rawValue

var fontAttributes: [NSFontDescriptor.AttributeName: Any] = [:]
fontAttributes[.family] = familyName
fontAttributes[.traits] = traits

let font = NSFont(descriptor: NSFontDescriptor(fontAttributes: fontAttributes), size: pointSize)
// return UIFontMetrics.default.scaledFont(for: font)
return font ?? self
}
}

#endif

0 comments on commit 820ec74

Please sign in to comment.