Skip to content

Commit 2dd5601

Browse files
ThomasGoulet73kant2002
authored andcommitted
Migrate DWrite FontCollectionLoader to managed.
Contributes to dotnet#5305
1 parent 881d8f0 commit 2dd5601

File tree

10 files changed

+126
-178
lines changed

10 files changed

+126
-178
lines changed

src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/DWriteInterfaces.h

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -150,38 +150,6 @@ namespace MS { namespace Internal { namespace Text { namespace TextInterface { n
150150
/*[Out, MarshalAs(UnmanagedType::Interface)]*/ IDWriteFontFile** fontFile
151151
);
152152
};
153-
154-
/// <summary>
155-
/// The font collection loader interface is used to construct a collection of fonts given a particular type of key.
156-
/// The font collection loader interface is recommended to be implemented by a singleton object.
157-
/// IMPORTANT: font collection loader implementations must not register themselves with DirectWrite factory
158-
/// inside their constructors and must not unregister themselves in their destructors, because
159-
/// registration and unregistraton operations increment and decrement the object reference count respectively.
160-
/// Instead, registration and unregistration of font file loaders with DirectWrite factory should be performed
161-
/// outside of the font file loader implementation as a separate step.
162-
/// </summary>
163-
[ComImport(), Guid("cca920e4-52f0-492b-bfa8-29c72ee0a468"), InterfaceType(ComInterfaceType::InterfaceIsIUnknown)]
164-
interface class IDWriteFontCollectionLoaderMirror
165-
{
166-
/// <summary>
167-
/// Creates a font file enumerator object that encapsulates a collection of font files.
168-
/// The font system calls back to this interface to create a font collection.
169-
/// </summary>
170-
/// <param name="collectionKey">Font collection key that uniquely identifies the collection of font files within
171-
/// the scope of the font collection loader being used.</param>
172-
/// <param name="collectionKeySize">Size of the font collection key in bytes.</param>
173-
/// <param name="fontFileEnumerator">Pointer to the newly created font file enumerator.</param>
174-
/// <returns>
175-
/// Standard HRESULT error code.
176-
/// </returns>
177-
[PreserveSig]
178-
HRESULT CreateEnumeratorFromKey(
179-
/*[In, MarshalAs(UnmanagedType::Interface)]*/ IntPtr factory,
180-
[In] void const* collectionKey,
181-
[In, MarshalAs(UnmanagedType::U4)] UINT32 collectionKeySize,
182-
/*[Out, MarshalAs(UnmanagedType::Interface)]*/ IntPtr* fontFileEnumerator
183-
);
184-
};
185153

186154
}}}}}
187155

src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/Factory.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include "FontFace.h"
1212
#include "FontCollection.h"
1313
#include "DWriteTypeConverter.h"
14-
#include "IFontSourceCollection.h"
15-
#include "FontCollectionLoader.h"
1614
#include "FontFileLoader.h"
1715
#include "TextAnalyzer.h"
1816
#include "NativePointerWrapper.h"

src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontCollectionLoader.cpp

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontCollectionLoader.h

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/IFontSourceCollection.h

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DirectWriteForwarder.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "DWriteWrapper\LocalizedStrings.cpp"
1919
#include "DWriteWrapper\NativePointerWrapper.cpp"
2020

