Austin Wise
Go to home page

Loading a WOFF font in SkiaSharp on Windows

Recently I found myself trying to use a font in WOFF format with SkiaSharp. SkiaSharp does not natively support WOFF fonts. Fortunatly the DirectWrite component in Windows can turn these fonts into the SFNT (aka TrueType) format that SkiaSharp supports.

There should be a way to do this that does not invlove using Windows-specific components. This JavaScript converter tool does not have a lot of code in it. So it should be possible to create a pure-C# implementation of the font conversion. But I could not find one and this code only has to run on Windows.


I'm using CsWin32 to generate the native interop bindings. I put this code in a separate library, as it seems like the source generator slows down Visual Studio quite a bit. Hopefully the CsWin32 source generator converts to an incremental generator and this becomes unnecessary.

First we define the dependency in our csproj file:

<Project Sdk="Microsoft.NET.Sdk">


        <AdditionalFiles Include="NativeMethods.txt" />

          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        <PackageReference Include="SkiaSharp" Version="2.80.3" />


Next we define which native APIs we want to call in NativeMethods.txt:


And now we can extract the font from the WOFF file:

using SkiaSharp;
using System;
using Windows.Win32;
using Windows.Win32.Graphics.DirectWrite;

namespace FontInterop;

public static class WoffFontLoader
    static readonly Guid IID_IDWriteFactory5 = Guid.Parse("958DB99A-BE2A-4F09-AF7D-65189803D1D3");

    public unsafe static SKTypeface LoadWoffFont(ReadOnlySpan<byte> woffFont)
        object factory;
        var hr = PInvoke.DWriteCreateFactory(
            out factory);
        var writerFactory = (IDWriteFactory5)factory;
        fixed (byte* pBytes = woffFont)
            IDWriteFontFileStream stream;
                out stream);
            ulong fileSize = 0;
            void* fragStart = (void*)IntPtr.Zero;
            void* cookie = (void*)IntPtr.Zero;
                stream.ReadFileFragment(&fragStart, 0, fileSize, &cookie);
                var data = SKData.CreateCopy((IntPtr)fragStart, (int)fileSize);
                return SKTypeface.FromData(data);
                if (new IntPtr(cookie) != IntPtr.Zero)

This COM interop could probably be done better. Perhaps the IDWriteFontFileStream should be cleaned eagerly up with Marshal.ReleaseComObject Perhaps the IDWriteFactory5 should be created once and cached. But for my short-lived console program, this runs fast enough as is.