From 73422282f3eb3b1e5841217fc50e5954681132e9 Mon Sep 17 00:00:00 2001 From: Alisa Mylnikova Date: Fri, 27 Apr 2018 17:29:10 +0700 Subject: [PATCH 1/2] Fix #337: SVGParser: Apply font weight to custom fonts --- Source/model/scene/Text.swift | 2 +- Source/render/RenderUtils.swift | 14 ++++++-------- Source/render/TextRenderer.swift | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Source/model/scene/Text.swift b/Source/model/scene/Text.swift index 31188329..f19314e2 100644 --- a/Source/model/scene/Text.swift +++ b/Source/model/scene/Text.swift @@ -67,7 +67,7 @@ open class Text: Node { let font: MFont if let f = self.font { - if let customFont = RenderUtils.loadFont(name: f.name, size: f.size) { + if let customFont = RenderUtils.loadFont(name: f.name, size: f.size, weight: f.weight) { font = customFont } else { font = MFont.systemFont(ofSize: CGFloat(f.size), weight: getWeight(f.weight)) diff --git a/Source/render/RenderUtils.swift b/Source/render/RenderUtils.swift index 9717fec2..d4bedc0f 100644 --- a/Source/render/RenderUtils.swift +++ b/Source/render/RenderUtils.swift @@ -90,7 +90,7 @@ class RenderUtils { fatalError("Unsupported node: \(node)") } - class func loadFont(name: String, size: Int) -> MFont? { + class func loadFont(name: String, size: Int, weight: String?) -> MFont? { let separationSet = CharacterSet(charactersIn: ",") let names = name.components(separatedBy: separationSet) var customFont: MFont? = .none @@ -99,14 +99,12 @@ class RenderUtils { return } - 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 + let fontName = fontName.trimmingCharacters(in: .whitespaces) + var fontDec = UIFontDescriptor(name: fontName, size: CGFloat(size)) + if weight == "bold" || weight == "bolder" { + fontDec = fontDec.withSymbolicTraits(.traitBold)! } - - customFont = MFont(name: fontName, size: CGFloat(size)) + customFont = MFont(descriptor: fontDec, size: CGFloat(size)) } return customFont diff --git a/Source/render/TextRenderer.swift b/Source/render/TextRenderer.swift index 81a0ac30..119d09a1 100644 --- a/Source/render/TextRenderer.swift +++ b/Source/render/TextRenderer.swift @@ -81,7 +81,7 @@ class TextRenderer: NodeRenderer { } if let textFont = text.font { - if let customFont = RenderUtils.loadFont(name: textFont.name, size: textFont.size) { + if let customFont = RenderUtils.loadFont(name: textFont.name, size: textFont.size, weight: textFont.weight) { return customFont } else { if let weight = getWeight(textFont.weight) { From f4adb557b8d226974690de803c9698408019c9a6 Mon Sep 17 00:00:00 2001 From: Alisa Mylnikova Date: Sat, 28 Apr 2018 13:04:13 +0700 Subject: [PATCH 2/2] MacOS adaptation --- Source/platform/iOS/Common_iOS.swift | 1 + Source/platform/macOS/Common_macOS.swift | 1 + Source/render/RenderUtils.swift | 9 +++++++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Source/platform/iOS/Common_iOS.swift b/Source/platform/iOS/Common_iOS.swift index 349f5ed7..e467ec00 100644 --- a/Source/platform/iOS/Common_iOS.swift +++ b/Source/platform/iOS/Common_iOS.swift @@ -13,6 +13,7 @@ import UIKit public typealias MRectCorner = UIRectCorner public typealias MFont = UIFont +public typealias MFontDescriptor = UIFontDescriptor public typealias MColor = UIColor public typealias MEvent = UIEvent public typealias MTouch = UITouch diff --git a/Source/platform/macOS/Common_macOS.swift b/Source/platform/macOS/Common_macOS.swift index 7ac0eb45..8896c90d 100644 --- a/Source/platform/macOS/Common_macOS.swift +++ b/Source/platform/macOS/Common_macOS.swift @@ -13,6 +13,7 @@ import Cocoa import Quartz public typealias MFont = NSFont +public typealias MFontDescriptor = NSFontDescriptor public typealias MColor = NSColor public typealias MEvent = NSEvent public typealias MTouch = NSTouch diff --git a/Source/render/RenderUtils.swift b/Source/render/RenderUtils.swift index d4bedc0f..99cef87d 100644 --- a/Source/render/RenderUtils.swift +++ b/Source/render/RenderUtils.swift @@ -98,11 +98,16 @@ class RenderUtils { if customFont != .none { return } - + let fontName = fontName.trimmingCharacters(in: .whitespaces) - var fontDec = UIFontDescriptor(name: fontName, size: CGFloat(size)) + var fontDec = MFontDescriptor(name: fontName, size: CGFloat(size)) if weight == "bold" || weight == "bolder" { + #if os(iOS) fontDec = fontDec.withSymbolicTraits(.traitBold)! + #elseif os(OSX) + fontDec = fontDec.withSymbolicTraits(.bold) + #endif + } customFont = MFont(descriptor: fontDec, size: CGFloat(size)) }