diff --git a/Source/render/RenderUtils.swift b/Source/render/RenderUtils.swift index 9717fec2..82e06599 100644 --- a/Source/render/RenderUtils.swift +++ b/Source/render/RenderUtils.swift @@ -90,26 +90,28 @@ class RenderUtils { fatalError("Unsupported node: \(node)") } + static let availableFonts = UIFont.familyNames.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 name == "serif" { + return MFont(name: "Georgia", size: CGFloat(size)) + } + if name == "sans-serif" { + return MFont(name: "Arial", size: CGFloat(size)) + } + if name == "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 { diff --git a/Source/render/TextRenderer.swift b/Source/render/TextRenderer.swift index 81a0ac30..45760dd5 100644 --- a/Source/render/TextRenderer.swift +++ b/Source/render/TextRenderer.swift @@ -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? { diff --git a/Source/svg/SVGParser.swift b/Source/svg/SVGParser.swift index f8a4f1c3..6dd27ed2 100644 --- a/Source/svg/SVGParser.swift +++ b/Source/svg/SVGParser.swift @@ -1268,7 +1268,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? {