Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

feat(native): Swift custom faces draw heavier under Bold Text (#8292)

Cindy Zhang7c45ff37c965
diff --git a/packages/native/apple/Sources/Astryx/Theme/Theme.swift b/packages/native/apple/Sources/Astryx/Theme/Theme.swift
index 522cedd999..4afa1a6f22 100644
--- a/packages/native/apple/Sources/Astryx/Theme/Theme.swift
+++ b/packages/native/apple/Sources/Astryx/Theme/Theme.swift
@@ -49,11 +49,16 @@ public struct AstryxStyle: Sendable {
public let textScale: CGFloat
/// The theme's replacements for shared icon names.
public let icons: [String: String]
+ /// Whether the person turned on Bold Text (`legibilityWeight == .bold`).
+ public let isBoldText: Bool
- public init(tokens: Tokens, textScale: CGFloat = 1, icons: [String: String] = [:]) {
+ public init(
+ tokens: Tokens, textScale: CGFloat = 1, icons: [String: String] = [:], isBoldText: Bool = false
+ ) {
self.tokens = tokens
self.textScale = textScale
self.icons = icons
+ self.isBoldText = isBoldText
}
/// A token color by its CSS name, for code that only has the name. A name
@@ -91,7 +96,8 @@ public struct AstryxStyle: Sendable {
astryxFont(
families: tokens.font.families(named: family) ?? [],
size: fontSize(token, fallback),
- weight: fontWeight(size(weight, 400))
+ weight: fontWeight(size(weight, 400)),
+ isBoldText: isBoldText
)
}
@@ -149,7 +155,10 @@ extension Color {
}
/// A font from a CSS family stack: the first system family or installed font.
-func astryxFont(families: [String], size: CGFloat, weight: Font.Weight) -> Font {
+/// System fonts follow Bold Text by themselves; an installed custom face is
+/// drawn one weight step heavier when Bold Text is on (theme environment
+/// spec, E1).
+func astryxFont(families: [String], size: CGFloat, weight: Font.Weight, isBoldText: Bool = false) -> Font {
for family in families {
switch family.lowercased() {
case "-apple-system", "blinkmacsystemfont", "system-ui", "ui-sans-serif", "sans-serif":
@@ -163,13 +172,29 @@ func astryxFont(families: [String], size: CGFloat, weight: Font.Weight) -> Font
default:
if astryxFontIsInstalled(family) {
// Fixed size: the size already carries the Dynamic Type scale.
- return .custom(family, fixedSize: size).weight(weight)
+ return .custom(family, fixedSize: size).weight(isBoldText ? astryxBoldTextWeight(weight) : weight)
}
}
}
return .system(size: size, weight: weight)
}
+/// The weight a custom face draws at with Bold Text on: one step heavier,
+/// with regular and medium both stepping to semibold, as iOS does for its own
+/// text.
+func astryxBoldTextWeight(_ weight: Font.Weight) -> Font.Weight {
+ switch weight {
+ case .ultraLight: .thin
+ case .thin: .light
+ case .light: .regular
+ case .regular, .medium: .semibold
+ case .semibold: .bold
+ case .bold: .heavy
+ case .heavy, .black: .black
+ default: weight
+ }
+}
+
func astryxFontIsInstalled(_ name: String) -> Bool {
#if canImport(UIKit)
UIFont(name: name, size: 12) != nil
@@ -219,7 +244,8 @@ extension EnvironmentValues {
return AstryxStyle(
tokens: colorScheme == .dark ? theme.dark : theme.light,
textScale: astryxTextScale(for: dynamicTypeSize),
- icons: theme.icons
+ icons: theme.icons,
+ isBoldText: legibilityWeight == .bold
)
}
}
diff --git a/packages/native/apple/Tests/AstryxTests/EnvironmentTests.swift b/packages/native/apple/Tests/AstryxTests/EnvironmentTests.swift
index d0acb80fd0..7880a39977 100644
--- a/packages/native/apple/Tests/AstryxTests/EnvironmentTests.swift
+++ b/packages/native/apple/Tests/AstryxTests/EnvironmentTests.swift
@@ -34,3 +34,33 @@ import Testing
#expect(Theme.default.light.font.familyBody.first == "-apple-system")
#expect(Theme.default.light.font.familyHeading.isEmpty == false)
}
+
+// Theme environment spec, E1: with Bold Text on, a custom face from the
+// theme's family list is drawn one weight step heavier; system fonts follow
+// Bold Text by themselves.
+
+@Test func boldTextStepsCustomFacesHeavier() {
+ let steps: [(Font.Weight, Font.Weight)] = [
+ (.ultraLight, .thin), (.thin, .light), (.light, .regular), (.regular, .semibold),
+ (.medium, .semibold), (.semibold, .bold), (.bold, .heavy), (.heavy, .black), (.black, .black),
+ ]
+ for (weight, bolder) in steps {
+ #expect(astryxBoldTextWeight(weight) == bolder)
+ }
+ #expect(
+ astryxFont(families: ["Helvetica"], size: 14, weight: .regular, isBoldText: true)
+ == .custom("Helvetica", fixedSize: 14).weight(.semibold))
+ #expect(
+ astryxFont(families: ["Helvetica"], size: 14, weight: .bold, isBoldText: true)
+ == .custom("Helvetica", fixedSize: 14).weight(.heavy))
+ #expect(
+ astryxFont(families: ["-apple-system"], size: 14, weight: .regular, isBoldText: true)
+ == .system(size: 14, weight: .regular))
+}
+
+@Test func theStyleReadsBoldTextFromTheEnvironment() {
+ var environment = EnvironmentValues()
+ #expect(environment.astryx.isBoldText == false)
+ environment.legibilityWeight = .bold
+ #expect(environment.astryx.isBoldText)
+}