Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]

- Honor every family in a Windows `FontFamily` list when DirectWrite falls back for missing characters (#668 by @houko).
- Add the `macos_universal` build target, which joins the arm64 and x64 apps, libraries and widgets into one direct-channel disk image and Sparkle feed (#265 by @houko).
- Stop resolving and emitting the whole tree when the platform reports only a new window frame, maximized state, lifecycle or title bar, as on every step of a window drag; what reads them through `Env` still updates (#178 by @houko).
- Add `TabStyle` and `IndicatorStyle` to `ui.Tabs` to restyle its tabs, the selected tab and the indicator (#958 by @houko).
Expand Down
2 changes: 1 addition & 1 deletion docs/architecture/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The engines draw the rest:
- `Language`, an inherited BCP 47 tag, picks the glyph forms of characters languages share, such as Han's Japanese and Chinese forms, and the fallback fonts and line breaks where the engine takes it. The root's is the language the app shows (`Env.Language`), else its locale, so text that sets none follows a switch of language at runtime; a span's own applies to its run. It travels in `Font.Language`, which the core's measurements and Apple's font cache key on: CoreText takes it as a run's `kCTLanguageAttributeName` and the language of `CTFontCreateUIFontForLanguage`, DirectWrite as the format's locale and `SetLocaleName` for a span in another, Pango as a language attribute, Android as the paint's text locale and the web as `lang` on the text's element, a span in another language on its own. Inputs and mini programs leave it out ([roadmap](../roadmap.md#finish-the-existing-platforms)).
- `AttributedText.Shadow`, the block's shadow: a Core Graphics shadow, a CSS `text-shadow`, Direct2D's shadow effect over the recorded glyphs, a GSK shadow node, Android's shadow layer.

`FontFamily` is one family or a CSS list (`Inter, "Noto Sans SC", sans-serif`), read by `driver.FontFamilies`; a character a family lacks comes from the next that has it. CoreText takes the others as a cascade list, each entry restyled with the face's weight and slant, CSS and Pango take the list as it is, and DirectWrite and Android take one family: the first installed on Windows, the first on Android ([roadmap](../roadmap.md#finish-the-existing-platforms)). A CoreText line takes its metrics from the faces its runs use, so a line wholly in a later family has that family's ascent and descent rather than CSS's strut of the first family's ([roadmap](../roadmap.md#finish-the-existing-platforms)).
`FontFamily` is one family or a CSS list (`Inter, "Noto Sans SC", sans-serif`), read by `driver.FontFamilies`; a character a family lacks comes from the next that has it. CoreText takes the others as a cascade list, each entry restyled with the face's weight and slant, CSS and Pango take the list as it is, DirectWrite draws in the first installed family and falls back along the others through an `IDWriteFontFallback`, then the system's, and Android takes only the first family ([roadmap](../roadmap.md#finish-the-existing-platforms)). A CoreText line takes its metrics from the faces its runs use, so a line wholly in a later family has that family's ascent and descent rather than CSS's strut of the first family's ([roadmap](../roadmap.md#finish-the-existing-platforms)).

Italic runs keep color emoji upright, in spans of their own (`driver.UprightEmoji`): emoji fonts have no italic face, and Chrome and Android would slant them where CoreText does not.

Expand Down
1 change: 0 additions & 1 deletion docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ These items wait for community contributions, and a change to one backend record
| Refresh room across inset changes on macOS | `scrolling-and-lists.md` | Changing `ContentInset` while a macOS view rests at its refresh indicator retains that room, as on Windows and Linux; `configureScroll` currently keeps its start using the ordinary inset, dropping it |
| Roles, states and font lists on Android | `elements.md`, `text.md` | `NsgoView` describes the roles NSH 0.4.0 added (`listbox`, `option`, `combobox`, `group`, `region`, `navigation`, `tree`, `treeitem`, `toolbar`, `menuitemcheckbox`, `menuitemradio`) and the pressed and busy states, and a `fontFamily` list falls back through its families with `Typeface.CustomFallbackBuilder` rather than setting the first, which needs `classes.dex` rebuilt |
| Language in inputs and mini programs | `text.md` | Text inputs and mini programs' text set their characters in `Style.Language` as text does on each backend, and Korean breaks lines between words as CSS's `word-break: keep-all` does; today only text and its spans take the language |
| Font lists on Windows | `text.md` | A character the first family of a `fontFamily` list lacks comes from the next that has it through an `IDWriteFontFallback`, rather than from the system's fallback, as elsewhere |
| Line-limit sizing on Windows, mini programs and HarmonyOS | [#965](https://github.com/nsgo-dev/nsgo/issues/965) | A text's measured box reflects its `MaxLines` truncation; Windows measures its wrapped lines before trimming at the height limit, mini programs add the ellipsis to the last wrapped line rather than truncating the remaining text, and HarmonyOS measures without the line limit |
| Font struts on Apple | [#305](https://github.com/nsgo-dev/nsgo/issues/305) | A line whose characters all come from a later family of a `fontFamily` list keeps at least the first family's ascent and descent on macOS and iOS, as CSS's strut does, with sizes, drawing, carets and hit tests agreeing; CoreText's framesetter takes each line's metrics from its runs' faces, so the backend would stack the lines itself |
| Table headers on phones | — | A `DataTable`'s sort control takes a tap in the iOS simulator, as it does on desktops and the web; the gallery's TSX `TestDataTableSorts` sees none there (2026-11-08) and runs on desktops alone |
Expand Down
14 changes: 14 additions & 0 deletions internal/platform/windows/d2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var (
iidD3D11Multithread = parseGUID("9b7e4e00-342c-4106-a19f-4f2704f689f0")
iidD2D1Factory1 = parseGUID("bb12d362-daee-4b9a-aa1d-14ba401cfa1f")
iidDWriteFactory = parseGUID("b859ee5a-d838-4b5b-a2e8-1adc7d93db48")
iidDWriteFactory2 = parseGUID("0439fc60-ca44-4994-8dee-3a9af7b732ec")
iidDWriteTextFormat1 = parseGUID("5f174b49-0d8b-4cfb-8bca-f1cce9d06c67")
iidDWriteTextLayout1 = parseGUID("9064d822-80a7-465c-a986-df65f78b8feb")
iidDWriteInline = parseGUID("8339fde3-106f-47ab-8373-1c6295eb10b3")
clsidGaussianBlur = parseGUID("1feb6d69-2fe6-4ac9-8c58-1d7f93e7a6a5")
Expand Down Expand Up @@ -123,9 +125,21 @@ const (
dwCreateTextLayout = 18
dwCreateEllipsisTrimmingSign = 20

// IDWriteFactory2
dw2GetSystemFontFallback = 26
dw2CreateFontFallbackBuilder = 27

// IDWriteTypography
typographyAddFontFeature = 3

// IDWriteFontFallbackBuilder
fallbackAddMapping = 3
fallbackAddMappings = 4
fallbackCreateFontFallback = 5

// IDWriteTextFormat1
formatSetFontFallback = 34

// IDWriteFontCollection, IDWriteFontFamily, IDWriteFont
collectionGetFontFamily = 4
collectionFindFamilyName = 5
Expand Down
9 changes: 7 additions & 2 deletions internal/platform/windows/fonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ func registerFonts(paths []string) {
// with it, and the families looked up before look again.
func (w *window) RegisterFont(data []byte) error { return addFonts(data) }

// addFonts makes appFonts of the system's fonts, the app's so far and
// data's, and has the families looked up before look again.
// addFonts makes appFonts of the system's fonts, the app's so far and data's, and has the families looked up and the fallbacks made before made again.
func addFonts(data ...[]byte) error {
err := buildFonts(data)
fallbacksMu.Lock()
for key, fallback := range fallbacks {
fallback.release()
delete(fallbacks, key)
}
fallbacksMu.Unlock()
// After fontsMu, which metricsOf takes under facesMu.
facesMu.Lock()
clear(faces)
Expand Down
19 changes: 19 additions & 0 deletions internal/platform/windows/logic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,25 @@ func TestFallbackFontsDrawMissingGlyphs(t *testing.T) {
}
}

// TestFallbackFontsFollowTheFamilyList sets the Thai letters Courier New lacks in the next family of the list, Tahoma, where DirectWrite's own fallback would pick its Thai face, and still falls back to the system for what no family of the list has.
func TestFallbackFontsFollowTheFamilyList(t *testing.T) {
w := testWindow()
width := func(text, family string) driver.Points {
p := w.Prepare(&driver.AttributedText{Text: text, Style: driver.TextStyle{Font: driver.Font{Family: family, Size: 32}, Color: driver.RGB(0)}})
defer p.Release()
return p.Measure(driver.Auto).Width
}
for _, c := range []struct{ text, list, family string }{
{"กกกก", "Courier New, Tahoma", "Tahoma"},
{"😀", "Courier New, Times New Roman", "Segoe UI Emoji"},
} {
got, want := width(c.text, c.list), width(c.text, c.family)
if got != want || got == 0 {
t.Errorf("%q in %q is %v wide, the %s glyphs are %v", c.text, c.list, got, c.family, want)
}
}
}

// TestSingleLineTextTruncatesInsteadOfWrapping checks MaxLines 1 keeps
// the text on one line, trimmed to the box's width.
func TestSingleLineTextTruncatesInsteadOfWrapping(t *testing.T) {
Expand Down
103 changes: 90 additions & 13 deletions internal/platform/windows/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,87 @@ var (
})
)

var (
fallbacksMu sync.Mutex
// fallbacks holds an IDWriteFontFallback per FontFamily list of more than one family, which formats share and keep until the app's fonts change.
fallbacks = map[string]*comObject{}
)

// unicodeRange is a DWRITE_UNICODE_RANGE.
type unicodeRange struct {
first, last uint32
}

// fallbackNames returns the Windows family each family of list names, in order.
func fallbackNames(list string) []string {
families := driver.FontFamilies(list)
names := make([]string, 0, len(families))
for _, f := range families {
switch f.Kind {
case driver.FamilyMonospace:
names = append(names, "Consolas")
case driver.FamilySerif:
names = append(names, "Times New Roman")
case driver.FamilySystem, driver.FamilyRounded:
names = append(names, defaultFontName)
default:
names = append(names, f.Name)
}
}
return names
}

// fontFallback returns a font fallback that sets each character in the first family of list that has it, then where the system's fallback would, or nil for a list of one family or a Windows without IDWriteFactory2: DirectWrite's own fallback follows the system's locale rather than the list. The caller releases it.
func fontFallback(list string) *comObject {
names := fallbackNames(list)
if len(names) < 2 {
return nil
}
fallbacksMu.Lock()
defer fallbacksMu.Unlock()
if f, ok := fallbacks[list]; ok {
f.addRef()
return f
}
factory := dwriteFactory()
if factory == nil {
return nil
}
f2 := factory.query(&iidDWriteFactory2)
if f2 == nil {
return nil
}
defer f2.release()
var builder, system, fallback *comObject
defer releaseAll(&builder, &system)
if failed(f2.call(dw2CreateFontFallbackBuilder, uintptr(unsafe.Pointer(&builder)))) {
return nil
}
fonts := fontCollection()
if fonts != nil {
defer fonts.release()
}
all := unicodeRange{first: 0, last: 0x10ffff}
targets := make([]*uint16, len(names))
for i, name := range names {
targets[i] = wide(name)
}
if failed(builder.callF(fallbackAddMapping, 1<<7, uintptr(unsafe.Pointer(&all)), 1,
uintptr(unsafe.Pointer(&targets[0])), uintptr(len(targets)), fonts.ptr(), 0, 0, f32(1))) {
return nil
}
// A custom fallback replaces the system's, which still covers what no family of the list has, such as emoji.
if !failed(f2.call(dw2GetSystemFontFallback, uintptr(unsafe.Pointer(&system)))) {
builder.call(fallbackAddMappings, system.ptr())
}
if failed(builder.call(fallbackCreateFontFallback, uintptr(unsafe.Pointer(&fallback)))) {
return nil
}
fallbacks[list] = fallback
fallback.addRef()
return fallback
}

var (
typographiesMu sync.Mutex
// typographies holds an IDWriteTypography per font variant, which
Expand Down Expand Up @@ -150,19 +231,7 @@ func firstInstalled(list string) string {
if name, ok := families[list]; ok {
return name
}
names := make([]string, 0, 1)
for _, f := range driver.FontFamilies(list) {
switch f.Kind {
case driver.FamilyMonospace:
names = append(names, "Consolas")
case driver.FamilySerif:
names = append(names, "Times New Roman")
case driver.FamilySystem, driver.FamilyRounded:
names = append(names, defaultFontName)
default:
names = append(names, f.Name)
}
}
names := fallbackNames(list)
if len(names) == 0 {
return defaultFontName
}
Expand Down Expand Up @@ -278,6 +347,14 @@ func textFormat(font driver.Font) *comObject {
uintptr(unsafe.Pointer(wide(font.Language))), uintptr(unsafe.Pointer(&format)))) {
return nil
}
if fallback := fontFallback(font.Family); fallback != nil {
// The format holds the fallback itself.
if format1 := format.query(&iidDWriteTextFormat1); format1 != nil {
format1.call(formatSetFontFallback, fallback.ptr())
format1.release()
}
fallback.release()
}
return format
}

Expand Down