Skip to content

Commit

Permalink
Migrate DWrite FontCollectionLoader to managed.
Browse files Browse the repository at this point in the history
Contributes to dotnet#5305
  • Loading branch information
ThomasGoulet73 committed Oct 27, 2023
1 parent 57e86a2 commit 7a8760a
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,38 +150,6 @@ namespace MS { namespace Internal { namespace Text { namespace TextInterface { n
/*[Out, MarshalAs(UnmanagedType::Interface)]*/ IDWriteFontFile** fontFile
);
};

/// <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)]
interface class 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]
HRESULT CreateEnumeratorFromKey(
/*[In, MarshalAs(UnmanagedType::Interface)]*/ IntPtr factory,
[In] void const* collectionKey,
[In, MarshalAs(UnmanagedType::U4)] UINT32 collectionKeySize,
/*[Out, MarshalAs(UnmanagedType::Interface)]*/ IntPtr* fontFileEnumerator
);
};

}}}}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include "FontFace.h"
#include "FontCollection.h"
#include "DWriteTypeConverter.h"
#include "IFontSourceCollection.h"
#include "FontCollectionLoader.h"
#include "FontFileLoader.h"
#include "TextAnalyzer.h"
#include "NativePointerWrapper.h"
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "DWriteWrapper\LocalizedStrings.cpp"
#include "DWriteWrapper\NativePointerWrapper.cpp"

#include "DWriteWrapper\FontCollectionLoader.cpp"
#include "DWriteWrapper\FontFileEnumerator.cpp"
#include "DWriteWrapper\FontFileLoader.cpp"
#include "DWriteWrapper\FontFileStream.cpp"
Expand Down
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
);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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;
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(IFontSourceCollectionFactory fontSourceCollectionFactory, FontFileLoader fontFileLoader)
{
_fontSourceCollectionFactory = fontSourceCollectionFactory;
_fontFileLoader = fontFileLoader;
}

[ComVisible(true)]
public int CreateEnumeratorFromKey(IntPtr factory, [In] void* collectionKey, [In, MarshalAs(UnmanagedType.U4)] uint collectionKeySize, IntPtr* fontFileEnumerator)
{
uint numberOfCharacters = collectionKeySize / sizeof(char);
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;
}
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,12 @@
<Compile Include="MS\Internal\Shaping\Substitution.cs" />
<Compile Include="MS\Internal\Shaping\TypefaceMap.cs" />
<Compile Include="MS\Internal\Shaping\UshortList2.cs" />
<Compile Include="MS\Internal\Text\TextInterface\DWriteInterfaces.cs" />
<Compile Include="MS\Internal\Text\TextInterface\DWriteLoader.cs" />
<Compile Include="MS\Internal\Text\TextInterface\DWriteUtil.cs" />
<Compile Include="MS\Internal\Text\TextInterface\Factory.cs" />
<Compile Include="MS\Internal\Text\TextInterface\FontCollectionLoader.cs" />
<Compile Include="MS\Internal\Text\TextInterface\IFontSourceCollection.cs" />
<Compile Include="MS\Internal\TextFormatting\Bidi.cs" />
<Compile Include="MS\Internal\TextFormatting\CultureMapper.cs" />
<Compile Include="MS\Internal\TextFormatting\DrawingState.cs" />
Expand Down

0 comments on commit 7a8760a

Please sign in to comment.