Start on a new (optional) DirectWrite backend
All checks were successful
pyrite-dev/milsko/pipeline/head This commit looks good

This commit is contained in:
IoI_xD 2026-07-16 20:14:40 -07:00
commit f938162899
12 changed files with 20963 additions and 15 deletions

217
src/text/directwrite.c Normal file
View file

@ -0,0 +1,217 @@
#ifdef USE_DIRECTWRITE
#define INITGUID
#include <Mw/Milsko.h>
#include <d2d1.h>
#ifndef WINBOOL
#define WINBOOL int
#endif
#include "dwrite_c.h"
#include "dwrite_3_c.h"
static struct dw {
HMODULE d2d1_lib;
HMODULE dwrite_lib;
HRESULT (*D2D1CreateFactory)(D2D1_FACTORY_TYPE factoryType, const IID* const riid, const D2D1_FACTORY_OPTIONS* pFactoryOptions, void** ppIFactory);
HRESULT (*DWriteCreateFactory)(int factoryType, const IID* const iid, IUnknown** factory);
} dw_table;
struct _MwFLFont {
int px;
void * data;
MwBool valid;
IDWriteFactory* dWriteFactory;
IDWriteTextFormat* dTextFormat;
ID2D1Factory* d2dFactory;
ID2D1HwndRenderTarget* rt;
ID2D1SolidColorBrush* blackBrush;
IDWriteGdiInterop *gdiInterop;
IDWriteFontFile * file;
IDWriteFontFace * font_face;
LOGFONT logfont;
HFONT font;
};
static int unpack_ttf_data(WCHAR * temp_path_name, unsigned char* data, unsigned int size) {
WCHAR temp_path_dir[MAX_PATH];
HANDLE tempFile;
int hr;
hr = GetTempPath2W(MAX_PATH, temp_path_dir);
if (hr == 0)
{
printf("GetTempPath2 failed for TTF, cannot use DirectWrite\n");
return 1;
}
hr = GetTempFileNameW(temp_path_dir, TEXT(L"MILSKO"), 0, temp_path_name);
if (hr == 0)
{
printf("GetTempFileName failed for TTF, cannot use DirectWrite\n");
return 1;
}
tempFile = CreateFileW(temp_path_name,
GENERIC_WRITE,
0,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (tempFile == INVALID_HANDLE_VALUE)
{
DWORD err = GetLastError();
if(err == ERROR_FILE_EXISTS) {
tempFile = CreateFileW(temp_path_name,
GENERIC_WRITE,
0,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (tempFile != INVALID_HANDLE_VALUE)
{
goto success;
} else {
err = GetLastError();
}
}
printf("CreateFile failed for TTF (%ld), cannot use DirectWrite\n", err);
return 1;
}
success:
hr = WriteFile(tempFile, data, size, NULL, NULL);
if(!hr) {
printf("WriteFile failed for TTF (%ld), cannot use DirectWrite\n", GetLastError());
return 1;
}
CloseHandle(tempFile);
printf("unpacked ttf data %p into %ws\n",data, temp_path_name);
return 0;
}
static void* dw_MwFontLoad(unsigned char* data, unsigned int size, int px) {
MwFLFont ttf = malloc(sizeof(*ttf));
HRESULT hr;
WCHAR font_name[MAX_PATH];
D2D1_FACTORY_OPTIONS d2d1_options;
d2d1_options.debugLevel = D2D1_DEBUG_LEVEL_ERROR;
memset(ttf, 0, sizeof(*ttf));
ttf->data = malloc(size);
memcpy(ttf->data, data, size);
ttf->px = px;
hr = dw_table.D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &IID_ID2D1Factory, &d2d1_options, (void**)&ttf->d2dFactory);
if(!SUCCEEDED(hr)) {
printf("D2D1CreateFactory failed for TTF (%08lX), cannot use DirectWrite\n", hr);
goto fail;
}
hr = dw_table.DWriteCreateFactory(0 /*DWRITE_FACTORY_TYPE_SHARED*/,&IID_IDWriteFactory, (struct IUnknown**)&ttf->dWriteFactory);
if(!SUCCEEDED(hr)) {
printf("DWriteCreateFactory failed for TTF (%08lx), cannot use DirectWrite\n", hr);
goto fail;
}
if(unpack_ttf_data(font_name, data, size) != 0) {
goto fail;
};
hr = ttf->dWriteFactory->lpVtbl->CreateFontFileReference(ttf->dWriteFactory, font_name, NULL, &ttf->file);
if(!SUCCEEDED(hr)) {
printf("CreateFontFileReference failed for TTF (%08lx), cannot use DirectWrite\n",hr);
goto fail;
}
hr = ttf->dWriteFactory->lpVtbl->CreateFontFace(ttf->dWriteFactory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &ttf->file, 0, DWRITE_FONT_SIMULATIONS_NONE, &ttf->font_face);
if(!SUCCEEDED(hr)) {
printf("CreateFontFace failed for TTF (%08lx), cannot use DirectWrite\n",hr);
goto fail;
}
hr = ttf->dWriteFactory->lpVtbl->GetGdiInterop(ttf->dWriteFactory, &ttf->gdiInterop);
if(!SUCCEEDED(hr)) {
printf("GetGdiInterop failed for TTF (%08lx), cannot use DirectWrite\n",hr);
goto fail;
}
ttf->valid = MwTRUE;
return ttf;
fail:
ttf->valid = MwFALSE;
return ttf;
}
static int dw_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text, MwLLColor color) {
return 0;
}
static int dw_MwTextWidth(MwFLFont ttf, const char* text) {
int tw = 15;
return tw + 1;
}
static int dw_MwTextHeight(MwFLFont ttf, int count) {
DWRITE_FONT_METRICS metrics;
int height;
IDWriteFontFace_GetMetrics(ttf->font_face, &metrics);
height = ceil(((float)metrics.xHeight * ttf->px / metrics.designUnitsPerEm) * count);
return height;
}
static void dw_MwFontFree(void* handle) {
MwFLFont ttf = handle;
free(ttf->data);
free(ttf);
}
int MWFL_DWSetup(void) {
/* Try and load DirectWrite. If it fails, return 1, signifying we default to bitmap drawing. */
dw_table.d2d1_lib = LoadLibrary("D2d1.dll");
if(!dw_table.d2d1_lib) {
printf("D2d1.dll not found, cannot use DirectWrite\n");
return 1;
}
dw_table.dwrite_lib = LoadLibrary("dwrite.dll");
if(!dw_table.dwrite_lib) {
printf("dwrite.dll not found, cannot use DirectWrite\n");
return 1;
}
#define D2D1_LOAD(x) dw_table.x = (typeof(dw_table.x))GetProcAddress(dw_table.d2d1_lib, #x); if(!dw_table.x) {printf("Can't use DirectWrite, "#x " not found."); return 1;}
#define DWRITE_LOAD(x) dw_table.x = (typeof(dw_table.x))GetProcAddress(dw_table.dwrite_lib, #x); if(!dw_table.x) {printf("Can't use DirectWrite, "#x " not found."); return 1;}
D2D1_LOAD(D2D1CreateFactory);
DWRITE_LOAD(DWriteCreateFactory);
MwFLDrawText = dw_MwDrawText;
MwFLTextWidth = dw_MwTextWidth;
MwFLTextHeight = dw_MwTextHeight;
MwFLFontLoad = dw_MwFontLoad;
MwFLFontFree = dw_MwFontFree;
return 0;
}
#endif