Skip to content

Commit

Permalink
Fix #335: Parse generic fonts from svg
Browse files Browse the repository at this point in the history
  • Loading branch information
f3dm76 committed Apr 27, 2018
1 parent e58629b commit 3e3bbe6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
32 changes: 17 additions & 15 deletions Source/render/RenderUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions Source/render/TextRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
2 changes: 1 addition & 1 deletion Source/svg/SVGParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down

0 comments on commit 3e3bbe6

Please sign in to comment.