Skip to content

[Managed DWrite] Migrate part of DWriteTypeConverter to managed #9902

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@

namespace MS { namespace Internal { namespace Text { namespace TextInterface
{
DWRITE_FACTORY_TYPE DWriteTypeConverter::Convert(FactoryType factoryType)
{
switch(factoryType)
{
case FactoryType::Shared : return DWRITE_FACTORY_TYPE_SHARED;
case FactoryType::Isolated : return DWRITE_FACTORY_TYPE_ISOLATED;
default : throw gcnew System::InvalidOperationException();
}
}

FontWeight DWriteTypeConverter::Convert(DWRITE_FONT_WEIGHT fontWeight)
{
// The commented cases are here only for completeness so that the code captures all the possible enum values.
Expand Down Expand Up @@ -373,29 +363,4 @@ namespace MS { namespace Internal { namespace Text { namespace TextInterface
}
}

DWRITE_MEASURING_MODE DWriteTypeConverter::Convert(TextFormattingMode measuringMode)
{
switch(measuringMode)
{
case TextFormattingMode::Ideal : return DWRITE_MEASURING_MODE_NATURAL;
case TextFormattingMode::Display : return DWRITE_MEASURING_MODE_GDI_CLASSIC;
// We do not support Natural Metrics mode in WPF
default : throw gcnew System::InvalidOperationException();
}
}

TextFormattingMode DWriteTypeConverter::Convert(DWRITE_MEASURING_MODE dwriteMeasuringMode)
{
switch(dwriteMeasuringMode)
{
case DWRITE_MEASURING_MODE_NATURAL : return TextFormattingMode::Ideal;
case DWRITE_MEASURING_MODE_GDI_CLASSIC : return TextFormattingMode::Display;
// We do not support Natural Metrics mode in WPF
// However, the build system complained about not having an explicit case
// for DWRITE_TEXT_MEASURING_METHOD_USE_DISPLAY_NATURAL_METRICS
case DWRITE_MEASURING_MODE_GDI_NATURAL : throw gcnew System::InvalidOperationException();
default : throw gcnew System::InvalidOperationException();
}
}

}}}}//MS::Internal::Text::TextInterface
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#define __DWRITETYPECONVERTER_H

#include "Common.h"
#include "FactoryType.h"
#include "FontWeight.h"
#include "FontFaceType.h"
#include "FontFileType.h"
Expand All @@ -19,7 +18,6 @@
#include "DWriteGlyphOffset.h"
#include "InformationalStringID.h"

using namespace System::Windows::Media;
namespace MS { namespace Internal { namespace Text { namespace TextInterface
{
/// <summary>
Expand All @@ -29,7 +27,6 @@ namespace MS { namespace Internal { namespace Text { namespace TextInterface
{
internal:

static DWRITE_FACTORY_TYPE Convert(FactoryType factoryType);
static FontWeight Convert(DWRITE_FONT_WEIGHT fontWeight);
static DWRITE_FONT_WEIGHT Convert(FontWeight fontWeight);
static FontFileType Convert(DWRITE_FONT_FILE_TYPE dwriteFontFileType);
Expand All @@ -48,8 +45,6 @@ namespace MS { namespace Internal { namespace Text { namespace TextInterface
static System::Windows::Point Convert(DWRITE_GLYPH_OFFSET dwriteGlyphOffset);
static DWRITE_INFORMATIONAL_STRING_ID Convert(InformationalStringID informationStringID);
static InformationalStringID Convert(DWRITE_INFORMATIONAL_STRING_ID dwriteInformationStringID);
static DWRITE_MEASURING_MODE Convert(TextFormattingMode measuringMode);
static TextFormattingMode Convert(DWRITE_MEASURING_MODE dwriteMeasuringMode);
};

}}}}//MS::Internal::Text::TextInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ using namespace MS::Internal::Text::TextInterface::Interfaces;
#endif
using namespace System::Threading;

typedef HRESULT (WINAPI *DWRITECREATEFACTORY)(DWRITE_FACTORY_TYPE factoryType, REFIID iid, IUnknown **factory);

extern void *GetDWriteCreateFactoryFunctionPointer();

namespace MS { namespace Internal { namespace Text { namespace TextInterface
{
HRESULT InternalFactory::CreateFontFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#define __FACTORY_H

#include "Common.h"
#include "FactoryType.h"
#include "FontFile.h"
#include "FontFace.h"
#include "FontCollection.h"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace MS.Internal.Interop.DWrite
{
/// <summary>
/// Specifies the type of DirectWrite factory object.
/// DirectWrite factory contains internal state such as font loader registration and cached font data.
/// In most cases it is recommended to use the shared factory object, because it allows multiple components
/// that use DirectWrite to share internal DirectWrite state and reduce memory usage.
/// However, there are cases when it is desirable to reduce the impact of a component,
/// such as a plug-in from an untrusted source, on the rest of the process by sandboxing and isolating it
/// from the rest of the process components. In such cases, it is recommended to use an isolated factory for the sandboxed
/// component.
/// </summary>
internal enum DWRITE_FACTORY_TYPE
{
/// <summary>
/// Shared factory allow for re-use of cached font data across multiple in process components.
/// Such factories also take advantage of cross process font caching components for better performance.
/// </summary>
DWRITE_FACTORY_TYPE_SHARED,

/// <summary>
/// Objects created from the isolated factory do not interact with internal DirectWrite state from other components.
/// </summary>
DWRITE_FACTORY_TYPE_ISOLATED
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace MS.Internal.Interop.DWrite
{
/// <summary>
/// The measuring method used for text layout.
/// </summary>
internal enum DWRITE_MEASURING_MODE
{
/// <summary>
/// Text is measured using glyph ideal metrics whose values are independent to the current display resolution.
/// </summary>
DWRITE_MEASURING_MODE_NATURAL,

/// <summary>
/// Text is measured using glyph display compatible metrics whose values tuned for the current display resolution.
/// </summary>
DWRITE_MEASURING_MODE_GDI_CLASSIC,

/// <summary>
// Text is measured using the same glyph display metrics as text measured by GDI using a font
// created with CLEARTYPE_NATURAL_QUALITY.
/// </summary>
DWRITE_MEASURING_MODE_GDI_NATURAL
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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.Windows.Media;
using MS.Internal.Interop.DWrite;

namespace MS.Internal.Text.TextInterface
{
/// <summary>
/// This class is used to convert data types back and forth between DWrite and DWriteWrapper.
/// </summary>
internal static class DWriteTypeConverterEx
{
internal static DWRITE_FACTORY_TYPE Convert(FactoryType factoryType)
{
switch (factoryType)
{
case FactoryType.Shared:
return DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_SHARED;
case FactoryType.Isolated:
return DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_ISOLATED;
default:
throw new InvalidOperationException();
}
}

internal static DWRITE_MEASURING_MODE Convert(TextFormattingMode measuringMode)
{
switch (measuringMode)
{
case TextFormattingMode.Ideal:
return DWRITE_MEASURING_MODE.DWRITE_MEASURING_MODE_NATURAL;
case TextFormattingMode.Display:
return DWRITE_MEASURING_MODE.DWRITE_MEASURING_MODE_GDI_CLASSIC;
// We do not support Natural Metrics mode in WPF
default:
throw new InvalidOperationException();
}
}

internal static TextFormattingMode Convert(DWRITE_MEASURING_MODE dwriteMeasuringMode)
{
switch (dwriteMeasuringMode)
{
case DWRITE_MEASURING_MODE.DWRITE_MEASURING_MODE_NATURAL:
return TextFormattingMode.Ideal;
case DWRITE_MEASURING_MODE.DWRITE_MEASURING_MODE_GDI_CLASSIC:
return TextFormattingMode.Display;
// We do not support Natural Metrics mode in WPF
// However, the build system complained about not having an explicit case
// for DWRITE_TEXT_MEASURING_METHOD_USE_DISPLAY_NATURAL_METRICS
case DWRITE_MEASURING_MODE.DWRITE_MEASURING_MODE_GDI_NATURAL:
default:
throw new InvalidOperationException();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void Initialize(FactoryType factoryType)

delegate* unmanaged<int, void*, void*, int> pfnDWriteCreateFactory = DWriteLoader.GetDWriteCreateFactoryFunctionPointer();

int hr = pfnDWriteCreateFactory((int)DWriteTypeConverter.Convert(factoryType), &iid, &factory);
int hr = pfnDWriteCreateFactory((int)DWriteTypeConverterEx.Convert(factoryType), &iid, &factory);

DWriteUtil.ConvertHresultToException(hr);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace MS.Internal.Text.TextInterface
{
/// <summary>
/// The type of the Factory.
/// </summary>
internal enum FactoryType
{
Shared,
Isolated
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,10 @@
<Compile Include="MS\Internal\Ink\InkSerializedFormat\StrokeDescriptor.cs" />
<Compile Include="MS\Internal\Ink\InkSerializedFormat\StrokeSerializer.cs" />
<Compile Include="MS\Internal\Ink\InkSerializedFormat\TransformDescriptor.cs" />
<Compile Include="MS\Internal\Interop\DWrite\DWRITE_FACTORY_TYPE.cs" />
<Compile Include="MS\Internal\Interop\DWrite\DWRITE_FONT_FACE_TYPE.cs" />
<Compile Include="MS\Internal\Interop\DWrite\DWRITE_FONT_SIMULATIONS.cs" />
<Compile Include="MS\Internal\Interop\DWrite\DWRITE_MEASURING_MODE.cs" />
<Compile Include="MS\Internal\Interop\DWrite\IDWriteFactory.cs" />
<Compile Include="MS\Internal\Interop\DWrite\IDWriteFontCollection.cs" />
<Compile Include="MS\Internal\Interop\DWrite\IDWriteFontCollectionLoader.cs" />
Expand Down Expand Up @@ -275,8 +277,10 @@
<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\DWriteTypeConverterEx.cs" />
<Compile Include="MS\Internal\Text\TextInterface\DWriteUtil.cs" />
<Compile Include="MS\Internal\Text\TextInterface\Factory.cs" />
<Compile Include="MS\Internal\Text\TextInterface\FactoryType.cs" />
<Compile Include="MS\Internal\Text\TextInterface\FontCollectionLoader.cs" />
<Compile Include="MS\Internal\Text\TextInterface\IFontSourceCollection.cs" />
<Compile Include="MS\Internal\TextFormatting\Bidi.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1875,7 +1875,7 @@ private void CreateOnChannel(DUCE.Channel channel)
command.GlyphCount = checked((UInt16)glyphCount);
command.BidiLevel = checked((UInt16)_bidiLevel);
command.pIDWriteFont = (UInt64)_glyphTypeface.GetDWriteFontAddRef;
command.DWriteTextMeasuringMethod = (UInt16)DWriteTypeConverter.
command.DWriteTextMeasuringMethod = (UInt16)DWriteTypeConverterEx.
Convert(_textFormattingMode);

// Advances
Expand Down