21-
#include "DWriteWrapper\FontCollectionLoader.cpp"
2221
#include "DWriteWrapper\FontFileEnumerator.cpp"
2322
#include "DWriteWrapper\FontFileLoader.cpp"
2423
#include "DWriteWrapper\FontFileStream.cpp"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
namespace MS.Internal.Text.TextInterface
8+
{
9+
/// <summary>
10+
/// The font collection loader interface is used to construct a collection of fonts given a particular type of key.
11+
/// The font collection loader interface is recommended to be implemented by a singleton object.
12+
/// IMPORTANT: font collection loader implementations must not register themselves with DirectWrite factory
13+
/// inside their constructors and must not unregister themselves in their destructors, because
14+
/// registration and unregistraton operations increment and decrement the object reference count respectively.
15+
/// Instead, registration and unregistration of font file loaders with DirectWrite factory should be performed
16+
/// outside of the font file loader implementation as a separate step.
17+
/// </summary>
18+
[ComImport]
19+
[Guid("cca920e4-52f0-492b-bfa8-29c72ee0a468")]
20+
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
21+
internal unsafe interface IDWriteFontCollectionLoaderMirror
22+
{
23+
/// <summary>
24+
/// Creates a font file enumerator object that encapsulates a collection of font files.
25+
/// The font system calls back to this interface to create a font collection.
26+
/// </summary>
27+
/// <param name="collectionKey">Font collection key that uniquely identifies the collection of font files within
28+
/// the scope of the font collection loader being used.</param>
29+
/// <param name="collectionKeySize">Size of the font collection key in bytes.</param>
30+
/// <param name="fontFileEnumerator">Pointer to the newly created font file enumerator.</param>
31+
/// <returns>
32+
/// Standard HRESULT error code.
33+
/// </returns>
34+
[PreserveSig]
35+
int CreateEnumeratorFromKey(
36+
/*[In, MarshalAs(UnmanagedType.Interface)]*/ IntPtr factory,
37+
[In] void* collectionKey,
38+
[In, MarshalAs(UnmanagedType.U4)] uint collectionKeySize,
39+
/*[Out, MarshalAs(UnmanagedType.Interface)]*/ IntPtr* fontFileEnumerator
40+
);
41+
};
42+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
using MS.Internal.Text.TextInterface.Interfaces;
7+
8+
namespace MS.Internal.Text.TextInterface
9+
{
10+
[ClassInterface(ClassInterfaceType.None)]
11+
[ComVisible(true)]
12+
internal unsafe class FontCollectionLoader : IDWriteFontCollectionLoaderMirror
13+
{
14+
private const int S_OK = unchecked((int)0L);
15+
private const int E_INVALIDARG = unchecked((int)0x80070057L);
16+
17+
private readonly IFontSourceCollectionFactory _fontSourceCollectionFactory;
18+
private readonly FontFileLoader _fontFileLoader;
19+
20+
public FontCollectionLoader(IFontSourceCollectionFactory fontSourceCollectionFactory, FontFileLoader fontFileLoader)
21+
{
22+
_fontSourceCollectionFactory = fontSourceCollectionFactory;
23+
_fontFileLoader = fontFileLoader;
24+
}
25+
26+
[ComVisible(true)]
27+
public int CreateEnumeratorFromKey(IntPtr factory, [In] void* collectionKey, [In, MarshalAs(UnmanagedType.U4)] uint collectionKeySize, IntPtr* fontFileEnumerator)
28+
{
29+
uint numberOfCharacters = collectionKeySize / sizeof(char);
30+
if ((fontFileEnumerator == null)
31+
|| (collectionKeySize % sizeof(char) != 0) // The collectionKeySize must be divisible by sizeof(WCHAR)
32+
|| (numberOfCharacters <= 1) // The collectionKey cannot be less than or equal 1 character as it has to contain the NULL character.
33+
|| (((char*)collectionKey)[numberOfCharacters - 1] != '\0')) // The collectionKey must end with the NULL character
34+
{
35+
return E_INVALIDARG;
36+
}
37+
38+
*fontFileEnumerator = IntPtr.Zero;
39+
40+
string uriString = new string((char*)collectionKey);
41+
int hr = S_OK;
42+
43+
try
44+
{
45+
IFontSourceCollection fontSourceCollection = _fontSourceCollectionFactory.Create(uriString);
46+
FontFileEnumerator fontFileEnum = new FontFileEnumerator(
47+
fontSourceCollection,
48+
_fontFileLoader,
49+
(Native.IDWriteFactory*)factory.ToPointer()
50+
);
51+
IntPtr pIDWriteFontFileEnumeratorMirror = Marshal.GetComInterfaceForObject(
52+
fontFileEnum,
53+
typeof(IDWriteFontFileEnumeratorMirror));
54+
55+
*fontFileEnumerator = pIDWriteFontFileEnumeratorMirror;
56+
}
57+
catch (Exception exception)
58+
{
59+
hr = Marshal.GetHRForException(exception);
60+
}
61+
62+
return hr;
63+
}
64+
}
65+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
6+
namespace MS.Internal.Text.TextInterface
7+
{
8+
internal interface IFontSourceCollection : IEnumerable<IFontSource>
9+
{
10+
}
11+
12+
internal interface IFontSourceCollectionFactory
13+
{
14+
IFontSourceCollection Create(string uriString);
15+
}
16+
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/PresentationCore.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,12 @@
289289
<Compile Include="MS\Internal\Shaping\Substitution.cs" />
290290
<Compile Include="MS\Internal\Shaping\TypefaceMap.cs" />
291291
<Compile Include="MS\Internal\Shaping\UshortList2.cs" />
292+
<Compile Include="MS\Internal\Text\TextInterface\DWriteInterfaces.cs" />
292293
<Compile Include="MS\Internal\Text\TextInterface\DWriteLoader.cs" />
293294
<Compile Include="MS\Internal\Text\TextInterface\DWriteUtil.cs" />
294295
<Compile Include="MS\Internal\Text\TextInterface\Factory.cs" />
296+
<Compile Include="MS\Internal\Text\TextInterface\FontCollectionLoader.cs" />
297+
<Compile Include="MS\Internal\Text\TextInterface\IFontSourceCollection.cs" />
295298
<Compile Include="MS\Internal\TextFormatting\Bidi.cs" />
296299
<Compile Include="MS\Internal\TextFormatting\CultureMapper.cs" />
297300
<Compile Include="MS\Internal\TextFormatting\DrawingState.cs" />

0 commit comments

Comments
 (0)