From b28fdf8139ad7169c6c2547f321e11702e3950d6 Mon Sep 17 00:00:00 2001 From: ThomasGoulet73 <51839772+ThomasGoulet73@users.noreply.github.com> Date: Thu, 27 Feb 2025 23:57:35 -0500 Subject: [PATCH] [Managed DWrite] Migrate FontFileEnumerator to managed Contributes to dotnet/wpf#5305 --- .../CPP/DWriteWrapper/FontFileEnumerator.cpp | 56 ------------- .../CPP/DWriteWrapper/FontFileEnumerator.h | 66 ---------------- .../CPP/DirectWriteForwarder.cpp | 1 - .../Text/TextInterface/DWriteInterfaces.cs | 35 ++++++++- .../TextInterface/FontCollectionLoader.cs | 4 +- .../Text/TextInterface/FontFileEnumerator.cs | 78 +++++++++++++++++++ .../PresentationCore/PresentationCore.csproj | 1 + 7 files changed, 115 insertions(+), 126 deletions(-) delete mode 100644 src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileEnumerator.cpp delete mode 100644 src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileEnumerator.h create mode 100644 src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/FontFileEnumerator.cs diff --git a/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileEnumerator.cpp b/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileEnumerator.cpp deleted file mode 100644 index 9837aca7b9c..00000000000 --- a/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileEnumerator.cpp +++ /dev/null @@ -1,56 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -#include "FontFileEnumerator.h" -#include "Factory.h" - -namespace MS { namespace Internal { namespace Text { namespace TextInterface -{ - FontFileEnumerator::FontFileEnumerator( - IEnumerable^ fontSourceCollection, - FontFileLoader^ fontFileLoader, - IDWriteFactory* factory - ) - { - _fontSourceCollectionEnumerator = fontSourceCollection->GetEnumerator(); - _fontFileLoader = fontFileLoader; - factory->AddRef(); - _factory = factory; - } - - [ComVisible(true)] - HRESULT FontFileEnumerator::MoveNext( - __out bool% hasCurrentFile - ) - { - HRESULT hr = S_OK; - try - { - hasCurrentFile = _fontSourceCollectionEnumerator->MoveNext(); - } - catch(System::Exception^ exception) - { - hr = System::Runtime::InteropServices::Marshal::GetHRForException(exception); - } - - return hr; - } - - HRESULT FontFileEnumerator::GetCurrentFontFile( - __out IDWriteFontFile** fontFile - ) - { - if (fontFile == 0) - { - return E_INVALIDARG; - } - - return InternalFactory::CreateFontFile( - _factory, - _fontFileLoader, - _fontSourceCollectionEnumerator->Current->Uri, - fontFile - ); - } -}}}}//MS::Internal::Text::TextInterface diff --git a/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileEnumerator.h b/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileEnumerator.h deleted file mode 100644 index b72985ff7fb..00000000000 --- a/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileEnumerator.h +++ /dev/null @@ -1,66 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -#ifndef __FONTFILEENUMERATOR_H -#define __FONTFILEENUMERATOR_H - -#include "IFontSource.h" -#include "Common.h" -#include "FontFileLoader.h" -#include "DWriteInterfaces.h" - -using namespace System; -using namespace System::Collections::Generic; -using namespace System::Diagnostics; - -namespace MS { namespace Internal { namespace Text { namespace TextInterface -{ - [ClassInterface(ClassInterfaceType::None), ComVisible(true)] - private ref class FontFileEnumerator : public IDWriteFontFileEnumeratorMirror - { - private: - - IEnumerator^ _fontSourceCollectionEnumerator; - FontFileLoader^ _fontFileLoader; - IDWriteFactory* _factory; - - public: - - FontFileEnumerator() { Debug::Fail("Assertion failed"); } - - FontFileEnumerator( - IEnumerable^ fontSourceCollection, - FontFileLoader^ fontFileLoader, - IDWriteFactory* factory - ); - - /// - /// Advances to the next font file in the collection. When it is first created, the enumerator is positioned - /// before the first element of the collection and the first call to MoveNext advances to the first file. - /// - /// Receives the value TRUE if the enumerator advances to a file, or FALSE if - /// the enumerator advanced past the last file in the collection. - /// - /// Standard HRESULT error code. - /// - [ComVisible(true)] - virtual HRESULT MoveNext( - __out bool% hasCurrentFile - ); - - /// - /// Gets a reference to the current font file. - /// - /// Pointer to the newly created font file object. - /// - /// Standard HRESULT error code. - /// - [ComVisible(true)] - virtual HRESULT GetCurrentFontFile( - __out IDWriteFontFile** fontFile - ); - }; -}}}}//MS::Internal::Text::TextInterface - -#endif //__FONTFILEENUMERATOR_H diff --git a/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DirectWriteForwarder.cpp b/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DirectWriteForwarder.cpp index 254ef87ecf5..5599dbb16c2 100644 --- a/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DirectWriteForwarder.cpp +++ b/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DirectWriteForwarder.cpp @@ -18,7 +18,6 @@ #include "DWriteWrapper\LocalizedStrings.cpp" #include "DWriteWrapper\NativePointerWrapper.cpp" -#include "DWriteWrapper\FontFileEnumerator.cpp" #include "DWriteWrapper\FontFileLoader.cpp" #include "DWriteWrapper\FontFileStream.cpp" diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/DWriteInterfaces.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/DWriteInterfaces.cs index 8da45bb7c27..023f4d6c5ef 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/DWriteInterfaces.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/DWriteInterfaces.cs @@ -3,6 +3,7 @@ using System; using System.Runtime.InteropServices; +using MS.Internal.Interop.DWrite; namespace MS.Internal.Text.TextInterface { @@ -38,5 +39,37 @@ int CreateEnumeratorFromKey( [In, MarshalAs(UnmanagedType.U4)] uint collectionKeySize, /*[Out, MarshalAs(UnmanagedType.Interface)]*/ IntPtr* fontFileEnumerator ); - }; + } + + /// + /// The font file enumerator interface encapsulates a collection of font files. The font system uses this interface + /// to enumerate font files when building a font collection. + /// + [ComImport] + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + [Guid("72755049-5ff7-435d-8348-4be97cfa6c7c")] + internal interface IDWriteFontFileEnumeratorMirror + { + /// + /// Advances to the next font file in the collection. When it is first created, the enumerator is positioned + /// before the first element of the collection and the first call to MoveNext advances to the first file. + /// + /// Receives the value TRUE if the enumerator advances to a file, or FALSE if + /// the enumerator advanced past the last file in the collection. + /// + /// Standard HRESULT error code. + /// + [PreserveSig] + int MoveNext([MarshalAs(UnmanagedType.Bool)] out bool hasCurrentFile); + + /// + /// Gets a reference to the current font file. + /// + /// Pointer to the newly created font file object. + /// + /// Standard HRESULT error code. + /// + [PreserveSig] + unsafe int GetCurrentFontFile(/*[Out, MarshalAs(UnmanagedType.Interface)]*/ IDWriteFontFile** fontFile); + } } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/FontCollectionLoader.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/FontCollectionLoader.cs index 8e5697db6d9..f7fbe10079e 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/FontCollectionLoader.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/FontCollectionLoader.cs @@ -4,7 +4,7 @@ using System; using System.Diagnostics; using System.Runtime.InteropServices; -using MS.Internal.Text.TextInterface.Interfaces; +using MS.Internal.Interop.DWrite; namespace MS.Internal.Text.TextInterface { @@ -63,7 +63,7 @@ public int CreateEnumeratorFromKey(IntPtr factory, [In] void* collectionKey, [In FontFileEnumerator fontFileEnum = new FontFileEnumerator( fontSourceCollection, _fontFileLoader, - (Native.IDWriteFactory*)factory.ToPointer() + (IDWriteFactory*)factory.ToPointer() ); IntPtr pIDWriteFontFileEnumeratorMirror = Marshal.GetComInterfaceForObject( fontFileEnum, diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/FontFileEnumerator.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/FontFileEnumerator.cs new file mode 100644 index 00000000000..7701ec0acf7 --- /dev/null +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/FontFileEnumerator.cs @@ -0,0 +1,78 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.InteropServices; +using MS.Internal.Interop.DWrite; + +namespace MS.Internal.Text.TextInterface +{ + [ClassInterface(ClassInterfaceType.None)] + [ComVisible(true)] + internal unsafe class FontFileEnumerator : IDWriteFontFileEnumeratorMirror + { + private const int S_OK = unchecked((int)0L); + private const int E_INVALIDARG = unchecked((int)0x80070057L); + + private readonly IEnumerator _fontSourceCollectionEnumerator; + private readonly FontFileLoader _fontFileLoader; + private readonly IDWriteFactory* _factory; + + public FontFileEnumerator() + { + Debug.Fail("Assertion failed"); + } + + public FontFileEnumerator(IEnumerable fontSourceCollection, FontFileLoader fontFileLoader, IDWriteFactory* factory) + { + _fontSourceCollectionEnumerator = fontSourceCollection.GetEnumerator(); + _fontFileLoader = fontFileLoader; + factory->AddRef(); + _factory = factory; + } + + /// + /// Advances to the next font file in the collection. When it is first created, the enumerator is positioned + /// before the first element of the collection and the first call to MoveNext advances to the first file. + /// + /// Receives the value TRUE if the enumerator advances to a file, or FALSE if + /// the enumerator advanced past the last file in the collection. + /// + /// Standard HRESULT error code. + /// + [ComVisible(true)] + public int MoveNext([MarshalAs(UnmanagedType.Bool)] out bool hasCurrentFile) + { + try + { + hasCurrentFile = _fontSourceCollectionEnumerator.MoveNext(); + return S_OK; + } + catch (Exception exception) + { + hasCurrentFile = default; + return Marshal.GetHRForException(exception); + } + } + + /// + /// Gets a reference to the current font file. + /// + /// Pointer to the newly created font file object. + /// + /// Standard HRESULT error code. + /// + [ComVisible(true)] + public unsafe int GetCurrentFontFile(IDWriteFontFile** fontFile) + { + if (fontFile is null) + return E_INVALIDARG; + + return InternalFactory.CreateFontFile( + (Native.IDWriteFactory*)_factory, + _fontFileLoader, + _fontSourceCollectionEnumerator.Current.Uri, + (Native.IDWriteFontFile**)fontFile + ); + } + } +} diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/PresentationCore.csproj b/src/Microsoft.DotNet.Wpf/src/PresentationCore/PresentationCore.csproj index e1fbbb77b0b..959499b31ec 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/PresentationCore.csproj +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/PresentationCore.csproj @@ -277,6 +277,7 @@ +