1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-09-11 05:05:23 +03:00

Fix #473 and #474: custom fonts are not loaded correctly

This commit is contained in:
Yuri Strot 2019-02-11 18:59:13 +07:00
parent 1200a87e2d
commit baea2ccfc6
4 changed files with 46 additions and 27 deletions

View File

@ -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())
}

View File

@ -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 {

View File

@ -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 {

View File

@ -33,36 +33,30 @@ class RenderUtils {
fatalError("Unsupported node: \(node)")
}
static let availableFonts = MFont.mFamilyNames.map { $0.lowercased() }
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
}
}
result["serif"] = "Georgia"
result["sans-serif"] = "Arial"
result["monospace"] = "Courier"
return result
}
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"
}
guard let fontName = findFontName(title: name) else {
return nil
}
if fontName.isEmpty {
return .none
}
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)
@ -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)
}