Skip to content

Commit

Permalink
Merge pull request #338 from f3dm76/task/fontWeight
Browse files Browse the repository at this point in the history
Fix #337: SVGParser: Apply font weight to custom fonts
  • Loading branch information
ystrot authored May 8, 2018
2 parents 762b3ab + 41b31ad commit 8648139
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Source/model/scene/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions Source/platform/iOS/Common_iOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Source/platform/macOS/Common_macOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 21 additions & 8 deletions Source/render/RenderUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,39 @@ class RenderUtils {

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

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

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

var fontName = ""
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))
fontName = font
}

if font == "serif" {
return MFont(name: "Georgia", size: CGFloat(size))
fontName = "Georgia"
}
if font == "sans-serif" {
return MFont(name: "Arial", size: CGFloat(size))
fontName = "Arial"
}
if font == "monospace" {
return MFont(name: "Courier", size: CGFloat(size))
fontName = "Courier"
}
}

return .none
if fontName.isEmpty {
return .none
}

var fontDesc = MFontDescriptor(name: fontName, size: CGFloat(size))
if weight == "bold" || weight == "bolder" {
#if os(iOS)
fontDesc = fontDesc.withSymbolicTraits(.traitBold)!
#elseif os(OSX)
fontDesc = fontDesc.withSymbolicTraits(.bold)
#endif

}
return MFont(descriptor: fontDesc, size: CGFloat(size))
}

class func applyOpacity(_ color: Color, opacity: Double) -> Color {
Expand Down
7 changes: 2 additions & 5 deletions Source/render/TextRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,11 @@ class TextRenderer: NodeRenderer {
// However it is needed for the Swift Package Manager to work accordingly.
return MFont()
}
guard let text = text else {
return MFont.systemFont(ofSize: 18.0)
}
guard let textFont = text.font else {
guard let text = text, let textFont = text.font else {
return MFont.systemFont(ofSize: MFont.mSystemFontSize)
}

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) {
Expand Down

0 comments on commit 8648139

Please sign in to comment.