Skip to content

Commit

Permalink
Custom fonts don't render correctly [#10] (#11)
Browse files Browse the repository at this point in the history
* Setup a baseline test for custom fonts not working correctly

#10

# Problem

When choosing certain fonts, like Avenir-Medium, NativeMarkKit renders them as the "base" font instead of the correct font.

# Solution

Create a test to reproduce the issue.

* Custom fonts don't render correctly [#10]

#10

# Problem

Certain fonts, like Avenir-Medium, don't correctly render. They end up using the "base" font style (e.g. "book" in this case).

It turns out calling `withSymbolTraits` with the existing traits mutates the description, in this case stripping off the font class.

# Solution

If no traits were specified, don't try to apply them.
  • Loading branch information
andyfinnell authored Jul 9, 2021
1 parent a1bf90c commit 0fdc7fd
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 24 deletions.
20 changes: 8 additions & 12 deletions Sources/NativeMarkKit/style/TextStyle+AppKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ private extension FontTraits {

private extension NSFontDescriptor {
func withTraits(_ fontTraits: FontTraits) -> NSFontDescriptor {
withSymbolicTraits(symbolicTraits.union(fontTraits.symbolicTraits))
guard fontTraits != .unspecified else {
return self
}
return withSymbolicTraits(symbolicTraits.union(fontTraits.symbolicTraits))
}
}

Expand All @@ -106,19 +109,12 @@ private extension FontDescriptor {
return NSFont.monospacedSystemFont(ofSize: size.pointSize, weight: .regular)
.fontDescriptor
} else {
let traits: [NSFontDescriptor.TraitKey: Any] = [
.symbolic: NSFontMonoSpaceTrait
]
return NSFontDescriptor(fontAttributes: [
.traits: traits,
.size: size.pointSize
])
return NSFont.systemFont(ofSize: size.pointSize)
.fontDescriptor
.withSymbolicTraits(.monoSpace)
}
case let .custom(fontName):
return NSFontDescriptor(fontAttributes: [
.name: fontName,
.size: size.pointSize
])
return NSFontDescriptor(name: fontName, size: size.pointSize)
}
}
}
Expand Down
23 changes: 11 additions & 12 deletions Sources/NativeMarkKit/style/TextStyle+UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ private extension FontTraits {

private extension UIFontDescriptor {
func withTraits(_ fontTraits: FontTraits) -> UIFontDescriptor {
withSymbolicTraits(symbolicTraits.union(fontTraits.symbolicTraits)) ?? self
guard fontTraits != .unspecified else {
return self
}

return withSymbolicTraits(symbolicTraits.union(fontTraits.symbolicTraits)) ?? self
}
}

Expand All @@ -116,19 +120,14 @@ private extension FontDescriptor {
return UIFont.monospacedSystemFont(ofSize: size.pointSize, weight: .regular)
.fontDescriptor
} else {
let traits: [UIFontDescriptor.TraitKey: Any] = [
.symbolic: UIFontDescriptor.SymbolicTraits.traitMonoSpace.rawValue
]
return UIFontDescriptor(fontAttributes: [
.traits: traits,
.size: size.pointSize
])
let baseDescriptor = UIFont.systemFont(ofSize: size.pointSize)
.fontDescriptor

return baseDescriptor.withSymbolicTraits(.traitMonoSpace)
?? baseDescriptor
}
case let .custom(fontName):
return UIFontDescriptor(fontAttributes: [
.name: fontName,
.size: size.pointSize
])
return UIFontDescriptor(name: fontName, size: size.pointSize)
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/NativeMarkKitTests/BasicRenderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ final class BasicRenderTest: XCTestCase {
XCTAssert(testCase.isPassing(for: self))
}

func testHelloWorldWithCustomFontTake2() {
let style = StyleSheet.default.duplicate().mutate(
block: [
.document: [
.textStyle(.custom(name: .custom("Avenir-Roman"), size: .fixed(19))),
.backgroundColor(.white),
.textColor(.black)
],
])
let testCase = RenderTestCase(name: "HelloWorldWithCustomFontTakeTwo",
nativeMark: "Avenir Avenir Avenir Avenir",
styleSheet: style,
width: 320)
XCTAssert(testCase.isPassing(for: self))
}

func testParagraphs() {
let nativeMark = """
This is the first
Expand Down
Binary file modified Tests/NativeMarkKitTests/Fixtures/HelloWorldWithCustomFont.iOS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0fdc7fd

Please sign in to comment.