Skip to content

Commit f377cd2

Browse files
Migrate DWrite FontCollectionLoader to managed.
Contributes to dotnet#5305
1 parent 793610e commit f377cd2

File tree

10 files changed

+143
-178
lines changed

10 files changed

+143
-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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.Diagnostics;
6+
using System.Runtime.InteropServices;
7+
using MS.Internal.Text.TextInterface.Interfaces;
8+
9+
namespace MS.Internal.Text.TextInterface
10+
{
11+
[ClassInterface(ClassInterfaceType.None)]
12+
[ComVisible(true)]
13+
internal unsafe class FontCollectionLoader : IDWriteFontCollectionLoaderMirror
14+
{
15+
private const int S_OK = unchecked((int)0L);
16+
private const int E_INVALIDARG = unchecked((int)0x80070057L);
17+
18+
private readonly IFontSourceCollectionFactory _fontSourceCollectionFactory;
19+
private readonly FontFileLoader _fontFileLoader;
20+
21+
public FontCollectionLoader()
22+
{
23+
Debug.Fail("Assertion failed");
24+
}
25+
26+
public FontCollectionLoader(IFontSourceCollectionFactory fontSourceCollectionFactory, FontFileLoader fontFileLoader)
27+
{
28+
_fontSourceCollectionFactory = fontSourceCollectionFactory;
29+
_fontFileLoader = fontFileLoader;
30+
}
31+
32+
/// <summary>
33+
/// Creates a font file enumerator object that encapsulates a collection of font files.
34+
/// The font system calls back to this interface to create a font collection.
35+
/// </summary>
36+
/// <param name="collectionKey">Font collection key that uniquely identifies the collection of font files within
37+
/// the scope of the font collection loader being used.</param>
38+
/// <param name="collectionKeySize">Size of the font collection key in bytes.</param>
39+
/// <param name="fontFileEnumerator">Pointer to the newly created font file enumerator.</param>
40+
/// <returns>
41+
/// Standard HRESULT error code.
42+
/// </returns>
43+
[ComVisible(true)]
44+
public int CreateEnumeratorFromKey(IntPtr factory, [In] void* collectionKey, [In, MarshalAs(UnmanagedType.U4)] uint collectionKeySize, IntPtr* fontFileEnumerator)
45+
{
46+
uint numberOfCharacters = collectionKeySize / sizeof(char);
47+
if ((fontFileEnumerator == null)
48+
|| (collectionKeySize % sizeof(char) != 0) // The collectionKeySize must be divisible by sizeof(WCHAR)
49+
|| (numberOfCharacters <= 1) // The collectionKey cannot be less than or equal 1 character as it has to contain the NULL character.
50+
|| (((char*)collectionKey)[numberOfCharacters - 1] != '\0')) // The collectionKey must end with the NULL character
51+
{
52+
return E_INVALIDARG;
53+
}
54+
55+
*fontFileEnumerator = IntPtr.Zero;
56+
57+
string uriString = new string((char*)collectionKey);
58+
int hr = S_OK;
59+
60+
try
61+
{
62+
IFontSourceCollection fontSourceCollection = _fontSourceCollectionFactory.Create(uriString);
63+
FontFileEnumerator fontFileEnum = new FontFileEnumerator(
64+
fontSourceCollection,
65+
_fontFileLoader,
66+
(Native.IDWriteFactory*)factory.ToPointer()
67+
);
68+
IntPtr pIDWriteFontFileEnumeratorMirror = Marshal.GetComInterfaceForObject(
69+
fontFileEnum,
70+
typeof(IDWriteFontFileEnumeratorMirror));
71+
72+
*fontFileEnumerator = pIDWriteFontFileEnumeratorMirror;
73+
}
74+
catch (Exception exception)
75+
{
76+
hr = Marshal.GetHRForException(exception);
77+
}
78+
79+
return hr;
80+
}
81+
}
82+
}
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
@@ -272,9 +272,12 @@
272272
<Compile Include="MS\Internal\Shaping\Substitution.cs" />
273273
<Compile Include="MS\Internal\Shaping\TypefaceMap.cs" />
274274
<Compile Include="MS\Internal\Shaping\UshortList2.cs" />
275+
<Compile Include="MS\Internal\Text\TextInterface\DWriteInterfaces.cs" />
275276
<Compile Include="MS\Internal\Text\TextInterface\DWriteLoader.cs" />
276277
<Compile Include="MS\Internal\Text\TextInterface\DWriteUtil.cs" />
277278
<Compile Include="MS\Internal\Text\TextInterface\Factory.cs" />
279+
<Compile Include="MS\Internal\Text\TextInterface\FontCollectionLoader.cs" />
280+
<Compile Include="MS\Internal\Text\TextInterface\IFontSourceCollection.cs" />
278281
<Compile Include="MS\Internal\TextFormatting\Bidi.cs" />
279282
<Compile Include="MS\Internal\TextFormatting\CultureMapper.cs" />
280283
<Compile Include="MS\Internal\TextFormatting\DrawingState.cs" />

0 commit comments

Comments
 (0)