DirectWrite support #28

Merged
IoI_xD merged 13 commits from directwrite into master 2026-07-18 11:39:53 +09:00
18 changed files with 24754 additions and 66 deletions

View file

@ -9,6 +9,7 @@ include(GNUInstallDirs)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
option(MW_CLASSIC_THEME "Use classic theme" OFF)
option(MW_BUILD_EXAMPLES "Build examples" OFF)
@ -29,12 +30,30 @@ endif()
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)
else()
option(MW_USE_FREETYPE2 "Use FreeType 2" ON)
endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL Windows)
if(${CMAKE_CROSSCOMPILING})
message(STATUS "DirectWrite support has been enabled")
option(MW_USE_DIRECTWRITE "Use DirectWrite" ON)
else()
# 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()
endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL Linux OR ${CMAKE_SYSTEM_NAME} STREQUAL FreeBSD)
option(MW_USE_WAYLAND "Enable Wayland backend" ON)
endif()
@ -69,8 +88,8 @@ if(MW_TRY_OPENGL)
endif()
if(MW_TRY_VULKAN)
check_include_files(vulkan/vulkan.h HAVE_VULKAN_VULKAN_H)
if(HAVE_VULKAN_VULKAN_H)
find_package(Vulkan OPTIONAL VULKAN)
if(${Vulkan_FOUND})
set(MW_BUILD_VULKAN ON)
message(STATUS "Vulkan widget has been enabled")
else()
@ -132,10 +151,10 @@ if(MW_USE_FREETYPE2)
if(Freetype_FOUND)
message(STATUS "FreeType2 found")
target_compile_definitions(
Mw
PRIVATE
USE_FREETYPE2
)
Mw
PRIVATE
USE_FREETYPE2
)
list(APPEND INCLUDE_DIRS ${FREETYPE_INCLUDE_DIRS})
list(APPEND LIBRARY_DIRS ${FREETYPE_LIBRARY_DIRS})
@ -144,6 +163,14 @@ if(MW_USE_FREETYPE2)
endif()
endif()
if(MW_USE_DIRECTWRITE)
target_compile_definitions(
Mw
PRIVATE
USE_DIRECTWRITE
)
endif()
if(MW_USE_WAYLAND)
function(scan_wayland_protocol_core)
execute_process(
@ -296,7 +323,8 @@ if(MW_CLASSIC_THEME)
)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
message("Building Milsko for ${CMAKE_SYSTEM_NAME}")
if(CMAKE_SYSTEM_NAME STREQUAL "Windows" OR CMAKE_SYSTEM_NAME STREQUAL "MSYS")
target_sources(
Mw
PRIVATE
@ -336,6 +364,7 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
-framework Cocoa
)
elseif(CMAKE_SYSTEM_NAME MATCHES "Retro")
target_sources(
Mw
PRIVATE
@ -354,6 +383,7 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Retro")
CLASSIC_MAC_OS
)
else()
find_package(PkgConfig)
find_package(X11)
pkg_check_modules(DBUS dbus-1)
@ -386,23 +416,26 @@ else()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
list(APPEND LIBRARIES dl)
endif()
find_package(DBus1)
if(${DBus1_FOUND})
list(APPEND INCLUDE_DIRS ${DBUS_INCLUDE_DIRS})
list(APPEND LIBRARY_DIRS ${DBUS_LIBRARY_DIRS})
target_compile_definitions(
Mw
PRIVATE
USE_DBUS
)
endif()
endif()
find_package(DBus1)
if(${DBus1_FOUND})
list(APPEND INCLUDE_DIRS ${DBUS_INCLUDE_DIRS})
list(APPEND LIBRARY_DIRS ${DBUS_LIBRARY_DIRS})
target_compile_definitions(
Mw
PRIVATE
USE_DBUS
)
if(UNIX)
list(APPEND LIBRARIES m)
endif()
list(APPEND LIBRARIES m)
target_include_directories(
Mw
PRIVATE
@ -457,6 +490,17 @@ install(
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/resource/${PROJECT_NAME}.pc.in
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc
@ONLY
)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc
DESTINATION $ENV{MINGW_PREFIX}/${CMAKE_INSTALL_DATADIR}/pkgconfig
)
if(MW_INSTALL_HEADERS)
install(
DIRECTORY include/

2
WatMakefile generated
View file

@ -319,7 +319,7 @@ examples/gldemos/tripaint.obj: examples/gldemos/tripaint.c
src/Mw.dll: external/libz/src/adler32.obj external/libz/src/compress.obj external/libz/src/crc32.obj external/libz/src/deflate.obj external/libz/src/gzclose.obj external/libz/src/gzlib.obj external/libz/src/gzread.obj external/libz/src/gzwrite.obj external/libz/src/infback.obj external/libz/src/inffast.obj external/libz/src/inflate.obj external/libz/src/inftrees.obj external/libz/src/trees.obj external/libz/src/uncompr.obj external/libz/src/zutil.obj external/stb_ds.obj external/stb_image.obj external/stb_truetype.obj src/abstract/charset.obj src/abstract/directory.obj src/abstract/dynamic.obj src/abstract/time.obj src/backend/gdi.obj src/color.obj src/core.obj src/cursor/arrow.obj src/cursor/cross.obj src/cursor/default.obj src/cursor/hidden.obj src/cursor/text.obj src/default.obj src/dialog/colorpicker.obj src/dialog/directorychooser.obj src/dialog/filechooser.obj src/dialog/messagebox.obj src/draw.obj src/icon/back.obj src/icon/clock.obj src/icon/computer.obj src/icon/directory.obj src/icon/down.obj src/icon/error.obj src/icon/file.obj src/icon/forward.obj src/icon/info.obj src/icon/left.obj src/icon/news.obj src/icon/note.obj src/icon/right.obj src/icon/search.obj src/icon/up.obj src/icon/warning.obj src/lowlevel.obj src/string.obj src/text/font/boldfont.obj src/text/font/boldmonottf.obj src/text/font/boldttf.obj src/text/font/font.obj src/text/font/monottf.obj src/text/font/ttf.obj src/text/ft2.obj src/text/stbtt.obj src/text/text.obj src/unicode.obj src/widget/box.obj src/widget/button.obj src/widget/calendar.obj src/widget/checkbox.obj src/widget/combobox.obj src/widget/entry.obj src/widget/frame.obj src/widget/image.obj src/widget/label.obj src/widget/listbox.obj src/widget/menu.obj src/widget/numberentry.obj src/widget/opengl.obj src/widget/progressbar.obj src/widget/radiobox.obj src/widget/scrollbar.obj src/widget/separator.obj src/widget/submenu.obj src/widget/subwindow.obj src/widget/tab.obj src/widget/table.obj src/widget/treeview.obj src/widget/viewport.obj src/widget/window.obj
$(LD) $(MW_LDFLAGS) option implib=src/Mw.lib name $@ file external/libz/src/adler32.obj file external/libz/src/compress.obj file external/libz/src/crc32.obj file external/libz/src/deflate.obj file external/libz/src/gzclose.obj file external/libz/src/gzlib.obj file external/libz/src/gzread.obj file external/libz/src/gzwrite.obj file external/libz/src/infback.obj file external/libz/src/inffast.obj file external/libz/src/inflate.obj file external/libz/src/inftrees.obj file external/libz/src/trees.obj file external/libz/src/uncompr.obj file external/libz/src/zutil.obj file external/stb_ds.obj file external/stb_image.obj file external/stb_truetype.obj file src/abstract/charset.obj file src/abstract/directory.obj file src/abstract/dynamic.obj file src/abstract/time.obj file src/backend/gdi.obj file src/color.obj file src/core.obj file src/cursor/arrow.obj file src/cursor/cross.obj file src/cursor/default.obj file src/cursor/hidden.obj file src/cursor/text.obj file src/default.obj file src/dialog/colorpicker.obj file src/dialog/directorychooser.obj file src/dialog/filechooser.obj file src/dialog/messagebox.obj file src/draw.obj file src/icon/back.obj file src/icon/clock.obj file src/icon/computer.obj file src/icon/directory.obj file src/icon/down.obj file src/icon/error.obj file src/icon/file.obj file src/icon/forward.obj file src/icon/info.obj file src/icon/left.obj file src/icon/news.obj file src/icon/note.obj file src/icon/right.obj file src/icon/search.obj file src/icon/up.obj file src/icon/warning.obj file src/lowlevel.obj file src/string.obj file src/text/font/boldfont.obj file src/text/font/boldmonottf.obj file src/text/font/boldttf.obj file src/text/font/font.obj file src/text/font/monottf.obj file src/text/font/ttf.obj file src/text/ft2.obj file src/text/stbtt.obj file src/text/text.obj file src/unicode.obj file src/widget/box.obj file src/widget/button.obj file src/widget/calendar.obj file src/widget/checkbox.obj file src/widget/combobox.obj file src/widget/entry.obj file src/widget/frame.obj file src/widget/image.obj file src/widget/label.obj file src/widget/listbox.obj file src/widget/menu.obj file src/widget/numberentry.obj file src/widget/opengl.obj file src/widget/progressbar.obj file src/widget/radiobox.obj file src/widget/scrollbar.obj file src/widget/separator.obj file src/widget/submenu.obj file src/widget/subwindow.obj file src/widget/tab.obj file src/widget/table.obj file src/widget/treeview.obj file src/widget/viewport.obj file src/widget/window.obj library clib3r.lib library opengl32.lib library gdi32.lib library user32.lib library advapi32.lib
external/libz/src/adler32.obj: external/libz/src/adler32.c

33
configure vendored
View file

@ -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,34 @@ 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") {
if ($^O eq "Win32")
{
require Win32;
Win32->import();
my ( $osvername, $major, $minor, $id ) = Win32::GetOSVersion();
if($major >= 10) {
param_set("directwrite", 1);
} else {
param_set("directwrite", 0);
}
} else {
# assume cross compiling
param_set("directwrite", 1);
}
} else {
param_set("directwrite", 0);
}
}
foreach my $l (@ARGV) {

View file

@ -1,28 +1,30 @@
file(
GLOB
EXAMPLES_VULKAN_SOURCES
*.c
GLOB
EXAMPLES_VULKAN_SOURCES
*.c
)
find_package(Vulkan)
foreach(PATH IN LISTS EXAMPLES_VULKAN_SOURCES)
get_filename_component(TARGET ${PATH} NAME_WE)
add_executable(${TARGET} MACOSX_BUNDLE ${PATH})
target_include_directories(
${TARGET}
PRIVATE
${Vulkan_INCLUDE_DIR}
)
target_link_libraries(
${TARGET}
PRIVATE
Mw
)
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Windows")
target_link_libraries(
${TARGET}
PRIVATE
m
)
endif()
endforeach()
if(${Vulkan_FOUND})
foreach(PATH IN LISTS EXAMPLES_VULKAN_SOURCES)
get_filename_component(TARGET ${PATH} NAME_WE)
add_executable(${TARGET} MACOSX_BUNDLE ${PATH})
target_include_directories(
${TARGET}
PRIVATE
${Vulkan_INCLUDE_DIR}
)
target_link_libraries(
${TARGET}
PRIVATE
Mw
)
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Windows")
target_link_libraries(
${TARGET}
PRIVATE
m
)
endif()
endforeach()
endif()

View file

@ -1,10 +1,3 @@
/**
* this example is quite minimal, you may need to modify it if your graphics card is more esoteric
*
* ioixd maintains this file. nishi doesn't know vulkan at all
*/
#include <Mw/Milsko.h>
#include <Mw/Widget/Vulkan.h>
@ -264,8 +257,8 @@ void vulkan_setup(MwWidget handle) {
swapchainCreateInfo.imageExtent = (VkExtent2D){.width = ow, .height = oh},
swapchainCreateInfo.imageArrayLayers = 1,
swapchainCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_SAMPLED_BIT;
VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_SAMPLED_BIT;
// th is how we specify no transformation.
swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchainCreateInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;

View file

@ -344,10 +344,14 @@ MWDECL int MWFL_FT2Setup(void);
#ifdef USE_STB_TRUETYPE
MWDECL int MwFL_STBTTSetup(void);
#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 (*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);

View file

@ -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");
}

10
resource/milsko.pc.in Normal file
View file

@ -0,0 +1,10 @@
prefix="@CMAKE_INSTALL_PREFIX@"
includedir="@CMAKE_INSTALL_PREFIX@/include"
libdir="@CMAKE_INSTALL_PREFIX@/bin"
Name: @PROJECT_NAME@
Description: @PROJECT_DESCRIPTION@
URL: @PROJECT_HOMEPAGE_URL@
Version: @PROJECT_VERSION@
Cflags: -I"${includedir}"/
Libs: -L"${libdir}" -lMw

View file

@ -310,12 +310,19 @@ 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)) {
font_setup:
h->root_font = MwFontLoad(MwTTFData, MwTTFDataSize);
h->root_boldfont = MwFontLoad(MwBoldTTFData, MwBoldTTFDataSize);
h->root_monofont = MwFontLoad(MwMonospaceTTFData, MwMonospaceTTFDataSize);
h->root_boldmonofont = MwFontLoad(MwBoldMonospaceTTFData, MwBoldMonospaceTTFDataSize);
if(!h->root_font) {
#ifdef USE_STB_TRUETYPE
MwFL_STBTTSetup();
goto font_setup;
#endif
}
} else
#endif
{
@ -732,7 +739,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 +790,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 +806,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);

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

