Skip to content

Commit

Permalink
[CM-589] use font style instead of isItalic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Pospesel authored and mpospese committed Jun 6, 2022
1 parent ab01ed6 commit 933c7e3
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 31 deletions.
19 changes: 10 additions & 9 deletions Sources/YMatterType/Typography/FontInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,30 @@ import UIKit

/// Information about a font family. Default implementation of FontRepresentable.
public struct FontInfo: FontRepresentable {
/// Suffix to use for Italic family font names "Italic"
/// Suffix to use for italic family font names "Italic"
public static let italicSuffix = "Italic"

/// Font family root name, e.g. "AvenirNext"
public let familyName: String

/// Whether this is an Italic font
public let isItalic: Bool
/// Font style, e.g. regular or italic
public let style: Typography.FontStyle

/// Initialize a `FontInfo` object
/// - Parameters:
/// - familyName: font family name
/// - isItalic: whether this font is Italic
public init(familyName: String, isItalic: Bool = false) {
/// - style: font style (default = `.regular`)
public init(familyName: String, style: Typography.FontStyle = .regular) {
self.familyName = familyName
self.isItalic = isItalic
self.style = style
}

/// Optional suffix to use for Italic version of the font.
/// Optional suffix to use for the font name.
///
/// Used by `FontRepresentable.fontName(for:compatibleWith:)`
/// e.g. "Italic" is a typical suffix for Italic fonts.
/// e.g. "Italic" is a typical suffix for italic fonts.
/// default = ""
public var fontNameSuffix: String {
isItalic ? FontInfo.italicSuffix : ""
(style == .italic) ? FontInfo.italicSuffix : ""
}
}
5 changes: 3 additions & 2 deletions Sources/YMatterType/Typography/FontRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public protocol FontRepresentable {
/// Font family root name, e.g. "AvenirNext"
var familyName: String { get }

/// Optional suffix to use for Italic version of the font.
/// Optional suffix to use for the font name.
///
/// Used by `FontRepresentable.fontName(for:compatibleWith:)`
/// e.g. "Italic" is a typical suffix for Italic fonts.
/// e.g. "Italic" is a typical suffix for italic fonts.
/// default = ""
var fontNameSuffix: String { get }

Expand Down
8 changes: 8 additions & 0 deletions Sources/YMatterType/Typography/Typography+Enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
import UIKit

extension Typography {
/// Font style (used together with font family name and font weight to load a specific font)
public enum FontStyle: String, CaseIterable {
/// Regular
case regular = "normal"
/// Italic
case italic
}

/// The nine basic font weights. Not all fonts support all 9 weights.
public enum FontWeight: CGFloat, CaseIterable {
/// ultralight (aka extra light) weight (100)
Expand Down
19 changes: 12 additions & 7 deletions Sources/YMatterType/Typography/Typography+SFPro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ public struct SFProFontFamily: FontRepresentable {
}
}

/// Whether the font family is italic or not
public var isItalic: Bool {
/// Font style, e.g. regular or italic
public var style: Typography.FontStyle {
let style: Typography.FontStyle
switch family {
case .displayItalic, .textItalic: return true
case .display, .text: return false
case .display, .text:
style = .regular
case .displayItalic, .textItalic:
style = .italic
}
return style
}

fileprivate init(family: SFProFamily) {
Expand Down Expand Up @@ -66,12 +70,13 @@ public struct SFProFontFamily: FontRepresentable {
}
}

/// Optional suffix to use for Italic version of the font.
/// Optional suffix to use for the font name.
///
/// Used by `FontRepresentable.fontName(for:compatibleWith:)`
/// e.g. "Italic" is a typical suffix for Italic fonts.
/// e.g. "Italic" is a typical suffix for italic fonts.
/// default = ""
public var fontNameSuffix: String {
isItalic ? FontInfo.italicSuffix : ""
(style == .italic) ? FontInfo.italicSuffix : ""
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/YMatterType/Typography/Typography.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public struct Typography {
/// Initializes a typography instance with the specified parameters
/// - Parameters:
/// - familyName: font family name
/// - isItalic: whether this font is italic or not (defaults to `.false`)
/// - fontStyle: font style (defaults to `regular`)
/// - fontWeight: font weight to use
/// - fontSize: font size to use
/// - lineHeight: line height to use
Expand All @@ -77,7 +77,7 @@ public struct Typography {
/// - isFixed: `true` if this font should never scale, `false` if it should scale (defaults to `.false`)
public init(
familyName: String,
isItalic: Bool = false,
fontStyle: FontStyle = .regular,
fontWeight: FontWeight,
fontSize: CGFloat,
lineHeight: CGFloat,
Expand All @@ -88,7 +88,7 @@ public struct Typography {
isFixed: Bool = false
) {
self.init(
fontFamily: FontInfo(familyName: familyName, isItalic: isItalic),
fontFamily: FontInfo(familyName: familyName, style: fontStyle),
fontWeight: fontWeight,
fontSize: fontSize,
lineHeight: lineHeight,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class TypographyFontTests: XCTestCase {
}

func testParagraphStyle() {
let fontInfo = FontInfo(familyName: "HelveticaNeue", isItalic: true)
let fontInfo = FontInfo(familyName: "HelveticaNeue", style: .italic)

sizes.forEach {
let typography = Typography(
Expand Down
18 changes: 9 additions & 9 deletions Tests/YMatterTypeTests/Typography/Typography+SFProTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ final class TypographySFProTests: XCTestCase {
}

func testSFProDisplay() {
_testFontFamily(Typography.sfProDisplay)
_testFontFamily(Typography.sfProDisplay, style: .regular)
}

func testSFProDisplayItalic() {
_testFontFamily(Typography.sfProDisplayItalic, isItalic: true)
_testFontFamily(Typography.sfProDisplayItalic, style: .italic)
}

func testSFProText() {
_testFontFamily(Typography.sfProText)
_testFontFamily(Typography.sfProText, style: .regular)
}

func testSFProTextItalic() {
_testFontFamily(Typography.sfProTextItalic, isItalic: true)
_testFontFamily(Typography.sfProTextItalic, style: .italic)
}
}

private extension TypographySFProTests {
func _testFontFamily(_ fontFamily: FontRepresentable, isItalic: Bool = false) {
func _testFontFamily(_ fontFamily: FontRepresentable, style: Typography.FontStyle) {
Typography.FontWeight.allCases.forEach {
let typography = Typography(
fontFamily: fontFamily,
Expand All @@ -54,12 +54,12 @@ private extension TypographySFProTests {
textStyle: .callout
)

_testTypography(typography, isItalic: isItalic, traits: nil)
_testTypography(typography, isItalic: isItalic, traits: boldTraits)
_testTypography(typography, style: style, traits: nil)
_testTypography(typography, style: style, traits: boldTraits)
}
}

func _testTypography(_ typography: Typography, isItalic: Bool, traits: UITraitCollection?) {
func _testTypography(_ typography: Typography, style: Typography.FontStyle, traits: UITraitCollection?) {
let layout = typography.generateLayout(compatibleWith: traits)

// we expect a font
Expand All @@ -73,7 +73,7 @@ private extension TypographySFProTests {

// we expect the font to be italic or not
let fontName = layout.font.fontName
if isItalic {
if style == .italic {
XCTAssertTrue(fontName.hasSuffix(FontInfo.italicSuffix))
} else {
XCTAssertFalse(fontName.hasSuffix(FontInfo.italicSuffix))
Expand Down

0 comments on commit 933c7e3

Please sign in to comment.