Skip to content

Commit

Permalink
Fix #473 and #474: custom fonts are not loaded correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ystrot committed Feb 11, 2019
1 parent 1200a87 commit baea2cc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Source/animation/AnimationProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ class AnimationContext {
var rootTransform: Transform?

func getLayoutTransform(_ renderer: NodeRenderer?) -> Transform {
if (rootTransform == nil) {
if rootTransform == nil {
if let view = renderer?.view, let node = view.renderer?.node() {
rootTransform = LayoutHelper.calcTransform(node, view.contentLayout, view.bounds.size.toMacaw())
}
Expand Down
4 changes: 4 additions & 0 deletions Source/platform/iOS/Common_iOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ extension MFont {
class var mFamilyNames: [String] {
return UIFont.familyNames
}

class func mFontNames(forFamily: String) -> [String] {
return UIFont.fontNames(forFamilyName: forFamily)
}
}

extension UIScreen {
Expand Down
11 changes: 11 additions & 0 deletions Source/platform/macOS/Common_macOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ extension NSFont {
class var mFamilyNames: [String] {
return NSFontManager.shared.availableFontFamilies
}

class func mFontNames(forFamily: String) -> [String] {
var result = [String]()
if let members = NSFontManager.shared.availableMembers(ofFontFamily: forFamily) {
for member in members {
result.append(member[0] as! String)
}
}
return result
}

}

extension NSScreen {
Expand Down
56 changes: 30 additions & 26 deletions Source/render/RenderUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,30 @@ class RenderUtils {
fatalError("Unsupported node: \(node)")
}

static let availableFonts = MFont.mFamilyNames.map { $0.lowercased() }

class func loadFont(name: String, size: Int, weight: String?) -> MFont? {

var fontName = ""
let fontPriorities = name.split(separator: ",").map { String($0).trimmingCharacters(in: CharacterSet(charactersIn: " '")) }

for font in fontPriorities {
let lowercasedFont = font.lowercased()

if availableFonts.contains(lowercasedFont) {
fontName = font
}

if lowercasedFont == "serif" {
fontName = "Georgia"
}
if lowercasedFont == "sans-serif" {
fontName = "Arial"
}
if lowercasedFont == "monospace" {
fontName = "Courier"
static let availableFonts = prepareFonts()

static func prepareFonts() -> [String: String] {
var result = [String: String]()
for family in MFont.mFamilyNames {
// TODO: do we need to include family names?
result[family.lowercased()] = family
for font in MFont.mFontNames(forFamily: family) {
result[font.lowercased()] = font
}
}
if fontName.isEmpty {
return .none
}
result["serif"] = "Georgia"
result["sans-serif"] = "Arial"
result["monospace"] = "Courier"
return result
}

class func loadFont(name: String, size: Int, weight: String?) -> MFont? {
guard let fontName = findFontName(title: name) else {
return nil
}
var fontDesc = MFontDescriptor(name: fontName, size: CGFloat(size))
if weight == "bold" || weight == "bolder" {
let lowerWeight = weight?.lowercased()
if lowerWeight == "bold" || lowerWeight == "bolder" {
#if os(iOS)
fontDesc = fontDesc.withSymbolicTraits(.traitBold)!
#elseif os(OSX)
Expand All @@ -73,6 +67,16 @@ class RenderUtils {
return MFont(descriptor: fontDesc, size: CGFloat(size))
}

static func findFontName(title: String) -> String? {
let fonts = title.split(separator: ",").map { String($0).trimmingCharacters(in: CharacterSet(charactersIn: " '")) }
for font in fonts {
if let availableFont = availableFonts[font.lowercased()] {
return availableFont
}
}
return nil
}

class func applyOpacity(_ color: Color, opacity: Double) -> Color {
return Color.rgba(r: color.r(), g: color.g(), b: color.b(), a: Double(color.a()) / 255.0 * opacity)
}
Expand Down

0 comments on commit baea2cc

Please sign in to comment.