Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #335: Parse generic fonts from svg #336

Merged
merged 4 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Source/platform/iOS/Common_iOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ extension MFont {
class var mSystemFontSize: CGFloat {
return UIFont.systemFontSize
}

class var mFamilyNames: [String] {
return UIFont.familyNames
}
}

extension UIScreen {
Expand Down
4 changes: 4 additions & 0 deletions Source/platform/macOS/Common_macOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ extension NSFont {
class var mSystemFontSize: CGFloat {
return NSFont.systemFontSize
}

class var mFamilyNames: [String] {
return NSFontManager.shared.availableFontFamilies
}
}

extension NSScreen {
Expand Down
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 = 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 {
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