Custom font never renders italic (.italic() and markdown *…*) because the family holds a single Typeface

Open
#578 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
android, swift
Domain
frontend, mobile

Research direction

Start in Font.swift around line 217, where Font.custom builds a FontFamily from a single Typeface; inspect how that family handles requested italic styles. Compare the proposed sibling resource font approach with building the family from ResourceFont, and check existing font tests or add focused coverage for .italic() and Markdown emphasis. Done means custom fonts with an available italic face render italic on Android while regular text remains upright.

Written by the indexing model from the issue text.

Description

A custom font loaded with Font.custom("Spectral-Regular", size:) becomes a single-typeface family (FontFamily(customTypeface) in Font.swift, line 217 in 1.63.0). Compose does not synthesize italic for a family built from an android.graphics.Typeface, so anything that requests FontStyle.Italic on that font renders upright:

  • Text("…").font(.custom("Spectral-Regular", size: 15)).italic()
  • markdown emphasis: Text(LocalizedStringKey("A *quiet* word")).font(.custom("Spectral-Regular", size: 15)) renders the asterisks away but not the italic.

On iOS both render in the italic face (the system picks Spectral-Italic from the same family).

Reduced example (not run in isolation; observed in our app with spectral_regular.ttf and spectral_italic.ttf in res/font):

Text(LocalizedStringKey("Ce livre tient en une phrase : *le bonheur n'attend pas.*"))
    .font(.custom("Spectral-Regular", size: 15))

The local fix we apply (works for us on device): when the resource name ends in _regular and a sibling _italic resource exists, build a two-face family from resource fonts:

let italique = name.hasSuffix("_regular") ? ctx.resources.getIdentifier(name.dropLast(8) + "_italic", "font", ctx.packageName) : 0
...
} else if italique != 0 {
    fontFamily = FontFamily(androidx.compose.ui.text.font.Font(resId: fid, weight: FontWeight.Normal, style: androidx.compose.ui.text.font.FontStyle.Normal),
                            androidx.compose.ui.text.font.Font(resId: italique, weight: FontWeight.Normal, style: androidx.compose.ui.text.font.FontStyle.Italic))
}

A more general approach might be to build FontFamily from ResourceFont instead of a loaded Typeface, so Compose can at least synthesize the italic.

Dominant language
Swift
Stars
334
Forks
95
Avg merge
2d 10h
Merged PRs (30d)
8

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from skiptools/skip-ui

All issues in skiptools/skip-ui

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.