-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds basic DirectWrite sample. Add OnPaint and OnSize to Window. I don't want to have a zillion `On` virtuals and there is a mechanism to attatch a set of handlers (say for mouse or touch input). This is partially for performance reasons, but also code bloat.
- Loading branch information
1 parent
9c96ca0
commit 4f83294
Showing
24 changed files
with
1,111 additions
and
80 deletions.
There are no files selected for viewing
This file contains 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
11 changes: 11 additions & 0 deletions
11
src/samples/DirectDraw/DirectWriteDemo/DirectWriteDemo.csproj
This file contains 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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\thirtytwo\thirtytwo.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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 @@ | ||
// Copyright (c) Jeremy W. Kuhne. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Drawing; | ||
using Windows; | ||
using Windows.Win32.Graphics.Direct2D; | ||
using Windows.Win32.Graphics.DirectWrite; | ||
using FontWeight = Windows.Win32.Graphics.DirectWrite.FontWeight; | ||
|
||
namespace DirectWriteDemo; | ||
|
||
internal class Program | ||
{ | ||
[STAThread] | ||
private static void Main() => Application.Run(new DirectWriteDemo()); | ||
|
||
private class DirectWriteDemo : MainWindow | ||
{ | ||
private const string Message = "Hello World From ... DirectWrite!"; | ||
|
||
protected TextFormat _textFormat; | ||
protected TextLayout? _textLayout; | ||
protected Typography _typography; | ||
|
||
protected SolidColorBrush? _blackBrush; | ||
|
||
public DirectWriteDemo() : base(title: "Simple DirectWrite Application", features: Features.EnableDirect2d) | ||
{ | ||
_textFormat = new("Gabriola", fontSize: 64) | ||
{ | ||
TextAlignment = TextAlignment.Center, | ||
ParagraphAlignment = ParagraphAlignment.Center | ||
}; | ||
|
||
_typography = new(); | ||
_typography.AddFontFeature(FontFeatureTag.StylisticSet7); | ||
} | ||
|
||
protected override void RenderTargetCreated() | ||
{ | ||
_blackBrush?.Dispose(); | ||
_blackBrush = RenderTarget.CreateSolidColorBrush(Color.Black); | ||
base.RenderTargetCreated(); | ||
} | ||
|
||
protected override void OnPaint() | ||
{ | ||
RenderTarget.Clear(Color.CornflowerBlue); | ||
RenderTarget.DrawTextLayout(default, _textLayout!, _blackBrush!); | ||
} | ||
|
||
protected override void OnSize(Size size) | ||
{ | ||
if (_textLayout is not null) | ||
{ | ||
_textLayout.MaxHeight = size.Height; | ||
_textLayout.MaxWidth = size.Width; | ||
return; | ||
} | ||
|
||
_textLayout = new(Message, _textFormat, RenderTarget.Size()); | ||
|
||
// (21, 12) is the range around "DirectWrite!" | ||
_textLayout.SetFontSize(100, (21, 12)); | ||
_textLayout.SetTypography(_typography, (0, Message.Length)); | ||
_textLayout.SetUnderline(true, (21, 12)); | ||
_textLayout.SetFontWeight(FontWeight.Bold, (21, 12)); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
_textFormat.Dispose(); | ||
_textLayout?.Dispose(); | ||
_blackBrush?.Dispose(); | ||
} | ||
|
||
base.Dispose(disposing); | ||
} | ||
} | ||
} |
This file contains 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 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
18 changes: 18 additions & 0 deletions
18
src/thirtytwo/Win32/Graphics/Direct2D/Common/D2D_POINT_F.cs
This file contains 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,18 @@ | ||
// Copyright (c) Jeremy W. Kuhne. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Drawing; | ||
|
||
namespace Windows.Win32.Graphics.Direct2D.Common; | ||
|
||
public partial struct D2D_POINT_2F | ||
{ | ||
public D2D_POINT_2F(float x, float y) | ||
{ | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
public static implicit operator D2D_POINT_2F(PointF value) => | ||
new(value.X, value.Y); | ||
} |
This file contains 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 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,27 @@ | ||
// Copyright (c) Jeremy W. Kuhne. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Windows.Win32.Graphics.Direct2D; | ||
|
||
/// <summary> | ||
/// Modifications made to the draw text call that influence how the text is rendered. | ||
/// [<see cref="D2D1_DRAW_TEXT_OPTIONS"/>] | ||
/// </summary> | ||
[Flags] | ||
public enum DrawTextOptions | ||
{ | ||
/// <inheritdoc cref="D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NO_SNAP"/> | ||
NoSnap = D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NO_SNAP, | ||
|
||
/// <inheritdoc cref="D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_CLIP"/> | ||
Clip = D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_CLIP, | ||
|
||
/// <inheritdoc cref="D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_ENABLE_COLOR_FONT"/> | ||
EnableColorFont = D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_ENABLE_COLOR_FONT, | ||
|
||
/// <inheritdoc cref="D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_DISABLE_COLOR_BITMAP_SNAPPING"/> | ||
DisableColorBitmapSnapping = D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_DISABLE_COLOR_BITMAP_SNAPPING, | ||
|
||
/// <inheritdoc cref="D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NONE"/> | ||
None = D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NONE | ||
} |
This file contains 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
39 changes: 39 additions & 0 deletions
39
src/thirtytwo/Win32/Graphics/DirectWrite/DirectWriteFactory.cs
This file contains 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,39 @@ | ||
// Copyright (c) Jeremy W. Kuhne. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Windows.Support; | ||
using Windows.Win32.System.Com; | ||
|
||
namespace Windows.Win32.Graphics.DirectWrite; | ||
|
||
public unsafe class DirectWriteFactory : DisposableBase.Finalizable, IPointer<IDWriteFactory> | ||
{ | ||
private readonly AgileComPointer<IDWriteFactory> _factory; | ||
|
||
public unsafe IDWriteFactory* Pointer { get; private set; } | ||
|
||
public DirectWriteFactory(DWRITE_FACTORY_TYPE factoryType = DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_SHARED) | ||
{ | ||
IDWriteFactory* factory; | ||
Interop.DWriteCreateFactory( | ||
factoryType, | ||
IID.Get<IDWriteFactory>(), | ||
(void**)&factory).ThrowOnFailure(); | ||
|
||
Pointer = factory; | ||
|
||
// Ensure that this can be disposed on the finalizer thread by giving the "last" ref count | ||
// to an agile pointer. | ||
_factory = new AgileComPointer<IDWriteFactory>(factory, takeOwnership: true); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
Pointer = null; | ||
|
||
if (disposing) | ||
{ | ||
_factory.Dispose(); | ||
} | ||
} | ||
} |
Oops, something went wrong.