mirror of
https://github.com/exyte/Macaw.git
synced 2024-09-21 01:47:44 +03:00
Merge pull request #336 from f3dm76/task/genericFonts
Fix #335: Parse generic fonts from svg
This commit is contained in:
commit
a0e2a80938
@ -77,6 +77,10 @@ extension MFont {
|
||||
class var mSystemFontSize: CGFloat {
|
||||
return UIFont.systemFontSize
|
||||
}
|
||||
|
||||
class var mFamilyNames: [String] {
|
||||
return UIFont.familyNames
|
||||
}
|
||||
}
|
||||
|
||||
extension UIScreen {
|
||||
|
@ -92,6 +92,10 @@ extension NSFont {
|
||||
class var mSystemFontSize: CGFloat {
|
||||
return NSFont.systemFontSize
|
||||
}
|
||||
|
||||
class var mFamilyNames: [String] {
|
||||
return NSFontManager.shared.availableFontFamilies
|
||||
}
|
||||
}
|
||||
|
||||
extension NSScreen {
|
||||
|
@ -90,26 +90,28 @@ class RenderUtils {
|
||||
fatalError("Unsupported node: \(node)")
|
||||
}
|
||||
|
||||
static let availableFonts = MFont.mFamilyNames.map{ $0.lowercased() }
|
||||
|
||||
class func loadFont(name: String, size: Int) -> MFont? {
|
||||
let separationSet = CharacterSet(charactersIn: ",")
|
||||
let names = name.components(separatedBy: separationSet)
|
||||
var customFont: MFont? = .none
|
||||
names.forEach { fontName in
|
||||
if customFont != .none {
|
||||
return
|
||||
|
||||
let fontPriorities = name.split(separator: ",").map{ String($0).trimmingCharacters(in: CharacterSet(charactersIn: " '")).lowercased() }
|
||||
for font in fontPriorities {
|
||||
if availableFonts.contains(font) {
|
||||
return MFont(name: font, size: CGFloat(size))
|
||||
}
|
||||
|
||||
if fontName.first == " " {
|
||||
let index = fontName.index(fontName.startIndex, offsetBy: 1)
|
||||
let fixedName = String(fontName.suffix(from: index))
|
||||
customFont = MFont(name: fixedName, size: CGFloat(size))
|
||||
return
|
||||
|
||||
if font == "serif" {
|
||||
return MFont(name: "Georgia", size: CGFloat(size))
|
||||
}
|
||||
if font == "sans-serif" {
|
||||
return MFont(name: "Arial", size: CGFloat(size))
|
||||
}
|
||||
if font == "monospace" {
|
||||
return MFont(name: "Courier", size: CGFloat(size))
|
||||
}
|
||||
|
||||
customFont = MFont(name: fontName, size: CGFloat(size))
|
||||
}
|
||||
|
||||
return customFont
|
||||
return .none
|
||||
}
|
||||
|
||||
class func applyOpacity(_ color: Color, opacity: Double) -> Color {
|
||||
|
@ -79,18 +79,18 @@ class TextRenderer: NodeRenderer {
|
||||
guard let text = text else {
|
||||
return MFont.systemFont(ofSize: 18.0)
|
||||
}
|
||||
|
||||
if let textFont = text.font {
|
||||
if let customFont = RenderUtils.loadFont(name: textFont.name, size: textFont.size) {
|
||||
return customFont
|
||||
} else {
|
||||
if let weight = getWeight(textFont.weight) {
|
||||
return MFont.systemFont(ofSize: CGFloat(textFont.size), weight: weight)
|
||||
}
|
||||
return MFont.systemFont(ofSize: CGFloat(textFont.size))
|
||||
}
|
||||
guard let textFont = text.font else {
|
||||
return MFont.systemFont(ofSize: MFont.mSystemFontSize)
|
||||
}
|
||||
|
||||
if let customFont = RenderUtils.loadFont(name: textFont.name, size: textFont.size) {
|
||||
return customFont
|
||||
} else {
|
||||
if let weight = getWeight(textFont.weight) {
|
||||
return MFont.systemFont(ofSize: CGFloat(textFont.size), weight: weight)
|
||||
}
|
||||
return MFont.systemFont(ofSize: CGFloat(textFont.size))
|
||||
}
|
||||
return MFont.systemFont(ofSize: MFont.mSystemFontSize)
|
||||
}
|
||||
|
||||
fileprivate func getWeight(_ weight: String) -> MFont.Weight? {
|
||||
|
@ -1271,7 +1271,7 @@ open class SVGParser {
|
||||
}
|
||||
|
||||
fileprivate func getFontName(_ attributes: [String: String]) -> String? {
|
||||
return attributes["font-family"]
|
||||
return attributes["font-family"]?.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
}
|
||||
|
||||
fileprivate func getFontSize(_ attributes: [String: String]) -> Int? {
|
||||
|
Loading…
Reference in New Issue
Block a user