From 4e2357c8d5360d7c4364cafef8c66b67117285b9 Mon Sep 17 00:00:00 2001 From: Collin Schneide <27441618+FaithfulAudio@users.noreply.github.com> Date: Sun, 26 Jul 2026 01:31:12 -0700 Subject: [PATCH 1/3] fix(text): resolve app-bundled font families by passing a DirectWrite font collection WindowsTextLayoutManager::GetTextLayout passed nullptr as the font collection to CreateTextFormat, which restricts resolution to the system collection. Every app-bundled font family therefore failed to resolve and every codepoint fell back to Segoe UI glyph 0 (.notdef) - icon fonts render as blank or tofu. Measured with a standalone DirectWrite probe replaying this exact call sequence against the eight stock react-native-vector-icons TTFs: with a collection that contains the font each draws a real glyph index (40, 1, 1, 13, 4, 4, 4, 2); with nullptr every one resolves to Segoe UI glyph 0. Adds DWriteAppFontCollection() to DWriteHelpers - the system font set merged with every *.ttf/*.otf under the app's Assets\ and Assets\Fonts\, built once via a magic static, failing closed to nullptr so behaviour is unchanged for apps that bundle no fonts - and passes it at the CreateTextFormat call site. Per-fragment SetFontFamilyName inherits the layout's collection, so only that one call site needed changing. Fixes #16306. Fixes #16308 (same root cause - the checksum and space-in-name diagnoses in those issues were both refuted by the probe). --- ...-native-windows-fix-app-bundled-fonts.json | 7 ++ .../Fabric/DWriteHelpers.cpp | 104 ++++++++++++++++++ .../Fabric/DWriteHelpers.h | 7 ++ .../WindowsTextLayoutManager.cpp | 4 +- 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 change/react-native-windows-fix-app-bundled-fonts.json diff --git a/change/react-native-windows-fix-app-bundled-fonts.json b/change/react-native-windows-fix-app-bundled-fonts.json new file mode 100644 index 00000000000..924e6ccec1c --- /dev/null +++ b/change/react-native-windows-fix-app-bundled-fonts.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Fabric: resolve app-bundled fonts (Assets, Assets\\Fonts) during text layout by merging them with the system font set into the DirectWrite font collection used by WindowsTextLayoutManager", + "packageName": "react-native-windows", + "email": "collindanielschneide@gmail.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.cpp b/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.cpp index 10d14e16709..62f3303c8f0 100644 --- a/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.cpp +++ b/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.cpp @@ -5,6 +5,11 @@ #include "DWriteHelpers.h" +#include +#include +#include +#include + namespace Microsoft::ReactNative { winrt::com_ptr<::IDWriteFactory> DWriteFactory() noexcept { @@ -16,4 +21,103 @@ winrt::com_ptr<::IDWriteFactory> DWriteFactory() noexcept { return s_dwriteFactory; } +namespace { + +// Directory that contains the running module, including the trailing separator: the +// package root for packaged (MSIX) apps and the directory next to the .exe for +// unpackaged apps. Bundled font assets are deployed below this directory. +std::wstring AppDirectory() noexcept { + wchar_t modulePath[MAX_PATH]{}; + const DWORD length = ::GetModuleFileNameW(nullptr, modulePath, MAX_PATH); + if (length == 0 || length >= MAX_PATH) { + return {}; + } + std::wstring path(modulePath, length); + const auto lastSeparator = path.find_last_of(L"\\/"); + if (lastSeparator == std::wstring::npos) { + return {}; + } + path.resize(lastSeparator + 1); + return path; +} + +// Adds every file matching + to the font-set builder and returns +// the number of files added. Per-file failures are skipped so that one bad font file +// cannot break font resolution for the rest of the app. +uint32_t AddFontFiles( + ::IDWriteFactory5 *factory, + ::IDWriteFontSetBuilder1 *builder, + const std::wstring &directory, + const wchar_t *pattern) noexcept { + uint32_t count = 0; + const std::wstring searchPattern = directory + pattern; + WIN32_FIND_DATAW findData{}; + const HANDLE findHandle = ::FindFirstFileW(searchPattern.c_str(), &findData); + if (findHandle == INVALID_HANDLE_VALUE) { + return count; + } + do { + if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { + const std::wstring fontPath = directory + findData.cFileName; + winrt::com_ptr<::IDWriteFontFile> fontFile; + if (SUCCEEDED(factory->CreateFontFileReference(fontPath.c_str(), nullptr, fontFile.put())) && + SUCCEEDED(builder->AddFontFile(fontFile.get()))) { + ++count; + } + } + } while (::FindNextFileW(findHandle, &findData)); + ::FindClose(findHandle); + return count; +} + +winrt::com_ptr<::IDWriteFontCollection> CreateAppFontCollection() noexcept { + try { + const std::wstring appDirectory = AppDirectory(); + if (appDirectory.empty()) { + return nullptr; + } + + const auto factory5 = DWriteFactory().as<::IDWriteFactory5>(); + + winrt::com_ptr<::IDWriteFontSetBuilder1> builder; + winrt::check_hresult(factory5->CreateFontSetBuilder(builder.put())); + + // Include the system font set so that system families keep resolving when this + // collection is used in place of the system collection. + winrt::com_ptr<::IDWriteFontSet> systemFontSet; + winrt::check_hresult(factory5->GetSystemFontSet(systemFontSet.put())); + winrt::check_hresult(builder->AddFontSet(systemFontSet.get())); + + uint32_t fontFileCount = 0; + for (const auto *subdirectory : {L"Assets\\", L"Assets\\Fonts\\"}) { + for (const auto *pattern : {L"*.ttf", L"*.otf"}) { + fontFileCount += AddFontFiles(factory5.get(), builder.get(), appDirectory + subdirectory, pattern); + } + } + if (fontFileCount == 0) { + // Nothing bundled: report "no app collection" so callers pass nullptr to DirectWrite + // and keep using DirectWrite's own (cached, updatable) system font collection. + return nullptr; + } + + winrt::com_ptr<::IDWriteFontSet> fontSet; + winrt::check_hresult(builder->CreateFontSet(fontSet.put())); + winrt::com_ptr<::IDWriteFontCollection1> collection; + winrt::check_hresult(factory5->CreateFontCollectionFromFontSet(fontSet.get(), collection.put())); + return collection.as<::IDWriteFontCollection>(); + } catch (...) { + // Fail closed: callers fall back to the system font collection (previous behavior). + return nullptr; + } +} + +} // namespace + +winrt::com_ptr<::IDWriteFontCollection> DWriteAppFontCollection() noexcept { + // Thread-safe (magic static) one-time initialization. Bundled font assets cannot + // change for the lifetime of the process, so the collection never needs rebuilding. + static const winrt::com_ptr<::IDWriteFontCollection> s_appFontCollection = CreateAppFontCollection(); + return s_appFontCollection; +} + } // namespace Microsoft::ReactNative diff --git a/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.h b/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.h index f46a1e6bd1a..6fdb4f7daf8 100644 --- a/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.h +++ b/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.h @@ -10,4 +10,11 @@ namespace Microsoft::ReactNative { winrt::com_ptr<::IDWriteFactory> DWriteFactory() noexcept; +// Font collection that merges the system font set with every font file bundled in the +// application's Assets\ and Assets\Fonts\ directories (*.ttf / *.otf), so app-bundled +// font families resolve during text layout exactly like installed fonts. Built once on +// first use. Returns nullptr when the app bundles no fonts or when the collection +// cannot be built; callers should treat nullptr as "use the system font collection". +winrt::com_ptr<::IDWriteFontCollection> DWriteAppFontCollection() noexcept; + } // namespace Microsoft::ReactNative diff --git a/vnext/Microsoft.ReactNative/Fabric/platform/react/renderer/textlayoutmanager/WindowsTextLayoutManager.cpp b/vnext/Microsoft.ReactNative/Fabric/platform/react/renderer/textlayoutmanager/WindowsTextLayoutManager.cpp index a63e0e2dca8..d37432ce924 100644 --- a/vnext/Microsoft.ReactNative/Fabric/platform/react/renderer/textlayoutmanager/WindowsTextLayoutManager.cpp +++ b/vnext/Microsoft.ReactNative/Fabric/platform/react/renderer/textlayoutmanager/WindowsTextLayoutManager.cpp @@ -116,7 +116,9 @@ void WindowsTextLayoutManager::GetTextLayout( outerFragment.textAttributes.fontFamily.empty() ? L"Segoe UI" : Microsoft::Common::Unicode::Utf8ToUtf16(outerFragment.textAttributes.fontFamily).c_str(), - nullptr, // Font collection (nullptr sets it to use the system font collection). + // Bundled app fonts merged over the system font set (nullptr when the app bundles + // no fonts, which selects the system font collection as before). + Microsoft::ReactNative::DWriteAppFontCollection().get(), static_cast(outerFragment.textAttributes.fontWeight.value_or( static_cast(DWRITE_FONT_WEIGHT_REGULAR))), style, From b7841a6eb196bc2215fa188961ae2b586dfa052d Mon Sep 17 00:00:00 2001 From: Collin Schneide <27441618+FaithfulAudio@users.noreply.github.com> Date: Tue, 4 Aug 2026 12:53:50 -0500 Subject: [PATCH 2/3] address review: keep the font-file search off every call, and off the hot path acoates-ms asked whether the font list can be cached so the file searches are not repeated, noting DWriteAppFontCollection is likely reached from more than one thread on first use. The directory enumeration already runs exactly once: s_appFontCollection is a function-local static with a dynamic initializer, so it is initialized a single time and concurrent first callers wait for that initialization rather than racing or repeating it ([stmt.dcl]/4). No call after the first touches the file system. That guarantee was load-bearing but only implied by the old comment, so it is now stated explicitly - including the thread-safety, which was the part worth being able to read off the page. What the old signature *did* cost on every call: GetTextLayout() invokes this once per text measure, and returning winrt::com_ptr by value put an AddRef/Release pair on that path for a pointer whose lifetime is already static and process-long. The accessor now returns a non-owning raw pointer, so the per-measure path has no refcount traffic at all; the static keeps the only reference. The call site drops its .get() accordingly. Not changed, and worth calling out in case it is the concern behind the question: the first call still does the enumeration inline, so whichever thread arrives first pays that cost and any thread arriving during it blocks. Moving that work off the measure path entirely (eager construction at instance setup) is a larger change and would be a behavioral one - happy to do it if that is what you would prefer here. --- .../Fabric/DWriteHelpers.cpp | 18 ++++++++++++++---- .../Fabric/DWriteHelpers.h | 18 ++++++++++++++---- .../WindowsTextLayoutManager.cpp | 2 +- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.cpp b/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.cpp index 62f3303c8f0..15f248fc89c 100644 --- a/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.cpp +++ b/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.cpp @@ -113,11 +113,21 @@ winrt::com_ptr<::IDWriteFontCollection> CreateAppFontCollection() noexcept { } // namespace -winrt::com_ptr<::IDWriteFontCollection> DWriteAppFontCollection() noexcept { - // Thread-safe (magic static) one-time initialization. Bundled font assets cannot - // change for the lifetime of the process, so the collection never needs rebuilding. +::IDWriteFontCollection *DWriteAppFontCollection() noexcept { + // One-time initialization, thread-safe by construction: a function-local static + // with a dynamic initializer is initialized exactly once, and concurrent callers + // that arrive during that window wait for it to complete rather than racing or + // repeating it ([stmt.dcl]/4). So the directory enumeration and the font-file + // references behind CreateAppFontCollection() happen on the first call only, + // whichever thread gets there first - subsequent calls never touch the file + // system. Bundled font assets cannot change while the process runs, so the + // collection never needs rebuilding. + // + // Held by value for the lifetime of the process and handed out as a non-owning + // raw pointer: GetTextLayout() calls this on every text measure, and returning a + // com_ptr by value would add an AddRef/Release pair to that path for no benefit. static const winrt::com_ptr<::IDWriteFontCollection> s_appFontCollection = CreateAppFontCollection(); - return s_appFontCollection; + return s_appFontCollection.get(); } } // namespace Microsoft::ReactNative diff --git a/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.h b/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.h index 6fdb4f7daf8..da083f37207 100644 --- a/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.h +++ b/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.h @@ -12,9 +12,19 @@ winrt::com_ptr<::IDWriteFactory> DWriteFactory() noexcept; // Font collection that merges the system font set with every font file bundled in the // application's Assets\ and Assets\Fonts\ directories (*.ttf / *.otf), so app-bundled -// font families resolve during text layout exactly like installed fonts. Built once on -// first use. Returns nullptr when the app bundles no fonts or when the collection -// cannot be built; callers should treat nullptr as "use the system font collection". -winrt::com_ptr<::IDWriteFontCollection> DWriteAppFontCollection() noexcept; +// font families resolve during text layout exactly like installed fonts. Returns +// nullptr when the app bundles no fonts or when the collection cannot be built; +// callers should treat nullptr as "use the system font collection". +// +// The collection - including the directory enumeration used to find the bundled font +// files - is built exactly once per process, on first use, and is then owned for the +// lifetime of the process. Initialization is thread-safe: concurrent first callers +// resolve to the same instance. +// +// Returns a NON-OWNING raw pointer on purpose. GetTextLayout() calls this on every +// text measure, so handing back a com_ptr by value would put an AddRef/Release pair +// on that path for a pointer whose lifetime is already static. Callers must not +// release it; take a com_ptr copy if they need to extend a reference. +::IDWriteFontCollection *DWriteAppFontCollection() noexcept; } // namespace Microsoft::ReactNative diff --git a/vnext/Microsoft.ReactNative/Fabric/platform/react/renderer/textlayoutmanager/WindowsTextLayoutManager.cpp b/vnext/Microsoft.ReactNative/Fabric/platform/react/renderer/textlayoutmanager/WindowsTextLayoutManager.cpp index d37432ce924..1d2b63c35af 100644 --- a/vnext/Microsoft.ReactNative/Fabric/platform/react/renderer/textlayoutmanager/WindowsTextLayoutManager.cpp +++ b/vnext/Microsoft.ReactNative/Fabric/platform/react/renderer/textlayoutmanager/WindowsTextLayoutManager.cpp @@ -118,7 +118,7 @@ void WindowsTextLayoutManager::GetTextLayout( : Microsoft::Common::Unicode::Utf8ToUtf16(outerFragment.textAttributes.fontFamily).c_str(), // Bundled app fonts merged over the system font set (nullptr when the app bundles // no fonts, which selects the system font collection as before). - Microsoft::ReactNative::DWriteAppFontCollection().get(), + Microsoft::ReactNative::DWriteAppFontCollection(), static_cast(outerFragment.textAttributes.fontWeight.value_or( static_cast(DWRITE_FONT_WEIGHT_REGULAR))), style, From 718cda42431ac15f3045e0788abc8c8a4dce9996 Mon Sep 17 00:00:00 2001 From: Collin Schneide <27441618+FaithfulAudio@users.noreply.github.com> Date: Wed, 5 Aug 2026 14:50:11 -0500 Subject: [PATCH 3/3] address review: cache the font-file list; fix the real first-use thread race Two changes, one per reading of the review comment. 1. The list of bundled font files is now its own cached static (AppFontFilePaths): the directory searches run exactly once per process and every consumer - including any future path that rebuilds a collection - reads the cached list and never touches the file system again. CreateAppFontCollection() contains no enumeration by construction. 2. The genuine first-use thread race was in DWriteFactory() itself: the existing `if (!s_dwriteFactory) { assign }` lazy-init is a data race when two threads make first use concurrently - and DWriteAppFontCollection() is reachable from more than one thread on first use, which makes that race live rather than theoretical. Converted to a function-local static with a dynamic initializer (thread-safe by [stmt.dcl]/4; /Zc:threadSafeInit is on by default and nothing in the RNW build disables it), so concurrent first callers wait for one initialization instead of racing it. Also removes the stray `#pragma once` this .cpp carried. --- .../Fabric/DWriteHelpers.cpp | 95 +++++++++++-------- 1 file changed, 57 insertions(+), 38 deletions(-) diff --git a/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.cpp b/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.cpp index 15f248fc89c..0a47a1cf38b 100644 --- a/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.cpp +++ b/vnext/Microsoft.ReactNative/Fabric/DWriteHelpers.cpp @@ -1,23 +1,30 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -#pragma once - #include "DWriteHelpers.h" #include #include #include #include +#include namespace Microsoft::ReactNative { winrt::com_ptr<::IDWriteFactory> DWriteFactory() noexcept { - static winrt::com_ptr<::IDWriteFactory> s_dwriteFactory; - if (!s_dwriteFactory) { + // Function-local static with a dynamic initializer: initialized exactly once, + // and concurrent first callers wait for that initialization rather than racing + // it ([stmt.dcl]/4, on by default in MSVC as /Zc:threadSafeInit). The previous + // `if (!s_dwriteFactory) { ...assign... }` pattern was a first-use data race: + // two threads could both observe the empty pointer and both create/assign a + // factory. DWriteAppFontCollection() below is reachable from more than one + // thread on first use, which makes that race live rather than theoretical. + static const winrt::com_ptr<::IDWriteFactory> s_dwriteFactory = [] { + winrt::com_ptr<::IDWriteFactory> factory; winrt::check_hresult(::DWriteCreateFactory( - DWRITE_FACTORY_TYPE_SHARED, __uuidof(s_dwriteFactory), reinterpret_cast<::IUnknown **>(s_dwriteFactory.put()))); - } + DWRITE_FACTORY_TYPE_SHARED, __uuidof(factory), reinterpret_cast<::IUnknown **>(factory.put()))); + return factory; + }(); return s_dwriteFactory; } @@ -41,39 +48,53 @@ std::wstring AppDirectory() noexcept { return path; } -// Adds every file matching + to the font-set builder and returns -// the number of files added. Per-file failures are skipped so that one bad font file -// cannot break font resolution for the rest of the app. -uint32_t AddFontFiles( - ::IDWriteFactory5 *factory, - ::IDWriteFontSetBuilder1 *builder, - const std::wstring &directory, - const wchar_t *pattern) noexcept { - uint32_t count = 0; +// Appends every file matching + to `paths`. Pure file-system +// enumeration - no DirectWrite objects are created here, so the result is cacheable +// independently of any factory or collection lifetime. +void AppendFontFiles(std::vector &paths, const std::wstring &directory, const wchar_t *pattern) noexcept { const std::wstring searchPattern = directory + pattern; WIN32_FIND_DATAW findData{}; const HANDLE findHandle = ::FindFirstFileW(searchPattern.c_str(), &findData); if (findHandle == INVALID_HANDLE_VALUE) { - return count; + return; } do { if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { - const std::wstring fontPath = directory + findData.cFileName; - winrt::com_ptr<::IDWriteFontFile> fontFile; - if (SUCCEEDED(factory->CreateFontFileReference(fontPath.c_str(), nullptr, fontFile.put())) && - SUCCEEDED(builder->AddFontFile(fontFile.get()))) { - ++count; - } + paths.emplace_back(directory + findData.cFileName); } } while (::FindNextFileW(findHandle, &findData)); ::FindClose(findHandle); - return count; } +// The cached list of bundled font files. The directory searches run exactly once per +// process, on whichever thread gets here first (thread-safe static initialization); +// every later caller - including any future path that rebuilds a collection - reads +// this list and never touches the file system again. Bundled assets cannot change +// while the process runs, so the list can never go stale. +const std::vector &AppFontFilePaths() noexcept { + static const std::vector s_paths = [] { + std::vector paths; + const std::wstring appDirectory = AppDirectory(); + if (!appDirectory.empty()) { + for (const auto *subdirectory : {L"Assets\\", L"Assets\\Fonts\\"}) { + for (const auto *pattern : {L"*.ttf", L"*.otf"}) { + AppendFontFiles(paths, appDirectory + subdirectory, pattern); + } + } + } + return paths; + }(); + return s_paths; +} + +// Builds the merged collection from the cached file list. Contains no directory +// enumeration by construction - see AppFontFilePaths(). winrt::com_ptr<::IDWriteFontCollection> CreateAppFontCollection() noexcept { try { - const std::wstring appDirectory = AppDirectory(); - if (appDirectory.empty()) { + const auto &fontFiles = AppFontFilePaths(); + if (fontFiles.empty()) { + // Nothing bundled: report "no app collection" so callers pass nullptr to DirectWrite + // and keep using DirectWrite's own (cached, updatable) system font collection. return nullptr; } @@ -88,15 +109,17 @@ winrt::com_ptr<::IDWriteFontCollection> CreateAppFontCollection() noexcept { winrt::check_hresult(factory5->GetSystemFontSet(systemFontSet.put())); winrt::check_hresult(builder->AddFontSet(systemFontSet.get())); + // Per-file failures are skipped so that one bad font file cannot break font + // resolution for the rest of the app. uint32_t fontFileCount = 0; - for (const auto *subdirectory : {L"Assets\\", L"Assets\\Fonts\\"}) { - for (const auto *pattern : {L"*.ttf", L"*.otf"}) { - fontFileCount += AddFontFiles(factory5.get(), builder.get(), appDirectory + subdirectory, pattern); + for (const auto &fontPath : fontFiles) { + winrt::com_ptr<::IDWriteFontFile> fontFile; + if (SUCCEEDED(factory5->CreateFontFileReference(fontPath.c_str(), nullptr, fontFile.put())) && + SUCCEEDED(builder->AddFontFile(fontFile.get()))) { + ++fontFileCount; } } if (fontFileCount == 0) { - // Nothing bundled: report "no app collection" so callers pass nullptr to DirectWrite - // and keep using DirectWrite's own (cached, updatable) system font collection. return nullptr; } @@ -114,14 +137,10 @@ winrt::com_ptr<::IDWriteFontCollection> CreateAppFontCollection() noexcept { } // namespace ::IDWriteFontCollection *DWriteAppFontCollection() noexcept { - // One-time initialization, thread-safe by construction: a function-local static - // with a dynamic initializer is initialized exactly once, and concurrent callers - // that arrive during that window wait for it to complete rather than racing or - // repeating it ([stmt.dcl]/4). So the directory enumeration and the font-file - // references behind CreateAppFontCollection() happen on the first call only, - // whichever thread gets there first - subsequent calls never touch the file - // system. Bundled font assets cannot change while the process runs, so the - // collection never needs rebuilding. + // One-time initialization, thread-safe by construction (same mechanism as the + // statics above): concurrent first callers wait rather than race or repeat. The + // underlying directory searches are cached separately in AppFontFilePaths(), so + // even a future change that rebuilds the collection can never re-run them. // // Held by value for the lifetime of the process and handed out as a non-owning // raw pointer: GetTextLayout() calls this on every text measure, and returning a