@ -0,0 +1,588 @@
/*
* DirectWrite Font backend
*
* This should dynamically load everything necessary and then gracefully fallback if the user isn't on Windows 10.
*/
#ifdef USE_DIRECTWRITE
#define INITGUID
#include <Mw/Milsko.h>
#include <d2d1.h>
#include <shobjidl.h>
#ifndef WINBOOL
#define WINBOOL int
#endif
/* dwrite is vendored because it's only mingw that gives us COBJMACRO */
#include "dwrite.h"
#include "dwrite_3.h"
typedef struct CustomTextRenderer {
IDWriteTextRendererVtbl* lpVtbl;
LONG refCount;
IDWriteFactory6* write_factory;
IDWriteBitmapRenderTarget* target;
int red, blue, green;
} CustomTextRenderer;
static CustomTextRenderer* CustomTextRenderer_Create(IDWriteFactory6* write_factory, IDWriteBitmapRenderTarget* target, MwLLColor color);
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);
HMODULE kernel32_lib;
} dw_table;
struct _MwFLFont {
int px;
void* data;
MwBool valid;
IDWriteFactory6* write_factory;
ID2D1Factory* d2dFactory;
IDWriteGdiInterop* gdiInterop;
IDWriteFontFile* file;
IDWriteFontFace* font_face;
IDWriteFontSetBuilder2* font_builder;
IDWriteFontSet* font_set;
IDWriteFontCollection1* font_collection;
IDWriteTextFormat* text_format;
LOGFONTW 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 = GetTempPathW(MAX_PATH, temp_path_dir);
if(hr == 0) {
printf("GetTempPathW 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);
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;
#define INTERFACE_CHECK(x, factory) \
do { \
x* rsrc = NULL; \
if(!SUCCEEDED((hr = x##_QueryInterface(factory, &IID_##x, (void**)&rsrc)))) { \
printf("Query for interface " #x " failed (%08lX). Cannot use DirectWrite.\n", hr); \
return NULL; \
}; \
} while(0);
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;
}
INTERFACE_CHECK(ID2D1Factory, ttf->d2dFactory);
hr = dw_table.DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, &IID_IDWriteFactory, (struct IUnknown**)&ttf->write_factory);
if(!SUCCEEDED(hr)) {
printf("DWriteCreateFactory failed for TTF (%08lx), cannot use DirectWrite\n", hr);
goto fail;
}
INTERFACE_CHECK(IDWriteFactory6, ttf->write_factory);
if(unpack_ttf_data(font_name, data, size) != 0) {
goto fail;
};
hr = IDWriteFactory6_CreateFontFileReference(ttf->write_factory, font_name, NULL, &ttf->file);
if(!SUCCEEDED(hr)) {
printf("CreateFontFileReference failed for TTF (%08lx), cannot use DirectWrite\n", hr);
goto fail;
}
INTERFACE_CHECK(IDWriteFontFile, ttf->file);
hr = IDWriteFactory_CreateFontFace(ttf->write_factory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &ttf->file, 0, DWRITE_FONT_SIMULATIONS_NONE, &ttf->font_face);
if(!SUCCEEDED(hr)) {
printf("IDWriteFactory_CreateFontFace failed for TTF (%08lx), cannot use DirectWrite\n", hr);
goto fail;
}
INTERFACE_CHECK(IDWriteFontFace, ttf->font_face);
hr = IDWriteFactory6_CreateFontSetBuilder(ttf->write_factory, &ttf->font_builder);
if(!SUCCEEDED(hr)) {
printf("IDWriteFactory6_CreateFontSetBuilder failed for TTF (%08lx), cannot use DirectWrite\n", hr);
goto fail;
}
INTERFACE_CHECK(IDWriteFontSetBuilder2, ttf->font_builder);
hr = IDWriteFontSetBuilder2_AddFontFile(ttf->font_builder, font_name);
if(!SUCCEEDED(hr)) {
printf("IDWriteFontSetBuilder2_AddFontFile failed for TTF (%08lx), cannot use DirectWrite\n", hr);
goto fail;
}
hr = IDWriteFontSetBuilder_CreateFontSet(ttf->font_builder, &ttf->font_set);
if(!SUCCEEDED(hr)) {
printf("IDWriteFontSetBuilder_CreateFontSet failed for TTF (%08lx), cannot use DirectWrite\n", hr);
goto fail;
}
INTERFACE_CHECK(IDWriteFontSet, ttf->font_set);
hr = IDWriteFactory3_CreateFontCollectionFromFontSet(ttf->write_factory, ttf->font_set, &ttf->font_collection);
if(!SUCCEEDED(hr)) {
printf("IDWriteFactory3_CreateFontCollectionFromFontSet failed for TTF (%08lx), cannot use DirectWrite\n", hr);
goto fail;
}
INTERFACE_CHECK(IDWriteFontCollection1, ttf->font_collection);
hr = IDWriteFactory6_GetGdiInterop(ttf->write_factory, &ttf->gdiInterop);
if(!SUCCEEDED(hr)) {
printf("GetGdiInterop failed for TTF (%08lx), cannot use DirectWrite\n", hr);
goto fail;
}
INTERFACE_CHECK(IDWriteGdiInterop, ttf->gdiInterop);
hr = IDWriteFactory_CreateTextFormat(ttf->write_factory, L"milsko font", (IDWriteFontCollection*)ttf->font_collection, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, px, L"en-US", &ttf->text_format);
if(!SUCCEEDED(hr)) {
printf("IDWriteFactory_CreateTextFormat failed for TTF (%08lx), cannot use DirectWrite\n", hr);
goto fail;
}
ttf->valid = MwTRUE;
return ttf;
fail:
ttf->valid = MwFALSE;
return NULL;
}
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;
IDWriteBitmapRenderTarget* target = NULL;
HRESULT hr;
RECT rc = {0};
HDC memoryHDC = NULL;
DWRITE_FONT_METRICS metrics = {0};
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);
GetClientRect(handle->lowlevel->gdi.hWnd, &rc);
MapWindowPoints(handle->lowlevel->gdi.hWnd, GetParent(handle->lowlevel->gdi.hWnd), (LPPOINT)&rc, 2);
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, color);
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_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;
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].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);
}
static void dw_MwFontFree(void* handle) {
MwFLFont ttf = handle;
free(ttf->data);
free(ttf);
}
/* ---- IUnknown ---- */
static HRESULT STDMETHODCALLTYPE CTR_QueryInterface(
IDWriteTextRenderer* iface, REFIID riid, void** ppvObject) {
if(!ppvObject)
return E_POINTER;
if(IsEqualGUID(riid, &IID_IUnknown) ||
IsEqualGUID(riid, &IID_IDWritePixelSnapping) ||
IsEqualGUID(riid, &IID_IDWriteTextRenderer)) {
*ppvObject = iface;
iface->lpVtbl->AddRef(iface);
return S_OK;
}
*ppvObject = NULL;
return E_NOINTERFACE;
}
static ULONG STDMETHODCALLTYPE CTR_AddRef(IDWriteTextRenderer* iface) {
CustomTextRenderer* This = (CustomTextRenderer*)iface;
return InterlockedIncrement(&This->refCount);
}
static ULONG STDMETHODCALLTYPE CTR_Release(IDWriteTextRenderer* iface) {
CustomTextRenderer* This = (CustomTextRenderer*)iface;
LONG ref = InterlockedDecrement(&This->refCount);
if(ref == 0)
HeapFree(GetProcessHeap(), 0, This);
return (ULONG)ref;
}
/* ---- IDWritePixelSnapping (base interface, treated as "core") ---- */
static HRESULT STDMETHODCALLTYPE CTR_IsPixelSnappingDisabled(
IDWriteTextRenderer* iface, void* clientDrawingContext, BOOL* isDisabled) {
(void)iface;
(void)clientDrawingContext;
if(!isDisabled)
return E_POINTER;
*isDisabled = FALSE;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE CTR_GetCurrentTransform(
IDWriteTextRenderer* iface, void* clientDrawingContext, DWRITE_MATRIX* transform) {
(void)iface;
(void)clientDrawingContext;
if(!transform)
return E_POINTER;
transform->m11 = 1.0f;
transform->m12 = 0.0f;
transform->m21 = 0.0f;
transform->m22 = 1.0f;
transform->dx = 0.0f;
transform->dy = 0.0f;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE CTR_GetPixelsPerDip(
IDWriteTextRenderer* iface, void* clientDrawingContext, FLOAT* pixelsPerDip) {
(void)iface;
(void)clientDrawingContext;
if(!pixelsPerDip)
return E_POINTER;
*pixelsPerDip = 1.0f;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE CTR_DrawGlyphRun(
IDWriteTextRenderer* iface,
void* clientDrawingContext,
FLOAT baselineOriginX,
FLOAT baselineOriginY,
DWRITE_MEASURING_MODE measuringMode,
DWRITE_GLYPH_RUN const* glyphRun,
DWRITE_GLYPH_RUN_DESCRIPTION const* glyphRunDescription,
IUnknown* clientDrawingEffect) {
IDWriteRenderingParams* params;
CustomTextRenderer* This = (CustomTextRenderer*)iface;
(void)clientDrawingContext;
(void)glyphRunDescription;
(void)clientDrawingEffect;
IDWriteFactory6_CreateRenderingParams(This->write_factory, &params);
return IDWriteBitmapRenderTarget_DrawGlyphRun(This->target, baselineOriginX, baselineOriginY, measuringMode, glyphRun, params, RGB(This->red, This->green, This->blue), NULL);
}
static HRESULT STDMETHODCALLTYPE CTR_DrawUnderline(
IDWriteTextRenderer* iface,
void* clientDrawingContext,
FLOAT baselineOriginX,
FLOAT baselineOriginY,
DWRITE_UNDERLINE const* underline,
IUnknown* clientDrawingEffect) {
(void)iface;
(void)clientDrawingContext;
(void)baselineOriginX;
(void)baselineOriginY;
(void)underline;
(void)clientDrawingEffect;
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE CTR_DrawStrikethrough(
IDWriteTextRenderer* iface,
void* clientDrawingContext,
FLOAT baselineOriginX,
FLOAT baselineOriginY,
DWRITE_STRIKETHROUGH const* strikethrough,
IUnknown* clientDrawingEffect) {
(void)iface;
(void)clientDrawingContext;
(void)baselineOriginX;
(void)baselineOriginY;
(void)strikethrough;
(void)clientDrawingEffect;
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE CTR_DrawInlineObject(
IDWriteTextRenderer* iface,
void* clientDrawingContext,
FLOAT originX,
FLOAT originY,
IDWriteInlineObject* inlineObject,
BOOL isSideways,
BOOL isRightToLeft,
IUnknown* clientDrawingEffect) {
(void)iface;
(void)clientDrawingContext;
(void)originX;
(void)originY;
(void)inlineObject;
(void)isSideways;
(void)isRightToLeft;
(void)clientDrawingEffect;
return E_NOTIMPL;
}
/* ---- Vtable instance ---- */
static IDWriteTextRendererVtbl CTR_Vtbl = {
CTR_QueryInterface,
CTR_AddRef,
CTR_Release,
CTR_IsPixelSnappingDisabled,
CTR_GetCurrentTransform,
CTR_GetPixelsPerDip,
CTR_DrawGlyphRun,
CTR_DrawUnderline,
CTR_DrawStrikethrough,
CTR_DrawInlineObject};
/* ---- Constructor ---- */
static CustomTextRenderer* CustomTextRenderer_Create(IDWriteFactory6* write_factory, IDWriteBitmapRenderTarget* target, MwLLColor color) {
CustomTextRenderer* obj =
(CustomTextRenderer*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
sizeof(CustomTextRenderer));
if(!obj)
return NULL;
obj->lpVtbl = &CTR_Vtbl;
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;
}
int MWFL_DWSetup(void) {
HRESULT hr;
HANDLE ole32lib;
/* CoInitialize, to my knowledge, is avaliable on every Windows version we actually support. But as of writing, CI fails because a specific mingw won't link to it by default. I'm not gonna waste my time chancing that it will properly link to ole32 either. */
HRESULT (*CoInitialize)(LPVOID pvReserved);
ole32lib = LoadLibrary("Ole32.dll");
if(!ole32lib) {
printf("ole32lib not found, cannot use DirectWrite\n");
return 1;
}
CoInitialize = (HRESULT (*)(LPVOID pvReserved))GetProcAddress(ole32lib, "CoInitialize");
if(!CoInitialize) {
printf("CoInitialize not found, cannot use DirectWrite\n");
return 1;
}
CoInitialize(NULL);
/* 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

5517
src/text/dwrite.h Normal file

File diff suppressed because it is too large Load diff

3297
src/text/dwrite_2.h Normal file

File diff suppressed because it is too large Load diff

15191
src/text/dwrite_3.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

@ -3,10 +3,11 @@
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;
#if defined(USE_FREETYPE2) || defined(USE_STB_TRUETYPE)
#if defined(USE_FREETYPE2) || defined(USE_STB_TRUETYPE) || defined(USE_DIRECTWRITE)
#define TTF
#endif
@ -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;
}
@ -297,6 +300,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