From 0b7e5ab90124eef2bef2e5972586d5ecc9f2b86e Mon Sep 17 00:00:00 2001 From: IoI_xD Date: Fri, 17 Jul 2026 17:36:16 -0700 Subject: [PATCH] ffinish --- CMakeLists.txt | 9 ++- configure | 26 +++++++-- include/Mw/LowLevel.h | 1 + pl/rules.pl | 4 ++ src/text/directwrite.c | 130 +++++++++++++++++++++++++++++++++-------- src/text/text.c | 3 + 6 files changed, 144 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 11bfb77..1e2ad47 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,14 @@ else() endif() if(${CMAKE_SYSTEM_NAME} STREQUAL Windows) - option(MW_USE_DIRECTWRITE "Use DirectWrite" ON) + # DirectWrite stuff can only be used on Win10+ + if(${CMAKE_SYSTEM_VERSION} GREATER_EQUAL 10) + message(STATUS "DirectWrite support has been enabled") + option(MW_USE_DIRECTWRITE "Use DirectWrite" ON) + else() + message(WARNING "DirectWrite support has been disabled (must be compiling on Win10+)") + option(MW_USE_DIRECTWRITE "Use DirectWrite" OFF) + endif() endif() if(${CMAKE_SYSTEM_NAME} STREQUAL Linux OR ${CMAKE_SYSTEM_NAME} STREQUAL FreeBSD) diff --git a/configure b/configure index 0192a22..9ce2cfa 100755 --- a/configure +++ b/configure @@ -2,6 +2,11 @@ our $target = `uname -s`; $target =~ s/\r?\n$//; +# MSYS with MinGW gives us some other shit idk +if (index($target, "MINGW") != -1) { + $target = "Windows"; +} + foreach my $l (@ARGV) { if ($l =~ /^--.+$/) { } @@ -39,14 +44,12 @@ require("./pl/utils.pl"); param_set("classic-theme", 0); param_set("stb-image", 1); -param_set("xrender", 1); param_set("opengl", 0); param_set("vulkan", 0); param_set("vulkan-string-helper", 1); param_set("shared", 1); param_set("static", 1); param_set("examples", 1); -param_set("dbus", 1); # (option that only makes sense on x11) param_set("allow-sloppy-focus", 0); @@ -56,7 +59,8 @@ my %features = ( "stb-image" => "use stb_image, instead use libjpeg/libpng", "stb-truetype" => "use stb_truetype", "freetype2" => "use FreeType2", - "xrender" => "use XRender", + "directwrite" => "(Windows only) use DirectWrite", + "xrender" => "(X11 only) use XRender", "opengl" => "build OpenGL widget", "vulkan" => "build Vulkan widget", "vulkan-string-helper" => "use Vulkan string helper", @@ -69,7 +73,7 @@ my %features = ( ); my @features_keys = ( "1classic-theme", "1stb-image", - "1stb-truetype", "1freetype2", + "1stb-truetype", "1freetype2", "1directwrite", "1opengl", "2xrender", "1vulkan", "2vulkan-string-helper", "1shared", "1static", @@ -87,13 +91,27 @@ sub def_platform { } if($target eq "Linux" || $target eq "FreeBSD") { param_set("x11", 1); + param_set("xrender", 1); if(!param_get("tiny")){ param_set("wayland", 1); } + param_set("dbus", 1); } if($target eq "NetBSD" || $target eq "OpenBSD") { param_set("x11", 1); } + if($target eq "Windows") { + use Win32; + my ( $osvername, $major, $minor, $id ) = Win32::GetOSVersion(); + + if($major >= 10) { + param_set("directwrite", 1); + } else { + param_set("directwrite", 0); + } + } else { + param_set("directwrite", 0); + } } foreach my $l (@ARGV) { diff --git a/include/Mw/LowLevel.h b/include/Mw/LowLevel.h index 2651c2a..3ecd065 100644 --- a/include/Mw/LowLevel.h +++ b/include/Mw/LowLevel.h @@ -351,6 +351,7 @@ MWDECL int MWFL_DWSetup(void); MWDECL int (*MwFLDrawText)(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text, MwLLColor color); MWDECL int (*MwFLTextWidth)(MwFLFont ttf, const char* text); MWDECL int (*MwFLTextHeight)(MwFLFont ttf, int count); +MWDECL int (*MwFLTextHeightWithText)(MwFLFont ttf, const char * text); MWDECL void* (*MwFLFontLoad)(unsigned char* data, unsigned int size, int px); MWDECL void (*MwFLFontFree)(void* handle); diff --git a/pl/rules.pl b/pl/rules.pl index bb6fa25..6f37482 100644 --- a/pl/rules.pl +++ b/pl/rules.pl @@ -134,6 +134,10 @@ if (param_get("freetype2")) { } } +if (param_get("directwrite")) { + add_cflags("-DUSE_DIRECTWRITE"); +} + if (param_get("vulkan") && param_get("vulkan-string-helper")) { add_cflags("-DHAS_VK_ENUM_STRING_HELPER"); } diff --git a/src/text/directwrite.c b/src/text/directwrite.c index 9bafe5d..42bd7c0 100644 --- a/src/text/directwrite.c +++ b/src/text/directwrite.c @@ -22,9 +22,10 @@ typedef struct CustomTextRenderer { LONG refCount; IDWriteFactory6 * write_factory; IDWriteBitmapRenderTarget * target; + int red, blue, green; } CustomTextRenderer; -static CustomTextRenderer *CustomTextRenderer_Create(IDWriteFactory6 * write_factory, IDWriteBitmapRenderTarget * target); +static CustomTextRenderer *CustomTextRenderer_Create(IDWriteFactory6 * write_factory, IDWriteBitmapRenderTarget * target, MwLLColor color); static struct dw { HMODULE d2d1_lib; @@ -111,8 +112,6 @@ success: CloseHandle(tempFile); - printf("unpacked ttf data %p into %ws\n",data, temp_path_name); - return 0; } @@ -197,9 +196,6 @@ static void* dw_MwFontLoad(unsigned char* data, unsigned int size, int px) { goto fail; } - - - ttf->valid = MwTRUE; return ttf; @@ -208,6 +204,8 @@ fail: return ttf; } +static int dw_MwTextOffset(MwFLFont ttf, const char* text) ; + static int dw_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text, MwLLColor color) { IDWriteTextLayout * text_layout; @@ -216,11 +214,13 @@ static int dw_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const ch RECT rc = {0}; HDC memoryHDC = NULL; DWRITE_FONT_METRICS metrics = {0}; - LONG width = 0, height = 0; - size_t wtext_len = strlen(text) * sizeof(UINT32); - WCHAR* wtext = malloc(wtext_len); + LONG width = 0, height = 0, offset = 0; + WCHAR* wtext = NULL; CustomTextRenderer * texRenderer; + size_t wtext_len = MultiByteToWideChar(CP_UTF8, 0, text, -1, NULL, 0); + wtext = malloc(wtext_len * 2); /* we get a weird buffer overflow when we try to free this later? allocate well above wtext_len then to prevent that. */ + memset(wtext, 0, wtext_len * 2); MultiByteToWideChar(CP_UTF8, 0, text, -1, wtext, wtext_len); IDWriteFontFace_GetMetrics(ttf->font_face, &metrics); @@ -228,37 +228,111 @@ static int dw_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const ch GetClientRect(handle->lowlevel->gdi.hWnd, &rc); MapWindowPoints(handle->lowlevel->gdi.hWnd, GetParent(handle->lowlevel->gdi.hWnd), (LPPOINT)&rc, 2); - width = rc.right - rc.left; - height = rc.bottom - rc.top; + width = MwTextWidth(handle,ttf, text); + height = MwTextHeight(handle, ttf, text); + offset = dw_MwTextOffset(ttf, text); hr = IDWriteFactory_CreateTextLayout(ttf->write_factory, wtext, wtext_len, ttf->text_format, width, height, &text_layout); IDWriteGdiInterop_CreateBitmapRenderTarget(ttf->gdiInterop, handle->lowlevel->gdi.hDC, width, height, &target); - texRenderer = CustomTextRenderer_Create(ttf->write_factory, target); + texRenderer = CustomTextRenderer_Create(ttf->write_factory, target, color); - IDWriteTextLayout_Draw(text_layout, target, (IDWriteTextRenderer*)texRenderer, point->x, point->y); + memoryHDC = IDWriteBitmapRenderTarget_GetMemoryDC(target); + + SetBkMode(handle->lowlevel->gdi.hDC, TRANSPARENT); + BitBlt(memoryHDC, 0, 0, width, height, handle->lowlevel->gdi.hDC, point->x, point->y - (height / 2), SRCCOPY); + + if (SUCCEEDED(hr)) { + IDWriteTextLayout_Draw(text_layout, target, (IDWriteTextRenderer*)texRenderer, offset, 0); + } + + BitBlt(handle->lowlevel->gdi.hDC, point->x, point->y - (height / 2), width, height, memoryHDC, 0, 0, SRCCOPY | NOMIRRORBITMAP); IDWriteBitmapRenderTarget_Release(target); + free(wtext); + return 0; } -static int dw_MwTextWidth(MwFLFont ttf, const char* text) { - int tw = (ttf->px * strlen(text)) / 2; +static int dw_MwTextOffset(MwFLFont ttf, const char* text) { + IDWriteTextLayout * text_layout; + DWRITE_FONT_METRICS metrics = {0}; + WCHAR* wtext = NULL; + size_t wtext_len = MultiByteToWideChar(CP_UTF8, 0, text, -1, NULL, 0); + UINT32 * codepoints = malloc(wtext_len * sizeof(UINT32)); + UINT16 * glyphIndices = malloc(wtext_len * sizeof(UINT16)); + int i; + DWRITE_GLYPH_METRICS * gmetrics; + int tw = 0; - return tw + 1; -} + wtext = malloc(wtext_len * 2); /* we get a weird buffer overflow when we try to free this later? allocate well above wtext_len then to prevent that. */ + memset(wtext, 0, wtext_len * 2); + MultiByteToWideChar(CP_UTF8, 0, text, -1, wtext, wtext_len); -static int dw_MwTextHeight(MwFLFont ttf, int count) { - DWRITE_FONT_METRICS metrics; - int height; + for(i = 0; i < wtext_len; i++) codepoints[i] = wtext[i]; IDWriteFontFace_GetMetrics(ttf->font_face, &metrics); - height = ceil(((float)metrics.capHeight * ttf->px / metrics.designUnitsPerEm) * count) * 2; + IDWriteFactory_CreateTextLayout(ttf->write_factory, wtext, wtext_len, ttf->text_format, 0, 0, &text_layout); - return height; + IDWriteFontFace_GetGlyphIndices(ttf->font_face, codepoints, wtext_len, glyphIndices); + + gmetrics = malloc(wtext_len * sizeof(DWRITE_GLYPH_METRICS)); + + IDWriteFontFace_GetDesignGlyphMetrics(ttf->font_face, glyphIndices, wtext_len, gmetrics, FALSE); + + for(i = 0; i < strlen(text); i++) { + int add = (gmetrics[i].leftSideBearing) * ((float)ttf->px / (float)metrics.designUnitsPerEm); + tw += add; + } + + free(wtext); + + return tw; +} + +static int dw_MwTextWidth(MwFLFont ttf, const char* text) { + IDWriteTextLayout * text_layout; + DWRITE_FONT_METRICS metrics = {0}; + WCHAR* wtext = NULL; + size_t wtext_len = MultiByteToWideChar(CP_UTF8, 0, text, -1, NULL, 0); + UINT32 * codepoints = malloc(wtext_len * sizeof(UINT32)); + UINT16 * glyphIndices = malloc(wtext_len * sizeof(UINT16)); + int i; + DWRITE_GLYPH_METRICS * gmetrics; + int tw = 0; + + wtext = malloc(wtext_len * 2); /* we get a weird buffer overflow when we try to free this later? allocate well above wtext_len then to prevent that. */ + memset(wtext, 0, wtext_len * 2); + MultiByteToWideChar(CP_UTF8, 0, text, -1, wtext, wtext_len); + + for(i = 0; i < wtext_len; i++) codepoints[i] = wtext[i]; + + IDWriteFontFace_GetMetrics(ttf->font_face, &metrics); + + IDWriteFactory_CreateTextLayout(ttf->write_factory, wtext, wtext_len, ttf->text_format, 0, 0, &text_layout); + + IDWriteFontFace_GetGlyphIndices(ttf->font_face, codepoints, wtext_len, glyphIndices); + + gmetrics = malloc(wtext_len * sizeof(DWRITE_GLYPH_METRICS)); + + IDWriteFontFace_GetDesignGlyphMetrics(ttf->font_face, glyphIndices, wtext_len, gmetrics, FALSE); + + for(i = 0; i < strlen(text); i++) { + int add = (gmetrics[i].advanceWidth + gmetrics[i].leftSideBearing) * ((float)ttf->px / (float)metrics.designUnitsPerEm); + tw += add; + } + + free(wtext); + + return tw; +} + +static int dw_MwTextHeight(MwFLFont ttf, int count) { + (void)count; + return (IDWriteTextFormat2_GetFontSize(ttf->text_format) / 72.0f) * GetDeviceCaps(GetDC(NULL), LOGPIXELSY); } @@ -356,9 +430,13 @@ static HRESULT STDMETHODCALLTYPE CTR_DrawGlyphRun( IDWriteRenderingParams *params; CustomTextRenderer * This = (CustomTextRenderer*)iface; + (void)clientDrawingContext; + (void)glyphRunDescription; + (void)clientDrawingEffect; + IDWriteFactory6_CreateRenderingParams(This->write_factory, ¶ms); - return IDWriteBitmapRenderTarget_DrawGlyphRun(This->target, baselineOriginX, baselineOriginY, measuringMode, glyphRun, params, RGB(255,255,255), NULL); + return IDWriteBitmapRenderTarget_DrawGlyphRun(This->target, baselineOriginX, baselineOriginY, measuringMode, glyphRun, params, RGB(This->red,This->green,This->blue), NULL); } static HRESULT STDMETHODCALLTYPE CTR_DrawUnderline( @@ -420,7 +498,7 @@ static IDWriteTextRendererVtbl CTR_Vtbl = { /* ---- Constructor ---- */ -static CustomTextRenderer *CustomTextRenderer_Create(IDWriteFactory6 * write_factory, IDWriteBitmapRenderTarget * target) +static CustomTextRenderer *CustomTextRenderer_Create(IDWriteFactory6 * write_factory, IDWriteBitmapRenderTarget * target, MwLLColor color) { CustomTextRenderer *obj = (CustomTextRenderer *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, @@ -432,6 +510,10 @@ static CustomTextRenderer *CustomTextRenderer_Create(IDWriteFactory6 * write_fac obj->refCount = 1; obj->write_factory = write_factory; obj->target = target; + obj->red = color->common.red; + obj->green = color->common.green; + obj->blue = color->common.blue; + return obj; } diff --git a/src/text/text.c b/src/text/text.c index 8f573b5..e43de8c 100644 --- a/src/text/text.c +++ b/src/text/text.c @@ -3,6 +3,7 @@ int (*MwFLDrawText)(MwWidget handle, MwFLFont font, MwPoint* point, const char* text, MwLLColor color) = NULL; int (*MwFLTextWidth)(MwFLFont font, const char* text) = NULL; int (*MwFLTextHeight)(MwFLFont font, int count) = NULL; +int (*MwFLTextHeightWithText)(MwFLFont ttf, const char * text) = NULL; void* (*MwFLFontLoad)(unsigned char* data, unsigned int size, int px) = NULL; void (*MwFLFontFree)(void* handle) = NULL; @@ -220,6 +221,8 @@ int MwTextHeight(MwWidget handle, MwFLFont ttf, const char* text) { #ifdef TTF if(MwFLTextHeight) if(!MwGetInteger(handle, MwNbitmapFont) && ttf != NULL && (st = MwFLTextHeight(ttf, c)) != -1) return st; + if(MwFLTextHeightWithText) + if(!MwGetInteger(handle, MwNbitmapFont) && ttf != NULL && (st = MwFLTextHeightWithText(ttf, text)) != -1) return st; #endif return FontHeight * c; }