Skip to content

Commit

Permalink
Fix #337: SVGParser: Apply font weight to custom fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
f3dm76 committed Apr 27, 2018
1 parent e58629b commit 7342228
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 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
14 changes: 6 additions & 8 deletions Source/render/RenderUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Source/render/TextRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7342228

Please sign in to comment.