DirectWrite support #28

Merged
IoI_xD merged 13 commits from directwrite into master 2026-07-18 11:39:53 +09:00
12 changed files with 20963 additions and 15 deletions
Showing only changes of commit f938162899 - Show all commits

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

IoI_xD 2026-07-16 20:14:40 -07:00

View file

@ -9,6 +9,7 @@ include(GNUInstallDirs)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}") set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
option(MW_CLASSIC_THEME "Use classic theme" OFF) option(MW_CLASSIC_THEME "Use classic theme" OFF)
option(MW_BUILD_EXAMPLES "Build examples" OFF) option(MW_BUILD_EXAMPLES "Build examples" OFF)
@ -29,12 +30,18 @@ endif()
option(MW_INSTALL_HEADERS "Install headers" ON) option(MW_INSTALL_HEADERS "Install headers" ON)
if(${CMAKE_SYSTEM_NAME} STREQUAL Darwin OR ${CMAKE_SYSTEM_NAME} MATCHES Retro) if( ${CMAKE_SYSTEM_NAME} STREQUAL Darwin
OR ${CMAKE_SYSTEM_NAME} MATCHES Retro
OR ${CMAKE_SYSTEM_NAME} MATCHES Windows)
option(MW_USE_FREETYPE2 "Use FreeType 2" OFF) option(MW_USE_FREETYPE2 "Use FreeType 2" OFF)
else() else()
option(MW_USE_FREETYPE2 "Use FreeType 2" ON) option(MW_USE_FREETYPE2 "Use FreeType 2" ON)
endif() endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL Windows)
option(MW_USE_DIRECTWRITE "Use DirectWrite" ON)
endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL Linux OR ${CMAKE_SYSTEM_NAME} STREQUAL FreeBSD) if(${CMAKE_SYSTEM_NAME} STREQUAL Linux OR ${CMAKE_SYSTEM_NAME} STREQUAL FreeBSD)
option(MW_USE_WAYLAND "Enable Wayland backend" ON) option(MW_USE_WAYLAND "Enable Wayland backend" ON)
endif() endif()
@ -144,6 +151,14 @@ if(MW_USE_FREETYPE2)
endif() endif()
endif() endif()
if(MW_USE_DIRECTWRITE)
target_compile_definitions(
Mw
PRIVATE
USE_DIRECTWRITE
)
endif()
if(MW_USE_WAYLAND) if(MW_USE_WAYLAND)
function(scan_wayland_protocol_core) function(scan_wayland_protocol_core)
execute_process( execute_process(

2
WatMakefile generated
View file

@ -444,6 +444,8 @@ src/text/font/ttf.obj: src/text/font/ttf.c
$(CC) $(MW_CFLAGS) -fo=$@ src/text/font/ttf.c $(CC) $(MW_CFLAGS) -fo=$@ src/text/font/ttf.c
src/text/ft2.obj: src/text/ft2.c src/text/ft2.obj: src/text/ft2.c
$(CC) $(MW_CFLAGS) -fo=$@ src/text/ft2.c $(CC) $(MW_CFLAGS) -fo=$@ src/text/ft2.c
src/text/directwrite.obj: src/text/directwrite.c
$(CC) $(MW_CFLAGS) -fo=$@ src/text/directwrite.c
src/text/stbtt.obj: src/text/stbtt.c src/text/stbtt.obj: src/text/stbtt.c
$(CC) $(MW_CFLAGS) -fo=$@ src/text/stbtt.c $(CC) $(MW_CFLAGS) -fo=$@ src/text/stbtt.c
src/text/text.obj: src/text/text.c src/text/text.obj: src/text/text.c

View file

@ -344,6 +344,9 @@ MWDECL int MWFL_FT2Setup(void);
#ifdef USE_STB_TRUETYPE #ifdef USE_STB_TRUETYPE
MWDECL int MwFL_STBTTSetup(void); MWDECL int MwFL_STBTTSetup(void);
#endif #endif
#ifdef USE_DIRECTWRITE
MWDECL int MWFL_DWSetup(void);
#endif
MWDECL int (*MwFLDrawText)(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text, MwLLColor color); MWDECL int (*MwFLDrawText)(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text, MwLLColor color);
MWDECL int (*MwFLTextWidth)(MwFLFont ttf, const char* text); MWDECL int (*MwFLTextWidth)(MwFLFont ttf, const char* text);

View file

@ -310,7 +310,7 @@ static MwWidget MwCreateWidget_Internal(MwClass widget_class, const char* name,
MwAddTickList(h); MwAddTickList(h);
} }
#if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) #if defined(USE_STB_TRUETYPE) || defined(USE_FREETYPE2) || defined(USE_DIRECTWRITE)
if(IsFirstVisible(h)) { if(IsFirstVisible(h)) {
h->root_font = MwFontLoad(MwTTFData, MwTTFDataSize); h->root_font = MwFontLoad(MwTTFData, MwTTFDataSize);
h->root_boldfont = MwFontLoad(MwBoldTTFData, MwBoldTTFDataSize); h->root_boldfont = MwFontLoad(MwBoldTTFData, MwBoldTTFDataSize);
@ -732,7 +732,7 @@ int MwGetInteger(MwWidget handle, const char* key) {
return MwDEFAULT; return MwDEFAULT;
} else { } else {
if(shgeti(handle->integer, key) == -1) { 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); if(strcmp(key, MwNbitmapFont) == 0) return inherit_integer(handle, key, 0);
#else #else
if(strcmp(key, MwNbitmapFont) == 0) return inherit_integer(handle, key, 1); 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); 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) { static void* inherit_void(MwWidget handle, const char* key) {
void* v; void* v;
@ -799,7 +799,7 @@ void* MwGetVoid(MwWidget handle, const char* key) {
if(v != NULL) return v; 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) { if(strcmp(key, MwNfont) == 0 || strcmp(key, MwNboldFont) == 0 || strcmp(key, MwNmonospaceFont) == 0 || strcmp(key, MwNboldMonospaceFont) == 0) {
v = inherit_void(handle, key); 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> #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[] = { unsigned char MwBoldMonospaceTTFData[] = {
0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x04, 0x00, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x04, 0x00, 0x10,
0x46, 0x46, 0x54, 0x4d, 0xac, 0x0d, 0xe4, 0x44, 0x00, 0x00, 0x8d, 0x3c, 0x46, 0x46, 0x54, 0x4d, 0xac, 0x0d, 0xe4, 0x44, 0x00, 0x00, 0x8d, 0x3c,

View file

@ -1,6 +1,6 @@
#include <Mw/Milsko.h> #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[] = { unsigned char MwBoldTTFData[] = {
0x00, 0x01, 0x00, 0x00, 0x00, 0x12, 0x01, 0x00, 0x00, 0x04, 0x00, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x12, 0x01, 0x00, 0x00, 0x04, 0x00, 0x20,
0x46, 0x46, 0x54, 0x4d, 0x94, 0x9f, 0x60, 0x18, 0x00, 0x00, 0x8e, 0xa8, 0x46, 0x46, 0x54, 0x4d, 0x94, 0x9f, 0x60, 0x18, 0x00, 0x00, 0x8e, 0xa8,

View file

@ -1,6 +1,6 @@
#include <Mw/Milsko.h> #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[] = { unsigned char MwMonospaceTTFData[] = {
0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x04, 0x00, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x04, 0x00, 0x10,
0x46, 0x46, 0x54, 0x4d, 0xac, 0x0d, 0xe4, 0x23, 0x00, 0x00, 0x8d, 0x3c, 0x46, 0x46, 0x54, 0x4d, 0xac, 0x0d, 0xe4, 0x23, 0x00, 0x00, 0x8d, 0x3c,

View file

@ -1,6 +1,6 @@
#include <Mw/Milsko.h> #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[] = { unsigned char MwTTFData[] = {
0x00, 0x01, 0x00, 0x00, 0x00, 0x12, 0x01, 0x00, 0x00, 0x04, 0x00, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x12, 0x01, 0x00, 0x00, 0x04, 0x00, 0x20,
0x46, 0x46, 0x54, 0x4d, 0x94, 0x92, 0x79, 0xd4, 0x00, 0x00, 0x89, 0xa4, 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* (*MwFLFontLoad)(unsigned char* data, unsigned int size, int px) = NULL;
void (*MwFLFontFree)(void* handle) = 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 #define TTF
#endif #endif
@ -297,6 +297,9 @@ static void bitmap_MwDrawText(MwWidget handle, MwPoint* point, const char* text,
typedef int (*call_t)(void); typedef int (*call_t)(void);
void MwFLSetup(void) { void MwFLSetup(void) {
call_t calls[] = { call_t calls[] = {
#ifdef USE_DIRECTWRITE
MWFL_DWSetup,
#endif
#ifdef USE_FREETYPE2 #ifdef USE_FREETYPE2
MWFL_FT2Setup, MWFL_FT2Setup,
#endif #endif