smourier/WicNet

`IDWriteTextFormat` argument in `ID2D1DeviceContext.DrawText(string, IDWriteTextFormat, ...)`

Closed this issue ยท 2 comments

Hi, I managed to draw almost everything using this library, except text. There are functions for drawing text in ID2D1RenderTargetExtensions but it requires IDWriteTextFormat which is not found in WicNet.

ID2D1HwndRenderTarget _renderTarget;
ID2D1DeviceContext _context;
IComObject<ID2D1Factory> _factory = D2D1Functions.D2D1CreateFactory(D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED);


var hwndRenderTargetProps = new D2D1_HWND_RENDER_TARGET_PROPERTIES()
{
    hwnd = this.Handle,
    pixelSize = new D2D_SIZE_U((uint)Width, (uint)Height),
};

_factory.Object.CreateHwndRenderTarget(new D2D1_RENDER_TARGET_PROPERTIES(), hwndRenderTargetProps, out _renderTarget)
    .ThrowOnError();
_renderTarget.Resize(new(ClientSize.Width, ClientSize.Height));
_renderTarget.SetDpi(96.0f, 96.0f);
_context = (ID2D1DeviceContext)_renderTarget;


// start drawing
_context.BeginDraw();

D2D_RECT_F rect = new(0, 0, 200, 100);
var brush = ...

_context.DrawText("hello world", <IDWriteTextFormat??>, rect, brush);


_context.EndDraw();

This is the extension function in ID2D1RenderTargetExtensions:

// in ID2D1RenderTargetExtensions
public static void DrawText(this ID2D1RenderTarget renderTarget,
  string text,
  IDWriteTextFormat format,
  D2D_RECT_F rect,
  ID2D1Brush brush,
  D2D1_DRAW_TEXT_OPTIONS options = D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NONE,
  DWRITE_MEASURING_MODE measuringMode = DWRITE_MEASURING_MODE.DWRITE_MEASURING_MODE_NATURAL)
{
    // ...
}

Thank you!

Hi,

Yes, there was no DirectWrite helpers included with WicNET (big piece), so I have imported quite a number from the Wice project here: https://github.com/aelyo-softworks/Wice which also uses DirectN here: https://github.com/smourier/DirectN

Now you can write a code like this:

using (var fac = DWriteFunctions.DWriteCreateFactory(DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_SHARED))
using (var bmp = WicBitmapSource.Load("SamsungSGH-P270.jpg"))
{
    bmp.ConvertTo(WicPixelFormat.GUID_WICPixelFormat32bppBGR);
    using (var memBmp = new WicBitmapSource(bmp.Width, bmp.Height, WicPixelFormat.GUID_WICPixelFormat32bppPRGBA))
    {
        using (var rt = memBmp.CreateDeviceContext())
        using (var dbmp = rt.CreateBitmapFromWicBitmap(bmp.ComObject))
        using (var brush = rt.CreateSolidColorBrush(_D3DCOLORVALUE.Green))
        using (var format = fac.CreateTextFormat("Segoe UI", 20))
        {
            rt.BeginDraw();
            rt.DrawBitmap(dbmp);
            rt.DrawText("Hello World!" + Environment.NewLine + "๐Ÿคฉ๐Ÿ˜›๐Ÿ˜‚", format, new D2D_RECT_F(10, 10, 200, 30), brush, D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_ENABLE_COLOR_FONT);
            rt.EndDraw();
        }
        memBmp.Save("helloworld.jpg");
    }
}

Which will create this (note the color font support):

helloworld

Thanks!
I followed this commit to copy all files: aelyo-softworks/Wice@e56abd0.
It's working perfectly