-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Managed DWrite] Migrate FontCollectionLoader to managed #6260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Kuldeep-MS
merged 1 commit into
dotnet:main
from
ThomasGoulet73:managed-dwrite-fontcollectionloader
Feb 27, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 0 additions & 63 deletions
63
src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontCollectionLoader.cpp
This file was deleted.
Oops, something went wrong.
56 changes: 0 additions & 56 deletions
56
src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontCollectionLoader.h
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/IFontSourceCollection.h
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...rosoft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/DWriteInterfaces.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace MS.Internal.Text.TextInterface | ||
{ | ||
/// <summary> | ||
/// The font collection loader interface is used to construct a collection of fonts given a particular type of key. | ||
/// The font collection loader interface is recommended to be implemented by a singleton object. | ||
/// IMPORTANT: font collection loader implementations must not register themselves with DirectWrite factory | ||
/// inside their constructors and must not unregister themselves in their destructors, because | ||
/// registration and unregistraton operations increment and decrement the object reference count respectively. | ||
/// Instead, registration and unregistration of font file loaders with DirectWrite factory should be performed | ||
/// outside of the font file loader implementation as a separate step. | ||
/// </summary> | ||
[ComImport] | ||
[Guid("cca920e4-52f0-492b-bfa8-29c72ee0a468")] | ||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] | ||
internal unsafe interface IDWriteFontCollectionLoaderMirror | ||
{ | ||
/// <summary> | ||
/// Creates a font file enumerator object that encapsulates a collection of font files. | ||
/// The font system calls back to this interface to create a font collection. | ||
/// </summary> | ||
/// <param name="collectionKey">Font collection key that uniquely identifies the collection of font files within | ||
/// the scope of the font collection loader being used.</param> | ||
/// <param name="collectionKeySize">Size of the font collection key in bytes.</param> | ||
/// <param name="fontFileEnumerator">Pointer to the newly created font file enumerator.</param> | ||
/// <returns> | ||
/// Standard HRESULT error code. | ||
/// </returns> | ||
[PreserveSig] | ||
int CreateEnumeratorFromKey( | ||
/*[In, MarshalAs(UnmanagedType.Interface)]*/ IntPtr factory, | ||
[In] void* collectionKey, | ||
[In, MarshalAs(UnmanagedType.U4)] uint collectionKeySize, | ||
/*[Out, MarshalAs(UnmanagedType.Interface)]*/ IntPtr* fontFileEnumerator | ||
); | ||
}; | ||
} |
82 changes: 82 additions & 0 deletions
82
...ft.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/FontCollectionLoader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using MS.Internal.Text.TextInterface.Interfaces; | ||
|
||
namespace MS.Internal.Text.TextInterface | ||
{ | ||
[ClassInterface(ClassInterfaceType.None)] | ||
[ComVisible(true)] | ||
internal unsafe class FontCollectionLoader : IDWriteFontCollectionLoaderMirror | ||
{ | ||
private const int S_OK = unchecked((int)0L); | ||
private const int E_INVALIDARG = unchecked((int)0x80070057L); | ||
|
||
private readonly IFontSourceCollectionFactory _fontSourceCollectionFactory; | ||
private readonly FontFileLoader _fontFileLoader; | ||
|
||
public FontCollectionLoader() | ||
{ | ||
Debug.Fail("Assertion failed"); | ||
} | ||
|
||
public FontCollectionLoader(IFontSourceCollectionFactory fontSourceCollectionFactory, FontFileLoader fontFileLoader) | ||
{ | ||
_fontSourceCollectionFactory = fontSourceCollectionFactory; | ||
_fontFileLoader = fontFileLoader; | ||
} | ||
|
||
/// <summary> | ||
/// Creates a font file enumerator object that encapsulates a collection of font files. | ||
/// The font system calls back to this interface to create a font collection. | ||
/// </summary> | ||
/// <param name="collectionKey">Font collection key that uniquely identifies the collection of font files within | ||
/// the scope of the font collection loader being used.</param> | ||
/// <param name="collectionKeySize">Size of the font collection key in bytes.</param> | ||
/// <param name="fontFileEnumerator">Pointer to the newly created font file enumerator.</param> | ||
/// <returns> | ||
/// Standard HRESULT error code. | ||
/// </returns> | ||
[ComVisible(true)] | ||
public int CreateEnumeratorFromKey(IntPtr factory, [In] void* collectionKey, [In, MarshalAs(UnmanagedType.U4)] uint collectionKeySize, IntPtr* fontFileEnumerator) | ||
Kuldeep-MS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
uint numberOfCharacters = collectionKeySize / sizeof(char); | ||
Kuldeep-MS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ((fontFileEnumerator == null) | ||
|| (collectionKeySize % sizeof(char) != 0) // The collectionKeySize must be divisible by sizeof(WCHAR) | ||
|| (numberOfCharacters <= 1) // The collectionKey cannot be less than or equal 1 character as it has to contain the NULL character. | ||
|| (((char*)collectionKey)[numberOfCharacters - 1] != '\0')) // The collectionKey must end with the NULL character | ||
{ | ||
return E_INVALIDARG; | ||
} | ||
|
||
*fontFileEnumerator = IntPtr.Zero; | ||
|
||
string uriString = new string((char*)collectionKey); | ||
int hr = S_OK; | ||
|
||
try | ||
{ | ||
IFontSourceCollection fontSourceCollection = _fontSourceCollectionFactory.Create(uriString); | ||
FontFileEnumerator fontFileEnum = new FontFileEnumerator( | ||
fontSourceCollection, | ||
_fontFileLoader, | ||
(Native.IDWriteFactory*)factory.ToPointer() | ||
); | ||
IntPtr pIDWriteFontFileEnumeratorMirror = Marshal.GetComInterfaceForObject( | ||
fontFileEnum, | ||
typeof(IDWriteFontFileEnumeratorMirror)); | ||
|
||
*fontFileEnumerator = pIDWriteFontFileEnumeratorMirror; | ||
} | ||
catch (Exception exception) | ||
{ | ||
hr = Marshal.GetHRForException(exception); | ||
} | ||
|
||
return hr; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...t.DotNet.Wpf/src/PresentationCore/MS/internal/Text/TextInterface/IFontSourceCollection.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace MS.Internal.Text.TextInterface | ||
{ | ||
internal interface IFontSourceCollection : IEnumerable<IFontSource> | ||
{ | ||
} | ||
|
||
internal interface IFontSourceCollectionFactory | ||
{ | ||
IFontSourceCollection Create(string uriString); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.