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

View file

@ -310,7 +310,7 @@ static MwWidget MwCreateWidget_Internal(MwClass widget_class, const char* name,
MwAddTickList(h);
}
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2)
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) || defined(USE_DIRECTWRITE)
if(IsFirstVisible(h)) {
h->root_font = MwFontLoad(MwTTFData, MwTTFDataSize);
h->root_boldfont = MwFontLoad(MwBoldTTFData, MwBoldTTFDataSize);
@ -732,7 +732,7 @@ int MwGetInteger(MwWidget handle, const char* key) {
return MwDEFAULT;
} else {
if(shgeti(handle->integer, key) == -1) {
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2)
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) || defined(USE_DIRECTWRITE)
if(strcmp(key, MwNbitmapFont) == 0) return inherit_integer(handle, key, 0);
#else
if(strcmp(key, MwNbitmapFont) == 0) return inherit_integer(handle, key, 1);
@ -783,7 +783,7 @@ const char* MwGetText(MwWidget handle, const char* key) {
return shget(handle->text, key);
}
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2)
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) || defined(USE_DIRECTWRITE)
static void* inherit_void(MwWidget handle, const char* key) {
void* v;
@ -799,7 +799,7 @@ void* MwGetVoid(MwWidget handle, const char* key) {
if(v != NULL) return v;
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2)
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) || defined(USE_DIRECTWRITE)
if(strcmp(key, MwNfont) == 0 || strcmp(key, MwNboldFont) == 0 || strcmp(key, MwNmonospaceFont) == 0 || strcmp(key, MwNboldMonospaceFont) == 0) {
v = inherit_void(handle, key);

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

15191
src/text/dwrite_3_c.h Normal file

File diff suppressed because it is too large Load diff

5517
src/text/dwrite_c.h Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
#include <Mw/Milsko.h>
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2)
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) || defined(USE_DIRECTWRITE)
unsigned char MwBoldMonospaceTTFData[] = {
0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x04, 0x00, 0x10,
0x46, 0x46, 0x54, 0x4d, 0xac, 0x0d, 0xe4, 0x44, 0x00, 0x00, 0x8d, 0x3c,

View file

@ -1,6 +1,6 @@
#include <Mw/Milsko.h>
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2)
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) || defined(USE_DIRECTWRITE)
unsigned char MwBoldTTFData[] = {
0x00, 0x01, 0x00, 0x00, 0x00, 0x12, 0x01, 0x00, 0x00, 0x04, 0x00, 0x20,
0x46, 0x46, 0x54, 0x4d, 0x94, 0x9f, 0x60, 0x18, 0x00, 0x00, 0x8e, 0xa8,

View file

@ -1,6 +1,6 @@
#include <Mw/Milsko.h>
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2)
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) || defined(USE_DIRECTWRITE)
unsigned char MwMonospaceTTFData[] = {
0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x04, 0x00, 0x10,
0x46, 0x46, 0x54, 0x4d, 0xac, 0x0d, 0xe4, 0x23, 0x00, 0x00, 0x8d, 0x3c,

View file

@ -1,6 +1,6 @@
#include <Mw/Milsko.h>
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2)
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) || defined(USE_DIRECTWRITE)
unsigned char MwTTFData[] = {
0x00, 0x01, 0x00, 0x00, 0x00, 0x12, 0x01, 0x00, 0x00, 0x04, 0x00, 0x20,
0x46, 0x46, 0x54, 0x4d, 0x94, 0x92, 0x79, 0xd4, 0x00, 0x00, 0x89, 0xa4,

View file

@ -6,7 +6,7 @@ int (*MwFLTextHeight)(MwFLFont font, int count) = NULL;
void* (*MwFLFontLoad)(unsigned char* data, unsigned int size, int px) = NULL;
void (*MwFLFontFree)(void* handle) = NULL;
#if defined(USE_FREETYPE2) || defined(USE_STB_TRUETYPE)
#if defined(USE_FREETYPE2) || defined(USE_STB_TRUETYPE) || defined(USE_DIRECTWRITE)
#define TTF
#endif
@ -297,6 +297,9 @@ static void bitmap_MwDrawText(MwWidget handle, MwPoint* point, const char* text,
typedef int (*call_t)(void);
void MwFLSetup(void) {
call_t calls[] = {
#ifdef USE_DIRECTWRITE
MWFL_DWSetup,
#endif
#ifdef USE_FREETYPE2
MWFL_FT2Setup,
#endif