Repository: microsoft/terminal
File: src/renderer/base/FontCache.h (C/C++)

Code before:

                wil::com_ptr<IDWriteFontCollection> systemFontCollection;
                THROW_IF_FAILED(factory->GetSystemFontCollection(systemFontCollection.addressof(), forceUpdate));

                // IDWriteFactory5 is supported since Windows 10, build 15021.
                const auto factory5 = factory.try_query<IDWriteFactory5>();
                if (!factory5)
                {
                    _fontCollection = systemFontCollection;
                    return;
                }

                // Cache nearby files.
                if (!_nearbyFilesFound)
                {
                    _nearbyFilesFound = true;

                    const std::filesystem::path module{ wil::GetModuleFileNameW<std::wstring>(nullptr) };
                    const auto folder{ module.parent_path() };

                    for (const auto& p : std::filesystem::directory_iterator(folder))
                    {
                        if (til::ends_with(p.path().native(), L".ttf"))
                        {
                            wil::com_ptr<IDWriteFontFile> fontFile;
                            if (SUCCEEDED_LOG(factory5->CreateFontFileReference(p.path().c_str(), nullptr, fontFile.addressof())))
                            {
                                _nearbyFiles.emplace_back(std::move(fontFile));
                            }
                        }
                    }

                    _nearbyFiles.shrink_to_fit();
                }

                wil::com_ptr<IDWriteFontSet> systemFontSet;
                // IDWriteFontCollection1 is supported since Windows 7.
                THROW_IF_FAILED(systemFontCollection.query<IDWriteFontCollection1>()->GetFontSet(systemFontSet.addressof()));

                wil::com_ptr<IDWriteFontSetBuilder1> fontSetBuilder;
                THROW_IF_FAILED(factory5->CreateFontSetBuilder(fontSetBuilder.addressof()));
                THROW_IF_FAILED(fontSetBuilder->AddFontSet(systemFontSet.get()));

                for (const auto& file : _nearbyFiles)
                {
                    LOG_IF_FAILED(fontSetBuilder->AddFontFile(file.get()));
                }

                wil::com_ptr<IDWriteFontSet> fontSet;
                THROW_IF_FAILED(fontSetBuilder->CreateFontSet(fontSet.addressof()));


Review comment (bug):
will this log generate a telemetry event using WIL's registered logging handler? Should we log on success? Am I getting this backwards, and it logs if failed only?

Code after:

            wil::com_ptr<IDWriteFontCollection> systemFontCollection;
            THROW_IF_FAILED(factory->GetSystemFontCollection(systemFontCollection.addressof(), forceUpdate));

            if constexpr (Feature_NearbyFontLoading::IsEnabled())
            {
                // IDWriteFactory5 is supported since Windows 10, build 15021.
                const auto factory5 = factory.try_query<IDWriteFactory5>();
                if (!factory5)
                {
                    return systemFontCollection;
                }

                const auto& nearbyFontFiles = getNearbyFontFiles(factory5.get());
                if (nearbyFontFiles.empty())
                {
                    return systemFontCollection;
                }

                wil::com_ptr<IDWriteFontSet> systemFontSet;
                // IDWriteFontCollection1 is supported since Windows 7.
                THROW_IF_FAILED(systemFontCollection.query<IDWriteFontCollection1>()->GetFontSet(systemFontSet.addressof()));

                wil::com_ptr<IDWriteFontSetBuilder1> fontSetBuilder;
                THROW_IF_FAILED(factory5->CreateFontSetBuilder(fontSetBuilder.addressof()));
                THROW_IF_FAILED(fontSetBuilder->AddFontSet(systemFontSet.get()));

                for (const auto& file : nearbyFontFiles)
                {
                    LOG_IF_FAILED(fontSetBuilder->AddFontFile(file.get()));
                }

                wil::com_ptr<IDWriteFontSet> fontSet;
                THROW_IF_FAILED(fontSetBuilder->CreateFontSet(fontSet.addressof()));

                wil::com_ptr<IDWriteFontCollection1> fontCollection;
                THROW_IF_FAILED(factory5->CreateFontCollectionFromFontSet(fontSet.get(), fontCollection.addressof()));

                return std::move(fontCollection);
            }
            else
            {
                return systemFontCollection;
            }
        }
    }

    inline wil::com_ptr<IDWriteFontCollection> GetCached()
    {
        return details::getFontCollection(false);
    }