Repository: microsoft/terminal
File: src/renderer/dx/DxFontRenderData.cpp (C++)

Code before:

[[nodiscard]] Microsoft::WRL::ComPtr<IDWriteTextAnalyzer1> DxFontRenderData::Analyzer() noexcept
{
    return _dwriteTextAnalyzer;
}

[[nodiscard]] Microsoft::WRL::ComPtr<IDWriteFontFallback> DxFontRenderData::SystemFontFallback()
{
    if (!_systemFontFallback)
    {
        ::Microsoft::WRL::ComPtr<IDWriteFactory2> factory2;
        THROW_IF_FAILED(_dwriteFactory.As(&factory2));
        factory2->GetSystemFontFallback(&_systemFontFallback);
    }

    return _systemFontFallback;
}

// Routine Description:
// - Creates a DirectWrite font collection of font files that are sitting next to the running
//   binary (in the same directory as the EXE).
// Arguments:
// - <none>
// Return Value:
// - DirectWrite font collection
[[nodiscard]] Microsoft::WRL::ComPtr<IDWriteFontCollection1> DxFontRenderData::NearbyCollection() const
{
    // Magic static so we only attempt to grovel the hard disk once no matter how many instances
    // of the font collection itself we require.
    static const auto knownPaths = s_GetNearbyFonts();

    if (!_nearbyCollection)
    {
        // Factory3 has a convenience to get us a font set builder.
        ::Microsoft::WRL::ComPtr<IDWriteFactory3> factory3;
        THROW_IF_FAILED(_dwriteFactory.As(&factory3));

        ::Microsoft::WRL::ComPtr<IDWriteFontSetBuilder> fontSetBuilder;
        THROW_IF_FAILED(factory3->CreateFontSetBuilder(&fontSetBuilder));

        // Builder2 has a convenience to just feed in paths to font files.
        ::Microsoft::WRL::ComPtr<IDWriteFontSetBuilder2> fontSetBuilder2;
        THROW_IF_FAILED(fontSetBuilder.As(&fontSetBuilder2));

        // Find the directory we're running from then enumerate all the TTF files
        // sitting next to us.
        const std::filesystem::path module{ wil::GetModuleFileNameW<std::wstring>(nullptr) };
        const auto folder = module.parent_path();

        for (auto& p : knownPaths)
        {

Review comment (suggestion):
nit: the return signature for this function ensures that everybody gets an AddRef and Release. You could instead use `const &` on the return to save a single addref/release in places like `NearbyCollection().Thing()`

Code after:
    _lineMetrics({}),
    _boxDrawingEffect{}
{
}

[[nodiscard]] Microsoft::WRL::ComPtr<IDWriteTextAnalyzer1> DxFontRenderData::Analyzer() noexcept
{
    return _dwriteTextAnalyzer;
}

[[nodiscard]] Microsoft::WRL::ComPtr<IDWriteFontFallback> DxFontRenderData::SystemFontFallback()
{
    if (!_systemFontFallback)
    {
        ::Microsoft::WRL::ComPtr<IDWriteFactory2> factory2;
        THROW_IF_FAILED(_dwriteFactory.As(&factory2));
        factory2->GetSystemFontFallback(&_systemFontFallback);
    }

    return _systemFontFallback;
}

// Routine Description:
// - Creates a DirectWrite font collection of font files that are sitting next to the running
//   binary (in the same directory as the EXE).
// Arguments:
// - <none>
// Return Value:
// - DirectWrite font collection. May be null if one cannot be created.
[[nodiscard]] const Microsoft::WRL::ComPtr<IDWriteFontCollection1>& DxFontRenderData::NearbyCollection() const
{
    // Magic static so we only attempt to grovel the hard disk once no matter how many instances
    // of the font collection itself we require.
    static const auto knownPaths = s_GetNearbyFonts();

    // The convenience interfaces for loading fonts from files
    // are only available on Windows 10+.
    // Don't try to look up if below that OS version.
    static const bool s_isWindows10OrGreater = IsWindows10OrGreater();

    if (s_isWindows10OrGreater && !_nearbyCollection)
    {
        // Factory3 has a convenience to get us a font set builder.
        ::Microsoft::WRL::ComPtr<IDWriteFactory3> factory3;
        THROW_IF_FAILED(_dwriteFactory.As(&factory3));

        ::Microsoft::WRL::ComPtr<IDWriteFontSetBuilder> fontSetBuilder;
        THROW_IF_FAILED(factory3->CreateFontSetBuilder(&fontSetBuilder));

        // Builder2 has a convenience to just feed in paths to font files.
        ::Microsoft::WRL::ComPtr<IDWriteFontSetBuilder2> fontSetBuilder2;